| @@ -7,11 +7,9 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | class Menu { |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | - * Class constructor | |
| 12 | - * | |
| 13 | - * @return void | |
| 11 | + * Initialize | |
| 14 | 12 | */ |
| 15 | 13 | public function __construct() { |
| 16 | 14 | add_action( 'admin_menu', [ $this, 'register_menu' ] ); |
| 17 | 15 | } |
| @@ -16,73 +14,26 @@ | ||
| 16 | 14 | add_action( 'admin_menu', [ $this, 'register_menu' ] ); |
| 17 | 15 | } |
| 18 | 16 | |
| 19 | 17 | /** |
| 20 | - * Add Texty admin menu | |
| 18 | + * Register admin menu | |
| 21 | 19 | * |
| 22 | 20 | * @return void |
| 23 | 21 | */ |
| 24 | 22 | public function register_menu() { |
| 25 | - global $submenu; | |
| 23 | + $position = apply_filters( 'texty_menu_position', 58 ); | |
| 26 | 24 | |
| 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( | |
| 25 | + $menu = add_menu_page( | |
| 37 | 26 | __( 'Texty', 'texty' ), |
| 38 | 27 | __( 'Texty', 'texty' ), |
| 39 | - $capability, | |
| 40 | - $slug, | |
| 28 | + 'manage_options', | |
| 29 | + 'texty', | |
| 41 | 30 | [ $this, 'render_page' ], |
| 42 | - $menu_icon, | |
| 43 | - $menu_position | |
| 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>' ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | |
| 32 | + $position | |
| 44 | 33 | ); |
| 45 | 34 | |
| 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' ] ); | |
| 35 | + add_action( 'admin_print_scripts-' . $menu, [ $this, 'enqueue_scripts' ] ); | |
| 85 | 36 | } |
| 86 | 37 | |
| 87 | 38 | /** |
| 88 | 39 | * Render the page |
| @@ -89,9 +40,9 @@ | ||
| 89 | 40 | * |
| 90 | 41 | * @return void |
| 91 | 42 | */ |
| 92 | 43 | public function render_page() { |
| 93 | - echo '<div id="texty-app" class="texty-app"></div>'; | |
| 44 | + echo '<div id="texty-app"></div>'; | |
| 94 | 45 | } |
| 95 | 46 | |
| 96 | 47 | /** |
| 97 | 48 | * Enqueue JS and CSS |
| @@ -98,33 +49,36 @@ | ||
| 98 | 49 | * |
| 99 | 50 | * @return void |
| 100 | 51 | */ |
| 101 | 52 | public function enqueue_scripts() { |
| 102 | - $asset_file = include TEXTY_DIR . '/dist/index.asset.php'; | |
| 53 | + $assets = [ | |
| 54 | + 'version' => TEXTY_VERSION, | |
| 55 | + 'dependencies' => [ | |
| 56 | + 'wp-api-fetch', | |
| 57 | + 'wp-i18n', | |
| 58 | + ], | |
| 59 | + ]; | |
| 103 | 60 | |
| 104 | - wp_register_script( | |
| 105 | - 'texty-admin', | |
| 106 | - TEXTY_URL . '/dist/index.js', | |
| 107 | - $asset_file['dependencies'], | |
| 108 | - $asset_file['version'], | |
| 109 | - true | |
| 110 | - ); | |
| 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 ); | |
| 111 | 74 | wp_localize_script( 'texty-admin', 'texty', $this->localize_script() ); |
| 112 | 75 | |
| 113 | - wp_register_style( | |
| 114 | - 'texty-vendor-css', | |
| 115 | - TEXTY_URL . '/dist/style-index.css', | |
| 116 | - [ 'wp-components' ], | |
| 117 | - $asset_file['version'] | |
| 118 | - ); | |
| 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'] ); | |
| 119 | 79 | |
| 120 | - wp_register_style( | |
| 121 | - 'texty-css', | |
| 122 | - TEXTY_URL . '/dist/index.css', | |
| 123 | - [ 'texty-vendor-css' ], | |
| 124 | - $asset_file['version'] | |
| 125 | - ); | |
| 126 | - | |
| 80 | + // enqueue scripts and styles | |
| 127 | 81 | wp_enqueue_script( 'texty-admin' ); |
| 128 | 82 | wp_enqueue_style( 'texty-css' ); |
| 129 | 83 | } |
| 130 | 84 | |
| @@ -133,18 +87,9 @@ | ||
| 133 | 87 | * |
| 134 | 88 | * @return array |
| 135 | 89 | */ |
| 136 | 90 | 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 | - ]; | |
| 91 | + $i18n = []; | |
| 147 | 92 | |
| 148 | 93 | return apply_filters( 'texty_localize_script', $i18n ); |
| 149 | 94 | } |
| 150 | 95 | } |