Completed
Push — prepare/4.1 ( 0b4dc2...26fe0a )
by Jeremy
279:11 queued 269:16
created

WPCOM_JSON_API_Get_Post_Endpoint::callback()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 16

Duplication

Lines 3
Ratio 10.71 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 16
c 1
b 0
f 1
nc 7
nop 3
dl 3
loc 28
rs 6.7272
1
<?php
2
class WPCOM_JSON_API_Get_Post_Endpoint extends WPCOM_JSON_API_Post_Endpoint {
3
	// /sites/%s/posts/%d      -> $blog_id, $post_id
4
	// /sites/%s/posts/name:%s -> $blog_id, $post_id // not documented
5
	// /sites/%s/posts/slug:%s -> $blog_id, $post_id
6
	function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
7
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
8
		if ( is_wp_error( $blog_id ) ) {
9
			return $blog_id;
10
		}
11
12
		$args = $this->query_args();
13
14
		if ( false === strpos( $path, '/posts/slug:' ) && false === strpos( $path, '/posts/name:' ) ) {
15
			$get_by = 'ID';
16
		} else {
17
			$get_by = 'name';
18
		}
19
20
		$return = $this->get_post_by( $get_by, $post_id, $args['context'] );
21
		if ( !$return || is_wp_error( $return ) ) {
22
			return $return;
23
		}
24
25 View Code Duplication
		if ( ! $this->current_user_can_access_post_type( $return['type'], $args['context'] ) ) {
26
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
27
		}
28
29
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
30
		do_action( 'wpcom_json_api_objects', 'posts' );
31
32
		return $return;
33
	}
34
}
35