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
elasticpress / includes / classes / StatusReport / ElasticPressIo.php

ElasticPressIo.php in ElasticPress 5.3.5, at includes/classes/StatusReport/ElasticPressIo.php

278 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ElasticPress.io report class
4 *
5 * @since 4.4.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\StatusReport;
10
11 use ElasticPress\Indexables;
12 use ElasticPress\Feature\InstantResults;
13 use ElasticPress\Utils;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * ElasticPressIo report class
19 *
20 * @package ElasticPress
21 */
22 class ElasticPressIo extends Report {
23
24 /**
25 * Return the report title
26 *
27 * @return string
28 */
29 public function get_title(): string {
30 return __( 'ElasticPress.io', 'elasticpress' );
31 }
32
33 /**
34 * Return the report fields
35 *
36 * @return array
37 */
38 public function get_groups(): array {
39 $groups = [
40 $this->get_autosuggest_group(),
41 $this->get_instant_results_group(),
42 $this->get_orders_search_group(),
43 ];
44
45 return array_values( array_filter( $groups ) );
46 }
47
48 /**
49 * Process the ElasticPress.io Autosuggest allowed parameters.
50 *
51 * @return array
52 */
53 protected function get_autosuggest_group(): array {
54 $autosuggest_feature = \ElasticPress\Features::factory()->get_registered_feature( 'autosuggest' );
55
56 if ( ! $autosuggest_feature->is_active() ) {
57 return [];
58 }
59
60 $title = __( 'Allowed Autosuggest Parameters', 'elasticpress' );
61 $allowed_params = $autosuggest_feature->epio_autosuggest_set_and_get();
62
63 $reset_url = [
64 'label' => 'Reset URL',
65 'value' => wp_kses_post(
66 sprintf(
67 /* translators: %s: URL */
68 __( 'If you need to reset the allowed parameters, you can do so by saving your Search Fields & Weighting settings or by visiting <a href="%s">this URL</a>.', 'elasticpress' ),
69 $autosuggest_feature->get_epio_public_request_url()
70 )
71 ),
72 ];
73
74 if ( empty( $allowed_params ) ) {
75 $fields = [
76 'not_available' => [
77 'label' => __( 'Allowed Autosuggest Parameters', 'elasticpress' ),
78 'value' => __( 'Allowed autosuggest parameters info not available.', 'elasticpress' ),
79 ],
80 'url' => $reset_url,
81 ];
82
83 return [
84 'title' => $title,
85 'fields' => $fields,
86 ];
87 }
88
89 $allowed_params = wp_parse_args(
90 $allowed_params,
91 [
92 'postTypes' => [],
93 'postStatus' => [],
94 'searchFields' => [],
95 'returnFields' => '',
96 ]
97 );
98
99 $fields = [
100 'Post Types' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['postTypes'] ),
101 'Post Status' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['postStatus'] ),
102 'Search Fields' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['searchFields'] ),
103 'Returned Fields' => wp_sprintf( esc_html( var_export( $allowed_params['returnFields'], true ) ) ), // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
104 ];
105
106 $formatted_fields = [];
107
108 foreach ( $fields as $label => $value ) {
109 $formatted_fields[ sanitize_title( $label ) ] = [
110 'label' => $label,
111 'value' => $value,
112 ];
113 }
114
115 $formatted_fields['url'] = $reset_url;
116
117 return [
118 'title' => $title,
119 'fields' => $formatted_fields,
120 ];
121 }
122
123 /**
124 * Process the ElasticPress.io Instant Results templates.
125 *
126 * @return array
127 */
128 protected function get_instant_results_group(): array {
129 $instant_results_feature = \ElasticPress\Features::factory()->get_registered_feature( 'instant-results' );
130
131 if ( ! $instant_results_feature->is_active() ) {
132 return [];
133 }
134
135 $title = __( 'Instant Results Template', 'elasticpress' );
136 $fields = [];
137
138 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
139 $sites = Utils\get_sites( 0, true );
140
141 foreach ( $sites as $site ) {
142 switch_to_blog( $site['blog_id'] );
143
144 $field = $this->get_instant_results_field();
145 $fields = array_merge( $fields, $field );
146
147 restore_current_blog();
148 }
149 } else {
150 $fields = $this->get_instant_results_field();
151 }
152
153 return [
154 'title' => $title,
155 'fields' => $fields,
156 ];
157 }
158
159 /**
160 * Process the ElasticPress.io Orders Search templates.
161 *
162 * @since 4.5.0
163 * @return array
164 */
165 protected function get_orders_search_group(): array {
166 $woocommerce_feature = \ElasticPress\Features::factory()->get_registered_feature( 'woocommerce' );
167
168 if ( ! $woocommerce_feature->is_active() ) {
169 return [];
170 }
171
172 if ( ! $woocommerce_feature->orders ) {
173 return [];
174 }
175
176 $title = __( 'Orders Search Template', 'elasticpress' );
177 $fields = [];
178
179 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
180 $sites = Utils\get_sites( 0, true );
181
182 foreach ( $sites as $site ) {
183 switch_to_blog( $site['blog_id'] );
184
185 $field = $this->get_orders_search_field();
186 $fields = array_merge( $fields, $field );
187
188 restore_current_blog();
189 }
190 } else {
191 $fields = $this->get_orders_search_field();
192 }
193
194 return [
195 'title' => $title,
196 'fields' => $fields,
197 ];
198 }
199
200 /**
201 * Process the ElasticPress.io Instant Results template.
202 *
203 * @return array
204 */
205 protected function get_instant_results_field(): array {
206 $index = Indexables::factory()->get( 'post' )->get_index_name();
207
208 if ( ! $index ) {
209 return [];
210 }
211
212 $feature = new InstantResults\InstantResults();
213 $template = $feature->epio_get_search_template();
214
215 if ( is_wp_error( $template ) ) {
216 return [
217 $index => [
218 'label' => $index,
219 'value' => $template->get_error_message(),
220 ],
221 ];
222 }
223
224 return [
225 $index => [
226 'label' => $index,
227 'value' => $template,
228 ],
229 ];
230 }
231
232 /**
233 * Return the report messages.
234 *
235 * @return array
236 * @since 4.5.0
237 */
238 public function get_messages(): array {
239 $messages = \ElasticPress\get_container()->get( '\ElasticPress\ElasticPressIo' )->get_endpoint_messages( true );
240 $messages = array_values( $messages );
241
242 return $messages;
243 }
244
245 /**
246 * Process the ElasticPress.io Orders Search template.
247 *
248 * @since 4.5.0
249 * @return array
250 */
251 protected function get_orders_search_field(): array {
252 $index = Indexables::factory()->get( 'post' )->get_index_name();
253
254 if ( ! $index ) {
255 return [];
256 }
257
258 $woocommerce_feature = \ElasticPress\Features::factory()->get_registered_feature( 'woocommerce' );
259 $template = $woocommerce_feature->orders_autosuggest->get_search_template();
260
261 if ( is_wp_error( $template ) ) {
262 return [
263 $index => [
264 'label' => $index,
265 'value' => $template->get_error_message(),
266 ],
267 ];
268 }
269
270 return [
271 $index => [
272 'label' => $index,
273 'value' => $template,
274 ],
275 ];
276 }
277 }
278