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