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
GetTickerPostsV1.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | namespace RT\ThePostGrid\Controllers\Api; |
| 4 | |
| 5 | use RT\ThePostGrid\Helpers\Fns; |
| 6 | use WP_Query; |
| 7 | |
| 8 | class GetTickerPostsV1 { |
| 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 | 'tickerquery', |
| 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 | public function get_all_posts( $data ) { |
| 28 | $_post_type = ! empty( $data['post_type'] ) ? esc_html( $data['post_type'] ) : 'post'; |
| 29 | $post_type = Fns::available_post_type( $_post_type ); |
| 30 | $args = [ |
| 31 | 'post_type' => $post_type, |
| 32 | 'post_status' => 'publish', |
| 33 | ]; |
| 34 | |
| 35 | $excluded_ids = null; |
| 36 | if ( $data['post_id'] ) { |
| 37 | $post_ids = explode( ',', $data['post_id'] ); |
| 38 | $post_ids = array_map( 'trim', $post_ids ); |
| 39 | |
| 40 | $args['post__in'] = $post_ids; |
| 41 | |
| 42 | if ( $excluded_ids != null && is_array( $excluded_ids ) ) { |
| 43 | $args['post__in'] = array_diff( $post_ids, $excluded_ids ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if ( 'yes' == $data['ignore_sticky_posts'] ) { |
| 48 | $args['ignore_sticky_posts'] = 1; |
| 49 | } |
| 50 | |
| 51 | if ( $orderby = $data['orderby'] ) { |
| 52 | $args['orderby'] = $orderby; |
| 53 | } |
| 54 | |
| 55 | if ( $data['order'] ) { |
| 56 | $args['order'] = $data['order']; |
| 57 | } |
| 58 | |
| 59 | if ( $data['instant_query'] ) { |
| 60 | $args = Fns::get_instant_query( $data['instant_query'], $args ); |
| 61 | } |
| 62 | |
| 63 | if ( $data['author'] ) { |
| 64 | $args['author__in'] = $data['author']; |
| 65 | } |
| 66 | |
| 67 | if ( $data['start_date'] || $data['end_date'] ) { |
| 68 | $args['date_query'] = [ |
| 69 | [ |
| 70 | 'after' => trim( $data['start_date'] ), |
| 71 | 'before' => trim( $data['end_date'] ), |
| 72 | 'inclusive' => true, |
| 73 | ], |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | // Taxonomy should implement after |
| 78 | $_taxonomies = get_object_taxonomies( $data['post_type'], 'objects' ); |
| 79 | $_taxonomy_list = $data['taxonomy_lists']; |
| 80 | $filtered_taxonomy_lists = []; |
| 81 | |
| 82 | foreach ( $_taxonomies as $index => $object ) { |
| 83 | if ( in_array( $object->name, Fns::get_excluded_taxonomy() ) ) { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | $filtered_taxonomy_lists[ $object->name ] = isset( $_taxonomy_list[ $object->name ] ) ? $_taxonomy_list[ $object->name ]['options'] : null; |
| 88 | $_term_list = isset( $_taxonomy_list[ $object->name ] ) ? wp_list_pluck( $_taxonomy_list[ $object->name ]['options'], 'value' ) : null; |
| 89 | if ( ! empty( $_term_list ) ) { |
| 90 | //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 91 | $args['tax_query'][] = [ |
| 92 | 'taxonomy' => $object->name, |
| 93 | 'field' => 'term_id', |
| 94 | 'terms' => $_term_list, |
| 95 | ]; |
| 96 | } |
| 97 | if ( ! empty( $args['tax_query'] ) && $data['relation'] ) { |
| 98 | $args['tax_query']['relation'] = $data['relation']; //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if ( $data['post_keyword'] ) { |
| 103 | $args['s'] = $data['post_keyword']; |
| 104 | } |
| 105 | |
| 106 | $excluded_ids = []; |
| 107 | $offset_posts = []; |
| 108 | if ( $data['exclude'] || $data['offset'] ) { |
| 109 | if ( $data['exclude'] ) { |
| 110 | $excluded_ids = explode( ',', $data['exclude'] ); |
| 111 | $excluded_ids = array_map( 'trim', $excluded_ids ); |
| 112 | } |
| 113 | |
| 114 | if ( $data['offset'] ) { |
| 115 | $_temp_args = $args; |
| 116 | unset( $_temp_args['paged'] ); |
| 117 | $_temp_args['posts_per_page'] = $data['offset']; |
| 118 | $_temp_args['fields'] = 'ids'; |
| 119 | |
| 120 | $offset_posts = get_posts( $_temp_args ); |
| 121 | } |
| 122 | |
| 123 | $excluded_post_ids = array_merge( $offset_posts, $excluded_ids ); |
| 124 | $args['post__not_in'] = array_unique( $excluded_post_ids ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in |
| 125 | } |
| 126 | |
| 127 | $query = new WP_Query( $args ); |
| 128 | |
| 129 | $send_data = [ |
| 130 | 'posts' => [], |
| 131 | 'total_post' => $query->found_posts, |
| 132 | 'query_args' => $args, |
| 133 | ]; |
| 134 | |
| 135 | // $send_data['total_post'] = esc_html( $query->found_posts ); |
| 136 | if ( $query->have_posts() ) { |
| 137 | $pCount = 1; |
| 138 | while ( $query->have_posts() ) { |
| 139 | $query->the_post(); |
| 140 | $id = get_the_id(); |
| 141 | $post_count = $query->post_count; |
| 142 | set_query_var( 'tpg_post_count', $pCount ); |
| 143 | set_query_var( 'tpg_total_posts', $post_count ); |
| 144 | $category_terms_list = get_the_terms( $id, $data['category_source'] ? $data['category_source'] : 'category' ); |
| 145 | $tags_terms_list = get_the_terms( $id, $data['tag_source'] ? $data['tag_source'] : 'post_tag' ); |
| 146 | $category_terms = wp_list_pluck( $category_terms_list, 'name' ); |
| 147 | $tags_terms = wp_list_pluck( $tags_terms_list, 'name' ); |
| 148 | |
| 149 | $excerpt_args = [ |
| 150 | 'excerpt_type' => $data['excerpt_type'], |
| 151 | 'excerpt_limit' => $data['excerpt_limit'], |
| 152 | 'excerpt_more_text' => $data['excerpt_more_text'], |
| 153 | ]; |
| 154 | |
| 155 | $exerpt = Fns::get_the_excerpt( $id, $excerpt_args ); |
| 156 | $author_id = get_the_author_meta( 'ID' ); |
| 157 | $count_key = Fns::get_post_view_count_meta_key(); |
| 158 | $get_view_count = get_post_meta( $id, $count_key, true ); |
| 159 | |
| 160 | $send_data['posts'][] = [ |
| 161 | 'author_name' => esc_html( get_the_author_meta( 'display_name', $author_id ) ), |
| 162 | 'comment_count' => esc_html( get_comments_number( $id ) ), |
| 163 | 'category' => ! empty( $category_terms ) ? $category_terms : [], |
| 164 | 'tags' => ! empty( $tags_terms ) ? $tags_terms : '', |
| 165 | 'excerpt' => $exerpt, |
| 166 | 'id' => $id, |
| 167 | 'thumb_url' => get_the_post_thumbnail_url( $id, 'thumbnail' ), |
| 168 | 'post_date' => esc_html( get_the_date() ), |
| 169 | 'post_link' => get_the_permalink(), |
| 170 | 'post_type' => $data['post_type'], |
| 171 | 'post_count' => esc_html( $get_view_count ), |
| 172 | 'title' => Fns::get_the_title( $id, $data ), // wp_kses( $post->post_title, Fns::allowedHtml() ), |
| 173 | 'tpg_post_count' => $pCount, |
| 174 | 'tpg_total_posts' => $post_count, |
| 175 | ]; |
| 176 | |
| 177 | $pCount ++; |
| 178 | } |
| 179 | } else { |
| 180 | $send_data['message'] = $data['no_posts_found_text'] ?? __( 'No posts found', 'the-post-grid' ); |
| 181 | $send_data['args'] = $args; |
| 182 | } |
| 183 | |
| 184 | wp_reset_postdata(); |
| 185 | |
| 186 | return rest_ensure_response( $send_data ); |
| 187 | } |
| 188 | } |