PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
← All changes | includes/classes/Screen/Sync.php +30 -182 4.6.15.3.5 View file →
@@ -7,13 +7,15 @@
7 7 */
8 8
9 9 namespace ElasticPress\Screen;
10 10
11 +use ElasticPress\Elasticsearch;
12 +use ElasticPress\Indexables;
11 13 use ElasticPress\IndexHelper;
14 +use ElasticPress\REST;
12 15 use ElasticPress\Screen;
16 +use ElasticPress\Stats;
13 17 use ElasticPress\Utils;
14 -use ElasticPress\Stats;
15 -use ElasticPress\Elasticsearch;
16 18
17 19 if ( ! defined( 'ABSPATH' ) ) {
18 20 exit; // Exit if accessed directly.
19 21 }
@@ -28,105 +30,13 @@
28 30 /**
29 31 * Initialize class
30 32 */
31 33 public function setup() {
32 - add_action( 'wp_ajax_ep_index', [ $this, 'action_wp_ajax_ep_index' ] );
33 - add_action( 'wp_ajax_ep_index_status', [ $this, 'action_wp_ajax_ep_index_status' ] );
34 - add_action( 'wp_ajax_ep_cancel_index', [ $this, 'action_wp_ajax_ep_cancel_index' ] );
35 -
36 34 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
35 + add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
37 36 }
38 37
39 38 /**
40 - * Getting the status of ongoing index fired by WP CLI
41 - *
42 - * @since 3.6.0
43 - */
44 - public function action_wp_ajax_ep_index_status() {
45 - if ( ! check_ajax_referer( 'ep_dashboard_nonce', 'nonce', false ) || ! EP_DASHBOARD_SYNC ) {
46 - wp_send_json_error( null, 403 );
47 - exit;
48 - }
49 -
50 - $index_meta = Utils\get_indexing_status();
51 -
52 - if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) {
53 - wp_send_json_success(
54 - [
55 - 'message' => sprintf(
56 - /* translators: 1. Number of objects indexed, 2. Total number of objects, 3. Last object ID */
57 - esc_html__( 'Processed %1$d/%2$d. Last Object ID: %3$d', 'elasticpress' ),
58 - $index_meta['offset'],
59 - $index_meta['found_items'],
60 - $index_meta['current_sync_item']['last_processed_object_id']
61 - ),
62 - 'index_meta' => $index_meta,
63 - ]
64 - );
65 - }
66 -
67 - wp_send_json_success(
68 - [
69 - 'is_finished' => true,
70 - 'totals' => Utils\get_option( 'ep_last_index' ),
71 - ]
72 - );
73 - }
74 -
75 - /**
76 - * Perform index
77 - *
78 - * @since 3.6.0
79 - */
80 - public function action_wp_ajax_ep_index() {
81 - if ( ! check_ajax_referer( 'ep_dashboard_nonce', 'nonce', false ) || ! EP_DASHBOARD_SYNC ) {
82 - wp_send_json_error( null, 403 );
83 - exit;
84 - }
85 -
86 - $index_meta = Utils\get_indexing_status();
87 -
88 - if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) {
89 - $this->action_wp_ajax_ep_index_status();
90 - exit;
91 - }
92 -
93 - IndexHelper::factory()->full_index(
94 - [
95 - 'method' => 'dashboard',
96 - 'put_mapping' => ! empty( $_REQUEST['put_mapping'] ),
97 - 'output_method' => [ $this, 'index_output' ],
98 - 'show_errors' => true,
99 - 'network_wide' => 0,
100 - ]
101 - );
102 - }
103 -
104 - /**
105 - * Cancel index
106 - *
107 - * @since 3.6.0
108 - */
109 - public function action_wp_ajax_ep_cancel_index() {
110 - if ( ! check_ajax_referer( 'ep_dashboard_nonce', 'nonce', false ) || ! EP_DASHBOARD_SYNC ) {
111 - wp_send_json_error( null, 403 );
112 - exit;
113 - }
114 -
115 - $index_meta = Utils\get_indexing_status();
116 -
117 - if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) {
118 - set_transient( 'ep_wpcli_sync_interrupted', true, MINUTE_IN_SECONDS );
119 - wp_send_json_success();
120 - exit;
121 - }
122 -
123 - Utils\delete_option( 'ep_index_meta' );
124 -
125 - wp_send_json_success();
126 - }
127 -
128 - /**
129 39 * Enqueue script.
130 40 *
131 41 * @since 3.6.0
132 42 * @return void
@@ -145,109 +55,47 @@
145 55 );
146 56
147 57 wp_set_script_translations( 'ep_sync_scripts', 'elasticpress' );
148 58
149 - wp_enqueue_style( 'wp-components' );
150 - wp_enqueue_style( 'wp-edit-post' );
151 -
152 59 wp_enqueue_style(
153 60 'ep_sync_style',
154 - EP_URL . 'dist/css/sync-styles.css',
155 - Utils\get_asset_info( 'sync-styles', 'dependencies' ),
156 - Utils\get_asset_info( 'sync-styles', 'version' )
61 + EP_URL . 'dist/css/sync-script.css',
62 + [ 'wp-components', 'wp-edit-post' ],
63 + Utils\get_asset_info( 'sync-script', 'version' )
157 64 );
158 65
159 - $data = array( 'nonce' => wp_create_nonce( 'ep_dashboard_nonce' ) );
160 - $index_meta = Utils\get_indexing_status();
161 - $last_sync = Utils\get_option( 'ep_last_sync', false );
66 + $indexables = Indexables::factory()->get_all();
162 67
163 - if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
164 - $install_complete_url = admin_url( 'network/admin.php?page=elasticpress&install_complete' );
165 - } else {
166 - $install_complete_url = admin_url( 'admin.php?page=elasticpress&install_complete' );
167 - }
168 -
169 - if ( isset( $_GET['do_sync'] ) && ( ! defined( 'EP_DASHBOARD_SYNC' ) || EP_DASHBOARD_SYNC ) ) { // phpcs:ignore WordPress.Security.NonceVerification
170 - $data['auto_start_index'] = true;
171 - }
172 -
173 - if ( ! empty( $index_meta ) ) {
174 - $data['index_meta'] = $index_meta;
175 - }
176 -
177 - $ep_last_index = IndexHelper::factory()->get_last_index();
178 -
179 68 $indices_comparison = Elasticsearch::factory()->get_indices_comparison();
180 - $sync_required = count( $indices_comparison['missing_indices'] ) > 0;
69 + $indices_missing = count( $indices_comparison['missing_indices'] ) > 0;
181 70
182 - if ( ! empty( $ep_last_index ) && ! $sync_required ) {
183 - $data['ep_last_sync_date'] = ! empty( $ep_last_index['end_date_time'] ) ? $ep_last_index['end_date_time'] : false;
184 - $data['ep_last_sync_failed'] = ! empty( $ep_last_index['failed'] ) || ! empty( $ep_last_index['errors'] ) ? true : false;
185 - }
71 + $post_types = Indexables::factory()->get( 'post' )->get_indexable_post_types();
72 + $post_types = array_values( $post_types );
186 73
187 - /**
188 - * Filter indexable labels used in dashboard sync UI
189 - *
190 - * @since 3.0
191 - * @hook ep_dashboard_indexable_labels
192 - * @param {array} $labels Current indexable lables
193 - * @return {array} New labels
194 - */
195 - $data['sync_indexable_labels'] = apply_filters(
196 - 'ep_dashboard_indexable_labels',
197 - [
198 - 'post' => [
199 - 'singular' => esc_html__( 'Post', 'elasticpress' ),
200 - 'plural' => esc_html__( 'Posts', 'elasticpress' ),
201 - ],
202 - 'term' => [
203 - 'singular' => esc_html__( 'Term', 'elasticpress' ),
204 - 'plural' => esc_html__( 'Terms', 'elasticpress' ),
205 - ],
206 - 'user' => [
207 - 'singular' => esc_html__( 'User', 'elasticpress' ),
208 - 'plural' => esc_html__( 'Users', 'elasticpress' ),
209 - ],
210 - 'comment' => [
211 - 'singular' => esc_html__( 'Comment', 'elasticpress' ),
212 - 'plural' => esc_html__( 'Comments', 'elasticpress' ),
213 - ],
214 - ]
215 - );
74 + $sync_history = ! $indices_missing ? IndexHelper::factory()->get_sync_history() : [];
216 75
217 - $data['ajax_url'] = admin_url( 'admin-ajax.php' );
218 - $data['install_sync'] = empty( $last_sync );
219 - $data['install_complete_url'] = esc_url( $install_complete_url );
220 - $data['sync_complete'] = esc_html__( 'Sync complete', 'elasticpress' );
221 - $data['sync_paused'] = esc_html__( 'Sync paused', 'elasticpress' );
222 - $data['sync_syncing'] = esc_html__( 'Syncing', 'elasticpress' );
223 - $data['sync_initial'] = esc_html__( 'Starting sync', 'elasticpress' );
224 - $data['sync_wpcli'] = esc_html__( 'WP CLI sync is occurring.', 'elasticpress' );
225 - $data['sync_error'] = esc_html__( 'An error occurred while syncing', 'elasticpress' );
226 - $data['sync_interrupted'] = esc_html__( 'Sync interrupted.', 'elasticpress' );
227 - $data['is_epio'] = Utils\is_epio();
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 + ];
228 87
229 88 wp_localize_script( 'ep_sync_scripts', 'epDash', $data );
230 89 }
231 90
232 91 /**
233 - * Output information received from the index helper class.
92 + * Register REST API routes.
234 93 *
235 - * @param array $message Message to be outputted with its status and additional info, if needed.
94 + * @since 5.0.0
95 + * @return void
236 96 */
237 - public static function index_output( $message ) {
238 - switch ( $message['status'] ) {
239 - case 'success':
240 - wp_send_json_success( $message );
241 - break;
242 -
243 - case 'error':
244 - wp_send_json_error( $message );
245 - break;
246 -
247 - default:
248 - wp_send_json( [ 'data' => $message ] );
249 - break;
250 - }
251 - exit;
97 + public function register_rest_routes() {
98 + $controller = new REST\Sync();
99 + $controller->register_routes();
252 100 }
253 101 }