class-abilities.php
2 months ago
class-ability-create-gallery.php
2 months ago
class-ability-get-gallery-layout-schema.php
2 months ago
class-ability-get-gallery.php
2 months ago
class-ability-list-galleries.php
2 months ago
class-ability-list-gallery-layouts.php
2 months ago
class-ability-media-search.php
2 months ago
class-ability-update-gallery-attachments.php
2 months ago
class-ability-update-gallery.php
2 months ago
functions.php
2 months ago
index.php
2 months ago
class-ability-list-galleries.php
151 lines
| 1 | <?php |
| 2 | /** |
| 3 | * List galleries ability. |
| 4 | */ |
| 5 | |
| 6 | if ( ! class_exists( 'FooGallery_Ability_List_Galleries' ) ) { |
| 7 | |
| 8 | class FooGallery_Ability_List_Galleries { |
| 9 | |
| 10 | const ID = 'foogallery/list-galleries'; |
| 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' => __( 'List Galleries', 'foogallery' ), |
| 30 | 'description' => __( 'List FooGallery galleries. Use `search` to match gallery titles, `status` to filter by post status, `layout` to filter by gallery layout slug, and `limit`/`offset` for pagination.', 'foogallery' ), |
| 31 | 'input_schema' => array( |
| 32 | 'type' => 'object', |
| 33 | 'properties' => array( |
| 34 | 'search' => array( |
| 35 | 'type' => 'string', |
| 36 | 'description' => __( 'Optional text search against the gallery title.', 'foogallery' ), |
| 37 | ), |
| 38 | 'status' => array( |
| 39 | 'type' => 'string', |
| 40 | 'enum' => array( 'draft', 'publish', 'private', 'any' ), |
| 41 | 'default' => 'any', |
| 42 | 'description' => __( 'Optional gallery post status filter. Use `any` to include draft, publish, and private galleries.', 'foogallery' ), |
| 43 | ), |
| 44 | 'layout' => array( |
| 45 | 'type' => 'string', |
| 46 | 'description' => __( 'Optional gallery layout slug, such as `default` or `masonry`, used to filter the results.', 'foogallery' ), |
| 47 | ), |
| 48 | 'limit' => array( |
| 49 | 'type' => 'integer', |
| 50 | 'minimum' => 1, |
| 51 | 'maximum' => 100, |
| 52 | 'default' => 20, |
| 53 | 'description' => __( 'Maximum number of galleries to return. Defaults to 20.', 'foogallery' ), |
| 54 | ), |
| 55 | 'offset' => array( |
| 56 | 'type' => 'integer', |
| 57 | 'minimum' => 0, |
| 58 | 'default' => 0, |
| 59 | 'description' => __( 'Number of matching galleries to skip before returning results. Use with `limit` for pagination.', 'foogallery' ), |
| 60 | ), |
| 61 | ), |
| 62 | ), |
| 63 | 'output_schema' => array( |
| 64 | 'type' => 'object', |
| 65 | 'properties' => array( |
| 66 | 'total' => array( |
| 67 | 'type' => 'integer', |
| 68 | 'description' => __( 'Total number of galleries matching the supplied filters.', 'foogallery' ), |
| 69 | ), |
| 70 | 'galleries' => array( |
| 71 | 'type' => 'array', |
| 72 | 'description' => __( 'Gallery summary records for the current page of results.', 'foogallery' ), |
| 73 | 'items' => foogallery_abilities_get_gallery_summary_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( 'edit_foogalleries' ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Execute the ability. |
| 101 | * |
| 102 | * @param array $args Ability arguments. |
| 103 | * |
| 104 | * @return array |
| 105 | */ |
| 106 | public function execute( $args ) { |
| 107 | $args = foogallery_abilities_normalize_input( $args ); |
| 108 | |
| 109 | $status = isset( $args['status'] ) ? sanitize_key( $args['status'] ) : 'any'; |
| 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 | $query_args = array( |
| 113 | 'post_type' => FOOGALLERY_CPT_GALLERY, |
| 114 | 'post_status' => 'any' === $status ? array( 'draft', 'publish', 'private' ) : $status, |
| 115 | 'posts_per_page' => $limit, |
| 116 | 'offset' => $offset, |
| 117 | 'orderby' => 'modified', |
| 118 | 'order' => 'DESC', |
| 119 | ); |
| 120 | |
| 121 | if ( ! empty( $args['search'] ) ) { |
| 122 | $query_args['s'] = sanitize_text_field( $args['search'] ); |
| 123 | } |
| 124 | |
| 125 | $layout = foogallery_abilities_get_requested_layout( $args ); |
| 126 | |
| 127 | if ( '' !== $layout ) { |
| 128 | $query_args['meta_query'] = array( |
| 129 | array( |
| 130 | 'key' => FOOGALLERY_META_TEMPLATE, |
| 131 | 'value' => $layout, |
| 132 | ), |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | $query = new WP_Query( $query_args ); |
| 137 | $galleries = array(); |
| 138 | |
| 139 | foreach ( $query->posts as $post ) { |
| 140 | $gallery = FooGallery::get( $post ); |
| 141 | $galleries[] = foogallery_abilities_prepare_gallery_summary( $gallery ); |
| 142 | } |
| 143 | |
| 144 | return array( |
| 145 | 'total' => intval( $query->found_posts ), |
| 146 | 'galleries' => $galleries, |
| 147 | ); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 |