builder name). */ const BUILDER_HOOKS = array( 'elementor/core/files/clear_cache' => 'Elementor regenerated its CSS', 'fl_builder_cache_cleared' => 'Beaver Builder cleared its cache', 'bricks/generate_css_file' => 'Bricks regenerated its CSS', 'update_option_avada_dynamic_css_posts' => 'Avada regenerated its CSS', 'update_option_fusion_options' => 'Avada options changed', 'update_option_et_divi' => 'Divi theme options changed', ); /** * Registers the listeners. Runs only while caching is on. */ public static function register() { foreach ( self::BUILDER_HOOKS as $hook => $cause ) { $hooks = array( $hook ); if ( 0 === strpos( $hook, 'update_option_' ) ) { $hooks[] = 'add_option_' . substr( $hook, 14 ); // The first save of an option fires add_option_*. } foreach ( $hooks as $name ) { add_action( $name, static function () use ( $cause ) { GorillaCache_Purge::all( $cause ); } ); } } if ( ! defined( 'PERFMATTERS_VERSION' ) ) { return; } foreach ( self::PERFMATTERS_OPTIONS as $option ) { foreach ( array( 'add_option_', 'update_option_' ) as $prefix ) { add_action( $prefix . $option, static function () { GorillaCache_Purge::all( 'Perfmatters settings changed' ); // Translated when it happens, never at load. } ); } } add_action( 'delete_option_perfmatters_used_css_time', static function () { GorillaCache_Purge::all( 'Perfmatters cleared its used CSS' ); } ); // Priority 0: Perfmatters' own handler ends the request, but our clear runs at "shutdown". foreach ( self::PERFMATTERS_AJAX as $hook ) { add_action( $hook, array( __CLASS__, 'on_perfmatters_ajax' ), 0 ); } add_action( 'admin_post_perfmatters_clear_used_css', array( __CLASS__, 'on_perfmatters_used_css_link' ), 0 ); add_action( 'admin_post_perfmatters_clear_minified', array( __CLASS__, 'on_perfmatters_minified_link' ), 0 ); if ( defined( 'WP_CLI' ) && WP_CLI && class_exists( 'WP_CLI' ) ) { foreach ( self::PERFMATTERS_COMMANDS as $command ) { WP_CLI::add_hook( 'after_invoke:perfmatters ' . $command, static function () { GorillaCache_Purge::all( 'Perfmatters files changed' ); GorillaCache_Purge::flush(); } ); } } add_action( 'current_screen', array( __CLASS__, 'check_snippets' ) ); } /** * A Perfmatters AJAX request that deletes its files: clear our copies too, but only for a request Perfmatters * itself will accept (an administrator, with Perfmatters' own nonce). This runs before Perfmatters' handler, so a * forged request must not clear our cache on its way to being refused. */ public static function on_perfmatters_ajax() { if ( ! current_user_can( 'manage_options' ) || false === check_ajax_referer( 'perfmatters-nonce', 'nonce', false ) ) { return; } GorillaCache_Purge::all( 'Perfmatters files cleared' ); } /** * Perfmatters' admin-bar link "Clear Used CSS", on the same terms: an administrator, with the nonce Perfmatters * puts on that link. */ public static function on_perfmatters_used_css_link() { if ( ! current_user_can( 'manage_options' ) || ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'perfmatters_clear_used_css' ) ) { return; } GorillaCache_Purge::all( 'Perfmatters files cleared' ); } /** * Perfmatters' admin-bar link "Clear Minified CSS/JS", on the same terms. */ public static function on_perfmatters_minified_link() { if ( ! current_user_can( 'manage_options' ) || ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'perfmatters_clear_minified' ) ) { return; } GorillaCache_Purge::all( 'Perfmatters files cleared' ); } /** * Perfmatters code snippets are saved to a file with no hook: compare its time on Perfmatters' screen. * * @param WP_Screen $screen The admin screen being loaded. */ public static function check_snippets( $screen ) { if ( ! $screen instanceof WP_Screen || ! in_array( $screen->id, array( 'settings_page_perfmatters', 'settings_page_perfmatters-network' ), true ) ) { return; } $uploads = wp_upload_dir( null, false ); $file = trailingslashit( $uploads['basedir'] ) . 'perfmatters/code-snippets/index.php'; $time = file_exists( $file ) ? (int) filemtime( $file ) : 0; $seen = get_option( 'gorilla_cache_snippets_time', null ); // Null: not recorded yet; 0: no snippets file. if ( null === $seen || $time !== (int) $seen ) { update_option( 'gorilla_cache_snippets_time', $time, false ); if ( null !== $seen ) { GorillaCache_Purge::all( 'Perfmatters snippets changed' ); } } } /** * Whether pages need a separate mobile copy: only when Perfmatters has a rule for one device (a preload, * a fetch priority or a Script Manager rule set to mobile or desktop). Almost every theme is responsive, * so one copy for all devices is the default. * * @return bool */ public static function needs_mobile_copy() { if ( ! defined( 'PERFMATTERS_VERSION' ) ) { return false; } $options = get_option( 'perfmatters_options', array() ); $preload = is_array( $options ) && isset( $options['preload'] ) ? $options['preload'] : array(); return self::has_device_rule( $preload ) || self::has_device_rule( get_option( 'perfmatters_script_manager', array() ) ); } /** * Whether Perfmatters' MU Mode unloads Gorilla Cache on some addresses (caching is then incomplete there). * * @return bool */ public static function unloaded_by_mu_mode() { $manager = get_option( 'perfmatters_script_manager', array() ); return defined( 'PERFMATTERS_VERSION' ) && is_array( $manager ) && ! empty( $manager['disabled']['plugins']['gorilla-cache'] ); } /** * Finds a non-empty "device" or "device_type" anywhere in a settings array. * * @param mixed $settings Settings. * @param int $depth Recursion depth. * @return bool */ private static function has_device_rule( $settings, $depth = 0 ) { if ( ! is_array( $settings ) || $depth > 6 ) { return false; } foreach ( $settings as $key => $value ) { if ( ( 'device' === $key || 'device_type' === $key ) && is_string( $value ) && '' !== $value ) { return true; } if ( is_array( $value ) && self::has_device_rule( $value, $depth + 1 ) ) { return true; } } return false; } }