PluginProbe
ElasticPress / 4.7.1
ElasticPress v4.7.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.7.1, at includes/classes/Screen/StatusReport.php

271 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 $query_logger = \ElasticPress\get_container()->get( '\ElasticPress\QueryLogger' );
82
83 if ( $query_logger ) {
84 $reports['failed-queries'] = new \ElasticPress\StatusReport\FailedQueries( $query_logger );
85 }
86
87 if ( Utils\is_epio() ) {
88 $reports['autosuggest'] = new \ElasticPress\StatusReport\ElasticPressIo();
89 }
90
91 $reports['wordpress'] = new \ElasticPress\StatusReport\WordPress();
92 $reports['indexable'] = new \ElasticPress\StatusReport\IndexableContent();
93 $reports['elasticpress'] = new \ElasticPress\StatusReport\ElasticPress();
94 $reports['indices'] = new \ElasticPress\StatusReport\Indices();
95 $reports['last-sync'] = new \ElasticPress\StatusReport\LastSync();
96 $reports['features'] = new \ElasticPress\StatusReport\Features();
97
98 /**
99 * Filter the reports executed in the Status Report page.
100 *
101 * @since 4.4.0
102 * @hook ep_status_report_reports
103 * @param {array<Report>} $reports Array of reports
104 * @return {array<Report>} New array of reports
105 */
106 $filtered_reports = apply_filters( 'ep_status_report_reports', $reports );
107
108 // phpcs:disable WordPress.Security.NonceVerification
109 $skipped_reports = isset( $_GET['ep-skip-reports'] ) ?
110 array_map( 'sanitize_text_field', (array) wp_unslash( $_GET['ep-skip-reports'] ) ) :
111 [];
112 // phpcs:enable WordPress.Security.NonceVerification
113
114 $filtered_reports = array_filter(
115 $filtered_reports,
116 function( $report_slug ) use ( $skipped_reports ) {
117 return ! in_array( $report_slug, $skipped_reports, true );
118 },
119 ARRAY_FILTER_USE_KEY
120 );
121
122 return $filtered_reports;
123 }
124
125 /**
126 * Render all reports (HTML and Copy & Paste button)
127 */
128 public function render_reports() {
129 $reports = $this->get_formatted_reports();
130
131 $copy_paste_output = [];
132
133 foreach ( $reports as $report ) {
134 $title = $report['title'];
135 $groups = $report['groups'];
136
137 $copy_paste_output[] = $this->render_copy_paste_report( $title, $groups );
138 }
139
140 ?>
141 <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>
142 <p class="ep-copy-button-wrapper">
143 <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">
144 <?php esc_html_e( 'Download report', 'elasticpress' ); ?>
145 </a>
146 <button class="button" data-clipboard-text="<?php echo esc_attr( implode( "\n\n", $copy_paste_output ) ); ?>" id="ep-copy-report" type="button">
147 <?php esc_html_e( 'Copy status report to clipboard', 'elasticpress' ); ?>
148 </button>
149 <span class="ep-copy-button-wrapper__success">
150 <?php esc_html_e( 'Copied!', 'elasticpress' ); ?>
151 </span>
152 </p>
153 <?php
154 }
155
156 /**
157 * Process and format the reports, then store them in the `formatted_reports` attribute.
158 *
159 * @since 4.5.0
160 * @return array
161 */
162 protected function get_formatted_reports() : array {
163 if ( empty( $this->formatted_reports ) ) {
164 $reports = $this->get_reports();
165
166 $this->formatted_reports = array_map(
167 function( $report ) {
168 return [
169 'actions' => $report->get_actions(),
170 'groups' => $report->get_groups(),
171 'messages' => $report->get_messages(),
172 'title' => $report->get_title(),
173 ];
174 },
175 $reports
176 );
177 }
178 return $this->formatted_reports;
179 }
180
181 /**
182 * Render the copy & paste report
183 *
184 * @param string $title Report title
185 * @param array $groups Report groups
186 * @return string
187 */
188 protected function render_copy_paste_report( string $title, array $groups ) : string {
189 $output = "## {$title} ##\n\n";
190
191 foreach ( $groups as $group ) {
192 $output .= "### {$group['title']} ###\n";
193 foreach ( $group['fields'] as $slug => $field ) {
194 $value = $field['value'] ?? '';
195
196 $output .= "{$slug}: ";
197 $output .= $this->render_value( $value );
198 $output .= "\n";
199 }
200 $output .= "\n";
201 }
202
203 return $output;
204 }
205
206 /**
207 * Render a value based on its type
208 *
209 * @param mixed $value The value
210 * @return string
211 */
212 protected function render_value( $value ) {
213 if ( is_array( $value ) || is_object( $value ) ) {
214 return var_export( $value, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
215 }
216
217 if ( is_bool( $value ) ) {
218 return $value ? 'true' : 'false';
219 }
220
221 return (string) $value;
222 }
223
224 /**
225 * Display a badge in the admin menu if there's admin notices from
226 * ElasticPress.io.
227 *
228 * @return void
229 */
230 public function admin_menu_count() {
231 global $menu, $submenu;
232
233 $messages = \ElasticPress\ElasticPressIo::factory()->get_endpoint_messages();
234
235 if ( empty( $messages ) ) {
236 return;
237 }
238
239 $count = count( $messages );
240 $title = sprintf(
241 /* translators: %d: Number of messages. */
242 _n( '%s message from ElasticPress.io', '%s messages from ElasticPress.io', $count, 'elasticpress' ),
243 $count
244 );
245
246 foreach ( $menu as $key => $value ) {
247 if ( 'elasticpress' === $value[2] ) {
248 $menu[ $key ][0] .= sprintf(
249 ' <span class="update-plugins"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>',
250 esc_html( $count ),
251 esc_attr( $title )
252 );
253 }
254 }
255
256 if ( ! isset( $submenu['elasticpress'] ) ) {
257 return;
258 }
259
260 foreach ( $submenu['elasticpress'] as $key => $value ) {
261 if ( 'elasticpress-status-report' === $value[2] ) {
262 $submenu['elasticpress'][ $key ][0] .= sprintf(
263 ' <span class="menu-counter"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>',
264 esc_html( $count ),
265 esc_attr( $title )
266 );
267 }
268 }
269 }
270 }
271