Completed
Pull Request — master (#1047)
by David
02:34
created

Jsonld_Endpoint::jsonld_using_post_meta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
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
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)/(?P<meta_value>.*)', array(
68
				'methods'  => 'GET',
69
				'callback' => array( $that, 'jsonld_using_post_meta' ),
70
			) );
71
72
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array(
73
				'methods'  => 'GET',
74
				'callback' => array( $that, 'jsonld_using_get_page_by_path' ),
75
			) );
76
77
		} );
78
79
	}
80
81
	/**
82
	 * Callback for the JSON-LD request.
83
	 *
84
	 * @param array $request {
85
	 *  The request array.
86
	 *
87
	 * @type int $id The post id.
88
	 * }
89
	 *
90
	 * @return WP_REST_Response
91
	 * @throws \Exception
92
	 */
93
	public function jsonld_using_post_id( $request ) {
94
95
		$post_id     = $request['id'];
96
		$is_homepage = ( 0 === $post_id );
97
98
		// Send the generated JSON-LD.
99
		$data     = $this->jsonld_service->get_jsonld( $is_homepage, $post_id );
100
		$response = new WP_REST_Response( $data );
101
102
		$cache_in_seconds = 86400;
103
		$date_timezone    = new DateTimeZone( 'GMT' );
104
		$date_now         = new DateTime( 'now', $date_timezone );
105
		$date_interval    = new DateInterval( "PT{$cache_in_seconds}S" );
106
		$expires          = $date_now->add( $date_interval )->format( 'D, j M Y H:i:s T' );
107
108
		$response->set_headers( array(
109
			'Content-Type'  => 'application/ld+json; charset=' . get_option( 'blog_charset' ),
110
			'Cache-Control' => "max-age=$cache_in_seconds",
111
			'Expires'       => $expires
112
		) );
113
114
		return $response;
115
	}
116
117
	/**
118
	 * Provide a JSON-LD given the itemId.
119
	 *
120
	 * @param array $request {
121
	 *  The request array.
122
	 *
123
	 * @type string $item_id The entity item id.
124
	 * }
125
	 *
126
	 * @return WP_REST_Response
127
	 * @throws \Exception
128
	 */
129
	public function jsonld_using_item_id( $request ) {
130
131
		$item_id = 'http://' . $request['item_id'];
132
		$post    = $this->entity_uri_service->get_entity( $item_id );
133
134
		if ( ! is_a( $post, 'WP_Post' ) ) {
135
			return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
136
		}
137
138
		return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) );
139
	}
140
141
	public function jsonld_using_get_page_by_path( $request ) {
142
143
		$post_name = $request['post_name'];
144
		$post_type = $request['post_type'];
145
146
		global $wpdb;
147
148
		$sql = "
149
			SELECT ID
150
			FROM $wpdb->posts
151
			WHERE post_name = %s
152
			 AND post_type = %s
153
		";
154
155
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) );
156
157
		if ( is_null( $post_id ) ) {
158
			return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
159
		}
160
161
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
162
	}
163
164
	public function jsonld_using_post_meta( $request ) {
165
166
		$meta_key   = $request['meta_key'];
167
		$meta_value = rawurldecode( $request['meta_value'] );
168
169
		global $wpdb;
170
171
		$sql = "
172
			SELECT post_id AS ID
173
			FROM $wpdb->postmeta
174
			WHERE meta_key = %s
175
			 AND meta_value LIKE %s
176
			LIMIT 1
177
		";
178
179
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, '%' . $wpdb->esc_like( $meta_value ) . '%' ) );
180
181
		if ( is_null( $post_id ) ) {
182
			return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) );
183
		}
184
185
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
186
	}
187
188
}