| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Generators\Schema; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Config\Schema_IDs; |
| 6 |
|
| 7 |
/** |
| 8 |
* Returns schema Website data. |
| 9 |
*/ |
| 10 |
class Website extends Abstract_Schema_Piece { |
| 11 |
|
| 12 |
/** |
| 13 |
* Determines whether or not a piece should be added to the graph. |
| 14 |
* |
| 15 |
* @return bool |
| 16 |
*/ |
| 17 |
public function is_needed() { |
| 18 |
return true; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Outputs code to allow recognition of the internal search engine. |
| 23 |
* |
| 24 |
* @return array Website data blob. |
| 25 |
*/ |
| 26 |
public function generate() { |
| 27 |
$data = [ |
| 28 |
'@type' => 'WebSite', |
| 29 |
'@id' => $this->context->site_url . Schema_IDs::WEBSITE_HASH, |
| 30 |
'url' => $this->context->site_url, |
| 31 |
'name' => $this->helpers->schema->html->smart_strip_tags( $this->context->site_name ), |
| 32 |
'description' => \get_bloginfo( 'description' ), |
| 33 |
]; |
| 34 |
|
| 35 |
if ( $this->context->site_represents_reference ) { |
| 36 |
$data['publisher'] = $this->context->site_represents_reference; |
| 37 |
} |
| 38 |
|
| 39 |
$data = $this->add_alternate_name( $data ); |
| 40 |
$data = $this->internal_search_section( $data ); |
| 41 |
$data = $this->helpers->schema->language->add_piece_language( $data ); |
| 42 |
|
| 43 |
return $data; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns an alternate name if one was specified in the Yoast SEO settings. |
| 48 |
* |
| 49 |
* @param array $data The website data array. |
| 50 |
* |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
private function add_alternate_name( $data ) { |
| 54 |
if ( $this->context->alternate_site_name !== '' ) { |
| 55 |
$data['alternateName'] = $this->helpers->schema->html->smart_strip_tags( $this->context->alternate_site_name ); |
| 56 |
} |
| 57 |
|
| 58 |
return $data; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Adds the internal search JSON LD code to the homepage if it's not disabled. |
| 63 |
* |
| 64 |
* @link https://developers.google.com/search/docs/data-types/sitelinks-searchbox |
| 65 |
* |
| 66 |
* @param array $data The website data array. |
| 67 |
* |
| 68 |
* @return array |
| 69 |
*/ |
| 70 |
private function internal_search_section( $data ) { |
| 71 |
/** |
| 72 |
* Filter: 'disable_wpseo_json_ld_search' - Allow disabling of the json+ld output. |
| 73 |
* |
| 74 |
* @param bool $display_search Whether or not to display json+ld search on the frontend. |
| 75 |
*/ |
| 76 |
if ( \apply_filters( 'disable_wpseo_json_ld_search', false ) ) { |
| 77 |
return $data; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Filter: 'wpseo_json_ld_search_url' - Allows filtering of the search URL for Yoast SEO. |
| 82 |
* |
| 83 |
* @param string $search_url The search URL for this site with a `{search_term_string}` variable. |
| 84 |
*/ |
| 85 |
$search_url = \apply_filters( 'wpseo_json_ld_search_url', $this->context->site_url . '?s={search_term_string}' ); |
| 86 |
|
| 87 |
$data['potentialAction'][] = [ |
| 88 |
'@type' => 'SearchAction', |
| 89 |
'target' => [ |
| 90 |
'@type' => 'EntryPoint', |
| 91 |
'urlTemplate' => $search_url, |
| 92 |
], |
| 93 |
'query-input' => [ |
| 94 |
'@type' => 'PropertyValueSpecification', |
| 95 |
'valueRequired' => true, |
| 96 |
'valueName' => 'search_term_string', |
| 97 |
], |
| 98 |
]; |
| 99 |
|
| 100 |
return $data; |
| 101 |
} |
| 102 |
} |
| 103 |
|