| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /** |
| 3 | 4 | * Setup the Posts Block Rest Calls |
| 4 | 5 | * |
| 5 | 6 | * @since 2.0.0 |
| @@ -6,461 +7,551 @@ | ||
| 6 | 7 | * @package AFT Blocks |
| 7 | 8 | */ |
| 8 | 9 | |
| 9 | 10 | |
| 11 | + | |
| 10 | 12 | /** |
| 11 | 13 | * Class to setup new rest calls. |
| 12 | 14 | */ |
| 13 | -if(!class_exists('Aft_Post_Rest_Controller')){ | |
| 14 | - class Aft_Post_Rest_Controller extends WP_REST_Controller { | |
| 15 | - /** | |
| 16 | - * Type property name. | |
| 17 | - */ | |
| 18 | - const PROP_TYPE = 'type'; | |
| 15 | +if (!class_exists('Aft_Post_Rest_Controller')) { | |
| 16 | + class Aft_Post_Rest_Controller extends WP_REST_Controller | |
| 17 | + { | |
| 19 | 18 | |
| 20 | - /** | |
| 21 | - * Type property name. | |
| 22 | - */ | |
| 23 | - const PROP_TYPE_ARRAY = 'type_array'; | |
| 19 | + /** | |
| 20 | + * Type property name. | |
| 21 | + */ | |
| 22 | + const PROP_TYPE = 'type'; | |
| 24 | 23 | |
| 25 | - /** | |
| 26 | - * Query property name. | |
| 27 | - */ | |
| 28 | - const PROP_QUERY = 'query'; | |
| 29 | - /** | |
| 30 | - * Query property name. | |
| 31 | - */ | |
| 32 | - const PROP_MULTIPLE = 'multiple'; | |
| 24 | + /** | |
| 25 | + * Type property name. | |
| 26 | + */ | |
| 27 | + const PROP_TYPE_ARRAY = 'type_array'; | |
| 33 | 28 | |
| 34 | - /** | |
| 35 | - * Query property name. | |
| 36 | - */ | |
| 37 | - const PROP_ORDER_BY = 'order_by'; | |
| 29 | + /** | |
| 30 | + * Query property name. | |
| 31 | + */ | |
| 32 | + const PROP_QUERY = 'query'; | |
| 33 | + /** | |
| 34 | + * Query property name. | |
| 35 | + */ | |
| 36 | + const PROP_MULTIPLE = 'multiple'; | |
| 38 | 37 | |
| 39 | - /** | |
| 40 | - * Query property name. | |
| 41 | - */ | |
| 42 | - const PROP_ORDER = 'order'; | |
| 38 | + /** | |
| 39 | + * Query property name. | |
| 40 | + */ | |
| 41 | + const PROP_ORDER_BY = 'order_by'; | |
| 43 | 42 | |
| 44 | - /** | |
| 45 | - * Query property name. | |
| 46 | - */ | |
| 47 | - const PROP_ALLOW_STICKY = 'allow_sticky'; | |
| 43 | + /** | |
| 44 | + * Query property name. | |
| 45 | + */ | |
| 46 | + const PROP_ORDER = 'order'; | |
| 48 | 47 | |
| 49 | - /** | |
| 50 | - * Query property name. | |
| 51 | - */ | |
| 52 | - const PROP_OFFSET = 'offset'; | |
| 48 | + /** | |
| 49 | + * Query property name. | |
| 50 | + */ | |
| 51 | + const PROP_ALLOW_STICKY = 'allow_sticky'; | |
| 53 | 52 | |
| 54 | - /** | |
| 55 | - * Query property name. | |
| 56 | - */ | |
| 57 | - const PROP_TAX = 'tax'; | |
| 53 | + /** | |
| 54 | + * Query property name. | |
| 55 | + */ | |
| 56 | + const PROP_OFFSET = 'offset'; | |
| 58 | 57 | |
| 59 | - /** | |
| 60 | - * Query property name. | |
| 61 | - */ | |
| 62 | - const PROP_EXCLUDE = 'exclude'; | |
| 58 | + /** | |
| 59 | + * Query property name. | |
| 60 | + */ | |
| 61 | + const PROP_TAX = 'tax'; | |
| 63 | 62 | |
| 64 | - /** | |
| 65 | - * Query property name. | |
| 66 | - */ | |
| 67 | - const PROP_CUSTOM_TAX = 'custom_tax'; | |
| 68 | - /** | |
| 69 | - * Query property name. | |
| 70 | - */ | |
| 71 | - const PROP_TAGS = 'tags'; | |
| 72 | - /** | |
| 73 | - * Query property name. | |
| 74 | - */ | |
| 75 | - const PROP_CATEGORY = 'category'; | |
| 63 | + /** | |
| 64 | + * Query property name. | |
| 65 | + */ | |
| 66 | + const PROP_EXCLUDE = 'exclude'; | |
| 76 | 67 | |
| 77 | - /** | |
| 78 | - * Query property name. | |
| 79 | - */ | |
| 80 | - const PROP_TAX_TYPE = 'tax_type'; | |
| 68 | + /** | |
| 69 | + * Query property name. | |
| 70 | + */ | |
| 71 | + const PROP_CUSTOM_TAX = 'custom_tax'; | |
| 72 | + /** | |
| 73 | + * Query property name. | |
| 74 | + */ | |
| 75 | + const PROP_TAGS = 'tags'; | |
| 76 | + /** | |
| 77 | + * Query property name. | |
| 78 | + */ | |
| 79 | + const PROP_CATEGORY = 'category'; | |
| 81 | 80 | |
| 82 | - /** | |
| 83 | - * Search property name. | |
| 84 | - */ | |
| 85 | - const PROP_SEARCH = 'search'; | |
| 81 | + /** | |
| 82 | + * Query property name. | |
| 83 | + */ | |
| 84 | + const PROP_TAX_TYPE = 'tax_type'; | |
| 86 | 85 | |
| 87 | - /** | |
| 88 | - * Include property name. | |
| 89 | - */ | |
| 90 | - const PROP_INCLUDE = 'include'; | |
| 86 | + /** | |
| 87 | + * Search property name. | |
| 88 | + */ | |
| 89 | + const PROP_SEARCH = 'search'; | |
| 91 | 90 | |
| 92 | - /** | |
| 93 | - * Per page property name. | |
| 94 | - */ | |
| 95 | - const PROP_PER_PAGE = 'per_page'; | |
| 91 | + /** | |
| 92 | + * Include property name. | |
| 93 | + */ | |
| 94 | + const PROP_INCLUDE = 'include'; | |
| 96 | 95 | |
| 97 | - /** | |
| 98 | - * Per page property name. | |
| 99 | - */ | |
| 100 | - const PROP_POST_ID = 'post_id'; | |
| 96 | + /** | |
| 97 | + * Per page property name. | |
| 98 | + */ | |
| 99 | + const PROP_PER_PAGE = 'per_page'; | |
| 101 | 100 | |
| 102 | - /** | |
| 103 | - * Page property name. | |
| 104 | - */ | |
| 105 | - const PROP_PAGE = 'page'; | |
| 101 | + /** | |
| 102 | + * Per page property name. | |
| 103 | + */ | |
| 104 | + const PROP_POST_ID = 'post_id'; | |
| 106 | 105 | |
| 107 | - /** | |
| 108 | - * Constructor. | |
| 109 | - */ | |
| 110 | - public function __construct() { | |
| 111 | - $this->namespace = 'aft/v1'; | |
| 112 | - $this->query_base = 'post-query'; | |
| 113 | - } | |
| 106 | + /** | |
| 107 | + * Page property name. | |
| 108 | + */ | |
| 109 | + const PROP_PAGE = 'page'; | |
| 114 | 110 | |
| 115 | - /** | |
| 116 | - * Registers the routes for the objects of the controller. | |
| 117 | - * | |
| 118 | - * @see register_rest_route() | |
| 119 | - */ | |
| 120 | - public function register_routes() { | |
| 121 | - | |
| 122 | - register_rest_route( | |
| 123 | - $this->namespace, | |
| 124 | - '/' . $this->query_base, | |
| 125 | - array( | |
| 126 | - array( | |
| 127 | - 'methods' => WP_REST_Server::READABLE, | |
| 128 | - 'callback' => array( $this, 'get_query_items' ), | |
| 129 | - 'permission_callback' => array( $this, 'get_items_permission_check' ), | |
| 130 | - 'args' => $this->get_query_params(), | |
| 131 | - ), | |
| 132 | - ) | |
| 133 | - ); | |
| 134 | - } | |
| 135 | - public function get_items_permission_check( $request ) { | |
| 136 | - return current_user_can( 'edit_posts' ); | |
| 137 | - } | |
| 138 | - public function get_query_items( $request ) { | |
| 139 | - | |
| 140 | - $prop_type = $request->get_param( self::PROP_TYPE ); | |
| 141 | - $query_type = $request->get_param( self::PROP_QUERY ); | |
| 142 | - $tax_type = $request->get_param( self::PROP_TAX_TYPE ); | |
| 143 | - | |
| 144 | - $categories = ( $request->get_param( self::PROP_CATEGORY ) ? wp_parse_list( $request->get_param( self::PROP_CATEGORY ) ) : array() ); | |
| 145 | - $tags = ( $request->get_param( self::PROP_TAGS ) ? wp_parse_list( $request->get_param( self::PROP_TAGS ) ) : array() ); | |
| 146 | - if ( empty( $query_type ) ) { | |
| 147 | - return array(); | |
| 148 | - } | |
| 149 | 111 | |
| 150 | - $query_args = array( | |
| 151 | - 'post_type' => $prop_type, | |
| 152 | - ); | |
| 153 | - if ( 'individual' === $query_type ) { | |
| 154 | - $query_args['post__in'] = $request->get_param( self::PROP_INCLUDE ); | |
| 155 | - $query_args['orderby'] = 'post__in'; | |
| 156 | - $query_args['posts_per_page'] = -1; | |
| 157 | - $query_args['ignore_sticky_posts'] = 1; | |
| 158 | - } else { | |
| 159 | - $query_args['posts_per_page'] = $request->get_param( self::PROP_PER_PAGE ); | |
| 160 | - $query_args['tax_query'] = array(); | |
| 161 | - $query_args['orderby'] = $request->get_param( self::PROP_ORDER_BY ); | |
| 162 | - $query_args['order'] = $request->get_param( self::PROP_ORDER ); | |
| 163 | - $query_args['offset'] = $request->get_param( self::PROP_OFFSET ); | |
| 164 | - | |
| 165 | - $query_args['post_status'] = 'publish'; | |
| 166 | - $query_args['ignore_sticky_posts'] = $request->get_param( self::PROP_ALLOW_STICKY ); | |
| 167 | - $current_post_id = $request->get_param( self::PROP_POST_ID ); | |
| 168 | - if ( ! empty( $current_post_id ) ) { | |
| 169 | - $query_args['post__not_in'] = array( $current_post_id ); | |
| 170 | - } | |
| 171 | - if ( 'post' !== $prop_type || $request->get_param( self::PROP_CUSTOM_TAX ) ) { | |
| 172 | - if ( $tax_type ) { | |
| 173 | - if(!empty($categories)){ | |
| 174 | - $query_args['tax_query'][] = array( | |
| 175 | - 'taxonomy' => ( isset( $tax_type ) ) ? $tax_type : 'category', | |
| 176 | - 'field' => 'id', | |
| 177 | - 'terms' => (isset($categories))?$categories:'', | |
| 178 | - 'operator' => ( isset( $exclude ) && 'exclude' === $exclude ? 'NOT IN' : 'IN' ), | |
| 179 | - ); | |
| 180 | - } | |
| 181 | - } | |
| 182 | - } else { | |
| 183 | - $tags = ( $request->get_param( self::PROP_TAGS ) ? wp_parse_list( $request->get_param( self::PROP_TAGS ) ) : array() ); | |
| 184 | - | |
| 185 | - $query_args['category__in'] = $categories; | |
| 186 | - $query_args['tag__in'] = $tags; | |
| 187 | - | |
| 188 | - } | |
| 189 | - } | |
| 190 | - $query = new WP_Query( $query_args ); | |
| 191 | - $posts = array(); | |
| 112 | + /** | |
| 113 | + * Constructor. | |
| 114 | + */ | |
| 115 | + public function __construct() | |
| 116 | + { | |
| 117 | + $this->namespace = 'aft/v1'; | |
| 118 | + $this->query_base = 'post-query'; | |
| 119 | + } | |
| 192 | 120 | |
| 193 | - foreach ( $query->posts as $post ) { | |
| 194 | - $posts[] = $this->prepare_query_item_for_response( $post, $request,$tax_type ); | |
| 195 | - } | |
| 121 | + /** | |
| 122 | + * Registers the routes for the objects of the controller. | |
| 123 | + * | |
| 124 | + * @see register_rest_route() | |
| 125 | + */ | |
| 126 | + public function register_routes() | |
| 127 | + { | |
| 196 | 128 | |
| 197 | - return rest_ensure_response( $posts ); | |
| 198 | - } | |
| 129 | + register_rest_route( | |
| 130 | + $this->namespace, | |
| 131 | + '/' . $this->query_base, | |
| 132 | + array( | |
| 133 | + array( | |
| 134 | + 'methods' => WP_REST_Server::READABLE, | |
| 135 | + 'callback' => array($this, 'get_query_items'), | |
| 136 | + 'permission_callback' => array($this, 'get_items_permission_check'), | |
| 137 | + 'args' => $this->get_query_params(), | |
| 138 | + ), | |
| 139 | + ) | |
| 140 | + ); | |
| 141 | + } | |
| 142 | + public function get_items_permission_check($request) | |
| 143 | + { | |
| 144 | + return current_user_can('edit_posts'); | |
| 145 | + } | |
| 146 | + public function get_query_items($request) | |
| 147 | + { | |
| 199 | 148 | |
| 200 | - public function prepare_query_item_for_response( $post, $request,$tax_type ) { | |
| 201 | - $excerpt = $this->blockspare_api_excerpt($post,$length=15); | |
| 202 | - $new_excerpt = apply_filters( 'the_excerpt', $excerpt ); | |
| 203 | - $data = array( | |
| 204 | - 'id' => $post->ID, | |
| 205 | - 'date' => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ), | |
| 206 | - 'date_gmt' => $this->prepare_date_response( $post->post_date_gmt ), | |
| 207 | - 'modified' => $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ), | |
| 208 | - 'modified_gmt' => $this->prepare_date_response( $post->post_modified_gmt ), | |
| 209 | - 'title' => array( | |
| 210 | - 'raw' => $post->post_title, | |
| 211 | - 'rendered' => get_the_title( $post->ID ), | |
| 212 | - ), | |
| 213 | - 'excerpt' => array( | |
| 214 | - 'raw' => $post->post_excerpt, | |
| 215 | - 'rendered' => get_the_excerpt( $post->ID ), | |
| 216 | - ), | |
| 217 | - 'data_content'=>wp_kses_post($excerpt), | |
| 218 | - 'type' => $post->post_type, | |
| 219 | - 'slug' => $post->post_name, | |
| 220 | - 'status' => $post->post_status, | |
| 221 | - 'link' => get_permalink( $post->ID ), | |
| 222 | - 'author' => absint( $post->post_author ), | |
| 223 | - 'display_name'=> get_the_author_meta('display_name', $object['author']), | |
| 224 | - 'author_link'=>get_author_posts_url($object['author']), | |
| 225 | - 'featured_media' => get_post_thumbnail_id( $post->ID ), | |
| 226 | - ); | |
| 149 | + $prop_type = $request->get_param(self::PROP_TYPE); | |
| 150 | + $query_type = $request->get_param(self::PROP_QUERY); | |
| 151 | + $tax_type = $request->get_param(self::PROP_TAX_TYPE); | |
| 227 | 152 | |
| 228 | - $attachment_id = get_post_thumbnail_id( $post->ID ); | |
| 229 | - | |
| 230 | - $image= wp_get_attachment_image_src( | |
| 231 | - get_post_thumbnail_id( $post ), | |
| 232 | - 'full', | |
| 233 | - false | |
| 234 | - ); | |
| 235 | - $sizes = get_intermediate_image_sizes(); | |
| 236 | - | |
| 237 | - $imageSizes = array( | |
| 238 | - 'full' => is_array($image) ? $image : '', | |
| 239 | - ); | |
| 240 | - | |
| 241 | - foreach ($sizes as $size) { | |
| 242 | - $imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : ''; | |
| 243 | - } | |
| 244 | 153 | |
| 245 | - $data['featured_image_src_large'] = $imageSizes; | |
| 246 | - | |
| 247 | - //$author_data = array(); | |
| 248 | - if ( post_type_supports( $post->post_type, 'author' ) ) { | |
| 249 | - $blockspare = new BlocksapreMultiAuthorForBackend(); | |
| 250 | - $data['author_info'] = $blockspare->blockspare_by_author($post); | |
| 251 | - } | |
| 252 | - if ( 'post' != $post->post_type || 'comments' != $post->post_type || 'author' != $post->post_type ) { | |
| 253 | - $blockspare = new BlocksapreMultiAuthorForBackend(); | |
| 254 | - | |
| 255 | - $data['author_info'] = $blockspare->blockspare_by_author($post); | |
| 256 | - | |
| 257 | - } | |
| 258 | - //$data['author_info'] = $author_data; | |
| 259 | - if ( post_type_supports( $post->post_type, 'comments' ) ) { | |
| 260 | - $comments_count = wp_count_comments( $post->ID ); | |
| 261 | - $data['comment_count'] = $comments_count->total_comments; | |
| 262 | - } | |
| 263 | - if ( 'post' === $post->post_type ) { | |
| 264 | - $data['category_info'] = get_the_category( $post->ID ); | |
| 265 | - $data['tag_info'] = get_the_tags( $post->ID ); | |
| 266 | - } | |
| 267 | - $taxonomies = get_object_taxonomies( $post->post_type, 'objects' ); | |
| 268 | - $taxs = array(); | |
| 269 | - $term_items = array(); | |
| 270 | - foreach ( $taxonomies as $term_slug => $term ) { | |
| 271 | - if ( ! $term->public || ! $term->show_ui ) { | |
| 272 | - continue; | |
| 273 | - } | |
| 274 | - $terms = get_the_terms( $post->ID, $term_slug ); | |
| 275 | - | |
| 276 | - if ( ! empty( $terms ) ) { | |
| 277 | - foreach ( $terms as $term_key => $term_item ) { | |
| 278 | - $term_items[] = array( | |
| 279 | - 'value' => $term_item->term_id, | |
| 280 | - 'label' => $term_item->name, | |
| 281 | - ); | |
| 282 | - } | |
| 283 | - | |
| 284 | - } | |
| 285 | - } | |
| 286 | - | |
| 287 | - $data['taxonomy_info'] = json_encode($term_items); | |
| 288 | - | |
| 289 | - return $data; | |
| 290 | - } | |
| 154 | + $categories = ($request->get_param(self::PROP_CATEGORY) ? wp_parse_list($request->get_param(self::PROP_CATEGORY)) : array()); | |
| 155 | + $tags = ($request->get_param(self::PROP_TAGS) ? wp_parse_list($request->get_param(self::PROP_TAGS)) : array()); | |
| 156 | + if (empty($query_type)) { | |
| 157 | + return array(); | |
| 158 | + } | |
| 291 | 159 | |
| 292 | - protected function blockspare_api_excerpt($post,$length=''){ | |
| 293 | - | |
| 294 | - $excerpt = get_post_field( | |
| 295 | - 'post_excerpt', | |
| 296 | - $post->post_id, | |
| 297 | - 'display' | |
| 298 | - ); | |
| 299 | - | |
| 300 | - | |
| 301 | - if ( empty( $excerpt ) ) { | |
| 302 | - | |
| 303 | - $excerpt = preg_replace( | |
| 304 | - array( | |
| 305 | - '/\<figcaption>.*\<\/figcaption>/', | |
| 306 | - '/\[caption.*\[\/caption\]/', | |
| 307 | - ), | |
| 308 | - '', | |
| 309 | - $post->post_content | |
| 310 | - ); | |
| 311 | - } | |
| 312 | - | |
| 313 | - | |
| 314 | - // Trim the excerpt if necessary. | |
| 315 | - if ( isset( $length ) ) { | |
| 316 | - $excerpt = wp_trim_words( | |
| 317 | - $excerpt, | |
| 318 | - $length | |
| 319 | - ); | |
| 320 | - } | |
| 321 | - | |
| 322 | - return $excerpt; | |
| 323 | - } | |
| 160 | + $query_args = array( | |
| 161 | + 'post_type' => $prop_type, | |
| 162 | + ); | |
| 163 | + if ('individual' === $query_type) { | |
| 164 | + $query_args['post__in'] = $request->get_param(self::PROP_INCLUDE); | |
| 165 | + $query_args['orderby'] = 'post__in'; | |
| 166 | + $query_args['posts_per_page'] = -1; | |
| 167 | + $query_args['ignore_sticky_posts'] = 1; | |
| 168 | + } else { | |
| 169 | + $query_args['posts_per_page'] = $request->get_param(self::PROP_PER_PAGE); | |
| 170 | + $query_args['tax_query'] = array(); | |
| 171 | + $query_args['orderby'] = $request->get_param(self::PROP_ORDER_BY); | |
| 172 | + $query_args['order'] = $request->get_param(self::PROP_ORDER); | |
| 173 | + $query_args['offset'] = $request->get_param(self::PROP_OFFSET); | |
| 324 | 174 | |
| 325 | - protected function prepare_date_response( $date_gmt, $date = null ) { | |
| 326 | - // Use the date if passed. | |
| 327 | - if ( isset( $date ) ) { | |
| 328 | - return mysql2date( 'Y-m-d\TH:i:s', $date, false ); | |
| 329 | - } | |
| 175 | + $query_args['post_status'] = 'publish'; | |
| 176 | + $query_args['ignore_sticky_posts'] = $request->get_param(self::PROP_ALLOW_STICKY); | |
| 177 | + $current_post_id = $request->get_param(self::PROP_POST_ID); | |
| 178 | + if (! empty($current_post_id)) { | |
| 179 | + $query_args['post__not_in'] = array($current_post_id); | |
| 180 | + } | |
| 181 | + if ('post' !== $prop_type || $request->get_param(self::PROP_CUSTOM_TAX)) { | |
| 182 | + if ($tax_type) { | |
| 183 | + if (!empty($categories)) { | |
| 184 | + $query_args['tax_query'][] = array( | |
| 185 | + 'taxonomy' => (isset($tax_type)) ? $tax_type : 'category', | |
| 186 | + 'field' => 'id', | |
| 187 | + 'terms' => (isset($categories)) ? $categories : '', | |
| 188 | + 'operator' => (isset($exclude) && 'exclude' === $exclude ? 'NOT IN' : 'IN'), | |
| 189 | + ); | |
| 190 | + } | |
| 191 | + } | |
| 192 | + } else if ('post' == $prop_type) { | |
| 193 | + if ($tax_type) { | |
| 330 | 194 | |
| 331 | - // Return null if $date_gmt is empty/zeros. | |
| 332 | - if ( '0000-00-00 00:00:00' === $date_gmt ) { | |
| 333 | - return null; | |
| 334 | - } | |
| 335 | 195 | |
| 336 | - // Return the formatted datetime. | |
| 337 | - return mysql2date( 'Y-m-d\TH:i:s', $date_gmt, false ); | |
| 338 | - } | |
| 196 | + if (!empty($categories)) { | |
| 197 | + $query_args['tax_query'][] = array( | |
| 198 | + 'taxonomy' => (isset($tax_type)) ? $tax_type : 'category', | |
| 199 | + 'field' => 'term_id', | |
| 200 | + 'terms' => (!empty($categories)) ? $categories : '', | |
| 201 | + 'operator' => (isset($exclude) && 'exclude' === $exclude ? 'NOT IN' : 'IN') | |
| 339 | 202 | |
| 203 | + ); | |
| 204 | + } else { | |
| 340 | 205 | |
| 341 | - public function get_query_params() { | |
| 342 | - $query_params = parent::get_collection_params(); | |
| 206 | + if ($tax_type == 'post_tag') { | |
| 207 | + if (empty($tags)) { | |
| 208 | + $taxonomy = $tax_type; // this is the name of the taxonomy | |
| 209 | + $terms = get_terms($taxonomy); | |
| 343 | 210 | |
| 344 | - $query_params[ self::PROP_TYPE ] = array( | |
| 345 | - 'description' => __( 'Limit results to items of a specific post type.', 'blockspare' ), | |
| 346 | - 'type' => 'string', | |
| 347 | - 'sanitize_callback' => array( $this, 'sanitize_post_type_string' ), | |
| 348 | - 'validate_callback' => array( $this, 'validate_post_type_string' ), | |
| 349 | - ); | |
| 350 | - | |
| 351 | - $query_params[ self::PROP_INCLUDE ] = array( | |
| 352 | - 'description' => __( 'Include posts by ID.', 'blockspare' ), | |
| 353 | - 'type' => 'array', | |
| 354 | - 'validate_callback' => array( $this, 'validate_post_ids' ), | |
| 355 | - 'sanitize_callback' => array( $this, 'sanitize_post_ids' ), | |
| 356 | - ); | |
| 357 | - $query_params[ self::PROP_PER_PAGE ] = array( | |
| 358 | - 'description' => __( 'Number of results to return.', 'blockspare' ), | |
| 359 | - 'type' => 'number', | |
| 360 | - 'sanitize_callback' => array( $this, 'sanitize_post_perpage' ), | |
| 361 | - 'default' => 25, | |
| 362 | - ); | |
| 363 | - $query_params[ self::PROP_QUERY ] = array( | |
| 364 | - 'description' => __( 'Define Type of Query.', 'blockspare' ), | |
| 365 | - 'type' => 'string', | |
| 366 | - ); | |
| 367 | - $query_params[ self::PROP_ORDER ] = array( | |
| 368 | - 'description' => __( 'Define Query Order.', 'blockspare' ), | |
| 369 | - 'type' => 'string', | |
| 370 | - ); | |
| 371 | - $query_params[ self::PROP_ORDER_BY ] = array( | |
| 372 | - 'description' => __( 'Define Query Order By.', 'blockspare' ), | |
| 373 | - 'type' => 'string', | |
| 374 | - ); | |
| 375 | - $query_params[ self::PROP_ALLOW_STICKY ] = array( | |
| 376 | - 'description' => __( 'Allow Sticky in Query.', 'blockspare' ), | |
| 377 | - 'type' => 'boolean', | |
| 378 | - 'sanitize_callback' => array( $this, 'sanitize_allow_sticky' ), | |
| 379 | - ); | |
| 380 | - $query_params[ self::PROP_EXCLUDE ] = array( | |
| 381 | - 'description' => __( 'Exclude Category.', 'blockspare' ), | |
| 382 | - 'type' => 'string', | |
| 383 | - ); | |
| 384 | - $query_params[ self::PROP_OFFSET ] = array( | |
| 385 | - 'description' => __( 'Number of items to offset in query.', 'blockspare' ), | |
| 386 | - 'type' => 'number', | |
| 387 | - 'sanitize_callback' => array( $this, 'sanitize_results_page_number' ), | |
| 388 | - 'default' => 0, | |
| 389 | - ); | |
| 390 | - $query_params[ self::PROP_POST_ID ] = array( | |
| 391 | - 'description' => __( 'The Current Post ID.', 'blockspare' ), | |
| 392 | - 'type' => 'number', | |
| 393 | - ); | |
| 394 | - $query_params[ self::PROP_CUSTOM_TAX ] = array( | |
| 395 | - 'description' => __( 'Check if using a custom Taxonomy', 'blockspare' ), | |
| 396 | - 'type' => 'boolean', | |
| 397 | - 'sanitize_callback' => array( $this, 'sanitize_allow_sticky' ), | |
| 398 | - ); | |
| 399 | - $query_params[ self::PROP_TAX_TYPE ] = array( | |
| 400 | - 'description' => __( 'Define Query Order By.', 'blockspare' ), | |
| 401 | - 'type' => 'string', | |
| 402 | - ); | |
| 403 | - $query_params[ self::PROP_CATEGORY ] = array( | |
| 404 | - 'description' => __( 'Include posts category.', 'blockspare' ), | |
| 405 | - 'type' => 'string', | |
| 406 | - 'sanitize_callback' => 'wp_parse_id_list', | |
| 407 | - ); | |
| 408 | - $query_params[ self::PROP_TAGS ] = array( | |
| 409 | - 'description' => __( 'Include posts tags.', 'blockspare' ), | |
| 410 | - 'type' => 'string', | |
| 411 | - 'sanitize_callback' => 'wp_parse_id_list', | |
| 412 | - ); | |
| 413 | - return $query_params; | |
| 414 | - } | |
| 211 | + $query_args['tax_query'][] = array( | |
| 212 | + 'taxonomy' => (isset($tax_type)) ? $tax_type : 'category', | |
| 213 | + 'field' => 'term_id', | |
| 214 | + 'terms' => wp_list_pluck($terms, 'term_id') | |
| 415 | 215 | |
| 416 | - public function sanitize_post_ids( $ids ) { | |
| 417 | - return array_map( 'absint', $ids ); | |
| 418 | - } | |
| 419 | - public function validate_post_ids( $ids ) { | |
| 420 | - return count( $ids ) > 0; | |
| 421 | - } | |
| 422 | - public function sanitize_post_perpage( $val ) { | |
| 423 | - return min( absint( $val ), 100 ); | |
| 424 | - } | |
| 425 | - public function sanitize_allow_sticky( $val ) { | |
| 426 | - return $val ? 0 : 1; | |
| 427 | - } | |
| 428 | - public function sanitize_results_page_number( $val ) { | |
| 429 | - return absint( $val ); | |
| 430 | - } | |
| 431 | - public function validate_post_type_string( $value ) { | |
| 432 | - $allowed_types = $this->get_allowed_post_types(); | |
| 433 | - return in_array( $value, $allowed_types ); | |
| 434 | - } | |
| 216 | + ); | |
| 217 | + } else { | |
| 218 | + $query_args['tax_query'][] = array( | |
| 219 | + 'taxonomy' => (isset($tax_type)) ? $tax_type : 'category', | |
| 220 | + 'field' => 'id', | |
| 221 | + 'terms' => $tags, | |
| 435 | 222 | |
| 436 | - public function sanitize_post_type_string( $post_type, $request ) { | |
| 437 | - return sanitize_text_field( $post_type ); | |
| 438 | - } | |
| 223 | + ); | |
| 224 | + } | |
| 225 | + } else { | |
| 439 | 226 | |
| 440 | - public function get_allowed_post_types() { | |
| 441 | - $allowed_types = array_values( | |
| 442 | - get_post_types( | |
| 443 | - array( | |
| 444 | - 'show_in_rest' => true, | |
| 445 | - 'public' => true, | |
| 446 | - ) | |
| 447 | - ) | |
| 448 | - ); | |
| 227 | + $taxonomy = $tax_type; // this is the name of the taxonomy | |
| 228 | + $terms = get_terms($taxonomy); | |
| 229 | + $query_args['tax_query'][] = array( | |
| 230 | + 'taxonomy' => (isset($tax_type)) ? $tax_type : 'category', | |
| 231 | + 'field' => 'term_id', | |
| 232 | + 'terms' => wp_list_pluck($terms, 'term_id') | |
| 449 | 233 | |
| 450 | - $key = array_search( 'attachment', $allowed_types, true ); | |
| 234 | + ); | |
| 235 | + } | |
| 236 | + } | |
| 237 | + } | |
| 238 | + } else { | |
| 239 | + $tags = ($request->get_param(self::PROP_TAGS) ? wp_parse_list($request->get_param(self::PROP_TAGS)) : array()); | |
| 451 | 240 | |
| 452 | - if ( false !== $key ) { | |
| 453 | - unset( $allowed_types[ $key ] ); | |
| 454 | - } | |
| 241 | + $query_args['category__in'] = $categories; | |
| 242 | + $query_args['tag__in'] = $tags; | |
| 243 | + } | |
| 244 | + } | |
| 245 | + $query = new WP_Query($query_args); | |
| 246 | + $posts = array(); | |
| 455 | 247 | |
| 456 | - /** | |
| 457 | - * Filter the allowed post types. | |
| 458 | - * | |
| 459 | - * Note that if you allow this for posts that are not otherwise public, | |
| 460 | - * this data will be accessible using this endpoint for any logged in user with the edit_post capability. | |
| 461 | - */ | |
| 462 | - return apply_filters( 'aft_blocks_allowed_post_types', $allowed_types ); | |
| 463 | - } | |
| 248 | + foreach ($query->posts as $post) { | |
| 249 | + $posts[] = $this->prepare_query_item_for_response($post, $request, $tax_type); | |
| 250 | + } | |
| 464 | 251 | |
| 465 | - } | |
| 466 | -} | |
| 252 | + return rest_ensure_response($posts); | |
| 253 | + } | |
| 254 | + | |
| 255 | + public function prepare_query_item_for_response($post, $request, $tax_type) | |
| 256 | + { | |
| 257 | + $excerpt = $this->blockspare_api_excerpt($post, $length = 15); | |
| 258 | + $new_excerpt = apply_filters('the_excerpt', $excerpt); | |
| 259 | + $data = array( | |
| 260 | + 'id' => $post->ID, | |
| 261 | + 'date' => $this->prepare_date_response($post->post_date_gmt, $post->post_date), | |
| 262 | + 'date_gmt' => $this->prepare_date_response($post->post_date_gmt), | |
| 263 | + 'modified' => $this->prepare_date_response($post->post_modified_gmt, $post->post_modified), | |
| 264 | + 'modified_gmt' => $this->prepare_date_response($post->post_modified_gmt), | |
| 265 | + 'title' => array( | |
| 266 | + 'raw' => $post->post_title, | |
| 267 | + 'rendered' => get_the_title($post->ID), | |
| 268 | + ), | |
| 269 | + 'excerpt' => array( | |
| 270 | + 'raw' => $post->post_excerpt, | |
| 271 | + 'rendered' => get_the_excerpt($post->ID), | |
| 272 | + ), | |
| 273 | + 'data_content' => wp_kses_post($excerpt), | |
| 274 | + 'type' => $post->post_type, | |
| 275 | + 'slug' => $post->post_name, | |
| 276 | + 'status' => $post->post_status, | |
| 277 | + 'link' => get_permalink($post->ID), | |
| 278 | + 'author' => absint($post->post_author), | |
| 279 | + 'display_name' => get_the_author_meta('display_name', $request['author']), | |
| 280 | + 'author_link' => get_author_posts_url($request['author']), | |
| 281 | + 'featured_media' => get_post_thumbnail_id($post->ID), | |
| 282 | + ); | |
| 283 | + | |
| 284 | + $attachment_id = get_post_thumbnail_id($post->ID); | |
| 285 | + | |
| 286 | + $image = wp_get_attachment_image_src( | |
| 287 | + get_post_thumbnail_id($post), | |
| 288 | + 'full', | |
| 289 | + false | |
| 290 | + ); | |
| 291 | + $sizes = get_intermediate_image_sizes(); | |
| 292 | + | |
| 293 | + $imageSizes = array( | |
| 294 | + 'full' => is_array($image) ? $image : '', | |
| 295 | + ); | |
| 296 | + | |
| 297 | + foreach ($sizes as $size) { | |
| 298 | + $imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : ''; | |
| 299 | + } | |
| 300 | + | |
| 301 | + $data['featured_image_src_large'] = $imageSizes; | |
| 302 | + | |
| 303 | + //$author_data = array(); | |
| 304 | + if (post_type_supports($post->post_type, 'author')) { | |
| 305 | + $blockspare = new BlocksapreMultiAuthorForBackend(); | |
| 306 | + | |
| 307 | + | |
| 308 | + $data['author_info'] = $blockspare->blockspare_by_author($post); | |
| 309 | + } | |
| 310 | + if ('post' != $post->post_type || 'comments' != $post->post_type || 'author' != $post->post_type) { | |
| 311 | + $blockspare = new BlocksapreMultiAuthorForBackend(); | |
| 312 | + | |
| 313 | + $data['author_info'] = $blockspare->blockspare_by_author($post); | |
| 314 | + } | |
| 315 | + //$data['author_info'] = $author_data; | |
| 316 | + if (post_type_supports($post->post_type, 'comments')) { | |
| 317 | + $comments_count = wp_count_comments($post->ID); | |
| 318 | + $data['comment_count'] = $comments_count->total_comments; | |
| 319 | + } | |
| 320 | + if ('post' === $post->post_type) { | |
| 321 | + if ($tax_type == 'category' || $tax_type == '') { | |
| 322 | + $data['category_info'] = get_the_category($post->ID); | |
| 323 | + } else if ($tax_type == 'post_tag') { | |
| 324 | + $data['taxonomy_info'] = json_encode(get_the_tags($post->ID)); | |
| 325 | + } else if (!empty($tax_type) && ($tax_type != 'category' || $tax_type != 'post_tag')) { | |
| 326 | + | |
| 327 | + $term_items = array(); | |
| 328 | + $terms = get_the_terms($post->ID, $tax_type); | |
| 329 | + | |
| 330 | + if (! empty($terms)) { | |
| 331 | + | |
| 332 | + foreach ($terms as $term_key => $term_item) { | |
| 333 | + $term_items[] = array( | |
| 334 | + 'term_id' => $term_item->term_id, | |
| 335 | + 'name' => $term_item->name, | |
| 336 | + ); | |
| 337 | + } | |
| 338 | + | |
| 339 | + $data['taxonomy_info'] = json_encode($term_items); | |
| 340 | + } | |
| 341 | + } else { | |
| 342 | + $data['category_info'] = get_the_category($post->ID); | |
| 343 | + } | |
| 344 | + } else { | |
| 345 | + $taxonomies = get_object_taxonomies($post->post_type, 'objects'); | |
| 346 | + $taxs = array(); | |
| 347 | + $term_items = array(); | |
| 348 | + foreach ($taxonomies as $term_slug => $term) { | |
| 349 | + if (! $term->public || ! $term->show_ui) { | |
| 350 | + continue; | |
| 351 | + } | |
| 352 | + $terms = get_the_terms($post->ID, $term_slug); | |
| 353 | + | |
| 354 | + if (! empty($terms)) { | |
| 355 | + | |
| 356 | + foreach ($terms as $term_key => $term_item) { | |
| 357 | + $term_items[] = array( | |
| 358 | + 'term_id' => $term_item->term_id, | |
| 359 | + 'name' => $term_item->name, | |
| 360 | + ); | |
| 361 | + } | |
| 362 | + } | |
| 363 | + } | |
| 364 | + $data['category_info'] = get_the_category($post->ID); | |
| 365 | + $data['taxonomy_info'] = json_encode($term_items); | |
| 366 | + } | |
| 367 | + | |
| 368 | + | |
| 369 | + return $data; | |
| 370 | + } | |
| 371 | + | |
| 372 | + protected function blockspare_api_excerpt($post, $length = '') | |
| 373 | + { | |
| 374 | + | |
| 375 | + $excerpt = get_post_field( | |
| 376 | + 'post_excerpt', | |
| 377 | + $post->post_id, | |
| 378 | + 'display' | |
| 379 | + ); | |
| 380 | + | |
| 381 | + preg_replace('~^(\s*(?: )?)*~i', '', $excerpt); | |
| 382 | + if (empty($excerpt)) { | |
| 383 | + | |
| 384 | + $excerpt = preg_replace( | |
| 385 | + array( | |
| 386 | + '/\<figcaption>.*\<\/figcaption>/', | |
| 387 | + '/\[caption.*\[\/caption\]/', | |
| 388 | + '`[[^]]*]`' | |
| 389 | + ), | |
| 390 | + '', | |
| 391 | + $post->post_content | |
| 392 | + ); | |
| 393 | + } | |
| 394 | + | |
| 395 | + | |
| 396 | + // Trim the excerpt if necessary. | |
| 397 | + if (isset($length)) { | |
| 398 | + $excerpt = wp_trim_words( | |
| 399 | + $excerpt, | |
| 400 | + $length | |
| 401 | + ); | |
| 402 | + } | |
| 403 | + | |
| 404 | + return $excerpt; | |
| 405 | + } | |
| 406 | + | |
| 407 | + protected function prepare_date_response($date_gmt, $date = null) | |
| 408 | + { | |
| 409 | + // Use the date if passed. | |
| 410 | + if (isset($date)) { | |
| 411 | + return mysql2date('Y-m-d\TH:i:s', $date, false); | |
| 412 | + } | |
| 413 | + | |
| 414 | + // Return null if $date_gmt is empty/zeros. | |
| 415 | + if ('0000-00-00 00:00:00' === $date_gmt) { | |
| 416 | + return null; | |
| 417 | + } | |
| 418 | + | |
| 419 | + // Return the formatted datetime. | |
| 420 | + return mysql2date('Y-m-d\TH:i:s', $date_gmt, false); | |
| 421 | + } | |
| 422 | + | |
| 423 | + | |
| 424 | + public function get_query_params() | |
| 425 | + { | |
| 426 | + $query_params = parent::get_collection_params(); | |
| 427 | + | |
| 428 | + $query_params[self::PROP_TYPE] = array( | |
| 429 | + 'description' => __('Limit results to items of a specific post type.', 'blockspare'), | |
| 430 | + 'type' => 'string', | |
| 431 | + 'sanitize_callback' => array($this, 'sanitize_post_type_string'), | |
| 432 | + 'validate_callback' => array($this, 'validate_post_type_string'), | |
| 433 | + ); | |
| 434 | + | |
| 435 | + $query_params[self::PROP_INCLUDE] = array( | |
| 436 | + 'description' => __('Include posts by ID.', 'blockspare'), | |
| 437 | + 'type' => 'array', | |
| 438 | + 'validate_callback' => array($this, 'validate_post_ids'), | |
| 439 | + 'sanitize_callback' => array($this, 'sanitize_post_ids'), | |
| 440 | + ); | |
| 441 | + $query_params[self::PROP_PER_PAGE] = array( | |
| 442 | + 'description' => __('Number of results to return.', 'blockspare'), | |
| 443 | + 'type' => 'number', | |
| 444 | + 'sanitize_callback' => array($this, 'sanitize_post_perpage'), | |
| 445 | + 'default' => 25, | |
| 446 | + ); | |
| 447 | + $query_params[self::PROP_QUERY] = array( | |
| 448 | + 'description' => __('Define Type of Query.', 'blockspare'), | |
| 449 | + 'type' => 'string', | |
| 450 | + ); | |
| 451 | + $query_params[self::PROP_ORDER] = array( | |
| 452 | + 'description' => __('Define Query Order.', 'blockspare'), | |
| 453 | + 'type' => 'string', | |
| 454 | + ); | |
| 455 | + $query_params[self::PROP_ORDER_BY] = array( | |
| 456 | + 'description' => __('Define Query Order By.', 'blockspare'), | |
| 457 | + 'type' => 'string', | |
| 458 | + ); | |
| 459 | + $query_params[self::PROP_ALLOW_STICKY] = array( | |
| 460 | + 'description' => __('Allow Sticky in Query.', 'blockspare'), | |
| 461 | + 'type' => 'boolean', | |
| 462 | + 'sanitize_callback' => array($this, 'sanitize_allow_sticky'), | |
| 463 | + ); | |
| 464 | + $query_params[self::PROP_EXCLUDE] = array( | |
| 465 | + 'description' => __('Exclude Category.', 'blockspare'), | |
| 466 | + 'type' => 'string', | |
| 467 | + ); | |
| 468 | + $query_params[self::PROP_OFFSET] = array( | |
| 469 | + 'description' => __('Number of items to offset in query.', 'blockspare'), | |
| 470 | + 'type' => 'number', | |
| 471 | + 'sanitize_callback' => array($this, 'sanitize_results_page_number'), | |
| 472 | + 'default' => 0, | |
| 473 | + ); | |
| 474 | + $query_params[self::PROP_POST_ID] = array( | |
| 475 | + 'description' => __('The Current Post ID.', 'blockspare'), | |
| 476 | + 'type' => 'number', | |
| 477 | + ); | |
| 478 | + $query_params[self::PROP_CUSTOM_TAX] = array( | |
| 479 | + 'description' => __('Check if using a custom Taxonomy', 'blockspare'), | |
| 480 | + 'type' => 'boolean', | |
| 481 | + 'sanitize_callback' => array($this, 'sanitize_allow_sticky'), | |
| 482 | + ); | |
| 483 | + $query_params[self::PROP_TAX_TYPE] = array( | |
| 484 | + 'description' => __('Define Query Order By.', 'blockspare'), | |
| 485 | + 'type' => 'string', | |
| 486 | + ); | |
| 487 | + $query_params[self::PROP_CATEGORY] = array( | |
| 488 | + 'description' => __('Include posts category.', 'blockspare'), | |
| 489 | + 'type' => 'string', | |
| 490 | + 'sanitize_callback' => 'wp_parse_id_list', | |
| 491 | + ); | |
| 492 | + $query_params[self::PROP_TAGS] = array( | |
| 493 | + 'description' => __('Include posts tags.', 'blockspare'), | |
| 494 | + 'type' => 'string', | |
| 495 | + 'sanitize_callback' => 'wp_parse_id_list', | |
| 496 | + ); | |
| 497 | + return $query_params; | |
| 498 | + } | |
| 499 | + | |
| 500 | + public function sanitize_post_ids($ids) | |
| 501 | + { | |
| 502 | + return array_map('absint', $ids); | |
| 503 | + } | |
| 504 | + public function validate_post_ids($ids) | |
| 505 | + { | |
| 506 | + return count($ids) > 0; | |
| 507 | + } | |
| 508 | + public function sanitize_post_perpage($val) | |
| 509 | + { | |
| 510 | + return min(absint($val), 100); | |
| 511 | + } | |
| 512 | + public function sanitize_allow_sticky($val) | |
| 513 | + { | |
| 514 | + return $val ? 0 : 1; | |
| 515 | + } | |
| 516 | + public function sanitize_results_page_number($val) | |
| 517 | + { | |
| 518 | + return absint($val); | |
| 519 | + } | |
| 520 | + public function validate_post_type_string($value) | |
| 521 | + { | |
| 522 | + $allowed_types = $this->get_allowed_post_types(); | |
| 523 | + return in_array($value, $allowed_types); | |
| 524 | + } | |
| 525 | + | |
| 526 | + public function sanitize_post_type_string($post_type, $request) | |
| 527 | + { | |
| 528 | + return sanitize_text_field($post_type); | |
| 529 | + } | |
| 530 | + | |
| 531 | + public function get_allowed_post_types() | |
| 532 | + { | |
| 533 | + $allowed_types = array_values( | |
| 534 | + get_post_types( | |
| 535 | + array( | |
| 536 | + 'show_in_rest' => true, | |
| 537 | + 'public' => true, | |
| 538 | + ) | |
| 539 | + ) | |
| 540 | + ); | |
| 541 | + | |
| 542 | + $key = array_search('attachment', $allowed_types, true); | |
| 543 | + | |
| 544 | + if (false !== $key) { | |
| 545 | + unset($allowed_types[$key]); | |
| 546 | + } | |
| 547 | + | |
| 548 | + /** | |
| 549 | + * Filter the allowed post types. | |
| 550 | + * | |
| 551 | + * Note that if you allow this for posts that are not otherwise public, | |
| 552 | + * this data will be accessible using this endpoint for any logged in user with the edit_post capability. | |
| 553 | + */ | |
| 554 | + return apply_filters('aft_blocks_allowed_post_types', $allowed_types); | |
| 555 | + } | |
| 556 | + } | |
| 557 | +} | |