| 1 |
<?php |
| 2 |
|
| 3 |
namespace Ens\Assets; |
| 4 |
|
| 5 |
use Ens\Config; |
| 6 |
use Ens\Utils\Helpers; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Enqueue |
| 10 |
* |
| 11 |
* @package ENS |
| 12 |
* |
| 13 |
* @since 1.0.0 |
| 14 |
*/ |
| 15 |
class Enqueue { |
| 16 |
|
| 17 |
/** |
| 18 |
* Config |
| 19 |
* |
| 20 |
* @var Config |
| 21 |
*/ |
| 22 |
private $identifier; |
| 23 |
|
| 24 |
/** |
| 25 |
* Initializing enqueue assets |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function init($identifier) { |
| 32 |
$this->identifier = $identifier; |
| 33 |
add_action( 'admin_enqueue_scripts', [$this, 'enqueue_assets'],999 ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Enqueue admin scripts and pass localized data |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function enqueue_assets() { |
| 44 |
$admin_script_handler = Helpers::get_config_data( $this->identifier,'admin_script_handler' ); |
| 45 |
$plugin_slug = Helpers::get_config_data( $this->identifier,'plugin_slug' ); |
| 46 |
|
| 47 |
$general_prefix = Helpers::get_config_data( $this->identifier,'general_prefix' ); |
| 48 |
|
| 49 |
wp_localize_script( $admin_script_handler, $general_prefix . '_ens_data', [ |
| 50 |
// Must stay the 'wp_rest' action: this value is sent as the |
| 51 |
// X-WP-Nonce header, and WordPress core rejects the whole request |
| 52 |
// with rest_cookie_invalid_nonce if that header is anything else. |
| 53 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 54 |
'api_version' => 'v1', |
| 55 |
'plugin_slug' => $plugin_slug, |
| 56 |
'base_url' => site_url(), |
| 57 |
'triggers' => apply_filters( 'ens_'.$this->identifier.'_available_actions', [] ) |
| 58 |
] ); |
| 59 |
} |
| 60 |
} |
| 61 |
|