| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Admin; |
| 13 |
|
| 14 |
use WIND_PRESS; |
| 15 |
use WindPress\WindPress\Core\Runtime; |
| 16 |
use WindPress\WindPress\Utils\Vite; |
| 17 |
class AdminPage |
| 18 |
{ |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
add_action('admin_menu', fn() => $this->add_admin_menu(), 1000001); |
| 22 |
} |
| 23 |
public static function get_page_url(): string |
| 24 |
{ |
| 25 |
return add_query_arg(['page' => WIND_PRESS::WP_OPTION], admin_url('admin.php')); |
| 26 |
} |
| 27 |
public function add_admin_menu() |
| 28 |
{ |
| 29 |
$hook = add_menu_page( |
| 30 |
__('WindPress', 'windpress'), |
| 31 |
__('WindPress', 'windpress'), |
| 32 |
'manage_options', |
| 33 |
WIND_PRESS::WP_OPTION, |
| 34 |
fn() => $this->render(), |
| 35 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file |
| 36 |
'data:image/svg+xml;base64,' . base64_encode(file_get_contents(dirname(WIND_PRESS::FILE) . '/windpress.svg')), |
| 37 |
1000001 |
| 38 |
); |
| 39 |
add_action('load-' . $hook, fn() => $this->init_hooks()); |
| 40 |
} |
| 41 |
private function render() |
| 42 |
{ |
| 43 |
do_action('a!windpress/admin/admin_page:render.before'); |
| 44 |
echo '<div id="windpress-app" class=""></div>'; |
| 45 |
do_action('a!windpress/admin/admin_page:render.after'); |
| 46 |
} |
| 47 |
private function init_hooks() |
| 48 |
{ |
| 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); |
| 52 |
} |
| 53 |
private function enqueue_scripts() |
| 54 |
{ |
| 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; |
| 72 |
} |
| 73 |
} |
| 74 |
|