PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
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 18.7, 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 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
18 */
19 abstract class Abstract_Aioseo_Importing_Action implements Importing_Action_Interface {
20
21 /**
22 * The plugin the class deals with.
23 *
24 * @var string
25 */
26 const PLUGIN = null;
27
28 /**
29 * The type the class deals with.
30 *
31 * @var string
32 */
33 const TYPE = null;
34
35 /**
36 * The AIOSEO helper.
37 *
38 * @var Aioseo_Helper
39 */
40 protected $aioseo_helper;
41
42 /**
43 * The import cursor helper.
44 *
45 * @var Import_Cursor_Helper
46 */
47 protected $import_cursor;
48
49 /**
50 * The options helper.
51 *
52 * @var Options_Helper
53 */
54 protected $options;
55
56 /**
57 * The sanitization helper.
58 *
59 * @var Sanitization_Helper
60 */
61 protected $sanitization;
62
63 /**
64 * The replacevar handler.
65 *
66 * @var Aioseo_Replacevar_Service
67 */
68 protected $replacevar_handler;
69
70 /**
71 * The robots provider service.
72 *
73 * @var Aioseo_Robots_Provider_Service
74 */
75 protected $robots_provider;
76
77 /**
78 * The robots transformer service.
79 *
80 * @var Aioseo_Robots_Transformer_Service
81 */
82 protected $robots_transformer;
83
84 /**
85 * Abstract_Aioseo_Importing_Action constructor.
86 *
87 * @param Import_Cursor_Helper $import_cursor The import cursor helper.
88 * @param Options_Helper $options The options helper.
89 * @param Sanitization_Helper $sanitization The sanitization helper.
90 * @param Aioseo_Replacevar_Service $replacevar_handler The replacevar handler.
91 * @param Aioseo_Robots_Provider_Service $robots_provider The robots provider service.
92 * @param Aioseo_Robots_Transformer_Service $robots_transformer The robots transfomer service.
93 */
94 public function __construct(
95 Import_Cursor_Helper $import_cursor,
96 Options_Helper $options,
97 Sanitization_Helper $sanitization,
98 Aioseo_Replacevar_Service $replacevar_handler,
99 Aioseo_Robots_Provider_Service $robots_provider,
100 Aioseo_Robots_Transformer_Service $robots_transformer
101 ) {
102 $this->import_cursor = $import_cursor;
103 $this->options = $options;
104 $this->sanitization = $sanitization;
105 $this->replacevar_handler = $replacevar_handler;
106 $this->robots_provider = $robots_provider;
107 $this->robots_transformer = $robots_transformer;
108 }
109
110 /**
111 * Sets the AIOSEO helper.
112 *
113 * @required
114 *
115 * @param Aioseo_Helper $aioseo_helper The AIOSEO helper.
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 = \get_class( $this );
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 = \get_class( $this );
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