PluginProbe
ElasticPress / 5.0.0
ElasticPress v5.0.0
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 / REST / Sync.php

Sync.php in ElasticPress 5.0.0, at includes/classes/REST/Sync.php

243 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sync REST API Controller
4 *
5 * @since 5.0.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\REST;
10
11 use ElasticPress\IndexHelper;
12 use ElasticPress\Utils;
13
14 /**
15 * Sync API controller class.
16 *
17 * @since 5.0.0
18 * @package elasticpress
19 */
20 class Sync {
21
22 /**
23 * Register routes.
24 *
25 * @return void
26 */
27 public function register_routes() {
28 register_rest_route(
29 'elasticpress/v1',
30 'sync',
31 [
32 'args' => $this->get_args(),
33 'callback' => [ $this, 'sync' ],
34 'methods' => 'POST',
35 'permission_callback' => [ $this, 'check_permission' ],
36 ]
37 );
38
39 register_rest_route(
40 'elasticpress/v1',
41 'sync',
42 [
43 'callback' => [ $this, 'get_sync_status' ],
44 'methods' => 'GET',
45 'permission_callback' => [ $this, 'check_permission' ],
46 ]
47 );
48
49 register_rest_route(
50 'elasticpress/v1',
51 'sync',
52 [
53 'callback' => [ $this, 'cancel_sync' ],
54 'methods' => 'DELETE',
55 'permission_callback' => [ $this, 'check_permission' ],
56 ]
57 );
58 }
59
60 /**
61 * Get args schema.
62 *
63 * @return array
64 */
65 public function get_args() {
66 return [
67 'include' => [
68 'description' => __( 'IDs of objects to sync.', 'elasticpress' ),
69 'items' => [
70 'type' => 'integer',
71 ],
72 'type' => 'array',
73 ],
74 'indexables' => [
75 'description' => __( 'Indexables to sync', 'elasticpress' ),
76 'items' => [
77 'type' => 'string',
78 ],
79 'required' => false,
80 'type' => 'array',
81 ],
82 'lower_limit_object_id' => [
83 'description' => __( 'Start of object ID range to sync,', 'elasticpress' ),
84 'type' => 'integer',
85 'required' => false,
86 ],
87 'offset' => [
88 'description' => __( 'Number of objects to skip.', 'elasticpress' ),
89 'required' => false,
90 'type' => 'integer',
91 ],
92 'post_type' => [
93 'description' => __( 'Post type to sync.', 'elasticpress' ),
94 'items' => [
95 'type' => 'string',
96 ],
97 'type' => 'array',
98 ],
99 'put_mapping' => [
100 'default' => false,
101 'description' => __( 'Whether to clear the index and send mapping before syncing.', 'elasticpress' ),
102 'type' => 'boolean',
103 'required' => false,
104 ],
105 'trigger' => [
106 'enum' => [ 'features', 'install', 'manual', 'upgrade' ],
107 'required' => false,
108 ],
109 'upper_limit_object_id' => [
110 'description' => __( 'End of object ID range to sync.', 'elasticpress' ),
111 'type' => 'integer',
112 'required' => false,
113 ],
114 ];
115 }
116
117 /**
118 * Check that the request has permission to sync.
119 *
120 * @return boolean
121 */
122 public function check_permission() {
123 $capability = Utils\get_capability();
124
125 return current_user_can( $capability );
126 }
127
128 /**
129 * Start or continue a sync.
130 *
131 * @param \WP_REST_Request $request Full details about the request.
132 * @return void
133 */
134 public function sync( \WP_REST_Request $request ) {
135 nocache_headers();
136
137 $index_meta = Utils\get_indexing_status();
138
139 if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) {
140 $this->get_sync_status( $request );
141 exit;
142 }
143
144 $args = array_merge(
145 [
146 'method' => 'dashboard',
147 'network_wide' => 0,
148 'output_method' => [ $this, 'output' ],
149 'show_errors' => true,
150 ],
151 $request->get_params()
152 );
153
154 IndexHelper::factory()->full_index( $args );
155 }
156
157 /**
158 * Output the result of indexing.
159 *
160 * @param array $message Index details.
161 * @return void
162 */
163 public function output( array $message ) {
164 switch ( $message['status'] ) {
165 case 'success':
166 wp_send_json_success( $message );
167 break;
168 case 'error':
169 wp_send_json_error( $message );
170 break;
171 default:
172 wp_send_json( [ 'data' => $message ] );
173 break;
174 }
175 }
176
177 /**
178 * Get the status of a sync in progress.
179 *
180 * @param \WP_REST_Request $request Full details about the request.
181 * @return void
182 */
183 public function get_sync_status( \WP_REST_Request $request ) {
184 nocache_headers();
185
186 $index_meta = Utils\get_indexing_status();
187
188 if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) {
189 wp_send_json_success(
190 [
191 'message' => sprintf(
192 /* translators: 1. Number of objects indexed, 2. Total number of objects, 3. Last object ID. */
193 esc_html__( 'Processed %1$d/%2$d. Last Object ID: %3$d', 'elasticpress' ),
194 $index_meta['offset'],
195 $index_meta['found_items'],
196 $index_meta['current_sync_item']['last_processed_object_id']
197 ),
198 'index_meta' => $index_meta,
199 ]
200 );
201 }
202
203 wp_send_json_success(
204 [
205 'is_finished' => true,
206 'totals' => IndexHelper::factory()->get_last_sync(),
207 ]
208 );
209 }
210
211 /**
212 * Cancel a sync in progress.
213 *
214 * @param \WP_REST_Request $request Full details about the request.
215 * @return void
216 */
217 public function cancel_sync( \WP_REST_Request $request ) {
218 nocache_headers();
219
220 $index_meta = Utils\get_indexing_status();
221
222 if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) {
223 set_transient( 'ep_wpcli_sync_interrupted', true, MINUTE_IN_SECONDS );
224 wp_send_json_success();
225 exit;
226 }
227
228 $index_meta = IndexHelper::factory()->get_index_meta();
229
230 if ( $index_meta ) {
231 IndexHelper::factory()->clear_index_meta();
232
233 wp_send_json_success(
234 IndexHelper::factory()->get_last_sync()
235 );
236
237 return;
238 }
239
240 wp_send_json_success();
241 }
242 }
243