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 / Screen / StatusReport.php

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

307 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ElasticPress Status Report class
4 *
5 * @since 4.4.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\Screen;
10
11 use ElasticPress\Utils;
12
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * Status Report class
17 *
18 * @package ElasticPress
19 */
20 class StatusReport {
21 /**
22 * The formatted/processed reports.
23 *
24 * @since 4.5.0
25 * @var array
26 */
27 protected $formatted_reports = [];
28
29 /**
30 * Initialize class
31 */
32 public function setup() {
33 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
34 add_action( 'admin_head', array( $this, 'admin_menu_count' ), 11 );
35 add_action( 'wp_ajax_ep_load_groups', array( $this, 'action_wp_ajax_ep_load_groups' ) );
36 }
37
38 /**
39 * Enqueue script.
40 *
41 * @return void
42 */
43 public function admin_enqueue_scripts() {
44 if ( 'status-report' !== \ElasticPress\Screen::factory()->get_current_screen() ) {
45 return;
46 }
47
48 wp_enqueue_script(
49 'ep_admin_status_report_scripts',
50 EP_URL . 'dist/js/status-report-script.js',
51 Utils\get_asset_info( 'status-report-script', 'dependencies' ),
52 Utils\get_asset_info( 'status-report-script', 'version' ),
53 true
54 );
55
56 $reports = $this->get_formatted_reports();
57
58 $plain_text_reports = [];
59
60 foreach ( $reports as $report ) {
61 $title = $report['title'];
62 $groups = $report['groups'];
63 $plain_text_reports[] = $this->render_copy_paste_report( $title, $groups, $report['isAjaxReport'] );
64 }
65
66 $plain_text_report = implode( "\n\n", $plain_text_reports );
67
68 wp_localize_script(
69 'ep_admin_status_report_scripts',
70 'epStatusReport',
71 [
72 'plainTextReport' => $plain_text_report,
73 'reports' => $reports,
74 'nonce' => wp_create_nonce( 'ep-status-report-nonce' ),
75 ]
76 );
77
78 wp_enqueue_style(
79 'ep_status_report_styles',
80 EP_URL . 'dist/css/status-report-script.css',
81 [ 'wp-components', 'wp-edit-post' ],
82 Utils\get_asset_info( 'status-report-script', 'version' )
83 );
84 }
85
86 /**
87 * AJAX action to load an individual report group.
88 *
89 * @since 5.2.0
90 *
91 * @return void
92 */
93 public function action_wp_ajax_ep_load_groups(): void {
94 if ( ! isset( $_POST['ep-status-report-nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ep-status-report-nonce'] ) ), 'ep-status-report-nonce' ) ) {
95 wp_send_json_error( [ 'message' => __( 'Nonce is not present.', 'elasticpress' ) ], 403 );
96 }
97
98 if ( empty( $this->formatted_reports ) ) {
99 $this->formatted_reports = $this->get_reports();
100 }
101
102 $post = wp_unslash( $_POST );
103
104 if ( empty( $this->formatted_reports[ $post['report'] ] ) ) {
105 wp_send_json_error( [ 'message' => __( 'Status report not found.', 'elasticpress' ) ], 404 );
106 }
107
108 $report = $this->formatted_reports[ $post['report'] ];
109
110 if ( ! $report instanceof \ElasticPress\StatusReport\AjaxReport ) {
111 wp_send_json_error( [ 'message' => __( 'Report is not an AJAX report.', 'elasticpress' ) ], 403 );
112 }
113
114 wp_send_json_success(
115 [
116 'groups' => $report->get_groups_ajax(),
117 'messages' => $report->get_messages(),
118 ],
119 200
120 );
121 }
122
123 /**
124 * Return all reports available
125 *
126 * @return array
127 */
128 public function get_reports(): array {
129 $reports = [];
130
131 $query_logger = \ElasticPress\get_container()->get( '\ElasticPress\QueryLogger' );
132
133 if ( $query_logger ) {
134 $reports['failed-queries'] = new \ElasticPress\StatusReport\FailedQueries( $query_logger );
135 }
136
137 if ( Utils\is_epio() ) {
138 $reports['autosuggest'] = new \ElasticPress\StatusReport\ElasticPressIo();
139 }
140
141 $reports['wordpress'] = new \ElasticPress\StatusReport\WordPress();
142 $reports['indexable'] = new \ElasticPress\StatusReport\IndexableContent();
143 $reports['elasticpress'] = new \ElasticPress\StatusReport\ElasticPress();
144 $reports['indices'] = new \ElasticPress\StatusReport\Indices();
145 $reports['last-sync'] = new \ElasticPress\StatusReport\LastSync();
146 $reports['features'] = new \ElasticPress\StatusReport\Features();
147
148 /**
149 * Filter the reports executed in the Status Report page.
150 *
151 * @since 4.4.0
152 * @hook ep_status_report_reports
153 * @param {array<Report>} $reports Array of reports
154 * @return {array<Report>} New array of reports
155 */
156 $filtered_reports = apply_filters( 'ep_status_report_reports', $reports );
157
158 // phpcs:disable WordPress.Security.NonceVerification
159 $skipped_reports = isset( $_GET['ep-skip-reports'] ) ?
160 array_map( 'sanitize_text_field', (array) wp_unslash( $_GET['ep-skip-reports'] ) ) :
161 [];
162 // phpcs:enable WordPress.Security.NonceVerification
163
164 $filtered_reports = array_filter(
165 $filtered_reports,
166 function ( $report_slug ) use ( $skipped_reports ) {
167 return ! in_array( $report_slug, $skipped_reports, true );
168 },
169 ARRAY_FILTER_USE_KEY
170 );
171
172 return $filtered_reports;
173 }
174
175 /**
176 * Process and format the reports, then store them in the `formatted_reports` attribute.
177 *
178 * @since 4.5.0
179 * @return array
180 */
181 protected function get_formatted_reports(): array {
182 if ( empty( $this->formatted_reports ) ) {
183 $reports = $this->get_reports();
184
185 $this->formatted_reports = array_map(
186 function ( $report ) {
187 return [
188 'actions' => $report->get_actions(),
189 'groups' => $report->get_groups(),
190 'messages' => $report->get_messages(),
191 'title' => $report->get_title(),
192 'isAjaxReport' => $report instanceof \ElasticPress\StatusReport\AjaxReport,
193 ];
194 },
195 $reports
196 );
197 }
198 return $this->formatted_reports;
199 }
200
201 /**
202 * Render the copy & paste report
203 *
204 * @param string $title Report title
205 * @param array $groups Report groups
206 * @param bool $is_ajax_report Whether the report is an AJAX report
207 *
208 * @return string
209 */
210 protected function render_copy_paste_report( string $title, array $groups, bool $is_ajax_report = false ): string {
211 $output = "## {$title} ##\n\n";
212
213 if ( $is_ajax_report ) {
214 $output .= $this->render_pending_generation();
215 return $output;
216 }
217
218 foreach ( $groups as $group ) {
219 $output .= "### {$group['title']} ###\n";
220 foreach ( $group['fields'] as $slug => $field ) {
221 $value = $field['value'] ?? '';
222
223 $output .= "{$slug}: ";
224 $output .= $this->render_value( $value );
225 $output .= "\n";
226 }
227 $output .= "\n";
228 }
229
230 return $output;
231 }
232
233 /**
234 * Render a value based on its type
235 *
236 * @param mixed $value The value
237 * @return string
238 */
239 protected function render_value( $value ) {
240 if ( is_array( $value ) || is_object( $value ) ) {
241 return var_export( $value, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
242 }
243
244 if ( is_bool( $value ) ) {
245 return $value ? 'true' : 'false';
246 }
247
248 return (string) $value;
249 }
250
251 /**
252 * Render a message when the report is pending generation
253 *
254 * @return string
255 */
256 protected function render_pending_generation() {
257 return __( 'Please generate a full report to see the content of this group.', 'elasticpress' );
258 }
259
260 /**
261 * Display a badge in the admin menu if there's admin notices from
262 * ElasticPress.io.
263 *
264 * @return void
265 */
266 public function admin_menu_count() {
267 global $menu, $submenu;
268
269 $messages = \ElasticPress\get_container()->get( '\ElasticPress\ElasticPressIo' )->get_endpoint_messages();
270
271 if ( empty( $messages ) ) {
272 return;
273 }
274
275 $count = count( $messages );
276 $title = sprintf(
277 /* translators: %d: Number of messages. */
278 _n( '%s message from ElasticPress.io', '%s messages from ElasticPress.io', $count, 'elasticpress' ),
279 $count
280 );
281
282 foreach ( $menu as $key => $value ) {
283 if ( 'elasticpress' === $value[2] ) {
284 $menu[ $key ][0] .= sprintf(
285 ' <span class="update-plugins"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>',
286 esc_html( $count ),
287 esc_attr( $title )
288 );
289 }
290 }
291
292 if ( ! isset( $submenu['elasticpress'] ) ) {
293 return;
294 }
295
296 foreach ( $submenu['elasticpress'] as $key => $value ) {
297 if ( 'elasticpress-status-report' === $value[2] ) {
298 $submenu['elasticpress'][ $key ][0] .= sprintf(
299 ' <span class="menu-counter"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>',
300 esc_html( $count ),
301 esc_attr( $title )
302 );
303 }
304 }
305 }
306 }
307