PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / actions / importing / abstract-aioseo-importing-action.php

abstract-aioseo-importing-action.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/actions/importing/abstract-aioseo-importing-action.php

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