PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.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 18.4, at src/routes/importing-route.php

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