| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin menu + asset enqueue. |
| 4 |
* |
| 5 |
* @package XSpeed |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace XSpeed; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
class Admin { |
| 13 |
|
| 14 |
const PAGE_SLUG = 'xspeed'; |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
add_action( 'admin_menu', array( $this, 'register_menu' ) ); |
| 18 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); |
| 19 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_menu_styles' ) ); |
| 20 |
} |
| 21 |
|
| 22 |
public function register_menu() { |
| 23 |
add_menu_page( |
| 24 |
__( 'xSpeed', 'xspeed' ), |
| 25 |
__( 'xSpeed', 'xspeed' ), |
| 26 |
'manage_options', |
| 27 |
self::PAGE_SLUG, |
| 28 |
array( $this, 'render' ), |
| 29 |
self::menu_icon(), |
| 30 |
80 |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* URL of the SVG menu icon — the official xSpeed brand mark. Uses |
| 36 |
* fill="currentColor" which renders black in <img> context; the inline |
| 37 |
* style below recolors it via CSS filter for the WP admin menu states. |
| 38 |
*/ |
| 39 |
private static function menu_icon() { |
| 40 |
return XSPEED_URL . 'assets/icon.svg'; |
| 41 |
} |
| 42 |
|
| 43 |
public function render() { |
| 44 |
echo '<div id="xspeed-app" class="xspeed-root"></div>'; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Enqueue the stylesheet that recolors the menu icon to match the WP |
| 49 |
* admin color scheme. Loads on every admin page (not just the plugin's |
| 50 |
* page) because the menu icon is visible site-wide. |
| 51 |
*/ |
| 52 |
public function enqueue_menu_styles() { |
| 53 |
wp_enqueue_style( |
| 54 |
'xspeed-menu-icon', |
| 55 |
XSPEED_URL . 'assets/menu-icon.css', |
| 56 |
array(), |
| 57 |
XSPEED_VERSION |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public function enqueue( $hook ) { |
| 62 |
if ( 'toplevel_page_' . self::PAGE_SLUG !== $hook ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$asset_js = XSPEED_DIR . 'assets/admin.js'; |
| 67 |
$asset_css = XSPEED_DIR . 'assets/admin.css'; |
| 68 |
|
| 69 |
if ( file_exists( $asset_js ) ) { |
| 70 |
wp_enqueue_script( |
| 71 |
'xspeed-admin', |
| 72 |
XSPEED_URL . 'assets/admin.js', |
| 73 |
array( 'wp-api-fetch' ), |
| 74 |
XSPEED_VERSION, |
| 75 |
true |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
if ( file_exists( $asset_css ) ) { |
| 80 |
wp_enqueue_style( |
| 81 |
'xspeed-admin', |
| 82 |
XSPEED_URL . 'assets/admin.css', |
| 83 |
array(), |
| 84 |
XSPEED_VERSION |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
wp_localize_script( |
| 89 |
'xspeed-admin', |
| 90 |
'XSpeedConfig', |
| 91 |
array( |
| 92 |
'restUrl' => esc_url_raw( rest_url( Rest_Api::NAMESPACE_V1 ) ), |
| 93 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 94 |
'version' => XSPEED_VERSION, |
| 95 |
'bootstrap' => self::bootstrap_payload(), |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Pre-rendered settings + status payload, baked into the page so the |
| 102 |
* React app can mount with real values instead of showing a loading state |
| 103 |
* while it waits for /settings and /status REST calls. |
| 104 |
*/ |
| 105 |
private static function bootstrap_payload() { |
| 106 |
$opts = Settings::get(); |
| 107 |
$stats = Cache::get_stats(); |
| 108 |
return array( |
| 109 |
'settings' => $opts, |
| 110 |
'status' => array( |
| 111 |
'enabled' => (bool) $opts['cache_enabled'], |
| 112 |
'stats' => $stats, |
| 113 |
'server' => array( |
| 114 |
'type' => Server::type(), |
| 115 |
'gzip_mode' => Server::gzip_mode(), |
| 116 |
'gzip_active' => Gzip::probe_active(), |
| 117 |
'nginx_snippet' => Gzip::nginx_snippet(), |
| 118 |
), |
| 119 |
), |
| 120 |
); |
| 121 |
} |
| 122 |
} |
| 123 |
|