class-ability-media-search.php
| 1 | <?php |
| 2 | /** |
| 3 | * Media search ability. |
| 4 | */ |
| 5 | |
| 6 | if ( ! class_exists( 'FooGallery_Ability_Media_Search' ) ) { |
| 7 | |
| 8 | class FooGallery_Ability_Media_Search { |
| 9 | |
| 10 | const ID = 'foogallery/media-search'; |
| 11 | |
| 12 | /** |
| 13 | * Register the ability. |
| 14 | */ |
| 15 | public function __construct() { |
| 16 | if ( foogallery_abilities_wp_api_available() ) { |
| 17 | add_action( 'foogallery_register_abilities', array( $this, 'register' ) ); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Register the ability with the WordPress core Abilities API. |
| 23 | */ |
| 24 | public function register() { |
| 25 | wp_register_ability( |
| 26 | self::ID, |
| 27 | array( |
| 28 | 'category' => FooGallery_Abilities::CATEGORY, |
| 29 | 'label' => __( 'Media Search', 'foogallery' ), |
| 30 | 'description' => __( 'Search media-library attachments using the required `search` text. Use `search_by` to restrict the search to specific attachment fields such as title, alt text, URL, or taxonomy terms, and `limit`/`offset` for pagination.', 'foogallery' ), |
| 31 | 'input_schema' => array( |
| 32 | 'type' => 'object', |
| 33 | 'required' => array( 'search' ), |
| 34 | 'properties' => array( |
| 35 | 'search' => array( |
| 36 | 'type' => 'string', |
| 37 | 'description' => __( 'The search text to match against attachment metadata.', 'foogallery' ), |
| 38 | ), |
| 39 | 'search_by' => array( |
| 40 | 'type' => 'array', |
| 41 | 'description' => __( 'Optional list of attachment fields to search. If omitted, the search runs across title, caption, description, alt text, URL, and taxonomy terms.', 'foogallery' ), |
| 42 | 'items' => array( |
| 43 | 'type' => 'string', |
| 44 | 'enum' => foogallery_abilities_get_media_searchable_fields(), |
| 45 | ), |
| 46 | ), |
| 47 | 'limit' => array( |
| 48 | 'type' => 'integer', |
| 49 | 'minimum' => 1, |
| 50 | 'maximum' => 100, |
| 51 | 'default' => 20, |
| 52 | 'description' => __( 'Maximum number of attachments to return. Defaults to 20.', 'foogallery' ), |
| 53 | ), |
| 54 | 'offset' => array( |
| 55 | 'type' => 'integer', |
| 56 | 'minimum' => 0, |
| 57 | 'default' => 0, |
| 58 | 'description' => __( 'Number of matching attachments to skip before returning results. Use with `limit` for pagination.', 'foogallery' ), |
| 59 | ), |
| 60 | ), |
| 61 | 'additionalProperties' => false, |
| 62 | ), |
| 63 | 'output_schema' => array( |
| 64 | 'type' => 'object', |
| 65 | 'properties' => array( |
| 66 | 'total' => array( |
| 67 | 'type' => 'integer', |
| 68 | 'description' => __( 'Total number of matching attachments.', 'foogallery' ), |
| 69 | ), |
| 70 | 'attachments' => array( |
| 71 | 'type' => 'array', |
| 72 | 'description' => __( 'Attachment records for the current page of results.', 'foogallery' ), |
| 73 | 'items' => foogallery_abilities_get_attachment_schema(), |
| 74 | ), |
| 75 | ), |
| 76 | ), |
| 77 | 'execute_callback' => array( $this, 'execute' ), |
| 78 | 'permission_callback' => array( $this, 'can_execute' ), |
| 79 | 'meta' => array( |
| 80 | 'annotations' => array( |
| 81 | 'readonly' => true, |
| 82 | 'idempotent' => true, |
| 83 | ), |
| 84 | 'show_in_rest' => true, |
| 85 | ), |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Check if the current user can execute the ability. |
| 92 | * |
| 93 | * @return bool |
| 94 | */ |
| 95 | public function can_execute( $args = null ) { |
| 96 | return current_user_can( 'upload_files' ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Execute the ability. |
| 101 | * |
| 102 | * @param array $args Ability arguments. |
| 103 | * |
| 104 | * @return array|WP_Error |
| 105 | */ |
| 106 | public function execute( $args ) { |
| 107 | $args = foogallery_abilities_normalize_input( $args ); |
| 108 | $search = isset( $args['search'] ) ? sanitize_text_field( $args['search'] ) : ''; |
| 109 | $search_by = isset( $args['search_by'] ) ? $args['search_by'] : array(); |
| 110 | $limit = isset( $args['limit'] ) ? max( 1, min( 100, absint( $args['limit'] ) ) ) : 20; |
| 111 | $offset = isset( $args['offset'] ) ? max( 0, absint( $args['offset'] ) ) : 0; |
| 112 | $search_result = foogallery_abilities_search_media_attachments( $search, $search_by, $limit, $offset ); |
| 113 | |
| 114 | if ( is_wp_error( $search_result ) ) { |
| 115 | return $search_result; |
| 116 | } |
| 117 | |
| 118 | $attachments = array(); |
| 119 | |
| 120 | foreach ( $search_result['attachment_ids'] as $attachment_id ) { |
| 121 | $attachments[] = foogallery_abilities_prepare_attachment( FooGalleryAttachment::get_by_id( $attachment_id ) ); |
| 122 | } |
| 123 | |
| 124 | return array( |
| 125 | 'total' => $search_result['total'], |
| 126 | 'attachments' => $attachments, |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 |