foreach($files as $file) { if(is_file($file)) { unlink($file); } } delete_option('perfmatters_used_css_time'); } //clear used css file for specific post or post type public static function clear_post_used_css() { if(empty($_POST['action']) || empty($_POST['nonce']) || empty($_POST['post_id'])) { return; } if($_POST['action'] != 'perfmatters_clear_post_used_css') { return; } if(!wp_verify_nonce($_POST['nonce'], 'perfmatters_clear_post_used_css')) { return; } $post_id = (int)$_POST['post_id']; $post_type = get_post_type($post_id); $path = $post_type == 'page' ? 'page-' . $post_id : $post_type; $file = PERFMATTERS_CACHE_DIR . 'css/' . $path . '.used.css'; if(is_file($file)) { unlink($file); } wp_send_json_success(); exit; } //add admin bar menu item public static function admin_bar_menu(WP_Admin_Bar $wp_admin_bar) { if(!current_user_can('manage_options') || !perfmatters_network_access()) { return; } $type = !is_admin() ? self::get_url_type() : ''; $menu_item = array( 'parent' => 'perfmatters', 'id' => 'perfmatters-clear-used-css', 'title' => __('Clear Used CSS', 'perfmatters') . ' (' . (!empty($type) ? __('Current', 'perfmatters') : __('All', 'perfmatters')) . ')', 'href' => add_query_arg(array( 'action' => 'perfmatters_clear_used_css', '_wp_http_referer' => rawurlencode($_SERVER['REQUEST_URI']), '_wpnonce' => wp_create_nonce('perfmatters_clear_used_css'), 'type' => $type ), admin_url('admin-post.php')) ); $wp_admin_bar->add_menu($menu_item); } //display admin notices public static function admin_notices() { if(get_transient('perfmatters_used_css_cleared') === false) { return; } delete_transient('perfmatters_used_css_cleared'); echo '

' . __('Used CSS cleared.', 'perfmatters' ) . '

'; } //clear used css from admin bar public static function admin_bar_clear_used_css() { if(!isset($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_key($_GET['_wpnonce']), 'perfmatters_clear_used_css')) { wp_nonce_ays(''); } if(!empty($_GET['type'])) { //clear specific type $file = PERFMATTERS_CACHE_DIR . 'css/' . $_GET['type'] . '.used.css'; if(is_file($file)) { unlink($file); } } else { //clear all self::clear_used_css(); if(is_admin()) { set_transient('perfmatters_used_css_cleared', 1); } } //go back to url where button was pressed wp_safe_redirect(esc_url_raw(wp_get_referer())); exit; } }