ACFV1.php
3 years ago
CountLayoutInstall.php
3 years ago
ElImport.php
3 years ago
FrontEndFilterV1.php
3 years ago
GetCategories.php
3 years ago
GetPostsV1.php
3 years ago
ImageSizeV1.php
3 years ago
RestApi.php
3 years ago
RttpgV1.php
3 years ago
GetCategories.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | namespace RT\ThePostGrid\Controllers\Api; |
| 4 | |
| 5 | class GetCategories { |
| 6 | public function __construct() { |
| 7 | add_action( "rest_api_init", [ $this, 'register_post_route' ] ); |
| 8 | } |
| 9 | |
| 10 | public function register_post_route() { |
| 11 | register_rest_route( 'rttpg/v1', 'categories', [ |
| 12 | 'methods' => 'POST', |
| 13 | 'callback' => [ $this, 'get_all_posts' ], |
| 14 | 'permission_callback' => function () { |
| 15 | return true; |
| 16 | } |
| 17 | ] ); |
| 18 | } |
| 19 | |
| 20 | |
| 21 | public function get_all_posts( $data ) { |
| 22 | $send_data = [ |
| 23 | 'categories' => [], |
| 24 | ]; |
| 25 | |
| 26 | $category = $data['category_lists']; |
| 27 | $count_cat = count( $category ); |
| 28 | |
| 29 | if ( is_array( $category ) && $count_cat > 0 ) { |
| 30 | $categories = wp_list_pluck( $category, 'value' ); |
| 31 | } else { |
| 32 | $categories = get_terms( 'category', array( |
| 33 | 'orderby' => 'count', |
| 34 | 'order' => 'DESC', |
| 35 | 'hide_empty' => 0, |
| 36 | 'fields' => 'ids', |
| 37 | 'number' => 5 |
| 38 | ) ); |
| 39 | } |
| 40 | |
| 41 | if ( ! empty( $categories ) ) { |
| 42 | foreach ( $categories as $cat ) { |
| 43 | $cat_info = get_term( $cat ); |
| 44 | $cat_thumb = get_term_meta( $cat, rtTpgPro()->category_thumb_meta_key, true ); |
| 45 | $send_data['categories'][] = [ |
| 46 | 'id' => esc_html( $cat ), |
| 47 | 'name' => esc_html( $cat_info->name ), |
| 48 | 'image' => wp_get_attachment_image_src( $cat_thumb, $data['image_size'] )[0], |
| 49 | 'count' => $cat_info->count, |
| 50 | 'link' => get_term_link( $cat_info ), |
| 51 | |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | } else { |
| 56 | $send_data['message'] = 'No category found'; |
| 57 | } |
| 58 | |
| 59 | wp_reset_postdata(); |
| 60 | |
| 61 | return rest_ensure_response( $send_data ); |
| 62 | } |
| 63 | } |
| 64 |