class-abilities.php
2 days ago
class-ability-create-gallery.php
2 days ago
class-ability-get-gallery-layout-schema.php
2 days ago
class-ability-get-gallery.php
2 days ago
class-ability-list-galleries.php
2 days ago
class-ability-list-gallery-layouts.php
2 days ago
class-ability-media-search.php
2 days ago
class-ability-update-gallery-attachments.php
2 days ago
class-ability-update-gallery.php
2 days ago
functions.php
2 days ago
index.php
2 days ago
class-ability-get-gallery-layout-schema.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Get gallery layout schema ability. |
| 9 | */ |
| 10 | |
| 11 | if ( ! class_exists( 'FooGallery_Ability_Get_Gallery_Layout_Schema' ) ) { |
| 12 | |
| 13 | class FooGallery_Ability_Get_Gallery_Layout_Schema { |
| 14 | |
| 15 | const ID = 'foogallery/get-gallery-layout-schema'; |
| 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 Layout Schema', 'foogallery' ), |
| 36 | 'description' => __( 'Return the schema for one gallery layout. Use the required `layout` slug to fetch the normalized field definitions for that layout.', 'foogallery' ), |
| 37 | 'input_schema' => array( |
| 38 | 'type' => 'object', |
| 39 | 'required' => array( 'layout' ), |
| 40 | 'properties' => array( |
| 41 | 'layout' => array( |
| 42 | 'type' => 'string', |
| 43 | 'description' => __( 'The gallery layout slug to inspect, such as `default` or `masonry`.', 'foogallery' ), |
| 44 | ), |
| 45 | ), |
| 46 | ), |
| 47 | 'output_schema' => array( |
| 48 | 'type' => 'object', |
| 49 | 'properties' => array( |
| 50 | 'layout' => array_merge( |
| 51 | foogallery_abilities_get_template_schema( true ), |
| 52 | array( |
| 53 | 'description' => __( 'The requested gallery layout definition, including the normalized setting field schema.', 'foogallery' ), |
| 54 | ) |
| 55 | ), |
| 56 | ), |
| 57 | ), |
| 58 | 'execute_callback' => array( $this, 'execute' ), |
| 59 | 'permission_callback' => array( $this, 'can_execute' ), |
| 60 | 'meta' => array( |
| 61 | 'annotations' => array( |
| 62 | 'readonly' => true, |
| 63 | 'idempotent' => true, |
| 64 | ), |
| 65 | 'show_in_rest' => true, |
| 66 | ), |
| 67 | ) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check if the current user can execute the ability. |
| 73 | * |
| 74 | * @return bool |
| 75 | */ |
| 76 | public function can_execute( $args = null ) { |
| 77 | return current_user_can( 'edit_foogalleries' ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Execute the ability. |
| 82 | * |
| 83 | * @param array $args Ability arguments. |
| 84 | * |
| 85 | * @return array|WP_Error |
| 86 | */ |
| 87 | public function execute( $args ) { |
| 88 | $args = foogallery_abilities_normalize_input( $args ); |
| 89 | |
| 90 | $layout = foogallery_abilities_get_requested_layout( $args ); |
| 91 | |
| 92 | if ( '' === $layout ) { |
| 93 | return new WP_Error( |
| 94 | 'foogallery_ability_missing_layout', |
| 95 | __( 'A valid layout slug is required.', 'foogallery' ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | $template = foogallery_abilities_get_template( $layout ); |
| 100 | |
| 101 | if ( is_wp_error( $template ) ) { |
| 102 | return $template; |
| 103 | } |
| 104 | |
| 105 | return array( |
| 106 | 'layout' => foogallery_abilities_prepare_template( $template, true ), |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 |