| @@ -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. |
| @@ -56,12 +57,11 @@ | ||
| 56 | 57 | |
| 57 | 58 | $plain_text_reports = []; |
| 58 | 59 | |
| 59 | 60 | foreach ( $reports as $report ) { |
| 60 | - $title = $report['title']; | |
| 61 | - $groups = $report['groups']; | |
| 62 | - | |
| 63 | - $plain_text_reports[] = $this->render_copy_paste_report( $title, $groups ); | |
| 61 | + $title = $report['title']; | |
| 62 | + $groups = $report['groups']; | |
| 63 | + $plain_text_reports[] = $this->render_copy_paste_report( $title, $groups, $report['isAjaxReport'] ); | |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | $plain_text_report = implode( "\n\n", $plain_text_reports ); |
| 67 | 67 | |
| @@ -70,8 +70,9 @@ | ||
| 70 | 70 | 'epStatusReport', |
| 71 | 71 | [ |
| 72 | 72 | 'plainTextReport' => $plain_text_report, |
| 73 | 73 | 'reports' => $reports, |
| 74 | + 'nonce' => wp_create_nonce( 'ep-status-report-nonce' ), | |
| 74 | 75 | ] |
| 75 | 76 | ); |
| 76 | 77 | |
| 77 | 78 | wp_enqueue_style( |
| @@ -82,13 +83,50 @@ | ||
| 82 | 83 | ); |
| 83 | 84 | } |
| 84 | 85 | |
| 85 | 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 | + /** | |
| 86 | 124 | * Return all reports available |
| 87 | 125 | * |
| 88 | 126 | * @return array |
| 89 | 127 | */ |
| 90 | - public function get_reports() : array { | |
| 128 | + public function get_reports(): array { | |
| 91 | 129 | $reports = []; |
| 92 | 130 | |
| 93 | 131 | $query_logger = \ElasticPress\get_container()->get( '\ElasticPress\QueryLogger' ); |
| 94 | 132 | |
| @@ -124,9 +162,9 @@ | ||
| 124 | 162 | // phpcs:enable WordPress.Security.NonceVerification |
| 125 | 163 | |
| 126 | 164 | $filtered_reports = array_filter( |
| 127 | 165 | $filtered_reports, |
| 128 | - function( $report_slug ) use ( $skipped_reports ) { | |
| 166 | + function ( $report_slug ) use ( $skipped_reports ) { | |
| 129 | 167 | return ! in_array( $report_slug, $skipped_reports, true ); |
| 130 | 168 | }, |
| 131 | 169 | ARRAY_FILTER_USE_KEY |
| 132 | 170 | ); |
| @@ -139,19 +177,20 @@ | ||
| 139 | 177 | * |
| 140 | 178 | * @since 4.5.0 |
| 141 | 179 | * @return array |
| 142 | 180 | */ |
| 143 | - protected function get_formatted_reports() : array { | |
| 181 | + protected function get_formatted_reports(): array { | |
| 144 | 182 | if ( empty( $this->formatted_reports ) ) { |
| 145 | 183 | $reports = $this->get_reports(); |
| 146 | 184 | |
| 147 | 185 | $this->formatted_reports = array_map( |
| 148 | - function( $report ) { | |
| 186 | + function ( $report ) { | |
| 149 | 187 | return [ |
| 150 | - 'actions' => $report->get_actions(), | |
| 151 | - 'groups' => $report->get_groups(), | |
| 152 | - 'messages' => $report->get_messages(), | |
| 153 | - '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, | |
| 154 | 193 | ]; |
| 155 | 194 | }, |
| 156 | 195 | $reports |
| 157 | 196 | ); |
| @@ -163,13 +202,20 @@ | ||
| 163 | 202 | * Render the copy & paste report |
| 164 | 203 | * |
| 165 | 204 | * @param string $title Report title |
| 166 | 205 | * @param array $groups Report groups |
| 206 | + * @param bool $is_ajax_report Whether the report is an AJAX report | |
| 207 | + * | |
| 167 | 208 | * @return string |
| 168 | 209 | */ |
| 169 | - 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 { | |
| 170 | 211 | $output = "## {$title} ##\n\n"; |
| 171 | 212 | |
| 213 | + if ( $is_ajax_report ) { | |
| 214 | + $output .= $this->render_pending_generation(); | |
| 215 | + return $output; | |
| 216 | + } | |
| 217 | + | |
| 172 | 218 | foreach ( $groups as $group ) { |
| 173 | 219 | $output .= "### {$group['title']} ###\n"; |
| 174 | 220 | foreach ( $group['fields'] as $slug => $field ) { |
| 175 | 221 | $value = $field['value'] ?? ''; |
| @@ -202,8 +248,17 @@ | ||
| 202 | 248 | return (string) $value; |
| 203 | 249 | } |
| 204 | 250 | |
| 205 | 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 | + /** | |
| 206 | 261 | * Display a badge in the admin menu if there's admin notices from |
| 207 | 262 | * ElasticPress.io. |
| 208 | 263 | * |
| 209 | 264 | * @return void |
| @@ -210,9 +265,9 @@ | ||
| 210 | 265 | */ |
| 211 | 266 | public function admin_menu_count() { |
| 212 | 267 | global $menu, $submenu; |
| 213 | 268 | |
| 214 | - $messages = \ElasticPress\ElasticPressIo::factory()->get_endpoint_messages(); | |
| 269 | + $messages = \ElasticPress\get_container()->get( '\ElasticPress\ElasticPressIo' )->get_endpoint_messages(); | |
| 215 | 270 | |
| 216 | 271 | if ( empty( $messages ) ) { |
| 217 | 272 | return; |
| 218 | 273 | } |