• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/integrations/admin/first-time-configuration-integration.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Admin;
4

5
use WP_User;
6
use WPSEO_Addon_Manager;
7
use WPSEO_Admin_Asset_Manager;
8
use WPSEO_Option_Tab;
9
use WPSEO_Shortlinker;
10
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
11
use Yoast\WP\SEO\Context\Meta_Tags_Context;
12
use Yoast\WP\SEO\Helpers\Options_Helper;
13
use Yoast\WP\SEO\Helpers\Product_Helper;
14
use Yoast\WP\SEO\Helpers\Social_Profiles_Helper;
15
use Yoast\WP\SEO\Integrations\Integration_Interface;
16
use Yoast\WP\SEO\Routes\Indexing_Route;
17

18
/**
19
 * First_Time_Configuration_Integration class
20
 */
21
class First_Time_Configuration_Integration implements Integration_Interface {
22

23
        /**
24
         * The admin asset manager.
25
         *
26
         * @var WPSEO_Admin_Asset_Manager
27
         */
28
        private $admin_asset_manager;
29

30
        /**
31
         * The addon manager.
32
         *
33
         * @var WPSEO_Addon_Manager
34
         */
35
        private $addon_manager;
36

37
        /**
38
         * The shortlinker.
39
         *
40
         * @var WPSEO_Shortlinker
41
         */
42
        private $shortlinker;
43

44
        /**
45
         * The options' helper.
46
         *
47
         * @var Options_Helper
48
         */
49
        private $options_helper;
50

51
        /**
52
         * The social profiles helper.
53
         *
54
         * @var Social_Profiles_Helper
55
         */
56
        private $social_profiles_helper;
57

58
        /**
59
         * The product helper.
60
         *
61
         * @var Product_Helper
62
         */
63
        private $product_helper;
64

65
        /**
66
         * The meta tags context helper.
67
         *
68
         * @var Meta_Tags_Context
69
         */
70
        private $meta_tags_context;
71

72
        /**
73
         * {@inheritDoc}
74
         */
75
        public static function get_conditionals() {
×
76
                return [ Admin_Conditional::class ];
×
77
        }
78

79
        /**
80
         * First_Time_Configuration_Integration constructor.
81
         *
82
         * @param WPSEO_Admin_Asset_Manager $admin_asset_manager    The admin asset manager.
83
         * @param WPSEO_Addon_Manager       $addon_manager          The addon manager.
84
         * @param WPSEO_Shortlinker         $shortlinker            The shortlinker.
85
         * @param Options_Helper            $options_helper         The options helper.
86
         * @param Social_Profiles_Helper    $social_profiles_helper The social profile helper.
87
         * @param Product_Helper            $product_helper         The product helper.
88
         * @param Meta_Tags_Context         $meta_tags_context      The meta tags context helper.
89
         */
90
        public function __construct(
×
91
                WPSEO_Admin_Asset_Manager $admin_asset_manager,
92
                WPSEO_Addon_Manager $addon_manager,
93
                WPSEO_Shortlinker $shortlinker,
94
                Options_Helper $options_helper,
95
                Social_Profiles_Helper $social_profiles_helper,
96
                Product_Helper $product_helper,
97
                Meta_Tags_Context $meta_tags_context
98
        ) {
99
                $this->admin_asset_manager    = $admin_asset_manager;
×
100
                $this->addon_manager          = $addon_manager;
×
101
                $this->shortlinker            = $shortlinker;
×
102
                $this->options_helper         = $options_helper;
×
103
                $this->social_profiles_helper = $social_profiles_helper;
×
104
                $this->product_helper         = $product_helper;
×
105
                $this->meta_tags_context      = $meta_tags_context;
×
106
        }
107

108
        /**
109
         * {@inheritDoc}
110
         */
111
        public function register_hooks() {
×
112
                \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
×
113
                \add_action( 'wpseo_settings_tabs_dashboard', [ $this, 'add_first_time_configuration_tab' ] );
×
114
        }
115

116
        /**
117
         * Adds a dedicated tab in the General sub-page.
118
         *
119
         * @param WPSEO_Options_Tabs $dashboard_tabs Object representing the tabs of the General sub-page.
120
         */
121
        public function add_first_time_configuration_tab( $dashboard_tabs ) {
×
122
                $dashboard_tabs->add_tab(
×
123
                        new WPSEO_Option_Tab(
×
124
                                'first-time-configuration',
×
125
                                \__( 'First-time configuration', 'wordpress-seo' ),
×
126
                                [ 'save_button' => false ]
×
127
                        )
128
                );
129
        }
130

131
        /**
132
         * Adds the data for the first-time configuration to the wpseoFirstTimeConfigurationData object.
133
         */
134
        public function enqueue_assets() {
×
135
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Date is not processed or saved.
136
                if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'wpseo_dashboard' || \is_network_admin() ) {
×
137
                        return;
×
138
                }
139

140
                $this->admin_asset_manager->enqueue_script( 'indexation' );
×
141
                $this->admin_asset_manager->enqueue_script( 'first-time-configuration' );
×
142
                $this->admin_asset_manager->enqueue_style( 'first-time-configuration' );
×
143
                $this->admin_asset_manager->enqueue_style( 'admin-css' );
×
144
                $this->admin_asset_manager->enqueue_style( 'monorepo' );
×
145

146
                $data = [
147
                        'disabled'     => ! \YoastSEO()->helpers->indexable->should_index_indexables(),
×
148
                        'amount'       => \YoastSEO()->helpers->indexing->get_filtered_unindexed_count(),
×
149
                        'firstTime'    => ( \YoastSEO()->helpers->indexing->is_initial_indexing() === true ),
×
150
                        'errorMessage' => '',
×
151
                        'restApi'      => [
152
                                'root'               => \esc_url_raw( \rest_url() ),
×
153
                                'indexing_endpoints' => $this->get_endpoints(),
×
154
                                'nonce'              => \wp_create_nonce( 'wp_rest' ),
×
155
                        ],
156
                ];
157

158
                /**
159
                 * Filter: 'wpseo_indexing_data' Filter to adapt the data used in the indexing process.
160
                 *
161
                 * @param array $data The indexing data to adapt.
162
                 */
163
                $data = \apply_filters( 'wpseo_indexing_data', $data );
×
164

165
                $this->admin_asset_manager->localize_script( 'indexation', 'yoastIndexingData', $data );
×
166

167
                $person_id       = $this->get_person_id();
×
168
                $social_profiles = $this->get_social_profiles();
×
169

170
                // This filter is documented in admin/views/tabs/metas/paper-content/general/knowledge-graph.php.
171
                $knowledge_graph_message = \apply_filters( 'wpseo_knowledge_graph_setting_msg', '' );
×
172

173
                $finished_steps        = $this->get_finished_steps();
×
174
                $options               = $this->get_company_or_person_options();
×
175
                $selected_option_label = '';
×
176
                $filtered_options      = \array_filter(
×
177
                        $options,
×
178
                        function ( $item ) {
179
                                return $item['value'] === $this->is_company_or_person();
×
180
                        }
181
                );
182
                $selected_option       = \reset( $filtered_options );
×
183
                if ( \is_array( $selected_option ) ) {
×
184
                        $selected_option_label = $selected_option['label'];
185
                }
186

187
                $data_ftc = [
188
                        'canEditUser'                => $this->can_edit_profile( $person_id ),
×
189
                        'companyOrPerson'            => $this->is_company_or_person(),
×
190
                        'companyOrPersonLabel'       => $selected_option_label,
×
191
                        'companyName'                => $this->get_company_name(),
×
192
                        'fallbackCompanyName'        => $this->get_fallback_company_name( $this->get_company_name() ),
×
193
                        'websiteName'                => $this->get_website_name(),
×
194
                        'fallbackWebsiteName'        => $this->get_fallback_website_name( $this->get_website_name() ),
×
195
                        'companyLogo'                => $this->get_company_logo(),
×
196
                        'companyLogoFallback'        => $this->get_company_fallback_logo( $this->get_company_logo() ),
×
197
                        'companyLogoId'              => $this->get_person_logo_id(),
×
198
                        'finishedSteps'              => $finished_steps,
×
199
                        'personId'                   => (int) $person_id,
×
200
                        'personName'                 => $this->get_person_name(),
×
201
                        'personLogo'                 => $this->get_person_logo(),
×
202
                        'personLogoFallback'         => $this->get_person_fallback_logo( $this->get_person_logo() ),
×
203
                        'personLogoId'               => $this->get_person_logo_id(),
×
204
                        'siteTagline'                => $this->get_site_tagline(),
205
                        'socialProfiles'             => [
206
                                'facebookUrl'     => $social_profiles['facebook_site'],
×
207
                                'twitterUsername' => $social_profiles['twitter_site'],
×
208
                                'otherSocialUrls' => $social_profiles['other_social_urls'],
209
                        ],
210
                        'isPremium'                  => $this->product_helper->is_premium(),
×
211
                        'tracking'                   => $this->has_tracking_enabled(),
×
212
                        'isTrackingAllowedMultisite' => $this->is_tracking_enabled_multisite(),
×
213
                        'isMainSite'                 => $this->is_main_site(),
×
214
                        'companyOrPersonOptions'     => $options,
×
215
                        'shouldForceCompany'         => $this->should_force_company(),
×
216
                        'knowledgeGraphMessage'      => $knowledge_graph_message,
217
                        'shortlinks'                 => [
218
                                'gdpr'                     => $this->shortlinker->build_shortlink( 'https://yoa.st/gdpr-config-workout' ),
×
219
                                'configIndexables'         => $this->shortlinker->build_shortlink( 'https://yoa.st/config-indexables' ),
×
220
                                'configIndexablesBenefits' => $this->shortlinker->build_shortlink( 'https://yoa.st/config-indexables-benefits' ),
221
                        ],
222
                ];
223

224
                $this->admin_asset_manager->localize_script( 'first-time-configuration', 'wpseoFirstTimeConfigurationData', $data_ftc );
×
225
        }
