"; exit; } // Start capturing the HTML output ob_start( 'ajo_end_caching' ); } // Save Output Buffer to file function ajo_end_caching( $buffer ) { // Do not cache very small outputs or errors if ( strlen( $buffer ) < 255 ) { return $buffer; } $cache_dir = ajo_get_cache_dir(); $cache_file = ajo_get_cache_filepath(); // Create cache directory if it doesn't exist if ( ! file_exists( $cache_dir ) ) { wp_mkdir_p( $cache_dir ); } // Save the file securely if ( is_writable( $cache_dir ) || is_writable( dirname( $cache_dir ) ) ) { file_put_contents( $cache_file, $buffer ); } return $buffer; } // Generate a unique file path for the current URL function ajo_get_cache_filepath() { $protocol = isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'; $url = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $hash = md5( $url ); return ajo_get_cache_dir() . $hash . '.html'; } /* ========================================================================== * 2. CACHE CLEARING LOGIC * ========================================================================== */ // The core function to empty the cache directory function ajo_clear_all_cache() { $cache_dir = ajo_get_cache_dir(); if ( ! is_dir( $cache_dir ) ) { return; } // Recursively delete files and folders inside the specific cache directory safely $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $cache_dir, RecursiveDirectoryIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST ); foreach ( $files as $fileinfo ) { $todo = ( $fileinfo->isDir() ? 'rmdir' : 'unlink' ); @$todo( $fileinfo->getRealPath() ); } } /* ========================================================================== * 3. ADMIN BAR BUTTON (One-Click Cleaner) * ========================================================================== */ // Add the button to the WordPress Top Admin Bar add_action( 'admin_bar_menu', 'ajo_add_admin_bar_button', 100 ); function ajo_add_admin_bar_button( $admin_bar ) { // Only show to administrators if ( ! current_user_can( 'manage_options' ) ) { return; } $nonce = wp_create_nonce( 'ajo_clear_cache_nonce' ); $url = admin_url( 'admin-post.php?action=ajo_clear_cache&_wpnonce=' . $nonce ); $admin_bar->add_node( [ 'id' => 'ajo-clear-cache', 'title' => 'AJO One-Click Cache Cleaner', 'href' => esc_url( $url ), 'meta' => [ 'class' => 'ajo-clear-cache-btn', 'title' => 'Clear all cached pages' ] ] ); } // Handle the click action add_action( 'admin_post_ajo_clear_cache', 'ajo_handle_clear_cache' ); function ajo_handle_clear_cache() { // Security checks: verify capability and nonce if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to perform this action.', 'ajo-one-click-cache-cleaner' ) ); } check_admin_referer( 'ajo_clear_cache_nonce' ); // Clear the cache ajo_clear_all_cache(); // Safely redirect back to where the user clicked the button $referer = wp_get_referer(); $redirect_url = $referer ? remove_query_arg( 'ajo_cleared', $referer ) : admin_url(); $redirect_url = add_query_arg( 'ajo_cleared', '1', $redirect_url ); wp_safe_redirect( esc_url_raw( $redirect_url ) ); exit; } // Show a success message in the admin panel after clearing add_action( 'admin_notices', 'ajo_admin_notice' ); function ajo_admin_notice() { if ( isset( $_GET['ajo_cleared'] ) && $_GET['ajo_cleared'] === '1' ) { echo '

' . esc_html__( 'All cache purged successfully!', 'ajo-one-click-cache-cleaner' ) . '

'; } } /* ========================================================================== * 4. AUTOMATIC CACHE CLEARING * ========================================================================== */ // Clear cache automatically when a post/page is created or updated add_action( 'save_post', 'ajo_auto_clear_cache_on_save', 10, 3 ); function ajo_auto_clear_cache_on_save( $post_id, $post, $update ) { // Ignore autosaves, revisions, and bulk edits to prevent performance hits if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Check if the user has permission to edit the post if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Fire the purge function ajo_clear_all_cache(); }