// Global variable to store polling interval if (typeof window.wpRocketIntervalId === 'undefined') { window.wpRocketIntervalId = null; } if (typeof window.wpRocketAddonIntervalId === 'undefined') { window.wpRocketAddonIntervalId = null; } //added the below line to avoid multiple provision ajax request on reload window.wpenqueued = true; jQuery(document).ready(function () { //check plugin activation in progress on reload: //different ajax action onclick and onload //only enqueue on the wp-rocket page jQuery.post(ajaxurl, { action: 'on_reload_plugin_activate', addon_slug: 'wp-rocket' }, function(response){ handle_plugin_activation_response(response, window.wpRocketIntervalId); // If queue in progress, start polling if (response.status === 'already_in_queue') { console.log("Polling on reload"); oc_plugin_status_polling(); } }); //check addon purchase status in progress on reload: //different ajax action onclick and onload //only enqueue on the wp-rocket page jQuery.post(ajaxurl, { action: 'check_addon_purchase_status_onload', addon_slug: 'wp-rocket' }, function(response){ handle_addon_purchase_response(response, window.wpRocketAddonIntervalId); // If queue in progress, start polling if (response.status === 'already_in_queue') { console.log("Polling on reload"); oc_addon_status_polling(); } }); //hide notice on close jQuery('.wpr-container .gv-notice-close').click(function(){ jQuery(this).parent().addClass('gv-hidden'); }); //on the button click check plugin activation jQuery('.oc-activate-wp-rocket-btn').click(function(){ oc_activate_wp_plugin(); }); //on the button click check addon status jQuery('.gv-button.get-wpr-btn').click(function(){ oc_addon_purchase_status(); }); // Capture WP-Rocket stats from WP-Rocket page & plugins entry jQuery("#onecom-wrap .oc-wp-rocket-cp-link").click(function (){ let args = { 'event_action': 'click_cp_activate_button', 'item_category': 'plugin', 'item_name': 'wp_rocket' }; oc_push_stats_by_js(args); }) jQuery("#onecom-ui .oc-wp-rocket-cp-link").click(function (){ let args = { 'event_action': 'click_cp_activate_button', 'item_category': 'plugin', 'item_name': 'wp_rocket' }; oc_push_stats_by_js(args); }) jQuery("#onecom-ui .wp-rocket-guide-link").click(function (){ let args = { 'event_action': 'click_learn_more_button', 'item_category': 'plugin', 'item_name': 'wp_rocket' }; oc_push_stats_by_js(args); }) jQuery("#onecom-wrap .wp-rocket-offer-link").click(function (){ let args = { 'event_action': 'click_wp_rocket_offer_link', 'item_category': 'plugin', 'item_name': 'wp_rocket' }; oc_push_stats_by_js(args); }) }); // Common function to poll plugin activation status function oc_plugin_status_polling(addon_slug = 'wp-rocket') { // If already polling, do nothing if (window.wpRocketIntervalId) return; // Define the polling function function pollStatus() { jQuery.post(ajaxurl, { action: 'activate_onclick_wp_plugin', addon_slug: addon_slug }, function(response){ handle_plugin_activation_response(response, window.wpRocketIntervalId); }); } // Call immediately at once pollStatus(); //Then schedule it to repeat every 20 seconds window.wpRocketIntervalId = setInterval(pollStatus, 5000); } // Common function to poll addon activation status //call on a twenty-seconds interval function oc_addon_status_polling(addon_slug = 'wp-rocket') { // If already polling, do nothing if (window.wpRocketAddonIntervalId) return; // Define the polling function function pollAddonStatus() { jQuery.post(ajaxurl, { action: 'check_addon_purchase_status', addon_slug: addon_slug }, function(response){ handle_addon_purchase_response(response, window.wpRocketAddonIntervalId); }); } // Call immediately at once pollAddonStatus(); //Then schedule it to repeat every 20 seconds window.wpRocketAddonIntervalId = setInterval(pollAddonStatus, 25000); } function stop_polling(intervalId, type) { if (intervalId) clearInterval(intervalId); if (type === 'plugin') { window.wpRocketIntervalId = null; } else if (type === 'addon') { window.wpRocketAddonIntervalId = null; } } // Common function to handle AJAX response function handle_plugin_activation_response(response, intervalId = null) { if (response.status === 'normal_reload') { //do nothing on normal reload return false; } if (response.status === 'added_to_queue') { // Push stats let args = { 'event_action': 'click_wp_activate_button', 'item_category': 'plugin', 'item_name': 'wp_rocket' }; oc_push_stats_by_js(args); jQuery('.gv-notice.gv-notice-warning.wpr-notice').removeClass('gv-hidden'); } else if (response.status === 'already_in_queue') { // Queue in progress → keep polling jQuery('.gv-notice.wpr-notice').addClass('gv-hidden'); jQuery('.gv-notice.gv-notice-warning.wpr-notice').removeClass('gv-hidden'); } else if (response.status === 'activated') { stop_polling(intervalId, 'plugin'); let btn = ''+response.btn_text+''; let insertAfter = '