[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * REST API: WP_REST_Post_Format_Search_Handler class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 5.6.0
   8   */
   9  
  10  /**
  11   * Core class representing a search handler for post formats in the REST API.
  12   *
  13   * @since 5.6.0
  14   *
  15   * @see WP_REST_Search_Handler
  16   */
  17  class WP_REST_Post_Format_Search_Handler extends WP_REST_Search_Handler {
  18  
  19      /**
  20       * Constructor.
  21       *
  22       * @since 5.6.0
  23       */
  24  	public function __construct() {
  25          $this->type = 'post-format';
  26      }
  27  
  28      /**
  29       * Searches the object type content for a given search request.
  30       *
  31       * @since 5.6.0
  32       *
  33       * @param WP_REST_Request $request Full REST request.
  34       * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
  35       *               an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
  36       *               total count for the matching search results.
  37       */
  38  	public function search_items( WP_REST_Request $request ) {
  39          $format_strings = get_post_format_strings();
  40          $format_slugs   = array_keys( $format_strings );
  41  
  42          $query_args = array();
  43  
  44          if ( ! empty( $request['search'] ) ) {
  45              $query_args['search'] = $request['search'];
  46          }
  47  
  48          /**
  49           * Filters the query arguments for a REST API search request.
  50           *
  51           * Enables adding extra arguments or setting defaults for a post format search request.
  52           *
  53           * @since 5.6.0
  54           *
  55           * @param array           $query_args Key value array of query var to query value.
  56           * @param WP_REST_Request $request    The request used.
  57           */
  58          $query_args = apply_filters( 'rest_post_format_search_query', $query_args, $request );
  59  
  60          $found_ids = array();
  61          foreach ( $format_slugs as $index => $format_slug ) {
  62              if ( ! empty( $query_args['search'] ) ) {
  63                  $format_string       = get_post_format_string( $format_slug );
  64                  $format_slug_match   = stripos( $format_slug, $query_args['search'] ) !== false;
  65                  $format_string_match = stripos( $format_string, $query_args['search'] ) !== false;
  66                  if ( ! $format_slug_match && ! $format_string_match ) {
  67                      continue;
  68                  }
  69              }
  70  
  71              $format_link = get_post_format_link( $format_slug );
  72              if ( $format_link ) {
  73                  $found_ids[] = $format_slug;
  74              }
  75          }
  76  
  77          $page     = (int) $request['page'];
  78          $per_page = (int) $request['per_page'];
  79  
  80          return array(
  81              self::RESULT_IDS   => array_slice( $found_ids, ( $page - 1 ) * $per_page, $per_page ),
  82              self::RESULT_TOTAL => count( $found_ids ),
  83          );
  84      }
  85  
  86      /**
  87       * Prepares the search result for a given ID.
  88       *
  89       * @since 5.6.0
  90       *
  91       * @param string $id     Item ID, the post format slug.
  92       * @param array  $fields Fields to include for the item.
  93       * @return array Associative array containing all fields for the item.
  94       */
  95  	public function prepare_item( $id, array $fields ) {
  96          $data = array();
  97  
  98          if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
  99              $data[ WP_REST_Search_Controller::PROP_ID ] = $id;
 100          }
 101  
 102          if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
 103              $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_post_format_string( $id );
 104          }
 105  
 106          if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
 107              $data[ WP_REST_Search_Controller::PROP_URL ] = get_post_format_link( $id );
 108          }
 109  
 110          if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
 111              $data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
 112          }
 113  
 114          return $data;
 115      }
 116  
 117      /**
 118       * Prepares links for the search result.
 119       *
 120       * @since 5.6.0
 121       *
 122       * @param string $id Item ID, the post format slug.
 123       * @return array Links for the given item.
 124       */
 125  	public function prepare_item_links( $id ) {
 126          return array();
 127      }
 128  }


Generated: Fri Jun 18 01:00:07 2021 Cross-referenced by PHPXref 0.7.1