woocommerce
/
vendor
/
wordpress
/
abilities-api
/
includes
/
assets
/
class-wp-abilities-assets-init.php
class-wp-abilities-assets-init.php
96 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Assets initialization for Abilities API. |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage Abilities_API |
| 7 | * @since 0.1.0 |
| 8 | */ |
| 9 | |
| 10 | declare( strict_types = 1 ); |
| 11 | |
| 12 | /** |
| 13 | * Handles initialization of Abilities API client assets. |
| 14 | * |
| 15 | * @since 0.1.0 |
| 16 | */ |
| 17 | class WP_Abilities_Assets_Init { |
| 18 | |
| 19 | /** |
| 20 | * Registers the Abilities API JavaScript client. |
| 21 | * |
| 22 | * Auto-detects whether running as a plugin or Composer package and registers |
| 23 | * the client script accordingly. |
| 24 | * |
| 25 | * @since 0.1.0 |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public static function register_assets(): void { |
| 30 | if ( wp_script_is( 'wp-abilities', 'registered' ) ) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | $base_path = ''; |
| 35 | $base_url = ''; |
| 36 | |
| 37 | if ( defined( 'WP_ABILITIES_API_DIR' ) ) { |
| 38 | // Running as a plugin |
| 39 | $base_path = wp_normalize_path( (string) WP_ABILITIES_API_DIR ); |
| 40 | $base_url = plugins_url( '', dirname( __DIR__, 2 ) . '/abilities-api.php' ); |
| 41 | } else { |
| 42 | // Running as a Composer package |
| 43 | $base_path = dirname( __DIR__, 2 ); |
| 44 | |
| 45 | $base_path = wp_normalize_path( (string) $base_path ); |
| 46 | $plugin_dir = wp_normalize_path( (string) WP_PLUGIN_DIR ); |
| 47 | |
| 48 | // For Composer, we need to determine the URL based on the installation location |
| 49 | if ( 0 === strpos( $base_path, $plugin_dir ) ) { |
| 50 | // Inside a plugin directory |
| 51 | $relative_path = str_replace( $plugin_dir, '', $base_path ); |
| 52 | $base_url = plugins_url( $relative_path ); |
| 53 | } else { |
| 54 | // Assume standard Composer vendor structure |
| 55 | $base_url = plugins_url( 'vendor/wordpress/abilities-api', dirname( $base_path, 2 ) ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** @var string $base_path PHPStan type assertion - base_path is always set above */ |
| 60 | $client_path = trailingslashit( $base_path ) . 'packages/client/build/'; |
| 61 | |
| 62 | if ( ! file_exists( $client_path . 'index.js' ) ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable -- asset file path can be variable based on plugin or Composer install |
| 67 | $asset = require_once $client_path . 'index.asset.php'; |
| 68 | |
| 69 | $client_url = trailingslashit( $base_url ) . 'packages/client/build/index.js'; |
| 70 | |
| 71 | wp_register_script( |
| 72 | 'wp-abilities', |
| 73 | $client_url, |
| 74 | $asset['dependencies'], |
| 75 | $asset['version'], |
| 76 | true |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Auto-enqueue Abilities API client on admin pages. |
| 82 | * |
| 83 | * This is primarily for development and testing purposes. |
| 84 | * |
| 85 | * @since 0.1.0 |
| 86 | * @return void |
| 87 | */ |
| 88 | public static function admin_enqueue_scripts(): void { |
| 89 | if ( ! wp_script_is( 'wp-abilities', 'registered' ) ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | wp_enqueue_script( 'wp-abilities' ); |
| 94 | } |
| 95 | } |
| 96 |