Controller.php
4 years ago
Endpoint.php
4 years ago
PostsEndpoint.php
1 year ago
TaxonomiesEndpoint.php
4 years ago
ThemesEndpoint.php
4 years ago
ThumbnailsEndpoint.php
4 years ago
ViewLoggerEndpoint.php
8 months ago
WidgetEndpoint.php
1 year ago
PostsEndpoint.php
220 lines
| 1 | <?php |
| 2 | namespace WordPressPopularPosts\Rest; |
| 3 | |
| 4 | use WordPressPopularPosts\Translate; |
| 5 | use WordPressPopularPosts\Traits\QueriesPosts; |
| 6 | |
| 7 | class PostsEndpoint extends Endpoint { |
| 8 | |
| 9 | use QueriesPosts; |
| 10 | |
| 11 | /** |
| 12 | * Initializes class. |
| 13 | * |
| 14 | * @param array |
| 15 | * @param \WordPressPopularPosts\Translate |
| 16 | * @param \WordPressPopularPosts\Output |
| 17 | */ |
| 18 | public function __construct(array $config, Translate $translate) |
| 19 | { |
| 20 | $this->config = $config; |
| 21 | $this->translate = $translate; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Registers the endpoint(s). |
| 26 | * |
| 27 | * @since 5.3.0 |
| 28 | */ |
| 29 | public function register() |
| 30 | { |
| 31 | $version = '1'; |
| 32 | $namespace = 'wordpress-popular-posts/v' . $version; |
| 33 | |
| 34 | register_rest_route($namespace, '/popular-posts', [ |
| 35 | [ |
| 36 | 'methods' => \WP_REST_Server::READABLE, |
| 37 | 'callback' => [$this, 'get_items'], |
| 38 | 'permission_callback' => '__return_true', |
| 39 | 'args' => $this->get_collection_params() |
| 40 | ] |
| 41 | ]); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Gets popular posts. |
| 46 | * |
| 47 | * @since 5.3.0 |
| 48 | * @param \WP_REST_Request $request Full data about the request. |
| 49 | * @return \WP_REST_Response |
| 50 | */ |
| 51 | public function get_items($request) |
| 52 | { |
| 53 | $params = $request->get_params(); |
| 54 | $lang = isset($params['lang']) ? $params['lang'] : null; |
| 55 | $popular_posts = []; |
| 56 | |
| 57 | // Multilang support |
| 58 | $this->set_lang($lang); |
| 59 | |
| 60 | $query = $this->maybe_query($params); |
| 61 | $results = $query->get_posts(); |
| 62 | |
| 63 | if ( is_array($results) && ! empty($results) ) { |
| 64 | foreach( $results as $popular_post ) { |
| 65 | $popular_posts[] = $this->prepare_item($popular_post, $request); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return new \WP_REST_Response($popular_posts, 200); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Retrieves the popular post's WP_Post object and formats it for the REST response. |
| 74 | * |
| 75 | * @since 4.1.0 |
| 76 | * |
| 77 | * @param object $popular_post The popular post object. |
| 78 | * @param \WP_REST_Request $request Full details about the request. |
| 79 | * @return array|mixed The formatted WP_Post object. |
| 80 | */ |
| 81 | private function prepare_item($popular_post, $request) |
| 82 | { |
| 83 | if ( $request->get_param('lang') ) { |
| 84 | $post_ID = $this->translate->get_object_id( |
| 85 | $popular_post->id, |
| 86 | get_post_type($popular_post->id) |
| 87 | ); |
| 88 | } else { |
| 89 | $post_ID = $popular_post->id; |
| 90 | } |
| 91 | |
| 92 | $wp_post = get_post($post_ID); |
| 93 | |
| 94 | // Borrow prepare_item_for_response method from WP_REST_Posts_Controller. |
| 95 | $posts_controller = new \WP_REST_Posts_Controller($wp_post->post_type, $request); |
| 96 | $data = $posts_controller->prepare_item_for_response($wp_post, $request); |
| 97 | |
| 98 | // Add pageviews from popular_post object to response. |
| 99 | $data->data['pageviews'] = $popular_post->pageviews; |
| 100 | |
| 101 | return $this->prepare_response_for_collection($data); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Retrieves the query params for the collections. |
| 106 | * |
| 107 | * @since 4.1.0 |
| 108 | * |
| 109 | * @return array Query parameters for the collection. |
| 110 | */ |
| 111 | public function get_collection_params() |
| 112 | { |
| 113 | return [ |
| 114 | 'post_type' => [ |
| 115 | 'description' => __('Return popular posts from specified custom post type(s).'), |
| 116 | 'type' => 'string', |
| 117 | 'default' => 'post', |
| 118 | 'sanitize_callback' => 'sanitize_text_field', |
| 119 | 'validate_callback' => 'rest_validate_request_arg', |
| 120 | ], |
| 121 | 'limit' => [ |
| 122 | 'description' => __('The maximum number of popular posts to return.'), |
| 123 | 'type' => 'integer', |
| 124 | 'default' => 10, |
| 125 | 'sanitize_callback' => 'absint', |
| 126 | 'validate_callback' => 'rest_validate_request_arg', |
| 127 | 'minimum' => 1, |
| 128 | ], |
| 129 | 'freshness' => [ |
| 130 | 'description' => __('Retrieve the most popular entries published within the specified time range.'), |
| 131 | 'type' => 'string', |
| 132 | 'enum' => ['0', '1'], |
| 133 | 'default' => '0', |
| 134 | 'sanitize_callback' => 'sanitize_text_field', |
| 135 | 'validate_callback' => 'rest_validate_request_arg', |
| 136 | ], |
| 137 | 'offset' => [ |
| 138 | 'description' => __('An offset point for the collection.'), |
| 139 | 'type' => 'integer', |
| 140 | 'default' => 0, |
| 141 | 'minimum' => 0, |
| 142 | 'sanitize_callback' => 'absint', |
| 143 | 'validate_callback' => 'rest_validate_request_arg', |
| 144 | ], |
| 145 | 'order_by' => [ |
| 146 | 'description' => __('Set the sorting option of the popular posts.'), |
| 147 | 'type' => 'string', |
| 148 | 'enum' => ['views', 'comments'], |
| 149 | 'default' => 'views', |
| 150 | 'sanitize_callback' => 'sanitize_text_field', |
| 151 | 'validate_callback' => 'rest_validate_request_arg', |
| 152 | ], |
| 153 | 'range' => [ |
| 154 | 'description' => __('Return popular posts from a specified time range.'), |
| 155 | 'type' => 'string', |
| 156 | 'enum' => ['last24hours', 'last7days', 'last30days', 'all', 'custom'], |
| 157 | 'default' => 'last24hours', |
| 158 | 'sanitize_callback' => 'sanitize_text_field', |
| 159 | 'validate_callback' => 'rest_validate_request_arg', |
| 160 | ], |
| 161 | 'time_unit' => [ |
| 162 | 'description' => __('Specifies the time unit of the custom time range.'), |
| 163 | 'type' => 'string', |
| 164 | 'enum' => ['minute', 'hour', 'day', 'week', 'month'], |
| 165 | 'default' => 'hour', |
| 166 | 'sanitize_callback' => 'sanitize_text_field', |
| 167 | 'validate_callback' => 'rest_validate_request_arg', |
| 168 | ], |
| 169 | 'time_quantity' => [ |
| 170 | 'description' => __('Specifies the number of time units of the custom time range.'), |
| 171 | 'type' => 'integer', |
| 172 | 'default' => 24, |
| 173 | 'minimum' => 1, |
| 174 | 'sanitize_callback' => 'absint', |
| 175 | 'validate_callback' => 'rest_validate_request_arg', |
| 176 | ], |
| 177 | 'pid' => [ |
| 178 | 'description' => __('Post IDs to exclude from the listing.'), |
| 179 | 'type' => 'string', |
| 180 | 'sanitize_callback' => function($pid) { |
| 181 | return rtrim(preg_replace('|[^0-9,]|', '', $pid), ','); |
| 182 | }, |
| 183 | 'validate_callback' => 'rest_validate_request_arg', |
| 184 | ], |
| 185 | 'exclude' => [ |
| 186 | 'description' => __('Post IDs to exclude from the listing.'), |
| 187 | 'type' => 'string', |
| 188 | 'sanitize_callback' => function($exclude) { |
| 189 | return rtrim(preg_replace('|[^0-9,]|', '', $exclude), ','); |
| 190 | }, |
| 191 | 'validate_callback' => 'rest_validate_request_arg', |
| 192 | ], |
| 193 | 'taxonomy' => [ |
| 194 | 'description' => __('Include posts in a specified taxonomy.'), |
| 195 | 'type' => 'string', |
| 196 | 'sanitize_callback' => function($taxonomy) { |
| 197 | return empty($taxonomy) ? 'category' : $taxonomy; |
| 198 | }, |
| 199 | 'validate_callback' => 'rest_validate_request_arg', |
| 200 | ], |
| 201 | 'term_id' => [ |
| 202 | 'description' => __('Taxonomy IDs, separated by comma (prefix a minus sign to exclude).'), |
| 203 | 'type' => 'string', |
| 204 | 'sanitize_callback' => function($term_id) { |
| 205 | return rtrim(preg_replace('|[^0-9,;-]|', '', $term_id), ','); |
| 206 | }, |
| 207 | 'validate_callback' => 'rest_validate_request_arg', |
| 208 | ], |
| 209 | 'author' => [ |
| 210 | 'description' => __('Include popular posts from author ID(s).'), |
| 211 | 'type' => 'string', |
| 212 | 'sanitize_callback' => function($author) { |
| 213 | return rtrim(preg_replace('|[^0-9,]|', '', $author), ','); |
| 214 | }, |
| 215 | 'validate_callback' => 'rest_validate_request_arg', |
| 216 | ], |
| 217 | ]; |
| 218 | } |
| 219 | } |
| 220 |