| @@ -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,44 +44,92 @@ | ||
| 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 | - /* this filter is documented in elasticpress.php */ | |
| 82 | - $query_logger = apply_filters( 'ep_query_logger', new \ElasticPress\QueryLogger() ); | |
| 131 | + $query_logger = \ElasticPress\get_container()->get( '\ElasticPress\QueryLogger' ); | |
| 83 | 132 | |
| 84 | 133 | if ( $query_logger ) { |
| 85 | 134 | $reports['failed-queries'] = new \ElasticPress\StatusReport\FailedQueries( $query_logger ); |
| 86 | 135 | } |
| @@ -105,14 +154,17 @@ | ||
| 105 | 154 | * @return {array<Report>} New array of reports |
| 106 | 155 | */ |
| 107 | 156 | $filtered_reports = apply_filters( 'ep_status_report_reports', $reports ); |
| 108 | 157 | |
| 109 | - $skipped_reports = ! empty( $_GET['ep-skip-reports'] ) ? (array) $_GET['ep-skip-reports'] : []; // phpcs:ignore WordPress.Security.NonceVerification | |
| 110 | - $skipped_reports = array_map( 'sanitize_text_field', $skipped_reports ); | |
| 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 | |
| 111 | 163 | |
| 112 | 164 | $filtered_reports = array_filter( |
| 113 | 165 | $filtered_reports, |
| 114 | - function( $report_slug ) use ( $skipped_reports ) { | |
| 166 | + function ( $report_slug ) use ( $skipped_reports ) { | |
| 115 | 167 | return ! in_array( $report_slug, $skipped_reports, true ); |
| 116 | 168 | }, |
| 117 | 169 | ARRAY_FILTER_USE_KEY |
| 118 | 170 | ); |
| @@ -120,55 +172,25 @@ | ||
| 120 | 172 | return $filtered_reports; |
| 121 | 173 | } |
| 122 | 174 | |
| 123 | 175 | /** |
| 124 | - * Render all reports (HTML and Copy & Paste button) | |
| 125 | - */ | |
| 126 | - public function render_reports() { | |
| 127 | - $reports = $this->get_formatted_reports(); | |
| 128 | - | |
| 129 | - $copy_paste_output = []; | |
| 130 | - | |
| 131 | - foreach ( $reports as $report ) { | |
| 132 | - $title = $report['title']; | |
| 133 | - $groups = $report['groups']; | |
| 134 | - | |
| 135 | - $copy_paste_output[] = $this->render_copy_paste_report( $title, $groups ); | |
| 136 | - } | |
| 137 | - | |
| 138 | - ?> | |
| 139 | - <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> | |
| 140 | - <p class="ep-copy-button-wrapper"> | |
| 141 | - <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"> | |
| 142 | - <?php esc_html_e( 'Download report', 'elasticpress' ); ?> | |
| 143 | - </a> | |
| 144 | - <button class="button" data-clipboard-text="<?php echo esc_attr( implode( "\n\n", $copy_paste_output ) ); ?>" id="ep-copy-report" type="button"> | |
| 145 | - <?php esc_html_e( 'Copy status report to clipboard', 'elasticpress' ); ?> | |
| 146 | - </button> | |
| 147 | - <span class="ep-copy-button-wrapper__success"> | |
| 148 | - <?php esc_html_e( 'Copied!', 'elasticpress' ); ?> | |
| 149 | - </span> | |
| 150 | - </p> | |
| 151 | - <?php | |
| 152 | - } | |
| 153 | - | |
| 154 | - /** | |
| 155 | 176 | * Process and format the reports, then store them in the `formatted_reports` attribute. |
| 156 | 177 | * |
| 157 | 178 | * @since 4.5.0 |
| 158 | 179 | * @return array |
| 159 | 180 | */ |
| 160 | - protected function get_formatted_reports() : array { | |
| 181 | + protected function get_formatted_reports(): array { | |
| 161 | 182 | if ( empty( $this->formatted_reports ) ) { |
| 162 | 183 | $reports = $this->get_reports(); |
| 163 | 184 | |
| 164 | 185 | $this->formatted_reports = array_map( |
| 165 | - function( $report ) { | |
| 186 | + function ( $report ) { | |
| 166 | 187 | return [ |
| 167 | - 'actions' => $report->get_actions(), | |
| 168 | - 'groups' => $report->get_groups(), | |
| 169 | - 'messages' => $report->get_messages(), | |
| 170 | - '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, | |
| 171 | 193 | ]; |
| 172 | 194 | }, |
| 173 | 195 | $reports |
| 174 | 196 | ); |
| @@ -180,13 +202,20 @@ | ||
| 180 | 202 | * Render the copy & paste report |
| 181 | 203 | * |
| 182 | 204 | * @param string $title Report title |
| 183 | 205 | * @param array $groups Report groups |
| 206 | + * @param bool $is_ajax_report Whether the report is an AJAX report | |
| 207 | + * | |
| 184 | 208 | * @return string |
| 185 | 209 | */ |
| 186 | - 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 { | |
| 187 | 211 | $output = "## {$title} ##\n\n"; |
| 188 | 212 | |
| 213 | + if ( $is_ajax_report ) { | |
| 214 | + $output .= $this->render_pending_generation(); | |
| 215 | + return $output; | |
| 216 | + } | |
| 217 | + | |
| 189 | 218 | foreach ( $groups as $group ) { |
| 190 | 219 | $output .= "### {$group['title']} ###\n"; |
| 191 | 220 | foreach ( $group['fields'] as $slug => $field ) { |
| 192 | 221 | $value = $field['value'] ?? ''; |
| @@ -219,8 +248,17 @@ | ||
| 219 | 248 | return (string) $value; |
| 220 | 249 | } |
| 221 | 250 | |
| 222 | 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 | + /** | |
| 223 | 261 | * Display a badge in the admin menu if there's admin notices from |
| 224 | 262 | * ElasticPress.io. |
| 225 | 263 | * |
| 226 | 264 | * @return void |
| @@ -227,9 +265,9 @@ | ||
| 227 | 265 | */ |
| 228 | 266 | public function admin_menu_count() { |
| 229 | 267 | global $menu, $submenu; |
| 230 | 268 | |
| 231 | - $messages = \ElasticPress\ElasticPressIo::factory()->get_endpoint_messages(); | |
| 269 | + $messages = \ElasticPress\get_container()->get( '\ElasticPress\ElasticPressIo' )->get_endpoint_messages(); | |
| 232 | 270 | |
| 233 | 271 | if ( empty( $messages ) ) { |
| 234 | 272 | return; |
| 235 | 273 | } |