Make WordPress Core

Ticket #13905: check-object-existence-in-caps-logic.13905.2.diff

File check-object-existence-in-caps-logic.13905.2.diff, 9.4 KB (added by filosofo, 14 years ago)
  • wp-includes/capabilities.php

     
    824824        case 'delete_page':
    825825                $author_data = get_userdata( $user_id );
    826826                $post = get_post( $args[0] );
    827                 $post_type = get_post_type_object( $post->post_type );
    828827
    829                 if ( 'delete_post' == $cap && $post_type && 'post' != $post_type->capability_type && ! $post_type->map_meta_cap ) {
    830                         $args = array_merge( array( $post_type->cap->delete_post, $user_id ), $args );
    831                         return call_user_func_array( 'map_meta_cap', $args );
    832                 }
     828                if ( ! empty( $post->post_type ) ) {
     829                        $post_type = get_post_type_object( $post->post_type );
    833830
    834                 if ( '' != $post->post_author && post_type_supports( $post_type->name, 'author' ) ) {
    835                         $post_author_data = get_userdata( $post->post_author );
    836                 } else {
    837                         // No author set yet or post type doesn't support authors,
    838                         // so default to current user for cap checks.
    839                         $post_author_data = $author_data;
    840                 }
     831                        if ( 'delete_post' == $cap && $post_type && 'post' != $post_type->capability_type && ! $post_type->map_meta_cap ) {
     832                                $args = array_merge( array( $post_type->cap->delete_post, $user_id ), $args );
     833                                return call_user_func_array( 'map_meta_cap', $args );
     834                        }
    841835
    842                 // If the user is the author...
    843                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
    844                         // If the post is published...
    845                         if ( 'publish' == $post->post_status ) {
    846                                 $caps[] = $post_type->cap->delete_published_posts;
    847                         } elseif ( 'trash' == $post->post_status ) {
    848                                 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
     836                        if ( '' != $post->post_author && post_type_supports( $post_type->name, 'author' ) ) {
     837                                $post_author_data = get_userdata( $post->post_author );
     838                        } else {
     839                                // No author set yet or post type doesn't support authors,
     840                                // so default to current user for cap checks.
     841                                $post_author_data = $author_data;
     842                        }
     843
     844                        // If the user is the author...
     845                        if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
     846                                // If the post is published...
     847                                if ( 'publish' == $post->post_status ) {
    849848                                        $caps[] = $post_type->cap->delete_published_posts;
     849                                } elseif ( 'trash' == $post->post_status ) {
     850                                        if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
     851                                                $caps[] = $post_type->cap->delete_published_posts;
     852                                } else {
     853                                        // If the post is draft...
     854                                        $caps[] = $post_type->cap->delete_posts;
     855                                }
    850856                        } else {
    851                                 // If the post is draft...
    852                                 $caps[] = $post_type->cap->delete_posts;
     857                                // The user is trying to edit someone else's post.
     858                                $caps[] = $post_type->cap->delete_others_posts;
     859                                // The post is published, extra cap required.
     860                                if ( 'publish' == $post->post_status )
     861                                        $caps[] = $post_type->cap->delete_published_posts;
     862                                elseif ( 'private' == $post->post_status )
     863                                        $caps[] = $post_type->cap->delete_private_posts;
    853864                        }
    854                 } else {
    855                         // The user is trying to edit someone else's post.
    856                         $caps[] = $post_type->cap->delete_others_posts;
    857                         // The post is published, extra cap required.
    858                         if ( 'publish' == $post->post_status )
    859                                 $caps[] = $post_type->cap->delete_published_posts;
    860                         elseif ( 'private' == $post->post_status )
    861                                 $caps[] = $post_type->cap->delete_private_posts;
    862865                }
    863866                break;
    864867                // edit_post breaks down to edit_posts, edit_published_posts, or
     
    867870        case 'edit_page':
    868871                $author_data = get_userdata( $user_id );
    869872                $post = get_post( $args[0] );
    870                 $post_type = get_post_type_object( $post->post_type );
     873               
     874                if ( ! empty( $post->post_type ) ) {
    871875
    872                 if ( 'edit_post' == $cap && $post_type && 'post' != $post_type->capability_type && ! $post_type->map_meta_cap ) {
    873                         $args = array_merge( array( $post_type->cap->edit_post, $user_id ), $args );
    874                         return call_user_func_array( 'map_meta_cap', $args );
    875                 }
     876                        $post_type = get_post_type_object( $post->post_type );
    876877
    877                 if ( '' != $post->post_author && post_type_supports( $post_type->name, 'author' ) ) {
    878                         $post_author_data = get_userdata( $post->post_author );
    879                 } else {
    880                         // No author set yet or post type doesn't support authors,
    881                         // so default to current user for cap checks.
    882                         $post_author_data = $author_data;
    883                 }
     878                        if ( 'edit_post' == $cap && $post_type && 'post' != $post_type->capability_type && ! $post_type->map_meta_cap ) {
     879                                $args = array_merge( array( $post_type->cap->edit_post, $user_id ), $args );
     880                                return call_user_func_array( 'map_meta_cap', $args );
     881                        }
    884882
    885                 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
    886                 // If the user is the author...
    887                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
    888                         // If the post is published...
    889                         if ( 'publish' == $post->post_status ) {
    890                                 $caps[] = $post_type->cap->edit_published_posts;
    891                         } elseif ( 'trash' == $post->post_status ) {
    892                                 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
     883                        if ( '' != $post->post_author && post_type_supports( $post_type->name, 'author' ) ) {
     884                                $post_author_data = get_userdata( $post->post_author );
     885                        } else {
     886                                // No author set yet or post type doesn't support authors,
     887                                // so default to current user for cap checks.
     888                                $post_author_data = $author_data;
     889                        }
     890
     891                        //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
     892                        // If the user is the author...
     893                        if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
     894                                // If the post is published...
     895                                if ( 'publish' == $post->post_status ) {
    893896                                        $caps[] = $post_type->cap->edit_published_posts;
     897                                } elseif ( 'trash' == $post->post_status ) {
     898                                        if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
     899                                                $caps[] = $post_type->cap->edit_published_posts;
     900                                } else {
     901                                        // If the post is draft...
     902                                        $caps[] = $post_type->cap->edit_posts;
     903                                }
    894904                        } else {
    895                                 // If the post is draft...
    896                                 $caps[] = $post_type->cap->edit_posts;
     905                                // The user is trying to edit someone else's post.
     906                                $caps[] = $post_type->cap->edit_others_posts;
     907                                // The post is published, extra cap required.
     908                                if ( 'publish' == $post->post_status )
     909                                        $caps[] = $post_type->cap->edit_published_posts;
     910                                elseif ( 'private' == $post->post_status )
     911                                        $caps[] = $post_type->cap->edit_private_posts;
    897912                        }
    898                 } else {
    899                         // The user is trying to edit someone else's post.
    900                         $caps[] = $post_type->cap->edit_others_posts;
    901                         // The post is published, extra cap required.
    902                         if ( 'publish' == $post->post_status )
    903                                 $caps[] = $post_type->cap->edit_published_posts;
    904                         elseif ( 'private' == $post->post_status )
    905                                 $caps[] = $post_type->cap->edit_private_posts;
    906913                }
    907914                break;
    908915        case 'read_post':
    909916        case 'read_page':
    910917                $author_data = get_userdata( $user_id );
    911918                $post = get_post( $args[0] );
    912                 $post_type = get_post_type_object( $post->post_type );
     919                if ( ! empty( $post->post_type ) ) {
     920                        $post_type = get_post_type_object( $post->post_type );
    913921
    914                 if ( 'read_post' == $cap && $post_type && 'post' != $post_type->capability_type && ! $post_type->map_meta_cap ) {
    915                         $args = array_merge( array( $post_type->cap->read_post, $user_id ), $args );
    916                         return call_user_func_array( 'map_meta_cap', $args );
    917                 }
     922                        if ( 'read_post' == $cap && $post_type && 'post' != $post_type->capability_type && ! $post_type->map_meta_cap ) {
     923                                $args = array_merge( array( $post_type->cap->read_post, $user_id ), $args );
     924                                return call_user_func_array( 'map_meta_cap', $args );
     925                        }
    918926
    919                 if ( 'private' != $post->post_status ) {
    920                         $caps[] = $post_type->cap->read;
    921                         break;
    922                 }
     927                        if ( 'private' != $post->post_status ) {
     928                                $caps[] = $post_type->cap->read;
     929                                break;
     930                        }
    923931
    924                 if ( '' != $post->post_author && post_type_supports( $post_type->name, 'author' ) ) {
    925                         $post_author_data = get_userdata( $post->post_author );
    926                 } else {
    927                         // No author set yet or post type doesn't support authors,
    928                         // so default to current user for cap checks.
    929                         $post_author_data = $author_data;
     932                        if ( '' != $post->post_author && post_type_supports( $post_type->name, 'author' ) ) {
     933                                $post_author_data = get_userdata( $post->post_author );
     934                        } else {
     935                                // No author set yet or post type doesn't support authors,
     936                                // so default to current user for cap checks.
     937                                $post_author_data = $author_data;
     938                        }
     939
     940                        if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
     941                                $caps[] = $post_type->cap->read;
     942                        else
     943                                $caps[] = $post_type->cap->read_private_posts;
    930944                }
    931 
    932                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
    933                         $caps[] = $post_type->cap->read;
    934                 else
    935                         $caps[] = $post_type->cap->read_private_posts;
    936945                break;
    937946        case 'edit_comment':
    938947                $comment = get_comment( $args[0] );
    939                 $post = get_post( $comment->comment_post_ID );
    940                 $post_type_object = get_post_type_object( $post->post_type );
    941948
    942                 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
     949                if ( ! empty( $comment->comment_post_ID ) ) {
     950                        $post = get_post( $comment->comment_post_ID );
     951                        if ( ! empty( $post->post_type ) ) {
     952                                $post_type_object = get_post_type_object( $post->post_type );
     953                                $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
     954                        }
     955                }
    943956                break;
    944957        case 'unfiltered_upload':
    945958                if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) )  )