| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin asset registration. |
| 4 |
* |
| 5 |
* @package TimVanIersel\RemoveSchema |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace TimVanIersel\RemoveSchema\Admin; |
| 11 |
|
| 12 |
use TimVanIersel\RemoveSchema\Contracts\Service; |
| 13 |
use TimVanIersel\RemoveSchema\Infrastructure\AssetLocator; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Registers and enqueues admin-facing scripts and styles. |
| 21 |
*/ |
| 22 |
final class AdminAssets implements Service { |
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
* |
| 26 |
* @param AssetLocator $assets Asset locator. |
| 27 |
*/ |
| 28 |
public function __construct( |
| 29 |
private readonly AssetLocator $assets |
| 30 |
) { |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register WordPress hooks for the service. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function register(): void { |
| 39 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Enqueue admin scripts and styles on the plugin settings page. |
| 44 |
* |
| 45 |
* @param string $hook_suffix Current admin page hook suffix. |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function enqueue( string $hook_suffix ): void { |
| 49 |
if ( 'settings_page_' . SettingsPage::pageSlug() !== $hook_suffix ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$script = $this->assets->script( 'admin/index.js' ); |
| 54 |
$style = $this->assets->style( 'admin/index.css' ); |
| 55 |
|
| 56 |
if ( null !== $script ) { |
| 57 |
wp_enqueue_script( |
| 58 |
'remove-schema-admin', |
| 59 |
$script['url'], |
| 60 |
$script['dependencies'], |
| 61 |
$script['version'], |
| 62 |
true |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
if ( null !== $style ) { |
| 67 |
wp_enqueue_style( |
| 68 |
'remove-schema-admin', |
| 69 |
$style['url'], |
| 70 |
array(), |
| 71 |
$style['version'] |
| 72 |
); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|