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-create-gallery.php
215 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Create gallery ability. |
| 9 | */ |
| 10 | |
| 11 | if ( ! class_exists( 'FooGallery_Ability_Create_Gallery' ) ) { |
| 12 | |
| 13 | class FooGallery_Ability_Create_Gallery { |
| 14 | |
| 15 | const ID = 'foogallery/create-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' => __( 'Create Gallery', 'foogallery' ), |
| 36 | 'description' => __( 'Create a new media-library-backed FooGallery gallery. `attachment_ids` is required, `layout` selects the gallery layout slug, and `settings` should be an array of setting update objects using the normalized IDs returned by the `foogallery/get-gallery-layout-schema` tool for that layout.', 'foogallery' ), |
| 37 | 'input_schema' => array( |
| 38 | 'type' => 'object', |
| 39 | 'required' => array( 'attachment_ids' ), |
| 40 | 'properties' => array( |
| 41 | 'title' => array( |
| 42 | 'type' => 'string', |
| 43 | 'description' => __( 'Optional gallery title. If omitted, FooGallery creates an `Untitled Gallery` draft title.', 'foogallery' ), |
| 44 | ), |
| 45 | 'status' => array( |
| 46 | 'type' => 'string', |
| 47 | 'enum' => array( 'draft', 'publish', 'private' ), |
| 48 | 'default' => 'draft', |
| 49 | 'description' => __( 'Optional initial gallery post status. Defaults to `draft`.', 'foogallery' ), |
| 50 | ), |
| 51 | 'layout' => array( |
| 52 | 'type' => 'string', |
| 53 | 'description' => __( 'Optional gallery layout slug. If omitted, FooGallery uses the current default gallery layout.', 'foogallery' ), |
| 54 | ), |
| 55 | 'attachment_ids' => array( |
| 56 | 'type' => 'array', |
| 57 | 'minItems' => 1, |
| 58 | 'description' => __( 'The media library attachment IDs to include in the new gallery.', 'foogallery' ), |
| 59 | 'items' => array( |
| 60 | 'type' => 'integer', |
| 61 | ), |
| 62 | ), |
| 63 | 'settings' => array( |
| 64 | 'type' => 'array', |
| 65 | 'description' => __( 'Optional array of layout setting update objects. Use the IDs and field types returned by the `foogallery/get-gallery-layout-schema` tool for the selected layout.', 'foogallery' ), |
| 66 | 'items' => foogallery_abilities_get_setting_update_schema(), |
| 67 | ), |
| 68 | 'sort' => array( |
| 69 | 'type' => 'string', |
| 70 | 'description' => __( 'Optional FooGallery sort mode to store for the gallery.', 'foogallery' ), |
| 71 | ), |
| 72 | 'custom_css' => array( |
| 73 | 'type' => 'string', |
| 74 | 'description' => __( 'Optional custom CSS to save with the gallery.', 'foogallery' ), |
| 75 | ), |
| 76 | ), |
| 77 | ), |
| 78 | 'output_schema' => array( |
| 79 | 'type' => 'object', |
| 80 | 'properties' => array( |
| 81 | 'gallery' => array_merge( |
| 82 | foogallery_abilities_get_gallery_detail_schema( false ), |
| 83 | array( |
| 84 | 'description' => __( 'The newly created gallery record.', 'foogallery' ), |
| 85 | ) |
| 86 | ), |
| 87 | ), |
| 88 | ), |
| 89 | 'execute_callback' => array( $this, 'execute' ), |
| 90 | 'permission_callback' => array( $this, 'can_execute' ), |
| 91 | 'meta' => array( |
| 92 | 'annotations' => array( |
| 93 | 'readonly' => false, |
| 94 | 'destructive' => false, |
| 95 | 'idempotent' => false, |
| 96 | ), |
| 97 | 'show_in_rest' => true, |
| 98 | ), |
| 99 | ) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Check if the current user can execute the ability. |
| 105 | * |
| 106 | * @param array $args Ability arguments. |
| 107 | * |
| 108 | * @return bool |
| 109 | */ |
| 110 | public function can_execute( $args ) { |
| 111 | $args = foogallery_abilities_normalize_input( $args ); |
| 112 | |
| 113 | $status = isset( $args['status'] ) ? foogallery_abilities_sanitize_gallery_status( $args['status'], 'draft' ) : 'draft'; |
| 114 | |
| 115 | if ( 'publish' === $status && ! current_user_can( 'publish_foogalleries' ) ) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | return current_user_can( 'create_foogalleries' ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Execute the ability. |
| 124 | * |
| 125 | * @param array $args Ability arguments. |
| 126 | * |
| 127 | * @return array|WP_Error |
| 128 | */ |
| 129 | public function execute( $args ) { |
| 130 | $args = foogallery_abilities_normalize_input( $args ); |
| 131 | |
| 132 | $layout = foogallery_abilities_get_requested_layout( $args, foogallery_default_gallery_template() ); |
| 133 | $template = foogallery_abilities_get_template( $layout ); |
| 134 | |
| 135 | if ( is_wp_error( $template ) ) { |
| 136 | return $template; |
| 137 | } |
| 138 | |
| 139 | $attachment_ids = foogallery_normalize_attachment_ids( |
| 140 | isset( $args['attachment_ids'] ) ? $args['attachment_ids'] : array() |
| 141 | ); |
| 142 | |
| 143 | if ( empty( $attachment_ids ) ) { |
| 144 | return new WP_Error( |
| 145 | 'foogallery_ability_missing_attachments', |
| 146 | __( 'At least one valid attachment ID is required to create a gallery.', 'foogallery' ) |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | $status = isset( $args['status'] ) ? foogallery_abilities_sanitize_gallery_status( $args['status'], 'draft' ) : 'draft'; |
| 151 | $title = isset( $args['title'] ) ? sanitize_text_field( $args['title'] ) : ''; |
| 152 | |
| 153 | if ( '' === $title ) { |
| 154 | $title = __( 'Untitled Gallery', 'foogallery' ); |
| 155 | } |
| 156 | |
| 157 | $request_context = array( |
| 158 | 'ability' => self::ID, |
| 159 | 'layout' => $template['slug'], |
| 160 | 'template' => $template['slug'], |
| 161 | 'attachment_ids' => $attachment_ids, |
| 162 | 'settings' => foogallery_abilities_prepare_template_setting_updates( |
| 163 | $template['slug'], |
| 164 | isset( $args['settings'] ) ? $args['settings'] : array() |
| 165 | ), |
| 166 | ); |
| 167 | |
| 168 | if ( is_wp_error( $request_context['settings'] ) ) { |
| 169 | return $request_context['settings']; |
| 170 | } |
| 171 | |
| 172 | $settings = foogallery_abilities_build_template_settings_base( $template['slug'] ); |
| 173 | $settings = foogallery_abilities_normalize_template_settings( |
| 174 | $template['slug'], |
| 175 | $request_context['settings'], |
| 176 | $settings, |
| 177 | 0, |
| 178 | $request_context |
| 179 | ); |
| 180 | |
| 181 | $insert_args = array( |
| 182 | 'title' => $title, |
| 183 | 'status' => $status, |
| 184 | 'template' => $template['slug'], |
| 185 | 'settings' => $settings, |
| 186 | 'attachment_ids' => $attachment_ids, |
| 187 | ); |
| 188 | |
| 189 | if ( isset( $args['sort'] ) ) { |
| 190 | $insert_args['sort'] = foogallery_abilities_sanitize_gallery_sort( $args['sort'] ); |
| 191 | } |
| 192 | |
| 193 | if ( isset( $args['custom_css'] ) ) { |
| 194 | $insert_args['custom_css'] = foogallery_sanitize_full( $args['custom_css'] ); |
| 195 | } |
| 196 | |
| 197 | $gallery_id = foogallery_insert_gallery( $insert_args, $request_context ); |
| 198 | |
| 199 | if ( is_wp_error( $gallery_id ) ) { |
| 200 | return foogallery_abilities_map_gallery_management_error( $gallery_id ); |
| 201 | } |
| 202 | |
| 203 | $gallery = foogallery_abilities_get_gallery( $gallery_id ); |
| 204 | |
| 205 | if ( is_wp_error( $gallery ) ) { |
| 206 | return $gallery; |
| 207 | } |
| 208 | |
| 209 | return array( |
| 210 | 'gallery' => foogallery_abilities_prepare_gallery_details( $gallery ), |
| 211 | ); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 |