• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.52
/src/integrations/admin/import-integration.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Admin;
4

5
use WPSEO_Admin_Asset_Manager;
6
use Yoast\WP\SEO\Conditionals\Import_Tool_Selected_Conditional;
7
use Yoast\WP\SEO\Conditionals\Yoast_Tools_Page_Conditional;
8
use Yoast\WP\SEO\Integrations\Integration_Interface;
9
use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter;
10
use Yoast\WP\SEO\Routes\Importing_Route;
11
use Yoast\WP\SEO\Services\Importing\Importable_Detector_Service;
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() {
2✔
45
                return [
1✔
46
                        Import_Tool_Selected_Conditional::class,
2✔
47
                        Yoast_Tools_Page_Conditional::class,
1✔
48
                ];
1✔
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(
2✔
59
                WPSEO_Admin_Asset_Manager $asset_manager,
60
                Importable_Detector_Service $importable_detector,
61
                Importing_Route $importing_route
62
        ) {
1✔
63
                $this->asset_manager       = $asset_manager;
2✔
64
                $this->importable_detector = $importable_detector;
2✔
65
                $this->importing_route     = $importing_route;
2✔
66
        }
1✔
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() {
2✔
76
                \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_import_script' ] );
2✔
77
        }
1✔
78

79
        /**
80
         * Enqueues the Import script.
81
         */
82
        public function enqueue_import_script() {
2✔
83
                \wp_enqueue_style( 'dashicons' );
2✔
84
                $this->asset_manager->enqueue_script( 'import' );
2✔
85

86
                $data = [
1✔
87
                        'restApi' => [
1✔
88
                                'root'                => \esc_url_raw( \rest_url() ),
2✔
89
                                'cleanup_endpoints'   => $this->get_cleanup_endpoints(),
2✔
90
                                'importing_endpoints' => $this->get_importing_endpoints(),
2✔
91
                                'nonce'               => \wp_create_nonce( 'wp_rest' ),
2✔
92
                        ],
1✔
93
                        'assets'  => [
1✔
94
                                'loading_msg_import'       => \esc_html__( 'The import can take a long time depending on your site\'s size.', 'wordpress-seo' ),
2✔
95
                                'loading_msg_cleanup'      => \esc_html__( 'The cleanup can take a long time depending on your site\'s size.', 'wordpress-seo' ),
2✔
96
                                'note'                     => \esc_html__( 'Note: ', 'wordpress-seo' ),
2✔
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' ),
2✔
98
                                'select_placeholder'       => \esc_html__( 'Select SEO plugin', 'wordpress-seo' ),
2✔
99
                                'no_data_msg'              => \esc_html__( 'No data found from other SEO plugins.', 'wordpress-seo' ),
2✔
100
                                'validation_failure'       => $this->get_validation_failure_alert(),
2✔
101
                                'import_failure'           => $this->get_import_failure_alert( true ),
2✔
102
                                'cleanup_failure'          => $this->get_import_failure_alert( false ),
2✔
103
                                'spinner'                  => \admin_url( 'images/loading.gif' ),
2✔
104
                                'replacing_texts'          => [
1✔
105
                                        'cleanup_button'       => \esc_html__( 'Clean up', 'wordpress-seo' ),
2✔
106
                                        'import_explanation'   => \esc_html__( 'Please select an SEO plugin below to see what data can be imported.', 'wordpress-seo' ),
2✔
107
                                        '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' ),
2✔
108
                                        /* translators: %s: expands to the name of the plugin that is selected to be imported */
109
                                        'select_header'        => \esc_html__( 'The import from %s includes:', 'wordpress-seo' ),
2✔
110
                                        'plugins'              => [
1✔
111
                                                'aioseo' => [
1✔
112
                                                        [
1✔
113
                                                                'data_name' => \esc_html__( 'Post metadata (SEO titles, descriptions, etc.)', 'wordpress-seo' ),
2✔
114
                                                                'data_note' => \esc_html__( 'Note: This metadata will only be imported if there is no existing Yoast SEO metadata yet.', 'wordpress-seo' ),
2✔
115
                                                        ],
1✔
116
                                                        [
1✔
117
                                                                'data_name' => \esc_html__( 'Default settings', 'wordpress-seo' ),
2✔
118
                                                                'data_note' => \esc_html__( 'Note: These settings will overwrite the default settings of Yoast SEO.', 'wordpress-seo' ),
2✔
119
                                                        ],
1✔
120
                                                ],
1✔
121
                                                'other' => [
1✔
122
                                                        [
1✔
123
                                                                'data_name' => \esc_html__( 'Post metadata (SEO titles, descriptions, etc.)', 'wordpress-seo' ),
2✔
124
                                                                'data_note' => \esc_html__( 'Note: This metadata will only be imported if there is no existing Yoast SEO metadata yet.', 'wordpress-seo' ),
2✔
125
                                                        ],
1✔
126
                                                ],
1✔
127
                                        ],
1✔
128
                                ],
1✔
129
                        ],
1✔
130
                ];
1✔
131

132
                /**
133
                 * Filter: 'wpseo_importing_data' Filter to adapt the data used in the import process.
134
                 *
135
                 * @param array $data The import data to adapt.
136
                 */
137
                $data = \apply_filters( 'wpseo_importing_data', $data );
2✔
138

139
                $this->asset_manager->localize_script( 'import', 'yoastImportData', $data );
2✔
140
        }
