Make WordPress Core

Ticket #44689: 44689.diff

File 44689.diff, 16.2 KB (added by shariqkhan2012, 7 years ago)

Changing the 'My Sites' page to tabular form like other similar screens (Posts list screen, users list screen etc)

  • src/wp-admin/includes/class-wp-ms-mysites-list-table.php

     
     1<?php
     2/**
     3 * List Table API: WP_MS_My_Sites_List_Table class
     4 *
     5 * @package WordPress
     6 * @subpackage Administration
     7 * @since X.Y.Z
     8 */
     9
     10/**
     11 * Core class used to implement displaying sites in a list table for the network admin.
     12 *
     13 * @since X.Y.Z
     14 * @access private
     15 *
     16 * @see WP_List_Table
     17 */
     18class WP_MS_My_Sites_List_Table extends WP_List_Table {
     19
     20        /**
     21         * Constructor.
     22         *
     23         * @since X.Y.Z
     24         *
     25         * @see WP_List_Table::__construct() for more information on default arguments.
     26         *
     27         * @param array $args An associative array of arguments.
     28         */
     29        public function __construct( $args = array() ) {
     30                parent::__construct(
     31                        array(
     32                                'plural' => 'sites',
     33                                'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
     34                        )
     35                );
     36        }
     37
     38        /**
     39         * @return bool
     40         */
     41        public function ajax_user_can() {
     42                //return current_user_can( 'manage_sites' );
     43                return current_user_can( 'read' );
     44        }
     45
     46        /**
     47         * Prepares the list of sites for display.
     48         *
     49         * @since X.Y.Z
     50         *
     51         * @global string $s
     52         * @global string $mode
     53         * @global wpdb   $wpdb
     54         */
     55        public function prepare_items() {
     56                global $s, $mode, $wpdb;
     57
     58                if ( ! empty( $_REQUEST['mode'] ) ) {
     59                        $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
     60                        set_user_setting( 'mysites_list_mode', $mode );
     61                } else {
     62                        $mode = get_user_setting( 'mysites_list_mode', 'list' );
     63                }
     64
     65                $per_page = $this->get_items_per_page( 'my_sites_per_page' );
     66
     67                $pagenum = $this->get_pagenum();
     68
     69                $s    = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
     70                $wild = '';
     71                if ( false !== strpos( $s, '*' ) ) {
     72                        $wild = '*';
     73                        $s    = trim( $s, '*' );
     74                }
     75
     76                /*
     77                 * If the network is large and a search is not being performed, show only
     78                 * the latest sites with no paging in order to avoid expensive count queries.
     79                 */
     80                if ( ! $s && wp_is_large_network() ) {
     81                        if ( ! isset( $_REQUEST['orderby'] ) ) {
     82                                $_GET['orderby'] = $_REQUEST['orderby'] = '';
     83                        }
     84                        if ( ! isset( $_REQUEST['order'] ) ) {
     85                                $_GET['order'] = $_REQUEST['order'] = 'DESC';
     86                        }
     87                }
     88
     89                $user_site_ids = array();
     90                $user_blogs = get_blogs_of_user( get_current_user_id() );
     91                if ( $user_blogs ) {
     92                        foreach ( $user_blogs as $user_blog ) {
     93                                $user_site_ids[] = $user_blog->userblog_id;
     94                        }
     95                }
     96
     97                $args = array(
     98                        'number'     => intval( $per_page ),
     99                        'offset'     => intval( ( $pagenum - 1 ) * $per_page ),
     100                        'network_id' => get_current_network_id(),
     101                );
     102
     103                if( ! empty( $user_site_ids ) ) {
     104                        $args['site__in'] = $user_site_ids;
     105                }
     106
     107                if ( empty( $s ) ) {
     108                        // Nothing to do.
     109                } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) ||
     110                                        preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
     111                                        preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
     112                                        preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
     113                        // IPv4 address
     114                        $sql          = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) );
     115                        $reg_blog_ids = $wpdb->get_col( $sql );
     116
     117                        if ( $reg_blog_ids ) {
     118                                if( ! empty( $user_site_ids ) ) {
     119                                        //$args['site__in'] = array_diff( $user_site_ids, $reg_blog_ids);
     120                                        $args['site__in'] = array_intersect( $user_site_ids, $reg_blog_ids);
     121                                } else {
     122                                        $args['site__in'] = $reg_blog_ids;
     123                                }
     124                                //$args['site__in'] = $reg_blog_ids;
     125                        }
     126                } elseif ( is_numeric( $s ) && empty( $wild ) ) {
     127                        $args['ID'] = $s;
     128                } else {
     129                        $args['search'] = $s;
     130
     131                        if ( ! is_subdomain_install() ) {
     132                                $args['search_columns'] = array( 'path' );
     133                        }
     134                }
     135
     136                $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
     137                if ( 'blogname' === $order_by ) {
     138                        if ( is_subdomain_install() ) {
     139                                $order_by = 'domain';
     140                        } else {
     141                                $order_by = 'path';
     142                        }
     143                } elseif ( ! $order_by ) {
     144                        $order_by = false;
     145                }
     146
     147                $args['orderby'] = $order_by;
     148
     149                if ( $order_by ) {
     150                        $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? 'DESC' : 'ASC';
     151                }
     152
     153                if ( wp_is_large_network() ) {
     154                        $args['no_found_rows'] = true;
     155                } else {
     156                        $args['no_found_rows'] = false;
     157                }
     158
     159                /**
     160                 * Filters the arguments for the site query in the sites list table.
     161                 *
     162                 * @since X.Y.Z
     163                 *
     164                 * @param array $args An array of get_sites() arguments.
     165                 */
     166                $args = apply_filters( 'ms_mysites_list_table_query_args', $args );
     167
     168                $_sites = get_sites( $args );
     169                if ( is_array( $_sites ) ) {
     170                        //TODO: Check if this should be uncommented
     171                        //update_site_cache( $_sites );
     172
     173                        $this->items = array_slice( $_sites, 0, $per_page );
     174                }
     175
     176                $total_sites = get_sites(
     177                        array_merge(
     178                                $args, array(
     179                                        'count'  => true,
     180                                        'offset' => 0,
     181                                        'number' => 0,
     182                                )
     183                        )
     184                );
     185
     186                $this->set_pagination_args(
     187                        array(
     188                                'total_items' => $total_sites,
     189                                'per_page'    => $per_page,
     190                        )
     191                );
     192        }
     193
     194        /**
     195         */
     196        public function no_items() {
     197                _e( 'No sites found.' );
     198        }
     199
     200        /**
     201         * @global string $mode List table view mode.
     202         *
     203         * @param string $which
     204         */
     205        protected function pagination( $which ) {
     206                global $mode;
     207
     208                parent::pagination( $which );
     209
     210                if ( 'top' === $which ) {
     211                        $this->view_switcher( $mode );
     212                }
     213        }
     214
     215        /**
     216         * @return array
     217         */
     218        public function get_columns() {
     219                $sites_columns = array(
     220                        'blogname'    => __( 'Blog name' ),
     221                );
     222
     223                /**
     224                 * Filters the displayed site columns in Sites list table.
     225                 *
     226                 * @since MU (X.Y.Z)
     227                 *
     228                 * @param string[] $sites_columns An array of displayed site columns. Default 'cb', 'blogname',
     229                 */
     230                return apply_filters( 'wpmu_myblogs_columns', $sites_columns );
     231        }
     232
     233        /**
     234         * @return array
     235         */
     236        protected function get_sortable_columns() {
     237                return array(
     238                        'blogname'    => 'blogname',
     239                );
     240        }
     241
     242
     243        /**
     244         * Handles the site name column output.
     245         *
     246         * @since X.Y.Z
     247         *
     248         * @global string $mode List table view mode.
     249         *
     250         * @param array $blog Current site.
     251         */
     252        public function column_blogname( $blog ) {
     253                global $mode;
     254                switch_to_blog( $blog['blog_id'] );
     255                $blogname = get_option( 'blogname' );
     256                echo '<strong>';
     257                echo '<a href="' . esc_url( get_admin_url( $blog['blog_id'] ) ) . '" class="edit">' . $blogname . '</a>';
     258                echo '</strong>';
     259
     260                if ( 'list' !== $mode ) {
     261                        printf(
     262                                /* translators: 1: site name, 2: site tagline. */
     263                                __( ' &#8211; %1$s' ),
     264                                '<em>' . get_option( 'blogdescription ' ) . '</em>'
     265                        );
     266
     267                }
     268                echo apply_filters( 'myblogs_options', '', (object) $blog );
     269                restore_current_blog();
     270        }
     271
     272
     273        /**
     274         * Handles output for the default column.
     275         *
     276         * @since X.Y.Z
     277         *
     278         * @param array  $blog        Current site.
     279         * @param string $column_name Current column name.
     280         */
     281        public function column_default( $blog, $column_name ) {
     282                /**
     283                 * Fires for each registered custom column in the Sites list table.
     284                 *
     285                 * @since X.Y.Z
     286                 *
     287                 * @param string $column_name The name of the column to display.
     288                 * @param int    $blog_id     The site ID.
     289                 */
     290                do_action( 'ms_mysites_custom_column', $column_name, $blog['blog_id'] );
     291        }
     292
     293        /**
     294         * @global string $mode
     295         */
     296        public function display_rows() {
     297                foreach ( $this->items as $blog ) {
     298                        $blog  = $blog->to_array();
     299
     300                        echo "<tr>";
     301                        $this->single_row_columns( $blog );
     302                        echo '</tr>';
     303                }
     304        }
     305
     306        /**
     307         * Gets the name of the default primary column.
     308         *
     309         * @since X.Y.Z
     310         *
     311         * @return string Name of the default primary column, in this case, 'blogname'.
     312         */
     313        protected function get_default_primary_column_name() {
     314                return 'blogname';
     315        }
     316
     317        /**
     318         * Generates and displays row action links.
     319         *
     320         * @since X.Y.Z
     321         *
     322         * @param object $blog        Site being acted upon.
     323         * @param string $column_name Current column name.
     324         * @param string $primary     Primary column name.
     325         * @return string Row actions output.
     326         */
     327        protected function handle_row_actions( $blog, $column_name, $primary ) {
     328                if ( $primary !== $column_name ) {
     329                        return;
     330                }
     331
     332                $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
     333
     334                // Preordered.
     335                $actions = array(
     336                        'backend'    => '',
     337                        'visit'      => '',
     338                );
     339
     340                $actions['backend'] = "<a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a>';             
     341                $actions['visit'] = "<a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='bookmark'>" . __( 'Visit' ) . '</a>';
     342
     343                /**
     344                 * Filters the row links displayed for each site on the My Sites screen.
     345                 *
     346                 * @since MU (3.0.0)
     347                 *
     348                 * @param string $actions   The HTML site link markup.
     349                 * @param object $user_blog An object containing the site data.
     350                 */
     351               
     352                $actions = apply_filters( 'myblogs_blog_actions', implode( ',' , array_filter( $actions ) ),(object) $blog);
     353                $actions = explode( ',' , $actions );
     354                return $this->row_actions( $actions );
     355        }
     356}
  • src/wp-admin/includes/list-table.php

     
    3737                'WP_MS_Sites_List_Table'       => 'ms-sites',
    3838                'WP_MS_Users_List_Table'       => 'ms-users',
    3939                'WP_MS_Themes_List_Table'      => 'ms-themes',
     40                'WP_MS_My_Sites_List_Table'    => 'ms-mysites',
    4041        );
    4142
    4243        if ( isset( $core_classes[ $class ] ) ) {
  • src/wp-admin/includes/misc.php

     
    628628                        case 'plugins_network_per_page':
    629629                        case 'themes_network_per_page':
    630630                        case 'site_themes_network_per_page':
     631                        case 'my_sites_per_page':
    631632                                $value = (int) $value;
    632633                                if ( $value < 1 || $value > 999 ) {
    633634                                        return;
  • src/wp-admin/my-sites.php

     
    1717        wp_die( __( 'Sorry, you are not allowed to access this page.' ) );
    1818}
    1919
    20 $action = isset( $_POST['action'] ) ? $_POST['action'] : 'splash';
     20$wp_list_table = _get_list_table( 'WP_MS_My_Sites_List_Table' );
     21$pagenum       = $wp_list_table->get_pagenum();
    2122
    22 $blogs = get_blogs_of_user( $current_user->ID );
    23 
    24 $updated = false;
    25 if ( 'updateblogsettings' == $action && isset( $_POST['primary_blog'] ) ) {
    26         check_admin_referer( 'update-my-sites' );
    27 
    28         $blog = get_site( (int) $_POST['primary_blog'] );
    29         if ( $blog && isset( $blog->domain ) ) {
    30                 update_user_option( $current_user->ID, 'primary_blog', (int) $_POST['primary_blog'], true );
    31                 $updated = true;
    32         } else {
    33                 wp_die( __( 'The primary site you chose does not exist.' ) );
    34         }
    35 }
    36 
    3723$title       = __( 'My Sites' );
    3824$parent_file = 'index.php';
    3925
     26add_screen_option( 'per_page' );
     27
    4028get_current_screen()->add_help_tab(
    4129        array(
    4230                'id'      => 'overview',
     
    5240        '<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>'
    5341);
    5442
    55 require_once( ABSPATH . 'wp-admin/admin-header.php' );
     43get_current_screen()->set_screen_reader_content(
     44        array(
     45                'heading_pagination' => __( 'Sites list navigation' ),
     46                'heading_list'       => __( 'Sites list' ),
     47        )
     48);
    5649
    57 if ( $updated ) { ?>
    58         <div id="message" class="updated notice is-dismissible"><p><strong><?php _e( 'Settings saved.' ); ?></strong></p></div>
    59 <?php } ?>
    6050
    61 <div class="wrap">
    62 <h1 class="wp-heading-inline">
    63 <?php
    64 echo esc_html( $title );
    65 ?>
    66 </h1>
     51$action = isset( $_POST['action'] ) ? $_POST['action'] : 'splash';
    6752
    68 <?php
    69 if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) {
    70         /** This filter is documented in wp-login.php */
    71         $sign_up_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
    72         printf( ' <a href="%s" class="page-title-action">%s</a>', esc_url( $sign_up_url ), esc_html_x( 'Add New', 'site' ) );
     53$blogs = get_blogs_of_user( $current_user->ID );
     54
     55$msg = '';
     56$updated = false;
     57if ( 'updateblogsettings' == $action && isset( $_POST['primary_blog'] ) ) {
     58        check_admin_referer( 'update-my-sites' );
     59
     60        $blog = get_site( (int) $_POST['primary_blog'] );
     61        if ( $blog && isset( $blog->domain ) ) {
     62                update_user_option( $current_user->ID, 'primary_blog', (int) $_POST['primary_blog'], true );
     63                $updated = true;
     64        } else {
     65                wp_die( __( 'The primary site you chose does not exist.' ) );
     66        }
    7367}
    7468
    75 if ( empty( $blogs ) ) :
    76         echo '<p>';
    77         _e( 'You must be a member of at least one site to use this page.' );
    78         echo '</p>';
    79 else :
     69$wp_list_table->prepare_items();
     70
     71require_once( ABSPATH . 'wp-admin/admin-header.php' );
     72
     73if ( $updated ) {
     74        $msg = '<div id="message" class="updated notice is-dismissible"><p><strong>' .  __( 'Settings saved.' ) . '</strong></p></div>';
     75}
    8076?>
    8177
    82 <hr class="wp-header-end">
     78<div class="wrap">
     79        <h1 class="wp-heading-inline"><?php echo esc_html( $title ); echo get_current_screen()->id; ?></h1>
    8380
    84 <form id="myblogs" method="post">
    8581        <?php
    86         choose_primary_blog();
     82        if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) {
     83                /** This filter is documented in wp-login.php */
     84                $sign_up_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
     85                printf( ' <a href="%s" class="page-title-action">%s</a>', esc_url( $sign_up_url ), esc_html_x( 'Add New', 'site' ) );
     86        }
     87
     88        if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
     89                /* translators: %s: search keywords */
     90                printf( '<span class="subtitle">' . __( 'Search results for &#8220;%s&#8221;' ) . '</span>', esc_html( $s ) );
     91        }
     92        ?>
     93
     94        <hr class="wp-header-end">
     95
     96        <form id="myblogs" method="post">
     97        <?php
     98        if ( ! empty( $blogs ) ) {
     99                choose_primary_blog();
     100        }
    87101        /**
    88102         * Fires before the sites list on the My Sites screen.
    89103         *
     
    92106        do_action( 'myblogs_allblogs_options' );
    93107        ?>
    94108        <br clear="all" />
    95         <ul class="my-sites striped">
     109        <div class="my-sites striped">
    96110        <?php
    97111        /**
    98112         * Enable the Global Settings section on the My Sites screen.
     
    112126                echo $settings_html;
    113127        }
    114128        reset( $blogs );
     129        ?>
     130        </div>
    115131
    116         foreach ( $blogs as $user_blog ) {
    117                 switch_to_blog( $user_blog->userblog_id );
    118 
    119                 echo '<li>';
    120                 echo "<h3>{$user_blog->blogname}</h3>";
    121 
    122                 $actions = "<a href='" . esc_url( home_url() ) . "'>" . __( 'Visit' ) . '</a>';
    123 
    124                 if ( current_user_can( 'read' ) ) {
    125                         $actions .= " | <a href='" . esc_url( admin_url() ) . "'>" . __( 'Dashboard' ) . '</a>';
    126                 }
    127 
    128                 /**
    129                  * Filters the row links displayed for each site on the My Sites screen.
    130                  *
    131                  * @since MU (3.0.0)
    132                  *
    133                  * @param string $actions   The HTML site link markup.
    134                  * @param object $user_blog An object containing the site data.
    135                  */
    136                 $actions = apply_filters( 'myblogs_blog_actions', $actions, $user_blog );
    137                 echo "<p class='my-sites-actions'>" . $actions . '</p>';
    138 
    139                 /** This filter is documented in wp-admin/my-sites.php */
    140                 echo apply_filters( 'myblogs_options', '', $user_blog );
    141                 echo '</li>';
    142 
    143                 restore_current_blog();
    144         }
    145         ?>
    146         </ul>
    147132        <?php
    148133        if ( count( $blogs ) > 1 || has_action( 'myblogs_allblogs_options' ) || has_filter( 'myblogs_options' ) ) {
    149134                ?>
     
    154139        }
    155140        ?>
    156141        </form>
    157 <?php endif; ?>
    158         </div>
     142
     143        <?php echo $msg; ?>
     144
     145        <form method="get" id="ms-search" class="wp-clearfix">
     146                <?php $wp_list_table->search_box( __( 'Search Sites' ), 'site' ); ?>
     147                <input type="hidden" name="action" value="blogs" />
     148        </form>
     149
     150        <form id="form-site-list" action="my-sites.php?action=allblogs" method="post">
     151                <?php $wp_list_table->display(); ?>
     152        </form>
     153</div>
    159154<?php
    160155include( ABSPATH . 'wp-admin/admin-footer.php' );