| 1 |
<?php |
| 2 |
/** |
| 3 |
* Setup the Posts Block Rest Calls |
| 4 |
* |
| 5 |
* @since 2.0.0 |
| 6 |
* @package AFT Blocks |
| 7 |
*/ |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* Class to setup new rest calls. |
| 12 |
*/ |
| 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'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Type property name. |
| 22 |
*/ |
| 23 |
const PROP_TYPE_ARRAY = 'type_array'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Query property name. |
| 27 |
*/ |
| 28 |
const PROP_QUERY = 'query'; |
| 29 |
/** |
| 30 |
* Query property name. |
| 31 |
*/ |
| 32 |
const PROP_MULTIPLE = 'multiple'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Query property name. |
| 36 |
*/ |
| 37 |
const PROP_ORDER_BY = 'order_by'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Query property name. |
| 41 |
*/ |
| 42 |
const PROP_ORDER = 'order'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Query property name. |
| 46 |
*/ |
| 47 |
const PROP_ALLOW_STICKY = 'allow_sticky'; |
| 48 |
|
| 49 |
/** |
| 50 |
* Query property name. |
| 51 |
*/ |
| 52 |
const PROP_OFFSET = 'offset'; |
| 53 |
|
| 54 |
/** |
| 55 |
* Query property name. |
| 56 |
*/ |
| 57 |
const PROP_TAX = 'tax'; |
| 58 |
|
| 59 |
/** |
| 60 |
* Query property name. |
| 61 |
*/ |
| 62 |
const PROP_EXCLUDE = 'exclude'; |
| 63 |
|
| 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'; |
| 76 |
|
| 77 |
/** |
| 78 |
* Query property name. |
| 79 |
*/ |
| 80 |
const PROP_TAX_TYPE = 'tax_type'; |
| 81 |
|
| 82 |
/** |
| 83 |
* Search property name. |
| 84 |
*/ |
| 85 |
const PROP_SEARCH = 'search'; |
| 86 |
|
| 87 |
/** |
| 88 |
* Include property name. |
| 89 |
*/ |
| 90 |
const PROP_INCLUDE = 'include'; |
| 91 |
|
| 92 |
/** |
| 93 |
* Per page property name. |
| 94 |
*/ |
| 95 |
const PROP_PER_PAGE = 'per_page'; |
| 96 |
|
| 97 |
/** |
| 98 |
* Per page property name. |
| 99 |
*/ |
| 100 |
const PROP_POST_ID = 'post_id'; |
| 101 |
|
| 102 |
/** |
| 103 |
* Page property name. |
| 104 |
*/ |
| 105 |
const PROP_PAGE = 'page'; |
| 106 |
|
| 107 |
/** |
| 108 |
* Constructor. |
| 109 |
*/ |
| 110 |
public function __construct() { |
| 111 |
$this->namespace = 'aft/v1'; |
| 112 |
$this->query_base = 'post-query'; |
| 113 |
} |
| 114 |
|
| 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 |
|
| 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 |
$query_args['tax_query'][] = array( |
| 174 |
'taxonomy' => ( isset( $tax_type ) ) ? $tax_type : 'category', |
| 175 |
'field' => 'id', |
| 176 |
'terms' => $categories, |
| 177 |
'operator' => ( isset( $exclude ) && 'exclude' === $exclude ? 'NOT IN' : 'IN' ), |
| 178 |
); |
| 179 |
} |
| 180 |
} else { |
| 181 |
$tags = ( $request->get_param( self::PROP_TAGS ) ? wp_parse_list( $request->get_param( self::PROP_TAGS ) ) : array() ); |
| 182 |
|
| 183 |
$query_args['category__in'] = $categories; |
| 184 |
$query_args['tag__in'] = $tags; |
| 185 |
|
| 186 |
} |
| 187 |
} |
| 188 |
$query = new WP_Query( $query_args ); |
| 189 |
$posts = array(); |
| 190 |
|
| 191 |
foreach ( $query->posts as $post ) { |
| 192 |
$posts[] = $this->prepare_query_item_for_response( $post, $request,$tax_type ); |
| 193 |
} |
| 194 |
|
| 195 |
return rest_ensure_response( $posts ); |
| 196 |
} |
| 197 |
|
| 198 |
public function prepare_query_item_for_response( $post, $request,$tax_type ) { |
| 199 |
$data = array( |
| 200 |
'id' => $post->ID, |
| 201 |
'date' => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ), |
| 202 |
'date_gmt' => $this->prepare_date_response( $post->post_date_gmt ), |
| 203 |
'modified' => $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ), |
| 204 |
'modified_gmt' => $this->prepare_date_response( $post->post_modified_gmt ), |
| 205 |
'title' => array( |
| 206 |
'raw' => $post->post_title, |
| 207 |
'rendered' => get_the_title( $post->ID ), |
| 208 |
), |
| 209 |
'excerpt' => array( |
| 210 |
'raw' => $post->post_excerpt, |
| 211 |
'rendered' => get_the_excerpt( $post->ID ), |
| 212 |
), |
| 213 |
'type' => $post->post_type, |
| 214 |
'slug' => $post->post_name, |
| 215 |
'status' => $post->post_status, |
| 216 |
'link' => get_permalink( $post->ID ), |
| 217 |
'author' => absint( $post->post_author ), |
| 218 |
'display_name'=> get_the_author_meta('display_name', $object['author']), |
| 219 |
'author_link'=>get_author_posts_url($object['author']), |
| 220 |
'featured_media' => get_post_thumbnail_id( $post->ID ), |
| 221 |
); |
| 222 |
|
| 223 |
$attachment_id = get_post_thumbnail_id( $post->ID ); |
| 224 |
|
| 225 |
$image= wp_get_attachment_image_src( |
| 226 |
get_post_thumbnail_id( $post ), |
| 227 |
'full', |
| 228 |
false |
| 229 |
); |
| 230 |
$sizes = get_intermediate_image_sizes(); |
| 231 |
|
| 232 |
$imageSizes = array( |
| 233 |
'full' => is_array($image) ? $image : '', |
| 234 |
); |
| 235 |
|
| 236 |
foreach ($sizes as $size) { |
| 237 |
$imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : ''; |
| 238 |
} |
| 239 |
|
| 240 |
$data['featured_image_src_large'] = $imageSizes; |
| 241 |
|
| 242 |
//$author_data = array(); |
| 243 |
if ( post_type_supports( $post->post_type, 'author' ) ) { |
| 244 |
$blockspare = new BlocksapreMultiAuthorForBackend(); |
| 245 |
$data['author_info'] = $blockspare->blockspare_by_author($post); |
| 246 |
} |
| 247 |
//$data['author_info'] = $author_data; |
| 248 |
if ( post_type_supports( $post->post_type, 'comments' ) ) { |
| 249 |
$comments_count = wp_count_comments( $post->ID ); |
| 250 |
$data['comment_count'] = $comments_count->total_comments; |
| 251 |
} |
| 252 |
if ( 'post' === $post->post_type ) { |
| 253 |
$data['category_info'] = get_the_category( $post->ID ); |
| 254 |
$data['tag_info'] = get_the_tags( $post->ID ); |
| 255 |
} |
| 256 |
$taxonomies = get_object_taxonomies( $post->post_type, 'objects' ); |
| 257 |
$taxs = array(); |
| 258 |
$term_items = array(); |
| 259 |
foreach ( $taxonomies as $term_slug => $term ) { |
| 260 |
if ( ! $term->public || ! $term->show_ui ) { |
| 261 |
continue; |
| 262 |
} |
| 263 |
$terms = get_the_terms( $post->ID, $term_slug ); |
| 264 |
|
| 265 |
if ( ! empty( $terms ) ) { |
| 266 |
foreach ( $terms as $term_key => $term_item ) { |
| 267 |
$term_items[] = array( |
| 268 |
'value' => $term_item->term_id, |
| 269 |
'label' => $term_item->name, |
| 270 |
); |
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
$data['taxonomy_info'] = json_encode($term_items); |
| 277 |
|
| 278 |
return $data; |
| 279 |
} |
| 280 |
|
| 281 |
protected function prepare_date_response( $date_gmt, $date = null ) { |
| 282 |
// Use the date if passed. |
| 283 |
if ( isset( $date ) ) { |
| 284 |
return mysql2date( 'Y-m-d\TH:i:s', $date, false ); |
| 285 |
} |
| 286 |
|
| 287 |
// Return null if $date_gmt is empty/zeros. |
| 288 |
if ( '0000-00-00 00:00:00' === $date_gmt ) { |
| 289 |
return null; |
| 290 |
} |
| 291 |
|
| 292 |
// Return the formatted datetime. |
| 293 |
return mysql2date( 'Y-m-d\TH:i:s', $date_gmt, false ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
public function get_query_params() { |
| 298 |
$query_params = parent::get_collection_params(); |
| 299 |
|
| 300 |
$query_params[ self::PROP_TYPE ] = array( |
| 301 |
'description' => __( 'Limit results to items of a specific post type.', 'blockspare' ), |
| 302 |
'type' => 'string', |
| 303 |
'sanitize_callback' => array( $this, 'sanitize_post_type_string' ), |
| 304 |
'validate_callback' => array( $this, 'validate_post_type_string' ), |
| 305 |
); |
| 306 |
|
| 307 |
$query_params[ self::PROP_INCLUDE ] = array( |
| 308 |
'description' => __( 'Include posts by ID.', 'blockspare' ), |
| 309 |
'type' => 'array', |
| 310 |
'validate_callback' => array( $this, 'validate_post_ids' ), |
| 311 |
'sanitize_callback' => array( $this, 'sanitize_post_ids' ), |
| 312 |
); |
| 313 |
$query_params[ self::PROP_PER_PAGE ] = array( |
| 314 |
'description' => __( 'Number of results to return.', 'blockspare' ), |
| 315 |
'type' => 'number', |
| 316 |
'sanitize_callback' => array( $this, 'sanitize_post_perpage' ), |
| 317 |
'default' => 25, |
| 318 |
); |
| 319 |
$query_params[ self::PROP_QUERY ] = array( |
| 320 |
'description' => __( 'Define Type of Query.', 'blockspare' ), |
| 321 |
'type' => 'string', |
| 322 |
); |
| 323 |
$query_params[ self::PROP_ORDER ] = array( |
| 324 |
'description' => __( 'Define Query Order.', 'blockspare' ), |
| 325 |
'type' => 'string', |
| 326 |
); |
| 327 |
$query_params[ self::PROP_ORDER_BY ] = array( |
| 328 |
'description' => __( 'Define Query Order By.', 'blockspare' ), |
| 329 |
'type' => 'string', |
| 330 |
); |
| 331 |
$query_params[ self::PROP_ALLOW_STICKY ] = array( |
| 332 |
'description' => __( 'Allow Sticky in Query.', 'blockspare' ), |
| 333 |
'type' => 'boolean', |
| 334 |
'sanitize_callback' => array( $this, 'sanitize_allow_sticky' ), |
| 335 |
); |
| 336 |
$query_params[ self::PROP_EXCLUDE ] = array( |
| 337 |
'description' => __( 'Exclude Category.', 'blockspare' ), |
| 338 |
'type' => 'string', |
| 339 |
); |
| 340 |
$query_params[ self::PROP_OFFSET ] = array( |
| 341 |
'description' => __( 'Number of items to offset in query.', 'blockspare' ), |
| 342 |
'type' => 'number', |
| 343 |
'sanitize_callback' => array( $this, 'sanitize_results_page_number' ), |
| 344 |
'default' => 0, |
| 345 |
); |
| 346 |
$query_params[ self::PROP_POST_ID ] = array( |
| 347 |
'description' => __( 'The Current Post ID.', 'blockspare' ), |
| 348 |
'type' => 'number', |
| 349 |
); |
| 350 |
$query_params[ self::PROP_CUSTOM_TAX ] = array( |
| 351 |
'description' => __( 'Check if using a custom Taxonomy', 'blockspare' ), |
| 352 |
'type' => 'boolean', |
| 353 |
'sanitize_callback' => array( $this, 'sanitize_allow_sticky' ), |
| 354 |
); |
| 355 |
$query_params[ self::PROP_TAX_TYPE ] = array( |
| 356 |
'description' => __( 'Define Query Order By.', 'blockspare' ), |
| 357 |
'type' => 'string', |
| 358 |
); |
| 359 |
$query_params[ self::PROP_CATEGORY ] = array( |
| 360 |
'description' => __( 'Include posts category.', 'blockspare' ), |
| 361 |
'type' => 'string', |
| 362 |
'sanitize_callback' => 'wp_parse_id_list', |
| 363 |
); |
| 364 |
$query_params[ self::PROP_TAGS ] = array( |
| 365 |
'description' => __( 'Include posts tags.', 'blockspare' ), |
| 366 |
'type' => 'string', |
| 367 |
'sanitize_callback' => 'wp_parse_id_list', |
| 368 |
); |
| 369 |
return $query_params; |
| 370 |
} |
| 371 |
|
| 372 |
public function sanitize_post_ids( $ids ) { |
| 373 |
return array_map( 'absint', $ids ); |
| 374 |
} |
| 375 |
public function validate_post_ids( $ids ) { |
| 376 |
return count( $ids ) > 0; |
| 377 |
} |
| 378 |
public function sanitize_post_perpage( $val ) { |
| 379 |
return min( absint( $val ), 100 ); |
| 380 |
} |
| 381 |
public function sanitize_allow_sticky( $val ) { |
| 382 |
return $val ? 0 : 1; |
| 383 |
} |
| 384 |
public function sanitize_results_page_number( $val ) { |
| 385 |
return absint( $val ); |
| 386 |
} |
| 387 |
public function validate_post_type_string( $value ) { |
| 388 |
$allowed_types = $this->get_allowed_post_types(); |
| 389 |
return in_array( $value, $allowed_types ); |
| 390 |
} |
| 391 |
|
| 392 |
public function sanitize_post_type_string( $post_type, $request ) { |
| 393 |
return sanitize_text_field( $post_type ); |
| 394 |
} |
| 395 |
|
| 396 |
public function get_allowed_post_types() { |
| 397 |
$allowed_types = array_values( |
| 398 |
get_post_types( |
| 399 |
array( |
| 400 |
'show_in_rest' => true, |
| 401 |
'public' => true, |
| 402 |
) |
| 403 |
) |
| 404 |
); |
| 405 |
|
| 406 |
$key = array_search( 'attachment', $allowed_types, true ); |
| 407 |
|
| 408 |
if ( false !== $key ) { |
| 409 |
unset( $allowed_types[ $key ] ); |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Filter the allowed post types. |
| 414 |
* |
| 415 |
* Note that if you allow this for posts that are not otherwise public, |
| 416 |
* this data will be accessible using this endpoint for any logged in user with the edit_post capability. |
| 417 |
*/ |
| 418 |
return apply_filters( 'aft_blocks_allowed_post_types', $allowed_types ); |
| 419 |
} |
| 420 |
|
| 421 |
} |
| 422 |
} |