| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Admin; |
| 4 |
|
| 5 |
/** |
| 6 |
* Menu Class |
| 7 |
*/ |
| 8 |
class Menu { |
| 9 |
|
| 10 |
/** |
| 11 |
* Class constructor |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
add_action( 'admin_menu', [ $this, 'register_menu' ] ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Add Texty admin menu |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function register_menu() { |
| 25 |
global $submenu; |
| 26 |
|
| 27 |
$capability = apply_filters( 'texty_admin_menu_capability', 'manage_options' ); |
| 28 |
if ( ! current_user_can( $capability ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$menu_position = apply_filters( 'texty_menu_position', 58 ); |
| 33 |
$slug = 'texty'; |
| 34 |
$menu_icon = 'data:image/svg+xml;base64,' . base64_encode( '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path fill="#a0a5aa" d="M10.52 1c.603 0 1.191.056 1.761.164a5.358 5.358 0 00-1.1 2.754 6.538 6.538 0 00-5.737 10.626l.629.773-.966 1.645h5.414a6.538 6.538 0 006.511-7.138 5.355 5.355 0 002.764-1.075 9.423 9.423 0 01-9.275 11.097H0l2.602-4.314-.013-.02A9.423 9.423 0 0110.521 1zm6.018 0a3.462 3.462 0 110 6.923 3.462 3.462 0 010-6.923z"/></svg>' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 35 |
|
| 36 |
$dashboard = add_menu_page( |
| 37 |
__( 'Texty', 'texty' ), |
| 38 |
__( 'Texty', 'texty' ), |
| 39 |
$capability, |
| 40 |
$slug, |
| 41 |
[ $this, 'render_page' ], |
| 42 |
$menu_icon, |
| 43 |
$menu_position |
| 44 |
); |
| 45 |
|
| 46 |
$submenus = apply_filters( |
| 47 |
'texty_admin_menu', |
| 48 |
[ |
| 49 |
[ |
| 50 |
'path' => 'dashboard', |
| 51 |
'title' => __( 'Dashboard', 'texty' ), |
| 52 |
], |
| 53 |
[ |
| 54 |
'path' => 'gateway', |
| 55 |
'title' => __( 'Gateway', 'texty' ), |
| 56 |
], |
| 57 |
[ |
| 58 |
'path' => 'notifications', |
| 59 |
'title' => __( 'Notifications', 'texty' ), |
| 60 |
], |
| 61 |
[ |
| 62 |
'path' => 'logs', |
| 63 |
'title' => __( 'Logs', 'texty' ), |
| 64 |
], |
| 65 |
], |
| 66 |
$capability, |
| 67 |
$slug |
| 68 |
); |
| 69 |
|
| 70 |
foreach ( $submenus as $item ) { |
| 71 |
$path = 'admin.php?page=' . $slug . '#/' . $item['path']; |
| 72 |
if ( filter_var( $item['path'], FILTER_VALIDATE_URL ) ) { |
| 73 |
$path = $item['path']; |
| 74 |
} |
| 75 |
$submenu[ $slug ][] = [ // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 76 |
$item['title'], |
| 77 |
$capability, |
| 78 |
$path, |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
do_action( 'texty_admin_menu', $capability, $menu_position ); |
| 83 |
|
| 84 |
add_action( 'admin_print_scripts-' . $dashboard, [ $this, 'enqueue_scripts' ] ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Render the page |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function render_page() { |
| 93 |
echo '<div id="texty-app" class="texty-app"></div>'; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Enqueue JS and CSS |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function enqueue_scripts() { |
| 102 |
$asset_file = include TEXTY_DIR . '/dist/index.asset.php'; |
| 103 |
|
| 104 |
wp_register_script( |
| 105 |
'texty-admin', |
| 106 |
TEXTY_URL . '/dist/index.js', |
| 107 |
$asset_file['dependencies'], |
| 108 |
$asset_file['version'], |
| 109 |
true |
| 110 |
); |
| 111 |
wp_localize_script( 'texty-admin', 'texty', $this->localize_script() ); |
| 112 |
|
| 113 |
wp_register_style( |
| 114 |
'texty-vendor-css', |
| 115 |
TEXTY_URL . '/dist/style-index.css', |
| 116 |
[ 'wp-components' ], |
| 117 |
$asset_file['version'] |
| 118 |
); |
| 119 |
|
| 120 |
wp_register_style( |
| 121 |
'texty-css', |
| 122 |
TEXTY_URL . '/dist/index.css', |
| 123 |
[ 'texty-vendor-css' ], |
| 124 |
$asset_file['version'] |
| 125 |
); |
| 126 |
|
| 127 |
wp_enqueue_script( 'texty-admin' ); |
| 128 |
wp_enqueue_style( 'texty-css' ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get the localize script |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
public function localize_script() { |
| 137 |
$i18n = [ |
| 138 |
'asset_url' => trailingslashit( TEXTY_URL ) . 'assets/', |
| 139 |
'site_name' => get_bloginfo( 'name' ), |
| 140 |
'rest_url' => esc_url_raw( rest_url() ), |
| 141 |
'ajax_url' => esc_url_raw( admin_url( 'admin-ajax.php' ) ), |
| 142 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 143 |
'version' => [ |
| 144 |
'lite' => TEXTY_VERSION, |
| 145 |
], |
| 146 |
]; |
| 147 |
|
| 148 |
return apply_filters( 'texty_localize_script', $i18n ); |
| 149 |
} |
| 150 |
} |
| 151 |
|