2026-02-05 17:08:59 +03:00

312 lines
10 KiB
PHP

<?php
// Required information for functions
const PWPC_ADDON_API = MIDDLEWARE_URL . '/features/addon/PREMIUM_WORDPRESS_CARE/status';
const PWPC_ADDON_CLUSTER_API = MIDDLEWARE_URL . '/features/addon/PREMIUM_WORDPRESS_CARE/status/cluster';
const WR_PWPC_API = MIDDLEWARE_URL . '/premium-wp-care';
add_action(
'admin_enqueue_scripts',
function ($hook) {
if ( function_exists( 'get_current_screen' ) && get_current_screen()->id === 'one-com_page_onecom-home' ) {
wp_deregister_style( 'wp-block-editor' );
}
if ( function_exists( 'get_current_screen' ) && get_current_screen()->id === 'one-com_page_onecom-home' ) {
wp_enqueue_script( 'oc_home_page' , ONECOM_WP_URL . 'modules/home/js/index.umd.js' , array( 'jquery' ) , ONECOM_WP_VERSION , true );
}
//Added allow to load resources on specific pages + one.com pages only
$allow = array(
'one-com_page_onecom-wp-health-monitor',
'toplevel_page_onecom-wp-spam-protection',
'toplevel_page_onecom-vcache-plugin',
'one-com_page_onecom-cdn',
'toplevel_page_onecom-wp-under-construction',
'one-com_page_onecom-wp-rocket',
'one-com_page_onecom-marketplace',
'one-com_page_onecom-marketplace-products',
);
global $load_onecom_wp_resources_slugs;
if(in_array($hook, $load_onecom_wp_resources_slugs) || in_array($hook, $allow)){
if ( SCRIPT_DEBUG || SCRIPT_DEBUG == 'true' ) {
wp_enqueue_script( 'oc_home_page_main', ONECOM_WP_URL . 'modules/home/js/main.js', array( 'jquery' ), ONECOM_WP_VERSION, true );
wp_enqueue_style( 'oc_home_page-css', ONECOM_WP_URL . 'modules/home/css/main.css', '', ONECOM_WP_VERSION, 'all' );
} else {
wp_enqueue_script( 'oc_home_page_main', ONECOM_WP_URL . 'assets/min-js/main.min.js', array( 'jquery' ), ONECOM_WP_VERSION, true );
wp_enqueue_style( 'oc_home_page-css', ONECOM_WP_URL . 'assets/min-css/main.min.css', '', ONECOM_WP_VERSION, 'all' );
}
wp_localize_script(
'oc_home_page_main',
'oc_home_ajax_obj',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'oc_home_ajax' ),
'home_url' => admin_url( 'admin.php?page=onecom-home' ),
)
);
}
}
);
function wporg_options_page_html() {
// check user capabilities
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
require_once ONECOM_WP_PATH . 'modules/home/templates/home.php';
}
function wporg_options_page() {
add_submenu_page( 'onecom-wp', __( 'Home', 'onecom-wp' ), '<span id="onecom_home">Home</span>', 'manage_options', 'onecom-home', 'wporg_options_page_html', -1 );
}
add_action( 'admin_menu', 'wporg_options_page' );
add_action(
'wp_ajax_oc_home_silence_tour',
function () {
update_site_option( 'oc_home_silence_tour', true );
wp_send_json( array( 'status' => 'success' ) );
}
);
add_action(
'wp_ajax_oc_home_premium_care_dismiss',
function () {
$status = update_site_option( 'oc_home_premium_care_dismiss', true );
if ( $status ) {
wp_send_json( array( 'status' => 'success' ) );
} else {
wp_send_json( array( 'status' => 'error' ) );
}
}
);
add_action( 'init', 'show_welcome_modal' );
// function to show the modal based on the user meta values
function show_welcome_modal(): void {
$welcome_modal_closed = false;
$user_id = get_current_user_id();
if ( $user_id ) {
// Retrieve the user meta
$welcome_modal_closed = get_user_meta( $user_id, 'oc-welcome-modal-closed', true );
}
if ( $welcome_modal_closed !== true && $welcome_modal_closed !== '1' ) {
add_action( 'admin_footer', 'welcome_popup_init' );
}
}
/**
* @return void
* function to include the template of welcome modal
*/
function welcome_popup_init() {
require_once ONECOM_WP_PATH . 'modules/home/templates/welcome-modal.php';
}
/**
* Check if current admin screen is for one.com plugin pages
*/
function oc_is_onecom_plugins_page() {
$screen = get_current_screen();
if ( ! $screen ) {
return false;
}
$allowed_screens = array(
'one-com_page_onecom-home',
'one-com_page_onecom-wp-health-monitor',
'toplevel_page_onecom-vcache-plugin',
'one-com_page_onecom-cdn',
'one-com_page_onecom-wp-rocket',
'one-com_page_onecom-wp-themes',
'admin_page_onecom-wp-recommended-plugins',
'admin_page_onecom-wp-discouraged-plugins',
'one-com_page_onecom-wp-staging',
'one-com_page_onecom-wp-staging-blocked',
'one-com_page_onecom-wp-error-page',
'one-com_page_onecom-wp-cookie-banner',
'toplevel_page_onecom-wp-under-construction',
'toplevel_page_onecom-wp-spam-protection',
'one-com_page_onecom-marketplace',
'one-com_page_onecom-marketplace-products'
);
return in_array( $screen->id, $allowed_screens, true );
}
// Include data consent and premium care modals
function oc_data_consent_modal_init() {
// Include data consent modal on all onecom plugins screen
if ( oc_is_onecom_plugins_page() ) {
require_once ONECOM_WP_PATH . 'modules/home/templates/data-consent-modal.php';
}
// Include premium wp care modal template on onecom home screen
if ( function_exists( 'get_current_screen' ) && get_current_screen()->id === 'one-com_page_onecom-home' ) {
require_once ONECOM_WP_PATH . 'modules/home/templates/premium-wp-care-modal.php';
}
}
add_action( 'admin_footer', 'oc_data_consent_modal_init' );
/**
* @return void
* Include data consent banner template if conditions met i.e
* * New onboarding + Consent status is never updated + onecom plugin pages + Admin capabilities
*/
function oc_data_consent_banner() {
// Exit if not a vaid plugin page
$screen = get_current_screen();
if ( ! oc_is_onecom_plugins_page() || ! current_user_can( 'manage_options' ) ) {
return;
}
// Exit if consent banner status is found
$data_consent_status = get_site_option( 'onecom_data_consent_status', false );
if ( '1' === $data_consent_status || '0' === $data_consent_status ) {
return;
}
// If timestamp is missing OR 24 hours haven't passed, skip
$timestamp = get_option( 'onecom_installation_timestamp' );
if ( ! $timestamp || ( time() - (int) $timestamp ) < 86400 ) {
return;
}
require_once ONECOM_WP_PATH . 'modules/home/templates/data-consent-banner.php';
}
add_action( 'admin_footer', 'oc_data_consent_banner' );
// Update consent banner status
add_action( 'wp_ajax_oc_update_consent_status', 'oc_update_consent_status' );
function oc_update_consent_status() {
if ( ! empty( $_POST ) && isset( $_POST['consent_status'] ) ) {
$status = isset( $_POST['consent_status'] ) ? intval( $_POST['consent_status'] ) : 0;
update_site_option( 'onecom_data_consent_status', $status );
wp_send_json_success( array( 'message' => 'Status updated', 'consent_status' => $status ) );
} else {
wp_send_json_error( array( 'message' => 'Failed to update status' ) );
}
}
// Request Premium care via API
add_action( 'wp_ajax_oc_request_premium_care', 'oc_request_premium_care' );
function oc_request_premium_care() {
if ( ! isset( $_POST['premium_wp_request'] ) ) {
wp_send_json_error( array( 'message' => 'Missing required data' ) );
}
// Get logged-in user info
$current_user = wp_get_current_user();
$user_name = $current_user->display_name;
$user_email = $current_user->user_email;
// Prepare data
$text = isset( $_POST['text'] ) ? sanitize_text_field( $_POST['text'] ) : '';
$final_description = "Description: " .$text . "\n\nWordPress user name: " . $user_name . "\nWordPress user email: " . $user_email . "\nWordPress installation: " . OC_HTTP_HOST;
$payload = array(
'text' => $final_description,
'subject' => 'I want to learn more about Premium WP Care',
);
// Send API request (replace URL with actual endpoint)
$response = wp_remote_post(
WR_PWPC_API,
array(
'headers' => array( 'Content-Type' => 'application/json' ),
'body' => wp_json_encode( $payload ),
'method' => 'POST',
'timeout' => 60,
)
);
if ( is_wp_error( $response ) ) {
wp_send_json_error(
array(
'message' => 'API error',
'error' => $response->get_error_message(),
)
);
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! empty( $body['success'] ) ) {
set_transient( 'onecom_premium_wp_care_request', '1', 24 * HOUR_IN_SECONDS );
wp_send_json_success(
array(
'message' => 'Request submitted successfully',
'api_response' => $body,
)
);
} else {
wp_send_json_error(
array(
'message' => 'API call failed',
'api_response' => $body,
)
);
}
}
// Fetch premium wp care addon info via feature endpoint or transient
function oc_premium_wp_care_addon_info( $force = false, $domain = '' ) {
$premium_wp_care_addon_info = get_site_transient( 'onecom_pwpc_addon_info' );
if ( ! empty( $premium_wp_care_addon_info ) && false === $force ) {
return $premium_wp_care_addon_info;
}
if ( ! $domain ) {
$domain = isset( $_SERVER['ONECOM_DOMAIN_NAME'] ) ? $_SERVER['ONECOM_DOMAIN_NAME'] : false;
}
if ( ! $domain ) {
return array(
'data' => null,
'error' => 'Empty domain',
'success' => false,
);
}
$api_url = is_cluster_domain() ? PWPC_ADDON_CLUSTER_API : PWPC_ADDON_API;
$result = wp_remote_get( $api_url, array( 'timeout' => 10 ) );
// Handle errors first
if ( is_wp_error( $result ) ) {
wp_send_json_error(
array(
'code' => 500,
'message' => $result->get_error_message(),
),
500
);
}
$response_code = wp_remote_retrieve_response_code( $result );
$response_body = wp_remote_retrieve_body( $result );
$response_json = json_decode( $response_body, true );
$response = $response_json ?? $response_body;
// save transient for next calls, & return latest response
set_site_transient( 'onecom_pwpc_addon_info', $response, 12 * HOUR_IN_SECONDS );
return $response;
}
/**
* Check if premium_wp_care plugin addon purchased
*/
function oc_is_premium_wp_care_addon_purchased(): bool {
$premium_wp_care_addon_info = oc_premium_wp_care_addon_info();
return (
is_array( $premium_wp_care_addon_info ) &&
array_key_exists( 'success', $premium_wp_care_addon_info ) &&
$premium_wp_care_addon_info['success'] &&
array_key_exists( 'data', $premium_wp_care_addon_info ) &&
array_key_exists( 'product', $premium_wp_care_addon_info['data'] ) &&
'PREMIUM_WORDPRESS_CARE' === $premium_wp_care_addon_info['data']['product'] &&
array_key_exists( 'source', $premium_wp_care_addon_info['data'] ) &&
'PURCHASED' === $premium_wp_care_addon_info['data']['source']
);
}