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