nitropack
Last commit date
classes
2 years ago
languages
2 years ago
nitropack-sdk
2 years ago
view
2 years ago
advanced-cache.php
3 years ago
batcache-compat.php
4 years ago
cf-helper.php
5 years ago
constants.php
2 years ago
diagnostics.php
3 years ago
functions.php
2 years ago
helpers.php
3 years ago
integrations.php
4 years ago
main.php
2 years ago
readme.txt
2 years ago
uninstall.php
2 years ago
wp-cli.php
3 years ago
main.php
251 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: NitroPack |
| 4 | Plugin URI: https://nitropack.io/platform/wordpress |
| 5 | Description: Automatic optimization for site speed and Core Web Vitals. Use 35+ features, including Caching, image optimization, critical CSS, and Cloudflare CDN. |
| 6 | Version: 1.8.1 |
| 7 | Author: NitroPack Inc. |
| 8 | Author URI: https://nitropack.io/ |
| 9 | License: GPL2 |
| 10 | License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 11 | Text Domain: nitropack |
| 12 | Domain Path: /languages |
| 13 | */ |
| 14 | |
| 15 | defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
| 16 | |
| 17 | $np_basePath = dirname(__FILE__) . '/'; |
| 18 | require_once $np_basePath . 'functions.php'; |
| 19 | require_once $np_basePath . 'helpers.php'; |
| 20 | require_once $np_basePath . 'diagnostics.php'; |
| 21 | |
| 22 | if (nitropack_is_wp_cli()) { |
| 23 | require_once $np_basePath . 'wp-cli.php'; |
| 24 | } |
| 25 | |
| 26 | if ( \NitroPack\Integration\Plugin\Ezoic::isActive() ) { |
| 27 | if (!nitropack_is_optimizer_request()) { |
| 28 | // We need to serve the cached content after Ezoic's output buffering has started at plugins_loaded,0 |
| 29 | add_action( 'plugins_loaded', function() { |
| 30 | add_filter( 'home_url', ['\NitroPack\Integration\Plugin\Ezoic', 'getHomeUrl'] ); |
| 31 | nitropack_handle_request("plugin-ezoic"); |
| 32 | remove_filter( 'home_url', ['\NitroPack\Integration\Plugin\Ezoic', 'getHomeUrl'] ); |
| 33 | }, 1 ); |
| 34 | } else { |
| 35 | add_action( 'plugins_loaded', ['\NitroPack\Integration\Plugin\Ezoic', 'disable'], 1); |
| 36 | } |
| 37 | } else { |
| 38 | nitropack_handle_request("plugin"); |
| 39 | } |
| 40 | |
| 41 | add_filter( 'nitro_script_output', function($script) { |
| 42 | $canPrintScripts = !nitropack_is_amp_page() // Make sure we don't accidentally print a non-amp compatible script to an amp page |
| 43 | && (!isset($_SERVER['HTTP_SEC_FETCH_DEST']) || $_SERVER['HTTP_SEC_FETCH_DEST'] === 'document') |
| 44 | && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'); |
| 45 | if ($canPrintScripts) { |
| 46 | return $script; |
| 47 | } else { |
| 48 | return ""; |
| 49 | } |
| 50 | }); |
| 51 | add_action( 'pre_post_update', 'nitropack_log_post_pre_update', 10, 3); |
| 52 | add_filter( 'woocommerce_rest_pre_insert_product_object', 'nitropack_log_product_pre_api_update', 10, 3 ); |
| 53 | // add_action( 'clean_post_cache', 'nitropack_post_updated', 10, 3); |
| 54 | // add_action( 'post_updated', 'nitropack_post_updated', 10, 3); |
| 55 | // add_action( 'save_post', 'nitropack_post_updated', 10, 3); |
| 56 | // add_action( 'edited_term_taxonomy', 'nitropack_post_updated', 10, 3); |
| 57 | // add_action( 'woocommerce_update_product', 'nitropack_postmeta_updated', 10, 4); |
| 58 | add_action( 'updated_postmeta', 'nitropack_postmeta_updated', 10, 4); |
| 59 | add_action( 'set_object_terms', 'nitropack_sot', 10, 6); |
| 60 | add_action( 'transition_post_status', 'nitropack_handle_post_transition', 10, 3); |
| 61 | add_action( 'publish_post', 'nitropack_handle_first_publish', 10, 1); |
| 62 | add_action( 'transition_comment_status', 'nitropack_handle_comment_transition', 10, 3); |
| 63 | add_action( 'comment_post', 'nitropack_handle_comment_post', 10, 2); |
| 64 | add_action( 'switch_theme', 'nitropack_theme_handler' ); |
| 65 | register_shutdown_function('nitropack_execute_purges'); |
| 66 | register_shutdown_function('nitropack_execute_invalidations'); |
| 67 | register_shutdown_function('nitropack_execute_warmups'); |
| 68 | |
| 69 | add_action( 'woocommerce_product_object_updated_props', 'nitropack_handle_product_updates', 0, 2); |
| 70 | add_action( 'woocommerce_rest_insert_product', function($post, $request, $creating) { |
| 71 | if (!$creating) { |
| 72 | nitropack_detect_changes_and_clean_post_cache($post); |
| 73 | } |
| 74 | }, 10, 3); |
| 75 | add_action( 'woocommerce_rest_insert_product_object', function($product, $request, $creating) { |
| 76 | if (!$creating) { |
| 77 | |
| 78 | $post = get_post($product->get_id()); |
| 79 | nitropack_detect_changes_and_clean_post_cache($post); |
| 80 | } |
| 81 | }, 10, 3); |
| 82 | |
| 83 | add_action('wcml_set_client_currency', function($currency) { |
| 84 | setcookie('np_wc_currency', $currency, time() + (86400 * 7), "/"); |
| 85 | }); |
| 86 | |
| 87 | if (nitropack_has_advanced_cache()) { |
| 88 | // Handle automated updates |
| 89 | if (!defined("NITROPACK_ADVANCED_CACHE_VERSION") || NITROPACK_VERSION != NITROPACK_ADVANCED_CACHE_VERSION) { |
| 90 | add_action( 'plugins_loaded', 'nitropack_install_advanced_cache' ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | add_action('wp_footer', 'nitropack_print_heartbeat_script'); |
| 95 | add_action('admin_footer', 'nitropack_print_heartbeat_script'); |
| 96 | /** |
| 97 | * Fires after a template file is loaded. |
| 98 | * |
| 99 | * @param string $template_file The full path to the template file. |
| 100 | * @param bool $require_once Whether to require_once or require. |
| 101 | */ |
| 102 | add_action('wp_after_load_template', function($template_file, $require_once) { |
| 103 | if (strpos( $template_file, '/footer') !== false) { |
| 104 | nitropack_print_heartbeat_script(); |
| 105 | } |
| 106 | }, PHP_INT_MAX, 2); // We need to run this after the footer is loaded. |
| 107 | |
| 108 | add_action('wp_footer', 'nitropack_print_cookie_handler_script'); |
| 109 | add_action('admin_footer', 'nitropack_print_cookie_handler_script'); |
| 110 | add_action('admin_footer', function() { |
| 111 | nitropack_setcookie("nitroCachedPage", 0, time() - 86400); |
| 112 | }); // Clear the nitroCachePage cookie |
| 113 | /** |
| 114 | * Fires after a template file is loaded. |
| 115 | * |
| 116 | * @param string $template_file The full path to the template file. |
| 117 | * @param bool $require_once Whether to require_once or require. |
| 118 | */ |
| 119 | add_action('wp_after_load_template', function($template_file, $require_once) { |
| 120 | if (strpos( $template_file, '/footer') !== false) { |
| 121 | nitropack_print_cookie_handler_script(); |
| 122 | } |
| 123 | }, PHP_INT_MAX, 2); // We need to run this after the footer is loaded. |
| 124 | |
| 125 | |
| 126 | if ( is_admin() ) { |
| 127 | add_action( 'admin_menu', 'nitropack_menu' ); |
| 128 | add_action( 'admin_init', 'register_nitropack_settings' ); |
| 129 | add_action( 'admin_notices', 'nitropack_admin_notices' ); |
| 130 | add_action( 'network_admin_notices', 'nitropack_admin_notices' ); |
| 131 | add_action( 'wp_ajax_nitropack_purge_cache', 'nitropack_purge_cache' ); |
| 132 | add_action( 'wp_ajax_nitropack_invalidate_cache', 'nitropack_invalidate_cache' ); |
| 133 | add_action( 'wp_ajax_nitropack_clear_residual_cache', 'nitropack_clear_residual_cache' ); |
| 134 | add_action( 'wp_ajax_nitropack_verify_connect', 'nitropack_verify_connect_ajax' ); |
| 135 | add_action( 'wp_ajax_nitropack_disconnect', 'nitropack_disconnect' ); |
| 136 | add_action( 'wp_ajax_nitropack_test_compression_ajax', 'nitropack_test_compression_ajax' ); |
| 137 | add_action( 'wp_ajax_nitropack_set_compression_ajax', 'nitropack_set_compression_ajax' ); |
| 138 | add_action( 'wp_ajax_nitropack_set_auto_cache_purge_ajax', 'nitropack_set_auto_cache_purge_ajax' ); |
| 139 | add_action( 'wp_ajax_nitropack_set_cart_cache_ajax', 'nitropack_set_cart_cache_ajax' ); |
| 140 | add_action( 'wp_ajax_nitropack_set_bb_cache_purge_sync_ajax', 'nitropack_set_bb_cache_purge_sync_ajax' ); |
| 141 | add_action( 'wp_ajax_nitropack_set_legacy_purge_ajax', 'nitropack_set_legacy_purge_ajax' ); |
| 142 | add_action( 'wp_ajax_nitropack_set_cacheable_post_types', 'nitropack_set_cacheable_post_types' ); |
| 143 | add_action( 'wp_ajax_nitropack_enable_warmup', 'nitropack_enable_warmup' ); |
| 144 | add_action( 'wp_ajax_nitropack_disable_warmup', 'nitropack_disable_warmup' ); |
| 145 | add_action( 'wp_ajax_nitropack_warmup_stats', 'nitropack_warmup_stats' ); |
| 146 | add_action( 'wp_ajax_nitropack_estimate_warmup', 'nitropack_estimate_warmup' ); |
| 147 | add_action( 'wp_ajax_nitropack_run_warmup', 'nitropack_run_warmup' ); |
| 148 | add_action( 'wp_ajax_nitropack_purge_single_cache', 'nitropack_purge_single_cache' ); |
| 149 | add_action( 'wp_ajax_nitropack_invalidate_single_cache', 'nitropack_invalidate_single_cache' ); |
| 150 | add_action( 'wp_ajax_nitropack_purge_entire_cache', 'nitropack_purge_entire_cache' ); |
| 151 | add_action( 'wp_ajax_nitropack_dismiss_hosting_notice', 'nitropack_dismiss_hosting_notice' ); |
| 152 | add_action( 'wp_ajax_nitropack_dismiss_woocommerce_notice', 'nitropack_dismiss_woocommerce_notice' ); |
| 153 | add_action( 'wp_ajax_nitropack_reconfigure_webhooks', 'nitropack_reconfigure_webhooks' ); |
| 154 | add_action( 'wp_ajax_nitropack_generate_report', 'nitropack_generate_report' );//diag_ajax_hook |
| 155 | add_action( 'wp_ajax_nitropack_enable_safemode', 'nitropack_enable_safemode' ); |
| 156 | add_action( 'wp_ajax_nitropack_disable_safemode', 'nitropack_disable_safemode' ); |
| 157 | add_action( 'wp_ajax_nitropack_safemode_status', 'nitropack_safemode_status' ); |
| 158 | add_action( 'wp_ajax_nitropack_rml_notification', 'nitropack_rml_notification' ); |
| 159 | add_action( 'activated_plugin', 'nitropack_upgrade_handler' ); |
| 160 | add_action( 'deactivated_plugin', 'nitropack_upgrade_handler' ); |
| 161 | add_action( 'upgrader_process_complete', 'nitropack_upgrade_handler'); |
| 162 | add_action( 'update_option_nitropack-enableCompression', 'nitropack_handle_compression_toggle', 10, 2 ); |
| 163 | add_action( 'add_meta_boxes', 'nitropack_add_meta_box' ); |
| 164 | add_action( 'plugins_loaded', 'nitropack_offer_safemode'); |
| 165 | |
| 166 | add_filter('get_nitropack_notifications', 'nitropack_ignore_dismissed_notifications', 10, 2); |
| 167 | |
| 168 | register_activation_hook( __FILE__, 'nitropack_activate' ); |
| 169 | register_deactivation_hook( __FILE__, 'nitropack_deactivate' ); |
| 170 | } else { |
| 171 | if (null !== $nitro = get_nitropack_sdk()) { |
| 172 | $GLOBALS["NitroPack.instance"] = $nitro; |
| 173 | if (get_option('nitropack-enableCompression') == 1) { |
| 174 | $nitro->enableCompression(); |
| 175 | } |
| 176 | add_action( 'wp', 'nitropack_init' ); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | function nitropack_menu() { |
| 181 | add_options_page( 'NitroPack Options', 'NitroPack', 'manage_options', 'nitropack', 'nitropack_options' ); |
| 182 | add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'nitropack_action_links' ); |
| 183 | } |
| 184 | |
| 185 | function nitropack_action_links ( $links ) { |
| 186 | $nitroLinks = array( |
| 187 | '<a href="' . admin_url( 'options-general.php?page=nitropack' ) . '" rel="noopener noreferrer">Settings</a>', |
| 188 | '<a href="https://support.nitropack.io/hc/en-us/categories/360005122034-Frequently-Asked-Questions-FAQs-" target="_blank" rel="noopener noreferrer">FAQ</a>', |
| 189 | '<a href="https://support.nitropack.io/hc/en-us" target="_blank" rel="noopener noreferrer">Docs</a>', |
| 190 | '<a href="https://support.nitropack.io/hc/en-us/requests/new" target="_blank" rel="noopener noreferrer">Support</a>', |
| 191 | ); |
| 192 | return array_merge( $nitroLinks, $links ); |
| 193 | } |
| 194 | |
| 195 | add_action( 'init', function() { |
| 196 | if (current_user_can( 'manage_options' )) { |
| 197 | |
| 198 | // Enqueue font awesome |
| 199 | add_action( 'wp_enqueue_scripts', 'nitropack_enqueue_load_fa'); |
| 200 | add_action( 'admin_enqueue_scripts', 'nitropack_enqueue_load_fa'); |
| 201 | |
| 202 | // Enqueue admin bar menu custom stylesheet |
| 203 | add_action( 'wp_enqueue_scripts', 'enqueue_nitropack_admin_bar_menu_stylesheet'); |
| 204 | add_action( 'admin_enqueue_scripts', 'enqueue_nitropack_admin_bar_menu_stylesheet'); |
| 205 | |
| 206 | // Enqueue admin menu custom javascript |
| 207 | add_action( 'wp_enqueue_scripts', 'nitropack_admin_bar_script' ); |
| 208 | add_action( 'admin_enqueue_scripts', 'nitropack_admin_bar_script' ); |
| 209 | |
| 210 | // Add our admin menu bar entry |
| 211 | add_action('admin_bar_menu', 'nitropack_admin_bar_menu', PHP_INT_MAX - 10 ); |
| 212 | add_action('plugins_loaded', 'nitropack_plugin_notices'); // Run the checks early, because we need to set some headers. The results from the checks will be cached, so future calls will work as expected. |
| 213 | |
| 214 | add_action( 'updated_option', 'nitropack_updated_option', ~PHP_INT_MAX, 3 ); |
| 215 | |
| 216 | \NitroPack\PluginStateHandler::init(); |
| 217 | |
| 218 | add_action( 'admin_enqueue_scripts', function() { |
| 219 | wp_enqueue_script('nitropack_notices_js', plugin_dir_url(__FILE__) . 'view/javascript/np_notices.js?np_v=' . NITROPACK_VERSION); |
| 220 | }); |
| 221 | |
| 222 | add_action('in_admin_header', function() { |
| 223 | $screen = get_current_screen(); |
| 224 | if ($screen->id === 'settings_page_nitropack') { |
| 225 | remove_all_actions( 'user_admin_notices' ); |
| 226 | remove_all_actions( 'admin_notices' ); |
| 227 | remove_all_actions( 'all_admin_notices' ); |
| 228 | } |
| 229 | }, 99); |
| 230 | } |
| 231 | }); |
| 232 | |
| 233 | /** |
| 234 | * Load text domain for translations |
| 235 | * |
| 236 | * @return void |
| 237 | */ |
| 238 | function nitropack_load_textdomain() { |
| 239 | global $l10n; |
| 240 | |
| 241 | $domain = 'nitropack'; |
| 242 | |
| 243 | if ( isset( $l10n[ $domain ] ) ) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/languages/' ); |
| 248 | } |
| 249 | |
| 250 | add_action( 'plugins_loaded','nitropack_load_textdomain' ); |
| 251 |