| 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\General\User_Interface\General_Page_Integration; |
| 13 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 14 |
use Yoast\WP\SEO\Helpers\Product_Helper; |
| 15 |
use Yoast\WP\SEO\Helpers\Social_Profiles_Helper; |
| 16 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 17 |
use Yoast\WP\SEO\Routes\Indexing_Route; |
| 18 |
|
| 19 |
/** |
| 20 |
* First_Time_Configuration_Integration class |
| 21 |
*/ |
| 22 |
class First_Time_Configuration_Integration implements Integration_Interface { |
| 23 |
|
| 24 |
/** |
| 25 |
* The admin asset manager. |
| 26 |
* |
| 27 |
* @var WPSEO_Admin_Asset_Manager |
| 28 |
*/ |
| 29 |
private $admin_asset_manager; |
| 30 |
|
| 31 |
/** |
| 32 |
* The addon manager. |
| 33 |
* |
| 34 |
* @var WPSEO_Addon_Manager |
| 35 |
*/ |
| 36 |
private $addon_manager; |
| 37 |
|
| 38 |
/** |
| 39 |
* The shortlinker. |
| 40 |
* |
| 41 |
* @var WPSEO_Shortlinker |
| 42 |
*/ |
| 43 |
private $shortlinker; |
| 44 |
|
| 45 |
/** |
| 46 |
* The options' helper. |
| 47 |
* |
| 48 |
* @var Options_Helper |
| 49 |
*/ |
| 50 |
private $options_helper; |
| 51 |
|
| 52 |
/** |
| 53 |
* The social profiles helper. |
| 54 |
* |
| 55 |
* @var Social_Profiles_Helper |
| 56 |
*/ |
| 57 |
private $social_profiles_helper; |
| 58 |
|
| 59 |
/** |
| 60 |
* The product helper. |
| 61 |
* |
| 62 |
* @var Product_Helper |
| 63 |
*/ |
| 64 |
private $product_helper; |
| 65 |
|
| 66 |
/** |
| 67 |
* The meta tags context helper. |
| 68 |
* |
| 69 |
* @var Meta_Tags_Context |
| 70 |
*/ |
| 71 |
private $meta_tags_context; |
| 72 |
|
| 73 |
/** |
| 74 |
* {@inheritDoc} |
| 75 |
*/ |
| 76 |
public static function get_conditionals() { |
| 77 |
return [ Admin_Conditional::class ]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* First_Time_Configuration_Integration constructor. |
| 82 |
* |
| 83 |
* @param WPSEO_Admin_Asset_Manager $admin_asset_manager The admin asset manager. |
| 84 |
* @param WPSEO_Addon_Manager $addon_manager The addon manager. |
| 85 |
* @param WPSEO_Shortlinker $shortlinker The shortlinker. |
| 86 |
* @param Options_Helper $options_helper The options helper. |
| 87 |
* @param Social_Profiles_Helper $social_profiles_helper The social profile helper. |
| 88 |
* @param Product_Helper $product_helper The product helper. |
| 89 |
* @param Meta_Tags_Context $meta_tags_context The meta tags context helper. |
| 90 |
*/ |
| 91 |
public function __construct( |
| 92 |
WPSEO_Admin_Asset_Manager $admin_asset_manager, |
| 93 |
WPSEO_Addon_Manager $addon_manager, |
| 94 |
WPSEO_Shortlinker $shortlinker, |
| 95 |
Options_Helper $options_helper, |
| 96 |
Social_Profiles_Helper $social_profiles_helper, |
| 97 |
Product_Helper $product_helper, |
| 98 |
Meta_Tags_Context $meta_tags_context |
| 99 |
) { |
| 100 |
$this->admin_asset_manager = $admin_asset_manager; |
| 101 |
$this->addon_manager = $addon_manager; |
| 102 |
$this->shortlinker = $shortlinker; |
| 103 |
$this->options_helper = $options_helper; |
| 104 |
$this->social_profiles_helper = $social_profiles_helper; |
| 105 |
$this->product_helper = $product_helper; |
| 106 |
$this->meta_tags_context = $meta_tags_context; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* {@inheritDoc} |
| 111 |
*/ |
| 112 |
public function register_hooks() { |
| 113 |
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); |
| 114 |
\add_action( 'wpseo_settings_tabs_dashboard', [ $this, 'add_first_time_configuration_tab' ] ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Adds a dedicated tab in the General sub-page. |
| 119 |
* |
| 120 |
* @param WPSEO_Options_Tabs $dashboard_tabs Object representing the tabs of the General sub-page. |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function add_first_time_configuration_tab( $dashboard_tabs ) { |
| 125 |
$dashboard_tabs->add_tab( |
| 126 |
new WPSEO_Option_Tab( |
| 127 |
'first-time-configuration', |
| 128 |
\__( 'First-time configuration', 'wordpress-seo' ), |
| 129 |
[ 'save_button' => false ], |
| 130 |
), |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Adds the data for the first-time configuration to the wpseoFirstTimeConfigurationData object. |
| 136 |
* |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public function enqueue_assets() { |
| 140 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Date is not processed or saved. |
| 141 |
if ( ! isset( $_GET['page'] ) || ( $_GET['page'] !== 'wpseo_dashboard' && $_GET['page'] !== General_Page_Integration::PAGE ) || \is_network_admin() ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
$this->admin_asset_manager->enqueue_script( 'indexation' ); |
| 146 |
$this->admin_asset_manager->enqueue_style( 'first-time-configuration' ); |
| 147 |
$this->admin_asset_manager->enqueue_style( 'admin-css' ); |
| 148 |
$this->admin_asset_manager->enqueue_style( 'monorepo' ); |
| 149 |
|
| 150 |
$data = [ |
| 151 |
'disabled' => ! \YoastSEO()->helpers->indexable->should_index_indexables(), |
| 152 |
'amount' => \YoastSEO()->helpers->indexing->get_filtered_unindexed_count(), |
| 153 |
'firstTime' => ( \YoastSEO()->helpers->indexing->is_initial_indexing() === true ), |
| 154 |
'errorMessage' => '', |
| 155 |
'restApi' => [ |
| 156 |
'root' => \esc_url_raw( \rest_url() ), |
| 157 |
'indexing_endpoints' => $this->get_endpoints(), |
| 158 |
'nonce' => \wp_create_nonce( 'wp_rest' ), |
| 159 |
], |
| 160 |
]; |
| 161 |
|
| 162 |
/** |
| 163 |
* Filter: 'wpseo_indexing_data' Filter to adapt the data used in the indexing process. |
| 164 |
* |
| 165 |
* @param array $data The indexing data to adapt. |
| 166 |
*/ |
| 167 |
$data = \apply_filters( 'wpseo_indexing_data', $data ); |
| 168 |
|
| 169 |
$this->admin_asset_manager->localize_script( 'indexation', 'yoastIndexingData', $data ); |
| 170 |
|
| 171 |
$person_id = $this->get_person_id(); |
| 172 |
$social_profiles = $this->get_social_profiles(); |
| 173 |
|
| 174 |
// This filter is documented in admin/views/tabs/metas/paper-content/general/knowledge-graph.php. |
| 175 |
$knowledge_graph_message = \apply_filters( 'wpseo_knowledge_graph_setting_msg', '' ); |
| 176 |
|
| 177 |
$finished_steps = $this->get_finished_steps(); |
| 178 |
$options = $this->get_company_or_person_options(); |
| 179 |
$selected_option_label = ''; |
| 180 |
$filtered_options = \array_filter( |
| 181 |
$options, |
| 182 |
function ( $item ) { |
| 183 |
return $item['value'] === $this->is_company_or_person(); |
| 184 |
}, |
| 185 |
); |
| 186 |
$selected_option = \reset( $filtered_options ); |
| 187 |
if ( \is_array( $selected_option ) ) { |
| 188 |
$selected_option_label = $selected_option['label']; |
| 189 |
} |
| 190 |
|
| 191 |
$data_ftc = [ |
| 192 |
'canEditUser' => $this->can_edit_profile( $person_id ), |
| 193 |
'companyOrPerson' => $this->is_company_or_person(), |
| 194 |
'companyOrPersonLabel' => $selected_option_label, |
| 195 |
'companyName' => $this->get_company_name(), |
| 196 |
'fallbackCompanyName' => $this->get_fallback_company_name( $this->get_company_name() ), |
| 197 |
'websiteName' => $this->get_website_name(), |
| 198 |
'fallbackWebsiteName' => $this->get_fallback_website_name( $this->get_website_name() ), |
| 199 |
'companyLogo' => $this->get_company_logo(), |
| 200 |
'companyLogoFallback' => $this->get_company_fallback_logo( $this->get_company_logo() ), |
| 201 |
'companyLogoId' => $this->get_person_logo_id(), |
| 202 |
'finishedSteps' => $finished_steps, |
| 203 |
'personId' => (int) $person_id, |
| 204 |
'personName' => $this->get_person_name(), |
| 205 |
'personLogo' => $this->get_person_logo(), |
| 206 |
'personLogoFallback' => $this->get_person_fallback_logo( $this->get_person_logo() ), |
| 207 |
'personLogoId' => $this->get_person_logo_id(), |
| 208 |
'siteTagline' => $this->get_site_tagline(), |
| 209 |
'socialProfiles' => [ |
| 210 |
'facebookUrl' => $social_profiles['facebook_site'], |
| 211 |
'twitterUsername' => $social_profiles['twitter_site'], |
| 212 |
'otherSocialUrls' => $social_profiles['other_social_urls'], |
| 213 |
], |
| 214 |
'isPremium' => $this->product_helper->is_premium(), |
| 215 |
'tracking' => $this->has_tracking_enabled(), |
| 216 |
'isTrackingAllowedMultisite' => $this->is_tracking_enabled_multisite(), |
| 217 |
'isMainSite' => $this->is_main_site(), |
| 218 |
'companyOrPersonOptions' => $options, |
| 219 |
'shouldForceCompany' => $this->should_force_company(), |
| 220 |
'knowledgeGraphMessage' => $knowledge_graph_message, |
| 221 |
'shortlinks' => [ |
| 222 |
'gdpr' => $this->shortlinker->build_shortlink( 'https://yoa.st/gdpr-config-workout' ), |
| 223 |
'configIndexables' => $this->shortlinker->build_shortlink( 'https://yoa.st/config-indexables' ), |
| 224 |
'configIndexablesBenefits' => $this->shortlinker->build_shortlink( 'https://yoa.st/config-indexables-benefits' ), |
| 225 |
], |
| 226 |
]; |
| 227 |
|
| 228 |
$this->admin_asset_manager->localize_script( 'general-page', 'wpseoFirstTimeConfigurationData', $data_ftc ); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Retrieves a list of the endpoints to use. |
| 233 |
* |
| 234 |
* @return array The endpoints. |
| 235 |
*/ |
| 236 |
protected function get_endpoints() { |
| 237 |
$endpoints = [ |
| 238 |
'prepare' => Indexing_Route::FULL_PREPARE_ROUTE, |
| 239 |
'terms' => Indexing_Route::FULL_TERMS_ROUTE, |
| 240 |
'posts' => Indexing_Route::FULL_POSTS_ROUTE, |
| 241 |
'archives' => Indexing_Route::FULL_POST_TYPE_ARCHIVES_ROUTE, |
| 242 |
'general' => Indexing_Route::FULL_GENERAL_ROUTE, |
| 243 |
'indexablesComplete' => Indexing_Route::FULL_INDEXABLES_COMPLETE_ROUTE, |
| 244 |
'post_link' => Indexing_Route::FULL_POST_LINKS_INDEXING_ROUTE, |
| 245 |
'term_link' => Indexing_Route::FULL_TERM_LINKS_INDEXING_ROUTE, |
| 246 |
]; |
| 247 |
|
| 248 |
$endpoints = \apply_filters( 'wpseo_indexing_endpoints', $endpoints ); |
| 249 |
|
| 250 |
$endpoints['complete'] = Indexing_Route::FULL_COMPLETE_ROUTE; |
| 251 |
|
| 252 |
return $endpoints; |
| 253 |
} |
| 254 |
|
| 255 |
// ** Private functions ** // |
| 256 |
|
| 257 |
/** |
| 258 |
* Returns the finished steps array. |
| 259 |
* |
| 260 |
* @return array An array with the finished steps. |
| 261 |
*/ |
| 262 |
private function get_finished_steps() { |
| 263 |
return $this->options_helper->get( 'configuration_finished_steps', [] ); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Returns the entity represented by the site. |
| 268 |
* |
| 269 |
* @return string The entity represented by the site. |
| 270 |
*/ |
| 271 |
private function is_company_or_person() { |
| 272 |
return $this->options_helper->get( 'company_or_person', '' ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Gets the company name from the option in the database. |
| 277 |
* |
| 278 |
* @return string The company name. |
| 279 |
*/ |
| 280 |
private function get_company_name() { |
| 281 |
return $this->options_helper->get( 'company_name', '' ); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Gets the fallback company name from the option in the database if there is no company name. |
| 286 |
* |
| 287 |
* @param string $company_name The given company name by the user, default empty string. |
| 288 |
* |
| 289 |
* @return string|false The company name. |
| 290 |
*/ |
| 291 |
private function get_fallback_company_name( $company_name ) { |
| 292 |
if ( $company_name ) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
return \get_bloginfo( 'name' ); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Gets the website name from the option in the database. |
| 301 |
* |
| 302 |
* @return string The website name. |
| 303 |
*/ |
| 304 |
private function get_website_name() { |
| 305 |
return $this->options_helper->get( 'website_name', '' ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Gets the fallback website name from the option in the database if there is no website name. |
| 310 |
* |
| 311 |
* @param string $website_name The given website name by the user, default empty string. |
| 312 |
* |
| 313 |
* @return string|false The website name. |
| 314 |
*/ |
| 315 |
private function get_fallback_website_name( $website_name ) { |
| 316 |
if ( $website_name ) { |
| 317 |
return false; |
| 318 |
} |
| 319 |
|
| 320 |
return \get_bloginfo( 'name' ); |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Gets the company logo from the option in the database. |
| 325 |
* |
| 326 |
* @return string The company logo. |
| 327 |
*/ |
| 328 |
private function get_company_logo() { |
| 329 |
return $this->options_helper->get( 'company_logo', '' ); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Gets the company logo id from the option in the database. |
| 334 |
* |
| 335 |
* @return string The company logo id. |
| 336 |
*/ |
| 337 |
private function get_company_logo_id() { |
| 338 |
return $this->options_helper->get( 'company_logo_id', '' ); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Gets the company logo url from the option in the database. |
| 343 |
* |
| 344 |
* @param string $company_logo The given company logo by the user, default empty. |
| 345 |
* |
| 346 |
* @return string|false The company logo URL. |
| 347 |
*/ |
| 348 |
private function get_company_fallback_logo( $company_logo ) { |
| 349 |
if ( $company_logo ) { |
| 350 |
return false; |
| 351 |
} |
| 352 |
$logo_id = $this->meta_tags_context->fallback_to_site_logo(); |
| 353 |
|
| 354 |
return \esc_url( \wp_get_attachment_url( $logo_id ) ); |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Gets the person id from the option in the database. |
| 359 |
* |
| 360 |
* @return int|null The person id, null if empty. |
| 361 |
*/ |
| 362 |
private function get_person_id() { |
| 363 |
return $this->options_helper->get( 'company_or_person_user_id' ); |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Gets the person id from the option in the database. |
| 368 |
* |
| 369 |
* @return int|null The person id, null if empty. |
| 370 |
*/ |
| 371 |
private function get_person_name() { |
| 372 |
$user = \get_userdata( $this->get_person_id() ); |
| 373 |
if ( $user instanceof WP_User ) { |
| 374 |
return $user->get( 'display_name' ); |
| 375 |
} |
| 376 |
|
| 377 |
return ''; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Gets the person avatar from the option in the database. |
| 382 |
* |
| 383 |
* @return string The person logo. |
| 384 |
*/ |
| 385 |
private function get_person_logo() { |
| 386 |
return $this->options_helper->get( 'person_logo', '' ); |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Gets the person logo url from the option in the database. |
| 391 |
* |
| 392 |
* @param string $person_logo The given person logo by the user, default empty. |
| 393 |
* |
| 394 |
* @return string|false The person logo URL. |
| 395 |
*/ |
| 396 |
private function get_person_fallback_logo( $person_logo ) { |
| 397 |
if ( $person_logo ) { |
| 398 |
return false; |
| 399 |
} |
| 400 |
$logo_id = $this->meta_tags_context->fallback_to_site_logo(); |
| 401 |
|
| 402 |
return \esc_url( \wp_get_attachment_url( $logo_id ) ); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Gets the person logo id from the option in the database. |
| 407 |
* |
| 408 |
* @return string The person logo id. |
| 409 |
*/ |
| 410 |
private function get_person_logo_id() { |
| 411 |
return $this->options_helper->get( 'person_logo_id', '' ); |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Gets the site tagline. |
| 416 |
* |
| 417 |
* @return string The site tagline. |
| 418 |
*/ |
| 419 |
private function get_site_tagline() { |
| 420 |
return \get_bloginfo( 'description' ); |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Gets the social profiles stored in the database. |
| 425 |
* |
| 426 |
* @return string[] The social profiles. |
| 427 |
*/ |
| 428 |
private function get_social_profiles() { |
| 429 |
return $this->social_profiles_helper->get_organization_social_profiles(); |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Checks whether tracking is enabled. |
| 434 |
* |
| 435 |
* @return bool True if tracking is enabled, false otherwise, null if in Free and conf. workout step not finished. |
| 436 |
*/ |
| 437 |
private function has_tracking_enabled() { |
| 438 |
$default = false; |
| 439 |
|
| 440 |
if ( $this->product_helper->is_premium() ) { |
| 441 |
$default = true; |
| 442 |
} |
| 443 |
|
| 444 |
return $this->options_helper->get( 'tracking', $default ); |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Checks whether tracking option is allowed at network level. |
| 449 |
* |
| 450 |
* @return bool True if option change is allowed, false otherwise. |
| 451 |
*/ |
| 452 |
private function is_tracking_enabled_multisite() { |
| 453 |
$default = true; |
| 454 |
|
| 455 |
if ( ! \is_multisite() ) { |
| 456 |
return $default; |
| 457 |
} |
| 458 |
|
| 459 |
return $this->options_helper->get( 'allow_tracking', $default ); |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Checks whether we are in a main site. |
| 464 |
* |
| 465 |
* @return bool True if it's the main site or a single site, false if it's a subsite. |
| 466 |
*/ |
| 467 |
private function is_main_site() { |
| 468 |
return \is_main_site(); |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Gets the options for the Company or Person select. |
| 473 |
* Returns only the company option if it is forced (by Local SEO), otherwise returns company and person option. |
| 474 |
* |
| 475 |
* @return array The options for the company-or-person select. |
| 476 |
*/ |
| 477 |
private function get_company_or_person_options() { |
| 478 |
$options = [ |
| 479 |
[ |
| 480 |
'label' => \__( 'Organization', 'wordpress-seo' ), |
| 481 |
'value' => 'company', |
| 482 |
'id' => 'company', |
| 483 |
], |
| 484 |
[ |
| 485 |
'label' => \__( 'Person', 'wordpress-seo' ), |
| 486 |
'value' => 'person', |
| 487 |
'id' => 'person', |
| 488 |
], |
| 489 |
]; |
| 490 |
if ( $this->should_force_company() ) { |
| 491 |
$options = [ |
| 492 |
[ |
| 493 |
'label' => \__( 'Organization', 'wordpress-seo' ), |
| 494 |
'value' => 'company', |
| 495 |
'id' => 'company', |
| 496 |
], |
| 497 |
]; |
| 498 |
} |
| 499 |
|
| 500 |
return $options; |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* Checks whether we should force "Organization". |
| 505 |
* |
| 506 |
* @return bool |
| 507 |
*/ |
| 508 |
private function should_force_company() { |
| 509 |
return $this->addon_manager->is_installed( WPSEO_Addon_Manager::LOCAL_SLUG ); |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Checks if the current user has the capability to edit a specific user. |
| 514 |
* |
| 515 |
* @param int $person_id The id of the person to edit. |
| 516 |
* |
| 517 |
* @return bool |
| 518 |
*/ |
| 519 |
private function can_edit_profile( $person_id ) { |
| 520 |
return \current_user_can( 'edit_user', $person_id ); |
| 521 |
} |
| 522 |
} |
| 523 |
|