| @@ -2,15 +2,14 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Integrations\Admin; |
| 4 | 4 | |
| 5 | 5 | use WPSEO_Admin_Asset_Manager; |
| 6 | -use Yoast\WP\SEO\Conditionals\AIOSEO_V4_Importer_Conditional; | |
| 6 | +use Yoast\WP\SEO\Conditionals\Import_Tool_Selected_Conditional; | |
| 7 | 7 | use Yoast\WP\SEO\Conditionals\Yoast_Tools_Page_Conditional; |
| 8 | -use Yoast\WP\SEO\Conditionals\Import_Tool_Selected_Conditional; | |
| 9 | 8 | use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 10 | 9 | use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter; |
| 11 | -use Yoast\WP\SEO\Services\Importing\Importable_Detector; | |
| 12 | 10 | use Yoast\WP\SEO\Routes\Importing_Route; |
| 11 | +use Yoast\WP\SEO\Services\Importing\Importable_Detector_Service; | |
| 13 | 12 | |
| 14 | 13 | /** |
| 15 | 14 | * Loads import script when on the Tool's page. |
| 16 | 15 | */ |
| @@ -23,18 +22,11 @@ | ||
| 23 | 22 | */ |
| 24 | 23 | protected $asset_manager; |
| 25 | 24 | |
| 26 | 25 | /** |
| 27 | - * Represents the AIOSEO V4 Importer conditional. | |
| 28 | - * | |
| 29 | - * @var AIOSEO_V4_Importer_Conditional | |
| 30 | - */ | |
| 31 | - protected $importer_conditional; | |
| 32 | - | |
| 33 | - /** | |
| 34 | 26 | * The Importable Detector service. |
| 35 | 27 | * |
| 36 | - * @var Importable_Detector | |
| 28 | + * @var Importable_Detector_Service | |
| 37 | 29 | */ |
| 38 | 30 | protected $importable_detector; |
| 39 | 31 | |
| 40 | 32 | /** |
| @@ -50,9 +42,8 @@ | ||
| 50 | 42 | * @return array |
| 51 | 43 | */ |
| 52 | 44 | public static function get_conditionals() { |
| 53 | 45 | return [ |
| 54 | - AIOSEO_V4_Importer_Conditional::class, | |
| 55 | 46 | Import_Tool_Selected_Conditional::class, |
| 56 | 47 | Yoast_Tools_Page_Conditional::class, |
| 57 | 48 | ]; |
| 58 | 49 | } |
| @@ -59,23 +50,20 @@ | ||
| 59 | 50 | |
| 60 | 51 | /** |
| 61 | 52 | * Import Integration constructor. |
| 62 | 53 | * |
| 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. | |
| 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. | |
| 67 | 57 | */ |
| 68 | 58 | public function __construct( |
| 69 | 59 | WPSEO_Admin_Asset_Manager $asset_manager, |
| 70 | - AIOSEO_V4_Importer_Conditional $importer_conditional, | |
| 71 | - Importable_Detector $importable_detector, | |
| 60 | + Importable_Detector_Service $importable_detector, | |
| 72 | 61 | Importing_Route $importing_route |
| 73 | 62 | ) { |
| 74 | - $this->asset_manager = $asset_manager; | |
| 75 | - $this->importer_conditional = $importer_conditional; | |
| 76 | - $this->importable_detector = $importable_detector; | |
| 77 | - $this->importing_route = $importing_route; | |
| 63 | + $this->asset_manager = $asset_manager; | |
| 64 | + $this->importable_detector = $importable_detector; | |
| 65 | + $this->importing_route = $importing_route; | |
| 78 | 66 | } |
| 79 | 67 | |
| 80 | 68 | /** |
| 81 | 69 | * Initializes the integration. |
| @@ -89,8 +77,10 @@ | ||
| 89 | 77 | } |
| 90 | 78 | |
| 91 | 79 | /** |
| 92 | 80 | * Enqueues the Import script. |
| 81 | + * | |
| 82 | + * @return void | |
| 93 | 83 | */ |
| 94 | 84 | public function enqueue_import_script() { |
| 95 | 85 | \wp_enqueue_style( 'dashicons' ); |
| 96 | 86 | $this->asset_manager->enqueue_script( 'import' ); |
| @@ -108,8 +98,9 @@ | ||
| 108 | 98 | 'note' => \esc_html__( 'Note: ', 'wordpress-seo' ), |
| 109 | 99 | '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 | 100 | 'select_placeholder' => \esc_html__( 'Select SEO plugin', 'wordpress-seo' ), |
| 111 | 101 | 'no_data_msg' => \esc_html__( 'No data found from other SEO plugins.', 'wordpress-seo' ), |
| 102 | + 'validation_failure' => $this->get_validation_failure_alert(), | |
| 112 | 103 | 'import_failure' => $this->get_import_failure_alert( true ), |
| 113 | 104 | 'cleanup_failure' => $this->get_import_failure_alert( false ), |
| 114 | 105 | 'spinner' => \admin_url( 'images/loading.gif' ), |
| 115 | 106 | 'replacing_texts' => [ |
| @@ -158,9 +149,11 @@ | ||
| 158 | 149 | protected function get_importing_endpoints() { |
| 159 | 150 | $available_actions = $this->importable_detector->detect_importers(); |
| 160 | 151 | $importing_endpoints = []; |
| 161 | 152 | |
| 162 | - foreach ( $available_actions as $plugin => $types ) { | |
| 153 | + $available_sorted_actions = $this->sort_actions( $available_actions ); | |
| 154 | + | |
| 155 | + foreach ( $available_sorted_actions as $plugin => $types ) { | |
| 163 | 156 | foreach ( $types as $type ) { |
| 164 | 157 | $importing_endpoints[ $plugin ][] = $this->importing_route->get_endpoint( $plugin, $type ); |
| 165 | 158 | } |
| 166 | 159 | } |
| @@ -168,8 +161,34 @@ | ||
| 168 | 161 | return $importing_endpoints; |
| 169 | 162 | } |
| 170 | 163 | |
| 171 | 164 | /** |
| 165 | + * Sorts the array of importing actions, by moving any validating actions to the start for every plugin. | |
| 166 | + * | |
| 167 | + * @param array $available_actions The array of actions that we want to sort. | |
| 168 | + * | |
| 169 | + * @return array The sorted array of actions. | |
| 170 | + */ | |
| 171 | + protected function sort_actions( $available_actions ) { | |
| 172 | + $first_action = 'validate_data'; | |
| 173 | + $available_sorted_actions = []; | |
| 174 | + | |
| 175 | + foreach ( $available_actions as $plugin => $plugin_available_actions ) { | |
| 176 | + | |
| 177 | + $validate_action_position = \array_search( $first_action, $plugin_available_actions, true ); | |
| 178 | + | |
| 179 | + if ( ! empty( $validate_action_position ) ) { | |
| 180 | + unset( $plugin_available_actions[ $validate_action_position ] ); | |
| 181 | + \array_unshift( $plugin_available_actions, $first_action ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + $available_sorted_actions[ $plugin ] = $plugin_available_actions; | |
| 185 | + } | |
| 186 | + | |
| 187 | + return $available_sorted_actions; | |
| 188 | + } | |
| 189 | + | |
| 190 | + /** | |
| 172 | 191 | * Retrieves a list of the importing endpoints to use. |
| 173 | 192 | * |
| 174 | 193 | * @return array The endpoints. |
| 175 | 194 | */ |
| @@ -183,8 +202,28 @@ | ||
| 183 | 202 | } |
| 184 | 203 | } |
| 185 | 204 | |
| 186 | 205 | return $importing_endpoints; |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * Gets the validation failure alert using the Alert_Presenter. | |
| 210 | + * | |
| 211 | + * @return string The validation failure alert. | |
| 212 | + */ | |
| 213 | + protected function get_validation_failure_alert() { | |
| 214 | + $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' ); | |
| 215 | + $content .= '<br/>'; | |
| 216 | + $content .= '<ol><li>'; | |
| 217 | + $content .= \esc_html__( 'If you have never saved any AIOSEO \'Search Appearance\' settings, please do that first and run the import again.', 'wordpress-seo' ); | |
| 218 | + $content .= '</li>'; | |
| 219 | + $content .= '<li>'; | |
| 220 | + $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' ); | |
| 221 | + $content .= '</li></ol>'; | |
| 222 | + | |
| 223 | + $validation_failure_alert = new Alert_Presenter( $content, 'error' ); | |
| 224 | + | |
| 225 | + return $validation_failure_alert->present(); | |
| 187 | 226 | } |
| 188 | 227 | |
| 189 | 228 | /** |
| 190 | 229 | * Gets the import failure alert using the Alert_Presenter. |