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