| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Shared asset registration. |
| 9 |
* |
| 10 |
* Registers the bundles Texty shares with its add-ons on wp-admin, the |
| 11 |
* front-end and the login page: |
| 12 |
* |
| 13 |
* - `texty-plugin-ui` — the @wedevs/plugin-ui kit (`window.texty.pluginUi`). |
| 14 |
* - `texty-components` — Texty's reusable components (`window.texty.components`). |
| 15 |
* |
| 16 |
* Add-ons `import` from `@wedevs/plugin-ui` / `@texty/components` and let |
| 17 |
* @wordpress/dependency-extraction-webpack-plugin (see |
| 18 |
* webpack-dependency-mapping.js) rewrite the import to the global plus a |
| 19 |
* dependency on the script handle — so one copy is loaded no matter how many |
| 20 |
* add-ons use it, including on storefront surfaces (checkout, my-account) and |
| 21 |
* the wp-login.php form where the admin SPA never loads. |
| 22 |
* |
| 23 |
* Nothing is enqueued here; consumers pull a bundle in by declaring the handle |
| 24 |
* as a dependency (or calling wp_enqueue_script directly). |
| 25 |
*/ |
| 26 |
class Assets { |
| 27 |
|
| 28 |
/** |
| 29 |
* Wire the global registration on every request context an add-on may |
| 30 |
* enqueue from: wp-admin, the front-end, and wp-login.php. |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
add_action( 'admin_enqueue_scripts', [ $this, 'register_assets' ], 5 ); |
| 34 |
add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ], 5 ); |
| 35 |
add_action( 'login_enqueue_scripts', [ $this, 'register_assets' ], 5 ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Register every shared bundle. |
| 40 |
* |
| 41 |
* The plugin-ui bundle goes first — `texty-components` (and `texty-admin`) |
| 42 |
* list it as a dependency through their generated asset files. |
| 43 |
* |
| 44 |
* @since 2.0.2 |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function register_assets() { |
| 49 |
$this->register_plugin_ui(); |
| 50 |
$this->register_components(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Register the `texty-plugin-ui` handle. |
| 55 |
* |
| 56 |
* The bundle assigns the plugin-ui export surface to |
| 57 |
* `window.texty.pluginUi` and its stylesheet is registered under the same |
| 58 |
* handle. Texty's own admin bundle depends on it too, so the kit is loaded |
| 59 |
* exactly once per page. |
| 60 |
* |
| 61 |
* @since 2.0.2 |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function register_plugin_ui() { |
| 66 |
$this->register_bundle( 'texty-plugin-ui', 'plugin-ui', 'plugin-ui.css' ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Register the `texty-components` handle. |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function register_components() { |
| 75 |
$this->register_bundle( 'texty-components', 'components', 'style-components.css', [ 'texty-plugin-ui' ] ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Register one built entry as a script (and, when present, a stylesheet) |
| 80 |
* under a single handle. |
| 81 |
* |
| 82 |
* Script dependencies come from the entry's generated `*.asset.php`, which |
| 83 |
* already carries the `texty-*` handles for any shared package the entry |
| 84 |
* imports. |
| 85 |
* |
| 86 |
* @since 2.0.2 |
| 87 |
* |
| 88 |
* @param string $handle Script + style handle to register. |
| 89 |
* @param string $entry Webpack entry name (dist/<entry>.js). |
| 90 |
* @param string $style_file Stylesheet filename inside dist/, if any. |
| 91 |
* @param array $style_deps Style dependencies. |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
protected function register_bundle( $handle, $entry, $style_file, $style_deps = [] ) { |
| 96 |
$asset_path = TEXTY_DIR . '/dist/' . $entry . '.asset.php'; |
| 97 |
|
| 98 |
if ( ! file_exists( $asset_path ) ) { |
| 99 |
// Bailing quietly here is what makes a stale/partial dist/ look like |
| 100 |
// a working install: `texty-admin` declares `texty-plugin-ui` as a |
| 101 |
// dependency, WP drops the whole handle when it is not registered, |
| 102 |
// and the admin page renders empty with no error anywhere. |
| 103 |
error_log( 'Texty: missing build artifact ' . $asset_path . ' — run `npm run build`. The "' . $handle . '" handle was not registered.' ); |
| 104 |
|
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$asset = include $asset_path; |
| 109 |
$deps = isset( $asset['dependencies'] ) && is_array( $asset['dependencies'] ) ? $asset['dependencies'] : []; |
| 110 |
$version = isset( $asset['version'] ) ? $asset['version'] : TEXTY_VERSION; |
| 111 |
|
| 112 |
if ( ! wp_script_is( $handle, 'registered' ) ) { |
| 113 |
wp_register_script( |
| 114 |
$handle, |
| 115 |
TEXTY_URL . '/dist/' . $entry . '.js', |
| 116 |
$deps, |
| 117 |
$version, |
| 118 |
true |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
$style_path = TEXTY_DIR . '/dist/' . $style_file; |
| 123 |
|
| 124 |
if ( ! wp_style_is( $handle, 'registered' ) && file_exists( $style_path ) ) { |
| 125 |
wp_register_style( |
| 126 |
$handle, |
| 127 |
TEXTY_URL . '/dist/' . $style_file, |
| 128 |
$style_deps, |
| 129 |
$version |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|