nitropack
Last commit date
classes
3 years ago
languages
3 years ago
nitropack-sdk
3 years ago
view
3 years ago
advanced-cache.php
3 years ago
batcache-compat.php
4 years ago
cf-helper.php
5 years ago
constants.php
3 years ago
diagnostics.php
3 years ago
functions.php
3 years ago
helpers.php
3 years ago
integrations.php
4 years ago
main.php
3 years ago
readme.txt
3 years ago
uninstall.php
3 years ago
wp-cli.php
3 years ago
main.php
220 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.7.0 |
| 7 | Author: NitroPack LLC |
| 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 | |
| 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_action( 'transition_post_status', 'nitropack_handle_post_transition', 10, 3); |
| 53 | add_action('set_object_terms', 'nitropack_handle_first_publish', 10, 6); |
| 54 | add_action( 'transition_comment_status', 'nitropack_handle_comment_transition', 10, 3); |
| 55 | add_action( 'comment_post', 'nitropack_handle_comment_post', 10, 2); |
| 56 | add_action( 'switch_theme', 'nitropack_theme_handler' ); |
| 57 | register_shutdown_function('nitropack_execute_purges'); |
| 58 | register_shutdown_function('nitropack_execute_invalidations'); |
| 59 | register_shutdown_function('nitropack_execute_warmups'); |
| 60 | |
| 61 | add_action( 'woocommerce_product_object_updated_props', 'nitropack_handle_product_updates', 0, 2); |
| 62 | add_action( 'woocommerce_rest_insert_product', function($post, $request, $creating) { |
| 63 | if (!$creating) { |
| 64 | nitropack_clean_post_cache($post); |
| 65 | } |
| 66 | }, 10, 3); |
| 67 | add_action( 'woocommerce_rest_insert_product_object', function($product, $request, $creating) { |
| 68 | if (!$creating) { |
| 69 | $post = get_post($product->id); |
| 70 | nitropack_clean_post_cache($post); |
| 71 | } |
| 72 | }, 10, 3); |
| 73 | |
| 74 | add_action('wcml_set_client_currency', function($currency) { |
| 75 | setcookie('np_wc_currency', $currency, time() + (86400 * 7), "/"); |
| 76 | }); |
| 77 | |
| 78 | if (nitropack_has_advanced_cache()) { |
| 79 | // Handle automated updates |
| 80 | if (!defined("NITROPACK_ADVANCED_CACHE_VERSION") || NITROPACK_VERSION != NITROPACK_ADVANCED_CACHE_VERSION) { |
| 81 | add_action( 'plugins_loaded', 'nitropack_install_advanced_cache' ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | add_action('wp_footer', 'nitropack_print_heartbeat_script'); |
| 86 | add_action('admin_footer', 'nitropack_print_heartbeat_script'); |
| 87 | add_action('get_footer', 'nitropack_print_heartbeat_script'); |
| 88 | |
| 89 | add_action('wp_footer', 'nitropack_print_cookie_handler_script'); |
| 90 | add_action('admin_footer', 'nitropack_print_cookie_handler_script'); |
| 91 | add_action('admin_footer', function() { |
| 92 | nitropack_setcookie("nitroCachedPage", 0, time() - 86400); |
| 93 | }); // Clear the nitroCachePage cookie |
| 94 | add_action('get_footer', 'nitropack_print_cookie_handler_script'); |
| 95 | |
| 96 | if ( is_admin() ) { |
| 97 | add_action( 'admin_menu', 'nitropack_menu' ); |
| 98 | add_action( 'admin_init', 'register_nitropack_settings' ); |
| 99 | add_action( 'admin_notices', 'nitropack_admin_notices' ); |
| 100 | add_action( 'network_admin_notices', 'nitropack_admin_notices' ); |
| 101 | add_action( 'wp_ajax_nitropack_purge_cache', 'nitropack_purge_cache' ); |
| 102 | add_action( 'wp_ajax_nitropack_invalidate_cache', 'nitropack_invalidate_cache' ); |
| 103 | add_action( 'wp_ajax_nitropack_clear_residual_cache', 'nitropack_clear_residual_cache' ); |
| 104 | add_action( 'wp_ajax_nitropack_verify_connect', 'nitropack_verify_connect_ajax' ); |
| 105 | add_action( 'wp_ajax_nitropack_disconnect', 'nitropack_disconnect' ); |
| 106 | add_action( 'wp_ajax_nitropack_test_compression_ajax', 'nitropack_test_compression_ajax' ); |
| 107 | add_action( 'wp_ajax_nitropack_set_compression_ajax', 'nitropack_set_compression_ajax' ); |
| 108 | add_action( 'wp_ajax_nitropack_set_auto_cache_purge_ajax', 'nitropack_set_auto_cache_purge_ajax' ); |
| 109 | add_action( 'wp_ajax_nitropack_set_cart_cache_ajax', 'nitropack_set_cart_cache_ajax' ); |
| 110 | add_action( 'wp_ajax_nitropack_set_bb_cache_purge_sync_ajax', 'nitropack_set_bb_cache_purge_sync_ajax' ); |
| 111 | add_action( 'wp_ajax_nitropack_set_cacheable_post_types', 'nitropack_set_cacheable_post_types' ); |
| 112 | add_action( 'wp_ajax_nitropack_enable_warmup', 'nitropack_enable_warmup' ); |
| 113 | add_action( 'wp_ajax_nitropack_disable_warmup', 'nitropack_disable_warmup' ); |
| 114 | add_action( 'wp_ajax_nitropack_warmup_stats', 'nitropack_warmup_stats' ); |
| 115 | add_action( 'wp_ajax_nitropack_estimate_warmup', 'nitropack_estimate_warmup' ); |
| 116 | add_action( 'wp_ajax_nitropack_run_warmup', 'nitropack_run_warmup' ); |
| 117 | add_action( 'wp_ajax_nitropack_purge_single_cache', 'nitropack_purge_single_cache' ); |
| 118 | add_action( 'wp_ajax_nitropack_invalidate_single_cache', 'nitropack_invalidate_single_cache' ); |
| 119 | add_action( 'wp_ajax_nitropack_purge_entire_cache', 'nitropack_purge_entire_cache' ); |
| 120 | add_action( 'wp_ajax_nitropack_dismiss_hosting_notice', 'nitropack_dismiss_hosting_notice' ); |
| 121 | add_action( 'wp_ajax_nitropack_dismiss_woocommerce_notice', 'nitropack_dismiss_woocommerce_notice' ); |
| 122 | add_action( 'wp_ajax_nitropack_reconfigure_webhooks', 'nitropack_reconfigure_webhooks' ); |
| 123 | add_action( 'wp_ajax_nitropack_generate_report', 'nitropack_generate_report' );//diag_ajax_hook |
| 124 | add_action( 'wp_ajax_nitropack_enable_safemode', 'nitropack_enable_safemode' ); |
| 125 | add_action( 'wp_ajax_nitropack_disable_safemode', 'nitropack_disable_safemode' ); |
| 126 | add_action( 'wp_ajax_nitropack_safemode_status', 'nitropack_safemode_status' ); |
| 127 | add_action( 'wp_ajax_nitropack_rml_notification', 'nitropack_rml_notification' ); |
| 128 | add_action( 'activated_plugin', 'nitropack_upgrade_handler' ); |
| 129 | add_action( 'deactivated_plugin', 'nitropack_upgrade_handler' ); |
| 130 | add_action( 'upgrader_process_complete', 'nitropack_upgrade_handler'); |
| 131 | add_action( 'update_option_nitropack-enableCompression', 'nitropack_handle_compression_toggle', 10, 2 ); |
| 132 | add_action( 'add_meta_boxes', 'nitropack_add_meta_box' ); |
| 133 | add_action( 'plugins_loaded', 'nitropack_offer_safemode'); |
| 134 | |
| 135 | add_filter('get_nitropack_notifications', 'nitropack_ignore_dismissed_notifications', 10, 2); |
| 136 | |
| 137 | register_activation_hook( __FILE__, 'nitropack_activate' ); |
| 138 | register_deactivation_hook( __FILE__, 'nitropack_deactivate' ); |
| 139 | } else { |
| 140 | if (null !== $nitro = get_nitropack_sdk()) { |
| 141 | $GLOBALS["NitroPack.instance"] = $nitro; |
| 142 | if (get_option('nitropack-enableCompression') == 1) { |
| 143 | $nitro->enableCompression(); |
| 144 | } |
| 145 | add_action( 'wp', 'nitropack_init' ); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | function nitropack_menu() { |
| 150 | add_options_page( 'NitroPack Options', 'NitroPack', 'manage_options', 'nitropack', 'nitropack_options' ); |
| 151 | add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'nitropack_action_links' ); |
| 152 | } |
| 153 | |
| 154 | function nitropack_action_links ( $links ) { |
| 155 | $nitroLinks = array( |
| 156 | '<a href="' . admin_url( 'options-general.php?page=nitropack' ) . '" rel="noopener noreferrer">Settings</a>', |
| 157 | '<a href="https://support.nitropack.io/hc/en-us/categories/360005122034-Frequently-Asked-Questions-FAQs-" target="_blank" rel="noopener noreferrer">FAQ</a>', |
| 158 | '<a href="https://support.nitropack.io/hc/en-us" target="_blank" rel="noopener noreferrer">Docs</a>', |
| 159 | '<a href="https://support.nitropack.io/hc/en-us/requests/new" target="_blank" rel="noopener noreferrer">Support</a>', |
| 160 | ); |
| 161 | return array_merge( $nitroLinks, $links ); |
| 162 | } |
| 163 | |
| 164 | add_action( 'init', function() { |
| 165 | if (current_user_can( 'manage_options' )) { |
| 166 | |
| 167 | // Enqueue font awesome |
| 168 | add_action( 'wp_enqueue_scripts', 'nitropack_enqueue_load_fa'); |
| 169 | add_action( 'admin_enqueue_scripts', 'nitropack_enqueue_load_fa'); |
| 170 | |
| 171 | // Enqueue admin bar menu custom stylesheet |
| 172 | add_action( 'wp_enqueue_scripts', 'enqueue_nitropack_admin_bar_menu_stylesheet'); |
| 173 | add_action( 'admin_enqueue_scripts', 'enqueue_nitropack_admin_bar_menu_stylesheet'); |
| 174 | |
| 175 | // Enqueue admin menu custom javascript |
| 176 | add_action( 'wp_enqueue_scripts', 'nitropack_admin_bar_script' ); |
| 177 | add_action( 'admin_enqueue_scripts', 'nitropack_admin_bar_script' ); |
| 178 | |
| 179 | // Add our admin menu bar entry |
| 180 | add_action('admin_bar_menu', 'nitropack_admin_bar_menu', PHP_INT_MAX - 10 ); |
| 181 | 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. |
| 182 | |
| 183 | add_action( 'updated_option', 'nitropack_updated_option', ~PHP_INT_MAX, 3 ); |
| 184 | |
| 185 | \NitroPack\PluginStateHandler::init(); |
| 186 | |
| 187 | add_action( 'admin_enqueue_scripts', function() { |
| 188 | wp_enqueue_script('nitropack_notices_js', plugin_dir_url(__FILE__) . 'view/javascript/np_notices.js?np_v=' . NITROPACK_VERSION); |
| 189 | }); |
| 190 | |
| 191 | add_action('in_admin_header', function() { |
| 192 | $screen = get_current_screen(); |
| 193 | if ($screen->id === 'settings_page_nitropack') { |
| 194 | remove_all_actions( 'user_admin_notices' ); |
| 195 | remove_all_actions( 'admin_notices' ); |
| 196 | remove_all_actions( 'all_admin_notices' ); |
| 197 | } |
| 198 | }, 99); |
| 199 | } |
| 200 | }); |
| 201 | |
| 202 | /** |
| 203 | * Load text domain for translations |
| 204 | * |
| 205 | * @return void |
| 206 | */ |
| 207 | function nitropack_load_textdomain() { |
| 208 | global $l10n; |
| 209 | |
| 210 | $domain = 'nitropack'; |
| 211 | |
| 212 | if ( isset( $l10n[ $domain ] ) ) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/languages/' ); |
| 217 | } |
| 218 | |
| 219 | add_action( 'plugins_loaded','nitropack_load_textdomain' ); |
| 220 |