class-schema-webpage.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend\Schema
  6. */
  7. /**
  8. * Returns schema WebPage data.
  9. *
  10. * @since 10.2
  11. */
  12. class WPSEO_Schema_WebPage implements WPSEO_Graph_Piece {
  13. /**
  14. * The date helper.
  15. *
  16. * @var WPSEO_Date_Helper
  17. */
  18. protected $date;
  19. /**
  20. * A value object with context variables.
  21. *
  22. * @var WPSEO_Schema_Context
  23. */
  24. private $context;
  25. /**
  26. * WPSEO_Schema_WebPage constructor.
  27. *
  28. * @param WPSEO_Schema_Context $context A value object with context variables.
  29. */
  30. public function __construct( WPSEO_Schema_Context $context ) {
  31. $this->context = $context;
  32. $this->date = new WPSEO_Date_Helper();
  33. }
  34. /**
  35. * Determines whether or not a piece should be added to the graph.
  36. *
  37. * @return bool
  38. */
  39. public function is_needed() {
  40. if ( is_404() ) {
  41. return false;
  42. }
  43. return true;
  44. }
  45. /**
  46. * Returns WebPage schema data.
  47. *
  48. * @return array WebPage schema data.
  49. */
  50. public function generate() {
  51. $data = [
  52. '@type' => $this->determine_page_type(),
  53. '@id' => $this->context->canonical . WPSEO_Schema_IDs::WEBPAGE_HASH,
  54. 'url' => $this->context->canonical,
  55. 'inLanguage' => get_bloginfo( 'language' ),
  56. 'name' => $this->context->title,
  57. 'isPartOf' => [
  58. '@id' => $this->context->site_url . WPSEO_Schema_IDs::WEBSITE_HASH,
  59. ],
  60. ];
  61. if ( is_front_page() ) {
  62. if ( $this->context->site_represents_reference ) {
  63. $data['about'] = $this->context->site_represents_reference;
  64. }
  65. }
  66. if ( is_singular() ) {
  67. $this->add_image( $data );
  68. $post = get_post( $this->context->id );
  69. $data['datePublished'] = $this->date->format( $post->post_date_gmt );
  70. $data['dateModified'] = $this->date->format( $post->post_modified_gmt );
  71. if ( get_post_type( $post ) === 'post' ) {
  72. $data = $this->add_author( $data, $post );
  73. }
  74. }
  75. if ( ! empty( $this->context->description ) ) {
  76. $data['description'] = strip_tags( $this->context->description, '<h1><h2><h3><h4><h5><h6><br><ol><ul><li><a><p><b><strong><i><em>' );
  77. }
  78. if ( $this->add_breadcrumbs() ) {
  79. $data['breadcrumb'] = [
  80. '@id' => $this->context->canonical . WPSEO_Schema_IDs::BREADCRUMB_HASH,
  81. ];
  82. }
  83. return $data;
  84. }
  85. /**
  86. * Adds an author property to the $data if the WebPage is not represented.
  87. *
  88. * @param array $data The WebPage schema.
  89. * @param WP_Post $post The post the context is representing.
  90. *
  91. * @return array The WebPage schema.
  92. */
  93. public function add_author( $data, $post ) {
  94. if ( $this->context->site_represents === false ) {
  95. $data['author'] = [ '@id' => WPSEO_Schema_Utils::get_user_schema_id( $post->post_author, $this->context ) ];
  96. }
  97. return $data;
  98. }
  99. /**
  100. * If we have an image, make it the primary image of the page.
  101. *
  102. * @param array $data WebPage schema data.
  103. */
  104. public function add_image( &$data ) {
  105. if ( $this->context->has_image ) {
  106. $data['primaryImageOfPage'] = [ '@id' => $this->context->canonical . WPSEO_Schema_IDs::PRIMARY_IMAGE_HASH ];
  107. }
  108. }
  109. /**
  110. * Determine if we should add a breadcrumb attribute.
  111. *
  112. * @return bool
  113. */
  114. private function add_breadcrumbs() {
  115. if ( is_front_page() ) {
  116. return false;
  117. }
  118. if ( $this->context->breadcrumbs_enabled ) {
  119. return true;
  120. }
  121. return false;
  122. }
  123. /**
  124. * Determine the page type for the current page.
  125. *
  126. * @return string
  127. */
  128. private function determine_page_type() {
  129. switch ( true ) {
  130. case is_search():
  131. $type = 'SearchResultsPage';
  132. break;
  133. case is_author():
  134. $type = 'ProfilePage';
  135. break;
  136. case WPSEO_Frontend_Page_Type::is_posts_page():
  137. case WPSEO_Frontend_Page_Type::is_home_posts_page():
  138. case is_archive():
  139. $type = 'CollectionPage';
  140. break;
  141. default:
  142. $type = 'WebPage';
  143. }
  144. /**
  145. * Filter: 'wpseo_schema_webpage_type' - Allow changing the WebPage type.
  146. *
  147. * @api string $type The WebPage type.
  148. */
  149. return apply_filters( 'wpseo_schema_webpage_type', $type );
  150. }
  151. }