[ Index ]

PHP Cross Reference of WordPress

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 the object type content for a given search request.
  44       *
  45       * @since 5.0.0
  46       *
  47       * @param WP_REST_Request $request Full REST request.
  48       * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
  49       *               an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
  50       *               total count for the matching search results.
  51       */
  52  	public function search_items( WP_REST_Request $request ) {
  53  
  54          // Get the post types to search for the current request.
  55          $post_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
  56          if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $post_types, true ) ) {
  57              $post_types = $this->subtypes;
  58          }
  59  
  60          $query_args = array(
  61              'post_type'           => $post_types,
  62              'post_status'         => 'publish',
  63              'paged'               => (int) $request['page'],
  64              'posts_per_page'      => (int) $request['per_page'],
  65              'ignore_sticky_posts' => true,
  66              'fields'              => 'ids',
  67          );
  68  
  69          if ( ! empty( $request['search'] ) ) {
  70              $query_args['s'] = $request['search'];
  71          }
  72  
  73          /**
  74           * Filters the query arguments for a REST API search request.
  75           *
  76           * Enables adding extra arguments or setting defaults for a post search request.
  77           *
  78           * @since 5.1.0
  79           *
  80           * @param array           $query_args Key value array of query var to query value.
  81           * @param WP_REST_Request $request    The request used.
  82           */
  83          $query_args = apply_filters( 'rest_post_search_query', $query_args, $request );
  84  
  85          $query     = new WP_Query();
  86          $found_ids = $query->query( $query_args );
  87          $total     = $query->found_posts;
  88  
  89          return array(
  90              self::RESULT_IDS   => $found_ids,
  91              self::RESULT_TOTAL => $total,
  92          );
  93      }
  94  
  95      /**
  96       * Prepares the search result for a given ID.
  97       *
  98       * @since 5.0.0
  99       *
 100       * @param int   $id     Item ID.
 101       * @param array $fields Fields to include for the item.
 102       * @return array Associative array containing all fields for the item.
 103       */
 104  	public function prepare_item( $id, array $fields ) {
 105          $post = get_post( $id );
 106  
 107          $data = array();
 108  
 109          if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
 110              $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $post->ID;
 111          }
 112  
 113          if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
 114              if ( post_type_supports( $post->post_type, 'title' ) ) {
 115                  add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
 116                  $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID );
 117                  remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
 118              } else {
 119                  $data[ WP_REST_Search_Controller::PROP_TITLE ] = '';
 120              }
 121          }
 122  
 123          if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
 124              $data[ WP_REST_Search_Controller::PROP_URL ] = get_permalink( $post->ID );
 125          }
 126  
 127          if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
 128              $data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
 129          }
 130  
 131          if ( in_array( WP_REST_Search_Controller::PROP_SUBTYPE, $fields, true ) ) {
 132              $data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = $post->post_type;
 133          }
 134  
 135          return $data;
 136      }
 137  
 138      /**
 139       * Prepares links for the search result of a given ID.
 140       *
 141       * @since 5.0.0
 142       *
 143       * @param int $id Item ID.
 144       * @return array Links for the given item.
 145       */
 146  	public function prepare_item_links( $id ) {
 147          $post = get_post( $id );
 148  
 149          $links = array();
 150  
 151          $item_route = rest_get_route_for_post( $post );
 152          if ( ! empty( $item_route ) ) {
 153              $links['self'] = array(
 154                  'href'       => rest_url( $item_route ),
 155                  'embeddable' => true,
 156              );
 157          }
 158  
 159          $links['about'] = array(
 160              'href' => rest_url( 'wp/v2/types/' . $post->post_type ),
 161          );
 162  
 163          return $links;
 164      }
 165  
 166      /**
 167       * Overwrites the default protected title format.
 168       *
 169       * By default, WordPress will show password protected posts with a title of
 170       * "Protected: %s". As the REST API communicates the protected status of a post
 171       * in a machine readable format, we remove the "Protected: " prefix.
 172       *
 173       * @since 5.0.0
 174       *
 175       * @return string Protected title format.
 176       */
 177  	public function protected_title_format() {
 178          return '%s';
 179      }
 180  
 181      /**
 182       * Attempts to detect the route to access a single item.
 183       *
 184       * @since 5.0.0
 185       * @deprecated 5.5.0 Use rest_get_route_for_post()
 186       * @see rest_get_route_for_post()
 187       *
 188       * @param WP_Post $post Post object.
 189       * @return string REST route relative to the REST base URI, or empty string if unknown.
 190       */
 191  	protected function detect_rest_item_route( $post ) {
 192          _deprecated_function( __METHOD__, '5.5.0', 'rest_get_route_for_post()' );
 193  
 194          return rest_get_route_for_post( $post );
 195      }
 196  
 197  }


Generated: Tue Dec 24 01:00:02 2024 Cross-referenced by PHPXref 0.7.1