[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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 post formats for a given search request.
  30       *
  31       * @since 5.6.0
  32       *
  33       * @param WP_REST_Request $request Full REST request.
  34       * @return array {
  35       *     Associative array containing found IDs and total count for the matching search results.
  36       *
  37       *     @type string[] $ids   Array containing slugs for the matching post formats.
  38       *     @type int      $total Total count for the matching search results.
  39       * }
  40       */
  41  	public function search_items( WP_REST_Request $request ) {
  42          $format_strings = get_post_format_strings();
  43          $format_slugs   = array_keys( $format_strings );
  44  
  45          $query_args = array();
  46  
  47          if ( ! empty( $request['search'] ) ) {
  48              $query_args['search'] = $request['search'];
  49          }
  50  
  51          /**
  52           * Filters the query arguments for a REST API post format search request.
  53           *
  54           * Enables adding extra arguments or setting defaults for a post format search request.
  55           *
  56           * @since 5.6.0
  57           *
  58           * @param array           $query_args Key value array of query var to query value.
  59           * @param WP_REST_Request $request    The request used.
  60           */
  61          $query_args = apply_filters( 'rest_post_format_search_query', $query_args, $request );
  62  
  63          $found_ids = array();
  64          foreach ( $format_slugs as $format_slug ) {
  65              if ( ! empty( $query_args['search'] ) ) {
  66                  $format_string       = get_post_format_string( $format_slug );
  67                  $format_slug_match   = stripos( $format_slug, $query_args['search'] ) !== false;
  68                  $format_string_match = stripos( $format_string, $query_args['search'] ) !== false;
  69                  if ( ! $format_slug_match && ! $format_string_match ) {
  70                      continue;
  71                  }
  72              }
  73  
  74              $format_link = get_post_format_link( $format_slug );
  75              if ( $format_link ) {
  76                  $found_ids[] = $format_slug;
  77              }
  78          }
  79  
  80          $page     = (int) $request['page'];
  81          $per_page = (int) $request['per_page'];
  82  
  83          return array(
  84              self::RESULT_IDS   => array_slice( $found_ids, ( $page - 1 ) * $per_page, $per_page ),
  85              self::RESULT_TOTAL => count( $found_ids ),
  86          );
  87      }
  88  
  89      /**
  90       * Prepares the search result for a given post format.
  91       *
  92       * @since 5.6.0
  93       *
  94       * @param string $id     Item ID, the post format slug.
  95       * @param array  $fields Fields to include for the item.
  96       * @return array {
  97       *     Associative array containing fields for the post format based on the `$fields` parameter.
  98       *
  99       *     @type string $id    Optional. Post format slug.
 100       *     @type string $title Optional. Post format name.
 101       *     @type string $url   Optional. Post format permalink URL.
 102       *     @type string $type  Optional. String 'post-format'.
 103       *}
 104       */
 105  	public function prepare_item( $id, array $fields ) {
 106          $data = array();
 107  
 108          if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
 109              $data[ WP_REST_Search_Controller::PROP_ID ] = $id;
 110          }
 111  
 112          if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
 113              $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_post_format_string( $id );
 114          }
 115  
 116          if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
 117              $data[ WP_REST_Search_Controller::PROP_URL ] = get_post_format_link( $id );
 118          }
 119  
 120          if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
 121              $data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
 122          }
 123  
 124          return $data;
 125      }
 126  
 127      /**
 128       * Prepares links for the search result.
 129       *
 130       * @since 5.6.0
 131       *
 132       * @param string $id Item ID, the post format slug.
 133       * @return array Links for the given item.
 134       */
 135  	public function prepare_item_links( $id ) {
 136          return array();
 137      }
 138  }


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref