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