| 1 |
<?php |
| 2 |
/** |
| 3 |
* The plugin gutenberg block query Initializer. |
| 4 |
* |
| 5 |
* @link https://shapedplugin.com/ |
| 6 |
* @since 2.0.0 |
| 7 |
* |
| 8 |
* @package Smart_Post_Show |
| 9 |
* @subpackage Smart_Post_Show/blocks/includes |
| 10 |
* @author ShapedPlugin <[email protected]> |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Sp_Smart_Post_Blocks_Query' ) ) { |
| 19 |
/** |
| 20 |
* Sp_Smart_Post_Blocks_Query class. |
| 21 |
*/ |
| 22 |
class Sp_Smart_Post_Blocks_Query { |
| 23 |
|
| 24 |
/** |
| 25 |
* This plugin's instance. |
| 26 |
* |
| 27 |
* @var Query |
| 28 |
*/ |
| 29 |
private static $instance; |
| 30 |
|
| 31 |
/** |
| 32 |
* Main Query Instance. |
| 33 |
* |
| 34 |
* Insures that only one instance of Blocks exists in memory at any one |
| 35 |
* time. Also prevents needing to define globals all over the place. |
| 36 |
* |
| 37 |
* @static |
| 38 |
* @return object|Query The one true Blocks |
| 39 |
*/ |
| 40 |
public static function instance() { |
| 41 |
if ( ! isset( self::$instance ) ) { |
| 42 |
self::$instance = new Sp_Smart_Post_Blocks_Query(); |
| 43 |
self::$instance->init(); |
| 44 |
} |
| 45 |
return self::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Initialize function |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function init() { |
| 54 |
add_action( 'wp_ajax_sp_smart_post_block_post_query', array( $this, 'sp_smart_post_block_post_query' ) ); |
| 55 |
add_action( 'wp_ajax_nopriv_sp_smart_post_block_post_query', array( $this, 'sp_smart_post_block_post_query' ) ); |
| 56 |
// query functions for get all post. |
| 57 |
if ( is_admin() ) { |
| 58 |
add_action( 'wp_ajax_sp_smart_post_block_all_post_query', array( $this, 'sp_smart_post_block_all_post_query' ) ); |
| 59 |
add_action( 'wp_ajax_nopriv_sp_smart_post_block_all_post_query', array( $this, 'sp_smart_post_block_all_post_query' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
// metadata query. |
| 63 |
add_action( 'wp_ajax_sp_smart_post_block_meta_data_query', array( $this, 'sp_smart_post_block_meta_data_query' ) ); |
| 64 |
add_action( 'wp_ajax_nopriv_sp_smart_post_block_meta_data_query', array( $this, 'sp_smart_post_block_meta_data_query' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Handles the frontend post query for the Smart Post Block. |
| 69 |
* |
| 70 |
* @param array $query_data The query data for fetching posts. |
| 71 |
* @param string $block_id Optional. The block ID. |
| 72 |
* @return array An array containing post data and total pages. |
| 73 |
*/ |
| 74 |
public static function post_query_frontend( $query_data, $block_id = '' ) { |
| 75 |
$config = self::prepare_query_config( $query_data, $block_id ); |
| 76 |
|
| 77 |
self::setup_image_filters( $config['image_srcset'] ); |
| 78 |
|
| 79 |
$post_query = \SmartPostShow\Blocks\PostQueryHandler::query( $query_data, 'normal', $block_id ); |
| 80 |
$the_query = $post_query['the_query']; |
| 81 |
$total_pages = $post_query['total_pages'] ?? 1; |
| 82 |
$all_post_ids = $post_query['post_ids'] ?? array(); |
| 83 |
|
| 84 |
$post_data = self::process_query_posts( $the_query, $config ); |
| 85 |
|
| 86 |
self::cleanup_image_filters( $config['image_srcset'] ); |
| 87 |
|
| 88 |
return array( $post_data, $total_pages, $all_post_ids ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Prepare configuration data from query parameters. |
| 93 |
* |
| 94 |
* @param array $query_data The query data. |
| 95 |
* @param string $block_id The block ID. |
| 96 |
* @return array Configuration array. |
| 97 |
*/ |
| 98 |
private static function prepare_query_config( $query_data, $block_id = '' ) { |
| 99 |
$config = array( |
| 100 |
'post_type' => $query_data['postType'] ?? 'post', |
| 101 |
'date_format_type' => $query_data['metaDateFormat'] ?? 'default', |
| 102 |
'custom_date_format' => $query_data['metaDateCustomDateFormat'] ?? '', |
| 103 |
'image_size' => $query_data['imageSize'] ?? '', |
| 104 |
'unique_id' => $query_data['uniqueId'] ?? '', |
| 105 |
'enable_seo_description' => $query_data['seoMetaShow'] ?? false, |
| 106 |
'active_seo_plugin' => $query_data['activeSeoPlugin'] ?? '', |
| 107 |
'exclude_post_without_image' => $query_data['excludePostWithoutImagePosts'] ?? false, |
| 108 |
'image_srcset' => ! empty( $query_data['imageSrcset'] ) ? (bool) $query_data['imageSrcset'] : false, |
| 109 |
'image_lazy_enable' => $query_data['imageLazyLoad'] ?? false, |
| 110 |
'taxonomy_type' => $query_data['catTabCategoryType'] ?? '', |
| 111 |
); |
| 112 |
|
| 113 |
$config['date_format'] = self::get_date_format( $config['date_format_type'], $config['custom_date_format'] ); |
| 114 |
$config['image_lazy_load'] = $config['image_lazy_enable'] ? 'lazy' : 'eager'; |
| 115 |
$config['multiple_post_type'] = self::prepare_multiple_post_types( $config['post_type'], $query_data ); |
| 116 |
|
| 117 |
// Handle pagination. |
| 118 |
if ( $query_data['paginationEnable'] ?? false ) { |
| 119 |
$paged_var = 'paged_' . $block_id; |
| 120 |
$query_data['currentPage'] = isset( $_GET[ $paged_var ] ) ? intval( $_GET[ $paged_var ] ) : $query_data['currentPage'] ?? 1; |
| 121 |
} |
| 122 |
|
| 123 |
return $config; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get the appropriate date format. |
| 128 |
* |
| 129 |
* @param string $date_format_type The date format type. |
| 130 |
* @param string $custom_format Custom date format. |
| 131 |
* @return string The date format. |
| 132 |
*/ |
| 133 |
private static function get_date_format( $date_format_type, $custom_format ) { |
| 134 |
switch ( $date_format_type ) { |
| 135 |
case 'F j, Y': |
| 136 |
case 'M j, Y': |
| 137 |
$date_format = $date_format_type; |
| 138 |
break; |
| 139 |
case 'custom': |
| 140 |
$date_format = $custom_format; |
| 141 |
break; |
| 142 |
default: |
| 143 |
$date_format = get_option( 'date_format' ); |
| 144 |
break; |
| 145 |
} |
| 146 |
return $date_format; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Prepare multiple post types array. |
| 151 |
* |
| 152 |
* @param string $current_post_type Current post type. |
| 153 |
* @param array $query_data Query data. |
| 154 |
* @return array Multiple post types. |
| 155 |
*/ |
| 156 |
private static function prepare_multiple_post_types( $current_post_type, $query_data ) { |
| 157 |
if ( 'multiple_post_type' !== $current_post_type || empty( $query_data['multiplePostType'] ) ) { |
| 158 |
return array( 'post' ); |
| 159 |
} |
| 160 |
|
| 161 |
return array_map( |
| 162 |
function ( $multi_type ) { |
| 163 |
return is_array( $multi_type ) && isset( $multi_type['value'] ) ? $multi_type['value'] : $multi_type->value; |
| 164 |
}, |
| 165 |
$query_data['multiplePostType'] |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Setup image-related filters. |
| 171 |
* |
| 172 |
* @param bool $image_srcset Whether srcset is enabled. |
| 173 |
*/ |
| 174 |
private static function setup_image_filters( $image_srcset ) { |
| 175 |
if ( ! $image_srcset ) { |
| 176 |
add_filter( 'wp_get_attachment_image_attributes', array( self::class, 'remove_image_srcset' ), 10, 3 ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Cleanup image-related filters. |
| 182 |
* |
| 183 |
* @param bool $image_srcset Whether srcset is enabled. |
| 184 |
*/ |
| 185 |
private static function cleanup_image_filters( $image_srcset ) { |
| 186 |
if ( ! $image_srcset ) { |
| 187 |
remove_filter( 'wp_get_attachment_image_attributes', array( self::class, 'remove_image_srcset' ), 10, 3 ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Process posts from the query. |
| 193 |
* |
| 194 |
* @param WP_Query $the_query The WordPress query object. |
| 195 |
* @param array $config Configuration array. |
| 196 |
* @return array Post data array. |
| 197 |
*/ |
| 198 |
private static function process_query_posts( $the_query, $config ) { |
| 199 |
$post_data = array(); |
| 200 |
|
| 201 |
if ( ! $the_query->have_posts() ) { |
| 202 |
return $post_data; |
| 203 |
} |
| 204 |
|
| 205 |
while ( $the_query->have_posts() ) { |
| 206 |
$the_query->the_post(); |
| 207 |
|
| 208 |
$single_post_data = self::prepare_single_post_data( $config ); |
| 209 |
$post_data[] = $single_post_data; |
| 210 |
} |
| 211 |
wp_reset_postdata(); |
| 212 |
return $post_data; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Prepare data for a single post. |
| 217 |
* |
| 218 |
* @param array $config Configuration array. |
| 219 |
* @return array Single post data. |
| 220 |
*/ |
| 221 |
private static function prepare_single_post_data( $config ) { |
| 222 |
$post_id = get_the_ID(); |
| 223 |
$image_id = get_post_thumbnail_id(); |
| 224 |
$author_meta_id = get_the_author_meta( 'ID' ); |
| 225 |
$current_date = get_the_date( 'c' ); |
| 226 |
|
| 227 |
$post_data = self::get_basic_post_data( $post_id, $image_id, $author_meta_id, $current_date, $config ); |
| 228 |
|
| 229 |
// Add WooCommerce product data if applicable. |
| 230 |
if ( 'product' === $config['post_type'] && function_exists( 'wc_get_product' ) ) { |
| 231 |
$post_data = array_merge( $post_data, self::get_product_data( $post_id ) ); |
| 232 |
} |
| 233 |
|
| 234 |
return $post_data; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Get basic post data. |
| 239 |
* |
| 240 |
* @param int $post_id Post ID. |
| 241 |
* @param int $image_id Image ID. |
| 242 |
* @param int $author_meta_id Author ID. |
| 243 |
* @param string $current_date Current date. |
| 244 |
* @param array $config Configuration array. |
| 245 |
* @return array Basic post data. |
| 246 |
*/ |
| 247 |
private static function get_basic_post_data( $post_id, $image_id, $author_meta_id, $current_date, $config ) { |
| 248 |
$archive_year = get_the_time( 'Y' ); |
| 249 |
$archive_month = get_the_time( 'm' ); |
| 250 |
$archive_day = get_the_time( 'd' ); |
| 251 |
$taxonomy_type = $config['taxonomy_type'] ?? ''; |
| 252 |
|
| 253 |
return array( |
| 254 |
'post_id' => $post_id, |
| 255 |
'title' => get_the_title(), |
| 256 |
'link' => get_the_permalink(), |
| 257 |
'content' => strip_shortcodes( get_the_content() ), |
| 258 |
'excerpt' => get_the_excerpt(), |
| 259 |
'post_thumbnail' => get_the_post_thumbnail(), |
| 260 |
|
| 261 |
'post_thumbnail_url' => get_the_post_thumbnail_url( $post_id, $config['image_size'] ?? 'thumbnail' ), |
| 262 |
'image_size' => $config['image_size'], |
| 263 |
|
| 264 |
'attachment_srcset' => wp_get_attachment_image_srcset( $image_id ), |
| 265 |
'attachment_metadata' => wp_get_attachment_metadata( $image_id ), |
| 266 |
'attachment_url' => wp_get_attachment_url( $image_id ), |
| 267 |
'post_thumbnail_id' => $image_id, |
| 268 |
'author' => get_the_author(), |
| 269 |
'author_id' => $author_meta_id, |
| 270 |
'author_avatar_url' => get_avatar_url( $author_meta_id ), |
| 271 |
'author_url' => get_author_posts_url( $author_meta_id ), |
| 272 |
'category_list' => get_the_category_list( '' ), |
| 273 |
'tag_list' => get_the_tag_list( '' ), |
| 274 |
'post_list' => get_post_format( '' ), |
| 275 |
'category' => get_the_category(), |
| 276 |
'date' => $current_date, |
| 277 |
'date_archive_url' => get_day_link( $archive_year, $archive_month, $archive_day ), |
| 278 |
'comment_count' => get_comment_count( $post_id ), |
| 279 |
'view_count' => get_post_meta( $post_id, '_post_views_count', true ), |
| 280 |
'like_count' => get_post_meta( $post_id, '_post_like_count', true ), |
| 281 |
'image_title' => get_the_title( $image_id ), |
| 282 |
'image_alt' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ), |
| 283 |
'post_date' => self::format_post_dates( $current_date, $config['date_format'] ), |
| 284 |
'badges_list' => get_the_terms( $post_id, 'sp_smart_badges' ), |
| 285 |
'all_term_list' => self::get_all_terms( $post_id, $taxonomy_type ), |
| 286 |
); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Format post dates in various formats. |
| 291 |
* |
| 292 |
* @param string $current_date Current date. |
| 293 |
* @param string $date_format Date format. |
| 294 |
* @return array Formatted dates. |
| 295 |
*/ |
| 296 |
private static function format_post_dates( $current_date, $date_format ) { |
| 297 |
return array( |
| 298 |
'default' => date_i18n( $date_format, strtotime( $current_date ) ), |
| 299 |
'day' => date_i18n( 'j', strtotime( $current_date ) ), |
| 300 |
'month' => date_i18n( 'F', strtotime( $current_date ) ), |
| 301 |
'year' => date_i18n( 'Y', strtotime( $current_date ) ), |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Get all terms of a single post. |
| 307 |
* |
| 308 |
* @param int $post_id Post ID.. |
| 309 |
* @param string $taxonomy_type Taxonomy Type. |
| 310 |
* @return string Terms markup. |
| 311 |
*/ |
| 312 |
private static function get_all_terms( $post_id, $taxonomy_type ) { |
| 313 |
if ( empty( $taxonomy_type ) ) { |
| 314 |
return ''; |
| 315 |
} |
| 316 |
$all_taxonomies = ''; |
| 317 |
$terms = get_the_terms( $post_id, $taxonomy_type ); |
| 318 |
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
| 319 |
$all_taxonomies .= '<ul class="post-categories">'; |
| 320 |
foreach ( $terms as $term ) { |
| 321 |
if ( empty( $term ) || is_wp_error( get_term_link( $term ) ) ) { |
| 322 |
continue; } |
| 323 |
$all_taxonomies .= '<li><a href="' . esc_url( get_term_link( $term ) ) . '" rel="noreferrer noopener" data-slug="' . esc_attr( $term->slug ) . '">' . esc_html( $term->name ) . '</a></li>'; |
| 324 |
} |
| 325 |
$all_taxonomies .= '</ul>'; |
| 326 |
} |
| 327 |
return $all_taxonomies; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Get WooCommerce product data. |
| 332 |
* |
| 333 |
* @param int $post_id Post ID. |
| 334 |
* @return array Product data. |
| 335 |
*/ |
| 336 |
private static function get_product_data( $post_id ) { |
| 337 |
$product = wc_get_product( $post_id ); |
| 338 |
|
| 339 |
if ( ! $product ) { |
| 340 |
return array(); |
| 341 |
} |
| 342 |
|
| 343 |
return array( |
| 344 |
'product_price' => $product->get_price_html(), |
| 345 |
'add_to_cart' => do_shortcode( '[add_to_cart id="' . $post_id . '" show_price="false" style="none"]' ), |
| 346 |
'average_rating' => $product->get_average_rating(), |
| 347 |
'review_count' => $product->get_review_count(), |
| 348 |
'on_sale' => $product->is_on_sale(), |
| 349 |
); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Ajax query function - refactored version. |
| 354 |
* |
| 355 |
* @return void |
| 356 |
*/ |
| 357 |
public function sp_smart_post_block_post_query() { |
| 358 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 359 |
|
| 360 |
if ( ! wp_verify_nonce( $nonce, 'sp_smart_post_block_nonce' ) ) { |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
$query_data = isset( $_POST['queryData'] ) ? sanitize_text_field( wp_unslash( $_POST['queryData'] ) ) : ''; |
| 365 |
$query_data = (array) json_decode( $query_data ); |
| 366 |
|
| 367 |
$config = self::prepare_ajax_config( $query_data ); |
| 368 |
$post_query = \SmartPostShow\Blocks\PostQueryHandler::query( $query_data ); |
| 369 |
|
| 370 |
$the_query = $post_query['the_query']; |
| 371 |
|
| 372 |
$post_data = self::process_query_posts( $the_query, $config ); |
| 373 |
$post_count = self::calculate_post_count( $config, $post_query ); |
| 374 |
|
| 375 |
wp_send_json( |
| 376 |
array( |
| 377 |
'posts' => $post_data, |
| 378 |
'posts_status' => count( $post_data ) > 0, |
| 379 |
'post_count' => $post_count, |
| 380 |
) |
| 381 |
); |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Prepare configuration for AJAX request. |
| 386 |
* |
| 387 |
* @param array $query_data Query data. |
| 388 |
* @return array Configuration array. |
| 389 |
*/ |
| 390 |
private static function prepare_ajax_config( $query_data ) { |
| 391 |
$config = array( |
| 392 |
'post_type' => $query_data['postType'] ?? 'post', |
| 393 |
'date_format_type' => $query_data['metaDateFormat'] ?? 'default', |
| 394 |
'image_size' => $query_data['imageSize'] ?? '', |
| 395 |
'custom_date_format' => $query_data['metaDateCustomDateFormat'] ?? '', |
| 396 |
'enable_seo_description' => $query_data['seoMetaShow'] ?? false, |
| 397 |
'active_seo_plugin' => $query_data['activeSeoPlugin'] ?? '', |
| 398 |
'exclude_post_without_image' => $query_data['excludePostWithoutImagePosts'] ?? false, |
| 399 |
'taxonomy_type' => $query_data['catTabCategoryType'] ?? '', |
| 400 |
); |
| 401 |
|
| 402 |
$config['date_format'] = self::get_date_format( $config['date_format_type'], $config['custom_date_format'] ); |
| 403 |
$config['multiple_post_type'] = self::prepare_ajax_multiple_post_types( $config['post_type'], $query_data ); |
| 404 |
|
| 405 |
return $config; |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Prepare multiple post types for AJAX request. |
| 410 |
* |
| 411 |
* @param string $current_post_type Current post type. |
| 412 |
* @param array $query_data Query data. |
| 413 |
* @return array Multiple post types. |
| 414 |
*/ |
| 415 |
private static function prepare_ajax_multiple_post_types( $current_post_type, $query_data ) { |
| 416 |
if ( 'multiple_post_type' !== $current_post_type || empty( $query_data['multiplePostType'] ) ) { |
| 417 |
return array( 'post' ); |
| 418 |
} |
| 419 |
|
| 420 |
return array_map( |
| 421 |
function ( $multi_type ) { |
| 422 |
return $multi_type->value; |
| 423 |
}, |
| 424 |
$query_data['multiplePostType'] |
| 425 |
); |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Calculate total post count. |
| 430 |
* |
| 431 |
* @param array $config Configuration array. |
| 432 |
* @param array $post_query Post query results. |
| 433 |
* @return int Post count. |
| 434 |
*/ |
| 435 |
private static function calculate_post_count( $config, $post_query ) { |
| 436 |
if ( isset( $post_query['post_count'] ) && $post_query['post_count'] ) { |
| 437 |
return $post_query['post_count']; |
| 438 |
} |
| 439 |
|
| 440 |
if ( 'multiple_post_type' === $config['post_type'] && ! empty( $config['multiple_post_type'] ) ) { |
| 441 |
$post_count = 0; |
| 442 |
foreach ( $config['multiple_post_type'] as $post_type ) { |
| 443 |
$counts = wp_count_posts( $post_type ); |
| 444 |
$post_count += isset( $counts->publish ) ? (int) $counts->publish : 0; |
| 445 |
} |
| 446 |
return $post_count; |
| 447 |
} |
| 448 |
|
| 449 |
$counts = wp_count_posts( $config['post_type'] ); |
| 450 |
return isset( $counts->publish ) ? (int) $counts->publish : 0; |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Method sp_smart_post_block_all_post_query |
| 455 |
* |
| 456 |
* @return void |
| 457 |
*/ |
| 458 |
public function sp_smart_post_block_all_post_query() { |
| 459 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 460 |
|
| 461 |
if ( ! wp_verify_nonce( $nonce, 'sp_smart_post_block_nonce' ) ) { |
| 462 |
return; |
| 463 |
} |
| 464 |
|
| 465 |
$query_data = isset( $_POST['postQueryData'] ) ? sanitize_text_field( wp_unslash( $_POST['postQueryData'] ) ) : ''; |
| 466 |
|
| 467 |
$query_data = (array) json_decode( $query_data ); |
| 468 |
// queries. |
| 469 |
$post_type = isset( $query_data['postType'] ) ? $query_data['postType'] : 'post'; |
| 470 |
$search_text = isset( $query_data['liveSearchText'] ) ? $query_data['liveSearchText'] : ''; |
| 471 |
|
| 472 |
$multiple_post_type = ( 'multiple_post_type' === $post_type && isset( $query_data['multiplePostType'] ) ) ? $query_data['multiplePostType'] : array(); |
| 473 |
$multiple_post_type = ( 'multiple_post_type' === $post_type && ! empty( $multiple_post_type ) ) ? array_map( |
| 474 |
function ( $multi_type ) { |
| 475 |
return $multi_type->value; |
| 476 |
}, |
| 477 |
$multiple_post_type |
| 478 |
) : array( 'post' ); |
| 479 |
|
| 480 |
$post_query_args = \SmartPostShow\Blocks\PostQueryHandler::query( $query_data, 'args' )['args']; |
| 481 |
$post_query_args['fields'] = 'ids'; |
| 482 |
$post_query_args['posts_per_page'] = 99999; |
| 483 |
|
| 484 |
$post_lists = get_posts( $post_query_args ); |
| 485 |
|
| 486 |
$the_search_query = new \WP_Query( |
| 487 |
array( |
| 488 |
'post_type' => ( 'multiple_post_type' === $post_type && ! empty( $multiple_post_type ) ) ? $multiple_post_type : $post_type, |
| 489 |
'posts_per_page' => 20, |
| 490 |
's' => $search_text, |
| 491 |
'post__in' => $post_lists, |
| 492 |
) |
| 493 |
); |
| 494 |
|
| 495 |
wp_send_json( |
| 496 |
array( |
| 497 |
'posts' => count( $post_lists ) > 0 ? $the_search_query->posts : array(), |
| 498 |
) |
| 499 |
); |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Method sp_smart_post_block_meta_data_query |
| 504 |
* |
| 505 |
* @return void |
| 506 |
*/ |
| 507 |
public function sp_smart_post_block_meta_data_query() { |
| 508 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 509 |
|
| 510 |
if ( ! wp_verify_nonce( $nonce, 'sp_smart_post_block_nonce' ) ) { |
| 511 |
return; |
| 512 |
} |
| 513 |
|
| 514 |
$query_data = isset( $_POST['metaQueryData'] ) ? sanitize_text_field( wp_unslash( $_POST['metaQueryData'] ) ) : ''; |
| 515 |
$query_data = (array) json_decode( $query_data ); |
| 516 |
// query data. |
| 517 |
$post_type = isset( $query_data['postType'] ) ? $query_data['postType'] : 'post'; |
| 518 |
|
| 519 |
$ajax_live_filter = isset( $query_data['ajaxLiveFilter'] ) ? (array) $query_data['ajaxLiveFilter'] : array(); |
| 520 |
|
| 521 |
$current_page = isset( $query_data['currentPage'] ) ? $query_data['currentPage'] : 1; |
| 522 |
|
| 523 |
$edit_site = isset( $query_data['editSite'] ) ? $query_data['editSite'] : ''; |
| 524 |
|
| 525 |
$block_name = isset( $query_data['blockName'] ) ? $query_data['blockName'] : 'post-carousel'; |
| 526 |
|
| 527 |
$keyword_search = isset( $query_data['keywordSearch'] ) ? $query_data['keywordSearch'] : ''; |
| 528 |
|
| 529 |
$taxonomies_live_filter = isset( $query_data['taxonomiesLiveFilter'] ) ? $query_data['taxonomiesLiveFilter'] : array(); |
| 530 |
$author_live_filter = isset( $query_data['authorLiveFilter'] ) ? $query_data['authorLiveFilter'] : array(); |
| 531 |
|
| 532 |
$multiple_post_type = ( 'multiple_post_type' === $post_type && isset( $query_data['multiplePostType'] ) ) ? $query_data |
| 533 |
['multiplePostType'] : array(); |
| 534 |
|
| 535 |
$multiple_post_type = ( 'multiple_post_type' === $post_type && ! empty( $multiple_post_type ) ) ? array_map( |
| 536 |
function ( $multi_type ) { |
| 537 |
return $multi_type->value; |
| 538 |
}, |
| 539 |
$multiple_post_type |
| 540 |
) : array( 'post' ); |
| 541 |
|
| 542 |
$multiple_post_type = ( 'multiple_post_type' === $post_type && ! empty( $multiple_post_type ) ) ? $multiple_post_type : $post_type; |
| 543 |
|
| 544 |
if ( ! empty( $taxonomies_live_filter ) || ! empty( $author_live_filter ) ) { |
| 545 |
$post_query_args = self::query( $query_data, 'args' ); |
| 546 |
|
| 547 |
$post_limit = $post_query_args['post_limit']; |
| 548 |
$post_in = $post_query_args['include_posts']; |
| 549 |
|
| 550 |
$post_query_args = $post_query_args['args']; |
| 551 |
$post_query_args['posts_per_page'] = $post_limit; |
| 552 |
$post_query_args['fields'] = 'ids'; |
| 553 |
|
| 554 |
if ( ! empty( $keyword_search ) ) { |
| 555 |
$post_query_args['s'] = $keyword_search; |
| 556 |
} |
| 557 |
|
| 558 |
$post_query_args['offset'] = 0; |
| 559 |
|
| 560 |
$term_query_args = $post_query_args; |
| 561 |
$author_query_args = $post_query_args; |
| 562 |
$post_lists = get_posts( $post_query_args ); |
| 563 |
$all_post_ids = $post_in ? $post_in : $post_lists; |
| 564 |
|
| 565 |
$ajax_live_filter_keys = array(); |
| 566 |
|
| 567 |
foreach ( $ajax_live_filter as $key => $value ) { |
| 568 |
switch ( $value->type ) { |
| 569 |
case 'taxonomy': |
| 570 |
if ( 'all' !== $value->id ) { |
| 571 |
$ajax_live_filter_keys[] = $value->taxonomy_type; |
| 572 |
} |
| 573 |
break; |
| 574 |
case 'author': |
| 575 |
if ( 'all' !== $value->id ) { |
| 576 |
$ajax_live_filter_keys[] = $key; |
| 577 |
} |
| 578 |
break; |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
// taxonomy. |
| 584 |
$all_taxonomies = get_object_taxonomies( $multiple_post_type, 'objects' ); |
| 585 |
$taxonomy_list = array(); |
| 586 |
|
| 587 |
foreach ( $all_taxonomies as $taxonomy ) { |
| 588 |
$terms = get_terms( |
| 589 |
array( |
| 590 |
'taxonomy' => $taxonomy->name, |
| 591 |
'hide_empty' => false, |
| 592 |
) |
| 593 |
); |
| 594 |
|
| 595 |
// Prepare the list of terms for this taxonomy. |
| 596 |
$term_ajax_filter = array(); |
| 597 |
$term_items = array(); |
| 598 |
$index = 0; |
| 599 |
|
| 600 |
foreach ( $terms as $term ) { |
| 601 |
|
| 602 |
$get_term_posts_ids = array( |
| 603 |
array( |
| 604 |
'taxonomy' => $term->taxonomy, |
| 605 |
'field' => 'term_id', |
| 606 |
'terms' => array( $term->term_id ), |
| 607 |
), |
| 608 |
); |
| 609 |
|
| 610 |
if ( ! empty( $taxonomies_live_filter ) ) { |
| 611 |
$term_query_args['tax_query'] = $get_term_posts_ids; |
| 612 |
|
| 613 |
$term_query_args['posts_per_page'] = 9999; |
| 614 |
|
| 615 |
$term_ids = get_posts( $term_query_args ); |
| 616 |
|
| 617 |
$term_ids = array_intersect( $all_post_ids, $term_ids ); |
| 618 |
|
| 619 |
if ( ( ! empty( $ajax_live_filter_keys ) && $ajax_live_filter_keys[0] !== $term->taxonomy ) || count( $ajax_live_filter_keys ) > 1 ) { |
| 620 |
$term_ids = array_intersect( $post_lists, $term_ids ); |
| 621 |
} |
| 622 |
|
| 623 |
$child_terms = get_term_children( $term->term_id, $taxonomy->name ); |
| 624 |
|
| 625 |
$total_post_count = count( $term_ids ); |
| 626 |
|
| 627 |
if ( is_wp_error( $child_terms ) && count( $child_terms ) > 0 ) { |
| 628 |
foreach ( $child_terms as $child_term_id ) { |
| 629 |
$child_term = get_term( $child_term_id, $taxonomy->name ); |
| 630 |
$total_post_count += $child_term->count; |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
$term_args = array( |
| 636 |
'id' => $index, |
| 637 |
'label' => $term->name, |
| 638 |
'slug' => $term->slug, |
| 639 |
'value' => $term->term_id, |
| 640 |
'type' => 'taxonomy', |
| 641 |
'taxonomy_type' => $term->taxonomy, |
| 642 |
); |
| 643 |
|
| 644 |
$term_items[] = $term_args; |
| 645 |
|
| 646 |
if ( ! empty( $taxonomies_live_filter ) && $total_post_count ) { |
| 647 |
$term_args['post_count'] = $total_post_count; |
| 648 |
$term_ajax_filter[] = $term_args; |
| 649 |
} |
| 650 |
++$index; |
| 651 |
} |
| 652 |
|
| 653 |
$taxonomy_list[] = array( |
| 654 |
'label' => $taxonomy->label, |
| 655 |
'name' => $taxonomy->name, |
| 656 |
'terms' => $term_ajax_filter, |
| 657 |
'terms_items' => $term_items, |
| 658 |
); |
| 659 |
} |
| 660 |
|
| 661 |
// authors details. |
| 662 |
$authors = get_users( array( 'role__in' => array( 'author', 'administrator', 'editor' ) ) ); |
| 663 |
|
| 664 |
$filtered_authors = array(); |
| 665 |
$author_list = array(); |
| 666 |
$author_post_ids = array(); |
| 667 |
|
| 668 |
foreach ( $authors as $author ) { |
| 669 |
|
| 670 |
if ( ! empty( $author_live_filter ) && ! empty( $author_query_args ) ) { |
| 671 |
unset( $author_query_args['author__not_in'] ); |
| 672 |
$author_query_args['author__in'] = array( $author->ID ); |
| 673 |
$author_query_args['fields'] = 'ids'; |
| 674 |
$author_post_ids = get_posts( $author_query_args ); |
| 675 |
$author_post_ids = array_intersect( $all_post_ids, $author_post_ids ); |
| 676 |
} |
| 677 |
|
| 678 |
$author_args = array( |
| 679 |
'id' => $author->ID, |
| 680 |
'role' => $author->roles, |
| 681 |
'type' => 'author', |
| 682 |
'value' => $author->ID, |
| 683 |
'label' => $author->display_name, |
| 684 |
); |
| 685 |
|
| 686 |
$author_list[] = $author_args; |
| 687 |
|
| 688 |
if ( ! empty( $author_live_filter ) && count( $author_post_ids ) > 0 ) { |
| 689 |
$author_args['post_count'] = count( $author_post_ids ); |
| 690 |
$filtered_authors[] = $author_args; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
// all post type list. |
| 695 |
$all_post_type_list = get_post_types( |
| 696 |
array( |
| 697 |
'public' => true, |
| 698 |
) |
| 699 |
); |
| 700 |
|
| 701 |
// query all meta fields. |
| 702 |
$meta_field_list = array(); |
| 703 |
if ( 'editSite' === $edit_site ) { |
| 704 |
global $wpdb; |
| 705 |
$keys = $wpdb->get_col( |
| 706 |
"SELECT meta_key |
| 707 |
FROM $wpdb->postmeta |
| 708 |
GROUP BY meta_key |
| 709 |
ORDER BY meta_key" |
| 710 |
); |
| 711 |
if ( $keys ) { |
| 712 |
natcasesort( $keys ); |
| 713 |
} |
| 714 |
// Remove empty custom field. |
| 715 |
$keys = array_filter( $keys ); |
| 716 |
foreach ( $keys as $key ) { |
| 717 |
/** |
| 718 |
* Don't hide protected meta fields, to able to select data of The Events Calendar... |
| 719 |
* |
| 720 |
* @since 2.0.0 |
| 721 |
* if ( is_protected_meta( $key, 'post' ) ) { |
| 722 |
* continue; |
| 723 |
* } |
| 724 |
*/ |
| 725 |
$meta_field_list[ esc_attr( $key ) ] = esc_html( $key ); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
$post_count = 0; |
| 730 |
if ( 'multiple_post_type' === $post_type && ! is_string( $multiple_post_type ) ) { |
| 731 |
foreach ( $multiple_post_type as $value ) { |
| 732 |
$post_count += isset( wp_count_posts( $value )->publish ) ? (int) wp_count_posts( $value )->publish : 0; |
| 733 |
} |
| 734 |
} else { |
| 735 |
$post_count = isset( wp_count_posts( $post_type )->publish ) ? (int) wp_count_posts( $post_type )->publish : 0; |
| 736 |
} |
| 737 |
|
| 738 |
wp_send_json( |
| 739 |
array( |
| 740 |
'taxonomies' => $taxonomy_list, |
| 741 |
'authors' => $filtered_authors, |
| 742 |
'author_list' => $author_list, |
| 743 |
'post_count' => $post_count, |
| 744 |
'image_sizes' => get_intermediate_image_sizes(), |
| 745 |
'all_post_type_list' => $all_post_type_list, |
| 746 |
'all_meta_field_list' => $meta_field_list, |
| 747 |
) |
| 748 |
); |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Remove srcset attribute from image attributes. |
| 753 |
* |
| 754 |
* @param array $attr Image attributes. |
| 755 |
* @param WP_Post $attachment Attachment post object. |
| 756 |
* @param string $size Image size. |
| 757 |
* @return array Modified image attributes. |
| 758 |
*/ |
| 759 |
public static function remove_image_srcset( $attr, $attachment, $size ) { |
| 760 |
unset( $attr['srcset'] ); |
| 761 |
return $attr; |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|