class-schema-website.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend\Schema
  6. */
  7. /**
  8. * Returns schema Website data.
  9. *
  10. * @since 10.2
  11. */
  12. class WPSEO_Schema_Website 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_Website 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 or not a piece should be added to the graph.
  29. *
  30. * @return bool
  31. */
  32. public function is_needed() {
  33. return true;
  34. }
  35. /**
  36. * Outputs code to allow recognition of the internal search engine.
  37. *
  38. * @since 1.5.7
  39. *
  40. * @link https://developers.google.com/structured-data/site-name
  41. *
  42. * @return array Website data blob.
  43. */
  44. public function generate() {
  45. $data = [
  46. '@type' => 'WebSite',
  47. '@id' => $this->context->site_url . WPSEO_Schema_IDs::WEBSITE_HASH,
  48. 'url' => $this->context->site_url,
  49. 'name' => $this->context->site_name,
  50. ];
  51. if ( $this->context->site_description ) {
  52. $data['description'] = $this->context->site_description;
  53. }
  54. if ( $this->context->site_represents_reference ) {
  55. $data['publisher'] = $this->context->site_represents_reference;
  56. }
  57. $data = $this->add_alternate_name( $data );
  58. $data = $this->internal_search_section( $data );
  59. return $data;
  60. }
  61. /**
  62. * Returns an alternate name if one was specified in the Yoast SEO settings.
  63. *
  64. * @param array $data The website data array.
  65. *
  66. * @return array $data
  67. */
  68. private function add_alternate_name( $data ) {
  69. if ( '' !== WPSEO_Options::get( 'alternate_website_name', '' ) ) {
  70. $data['alternateName'] = WPSEO_Options::get( 'alternate_website_name' );
  71. }
  72. return $data;
  73. }
  74. /**
  75. * Adds the internal search JSON LD code to the homepage if it's not disabled.
  76. *
  77. * @link https://developers.google.com/structured-data/slsb-overview
  78. *
  79. * @param array $data The website data array.
  80. *
  81. * @return array $data
  82. */
  83. private function internal_search_section( $data ) {
  84. /**
  85. * Filter: 'disable_wpseo_json_ld_search' - Allow disabling of the json+ld output.
  86. *
  87. * @api bool $display_search Whether or not to display json+ld search on the frontend.
  88. */
  89. if ( ! apply_filters( 'disable_wpseo_json_ld_search', false ) ) {
  90. /**
  91. * Filter: 'wpseo_json_ld_search_url' - Allows filtering of the search URL for Yoast SEO.
  92. *
  93. * @api string $search_url The search URL for this site with a `{search_term_string}` variable.
  94. */
  95. $search_url = apply_filters( 'wpseo_json_ld_search_url', $this->context->site_url . '?s={search_term_string}' );
  96. $data['potentialAction'] = [
  97. '@type' => 'SearchAction',
  98. 'target' => $search_url,
  99. 'query-input' => 'required name=search_term_string',
  100. ];
  101. }
  102. return $data;
  103. }
  104. }