nitropack
Last commit date
assets
1 month ago
classes
2 weeks ago
languages
1 month ago
nitropack-sdk
2 weeks ago
view
2 weeks ago
advanced-cache.php
1 year ago
batcache-compat.php
1 year ago
constants.php
2 weeks ago
functions.php
2 weeks ago
helpers.php
1 year ago
main.php
2 weeks ago
readme.txt
2 weeks ago
uninstall.php
7 months ago
main.php
164 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.19.7 |
| 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 | |
| 23 | require_once $np_basePath . 'functions.php'; |
| 24 | require_once $np_basePath . 'helpers.php'; |
| 25 | |
| 26 | new NitroPack\PageSpeedBoost(); |
| 27 | |
| 28 | if ( nitropack_is_wp_cli() ) { |
| 29 | $nitropack_cli = new \NitroPack\WordPress\CLI(); |
| 30 | $nitropack_cli->init(); |
| 31 | } |
| 32 | |
| 33 | if ( \NitroPack\Integration\Plugin\Ezoic::isActive() ) { |
| 34 | if ( ! nitropack_is_optimizer_request() ) { |
| 35 | // We need to serve the cached content after Ezoic's output buffering has started at plugins_loaded,0 |
| 36 | add_action( 'plugins_loaded', function () { |
| 37 | add_filter( 'home_url', [ '\NitroPack\Integration\Plugin\Ezoic', 'getHomeUrl' ] ); |
| 38 | nitropack_handle_request( "plugin-ezoic" ); |
| 39 | remove_filter( 'home_url', [ '\NitroPack\Integration\Plugin\Ezoic', 'getHomeUrl' ] ); |
| 40 | }, 1 ); |
| 41 | } else { |
| 42 | add_action( 'plugins_loaded', [ '\NitroPack\Integration\Plugin\Ezoic', 'disable' ], 1 ); |
| 43 | } |
| 44 | } else { |
| 45 | nitropack_handle_request( "plugin" ); |
| 46 | } |
| 47 | |
| 48 | add_filter( 'nitro_script_output', function ( $script ) { |
| 49 | $isPrefetch = isset( $_SERVER['HTTP_SEC_FETCH_DEST'] ) |
| 50 | && $_SERVER['HTTP_SEC_FETCH_DEST'] === 'empty' |
| 51 | && ( |
| 52 | ( isset( $_SERVER['HTTP_SEC_PURPOSE'] ) && $_SERVER['HTTP_SEC_PURPOSE'] === 'prefetch' ) |
| 53 | || |
| 54 | ( isset( $_SERVER['HTTP_PURPOSE'] ) && $_SERVER['HTTP_PURPOSE'] === 'prefetch' ) |
| 55 | ); |
| 56 | |
| 57 | $canPrintScripts = ! nitropack_is_amp_page() // Make sure we don't accidentally print a non-amp compatible script to an amp page |
| 58 | && ( ! isset( $_SERVER['HTTP_SEC_FETCH_DEST'] ) || $_SERVER['HTTP_SEC_FETCH_DEST'] === 'document' || $isPrefetch ) |
| 59 | && ( ! isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) || strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) !== 'xmlhttprequest' ); |
| 60 | |
| 61 | if ( $canPrintScripts ) { |
| 62 | return $script; |
| 63 | } else { |
| 64 | return ""; |
| 65 | } |
| 66 | } ); |
| 67 | |
| 68 | add_action( 'pre_post_update', 'nitropack_log_post_pre_update', 10, 3 ); |
| 69 | add_filter( 'woocommerce_rest_pre_insert_product_object', 'nitropack_log_product_pre_api_update', 10, 3 ); |
| 70 | add_action( 'transition_post_status', 'nitropack_handle_post_transition', 10, 3 ); |
| 71 | add_action( 'transition_comment_status', 'nitropack_handle_comment_transition', 10, 3 ); |
| 72 | add_action( 'comment_post', 'nitropack_handle_comment_post', 10, 2 ); |
| 73 | add_action( 'switch_theme', 'nitropack_theme_handler' ); |
| 74 | //add invalidations |
| 75 | \NitroPack\WordPress\Invalidations::getInstance(); |
| 76 | |
| 77 | register_shutdown_function( 'nitropack_execute_purges' ); |
| 78 | register_shutdown_function( 'nitropack_execute_invalidations' ); |
| 79 | register_shutdown_function( 'nitropack_execute_warmups' ); |
| 80 | |
| 81 | |
| 82 | |
| 83 | if ( nitropack_has_advanced_cache() ) { |
| 84 | // Handle automated updates |
| 85 | if ( ! defined( "NITROPACK_ADVANCED_CACHE_VERSION" ) || NITROPACK_VERSION != NITROPACK_ADVANCED_CACHE_VERSION ) { |
| 86 | add_action( 'plugins_loaded', 'nitropack_install_advanced_cache' ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | add_action( 'wp_footer', 'nitropack_print_heartbeat_script' ); |
| 91 | add_action( 'admin_footer', 'nitropack_print_heartbeat_script' ); |
| 92 | add_action( 'get_footer', 'nitropack_print_heartbeat_script' ); |
| 93 | |
| 94 | add_action( 'wp_footer', 'nitropack_print_cookie_handler_script' ); |
| 95 | add_action( 'admin_footer', 'nitropack_print_cookie_handler_script' ); |
| 96 | add_action( 'admin_footer', function () { |
| 97 | nitropack_setcookie( "nitroCachedPage", 0, time() - 86400 ); |
| 98 | } ); // Clear the nitroCachePage cookie |
| 99 | add_action( 'get_footer', 'nitropack_print_cookie_handler_script' ); |
| 100 | |
| 101 | \NitroPack\WordPress\Admin::getInstance(); |
| 102 | |
| 103 | if ( is_admin() ) { |
| 104 | |
| 105 | |
| 106 | add_action( 'wp_ajax_nitropack_dismiss_hosting_notice', 'nitropack_dismiss_hosting_notice' ); |
| 107 | add_action( 'wp_ajax_nitropack_reconfigure_webhooks', 'nitropack_reconfigure_webhooks' ); |
| 108 | |
| 109 | add_action( 'activated_plugin', 'nitropack_upgrade_handler' ); |
| 110 | add_action( 'deactivated_plugin', 'nitropack_upgrade_handler' ); |
| 111 | add_action( 'upgrader_process_complete', 'nitropack_upgrade_handler' ); |
| 112 | add_action( 'update_option_nitropack-enableCompression', 'nitropack_handle_compression_toggle', 10, 2 ); |
| 113 | |
| 114 | } else { |
| 115 | if ( null !== $nitro = get_nitropack_sdk() ) { |
| 116 | $GLOBALS["NitroPack.instance"] = $nitro; |
| 117 | if ( get_option( 'nitropack-enableCompression' ) == 1 ) { |
| 118 | $nitro->enableCompression(); |
| 119 | } |
| 120 | add_action( 'wp', 'nitropack_init' ); |
| 121 | } |
| 122 | } |
| 123 | /** |
| 124 | * This function is called when the plugin is activated/deactivated. Works for wp-cli as well. |
| 125 | */ |
| 126 | register_activation_hook( __FILE__, 'nitropack_activate' ); |
| 127 | register_deactivation_hook( __FILE__, 'nitropack_deactivate' ); |
| 128 | |
| 129 | add_action( 'init', function () { |
| 130 | if ( current_user_can( 'manage_options' ) ) { |
| 131 | |
| 132 | \NitroPack\PluginStateHandler::init(); |
| 133 | |
| 134 | add_action( 'in_admin_header', function () { |
| 135 | $screen = get_current_screen(); |
| 136 | if ( $screen->id === 'toplevel_page_nitropack' ) { |
| 137 | remove_all_actions( 'user_admin_notices' ); |
| 138 | remove_all_actions( 'admin_notices' ); |
| 139 | remove_all_actions( 'all_admin_notices' ); |
| 140 | } |
| 141 | }, 10 ); |
| 142 | } |
| 143 | ( new \NitroPack\WordPress\Cron() )->schedule_events(); |
| 144 | } ); |
| 145 | |
| 146 | /** |
| 147 | * Load text domain for translations |
| 148 | * http://stackoverflow.com/questions/79198701/notice-function-load-textdomain-just-in-time-was-called-incorrectly - for WP 6.7 |
| 149 | * @return void |
| 150 | */ |
| 151 | function nitropack_load_textdomain() { |
| 152 | global $l10n; |
| 153 | |
| 154 | $domain = 'nitropack'; |
| 155 | |
| 156 | if ( isset( $l10n[ $domain ] ) ) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/languages/' ); |
| 161 | } |
| 162 | |
| 163 | add_action( 'init', 'nitropack_load_textdomain' ); |
| 164 |