Make WordPress Core

Changeset 34084


Ignore:
Timestamp:
09/12/2015 05:12:25 PM (11 years ago)
Author:
wonderboymusic
Message:

WP_Posts_List_Table: there are a cadre of edit.php URLs that are generated by string-building instead of using our handy functions. Create a helper method, ->get_edit_link() that standardizes the generation and escaping of these URLs.

Props BdN3504 for the initial patch on the ticket.
Fixes #32376.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-posts-list-table.php

    r34005 r34084  
    192192                        return $this->screen->post_type === $_GET['post_type'];
    193193                }
     194
     195                return 1 === count( $_GET ) && ! empty( $_GET['mode'] );
     196        }
     197
     198        /**
     199         * Helper to create links to `edit.php` with params
     200         *
     201         * @since 4.4.0
     202         *
     203         * @return string The formatted link string.
     204         */
     205        protected function get_edit_link( $args, $label, $class = '' ) {
     206                $url = add_query_arg( $args, 'edit.php' );
     207
     208                $class_html = '';
     209                if ( ! empty( $class ) ) {
     210                         $class_html = sprintf(
     211                                ' class="%s"',
     212                                esc_attr( $class )
     213                        );
     214                }
     215
     216                return sprintf(
     217                        '<a href="%s"%s>%s</a>',
     218                        esc_url( $url ),
     219                        $class_html,
     220                        $label
     221                );
    194222        }
    195223
     
    211239                $num_posts = wp_count_posts( $post_type, 'readable' );
    212240                $class = '';
    213                 $allposts = '';
    214241
    215242                $current_user_id = get_current_user_id();
     243                $all_args = array( 'post_type' => $post_type );
    216244
    217245                if ( $this->user_posts_count ) {
    218                         if ( isset( $_GET['author'] ) && ( $_GET['author'] == $current_user_id ) )
    219                                 $class = ' class="current"';
    220                         $status_links['mine'] = "<a href='edit.php?post_type=$post_type&author=$current_user_id'$class>" . sprintf( _nx( 'Mine <span class="count">(%s)</span>', 'Mine <span class="count">(%s)</span>', $this->user_posts_count, 'posts' ), number_format_i18n( $this->user_posts_count ) ) . '</a>';
    221                         $allposts = '&all_posts=1';
     246                        if ( isset( $_GET['author'] ) && ( $_GET['author'] == $current_user_id ) ) {
     247                                $class = 'current';
     248                        }
     249
     250                        $mine_args = array(
     251                                'post_type' => $post_type,
     252                                'author' => $current_user_id
     253                        );
     254
     255                        $mine_inner_html = sprintf(
     256                                _nx(
     257                                        'Mine <span class="count">(%s)</span>',
     258                                        'Mine <span class="count">(%s)</span>',
     259                                        $this->user_posts_count,
     260                                        'posts'
     261                                ),
     262                                number_format_i18n( $this->user_posts_count )
     263                        );
     264
     265                        $status_links['mine'] = $this->get_edit_link( $mine_args, $mine_inner_html, $class );
     266
     267                        $all_args['all_posts'] = 1;
    222268                        $class = '';
    223269                }
     
    230276
    231277                if ( empty( $class ) && ( ( $this->is_base_request() && ! $this->user_posts_count ) || isset( $_REQUEST['all_posts'] ) ) ) {
    232                         $class =  ' class="current"';
     278                        $class = 'current';
    233279                }
    234280
     
    243289                );
    244290
    245                 $status_links['all'] = "<a href='edit.php?post_type=$post_type{$allposts}'$class>" . $all_inner_html . '</a>';
     291                $status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class );
    246292
    247293                foreach ( get_post_stati(array('show_in_admin_status_list' => true), 'objects') as $status ) {
     
    250296                        $status_name = $status->name;
    251297
    252                         if ( !in_array( $status_name, $avail_post_stati ) )
     298                        if ( ! in_array( $status_name, $avail_post_stati ) || empty( $num_posts->$status_name ) ) {
    253299                                continue;
    254 
    255                         if ( empty( $num_posts->$status_name ) )
    256                                 continue;
    257 
    258                         if ( isset($_REQUEST['post_status']) && $status_name == $_REQUEST['post_status'] )
    259                                 $class = ' class="current"';
    260 
    261                         $status_links[$status_name] = "<a href='edit.php?post_status=$status_name&amp;post_type=$post_type'$class>" . sprintf( translate_nooped_plural( $status->label_count, $num_posts->$status_name ), number_format_i18n( $num_posts->$status_name ) ) . '</a>';
     300                        }
     301
     302                        if ( isset($_REQUEST['post_status']) && $status_name == $_REQUEST['post_status'] ) {
     303                                $class = 'current';
     304                        }
     305
     306                        $status_args = array(
     307                                'post_status' => $status_name,
     308                                'post_type' => $post_type,
     309                        );
     310
     311                        $status_label = sprintf(
     312                                translate_nooped_plural( $status->label_count, $num_posts->$status_name ),
     313                                number_format_i18n( $num_posts->$status_name )
     314                        );
     315
     316                        $status_links[ $status_name ] = $this->get_edit_link( $status_args, $status_label, $class );
    262317                }
    263318
    264319                if ( ! empty( $this->sticky_posts_count ) ) {
    265                         $class = ! empty( $_REQUEST['show_sticky'] ) ? ' class="current"' : '';
    266 
    267                         $sticky_link = array( 'sticky' => "<a href='edit.php?post_type=$post_type&amp;show_sticky=1'$class>" . sprintf( _nx( 'Sticky <span class="count">(%s)</span>', 'Sticky <span class="count">(%s)</span>', $this->sticky_posts_count, 'posts' ), number_format_i18n( $this->sticky_posts_count ) ) . '</a>' );
     320                        $class = ! empty( $_REQUEST['show_sticky'] ) ? 'current' : '';
     321
     322                        $sticky_args = array(
     323                                'post_type'     => $post_type,
     324                                'show_sticky' => 1
     325                        );
     326
     327                        $sticky_inner_html = sprintf(
     328                                _nx(
     329                                        'Sticky <span class="count">(%s)</span>',
     330                                        'Sticky <span class="count">(%s)</span>',
     331                                        $this->sticky_posts_count,
     332                                        'posts'
     333                                ),
     334                                number_format_i18n( $this->sticky_posts_count )
     335                        );
     336
     337                        $sticky_link = array(
     338                                'sticky' => $this->get_edit_link( $sticky_args, $sticky_inner_html, $class )
     339                        );
    268340
    269341                        // Sticky comes after Publish, or if not listed, after All.
     
    761833                        $label = get_post_format_string( $format );
    762834
    763                         echo '<a href="' . esc_url( add_query_arg( array( 'post_format' => $format, 'post_type' => $post->post_type ), 'edit.php' ) ) . '" class="post-state-format post-format-icon post-format-' . $format . '" title="' . $label . '">' . $label . ":</a> ";
     835                        $format_class = 'post-state-format post-format-icon post-format-' . $format;
     836
     837                        $format_args = array(
     838                                'post_format' => $format,
     839                                'post_type' => $post->post_type
     840                        );
     841
     842                        echo $this->get_edit_link( $format_args, $label . ':', $format_class );
    764843                }
    765844
     
    896975         */
    897976        public function column_author( $post ) {
    898                 printf( '<a href="%s">%s</a>',
    899                         esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'author' => get_the_author_meta( 'ID' ) ), 'edit.php' )),
    900                         get_the_author()
     977                $args = array(
     978                        'post_type' => $post->post_type,
     979                        'author' => get_the_author_meta( 'ID' )
    901980                );
     981                echo $this->get_edit_link( $args, get_the_author() );
    902982        }
    903983
     
    9381018                                        }
    9391019
    940                                         $out[] = sprintf( '<a href="%s">%s</a>',
    941                                                 esc_url( add_query_arg( $posts_in_term_qv, 'edit.php' ) ),
    942                                                 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
    943                                         );
     1020                                        $label = esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) );
     1021                                        $out[] = $this->get_edit_link( $posts_in_term_qv, $label );
    9441022                                }
    9451023                                /* translators: used between list items, there is a space after the comma */
Note: See TracChangeset for help on using the changeset viewer.