| @@ -11,45 +11,63 @@ | ||
| 11 | 11 | declare (strict_types=1); |
| 12 | 12 | namespace WindPress\WindPress\Admin; |
| 13 | 13 | |
| 14 | 14 | use WIND_PRESS; |
| 15 | -use WindPress\WindPress\Utils\AssetVite; | |
| 15 | +use WindPress\WindPress\Core\Runtime; | |
| 16 | +use WindPress\WindPress\Utils\Vite; | |
| 16 | 17 | class AdminPage |
| 17 | 18 | { |
| 18 | 19 | public function __construct() |
| 19 | 20 | { |
| 20 | - \add_action('admin_menu', fn() => $this->add_admin_menu(), 1000001); | |
| 21 | + add_action('admin_menu', fn() => $this->add_admin_menu(), 1000001); | |
| 21 | 22 | } |
| 22 | - public static function get_page_url() : string | |
| 23 | + public static function get_page_url(): string | |
| 23 | 24 | { |
| 24 | - return \add_query_arg(['page' => WIND_PRESS::WP_OPTION], \admin_url('admin.php')); | |
| 25 | + return add_query_arg(['page' => WIND_PRESS::WP_OPTION], admin_url('admin.php')); | |
| 25 | 26 | } |
| 26 | 27 | public function add_admin_menu() |
| 27 | 28 | { |
| 28 | - $hook = \add_menu_page( | |
| 29 | - \__('WindPress', 'windpress'), | |
| 30 | - \__('WindPress', 'windpress'), | |
| 29 | + $hook = add_menu_page( | |
| 30 | + __('WindPress', 'windpress'), | |
| 31 | + __('WindPress', 'windpress'), | |
| 31 | 32 | 'manage_options', |
| 32 | 33 | WIND_PRESS::WP_OPTION, |
| 33 | 34 | fn() => $this->render(), |
| 34 | 35 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file |
| 35 | - 'data:image/svg+xml;base64,' . \base64_encode(\file_get_contents(\dirname(WIND_PRESS::FILE) . '/windpress.svg')), | |
| 36 | + 'data:image/svg+xml;base64,' . base64_encode(file_get_contents(dirname(WIND_PRESS::FILE) . '/windpress.svg')), | |
| 36 | 37 | 1000001 |
| 37 | 38 | ); |
| 38 | - \add_action('load-' . $hook, fn() => $this->init_hooks()); | |
| 39 | + add_action('load-' . $hook, fn() => $this->init_hooks()); | |
| 39 | 40 | } |
| 40 | 41 | private function render() |
| 41 | 42 | { |
| 43 | + do_action('a!windpress/admin/admin_page:render.before'); | |
| 42 | 44 | echo '<div id="windpress-app" class=""></div>'; |
| 45 | + do_action('a!windpress/admin/admin_page:render.after'); | |
| 43 | 46 | } |
| 44 | 47 | private function init_hooks() |
| 45 | 48 | { |
| 46 | - \add_action('admin_enqueue_scripts', fn() => $this->enqueue_scripts(), 1000001); | |
| 49 | + add_filter('f!windpress/core/runtime:print_windpress_metadata', fn($metadata) => $this->print_windpress_metadata($metadata), 1000001); | |
| 50 | + add_action('admin_enqueue_scripts', fn() => Runtime::get_instance()->print_windpress_metadata(), 1000001); | |
| 51 | + add_action('admin_enqueue_scripts', fn() => $this->enqueue_scripts(), 1000001); | |
| 47 | 52 | } |
| 48 | 53 | private function enqueue_scripts() |
| 49 | 54 | { |
| 50 | - $handle = WIND_PRESS::WP_OPTION . ':admin'; | |
| 51 | - AssetVite::get_instance()->enqueue_asset('assets/apps/dashboard/main.js', ['handle' => $handle, 'in_footer' => \true]); | |
| 52 | - \wp_set_script_translations($handle, 'windpress'); | |
| 53 | - \wp_localize_script($handle, 'windpress', ['_version' => WIND_PRESS::VERSION, '_wpnonce' => \wp_create_nonce(WIND_PRESS::WP_OPTION), 'rest_api' => ['nonce' => \wp_create_nonce('wp_rest'), 'root' => \esc_url_raw(\rest_url()), 'namespace' => WIND_PRESS::REST_NAMESPACE, 'url' => \esc_url_raw(\rest_url(WIND_PRESS::REST_NAMESPACE))], 'assets' => ['url' => AssetVite::asset_base_url()], 'site_meta' => ['name' => \get_bloginfo('name'), 'site_url' => \get_site_url()]]); | |
| 55 | + do_action('a!windpress/admin/admin_page:enqueue_scripts.before'); | |
| 56 | + $handle = WIND_PRESS::WP_OPTION . '-admin'; | |
| 57 | + $i18n_src = Vite::base_url('wp-i18n.js'); | |
| 58 | + if (!is_file(dirname(WIND_PRESS::FILE) . '/assets/dist/wp-i18n.js')) { | |
| 59 | + $i18n_src = plugins_url('resources/wp-i18n.js', WIND_PRESS::FILE); | |
| 60 | + } | |
| 61 | + wp_enqueue_script($handle . '-i18n', $i18n_src, ['wp-i18n'], null); | |
| 62 | + wp_set_script_translations($handle . '-i18n', 'windpress'); | |
| 63 | + Vite::assets()->enqueue('resources/dashboard/main.ts', ['handle' => $handle, 'in_footer' => \true, 'dependencies' => ['wp-hooks']]); | |
| 64 | + do_action('a!windpress/admin/admin_page:enqueue_scripts.after'); | |
| 65 | + } | |
| 66 | + function print_windpress_metadata($metadata) | |
| 67 | + { | |
| 68 | + $current_user = wp_get_current_user(); | |
| 69 | + $metadata['is_ubiquitous'] = \false; | |
| 70 | + $metadata['current_user'] = ['name' => $current_user->display_name, 'avatar' => get_avatar_url($current_user->ID), 'role' => $current_user->roles[0]]; | |
| 71 | + return $metadata; | |
| 54 | 72 | } |
| 55 | 73 | } |