nitropack
Last commit date
classes
6 months ago
languages
7 months ago
nitropack-sdk
6 months ago
view
6 months ago
advanced-cache.php
1 year ago
batcache-compat.php
1 year ago
constants.php
7 months ago
diagnostics.php
11 months ago
functions.php
6 months ago
helpers.php
1 year ago
main.php
6 months ago
readme.txt
6 months ago
uninstall.php
7 months ago
main.php
273 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.18.8 |
| 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 | if ( ! defined( 'NITROPACK_BASENAME' ) ) { |
| 18 | define( 'NITROPACK_BASENAME', plugin_basename( __FILE__ ) ); |
| 19 | } |
| 20 | |
| 21 | $np_basePath = dirname( __FILE__ ) . '/'; |
| 22 | require_once $np_basePath . 'functions.php'; |
| 23 | require_once $np_basePath . 'helpers.php'; |
| 24 | require_once $np_basePath . 'diagnostics.php'; |
| 25 | |
| 26 | if ( nitropack_is_wp_cli() ) { |
| 27 | $nitropack_cli = new \NitroPack\WordPress\CLI(); |
| 28 | $nitropack_cli->init(); |
| 29 | } |
| 30 | |
| 31 | if ( \NitroPack\Integration\Plugin\Ezoic::isActive() ) { |
| 32 | if ( ! nitropack_is_optimizer_request() ) { |
| 33 | // We need to serve the cached content after Ezoic's output buffering has started at plugins_loaded,0 |
| 34 | add_action( 'plugins_loaded', function () { |
| 35 | add_filter( 'home_url', [ '\NitroPack\Integration\Plugin\Ezoic', 'getHomeUrl' ] ); |
| 36 | nitropack_handle_request( "plugin-ezoic" ); |
| 37 | remove_filter( 'home_url', [ '\NitroPack\Integration\Plugin\Ezoic', 'getHomeUrl' ] ); |
| 38 | }, 1 ); |
| 39 | } else { |
| 40 | add_action( 'plugins_loaded', [ '\NitroPack\Integration\Plugin\Ezoic', 'disable' ], 1 ); |
| 41 | } |
| 42 | } else { |
| 43 | nitropack_handle_request( "plugin" ); |
| 44 | } |
| 45 | |
| 46 | add_filter( 'nitro_script_output', function ($script) { |
| 47 | $isPrefetch = isset( $_SERVER['HTTP_SEC_FETCH_DEST'] ) |
| 48 | && $_SERVER['HTTP_SEC_FETCH_DEST'] === 'empty' |
| 49 | && ( |
| 50 | ( isset( $_SERVER['HTTP_SEC_PURPOSE'] ) && $_SERVER['HTTP_SEC_PURPOSE'] === 'prefetch' ) |
| 51 | || |
| 52 | ( isset( $_SERVER['HTTP_PURPOSE'] ) && $_SERVER['HTTP_PURPOSE'] === 'prefetch' ) |
| 53 | ); |
| 54 | |
| 55 | $canPrintScripts = ! nitropack_is_amp_page() // Make sure we don't accidentally print a non-amp compatible script to an amp page |
| 56 | && ( ! isset( $_SERVER['HTTP_SEC_FETCH_DEST'] ) || $_SERVER['HTTP_SEC_FETCH_DEST'] === 'document' || $isPrefetch ) |
| 57 | && ( ! isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) || strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) !== 'xmlhttprequest' ); |
| 58 | |
| 59 | if ( $canPrintScripts ) { |
| 60 | return $script; |
| 61 | } else { |
| 62 | return ""; |
| 63 | } |
| 64 | } ); |
| 65 | add_action( 'pre_post_update', 'nitropack_log_post_pre_update', 10, 3 ); |
| 66 | add_filter( 'woocommerce_rest_pre_insert_product_object', 'nitropack_log_product_pre_api_update', 10, 3 ); |
| 67 | add_action( 'set_object_terms', 'nitropack_sot', 10, 6 ); |
| 68 | add_action( 'transition_post_status', 'nitropack_handle_post_transition', 10, 3 ); |
| 69 | //add_action('publish_post', 'nitropack_handle_first_publish', 10, 1); |
| 70 | add_action( 'transition_comment_status', 'nitropack_handle_comment_transition', 10, 3 ); |
| 71 | add_action( 'comment_post', 'nitropack_handle_comment_post', 10, 2 ); |
| 72 | add_action( 'woocommerce_reduce_order_item_stock', 'nitropack_invalidate_stock_on_order_submit', 10, 3 ); |
| 73 | add_action( 'switch_theme', 'nitropack_theme_handler' ); |
| 74 | register_shutdown_function( 'nitropack_execute_purges' ); |
| 75 | register_shutdown_function( 'nitropack_execute_invalidations' ); |
| 76 | register_shutdown_function( 'nitropack_execute_warmups' ); |
| 77 | |
| 78 | add_action( 'woocommerce_product_object_updated_props', 'nitropack_handle_product_updates', 0, 2 ); |
| 79 | add_action( 'woocommerce_rest_insert_product', function ($post, $request, $creating) { |
| 80 | if ( ! $creating ) { |
| 81 | nitropack_detect_changes_and_clean_post_cache( $post ); |
| 82 | } |
| 83 | }, 10, 3 ); |
| 84 | add_action( 'woocommerce_rest_insert_product_object', function ($product, $request, $creating) { |
| 85 | if ( ! $creating ) { |
| 86 | |
| 87 | $post = get_post( $product->get_id() ); |
| 88 | if ( ! defined( 'NITROPACK_PURGE_CACHE' ) ) { |
| 89 | nitropack_detect_changes_and_clean_post_cache( $post ); |
| 90 | } |
| 91 | } |
| 92 | }, 10, 3 ); |
| 93 | |
| 94 | if ( nitropack_has_advanced_cache() ) { |
| 95 | // Handle automated updates |
| 96 | if ( ! defined( "NITROPACK_ADVANCED_CACHE_VERSION" ) || NITROPACK_VERSION != NITROPACK_ADVANCED_CACHE_VERSION ) { |
| 97 | add_action( 'plugins_loaded', 'nitropack_install_advanced_cache' ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | add_action( 'wp_footer', 'nitropack_print_heartbeat_script' ); |
| 102 | add_action( 'admin_footer', 'nitropack_print_heartbeat_script' ); |
| 103 | add_action( 'get_footer', 'nitropack_print_heartbeat_script' ); |
| 104 | |
| 105 | add_action( 'wp_footer', 'nitropack_print_cookie_handler_script' ); |
| 106 | add_action( 'admin_footer', 'nitropack_print_cookie_handler_script' ); |
| 107 | add_action( 'admin_footer', function () { |
| 108 | nitropack_setcookie( "nitroCachedPage", 0, time() - 86400 ); |
| 109 | } ); // Clear the nitroCachePage cookie |
| 110 | add_action( 'get_footer', 'nitropack_print_cookie_handler_script' ); |
| 111 | |
| 112 | if (is_admin()) { |
| 113 | add_action('admin_menu', 'nitropack_menu'); |
| 114 | add_action('admin_init', 'register_nitropack_settings'); |
| 115 | |
| 116 | add_action('wp_ajax_nitropack_purge_cache', 'nitropack_purge_cache'); |
| 117 | add_action('wp_ajax_nitropack_invalidate_cache', 'nitropack_invalidate_cache'); |
| 118 | add_action('wp_ajax_nitropack_clear_residual_cache', 'nitropack_clear_residual_cache'); |
| 119 | add_action('wp_ajax_nitropack_verify_connect', 'nitropack_verify_connect_ajax'); |
| 120 | add_action('wp_ajax_nitropack_disconnect', 'nitropack_disconnect'); |
| 121 | |
| 122 | add_action('wp_ajax_nitropack_test_compression_ajax', 'nitropack_test_compression_ajax'); |
| 123 | add_action('wp_ajax_nitropack_set_compression_ajax', 'nitropack_set_compression_ajax'); |
| 124 | add_action('wp_ajax_nitropack_set_can_editor_clear_cache', 'nitropack_set_can_editor_clear_cache'); |
| 125 | add_action('wp_ajax_nitropack_set_stock_reduce_status', 'nitropack_set_stock_reduce_status'); |
| 126 | add_action('wp_ajax_nitropack_set_auto_cache_purge_ajax', 'nitropack_set_auto_cache_purge_ajax'); |
| 127 | add_action('wp_ajax_nitropack_set_cart_cache_ajax', 'nitropack_set_cart_cache_ajax'); |
| 128 | add_action('wp_ajax_nitropack_set_bb_cache_purge_sync_ajax', 'nitropack_set_bb_cache_purge_sync_ajax'); |
| 129 | add_action('wp_ajax_nitropack_set_cacheable_post_types', 'nitropack_set_cacheable_post_types'); |
| 130 | add_action('wp_ajax_nitropack_enable_warmup', 'nitropack_enable_warmup'); |
| 131 | add_action('wp_ajax_nitropack_disable_warmup', 'nitropack_disable_warmup'); |
| 132 | add_action('wp_ajax_nitropack_warmup_stats', 'nitropack_warmup_stats'); |
| 133 | add_action('wp_ajax_nitropack_estimate_warmup', 'nitropack_estimate_warmup'); |
| 134 | add_action('wp_ajax_nitropack_run_warmup', 'nitropack_run_warmup'); |
| 135 | add_action('wp_ajax_nitropack_purge_single_cache', 'nitropack_purge_single_cache'); |
| 136 | add_action('wp_ajax_nitropack_invalidate_single_cache', 'nitropack_invalidate_single_cache'); |
| 137 | add_action('wp_ajax_nitropack_purge_entire_cache', 'nitropack_purge_entire_cache'); |
| 138 | add_action('wp_ajax_nitropack_dismiss_hosting_notice', 'nitropack_dismiss_hosting_notice'); |
| 139 | |
| 140 | add_action('wp_ajax_nitropack_reconfigure_webhooks', 'nitropack_reconfigure_webhooks'); |
| 141 | add_action('wp_ajax_nitropack_generate_report', 'nitropack_generate_report'); //diag_ajax_hook |
| 142 | |
| 143 | add_action('admin_init', 'nitropack_autooptimize_new_post_types_and_taxonomies'); |
| 144 | add_action('activated_plugin', 'nitropack_upgrade_handler'); |
| 145 | add_action('deactivated_plugin', 'nitropack_upgrade_handler'); |
| 146 | add_action('upgrader_process_complete', 'nitropack_upgrade_handler'); |
| 147 | add_action('update_option_nitropack-enableCompression', 'nitropack_handle_compression_toggle', 10, 2); |
| 148 | add_action('add_meta_boxes', 'nitropack_add_meta_box'); |
| 149 | |
| 150 | |
| 151 | } else { |
| 152 | if ( null !== $nitro = get_nitropack_sdk() ) { |
| 153 | $GLOBALS["NitroPack.instance"] = $nitro; |
| 154 | if ( get_option( 'nitropack-enableCompression' ) == 1 ) { |
| 155 | $nitro->enableCompression(); |
| 156 | } |
| 157 | add_action( 'wp', 'nitropack_init' ); |
| 158 | } |
| 159 | } |
| 160 | /** |
| 161 | * This function is called when the plugin is activated/deactivated. Works for wp-cli as well. |
| 162 | */ |
| 163 | register_activation_hook( __FILE__, 'nitropack_activate' ); |
| 164 | register_deactivation_hook( __FILE__, 'nitropack_deactivate' ); |
| 165 | |
| 166 | function nitropack_menu() { |
| 167 | global $submenu; |
| 168 | |
| 169 | add_menu_page( |
| 170 | 'NitroPack Options', |
| 171 | 'NitroPack', |
| 172 | 'manage_options', |
| 173 | 'nitropack', |
| 174 | 'nitropack_options', |
| 175 | 'dashicons-performance', |
| 176 | 25 |
| 177 | ); |
| 178 | if ( get_nitropack()->getDistribution() !== "oneclick" ) { |
| 179 | add_submenu_page( |
| 180 | 'nitropack', |
| 181 | 'System Report', |
| 182 | 'System Report', |
| 183 | 'manage_options', |
| 184 | 'admin.php?page=nitropack&subpage=system-report' |
| 185 | ); |
| 186 | } |
| 187 | if ( isset( $submenu['nitropack'] ) ) { |
| 188 | foreach ( $submenu['nitropack'] as &$item ) { |
| 189 | if ( $item[0] === 'NitroPack' ) { |
| 190 | $item[0] = 'Dashboard'; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'nitropack_action_links' ); |
| 196 | } |
| 197 | |
| 198 | function nitropack_highlight_submenus( $parent_file ) { |
| 199 | |
| 200 | global $submenu_file; |
| 201 | |
| 202 | if ( isset( $_GET['page'] ) && isset( $_GET['subpage'] ) ) |
| 203 | $submenu_file = 'admin.php?page=' . $_GET['page'] . '&subpage=' . $_GET['subpage']; |
| 204 | |
| 205 | return $parent_file; |
| 206 | } |
| 207 | add_filter( 'parent_file', 'nitropack_highlight_submenus' ); |
| 208 | |
| 209 | function nitropack_action_links( $links ) { |
| 210 | $nitroLinks = array( |
| 211 | '<a href="https://support.nitropack.io/hc/en-us/categories/360005122034-Frequently-Asked-Questions-FAQs-" target="_blank" rel="noopener noreferrer">FAQ</a>', |
| 212 | '<a href="https://support.nitropack.io/hc/en-us" target="_blank" rel="noopener noreferrer">Docs</a>', |
| 213 | '<a href="https://support.nitropack.io/hc/en-us/requests/new" target="_blank" rel="noopener noreferrer">Support</a>', |
| 214 | ); |
| 215 | |
| 216 | if ( get_nitropack()->getDistribution() == "oneclick" ) { |
| 217 | $nitroLinks = apply_filters( "nitropack_oneclick_action_links", $nitroLinks ); |
| 218 | } |
| 219 | |
| 220 | array_unshift( $nitroLinks, '<a href="' . admin_url( 'admin.php?page=nitropack' ) . '" rel="noopener noreferrer">Settings</a>' ); |
| 221 | |
| 222 | return array_merge( $nitroLinks, $links ); |
| 223 | } |
| 224 | |
| 225 | add_action( 'init', function () { |
| 226 | if ( current_user_can( 'manage_options' ) ) { |
| 227 | |
| 228 | // Enqueue admin bar menu custom stylesheet |
| 229 | add_action( 'wp_enqueue_scripts', 'enqueue_nitropack_admin_bar_menu_stylesheet' ); |
| 230 | add_action( 'admin_enqueue_scripts', 'enqueue_nitropack_admin_bar_menu_stylesheet' ); |
| 231 | |
| 232 | // Enqueue admin menu custom javascript |
| 233 | add_action( 'wp_enqueue_scripts', 'nitropack_admin_bar_script' ); |
| 234 | add_action( 'admin_enqueue_scripts', 'nitropack_admin_bar_script' ); |
| 235 | |
| 236 | // Add our admin menu bar entry |
| 237 | add_action( 'admin_bar_menu', 'nitropack_admin_bar_menu', PHP_INT_MAX - 10 ); |
| 238 | |
| 239 | add_action( 'updated_option', 'nitropack_updated_option', ~PHP_INT_MAX, 3 ); |
| 240 | |
| 241 | \NitroPack\PluginStateHandler::init(); |
| 242 | |
| 243 | add_action( 'in_admin_header', function () { |
| 244 | $screen = get_current_screen(); |
| 245 | if ( $screen->id === 'toplevel_page_nitropack' ) { |
| 246 | remove_all_actions( 'user_admin_notices' ); |
| 247 | remove_all_actions( 'admin_notices' ); |
| 248 | remove_all_actions( 'all_admin_notices' ); |
| 249 | } |
| 250 | }, 10 ); |
| 251 | } |
| 252 | ( new \NitroPack\WordPress\Cron() )->schedule_events(); |
| 253 | } ); |
| 254 | |
| 255 | /** |
| 256 | * Load text domain for translations |
| 257 | * http://stackoverflow.com/questions/79198701/notice-function-load-textdomain-just-in-time-was-called-incorrectly - for WP 6.7 |
| 258 | * @return void |
| 259 | */ |
| 260 | function nitropack_load_textdomain() { |
| 261 | global $l10n; |
| 262 | |
| 263 | $domain = 'nitropack'; |
| 264 | |
| 265 | if ( isset( $l10n[ $domain ] ) ) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/languages/' ); |
| 270 | } |
| 271 | |
| 272 | add_action( 'init', 'nitropack_load_textdomain' ); |
| 273 |