| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sync (Dashboard Index) functionality |
| 4 |
* |
| 5 |
* @since 3.6.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Screen; |
| 10 |
|
| 11 |
use ElasticPress\Elasticsearch; |
| 12 |
use ElasticPress\Indexables; |
| 13 |
use ElasticPress\IndexHelper; |
| 14 |
use ElasticPress\REST; |
| 15 |
use ElasticPress\Screen; |
| 16 |
use ElasticPress\Stats; |
| 17 |
use ElasticPress\Utils; |
| 18 |
|
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; // Exit if accessed directly. |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Class Sync |
| 25 |
* |
| 26 |
* @since 3.6.0 |
| 27 |
* @package ElasticPress |
| 28 |
*/ |
| 29 |
class Sync { |
| 30 |
/** |
| 31 |
* Initialize class |
| 32 |
*/ |
| 33 |
public function setup() { |
| 34 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 35 |
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Enqueue script. |
| 40 |
* |
| 41 |
* @since 3.6.0 |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function admin_enqueue_scripts() { |
| 45 |
if ( 'sync' !== Screen::factory()->get_current_screen() ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
wp_enqueue_script( |
| 50 |
'ep_sync_scripts', |
| 51 |
EP_URL . 'dist/js/sync-script.js', |
| 52 |
Utils\get_asset_info( 'sync-script', 'dependencies' ), |
| 53 |
Utils\get_asset_info( 'sync-script', 'version' ), |
| 54 |
true |
| 55 |
); |
| 56 |
|
| 57 |
wp_set_script_translations( 'ep_sync_scripts', 'elasticpress' ); |
| 58 |
|
| 59 |
wp_enqueue_style( |
| 60 |
'ep_sync_style', |
| 61 |
EP_URL . 'dist/css/sync-script.css', |
| 62 |
[ 'wp-components', 'wp-edit-post' ], |
| 63 |
Utils\get_asset_info( 'sync-script', 'version' ) |
| 64 |
); |
| 65 |
|
| 66 |
$indexables = Indexables::factory()->get_all(); |
| 67 |
|
| 68 |
$indices_comparison = Elasticsearch::factory()->get_indices_comparison(); |
| 69 |
$indices_missing = count( $indices_comparison['missing_indices'] ) > 0; |
| 70 |
|
| 71 |
$post_types = Indexables::factory()->get( 'post' )->get_indexable_post_types(); |
| 72 |
$post_types = array_values( $post_types ); |
| 73 |
|
| 74 |
$sync_history = ! $indices_missing ? IndexHelper::factory()->get_sync_history() : []; |
| 75 |
|
| 76 |
$data = [ |
| 77 |
'apiUrl' => rest_url( 'elasticpress/v1/sync' ), |
| 78 |
'autoIndex' => Utils\isset_do_sync_parameter() && ( ! defined( 'EP_DASHBOARD_SYNC' ) || EP_DASHBOARD_SYNC ), |
| 79 |
'indexMeta' => Utils\get_indexing_status(), |
| 80 |
'indexables' => array_map( fn( $indexable ) => [ $indexable->slug, $indexable->labels['plural'] ], $indexables ), |
| 81 |
'isEpio' => Utils\is_epio(), |
| 82 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 83 |
'postTypes' => array_map( fn( $post_type ) => [ $post_type, get_post_type_object( $post_type )->labels->name ], $post_types ), |
| 84 |
'syncHistory' => $sync_history, |
| 85 |
'syncTrigger' => Utils\isset_do_sync_parameter() ? sanitize_text_field( wp_unslash( $_GET['do_sync'] ) ) : null, // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated |
| 86 |
]; |
| 87 |
|
| 88 |
wp_localize_script( 'ep_sync_scripts', 'epDash', $data ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Register REST API routes. |
| 93 |
* |
| 94 |
* @since 5.0.0 |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function register_rest_routes() { |
| 98 |
$controller = new REST\Sync(); |
| 99 |
$controller->register_routes(); |
| 100 |
} |
| 101 |
} |
| 102 |
|