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

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