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/post.php

    r42228 r42343  
    2020function _wp_translate_postdata( $update = false, $post_data = null ) {
    2121
    22     if ( empty($post_data) )
     22    if ( empty( $post_data ) ) {
    2323        $post_data = &$_POST;
    24 
    25     if ( $update )
     24    }
     25
     26    if ( $update ) {
    2627        $post_data['ID'] = (int) $post_data['post_ID'];
     28    }
    2729
    2830    $ptype = get_post_type_object( $post_data['post_type'] );
    2931
    3032    if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
    31         if ( 'page' == $post_data['post_type'] )
     33        if ( 'page' == $post_data['post_type'] ) {
    3234            return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
    33         else
     35        } else {
    3436            return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
     37        }
    3538    } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
    36         if ( 'page' == $post_data['post_type'] )
     39        if ( 'page' == $post_data['post_type'] ) {
    3740            return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
    38         else
     41        } else {
    3942            return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
    40     }
    41 
    42     if ( isset( $post_data['content'] ) )
     43        }
     44    }
     45
     46    if ( isset( $post_data['content'] ) ) {
    4347        $post_data['post_content'] = $post_data['content'];
    44 
    45     if ( isset( $post_data['excerpt'] ) )
     48    }
     49
     50    if ( isset( $post_data['excerpt'] ) ) {
    4651        $post_data['post_excerpt'] = $post_data['excerpt'];
    47 
    48     if ( isset( $post_data['parent_id'] ) )
     52    }
     53
     54    if ( isset( $post_data['parent_id'] ) ) {
    4955        $post_data['post_parent'] = (int) $post_data['parent_id'];
    50 
    51     if ( isset($post_data['trackback_url']) )
     56    }
     57
     58    if ( isset( $post_data['trackback_url'] ) ) {
    5259        $post_data['to_ping'] = $post_data['trackback_url'];
     60    }
    5361
    5462    $post_data['user_ID'] = get_current_user_id();
    5563
    56     if (!empty ( $post_data['post_author_override'] ) ) {
     64    if ( ! empty( $post_data['post_author_override'] ) ) {
    5765        $post_data['post_author'] = (int) $post_data['post_author_override'];
    5866    } else {
    59         if (!empty ( $post_data['post_author'] ) ) {
     67        if ( ! empty( $post_data['post_author'] ) ) {
    6068            $post_data['post_author'] = (int) $post_data['post_author'];
    6169        } else {
     
    6876
    6977        if ( $update ) {
    70             if ( 'page' == $post_data['post_type'] )
     78            if ( 'page' == $post_data['post_type'] ) {
    7179                return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
    72             else
     80            } else {
    7381                return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
     82            }
    7483        } else {
    75             if ( 'page' == $post_data['post_type'] )
     84            if ( 'page' == $post_data['post_type'] ) {
    7685                return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
    77             else
     86            } else {
    7887                return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
     88            }
    7989        }
    8090    }
     
    94104
    95105    // What to do based on which button they pressed
    96     if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
     106    if ( isset( $post_data['saveasdraft'] ) && '' != $post_data['saveasdraft'] ) {
    97107        $post_data['post_status'] = 'draft';
    98     if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
     108    }
     109    if ( isset( $post_data['saveasprivate'] ) && '' != $post_data['saveasprivate'] ) {
    99110        $post_data['post_status'] = 'private';
    100     if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) )
     111    }
     112    if ( isset( $post_data['publish'] ) && ( '' != $post_data['publish'] ) && ( ! isset( $post_data['post_status'] ) || $post_data['post_status'] != 'private' ) ) {
    101113        $post_data['post_status'] = 'publish';
    102     if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
     114    }
     115    if ( isset( $post_data['advanced'] ) && '' != $post_data['advanced'] ) {
    103116        $post_data['post_status'] = 'draft';
    104     if ( isset($post_data['pending']) && '' != $post_data['pending'] )
     117    }
     118    if ( isset( $post_data['pending'] ) && '' != $post_data['pending'] ) {
    105119        $post_data['post_status'] = 'pending';
    106 
    107     if ( isset( $post_data['ID'] ) )
     120    }
     121
     122    if ( isset( $post_data['ID'] ) ) {
    108123        $post_id = $post_data['ID'];
    109     else
     124    } else {
    110125        $post_id = false;
     126    }
    111127    $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
    112128
     
    119135    // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
    120136    // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
    121     if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) )
    122         if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) )
     137    if ( isset( $post_data['post_status'] ) && ( in_array( $post_data['post_status'], $published_statuses ) && ! current_user_can( $ptype->cap->publish_posts ) ) ) {
     138        if ( ! in_array( $previous_status, $published_statuses ) || ! current_user_can( 'edit_post', $post_id ) ) {
    123139            $post_data['post_status'] = 'pending';
     140        }
     141    }
    124142
    125143    if ( ! isset( $post_data['post_status'] ) ) {
     
    131149    }
    132150
    133     if (!isset( $post_data['comment_status'] ))
     151    if ( ! isset( $post_data['comment_status'] ) ) {
    134152        $post_data['comment_status'] = 'closed';
    135 
    136     if (!isset( $post_data['ping_status'] ))
     153    }
     154
     155    if ( ! isset( $post_data['ping_status'] ) ) {
    137156        $post_data['ping_status'] = 'closed';
    138 
    139     foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
    140         if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) {
     157    }
     158
     159    foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) {
     160        if ( ! empty( $post_data[ 'hidden_' . $timeunit ] ) && $post_data[ 'hidden_' . $timeunit ] != $post_data[ $timeunit ] ) {
    141161            $post_data['edit_date'] = '1';
    142162            break;
     
    144164    }
    145165
    146     if ( !empty( $post_data['edit_date'] ) ) {
    147         $aa = $post_data['aa'];
    148         $mm = $post_data['mm'];
    149         $jj = $post_data['jj'];
    150         $hh = $post_data['hh'];
    151         $mn = $post_data['mn'];
    152         $ss = $post_data['ss'];
    153         $aa = ($aa <= 0 ) ? date('Y') : $aa;
    154         $mm = ($mm <= 0 ) ? date('n') : $mm;
    155         $jj = ($jj > 31 ) ? 31 : $jj;
    156         $jj = ($jj <= 0 ) ? date('j') : $jj;
    157         $hh = ($hh > 23 ) ? $hh -24 : $hh;
    158         $mn = ($mn > 59 ) ? $mn -60 : $mn;
    159         $ss = ($ss > 59 ) ? $ss -60 : $ss;
    160         $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
    161         $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
    162         if ( !$valid_date ) {
     166    if ( ! empty( $post_data['edit_date'] ) ) {
     167        $aa                     = $post_data['aa'];
     168        $mm                     = $post_data['mm'];
     169        $jj                     = $post_data['jj'];
     170        $hh                     = $post_data['hh'];
     171        $mn                     = $post_data['mn'];
     172        $ss                     = $post_data['ss'];
     173        $aa                     = ( $aa <= 0 ) ? date( 'Y' ) : $aa;
     174        $mm                     = ( $mm <= 0 ) ? date( 'n' ) : $mm;
     175        $jj                     = ( $jj > 31 ) ? 31 : $jj;
     176        $jj                     = ( $jj <= 0 ) ? date( 'j' ) : $jj;
     177        $hh                     = ( $hh > 23 ) ? $hh - 24 : $hh;
     178        $mn                     = ( $mn > 59 ) ? $mn - 60 : $mn;
     179        $ss                     = ( $ss > 59 ) ? $ss - 60 : $ss;
     180        $post_data['post_date'] = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss );
     181        $valid_date             = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
     182        if ( ! $valid_date ) {
    163183            return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
    164184        }
     
    189209    global $wpdb;
    190210
    191     if ( empty($post_data) )
     211    if ( empty( $post_data ) ) {
    192212        $post_data = &$_POST;
     213    }
    193214
    194215    // Clear out any data in internal vars.
    195216    unset( $post_data['filter'] );
    196217
    197     $post_ID = (int) $post_data['post_ID'];
    198     $post = get_post( $post_ID );
    199     $post_data['post_type'] = $post->post_type;
     218    $post_ID                     = (int) $post_data['post_ID'];
     219    $post                        = get_post( $post_ID );
     220    $post_data['post_type']      = $post->post_type;
    200221    $post_data['post_mime_type'] = $post->post_mime_type;
    201222
     
    208229    }
    209230
    210     $ptype = get_post_type_object($post_data['post_type']);
    211     if ( !current_user_can( 'edit_post', $post_ID ) ) {
    212         if ( 'page' == $post_data['post_type'] )
    213             wp_die( __('Sorry, you are not allowed to edit this page.' ));
    214         else
    215             wp_die( __('Sorry, you are not allowed to edit this post.' ));
     231    $ptype = get_post_type_object( $post_data['post_type'] );
     232    if ( ! current_user_can( 'edit_post', $post_ID ) ) {
     233        if ( 'page' == $post_data['post_type'] ) {
     234            wp_die( __( 'Sorry, you are not allowed to edit this page.' ) );
     235        } else {
     236            wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
     237        }
    216238    }
    217239
    218240    if ( post_type_supports( $ptype->name, 'revisions' ) ) {
    219         $revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC', 'posts_per_page' => 1 ) );
    220         $revision = current( $revisions );
     241        $revisions = wp_get_post_revisions(
     242            $post_ID, array(
     243                'order'          => 'ASC',
     244                'posts_per_page' => 1,
     245            )
     246        );
     247        $revision  = current( $revisions );
    221248
    222249        // Check if the revisions have been upgraded
    223         if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 )
     250        if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) {
    224251            _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
    225     }
    226 
    227     if ( isset($post_data['visibility']) ) {
     252        }
     253    }
     254
     255    if ( isset( $post_data['visibility'] ) ) {
    228256        switch ( $post_data['visibility'] ) {
    229             case 'public' :
     257            case 'public':
    230258                $post_data['post_password'] = '';
    231259                break;
    232             case 'password' :
     260            case 'password':
    233261                unset( $post_data['sticky'] );
    234262                break;
    235             case 'private' :
    236                 $post_data['post_status'] = 'private';
     263            case 'private':
     264                $post_data['post_status']   = 'private';
    237265                $post_data['post_password'] = '';
    238266                unset( $post_data['sticky'] );
     
    242270
    243271    $post_data = _wp_translate_postdata( true, $post_data );
    244     if ( is_wp_error($post_data) )
     272    if ( is_wp_error( $post_data ) ) {
    245273        wp_die( $post_data->get_error_message() );
     274    }
    246275
    247276    // Post Formats
    248     if ( isset( $post_data['post_format'] ) )
     277    if ( isset( $post_data['post_format'] ) ) {
    249278        set_post_format( $post_ID, $post_data['post_format'] );
     279    }
    250280
    251281    $format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
    252282    foreach ( $format_meta_urls as $format_meta_url ) {
    253283        $keyed = '_format_' . $format_meta_url;
    254         if ( isset( $post_data[ $keyed ] ) )
     284        if ( isset( $post_data[ $keyed ] ) ) {
    255285            update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) );
     286        }
    256287    }
    257288
     
    261292        $keyed = '_format_' . $key;
    262293        if ( isset( $post_data[ $keyed ] ) ) {
    263             if ( current_user_can( 'unfiltered_html' ) )
     294            if ( current_user_can( 'unfiltered_html' ) ) {
    264295                update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
    265             else
     296            } else {
    266297                update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
     298            }
    267299        }
    268300    }
     
    283315
    284316    // Meta Stuff
    285     if ( isset($post_data['meta']) && $post_data['meta'] ) {
     317    if ( isset( $post_data['meta'] ) && $post_data['meta'] ) {
    286318        foreach ( $post_data['meta'] as $key => $value ) {
    287             if ( !$meta = get_post_meta_by_id( $key ) )
     319            if ( ! $meta = get_post_meta_by_id( $key ) ) {
    288320                continue;
    289             if ( $meta->post_id != $post_ID )
     321            }
     322            if ( $meta->post_id != $post_ID ) {
    290323                continue;
    291             if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) )
     324            }
     325            if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) ) {
    292326                continue;
    293             if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) )
     327            }
     328            if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) ) {
    294329                continue;
     330            }
    295331            update_meta( $key, $value['key'], $value['value'] );
    296332        }
    297333    }
    298334
    299     if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {
     335    if ( isset( $post_data['deletemeta'] ) && $post_data['deletemeta'] ) {
    300336        foreach ( $post_data['deletemeta'] as $key => $value ) {
    301             if ( !$meta = get_post_meta_by_id( $key ) )
     337            if ( ! $meta = get_post_meta_by_id( $key ) ) {
    302338                continue;
    303             if ( $meta->post_id != $post_ID )
     339            }
     340            if ( $meta->post_id != $post_ID ) {
    304341                continue;
    305             if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) )
     342            }
     343            if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) ) {
    306344                continue;
     345            }
    307346            delete_meta( $key );
    308347        }
     
    311350    // Attachment stuff
    312351    if ( 'attachment' == $post_data['post_type'] ) {
    313         if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) {
     352        if ( isset( $post_data['_wp_attachment_image_alt'] ) ) {
    314353            $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
    315354            if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) {
     
    361400
    362401    if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) {
    363         if ( ! empty( $post_data['sticky'] ) )
     402        if ( ! empty( $post_data['sticky'] ) ) {
    364403            stick_post( $post_ID );
    365         else
     404        } else {
    366405            unstick_post( $post_ID );
     406        }
    367407    }
    368408
     
    386426    global $wpdb;
    387427
    388     if ( empty($post_data) )
     428    if ( empty( $post_data ) ) {
    389429        $post_data = &$_POST;
    390 
    391     if ( isset($post_data['post_type']) )
    392         $ptype = get_post_type_object($post_data['post_type']);
    393     else
    394         $ptype = get_post_type_object('post');
    395 
    396     if ( !current_user_can( $ptype->cap->edit_posts ) ) {
    397         if ( 'page' == $ptype->name )
    398             wp_die( __('Sorry, you are not allowed to edit pages.'));
    399         else
    400             wp_die( __('Sorry, you are not allowed to edit posts.'));
     430    }
     431
     432    if ( isset( $post_data['post_type'] ) ) {
     433        $ptype = get_post_type_object( $post_data['post_type'] );
     434    } else {
     435        $ptype = get_post_type_object( 'post' );
     436    }
     437
     438    if ( ! current_user_can( $ptype->cap->edit_posts ) ) {
     439        if ( 'page' == $ptype->name ) {
     440            wp_die( __( 'Sorry, you are not allowed to edit pages.' ) );
     441        } else {
     442            wp_die( __( 'Sorry, you are not allowed to edit posts.' ) );
     443        }
    401444    }
    402445
    403446    if ( -1 == $post_data['_status'] ) {
    404447        $post_data['post_status'] = null;
    405         unset($post_data['post_status']);
     448        unset( $post_data['post_status'] );
    406449    } else {
    407450        $post_data['post_status'] = $post_data['_status'];
    408451    }
    409     unset($post_data['_status']);
     452    unset( $post_data['_status'] );
    410453
    411454    if ( ! empty( $post_data['post_status'] ) ) {
     
    420463
    421464    $reset = array(
    422         'post_author', 'post_status', 'post_password',
    423         'post_parent', 'page_template', 'comment_status',
    424         'ping_status', 'keep_private', 'tax_input',
    425         'post_category', 'sticky', 'post_format',
     465        'post_author',
     466        'post_status',
     467        'post_password',
     468        'post_parent',
     469        'page_template',
     470        'comment_status',
     471        'ping_status',
     472        'keep_private',
     473        'tax_input',
     474        'post_category',
     475        'sticky',
     476        'post_format',
    426477    );
    427478
    428479    foreach ( $reset as $field ) {
    429         if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) )
    430             unset($post_data[$field]);
    431     }
    432 
    433     if ( isset($post_data['post_category']) ) {
    434         if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) )
     480        if ( isset( $post_data[ $field ] ) && ( '' == $post_data[ $field ] || -1 == $post_data[ $field ] ) ) {
     481            unset( $post_data[ $field ] );
     482        }
     483    }
     484
     485    if ( isset( $post_data['post_category'] ) ) {
     486        if ( is_array( $post_data['post_category'] ) && ! empty( $post_data['post_category'] ) ) {
    435487            $new_cats = array_map( 'absint', $post_data['post_category'] );
    436         else
    437             unset($post_data['post_category']);
     488        } else {
     489            unset( $post_data['post_category'] );
     490        }
    438491    }
    439492
    440493    $tax_input = array();
    441     if ( isset($post_data['tax_input'])) {
     494    if ( isset( $post_data['tax_input'] ) ) {
    442495        foreach ( $post_data['tax_input'] as $tax_name => $terms ) {
    443             if ( empty($terms) )
     496            if ( empty( $terms ) ) {
    444497                continue;
     498            }
    445499            if ( is_taxonomy_hierarchical( $tax_name ) ) {
    446500                $tax_input[ $tax_name ] = array_map( 'absint', $terms );
    447501            } else {
    448502                $comma = _x( ',', 'tag delimiter' );
    449                 if ( ',' !== $comma )
     503                if ( ',' !== $comma ) {
    450504                    $terms = str_replace( $comma, ',', $terms );
     505                }
    451506                $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
    452507            }
     
    454509    }
    455510
    456     if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) {
    457         $pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'");
     511    if ( isset( $post_data['post_parent'] ) && ( $parent = (int) $post_data['post_parent'] ) ) {
     512        $pages    = $wpdb->get_results( "SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'" );
    458513        $children = array();
    459514
     
    470525    }
    471526
    472     $updated = $skipped = $locked = array();
     527    $updated          = $skipped = $locked = array();
    473528    $shared_post_data = $post_data;
    474529
     
    479534        $post_type_object = get_post_type_object( get_post_type( $post_ID ) );
    480535
    481         if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( 'edit_post', $post_ID ) ) {
     536        if ( ! isset( $post_type_object ) || ( isset( $children ) && in_array( $post_ID, $children ) ) || ! current_user_can( 'edit_post', $post_ID ) ) {
    482537            $skipped[] = $post_ID;
    483538            continue;
     
    489544        }
    490545
    491         $post = get_post( $post_ID );
     546        $post      = get_post( $post_ID );
    492547        $tax_names = get_object_taxonomies( $post );
    493548        foreach ( $tax_names as $tax_name ) {
    494             $taxonomy_obj = get_taxonomy($tax_name);
    495             if ( isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) )
    496                 $new_terms = $tax_input[$tax_name];
    497             else
     549            $taxonomy_obj = get_taxonomy( $tax_name );
     550            if ( isset( $tax_input[ $tax_name ] ) && current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
     551                $new_terms = $tax_input[ $tax_name ];
     552            } else {
    498553                $new_terms = array();
    499 
    500             if ( $taxonomy_obj->hierarchical )
    501                 $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') );
    502             else
    503                 $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') );
    504 
    505             $post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms );
    506         }
    507 
    508         if ( isset($new_cats) && in_array( 'category', $tax_names ) ) {
    509             $cats = (array) wp_get_post_categories($post_ID);
    510             $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
     554            }
     555
     556            if ( $taxonomy_obj->hierarchical ) {
     557                $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'ids' ) );
     558            } else {
     559                $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'names' ) );
     560            }
     561
     562            $post_data['tax_input'][ $tax_name ] = array_merge( $current_terms, $new_terms );
     563        }
     564
     565        if ( isset( $new_cats ) && in_array( 'category', $tax_names ) ) {
     566            $cats                       = (array) wp_get_post_categories( $post_ID );
     567            $post_data['post_category'] = array_unique( array_merge( $cats, $new_cats ) );
    511568            unset( $post_data['tax_input']['category'] );
    512569        }
    513570
    514         $post_data['post_type'] = $post->post_type;
     571        $post_data['post_type']      = $post->post_type;
    515572        $post_data['post_mime_type'] = $post->post_mime_type;
    516         $post_data['guid'] = $post->guid;
     573        $post_data['guid']           = $post->guid;
    517574
    518575        foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) {
     
    522579        }
    523580
    524         $post_data['ID'] = $post_ID;
     581        $post_data['ID']      = $post_ID;
    525582        $post_data['post_ID'] = $post_ID;
    526583
     
    539596
    540597        if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
    541             if ( 'sticky' == $post_data['sticky'] )
     598            if ( 'sticky' == $post_data['sticky'] ) {
    542599                stick_post( $post_ID );
    543             else
     600            } else {
    544601                unstick_post( $post_ID );
    545         }
    546     }
    547 
    548     return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
     602            }
     603        }
     604    }
     605
     606    return array(
     607        'updated' => $updated,
     608        'skipped' => $skipped,
     609        'locked'  => $locked,
     610    );
    549611}
    550612
     
    560622function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
    561623    $post_title = '';
    562     if ( !empty( $_REQUEST['post_title'] ) )
    563         $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ));
     624    if ( ! empty( $_REQUEST['post_title'] ) ) {
     625        $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ) );
     626    }
    564627
    565628    $post_content = '';
    566     if ( !empty( $_REQUEST['content'] ) )
    567         $post_content = esc_html( wp_unslash( $_REQUEST['content'] ));
     629    if ( ! empty( $_REQUEST['content'] ) ) {
     630        $post_content = esc_html( wp_unslash( $_REQUEST['content'] ) );
     631    }
    568632
    569633    $post_excerpt = '';
    570     if ( !empty( $_REQUEST['excerpt'] ) )
    571         $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ));
     634    if ( ! empty( $_REQUEST['excerpt'] ) ) {
     635        $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ) );
     636    }
    572637
    573638    if ( $create_in_db ) {
    574         $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
    575         $post = get_post( $post_id );
    576         if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
     639        $post_id = wp_insert_post(
     640            array(
     641                'post_title'  => __( 'Auto Draft' ),
     642                'post_type'   => $post_type,
     643                'post_status' => 'auto-draft',
     644            )
     645        );
     646        $post    = get_post( $post_id );
     647        if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
    577648            set_post_format( $post, get_option( 'default_post_format' ) );
     649        }
    578650    } else {
    579         $post = new stdClass;
    580         $post->ID = 0;
    581         $post->post_author = '';
    582         $post->post_date = '';
    583         $post->post_date_gmt = '';
    584         $post->post_password = '';
    585         $post->post_name = '';
    586         $post->post_type = $post_type;
    587         $post->post_status = 'draft';
    588         $post->to_ping = '';
    589         $post->pinged = '';
     651        $post                 = new stdClass;
     652        $post->ID             = 0;
     653        $post->post_author    = '';
     654        $post->post_date      = '';
     655        $post->post_date_gmt  = '';
     656        $post->post_password  = '';
     657        $post->post_name      = '';
     658        $post->post_type      = $post_type;
     659        $post->post_status    = 'draft';
     660        $post->to_ping        = '';
     661        $post->pinged         = '';
    590662        $post->comment_status = get_default_comment_status( $post_type );
    591         $post->ping_status = get_default_comment_status( $post_type, 'pingback' );
    592         $post->post_pingback = get_option( 'default_pingback_flag' );
    593         $post->post_category = get_option( 'default_category' );
    594         $post->page_template = 'default';
    595         $post->post_parent = 0;
    596         $post->menu_order = 0;
    597         $post = new WP_Post( $post );
     663        $post->ping_status    = get_default_comment_status( $post_type, 'pingback' );
     664        $post->post_pingback  = get_option( 'default_pingback_flag' );
     665        $post->post_category  = get_option( 'default_category' );
     666        $post->page_template  = 'default';
     667        $post->post_parent    = 0;
     668        $post->menu_order     = 0;
     669        $post                 = new WP_Post( $post );
    598670    }
    599671
     
    643715 * @return int Post ID if post exists, 0 otherwise.
    644716 */
    645 function post_exists($title, $content = '', $date = '') {
     717function post_exists( $title, $content = '', $date = '' ) {
    646718    global $wpdb;
    647719
    648     $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
     720    $post_title   = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
    649721    $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
    650     $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
     722    $post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
    651723
    652724    $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
    653     $args = array();
    654 
    655     if ( !empty ( $date ) ) {
     725    $args  = array();
     726
     727    if ( ! empty( $date ) ) {
    656728        $query .= ' AND post_date = %s';
    657729        $args[] = $post_date;
    658730    }
    659731
    660     if ( !empty ( $title ) ) {
     732    if ( ! empty( $title ) ) {
    661733        $query .= ' AND post_title = %s';
    662734        $args[] = $post_title;
    663735    }
    664736
    665     if ( !empty ( $content ) ) {
     737    if ( ! empty( $content ) ) {
    666738        $query .= ' AND post_content = %s';
    667739        $args[] = $post_content;
    668740    }
    669741
    670     if ( !empty ( $args ) )
    671         return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
     742    if ( ! empty( $args ) ) {
     743        return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
     744    }
    672745
    673746    return 0;
     
    684757 */
    685758function wp_write_post() {
    686     if ( isset($_POST['post_type']) )
    687         $ptype = get_post_type_object($_POST['post_type']);
    688     else
    689         $ptype = get_post_type_object('post');
    690 
    691     if ( !current_user_can( $ptype->cap->edit_posts ) ) {
    692         if ( 'page' == $ptype->name )
     759    if ( isset( $_POST['post_type'] ) ) {
     760        $ptype = get_post_type_object( $_POST['post_type'] );
     761    } else {
     762        $ptype = get_post_type_object( 'post' );
     763    }
     764
     765    if ( ! current_user_can( $ptype->cap->edit_posts ) ) {
     766        if ( 'page' == $ptype->name ) {
    693767            return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) );
    694         else
     768        } else {
    695769            return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) );
     770        }
    696771    }
    697772
     
    702777
    703778    // Edit don't write if we have a post id.
    704     if ( isset( $_POST['post_ID'] ) )
     779    if ( isset( $_POST['post_ID'] ) ) {
    705780        return edit_post();
    706 
    707     if ( isset($_POST['visibility']) ) {
     781    }
     782
     783    if ( isset( $_POST['visibility'] ) ) {
    708784        switch ( $_POST['visibility'] ) {
    709             case 'public' :
     785            case 'public':
    710786                $_POST['post_password'] = '';
    711787                break;
    712             case 'password' :
     788            case 'password':
    713789                unset( $_POST['sticky'] );
    714790                break;
    715             case 'private' :
    716                 $_POST['post_status'] = 'private';
     791            case 'private':
     792                $_POST['post_status']   = 'private';
    717793                $_POST['post_password'] = '';
    718794                unset( $_POST['sticky'] );
     
    722798
    723799    $translated = _wp_translate_postdata( false );
    724     if ( is_wp_error($translated) )
     800    if ( is_wp_error( $translated ) ) {
    725801        return $translated;
     802    }
    726803
    727804    // Create the post.
    728805    $post_ID = wp_insert_post( $_POST );
    729     if ( is_wp_error( $post_ID ) )
     806    if ( is_wp_error( $post_ID ) ) {
    730807        return $post_ID;
    731 
    732     if ( empty($post_ID) )
     808    }
     809
     810    if ( empty( $post_ID ) ) {
    733811        return 0;
     812    }
    734813
    735814    add_meta( $post_ID );
     
    754833function write_post() {
    755834    $result = wp_write_post();
    756     if ( is_wp_error( $result ) )
     835    if ( is_wp_error( $result ) ) {
    757836        wp_die( $result->get_error_message() );
    758     else
     837    } else {
    759838        return $result;
     839    }
    760840}
    761841
     
    775855    $post_ID = (int) $post_ID;
    776856
    777     $metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
    778     $metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
    779     $metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : '';
    780     if ( is_string( $metavalue ) )
     857    $metakeyselect = isset( $_POST['metakeyselect'] ) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
     858    $metakeyinput  = isset( $_POST['metakeyinput'] ) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
     859    $metavalue     = isset( $_POST['metavalue'] ) ? $_POST['metavalue'] : '';
     860    if ( is_string( $metavalue ) ) {
    781861        $metavalue = trim( $metavalue );
    782 
    783     if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) {
     862    }
     863
     864    if ( ( '0' === $metavalue || ! empty( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) ) {
    784865        /*
    785866         * We have a key/value pair. If both the select and the input
    786867         * for the key have data, the input takes precedence.
    787868         */
    788         if ( '#NONE#' != $metakeyselect )
     869        if ( '#NONE#' != $metakeyselect ) {
    789870            $metakey = $metakeyselect;
    790 
    791         if ( $metakeyinput )
     871        }
     872
     873        if ( $metakeyinput ) {
    792874            $metakey = $metakeyinput; // default
    793 
    794         if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) )
     875        }
     876
     877        if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) {
    795878            return false;
     879        }
    796880
    797881        $metakey = wp_slash( $metakey );
     
    812896 */
    813897function delete_meta( $mid ) {
    814     return delete_metadata_by_mid( 'post' , $mid );
     898    return delete_metadata_by_mid( 'post', $mid );
    815899}
    816900
     
    827911    global $wpdb;
    828912
    829     $keys = $wpdb->get_col( "
     913    $keys = $wpdb->get_col(
     914        "
    830915            SELECT meta_key
    831916            FROM $wpdb->postmeta
    832917            GROUP BY meta_key
    833             ORDER BY meta_key" );
     918            ORDER BY meta_key"
     919    );
    834920
    835921    return $keys;
     
    861947    global $wpdb;
    862948
    863     return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
     949    return $wpdb->get_results(
     950        $wpdb->prepare(
     951            "SELECT meta_key, meta_value, meta_id, post_id
    864952            FROM $wpdb->postmeta WHERE post_id = %d
    865             ORDER BY meta_key,meta_id", $postid), ARRAY_A );
     953            ORDER BY meta_key,meta_id", $postid
     954        ), ARRAY_A
     955    );
    866956}
    867957
     
    877967 */
    878968function update_meta( $meta_id, $meta_key, $meta_value ) {
    879     $meta_key = wp_unslash( $meta_key );
     969    $meta_key   = wp_unslash( $meta_key );
    880970    $meta_value = wp_unslash( $meta_value );
    881971
     
    897987 */
    898988function _fix_attachment_links( $post ) {
    899     $post = get_post( $post, ARRAY_A );
     989    $post    = get_post( $post, ARRAY_A );
    900990    $content = $post['post_content'];
    901991
    902992    // Don't run if no pretty permalinks or post is not published, scheduled, or privately published.
    903     if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ) ) )
     993    if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ) ) ) {
    904994        return;
     995    }
    905996
    906997    // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero)
    907     if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) )
     998    if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) {
    908999        return;
    909 
    910     $site_url = get_bloginfo('url');
    911     $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
    912     $replace = '';
     1000    }
     1001
     1002    $site_url = get_bloginfo( 'url' );
     1003    $site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // remove the http(s)
     1004    $replace  = '';
    9131005
    9141006    foreach ( $link_matches[1] as $key => $value ) {
    915         if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-')
    916             || !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
    917             || !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) )
     1007        if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' )
     1008            || ! preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
     1009            || ! preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) {
    9181010                continue;
    919 
    920         $quote = $url_match[1]; // the quote (single or double)
     1011        }
     1012
     1013        $quote  = $url_match[1]; // the quote (single or double)
    9211014        $url_id = (int) $url_match[2];
    9221015        $rel_id = (int) $rel_match[1];
    9231016
    924         if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false )
     1017        if ( ! $url_id || ! $rel_id || $url_id != $rel_id || strpos( $url_match[0], $site_url ) === false ) {
    9251018            continue;
    926 
    927         $link = $link_matches[0][$key];
     1019        }
     1020
     1021        $link    = $link_matches[0][ $key ];
    9281022        $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
    9291023
     
    9341028        $post['post_content'] = $content;
    9351029        // Escape data pulled from DB.
    936         $post = add_magic_quotes($post);
    937 
    938         return wp_update_post($post);
     1030        $post = add_magic_quotes( $post );
     1031
     1032        return wp_update_post( $post );
    9391033    }
    9401034}
     
    9481042 * @return array As array of all the statuses for the supplied post type
    9491043 */
    950 function get_available_post_statuses($type = 'post') {
    951     $stati = wp_count_posts($type);
    952 
    953     return array_keys(get_object_vars($stati));
     1044function get_available_post_statuses( $type = 'post' ) {
     1045    $stati = wp_count_posts( $type );
     1046
     1047    return array_keys( get_object_vars( $stati ) );
    9541048}
    9551049
     
    9631057 */
    9641058function wp_edit_posts_query( $q = false ) {
    965     if ( false === $q )
     1059    if ( false === $q ) {
    9661060        $q = $_GET;
    967     $q['m'] = isset($q['m']) ? (int) $q['m'] : 0;
    968     $q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
    969     $post_stati  = get_post_stati();
    970 
    971     if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) )
     1061    }
     1062    $q['m']     = isset( $q['m'] ) ? (int) $q['m'] : 0;
     1063    $q['cat']   = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
     1064    $post_stati = get_post_stati();
     1065
     1066    if ( isset( $q['post_type'] ) && in_array( $q['post_type'], get_post_types() ) ) {
    9721067        $post_type = $q['post_type'];
    973     else
     1068    } else {
    9741069        $post_type = 'post';
    975 
    976     $avail_post_stati = get_available_post_statuses($post_type);
    977 
    978     if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
     1070    }
     1071
     1072    $avail_post_stati = get_available_post_statuses( $post_type );
     1073
     1074    if ( isset( $q['post_status'] ) && in_array( $q['post_status'], $post_stati ) ) {
    9791075        $post_status = $q['post_status'];
    980         $perm = 'readable';
     1076        $perm        = 'readable';
    9811077    }
    9821078
     
    9931089    }
    9941090
    995     $per_page = "edit_{$post_type}_per_page";
     1091    $per_page       = "edit_{$post_type}_per_page";
    9961092    $posts_per_page = (int) get_user_option( $per_page );
    997     if ( empty( $posts_per_page ) || $posts_per_page < 1 )
     1093    if ( empty( $posts_per_page ) || $posts_per_page < 1 ) {
    9981094        $posts_per_page = 20;
     1095    }
    9991096
    10001097    /**
     
    10231120    $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
    10241121
    1025     $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
     1122    $query = compact( 'post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page' );
    10261123
    10271124    // Hierarchical types require special args.
    1028     if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
    1029         $query['orderby'] = 'menu_order title';
    1030         $query['order'] = 'asc';
    1031         $query['posts_per_page'] = -1;
     1125    if ( is_post_type_hierarchical( $post_type ) && ! isset( $orderby ) ) {
     1126        $query['orderby']                = 'menu_order title';
     1127        $query['order']                  = 'asc';
     1128        $query['posts_per_page']         = -1;
    10321129        $query['posts_per_archive_page'] = -1;
    1033         $query['fields'] = 'id=>parent';
    1034     }
    1035 
    1036     if ( ! empty( $q['show_sticky'] ) )
     1130        $query['fields']                 = 'id=>parent';
     1131    }
     1132
     1133    if ( ! empty( $q['show_sticky'] ) ) {
    10371134        $query['post__in'] = (array) get_option( 'sticky_posts' );
     1135    }
    10381136
    10391137    wp( $query );
     
    10521150 * @return mixed
    10531151 */
    1054 function get_available_post_mime_types($type = 'attachment') {
     1152function get_available_post_mime_types( $type = 'attachment' ) {
    10551153    global $wpdb;
    10561154
    1057     $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
     1155    $types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) );
    10581156    return $types;
    10591157}
     
    10721170        $q = $_GET;
    10731171    }
    1074     $q['m']   = isset( $q['m'] ) ? (int) $q['m'] : 0;
    1075     $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
     1172    $q['m']         = isset( $q['m'] ) ? (int) $q['m'] : 0;
     1173    $q['cat']       = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
    10761174    $q['post_type'] = 'attachment';
    1077     $post_type = get_post_type_object( 'attachment' );
    1078     $states = 'inherit';
     1175    $post_type      = get_post_type_object( 'attachment' );
     1176    $states         = 'inherit';
    10791177    if ( current_user_can( $post_type->cap->read_private_posts ) ) {
    10801178        $states .= ',private';
     
    10991197
    11001198    $post_mime_types = get_post_mime_types();
    1101     if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) {
    1102         unset($q['post_mime_type']);
     1199    if ( isset( $q['post_mime_type'] ) && ! array_intersect( (array) $q['post_mime_type'], array_keys( $post_mime_types ) ) ) {
     1200        unset( $q['post_mime_type'] );
    11031201    }
    11041202
     
    11341232    wp( wp_edit_attachments_query_vars( $q ) );
    11351233
    1136     $post_mime_types = get_post_mime_types();
     1234    $post_mime_types       = get_post_mime_types();
    11371235    $avail_post_mime_types = get_available_post_mime_types( 'attachment' );
    11381236
     
    11521250    if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
    11531251        $classes = array( '' );
    1154     } elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
    1155         if ( !is_array( $closed ) ) {
     1252    } elseif ( $closed = get_user_option( 'closedpostboxes_' . $page ) ) {
     1253        if ( ! is_array( $closed ) ) {
    11561254            $classes = array( '' );
    11571255        } else {
     
    11861284 * @return array Array containing the sample permalink with placeholder for the post name, and the post name.
    11871285 */
    1188 function get_sample_permalink($id, $title = null, $name = null) {
     1286function get_sample_permalink( $id, $title = null, $name = null ) {
    11891287    $post = get_post( $id );
    1190     if ( ! $post )
     1288    if ( ! $post ) {
    11911289        return array( '', '' );
    1192 
    1193     $ptype = get_post_type_object($post->post_type);
     1290    }
     1291
     1292    $ptype = get_post_type_object( $post->post_type );
    11941293
    11951294    $original_status = $post->post_status;
    1196     $original_date = $post->post_date;
    1197     $original_name = $post->post_name;
     1295    $original_date   = $post->post_date;
     1296    $original_name   = $post->post_name;
    11981297
    11991298    // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
    12001299    if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ) ) ) {
    12011300        $post->post_status = 'publish';
    1202         $post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
     1301        $post->post_name   = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
    12031302    }
    12041303
    12051304    // If the user wants to set a new name -- override the current one
    12061305    // Note: if empty name is supplied -- use the title instead, see #6072
    1207     if ( !is_null($name) )
    1208         $post->post_name = sanitize_title($name ? $name : $title, $post->ID);
    1209 
    1210     $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
     1306    if ( ! is_null( $name ) ) {
     1307        $post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
     1308    }
     1309
     1310    $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
    12111311
    12121312    $post->filter = 'sample';
    12131313
    1214     $permalink = get_permalink($post, true);
     1314    $permalink = get_permalink( $post, true );
    12151315
    12161316    // Replace custom post_type Token with generic pagename token for ease of use.
    1217     $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
     1317    $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
    12181318
    12191319    // Handle page hierarchy
    12201320    if ( $ptype->hierarchical ) {
    1221         $uri = get_page_uri($post);
     1321        $uri = get_page_uri( $post );
    12221322        if ( $uri ) {
    1223             $uri = untrailingslashit($uri);
     1323            $uri = untrailingslashit( $uri );
    12241324            $uri = strrev( stristr( strrev( $uri ), '/' ) );
    1225             $uri = untrailingslashit($uri);
     1325            $uri = untrailingslashit( $uri );
    12261326        }
    12271327
    12281328        /** This filter is documented in wp-admin/edit-tag-form.php */
    12291329        $uri = apply_filters( 'editable_slug', $uri, $post );
    1230         if ( !empty($uri) )
     1330        if ( ! empty( $uri ) ) {
    12311331            $uri .= '/';
    1232         $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
     1332        }
     1333        $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
    12331334    }
    12341335
    12351336    /** This filter is documented in wp-admin/edit-tag-form.php */
    1236     $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) );
     1337    $permalink         = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) );
    12371338    $post->post_status = $original_status;
    1238     $post->post_date = $original_date;
    1239     $post->post_name = $original_name;
    1240     unset($post->filter);
     1339    $post->post_date   = $original_date;
     1340    $post->post_name   = $original_name;
     1341    unset( $post->filter );
    12411342
    12421343    /**
     
    12661367function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
    12671368    $post = get_post( $id );
    1268     if ( ! $post )
     1369    if ( ! $post ) {
    12691370        return '';
    1270 
    1271     list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
    1272 
    1273     $view_link = false;
     1371    }
     1372
     1373    list($permalink, $post_name) = get_sample_permalink( $post->ID, $new_title, $new_slug );
     1374
     1375    $view_link      = false;
    12741376    $preview_target = '';
    12751377
    12761378    if ( current_user_can( 'read_post', $post->ID ) ) {
    12771379        if ( 'draft' === $post->post_status || empty( $post->post_name ) ) {
    1278             $view_link = get_preview_post_link( $post );
     1380            $view_link      = get_preview_post_link( $post );
    12791381            $preview_target = " target='wp-preview-{$post->ID}'";
    12801382        } else {
     
    12941396        if ( false !== $view_link ) {
    12951397            $display_link = urldecode( $view_link );
    1296             $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n";
     1398            $return      .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n";
    12971399        } else {
    12981400            $return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
     
    13001402
    13011403        // Encourage a pretty permalink setting
    1302         if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) {
    1303             $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
     1404        if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && ! ( 'page' == get_option( 'show_on_front' ) && $id == get_option( 'page_on_front' ) ) ) {
     1405            $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __( 'Change Permalinks' ) . "</a></span>\n";
    13041406        }
    13051407    } else {
     
    13111413
    13121414        $post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>';
    1313         $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) );
    1314 
    1315         $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
     1415        $display_link   = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) );
     1416
     1417        $return  = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
    13161418        $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n";
    13171419        $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
     
    13541456    $upload_iframe_src  = get_upload_iframe_src( 'image', $post->ID );
    13551457
    1356     $content = sprintf( $set_thumbnail_link,
     1458    $content = sprintf(
     1459        $set_thumbnail_link,
    13571460        esc_url( $upload_iframe_src ),
    13581461        '', // Empty when there's no featured image set, `aria-describedby` attribute otherwise.
     
    13851488
    13861489        if ( ! empty( $thumbnail_html ) ) {
    1387             $content = sprintf( $set_thumbnail_link,
     1490            $content  = sprintf(
     1491                $set_thumbnail_link,
    13881492                esc_url( $upload_iframe_src ),
    13891493                ' aria-describedby="set-post-thumbnail-desc"',
     
    14651569    }
    14661570
    1467     $now = time();
     1571    $now  = time();
    14681572    $lock = "$now:$user_id";
    14691573
     
    14801584 */
    14811585function _admin_notice_post_locked() {
    1482     if ( ! $post = get_post() )
     1586    if ( ! $post = get_post() ) {
    14831587        return;
     1588    }
    14841589
    14851590    $user = null;
    1486     if (  $user_id = wp_check_post_lock( $post->ID ) )
     1591    if ( $user_id = wp_check_post_lock( $post->ID ) ) {
    14871592        $user = get_userdata( $user_id );
     1593    }
    14881594
    14891595    if ( $user ) {
     
    15001606         * @param WP_User|bool $user    WP_User object on success, false otherwise.
    15011607         */
    1502         if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) )
     1608        if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) ) {
    15031609            return;
     1610        }
    15041611
    15051612        $locked = true;
     
    15111618        false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) {
    15121619
    1513         $sendback_text = __('Go back');
     1620        $sendback_text = __( 'Go back' );
    15141621    } else {
    15151622        $sendback = admin_url( 'edit.php' );
    15161623
    1517         if ( 'post' != $post->post_type )
     1624        if ( 'post' != $post->post_type ) {
    15181625            $sendback = add_query_arg( 'post_type', $post->post_type, $sendback );
     1626        }
    15191627
    15201628        $sendback_text = get_post_type_object( $post->post_type )->labels->all_items;
     
    15341642            if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
    15351643                // Latest content is in autosave
    1536                 $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
    1537                 $query_args['preview_id'] = $post->ID;
     1644                $nonce                       = wp_create_nonce( 'post_preview_' . $post->ID );
     1645                $query_args['preview_id']    = $post->ID;
    15381646                $query_args['preview_nonce'] = $nonce;
    15391647            }
     
    15621670        <p class="currently-editing wp-tab-first" tabindex="0">
    15631671        <?php
    1564             if ( $override ) {
    1565                 /* translators: %s: user's display name */
    1566                 printf( __( '%s is already editing this post. Do you want to take over?' ), esc_html( $user->display_name ) );
    1567             } else {
    1568                 /* translators: %s: user's display name */
    1569                 printf( __( '%s is already editing this post.' ), esc_html( $user->display_name ) );
    1570             }
     1672        if ( $override ) {
     1673            /* translators: %s: user's display name */
     1674            printf( __( '%s is already editing this post. Do you want to take over?' ), esc_html( $user->display_name ) );
     1675        } else {
     1676            /* translators: %s: user's display name */
     1677            printf( __( '%s is already editing this post.' ), esc_html( $user->display_name ) );
     1678        }
    15711679        ?>
    15721680        </p>
     
    15841692        <a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a>
    15851693        <?php if ( $preview_link ) { ?>
    1586         <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e('Preview'); ?></a>
     1694        <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e( 'Preview' ); ?></a>
    15871695        <?php
    1588         }
     1696}
    15891697
    15901698        // Allow plugins to prevent some users overriding the post lock
    1591         if ( $override ) {
    1592             ?>
    1593             <a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', wp_nonce_url( get_edit_post_link( $post->ID, 'url' ), 'lock-post_' . $post->ID ) ) ); ?>"><?php _e('Take over'); ?></a>
     1699if ( $override ) {
     1700    ?>
     1701    <a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', wp_nonce_url( get_edit_post_link( $post->ID, 'url' ), 'lock-post_' . $post->ID ) ) ); ?>"><?php _e( 'Take over' ); ?></a>
    15941702            <?php
    1595         }
     1703}
    15961704
    15971705        ?>
     
    16061714            <span class="currently-editing"></span><br />
    16071715            <span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" alt="" /> <?php _e( 'Saving revision&hellip;' ); ?></span>
    1608             <span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span>
     1716            <span class="locked-saved hidden"><?php _e( 'Your latest changes were saved as a revision.' ); ?></span>
    16091717            </p>
    16101718            <?php
     
    16391747function wp_create_post_autosave( $post_data ) {
    16401748    if ( is_numeric( $post_data ) ) {
    1641         $post_id = $post_data;
     1749        $post_id   = $post_data;
    16421750        $post_data = $_POST;
    16431751    } else {
     
    16461754
    16471755    $post_data = _wp_translate_postdata( true, $post_data );
    1648     if ( is_wp_error( $post_data ) )
     1756    if ( is_wp_error( $post_data ) ) {
    16491757        return $post_data;
     1758    }
    16501759
    16511760    $post_author = get_current_user_id();
     
    16531762    // Store one autosave per author. If there is already an autosave, overwrite it.
    16541763    if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
    1655         $new_autosave = _wp_post_revision_data( $post_data, true );
    1656         $new_autosave['ID'] = $old_autosave->ID;
     1764        $new_autosave                = _wp_post_revision_data( $post_data, true );
     1765        $new_autosave['ID']          = $old_autosave->ID;
    16571766        $new_autosave['post_author'] = $post_author;
    16581767
    16591768        // If the new autosave has the same content as the post, delete the autosave.
    1660         $post = get_post( $post_id );
     1769        $post                  = get_post( $post_id );
    16611770        $autosave_is_different = false;
    16621771        foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
     
    17001809function post_preview() {
    17011810
    1702     $post_ID = (int) $_POST['post_ID'];
     1811    $post_ID     = (int) $_POST['post_ID'];
    17031812    $_POST['ID'] = $post_ID;
    17041813
     
    17181827        $is_autosave = true;
    17191828
    1720         if ( isset( $_POST['post_status'] ) && 'auto-draft' == $_POST['post_status'] )
     1829        if ( isset( $_POST['post_status'] ) && 'auto-draft' == $_POST['post_status'] ) {
    17211830            $_POST['post_status'] = 'draft';
     1831        }
    17221832
    17231833        $saved_post_id = wp_create_post_autosave( $post->ID );
    17241834    }
    17251835
    1726     if ( is_wp_error( $saved_post_id ) )
     1836    if ( is_wp_error( $saved_post_id ) ) {
    17271837        wp_die( $saved_post_id->get_error_message() );
     1838    }
    17281839
    17291840    $query_args = array();
    17301841
    17311842    if ( $is_autosave && $saved_post_id ) {
    1732         $query_args['preview_id'] = $post->ID;
     1843        $query_args['preview_id']    = $post->ID;
    17331844        $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
    17341845
     
    17581869function wp_autosave( $post_data ) {
    17591870    // Back-compat
    1760     if ( ! defined( 'DOING_AUTOSAVE' ) )
     1871    if ( ! defined( 'DOING_AUTOSAVE' ) ) {
    17611872        define( 'DOING_AUTOSAVE', true );
    1762 
    1763     $post_id = (int) $post_data['post_id'];
     1873    }
     1874
     1875    $post_id         = (int) $post_data['post_id'];
    17641876    $post_data['ID'] = $post_data['post_ID'] = $post_id;
    17651877
     
    17741886    }
    17751887
    1776     if ( 'auto-draft' == $post->post_status )
     1888    if ( 'auto-draft' == $post->post_status ) {
    17771889        $post_data['post_status'] = 'draft';
    1778 
    1779     if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) )
     1890    }
     1891
     1892    if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) ) {
    17801893        $post_data['post_category'] = explode( ',', $post_data['catslist'] );
     1894    }
    17811895
    17821896    if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
     
    17941908 * @param int $post_id Optional. Post ID.
    17951909 */
    1796 function redirect_post($post_id = '') {
    1797     if ( isset($_POST['save']) || isset($_POST['publish']) ) {
     1910function redirect_post( $post_id = '' ) {
     1911    if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) {
    17981912        $status = get_post_status( $post_id );
    17991913
     
    18141928
    18151929        $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
    1816     } elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) {
     1930    } elseif ( isset( $_POST['addmeta'] ) && $_POST['addmeta'] ) {
    18171931        $location = add_query_arg( 'message', 2, wp_get_referer() );
    1818         $location = explode('#', $location);
     1932        $location = explode( '#', $location );
    18191933        $location = $location[0] . '#postcustom';
    1820     } elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
     1934    } elseif ( isset( $_POST['deletemeta'] ) && $_POST['deletemeta'] ) {
    18211935        $location = add_query_arg( 'message', 3, wp_get_referer() );
    1822         $location = explode('#', $location);
     1936        $location = explode( '#', $location );
    18231937        $location = $location[0] . '#postcustom';
    18241938    } else {
     
    18791993        }
    18801994
    1881         $_term = get_terms( $taxonomy, array(
    1882             'name' => $term,
    1883             'fields' => 'ids',
    1884             'hide_empty' => false,
    1885         ) );
     1995        $_term = get_terms(
     1996            $taxonomy, array(
     1997                'name'       => $term,
     1998                'fields'     => 'ids',
     1999                'hide_empty' => false,
     2000            )
     2001        );
    18862002
    18872003        if ( ! empty( $_term ) ) {
Note: See TracChangeset for help on using the changeset viewer.