← All changes
|
jetpack_vendor/automattic/jetpack-seo/src/class-website-schema-node.php
+69
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,69 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Site-level WebSite Schema.org node builder. | |
| 4 | + * | |
| 5 | + * Builds the WebSite JSON-LD node from existing site identity: Site Title, | |
| 6 | + * site URL, Tagline, site language, and core WordPress search. | |
| 7 | + * | |
| 8 | + * @package automattic/jetpack-seo-package | |
| 9 | + */ | |
| 10 | + | |
| 11 | +namespace Automattic\Jetpack\SEO; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Builds the site-level WebSite node. | |
| 15 | + */ | |
| 16 | +class Website_Schema_Node { | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * Build the WebSite node, or null when the site has no name to identify it. | |
| 20 | + * | |
| 21 | + * @return array|null | |
| 22 | + */ | |
| 23 | + public static function build() { | |
| 24 | + $name = self::text( get_bloginfo( 'name' ) ); | |
| 25 | + if ( '' === $name ) { | |
| 26 | + return null; | |
| 27 | + } | |
| 28 | + | |
| 29 | + $node = array( | |
| 30 | + '@type' => 'WebSite', | |
| 31 | + '@id' => Schema_Node_Ids::website(), | |
| 32 | + 'name' => $name, | |
| 33 | + 'url' => home_url( '/' ), | |
| 34 | + 'potentialAction' => array( | |
| 35 | + '@type' => 'SearchAction', | |
| 36 | + 'target' => array( | |
| 37 | + '@type' => 'EntryPoint', | |
| 38 | + 'urlTemplate' => home_url( '/?s={search_term_string}' ), | |
| 39 | + ), | |
| 40 | + 'query-input' => 'required name=search_term_string', | |
| 41 | + ), | |
| 42 | + ); | |
| 43 | + | |
| 44 | + $description = self::text( get_bloginfo( 'description' ) ); | |
| 45 | + if ( '' !== $description ) { | |
| 46 | + $node['description'] = $description; | |
| 47 | + } | |
| 48 | + | |
| 49 | + $language = self::text( get_bloginfo( 'language' ) ); | |
| 50 | + if ( '' !== $language ) { | |
| 51 | + $node['inLanguage'] = $language; | |
| 52 | + } | |
| 53 | + | |
| 54 | + return $node; | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Normalize a scalar site value to trimmed plain text. | |
| 59 | + * | |
| 60 | + * @param mixed $value Raw value. | |
| 61 | + * @return string | |
| 62 | + */ | |
| 63 | + private static function text( $value ) { | |
| 64 | + if ( ! is_string( $value ) ) { | |
| 65 | + return ''; | |
| 66 | + } | |
| 67 | + return trim( wp_strip_all_tags( $value ) ); | |
| 68 | + } | |
| 69 | +} | |