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 / extensions / albums / class-posttypes.php
foogallery / extensions / albums Last commit date
admin 2 months ago css 2 months ago js 2 months ago public 2 months ago album-default.php 2 months ago album-stack.php 2 months ago class-albums-extension.php 2 months ago class-foogallery-album.php 2 months ago class-posttypes.php 2 months ago foogallery-albums.png 2 months ago functions.php 2 months ago
class-posttypes.php
293 lines
1 <?php
2
3 /**
4 * FooGallery Album Custom Post Types
5 *
6 * @package FooGallery
7 */
8
9 if ( ! class_exists( 'FooGallery_Albums_PostTypes' ) ) {
10 /**
11 * Class FooGallery_Post_Type
12 *
13 * Handles the registration and management of custom post types for FooGallery.
14 */
15 class FooGallery_Albums_PostTypes {
16
17 const ALBUM_CAPABILITIES = array(
18 'edit_post' => 'edit_foogallery-album',
19 'read_post' => 'read_foogallery-album',
20 'delete_post' => 'delete_foogallery-album',
21 'edit_posts' => 'edit_foogallery-albums',
22 'edit_others_posts' => 'edit_others_foogallery-albums',
23 'delete_posts' => 'delete_foogallery-albums',
24 'publish_posts' => 'publish_foogallery-albums',
25 'read_private_posts' => 'read_private_foogallery-albums',
26 'create_posts' => 'create_foogallery-albums'
27 );
28
29 /**
30 * Constructor method.
31 */
32 public function __construct() {
33 // register the post types.
34 add_action( 'init', array( $this, 'register_posttype' ) );
35
36 //register the custom capabilities.
37 add_action( 'admin_init', array( $this, 'add_capabilities' ) );
38
39 // update post type messages.
40 add_filter( 'post_updated_messages', array( $this, 'update_messages' ) );
41
42 // update post bulk messages.
43 add_filter( 'bulk_post_updated_messages', array( $this, 'update_bulk_messages' ), 10, 2 );
44
45 //clear capabilities after option update.
46 add_action( 'update_option_foogallery', array( $this, 'clear_capabilities' ), 20, 3 );
47 }
48
49 /**
50 * Registers the custom post types.
51 */
52 public function register_posttype() {
53
54 $args = array(
55 'labels' => array(
56 'name' => __( 'Albums', 'foogallery' ),
57 'singular_name' => __( 'Album', 'foogallery' ),
58 'add_new' => __( 'Add Album', 'foogallery' ),
59 'add_new_item' => __( 'Add New Album', 'foogallery' ),
60 'edit_item' => __( 'Edit Album', 'foogallery' ),
61 'new_item' => __( 'New Album', 'foogallery' ),
62 'view_item' => __( 'View Album', 'foogallery' ),
63 'search_items' => __( 'Search Albums', 'foogallery' ),
64 'not_found' => __( 'No Albums found', 'foogallery' ),
65 'not_found_in_trash' => __( 'No Albums found in Trash', 'foogallery' ),
66 'all_items' => __( 'Albums', 'foogallery' ),
67 ),
68 'hierarchical' => false,
69 'public' => false,
70 'rewrite' => false,
71 'show_ui' => true,
72 'show_in_rest' => true,
73 'rest_base' => 'foogallery-album',
74 'supports' => array( 'title' ),
75 'map_meta_cap' => true,
76 'show_in_menu' => foogallery_admin_menu_parent_slug(),
77 'capabilities' => FooGallery_Albums_PostTypes::ALBUM_CAPABILITIES
78 );
79
80 $args = apply_filters( 'foogallery_album_posttype_register_args', $args );
81 register_post_type( FOOGALLERY_CPT_ALBUM, $args );
82 }
83
84 /**
85 * Returns the resolved album creator role.
86 *
87 * @return string
88 */
89 private function get_album_creator_role() {
90 $album_creator_role = foogallery_get_setting( 'album_creator_role', 'inherit' );
91
92 if ( 'inherit' === $album_creator_role ) {
93 $album_creator_role = foogallery_setting_gallery_creator_role();
94 }
95
96 return $album_creator_role;
97 }
98
99 /**
100 * Determine if capability syncing can be skipped for administrator-only mode.
101 *
102 * @param string $album_creator_role The resolved album creator role.
103 * @param mixed $previous_capabilities The previously stored capability sync marker.
104 *
105 * @return bool
106 */
107 private function should_skip_admin_only_capability_sync( $album_creator_role, $previous_capabilities ) {
108 if ( 'administrator' !== $album_creator_role || 'administrator' !== $previous_capabilities ) {
109 return false;
110 }
111
112 $role = get_role( 'administrator' );
113
114 if ( is_null( $role ) ) {
115 return false;
116 }
117
118 foreach ( self::ALBUM_CAPABILITIES as $cap ) {
119 if ( ! $role->has_cap( $cap ) ) {
120 return false;
121 }
122 }
123
124 return true;
125 }
126
127 /**
128 * Adds capabilities to the allowed roles, based on the album creator role that is set.
129 *
130 * @return void
131 */
132 function add_capabilities( $force = false ) {
133 global $foogallery_albums_adding_capabilities;
134
135 $album_creator_role = $this->get_album_creator_role();
136 $previous_capabilities = get_option( 'foogallery_albums_capabilities_set' );
137
138 if ( ! $force && $this->should_skip_admin_only_capability_sync( $album_creator_role, $previous_capabilities ) ) {
139 return;
140 }
141
142 $roles = foogallery_get_roles_and_higher( $album_creator_role );
143
144 if ( $force || $album_creator_role !== $previous_capabilities || $this->roles_missing_capabilities( $roles ) ) {
145
146 $foogallery_albums_adding_capabilities = true;
147 update_option( 'foogallery_albums_capabilities_set', $album_creator_role );
148 $foogallery_albums_adding_capabilities = false;
149
150 foreach ( $roles as $the_role ) {
151 $role = get_role( $the_role );
152
153 if ( !is_null( $role ) ) {
154
155 foreach ( FooGallery_Albums_PostTypes::ALBUM_CAPABILITIES as $cap_key => $cap ) {
156 $role->add_cap( $cap );
157 }
158 }
159 }
160
161 wp_get_current_user()->get_role_caps();
162 }
163 }
164
165 /**
166 * Determine if any allowed album roles are missing required album capabilities.
167 *
168 * @param string[] $roles Allowed album roles.
169 *
170 * @return bool
171 */
172 private function roles_missing_capabilities( $roles ) {
173 foreach ( $roles as $the_role ) {
174 $role = get_role( $the_role );
175
176 if ( is_null( $role ) ) {
177 continue;
178 }
179
180 foreach ( FooGallery_Albums_PostTypes::ALBUM_CAPABILITIES as $cap ) {
181 if ( ! $role->has_cap( $cap ) ) {
182 return true;
183 }
184 }
185 }
186
187 return false;
188 }
189
190 /**
191 * Clears the capabilities based on the new value, old value, and option.
192 *
193 * @param mixed $old_value The old value.
194 * @param mixed $value The new value.
195 * @param string $option The option.
196 */
197 function clear_capabilities( $old_value, $value, $option ) {
198 global $foogallery_albums_adding_capabilities;
199 // Get out early, if we are busy updating album capabilities.
200 if ( $foogallery_albums_adding_capabilities ) {
201 return;
202 }
203
204 if ( $old_value === $value ) {
205 return;
206 }
207
208 $album_creator_role = $this->get_album_creator_role();
209 $previous_capabilities = get_option('foogallery_albums_capabilities_set' );
210
211 if ( $this->should_skip_admin_only_capability_sync( $album_creator_role, $previous_capabilities ) ) {
212 return;
213 }
214
215 if ( $album_creator_role !== $previous_capabilities ) {
216 // Get all roles
217 $roles = wp_roles()->get_names();
218
219 // Loop through each role and remove the capabilities
220 foreach ($roles as $role => $name) {
221 $role_obj = get_role($role);
222 if (!is_null($role_obj)) {
223 foreach ( FooGallery_Albums_PostTypes::ALBUM_CAPABILITIES as $cap_key => $cap ) {
224 $role_obj->remove_cap($cap);
225 }
226 }
227 }
228
229 $this->add_capabilities( true );
230 }
231 }
232
233 /**
234 * Customize the update messages for an album
235 *
236 * @global object $post The current post object.
237 *
238 * @param array $messages Array of default post updated messages.
239 *
240 * @return array $messages Amended array of post updated messages.
241 */
242 public function update_messages( $messages ) {
243
244 global $post;
245
246 // Add our album messages.
247 $messages[ FOOGALLERY_CPT_ALBUM ] = apply_filters(
248 'foogallery_album_posttype_update_messages',
249 array(
250 0 => '',
251 1 => __( 'Album updated.', 'foogallery' ),
252 2 => __( 'Album custom field updated.', 'foogallery' ),
253 3 => __( 'Album custom field deleted.', 'foogallery' ),
254 4 => __( 'Album updated.', 'foogallery' ),
255 5 => isset( $_GET['revision'] ) ? sprintf( __( 'Album restored to revision from %s.', 'foogallery' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
256 6 => __( 'Album published.', 'foogallery' ),
257 7 => __( 'Album saved.', 'foogallery' ),
258 8 => __( 'Album submitted.', 'foogallery' ),
259 9 => sprintf( __( 'Album scheduled for: <strong>%1$s</strong>.', 'foogallery' ), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) ),
260 10 => __( 'Album draft updated.', 'foogallery' ),
261 )
262 );
263
264 return $messages;
265
266 }
267
268 /**
269 * Customize the bulk update messages for a album
270 *
271 * @param array $bulk_messages Array of default bulk updated messages.
272 * @param array $bulk_counts Array containing count of posts involved in the action.
273 *
274 * @return array mixed Amended array of bulk updated messages.
275 */
276 public function update_bulk_messages( $bulk_messages, $bulk_counts ) {
277
278 $bulk_messages[ FOOGALLERY_CPT_ALBUM ] = apply_filters(
279 'foogallery_album_posttype_bulk_update_messages',
280 array(
281 'updated' => _n( '%s Album updated.', '%s Albums updated.', $bulk_counts['updated'], 'foogallery' ),
282 'locked' => _n( '%s Album not updated, somebody is editing it.', '%s Albums not updated, somebody is editing them.', $bulk_counts['locked'], 'foogallery' ),
283 'deleted' => _n( '%s Album permanently deleted.', '%s Albums permanently deleted.', $bulk_counts['deleted'], 'foogallery' ),
284 'trashed' => _n( '%s Album moved to the Trash.', '%s Albums moved to the Trash.', $bulk_counts['trashed'], 'foogallery' ),
285 'untrashed' => _n( '%s Album restored from the Trash.', '%s Albums restored from the Trash.', $bulk_counts['untrashed'], 'foogallery' ),
286 )
287 );
288
289 return $bulk_messages;
290 }
291 }
292 }
293