| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Importing; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 7 |
use Yoast\WP\SEO\Helpers\Sanitization_Helper; |
| 8 |
use Yoast\WP\SEO\Services\Importing\Aioseo_Replacevar_Handler; |
| 9 |
use Yoast\WP\SEO\Services\Importing\Aioseo_Robots_Provider_Service; |
| 10 |
use Yoast\WP\SEO\Services\Importing\Aioseo_Robots_Transformer_Service; |
| 11 |
|
| 12 |
/** |
| 13 |
* Importing action interface. |
| 14 |
*/ |
| 15 |
abstract class Abstract_Importing_Action implements Importing_Action_Interface { |
| 16 |
|
| 17 |
/** |
| 18 |
* The plugin the class deals with. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
const PLUGIN = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* The type the class deals with. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
const TYPE = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* The options helper. |
| 33 |
* |
| 34 |
* @var Options_Helper |
| 35 |
*/ |
| 36 |
protected $options; |
| 37 |
|
| 38 |
/** |
| 39 |
* The sanitization helper. |
| 40 |
* |
| 41 |
* @var Sanitization_Helper |
| 42 |
*/ |
| 43 |
protected $sanitization; |
| 44 |
|
| 45 |
/** |
| 46 |
* The replacevar handler. |
| 47 |
* |
| 48 |
* @var Aioseo_Replacevar_Handler |
| 49 |
*/ |
| 50 |
protected $replacevar_handler; |
| 51 |
|
| 52 |
/** |
| 53 |
* The robots provider service. |
| 54 |
* |
| 55 |
* @var Aioseo_Robots_Provider_Service |
| 56 |
*/ |
| 57 |
protected $robots_provider; |
| 58 |
|
| 59 |
/** |
| 60 |
* The robots transformer service. |
| 61 |
* |
| 62 |
* @var Aioseo_Robots_Transformer_Service |
| 63 |
*/ |
| 64 |
protected $robots_transformer; |
| 65 |
|
| 66 |
/** |
| 67 |
* Abstract_Importing_Action constructor. |
| 68 |
* |
| 69 |
* @param Options_Helper $options The options helper. |
| 70 |
* @param Sanitization_Helper $sanitization The sanitization helper. |
| 71 |
* @param Aioseo_Replacevar_Handler $replacevar_handler The replacevar handler. |
| 72 |
* @param Aioseo_Robots_Provider_Service $robots_provider The robots provider service. |
| 73 |
* @param Aioseo_Robots_Transformer_Service $robots_transformer The robots transfomer service. |
| 74 |
*/ |
| 75 |
public function __construct( |
| 76 |
Options_Helper $options, |
| 77 |
Sanitization_Helper $sanitization, |
| 78 |
Aioseo_Replacevar_Handler $replacevar_handler, |
| 79 |
Aioseo_Robots_Provider_Service $robots_provider, |
| 80 |
Aioseo_Robots_Transformer_Service $robots_transformer |
| 81 |
) { |
| 82 |
$this->options = $options; |
| 83 |
$this->sanitization = $sanitization; |
| 84 |
$this->replacevar_handler = $replacevar_handler; |
| 85 |
$this->robots_provider = $robots_provider; |
| 86 |
$this->robots_transformer = $robots_transformer; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* The name of the plugin we import from. |
| 91 |
* |
| 92 |
* @return string The plugin we import from. |
| 93 |
* |
| 94 |
* @throws Exception If the PLUGIN constant is not set in the child class. |
| 95 |
*/ |
| 96 |
public function get_plugin() { |
| 97 |
$class = get_class( $this ); |
| 98 |
$plugin = $class::PLUGIN; |
| 99 |
|
| 100 |
if ( $plugin === null ) { |
| 101 |
throw new Exception( 'Importing action without explicit plugin' ); |
| 102 |
} |
| 103 |
|
| 104 |
return $plugin; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* The data type we import from the plugin. |
| 109 |
* |
| 110 |
* @return string The data type we import from the plugin. |
| 111 |
* |
| 112 |
* @throws Exception If the TYPE constant is not set in the child class. |
| 113 |
*/ |
| 114 |
public function get_type() { |
| 115 |
$class = get_class( $this ); |
| 116 |
$type = $class::TYPE; |
| 117 |
|
| 118 |
if ( $type === null ) { |
| 119 |
throw new Exception( 'Importing action without explicit type' ); |
| 120 |
} |
| 121 |
|
| 122 |
return $type; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Can the current action import the data from plugin $plugin of type $type? |
| 127 |
* |
| 128 |
* @param string $plugin The plugin to import from. |
| 129 |
* @param string $type The type of data to import. |
| 130 |
* |
| 131 |
* @return bool True if this action can handle the combination of Plugin and Type. |
| 132 |
* |
| 133 |
* @throws Exception If the TYPE constant is not set in the child class. |
| 134 |
*/ |
| 135 |
public function is_compatible_with( $plugin = null, $type = null ) { |
| 136 |
if ( empty( $plugin ) && empty( $type ) ) { |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
if ( $plugin === $this->get_plugin() && empty( $type ) ) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
if ( empty( $plugin ) && $type === $this->get_type() ) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
if ( $plugin === $this->get_plugin() && $type === $this->get_type() ) { |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Gets the completed id (to be used as a key for the importing_completed option). |
| 157 |
* |
| 158 |
* @return string The completed id. |
| 159 |
*/ |
| 160 |
public function get_completed_id() { |
| 161 |
return $this->get_cursor_id(); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns the stored state of completedness. |
| 166 |
* |
| 167 |
* @return int The stored state of completedness. |
| 168 |
*/ |
| 169 |
public function get_completed() { |
| 170 |
$completed_id = $this->get_completed_id(); |
| 171 |
$importers_completions = $this->options->get( 'importing_completed', [] ); |
| 172 |
|
| 173 |
return ( isset( $importers_completions[ $completed_id ] ) ) ? $importers_completions[ $completed_id ] : false; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Stores the current state of completedness. |
| 178 |
* |
| 179 |
* @param bool $completed Whether the importer is completed. |
| 180 |
* |
| 181 |
* @return void. |
| 182 |
*/ |
| 183 |
public function set_completed( $completed ) { |
| 184 |
$completed_id = $this->get_completed_id(); |
| 185 |
$current_importers_completions = $this->options->get( 'importing_completed', [] ); |
| 186 |
|
| 187 |
$current_importers_completions[ $completed_id ] = $completed; |
| 188 |
$this->options->set( 'importing_completed', $current_importers_completions ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Returns whether the importing action is enabled. |
| 193 |
* |
| 194 |
* @return bool True by default unless a child class overrides it. |
| 195 |
*/ |
| 196 |
public function is_enabled() { |
| 197 |
return true; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Gets the cursor id. |
| 202 |
* |
| 203 |
* @return string The cursor id. |
| 204 |
*/ |
| 205 |
protected function get_cursor_id() { |
| 206 |
return $this->get_plugin() . '_' . $this->get_type(); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Minimally transforms data to be imported. |
| 211 |
* |
| 212 |
* @param string $meta_data The meta data to be imported. |
| 213 |
* |
| 214 |
* @return string The transformed meta data. |
| 215 |
*/ |
| 216 |
public function simple_import( $meta_data ) { |
| 217 |
// Transform the replace vars into Yoast replace vars. |
| 218 |
$transformed_data = $this->replacevar_handler->transform( $meta_data ); |
| 219 |
|
| 220 |
return $this->sanitization->sanitize_text_field( \html_entity_decode( $transformed_data ) ); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Transforms URL to be imported. |
| 225 |
* |
| 226 |
* @param string $meta_data The meta data to be imported. |
| 227 |
* |
| 228 |
* @return string The transformed URL. |
| 229 |
*/ |
| 230 |
public function url_import( $meta_data ) { |
| 231 |
// We put null as the allowed protocols here, to have the WP default allowed protocols, see https://developer.wordpress.org/reference/functions/wp_allowed_protocols. |
| 232 |
return $this->sanitization->sanitize_url( $meta_data, null ); |
| 233 |
} |
| 234 |
} |
| 235 |
|