PluginProbe
ElasticPress / 5.0.2
ElasticPress v5.0.2
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.0.2, at includes/classes/Screen/StatusReport.php

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