Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 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-includes/bookmark.php

    r39600 r42343  
    2020 * @return array|object|null Type returned depends on $output value.
    2121 */
    22 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
     22function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) {
    2323    global $wpdb;
    2424
    25     if ( empty($bookmark) ) {
    26         if ( isset($GLOBALS['link']) )
     25    if ( empty( $bookmark ) ) {
     26        if ( isset( $GLOBALS['link'] ) ) {
    2727            $_bookmark = & $GLOBALS['link'];
    28         else
     28        } else {
    2929            $_bookmark = null;
    30     } elseif ( is_object($bookmark) ) {
    31         wp_cache_add($bookmark->link_id, $bookmark, 'bookmark');
     30        }
     31    } elseif ( is_object( $bookmark ) ) {
     32        wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' );
    3233        $_bookmark = $bookmark;
    3334    } else {
    34         if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
     35        if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) {
    3536            $_bookmark = & $GLOBALS['link'];
    36         } elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
    37             $_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
     37        } elseif ( ! $_bookmark = wp_cache_get( $bookmark, 'bookmark' ) ) {
     38            $_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) );
    3839            if ( $_bookmark ) {
    3940                $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
     
    4344    }
    4445
    45     if ( ! $_bookmark )
     46    if ( ! $_bookmark ) {
    4647        return $_bookmark;
    47 
    48     $_bookmark = sanitize_bookmark($_bookmark, $filter);
     48    }
     49
     50    $_bookmark = sanitize_bookmark( $_bookmark, $filter );
    4951
    5052    if ( $output == OBJECT ) {
    5153        return $_bookmark;
    5254    } elseif ( $output == ARRAY_A ) {
    53         return get_object_vars($_bookmark);
     55        return get_object_vars( $_bookmark );
    5456    } elseif ( $output == ARRAY_N ) {
    55         return array_values(get_object_vars($_bookmark));
     57        return array_values( get_object_vars( $_bookmark ) );
    5658    } else {
    5759        return $_bookmark;
     
    7375    $bookmark = get_bookmark( $bookmark );
    7476
    75     if ( is_wp_error($bookmark) )
     77    if ( is_wp_error( $bookmark ) ) {
    7678        return $bookmark;
    77 
    78     if ( !is_object($bookmark) )
     79    }
     80
     81    if ( ! is_object( $bookmark ) ) {
    7982        return '';
    80 
    81     if ( !isset($bookmark->$field) )
     83    }
     84
     85    if ( ! isset( $bookmark->$field ) ) {
    8286        return '';
    83 
    84     return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
     87    }
     88
     89    return sanitize_bookmark_field( $field, $bookmark->$field, $bookmark->link_id, $context );
    8590}
    8691
     
    120125
    121126    $defaults = array(
    122         'orderby' => 'name', 'order' => 'ASC',
    123         'limit' => -1, 'category' => '',
    124         'category_name' => '', 'hide_invisible' => 1,
    125         'show_updated' => 0, 'include' => '',
    126         'exclude' => '', 'search' => ''
     127        'orderby'        => 'name',
     128        'order'          => 'ASC',
     129        'limit'          => -1,
     130        'category'       => '',
     131        'category_name'  => '',
     132        'hide_invisible' => 1,
     133        'show_updated'   => 0,
     134        'include'        => '',
     135        'exclude'        => '',
     136        'search'         => '',
    127137    );
    128138
    129139    $r = wp_parse_args( $args, $defaults );
    130140
    131     $key = md5( serialize( $r ) );
     141    $key   = md5( serialize( $r ) );
    132142    $cache = false;
    133143    if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
     
    159169    $inclusions = '';
    160170    if ( ! empty( $r['include'] ) ) {
    161         $r['exclude'] = '';  //ignore exclude, category, and category_name params if using include
    162         $r['category'] = '';
     171        $r['exclude']       = '';  //ignore exclude, category, and category_name params if using include
     172        $r['category']      = '';
    163173        $r['category_name'] = '';
    164         $inclinks = preg_split( '/[\s,]+/', $r['include'] );
     174        $inclinks           = preg_split( '/[\s,]+/', $r['include'] );
    165175        if ( count( $inclinks ) ) {
    166176            foreach ( $inclinks as $inclink ) {
     
    173183        }
    174184    }
    175     if (! empty( $inclusions ) ) {
     185    if ( ! empty( $inclusions ) ) {
    176186        $inclusions .= ')';
    177187    }
     
    195205
    196206    if ( ! empty( $r['category_name'] ) ) {
    197         if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) {
     207        if ( $r['category'] = get_term_by( 'name', $r['category_name'], 'link_category' ) ) {
    198208            $r['category'] = $r['category']->term_id;
    199209        } else {
     
    207217    $search = '';
    208218    if ( ! empty( $r['search'] ) ) {
    209         $like = '%' . $wpdb->esc_like( $r['search'] ) . '%';
    210         $search = $wpdb->prepare(" AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ", $like, $like, $like );
     219        $like   = '%' . $wpdb->esc_like( $r['search'] ) . '%';
     220        $search = $wpdb->prepare( ' AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ', $like, $like, $like );
    211221    }
    212222
    213223    $category_query = '';
    214     $join = '';
     224    $join           = '';
    215225    if ( ! empty( $r['category'] ) ) {
    216226        $incategories = preg_split( '/[\s,]+/', $r['category'] );
    217         if ( count($incategories) ) {
     227        if ( count( $incategories ) ) {
    218228            foreach ( $incategories as $incat ) {
    219229                if ( empty( $category_query ) ) {
     
    227237    if ( ! empty( $category_query ) ) {
    228238        $category_query .= ") AND taxonomy = 'link_category'";
    229         $join = " INNER JOIN $wpdb->term_relationships AS tr ON ($wpdb->links.link_id = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id";
     239        $join            = " INNER JOIN $wpdb->term_relationships AS tr ON ($wpdb->links.link_id = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id";
    230240    }
    231241
    232242    if ( $r['show_updated'] ) {
    233         $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ";
     243        $recently_updated_test = ', IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ';
    234244    } else {
    235245        $recently_updated_test = '';
     
    239249
    240250    $orderby = strtolower( $r['orderby'] );
    241     $length = '';
     251    $length  = '';
    242252    switch ( $orderby ) {
    243253        case 'length':
    244             $length = ", CHAR_LENGTH(link_name) AS length";
     254            $length = ', CHAR_LENGTH(link_name) AS length';
    245255            break;
    246256        case 'rand':
     
    252262        default:
    253263            $orderparams = array();
    254             $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );
     264            $keys        = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );
    255265            foreach ( explode( ',', $orderby ) as $ordparam ) {
    256266                $ordparam = trim( $ordparam );
     
    279289    }
    280290
    281     $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
     291    $query  = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
    282292    $query .= " $exclusions $inclusions $search";
    283293    $query .= " ORDER BY $orderby $order";
     
    304314 * @param stdClass|array $bookmark Bookmark row
    305315 * @param string $context Optional, default is 'display'. How to filter the
    306  *      fields
     316 *      fields
    307317 * @return stdClass|array Same type as $bookmark but with fields sanitized.
    308318 */
    309 function sanitize_bookmark($bookmark, $context = 'display') {
    310     $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
    311         'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
    312         'link_rel', 'link_notes', 'link_rss', );
    313 
    314     if ( is_object($bookmark) ) {
     319function sanitize_bookmark( $bookmark, $context = 'display' ) {
     320    $fields = array(
     321        'link_id',
     322        'link_url',
     323        'link_name',
     324        'link_image',
     325        'link_target',
     326        'link_category',
     327        'link_description',
     328        'link_visible',
     329        'link_owner',
     330        'link_rating',
     331        'link_updated',
     332        'link_rel',
     333        'link_notes',
     334        'link_rss',
     335    );
     336
     337    if ( is_object( $bookmark ) ) {
    315338        $do_object = true;
    316         $link_id = $bookmark->link_id;
     339        $link_id   = $bookmark->link_id;
    317340    } else {
    318341        $do_object = false;
    319         $link_id = $bookmark['link_id'];
     342        $link_id   = $bookmark['link_id'];
    320343    }
    321344
    322345    foreach ( $fields as $field ) {
    323346        if ( $do_object ) {
    324             if ( isset($bookmark->$field) )
    325                 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context);
     347            if ( isset( $bookmark->$field ) ) {
     348                $bookmark->$field = sanitize_bookmark_field( $field, $bookmark->$field, $link_id, $context );
     349            }
    326350        } else {
    327             if ( isset($bookmark[$field]) )
    328                 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context);
     351            if ( isset( $bookmark[ $field ] ) ) {
     352                $bookmark[ $field ] = sanitize_bookmark_field( $field, $bookmark[ $field ], $link_id, $context );
     353            }
    329354        }
    330355    }
     
    359384function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) {
    360385    switch ( $field ) {
    361     case 'link_id' : // ints
    362     case 'link_rating' :
    363         $value = (int) $value;
    364         break;
    365     case 'link_category' : // array( ints )
    366         $value = array_map('absint', (array) $value);
    367         // We return here so that the categories aren't filtered.
    368         // The 'link_category' filter is for the name of a link category, not an array of a link's link categories
     386        case 'link_id': // ints
     387        case 'link_rating':
     388            $value = (int) $value;
     389            break;
     390        case 'link_category': // array( ints )
     391            $value = array_map( 'absint', (array) $value );
     392            // We return here so that the categories aren't filtered.
     393            // The 'link_category' filter is for the name of a link category, not an array of a link's link categories
     394            return $value;
     395
     396        case 'link_visible': // bool stored as Y|N
     397            $value = preg_replace( '/[^YNyn]/', '', $value );
     398            break;
     399        case 'link_target': // "enum"
     400            $targets = array( '_top', '_blank' );
     401            if ( ! in_array( $value, $targets ) ) {
     402                $value = '';
     403            }
     404            break;
     405    }
     406
     407    if ( 'raw' == $context ) {
    369408        return $value;
    370 
    371     case 'link_visible' : // bool stored as Y|N
    372         $value = preg_replace('/[^YNyn]/', '', $value);
    373         break;
    374     case 'link_target' : // "enum"
    375         $targets = array('_top', '_blank');
    376         if ( ! in_array($value, $targets) )
    377             $value = '';
    378         break;
    379     }
    380 
    381     if ( 'raw' == $context )
    382         return $value;
     409    }
    383410
    384411    if ( 'edit' == $context ) {
     
    389416            $value = esc_html( $value ); // textarea_escaped
    390417        } else {
    391             $value = esc_attr($value);
     418            $value = esc_attr( $value );
    392419        }
    393420    } elseif ( 'db' == $context ) {
     
    418445    wp_cache_delete( $bookmark_id, 'bookmark' );
    419446    wp_cache_delete( 'get_bookmarks', 'bookmark' );
    420     clean_object_term_cache( $bookmark_id, 'link');
    421 }
     447    clean_object_term_cache( $bookmark_id, 'link' );
     448}
Note: See TracChangeset for help on using the changeset viewer.