class-schema-organization.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend\Schema
  6. */
  7. /**
  8. * Returns schema Organization data.
  9. *
  10. * @since 10.2
  11. */
  12. class WPSEO_Schema_Organization implements WPSEO_Graph_Piece {
  13. /**
  14. * A value object with context variables.
  15. *
  16. * @var WPSEO_Schema_Context
  17. */
  18. private $context;
  19. /**
  20. * WPSEO_Schema_Organization constructor.
  21. *
  22. * @param WPSEO_Schema_Context $context A value object with context variables.
  23. */
  24. public function __construct( WPSEO_Schema_Context $context ) {
  25. $this->context = $context;
  26. }
  27. /**
  28. * Determines whether an Organization graph piece should be added.
  29. *
  30. * @return bool
  31. */
  32. public function is_needed() {
  33. return ( $this->context->site_represents === 'company' );
  34. }
  35. /**
  36. * Returns the Organization Schema data.
  37. *
  38. * @return array $data The Organization schema.
  39. */
  40. public function generate() {
  41. $data = [
  42. '@type' => 'Organization',
  43. '@id' => $this->context->site_url . WPSEO_Schema_IDs::ORGANIZATION_HASH,
  44. 'name' => $this->context->company_name,
  45. 'url' => $this->context->site_url,
  46. 'sameAs' => $this->fetch_social_profiles(),
  47. ];
  48. $data = $this->add_logo( $data );
  49. return $data;
  50. }
  51. /**
  52. * Adds a site's logo.
  53. *
  54. * @param array $data The Organization schema.
  55. *
  56. * @return array $data The Organization schema.
  57. */
  58. private function add_logo( $data ) {
  59. $schema_id = $this->context->site_url . WPSEO_Schema_IDs::ORGANIZATION_LOGO_HASH;
  60. $schema_image = new WPSEO_Schema_Image( $schema_id );
  61. $data['logo'] = $schema_image->generate_from_attachment_id( $this->context->company_logo_id, $this->context->company_name );
  62. $data['image'] = [ '@id' => $schema_id ];
  63. return $data;
  64. }
  65. /**
  66. * Retrieve the social profiles to display in the organization schema.
  67. *
  68. * @since 1.8
  69. *
  70. * @link https://developers.google.com/webmasters/structured-data/customize/social-profiles
  71. *
  72. * @return array $profiles An array of social profiles.
  73. */
  74. private function fetch_social_profiles() {
  75. $profiles = [];
  76. $social_profiles = [
  77. 'facebook_site',
  78. 'instagram_url',
  79. 'linkedin_url',
  80. 'myspace_url',
  81. 'youtube_url',
  82. 'pinterest_url',
  83. 'wikipedia_url',
  84. ];
  85. foreach ( $social_profiles as $profile ) {
  86. if ( WPSEO_Options::get( $profile, '' ) !== '' ) {
  87. $profiles[] = WPSEO_Options::get( $profile );
  88. }
  89. }
  90. if ( WPSEO_Options::get( 'twitter_site', '' ) !== '' ) {
  91. $profiles[] = 'https://twitter.com/' . WPSEO_Options::get( 'twitter_site' );
  92. }
  93. /**
  94. * Filter: 'wpseo_schema_organization_social_profiles' - Allows filtering social profiles for the
  95. * represented organization.
  96. *
  97. * @api string[] $profiles
  98. */
  99. $profiles = apply_filters( 'wpseo_schema_organization_social_profiles', $profiles );
  100. return $profiles;
  101. }
  102. }