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