PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.3
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 / integrations / admin / configuration-workout-integration.php

configuration-workout-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.3, at src/integrations/admin/configuration-workout-integration.php

369 lines 10.3 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\Integrations\Admin;
4
5 use WPSEO_Addon_Manager;
6 use WPSEO_Admin_Asset_Manager;
7 use WPSEO_Shortlinker;
8 use WPSEO_Utils;
9 use Yoast\WP\SEO\Conditionals\Admin_Conditional;
10 use Yoast\WP\SEO\Helpers\Options_Helper;
11 use Yoast\WP\SEO\Helpers\Product_Helper;
12 use Yoast\WP\SEO\Integrations\Integration_Interface;
13 use Yoast\WP\SEO\Routes\Indexing_Route;
14
15 /**
16 * ConfigurationWorkoutsIntegration class
17 */
18 class Configuration_Workout_Integration implements Integration_Interface {
19
20 /**
21 * The admin asset manager.
22 *
23 * @var WPSEO_Admin_Asset_Manager
24 */
25 private $admin_asset_manager;
26
27 /**
28 * The addon manager.
29 *
30 * @var WPSEO_Addon_Manager
31 */
32 private $addon_manager;
33
34 /**
35 * The shortlinker.
36 *
37 * @var WPSEO_Shortlinker
38 */
39 private $shortlinker;
40
41 /**
42 * The options' helper.
43 *
44 * @var Options_Helper
45 */
46 private $options_helper;
47
48 /**
49 * The product helper.
50 *
51 * @var \Yoast\WP\SEO\Helpers\Product_Helper
52 */
53 private $product_helper;
54
55 /**
56 * {@inheritDoc}
57 */
58 public static function get_conditionals() {
59 return [ Admin_Conditional::class ];
60 }
61
62 /**
63 * Configuration_Workout_Integration constructor.
64 *
65 * @param WPSEO_Admin_Asset_Manager $admin_asset_manager The admin asset manager.
66 * @param WPSEO_Addon_Manager $addon_manager The addon manager.
67 * @param WPSEO_Shortlinker $shortlinker The shortlinker.
68 * @param Options_Helper $options_helper The options helper.
69 * @param Product_Helper $product_helper The product helper.
70 */
71 public function __construct(
72 WPSEO_Admin_Asset_Manager $admin_asset_manager,
73 WPSEO_Addon_Manager $addon_manager,
74 WPSEO_Shortlinker $shortlinker,
75 Options_Helper $options_helper,
76 Product_Helper $product_helper
77 ) {
78 $this->admin_asset_manager = $admin_asset_manager;
79 $this->addon_manager = $addon_manager;
80 $this->shortlinker = $shortlinker;
81 $this->options_helper = $options_helper;
82 $this->product_helper = $product_helper;
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 public function register_hooks() {
89 \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
90 }
91
92 /**
93 * Adds the data for the configuration workout to the wpseoWorkoutsData object.
94 */
95 public function enqueue_assets() {
96 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Date is not processed or saved.
97 if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'wpseo_workouts' ) {
98 return;
99 }
100
101 $this->admin_asset_manager->enqueue_script( 'indexation' );
102 $this->admin_asset_manager->enqueue_style( 'admin-css' );
103 $this->admin_asset_manager->enqueue_style( 'monorepo' );
104
105 $data = [
106 'disabled' => ! YoastSEO()->helpers->indexable->should_index_indexables(),
107 'amount' => YoastSEO()->helpers->indexing->get_filtered_unindexed_count(),
108 'firstTime' => ( YoastSEO()->helpers->indexing->is_initial_indexing() === true ),
109 'errorMessage' => '',
110 'restApi' => [
111 'root' => \esc_url_raw( \rest_url() ),
112 'indexing_endpoints' => $this->get_endpoints(),
113 'nonce' => \wp_create_nonce( 'wp_rest' ),
114 ],
115 ];
116
117 /**
118 * Filter: 'wpseo_indexing_data' Filter to adapt the data used in the indexing process.
119 *
120 * @param array $data The indexing data to adapt.
121 */
122 $data = \apply_filters( 'wpseo_indexing_data', $data );
123
124 $this->admin_asset_manager->localize_script( 'indexation', 'yoastIndexingData', $data );
125
126 $social_profiles = $this->get_social_profiles();
127
128 // This filter is documented in admin/views/tabs/metas/paper-content/general/knowledge-graph.php.
129 $knowledge_graph_message = apply_filters( 'wpseo_knowledge_graph_setting_msg', '' );
130
131 $options = $this->get_company_or_person_options();
132 $selected_option_label = '';
133 $filtered_options = \array_filter(
134 $options,
135 function ( $item ) {
136 return $item['value'] === $this->is_company_or_person();
137 }
138 );
139 $selected_option = \reset( $filtered_options );
140 if ( \is_array( $selected_option ) ) {
141 $selected_option_label = $selected_option['name'];
142 }
143
144 $this->admin_asset_manager->add_inline_script(
145 'workouts',
146 \sprintf(
147 'window.wpseoWorkoutsData["configuration"] = {
148 "companyOrPerson": "%s",
149 "companyOrPersonLabel": "%s",
150 "companyName": "%s",
151 "companyLogo": "%s",
152 "companyLogoId": %d,
153 "personId": %d,
154 "personLogo": "%s",
155 "personLogoId": %d,
156 "siteTagline": "%s",
157 "socialProfiles": {
158 "facebookUrl": "%s",
159 "twitterUsername": "%s",
160 "instagramUrl": "%s",
161 "linkedinUrl": "%s",
162 "myspaceUrl": "%s",
163 "pinterestUrl": "%s",
164 "youtubeUrl": "%s",
165 "wikipediaUrl": "%s",
166 },
167 "tracking": %d,
168 "companyOrPersonOptions": %s,
169 "shouldForceCompany": %d,
170 "knowledgeGraphMessage": "%s",
171 "shortlinks": {
172 "workoutGuide": "%s",
173 "indexData": "%s",
174 "gdpr": "%s",
175 },
176 };',
177 $this->is_company_or_person(),
178 $selected_option_label,
179 $this->get_company_name(),
180 $this->get_company_logo(),
181 $this->get_company_logo_id(),
182 $this->get_person_id(),
183 $this->get_person_logo(),
184 $this->get_person_logo_id(),
185 $this->get_site_tagline(),
186 $social_profiles['facebook_url'],
187 $social_profiles['twitter_username'],
188 $social_profiles['instagram_url'],
189 $social_profiles['linkedin_url'],
190 $social_profiles['myspace_url'],
191 $social_profiles['pinterest_url'],
192 $social_profiles['youtube_url'],
193 $social_profiles['wikipedia_url'],
194 $this->has_tracking_enabled(),
195 WPSEO_Utils::format_json_encode( $options ),
196 $this->should_force_company(),
197 $knowledge_graph_message,
198 $this->shortlinker->build_shortlink( 'https://yoa.st/config-workout-guide' ),
199 $this->shortlinker->build_shortlink( 'https://yoa.st/config-workout-index-data' ),
200 $this->shortlinker->build_shortlink( 'https://yoa.st/gdpr-config-workout' )
201 ),
202 'before'
203 );
204 }
205
206 /**
207 * Retrieves a list of the endpoints to use.
208 *
209 * @return array The endpoints.
210 */
211 protected function get_endpoints() {
212 $endpoints = [
213 'prepare' => Indexing_Route::FULL_PREPARE_ROUTE,
214 'terms' => Indexing_Route::FULL_TERMS_ROUTE,
215 'posts' => Indexing_Route::FULL_POSTS_ROUTE,
216 'archives' => Indexing_Route::FULL_POST_TYPE_ARCHIVES_ROUTE,
217 'general' => Indexing_Route::FULL_GENERAL_ROUTE,
218 'indexablesComplete' => Indexing_Route::FULL_INDEXABLES_COMPLETE_ROUTE,
219 'post_link' => Indexing_Route::FULL_POST_LINKS_INDEXING_ROUTE,
220 'term_link' => Indexing_Route::FULL_TERM_LINKS_INDEXING_ROUTE,
221 ];
222
223 $endpoints = \apply_filters( 'wpseo_indexing_endpoints', $endpoints );
224
225 $endpoints['complete'] = Indexing_Route::FULL_COMPLETE_ROUTE;
226
227 return $endpoints;
228 }
229
230 // ** Private functions ** //
231
232 /**
233 * Returns the entity represented by the site.
234 *
235 * @return string The entity represented by the site.
236 */
237 private function is_company_or_person() {
238 return $this->options_helper->get( 'company_or_person', '' );
239 }
240
241 /**
242 * Gets the company name from the option in the database.
243 *
244 * @return string The company name.
245 */
246 private function get_company_name() {
247 return $this->options_helper->get( 'company_name', '' );
248 }
249
250 /**
251 * Gets the company logo from the option in the database.
252 *
253 * @return string The company logo.
254 */
255 private function get_company_logo() {
256 return $this->options_helper->get( 'company_logo', '' );
257 }
258
259 /**
260 * Gets the company logo id from the option in the database.
261 *
262 * @return string The company logo id.
263 */
264 private function get_company_logo_id() {
265 return $this->options_helper->get( 'company_logo_id', '' );
266 }
267
268 /**
269 * Gets the person id from the option in the database.
270 *
271 * @return int|null The person id, null if empty.
272 */
273 private function get_person_id() {
274 return $this->options_helper->get( 'company_or_person_user_id' );
275 }
276
277 /**
278 * Gets the person avatar from the option in the database.
279 *
280 * @return string The person logo.
281 */
282 private function get_person_logo() {
283 return $this->options_helper->get( 'person_logo', '' );
284 }
285
286 /**
287 * Gets the person logo id from the option in the database.
288 *
289 * @return string The person logo id.
290 */
291 private function get_person_logo_id() {
292 return $this->options_helper->get( 'person_logo_id', '' );
293 }
294
295 /**
296 * Gets the site tagline.
297 *
298 * @return string The site tagline.
299 */
300 private function get_site_tagline() {
301 return \get_bloginfo( 'description' );
302 }
303
304 /**
305 * Gets the social profiles stored in the database.
306 *
307 * @return string[] The social profiles.
308 */
309 private function get_social_profiles() {
310 return [
311 'facebook_url' => $this->options_helper->get( 'facebook_site', '' ),
312 'twitter_username' => $this->options_helper->get( 'twitter_site', '' ),
313 'instagram_url' => $this->options_helper->get( 'instagram_url', '' ),
314 'linkedin_url' => $this->options_helper->get( 'linkedin_url', '' ),
315 'myspace_url' => $this->options_helper->get( 'myspace_url', '' ),
316 'pinterest_url' => $this->options_helper->get( 'pinterest_url', '' ),
317 'youtube_url' => $this->options_helper->get( 'youtube_url', '' ),
318 'wikipedia_url' => $this->options_helper->get( 'wikipedia_url', '' ),
319 ];
320 }
321
322 /**
323 * Checks whether tracking is enabled.
324 *
325 * @return bool True if tracking is enabled, false otherwise, null if in Free and conf. workout step not finished.
326 */
327 private function has_tracking_enabled() {
328 $default = false;
329
330 if ( $this->product_helper->is_premium() ) {
331 $default = true;
332 }
333
334 return $this->options_helper->get( 'tracking', $default );
335 }
336
337 /**
338 * Gets the options for the Company or Person select.
339 * Returns only the company option if it is forced (by Local SEO), otherwise returns company and person option.
340 *
341 * @return array The options for the company-or-person select.
342 */
343 private function get_company_or_person_options() {
344 $options = [
345 [
346 'name' => __( 'Organization', 'wordpress-seo' ),
347 'value' => 'company',
348 ],
349 ];
350 if ( ! $this->should_force_company() ) {
351 $options[] = [
352 'name' => __( 'Person', 'wordpress-seo' ),
353 'value' => 'person',
354 ];
355 }
356
357 return $options;
358 }
359
360 /**
361 * Checks whether we should force "Organization".
362 *
363 * @return bool
364 */
365 private function should_force_company() {
366 return $this->addon_manager->is_installed( WPSEO_Addon_Manager::LOCAL_SLUG );
367 }
368 }
369