PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4
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.4, at src/actions/importing/abstract-aioseo-importing-action.php

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