Changeset 6223

Show
Ignore:
Timestamp:
10/11/07 05:46:57 (1 year ago)
Author:
markjaquith
Message:

Code cleanup and standardization for functions.php

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-includes/functions.php

    r6222 r6223  
    11<?php 
    22 
    3 function mysql2date($dateformatstring, $mysqlstring, $translate = true) { 
     3function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) { 
    44    global $wp_locale; 
    55    $m = $mysqlstring; 
    6     if ( empty($m) ) { 
     6    if ( empty( $m ) ) 
    77        return false; 
    8     } 
    98    $i = mktime( 
    109        (int) substr( $m, 11, 2 ), (int) substr( $m, 14, 2 ), (int) substr( $m, 17, 2 ), 
     
    1817        $i = 0; 
    1918 
    20     if ( !empty($wp_locale->month) && !empty($wp_locale->weekday) && $translate ) { 
    21         $datemonth = $wp_locale->get_month(date('m', $i)); 
    22         $datemonth_abbrev = $wp_locale->get_month_abbrev($datemonth); 
    23         $dateweekday = $wp_locale->get_weekday(date('w', $i)); 
    24         $dateweekday_abbrev = $wp_locale->get_weekday_abbrev($dateweekday); 
    25         $datemeridiem = $wp_locale->get_meridiem(date('a', $i)); 
    26         $datemeridiem_capital = $wp_locale->get_meridiem(date('A', $i)); 
    27         $dateformatstring = ' '.$dateformatstring; 
    28         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring); 
    29         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring); 
    30         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring); 
    31         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring); 
    32         $dateformatstring = preg_replace("/([^\\\])a/", "\\1".backslashit($datemeridiem), $dateformatstring); 
    33         $dateformatstring = preg_replace("/([^\\\])A/", "\\1".backslashit($datemeridiem_capital), $dateformatstring); 
    34  
    35         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1); 
    36     } 
    37     $j = @date($dateformatstring, $i); 
    38     if ( !$j ) { 
    39     // for debug purposes 
    40     //  echo $i." ".$mysqlstring; 
    41     } 
     19    if ( !empty( $wp_locale->month ) && !empty( $wp_locale->weekday ) && $translate ) { 
     20        $datemonth = $wp_locale->get_month( date( 'm', $i ) ); 
     21        $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth ); 
     22        $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) ); 
     23        $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday ); 
     24        $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) ); 
     25        $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) ); 
     26        $dateformatstring = ' ' . $dateformatstring; 
     27        $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring ); 
     28        $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring ); 
     29        $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring ); 
     30        $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring ); 
     31        $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring ); 
     32        $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring ); 
     33 
     34        $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 ); 
     35    } 
     36    $j = @date( $dateformatstring, $i ); 
     37 
     38    /* 
     39    if ( !$j ) // for debug purposes 
     40        echo $i." ".$mysqlstring; 
     41    */ 
     42 
    4243    return $j; 
    4344} 
    4445 
    45 function current_time($type, $gmt = 0) { 
    46     switch ($type) { 
     46 
     47function current_time( $type, $gmt = 0 ) { 
     48    switch ( $type ) { 
    4749        case 'mysql': 
    48             if ( $gmt ) $d = gmdate('Y-m-d H:i:s'); 
    49             else $d = gmdate('Y-m-d H:i:s', (time() + (get_option('gmt_offset') * 3600))); 
    50             return $d; 
     50            return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) ); 
    5151            break; 
    5252        case 'timestamp': 
    53             if ( $gmt ) $d = time(); 
    54             else $d = time() + (get_option('gmt_offset') * 3600); 
    55             return $d; 
     53            return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * 3600 ); 
    5654            break; 
    5755    } 
    5856} 
    5957 
    60 function date_i18n($dateformatstring, $unixtimestamp) { 
     58 
     59function date_i18n( $dateformatstring, $unixtimestamp ) { 
    6160    global $wp_locale; 
    6261    $i = $unixtimestamp; 
    63     if ( (!empty($wp_locale->month)) && (!empty($wp_locale->weekday)) ) { 
    64         $datemonth = $wp_locale->get_month(date('m', $i)); 
    65         $datemonth_abbrev = $wp_locale->get_month_abbrev($datemonth); 
    66         $dateweekday = $wp_locale->get_weekday(date('w', $i)); 
    67         $dateweekday_abbrev = $wp_locale->get_weekday_abbrev($dateweekday); 
    68         $datemeridiem = $wp_locale->get_meridiem(date('a', $i)); 
    69         $datemeridiem_capital = $wp_locale->get_meridiem(date('A', $i)); 
     62    if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) { 
     63        $datemonth = $wp_locale->get_month( date( 'm', $i ) ); 
     64        $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth ); 
     65        $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) ); 
     66        $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday ); 
     67        $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) ); 
     68        $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) ); 
    7069        $dateformatstring = ' '.$dateformatstring; 
    71         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring); 
    72         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring); 
    73         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring); 
    74         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring); 
    75         $dateformatstring = preg_replace("/([^\\\])a/", "\\1".backslashit($datemeridiem), $dateformatstring); 
    76         $dateformatstring = preg_replace("/([^\\\])A/", "\\1".backslashit($datemeridiem_capital), $dateformatstring); 
    77  
    78         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1); 
    79     } 
    80     $j = @date($dateformatstring, $i); 
     70        $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring ); 
     71        $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring ); 
     72        $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring ); 
     73        $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring ); 
     74        $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring ); 
     75        $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring ); 
     76 
     77        $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 ); 
     78    } 
     79    $j = @date( $dateformatstring, $i ); 
    8180    return $j; 
    8281} 
    8382 
    84 function number_format_i18n($number, $decimals = null) { 
     83 
     84function number_format_i18n( $number, $decimals = null ) { 
    8585    global $wp_locale; 
    8686    // let the user override the precision only 
    87     $decimals = is_null($decimals)? $wp_locale->number_format['decimals'] : intval($decimals); 
    88  
    89     return number_format($number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep']); 
    90 
    91  
    92 function size_format($bytes, $decimals = null) { 
     87    $decimals = ( is_null( $decimals ) ) ? $wp_locale->number_format['decimals'] : intval( $decimals ); 
     88 
     89    return number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] ); 
     90
     91 
     92 
     93function size_format( $bytes, $decimals = null ) { 
    9394    // technically the correct unit names for powers of 1024 are KiB, MiB etc 
    9495    // see http://en.wikipedia.org/wiki/Byte 
    9596    $quant = array( 
    96         'TB' => pow(1024, 4), 
    97         'GB' => pow(1024, 3), 
    98         'MB' => pow(1024, 2), 
    99         'kB' => pow(1024, 1), 
    100         'B'  => pow(1024, 0), 
     97        // ========================= Origin ==== 
     98        'TB' => 1099511627776,  // pow( 1024, 4) 
     99        'GB' => 1073741824,     // pow( 1024, 3) 
     100        'MB' => 1048576,        // pow( 1024, 2) 
     101        'kB' => 1024,           // pow( 1024, 1) 
     102        'B ' => 1,              // pow( 1024, 0) 
    101103    ); 
    102104 
    103     foreach ($quant as $unit => $mag) 
    104         if ( intval($bytes) >= $mag ) 
    105             return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit; 
    106 
    107  
    108 function get_weekstartend($mysqlstring, $start_of_week) { 
    109     $my = substr($mysqlstring,0,4); 
    110     $mm = substr($mysqlstring,8,2); 
    111     $md = substr($mysqlstring,5,2); 
    112     $day = mktime(0,0,0, $md, $mm, $my); 
    113     $weekday = date('w',$day); 
     105    foreach ( $quant as $unit => $mag ) 
     106        if ( intval( $bytes ) >= $mag ) 
     107            return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit; 
     108
     109 
     110 
     111function get_weekstartend( $mysqlstring, $start_of_week ) { 
     112    $my = substr( $mysqlstring, 0, 4 ); 
     113    $mm = substr( $mysqlstring, 8, 2 ); 
     114    $md = substr( $mysqlstring, 5, 2 ); 
     115    $day = mktime( 0, 0, 0, $md, $mm, $my ); 
     116    $weekday = date( 'w', $day ); 
    114117    $i = 86400; 
    115118 
    116     if ( $weekday < get_option('start_of_week') ) 
    117         $weekday = 7 - (get_option('start_of_week') - $weekday); 
    118  
    119     while ($weekday > get_option('start_of_week')) { 
    120         $weekday = date('w',$day); 
    121         if ( $weekday < get_option('start_of_week') ) 
    122             $weekday = 7 - (get_option('start_of_week') - $weekday); 
     119    if ( $weekday < get_option( 'start_of_week' ) ) 
     120        $weekday = 7 - ( get_option( 'start_of_week' ) - $weekday ); 
     121 
     122    while ( $weekday > get_option( 'start_of_week' ) ) { 
     123        $weekday = date( 'w', $day ); 
     124        if ( $weekday < get_option( 'start_of_week' ) ) 
     125            $weekday = 7 - ( get_option( 'start_of_week' ) - $weekday ); 
    123126 
    124127        $day = $day - 86400; 
     
    126129    } 
    127130    $week['start'] = $day + 86400 - $i; 
    128     // $week['end'] = $day - $i + 691199; 
    129131    $week['end'] = $week['start'] + 604799; 
    130132    return $week; 
    131133} 
    132134 
    133 function maybe_unserialize($original) { 
    134     if ( is_serialized($original) ) // don't attempt to unserialize data that wasn't serialized going in 
    135         if ( false !== $gm = @ unserialize($original) ) 
     135 
     136function maybe_unserialize( $original ) { 
     137    if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in 
     138        if ( false !== $gm = @unserialize( $original ) ) 
    136139            return $gm; 
    137140    return $original; 
    138141} 
    139142 
    140 function is_serialized($data) { 
     143 
     144function is_serialized( $data ) { 
    141145    // if it isn't a string, it isn't serialized 
    142     if ( !is_string($data) ) 
     146    if ( !is_string( $data ) ) 
    143147        return false; 
    144     $data = trim($data); 
     148    $data = trim( $data ); 
    145149    if ( 'N;' == $data ) 
    146150        return true; 
    147     if ( !preg_match('/^([adObis]):/', $data, $badions) ) 
     151    if ( !preg_match( '/^([adObis]):/', $data, $badions ) ) 
    148152        return false; 
    149     switch ( $badions[1] ) : 
    150     case 'a' : 
    151     case 'O' : 
    152     case 's' : 
    153         if ( preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data) ) 
    154             return true; 
    155         break; 
    156     case 'b' : 
    157     case 'i' : 
    158     case 'd' : 
    159         if ( preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data) ) 
    160             return true; 
    161         break; 
    162     endswitch; 
     153    switch ( $badions[1] ) { 
     154       case 'a' : 
     155       case 'O' : 
     156       case 's' : 
     157           if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) ) 
     158               return true; 
     159           break; 
     160       case 'b' : 
     161       case 'i' : 
     162       case 'd' : 
     163           if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) ) 
     164               return true; 
     165           break; 
     166    } 
    163167    return false; 
    164168} 
    165169 
    166 function is_serialized_string($data) { 
     170 
     171function is_serialized_string( $data ) { 
    167172    // if it isn't a string, it isn't a serialized string 
    168     if ( !is_string($data) ) 
     173    if ( !is_string( $data ) ) 
    169174        return false; 
    170     $data = trim($data); 
    171     if ( preg_match('/^s:[0-9]+:.*;$/s',$data) ) // this should fetch all serialized strings 
     175    $data = trim( $data ); 
     176    if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings 
    172177        return true; 
    173178    return false; 
    174179} 
    175180 
     181 
    176182/* Options functions */ 
    177183 
    178184// expects $setting to already be SQL-escaped 
    179 function get_option($setting) { 
     185function get_option( $setting ) { 
    180186    global $wpdb; 
    181187 
     
    186192 
    187193    // prevent non-existent options from triggering multiple queries 
    188     $notoptions = wp_cache_get('notoptions', 'options'); 
    189     if ( isset($notoptions[$setting]) ) 
     194    $notoptions = wp_cache_get( 'notoptions', 'options' ); 
     195    if ( isset( $notoptions[$setting] ) ) 
    190196        return false; 
    191197 
    192198    $alloptions = wp_load_alloptions(); 
    193199 
    194     if ( isset($alloptions[$setting]) ) { 
     200    if ( isset( $alloptions[$setting] ) ) { 
    195201        $value = $alloptions[$setting]; 
    196202    } else { 
    197         $value = wp_cache_get($setting, 'options'); 
     203        $value = wp_cache_get( $setting, 'options' ); 
    198204 
    199205        if ( false === $value ) { 
    200             if ( defined('WP_INSTALLING') ) 
     206            if ( defined( 'WP_INSTALLING' ) ) 
    201207                $wpdb->hide_errors(); 
    202208            // expected_slashed ($setting) 
    203             $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1"); 
    204             if ( defined('WP_INSTALLING') ) 
     209            $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1" ); 
     210            if ( defined( 'WP_INSTALLING' ) ) 
    205211                $wpdb->show_errors(); 
    206212 
    207             if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values 
     213            if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values 
    208214                $value = $row->option_value; 
    209                 wp_cache_add($setting, $value, 'options'); 
     215                wp_cache_add( $setting, $value, 'options' ); 
    210216            } else { // option does not exist, so we must cache its non-existence 
    211217                $notoptions[$setting] = true; 
    212                 wp_cache_set('notoptions', $notoptions, 'options'); 
     218                wp_cache_set( 'notoptions', $notoptions, 'options' ); 
    213219                return false; 
    214220            } 
     
    218224    // If home is not set use siteurl. 
    219225    if ( 'home' == $setting && '' == $value ) 
    220         return get_option('siteurl'); 
    221  
    222     if ( in_array($setting, array('siteurl', 'home', 'category_base', 'tag_base')) ) 
    223         $value = untrailingslashit($value); 
    224  
    225     return apply_filters( 'option_' . $setting, maybe_unserialize($value) ); 
    226 
    227  
    228 function wp_protect_special_option($option) { 
    229     $protected = array('alloptions', 'notoptions'); 
    230     if ( in_array($option, $protected) ) 
    231         die(sprintf(__('%s is a protected WP option and may not be modified'), wp_specialchars($option))); 
    232 
    233  
    234 function form_option($option) { 
    235     echo attribute_escape(get_option($option)); 
     226        return get_option( 'siteurl' ); 
     227 
     228    if ( in_array( $setting, array('siteurl', 'home', 'category_base', 'tag_base') ) ) 
     229        $value = untrailingslashit( $value ); 
     230 
     231    return apply_filters( 'option_' . $setting, maybe_unserialize( $value ) ); 
     232
     233 
     234 
     235function wp_protect_special_option( $option ) { 
     236    $protected = array( 'alloptions', 'notoptions' ); 
     237    if ( in_array( $option, $protected ) ) 
     238        die( sprintf( __( '%s is a protected WP option and may not be modified' ), wp_specialchars( $option ) ) ); 
     239
     240 
     241function form_option( $option ) { 
     242    echo attribute_escape (get_option( $option ) ); 
    236243} 
    237244 
     
    239246    global $wpdb, $wp_queries; 
    240247    $wpdb->hide_errors(); 
    241     if ( !$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) { 
    242         $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options"); 
    243     } 
     248    if ( !$options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) 
     249        $options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); 
    244250    $wpdb->show_errors(); 
    245251 
    246     foreach ($options as $option) { 
     252    foreach ( $options as $option ) { 
    247253        // "When trying to design a foolproof system, 
    248254        //  never underestimate the ingenuity of the fools :)" -- Dougal 
    249         if ( 'siteurl' == $option->option_name ) 
    250             $option->option_value = preg_replace('|/+$|', '', $option->option_value); 
    251         if ( 'home' == $option->option_name ) 
    252             $option->option_value = preg_replace('|/+$|', '', $option->option_value); 
    253         if ( 'category_base' == $option->option_name ) 
    254             $option->option_value = preg_replace('|/+$|', '', $option->option_value); 
    255         $value = maybe_unserialize($option->option_value); 
    256         $all_options->{$option->option_name} = apply_filters('pre_option_' . $option->option_name, $value); 
    257     } 
    258     return apply_filters('all_options', $all_options); 
    259 
     255        if ( in_array( $option->option_name, array( 'siteurl', 'home', 'category_base' ) ) ) 
     256            $option->option_value = untrailingslashit( $option->option_value ); 
     257        $value = maybe_unserialize( $option->option_value ); 
     258        $all_options->{$option->option_name} = apply_filters( 'pre_option_' . $option->option_name, $value ); 
     259    } 
     260    return apply_filters( 'all_options', $all_options ); 
     261
     262 
    260263 
    261264function wp_load_alloptions() { 
    262265    global $wpdb; 
    263266 
    264     $alloptions = wp_cache_get('alloptions', 'options'); 
     267    $alloptions = wp_cache_get( 'alloptions', 'options' ); 
    265268 
    266269    if ( !$alloptions ) { 
    267270        $wpdb->hide_errors(); 
    268         if ( !$alloptions_db = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) 
    269             $alloptions_db = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options"); 
     271        if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) 
     272            $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); 
    270273        $wpdb->show_errors(); 
    271274        $alloptions = array(); 
    272275        foreach ( (array) $alloptions_db as $o ) 
    273276            $alloptions[$o->option_name] = $o->option_value; 
    274         wp_cache_add('alloptions', $alloptions, 'options'); 
     277        wp_cache_add( 'alloptions', $alloptions, 'options' ); 
    275278    } 
    276279    return $alloptions; 
    277280} 
    278281 
     282 
    279283// expects $option_name to NOT be SQL-escaped 
    280 function update_option($option_name, $newvalue) { 
     284function update_option( $option_name, $newvalue ) { 
    281285    global $wpdb; 
    282286 
    283     wp_protect_special_option($option_name); 
    284  
    285     $safe_option_name = $wpdb->escape($option_name); 
    286     $newvalue = sanitize_option($option_name, $newvalue); 
    287  
    288     if ( is_string($newvalue) ) 
    289         $newvalue = trim($newvalue); 
     287    wp_protect_special_option( $option_name ); 
     288 
     289    $safe_option_name = $wpdb->escape( $option_name ); 
     290    $newvalue = sanitize_option( $option_name, $newvalue ); 
     291 
     292    // Likely legacy -- can we drop this? 
     293    if ( is_string( $newvalue ) ) 
     294        $newvalue = trim( $newvalue ); 
    290295 
    291296    // If the new and old values are the same, no need to update. 
    292     $oldvalue = get_option($safe_option_name); 
    293     if ( $newvalue === $oldvalue ) { 
     297    $oldvalue = get_option( $safe_option_name ); 
     298    if ( $newvalue === $oldvalue ) 
    294299        return false; 
    295     } 
    296300 
    297301    if ( false === $oldvalue ) { 
    298         add_option($option_name, $newvalue); 
     302        add_option( $option_name, $newvalue ); 
    299303        return true; 
    300304    } 
    301305 
    302     $notoptions = wp_cache_get('notoptions', 'options'); 
    303     if ( is_array($notoptions) && isset($notoptions[$option_name]) ) { 
    304         unset($notoptions[$option_name]); 
    305         wp_cache_set('notoptions', $notoptions, 'options'); 
     306    $notoptions = wp_cache_get( 'notoptions', 'options' ); 
     307    if ( is_array( $notoptions ) && isset( $notoptions[$option_name] ) ) { 
     308        unset( $notoptions[$option_name] ); 
     309        wp_cache_set( 'notoptions', $notoptions, 'options' ); 
    306310    } 
    307311 
    308312    $_newvalue = $newvalue; 
    309     $newvalue = maybe_serialize($newvalue); 
     313    $newvalue = maybe_serialize( $newvalue ); 
    310314 
    311315    $alloptions = wp_load_alloptions(); 
    312     if ( isset($alloptions[$option_name]) ) { 
     316    if ( isset( $alloptions[$option_name] ) ) { 
    313317        $alloptions[$option_name] = $newvalue; 
    314         wp_cache_set('alloptions', $alloptions, 'options'); 
     318        wp_cache_set( 'alloptions', $alloptions, 'options' ); 
    315319    } else { 
    316         wp_cache_set($option_name, $newvalue, 'options'); 
    317     } 
    318  
    319     $wpdb->query($wpdb->prepare("UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", $newvalue, $option_name)); 
     320        wp_cache_set( $option_name, $newvalue, 'options' ); 
     321    } 
     322 
     323    $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", $newvalue, $option_name ) ); 
    320324    if ( $wpdb->rows_affected == 1 ) { 
    321         do_action("update_option_{$option_name}", $oldvalue, $_newvalue); 
     325        do_action( "update_option_{$option_name}", $oldvalue, $_newvalue ); 
    322326        return true; 
    323327    } 
    324328    return false; 
    325329} 
     330 
    326331 
    327332// thx Alex Stapleton, http://alex.vort-x.net/blog/ 
    328333// expects $name to NOT be SQL-escaped 
    329 function add_option($name, $value = '', $deprecated = '', $autoload = 'yes') { 
     334function add_option( $name, $value = '', $deprecated = '', $autoload = 'yes' ) { 
    330335    global $wpdb; 
    331336 
    332     wp_protect_special_option($name); 
    333     $safe_name = $wpdb->escape($name); 
     337    wp_protect_special_option( $name ); 
     338    $safe_name = $wpdb->escape( $name ); 
    334339 
    335340    // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 
    336     $notoptions = wp_cache_get('notoptions', 'options'); 
    337     if ( !is_array($notoptions) || !isset($notoptions[$name]) ) 
    338         if ( false !== get_option($safe_name) ) 
     341    $notoptions = wp_cache_get( 'notoptions', 'options' ); 
     342    if ( !is_array( $notoptions ) || !isset( $notoptions[$name] ) ) 
     343        if ( false !== get_option( $safe_name ) ) 
    339344            return; 
    340345 
    341     $value = maybe_serialize($value); 
     346    $value = maybe_serialize( $value ); 
    342347    $autoload = ( 'no' === $autoload ) ? 'no' : 'yes'; 
    343348 
     
    345350        $alloptions = wp_load_alloptions(); 
    346351        $alloptions[$name] = $value; 
    347         wp_cache_set('alloptions', $alloptions, 'options'); 
     352        wp_cache_set( 'alloptions', $alloptions, 'options' ); 
    348353    } else { 
    349         wp_cache_set($name, $value, 'options'); 
     354        wp_cache_set( $name, $value, 'options' ); 
    350355    } 
    351356 
    352357    // This option exists now 
    353     $notoptions = wp_cache_get('notoptions', 'options'); // yes, again... we need it to be fresh 
    354     if ( is_array($notoptions) && isset($notoptions[$name]) ) { 
    355         unset($notoptions[$name]); 
    356         wp_cache_set('notoptions', $notoptions, 'options'); 
    357     } 
    358  
    359     $wpdb->query($wpdb->prepare("INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %s, %s)", $name, $value, $autoload)); 
     358    $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh 
     359    if ( is_array( $notoptions ) && isset( $notoptions[$name] ) ) { 
     360        unset( $notoptions[$name] ); 
     361        wp_cache_set( 'notoptions', $notoptions, 'options' ); 
     362    } 
     363 
     364    $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %s, %s)", $name, $value, $autoload ) ); 
    360365 
    361366    return; 
    362367} 
    363368 
    364 function delete_option($name) { 
     369 
     370function delete_option( $name ) { 
    365371    global $wpdb; 
    366372 
    367     wp_protect_special_option($name); 
     373    wp_protect_special_option( $name ); 
    368374 
    369375    // Get the ID, if no ID then return 
    370376    // expected_slashed ($name) 
    371     $option = $wpdb->get_row("SELECT option_id, autoload FROM $wpdb->options WHERE option_name = '$name'"); 
    372     if ( !$option->option_id ) return false; 
     377    $option = $wpdb->get_row( "SELECT option_id, autoload FROM $wpdb->options WHERE option_name = '$name'" ); 
     378    if ( !$option->option_id ) 
     379        return false; 
    373380    // expected_slashed ($name) 
    374     $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'"); 
     381    $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$name'" ); 
    375382    if ( 'yes' == $option->autoload ) { 
    376383        $alloptions = wp_load_alloptions(); 
    377         if ( isset($alloptions[$name]) ) { 
    378             unset($alloptions[$name]); 
    379             wp_cache_set('alloptions', $alloptions, 'options'); 
     384        if ( isset( $alloptions[$name] ) ) { 
     385            unset( $alloptions[$name] ); 
     386            wp_cache_set( 'alloptions', $alloptions, 'options' ); 
    380387        } 
    381388    } else { 
    382         wp_cache_delete($name, 'options'); 
     389        wp_cache_delete( $name, 'options' ); 
    383390    } 
    384391    return true; 
    385392} 
    386393 
    387 function maybe_serialize($data) { 
    388     if ( is_string($data) ) 
    389         $data = trim($data); 
    390     elseif ( is_array($data) || is_object($data) ) 
    391         return serialize($data); 
    392     if ( is_serialized($data) ) 
    393         return serialize($data); 
     394 
     395function maybe_serialize( $data ) { 
     396    if ( is_string( $data ) ) 
     397        $data = trim( $data ); 
     398    elseif ( is_array( $data ) || is_object( $data ) ) 
     399        return serialize( $data ); 
     400    if ( is_serialized( $data ) ) 
     401        return serialize( $data ); 
    394402    return $data; 
    395403} 
    396404 
     405 
    397406function gzip_compression() { 
    398     if ( !get_option( 'gzipcompression' ) ) { 
     407    if ( !get_option( 'gzipcompression' ) || ini_get( 'zlib.output_compression' ) == 'On' || ini_get( 'zlib.output_compression_level' ) > 0  || ini_get( 'output_handler' ) == 'ob_gzhandler' || !extension_loaded( 'zlib' ) ) 
    399408        return false; 
    400     } 
    401  
    402     if ( ( ini_get( 'zlib.output_compression' ) == 'On' || ini_get( 'zlib.output_compression_level' ) > 0 ) || ini_get( 'output_handler' ) == 'ob_gzhandler' ) { 
    403         return false; 
    404     } 
    405  
    406     if ( extension_loaded( 'zlib' ) ) { 
    407         ob_start( 'ob_gzhandler' ); 
    408     } 
    409 
    410  
    411 function make_url_footnote($content) { 
    412     preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches); 
     409    ob_start( 'ob_gzhandler' ); 
     410
     411 
     412 
     413function make_url_footnote( $content ) { 
     414    preg_match_all( '/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches ); 
    413415    $j = 0; 
    414     for ($i=0; $i<count($matches[0]); $i++) { 
    415         $links_summary = (!$j) ? "\n" : $links_summary; 
     416    for ( $i=0; $i<count($matches[0]); $i++ ) { 
     417        $links_summary = ( !$j ) ? "\n" : $links_summary; 
    416418        $j++; 
    417419        $link_match = $matches[0][$i]; 
     
    419421        $link_url = $matches[2][$i]; 
    420422        $link_text = $matches[4][$i]; 
    421         $content = str_replace($link_match, $link_text.' '.$link_number, $content); 
    422         $link_url = ((strtolower(substr($link_url,0,7)) != 'http://') && (strtolower(substr($link_url,0,8)) != 'https://')) ? get_option('home') . $link_url : $link_url; 
    423         $links_summary .= "\n".$link_number.' '.$link_url; 
    424     } 
    425     $content = strip_tags($content); 
     423        $content = str_replace( $link_match, $link_text . ' ' . $link_number, $content ); 
     424        $link_url = ( ( strtolower( substr( $link_url, 0, 7 ) ) != 'http://' ) && ( strtolower( substr( $link_url, 0, 8 ) ) != 'https://' ) ) ? get_option( 'home' ) . $link_url : $link_url; 
     425        $links_summary .= "\n" . $link_number . ' ' . $link_url; 
     426    } 
     427    $content = strip_tags( $content ); 
    426428    $content .= $links_summary; 
    427429    return $content; 
     
    429431 
    430432 
    431 function xmlrpc_getposttitle($content) { 
     433function xmlrpc_getposttitle( $content ) { 
    432434    global $post_default_title; 
    433     if ( preg_match('/<title>(.+?)<\/title>/is', $content, $matchtitle) ) { 
     435    if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) { 
    434436        $post_title = $matchtitle[0]; 
    435         $post_title = preg_replace('/<title>/si', '', $post_title); 
    436         $post_title = preg_replace('/<\/title>/si', '', $post_title); 
     437        $post_title = preg_replace( '/<title>/si', '', $post_title ); 
     438        $post_title = preg_replace( '/<\/title>/si', '', $post_title ); 
    437439    } else { 
    438440        $post_title = $post_default_title; 
     
    441443} 
    442444 
    443 function xmlrpc_getpostcategory($content) { 
     445 
     446function xmlrpc_getpostcategory( $content ) { 
    444447    global $post_default_category; 
    445     if ( preg_match('/<category>(.+?)<\/category>/is', $content, $matchcat) ) { 
    446         $post_category = trim($matchcat[1], ','); 
    447         $post_category = explode(',', $post_category); 
     448    if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) { 
     449        $post_category = trim( $matchcat[1], ',' ); 
     450        $post_category = explode( ',', $post_category ); 
    448451    } else { 
    449452        $post_category = $post_default_category; 
     
    452455} 
    453456 
    454 function xmlrpc_removepostdata($content) { 
    455     $content = preg_replace('/<title>(.+?)<\/title>/si', '', $content); 
    456     $content = preg_replace('/<category>(.+?)<\/category>/si', '', $content); 
    457     $content = trim($content); 
     457 
     458function xmlrpc_removepostdata( $content ) { 
     459    $content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content ); 
     460    $content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content ); 
     461    $content = trim( $content ); 
    458462    return $content; 
    459463} 
    460464 
    461 function debug_fopen($filename, $mode) { 
     465 
     466function debug_fopen( $filename, $mode ) { 
    462467    global $debug; 
    463     if ( $debug == 1 ) { 
    464         $fp = fopen($filename, $mode); 
     468    if ( 1 == $debug ) { 
     469        $fp = fopen( $filename, $mode ); 
    465470        return $fp; 
    466471    } else { 
     
    469474} 
    470475 
    471 function debug_fwrite($fp, $string) { 
     476 
     477function debug_fwrite( $fp, $string ) { 
    472478    global $debug; 
    473     if ( $debug == 1 ) { 
    474         fwrite($fp, $string); 
    475    
    476 
    477  
    478 function debug_fclose($fp) { 
     479    if ( 1 == $debug ) 
     480        fwrite( $fp, $string ); 
     481
     482 
     483 
     484function debug_fclose( $fp ) { 
    479485    global $debug; 
    480     if ( $debug == 1 ) { 
    481         fclose($fp); 
    482     } 
     486    if ( 1 == $debug ) 
     487        fclose( $fp ); 
    483488} 
    484489 
    485490function do_enclose( $content, $post_ID ) { 
    486491    global $wp_version, $wpdb; 
    487     include_once (ABSPATH . WPINC . '/class-IXR.php'); 
    488  
    489     $log = debug_fopen(ABSPATH . 'enclosures.log', 'a'); 
     492    include_once( ABSPATH . WPINC . '/class-IXR.php' ); 
     493 
     494    $log = debug_fopen( ABSPATH . 'enclosures.log', 'a' ); 
    490495    $post_links = array(); 
    491     debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n"); 
     496    debug_fwrite( $log, 'BEGIN ' . date( 'YmdHis', time() ) . "\n" ); 
    492497 
    493498    $pung = get_enclosed( $post_ID ); 
     
    498503    $any = $ltrs . $gunk . $punc; 
    499504 
    500     preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp); 
    501  
    502     debug_fwrite($log, 'Post contents:'); 
    503     debug_fwrite($log, $content."\n"); 
    504  
    505     foreach($post_links_temp[0] as $link_test) : 
    506         if ( !in_array($link_test, $pung) ) : // If we haven't pung it already 
    507             $test = parse_url($link_test); 
    508             if ( isset($test['query']) ) 
     505    preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp ); 
     506 
     507    debug_fwrite( $log, 'Post contents:' ); 
     508    debug_fwrite( $log, $content . "\n" ); 
     509 
     510    foreach ( $post_links_temp[0] as $link_test ) { 
     511        if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already 
     512            $test = parse_url( $link_test ); 
     513            if ( isset( $test['query'] ) ) 
    509514                $post_links[] = $link_test; 
    510             elseif (($test['path'] != '/') && ($test['path'] != '')
     515            elseif ( $test['path'] != '/' && $test['path'] != ''
    511516                $post_links[] = $link_test; 
    512         endif; 
    513     endforeach; 
    514  
    515     foreach ($post_links as $url) : 
    516         if ( $url != '' && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $url.'%')) ) { 
     517        } 
     518    } 
     519 
     520    foreach ( $post_links as $url ) { 
     521        if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $url . '%' ) ) ) { 
    517522            if ( $headers = wp_get_http_headers( $url) ) { 
    518523                $len = (int) $headers['content-length']; 
     
    521526                if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) { 
    522527                    $meta_value = "$url\n$len\n$type\n"; 
    523                     $wpdb->query($wpdb->prepare("INSERT INTO `$wpdb->postmeta` ( `post_id` , `meta_key` , `meta_value` ) 
    524                     VALUES ( %d, 'enclosure' , %s)", $post_ID, $meta_value)); 
     528                    $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->postmeta` ( `post_id` , `meta_key` , `meta_value` ) 
     529                    VALUES ( %d, 'enclosure' , %s)", $post_ID, $meta_value ) ); 
    525530                } 
    526531            } 
    527532        } 
    528     endforeach; 
    529 
     533    } 
     534
     535 
    530536 
    531537function wp_get_http_headers( $url, $red = 1 ) { 
     
    537543 
    538544    $parts = parse_url( $url ); 
    539     $file = $parts['path'] . ($parts['query'] ? '?'.$parts['query'] : ''); 
     545    $file = $parts['path'] . ( ( $parts['query'] ) ? '?' . $parts['query'] : '' ); 
    540546    $host = $parts['host']; 
    541547    if ( !isset( $parts['port'] ) ) 
     
    544550    $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\nUser-Agent: WordPress/" . $wp_version . "\r\n\r\n"; 
    545551 
    546     $fp = @fsockopen($host, $parts['port'], $err_num, $err_msg, 3); 
     552    $fp = @fsockopen( $host, $parts['port'], $err_num, $err_msg, 3 ); 
    547553    if ( !$fp ) 
    548554        return false; 
     
    553559        $response .= fgets( $fp, 2048 ); 
    554560    fclose( $fp ); 
    555     preg_match_all('/(.*?): (.*)\r/', $response, $matches); 
    556     $count = count($matches[1]); 
    557     for ( $i = 0; $i < $count; $i++) { 
    558         $key = strtolower($matches[1][$i]); 
     561    preg_match_all( '/(.*?): (.*)\r/', $response, $matches ); 
     562    $count = count( $matches[1] ); 
     563    for ( $i = 0; $i < $count; $i++ ) { 
     564        $key = strtolower( $matches[1][$i] ); 
    559565        $headers["$key"] = $matches[2][$i]; 
    560566    } 
    561567 
    562     preg_match('/.*([0-9]{3}).*/', $response, $return); 
     568    preg_match( '/.*([0-9]{3}).*/', $response, $return ); 
    563569    $headers['response'] = $return[1]; // HTTP response code eg 204, 200, 404 
    564570 
    565571        $code = $headers['response']; 
    566         if ( ('302' == $code || '301' == $code) && isset($headers['location']) ) 
     572        if ( ( '302' == $code || '301' == $code ) && isset( $headers['location'] ) ) 
    567573                return wp_get_http_headers( $headers['location'], ++$red ); 
    568574 
    569575    return $headers; 
    570576} 
     577 
    571578 
    572579function is_new_day() { 
    573580    global $day, $previousday; 
    574     if ( $day != $previousday ) { 
    575         return(1); 
    576     } else { 
    577         return(0); 
    578     } 
    579 
    580  
    581 function build_query($data) { 
    582     return _http_build_query($data, NULL, '&', '', false); 
    583 
     581    if ( $day != $previousday ) 
     582        return 1; 
     583    else 
     584        return 0; 
     585
     586 
     587 
     588function build_query( $data ) { 
     589    return _http_build_query( $data, NULL, '&', '', false ); 
     590
     591 
    584592 
    585593/* 
     
    595603function add_query_arg() { 
    596604    $ret = ''; 
    597     if ( is_array(func_get_arg(0)) ) { 
    598         if ( @func_num_args() < 2 || false === @func_get_arg(1) ) 
     605    if ( is_array( func_get_arg(0) ) ) { 
     606        if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) ) 
    599607            $uri = $_SERVER['REQUEST_URI']; 
    600608        else 
    601             $uri = @func_get_arg(1); 
     609            $uri = @func_get_arg( 1 ); 
    602610    } else { 
    603         if ( @func_num_args() < 3 || false === @func_get_arg(2) ) 
     611        if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) ) 
    604612            $uri = $_SERVER['REQUEST_URI']; 
    605613        else 
    606             $uri = @func_get_arg(2); 
    607     } 
    608  
    609     if ( $frag = strstr($uri, '#') ) 
    610         $uri = substr($uri, 0, -strlen($frag)); 
     614            $uri = @func_get_arg( 2 ); 
     615    } 
     616 
     617    if ( $frag = strstr( $uri, '#' ) ) 
     618        $uri = substr( $uri, 0, -strlen( $frag ) ); 
    611619    else 
    612620        $frag = ''; 
    613621 
    614     if ( preg_match('|^https?://|i', $uri, $matches) ) { 
     622    if ( preg_match( '|^https?://|i', $uri, $matches ) ) { 
    615623        $protocol = $matches[0]; 
    616         $uri = substr($uri, strlen($protocol)); 
     624        $uri = substr( $uri, strlen( $protocol ) ); 
    617625    } else { 
    618626        $protocol = ''; 
    619627    } 
    620628 
    621     if (strpos($uri, '?') !== false) { 
    622         $parts = explode('?', $uri, 2); 
    623         if ( 1 == count($parts) ) { 
     629    if ( strpos( $uri, '?' ) !== false ) { 
     630        $parts = explode( '?', $uri, 2 ); 
     631        if ( 1 == count( $parts ) ) { 
    624632            $base = '?'; 
    625633            $query = $parts[0]; 
     
    628636            $query = $parts[1]; 
    629637        } 
    630     } elseif (!empty($protocol) || strpos($uri, '=') === false ) { 
     638    } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) { 
    631639        $base = $uri . '?'; 
    632640        $query = ''; 
     
    636644    } 
    637645 
    638     wp_parse_str($query, $qs); 
    639     $qs = urlencode_deep($qs); // this re-URL-encodes things that were already in the query string 
    640     if ( is_array(func_get_arg(0)) ) { 
    641         $kayvees = func_get_arg(0); 
    642         $qs = array_merge($qs, $kayvees); 
     646    wp_parse_str( $query, $qs ); 
     647    $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string 
     648    if ( is_array( func_get_arg( 0 ) ) ) { 
     649        $kayvees = func_get_arg( 0 ); 
     650        $qs = array_merge( $qs, $kayvees ); 
    643651    } else { 
    644         $qs[func_get_arg(0)] = func_get_arg(1); 
     652        $qs[func_get_arg( 0 )] = func_get_arg( 1 ); 
    645653    } 
    646654 
    647655    foreach ( $qs as $k => $v ) { 
    648656        if ( $v === false ) 
    649             unset($qs[$k]); 
    650     } 
    651  
    652     $ret = build_query($qs); 
    653     $ret = trim($ret, '?'); 
    654     $ret = preg_replace('#=(&|$)#', '$1', $ret); 
     657            unset( $qs[$k] ); 
     658    } 
     659 
     660    $ret = build_query( $qs ); 
     661    $ret = trim( $ret, '?' ); 
     662    $ret = preg_replace( '#=(&|$)#', '$1', $ret ); 
    655663    $ret = $protocol . $base . $ret . $frag; 
    656     $ret = rtrim($ret, '?'); 
     664    $ret = rtrim( $ret, '?' ); 
    657665    return $ret; 
    658666} 
     667 
    659668 
    660669/* 
     
    668677*/ 
    669678 
    670 function remove_query_arg($key, $query=FALSE) { 
    671     if ( is_array($key) ) { // removing multiple keys 
     679function remove_query_arg( $key, $query=FALSE ) { 
     680    if ( is_array( $key ) ) { // removing multiple keys 
    672681        foreach ( (array) $key as $k ) 
    673             $query = add_query_arg($k, FALSE, $query); 
     682            $query = add_query_arg( $k, FALSE, $query ); 
    674683        return $query; 
    675684    } 
    676     return add_query_arg($key, FALSE, $query); 
    677 
    678  
    679 function add_magic_quotes($array) { 
     685    return add_query_arg( $key, FALSE, $query ); 
     686
     687 
     688 
     689function add_magic_quotes( $array ) { 
    680690    global $wpdb; 
    681691 
    682     foreach ($array as $k => $v