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-list-gallery-layouts.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * List gallery layouts ability. |
| 9 | */ |
| 10 | |
| 11 | if ( ! class_exists( 'FooGallery_Ability_List_Gallery_Layouts' ) ) { |
| 12 | |
| 13 | class FooGallery_Ability_List_Gallery_Layouts { |
| 14 | |
| 15 | const ID = 'foogallery/list-layouts'; |
| 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 Gallery Layouts', 'foogallery' ), |
| 36 | 'description' => __( 'List registered FooGallery gallery layouts. Use `layout` to limit the response to one layout and `include_fields` to include or omit the normalized setting field definitions.', 'foogallery' ), |
| 37 | 'input_schema' => array( |
| 38 | 'type' => 'object', |
| 39 | 'properties' => array( |
| 40 | 'layout' => array( |
| 41 | 'type' => 'string', |
| 42 | 'description' => __( 'Optional gallery layout slug used to return a single layout record instead of the full registry.', 'foogallery' ), |
| 43 | ), |
| 44 | 'include_fields' => array( |
| 45 | 'type' => 'boolean', |
| 46 | 'default' => true, |
| 47 | 'description' => __( 'When `true`, include the normalized setting field definitions for each layout. When `false`, return only the top-level layout metadata.', 'foogallery' ), |
| 48 | ), |
| 49 | ), |
| 50 | ), |
| 51 | 'output_schema' => array( |
| 52 | 'type' => 'object', |
| 53 | 'properties' => array( |
| 54 | 'layouts' => array( |
| 55 | 'type' => 'array', |
| 56 | 'description' => __( 'The gallery layouts that matched the request.', 'foogallery' ), |
| 57 | 'items' => foogallery_abilities_get_template_schema( true ), |
| 58 | ), |
| 59 | ), |
| 60 | ), |
| 61 | 'execute_callback' => array( $this, 'execute' ), |
| 62 | 'permission_callback' => array( $this, 'can_execute' ), |
| 63 | 'meta' => array( |
| 64 | 'annotations' => array( |
| 65 | 'readonly' => true, |
| 66 | 'idempotent' => true, |
| 67 | ), |
| 68 | 'show_in_rest' => true, |
| 69 | ), |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Check if the current user can execute the ability. |
| 76 | * |
| 77 | * @return bool |
| 78 | */ |
| 79 | public function can_execute( $args = null ) { |
| 80 | return current_user_can( 'edit_foogalleries' ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Execute the ability. |
| 85 | * |
| 86 | * @param array $args Ability arguments. |
| 87 | * |
| 88 | * @return array|WP_Error |
| 89 | */ |
| 90 | public function execute( $args ) { |
| 91 | $args = foogallery_abilities_normalize_input( $args ); |
| 92 | |
| 93 | $include_fields = ! isset( $args['include_fields'] ) || (bool) $args['include_fields']; |
| 94 | $layout = foogallery_abilities_get_requested_layout( $args ); |
| 95 | |
| 96 | if ( '' !== $layout ) { |
| 97 | $template = foogallery_abilities_get_template( $layout ); |
| 98 | |
| 99 | if ( is_wp_error( $template ) ) { |
| 100 | return $template; |
| 101 | } |
| 102 | |
| 103 | return array( |
| 104 | 'layouts' => array( |
| 105 | foogallery_abilities_prepare_template( $template, $include_fields ), |
| 106 | ), |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | $layouts = array(); |
| 111 | foreach ( foogallery_gallery_templates() as $template ) { |
| 112 | $layouts[] = foogallery_abilities_prepare_template( $template, $include_fields ); |
| 113 | } |
| 114 | |
| 115 | return array( |
| 116 | 'layouts' => $layouts, |
| 117 | ); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 |