PluginProbe
ElasticPress / 4.6.1
ElasticPress v4.6.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 / StatusReport.php

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

272 lines 7.2 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 }
36
37 /**
38 * Enqueue script.
39 *
40 * @return void
41 */
42 public function admin_enqueue_scripts() {
43 if ( 'status-report' !== \ElasticPress\Screen::factory()->get_current_screen() ) {
44 return;
45 }
46
47 $script_deps = Utils\get_asset_info( 'status-report-script', 'dependencies' );
48
49 wp_enqueue_script(
50 'ep_admin_status_report_scripts',
51 EP_URL . 'dist/js/status-report-script.js',
52 array_merge( $script_deps, [ 'clipboard' ] ),
53 Utils\get_asset_info( 'status-report-script', 'version' ),
54 true
55 );
56
57 wp_localize_script(
58 'ep_admin_status_report_scripts',
59 'epStatusReport',
60 $this->get_formatted_reports()
61 );
62
63 $style_deps = Utils\get_asset_info( 'status-report-styles', 'dependencies' );
64
65 wp_enqueue_style(
66 'ep_status_report_styles',
67 EP_URL . 'dist/css/status-report-styles.css',
68 array_merge( $style_deps, [ 'wp-edit-post' ] ),
69 Utils\get_asset_info( 'status-report-styles', 'version' )
70 );
71 }
72
73 /**
74 * Return all reports available
75 *
76 * @return array
77 */
78 public function get_reports() : array {
79 $reports = [];
80
81 /* this filter is documented in elasticpress.php */
82 $query_logger = apply_filters( 'ep_query_logger', new \ElasticPress\QueryLogger() );
83
84 if ( $query_logger ) {
85 $reports['failed-queries'] = new \ElasticPress\StatusReport\FailedQueries( $query_logger );
86 }
87
88 if ( Utils\is_epio() ) {
89 $reports['autosuggest'] = new \ElasticPress\StatusReport\ElasticPressIo();
90 }
91
92 $reports['wordpress'] = new \ElasticPress\StatusReport\WordPress();
93 $reports['indexable'] = new \ElasticPress\StatusReport\IndexableContent();
94 $reports['elasticpress'] = new \ElasticPress\StatusReport\ElasticPress();
95 $reports['indices'] = new \ElasticPress\StatusReport\Indices();
96 $reports['last-sync'] = new \ElasticPress\StatusReport\LastSync();
97 $reports['features'] = new \ElasticPress\StatusReport\Features();
98
99 /**
100 * Filter the reports executed in the Status Report page.
101 *
102 * @since 4.4.0
103 * @hook ep_status_report_reports
104 * @param {array<Report>} $reports Array of reports
105 * @return {array<Report>} New array of reports
106 */
107 $filtered_reports = apply_filters( 'ep_status_report_reports', $reports );
108
109 // phpcs:disable WordPress.Security.NonceVerification
110 $skipped_reports = isset( $_GET['ep-skip-reports'] ) ?
111 array_map( 'sanitize_text_field', (array) wp_unslash( $_GET['ep-skip-reports'] ) ) :
112 [];
113 // phpcs:enable WordPress.Security.NonceVerification
114
115 $filtered_reports = array_filter(
116 $filtered_reports,
117 function( $report_slug ) use ( $skipped_reports ) {
118 return ! in_array( $report_slug, $skipped_reports, true );
119 },
120 ARRAY_FILTER_USE_KEY
121 );
122
123 return $filtered_reports;
124 }
125
126 /**
127 * Render all reports (HTML and Copy & Paste button)
128 */
129 public function render_reports() {
130 $reports = $this->get_formatted_reports();
131
132 $copy_paste_output = [];
133
134 foreach ( $reports as $report ) {
135 $title = $report['title'];
136 $groups = $report['groups'];
137
138 $copy_paste_output[] = $this->render_copy_paste_report( $title, $groups );
139 }
140
141 ?>
142 <p><?php esc_html_e( 'This screen provides a list of information related to ElasticPress and synced content that can be helpful during troubleshooting. This list can also be copy/pasted and shared as needed.', 'elasticpress' ); ?></p>
143 <p class="ep-copy-button-wrapper">
144 <a download="elasticpress-report.txt" href="data:text/plain;charset=utf-8,<?php echo rawurlencode( implode( "\n\n", $copy_paste_output ) ); ?>" class="button button-primary" id="ep-download-report">
145 <?php esc_html_e( 'Download report', 'elasticpress' ); ?>
146 </a>
147 <button class="button" data-clipboard-text="<?php echo esc_attr( implode( "\n\n", $copy_paste_output ) ); ?>" id="ep-copy-report" type="button">
148 <?php esc_html_e( 'Copy status report to clipboard', 'elasticpress' ); ?>
149 </button>
150 <span class="ep-copy-button-wrapper__success">
151 <?php esc_html_e( 'Copied!', 'elasticpress' ); ?>
152 </span>
153 </p>
154 <?php
155 }
156
157 /**
158 * Process and format the reports, then store them in the `formatted_reports` attribute.
159 *
160 * @since 4.5.0
161 * @return array
162 */
163 protected function get_formatted_reports() : array {
164 if ( empty( $this->formatted_reports ) ) {
165 $reports = $this->get_reports();
166
167 $this->formatted_reports = array_map(
168 function( $report ) {
169 return [
170 'actions' => $report->get_actions(),
171 'groups' => $report->get_groups(),
172 'messages' => $report->get_messages(),
173 'title' => $report->get_title(),
174 ];
175 },
176 $reports
177 );
178 }
179 return $this->formatted_reports;
180 }
181
182 /**
183 * Render the copy & paste report
184 *
185 * @param string $title Report title
186 * @param array $groups Report groups
187 * @return string
188 */
189 protected function render_copy_paste_report( string $title, array $groups ) : string {
190 $output = "## {$title} ##\n\n";
191
192 foreach ( $groups as $group ) {
193 $output .= "### {$group['title']} ###\n";
194 foreach ( $group['fields'] as $slug => $field ) {
195 $value = $field['value'] ?? '';
196
197 $output .= "{$slug}: ";
198 $output .= $this->render_value( $value );
199 $output .= "\n";
200 }
201 $output .= "\n";
202 }
203
204 return $output;
205 }
206
207 /**
208 * Render a value based on its type
209 *
210 * @param mixed $value The value
211 * @return string
212 */
213 protected function render_value( $value ) {
214 if ( is_array( $value ) || is_object( $value ) ) {
215 return var_export( $value, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
216 }
217
218 if ( is_bool( $value ) ) {
219 return $value ? 'true' : 'false';
220 }
221
222 return (string) $value;
223 }
224
225 /**
226 * Display a badge in the admin menu if there's admin notices from
227 * ElasticPress.io.
228 *
229 * @return void
230 */
231 public function admin_menu_count() {
232 global $menu, $submenu;
233
234 $messages = \ElasticPress\ElasticPressIo::factory()->get_endpoint_messages();
235
236 if ( empty( $messages ) ) {
237 return;
238 }
239
240 $count = count( $messages );
241 $title = sprintf(
242 /* translators: %d: Number of messages. */
243 _n( '%s message from ElasticPress.io', '%s messages from ElasticPress.io', $count, 'elasticpress' ),
244 $count
245 );
246
247 foreach ( $menu as $key => $value ) {
248 if ( 'elasticpress' === $value[2] ) {
249 $menu[ $key ][0] .= sprintf(
250 ' <span class="update-plugins"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>',
251 esc_html( $count ),
252 esc_attr( $title )
253 );
254 }
255 }
256
257 if ( ! isset( $submenu['elasticpress'] ) ) {
258 return;
259 }
260
261 foreach ( $submenu['elasticpress'] as $key => $value ) {
262 if ( 'elasticpress-status-report' === $value[2] ) {
263 $submenu['elasticpress'][ $key ][0] .= sprintf(
264 ' <span class="menu-counter"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>',
265 esc_html( $count ),
266 esc_attr( $title )
267 );
268 }
269 }
270 }
271 }
272