| 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_terms_for_post' => array( |
| 24 |
'callback' => array( $this, 'get_terms_for_post' ), |
| 25 |
'capability' => 'edit_posts', |
| 26 |
'fields' => [ |
| 27 |
'post_id' => 'integer', |
| 28 |
'taxonomy' => 'string', |
| 29 |
], |
| 30 |
), |
| 31 |
'get_all_meta_keys_for_post_type' => array( |
| 32 |
'callback' => array( $this, 'get_all_meta_keys_for_post_type' ), |
| 33 |
'capability' => 'edit_posts', |
| 34 |
'fields' => [ |
| 35 |
'post_type' => 'string', |
| 36 |
], |
| 37 |
), |
| 38 |
'get_post_meta' => array( |
| 39 |
'callback' => array( $this, 'get_post_meta' ), |
| 40 |
'capability' => 'edit_posts', |
| 41 |
'fields' => [ |
| 42 |
'post_id' => 'integer', |
| 43 |
'meta_key' => 'string', |
| 44 |
], |
| 45 |
), |
| 46 |
'search_posts' => array( |
| 47 |
'callback' => array( $this, 'search_posts' ), |
| 48 |
'capability' => 'edit_posts', |
| 49 |
'fields' => [ |
| 50 |
'query' => 'string', |
| 51 |
], |
| 52 |
), |
| 53 |
'get_author_data' => array( |
| 54 |
'callback' => array( $this, 'get_author_data' ), |
| 55 |
'capability' => 'edit_posts', |
| 56 |
'fields' => [ |
| 57 |
'post_id' => 'integer', |
| 58 |
'author_id' => 'integer', |
| 59 |
], |
| 60 |
), |
| 61 |
'get_author_meta' => array( |
| 62 |
'callback' => array( $this, 'get_author_meta' ), |
| 63 |
'capability' => 'edit_posts', |
| 64 |
'fields' => [ |
| 65 |
'author_id' => 'integer', |
| 66 |
'meta_key' => 'string', |
| 67 |
], |
| 68 |
), |
| 69 |
'get_post_comments_count' => array( |
| 70 |
'callback' => array( $this, 'get_post_comments_count' ), |
| 71 |
'capability' => 'edit_posts', |
| 72 |
'fields' => [ |
| 73 |
'postID' => 'integer', |
| 74 |
], |
| 75 |
), |
| 76 |
'get_featured_media_data' => array( |
| 77 |
'callback' => array( $this, 'get_featured_media_data' ), |
| 78 |
'capability' => 'edit_posts', |
| 79 |
'fields' => [ |
| 80 |
'postID' => 'integer', |
| 81 |
], |
| 82 |
), |
| 83 |
'get_all_taxonomies_terms_data' => array( |
| 84 |
'callback' => array( $this, 'get_all_taxonomies_terms_data' ), |
| 85 |
'capability' => 'edit_posts' |
| 86 |
), |
| 87 |
'search_medias' => array( |
| 88 |
'callback' => array( $this, 'search_medias' ), |
| 89 |
'capability' => 'edit_posts', |
| 90 |
'fields' => [ |
| 91 |
'query' => 'string', |
| 92 |
'mediaID' => 'integer', |
| 93 |
], |
| 94 |
), |
| 95 |
'get_all_authors_data' => array( |
| 96 |
'callback' => array( $this, 'get_all_authors_data' ), |
| 97 |
'capability' => 'edit_posts' |
| 98 |
), |
| 99 |
'get_author_profile_picture_data' => array( |
| 100 |
'callback' => array( $this, 'get_author_profile_picture_data' ), |
| 101 |
'capability' => 'edit_posts', |
| 102 |
'fields' => [ |
| 103 |
'postID' => 'integer', |
| 104 |
], |
| 105 |
), |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
public function get_taxonomies_data( $payload ) { |
| 110 |
$post_type = $payload['post_type']; |
| 111 |
|
| 112 |
if ( empty( $post_type ) ) { |
| 113 |
wp_send_json_error( __( 'Sorry, Post type is required!', 'ablocks' ) ); |
| 114 |
} |
| 115 |
|
| 116 |
$all_taxonomies = Helper::get_taxonomies_data_for_post_type( $post_type ); |
| 117 |
|
| 118 |
wp_send_json_success( $all_taxonomies ); |
| 119 |
} |
| 120 |
|
| 121 |
public function get_terms_for_post( $payload ) { |
| 122 |
$terms = Helper::get_terms_for_post( $payload['taxonomy'], $payload['post_id'] ); |
| 123 |
wp_send_json_success( $terms ); |
| 124 |
} |
| 125 |
|
| 126 |
public function get_all_meta_keys_for_post_type( $payload ) { |
| 127 |
global $wpdb; |
| 128 |
$post_type = $payload['post_type']; |
| 129 |
|
| 130 |
if ( empty( $post_type ) ) { |
| 131 |
wp_send_json_error( __( 'Sorry, Post type is required!', 'ablocks' ) ); |
| 132 |
} |
| 133 |
|
| 134 |
$query = " |
| 135 |
SELECT DISTINCT pm.meta_key |
| 136 |
FROM {$wpdb->postmeta} pm |
| 137 |
INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID |
| 138 |
WHERE p.post_type = %s |
| 139 |
AND pm.meta_key NOT LIKE '\_%' |
| 140 |
"; |
| 141 |
|
| 142 |
$meta_keys = $wpdb->get_col( $wpdb->prepare( $query, $post_type ) ); |
| 143 |
|
| 144 |
wp_send_json_success( $meta_keys ); |
| 145 |
} |
| 146 |
|
| 147 |
public function get_post_meta( $payload ) { |
| 148 |
|
| 149 |
if ( empty( $payload['post_id'] ) || empty( $payload['meta_key'] ) ) { |
| 150 |
wp_send_json_error( __( 'Sorry, Post ID and Meta Key is required!', 'ablocks' ) ); |
| 151 |
} |
| 152 |
|
| 153 |
$post_id = $payload['post_id']; |
| 154 |
$meta_key = $payload['meta_key']; |
| 155 |
|
| 156 |
$result = get_post_meta( $post_id, $meta_key, true ); |
| 157 |
|
| 158 |
wp_send_json_success( $result ); |
| 159 |
} |
| 160 |
|
| 161 |
public function search_posts( $payload ) { |
| 162 |
global $wpdb; |
| 163 |
$search_query = $payload['query']; |
| 164 |
$included_post_types = get_post_types( |
| 165 |
[ |
| 166 |
'public' => true, |
| 167 |
'show_in_rest' => true, // using `'show_in_rest' => true` because, unwanted post-types like `elementor_library` was being returned without this. |
| 168 |
], |
| 169 |
'names' |
| 170 |
); |
| 171 |
$included_post_types = array_values( $included_post_types ); |
| 172 |
$excluded_post_types = [ 'attachment', 'elementor_library' ]; |
| 173 |
$included_post_types = array_diff( $included_post_types, $excluded_post_types ); |
| 174 |
$post_type_placeholders = implode( ',', array_fill( 0, count( $included_post_types ), '%s' ) ); |
| 175 |
if ( empty( $search_query ) ) { |
| 176 |
$query = " |
| 177 |
SELECT ID, post_title, post_type |
| 178 |
FROM $wpdb->posts |
| 179 |
WHERE post_status = 'publish' |
| 180 |
AND post_type IN ($post_type_placeholders) |
| 181 |
ORDER BY post_date DESC |
| 182 |
LIMIT 5 |
| 183 |
"; |
| 184 |
$prepared_query = $wpdb->prepare( $query, ...$included_post_types ); |
| 185 |
} else { |
| 186 |
$query = " |
| 187 |
SELECT ID, post_title, post_type |
| 188 |
FROM $wpdb->posts |
| 189 |
WHERE post_status = 'publish' |
| 190 |
AND post_title LIKE %s |
| 191 |
AND post_type IN ($post_type_placeholders) |
| 192 |
LIMIT 5 |
| 193 |
"; |
| 194 |
$safe_query = '%' . $wpdb->esc_like( $search_query ) . '%'; |
| 195 |
$prepared_query = $wpdb->prepare( $query, array_merge( [ $safe_query ], $included_post_types ) ); |
| 196 |
}//end if |
| 197 |
$results = $wpdb->get_results( $prepared_query, ARRAY_A ); |
| 198 |
wp_send_json_success( $results ); |
| 199 |
} |
| 200 |
|
| 201 |
public function get_author_data( $payload ) { |
| 202 |
$post_id = $payload['post_id'] ?? 0; |
| 203 |
$author_id = $payload['author_id'] ?? 0; |
| 204 |
|
| 205 |
if ( ! $post_id || ! get_post( $post_id ) ) { |
| 206 |
wp_send_json_error( 'Invalid post ID.', 400 ); |
| 207 |
} |
| 208 |
|
| 209 |
$data = Helper::get_author_data( $post_id, $author_id ); |
| 210 |
|
| 211 |
wp_send_json_success( $data ); |
| 212 |
} |
| 213 |
|
| 214 |
public function get_author_meta( $payload ) { |
| 215 |
$meta_key = $payload['meta_key']; |
| 216 |
$author_id = $payload['author_id']; |
| 217 |
$data = get_user_meta( $author_id, $meta_key, true ); |
| 218 |
wp_send_json_success( $data ); |
| 219 |
} |
| 220 |
|
| 221 |
public function get_post_comments_count( $payload ) { |
| 222 |
$postID = $payload['postID'] ?? 0; |
| 223 |
$result = get_comments_number( $postID ); |
| 224 |
wp_send_json_success( $result ); |
| 225 |
} |
| 226 |
|
| 227 |
public function get_featured_media_data( $payload ) { |
| 228 |
$postID = $payload['postID'] ?? 0; |
| 229 |
|
| 230 |
$result = []; |
| 231 |
|
| 232 |
if ( $postID ) { |
| 233 |
$attachment_id = get_post_meta( $postID, '_thumbnail_id', true ); |
| 234 |
|
| 235 |
if ( $attachment_id ) { |
| 236 |
$attachment = get_post( $attachment_id ); |
| 237 |
|
| 238 |
if ( $attachment ) { |
| 239 |
$result = [ |
| 240 |
'featured_image_title' => $attachment->post_title, |
| 241 |
'featured_image_alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ), |
| 242 |
'featured_image_caption' => $attachment->post_excerpt, |
| 243 |
'featured_image_description' => $attachment->post_content, |
| 244 |
'featured_image_file_url' => wp_get_attachment_url( $attachment_id ), |
| 245 |
'featured_image_attachment_url' => get_permalink( $attachment_id ) |
| 246 |
]; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
wp_send_json_success( $result ); |
| 252 |
} |
| 253 |
|
| 254 |
public function get_all_taxonomies_terms_data() { |
| 255 |
|
| 256 |
$taxonomies = get_taxonomies( [ 'public' => true ], 'objects' ); |
| 257 |
$data = []; |
| 258 |
|
| 259 |
foreach ( $taxonomies as $taxonomy => $tax_obj ) { |
| 260 |
$terms = get_terms( [ |
| 261 |
'taxonomy' => $taxonomy, |
| 262 |
'hide_empty' => false |
| 263 |
] ); |
| 264 |
|
| 265 |
if ( empty( $terms ) || is_wp_error( $terms ) ) { |
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
foreach ( $terms as $term ) { |
| 270 |
$data[] = [ |
| 271 |
'value' => $term->term_id, |
| 272 |
'term_link' => get_term_link( $term ), |
| 273 |
'label' => "{$tax_obj->label}: {$term->name}", |
| 274 |
]; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
wp_send_json_success( $data ); |
| 279 |
} |
| 280 |
|
| 281 |
public function search_medias( $payload ) { |
| 282 |
|
| 283 |
global $wpdb; |
| 284 |
$search_query = $payload['query'] ?? ''; |
| 285 |
$media_id = $payload['mediaId'] ?? 0; |
| 286 |
|
| 287 |
if ( empty( $search_query ) ) { |
| 288 |
$prepared_query = $wpdb->prepare( |
| 289 |
"SELECT ID, post_title FROM {$wpdb->posts} |
| 290 |
WHERE post_type = 'attachment' AND post_status = 'inherit' |
| 291 |
ORDER BY post_date DESC LIMIT 5" |
| 292 |
); |
| 293 |
} else { |
| 294 |
$prepared_query = $wpdb->prepare( |
| 295 |
"SELECT ID, post_title FROM {$wpdb->posts} |
| 296 |
WHERE post_type = 'attachment' AND post_status = 'inherit' |
| 297 |
AND post_title LIKE %s |
| 298 |
ORDER BY post_date DESC LIMIT 5", |
| 299 |
'%' . $wpdb->esc_like( $search_query ) . '%' |
| 300 |
); |
| 301 |
} |
| 302 |
|
| 303 |
$results = $wpdb->get_results( $prepared_query, ARRAY_A ); |
| 304 |
|
| 305 |
$media_items = array_map(function ( $media ) { |
| 306 |
return [ |
| 307 |
'value' => $media['ID'], |
| 308 |
'label' => $media['post_title'], |
| 309 |
'url' => get_attachment_link( $media['ID'] ) |
| 310 |
]; |
| 311 |
}, $results); |
| 312 |
|
| 313 |
$media_item = null; |
| 314 |
$media_id_present_in_result = $media_id && ! in_array( "{$media_id}", array_column( $media_items, 'value' ) ); |
| 315 |
|
| 316 |
if ( $media_id_present_in_result ) { |
| 317 |
$media_item = $wpdb->get_row($wpdb->prepare( |
| 318 |
"SELECT ID, post_title FROM {$wpdb->posts} |
| 319 |
WHERE ID = %d AND post_type = 'attachment' AND post_status = 'inherit'", |
| 320 |
$media_id |
| 321 |
), ARRAY_A); |
| 322 |
} |
| 323 |
|
| 324 |
if ( $media_item ) { |
| 325 |
array_unshift($media_items, [ |
| 326 |
'value' => $media_item['ID'], |
| 327 |
'label' => $media_item['post_title'], |
| 328 |
'url' => get_attachment_link( $media_item['ID'] ) |
| 329 |
]); |
| 330 |
} |
| 331 |
|
| 332 |
wp_send_json_success( $media_items ); |
| 333 |
} |
| 334 |
|
| 335 |
public function get_all_authors_data() { |
| 336 |
|
| 337 |
$args = array( |
| 338 |
'has_published_posts' => true, |
| 339 |
'orderby' => 'display_name', |
| 340 |
'order' => 'ASC', |
| 341 |
'fields' => array( 'ID', 'display_name', 'user_email' ), |
| 342 |
); |
| 343 |
|
| 344 |
$users = get_users( $args ); |
| 345 |
$authors = array(); |
| 346 |
|
| 347 |
foreach ( $users as $user ) { |
| 348 |
$authors[] = array( |
| 349 |
'value' => $user->ID, |
| 350 |
'label' => $user->display_name . " ({$user->user_email})", |
| 351 |
); |
| 352 |
} |
| 353 |
|
| 354 |
wp_send_json_success( $authors ); |
| 355 |
} |
| 356 |
|
| 357 |
public function get_author_profile_picture_data( $payload ) { |
| 358 |
|
| 359 |
$post_id = $payload['postID'] ?? 0; |
| 360 |
|
| 361 |
if ( ! $post_id ) { |
| 362 |
$post_id = get_the_ID(); |
| 363 |
} |
| 364 |
|
| 365 |
$author_id = get_post_field( 'post_author', $post_id ); |
| 366 |
$result = get_avatar_url( $author_id ); |
| 367 |
|
| 368 |
wp_send_json_success( $result ); |
| 369 |
} |
| 370 |
} |
| 371 |
|