Make WordPress Core


Ignore:
Timestamp:
08/12/2010 11:21:05 PM (15 years ago)
Author:
scribu
Message:

Introduce WP_List_Table::check_permissions() and WP_List_Table::prepare_items(). Fixes #14596; See #14579

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/default-list-tables.php

    r15491 r15496  
    2323    var $_hierarchical_display;
    2424
    25     function WP_Posts_Table( $context = 'normal' ) {
    26         global $post_type_object, $post_type, $avail_post_stati, $wp_query, $per_page, $mode, $current_screen;
     25    function WP_Posts_Table() {
     26        global $post_type_object, $post_type, $current_screen;
    2727
    2828        if ( !isset( $_REQUEST['post_type'] ) )
     
    3636        $post_type_object = get_post_type_object( $post_type );
    3737
    38         if ( !current_user_can( $post_type_object->cap->edit_posts ) )
    39             wp_die( __( 'Cheatin’ uh?' ) );
    40 
    4138        parent::WP_List_Table( array(
    4239            'screen' => $current_screen,
    4340            'plural' => 'posts',
    4441        ) );
    45 
    46         if ( 'normal' != $context )
    47             return;
     42    }
     43
     44    function check_permissions() {
     45        global $post_type_object;
     46
     47        if ( !current_user_can( $post_type_object->cap->edit_posts ) )
     48            wp_die( __( 'Cheatin’ uh?' ) );
     49    }
     50
     51    function prepare_items() {
     52        global $post_type_object, $post_type, $avail_post_stati, $wp_query, $per_page, $mode;
    4853
    4954        $avail_post_stati = wp_edit_posts_query();
     
    10731078class WP_Media_Table extends WP_List_Table {
    10741079
    1075     function WP_Media_Table( $context = 'normal' ) {
    1076         global $wpdb, $wp_query, $detached, $post_mime_types, $avail_post_mime_types;
    1077 
    1078         if ( isset( $_REQUEST['find_detached'] ) ) {
    1079             if ( !current_user_can( 'edit_posts' ) )
    1080                 wp_die( __( 'You are not allowed to scan for lost attachments.' ) );
    1081 
    1082             $lost = $wpdb->get_col( "
    1083                 SELECT ID FROM $wpdb->posts
    1084                 WHERE post_type = 'attachment' AND post_parent > '0'
    1085                 AND post_parent NOT IN (
    1086                     SELECT ID FROM $wpdb->posts
    1087                     WHERE post_type NOT IN ( 'attachment', '" . join( "', '", get_post_types( array( 'public' => false ) ) ) . "' )
    1088                 )
    1089             " );
    1090 
    1091             $_REQUEST['detached'] = 1;
    1092         }
    1093 
    1094         $detached = isset( $_REQUEST['detached'] );
     1080    function WP_Media_Table() {
     1081        global $detached;
     1082
     1083        $detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
    10951084
    10961085        parent::WP_List_Table( array(
     
    10981087            'plural' => 'media'
    10991088        ) );
    1100 
    1101         if ( 'normal' != $context )
    1102             return;
     1089    }
     1090
     1091    function check_permissions() {
     1092        if ( !current_user_can('upload_files') )
     1093            wp_die( __( 'You do not have permission to upload files.' ) );
     1094    }       
     1095
     1096    function prepare_items() {
     1097        global $lost, $wpdb, $wp_query, $post_mime_types, $avail_post_mime_types;
    11031098
    11041099        $q = $_REQUEST;
     
    13581353        } else {
    13591354?>
    1360             <td <?php echo $attributes ?>><?php _e( '( Unattached )' ); ?><br />
     1355            <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
    13611356            <a class="hide-if-no-js" onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e( 'Attach' ); ?></a></td>
    13621357<?php
     
    15031498    var $callback_args;
    15041499
    1505     function WP_Terms_Table( $context = 'normal' ) {
     1500    function WP_Terms_Table() {
    15061501        global $post_type, $taxonomy, $tax, $current_screen;
     1502
     1503        wp_reset_vars( array( 'action', 'taxonomy', 'post_type' ) );
     1504
     1505        if ( empty( $taxonomy ) )
     1506            $taxonomy = 'post_tag';
     1507
     1508        if ( !taxonomy_exists( $taxonomy ) )
     1509            wp_die( __( 'Invalid taxonomy' ) );
     1510
     1511        $tax = get_taxonomy( $taxonomy );
     1512
     1513        if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'public' => true ) ) ) )
     1514            $post_type = 'post';
     1515
     1516        if ( !isset( $current_screen ) )
     1517            set_current_screen( 'edit-' . $taxonomy );
    15071518
    15081519        parent::WP_List_Table( array(
     
    15111522            'singular' => 'tag',
    15121523        ) );
    1513 
    1514         if ( 'normal' != $context )
    1515             return;
    1516 
    1517         wp_reset_vars( array( 'action', 'tag', 'taxonomy', 'post_type' ) );
    1518 
    1519         if ( empty( $taxonomy ) )
    1520             $taxonomy = 'post_tag';
    1521 
    1522         if ( !taxonomy_exists( $taxonomy ) )
    1523             wp_die( __( 'Invalid taxonomy' ) );
    1524 
    1525         $tax = get_taxonomy( $taxonomy );
    1526 
    1527         if ( ! current_user_can( $tax->cap->manage_terms ) )
     1524    }
     1525
     1526    function check_permissions( $type = 'manage' ) {
     1527        global $tax;
     1528
     1529        $cap = 'manage' == $type ? $tax->cap->manage_terms : $tax->cap->edit_terms;
     1530
     1531        if ( !current_user_can( $tax->cap->manage_terms ) )
    15281532            wp_die( __( 'Cheatin&#8217; uh?' ) );
    1529 
    1530         if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'public' => true ) ) ) )
    1531             $post_type = 'post';
     1533    }
     1534
     1535    function prepare_items() {
     1536        global $taxonomy;
    15321537
    15331538        $tags_per_page = (int) get_user_option( 'edit_' .  $taxonomy . '_per_page' );
     
    18521857class WP_Users_Table extends WP_List_Table {
    18531858
    1854     function WP_Users_Table( $context = 'normal' ) {
     1859    function WP_Users_Table() {
    18551860        global $role, $usersearch;
    18561861
     
    18591864            'plural' => 'users'
    18601865        ) );
    1861 
    1862         if ( 'normal' != $context )
    1863             return;
    1864 
     1866    }
     1867
     1868    function check_permissions() {
     1869        if ( !current_user_can('list_users') )
     1870            wp_die(__('Cheatin&#8217; uh?'));
     1871    }
     1872
     1873    function prepare_items() {
    18651874        $usersearch = isset( $_REQUEST['s'] ) ? $_REQUEST['s'] : '';
    18661875
     
    20902099class WP_Comments_Table extends WP_List_Table {
    20912100
    2092     function WP_Comments_Table( $context = 'normal' ) {
    2093         global $comments, $extra_comments, $total_comments, $post_id, $comment_status, $mode;
    2094 
    2095         $mode = ( empty( $_REQUEST['mode'] ) ) ? 'detail' : $_REQUEST['mode'];
    2096 
     2101    function WP_Comments_Table() {
    20972102        parent::WP_List_Table( array(
    20982103            'screen' => 'edit-comments',
    20992104            'plural' => 'comments'
    21002105        ) );
    2101 
    2102         if ( 'normal' != $context )
    2103             return;
     2106    }
     2107
     2108    function check_permissions() {
     2109        if ( !current_user_can('edit_posts') )
     2110            wp_die(__('Cheatin&#8217; uh?'));
     2111    }
     2112
     2113    function prepare_items() {
     2114        global $comments, $extra_comments, $total_comments, $post_id, $comment_status, $mode;
    21042115
    21052116        $post_id = isset( $_REQUEST['post_ID'] ) ? absint( $_REQUEST['post_ID'] ) : 0;
     2117
     2118        $mode = ( empty( $_REQUEST['mode'] ) ) ? 'detail' : $_REQUEST['mode'];
    21062119
    21072120        $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
     
    25092522
    25102523    function WP_Links_Table() {
     2524        parent::WP_List_Table( array(
     2525            'screen' => 'link-manager',
     2526            'plural' => 'bookmarks',
     2527        ) );
     2528    }
     2529
     2530    function check_permissions() {
     2531        if ( ! current_user_can( 'manage_links' ) )
     2532            wp_die( __( 'You do not have sufficient permissions to edit the links for this site.' ) );
     2533    }
     2534
     2535    function prepare_items() {
    25112536        global $cat_id, $s, $orderby, $order;
    25122537
     
    25252550
    25262551        $this->items = get_bookmarks( $args );
    2527 
    2528         parent::WP_List_Table( array(
    2529             'screen' => 'link-manager',
    2530             'plural' => 'bookmarks',
    2531         ) );
    2532     }
     2552    }       
    25332553
    25342554    function no_items() {
     
    26912711
    26922712    function WP_Sites_Table() {
     2713        parent::WP_List_Table( array(
     2714            'screen' => 'ms-sites',
     2715            'plural' => 'sites',
     2716        ) );
     2717    }
     2718
     2719    function check_permissions() {     
     2720        if ( ! current_user_can( 'manage_sites' ) )
     2721            wp_die( __( 'You do not have permission to access this page.' ) );
     2722    }
     2723
     2724    function prepare_items() {
    26932725        global $s, $mode, $wpdb;
    26942726
    26952727        $mode = ( empty( $_REQUEST['mode'] ) ) ? 'list' : $_REQUEST['mode'];
    2696 
    2697         parent::WP_List_Table( array(
    2698             'callback' => 'site_rows',
    2699             'screen' => 'ms-sites',
    2700         ) );
    27012728
    27022729        $pagenum = $this->get_pagenum();
     
    29793006
    29803007    function WP_MS_Users_Table() {
    2981         global $s, $mode, $wpdb;
    2982 
    2983         $mode = ( empty( $_REQUEST['mode'] ) ) ? 'list' : $_REQUEST['mode'];
    2984 
    29853008        parent::WP_List_Table( array(
    29863009            'screen' => 'ms-users',
    29873010        ) );
     3011    }
     3012
     3013    function check_permissions() {         
     3014        if ( !is_multisite() )
     3015            wp_die( __( 'Multisite support is not enabled.' ) );
     3016   
     3017        if ( ! current_user_can( 'manage_network_users' ) )
     3018            wp_die( __( 'You do not have permission to access this page.' ) );
     3019    }
     3020
     3021    function prepare_items() {
     3022        global $s, $mode, $wpdb;
     3023
     3024        $mode = ( empty( $_REQUEST['mode'] ) ) ? 'list' : $_REQUEST['mode'];
    29883025
    29893026        $pagenum = $this->get_pagenum();
     
    32023239
    32033240    function WP_Plugins_Table() {
    3204         global $status, $plugins, $totals, $page, $orderby, $order, $s;
    3205 
    32063241        parent::WP_List_Table( array(
    32073242            'screen' => 'plugins',
    32083243            'plural' => 'plugins',
    32093244        ) );
     3245    }
     3246
     3247    function check_permissions() {
     3248        if ( is_multisite() ) {
     3249            $menu_perms = get_site_option( 'menu_items', array() );
     3250
     3251            if ( empty( $menu_perms['plugins'] ) ) {
     3252                if ( !is_super_admin() )
     3253                    wp_die( __( 'Cheatin&#8217; uh?' ) );
     3254            }
     3255        }
     3256
     3257        if ( !current_user_can('activate_plugins') )
     3258            wp_die( __( 'You do not have sufficient permissions to manage plugins for this site.' ) );
     3259    }
     3260
     3261    function prepare_items() {
     3262        global $status, $plugins, $totals, $page, $orderby, $order, $s;
    32103263
    32113264        wp_reset_vars( array( 'orderby', 'order', 's' ) );
    3212 
    3213         $default_status = get_user_option( 'plugins_last_view' );
    3214         if ( empty( $default_status ) )
    3215             $default_status = 'all';
    3216         $status = isset( $_REQUEST['plugin_status'] ) ? $_REQUEST['plugin_status'] : $default_status;
    3217         if ( !in_array( $status, array( 'all', 'active', 'inactive', 'recently_activated', 'upgrade', 'network', 'mustuse', 'dropins', 'search' ) ) )
    3218             $status = 'all';
    3219         if ( $status != $default_status && 'search' != $status )
    3220             update_user_meta( get_current_user_id(), 'plugins_last_view', $status );
    32213265
    32223266        $page = $this->get_pagenum();
     
    35393583
    35403584    function WP_Plugin_Install_Table() {
    3541         include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
    3542 
    3543         global $tabs, $tab, $paged, $type, $term;
    3544 
    35453585        parent::WP_List_Table( array(
    35463586            'screen' => 'plugin-install',
    35473587        ) );
     3588    }
     3589
     3590    function check_permissions() {
     3591        if ( ! current_user_can('install_plugins') )
     3592            wp_die(__('You do not have sufficient permissions to install plugins on this site.'));     
     3593    }
     3594
     3595    function prepare_items() {
     3596        include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
     3597
     3598        global $tabs, $tab, $paged, $type, $term;
    35483599
    35493600        wp_reset_vars( array( 'tab' ) );
     
    37443795
    37453796    function WP_Themes_Table() {
     3797        parent::__construct( array(
     3798            'screen' => 'themes',
     3799        ) );
     3800    }
     3801
     3802    function check_permissions() {
     3803        if ( !current_user_can('switch_themes') && !current_user_can('edit_theme_options') )
     3804            wp_die( __( 'Cheatin&#8217; uh?' ) );
     3805    }
     3806   
     3807    function prepare_items() {
    37463808        global $ct;
    37473809
     
    37583820
    37593821        $this->items = array_slice( $themes, $start, $per_page );
    3760 
    3761         parent::__construct( array(
    3762             'screen' => 'themes',
    3763         ) );
    37643822
    37653823        $this->set_pagination_args( array(
     
    38863944
    38873945    function WP_Theme_Install_Table() {
    3888         include( ABSPATH . 'wp-admin/includes/theme-install.php' );
    3889 
    3890         global $tabs, $tab, $paged, $type, $term, $theme_field_defaults;
    3891 
    38923946        parent::WP_List_Table( array(
    38933947            'screen' => 'theme-install',
    38943948        ) );
    3895 
     3949    }
     3950
     3951    function check_permissions() {
     3952        if ( ! current_user_can('install_themes') )
     3953            wp_die( __( 'You do not have sufficient permissions to install themes on this site.' ) );
     3954    }
     3955
     3956    function prepare_items() {
     3957        include( ABSPATH . 'wp-admin/includes/theme-install.php' );
     3958
     3959        global $tabs, $tab, $paged, $type, $term, $theme_field_defaults;       
     3960       
    38963961        wp_reset_vars( array( 'tab' ) );
    38973962
Note: See TracChangeset for help on using the changeset viewer.