PluginProbe ʕ •ᴥ•ʔ
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / 7.2.9
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid v7.2.9
7.9.3 7.9.2 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.2.0 4.2.1 4.2.2 4.2.3 5.0.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 6.0.0 7.0.0 7.0.1 7.0.2 7.1.0 7.2.0 7.2.1 7.2.10 7.2.11 7.2.2 7.2.3 7.2.4 7.2.5 7.2.6 7.2.7 7.2.8 7.2.9 7.3.0 7.3.1 7.4.0 7.4.1 7.4.2 7.4.3 7.5.0 7.6.0 7.6.1 7.7.0 7.7.1 7.7.10 7.7.11 7.7.12 7.7.13 7.7.14 7.7.15 7.7.16 7.7.17 7.7.18 7.7.19 7.7.2 7.7.20 7.7.21 7.7.22 7.7.3 7.7.4 7.7.5 7.7.6 7.7.7 7.7.8 7.7.9 7.8.0 7.8.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1
the-post-grid / app / Controllers / Api / GetCategories.php
the-post-grid / app / Controllers / Api Last commit date
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