PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.11.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.11.0
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / ajax / dynamic-content.php

dynamic-content.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.11.0, at includes/ajax/dynamic-content.php

436 lines 12.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocks\Ajax;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use ABlocks\Classes\AbstractAjaxHandler;
10 use ABlocks\Helper;
11
12 class DynamicContent extends AbstractAjaxHandler {
13
14 public function __construct() {
15 $this->actions = array(
16 'get_taxonomies_data' => array(
17 'callback' => array( $this, 'get_taxonomies_data' ),
18 'capability' => 'edit_posts',
19 'fields' => [
20 'post_type' => 'string',
21 ],
22 ),
23 'get_taxonomy_term_data' => array(
24 'callback' => array( $this, 'get_taxonomy_term_data' ),
25 'capability' => 'edit_posts',
26 'fields' => [
27 'taxonomy' => 'string',
28 ],
29 ),
30 'get_terms_for_post' => array(
31 'callback' => array( $this, 'get_terms_for_post' ),
32 'capability' => 'edit_posts',
33 'fields' => [
34 'post_id' => 'integer',
35 'taxonomy' => 'string',
36 ],
37 ),
38 'get_all_meta_keys_for_post_type' => array(
39 'callback' => array( $this, 'get_all_meta_keys_for_post_type' ),
40 'capability' => 'edit_posts',
41 'fields' => [
42 'post_type' => 'string',
43 ],
44 ),
45 'get_post_meta' => array(
46 'callback' => array( $this, 'get_post_meta' ),
47 'capability' => 'edit_posts',
48 'fields' => [
49 'post_id' => 'integer',
50 'meta_key' => 'string',
51 ],
52 ),
53 'search_posts' => array(
54 'callback' => array( $this, 'search_posts' ),
55 'capability' => 'edit_posts',
56 'fields' => [
57 'query' => 'string',
58 ],
59 ),
60 'get_author_data' => array(
61 'callback' => array( $this, 'get_author_data' ),
62 'capability' => 'edit_posts',
63 'fields' => [
64 'post_id' => 'integer',
65 'author_id' => 'integer',
66 ],
67 ),
68 'get_author_meta' => array(
69 'callback' => array( $this, 'get_author_meta' ),
70 'capability' => 'edit_posts',
71 'fields' => [
72 'author_id' => 'integer',
73 'meta_key' => 'string',
74 ],
75 ),
76 'get_post_comments_count' => array(
77 'callback' => array( $this, 'get_post_comments_count' ),
78 'capability' => 'edit_posts',
79 'fields' => [
80 'postID' => 'integer',
81 ],
82 ),
83 'get_featured_media_data' => array(
84 'callback' => array( $this, 'get_featured_media_data' ),
85 'capability' => 'edit_posts',
86 'fields' => [
87 'postID' => 'integer',
88 ],
89 ),
90 'get_all_taxonomies_terms_data' => array(
91 'callback' => array( $this, 'get_all_taxonomies_terms_data' ),
92 'capability' => 'edit_posts'
93 ),
94 'search_medias' => array(
95 'callback' => array( $this, 'search_medias' ),
96 'capability' => 'edit_posts',
97 'fields' => [
98 'query' => 'string',
99 'mediaID' => 'integer',
100 ],
101 ),
102 'get_all_authors_data' => array(
103 'callback' => array( $this, 'get_all_authors_data' ),
104 'capability' => 'edit_posts'
105 ),
106 'get_author_profile_picture_data' => array(
107 'callback' => array( $this, 'get_author_profile_picture_data' ),
108 'capability' => 'edit_posts',
109 'fields' => [
110 'postID' => 'integer',
111 ],
112 ),
113 'get_academy_course_data' => array(
114 'callback' => array( $this, 'get_academy_course_data' ),
115 'capability' => 'edit_posts',
116 'fields' => [
117 'post_id' => 'integer',
118 'meta_key' => 'string',
119 ],
120 ),
121 );
122 }
123
124 public function get_taxonomies_data( $payload ) {
125 $post_type = $payload['post_type'];
126
127 if ( empty( $post_type ) ) {
128 wp_send_json_error( __( 'Sorry, Post type is required!', 'ablocks' ) );
129 }
130
131 $all_taxonomies = Helper::get_taxonomies_data_for_post_type( $post_type );
132
133 wp_send_json_success( $all_taxonomies );
134 }
135
136 public function get_taxonomy_term_data( $payload ) {
137 if ( empty( $payload['taxonomy'] ) ) {
138 wp_send_json_error( __( 'Sorry, Taxonomy is required', 'ablocks' ) );
139 }
140 $terms = get_terms([
141 'taxonomy' => $payload['taxonomy'],
142 'hide_empty' => false, // Set to true if you only want terms attached to posts
143 ]);
144
145 $results = array_map(function( $term ) {
146 return [
147 'label' => $term->name,
148 'value' => $term->term_id,
149 ];
150 }, $terms);
151
152 wp_send_json_success( $results );
153 }
154
155 public function get_terms_for_post( $payload ) {
156 $terms = Helper::get_terms_for_post( $payload['taxonomy'], $payload['post_id'] );
157 wp_send_json_success( $terms );
158 }
159
160 public function get_all_meta_keys_for_post_type( $payload ) {
161 global $wpdb;
162 $post_type = $payload['post_type'];
163
164 if ( empty( $post_type ) ) {
165 wp_send_json_error( __( 'Sorry, Post type is required!', 'ablocks' ) );
166 }
167
168 $query = "
169 SELECT DISTINCT pm.meta_key
170 FROM {$wpdb->postmeta} pm
171 INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
172 WHERE p.post_type = %s
173 AND pm.meta_key NOT LIKE '\_%'
174 ";
175
176 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
177 $meta_keys = $wpdb->get_col( $wpdb->prepare( $query, $post_type ) );
178
179 wp_send_json_success( $meta_keys );
180 }
181
182 public function get_post_meta( $payload ) {
183
184 if ( empty( $payload['post_id'] ) || empty( $payload['meta_key'] ) ) {
185 wp_send_json_error( __( 'Sorry, Post ID and Meta Key is required!', 'ablocks' ) );
186 }
187
188 $post_id = (int) $payload['post_id'];
189 $meta_key = $payload['meta_key'];
190
191 $result = get_post_meta( $post_id, $meta_key, true );
192 wp_send_json_success( $result );
193 }
194
195 public function search_posts( $payload ) {
196 global $wpdb;
197 $search_query = $payload['query'];
198 $included_post_types = get_post_types(
199 [
200 'public' => true,
201 'show_in_rest' => true, // using `'show_in_rest' => true` because, unwanted post-types like `elementor_library` was being returned without this.
202 ],
203 'names'
204 );
205 $included_post_types = array_values( $included_post_types );
206 $excluded_post_types = [ 'attachment', 'elementor_library' ];
207 $included_post_types = array_diff( $included_post_types, $excluded_post_types );
208 $post_type_placeholders = implode( ',', array_fill( 0, count( $included_post_types ), '%s' ) );
209 if ( empty( $search_query ) ) {
210 $query = "
211 SELECT ID, post_title, post_type
212 FROM $wpdb->posts
213 WHERE post_status = 'publish'
214 AND post_type IN ($post_type_placeholders)
215 ORDER BY post_date DESC
216 LIMIT 5
217 ";
218 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
219 $prepared_query = $wpdb->prepare( $query, ...$included_post_types );
220 } else {
221 $query = "
222 SELECT ID, post_title, post_type
223 FROM $wpdb->posts
224 WHERE post_status = 'publish'
225 AND post_title LIKE %s
226 AND post_type IN ($post_type_placeholders)
227 LIMIT 5
228 ";
229 $safe_query = '%' . $wpdb->esc_like( $search_query ) . '%';
230 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
231 $prepared_query = $wpdb->prepare( $query, array_merge( [ $safe_query ], $included_post_types ) );
232 }//end if
233 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
234 $results = $wpdb->get_results( $prepared_query, ARRAY_A );
235 wp_send_json_success( $results );
236 }
237
238 public function get_author_data( $payload ) {
239 $post_id = $payload['post_id'] ?? 0;
240 $author_id = $payload['author_id'] ?? 0;
241
242 if ( ! $post_id || ! get_post( $post_id ) ) {
243 wp_send_json_error( 'Invalid post ID.', 400 );
244 }
245
246 $data = Helper::get_author_data( $post_id, $author_id );
247
248 wp_send_json_success( $data );
249 }
250
251 public function get_author_meta( $payload ) {
252 $meta_key = $payload['meta_key'];
253 $author_id = $payload['author_id'];
254 $data = get_user_meta( $author_id, $meta_key, true );
255 wp_send_json_success( $data );
256 }
257
258 public function get_post_comments_count( $payload ) {
259 $postID = $payload['postID'] ?? 0;
260 $result = get_comments_number( $postID );
261 wp_send_json_success( $result );
262 }
263
264 public function get_featured_media_data( $payload ) {
265 $postID = $payload['postID'] ?? 0;
266
267 $result = [];
268
269 if ( $postID ) {
270 $attachment_id = get_post_meta( $postID, '_thumbnail_id', true );
271
272 if ( $attachment_id ) {
273 $attachment = get_post( $attachment_id );
274
275 if ( $attachment ) {
276 $result = [
277 'featured_image_title' => $attachment->post_title,
278 'featured_image_alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
279 'featured_image_caption' => $attachment->post_excerpt,
280 'featured_image_description' => $attachment->post_content,
281 'featured_image_file_url' => wp_get_attachment_url( $attachment_id ),
282 'featured_image_attachment_url' => get_permalink( $attachment_id )
283 ];
284 }
285 }
286 }
287
288 wp_send_json_success( $result );
289 }
290
291 public function get_all_taxonomies_terms_data() {
292
293 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
294 $data = [];
295
296 foreach ( $taxonomies as $taxonomy => $tax_obj ) {
297 $terms = get_terms( [
298 'taxonomy' => $taxonomy,
299 'hide_empty' => false
300 ] );
301
302 if ( empty( $terms ) || is_wp_error( $terms ) ) {
303 continue;
304 }
305
306 foreach ( $terms as $term ) {
307 $data[] = [
308 'value' => $term->term_id,
309 'term_link' => get_term_link( $term ),
310 'label' => "{$tax_obj->label}: {$term->name}",
311 ];
312 }
313 }
314
315 wp_send_json_success( $data );
316 }
317
318 public function search_medias( $payload ) {
319
320 global $wpdb;
321 $search_query = $payload['query'] ?? '';
322 $media_id = $payload['mediaId'] ?? 0;
323
324 if ( empty( $search_query ) ) {
325 $prepared_query = "SELECT ID, post_title FROM {$wpdb->posts}
326 WHERE post_type = 'attachment' AND post_status = 'inherit'
327 ORDER BY post_date DESC LIMIT 5";
328 } else {
329 $prepared_query = $wpdb->prepare(
330 "SELECT ID, post_title FROM {$wpdb->posts}
331 WHERE post_type = 'attachment' AND post_status = 'inherit'
332 AND post_title LIKE %s
333 ORDER BY post_date DESC LIMIT 5",
334 '%' . $wpdb->esc_like( $search_query ) . '%'
335 );
336 }
337
338 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
339 $results = $wpdb->get_results( $prepared_query, ARRAY_A );
340
341 $media_items = array_map(function ( $media ) {
342 return [
343 'value' => $media['ID'],
344 'label' => $media['post_title'],
345 'url' => get_attachment_link( $media['ID'] )
346 ];
347 }, $results);
348
349 $media_item = null;
350 $media_id_present_in_result = $media_id && ! in_array( "{$media_id}", array_column( $media_items, 'value' ) );
351
352 if ( $media_id_present_in_result ) {
353 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
354 $media_item = $wpdb->get_row($wpdb->prepare(
355 "SELECT ID, post_title FROM {$wpdb->posts}
356 WHERE ID = %d AND post_type = 'attachment' AND post_status = 'inherit'",
357 $media_id
358 ), ARRAY_A);
359 }
360
361 if ( $media_item ) {
362 array_unshift($media_items, [
363 'value' => $media_item['ID'],
364 'label' => $media_item['post_title'],
365 'url' => get_attachment_link( $media_item['ID'] )
366 ]);
367 }
368
369 wp_send_json_success( $media_items );
370 }
371
372 public function get_all_authors_data() {
373
374 $args = array(
375 'has_published_posts' => true,
376 'orderby' => 'display_name',
377 'order' => 'ASC',
378 'fields' => array( 'ID', 'display_name', 'user_email' ),
379 );
380
381 $users = get_users( $args );
382 $authors = array();
383
384 foreach ( $users as $user ) {
385 $authors[] = array(
386 'value' => $user->ID,
387 'label' => $user->display_name . " ({$user->user_email})",
388 );
389 }
390
391 wp_send_json_success( $authors );
392 }
393
394 public function get_author_profile_picture_data( $payload ) {
395
396 $post_id = $payload['postID'] ?? 0;
397
398 if ( ! $post_id ) {
399 $post_id = get_the_ID();
400 }
401
402 $author_id = get_post_field( 'post_author', $post_id );
403 $result = get_avatar_url( $author_id );
404
405 wp_send_json_success( $result );
406 }
407
408 public function get_academy_course_data( $payload ) {
409
410 $post_id = $payload['post_id'] ?? 0;
411 $meta_key = $payload['meta_key'] ?? '';
412
413 if ( ! $post_id ) {
414 $post_id = get_the_ID();
415 }
416
417 if ( ! Helper::is_active_academy() ) {
418 wp_send_json_error( '' );
419 }
420
421 $Analytics = new \Academy\Classes\Analytics();
422 $results = 0;
423 if ( 'numberOfTopics' === $meta_key ) {
424 $curriculums = \Academy\Helper::get_course_curriculums_number_of_counts( $post_id );
425 $results = (int) isset( $curriculums['total_topics'] ) ? $curriculums['total_topics'] : 0;
426 } elseif ( 'numberOfReviews' === $meta_key ) {
427 $results = (int) $Analytics->get_total_number_of_reviews_by_course_id( $post_id );
428 } elseif ( 'numberOfEnrolled' === $meta_key ) {
429 $results = (int) $Analytics->get_total_number_of_enrolled_by_course_id( $post_id );
430 } elseif ( 'totalDuration' === $meta_key ) {
431 $results = (int) \Academy\Helper::get_course_duration( $post_id );
432 }
433 wp_send_json_success( $results );
434 }
435 }
436