PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
← All changes | src/routes/importing-route.php +37 -24 18.1 → trunk View file →
@@ -1,14 +1,16 @@
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 -use Yoast\WP\SEO\Conditionals\AIOSEO_V4_Importer_Conditional;
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 -use Yoast\WP\SEO\Services\Importing\Importer_Action_Filter_Trait;
12 +use Yoast\WP\SEO\Services\Importing\Importable_Detector_Service;
11 13
12 14 /**
13 15 * Importing_Route class.
14 16 *
@@ -15,9 +17,9 @@
15 17 * Importing route for importing from other SEO plugins.
16 18 */
17 19 class Importing_Route extends Abstract_Action_Route {
18 20
19 - use Importer_Action_Filter_Trait;
21 + use No_Conditionals;
20 22
21 23 /**
22 24 * The import route constant.
23 25 *
@@ -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 *
@@ -32,23 +34,26 @@
32 34 */
33 35 protected $importers = [];
34 36
35 37 /**
36 - * Importing_Route constructor.
38 + * The importable detector service.
37 39 *
38 - * @param Importing_Action_Interface ...$importers All available importers.
40 + * @var Importable_Detector_Service
39 41 */
40 - public function __construct( Importing_Action_Interface ...$importers ) {
41 - $this->importers = $importers;
42 - }
42 + protected $importable_detector;
43 43
44 44 /**
45 - * Returns the conditionals based in which this loadable should be active.
45 + * Importing_Route constructor.
46 46 *
47 - * @return array
47 + * @param Importable_Detector_Service $importable_detector The importable detector service.
48 + * @param Importing_Action_Interface ...$importers All available importers.
48 49 */
49 - public static function get_conditionals() {
50 - return [ AIOSEO_V4_Importer_Conditional::class ];
50 + public function __construct(
51 + Importable_Detector_Service $importable_detector,
52 + Importing_Action_Interface ...$importers
53 + ) {
54 + $this->importable_detector = $importable_detector;
55 + $this->importers = $importers;
51 56 }
52 57
53 58 /**
54 59 * Registers routes with WordPress.
@@ -55,9 +60,9 @@
55 60 *
56 61 * @return void
57 62 */
58 63 public function register_routes() {
59 - register_rest_route(
64 + \register_rest_route(
60 65 Main::API_V1_NAMESPACE,
61 66 self::ROUTE,
62 67 [
63 68 'callback' => [ $this, 'execute' ],
@@ -62,14 +67,14 @@
62 67 [
63 68 'callback' => [ $this, 'execute' ],
64 69 'permission_callback' => [ $this, 'is_user_permitted_to_import' ],
65 70 'methods' => [ 'POST' ],
66 - ]
71 + ],
67 72 );
68 73 }
69 74
70 75 /**
71 - * Executes the rest request.
76 + * Executes the rest request, but only if the respective action is enabled.
72 77 *
73 78 * @param mixed $data The request parameters.
74 79 *
75 80 * @return WP_REST_Response|false Response or false on non-existent route.
@@ -82,33 +87,41 @@
82 87
83 88 try {
84 89 $importer = $this->get_importer( $plugin, $type );
85 90
86 - if ( $importer === false ) {
91 + if ( $importer === false || ! $importer->is_enabled() ) {
87 92 return new WP_Error(
88 93 'rest_no_route',
89 94 'Requested importer not found',
90 95 [
91 96 'status' => 404,
92 - ]
97 + ],
93 98 );
94 99 }
95 100
96 101 $result = $importer->index();
97 102
98 - if ( $result === false || count( $result ) === 0 ) {
103 + if ( $result === false || \count( $result ) === 0 ) {
99 104 $next_url = false;
100 105 }
101 106
102 107 return $this->respond_with(
103 108 $result,
104 - $next_url
109 + $next_url,
105 110 );
106 - } 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 +
107 120 return new WP_Error(
108 121 'wpseo_error_indexing',
109 122 $exception->getMessage(),
110 - [ 'stackTrace' => $exception->getTraceAsString() ]
123 + [ 'stackTrace' => $exception->getTraceAsString() ],
111 124 );
112 125 }
113 126 }
114 127
@@ -120,11 +133,11 @@
120 133 *
121 134 * @return Importing_Action_Interface|false The importer, or false if no importer was found.
122 135 */
123 136 protected function get_importer( $plugin, $type ) {
124 - $importers = $this->filter_actions( $this->importers, $plugin, $type );
137 + $importers = $this->importable_detector->filter_actions( $this->importers, $plugin, $type );
125 138
126 - if ( count( $importers ) !== 1 ) {
139 + if ( \count( $importers ) !== 1 ) {
127 140 return false;
128 141 }
129 142
130 143 return \current( $importers );