Completed
Push — develop ( a97e0b...f2bea8 )
by David
02:55 queued 11s
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 Wordlift_Jsonld_Service;
12
use WP_REST_Request;
13
use WP_REST_Response;
14
use WP_REST_Server;
15
16
/**
17
 * Class Jsonld_Endpoint
18
 *
19
 * @package Wordlift\Jsonld
20
 */
21
class Jsonld_Endpoint {
22
23
	/**
24
	 * The {@link Wordlift_Jsonld_Service} instance.
25
	 *
26
	 * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance.
27
	 */
28
	private $jsonld_service;
29
	/**
30
	 * @var \Wordlift_Entity_Uri_Service
31
	 */
32
	private $entity_uri_service;
33
34
	/**
35
	 * Jsonld_Endpoint constructor.
36
	 *
37
	 * @param \Wordlift_Jsonld_Service $jsonld_service
38
	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service
39
	 */
40
	public function __construct( $jsonld_service, $entity_uri_service ) {
41
42
		$this->jsonld_service     = $jsonld_service;
43
		$this->entity_uri_service = $entity_uri_service;
44
45
		// PHP 5.3 compatibility.
46
		$that = $this;
47
		add_action( 'rest_api_init', function () use ( $that ) {
48
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array(
49
				'methods'  => WP_REST_Server::READABLE,
50
				'callback' => array( $that, 'jsonld_using_post_id' ),
51
				'args'     => array(
52
					'id' => array(
53
						'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...
54
							return is_numeric( $param );
55
						},
56
						'sanitize_callback' => 'absint',
57
					),
58
				)
59
			) );
60
61
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array(
62
				'methods'  => 'GET',
63
				'callback' => array( $that, 'jsonld_using_item_id' ),
64
			) );
65
66
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array(
67
				'methods'  => 'GET',
68
				'callback' => array( $that, 'jsonld_using_post_meta' ),
69
			) );
70
71
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array(
72
				'methods'  => 'GET',
73
				'callback' => array( $that, 'jsonld_using_get_page_by_path' ),
74
			) );
75
76
		} );
77
78
	}
79
80
	/**
81
	 * Callback for the JSON-LD request.
82
	 *
83
	 * @param array $request {
84
	 *  The request array.
85
	 *
86
	 * @type int $id The post id.
87
	 * }
88
	 *
89
	 * @return WP_REST_Response
90
	 * @throws \Exception
91
	 */
92
	public function jsonld_using_post_id( $request ) {
93
94
		$post_id     = $request['id'];
95
		$is_homepage = ( 0 === $post_id );
96
97
		// Send the generated JSON-LD.
98
		$data = $this->jsonld_service->get_jsonld( $is_homepage, $post_id );
99
100
		return Jsonld_Response_Helper::to_response( $data );
101
	}
102
103
	/**
104
	 * Provide a JSON-LD given the itemId.
105
	 *
106
	 * @param array $request {
107
	 *  The request array.
108
	 *
109
	 * @type string $item_id The entity item id.
110
	 * }
111
	 *
112
	 * @return WP_REST_Response
113
	 * @throws \Exception
114
	 */
115
	public function jsonld_using_item_id( $request ) {
116
117
		$item_id = 'http://' . $request['item_id'];
118
		$post    = $this->entity_uri_service->get_entity( $item_id );
119
120
		if ( ! is_a( $post, 'WP_Post' ) ) {
121
			return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
122
		}
123
124
		return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) );
125
	}
126
127 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...
128
129
		$post_name = $request['post_name'];
130
		$post_type = $request['post_type'];
131
132
		global $wpdb;
133
134
		$sql = "
135
			SELECT ID
136
			FROM $wpdb->posts
137
			WHERE post_name = %s
138
			 AND post_type = %s
139
		";
140
141
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) );
142
143
		if ( is_null( $post_id ) ) {
144
			return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
145
		}
146
147
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
148
	}
149
150
	/**
151
	 * @param WP_REST_Request $request
152
	 *
153
	 * @return WP_REST_Response
154
	 * @throws \Exception
155
	 */
156 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...
157
158
		$meta_key   = $request['meta_key'];
159
		$meta_value = current( $request->get_query_params( 'meta_value' ) );
160
161
		global $wpdb;
162
163
		$sql = "
164
			SELECT post_id AS ID
165
			FROM $wpdb->postmeta
166
			WHERE meta_key = %s
167
			 AND meta_value = %s
168
			LIMIT 1
169
		";
170
171
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) );
172
173
		if ( is_null( $post_id ) ) {
174
			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' ) );
175
		}
176
177
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
178
	}
179
180
}
181