PluginProbe ʕ •ᴥ•ʔ
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / 5.0.3
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid v5.0.3
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 / RestApi.php
the-post-grid / app / Controllers / Api Last commit date
ACFV1.php 3 years ago FrontEndFilterV1.php 3 years ago GetPostsV1.php 3 years ago ImageSizeV1.php 3 years ago RestApi.php 3 years ago RttpgV1.php 3 years ago
RestApi.php
175 lines
1 <?php
2
3 namespace RT\ThePostGrid\Controllers\Api;
4
5 use RT\ThePostGrid\Helpers\Fns;
6
7 class RestApi {
8 /**
9 * Register rest route
10 */
11 public function __construct() {
12 add_action( 'rest_api_init', [ $this, 'init_rest_routes' ], 99 );
13 new ImageSizeV1();
14 new GetPostsV1();
15 new ACFV1();
16 new FrontEndFilterV1();
17 }
18
19 /**
20 * Init rest route
21 *
22 * @return void
23 */
24 public function init_rest_routes() {
25 $auth = new RttpgV1();
26 $auth->register_routes();
27 $this->rttpg_register_rest_fields();
28 }
29
30 function rttpg_register_rest_fields() {
31 $post_type = Fns::get_post_types();
32
33 // var_dump($post_type);
34 // die();
35
36 foreach ( $post_type as $key => $value ) {
37
38 // Featured image.
39 register_rest_field(
40 $key,
41 'rttpg_featured_image_url',
42 [
43 'get_callback' => [ $this, 'rttpg_get_featured_image_url' ],
44 'update_callback' => null,
45 'schema' => [
46 'description' => __( 'Different sized featured images' ),
47 'type' => 'array',
48 ],
49 ]
50 );
51
52 // Author info.
53 register_rest_field(
54 $key,
55 'rttpg_author',
56 [
57 'get_callback' => [ $this, 'rttpg_get_author_info' ],
58 'update_callback' => null,
59 'schema' => null,
60 ]
61 );
62
63 // Add comment info.
64 register_rest_field(
65 $key,
66 'rttpg_comment',
67 [
68 'get_callback' => [ $this, 'rttpg_get_comment_info' ],
69 'update_callback' => null,
70 'schema' => null,
71 ]
72 );
73
74 // Category links.
75 register_rest_field(
76 $key,
77 'rttpg_category',
78 [
79 'get_callback' => [ $this, 'rttpg_get_category_list' ],
80 'update_callback' => null,
81 'schema' => [
82 'description' => __( 'Category list links' ),
83 'type' => 'string',
84 ],
85 ]
86 );
87
88 // Excerpt.
89 register_rest_field(
90 $key,
91 'rttpg_excerpt',
92 [
93 'get_callback' => [ $this, 'rttpg_get_excerpt' ],
94 'update_callback' => null,
95 'schema' => null,
96 ]
97 );
98 }
99 }
100
101
102 // Author.
103 function rttpg_get_author_info( $object ) {
104 $author = ( isset( $object['author'] ) ) ? $object['author'] : '';
105
106 $author_data['display_name'] = get_the_author_meta( 'display_name', $author );
107 $author_data['author_link'] = get_author_posts_url( $author );
108
109 return $author_data;
110 }
111
112 // Comment.
113 function rttpg_get_comment_info( $object ) {
114 $comments_count = wp_count_comments( $object['id'] );
115
116 return $comments_count->total_comments;
117 }
118
119 // Category list.
120
121 function rttpg_get_category_list( $object ) {
122 $taxonomies = get_post_taxonomies( $object['id'] );
123 if ( 'post' === get_post_type() ) {
124 return get_the_category_list( esc_html__( ' ' ), '', $object['id'] );
125 } else {
126 if ( ! empty( $taxonomies ) ) {
127 return get_the_term_list( $object['id'], $taxonomies[0], ' ' );
128 }
129 }
130 }
131
132
133 // Feature image.
134 function rttpg_get_featured_image_url( $object ) {
135
136 $featured_images = [];
137 if ( ! isset( $object['featured_media'] ) ) {
138 return $featured_images;
139 }
140
141 $image = wp_get_attachment_image_src( $object['featured_media'], 'full', false );
142 if ( is_array( $image ) ) {
143 $featured_images['full'] = $image;
144 $featured_images['landscape'] = wp_get_attachment_image_src( $object['featured_media'], 'rttpg_landscape', false );
145 $featured_images['portraits'] = wp_get_attachment_image_src( $object['featured_media'], 'rttpg_portrait', false );
146 $featured_images['thumbnail'] = wp_get_attachment_image_src( $object['featured_media'], 'rttpg_thumbnail', false );
147
148 $image_sizes = Fns::get_image_sizes();
149 // var_dump($image_sizes);
150 // die();
151 foreach ( $image_sizes as $key => $value ) {
152 $size = $key;
153 $featured_images[ $size ] = wp_get_attachment_image_src(
154 $object['featured_media'],
155 $size,
156 false
157 );
158 }
159
160 return $featured_images;
161 }
162
163 }
164
165 // Excerpt.
166 function rttpg_get_excerpt( $object ) {
167 $excerpt = wp_trim_words( get_the_excerpt( $object['id'] ) );
168 if ( ! $excerpt ) {
169 $excerpt = null;
170 }
171
172 return $excerpt;
173 }
174
175 }