| @@ -7,10 +7,10 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ElasticPress\StatusReport; |
| 10 | 10 | |
| 11 | -use \ElasticPress\QueryLogger; | |
| 12 | -use \ElasticPress\Utils; | |
| 11 | +use ElasticPress\QueryLogger; | |
| 12 | +use ElasticPress\Utils; | |
| 13 | 13 | |
| 14 | 14 | defined( 'ABSPATH' ) || exit; |
| 15 | 15 | |
| 16 | 16 | /** |
| @@ -40,9 +40,9 @@ | ||
| 40 | 40 | * Return the report title |
| 41 | 41 | * |
| 42 | 42 | * @return string |
| 43 | 43 | */ |
| 44 | - public function get_title() : string { | |
| 44 | + public function get_title(): string { | |
| 45 | 45 | return __( 'Failed Queries', 'elasticpress' ); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** |
| @@ -49,9 +49,9 @@ | ||
| 49 | 49 | * Return the report fields |
| 50 | 50 | * |
| 51 | 51 | * @return array |
| 52 | 52 | */ |
| 53 | - public function get_groups() : array { | |
| 53 | + public function get_groups(): array { | |
| 54 | 54 | $this->maybe_clear_logs(); |
| 55 | 55 | |
| 56 | 56 | $logs = $this->query_logger->get_logs( false ); |
| 57 | 57 | |
| @@ -107,9 +107,9 @@ | ||
| 107 | 107 | * Return the output of a button to clear the logged queries |
| 108 | 108 | * |
| 109 | 109 | * @return string |
| 110 | 110 | */ |
| 111 | - public function get_actions() : array { | |
| 111 | + public function get_actions(): array { | |
| 112 | 112 | global $wp; |
| 113 | 113 | |
| 114 | 114 | $logs = $this->query_logger->get_logs( false ); |
| 115 | 115 | |
| @@ -131,13 +131,22 @@ | ||
| 131 | 131 | /** |
| 132 | 132 | * If a nonce is present, clear the logs |
| 133 | 133 | */ |
| 134 | 134 | protected function maybe_clear_logs() { |
| 135 | - if ( empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'ep-clear-logged-queries' ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 135 | + if ( empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'ep-clear-logged-queries' ) ) { | |
| 136 | 136 | return; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | $this->query_logger->clear_logs(); |
| 140 | + | |
| 141 | + if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { | |
| 142 | + $redirect_url = network_admin_url( 'admin.php?page=elasticpress-status-report' ); | |
| 143 | + } else { | |
| 144 | + $redirect_url = admin_url( 'admin.php?page=elasticpress-status-report' ); | |
| 145 | + } | |
| 146 | + | |
| 147 | + wp_safe_redirect( $redirect_url ); | |
| 148 | + exit(); | |
| 140 | 149 | } |
| 141 | 150 | |
| 142 | 151 | /** |
| 143 | 152 | * Given a log, try to find the error and its solution |
| @@ -145,20 +154,19 @@ | ||
| 145 | 154 | * @param array $log The log |
| 146 | 155 | * @return array The error in index 0, solution in index 1 |
| 147 | 156 | */ |
| 148 | 157 | public function analyze_log( $log ) { |
| 149 | - $error = ''; | |
| 150 | - | |
| 151 | - if ( ! empty( $log['result']['error'] ) && ! empty( $log['result']['error']['root_cause'][0]['reason'] ) ) { | |
| 152 | - $error = $log['result']['error']['root_cause'][0]['reason']; | |
| 158 | + if ( is_array( $log['result'] ) && ! empty( $log['result']['is_wp_error'] ) ) { | |
| 159 | + return [ | |
| 160 | + $log['result']['message'], | |
| 161 | + __( 'It seems WordPress was not able to complete the request. Review the error message and your configuration.', 'elasticpress' ), | |
| 162 | + ]; | |
| 153 | 163 | } |
| 154 | 164 | |
| 155 | - if ( ! empty( $log['result']['errors'] ) && ! empty( $log['result']['items'] ) && ! empty( $log['result']['items'][0]['index']['error']['reason'] ) ) { | |
| 156 | - $error = $log['result']['items'][0]['index']['error']['reason']; | |
| 157 | - } | |
| 165 | + $error = Utils\get_elasticsearch_error_reason( $log ); | |
| 158 | 166 | |
| 159 | 167 | $solution = ( ! empty( $error ) ) ? |
| 160 | - $this->maybe_suggest_solution_for_es( $error ) : | |
| 168 | + ( new \ElasticPress\ElasticsearchErrorInterpreter() )->maybe_suggest_solution_for_es( $error )['solution'] : | |
| 161 | 169 | ''; |
| 162 | 170 | |
| 163 | 171 | return [ $error, $solution ]; |
| 164 | 172 | } |
| @@ -163,72 +171,16 @@ | ||
| 163 | 171 | return [ $error, $solution ]; |
| 164 | 172 | } |
| 165 | 173 | |
| 166 | 174 | /** |
| 167 | - * Given an Elasticsearch error, try to suggest a solution | |
| 175 | + * DEPRECATED. Given an Elasticsearch error, try to suggest a solution | |
| 168 | 176 | * |
| 177 | + * @deprecated 5.0.0 | |
| 169 | 178 | * @param string $error The error |
| 170 | 179 | * @return string |
| 171 | 180 | */ |
| 172 | 181 | protected function maybe_suggest_solution_for_es( $error ) { |
| 173 | - $sync_url = Utils\get_sync_url(); | |
| 182 | + _deprecated_function( __METHOD__, '5.0.0', '\ElasticPress\ElasticsearchErrorInterpreter::maybe_suggest_solution_for_es()' ); | |
| 174 | 183 | |
| 175 | - if ( preg_match( '/no such index \[(.*?)\]/', $error, $matches ) ) { | |
| 176 | - return sprintf( | |
| 177 | - /* translators: 1. Index name; 2. Sync Page URL */ | |
| 178 | - __( 'It seems the %1$s index is missing. <a href="%2$s">Delete all data and sync</a> to fix the issue.', 'elasticpress' ), | |
| 179 | - '<code>' . $matches[1] . '</code>', | |
| 180 | - $sync_url | |
| 181 | - ); | |
| 182 | - } | |
| 183 | - | |
| 184 | - if ( preg_match( '/No mapping found for \[(.*?)\] in order to sort on/', $error, $matches ) ) { | |
| 185 | - return sprintf( | |
| 186 | - /* translators: 1. Index name; 2. Sync Page URL */ | |
| 187 | - __( 'The field %1$s was not found. Make sure it is added to the list of indexed fields and run <a href="%2$s">a new sync</a> to fix the issue.', 'elasticpress' ), | |
| 188 | - '<code>' . $matches[1] . '</code>', | |
| 189 | - $sync_url | |
| 190 | - ); | |
| 191 | - } | |
| 192 | - | |
| 193 | - /* translators: 1. Field name; 2. Sync Page URL */ | |
| 194 | - $field_type_solution = __( 'It seems you saved a post without doing a full sync first because <code>%1$s</code> is missing the correct mapping type. <a href="%2$s">Delete all data and sync</a> to fix the issue.', 'elasticpress' ); | |
| 195 | - | |
| 196 | - if ( preg_match( '/Fielddata is disabled on text fields by default. Set fielddata=true on \[(.*?)\]/', $error, $matches ) ) { | |
| 197 | - return sprintf( $field_type_solution, $matches[1], $sync_url ); | |
| 198 | - } | |
| 199 | - | |
| 200 | - if ( preg_match( '/field \[(.*?)\] is of type \[(.*?)\], but only numeric types are supported./', $error, $matches ) ) { | |
| 201 | - return sprintf( $field_type_solution, $matches[1], $sync_url ); | |
| 202 | - } | |
| 203 | - | |
| 204 | - if ( preg_match( '/Alternatively, set fielddata=true on \[(.*?)\] in order to load field data by uninverting the inverted index./', $error, $matches ) ) { | |
| 205 | - return sprintf( $field_type_solution, $matches[1], $sync_url ); | |
| 206 | - } | |
| 207 | - | |
| 208 | - if ( preg_match( '/Limit of total fields \[(.*?)\] in index \[(.*?)\] has been exceeded/', $error, $matches ) ) { | |
| 209 | - return sprintf( | |
| 210 | - /* translators: Elasticsearch or ElasticPress.io; 2. Link to article; 3. Link to article */ | |
| 211 | - __( 'Your website content has more public custom fields than %1$s is able to store. Check our articles about <a href="%2$s">Elasticsearch field limitations</a> and <a href="%3$s">how to index just the custom fields you need</a> and sync again.', 'elasticpress' ), | |
| 212 | - Utils\is_epio() ? __( 'ElasticPress.io', 'elasticpress' ) : __( 'Elasticsearch', 'elasticpress' ), | |
| 213 | - 'https://elasticpress.zendesk.com/hc/en-us/articles/360051401212-I-get-the-error-Limit-of-total-fields-in-index-has-been-exceeded-', | |
| 214 | - 'https://elasticpress.zendesk.com/hc/en-us/articles/360052019111' | |
| 215 | - ); | |
| 216 | - } | |
| 217 | - | |
| 218 | - // field limit | |
| 219 | - | |
| 220 | - if ( Utils\is_epio() ) { | |
| 221 | - return sprintf( | |
| 222 | - /* translators: ElasticPress.io My Account URL */ | |
| 223 | - __( 'We did not recognize this error. Please open an ElasticPress.io <a href="%s">support ticket</a> so we can troubleshoot further.', 'elasticpress' ), | |
| 224 | - 'https://www.elasticpress.io/my-account/' | |
| 225 | - ); | |
| 226 | - } | |
| 227 | - | |
| 228 | - return sprintf( | |
| 229 | - /* translators: New GitHub issue URL */ | |
| 230 | - __( 'We did not recognize this error. Please consider opening a <a href="%s">GitHub Issue</a> so we can add it to our list of supported errors. ', 'elasticpress' ), | |
| 231 | - 'https://github.com/10up/ElasticPress/issues/new/choose' | |
| 232 | - ); | |
| 184 | + return ( new \ElasticPress\ElasticsearchErrorInterpreter() )->maybe_suggest_solution_for_es( $error )['solution']; | |
| 233 | 185 | } |
| 234 | 186 | } |