| 865 | |
| 866 | function 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 | } |
| 920 | add_filter( 'user_has_cap', 'wp_maybe_grant_customize_changeset_caps', 1, 4 ); |