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
← All changes | includes/classes/Screen/StatusReport.php +87 -51 4.7.25.3.5 View file →
@@ -7,9 +7,9 @@
7 7 */
8 8
9 9 namespace ElasticPress\Screen;
10 10
11 -use \ElasticPress\Utils;
11 +use ElasticPress\Utils;
12 12
13 13 defined( 'ABSPATH' ) || exit;
14 14
15 15 /**
@@ -31,8 +31,9 @@
31 31 */
32 32 public function setup() {
33 33 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
34 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' ) );
35 36 }
36 37
37 38 /**
38 39 * Enqueue script.
@@ -43,40 +44,89 @@
43 44 if ( 'status-report' !== \ElasticPress\Screen::factory()->get_current_screen() ) {
44 45 return;
45 46 }
46 47
47 - $script_deps = Utils\get_asset_info( 'status-report-script', 'dependencies' );
48 -
49 48 wp_enqueue_script(
50 49 'ep_admin_status_report_scripts',
51 50 EP_URL . 'dist/js/status-report-script.js',
52 - array_merge( $script_deps, [ 'clipboard' ] ),
51 + Utils\get_asset_info( 'status-report-script', 'dependencies' ),
53 52 Utils\get_asset_info( 'status-report-script', 'version' ),
54 53 true
55 54 );
56 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 +
57 68 wp_localize_script(
58 69 'ep_admin_status_report_scripts',
59 70 'epStatusReport',
60 - $this->get_formatted_reports()
71 + [
72 + 'plainTextReport' => $plain_text_report,
73 + 'reports' => $reports,
74 + 'nonce' => wp_create_nonce( 'ep-status-report-nonce' ),
75 + ]
61 76 );
62 77
63 - $style_deps = Utils\get_asset_info( 'status-report-styles', 'dependencies' );
64 -
65 78 wp_enqueue_style(
66 79 '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' )
80 + EP_URL . 'dist/css/status-report-script.css',
81 + [ 'wp-components', 'wp-edit-post' ],
82 + Utils\get_asset_info( 'status-report-script', 'version' )
70 83 );
71 84 }
72 85
73 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 + /**
74 124 * Return all reports available
75 125 *
76 126 * @return array
77 127 */
78 - public function get_reports() : array {
128 + public function get_reports(): array {
79 129 $reports = [];
80 130
81 131 $query_logger = \ElasticPress\get_container()->get( '\ElasticPress\QueryLogger' );
82 132
@@ -112,9 +162,9 @@
112 162 // phpcs:enable WordPress.Security.NonceVerification
113 163
114 164 $filtered_reports = array_filter(
115 165 $filtered_reports,
116 - function( $report_slug ) use ( $skipped_reports ) {
166 + function ( $report_slug ) use ( $skipped_reports ) {
117 167 return ! in_array( $report_slug, $skipped_reports, true );
118 168 },
119 169 ARRAY_FILTER_USE_KEY
120 170 );
@@ -122,55 +172,25 @@
122 172 return $filtered_reports;
123 173 }
124 174
125 175 /**
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 176 * Process and format the reports, then store them in the `formatted_reports` attribute.
158 177 *
159 178 * @since 4.5.0
160 179 * @return array
161 180 */
162 - protected function get_formatted_reports() : array {
181 + protected function get_formatted_reports(): array {
163 182 if ( empty( $this->formatted_reports ) ) {
164 183 $reports = $this->get_reports();
165 184
166 185 $this->formatted_reports = array_map(
167 - function( $report ) {
186 + function ( $report ) {
168 187 return [
169 - 'actions' => $report->get_actions(),
170 - 'groups' => $report->get_groups(),
171 - 'messages' => $report->get_messages(),
172 - 'title' => $report->get_title(),
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,
173 193 ];
174 194 },
175 195 $reports
176 196 );
@@ -182,13 +202,20 @@
182 202 * Render the copy & paste report
183 203 *
184 204 * @param string $title Report title
185 205 * @param array $groups Report groups
206 + * @param bool $is_ajax_report Whether the report is an AJAX report
207 + *
186 208 * @return string
187 209 */
188 - protected function render_copy_paste_report( string $title, array $groups ) : string {
210 + protected function render_copy_paste_report( string $title, array $groups, bool $is_ajax_report = false ): string {
189 211 $output = "## {$title} ##\n\n";
190 212
213 + if ( $is_ajax_report ) {
214 + $output .= $this->render_pending_generation();
215 + return $output;
216 + }
217 +
191 218 foreach ( $groups as $group ) {
192 219 $output .= "### {$group['title']} ###\n";
193 220 foreach ( $group['fields'] as $slug => $field ) {
194 221 $value = $field['value'] ?? '';
@@ -221,8 +248,17 @@
221 248 return (string) $value;
222 249 }
223 250
224 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 + /**
225 261 * Display a badge in the admin menu if there's admin notices from
226 262 * ElasticPress.io.
227 263 *
228 264 * @return void
@@ -229,9 +265,9 @@
229 265 */
230 266 public function admin_menu_count() {
231 267 global $menu, $submenu;
232 268
233 - $messages = \ElasticPress\ElasticPressIo::factory()->get_endpoint_messages();
269 + $messages = \ElasticPress\get_container()->get( '\ElasticPress\ElasticPressIo' )->get_endpoint_messages();
234 270
235 271 if ( empty( $messages ) ) {
236 272 return;
237 273 }