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-get-gallery.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Get gallery ability. |
| 9 | */ |
| 10 | |
| 11 | if ( ! class_exists( 'FooGallery_Ability_Get_Gallery' ) ) { |
| 12 | |
| 13 | class FooGallery_Ability_Get_Gallery { |
| 14 | |
| 15 | const ID = 'foogallery/get-gallery'; |
| 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' => __( 'Get Gallery', 'foogallery' ), |
| 36 | 'description' => __( 'Return one FooGallery gallery by `gallery_id`. Set `include_attachment_detail` to `true` only when full attachment metadata is needed; otherwise the response includes attachment IDs only.', 'foogallery' ), |
| 37 | 'input_schema' => array( |
| 38 | 'type' => 'object', |
| 39 | 'required' => array( 'gallery_id' ), |
| 40 | 'properties' => array( |
| 41 | 'gallery_id' => array( |
| 42 | 'type' => 'integer', |
| 43 | 'description' => __( 'The numeric ID of the FooGallery gallery to fetch.', 'foogallery' ), |
| 44 | ), |
| 45 | 'include_attachment_detail' => array( |
| 46 | 'type' => 'boolean', |
| 47 | 'default' => false, |
| 48 | 'description' => __( 'When `true`, include the full `attachments` array with attachment metadata. When `false`, return only `attachment_ids`.', 'foogallery' ), |
| 49 | ), |
| 50 | ), |
| 51 | 'additionalProperties' => false, |
| 52 | ), |
| 53 | 'output_schema' => array( |
| 54 | 'type' => 'object', |
| 55 | 'properties' => array( |
| 56 | 'gallery' => array_merge( |
| 57 | foogallery_abilities_get_gallery_detail_schema( true ), |
| 58 | array( |
| 59 | 'description' => __( 'The requested gallery record with normalized settings and, optionally, full attachment details.', 'foogallery' ), |
| 60 | ) |
| 61 | ), |
| 62 | ), |
| 63 | ), |
| 64 | 'execute_callback' => array( $this, 'execute' ), |
| 65 | 'permission_callback' => array( $this, 'can_execute' ), |
| 66 | 'meta' => array( |
| 67 | 'annotations' => array( |
| 68 | 'readonly' => true, |
| 69 | 'idempotent' => true, |
| 70 | ), |
| 71 | 'show_in_rest' => true, |
| 72 | ), |
| 73 | ) |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Check if the current user can execute the ability. |
| 79 | * |
| 80 | * @param array $args Ability arguments. |
| 81 | * |
| 82 | * @return bool |
| 83 | */ |
| 84 | public function can_execute( $args ) { |
| 85 | $args = foogallery_abilities_normalize_input( $args ); |
| 86 | |
| 87 | $gallery_id = isset( $args['gallery_id'] ) ? absint( $args['gallery_id'] ) : 0; |
| 88 | |
| 89 | return $gallery_id > 0 && current_user_can( 'edit_post', $gallery_id ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Execute the ability. |
| 94 | * |
| 95 | * @param array $args Ability arguments. |
| 96 | * |
| 97 | * @return array|WP_Error |
| 98 | */ |
| 99 | public function execute( $args ) { |
| 100 | $args = foogallery_abilities_normalize_input( $args ); |
| 101 | |
| 102 | $include_attachment_detail = isset( $args['include_attachment_detail'] ) ? wp_validate_boolean( $args['include_attachment_detail'] ) : false; |
| 103 | |
| 104 | $gallery = foogallery_abilities_get_gallery( isset( $args['gallery_id'] ) ? $args['gallery_id'] : 0 ); |
| 105 | |
| 106 | if ( is_wp_error( $gallery ) ) { |
| 107 | return $gallery; |
| 108 | } |
| 109 | |
| 110 | return array( |
| 111 | 'gallery' => foogallery_abilities_prepare_gallery_details( $gallery, $include_attachment_detail ), |
| 112 | ); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 |