| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Routes; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use WP_REST_Response; |
| 7 |
use Yoast\WP\SEO\Actions\Importing\Importing_Action_Interface; |
| 8 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 9 |
use Yoast\WP\SEO\Exceptions\Importing\Aioseo_Validation_Exception; |
| 10 |
use Yoast\WP\SEO\Main; |
| 11 |
use Yoast\WP\SEO\Services\Importing\Importable_Detector_Service; |
| 12 |
|
| 13 |
/** |
| 14 |
* Importing_Route class. |
| 15 |
* |
| 16 |
* Importing route for importing from other SEO plugins. |
| 17 |
*/ |
| 18 |
class Importing_Route extends Abstract_Action_Route { |
| 19 |
|
| 20 |
use No_Conditionals; |
| 21 |
|
| 22 |
/** |
| 23 |
* The import route constant. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
const ROUTE = '/import/(?P<plugin>[\w-]+)/(?P<type>[\w-]+)'; |
| 28 |
|
| 29 |
/** |
| 30 |
* List of available importers. |
| 31 |
* |
| 32 |
* @var Importing_Action_Interface[] |
| 33 |
*/ |
| 34 |
protected $importers = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* The importable detector service. |
| 38 |
* |
| 39 |
* @var Importable_Detector_Service |
| 40 |
*/ |
| 41 |
protected $importable_detector; |
| 42 |
|
| 43 |
/** |
| 44 |
* Importing_Route constructor. |
| 45 |
* |
| 46 |
* @param Importable_Detector_Service $importable_detector The importable detector service. |
| 47 |
* @param Importing_Action_Interface ...$importers All available importers. |
| 48 |
*/ |
| 49 |
public function __construct( |
| 50 |
Importable_Detector_Service $importable_detector, |
| 51 |
Importing_Action_Interface ...$importers |
| 52 |
) { |
| 53 |
$this->importable_detector = $importable_detector; |
| 54 |
$this->importers = $importers; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Registers routes with WordPress. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function register_routes() { |
| 63 |
\register_rest_route( |
| 64 |
Main::API_V1_NAMESPACE, |
| 65 |
self::ROUTE, |
| 66 |
[ |
| 67 |
'callback' => [ $this, 'execute' ], |
| 68 |
'permission_callback' => [ $this, 'is_user_permitted_to_import' ], |
| 69 |
'methods' => [ 'POST' ], |
| 70 |
] |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Executes the rest request, but only if the respective action is enabled. |
| 76 |
* |
| 77 |
* @param mixed $data The request parameters. |
| 78 |
* |
| 79 |
* @return WP_REST_Response|false Response or false on non-existent route. |
| 80 |
*/ |
| 81 |
public function execute( $data ) { |
| 82 |
$plugin = (string) $data['plugin']; |
| 83 |
$type = (string) $data['type']; |
| 84 |
|
| 85 |
$next_url = $this->get_endpoint( $plugin, $type ); |
| 86 |
|
| 87 |
try { |
| 88 |
$importer = $this->get_importer( $plugin, $type ); |
| 89 |
|
| 90 |
if ( $importer === false || ! $importer->is_enabled() ) { |
| 91 |
return new WP_Error( |
| 92 |
'rest_no_route', |
| 93 |
'Requested importer not found', |
| 94 |
[ |
| 95 |
'status' => 404, |
| 96 |
] |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
$result = $importer->index(); |
| 101 |
|
| 102 |
if ( $result === false || \count( $result ) === 0 ) { |
| 103 |
$next_url = false; |
| 104 |
} |
| 105 |
|
| 106 |
return $this->respond_with( |
| 107 |
$result, |
| 108 |
$next_url |
| 109 |
); |
| 110 |
} catch ( \Exception $exception ) { |
| 111 |
if ( $exception instanceof Aioseo_Validation_Exception ) { |
| 112 |
return new WP_Error( |
| 113 |
'wpseo_error_validation', |
| 114 |
$exception->getMessage(), |
| 115 |
[ 'stackTrace' => $exception->getTraceAsString() ] |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
return new WP_Error( |
| 120 |
'wpseo_error_indexing', |
| 121 |
$exception->getMessage(), |
| 122 |
[ 'stackTrace' => $exception->getTraceAsString() ] |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Gets the right importer for the given arguments. |
| 129 |
* |
| 130 |
* @param string $plugin The plugin to import from. |
| 131 |
* @param string $type The type of entity to import. |
| 132 |
* |
| 133 |
* @return Importing_Action_Interface|false The importer, or false if no importer was found. |
| 134 |
*/ |
| 135 |
protected function get_importer( $plugin, $type ) { |
| 136 |
$importers = $this->importable_detector->filter_actions( $this->importers, $plugin, $type ); |
| 137 |
|
| 138 |
if ( \count( $importers ) !== 1 ) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
return \current( $importers ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Gets the right endpoint for the given arguments. |
| 147 |
* |
| 148 |
* @param string $plugin The plugin to import from. |
| 149 |
* @param string $type The type of entity to import. |
| 150 |
* |
| 151 |
* @return string|false The endpoint for the given action or false on failure of finding the one. |
| 152 |
*/ |
| 153 |
public function get_endpoint( $plugin, $type ) { |
| 154 |
if ( empty( $plugin ) || empty( $type ) ) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
return Main::API_V1_NAMESPACE . "/import/{$plugin}/{$type}"; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Whether or not the current user is allowed to import. |
| 163 |
* |
| 164 |
* @return bool Whether or not the current user is allowed to import. |
| 165 |
*/ |
| 166 |
public function is_user_permitted_to_import() { |
| 167 |
return \current_user_can( 'activate_plugins' ); |
| 168 |
} |
| 169 |
} |
| 170 |
|