226

227
        /**
228
         * Retrieves a list of the endpoints to use.
229
         *
230
         * @return array The endpoints.
231
         */
232
        protected function get_endpoints() {
233
                $endpoints = [
234
                        'prepare'            => Indexing_Route::FULL_PREPARE_ROUTE,
235
                        'terms'              => Indexing_Route::FULL_TERMS_ROUTE,
236
                        'posts'              => Indexing_Route::FULL_POSTS_ROUTE,
237
                        'archives'           => Indexing_Route::FULL_POST_TYPE_ARCHIVES_ROUTE,
238
                        'general'            => Indexing_Route::FULL_GENERAL_ROUTE,
239
                        'indexablesComplete' => Indexing_Route::FULL_INDEXABLES_COMPLETE_ROUTE,
240
                        'post_link'          => Indexing_Route::FULL_POST_LINKS_INDEXING_ROUTE,
241
                        'term_link'          => Indexing_Route::FULL_TERM_LINKS_INDEXING_ROUTE,
242
                ];
243

244
                $endpoints = \apply_filters( 'wpseo_indexing_endpoints', $endpoints );
245

246
                $endpoints['complete'] = Indexing_Route::FULL_COMPLETE_ROUTE;
247

248
                return $endpoints;
249
        }
250

251
        // ** Private functions ** //
