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