|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Get a collection of posts by search term. |
| 4 | + * |
| 5 | + * @param array $request Options for the function. |
| 6 | + * @return array|null Post array,
* or null if none. |
| 7 | + * @since 0.0.1 |
| 8 | + */ |
| 9 | + |
| 10 | +function bwe_get_search( WP_REST_Request $request ) { |
| 11 | + |
| 12 | + // check for params |
| 13 | + $posts_per_page = $request['per_page']?: '10'; |
| 14 | + $page = $request['page']?: '1'; |
| 15 | + $category = $request['category']?: null; |
| 16 | + $tag = $request['tag']?: null; |
| 17 | + $show_content = $request['content']?: true; |
| 18 | + $search = $request['search']?: null; |
| 19 | + |
| 20 | + // WP_Query arguments |
| 21 | + $args = array( |
| 22 | + 'nopaging' => false, |
| 23 | + 'posts_per_page' => $posts_per_page, |
| 24 | + 'paged' => $page, |
| 25 | + 'cat' => $category, |
| 26 | + 'tag_id' => $tag, |
| 27 | + 's' => $search |
| 28 | + ); |
| 29 | + |
| 30 | + // The Query |
| 31 | + $query = new WP_Query( $args ); |
| 32 | + |
| 33 | + // Setup Posts Array |
| 34 | + $posts = array(); |
| 35 | + |
| 36 | + // The Loop |
| 37 | + if ( $query->have_posts() ) { |
| 38 | + while ( $query->have_posts() ) { |
| 39 | + $query->the_post(); |
| 40 | + |
| 41 | + // For Headers |
| 42 | + $total = $query->found_posts; |
| 43 | + $pages = $query->max_num_pages; |
| 44 | + |
| 45 | + // better wordpress endpoint post object |
| 46 | + $bwe_post = new stdClass(); |
| 47 | + |
| 48 | + // get post data |
| 49 | + $bwe_post->id = get_the_ID(); |
| 50 | + $bwe_post->title = get_the_title(); |
| 51 | + $bwe_post->slug = basename(get_permalink()); |
| 52 | + $bwe_post->date = get_the_date('c'); |
| 53 | + $bwe_post->excerpt = get_the_excerpt(); |
| 54 | + |
| 55 | + // show post content unless parameter is false |
| 56 | + if( $show_content == true ) { |
| 57 | + $bwe_post->content = apply_filters('the_content', get_the_content()); |
| 58 | + } |
| 59 | + |
| 60 | + $bwe_post->author = esc_html__(get_the_author(), 'text_domain'); |
| 61 | + $bwe_post->author_id = get_the_author_meta('ID'); |
| 62 | + |
| 63 | + /* |
| 64 | + * |
| 65 | + * get category data using get_the_category() |
| 66 | + * |
| 67 | + */ |
| 68 | + $categories = get_the_category(); |
| 69 | + |
| 70 | + $bwe_categories = []; |
| 71 | + $bwe_category_ids = []; |
| 72 | + |
| 73 | + if( !empty($categories) ){ |
| 74 | + foreach ($categories as $key => $category) { |
| 75 | + array_push($bwe_category_ids, $category->term_id); |
| 76 | + array_push($bwe_categories, $category->cat_name); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $bwe_post->category_ids = $bwe_category_ids; |
| 81 | + $bwe_post->category_names = $bwe_categories; |
| 82 | + |
| 83 | + /* |
| 84 | + * |
| 85 | + * get tag data using get_the_tags() |
| 86 | + * |
| 87 | + */ |
| 88 | + $tags = get_the_tags(); |
| 89 | + |
| 90 | + $bwe_tags = []; |
| 91 | + $bwe_tag_ids = []; |
| 92 | + |
| 93 | + if( !empty($tags) ){ |
| 94 | + foreach ($tags as $key => $tag) { |
| 95 | + array_push($bwe_tag_ids, $tag->term_id); |
| 96 | + array_push($bwe_tags, $tag->name); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + $bwe_post->tag_ids = $bwe_tag_ids; |
| 102 | + $bwe_post->tag_names = $bwe_tags; |
| 103 | + |
| 104 | + /* |
| 105 | + * |
| 106 | + * return acf fields if they exist |
| 107 | + * |
| 108 | + */ |
| 109 | + $bwe_post->acf = bwe_get_acf(); |
| 110 | + |
| 111 | + /* |
| 112 | + * |
| 113 | + * get possible thumbnail sizes and urls |
| 114 | + * |
| 115 | + */ |
| 116 | + $thumbnail_names = get_intermediate_image_sizes(); |
| 117 | + $bwe_thumbnails = new stdClass(); |
| 118 | + |
| 119 | + if( has_post_thumbnail() ){ |
| 120 | + foreach ($thumbnail_names as $key => $name) { |
| 121 | + $bwe_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name)); |
| 122 | + } |
| 123 | + |
| 124 | + $bwe_post->media = $bwe_thumbnails; |
| 125 | + } else { |
| 126 | + $bwe_post->media = false; |
| 127 | + } |
| 128 | + |
| 129 | + // Push the post to the main $post array |
| 130 | + array_push($posts, $bwe_post); |
| 131 | + } |
| 132 | + |
| 133 | + // return the post array |
| 134 | + $response = rest_ensure_response( $posts ); |
| 135 | + $response->header( 'X-WP-Total', (int) $total ); |
| 136 | + $response->header( 'X-WP-TotalPages', (int) $pages ); |
| 137 | + return $response; |
| 138 | + |
| 139 | + } else { |
| 140 | + // return empty posts array if no posts |
| 141 | + return $posts; |
| 142 | + } |
| 143 | + |
| 144 | + // Restore original Post Data |
| 145 | + wp_reset_postdata(); |
| 146 | +} |
| 147 | + |
| 148 | +/* |
| 149 | + * |
| 150 | + * Register Rest API Endpoint |
| 151 | + * |
| 152 | + */ |
| 153 | +add_action( 'rest_api_init', function () { |
| 154 | + register_rest_route( 'better-wp-endpoints/v1', '/search/', array( |
| 155 | + 'methods' => 'GET', |
| 156 | + 'callback' => 'bwe_get_search', |
| 157 | + 'args' => array( |
| 158 | + 'per_page' => array( |
| 159 | + 'description' => 'Maxiumum number of items to show per page.', |
| 160 | + 'type' => 'integer', |
| 161 | + 'validate_callback' => function( $param, $request, $key ) { |
| 162 | + return is_numeric( $param ); |
| 163 | + }, |
| 164 | + 'sanitize_callback' => 'absint', |
| 165 | + ), |
| 166 | + 'page' => array( |
| 167 | + 'description' => 'Current page of the collection.', |
| 168 | + 'type' => 'integer', |
| 169 | + 'validate_callback' => function( $param, $request, $key ) { |
| 170 | + return is_numeric( $param ); |
| 171 | + }, |
| 172 | + 'sanitize_callback' => 'absint' |
| 173 | + ), |
| 174 | + 'category' => array( |
| 175 | + 'description' => 'Get a category from the collection.', |
| 176 | + 'type' => 'integer', |
| 177 | + 'validate_callback' => function( $param, $request, $key ) { |
| 178 | + return is_numeric( $param ); |
| 179 | + }, |
| 180 | + 'sanitize_callback' => 'absint' |
| 181 | + ), |
| 182 | + 'tag' => array( |
| 183 | + 'description' => 'Get a tag from the collection.', |
| 184 | + 'type' => 'integer', |
| 185 | + 'validate_callback' => function( $param, $request, $key ) { |
| 186 | + return is_numeric( $param ); |
| 187 | + }, |
| 188 | + 'sanitize_callback' => 'absint' |
| 189 | + ), |
| 190 | + 'content' => array( |
| 191 | + 'description' => 'Hide or show the_content from the collection.', |
| 192 | + 'type' => 'boolean', |
| 193 | + 'validate_callback' => function( $param, $request, $key ) { |
| 194 | + |
| 195 | + if ( $param == 'true' || $param == 'TRUE' ) { |
| 196 | + $param = true; |
| 197 | + } else if( $param == 'false' || $param == 'FALSE') { |
| 198 | + $param = false; |
| 199 | + } |
| 200 | + |
| 201 | + return is_bool( $param ); |
| 202 | + } |
| 203 | + ), |
| 204 | + 'search' => array( |
| 205 | + 'description' => 'The search term used to fetch the collection.', |
| 206 | + 'type' => 'string', |
| 207 | + 'required' => true, |
| 208 | + 'validate_callback' => function($param, $request, $key) { |
| 209 | + return is_string( $param ); |
| 210 | + }, |
| 211 | + 'sanitize_callback' => 'sanitize_text_field' |
| 212 | + ), |
| 213 | + ), |
| 214 | + ) ); |
| 215 | +} ); |
0 commit comments