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

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