Completed
Push — master ( 10cfb1...e88622 )
by David
02:17
created

Jsonld_Endpoint::jsonld_using_post_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file defines the JSON-LD endpoint.
4
 *
5
 * @author David Riccitelli <[email protected]>
6
 * @package Wordlift\Jsonld
7
 */
8
9
namespace Wordlift\Jsonld;
10
11
use DateInterval;
12
use DateTime;
13
use DateTimeZone;
14
use Wordlift_Jsonld_Service;
15
use WP_REST_Response;
16
17
/**
18
 * Class Jsonld_Endpoint
19
 *
20
 * @package Wordlift\Jsonld
21
 */
22
class Jsonld_Endpoint {
23
24
	/**
25
	 * The {@link Wordlift_Jsonld_Service} instance.
26
	 *
27
	 * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance.
28
	 */
29
	private $jsonld_service;
30
	/**
31
	 * @var \Wordlift_Entity_Uri_Service
32
	 */
33
	private $entity_uri_service;
34
35
	/**
36
	 * Jsonld_Endpoint constructor.
37
	 *
38
	 * @param \Wordlift_Jsonld_Service $jsonld_service
39
	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service
40
	 */
41
	public function __construct( $jsonld_service, $entity_uri_service ) {
42
43
		$this->jsonld_service     = $jsonld_service;
44
		$this->entity_uri_service = $entity_uri_service;
45
46
		// PHP 5.3 compatibility.
47
		$that = $this;
48
		add_action( 'rest_api_init', function () use ( $that ) {
49
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array(
50
				'methods'  => 'GET',
51
				'callback' => array( $that, 'jsonld_using_post_id' ),
52
				'args'     => array(
53
					'id' => array(
54
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
							return is_numeric( $param );
56
						},
57
						'sanitize_callback' => 'absint',
58
					),
59
				)
60
			) );
61
62
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array(
63
				'methods'  => 'GET',
64
				'callback' => array( $that, 'jsonld_using_item_id' ),
65
			) );
66
		} );
67
68
	}
69
70
	/**
71
	 * Callback for the JSON-LD request.
72
	 *
73
	 * @param array $request {
74
	 *  The request array.
75
	 *
76
	 * @type int $id The post id.
77
	 * }
78
	 *
79
	 * @return WP_REST_Response
80
	 * @throws \Exception
81
	 */
82
	public function jsonld_using_post_id( $request ) {
83
84
		$post_id     = $request['id'];
85
		$is_homepage = ( 0 === $post_id );
86
87
		// Send the generated JSON-LD.
88
		$data     = $this->jsonld_service->get_jsonld( $is_homepage, $post_id );
89
		$response = new WP_REST_Response( $data );
90
91
		$cache_in_seconds = 86400;
92
		$date_timezone    = new DateTimeZone( 'GMT' );
93
		$date_now         = new DateTime( 'now', $date_timezone );
94
		$date_interval    = new DateInterval( "PT{$cache_in_seconds}S" );
95
		$expires          = $date_now->add( $date_interval )->format( 'D, j M Y H:i:s T' );
96
97
		$response->set_headers( array(
98
			'Content-Type'  => 'application/ld+json; charset=' . get_option( 'blog_charset' ),
99
			'Cache-Control' => "max-age=$cache_in_seconds",
100
			'Expires'       => $expires
101
		) );
102
103
		return $response;
104
	}
105
106
	/**
107
	 * Provide a JSON-LD given the itemId.
108
	 *
109
	 * @param array $request {
110
	 *  The request array.
111
	 *
112
	 * @type string $item_id The entity item id.
113
	 * }
114
	 *
115
	 * @return WP_REST_Response
116
	 * @throws \Exception
117
	 */
118
	public function jsonld_using_item_id( $request ) {
119
120
		$item_id = 'http://' . $request['item_id'];
121
		$post    = $this->entity_uri_service->get_entity( $item_id );
122
123
		if ( ! is_a( $post, 'WP_Post' ) ) {
124
			return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
125
		}
126
127
		return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) );
128
	}
129
130
131
}