Completed
Push — develop ( 844154...ac08d5 )
by David
02:57 queued 11s
created

Jsonld_Endpoint::jsonld_using_meta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 37
rs 9.328
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 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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $jsonld_service of type object<Wordlift\Jsonld\Jsonld_Service> is incompatible with the declared type object<Wordlift_Jsonld_Service> of property $jsonld_service.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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/meta/(?P<meta_key>[^/]+)', array(
72
				'methods'  => 'GET',
73
				'callback' => array( $that, 'jsonld_using_meta' ),
74
			) );
75
76
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array(
77
				'methods'  => 'GET',
78
				'callback' => array( $that, 'jsonld_using_get_page_by_path' ),
79
			) );
80
81
		} );
82
83
	}
84
85
	/**
86
	 * Callback for the JSON-LD request.
87
	 *
88
	 * @param array $request {
89
	 *  The request array.
90
	 *
91
	 * @type int $id The post id.
92
	 * }
93
	 *
94
	 * @return WP_REST_Response
95
	 * @throws \Exception
96
	 */
97
	public function jsonld_using_post_id( $request ) {
98
99
		$post_id = $request['id'];
100
		$type    = ( 0 === $post_id ) ? Jsonld_Service::TYPE_HOMEPAGE : Jsonld_Service::TYPE_POST;
101
102
		// Send the generated JSON-LD.
103
		$data = $this->jsonld_service->get( $type, $post_id );
0 ignored issues
show
Unused Code introduced by
The call to Wordlift_Jsonld_Service::get() has too many arguments starting with $type.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
Are you sure the assignment to $data is correct as $this->jsonld_service->get($type, $post_id) (which targets Wordlift_Jsonld_Service::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
104
105
		return Jsonld_Response_Helper::to_response( $data );
106
	}
107
108
	/**
109
	 * Provide a JSON-LD given the itemId.
110
	 *
111
	 * @param array $request {
112
	 *  The request array.
113
	 *
114
	 * @type string $item_id The entity item id.
115
	 * }
116
	 *
117
	 * @return WP_REST_Response
118
	 * @throws \Exception
119
	 */
120
	public function jsonld_using_item_id( $request ) {
121
122
		$item_id = 'http://' . $request['item_id'];
123
		$post    = $this->entity_uri_service->get_entity( $item_id );
124
125
		if ( ! is_a( $post, 'WP_Post' ) ) {
126
			return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
127
		}
128
129
		return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) );
130
	}
131
132
	public function jsonld_using_get_page_by_path( $request ) {
133
134
		$post_name = $request['post_name'];
135
		$post_type = $request['post_type'];
136
137
		global $wpdb;
138
139
		$sql = "
140
			SELECT ID
141
			FROM $wpdb->posts
142
			WHERE post_name = %s
143
			 AND post_type = %s
144
		";
145
146
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) );
147
148
		if ( is_null( $post_id ) ) {
149
			return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
150
		}
151
152
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
153
	}
154
155
	/**
156
	 * @param WP_REST_Request $request
157
	 *
158
	 * @return WP_REST_Response
159
	 * @throws \Exception
160
	 */
161
	public function jsonld_using_post_meta( $request ) {
162
163
		$meta_key   = $request['meta_key'];
164
		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
165
166
		global $wpdb;
167
168
		$sql = "
169
			SELECT post_id AS ID
170
			FROM $wpdb->postmeta
171
			WHERE meta_key = %s
172
			 AND meta_value = %s
173
			LIMIT 1
174
		";
175
176
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) );
177
178
		if ( is_null( $post_id ) ) {
179
			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' ) );
180
		}
181
182
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
183
	}
184
185
	public function jsonld_using_meta( $request ) {
186
187
		global $wpdb;
188
189
		$meta_key   = $request['meta_key'];
190
		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
191
192
		$results = $wpdb->get_results( $wpdb->prepare(
193
			"
194
			SELECT pm.post_id AS id, %s AS type
195
			 FROM {$wpdb->postmeta} pm
196
			 	INNER JOIN {$wpdb->posts} p
197
			 		ON p.ID = pm.post_id AND p.post_status = 'publish'
198
			 WHERE pm.meta_key = %s AND pm.meta_value = %s
199
			 UNION
200
			 SELECT term_id AS id, %s AS type
201
			 FROM {$wpdb->termmeta}
202
			 WHERE meta_key = %s AND meta_value = %s
203
			",
204
			Jsonld_Service::TYPE_POST,
205
			$meta_key,
206
			$meta_value,
207
			Jsonld_Service::TYPE_TERM,
208
			$meta_key,
209
			$meta_value
210
		) );
211
212
		$jsonld_service = $this->jsonld_service;
213
214
		$data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) {
215
			$jsonld = $jsonld_service->get( $result->type, $result->id );
0 ignored issues
show
Unused Code introduced by
The call to Wordlift_Jsonld_Service::get() has too many arguments starting with $result->type.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
Are you sure the assignment to $jsonld is correct as $jsonld_service->get($result->type, $result->id) (which targets Wordlift_Jsonld_Service::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
216
217
			return array_merge( $carry, $jsonld );
218
		}, array() );
219
220
		return Jsonld_Response_Helper::to_response( $data );
221
	}
222
223
}
224