| 1 |
<?php |
| 2 |
/** |
| 3 |
* Powered Cache Development Mode Handler |
| 4 |
* |
| 5 |
* @package PoweredCache |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PoweredCache; |
| 9 |
|
| 10 |
use PoweredCache\Utils; |
| 11 |
use const PoweredCache\Constants\SETTING_OPTION; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Development Mode handler for Powered Cache. |
| 17 |
* Handles admin bar integration, UI display, and disabling logic |
| 18 |
* when development mode is active. |
| 19 |
* |
| 20 |
* @since 3.6 |
| 21 |
*/ |
| 22 |
class DevMode { |
| 23 |
|
| 24 |
/** |
| 25 |
* Initialize development mode behaviors. |
| 26 |
* |
| 27 |
* @return void |
| 28 |
* @since 3.6 |
| 29 |
*/ |
| 30 |
public static function setup() { |
| 31 |
add_action( 'admin_bar_menu', [ __CLASS__, 'add_admin_bar_item' ], 100 ); |
| 32 |
add_action( 'admin_head', [ __CLASS__, 'add_admin_bar_css' ] ); |
| 33 |
add_action( 'wp_head', [ __CLASS__, 'add_admin_bar_css' ] ); |
| 34 |
add_action( 'admin_post_powered_cache_exit_dev_mode', [ __CLASS__, 'handle_exit_dev_mode' ] ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Add admin bar button for disabling development mode. |
| 39 |
* Only shown to authorized users based on network mode. |
| 40 |
* |
| 41 |
* @param \WP_Admin_Bar $wp_admin_bar The admin bar object. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
* @since 3.6 |
| 45 |
*/ |
| 46 |
public static function add_admin_bar_item( $wp_admin_bar ) { |
| 47 |
if ( ! is_admin_bar_showing() ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$title = is_admin() ? esc_html__( 'Disable Dev Mode', 'powered-cache' ) : esc_html__( 'Powered Cache: Disable Dev Mode', 'powered-cache' ); |
| 56 |
|
| 57 |
$wp_admin_bar->add_node( |
| 58 |
[ |
| 59 |
'id' => 'powered-cache-exit-dev-mode', |
| 60 |
'title' => '🚧 ' . $title, |
| 61 |
'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_exit_dev_mode' ), 'powered_cache_exit_dev_mode' ), |
| 62 |
'meta' => [ 'class' => 'powered-cache-dev-mode-warning' ], |
| 63 |
] |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Inject custom styling into admin bar when dev mode is active. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
* @since 3.6 |
| 72 |
*/ |
| 73 |
public static function add_admin_bar_css() { |
| 74 |
if ( ! is_admin_bar_showing() ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
echo '<style> |
| 79 |
#wpadminbar #wp-admin-bar-powered-cache-exit-dev-mode > .ab-item { |
| 80 |
background-color: #f1c40f !important; |
| 81 |
color: #000 !important; |
| 82 |
font-weight: bold; |
| 83 |
} |
| 84 |
</style>'; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Handle the request to disable development mode. |
| 89 |
* Only authorized users can perform this action based on the context. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
* @since 3.6 |
| 93 |
*/ |
| 94 |
public static function handle_exit_dev_mode() { |
| 95 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 96 |
wp_die( esc_html__( 'Unauthorized request.', 'powered-cache' ) ); |
| 97 |
} |
| 98 |
|
| 99 |
if ( ! check_admin_referer( 'powered_cache_exit_dev_mode' ) ) { |
| 100 |
wp_die( esc_html__( 'Invalid nonce.', 'powered-cache' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
$settings = Utils\get_settings(); |
| 104 |
$settings['dev_mode'] = false; |
| 105 |
|
| 106 |
if ( POWERED_CACHE_IS_NETWORK ) { |
| 107 |
update_site_option( SETTING_OPTION, $settings ); |
| 108 |
} else { |
| 109 |
update_option( SETTING_OPTION, $settings ); |
| 110 |
} |
| 111 |
|
| 112 |
Config::factory()->save_configuration( $settings, POWERED_CACHE_IS_NETWORK ); |
| 113 |
|
| 114 |
$redirect_url = wp_get_referer() ? wp_get_referer() : admin_url( 'admin.php?page=powered-cache' ); |
| 115 |
wp_safe_redirect( $redirect_url ); |
| 116 |
exit; |
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|