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

158 lines 3.6 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\AIOSEO_V4_Importer_Conditional;
9 use Yoast\WP\SEO\Main;
10 use Yoast\WP\SEO\Services\Importing\Importer_Action_Filter_Trait;
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 Importer_Action_Filter_Trait;
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 * Importing_Route constructor.
37 *
38 * @param Importing_Action_Interface ...$importers All available importers.
39 */
40 public function __construct( Importing_Action_Interface ...$importers ) {
41 $this->importers = $importers;
42 }
43
44 /**
45 * Returns the conditionals based in which this loadable should be active.
46 *
47 * @return array
48 */
49 public static function get_conditionals() {
50 return [ AIOSEO_V4_Importer_Conditional::class ];
51 }
52
53 /**
54 * Registers routes with WordPress.
55 *
56 * @return void
57 */
58 public function register_routes() {
59 register_rest_route(
60 Main::API_V1_NAMESPACE,
61 self::ROUTE,
62 [
63 'callback' => [ $this, 'execute' ],
64 'permission_callback' => [ $this, 'is_user_permitted_to_import' ],
65 'methods' => [ 'POST' ],
66 ]
67 );
68 }
69
70 /**
71 * Executes the rest request.
72 *
73 * @param mixed $data The request parameters.
74 *
75 * @return WP_REST_Response|false Response or false on non-existent route.
76 */
77 public function execute( $data ) {
78 $plugin = (string) $data['plugin'];
79 $type = (string) $data['type'];
80
81 $next_url = $this->get_endpoint( $plugin, $type );
82
83 try {
84 $importer = $this->get_importer( $plugin, $type );
85
86 if ( $importer === false ) {
87 return new WP_Error(
88 'rest_no_route',
89 'Requested importer not found',
90 [
91 'status' => 404,
92 ]
93 );
94 }
95
96 $result = $importer->index();
97
98 if ( $result === false || count( $result ) === 0 ) {
99 $next_url = false;
100 }
101
102 return $this->respond_with(
103 $result,
104 $next_url
105 );
106 } catch ( \Exception $exception ) {
107 return new WP_Error(
108 'wpseo_error_indexing',
109 $exception->getMessage(),
110 [ 'stackTrace' => $exception->getTraceAsString() ]
111 );
112 }
113 }
114
115 /**
116 * Gets the right importer for the given arguments.
117 *
118 * @param string $plugin The plugin to import from.
119 * @param string $type The type of entity to import.
120 *
121 * @return Importing_Action_Interface|false The importer, or false if no importer was found.
122 */
123 protected function get_importer( $plugin, $type ) {
124 $importers = $this->filter_actions( $this->importers, $plugin, $type );
125
126 if ( count( $importers ) !== 1 ) {
127 return false;
128 }
129
130 return \current( $importers );
131 }
132
133 /**
134 * Gets the right endpoint for the given arguments.
135 *
136 * @param string $plugin The plugin to import from.
137 * @param string $type The type of entity to import.
138 *
139 * @return string|false The endpoint for the given action or false on failure of finding the one.
140 */
141 public function get_endpoint( $plugin, $type ) {
142 if ( empty( $plugin ) || empty( $type ) ) {
143 return false;
144 }
145
146 return Main::API_V1_NAMESPACE . "/import/{$plugin}/{$type}";
147 }
148
149 /**
150 * Whether or not the current user is allowed to import.
151 *
152 * @return bool Whether or not the current user is allowed to import.
153 */
154 public function is_user_permitted_to_import() {
155 return \current_user_can( 'activate_plugins' );
156 }
157 }
158