| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles all functionality related to the Admin Bar. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Admin; |
| 11 |
|
| 12 |
use SolidWP\Performance\Http\Request; |
| 13 |
use WP_Admin_Bar; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Handles all functionality related to the Admin Bar. |
| 21 |
* |
| 22 |
* @since 0.1.0 |
| 23 |
* |
| 24 |
* @package SolidWP\Performance |
| 25 |
*/ |
| 26 |
class Admin_Bar { |
| 27 |
|
| 28 |
public const MENU_ID = 'swpsp_cache'; |
| 29 |
public const PURGE_CACHE_ID = 'swpsp_cache_purge'; |
| 30 |
public const PURGE_CACHE_CURRENT_ID = 'swpsp_cache_purge_current'; |
| 31 |
public const NONCE_ACTION = 'swpsp_purge_page_cache'; |
| 32 |
public const ID = 'swpsp_id'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Adds a new menu item to the admin bar for purging cache. |
| 36 |
* |
| 37 |
* @since 0.1.0 |
| 38 |
* |
| 39 |
* @action admin_bar_menu |
| 40 |
* |
| 41 |
* @param WP_Admin_Bar $admin_bar An instance of the admin bar to adjust. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function add_purge_admin_bar_link( WP_Admin_Bar $admin_bar ): void { |
| 46 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$admin_bar->add_menu( |
| 51 |
[ |
| 52 |
'id' => self::MENU_ID, |
| 53 |
'parent' => null, |
| 54 |
'title' => esc_html__( 'Kadence Performance', 'solid-performance' ), |
| 55 |
] |
| 56 |
); |
| 57 |
|
| 58 |
$admin_bar->add_menu( |
| 59 |
[ |
| 60 |
'id' => self::PURGE_CACHE_ID, |
| 61 |
'parent' => self::MENU_ID, |
| 62 |
'title' => esc_html__( 'Clear Page Cache', 'solid-performance' ), |
| 63 |
'href' => add_query_arg( |
| 64 |
[ |
| 65 |
self::PURGE_CACHE_ID => true, |
| 66 |
'_wpnonce' => wp_create_nonce( self::NONCE_ACTION ), |
| 67 |
] |
| 68 |
), |
| 69 |
] |
| 70 |
); |
| 71 |
|
| 72 |
if ( ! is_admin() && ! wp_doing_ajax() && ! is_404() ) { |
| 73 |
$request = new Request(); |
| 74 |
|
| 75 |
$admin_bar->add_menu( |
| 76 |
[ |
| 77 |
'id' => self::PURGE_CACHE_CURRENT_ID, |
| 78 |
'parent' => self::MENU_ID, |
| 79 |
'title' => esc_html__( 'Clear Current Page', 'solid-performance' ), |
| 80 |
'href' => add_query_arg( |
| 81 |
[ |
| 82 |
self::PURGE_CACHE_CURRENT_ID => true, |
| 83 |
self::ID => rawurlencode( base64_encode( strtok( $request->uri, '?' ) ) ), |
| 84 |
'_wpnonce' => wp_create_nonce( self::NONCE_ACTION ), |
| 85 |
] |
| 86 |
), |
| 87 |
] |
| 88 |
); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|