Completed
Push — develop ( bf654c...059c7c )
by David
02:47
created

Wordlift_Website_Jsonld_Converter::create_schema()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Converters: JSON-LD Website Converter.
4
 *
5
 * This file defines a converter for home and blog pages to JSON-LD array.
6
 *
7
 * @since   3.14.0
8
 * @package Wordlift
9
 */
10
11
/**
12
 * Define the {@link Wordlift_Website_Jsonld_Converter} class.
13
 *
14
 * @since 3.14.0
15
 */
16
class Wordlift_Website_Jsonld_Converter extends Wordlift_Post_To_Jsonld_Converter {
17
18
	/**
19
	 * Convert the home/blog page to a JSON-LD array.
20
	 *
21
	 * @since 3.14.0
22
	 *
23
	 * @return array A JSON-LD array.
24
	 */
25
	public function create_schema() {
26
27
		// Create new jsonld.
28
		$home_url = home_url( '/' );
29
30
		$jsonld = array(
31
			'@context'      => 'http://schema.org',
32
			'@type'         => 'WebSite',
33
			'@id'           => "$home_url#website",
34
			'name'          => get_bloginfo( 'name' ),
35
			'alternateName' => get_bloginfo( 'description' ),
36
			'url'           => $home_url,
37
		);
38
39
		// Add publisher information.
40
		$this->set_publisher( $jsonld );
41
42
		// Add search action.
43
		$this->set_search_action( $jsonld );
44
45
		/**
46
		 * Call the `wl_website_jsonld` filter.
47
		 *
48
		 * @api
49
		 *
50
		 * @since 3.14.0
51
		 *
52
		 * @param array $jsonld The JSON-LD structure.
53
		 */
54
		return apply_filters( 'wl_website_jsonld', $jsonld );
55
	}
56
57
	/**
58
	 * Add SearchAction part to the schema
59
	 *
60
	 * @since 3.14.0
61
	 *
62
	 * @param array $params The parameters array.
63
	 */
64
	private function set_search_action( &$params ) {
65
		/**
66
		 * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL.
67
		 *
68
		 * @since  3.14.0
69
		 * @api    string $search_url The search URL for this site with a `{search_term_string}` variable.
70
		 */
71
		$search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' );
72
73
		// Add search action
74
		$params['potentialAction'] = array(
75
			'@type'       => 'SearchAction',
76
			'target'      => $search_url,
77
			'query-input' => 'required name=search_term_string',
78
		);
79
80
	}
81
82
}
83