252

253
        /**
254
         * Returns the finished steps array.
255
         *
256
         * @return array An array with the finished steps.
257
         */
258
        private function get_finished_steps() {
259
                return $this->options_helper->get( 'configuration_finished_steps', [] );
260
        }
261

262
        /**
263
         * Returns the entity represented by the site.
264
         *
265
         * @return string The entity represented by the site.
266
         */
267
        private function is_company_or_person() {
268
                return $this->options_helper->get( 'company_or_person', '' );
269
        }
270

271
        /**
272
         * Gets the company name from the option in the database.
273
         *
274
         * @return string The company name.
275
         */
276
        private function get_company_name() {
277
                return $this->options_helper->get( 'company_name', '' );
278
        }
279

280
        /**
281
         * Gets the fallback company name from the option in the database if there is no company name.
282
         *
283
         * @param string $company_name The given company name by the user, default empty string.
284
         *
285
         * @return string|false The company name.
286
         */
287
        private function get_fallback_company_name( $company_name ) {
×
288
                if ( $company_name ) {
×
289
                        return false;
290
                }
291

292
                return \get_bloginfo( 'name' );
293
        }
294

295
        /**
296
         * Gets the website name from the option in the database.
297
         *
298
         * @return string The website name.
299
         */
300
        private function get_website_name() {
301
                return $this->options_helper->get( 'website_name', '' );
302
        }
303

304
        /**
305
         * Gets the fallback website name from the option in the database if there is no website name.
306
         *
307
         * @param string $website_name The given website name by the user, default empty string.
308
         *
309
         * @return string|false The website name.
310
         */
311
        private function get_fallback_website_name( $website_name ) {
×
312
                if ( $website_name ) {
×
313
                        return false;
314
                }
315

316
                return \get_bloginfo( 'name' );
317
        }
318

319
        /**
320
         * Gets the company logo from the option in the database.
321
         *
322
         * @return string The company logo.
323
         */
324
        private function get_company_logo() {
325
                return $this->options_helper->get( 'company_logo', '' );
326
        }
327

328
        /**
329
         * Gets the company logo id from the option in the database.
330
         *
331
         * @return string The company logo id.
332
         */
333
        private function get_company_logo_id() {
334
                return $this->options_helper->get( 'company_logo_id', '' );
335
        }
336

337
        /**
338
         * Gets the company logo url from the option in the database.
339
         *
340
         * @param string $company_logo The given company logo by the user, default empty.
341
         *
342
         * @return string|false The company logo URL.
343
         */
344
        private function get_company_fallback_logo( $company_logo ) {
×
345
                if ( $company_logo ) {
×
346
                        return false;
347
                }
348
                $logo_id = $this->meta_tags_context->fallback_to_site_logo();
349

350
                return \esc_url( \wp_get_attachment_url( $logo_id ) );
351
        }
352

353
        /**
354
         * Gets the person id from the option in the database.
355
         *
356
         * @return int|null The person id, null if empty.
357
         */
