[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/rest-api/search/ -> class-wp-rest-post-search-handler.php (source)

   1  <?php
   2  /**
   3   * REST API: WP_REST_Post_Search_Handler class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 5.0.0
   8   */
   9  
  10  /**
  11   * Core class representing a search handler for posts in the REST API.
  12   *
  13   * @since 5.0.0
  14   *
  15   * @see WP_REST_Search_Handler
  16   */
  17  class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
  18  
  19      /**
  20       * Constructor.
  21       *
  22       * @since 5.0.0
  23       */
  24  	public function __construct() {
  25          $this->type = 'post';
  26  
  27          // Support all public post types except attachments.
  28          $this->subtypes = array_diff(
  29              array_values(
  30                  get_post_types(
  31                      array(
  32                          'public'       => true,
  33                          'show_in_rest' => true,
  34                      ),
  35                      'names'
  36                  )
  37              ),
  38              array( 'attachment' )
  39          );
  40      }
  41  
  42      /**
  43       * Searches posts for a given search request.
  44       *
  45       * @since 5.0.0
  46       *
  47       * @param WP_REST_Request $request Full REST request.
  48       * @return array {
  49       *     Associative array containing found IDs and total count for the matching search results.
  50       *
  51       *     @type int[] $ids   Array containing the matching post IDs.
  52       *     @type int   $total Total count for the matching search results.
  53       * }
  54       */
  55  	public function search_items( WP_REST_Request $request ) {
  56  
  57          // Get the post types to search for the current request.
  58          $post_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
  59          if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $post_types, true ) ) {
  60              $post_types = $this->subtypes;
  61          }
  62  
  63          $query_args = array(
  64              'post_type'           => $post_types,
  65              'post_status'         => 'publish',
  66              'paged'               => (int) $request['page'],
  67              'posts_per_page'      => (int) $request['per_page'],
  68              'ignore_sticky_posts' => true,
  69          );
  70  
  71          if ( ! empty( $request['search'] ) ) {
  72              $query_args['s'] = $request['search'];
  73          }
  74  
  75          if ( ! empty( $request['exclude'] ) ) {
  76              $query_args['post__not_in'] = $request['exclude'];
  77          }
  78  
  79          if ( ! empty( $request['include'] ) ) {
  80              $query_args['post__in'] = $request['include'];
  81          }
  82  
  83          /**
  84           * Filters the query arguments for a REST API post search request.
  85           *
  86           * Enables adding extra arguments or setting defaults for a post search request.
  87           *
  88           * @since 5.1.0
  89           *
  90           * @param array           $query_args Key value array of query var to query value.
  91           * @param WP_REST_Request $request    The request used.
  92           */
  93          $query_args = apply_filters( 'rest_post_search_query', $query_args, $request );
  94  
  95          $query = new WP_Query();
  96          $posts = $query->query( $query_args );
  97          // Querying the whole post object will warm the object cache, avoiding an extra query per result.
  98          $found_ids = wp_list_pluck( $posts, 'ID' );
  99          $total     = $query->found_posts;
 100  
 101          return array(
 102              self::RESULT_IDS   => $found_ids,
 103              self::RESULT_TOTAL => $total,
 104          );
 105      }
 106  
 107      /**
 108       * Prepares the search result for a given post ID.
 109       *
 110       * @since 5.0.0
 111       *
 112       * @param int   $id     Post ID.
 113       * @param array $fields Fields to include for the post.
 114       * @return array {
 115       *     Associative array containing fields for the post based on the `$fields` parameter.
 116       *
 117       *     @type int    $id    Optional. Post ID.
 118       *     @type string $title Optional. Post title.
 119       *     @type string $url   Optional. Post permalink URL.
 120       *     @type string $type  Optional. Post type.
 121       * }
 122       */
 123  	public function prepare_item( $id, array $fields ) {
 124          $post = get_post( $id );
 125  
 126          $data = array();
 127  
 128          if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
 129              $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $post->ID;
 130          }
 131  
 132          if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
 133              if ( post_type_supports( $post->post_type, 'title' ) ) {
 134                  add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
 135                  add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
 136                  $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID );
 137                  remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
 138                  remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
 139              } else {
 140                  $data[ WP_REST_Search_Controller::PROP_TITLE ] = '';
 141              }
 142          }
 143  
 144          if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
 145              $data[ WP_REST_Search_Controller::PROP_URL ] = get_permalink( $post->ID );
 146          }
 147  
 148          if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
 149              $data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
 150          }
 151  
 152          if ( in_array( WP_REST_Search_Controller::PROP_SUBTYPE, $fields, true ) ) {
 153              $data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = $post->post_type;
 154          }
 155  
 156          return $data;
 157      }
 158  
 159      /**
 160       * Prepares links for the search result of a given ID.
 161       *
 162       * @since 5.0.0
 163       *
 164       * @param int $id Item ID.
 165       * @return array Links for the given item.
 166       */
 167  	public function prepare_item_links( $id ) {
 168          $post = get_post( $id );
 169  
 170          $links = array();
 171  
 172          $item_route = rest_get_route_for_post( $post );
 173          if ( ! empty( $item_route ) ) {
 174              $links['self'] = array(
 175                  'href'       => rest_url( $item_route ),
 176                  'embeddable' => true,
 177              );
 178          }
 179  
 180          $links['about'] = array(
 181              'href' => rest_url( 'wp/v2/types/' . $post->post_type ),
 182          );
 183  
 184          return $links;
 185      }
 186  
 187      /**
 188       * Overwrites the default protected and private title format.
 189       *
 190       * By default, WordPress will show password protected or private posts with a title of
 191       * "Protected: %s" or "Private: %s", as the REST API communicates the status of a post
 192       * in a machine-readable format, we remove the prefix.
 193       *
 194       * @since 5.0.0
 195       *
 196       * @return string Title format.
 197       */
 198  	public function protected_title_format() {
 199          return '%s';
 200      }
 201  
 202      /**
 203       * Attempts to detect the route to access a single item.
 204       *
 205       * @since 5.0.0
 206       * @deprecated 5.5.0 Use rest_get_route_for_post()
 207       * @see rest_get_route_for_post()
 208       *
 209       * @param WP_Post $post Post object.
 210       * @return string REST route relative to the REST base URI, or empty string if unknown.
 211       */
 212  	protected function detect_rest_item_route( $post ) {
 213          _deprecated_function( __METHOD__, '5.5.0', 'rest_get_route_for_post()' );
 214  
 215          return rest_get_route_for_post( $post );
 216      }
 217  }


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref