AbilitiesClient.php
64 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Abilities API Client (Namespaced Version) |
| 4 | * |
| 5 | * Simple interface for enabling WordPress Abilities API client scripts. |
| 6 | * This version uses WooCommerce's PSR-4 namespace structure. |
| 7 | * |
| 8 | * @package Automattic\WooCommerce\Internal\AbilitiesApi |
| 9 | * @version 10.4.0 |
| 10 | */ |
| 11 | |
| 12 | declare( strict_types=1 ); |
| 13 | |
| 14 | namespace Automattic\WooCommerce\Internal\AbilitiesApi; |
| 15 | |
| 16 | /** |
| 17 | * AbilitiesClient class. |
| 18 | */ |
| 19 | class AbilitiesClient { |
| 20 | |
| 21 | /** |
| 22 | * Whether the client has been enabled. |
| 23 | * |
| 24 | * @var bool |
| 25 | */ |
| 26 | private static bool $enabled = false; |
| 27 | |
| 28 | /** |
| 29 | * Enable the WordPress Abilities API client for admin pages. |
| 30 | * |
| 31 | * This is the main method external plugins should use to enable |
| 32 | * the abilities API JavaScript client. |
| 33 | * |
| 34 | * @return bool True if successfully enabled, false otherwise. |
| 35 | */ |
| 36 | public static function enable(): bool { |
| 37 | // Only enable once. |
| 38 | if ( self::$enabled ) { |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | // Hook into admin_enqueue_scripts to enqueue when needed. |
| 43 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_for_admin' ) ); |
| 44 | |
| 45 | self::$enabled = true; |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Internal method to handle script enqueueing. |
| 51 | */ |
| 52 | public static function enqueue_for_admin(): void { |
| 53 | // Only enqueue on admin pages. |
| 54 | if ( ! is_admin() ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // Enqueue the script if it's registered. |
| 59 | if ( wp_script_is( 'wp-abilities', 'registered' ) ) { |
| 60 | wp_enqueue_script( 'wp-abilities' ); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 |