1✔
141

142
        /**
143
         * Retrieves a list of the importing endpoints to use.
144
         *
145
         * @return array The endpoints.
146
         */
147
        protected function get_importing_endpoints() {
2✔
148
                $available_actions   = $this->importable_detector->detect_importers();
2✔
149
                $importing_endpoints = [];
2✔
150

151
                $available_sorted_actions = $this->sort_actions( $available_actions );
2✔
152

153
                foreach ( $available_sorted_actions as $plugin => $types ) {
2✔
154
                        foreach ( $types as $type ) {
2✔
155
                                $importing_endpoints[ $plugin ][] = $this->importing_route->get_endpoint( $plugin, $type );
2✔
156
                        }
157
                }
158

159
                return $importing_endpoints;
2✔
160
        }
161

162
        /**
163
         * Sorts the array of importing actions, by moving any validating actions to the start for every plugin.
164
         *
165
         * @param array $available_actions The array of actions that we want to sort.
166
         *
167
         * @return array The sorted array of actions.
168
         */
169
        protected function sort_actions( $available_actions ) {
2✔
170
                $first_action             = 'validate_data';
2✔
171
                $available_sorted_actions = [];
2✔
172

173
                foreach ( $available_actions as $plugin => $plugin_available_actions ) {
2✔
174

175
                        $validate_action_position = \array_search( $first_action, $plugin_available_actions, true );
2✔
176

177
                        if ( ! empty( $validate_action_position ) ) {
2✔
178
                                unset( $plugin_available_actions[ $validate_action_position ] );
2✔
179
                                \array_unshift( $plugin_available_actions, $first_action );
2✔
180
                        }
181

182
                        $available_sorted_actions[ $plugin ] = $plugin_available_actions;
2✔
183
                }
184

185
                return $available_sorted_actions;
2✔
186
        }
187

188
        /**
189
         * Retrieves a list of the importing endpoints to use.
190
         *
191
         * @return array The endpoints.
192
         */
193
        protected function get_cleanup_endpoints() {
×
194
                $available_actions   = $this->importable_detector->detect_cleanups();
×
195
                $importing_endpoints = [];
×
196

197
                foreach ( $available_actions as $plugin => $types ) {
×
198
                        foreach ( $types as $type ) {
×
199
                                $importing_endpoints[ $plugin ][] = $this->importing_route->get_endpoint( $plugin, $type );
×
200
                        }
201
                }
202

203
                return $importing_endpoints;
×
204
        }
205

206
        /**
207
         * Gets the validation failure alert using the Alert_Presenter.
208
         *
209
         * @return string The validation failure alert.
210
         */
211
        protected function get_validation_failure_alert() {
2✔
212
                $content  = \esc_html__( 'The AIOSEO import was cancelled because some AIOSEO data is missing. Please try and take the following steps to fix this:', 'wordpress-seo' );
2✔
213
                $content .= '<br/>';
2✔
214
                $content .= '<ol><li>';
2✔
215
                $content .= \esc_html__( 'If you have never saved any AIOSEO \'Search Appearance\' settings, please do that first and run the import again.', 'wordpress-seo' );
2✔
216
                $content .= '</li>';
2✔
217
                $content .= '<li>';
2✔
218
                $content .= \esc_html__( 'If you already have saved AIOSEO \'Search Appearance\' settings and the issue persists, please contact our support team so we can take a closer look.', 'wordpress-seo' );
2✔
219
                $content .= '</li></ol>';
2✔
220

221
                $validation_failure_alert = new Alert_Presenter( $content, 'error' );
2✔
222

223
                return $validation_failure_alert->present();
2✔
224
        }
225

226
        /**
227
         * Gets the import failure alert using the Alert_Presenter.
228
         *
229
         * @param bool $is_import Wether it's an import or not.
230
         *
231
         * @return string The import failure alert.
232
         */
233
        protected function get_import_failure_alert( $is_import ) {
2✔
234
                $content = \esc_html__( 'Cleanup failed with the following error:', 'wordpress-seo' );
2✔
235
                if ( $is_import ) {
2✔
236
                        $content = \esc_html__( 'Import failed with the following error:', 'wordpress-seo' );
2✔
237
                }
238

239
                $content .= '<br/><br/>';
2✔
240
                $content .= \esc_html( '%s' );
2✔
241

242
                $import_failure_alert = new Alert_Presenter( $content, 'error' );
2✔
243

244
                return $import_failure_alert->present();
2✔
245
        }
246
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc