Completed
Push — update/crm-integration-fixes ( a3fc2b )
by Jeremy
14:51 queued 06:26
created

WPCOM_JSON_API_Get_Post_Endpoint::callback()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 28

Duplication

Lines 3
Ratio 10.71 %

Importance

Changes 0
Metric Value
cc 7
nc 7
nop 3
dl 3
loc 28
rs 8.5386
c 0
b 0
f 0
1
<?php
2
3
new WPCOM_JSON_API_Get_Post_Endpoint( array(
4
	'description' => 'Get a single post (by ID).',
5
	'group'       => 'posts',
6
	'stat'        => 'posts:1',
7
	'new_version' => '1.1',
8
	'max_version' => '1',
9
	'method'      => 'GET',
10
	'path'        => '/sites/%s/posts/%d',
11
	'path_labels' => array(
12
		'$site'    => '(int|string) Site ID or domain',
13
		'$post_ID' => '(int) The post ID',
14
	),
15
16
	'example_request'  => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/7'
17
) );
18
19
new WPCOM_JSON_API_Get_Post_Endpoint( array(
20
	'description' => 'Get a single post (by name)',
21
	'group'       => '__do_not_document',
22
	'stat'        => 'posts:name',
23
	'method'      => 'GET',
24
	'path'        => '/sites/%s/posts/name:%s',
25
	'path_labels' => array(
26
		'$site'      => '(int|string) Site ID or domain',
27
		'$post_name' => '(string) The post name (a.k.a. slug)',
28
	),
29
30
	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/name:blogging-and-stuff',
31
) );
32
33
new WPCOM_JSON_API_Get_Post_Endpoint( array(
34
	'description' => 'Get a single post (by slug).',
35
	'group'       => 'posts',
36
	'stat'        => 'posts:slug',
37
	'new_version' => '1.1',
38
	'max_version' => '1',
39
	'method'      => 'GET',
40
	'path'        => '/sites/%s/posts/slug:%s',
41
	'path_labels' => array(
42
		'$site'      => '(int|string) Site ID or domain',
43
		'$post_slug' => '(string) The post slug (a.k.a. sanitized name)',
44
	),
45
46
	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/slug:blogging-and-stuff',
47
) );
48
49
class WPCOM_JSON_API_Get_Post_Endpoint extends WPCOM_JSON_API_Post_Endpoint {
50
	// /sites/%s/posts/%d      -> $blog_id, $post_id
51
	// /sites/%s/posts/name:%s -> $blog_id, $post_id // not documented
52
	// /sites/%s/posts/slug:%s -> $blog_id, $post_id
53
	function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
54
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
55
		if ( is_wp_error( $blog_id ) ) {
56
			return $blog_id;
57
		}
58
59
		$args = $this->query_args();
60
61
		if ( false === strpos( $path, '/posts/slug:' ) && false === strpos( $path, '/posts/name:' ) ) {
62
			$get_by = 'ID';
63
		} else {
64
			$get_by = 'name';
65
		}
66
67
		$return = $this->get_post_by( $get_by, $post_id, $args['context'] );
68
		if ( !$return || is_wp_error( $return ) ) {
69
			return $return;
70
		}
71
72 View Code Duplication
		if ( ! $this->current_user_can_access_post_type( $return['type'], $args['context'] ) ) {
73
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'unknown_post'.

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...
74
		}
75
76
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
77
		do_action( 'wpcom_json_api_objects', 'posts' );
78
79
		return $return;
80
	}
81
}
82