PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.2.6
Gallery : FooGallery v3.2.6
3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / abilities / class-ability-update-gallery.php
foogallery / includes / abilities Last commit date
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.php
234 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Update gallery ability.
9 */
10
11 if ( ! class_exists( 'FooGallery_Ability_Update_Gallery' ) ) {
12
13 class FooGallery_Ability_Update_Gallery {
14
15 const ID = 'foogallery/update-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' => __( 'Update Gallery', 'foogallery' ),
36 'description' => __( 'Update an existing FooGallery gallery by `gallery_id`. Use `layout` to switch layouts, derive valid `settings` IDs from the `foogallery/get-gallery-layout-schema` tool for that layout, and pass `settings` as an array of setting update objects. You can also update `title`, `status`, `sort`, or `custom_css`.', '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 'title' => array(
46 'type' => 'string',
47 'description' => __( 'Optional replacement gallery title.', 'foogallery' ),
48 ),
49 'status' => array(
50 'type' => 'string',
51 'enum' => array( 'draft', 'publish', 'private' ),
52 'description' => __( 'Optional replacement post status for the gallery.', 'foogallery' ),
53 ),
54 'layout' => array(
55 'type' => 'string',
56 'description' => __( 'Optional gallery layout slug. When changed, FooGallery rebuilds the stored settings base for the new layout before applying any supplied `settings`.', 'foogallery' ),
57 ),
58 'settings' => array(
59 'type' => 'array',
60 '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 active or target layout.', 'foogallery' ),
61 'items' => foogallery_abilities_get_setting_update_schema(),
62 ),
63 'sort' => array(
64 'type' => 'string',
65 'description' => __( 'Optional FooGallery sort mode to store for the gallery.', 'foogallery' ),
66 ),
67 'custom_css' => array(
68 'type' => 'string',
69 'description' => __( 'Optional custom CSS to save with the gallery. Pass an empty string to clear it.', 'foogallery' ),
70 ),
71 ),
72 ),
73 'output_schema' => array(
74 'type' => 'object',
75 'properties' => array(
76 'gallery' => array_merge(
77 foogallery_abilities_get_gallery_detail_schema( false ),
78 array(
79 'description' => __( 'The updated gallery record after all changes have been applied.', 'foogallery' ),
80 )
81 ),
82 ),
83 ),
84 'execute_callback' => array( $this, 'execute' ),
85 'permission_callback' => array( $this, 'can_execute' ),
86 'meta' => array(
87 'annotations' => array(
88 'readonly' => false,
89 'destructive' => true,
90 'idempotent' => true,
91 ),
92 'show_in_rest' => true,
93 ),
94 )
95 );
96 }
97
98 /**
99 * Check if the current user can execute the ability.
100 *
101 * @param array $args Ability arguments.
102 *
103 * @return bool
104 */
105 public function can_execute( $args ) {
106 $args = foogallery_abilities_normalize_input( $args );
107
108 $gallery_id = isset( $args['gallery_id'] ) ? absint( $args['gallery_id'] ) : 0;
109 if ( $gallery_id <= 0 || ! current_user_can( 'edit_post', $gallery_id ) ) {
110 return false;
111 }
112
113 if ( isset( $args['status'] ) ) {
114 $status = foogallery_abilities_sanitize_gallery_status( $args['status'], '' );
115 if ( 'publish' === $status && ! current_user_can( 'publish_foogalleries' ) ) {
116 return false;
117 }
118 }
119
120 return true;
121 }
122
123 /**
124 * Execute the ability.
125 *
126 * @param array $args Ability arguments.
127 *
128 * @return array|WP_Error
129 */
130 public function execute( $args ) {
131 $args = foogallery_abilities_normalize_input( $args );
132
133 $gallery = foogallery_abilities_get_gallery( isset( $args['gallery_id'] ) ? $args['gallery_id'] : 0 );
134
135 if ( is_wp_error( $gallery ) ) {
136 return $gallery;
137 }
138
139 $target_template = foogallery_abilities_get_requested_layout( $args, $gallery->gallery_template );
140 $template = foogallery_abilities_get_template( $target_template );
141
142 if ( is_wp_error( $template ) ) {
143 return $template;
144 }
145
146 if ( array_key_exists( 'title', $args ) || array_key_exists( 'status', $args ) ) {
147 $post_update = array(
148 'ID' => $gallery->ID,
149 );
150
151 if ( array_key_exists( 'title', $args ) ) {
152 $post_update['post_title'] = sanitize_text_field( $args['title'] );
153 }
154
155 if ( array_key_exists( 'status', $args ) ) {
156 $post_update['post_status'] = foogallery_abilities_sanitize_gallery_status( $args['status'], $gallery->post_status );
157 }
158
159 $result = wp_update_post( $post_update, true );
160
161 if ( is_wp_error( $result ) ) {
162 return $result;
163 }
164 }
165
166 $request_context = array(
167 'ability' => self::ID,
168 'gallery_id' => $gallery->ID,
169 'layout' => $template['slug'],
170 'template' => $template['slug'],
171 'settings' => foogallery_abilities_prepare_template_setting_updates(
172 $template['slug'],
173 isset( $args['settings'] ) ? $args['settings'] : array()
174 ),
175 );
176
177 if ( is_wp_error( $request_context['settings'] ) ) {
178 return $request_context['settings'];
179 }
180
181 if ( $gallery->gallery_template === $template['slug'] ) {
182 $settings = array_merge(
183 foogallery_abilities_build_template_settings_base( $template['slug'] ),
184 is_array( $gallery->settings ) ? $gallery->settings : array()
185 );
186 } else {
187 $settings = foogallery_abilities_build_template_settings_base( $template['slug'] );
188 }
189
190 $settings = foogallery_abilities_normalize_template_settings(
191 $template['slug'],
192 $request_context['settings'],
193 $settings,
194 $gallery->ID,
195 $request_context
196 );
197
198 update_post_meta( $gallery->ID, FOOGALLERY_META_TEMPLATE, $template['slug'] );
199 update_post_meta( $gallery->ID, FOOGALLERY_META_SETTINGS, $settings );
200
201 if ( array_key_exists( 'sort', $args ) ) {
202 $sort = foogallery_abilities_sanitize_gallery_sort( $args['sort'] );
203 if ( '' === $sort ) {
204 delete_post_meta( $gallery->ID, FOOGALLERY_META_SORT );
205 } else {
206 update_post_meta( $gallery->ID, FOOGALLERY_META_SORT, $sort );
207 }
208 }
209
210 if ( array_key_exists( 'custom_css', $args ) ) {
211 $custom_css = foogallery_sanitize_full( $args['custom_css'] );
212 if ( '' === $custom_css ) {
213 delete_post_meta( $gallery->ID, FOOGALLERY_META_CUSTOM_CSS );
214 } else {
215 update_post_meta( $gallery->ID, FOOGALLERY_META_CUSTOM_CSS, $custom_css );
216 }
217 }
218
219 foogallery_abilities_clear_gallery_cache( $gallery->ID );
220 do_action( 'foogallery_after_save_gallery', $gallery->ID, $request_context );
221
222 $gallery = foogallery_abilities_get_gallery( $gallery->ID );
223
224 if ( is_wp_error( $gallery ) ) {
225 return $gallery;
226 }
227
228 return array(
229 'gallery' => foogallery_abilities_prepare_gallery_details( $gallery ),
230 );
231 }
232 }
233 }
234