| @@ -1,12 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Routes; |
| 4 | 4 | |
| 5 | +use Exception; | |
| 5 | 6 | use WP_Error; |
| 6 | 7 | use WP_REST_Response; |
| 7 | 8 | use Yoast\WP\SEO\Actions\Importing\Importing_Action_Interface; |
| 8 | 9 | use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 10 | +use Yoast\WP\SEO\Exceptions\Importing\Aioseo_Validation_Exception; | |
| 9 | 11 | use Yoast\WP\SEO\Main; |
| 10 | 12 | use Yoast\WP\SEO\Services\Importing\Importable_Detector_Service; |
| 11 | 13 | |
| 12 | 14 | /** |
| @@ -22,9 +24,9 @@ | ||
| 22 | 24 | * The import route constant. |
| 23 | 25 | * |
| 24 | 26 | * @var string |
| 25 | 27 | */ |
| 26 | - const ROUTE = '/import/(?P<plugin>[\w-]+)/(?P<type>[\w-]+)'; | |
| 28 | + public const ROUTE = '/import/(?P<plugin>[\w-]+)/(?P<type>[\w-]+)'; | |
| 27 | 29 | |
| 28 | 30 | /** |
| 29 | 31 | * List of available importers. |
| 30 | 32 | * |
| @@ -58,9 +60,9 @@ | ||
| 58 | 60 | * |
| 59 | 61 | * @return void |
| 60 | 62 | */ |
| 61 | 63 | public function register_routes() { |
| 62 | - register_rest_route( | |
| 64 | + \register_rest_route( | |
| 63 | 65 | Main::API_V1_NAMESPACE, |
| 64 | 66 | self::ROUTE, |
| 65 | 67 | [ |
| 66 | 68 | 'callback' => [ $this, 'execute' ], |
| @@ -65,9 +67,9 @@ | ||
| 65 | 67 | [ |
| 66 | 68 | 'callback' => [ $this, 'execute' ], |
| 67 | 69 | 'permission_callback' => [ $this, 'is_user_permitted_to_import' ], |
| 68 | 70 | 'methods' => [ 'POST' ], |
| 69 | - ] | |
| 71 | + ], | |
| 70 | 72 | ); |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | 75 | /** |
| @@ -91,27 +93,35 @@ | ||
| 91 | 93 | 'rest_no_route', |
| 92 | 94 | 'Requested importer not found', |
| 93 | 95 | [ |
| 94 | 96 | 'status' => 404, |
| 95 | - ] | |
| 97 | + ], | |
| 96 | 98 | ); |
| 97 | 99 | } |
| 98 | 100 | |
| 99 | 101 | $result = $importer->index(); |
| 100 | 102 | |
| 101 | - if ( $result === false || count( $result ) === 0 ) { | |
| 103 | + if ( $result === false || \count( $result ) === 0 ) { | |
| 102 | 104 | $next_url = false; |
| 103 | 105 | } |
| 104 | 106 | |
| 105 | 107 | return $this->respond_with( |
| 106 | 108 | $result, |
| 107 | - $next_url | |
| 109 | + $next_url, | |
| 108 | 110 | ); |
| 109 | - } catch ( \Exception $exception ) { | |
| 111 | + } catch ( Exception $exception ) { | |
| 112 | + if ( $exception instanceof Aioseo_Validation_Exception ) { | |
| 113 | + return new WP_Error( | |
| 114 | + 'wpseo_error_validation', | |
| 115 | + $exception->getMessage(), | |
| 116 | + [ 'stackTrace' => $exception->getTraceAsString() ], | |
| 117 | + ); | |
| 118 | + } | |
| 119 | + | |
| 110 | 120 | return new WP_Error( |
| 111 | 121 | 'wpseo_error_indexing', |
| 112 | 122 | $exception->getMessage(), |
| 113 | - [ 'stackTrace' => $exception->getTraceAsString() ] | |
| 123 | + [ 'stackTrace' => $exception->getTraceAsString() ], | |
| 114 | 124 | ); |
| 115 | 125 | } |
| 116 | 126 | } |
| 117 | 127 | |
| @@ -125,9 +135,9 @@ | ||
| 125 | 135 | */ |
| 126 | 136 | protected function get_importer( $plugin, $type ) { |
| 127 | 137 | $importers = $this->importable_detector->filter_actions( $this->importers, $plugin, $type ); |
| 128 | 138 | |
| 129 | - if ( count( $importers ) !== 1 ) { | |
| 139 | + if ( \count( $importers ) !== 1 ) { | |
| 130 | 140 | return false; |
| 131 | 141 | } |
| 132 | 142 | |
| 133 | 143 | return \current( $importers ); |