Make WordPress Core

Ticket #40922: 40922.5.diff

File 40922.5.diff, 3.3 KB (added by dlh, 7 years ago)
  • src/wp-includes/capabilities.php

     
    862862
    863863        return $allcaps;
    864864}
     865
     866function wp_maybe_grant_customize_changeset_caps( $allcaps, $caps, $args, $user ) {
     867        global $post_type_meta_caps;
     868
     869        $changeset_post_type = get_post_type_object( 'customize_changeset' );
     870
     871        // Do something only when the capability type is the default.
     872        if ( ( $changeset_post_type instanceof WP_Post_Type ) && ( 'customize_changeset' === $changeset_post_type->capability_type ) ) {
     873                $changeset_caps = get_object_vars( $changeset_post_type->cap );
     874
     875                // Reduce $changeset_caps to post type primitives.
     876                unset( $changeset_caps['read'] );
     877                foreach ( $changeset_caps as $key => $cap ) {
     878                        if ( isset( $post_type_meta_caps[ $cap ] ) ) {
     879                                unset( $changeset_caps[ $key ] );
     880                        }
     881                }
     882
     883                // Do something only when at least one of the required $caps is a changeset primitive.
     884                if ( array_intersect( array_values( $changeset_caps ), $caps ) ) {
     885                        // All changeset capabilities were previously 'customize'.
     886                        $user_can_args = array( $user->ID, 'customize' );
     887
     888                        /*
     889                         * For compatibility with cases when 'customize' was passed a post
     890                         * ID, also pass $args if the object ID corresponds to a changeset.
     891                         */
     892                        if (
     893                                isset( $args[2] )
     894                                && is_numeric( $args[2] )
     895                                && ( intval( $args[2] ) > 0 )
     896                                && ( 'customize_changeset' === get_post_type( $args[2] ) )
     897                        ) {
     898                                $user_can_args = array_merge( $user_can_args, array_slice( $args, 2 ) );
     899                        }
     900
     901                        /*
     902                         * Avoid an infinite loop if 'customize' has been mapped to a
     903                         * changeset primitive. But note that if 'customize' *is* mapped to
     904                         * any changeset primitives, then this function won't be hooked in
     905                         * to dynamically grant the user those primitives.
     906                         */
     907                        remove_filter( 'user_has_cap', 'wp_maybe_grant_customize_changeset_caps', 1, 4 );
     908                        $user_can = call_user_func_array( 'user_can', $user_can_args );
     909                        add_filter( 'user_has_cap', 'wp_maybe_grant_customize_changeset_caps', 1, 4 );
     910
     911                        $allcaps = array_merge(
     912                                $allcaps,
     913                                array_fill_keys( array_values( $changeset_caps ), $user_can )
     914                        );
     915                }
     916        }
     917
     918        return $allcaps;
     919}
     920add_filter( 'user_has_cap', 'wp_maybe_grant_customize_changeset_caps', 1, 4 );
  • src/wp-includes/post.php

     
    176176                'delete_with_user' => false,
    177177                'supports' => array( 'title', 'author' ),
    178178                'capability_type' => 'customize_changeset',
    179                 'capabilities' => array(
    180                         'create_posts' => 'customize',
    181                         'delete_others_posts' => 'customize',
    182                         'delete_post' => 'customize',
    183                         'delete_posts' => 'customize',
    184                         'delete_private_posts' => 'customize',
    185                         'delete_published_posts' => 'customize',
    186                         'edit_others_posts' => 'customize',
    187                         'edit_post' => 'customize',
    188                         'edit_posts' => 'customize',
    189                         'edit_private_posts' => 'customize',
    190                         'edit_published_posts' => 'do_not_allow',
    191                         'publish_posts' => 'customize',
    192                         'read' => 'read',
    193                         'read_post' => 'customize',
    194                         'read_private_posts' => 'customize',
    195                 ),
    196179        ) );
    197180
    198181        register_post_status( 'publish', array(