PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.3.2
Gallery : FooGallery v3.3.2
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / abilities / class-ability-media-search.php
foogallery / includes / abilities Last commit date
class-abilities.php 3 days ago class-ability-create-gallery.php 3 days ago class-ability-get-gallery-layout-schema.php 3 days ago class-ability-get-gallery.php 3 days ago class-ability-list-galleries.php 3 days ago class-ability-list-gallery-layouts.php 3 days ago class-ability-media-search.php 3 days ago class-ability-update-gallery-attachments.php 3 days ago class-ability-update-gallery.php 3 days ago functions.php 3 days ago index.php 3 days ago
class-ability-media-search.php
137 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Media search ability.
9 */
10
11 if ( ! class_exists( 'FooGallery_Ability_Media_Search' ) ) {
12
13 class FooGallery_Ability_Media_Search {
14
15 const ID = 'foogallery/media-search';
16
17 /**
18 * Register the ability.
19 */
20 public function __construct() {
21 if ( foogallery_abilities_wp_api_available() ) {
22 add_action( 'foogallery_register_abilities', array( $this, 'register' ) );
23 }
24 }
25
26 /**
27 * Register the ability with the WordPress core Abilities API.
28 */
29 public function register() {
30 // phpcs:ignore -- This callback is registered only when the WordPress Abilities API is available.
31 wp_register_ability(
32 self::ID,
33 array(
34 'category' => FooGallery_Abilities::CATEGORY,
35 'label' => __( 'Media Search', 'foogallery' ),
36 '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' ),
37 'input_schema' => array(
38 'type' => 'object',
39 'required' => array( 'search' ),
40 'properties' => array(
41 'search' => array(
42 'type' => 'string',
43 'description' => __( 'The search text to match against attachment metadata.', 'foogallery' ),
44 ),
45 'search_by' => array(
46 'type' => 'array',
47 'description' => __( 'Optional list of attachment fields to search. If omitted, the search runs across title, caption, description, alt text, URL, and taxonomy terms.', 'foogallery' ),
48 'items' => array(
49 'type' => 'string',
50 'enum' => foogallery_abilities_get_media_searchable_fields(),
51 ),
52 ),
53 'limit' => array(
54 'type' => 'integer',
55 'minimum' => 1,
56 'maximum' => 100,
57 'default' => 20,
58 'description' => __( 'Maximum number of attachments to return. Defaults to 20.', 'foogallery' ),
59 ),
60 'offset' => array(
61 'type' => 'integer',
62 'minimum' => 0,
63 'default' => 0,
64 'description' => __( 'Number of matching attachments to skip before returning results. Use with `limit` for pagination.', 'foogallery' ),
65 ),
66 ),
67 'additionalProperties' => false,
68 ),
69 'output_schema' => array(
70 'type' => 'object',
71 'properties' => array(
72 'total' => array(
73 'type' => 'integer',
74 'description' => __( 'Total number of matching attachments.', 'foogallery' ),
75 ),
76 'attachments' => array(
77 'type' => 'array',
78 'description' => __( 'Attachment records for the current page of results.', 'foogallery' ),
79 'items' => foogallery_abilities_get_attachment_schema(),
80 ),
81 ),
82 ),
83 'execute_callback' => array( $this, 'execute' ),
84 'permission_callback' => array( $this, 'can_execute' ),
85 'meta' => array(
86 'annotations' => array(
87 'readonly' => true,
88 'idempotent' => true,
89 ),
90 'show_in_rest' => true,
91 ),
92 )
93 );
94 }
95
96 /**
97 * Check if the current user can execute the ability.
98 *
99 * @return bool
100 */
101 public function can_execute( $args = null ) {
102 return current_user_can( 'upload_files' );
103 }
104
105 /**
106 * Execute the ability.
107 *
108 * @param array $args Ability arguments.
109 *
110 * @return array|WP_Error
111 */
112 public function execute( $args ) {
113 $args = foogallery_abilities_normalize_input( $args );
114 $search = isset( $args['search'] ) ? sanitize_text_field( $args['search'] ) : '';
115 $search_by = isset( $args['search_by'] ) ? $args['search_by'] : array();
116 $limit = isset( $args['limit'] ) ? max( 1, min( 100, absint( $args['limit'] ) ) ) : 20;
117 $offset = isset( $args['offset'] ) ? max( 0, absint( $args['offset'] ) ) : 0;
118 $search_result = foogallery_abilities_search_media_attachments( $search, $search_by, $limit, $offset );
119
120 if ( is_wp_error( $search_result ) ) {
121 return $search_result;
122 }
123
124 $attachments = array();
125
126 foreach ( $search_result['attachment_ids'] as $attachment_id ) {
127 $attachments[] = foogallery_abilities_prepare_attachment( FooGalleryAttachment::get_by_id( $attachment_id ) );
128 }
129
130 return array(
131 'total' => $search_result['total'],
132 'attachments' => $attachments,
133 );
134 }
135 }
136 }
137