Completed
Push — develop ( fc3971...71605d )
by David
03:09 queued 11s
created

Jsonld_Adapter::wp_head()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 0
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file defines the JSON-LD adapter.
4
 *
5
 * The JSON-LD adapter hooks to the WordPress `wp_head` function to publish the JSON-LD in the `<head></head>` fragment.
6
 * At the same time the `wlSettings` localize script structure turns the JSON-LD off for asynchronous processing.
7
 *
8
 * @author David Riccitelli <[email protected]>
9
 * @package Wordlift\Jsonld
10
 * @since 3.25.1
11
 */
12
13
namespace Wordlift\Jsonld;
14
15
use Wordlift_Jsonld_Service;
16
17
/**
18
 * Class Jsonld_Adapter
19
 *
20
 * @package Wordlift\Jsonld
21
 */
22
class Jsonld_Adapter {
23
24
	/**
25
	 * @var Wordlift_Jsonld_Service
26
	 */
27
	private $jsonld_service;
28
29
	/**
30
	 * Jsonld_Adapter constructor.
31
	 *
32
	 * @param \Wordlift_Jsonld_Service $jsonld_service
33
	 */
34
	public function __construct( $jsonld_service ) {
35
36
		$this->jsonld_service = $jsonld_service;
37
38
		add_action( 'wp_head', array( $this, 'wp_head' ) );
39
40
	}
41
42
	public function wp_head() {
43
44
		// Bail out if `wl_jsonld_enabled` isn't enabled.
45
		if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
46
			return;
47
		}
48
49
		// Determine whether this is the home page or whether we're displaying a single post.
50
		$is_homepage = is_home() || is_front_page();
51
		$post_id     = is_singular() ? get_the_ID() : null;
52
53
		// Get the JSON-LD.
54
		$jsonld = json_encode( $this->jsonld_service->get_jsonld( $is_homepage, $post_id ) );
55
		// Finally print the JSON-LD out.
56
		?>
57
        <script type="application/ld+json" id="wl-jsonld"><?php echo $jsonld; ?></script>
58
		<?php
59
60
	}
61
62
}
63