PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
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 / integrations / admin / import-integration.php

import-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at src/integrations/admin/import-integration.php

210 lines 7.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\Integrations\Admin;
4
5 use WPSEO_Admin_Asset_Manager;
6 use Yoast\WP\SEO\Conditionals\AIOSEO_V4_Importer_Conditional;
7 use Yoast\WP\SEO\Conditionals\Yoast_Tools_Page_Conditional;
8 use Yoast\WP\SEO\Conditionals\Import_Tool_Selected_Conditional;
9 use Yoast\WP\SEO\Integrations\Integration_Interface;
10 use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter;
11 use Yoast\WP\SEO\Services\Importing\Importable_Detector;
12 use Yoast\WP\SEO\Routes\Importing_Route;
13
14 /**
15 * Loads import script when on the Tool's page.
16 */
17 class Import_Integration implements Integration_Interface {
18
19 /**
20 * Contains the asset manager.
21 *
22 * @var WPSEO_Admin_Asset_Manager
23 */
24 protected $asset_manager;
25
26 /**
27 * Represents the AIOSEO V4 Importer conditional.
28 *
29 * @var AIOSEO_V4_Importer_Conditional
30 */
31 protected $importer_conditional;
32
33 /**
34 * The Importable Detector service.
35 *
36 * @var Importable_Detector
37 */
38 protected $importable_detector;
39
40 /**
41 * The Importing Route class.
42 *
43 * @var Importing_Route
44 */
45 protected $importing_route;
46
47 /**
48 * Returns the conditionals based on which this loadable should be active.
49 *
50 * @return array
51 */
52 public static function get_conditionals() {
53 return [
54 AIOSEO_V4_Importer_Conditional::class,
55 Import_Tool_Selected_Conditional::class,
56 Yoast_Tools_Page_Conditional::class,
57 ];
58 }
59
60 /**
61 * Import Integration constructor.
62 *
63 * @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
64 * @param AIOSEO_V4_Importer_Conditional $importer_conditional The AIOSEO V4 Importer conditional.
65 * @param Importable_Detector $importable_detector The importable detector.
66 * @param Importing_Route $importing_route The importing route.
67 */
68 public function __construct(
69 WPSEO_Admin_Asset_Manager $asset_manager,
70 AIOSEO_V4_Importer_Conditional $importer_conditional,
71 Importable_Detector $importable_detector,
72 Importing_Route $importing_route
73 ) {
74 $this->asset_manager = $asset_manager;
75 $this->importer_conditional = $importer_conditional;
76 $this->importable_detector = $importable_detector;
77 $this->importing_route = $importing_route;
78 }
79
80 /**
81 * Initializes the integration.
82 *
83 * This is the place to register hooks and filters.
84 *
85 * @return void
86 */
87 public function register_hooks() {
88 \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_import_script' ] );
89 }
90
91 /**
92 * Enqueues the Import script.
93 */
94 public function enqueue_import_script() {
95 \wp_enqueue_style( 'dashicons' );
96 $this->asset_manager->enqueue_script( 'import' );
97
98 $data = [
99 'restApi' => [
100 'root' => \esc_url_raw( \rest_url() ),
101 'cleanup_endpoints' => $this->get_cleanup_endpoints(),
102 'importing_endpoints' => $this->get_importing_endpoints(),
103 'nonce' => \wp_create_nonce( 'wp_rest' ),
104 ],
105 'assets' => [
106 'loading_msg_import' => \esc_html__( 'The import can take a long time depending on your site\'s size.', 'wordpress-seo' ),
107 'loading_msg_cleanup' => \esc_html__( 'The cleanup can take a long time depending on your site\'s size.', 'wordpress-seo' ),
108 'note' => \esc_html__( 'Note: ', 'wordpress-seo' ),
109 'cleanup_after_import_msg' => \esc_html__( 'After you\'ve imported data from another SEO plugin, please make sure to clean up all the original data from that plugin. (step 5)', 'wordpress-seo' ),
110 'select_placeholder' => \esc_html__( 'Select SEO plugin', 'wordpress-seo' ),
111 'no_data_msg' => \esc_html__( 'No data found from other SEO plugins.', 'wordpress-seo' ),
112 'import_failure' => $this->get_import_failure_alert( true ),
113 'cleanup_failure' => $this->get_import_failure_alert( false ),
114 'spinner' => \admin_url( 'images/loading.gif' ),
115 'replacing_texts' => [
116 'cleanup_button' => \esc_html__( 'Clean up', 'wordpress-seo' ),
117 'import_explanation' => \esc_html__( 'Please select an SEO plugin below to see what data can be imported.', 'wordpress-seo' ),
118 'cleanup_explanation' => \esc_html__( 'Once you\'re certain that your site is working properly with the imported data from another SEO plugin, you can clean up all the original data from that plugin.', 'wordpress-seo' ),
119 /* translators: %s: expands to the name of the plugin that is selected to be imported */
120 'select_header' => \esc_html__( 'The import from %s includes:', 'wordpress-seo' ),
121 'plugins' => [
122 'aioseo' => [
123 [
124 'data_name' => \esc_html__( 'Post metadata (SEO titles, descriptions, etc.)', 'wordpress-seo' ),
125 'data_note' => \esc_html__( 'Note: This metadata will only be imported if there is no existing Yoast SEO metadata yet.', 'wordpress-seo' ),
126 ],
127 [
128 'data_name' => \esc_html__( 'Default settings', 'wordpress-seo' ),
129 'data_note' => \esc_html__( 'Note: These settings will overwrite the default settings of Yoast SEO.', 'wordpress-seo' ),
130 ],
131 ],
132 'other' => [
133 [
134 'data_name' => \esc_html__( 'Post metadata (SEO titles, descriptions, etc.)', 'wordpress-seo' ),
135 'data_note' => \esc_html__( 'Note: This metadata will only be imported if there is no existing Yoast SEO metadata yet.', 'wordpress-seo' ),
136 ],
137 ],
138 ],
139 ],
140 ],
141 ];
142
143 /**
144 * Filter: 'wpseo_importing_data' Filter to adapt the data used in the import process.
145 *
146 * @param array $data The import data to adapt.
147 */
148 $data = \apply_filters( 'wpseo_importing_data', $data );
149
150 $this->asset_manager->localize_script( 'import', 'yoastImportData', $data );
151 }
152
153 /**
154 * Retrieves a list of the importing endpoints to use.
155 *
156 * @return array The endpoints.
157 */
158 protected function get_importing_endpoints() {
159 $available_actions = $this->importable_detector->detect_importers();
160 $importing_endpoints = [];
161
162 foreach ( $available_actions as $plugin => $types ) {
163 foreach ( $types as $type ) {
164 $importing_endpoints[ $plugin ][] = $this->importing_route->get_endpoint( $plugin, $type );
165 }
166 }
167
168 return $importing_endpoints;
169 }
170
171 /**
172 * Retrieves a list of the importing endpoints to use.
173 *
174 * @return array The endpoints.
175 */
176 protected function get_cleanup_endpoints() {
177 $available_actions = $this->importable_detector->detect_cleanups();
178 $importing_endpoints = [];
179
180 foreach ( $available_actions as $plugin => $types ) {
181 foreach ( $types as $type ) {
182 $importing_endpoints[ $plugin ][] = $this->importing_route->get_endpoint( $plugin, $type );
183 }
184 }
185
186 return $importing_endpoints;
187 }
188
189 /**
190 * Gets the import failure alert using the Alert_Presenter.
191 *
192 * @param bool $is_import Wether it's an import or not.
193 *
194 * @return string The import failure alert.
195 */
196 protected function get_import_failure_alert( $is_import ) {
197 $content = \esc_html__( 'Cleanup failed with the following error:', 'wordpress-seo' );
198 if ( $is_import ) {
199 $content = \esc_html__( 'Import failed with the following error:', 'wordpress-seo' );
200 }
201
202 $content .= '<br/><br/>';
203 $content .= \esc_html( '%s' );
204
205 $import_failure_alert = new Alert_Presenter( $content, 'error' );
206
207 return $import_failure_alert->present();
208 }
209 }
210