358
        private function get_person_id() {
359
                return $this->options_helper->get( 'company_or_person_user_id' );
360
        }
361

362
        /**
363
         * Gets the person id from the option in the database.
364
         *
365
         * @return int|null The person id, null if empty.
366
         */
367
        private function get_person_name() {
×
368
                $user = \get_userdata( $this->get_person_id() );
×
369
                if ( $user instanceof WP_User ) {
×
370
                        return $user->get( 'display_name' );
371
                }
372

373
                return '';
374
        }
375

376
        /**
377
         * Gets the person avatar from the option in the database.
378
         *
379
         * @return string The person logo.
380
         */
381
        private function get_person_logo() {
382
                return $this->options_helper->get( 'person_logo', '' );
383
        }
384

385
        /**
386
         * Gets the person logo url from the option in the database.
387
         *
388
         * @param string $person_logo The given person logo by the user, default empty.
389
         *
390
         * @return string|false The person logo URL.
391
         */
392
        private function get_person_fallback_logo( $person_logo ) {
×
393
                if ( $person_logo ) {
×
394
                        return false;
395
                }
396
                $logo_id = $this->meta_tags_context->fallback_to_site_logo();
397

398
                return \esc_url( \wp_get_attachment_url( $logo_id ) );
399
        }
400

401
        /**
402
         * Gets the person logo id from the option in the database.
403
         *
404
         * @return string The person logo id.
405
         */
406
        private function get_person_logo_id() {
407
                return $this->options_helper->get( 'person_logo_id', '' );
408
        }
409

410
        /**
411
         * Gets the site tagline.
412
         *
413
         * @return string The site tagline.
414
         */
415
        private function get_site_tagline() {
416
                return \get_bloginfo( 'description' );
417
        }
418

419
        /**
420
         * Gets the social profiles stored in the database.
421
         *
422
         * @return string[] The social profiles.
423
         */
424
        private function get_social_profiles() {
425
                return $this->social_profiles_helper->get_organization_social_profiles();
426
        }
427

428
        /**
429
         * Checks whether tracking is enabled.
430
         *
431
         * @return bool True if tracking is enabled, false otherwise, null if in Free and conf. workout step not finished.
432
         */
433
        private function has_tracking_enabled() {
×
434
                $default = false;
435

436
                if ( $this->product_helper->is_premium() ) {
×
437
                        $default = true;
438
                }
439

440
                return $this->options_helper->get( 'tracking', $default );
441
        }
442

443
        /**
444
         * Checks whether tracking option is allowed at network level.
445
         *
446
         * @return bool True if option change is allowed, false otherwise.
447
         */
448
        private function is_tracking_enabled_multisite() {
×
449
                $default = true;
450

451
                if ( ! \is_multisite() ) {
×
452
                        return $default;
453
                }
454

455
                return $this->options_helper->get( 'allow_tracking', $default );
456
        }
457

458
        /**
459
         * Checks whether we are in a main site.
460
         *
461
         * @return bool True if it's the main site or a single site, false if it's a subsite.
462
         */
463
        private function is_main_site() {
464
                return \is_main_site();
465
        }
466

467
        /**
468
         * Gets the options for the Company or Person select.
469
         * Returns only the company option if it is forced (by Local SEO), otherwise returns company and person option.
470
         *
471
         * @return array The options for the company-or-person select.
472
         */
473
        private function get_company_or_person_options() {
×
474
                $options = [
475
                        [
476
                                'label' => \__( 'Organization', 'wordpress-seo' ),
×
477
                                'value' => 'company',
×
478
                                'id'    => 'company',
479
                        ],
480
                        [
481
                                'label' => \__( 'Person', 'wordpress-seo' ),
×
482
                                'value' => 'person',
×
483
                                'id'    => 'person',
484
                        ],
485
                ];
486
                if ( $this->should_force_company() ) {
487
                        $options = [
488
                                [
489
                                        'label' => \__( 'Organization', 'wordpress-seo' ),
×
490
                                        'value' => 'company',
×
491
                                        'id'    => 'company',
492
                                ],
493
                        ];
494
                }
495

496
                return $options;
497
        }
498

499
        /**
500
         * Checks whether we should force "Organization".
501
         *
502
         * @return bool
503
         */
504
        private function should_force_company() {
505
                return $this->addon_manager->is_installed( WPSEO_Addon_Manager::LOCAL_SLUG );
506
        }
507

508
        /**
509
         * Checks if the current user has the capability to edit a specific user.
510
         *
511
         * @param int $person_id The id of the person to edit.
512
         *
513
         * @return bool
514
         */
515
        private function can_edit_profile( $person_id ) {
516
                return \current_user_can( 'edit_user', $person_id );
517
        }
518
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc