Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (6 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/export.php

    r40958 r42343  
    5757    global $wpdb, $post;
    5858
    59     $defaults = array( 'content' => 'all', 'author' => false, 'category' => false,
    60         'start_date' => false, 'end_date' => false, 'status' => false,
     59    $defaults = array(
     60        'content'    => 'all',
     61        'author'     => false,
     62        'category'   => false,
     63        'start_date' => false,
     64        'end_date'   => false,
     65        'status'     => false,
    6166    );
    62     $args = wp_parse_args( $args, $defaults );
     67    $args     = wp_parse_args( $args, $defaults );
    6368
    6469    /**
     
    7580        $sitename .= '.';
    7681    }
    77     $date = date( 'Y-m-d' );
    78     $wp_filename = $sitename . 'wordpress.' . $date . '.xml';
     82    $date        = date( 'Y-m-d' );
     83    $wp_filename = $sitename . 'WordPress.' . $date . '.xml';
    7984    /**
    8085     * Filters the export filename.
     
    9499    if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) {
    95100        $ptype = get_post_type_object( $args['content'] );
    96         if ( ! $ptype->can_export )
     101        if ( ! $ptype->can_export ) {
    97102            $args['content'] = 'post';
     103        }
    98104
    99105        $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
    100106    } else {
    101107        $post_types = get_post_types( array( 'can_export' => true ) );
    102         $esses = array_fill( 0, count($post_types), '%s' );
    103         $where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
    104     }
    105 
    106     if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )
     108        $esses      = array_fill( 0, count( $post_types ), '%s' );
     109        $where      = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
     110    }
     111
     112    if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) ) {
    107113        $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
    108     else
     114    } else {
    109115        $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
     116    }
    110117
    111118    $join = '';
    112119    if ( $args['category'] && 'post' == $args['content'] ) {
    113120        if ( $term = term_exists( $args['category'], 'category' ) ) {
    114             $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
     121            $join   = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
    115122            $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );
    116123        }
     
    118125
    119126    if ( 'post' == $args['content'] || 'page' == $args['content'] || 'attachment' == $args['content'] ) {
    120         if ( $args['author'] )
     127        if ( $args['author'] ) {
    121128            $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
    122 
    123         if ( $args['start_date'] )
    124             $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );
    125 
    126         if ( $args['end_date'] )
    127             $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime('+1 month', strtotime($args['end_date'])) ) );
     129        }
     130
     131        if ( $args['start_date'] ) {
     132            $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime( $args['start_date'] ) ) );
     133        }
     134
     135        if ( $args['end_date'] ) {
     136            $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime( '+1 month', strtotime( $args['end_date'] ) ) ) );
     137        }
    128138    }
    129139
     
    137147    $cats = $tags = $terms = array();
    138148    if ( isset( $term ) && $term ) {
    139         $cat = get_term( $term['term_id'], 'category' );
     149        $cat  = get_term( $term['term_id'], 'category' );
    140150        $cats = array( $cat->term_id => $cat );
    141151        unset( $term, $cat );
    142152    } elseif ( 'all' == $args['content'] ) {
    143153        $categories = (array) get_categories( array( 'get' => 'all' ) );
    144         $tags = (array) get_tags( array( 'get' => 'all' ) );
     154        $tags       = (array) get_tags( array( 'get' => 'all' ) );
    145155
    146156        $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
    147         $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
     157        $custom_terms      = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
    148158
    149159        // Put categories in order with no child going before its parent.
    150160        while ( $cat = array_shift( $categories ) ) {
    151             if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
    152                 $cats[$cat->term_id] = $cat;
    153             else
     161            if ( $cat->parent == 0 || isset( $cats[ $cat->parent ] ) ) {
     162                $cats[ $cat->term_id ] = $cat;
     163            } else {
    154164                $categories[] = $cat;
     165            }
    155166        }
    156167
    157168        // Put terms in order with no child going before its parent.
    158169        while ( $t = array_shift( $custom_terms ) ) {
    159             if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
    160                 $terms[$t->term_id] = $t;
    161             else
     170            if ( $t->parent == 0 || isset( $terms[ $t->parent ] ) ) {
     171                $terms[ $t->term_id ] = $t;
     172            } else {
    162173                $custom_terms[] = $t;
     174            }
    163175        }
    164176
     
    193205    function wxr_site_url() {
    194206        // Multisite: the base URL.
    195         if ( is_multisite() )
     207        if ( is_multisite() ) {
    196208            return network_home_url();
    197         // WordPress (single site): the blog URL.
    198         else
     209        } // WordPress (single site): the blog URL.
     210        else {
    199211            return get_bloginfo_rss( 'url' );
     212        }
    200213    }
    201214
     
    208221     */
    209222    function wxr_cat_name( $category ) {
    210         if ( empty( $category->name ) )
    211             return;
     223        if ( empty( $category->name ) ) {
     224            return;
     225        }
    212226
    213227        echo '<wp:cat_name>' . wxr_cdata( $category->name ) . "</wp:cat_name>\n";
     
    222236     */
    223237    function wxr_category_description( $category ) {
    224         if ( empty( $category->description ) )
    225             return;
     238        if ( empty( $category->description ) ) {
     239            return;
     240        }
    226241
    227242        echo '<wp:category_description>' . wxr_cdata( $category->description ) . "</wp:category_description>\n";
     
    236251     */
    237252    function wxr_tag_name( $tag ) {
    238         if ( empty( $tag->name ) )
    239             return;
     253        if ( empty( $tag->name ) ) {
     254            return;
     255        }
    240256
    241257        echo '<wp:tag_name>' . wxr_cdata( $tag->name ) . "</wp:tag_name>\n";
     
    250266     */
    251267    function wxr_tag_description( $tag ) {
    252         if ( empty( $tag->description ) )
    253             return;
     268        if ( empty( $tag->description ) ) {
     269            return;
     270        }
    254271
    255272        echo '<wp:tag_description>' . wxr_cdata( $tag->description ) . "</wp:tag_description>\n";
     
    264281     */
    265282    function wxr_term_name( $term ) {
    266         if ( empty( $term->name ) )
    267             return;
     283        if ( empty( $term->name ) ) {
     284            return;
     285        }
    268286
    269287        echo '<wp:term_name>' . wxr_cdata( $term->name ) . "</wp:term_name>\n";
     
    278296     */
    279297    function wxr_term_description( $term ) {
    280         if ( empty( $term->description ) )
    281             return;
     298        if ( empty( $term->description ) ) {
     299            return;
     300        }
    282301
    283302        echo "\t\t<wp:term_description>" . wxr_cdata( $term->description ) . "</wp:term_description>\n";
     
    327346        global $wpdb;
    328347
    329         if ( !empty( $post_ids ) ) {
     348        if ( ! empty( $post_ids ) ) {
    330349            $post_ids = array_map( 'absint', $post_ids );
    331             $and = 'AND ID IN ( ' . implode( ', ', $post_ids ) . ')';
     350            $and      = 'AND ID IN ( ' . implode( ', ', $post_ids ) . ')';
    332351        } else {
    333352            $and = '';
     
    336355        $authors = array();
    337356        $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft' $and" );
    338         foreach ( (array) $results as $result )
     357        foreach ( (array) $results as $result ) {
    339358            $authors[] = get_userdata( $result->post_author );
     359        }
    340360
    341361        $authors = array_filter( $authors );
     
    360380    function wxr_nav_menu_terms() {
    361381        $nav_menus = wp_get_nav_menus();
    362         if ( empty( $nav_menus ) || ! is_array( $nav_menus ) )
    363             return;
     382        if ( empty( $nav_menus ) || ! is_array( $nav_menus ) ) {
     383            return;
     384        }
    364385
    365386        foreach ( $nav_menus as $menu ) {
     
    382403
    383404        $taxonomies = get_object_taxonomies( $post->post_type );
    384         if ( empty( $taxonomies ) )
    385             return;
     405        if ( empty( $taxonomies ) ) {
     406            return;
     407        }
    386408        $terms = wp_get_object_terms( $post->ID, $taxonomies );
    387409
     
    392414
    393415    /**
    394      *
    395416     * @param bool   $return_me
    396417     * @param string $meta_key
     
    398419     */
    399420    function wxr_filter_postmeta( $return_me, $meta_key ) {
    400         if ( '_edit_lock' == $meta_key )
     421        if ( '_edit_lock' == $meta_key ) {
    401422            $return_me = true;
     423        }
    402424        return $return_me;
    403425    }
    404426    add_filter( 'wxr_export_skip_postmeta', 'wxr_filter_postmeta', 10, 2 );
    405427
    406     echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . "\" ?>\n";
     428    echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . "\" ?>\n";
    407429
    408430    ?>
     
    449471        <wp:term_id><?php echo intval( $c->term_id ); ?></wp:term_id>
    450472        <wp:category_nicename><?php echo wxr_cdata( $c->slug ); ?></wp:category_nicename>
    451         <wp:category_parent><?php echo wxr_cdata( $c->parent ? $cats[$c->parent]->slug : '' ); ?></wp:category_parent>
    452         <?php wxr_cat_name( $c );
     473        <wp:category_parent><?php echo wxr_cdata( $c->parent ? $cats[ $c->parent ]->slug : '' ); ?></wp:category_parent>
     474        <?php
     475        wxr_cat_name( $c );
    453476        wxr_category_description( $c );
    454         wxr_term_meta( $c ); ?>
     477        wxr_term_meta( $c );
     478        ?>
    455479    </wp:category>
    456480<?php endforeach; ?>
     
    459483        <wp:term_id><?php echo intval( $t->term_id ); ?></wp:term_id>
    460484        <wp:tag_slug><?php echo wxr_cdata( $t->slug ); ?></wp:tag_slug>
    461         <?php wxr_tag_name( $t );
     485        <?php
     486        wxr_tag_name( $t );
    462487        wxr_tag_description( $t );
    463         wxr_term_meta( $t ); ?>
     488        wxr_term_meta( $t );
     489        ?>
    464490    </wp:tag>
    465491<?php endforeach; ?>
     
    469495        <wp:term_taxonomy><?php echo wxr_cdata( $t->taxonomy ); ?></wp:term_taxonomy>
    470496        <wp:term_slug><?php echo wxr_cdata( $t->slug ); ?></wp:term_slug>
    471         <wp:term_parent><?php echo wxr_cdata( $t->parent ? $terms[$t->parent]->slug : '' ); ?></wp:term_parent>
    472         <?php wxr_term_name( $t );
     497        <wp:term_parent><?php echo wxr_cdata( $t->parent ? $terms[ $t->parent ]->slug : '' ); ?></wp:term_parent>
     498        <?php
     499        wxr_term_name( $t );
    473500        wxr_term_description( $t );
    474         wxr_term_meta( $t ); ?>
     501        wxr_term_meta( $t );
     502        ?>
    475503    </wp:term>
    476504<?php endforeach; ?>
    477 <?php if ( 'all' == $args['content'] ) wxr_nav_menu_terms(); ?>
     505<?php
     506if ( 'all' == $args['content'] ) {
     507    wxr_nav_menu_terms();}
     508?>
    478509
    479510    <?php
     
    482513    ?>
    483514
    484 <?php if ( $post_ids ) {
     515<?php
     516if ( $post_ids ) {
    485517    /**
    486518     * @global WP_Query $wp_query
     
    493525    // Fetch 20 posts at a time rather than loading the entire table into memory.
    494526    while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
    495     $where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
    496     $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
    497 
    498     // Begin Loop.
    499     foreach ( $posts as $post ) {
    500         setup_postdata( $post );
    501         $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
    502 ?>
     527        $where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
     528        $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
     529
     530        // Begin Loop.
     531        foreach ( $posts as $post ) {
     532            setup_postdata( $post );
     533            $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
     534    ?>
    503535    <item>
    504         <title><?php
     536        <title>
     537        <?php
    505538            /** This filter is documented in wp-includes/feed.php */
    506539            echo apply_filters( 'the_title_rss', $post->post_title );
    507         ?></title>
    508         <link><?php the_permalink_rss() ?></link>
     540        ?>
     541        </title>
     542        <link><?php the_permalink_rss(); ?></link>
    509543        <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
    510544        <dc:creator><?php echo wxr_cdata( get_the_author_meta( 'login' ) ); ?></dc:creator>
    511545        <guid isPermaLink="false"><?php the_guid(); ?></guid>
    512546        <description></description>
    513         <content:encoded><?php
     547        <content:encoded>
     548        <?php
    514549            /**
    515550             * Filters the post content used for WXR exports.
     
    520555             */
    521556            echo wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) );
    522         ?></content:encoded>
    523         <excerpt:encoded><?php
     557        ?>
     558        </content:encoded>
     559        <excerpt:encoded>
     560        <?php
    524561            /**
    525562             * Filters the post excerpt used for WXR exports.
     
    530567             */
    531568            echo wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) );
    532         ?></excerpt:encoded>
     569        ?>
     570        </excerpt:encoded>
    533571        <wp:post_id><?php echo intval( $post->ID ); ?></wp:post_id>
    534572        <wp:post_date><?php echo wxr_cdata( $post->post_date ); ?></wp:post_date>
     
    543581        <wp:post_password><?php echo wxr_cdata( $post->post_password ); ?></wp:post_password>
    544582        <wp:is_sticky><?php echo intval( $is_sticky ); ?></wp:is_sticky>
    545 <?php   if ( $post->post_type == 'attachment' ) : ?>
     583    <?php   if ( $post->post_type == 'attachment' ) : ?>
    546584        <wp:attachment_url><?php echo wxr_cdata( wp_get_attachment_url( $post->ID ) ); ?></wp:attachment_url>
    547 <?php   endif; ?>
    548 <?php   wxr_post_taxonomy(); ?>
    549 <?php   $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
    550         foreach ( $postmeta as $meta ) :
    551             /**
    552              * Filters whether to selectively skip post meta used for WXR exports.
    553              *
    554              * Returning a truthy value to the filter will skip the current meta
    555              * object from being exported.
    556              *
    557              * @since 3.3.0
    558              *
    559              * @param bool   $skip     Whether to skip the current post meta. Default false.
    560              * @param string $meta_key Current meta key.
    561              * @param object $meta     Current meta object.
    562              */
    563             if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) )
    564                 continue;
     585    <?php endif; ?>
     586    <?php wxr_post_taxonomy(); ?>
     587    <?php
     588    $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
     589    foreach ( $postmeta as $meta ) :
     590        /**
     591         * Filters whether to selectively skip post meta used for WXR exports.
     592         *
     593         * Returning a truthy value to the filter will skip the current meta
     594         * object from being exported.
     595         *
     596         * @since 3.3.0
     597         *
     598         * @param bool   $skip     Whether to skip the current post meta. Default false.
     599         * @param string $meta_key Current meta key.
     600         * @param object $meta     Current meta object.
     601         */
     602        if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) ) {
     603            continue;
     604        }
    565605        ?>
    566606        <wp:postmeta>
    567             <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
    568             <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
     607        <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
     608        <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
    569609        </wp:postmeta>
    570 <?php   endforeach;
     610<?php
     611endforeach;
    571612
    572613        $_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
    573         $comments = array_map( 'get_comment', $_comments );
    574         foreach ( $comments as $c ) : ?>
     614        $comments  = array_map( 'get_comment', $_comments );
     615    foreach ( $comments as $c ) :
     616        ?>
    575617        <wp:comment>
    576618            <wp:comment_id><?php echo intval( $c->comment_ID ); ?></wp:comment_id>
     
    581623            <wp:comment_date><?php echo wxr_cdata( $c->comment_date ); ?></wp:comment_date>
    582624            <wp:comment_date_gmt><?php echo wxr_cdata( $c->comment_date_gmt ); ?></wp:comment_date_gmt>
    583             <wp:comment_content><?php echo wxr_cdata( $c->comment_content ) ?></wp:comment_content>
     625            <wp:comment_content><?php echo wxr_cdata( $c->comment_content ); ?></wp:comment_content>
    584626            <wp:comment_approved><?php echo wxr_cdata( $c->comment_approved ); ?></wp:comment_approved>
    585627            <wp:comment_type><?php echo wxr_cdata( $c->comment_type ); ?></wp:comment_type>
    586628            <wp:comment_parent><?php echo intval( $c->comment_parent ); ?></wp:comment_parent>
    587629            <wp:comment_user_id><?php echo intval( $c->user_id ); ?></wp:comment_user_id>
    588 <?php       $c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
    589             foreach ( $c_meta as $meta ) :
    590                 /**
    591                  * Filters whether to selectively skip comment meta used for WXR exports.
    592                  *
    593                  * Returning a truthy value to the filter will skip the current meta
    594                  * object from being exported.
    595                  *
    596                  * @since 4.0.0
    597                  *
    598                  * @param bool   $skip     Whether to skip the current comment meta. Default false.
    599                  * @param string $meta_key Current meta key.
    600                  * @param object $meta     Current meta object.
    601                  */
    602                 if ( apply_filters( 'wxr_export_skip_commentmeta', false, $meta->meta_key, $meta ) ) {
    603                     continue;
    604                 }
    605             ?>
    606             <wp:commentmeta>
    607                 <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
    608                 <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
     630<?php
     631  $c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
     632foreach ( $c_meta as $meta ) :
     633    /**
     634     * Filters whether to selectively skip comment meta used for WXR exports.
     635     *
     636     * Returning a truthy value to the filter will skip the current meta
     637     * object from being exported.
     638     *
     639     * @since 4.0.0
     640     *
     641     * @param bool   $skip     Whether to skip the current comment meta. Default false.
     642     * @param string $meta_key Current meta key.
     643     * @param object $meta     Current meta object.
     644     */
     645    if ( apply_filters( 'wxr_export_skip_commentmeta', false, $meta->meta_key, $meta ) ) {
     646        continue;
     647    }
     648    ?>
     649    <wp:commentmeta>
     650    <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
     651            <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
    609652            </wp:commentmeta>
    610 <?php       endforeach; ?>
     653<?php   endforeach; ?>
    611654        </wp:comment>
    612655<?php   endforeach; ?>
    613     </item>
    614 <?php
    615     }
    616     }
    617 } ?>
     656        </item>
     657    <?php
     658        }
     659    }
     660}
     661?>
    618662</channel>
    619663</rss>
Note: See TracChangeset for help on using the changeset viewer.