PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.4
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
wordpress-seo / src / routes / importing-route.php

importing-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.4, at src/routes/importing-route.php

171 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Routes;
4
5 use Exception;
6 use WP_Error;
7 use WP_REST_Response;
8 use Yoast\WP\SEO\Actions\Importing\Importing_Action_Interface;
9 use Yoast\WP\SEO\Conditionals\No_Conditionals;
10 use Yoast\WP\SEO\Exceptions\Importing\Aioseo_Validation_Exception;
11 use Yoast\WP\SEO\Main;
12 use Yoast\WP\SEO\Services\Importing\Importable_Detector_Service;
13
14 /**
15 * Importing_Route class.
16 *
17 * Importing route for importing from other SEO plugins.
18 */
19 class Importing_Route extends Abstract_Action_Route {
20
21 use No_Conditionals;
22
23 /**
24 * The import route constant.
25 *
26 * @var string
27 */
28 public const ROUTE = '/import/(?P<plugin>[\w-]+)/(?P<type>[\w-]+)';
29
30 /**
31 * List of available importers.
32 *
33 * @var Importing_Action_Interface[]
34 */
35 protected $importers = [];
36
37 /**
38 * The importable detector service.
39 *
40 * @var Importable_Detector_Service
41 */
42 protected $importable_detector;
43
44 /**
45 * Importing_Route constructor.
46 *
47 * @param Importable_Detector_Service $importable_detector The importable detector service.
48 * @param Importing_Action_Interface ...$importers All available importers.
49 */
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;
56 }
57
58 /**
59 * Registers routes with WordPress.
60 *
61 * @return void
62 */
63 public function register_routes() {
64 \register_rest_route(
65 Main::API_V1_NAMESPACE,
66 self::ROUTE,
67 [
68 'callback' => [ $this, 'execute' ],
69 'permission_callback' => [ $this, 'is_user_permitted_to_import' ],
70 'methods' => [ 'POST' ],
71 ],
72 );
73 }
74
75 /**
76 * Executes the rest request, but only if the respective action is enabled.
77 *
78 * @param mixed $data The request parameters.
79 *
80 * @return WP_REST_Response|false Response or false on non-existent route.
81 */
82 public function execute( $data ) {
83 $plugin = (string) $data['plugin'];
84 $type = (string) $data['type'];
85
86 $next_url = $this->get_endpoint( $plugin, $type );
87
88 try {
89 $importer = $this->get_importer( $plugin, $type );
90
91 if ( $importer === false || ! $importer->is_enabled() ) {
92 return new WP_Error(
93 'rest_no_route',
94 'Requested importer not found',
95 [
96 'status' => 404,
97 ],
98 );
99 }
100
101 $result = $importer->index();
102
103 if ( $result === false || \count( $result ) === 0 ) {
104 $next_url = false;
105 }
106
107 return $this->respond_with(
108 $result,
109 $next_url,
110 );
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
120 return new WP_Error(
121 'wpseo_error_indexing',
122 $exception->getMessage(),
123 [ 'stackTrace' => $exception->getTraceAsString() ],
124 );
125 }
126 }
127
128 /**
129 * Gets the right importer for the given arguments.
130 *
131 * @param string $plugin The plugin to import from.
132 * @param string $type The type of entity to import.
133 *
134 * @return Importing_Action_Interface|false The importer, or false if no importer was found.
135 */
136 protected function get_importer( $plugin, $type ) {
137 $importers = $this->importable_detector->filter_actions( $this->importers, $plugin, $type );
138
139 if ( \count( $importers ) !== 1 ) {
140 return false;
141 }
142
143 return \current( $importers );
144 }
145
146 /**
147 * Gets the right endpoint for the given arguments.
148 *
149 * @param string $plugin The plugin to import from.
150 * @param string $type The type of entity to import.
151 *
152 * @return string|false The endpoint for the given action or false on failure of finding the one.
153 */
154 public function get_endpoint( $plugin, $type ) {
155 if ( empty( $plugin ) || empty( $type ) ) {
156 return false;
157 }
158
159 return Main::API_V1_NAMESPACE . "/import/{$plugin}/{$type}";
160 }
161
162 /**
163 * Whether or not the current user is allowed to import.
164 *
165 * @return bool Whether or not the current user is allowed to import.
166 */
167 public function is_user_permitted_to_import() {
168 return \current_user_can( 'activate_plugins' );
169 }
170 }
171