PluginProbe
ElasticPress / 4.5.1
ElasticPress v4.5.1
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
elasticpress / includes / classes / Screen / Sync.php

Sync.php in ElasticPress 4.5.1, at includes/classes/Screen/Sync.php

262 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\IndexHelper;
12 use ElasticPress\Screen;
13 use ElasticPress\Utils;
14 use ElasticPress\Stats;
15 use ElasticPress\Elasticsearch;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly.
19 }
20
21 /**
22 * Class Sync
23 *
24 * @since 3.6.0
25 * @package ElasticPress
26 */
27 class Sync {
28 /**
29 * Initialize class
30 */
31 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 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
37 }
38
39 /**
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 * Enqueue script.
130 *
131 * @since 3.6.0
132 * @return void
133 */
134 public function admin_enqueue_scripts() {
135 if ( 'sync' !== Screen::factory()->get_current_screen() ) {
136 return;
137 }
138
139 wp_enqueue_script(
140 'ep_sync_scripts',
141 EP_URL . 'dist/js/sync-script.js',
142 Utils\get_asset_info( 'sync-script', 'dependencies' ),
143 Utils\get_asset_info( 'sync-script', 'version' ),
144 true
145 );
146
147 wp_set_script_translations( 'ep_sync_scripts', 'elasticpress' );
148
149 wp_enqueue_style( 'wp-components' );
150 wp_enqueue_style( 'wp-edit-post' );
151
152 wp_enqueue_style(
153 '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' )
157 );
158
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 );
162
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 $sync_required = false;
180 $all_index_names = Elasticsearch::factory()->get_index_names();
181 $cluster_indices = Elasticsearch::factory()->get_cluster_indices();
182
183 $cluster_index_names = wp_list_pluck( $cluster_indices, 'index' );
184 $synced_index_names = array_intersect( $all_index_names, $cluster_index_names );
185
186 if ( $synced_index_names !== $all_index_names ) {
187 $sync_required = true;
188 }
189
190 if ( ! empty( $ep_last_index ) && ! $sync_required ) {
191 $data['ep_last_sync_date'] = ! empty( $ep_last_index['end_date_time'] ) ? $ep_last_index['end_date_time'] : false;
192 $data['ep_last_sync_failed'] = ! empty( $ep_last_index['failed'] ) || ! empty( $ep_last_index['errors'] ) ? true : false;
193 }
194
195 /**
196 * Filter indexable labels used in dashboard sync UI
197 *
198 * @since 3.0
199 * @hook ep_dashboard_indexable_labels
200 * @param {array} $labels Current indexable lables
201 * @return {array} New labels
202 */
203 $data['sync_indexable_labels'] = apply_filters(
204 'ep_dashboard_indexable_labels',
205 [
206 'post' => [
207 'singular' => esc_html__( 'Post', 'elasticpress' ),
208 'plural' => esc_html__( 'Posts', 'elasticpress' ),
209 ],
210 'term' => [
211 'singular' => esc_html__( 'Term', 'elasticpress' ),
212 'plural' => esc_html__( 'Terms', 'elasticpress' ),
213 ],
214 'user' => [
215 'singular' => esc_html__( 'User', 'elasticpress' ),
216 'plural' => esc_html__( 'Users', 'elasticpress' ),
217 ],
218 'comment' => [
219 'singular' => esc_html__( 'Comment', 'elasticpress' ),
220 'plural' => esc_html__( 'Comments', 'elasticpress' ),
221 ],
222 ]
223 );
224
225 $data['ajax_url'] = admin_url( 'admin-ajax.php' );
226 $data['install_sync'] = empty( $last_sync );
227 $data['install_complete_url'] = esc_url( $install_complete_url );
228 $data['sync_complete'] = esc_html__( 'Sync complete', 'elasticpress' );
229 $data['sync_paused'] = esc_html__( 'Sync paused', 'elasticpress' );
230 $data['sync_syncing'] = esc_html__( 'Syncing', 'elasticpress' );
231 $data['sync_initial'] = esc_html__( 'Starting sync', 'elasticpress' );
232 $data['sync_wpcli'] = esc_html__( 'WP CLI sync is occurring.', 'elasticpress' );
233 $data['sync_error'] = esc_html__( 'An error occurred while syncing', 'elasticpress' );
234 $data['sync_interrupted'] = esc_html__( 'Sync interrupted.', 'elasticpress' );
235 $data['is_epio'] = Utils\is_epio();
236
237 wp_localize_script( 'ep_sync_scripts', 'epDash', $data );
238 }
239
240 /**
241 * Output information received from the index helper class.
242 *
243 * @param array $message Message to be outputted with its status and additional info, if needed.
244 */
245 public static function index_output( $message ) {
246 switch ( $message['status'] ) {
247 case 'success':
248 wp_send_json_success( $message );
249 break;
250
251 case 'error':
252 wp_send_json_error( $message );
253 break;
254
255 default:
256 wp_send_json( [ 'data' => $message ] );
257 break;
258 }
259 exit;
260 }
261 }
262