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 / RestApi.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
RestApi.php
173 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 new ElImport();
18 new CountLayoutInstall();
19 new GetCategories();
20 }
21
22 /**
23 * Init rest route
24 *
25 * @return void
26 */
27 public function init_rest_routes() {
28 $auth = new RttpgV1();
29 $auth->register_routes();
30 $this->rttpg_register_rest_fields();
31 }
32
33 function rttpg_register_rest_fields() {
34 $post_type = Fns::get_post_types();
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 foreach ( $image_sizes as $key => $value ) {
150 $size = $key;
151 $featured_images[ $size ] = wp_get_attachment_image_src(
152 $object['featured_media'],
153 $size,
154 false
155 );
156 }
157
158 return $featured_images;
159 }
160
161 }
162
163 // Excerpt.
164 function rttpg_get_excerpt( $object ) {
165 $excerpt = wp_trim_words( get_the_excerpt( $object['id'] ) );
166 if ( ! $excerpt ) {
167 $excerpt = null;
168 }
169
170 return $excerpt;
171 }
172
173 }