| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our action that enables refreshing the site from the admin bar. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh; |
| 9 |
|
| 10 |
use JordanLeven\Plugins\ForceRefresh\Services\Options_Storage_Service; |
| 11 |
|
| 12 |
define( 'FILE_NAME_ADMIN_BAR', 'force-refresh-menu-bar' ); |
| 13 |
define( 'HTML_ID_REFRESH_FROM_MENUBAR', 'force-refresh__menu-bar' ); |
| 14 |
define( 'HTML_ID_REFRESH_NOTIFICATION_CONTAINER', 'force-refresh-notification-container' ); |
| 15 |
|
| 16 |
/** |
| 17 |
* Function to determine whether or not to include admin bar HTML. |
| 18 |
* |
| 19 |
* @return bool True if the admin bar HTML should be rendered. |
| 20 |
*/ |
| 21 |
function render_admin_bar_html(): bool { |
| 22 |
return user_can_request_force_refresh() && Options_Storage_Service::get_show_in_admin_bar(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Function to show the Force Refresh option in the WP Admin bar. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
function show_force_refresh_in_wp_admin_bar() { |
| 31 |
// Globalize the WP Admin Bar object. |
| 32 |
global $wp_admin_bar; |
| 33 |
|
| 34 |
// If the user isn't able to request a refresh, then stop eval. |
| 35 |
if ( ! render_admin_bar_html() || ! is_admin() ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
// Add the menu. |
| 40 |
$wp_admin_bar->add_menu( |
| 41 |
array( |
| 42 |
'id' => 'force-refresh', |
| 43 |
'title' => '<div id="' . HTML_ID_REFRESH_FROM_MENUBAR . '"></div>', |
| 44 |
'href' => null, |
| 45 |
) |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
add_action( |
| 50 |
'wp_before_admin_bar_render', |
| 51 |
__NAMESPACE__ . '\\show_force_refresh_in_wp_admin_bar', |
| 52 |
999 |
| 53 |
); |
| 54 |
|
| 55 |
add_action( |
| 56 |
'in_admin_header', |
| 57 |
function () { |
| 58 |
// If the user isn't able to request a refresh, then stop eval. |
| 59 |
if ( ! render_admin_bar_html() ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
echo '<div id="' . esc_html( HTML_ID_REFRESH_NOTIFICATION_CONTAINER ) . '"></div>'; |
| 64 |
} |
| 65 |
); |
| 66 |
|