Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 93 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_Fetch_Post | |
0.00% |
0 / 93 |
|
0.00% |
0 / 5 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| register_rest_route | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
2 | |||
| get_blog_post_articles | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| get_post | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| get_post_alternate_data | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WP_REST_Help_Center_Fetch_Post file. |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | namespace A8C\FSE; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use Automattic\Jetpack\Jetpack_Mu_Wpcom\Common; |
| 12 | |
| 13 | /** |
| 14 | * Class WP_REST_Help_Center_Fetch_Post. |
| 15 | */ |
| 16 | class WP_REST_Help_Center_Fetch_Post extends \WP_REST_Controller { |
| 17 | /** |
| 18 | * WP_REST_Help_Center_Fetch_Post constructor. |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | $this->namespace = 'help-center'; |
| 22 | $this->rest_base = 'fetch-post'; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Register available routes. |
| 27 | */ |
| 28 | public function register_rest_route() { |
| 29 | register_rest_route( |
| 30 | $this->namespace, |
| 31 | '/' . $this->rest_base, |
| 32 | array( |
| 33 | 'methods' => \WP_REST_Server::READABLE, |
| 34 | 'callback' => array( $this, 'get_post' ), |
| 35 | 'permission_callback' => 'is_user_logged_in', |
| 36 | 'args' => array( |
| 37 | 'blog_id' => array( |
| 38 | 'type' => 'number', |
| 39 | ), |
| 40 | 'post_id' => array( |
| 41 | 'type' => 'number', |
| 42 | ), |
| 43 | 'post_url' => array( |
| 44 | 'type' => 'string', |
| 45 | ), |
| 46 | ), |
| 47 | ) |
| 48 | ); |
| 49 | |
| 50 | register_rest_route( |
| 51 | $this->namespace, |
| 52 | '/articles', |
| 53 | array( |
| 54 | 'methods' => \WP_REST_Server::READABLE, |
| 55 | 'callback' => array( $this, 'get_blog_post_articles' ), |
| 56 | 'permission_callback' => 'is_user_logged_in', |
| 57 | 'args' => array( |
| 58 | 'blog_id' => array( |
| 59 | 'type' => 'number', |
| 60 | 'required' => true, |
| 61 | ), |
| 62 | 'post_ids' => array( |
| 63 | 'type' => 'array', |
| 64 | 'required' => true, |
| 65 | 'items' => array( |
| 66 | 'type' => 'string', |
| 67 | ), |
| 68 | ), |
| 69 | ), |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Should return blog post articles. |
| 76 | * |
| 77 | * @param \WP_REST_Request $request The request sent to the API. |
| 78 | */ |
| 79 | public function get_blog_post_articles( \WP_REST_Request $request ) { |
| 80 | $query_parameters = array( |
| 81 | 'blog_id' => $request['blog_id'], |
| 82 | 'post_ids' => $request['post_ids'], |
| 83 | ); |
| 84 | $body = Client::wpcom_json_api_request_as_user( |
| 85 | '/help/articles?' . http_build_query( $query_parameters ) |
| 86 | ); |
| 87 | |
| 88 | if ( is_wp_error( $body ) ) { |
| 89 | return $body; |
| 90 | } |
| 91 | |
| 92 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 93 | |
| 94 | return rest_ensure_response( $response ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Should return the post. |
| 99 | * |
| 100 | * @param \WP_REST_Request $request The request sent to the API. |
| 101 | */ |
| 102 | public function get_post( \WP_REST_Request $request ) { |
| 103 | if ( isset( $request['post_url'] ) ) { |
| 104 | $body = Client::wpcom_json_api_request_as_user( |
| 105 | '/help/article?post_url=' . $request['post_url'] |
| 106 | ); |
| 107 | } else { |
| 108 | $alternate_data = $this->get_post_alternate_data( $request['blog_id'], $request['post_id'] ); |
| 109 | if ( is_wp_error( $alternate_data ) ) { |
| 110 | return $alternate_data; |
| 111 | } |
| 112 | |
| 113 | $body = Client::wpcom_json_api_request_as_user( |
| 114 | '/help/article/' . $alternate_data['blog_id'] . '/' . $alternate_data['post_id'] |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | if ( is_wp_error( $body ) ) { |
| 119 | return $body; |
| 120 | } |
| 121 | |
| 122 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 123 | |
| 124 | return rest_ensure_response( $response ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the alternate data of the post according to the locale. |
| 129 | * |
| 130 | * @param int $blog_id The blog ID. |
| 131 | * @param int $post_id The post ID. |
| 132 | * |
| 133 | * @return array The alternate data. |
| 134 | */ |
| 135 | public function get_post_alternate_data( $blog_id, $post_id ) { |
| 136 | $locale = Common\determine_iso_639_locale(); |
| 137 | $default_alternate_data = array( |
| 138 | 'post_id' => $post_id, |
| 139 | 'blog_id' => $blog_id, |
| 140 | ); |
| 141 | if ( $locale === 'en' ) { |
| 142 | return $default_alternate_data; |
| 143 | } |
| 144 | |
| 145 | $body = Client::wpcom_json_api_request_as_user( |
| 146 | "/support/alternates/$blog_id/posts/$post_id", |
| 147 | '1.1', |
| 148 | array(), |
| 149 | null, |
| 150 | 'rest' |
| 151 | ); |
| 152 | |
| 153 | if ( is_wp_error( $body ) ) { |
| 154 | return $body; |
| 155 | } |
| 156 | |
| 157 | $response = json_decode( wp_remote_retrieve_body( $body ), true ); |
| 158 | if ( ! array_key_exists( $locale, $response ) ) { |
| 159 | return $default_alternate_data; |
| 160 | } |
| 161 | |
| 162 | $alternate_data = $response[ $locale ]; |
| 163 | return array( |
| 164 | 'blog_id' => $alternate_data['blog_id'], |
| 165 | 'post_id' => $alternate_data['page_id'], |
| 166 | ); |
| 167 | } |
| 168 | } |