| 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 |
$import_failure_alert = $this->get_import_failure_alert(); |
| 99 |
|
| 100 |
$data = [ |
| 101 |
'restApi' => [ |
| 102 |
'root' => \esc_url_raw( \rest_url() ), |
| 103 |
'importing_endpoints' => $this->get_importing_endpoints(), |
| 104 |
'nonce' => \wp_create_nonce( 'wp_rest' ), |
| 105 |
], |
| 106 |
'assets' => [ |
| 107 |
'loading_msg' => \esc_html__( 'The import can take a long time depending on your site\'s size.', 'wordpress-seo' ), |
| 108 |
'select_placeholder' => \esc_html__( 'Select SEO plugin', 'wordpress-seo' ), |
| 109 |
'no_data_msg' => \esc_html__( 'No data found from other SEO plugins.', 'wordpress-seo' ), |
| 110 |
'import_failure' => $import_failure_alert, |
| 111 |
'spinner' => \admin_url( 'images/loading.gif' ), |
| 112 |
], |
| 113 |
]; |
| 114 |
|
| 115 |
/** |
| 116 |
* Filter: 'wpseo_importing_data' Filter to adapt the data used in the import process. |
| 117 |
* |
| 118 |
* @param array $data The import data to adapt. |
| 119 |
*/ |
| 120 |
$data = \apply_filters( 'wpseo_importing_data', $data ); |
| 121 |
|
| 122 |
$this->asset_manager->localize_script( 'import', 'yoastImportData', $data ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Retrieves a list of the importing endpoints to use. |
| 127 |
* |
| 128 |
* @return array The endpoints. |
| 129 |
*/ |
| 130 |
protected function get_importing_endpoints() { |
| 131 |
$available_actions = $this->importable_detector->detect(); |
| 132 |
$importing_endpoints = []; |
| 133 |
|
| 134 |
foreach ( $available_actions as $plugin => $types ) { |
| 135 |
foreach ( $types as $type ) { |
| 136 |
$importing_endpoints[ $plugin ][] = $this->importing_route->get_endpoint( $plugin, $type ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return $importing_endpoints; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Gets the import failure alert using the Alert_Presenter. |
| 145 |
* |
| 146 |
* @return string The import failure alert. |
| 147 |
*/ |
| 148 |
protected function get_import_failure_alert() { |
| 149 |
$content = \esc_html__( 'Import failed with the following error:', 'wordpress-seo' ); |
| 150 |
$content .= '<br/><br/>'; |
| 151 |
$content .= \esc_html( '%s' ); |
| 152 |
|
| 153 |
$import_failure_alert = new Alert_Presenter( $content, 'error' ); |
| 154 |
|
| 155 |
return $import_failure_alert->present(); |
| 156 |
} |
| 157 |
} |
| 158 |
|