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-update-gallery-attachments.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Update gallery attachments ability. |
| 9 | */ |
| 10 | |
| 11 | if ( ! class_exists( 'FooGallery_Ability_Update_Gallery_Attachments' ) ) { |
| 12 | |
| 13 | class FooGallery_Ability_Update_Gallery_Attachments { |
| 14 | |
| 15 | const ID = 'foogallery/update-gallery-attachments'; |
| 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' => __( 'Update Gallery Attachments', 'foogallery' ), |
| 36 | 'description' => __( 'Update gallery attachments for the gallery identified by `gallery_id`. Use `replace_attachment_ids` by itself to replace the full list, or use `append_attachment_ids` and/or `remove_attachment_ids` together for incremental changes.', '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 update.', 'foogallery' ), |
| 44 | ), |
| 45 | 'replace_attachment_ids' => array( |
| 46 | 'type' => 'array', |
| 47 | 'description' => __( 'Replace the gallery attachment list with this exact set of attachment IDs. Do not combine with append/remove.', 'foogallery' ), |
| 48 | 'items' => array( |
| 49 | 'type' => 'integer', |
| 50 | ), |
| 51 | ), |
| 52 | 'append_attachment_ids' => array( |
| 53 | 'type' => 'array', |
| 54 | 'description' => __( 'Attachment IDs to append to the existing gallery without duplicating IDs already present.', 'foogallery' ), |
| 55 | 'items' => array( |
| 56 | 'type' => 'integer', |
| 57 | ), |
| 58 | ), |
| 59 | 'remove_attachment_ids' => array( |
| 60 | 'type' => 'array', |
| 61 | 'description' => __( 'Attachment IDs to remove from the existing gallery. Use on its own or together with `append_attachment_ids`, but not with `replace_attachment_ids`.', 'foogallery' ), |
| 62 | 'items' => array( |
| 63 | 'type' => 'integer', |
| 64 | ), |
| 65 | ), |
| 66 | ), |
| 67 | 'additionalProperties' => false, |
| 68 | ), |
| 69 | 'output_schema' => array( |
| 70 | 'type' => 'object', |
| 71 | 'properties' => array( |
| 72 | 'gallery' => array_merge( |
| 73 | foogallery_abilities_get_gallery_summary_schema(), |
| 74 | array( |
| 75 | 'description' => __( 'The updated gallery summary after the attachment change has been applied.', 'foogallery' ), |
| 76 | ) |
| 77 | ), |
| 78 | 'attachment_ids' => array( |
| 79 | 'type' => 'array', |
| 80 | 'description' => __( 'The gallery attachment IDs after the update.', 'foogallery' ), |
| 81 | 'items' => array( |
| 82 | 'type' => 'integer', |
| 83 | ), |
| 84 | ), |
| 85 | 'attachment_count' => array( |
| 86 | 'type' => 'integer', |
| 87 | 'description' => __( 'The number of attachments remaining in the gallery after the update.', 'foogallery' ), |
| 88 | ), |
| 89 | ), |
| 90 | ), |
| 91 | 'execute_callback' => array( $this, 'execute' ), |
| 92 | 'permission_callback' => array( $this, 'can_execute' ), |
| 93 | 'meta' => array( |
| 94 | 'annotations' => array( |
| 95 | 'readonly' => false, |
| 96 | 'destructive' => true, |
| 97 | 'idempotent' => true, |
| 98 | ), |
| 99 | 'show_in_rest' => true, |
| 100 | ), |
| 101 | ) |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Check if the current user can execute the ability. |
| 107 | * |
| 108 | * @param array $args Ability arguments. |
| 109 | * |
| 110 | * @return bool |
| 111 | */ |
| 112 | public function can_execute( $args ) { |
| 113 | $args = foogallery_abilities_normalize_input( $args ); |
| 114 | |
| 115 | $gallery_id = isset( $args['gallery_id'] ) ? absint( $args['gallery_id'] ) : 0; |
| 116 | |
| 117 | return $gallery_id > 0 && current_user_can( 'edit_post', $gallery_id ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Execute the ability. |
| 122 | * |
| 123 | * @param array $args Ability arguments. |
| 124 | * |
| 125 | * @return array|WP_Error |
| 126 | */ |
| 127 | public function execute( $args ) { |
| 128 | $args = foogallery_abilities_normalize_input( $args ); |
| 129 | $gallery_id = isset( $args['gallery_id'] ) ? absint( $args['gallery_id'] ) : 0; |
| 130 | $changes = array(); |
| 131 | |
| 132 | if ( array_key_exists( 'replace_attachment_ids', $args ) ) { |
| 133 | $changes['replace'] = foogallery_normalize_attachment_ids( $args['replace_attachment_ids'] ); |
| 134 | } |
| 135 | |
| 136 | if ( array_key_exists( 'append_attachment_ids', $args ) ) { |
| 137 | $changes['add'] = foogallery_normalize_attachment_ids( $args['append_attachment_ids'] ); |
| 138 | } |
| 139 | |
| 140 | if ( array_key_exists( 'remove_attachment_ids', $args ) ) { |
| 141 | $changes['remove'] = foogallery_normalize_attachment_ids( $args['remove_attachment_ids'] ); |
| 142 | } |
| 143 | |
| 144 | $new_attachment_ids = foogallery_update_gallery_attachments( |
| 145 | $gallery_id, |
| 146 | $changes, |
| 147 | array( |
| 148 | 'ability' => self::ID, |
| 149 | ) |
| 150 | ); |
| 151 | |
| 152 | if ( is_wp_error( $new_attachment_ids ) ) { |
| 153 | return foogallery_abilities_map_gallery_management_error( $new_attachment_ids ); |
| 154 | } |
| 155 | |
| 156 | $gallery = foogallery_abilities_get_gallery( $gallery_id ); |
| 157 | |
| 158 | if ( is_wp_error( $gallery ) ) { |
| 159 | return $gallery; |
| 160 | } |
| 161 | |
| 162 | return array( |
| 163 | 'gallery' => foogallery_abilities_prepare_gallery_summary( $gallery ), |
| 164 | 'attachment_ids' => $gallery->item_attachment_ids(), |
| 165 | 'attachment_count' => intval( $gallery->attachment_count() ), |
| 166 | ); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 |