admin
3 months ago
compatibility
3 months ago
extensions
3 months ago
foopluginbase
3 months ago
public
3 months ago
thumbs
3 months ago
asset-manifest.php
3 months ago
class-attachment-filters.php
3 months ago
class-command-palette.php
3 months ago
class-foogallery-animated-gif-support.php
3 months ago
class-foogallery-attachment-custom-class.php
3 months ago
class-foogallery-attachment-type.php
3 months ago
class-foogallery-attachment.php
3 months ago
class-foogallery-cache.php
3 months ago
class-foogallery-common-fields.php
3 months ago
class-foogallery-crop-position.php
3 months ago
class-foogallery-datasource-media_library.php
3 months ago
class-foogallery-debug.php
3 months ago
class-foogallery-extensions-compatibility.php
3 months ago
class-foogallery-force-https.php
3 months ago
class-foogallery-lazyload.php
3 months ago
class-foogallery-license-constant-handler.php
3 months ago
class-foogallery-lightbox.php
3 months ago
class-foogallery-paging.php
3 months ago
class-foogallery-password-protect.php
3 months ago
class-foogallery-sitemaps.php
3 months ago
class-foogallery-widget.php
3 months ago
class-foogallery.php
3 months ago
class-gallery-advanced-settings.php
3 months ago
class-il8n.php
3 months ago
class-override-thumbnail.php
3 months ago
class-posttypes.php
3 months ago
class-previews.php
3 months ago
class-retina.php
3 months ago
class-thumbnail-dimensions.php
3 months ago
class-thumbnails.php
3 months ago
class-version-check.php
3 months ago
constants.php
3 months ago
functions.php
3 months ago
includes.php
3 months ago
index.php
3 months ago
render-functions.php
3 months ago
class-posttypes.php
206 lines
| 1 | <?php |
| 2 | /* |
| 3 | * FooGallery Custom Post Types and Custom Taxonomy Registration class |
| 4 | */ |
| 5 | |
| 6 | if ( ! class_exists( 'FooGallery_PostTypes' ) ) { |
| 7 | |
| 8 | class FooGallery_PostTypes { |
| 9 | |
| 10 | const GALLERY_CAPABILITIES = array( |
| 11 | 'edit_post' => 'edit_foogallery', |
| 12 | 'read_post' => 'read_foogallery', |
| 13 | 'delete_post' => 'delete_foogallery', |
| 14 | 'edit_posts' => 'edit_foogalleries', |
| 15 | 'edit_others_posts' => 'edit_others_foogalleries', |
| 16 | 'delete_posts' => 'delete_foogalleries', |
| 17 | 'publish_posts' => 'publish_foogalleries', |
| 18 | 'read_private_posts' => 'read_private_foogalleries', |
| 19 | 'create_posts' => 'create_foogalleries' |
| 20 | ); |
| 21 | |
| 22 | function __construct() { |
| 23 | //register the post types |
| 24 | add_action( 'init', array( $this, 'register' ) ); |
| 25 | |
| 26 | //register the custom capabilities. |
| 27 | add_action( 'admin_init', array( $this, 'add_capabilities' ) ); |
| 28 | |
| 29 | //update post type messages |
| 30 | add_filter( 'post_updated_messages', array( $this, 'update_messages' ) ); |
| 31 | |
| 32 | //update post bulk messages |
| 33 | add_filter( 'bulk_post_updated_messages', array( $this, 'update_bulk_messages' ), 10, 2 ); |
| 34 | |
| 35 | //clear capabilities after option update. |
| 36 | add_action( 'update_option_foogallery', array( $this, 'clear_capabilities' ), 10, 3 ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Registers the custom post type for galleries. |
| 41 | * |
| 42 | * This function is responsible for registering the custom post type 'gallery' used by the FooGallery plugin. |
| 43 | * It now includes custom capabilities and adds filters for custom capability mapping and menu visibility control. |
| 44 | */ |
| 45 | function register() { |
| 46 | |
| 47 | $args = array( |
| 48 | 'labels' => array( |
| 49 | 'name' => __( 'Galleries', 'foogallery' ), |
| 50 | 'singular_name' => __( 'Gallery', 'foogallery' ), |
| 51 | 'add_new' => __( 'Add Gallery', 'foogallery' ), |
| 52 | 'add_new_item' => __( 'Add New Gallery', 'foogallery' ), |
| 53 | 'edit_item' => __( 'Edit Gallery', 'foogallery' ), |
| 54 | 'new_item' => __( 'New Gallery', 'foogallery' ), |
| 55 | 'view_item' => __( 'View Gallery', 'foogallery' ), |
| 56 | 'search_items' => __( 'Search Galleries', 'foogallery' ), |
| 57 | 'not_found' => __( 'No Galleries found', 'foogallery' ), |
| 58 | 'not_found_in_trash' => __( 'No Galleries found in Trash', 'foogallery' ), |
| 59 | 'menu_name' => foogallery_plugin_name(), |
| 60 | 'all_items' => __( 'Galleries', 'foogallery' ), |
| 61 | ), |
| 62 | 'hierarchical' => false, |
| 63 | 'public' => false, // set to false to make the post type private |
| 64 | 'rewrite' => false, |
| 65 | 'show_ui' => true, |
| 66 | 'show_in_menu' => true, |
| 67 | 'show_in_rest' => true, |
| 68 | 'rest_base' => 'foogallery', |
| 69 | 'menu_icon' => 'dashicons-format-gallery', |
| 70 | 'supports' => array( 'title', 'thumbnail' ), |
| 71 | 'capabilities' => FooGallery_PostTypes::GALLERY_CAPABILITIES |
| 72 | ); |
| 73 | |
| 74 | $args = apply_filters( 'foogallery_gallery_posttype_register_args', $args ); |
| 75 | register_post_type( FOOGALLERY_CPT_GALLERY, $args ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Adds capabilities to the allowed roles, based on the gallery creator role that is set. |
| 80 | * |
| 81 | * @return void |
| 82 | */ |
| 83 | function add_capabilities( $force = false ) { |
| 84 | global $foogallery_adding_capabilities; |
| 85 | |
| 86 | $gallery_creator_role = foogallery_setting_gallery_creator_role(); |
| 87 | $previous_capabilities = get_option('foogallery_capabilities_set' ); |
| 88 | |
| 89 | if ( $force || $gallery_creator_role !== $previous_capabilities ) { |
| 90 | |
| 91 | $foogallery_adding_capabilities = true; |
| 92 | update_option( 'foogallery_capabilities_set', $gallery_creator_role ); |
| 93 | $foogallery_adding_capabilities = false; |
| 94 | |
| 95 | // Get the roles |
| 96 | $roles = foogallery_get_roles_and_higher( $gallery_creator_role ); |
| 97 | |
| 98 | foreach ( $roles as $the_role ) { |
| 99 | $role = get_role( $the_role ); |
| 100 | |
| 101 | if ( !is_null( $role ) ) { |
| 102 | |
| 103 | foreach ( FooGallery_PostTypes::GALLERY_CAPABILITIES as $cap_key => $cap ) { |
| 104 | $role->add_cap( $cap ); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Clears the capabilities based on the new value, old value, and option. |
| 113 | * |
| 114 | * @param mixed $old_value The old value. |
| 115 | * @param mixed $value The new value. |
| 116 | * @param string $option The option. |
| 117 | */ |
| 118 | function clear_capabilities( $old_value, $value, $option ) { |
| 119 | global $foogallery_adding_capabilities; |
| 120 | // Get out early, if we are busy updating capabilities. |
| 121 | if ( $foogallery_adding_capabilities ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | if ( $old_value === $value ) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | $gallery_creator_role = foogallery_setting_gallery_creator_role(); |
| 130 | $previous_capabilities = get_option('foogallery_capabilities_set' ); |
| 131 | |
| 132 | if ( $gallery_creator_role !== $previous_capabilities ) { |
| 133 | |
| 134 | // Get all roles |
| 135 | $roles = wp_roles()->get_names(); |
| 136 | |
| 137 | // Loop through each role and remove the capabilities |
| 138 | foreach ( $roles as $role => $name ) { |
| 139 | $role_obj = get_role($role); |
| 140 | if (!is_null($role_obj)) { |
| 141 | foreach ( FooGallery_PostTypes::GALLERY_CAPABILITIES as $cap_key => $cap ) { |
| 142 | $role_obj->remove_cap($cap); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | do_action('foogallery_gallery_posttype_clear_capabilities'); |
| 148 | $this->add_capabilities( true ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Customize the update messages for a gallery. |
| 154 | * |
| 155 | * @global object $post The current post object. |
| 156 | * |
| 157 | * @param array $messages Array of default post updated messages. |
| 158 | * |
| 159 | * @return array $messages Amended array of post updated messages. |
| 160 | */ |
| 161 | public function update_messages( $messages ) { |
| 162 | global $post; |
| 163 | |
| 164 | // Add our gallery messages. |
| 165 | $messages[FOOGALLERY_CPT_GALLERY] = apply_filters( 'foogallery_gallery_posttype_update_messages', |
| 166 | array( |
| 167 | 0 => '', |
| 168 | 1 => __( 'Gallery updated.', 'foogallery' ), |
| 169 | 2 => __( 'Gallery custom field updated.', 'foogallery' ), |
| 170 | 3 => __( 'Gallery custom field deleted.', 'foogallery' ), |
| 171 | 4 => __( 'Gallery updated.', 'foogallery' ), |
| 172 | 5 => isset($_GET['revision']) ? sprintf( __( 'Gallery restored to revision from %s.', 'foogallery' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
| 173 | 6 => __( 'Gallery published.', 'foogallery' ), |
| 174 | 7 => __( 'Gallery saved.', 'foogallery' ), |
| 175 | 8 => __( 'Gallery submitted.', 'foogallery' ), |
| 176 | 9 => sprintf( __( 'Gallery scheduled for: <strong>%1$s</strong>.', 'foogallery' ), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) ), |
| 177 | 10 => __( 'Gallery draft updated.', 'foogallery' ) |
| 178 | ) |
| 179 | ); |
| 180 | |
| 181 | return $messages; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Customize the bulk update messages for a gallery |
| 186 | * |
| 187 | * @param array $bulk_messages Array of default bulk updated messages. |
| 188 | * @param array $bulk_counts Array containing count of posts involved in the action. |
| 189 | * |
| 190 | * @return array mixed Amended array of bulk updated messages. |
| 191 | */ |
| 192 | function update_bulk_messages( $bulk_messages, $bulk_counts ) { |
| 193 | $bulk_messages[FOOGALLERY_CPT_GALLERY] = apply_filters( 'foogallery_gallery_posttype_bulk_update_messages', |
| 194 | array( |
| 195 | 'updated' => _n( '%s Gallery updated.', '%s Galleries updated.', $bulk_counts['updated'], 'foogallery' ), |
| 196 | 'locked' => _n( '%s Gallery not updated, somebody is editing it.', '%s Galleries not updated, somebody is editing them.', $bulk_counts['locked'], 'foogallery' ), |
| 197 | 'deleted' => _n( '%s Gallery permanently deleted.', '%s Galleries permanently deleted.', $bulk_counts['deleted'], 'foogallery' ), |
| 198 | 'trashed' => _n( '%s Gallery moved to the Trash.', '%s Galleries moved to the Trash.', $bulk_counts['trashed'], 'foogallery' ), |
| 199 | 'untrashed' => _n( '%s Gallery restored from the Trash.', '%s Galleries restored from the Trash.', $bulk_counts['untrashed'], 'foogallery' ), |
| 200 | ) |
| 201 | ); |
| 202 | |
| 203 | return $bulk_messages; |
| 204 | } |
| 205 | } |
| 206 | } |