class-abilities.php
2 months ago
class-ability-create-gallery.php
2 months ago
class-ability-get-gallery-layout-schema.php
2 months ago
class-ability-get-gallery.php
2 months ago
class-ability-list-galleries.php
2 months ago
class-ability-list-gallery-layouts.php
2 months ago
class-ability-media-search.php
2 months ago
class-ability-update-gallery-attachments.php
2 months ago
class-ability-update-gallery.php
2 months ago
functions.php
2 months ago
index.php
2 months ago
class-ability-update-gallery-attachments.php
211 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Update gallery attachments ability. |
| 4 | */ |
| 5 | |
| 6 | if ( ! class_exists( 'FooGallery_Ability_Update_Gallery_Attachments' ) ) { |
| 7 | |
| 8 | class FooGallery_Ability_Update_Gallery_Attachments { |
| 9 | |
| 10 | const ID = 'foogallery/update-gallery-attachments'; |
| 11 | |
| 12 | /** |
| 13 | * Register the ability. |
| 14 | */ |
| 15 | public function __construct() { |
| 16 | if ( foogallery_abilities_wp_api_available() ) { |
| 17 | add_action( 'foogallery_register_abilities', array( $this, 'register' ) ); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Register the ability with the WordPress core Abilities API. |
| 23 | */ |
| 24 | public function register() { |
| 25 | wp_register_ability( |
| 26 | self::ID, |
| 27 | array( |
| 28 | 'category' => FooGallery_Abilities::CATEGORY, |
| 29 | 'label' => __( 'Update Gallery Attachments', 'foogallery' ), |
| 30 | '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' ), |
| 31 | 'input_schema' => array( |
| 32 | 'type' => 'object', |
| 33 | 'required' => array( 'gallery_id' ), |
| 34 | 'properties' => array( |
| 35 | 'gallery_id' => array( |
| 36 | 'type' => 'integer', |
| 37 | 'description' => __( 'The numeric ID of the FooGallery gallery to update.', 'foogallery' ), |
| 38 | ), |
| 39 | 'replace_attachment_ids' => array( |
| 40 | 'type' => 'array', |
| 41 | 'description' => __( 'Replace the gallery attachment list with this exact set of attachment IDs. Do not combine with append/remove.', 'foogallery' ), |
| 42 | 'items' => array( |
| 43 | 'type' => 'integer', |
| 44 | ), |
| 45 | ), |
| 46 | 'append_attachment_ids' => array( |
| 47 | 'type' => 'array', |
| 48 | 'description' => __( 'Attachment IDs to append to the existing gallery without duplicating IDs already present.', 'foogallery' ), |
| 49 | 'items' => array( |
| 50 | 'type' => 'integer', |
| 51 | ), |
| 52 | ), |
| 53 | 'remove_attachment_ids' => array( |
| 54 | 'type' => 'array', |
| 55 | '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' ), |
| 56 | 'items' => array( |
| 57 | 'type' => 'integer', |
| 58 | ), |
| 59 | ), |
| 60 | ), |
| 61 | 'additionalProperties' => false, |
| 62 | ), |
| 63 | 'output_schema' => array( |
| 64 | 'type' => 'object', |
| 65 | 'properties' => array( |
| 66 | 'gallery' => array_merge( |
| 67 | foogallery_abilities_get_gallery_summary_schema(), |
| 68 | array( |
| 69 | 'description' => __( 'The updated gallery summary after the attachment change has been applied.', 'foogallery' ), |
| 70 | ) |
| 71 | ), |
| 72 | 'attachment_ids' => array( |
| 73 | 'type' => 'array', |
| 74 | 'description' => __( 'The gallery attachment IDs after the update.', 'foogallery' ), |
| 75 | 'items' => array( |
| 76 | 'type' => 'integer', |
| 77 | ), |
| 78 | ), |
| 79 | 'attachment_count' => array( |
| 80 | 'type' => 'integer', |
| 81 | 'description' => __( 'The number of attachments remaining in the gallery after the update.', 'foogallery' ), |
| 82 | ), |
| 83 | ), |
| 84 | ), |
| 85 | 'execute_callback' => array( $this, 'execute' ), |
| 86 | 'permission_callback' => array( $this, 'can_execute' ), |
| 87 | 'meta' => array( |
| 88 | 'annotations' => array( |
| 89 | 'readonly' => false, |
| 90 | 'destructive' => true, |
| 91 | 'idempotent' => true, |
| 92 | ), |
| 93 | 'show_in_rest' => true, |
| 94 | ), |
| 95 | ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Check if the current user can execute the ability. |
| 101 | * |
| 102 | * @param array $args Ability arguments. |
| 103 | * |
| 104 | * @return bool |
| 105 | */ |
| 106 | public function can_execute( $args ) { |
| 107 | $args = foogallery_abilities_normalize_input( $args ); |
| 108 | |
| 109 | $gallery_id = isset( $args['gallery_id'] ) ? absint( $args['gallery_id'] ) : 0; |
| 110 | |
| 111 | return $gallery_id > 0 && current_user_can( 'edit_post', $gallery_id ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Execute the ability. |
| 116 | * |
| 117 | * @param array $args Ability arguments. |
| 118 | * |
| 119 | * @return array|WP_Error |
| 120 | */ |
| 121 | public function execute( $args ) { |
| 122 | $args = foogallery_abilities_normalize_input( $args ); |
| 123 | |
| 124 | $gallery = foogallery_abilities_get_gallery( isset( $args['gallery_id'] ) ? $args['gallery_id'] : 0 ); |
| 125 | |
| 126 | if ( is_wp_error( $gallery ) ) { |
| 127 | return $gallery; |
| 128 | } |
| 129 | |
| 130 | if ( $gallery->datasource_name !== foogallery_default_datasource() ) { |
| 131 | return new WP_Error( |
| 132 | 'foogallery_ability_unsupported_datasource', |
| 133 | __( 'This ability currently supports media-library-backed galleries only.', 'foogallery' ) |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | $has_replace = array_key_exists( 'replace_attachment_ids', $args ); |
| 138 | $has_append = array_key_exists( 'append_attachment_ids', $args ); |
| 139 | $has_remove = array_key_exists( 'remove_attachment_ids', $args ); |
| 140 | |
| 141 | if ( ! $has_replace && ! $has_append && ! $has_remove ) { |
| 142 | return new WP_Error( |
| 143 | 'foogallery_ability_no_attachment_changes', |
| 144 | __( 'Provide replace_attachment_ids, append_attachment_ids, remove_attachment_ids, or a combination of append/remove.', 'foogallery' ) |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | if ( $has_replace && ( $has_append || $has_remove ) ) { |
| 149 | return new WP_Error( |
| 150 | 'foogallery_ability_conflicting_attachment_changes', |
| 151 | __( 'replace_attachment_ids cannot be combined with append_attachment_ids or remove_attachment_ids. Use replace on its own, or use append/remove together.', 'foogallery' ) |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | if ( $has_replace ) { |
| 156 | $new_attachment_ids = foogallery_abilities_validate_attachment_ids( $args['replace_attachment_ids'] ); |
| 157 | if ( is_wp_error( $new_attachment_ids ) ) { |
| 158 | return $new_attachment_ids; |
| 159 | } |
| 160 | } else { |
| 161 | $new_attachment_ids = foogallery_abilities_normalize_attachment_ids( $gallery->attachment_ids ); |
| 162 | } |
| 163 | |
| 164 | if ( $has_append ) { |
| 165 | $append_ids = foogallery_abilities_validate_attachment_ids( $args['append_attachment_ids'] ); |
| 166 | if ( is_wp_error( $append_ids ) ) { |
| 167 | return $append_ids; |
| 168 | } |
| 169 | |
| 170 | foreach ( $append_ids as $attachment_id ) { |
| 171 | if ( ! in_array( $attachment_id, $new_attachment_ids, true ) ) { |
| 172 | $new_attachment_ids[] = $attachment_id; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if ( $has_remove ) { |
| 178 | $remove_ids = foogallery_abilities_normalize_attachment_ids( $args['remove_attachment_ids'] ); |
| 179 | $new_attachment_ids = array_values( array_diff( $new_attachment_ids, $remove_ids ) ); |
| 180 | } |
| 181 | |
| 182 | update_post_meta( $gallery->ID, FOOGALLERY_META_ATTACHMENTS, $new_attachment_ids ); |
| 183 | update_post_meta( $gallery->ID, FOOGALLERY_META_DATASOURCE, foogallery_default_datasource() ); |
| 184 | delete_post_meta( $gallery->ID, FOOGALLERY_META_DATASOURCE_VALUE ); |
| 185 | |
| 186 | foogallery_abilities_clear_gallery_cache( $gallery->ID ); |
| 187 | do_action( |
| 188 | 'foogallery_after_save_gallery', |
| 189 | $gallery->ID, |
| 190 | array( |
| 191 | 'ability' => self::ID, |
| 192 | 'gallery_id' => $gallery->ID, |
| 193 | 'attachment_ids' => $new_attachment_ids, |
| 194 | ) |
| 195 | ); |
| 196 | |
| 197 | $gallery = foogallery_abilities_get_gallery( $gallery->ID ); |
| 198 | |
| 199 | if ( is_wp_error( $gallery ) ) { |
| 200 | return $gallery; |
| 201 | } |
| 202 | |
| 203 | return array( |
| 204 | 'gallery' => foogallery_abilities_prepare_gallery_summary( $gallery ), |
| 205 | 'attachment_ids' => $gallery->item_attachment_ids(), |
| 206 | 'attachment_count' => intval( $gallery->attachment_count() ), |
| 207 | ); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 |