| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dashboard screen class. |
| 4 |
* |
| 5 |
* @since 5.0.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Screen; |
| 10 |
|
| 11 |
use ElasticPress\Features as FeaturesStore; |
| 12 |
use ElasticPress\REST; |
| 13 |
use ElasticPress\Screen; |
| 14 |
use ElasticPress\Utils; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; // Exit if accessed directly. |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Dashboard screen. |
| 22 |
* |
| 23 |
* @since 5.0.0 |
| 24 |
* @package ElasticPress |
| 25 |
*/ |
| 26 |
class Features { |
| 27 |
/** |
| 28 |
* Initialize class |
| 29 |
*/ |
| 30 |
public function setup() { |
| 31 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 32 |
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Enqueue script. |
| 37 |
* |
| 38 |
* @since 3.6.0 |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function admin_enqueue_scripts() { |
| 42 |
if ( 'dashboard' !== Screen::factory()->get_current_screen() ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
wp_enqueue_script( |
| 47 |
'ep_features_script', |
| 48 |
EP_URL . 'dist/js/features-script.js', |
| 49 |
Utils\get_asset_info( 'features-script', 'dependencies' ), |
| 50 |
Utils\get_asset_info( 'features-script', 'version' ), |
| 51 |
true |
| 52 |
); |
| 53 |
|
| 54 |
wp_set_script_translations( 'ep_features_script', 'elasticpress' ); |
| 55 |
|
| 56 |
wp_enqueue_style( |
| 57 |
'ep_features_script', |
| 58 |
EP_URL . 'dist/css/features-script.css', |
| 59 |
[ 'wp-components', 'wp-edit-post' ], |
| 60 |
Utils\get_asset_info( 'features-script', 'version' ) |
| 61 |
); |
| 62 |
|
| 63 |
$store = FeaturesStore::factory(); |
| 64 |
|
| 65 |
$features = $store->registered_features; |
| 66 |
$features = array_map( fn( $f ) => $f->get_json(), $features ); |
| 67 |
$features = array_values( $features ); |
| 68 |
|
| 69 |
$sync_url = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ? |
| 70 |
network_admin_url( 'admin.php?page=elasticpress-sync' ) : |
| 71 |
admin_url( 'admin.php?page=elasticpress-sync' ); |
| 72 |
|
| 73 |
$data = [ |
| 74 |
'apiUrl' => rest_url( 'elasticpress/v1/features' ), |
| 75 |
'epioLogoUrl' => esc_url( plugins_url( '/images/logo-elasticpress-io.svg', EP_FILE ) ), |
| 76 |
'features' => $features, |
| 77 |
'indexMeta' => Utils\get_indexing_status(), |
| 78 |
'settings' => $store->get_feature_settings(), |
| 79 |
'settingsDraft' => $store->get_feature_settings_draft(), |
| 80 |
'syncUrl' => $sync_url, |
| 81 |
'syncNonce' => wp_create_nonce( 'ep_sync_nonce' ), |
| 82 |
'featureGroups' => $store->get_feature_groups(), |
| 83 |
]; |
| 84 |
|
| 85 |
wp_localize_script( 'ep_features_script', 'epDashboard', $data ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Register REST API routes. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function register_rest_routes() { |
| 94 |
$controller = new REST\Features(); |
| 95 |
$controller->register_routes(); |
| 96 |
} |
| 97 |
} |
| 98 |
|