ACFV1.php
8 months ago
ElImport.php
8 months ago
FrontEndFilterV1.php
8 months ago
GetBuilderData.php
8 months ago
GetCategories.php
8 months ago
GetPostsV1.php
8 months ago
GetTermObject.php
8 months ago
GetTickerPostsV1.php
8 months ago
ImageSizeV1.php
8 months ago
RestApi.php
8 months ago
GetPostsV1.php
413 lines
| 1 | <?php |
| 2 | |
| 3 | namespace RT\ThePostGrid\Controllers\Api; |
| 4 | |
| 5 | use RT\ThePostGrid\Helpers\Fns; |
| 6 | use WP_Query; |
| 7 | |
| 8 | class GetPostsV1 { |
| 9 | public function __construct() { |
| 10 | add_action( 'rest_api_init', [ $this, 'register_post_route' ] ); |
| 11 | } |
| 12 | |
| 13 | public function register_post_route() { |
| 14 | register_rest_route( |
| 15 | 'rttpg/v1', |
| 16 | 'query', |
| 17 | [ |
| 18 | 'methods' => 'POST', |
| 19 | 'callback' => [ $this, 'get_all_posts' ], |
| 20 | 'permission_callback' => function () { |
| 21 | return current_user_can( 'edit_posts' ); |
| 22 | }, |
| 23 | ] |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | public function get_all_posts( $data ) { |
| 29 | |
| 30 | |
| 31 | $prefix = isset( $data['prefix'] ) ? $data['prefix'] : 'grid'; |
| 32 | |
| 33 | $_post_type = ! empty( $data['post_type'] ) ? esc_html( $data['post_type'] ) : 'post'; |
| 34 | $_post_types = ! empty( $data['post_types'] ) ? Fns::escape_array( $data['post_types'] ) : [ 'post' ]; |
| 35 | |
| 36 | if ( rtTPG()->hasPro() && 'yes' === $data['multiple_post_type'] ) { |
| 37 | $post_type = Fns::available_post_types( $_post_types, true ); |
| 38 | } else { |
| 39 | $post_type = Fns::available_post_type( $_post_type ); |
| 40 | } |
| 41 | |
| 42 | $args = [ |
| 43 | 'post_type' => $post_type, |
| 44 | 'post_status' => 'publish', |
| 45 | ]; |
| 46 | |
| 47 | $excluded_ids = null; |
| 48 | |
| 49 | if ( $data['post_id'] ) { |
| 50 | $post_ids = explode( ',', $data['post_id'] ); |
| 51 | $post_ids = array_map( 'trim', $post_ids ); |
| 52 | |
| 53 | $args['post__in'] = $post_ids; |
| 54 | |
| 55 | if ( $excluded_ids != null && is_array( $excluded_ids ) ) { |
| 56 | $args['post__in'] = array_diff( $post_ids, $excluded_ids ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if ( $prefix !== 'slider' && 'show' === $data['show_pagination'] ) { |
| 61 | $_paged = is_front_page() ? 'page' : 'paged'; |
| 62 | $args['paged'] = get_query_var( $_paged ) ? absint( get_query_var( $_paged ) ) : absint( $data['page'] ); |
| 63 | } |
| 64 | |
| 65 | if ( rtTPG()->hasPro() && 'yes' == $data['ignore_sticky_posts'] ) { |
| 66 | $args['ignore_sticky_posts'] = 1; |
| 67 | } |
| 68 | |
| 69 | if ( $orderby = $data['orderby'] ) { |
| 70 | if ( ! rtTPG()->hasPro() && 'rand' == $orderby ) { |
| 71 | $orderby = 'date'; |
| 72 | } |
| 73 | $args['orderby'] = $orderby; |
| 74 | } |
| 75 | |
| 76 | if ( $data['order'] ) { |
| 77 | $args['order'] = $data['order']; |
| 78 | } |
| 79 | |
| 80 | if ( $data['instant_query'] ) { |
| 81 | $args = Fns::get_instant_query( $data['instant_query'], $args ); |
| 82 | } |
| 83 | |
| 84 | if ( $data['author'] ) { |
| 85 | $args['author__in'] = $data['author']; |
| 86 | } |
| 87 | |
| 88 | if ( rtTPG()->hasPro() && ( $data['start_date'] || $data['end_date'] ) ) { |
| 89 | $args['date_query'] = [ |
| 90 | [ |
| 91 | 'after' => trim( $data['start_date'] ), |
| 92 | 'before' => trim( $data['end_date'] ), |
| 93 | 'inclusive' => true, |
| 94 | ], |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | // Taxonomy should implement after |
| 99 | $_taxonomies = get_object_taxonomies( $data['post_type'], 'objects' ); |
| 100 | $_taxonomy_list = $data['taxonomy_lists']; |
| 101 | $filtered_taxonomy_lists = []; |
| 102 | |
| 103 | foreach ( $_taxonomies as $index => $object ) { |
| 104 | if ( in_array( $object->name, Fns::get_excluded_taxonomy() ) ) { |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | $filtered_taxonomy_lists[ $object->name ] = isset( $_taxonomy_list[ $object->name ] ) ? $_taxonomy_list[ $object->name ]['options'] : null; |
| 109 | $_term_list = isset( $_taxonomy_list[ $object->name ] ) ? wp_list_pluck( $_taxonomy_list[ $object->name ]['options'], 'value' ) : null; |
| 110 | if ( ! empty( $_term_list ) ) { |
| 111 | //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 112 | $args['tax_query'][] = [ |
| 113 | 'taxonomy' => $object->name, |
| 114 | 'field' => 'term_id', |
| 115 | 'terms' => $_term_list, |
| 116 | ]; |
| 117 | } |
| 118 | if ( ! empty( $args['tax_query'] ) && $data['relation'] ) { |
| 119 | $args['tax_query']['relation'] = $data['relation']; //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if ( $data['post_keyword'] ) { |
| 124 | $args['s'] = $data['post_keyword']; |
| 125 | } |
| 126 | |
| 127 | $excluded_ids = []; |
| 128 | $offset_posts = []; |
| 129 | if ( $data['exclude'] || $data['offset'] ) { |
| 130 | if ( $data['exclude'] ) { |
| 131 | $excluded_ids = explode( ',', $data['exclude'] ); |
| 132 | $excluded_ids = array_map( 'trim', $excluded_ids ); |
| 133 | } |
| 134 | |
| 135 | if ( $data['offset'] ) { |
| 136 | $_temp_args = $args; |
| 137 | unset( $_temp_args['paged'] ); |
| 138 | $_temp_args['posts_per_page'] = $data['offset']; |
| 139 | $_temp_args['fields'] = 'ids'; |
| 140 | |
| 141 | $offset_posts = get_posts( $_temp_args ); |
| 142 | } |
| 143 | |
| 144 | $excluded_post_ids = array_merge( $offset_posts, $excluded_ids ); |
| 145 | $args['post__not_in'] = array_unique( $excluded_post_ids ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in |
| 146 | } |
| 147 | |
| 148 | if ( $prefix !== 'slider' ) { |
| 149 | if ( $data['post_limit'] ) { |
| 150 | if ( 'show' !== $data['show_pagination'] ) { |
| 151 | $args['posts_per_page'] = $data['post_limit']; |
| 152 | } else { |
| 153 | $tempArgs = $args; |
| 154 | $tempArgs['posts_per_page'] = $data['post_limit']; |
| 155 | $tempArgs['paged'] = 1; |
| 156 | $tempArgs['fields'] = 'ids'; |
| 157 | if ( ! empty( $offset_posts ) ) { |
| 158 | $tempArgs['post__not_in'] = $offset_posts; //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in |
| 159 | } |
| 160 | $tempQ = new WP_Query( $tempArgs ); |
| 161 | if ( ! empty( $tempQ->posts ) ) { |
| 162 | $args['post__in'] = $tempQ->posts; |
| 163 | $args['posts_per_page'] = $data['post_limit']; |
| 164 | } |
| 165 | } |
| 166 | } else { |
| 167 | $_posts_per_page = 9; |
| 168 | if ( 'grid' === $prefix ) { |
| 169 | if ( $data['grid_layout'] == 'grid-layout5' ) { |
| 170 | $_posts_per_page = 5; |
| 171 | } elseif ( in_array( $data['grid_layout'], [ 'grid-layout6', 'grid-layout6-2' ] ) ) { |
| 172 | $_posts_per_page = 3; |
| 173 | } elseif ( in_array( $data['grid_layout'], [ 'grid-layout5', 'grid-layout5-2' ] ) ) { |
| 174 | $_posts_per_page = 5; |
| 175 | } |
| 176 | } elseif ( 'list' === $prefix ) { |
| 177 | if ( in_array( $data['list_layout'], [ 'list-layout2', 'list-layout2-2' ] ) ) { |
| 178 | $_posts_per_page = 7; |
| 179 | } elseif ( in_array( $data['list_layout'], [ 'list-layout3', 'list-layout3-2' ] ) ) { |
| 180 | $_posts_per_page = 5; |
| 181 | } |
| 182 | } elseif ( 'grid_hover' === $prefix ) { |
| 183 | if ( in_array( $data['grid_hover_layout'], [ 'grid_hover-layout4', 'grid_hover-layout4-2' ] ) ) { |
| 184 | $_posts_per_page = 7; |
| 185 | } elseif ( in_array( |
| 186 | $data['grid_hover_layout'], |
| 187 | [ |
| 188 | 'grid_hover-layout5', |
| 189 | 'grid_hover-layout5-2', |
| 190 | ] |
| 191 | ) ) { |
| 192 | $_posts_per_page = 3; |
| 193 | } elseif ( in_array( |
| 194 | $data['grid_hover_layout'], |
| 195 | [ |
| 196 | 'grid_hover-layout6', |
| 197 | 'grid_hover-layout6-2', |
| 198 | 'grid_hover-layout9', |
| 199 | 'grid_hover-layout9-2', |
| 200 | 'grid_hover-layout10', |
| 201 | 'grid_hover-layout11', |
| 202 | ] |
| 203 | ) |
| 204 | ) { |
| 205 | $_posts_per_page = 4; |
| 206 | } elseif ( in_array( |
| 207 | $data['grid_hover_layout'], |
| 208 | [ |
| 209 | 'grid_hover-layout7', |
| 210 | 'grid_hover-layout7-2', |
| 211 | 'grid_hover-layout8', |
| 212 | ] |
| 213 | ) ) { |
| 214 | $_posts_per_page = 5; |
| 215 | } elseif ( in_array( |
| 216 | $data['grid_hover_layout'], |
| 217 | [ |
| 218 | 'grid_hover-layout6', |
| 219 | 'grid_hover-layout6-2', |
| 220 | ] |
| 221 | ) ) { |
| 222 | $_posts_per_page = 4; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | $args['posts_per_page'] = $_posts_per_page; |
| 227 | } |
| 228 | |
| 229 | if ( isset( $data['display_per_page'] ) && $data['display_per_page'] > 0 ) { |
| 230 | $args['posts_per_page'] = $data['display_per_page']; |
| 231 | } |
| 232 | } else { |
| 233 | $slider_per_page = $data['post_limit']; |
| 234 | if ( $data['slider_layout'] == 'slider-layout10' ) { |
| 235 | $slider_reminder = ( intval( $data['post_limit'], 10 ) % 5 ); |
| 236 | if ( $slider_reminder ) { |
| 237 | $slider_per_page = ( $data['post_limit'] - $slider_reminder + 5 ); |
| 238 | } |
| 239 | } |
| 240 | $args['posts_per_page'] = $slider_per_page; |
| 241 | } |
| 242 | |
| 243 | $_all_taxs = wp_list_pluck( $_taxonomies, 'label', 'name' ); |
| 244 | |
| 245 | $post_tax_list = array_filter( |
| 246 | $_all_taxs, |
| 247 | function ( $item ) { |
| 248 | return ( ! in_array( |
| 249 | $item, |
| 250 | [ |
| 251 | 'post_format', |
| 252 | 'elementor_library_type', |
| 253 | 'product_visibility', |
| 254 | 'product_shipping_class', |
| 255 | ], |
| 256 | true |
| 257 | ) ); |
| 258 | }, |
| 259 | ARRAY_FILTER_USE_KEY |
| 260 | ); |
| 261 | |
| 262 | if ( ! empty( $data['is_builder'] ) && $data['is_builder'] === 'yes' ) { |
| 263 | $args['posts_per_page'] = get_option( 'posts_per_page' ); |
| 264 | } |
| 265 | |
| 266 | $query = new WP_Query( $args ); |
| 267 | |
| 268 | $post_layout = $data[ $prefix . '_layout' ]; |
| 269 | |
| 270 | $send_data = [ |
| 271 | 'posts' => [], |
| 272 | 'total_post' => $query->found_posts, |
| 273 | 'query_args' => $args, |
| 274 | 'query_info' => [ |
| 275 | 'prefix' => $prefix, |
| 276 | 'layout' => $post_layout, |
| 277 | 'taxonomy' => $post_tax_list, |
| 278 | ], |
| 279 | ]; |
| 280 | |
| 281 | |
| 282 | // $send_data['total_post'] = esc_html( $query->found_posts ); |
| 283 | if ( $query->have_posts() ) { |
| 284 | $pCount = 1; |
| 285 | while ( $query->have_posts() ) { |
| 286 | $query->the_post(); |
| 287 | $id = get_the_id(); |
| 288 | $post_count = $query->post_count; |
| 289 | set_query_var( 'tpg_post_count', $pCount ); |
| 290 | set_query_var( 'tpg_total_posts', $post_count ); |
| 291 | global $post; |
| 292 | |
| 293 | $cf_group = isset( $data['acf_data_lists'][ $data['post_type'] . '_cf_group' ] ) ? $data['acf_data_lists'][ $data['post_type'] . '_cf_group' ]['options'] : []; |
| 294 | $cf_group_collection = wp_list_pluck( $cf_group, 'value' ); |
| 295 | |
| 296 | $acfArgs = [ |
| 297 | 'is_guten' => 'yes', |
| 298 | 'show_acf' => $data['show_acf'], |
| 299 | 'cf_hide_empty_value' => $data['cf_hide_empty_value'], |
| 300 | 'cf_show_only_value' => $data['cf_show_only_value'], |
| 301 | 'cf_hide_group_title' => $data['cf_hide_group_title'], |
| 302 | 'cf_group' => $cf_group_collection, |
| 303 | ]; |
| 304 | |
| 305 | $acf_data = Fns::tpg_get_acf_data_elementor( $acfArgs, $id, false ); |
| 306 | |
| 307 | // First image from the post |
| 308 | preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_the_content(), $matches ); |
| 309 | if ( empty( $first_img ) ) { // Defines a default image |
| 310 | $first_img = RT_THE_POST_GRID_PLUGIN_PATH . '/images/default.png'; |
| 311 | } else { |
| 312 | $first_img = ! empty( $matches[1][0] ) ? $matches[1][0] : ''; |
| 313 | } |
| 314 | |
| 315 | $category_terms_list = get_the_terms( $id, $data['category_source'] ? $data['category_source'] : 'category' ); |
| 316 | $tags_terms_list = get_the_terms( $id, $data['tag_source'] ? $data['tag_source'] : 'post_tag' ); |
| 317 | |
| 318 | $category_terms = wp_list_pluck( $category_terms_list, 'name' ); |
| 319 | $tags_terms = wp_list_pluck( $tags_terms_list, 'name' ); |
| 320 | |
| 321 | // Working Here 13-12-22 |
| 322 | |
| 323 | $_cat_bg_meta = []; |
| 324 | if ( $data['post_type'] == 'post' ) { |
| 325 | $_category_list_term = wp_list_pluck( $category_terms_list, 'term_id' ); |
| 326 | foreach ( $_category_list_term as $item_id ) { |
| 327 | $meta_color = get_term_meta( $item_id, 'rttpg_category_color', true ); |
| 328 | $_cat_bg_meta[] = $meta_color ? "#{$meta_color}" : ''; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | $img_url = esc_url_raw( get_the_post_thumbnail_url( $id, $data['image_size'] ) ); |
| 333 | $img_offset_url = ''; |
| 334 | if ( isset( $data['image_offset_size'] ) && $data['image_offset_size'] ) { |
| 335 | $img_offset_url = esc_url_raw( get_the_post_thumbnail_url( $id, $data['image_offset_size'] ) ); |
| 336 | } |
| 337 | |
| 338 | if ( 'first_image' === $data['media_source'] ) { |
| 339 | $img_url = Fns::get_content_first_image( $id, 'url' ); |
| 340 | } |
| 341 | |
| 342 | if ( isset( $data['default_image']['id'] ) && ! $img_url ) { |
| 343 | $img_size_key = 'image_size'; |
| 344 | $default_img_url = wp_get_attachment_image_src( $data['default_image']['id'], $data[ $img_size_key ] ); |
| 345 | $img_url = $default_img_url[0]; |
| 346 | } |
| 347 | |
| 348 | $excerpt_args = [ |
| 349 | 'excerpt_type' => $data['excerpt_type'], |
| 350 | 'excerpt_limit' => $data['excerpt_limit'], |
| 351 | 'excerpt_more_text' => $data['excerpt_more_text'], |
| 352 | ]; |
| 353 | |
| 354 | $exerpt = Fns::get_the_excerpt( $id, $excerpt_args ); |
| 355 | |
| 356 | $author_id = get_the_author_meta( 'ID' ); |
| 357 | $author_avatar_url = get_avatar_url( $author_id ); |
| 358 | |
| 359 | $count_key = Fns::get_post_view_count_meta_key(); |
| 360 | $get_view_count = get_post_meta( $id, $count_key, true ); |
| 361 | |
| 362 | $post_data = [ |
| 363 | 'author_name' => esc_html( get_the_author_meta( 'display_name', $author_id ) ), |
| 364 | 'avatar_url' => esc_url( $author_avatar_url ), |
| 365 | 'comment_count' => esc_html( get_comments_number( $id ) ), |
| 366 | 'content' => get_the_content(), |
| 367 | 'category' => ! empty( $category_terms ) ? $category_terms : [], |
| 368 | 'category_bg' => ! empty( $_cat_bg_meta ) ? $_cat_bg_meta : [], |
| 369 | 'tags' => ! empty( $tags_terms ) ? $tags_terms : '', |
| 370 | 'excerpt' => $exerpt, |
| 371 | 'id' => $id, |
| 372 | 'image_url' => $img_url, |
| 373 | 'thumb_url' => get_the_post_thumbnail_url( $id, 'thumbnail' ), |
| 374 | 'offset_image_url' => $img_offset_url, |
| 375 | 'post_date' => esc_html( get_the_date() ), |
| 376 | 'post_link' => get_the_permalink(), |
| 377 | 'post_type' => $data['post_type'], |
| 378 | 'prefix' => $prefix, |
| 379 | 'post_count' => esc_html( $get_view_count ), |
| 380 | 'title' => Fns::get_the_title( $id, $data ), // wp_kses( $post->post_title, Fns::allowedHtml() ), |
| 381 | 'taxonomy_lists' => $filtered_taxonomy_lists, |
| 382 | 'post_class' => join( ' ', get_post_class( null, $id ) ), |
| 383 | 'layout_style' => $data['layout_style'], |
| 384 | 'hover_animation' => $data['hover_animation'], |
| 385 | 'acf_data' => $acf_data, |
| 386 | 'tpg_post_count' => $pCount, |
| 387 | 'tpg_total_posts' => $post_count, |
| 388 | ]; |
| 389 | |
| 390 | if ( isset( $data['show_event_date'] ) && 'on' === $data['show_event_date'] ) { |
| 391 | ob_start(); |
| 392 | Fns::event_information( $data ); |
| 393 | $event_html = ob_get_clean(); |
| 394 | $post_data['event_html'] = $event_html; |
| 395 | } else { |
| 396 | $post_data['event_html'] = ''; |
| 397 | } |
| 398 | |
| 399 | $send_data['posts'][] = $post_data; |
| 400 | $pCount ++; |
| 401 | } |
| 402 | } else { |
| 403 | $send_data['message'] = $data['no_posts_found_text'] ?? __( 'No posts found', 'the-post-grid' ); |
| 404 | $send_data['args'] = $args; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | wp_reset_postdata(); |
| 409 | |
| 410 | return rest_ensure_response( $send_data ); |
| 411 | } |
| 412 | } |
| 413 |