Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.30% covered (success)
96.30%
26 / 27
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Website_Schema_Node
96.30% covered (success)
96.30%
26 / 27
50.00% covered (danger)
50.00%
1 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 build
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
4
 text
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
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
11namespace Automattic\Jetpack\SEO;
12
13/**
14 * Builds the site-level WebSite node.
15 */
16class 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}