Completed
Push — master ( 38c060...9e660e )
by David
02:43
created

Jsonld_Endpoint::jsonld_using_post_meta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 23
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_Request;
16
use WP_REST_Response;
17
18
/**
19
 * Class Jsonld_Endpoint
20
 *
21
 * @package Wordlift\Jsonld
22
 */
23
class Jsonld_Endpoint {
24
25
	/**
26
	 * The {@link Wordlift_Jsonld_Service} instance.
27
	 *
28
	 * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance.
29
	 */
30
	private $jsonld_service;
31
	/**
32
	 * @var \Wordlift_Entity_Uri_Service
33
	 */
34
	private $entity_uri_service;
35
36
	/**
37
	 * Jsonld_Endpoint constructor.
38
	 *
39
	 * @param \Wordlift_Jsonld_Service $jsonld_service
40
	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service
41
	 */
42
	public function __construct( $jsonld_service, $entity_uri_service ) {
43
44
		$this->jsonld_service     = $jsonld_service;
45
		$this->entity_uri_service = $entity_uri_service;
46
47
		// PHP 5.3 compatibility.
48
		$that = $this;
49
		add_action( 'rest_api_init', function () use ( $that ) {
50
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array(
51
				'methods'  => 'GET',
52
				'callback' => array( $that, 'jsonld_using_post_id' ),
53
				'args'     => array(
54
					'id' => array(
55
						'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...
56
							return is_numeric( $param );
57
						},
58
						'sanitize_callback' => 'absint',
59
					),
60
				)
61
			) );
62
63
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array(
64
				'methods'  => 'GET',
65
				'callback' => array( $that, 'jsonld_using_item_id' ),
66
			) );
67
68
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array(
69
				'methods'  => 'GET',
70
				'callback' => array( $that, 'jsonld_using_post_meta' ),
71
			) );
72
73
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array(
74
				'methods'  => 'GET',
75
				'callback' => array( $that, 'jsonld_using_get_page_by_path' ),
76
			) );
77
78
		} );
79
80
	}
81
82
	/**
83
	 * Callback for the JSON-LD request.
84
	 *
85
	 * @param array $request {
86
	 *  The request array.
87
	 *
88
	 * @type int $id The post id.
89
	 * }
90
	 *
91
	 * @return WP_REST_Response
92
	 * @throws \Exception
93
	 */
94
	public function jsonld_using_post_id( $request ) {
95
96
		$post_id     = $request['id'];
97
		$is_homepage = ( 0 === $post_id );
98
99
		// Send the generated JSON-LD.
100
		$data     = $this->jsonld_service->get_jsonld( $is_homepage, $post_id );
101
		$response = new WP_REST_Response( $data );
102
103
		$cache_in_seconds = 86400;
104
		$date_timezone    = new DateTimeZone( 'GMT' );
105
		$date_now         = new DateTime( 'now', $date_timezone );
106
		$date_interval    = new DateInterval( "PT{$cache_in_seconds}S" );
107
		$expires          = $date_now->add( $date_interval )->format( 'D, j M Y H:i:s T' );
108
109
		$response->set_headers( array(
110
			'Content-Type'  => 'application/ld+json; charset=' . get_option( 'blog_charset' ),
111
			'Cache-Control' => "max-age=$cache_in_seconds",
112
			'Expires'       => $expires
113
		) );
114
115
		return $response;
116
	}
117
118
	/**
119
	 * Provide a JSON-LD given the itemId.
120
	 *
121
	 * @param array $request {
122
	 *  The request array.
123
	 *
124
	 * @type string $item_id The entity item id.
125
	 * }
126
	 *
127
	 * @return WP_REST_Response
128
	 * @throws \Exception
129
	 */
130
	public function jsonld_using_item_id( $request ) {
131
132
		$item_id = 'http://' . $request['item_id'];
133
		$post    = $this->entity_uri_service->get_entity( $item_id );
134
135
		if ( ! is_a( $post, 'WP_Post' ) ) {
136
			return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
137
		}
138
139
		return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) );
140
	}
141
142 View Code Duplication
	public function jsonld_using_get_page_by_path( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
144
		$post_name = $request['post_name'];
145
		$post_type = $request['post_type'];
146
147
		global $wpdb;
148
149
		$sql = "
150
			SELECT ID
151
			FROM $wpdb->posts
152
			WHERE post_name = %s
153
			 AND post_type = %s
154
		";
155
156
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) );
157
158
		if ( is_null( $post_id ) ) {
159
			return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
160
		}
161
162
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
163
	}
164
165
	/**
166
	 * @param WP_REST_Request $request
167
	 *
168
	 * @return WP_REST_Response
169
	 * @throws \Exception
170
	 */
171 View Code Duplication
	public function jsonld_using_post_meta( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
173
		$meta_key   = $request['meta_key'];
174
		$meta_value = current( $request->get_query_params( 'meta_value' ) );
175
176
		global $wpdb;
177
178
		$sql = "
179
			SELECT post_id AS ID
180
			FROM $wpdb->postmeta
181
			WHERE meta_key = %s
182
			 AND meta_value = %s
183
			LIMIT 1
184
		";
185
186
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) );
187
188
		if ( is_null( $post_id ) ) {
189
			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' ) );
190
		}
191
192
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
193
	}
194
195
}
196