configure_groups(); $path = urldecode( esc_url_raw( wp_unslash( $_GET['path'] ) ) ); if ( preg_match( '/\.{2,}/', $path ) ) { die( 'Suspected Directory Traversal Attack' ); } $url = get_home_url() . $path; $url = apply_filters( 'batcache_manager_link', $url ); if ( empty( $url ) ) return false; do_action( 'batcache_manager_before_flush', $url ); $url = set_url_scheme( $url, 'http' ); update_option( 'single-page-url-flushed', $url ); $url_key = md5( $url ); if ( is_object( $batcache ) ) { wp_cache_add( "{$url_key}_version", 0, $batcache->group ); wp_cache_incr( "{$url_key}_version", 1, $batcache->group ); } if ( property_exists( $wp_object_cache, 'no_remote_groups' ) ) { $k = array_search( $batcache->group, (array) $wp_object_cache->no_remote_groups ); if ( false !== $k ) { unset( $wp_object_cache->no_remote_groups[ $k ] ); wp_cache_set( "{$url_key}_version", $batcache->group ); $wp_object_cache->no_remote_groups[ $k ] = $batcache->group; } } do_action( 'batcache_manager_after_flush', $url ); update_option( 'flush-object-cache-for-single-page-time-stamp', gmdate( 'j M Y, g:ia' ) . ' UTC' ); wp_send_json_success( array( 'flushed' => 'batcache' ) ); } public function pcm_purge_current_page_edge_cache() { if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'pcm_nonce' ) ) { die( json_encode( array( 'Security Error!', 'error', 'alert' ) ) ); } $path = urldecode( esc_url_raw( wp_unslash( $_GET['path'] ) ) ); if ( preg_match( '/\.{2,}/', $path ) ) { die( 'Suspected Directory Traversal Attack' ); } $url = get_home_url() . $path; update_option( 'edge-cache-single-page-url-purged', $url ); if ( empty( $url ) ) return false; if ( class_exists( 'Edge_Cache_Plugin' ) ) { $edge_cache = Edge_Cache_Plugin::get_instance(); $result = $edge_cache->purge_uris_now( array( $url ) ); update_option( 'single-page-edge-cache-purge-time-stamp', gmdate( 'j M Y, g:ia' ) . ' UTC' ); wp_send_json_success( array( 'flushed' => 'edge-cache' ) ); } wp_send_json_error( array( 'reason' => 'Edge_Cache_Plugin not available' ) ); } public function load_toolbar_css() { wp_enqueue_style( 'pressable-cache-management-toolbar', plugin_dir_url( dirname( __FILE__ ) ) . 'public/css/toolbar.css', array(), time(), 'all' ); } public function load_toolbar_js() { wp_enqueue_script( 'pcm-toolbar', plugin_dir_url( dirname( __FILE__ ) ) . 'public/js/toolbar.js', array( 'jquery' ), time(), true ); // Pass nonce and edge-cache state to JS for BOTH admin and frontend contexts. // pcm_nonce from print_my_inline_script() only runs on wp_footer (frontend). // wp_localize_script covers both admin and frontend reliably. $edge_on = ( get_option('edge-cache-enabled') === 'enabled' ) ? '1' : '0'; wp_localize_script( 'pcm-toolbar', 'pcmToolbarData', array( 'nonce' => wp_create_nonce( 'pcm_nonce' ), 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'flushEdge'=> $edge_on, ) ); } public function load_remove_branding_toolbar_js() { wp_enqueue_script( 'pcm-toolbar-branding', plugin_dir_url( dirname( __FILE__ ) ) . 'public/js/toolbar_remove_branding.js', array( 'jquery' ), time(), true ); } public function print_my_inline_script() { ?> add_node( array( 'id' => $parent, 'title' => __( 'Flush Cache', 'pressable_cache_management' ), 'class' => 'pcm-toolbar-child', )); // Combined item — JS fires both Batcache + Edge Cache flushes in sequence $wp_admin_bar->add_menu( array( 'id' => 'pcm-toolbar-parent-remove-branding-flush-cache-of-this-page', 'title' => $flush_label, 'parent' => $parent, 'meta' => array( 'class' => 'pcm-toolbar-child' ), )); } else { $parent = 'pcm-toolbar-parent'; $wp_admin_bar->add_node( array( 'id' => $parent, 'title' => __( 'Flush Cache', 'pressable_cache_management' ), )); // Combined item — JS fires both Batcache + Edge Cache flushes in sequence $wp_admin_bar->add_menu( array( 'id' => 'pcm-toolbar-parent-flush-cache-of-this-page', 'title' => $flush_label, 'parent' => $parent, 'meta' => array( 'class' => 'pcm-toolbar-child' ), )); } } // Empty admin-side toolbar (handled by object_cache_admin_bar.php) public function PcmFlushCacheAdminbar() {} } } add_action( 'init', 'pcm_show_flush_cache_option_for_single_page' ); function pcm_show_flush_cache_option_for_single_page() { $current_user = wp_get_current_user(); if ( current_user_can('manage_woocommerce') || current_user_can('administrator') ) { $toolbar = new PcmFlushCacheAdminbar(); $toolbar->add(); } else { if ( ! function_exists('load_admin_toolbar_css') ) { function load_admin_toolbar_css() { wp_enqueue_style( 'pressable-cache-management-toolbar', plugin_dir_url( dirname( __FILE__ ) ) . 'public/css/toolbar.css', array(), time(), 'all' ); } } add_action( 'init', 'load_admin_toolbar_css' ); } } }