| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Admin; |
| 4 |
|
| 5 |
/** |
| 6 |
* Menu Class |
| 7 |
*/ |
| 8 |
class Menu { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'admin_menu', [ $this, 'register_menu' ] ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Register admin menu |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function register_menu() { |
| 23 |
$position = apply_filters( 'texty_menu_position', 58 ); |
| 24 |
|
| 25 |
$menu = add_menu_page( |
| 26 |
__( 'Texty', 'texty' ), |
| 27 |
__( 'Texty', 'texty' ), |
| 28 |
'manage_options', |
| 29 |
'texty', |
| 30 |
[ $this, 'render_page' ], |
| 31 |
'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>' ), |
| 32 |
$position |
| 33 |
); |
| 34 |
|
| 35 |
add_action( 'admin_print_scripts-' . $menu, [ $this, 'enqueue_scripts' ] ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Render the page |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function render_page() { |
| 44 |
echo '<div id="texty-app"></div>'; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Enqueue JS and CSS |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function enqueue_scripts() { |
| 53 |
$assets = [ |
| 54 |
'version' => TEXTY_VERSION, |
| 55 |
'dependencies' => [ |
| 56 |
'wp-api-fetch', |
| 57 |
'wp-i18n', |
| 58 |
], |
| 59 |
]; |
| 60 |
|
| 61 |
$url = TEXTY_URL . '/dist'; |
| 62 |
|
| 63 |
// for local development |
| 64 |
// when webpack "hot module replacement" is enabled, this |
| 65 |
// constant needs to be turned "true" on "wp-config.php" |
| 66 |
if ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) { |
| 67 |
$url = 'http://localhost:8080'; |
| 68 |
} |
| 69 |
|
| 70 |
// register scripts |
| 71 |
wp_register_script( 'texty-runtime', $url . '/runtime.js', $assets['dependencies'], $assets['version'], true ); |
| 72 |
wp_register_script( 'texty-vendors', $url . '/vendors.js', [ 'texty-runtime' ], $assets['version'], true ); |
| 73 |
wp_register_script( 'texty-admin', $url . '/app.js', [ 'texty-vendors' ], $assets['version'], true ); |
| 74 |
wp_localize_script( 'texty-admin', 'texty', $this->localize_script() ); |
| 75 |
|
| 76 |
// register styles |
| 77 |
wp_register_style( 'texty-vendors-css', $url . '/vendors.css', [ 'wp-components' ], $assets['version'] ); |
| 78 |
wp_register_style( 'texty-css', $url . '/app.css', [ 'texty-vendors-css' ], $assets['version'] ); |
| 79 |
|
| 80 |
// enqueue scripts and styles |
| 81 |
wp_enqueue_script( 'texty-admin' ); |
| 82 |
wp_enqueue_style( 'texty-css' ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get the localize script |
| 87 |
* |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
public function localize_script() { |
| 91 |
$i18n = []; |
| 92 |
|
| 93 |
return apply_filters( 'texty_localize_script', $i18n ); |
| 94 |
} |
| 95 |
} |
| 96 |
|