Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r15995 r17435  
    2828        return false;
    2929
    30     if ( 'G' == $dateformatstring ) {
     30    if ( 'G' == $dateformatstring )
    3131        return strtotime( $m . ' +0000' );
    32     }
    3332
    3433    $i = strtotime( $m );
     
    3736        return $i;
    3837
    39     if ( $translate)
    40         return date_i18n( $dateformatstring, $i );
     38    if ( $translate )
     39        return date_i18n( $dateformatstring, $i );
    4140    else
    42         return date( $dateformatstring, $i );
     41        return date( $dateformatstring, $i );
    4342}
    4443
     
    120119        $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
    121120    }
     121    $timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
     122    $timezone_formats_re = implode( '|', $timezone_formats );
     123    if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) && wp_timezone_supported() ) {
     124        $timezone_string = get_option( 'timezone_string' );
     125        if ( $timezone_string ) {
     126            $timezone_object = timezone_open( $timezone_string );
     127            $date_object = date_create( null, $timezone_object );
     128            foreach( $timezone_formats as $timezone_format ) {
     129                if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
     130                    $formatted = date_format( $date_object, $timezone_format );
     131                    $dateformatstring = ' '.$dateformatstring;
     132                    $dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
     133                    $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
     134                }
     135            }
     136        }
     137    }
    122138    $j = @$datefunc( $dateformatstring, $i );
    123139    // allow plugins to redo this entirely for languages with untypical grammars
     
    231247function is_serialized( $data ) {
    232248    // if it isn't a string, it isn't serialized
    233     if ( !is_string( $data ) )
     249    if ( ! is_string( $data ) )
    234250        return false;
    235251    $data = trim( $data );
    236     if ( 'N;' == $data )
     252    if ( 'N;' == $data )
    237253        return true;
    238     if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
     254    $length = strlen( $data );
     255    if ( $length < 4 )
    239256        return false;
    240     switch ( $badions[1] ) {
     257    if ( ':' !== $data[1] )
     258        return false;
     259    $lastc = $data[$length-1];
     260    if ( ';' !== $lastc && '}' !== $lastc )
     261        return false;
     262    $token = $data[0];
     263    switch ( $token ) {
     264        case 's' :
     265            if ( '"' !== $data[$length-2] )
     266                return false;
    241267        case 'a' :
    242268        case 'O' :
    243         case 's' :
    244             if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
    245                 return true;
    246             break;
     269            return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
    247270        case 'b' :
    248271        case 'i' :
    249272        case 'd' :
    250             if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
    251                 return true;
    252             break;
     273            return (bool) preg_match( "/^{$token}:[0-9.E-]+;\$/", $data );
    253274    }
    254275    return false;
     
    413434        $wpdb->suppress_errors($suppress);
    414435        $alloptions = array();
    415         foreach ( (array) $alloptions_db as $o )
     436        foreach ( (array) $alloptions_db as $o ) {
    416437            $alloptions[$o->option_name] = $o->option_value;
    417             if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
    418                 wp_cache_add( 'alloptions', $alloptions, 'options' );
     438        }
     439        if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
     440            wp_cache_add( 'alloptions', $alloptions, 'options' );
    419441    }
    420442
     
    440462        $site_id = $wpdb->siteid;
    441463
    442     $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'dashboard_blog', 'can_compress_scripts', 'global_terms_enabled' );
     464    $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled' );
    443465
    444466    $core_options_in = "'" . implode("', '", $core_options) . "'";
     
    547569 * @subpackage Option
    548570 * @since 1.0.0
    549  * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton
    550571 *
    551572 * @uses do_action() Calls 'add_option' hook before adding the option.
     
    671692    global $_wp_using_ext_object_cache;
    672693
    673     do_action( 'delete_transient_' . $transient, $transient );
     694    do_action( 'delete_transient_' . $transient, $transient );
    674695
    675696    if ( $_wp_using_ext_object_cache ) {
     
    759780    global $_wp_using_ext_object_cache;
    760781
    761     $value = apply_filters( 'pre_set_transient_' . $transient, $value );
     782    $value = apply_filters( 'pre_set_transient_' . $transient, $value );
    762783
    763784    if ( $_wp_using_ext_object_cache ) {
     
    11641185function do_enclose( $content, $post_ID ) {
    11651186    global $wpdb;
     1187
     1188    //TODO: Tidy this ghetto code up and make the debug code optional
    11661189    include_once( ABSPATH . WPINC . '/class-IXR.php' );
    11671190
     
    11841207    foreach ( $pung as $link_test ) {
    11851208        if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
    1186             $mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $link_test . '%') );
     1209            $mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape( $link_test ) . '%') );
    11871210            do_action( 'delete_postmeta', $mid );
    11881211            $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $mid ) ) );
     
    11981221            if ( isset( $test['query'] ) )
    11991222                $post_links[] = $link_test;
    1200             elseif ( $test['path'] != '/' && $test['path'] != '' )
     1223            elseif ( isset($test['path']) && ( $test['path'] != '/' ) &&  ($test['path'] != '' ) )
    12011224                $post_links[] = $link_test;
    12021225        }
     
    12041227
    12051228    foreach ( (array) $post_links as $url ) {
    1206         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 . '%' ) ) ) {
     1229        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, like_escape( $url ) . '%' ) ) ) {
    12071230
    12081231            if ( $headers = wp_get_http_headers( $url) ) {
     
    13221345 */
    13231346function is_new_day() {
    1324     global $day, $previousday;
    1325     if ( $day != $previousday )
     1347    global $currentday, $previousday;
     1348    if ( $currentday != $previousday )
    13261349        return 1;
    13271350    else
     
    16201643 * by different browsers
    16211644 *
    1622  * @since 2.8
     1645 * @since 2.8.0
    16231646 *
    16241647 * @uses apply_filters()
     
    16341657
    16351658    if ( function_exists('apply_filters') ) {
    1636         $headers = apply_filters('nocache_headers', $headers);
     1659        $headers = (array) apply_filters('nocache_headers', $headers);
    16371660    }
    16381661    return $headers;
     
    16501673function nocache_headers() {
    16511674    $headers = wp_get_nocache_headers();
    1652     foreach( (array) $headers as $name => $field_value )
     1675    foreach( $headers as $name => $field_value )
    16531676        @header("{$name}: {$field_value}");
    16541677}
     
    20592082        return true;
    20602083
    2061     if ( strlen($path) == 0 || $path{0} == '.' )
     2084    if ( strlen($path) == 0 || $path[0] == '.' )
    20622085        return false;
    20632086
     
    21912214 * unique.
    21922215 *
    2193  * The callback must accept two parameters, the first one is the directory and
    2194  * the second is the filename. The callback must be a function.
    2195  *
    2196  * @since 2.5
     2216 * The callback is passed three parameters, the first one is the directory, the
     2217 * second is the filename, and the third is the extension.
     2218 *
     2219 * @since 2.5.0
    21972220 *
    21982221 * @param string $dir
    21992222 * @param string $filename
    2200  * @param string $unique_filename_callback Function name, must be a function.
     2223 * @param mixed $unique_filename_callback Callback.
    22012224 * @return string New filename, if given wasn't unique.
    22022225 */
     
    22142237        $name = '';
    22152238
    2216     // Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
     2239    // Increment the file number until we have a unique file to save in $dir. Use callback if supplied.
    22172240    if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {
    2218         $filename = $unique_filename_callback( $dir, $name );
     2241        $filename = call_user_func( $unique_filename_callback, $dir, $name, $ext );
    22192242    } else {
    22202243        $number = '';
     
    25742597        $trans['activate']['plugin']   = array( __( 'Your attempt to activate this plugin: &#8220;%s&#8221; has failed.' ), 'use_id' );
    25752598        $trans['deactivate']['plugin'] = array( __( 'Your attempt to deactivate this plugin: &#8220;%s&#8221; has failed.' ), 'use_id' );
    2576         $trans['upgrade']['plugin']    = array( __( 'Your attempt to upgrade this plugin: &#8220;%s&#8221; has failed.' ), 'use_id' );
     2599        $trans['upgrade']['plugin']    = array( __( 'Your attempt to update this plugin: &#8220;%s&#8221; has failed.' ), 'use_id' );
    25772600
    25782601        $trans['add']['post']          = array( __( 'Your attempt to add this post has failed.' ), false );
     
    26582681 */
    26592682function wp_die( $message, $title = '', $args = array() ) {
     2683    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
     2684        die('-1');
     2685
    26602686    if ( function_exists( 'apply_filters' ) ) {
    26612687        $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler');
    2662     }else {
     2688    } else {
    26632689        $function = '_default_wp_die_handler';
    26642690    }
     
    27402766<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    27412767<!-- Ticket #11289, IE bug fix: always pad the error page with enough characters such that it is greater than 512 bytes, even after gzip compression abcdefghijklmnopqrstuvwxyz1234567890aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz11223344556677889900abacbcbdcdcededfefegfgfhghgihihjijikjkjlklkmlmlnmnmononpopoqpqprqrqsrsrtstsubcbcdcdedefefgfabcadefbghicjkldmnoepqrfstugvwxhyz1i234j567k890laabmbccnddeoeffpgghqhiirjjksklltmmnunoovppqwqrrxsstytuuzvvw0wxx1yyz2z113223434455666777889890091abc2def3ghi4jkl5mno6pqr7stu8vwx9yz11aab2bcc3dd4ee5ff6gg7hh8ii9j0jk1kl2lmm3nnoo4p5pq6qrr7ss8tt9uuvv0wwx1x2yyzz13aba4cbcb5dcdc6dedfef8egf9gfh0ghg1ihi2hji3jik4jkj5lkl6kml7mln8mnm9ono -->
    2742 <html xmlns="http://www.w3.org/1999/xhtml" <?php if ( function_exists( 'language_attributes' ) ) language_attributes(); ?>>
     2768<html xmlns="http://www.w3.org/1999/xhtml" <?php if ( function_exists( 'language_attributes' ) && function_exists( 'is_rtl' ) ) language_attributes(); else echo "dir='$text_direction'"; ?>>
    27432769<head>
    27442770    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     
    29793005
    29803006/**
     3007 * Extract a slice of an array, given a list of keys
     3008 *
     3009 * @since 3.1.0
     3010 *
     3011 * @param array $array The original array
     3012 * @param array $keys The list of keys
     3013 * @return array The array slice
     3014 */
     3015function wp_array_slice_assoc( $array, $keys ) {
     3016    $slice = array();
     3017    foreach ( $keys as $key )
     3018        if ( isset( $array[ $key ] ) )
     3019            $slice[ $key ] = $array[ $key ];
     3020
     3021    return $slice;
     3022}
     3023
     3024/**
    29813025 * Filters a list of objects, based on a set of key => value arguments
    29823026 *
     
    29913035 */
    29923036function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
    2993     if ( !is_array($list) )
     3037    if ( ! is_array( $list ) )
    29943038        return array();
    29953039
    2996     if ( empty($args) )
    2997         $args = array();
    2998 
    2999     if ( empty($args) && !$field )
    3000         return $list;   // nothing to do
    3001 
    3002     $count = count($args);
    3003 
     3040    $list = wp_list_filter( $list, $args, $operator );
     3041
     3042    if ( $field )
     3043        $list = wp_list_pluck( $list, $field );
     3044
     3045    return $list;
     3046}
     3047
     3048/**
     3049 * Filters a list of objects, based on a set of key => value arguments
     3050 *
     3051 * @since 3.1.0
     3052 *
     3053 * @param array $list An array of objects to filter
     3054 * @param array $args An array of key => value arguments to match against each object
     3055 * @param string $operator The logical operation to perform:
     3056 *    'AND' means all elements from the array must match;
     3057 *    'OR' means only one element needs to match;
     3058 *    'NOT' means no elements may match.
     3059 *   The default is 'AND'.
     3060 * @return array
     3061 */
     3062function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
     3063    if ( ! is_array( $list ) )
     3064        return array();
     3065
     3066    if ( empty( $args ) )
     3067        return $list;
     3068
     3069    $operator = strtoupper( $operator );
     3070    $count = count( $args );
    30043071    $filtered = array();
    30053072
    30063073    foreach ( $list as $key => $obj ) {
    3007         $matched = count(array_intersect_assoc(get_object_vars($obj), $args));
    3008         if ( ('and' == $operator && $matched == $count) || ('or' == $operator && $matched <= $count) ) {
    3009             if ( $field )
    3010                 $filtered[] = $obj->$field;
    3011             else
    3012                 $filtered[$key] = $obj;
     3074        $matched = count( array_intersect_assoc( (array) $obj, $args ) );
     3075        if ( ( 'AND' == $operator && $matched == $count )
     3076          || ( 'OR' == $operator && $matched <= $count )
     3077          || ( 'NOT' == $operator && 0 == $matched ) ) {
     3078            $filtered[$key] = $obj;
    30133079        }
    30143080    }
    30153081
    30163082    return $filtered;
     3083}
     3084
     3085/**
     3086 * Pluck a certain field out of each object in a list
     3087 *
     3088 * @since 3.1.0
     3089 *
     3090 * @param array $list A list of objects or arrays
     3091 * @param int|string $field A field from the object to place instead of the entire object
     3092 * @return array
     3093 */
     3094function wp_list_pluck( $list, $field ) {
     3095    foreach ( $list as $key => $value ) {
     3096        $value = (array) $value;
     3097        $list[ $key ] = $value[ $field ];
     3098    }
     3099
     3100    return $list;
    30173101}
    30183102
     
    30703154    for ($i=0; $i<$levels; $i++)
    30713155        ob_end_flush();
    3072 }
    3073 
    3074 /**
    3075  * Load the correct database class file.
    3076  *
    3077  * This function is used to load the database class file either at runtime or by
    3078  * wp-admin/setup-config.php We must globalise $wpdb to ensure that it is
    3079  * defined globally by the inline code in wp-db.php.
    3080  *
    3081  * @since 2.5.0
    3082  * @global $wpdb WordPress Database Object
    3083  */
    3084 function require_wp_db() {
    3085     global $wpdb;
    3086     if ( file_exists( WP_CONTENT_DIR . '/db.php' ) )
    3087         require_once( WP_CONTENT_DIR . '/db.php' );
    3088     else
    3089         require_once( ABSPATH . WPINC . '/wp-db.php' );
    30903156}
    30913157
     
    32103276 * function.
    32113277 *
    3212  * The current behavior is to trigger an user error if WP_DEBUG is true.
    3213  *
    3214  * This function is to be used in every function in depreceated.php
     3278 * The current behavior is to trigger a user error if WP_DEBUG is true.
     3279 *
     3280 * This function is to be used in every function that is deprecated.
    32153281 *
    32163282 * @package WordPress
     
    32483314 * file.
    32493315 *
    3250  * The current behavior is to trigger an user error if WP_DEBUG is true.
    3251  *
    3252  * This function is to be used in every file that is depreceated
     3316 * The current behavior is to trigger a user error if WP_DEBUG is true.
     3317 *
     3318 * This function is to be used in every file that is deprecated.
    32533319 *
    32543320 * @package WordPress
     
    32963362 * argument.
    32973363 *
    3298  * The current behavior is to trigger an user error if WP_DEBUG is true.
     3364 * The current behavior is to trigger a user error if WP_DEBUG is true.
    32993365 *
    33003366 * @package WordPress
     
    33223388        else
    33233389            trigger_error( sprintf( __('%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.'), $function, $version ) );
     3390    }
     3391}
     3392
     3393/**
     3394 * Marks something as being incorrectly called.
     3395 *
     3396 * There is a hook doing_it_wrong_run that will be called that can be used
     3397 * to get the backtrace up to what file and function called the deprecated
     3398 * function.
     3399 *
     3400 * The current behavior is to trigger a user error if WP_DEBUG is true.
     3401 *
     3402 * @package WordPress
     3403 * @subpackage Debug
     3404 * @since 3.1.0
     3405 * @access private
     3406 *
     3407 * @uses do_action() Calls 'doing_it_wrong_run' and passes the function arguments.
     3408 * @uses apply_filters() Calls 'doing_it_wrong_trigger_error' and expects boolean value of true to do
     3409 *   trigger or false to not trigger error.
     3410 *
     3411 * @param string $function The function that was called.
     3412 * @param string $message A message explaining what has been done incorrectly.
     3413 * @param string $version The version of WordPress where the message was added.
     3414 */
     3415function _doing_it_wrong( $function, $message, $version ) {
     3416
     3417    do_action( 'doing_it_wrong_run', $function, $message, $version );
     3418
     3419    // Allow plugin to filter the output error trigger
     3420    if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
     3421        $version = is_null( $version ) ? '' : sprintf( __( '(This message was added in version %s.)' ), $version );
     3422        trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
    33243423    }
    33253424}
     
    33683467
    33693468/**
     3469 * Check if IIS 7 supports pretty permalinks
     3470 *
     3471 * @since 2.8.0
     3472 *
     3473 * @return bool
     3474 */
     3475function iis7_supports_permalinks() {
     3476    global $is_iis7;
     3477
     3478    $supports_permalinks = false;
     3479    if ( $is_iis7 ) {
     3480        /* First we check if the DOMDocument class exists. If it does not exist,
     3481         * which is the case for PHP 4.X, then we cannot easily update the xml configuration file,
     3482         * hence we just bail out and tell user that pretty permalinks cannot be used.
     3483         * This is not a big issue because PHP 4.X is going to be depricated and for IIS it
     3484         * is recommended to use PHP 5.X NTS.
     3485         * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
     3486         * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
     3487         * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
     3488         * via ISAPI then pretty permalinks will not work.
     3489         */
     3490        $supports_permalinks = class_exists('DOMDocument') && isset($_SERVER['IIS_UrlRewriteModule']) && ( php_sapi_name() == 'cgi-fcgi' );
     3491    }
     3492
     3493    return apply_filters('iis7_supports_permalinks', $supports_permalinks);
     3494}
     3495
     3496/**
    33703497 * File validates against allowed set of defined rules.
    33713498 *
     
    34733600        $url = preg_replace('|/wp-admin/.*|i', '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    34743601    }
    3475     return $url;
     3602    return rtrim($url, '/');
    34763603}
    34773604
     
    37863913    global $_wp_using_ext_object_cache;
    37873914
    3788     $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
     3915    $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
    37893916
    37903917    if ( $_wp_using_ext_object_cache ) {
     
    41374264
    41384265/**
    4139  * Parse the file contents to retrieve its metadata.
    4140  *
    4141  * Searches for metadata for a file, such as a plugin or theme.  Each piece of
    4142  * metadata must be on its own line. For a field spanning multple lines, it
    4143  * must not have any newlines or only parts of it will be displayed.
    4144  *
    4145  * Some users have issues with opening large files and manipulating the contents
    4146  * for want is usually the first 1kiB or 2kiB. This function stops pulling in
    4147  * the file contents when it has all of the required data.
    4148  *
    4149  * The first 8kiB of the file will be pulled in and if the file data is not
    4150  * within that first 8kiB, then the author should correct their plugin file
    4151  * and move the data headers to the top.
    4152  *
    4153  * The file is assumed to have permissions to allow for scripts to read
    4154  * the file. This is not checked however and the file is only opened for
    4155  * reading.
     4266 * Retrieve metadata from a file.
     4267 *
     4268 * Searches for metadata in the first 8kiB of a file, such as a plugin or theme.
     4269 * Each piece of metadata must be on its own line. Fields can not span multple
     4270 * lines, the value will get cut at the end of the first line.
     4271 *
     4272 * If the file data is not within that first 8kiB, then the author should correct
     4273 * their plugin file and move the data headers to the top.
     4274 *
     4275 * @see http://codex.wordpress.org/File_Header
    41564276 *
    41574277 * @since 2.9.0
    4158  *
    41594278 * @param string $file Path to the file
    4160  * @param bool $markup If the returned data should have HTML markup applied
    4161  * @param string $context If specified adds filter hook "extra_<$context>_headers"
     4279 * @param array $default_headers List of headers, in the format array('HeaderKey' => 'Header Name')
     4280 * @param string $context If specified adds filter hook "extra_{$context}_headers"
    41624281 */
    41634282function get_file_data( $file, $default_headers, $context = '' ) {
     
    41724291
    41734292    if ( $context != '' ) {
    4174         $extra_headers = apply_filters( "extra_$context".'_headers', array() );
     4293        $extra_headers = apply_filters( "extra_{$context}_headers", array() );
    41754294
    41764295        $extra_headers = array_flip( $extra_headers );
     
    41784297            $extra_headers[$key] = $key;
    41794298        }
    4180         $all_headers = array_merge($extra_headers, $default_headers);
     4299        $all_headers = array_merge( $extra_headers, (array) $default_headers );
    41814300    } else {
    41824301        $all_headers = $default_headers;
    41834302    }
    41844303
    4185 
    41864304    foreach ( $all_headers as $field => $regex ) {
    4187         preg_match( '/' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field});
     4305        preg_match( '/^[ \t\/*#]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field});
    41884306        if ( !empty( ${$field} ) )
    41894307            ${$field} = _cleanup_header_comment( ${$field}[1] );
     
    41964314    return $file_data;
    41974315}
    4198 /*
     4316
     4317/**
    41994318 * Used internally to tidy up the search terms
    42004319 *
     
    42984417}
    42994418
     4419/**
     4420 * Finds hierarchy loops using a callback function that maps object IDs to parent IDs.
     4421 *
     4422 * @since 3.1.0
     4423 * @access private
     4424 *
     4425 * @param callback $callback function that accepts ( ID, $callback_args ) and outputs parent_ID
     4426 * @param int $start The ID to start the loop check at
     4427 * @param int $start_parent the parent_ID of $start to use instead of calling $callback( $start ). Use null to always use $callback
     4428 * @param array $callback_args optional additional arguments to send to $callback
     4429 * @return array IDs of all members of loop
     4430 */
     4431function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
     4432    $override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
     4433
     4434    if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
     4435        return array();
     4436
     4437    return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
     4438}
     4439
     4440/**
     4441 * Uses the "The Tortoise and the Hare" algorithm to detect loops.
     4442 *
     4443 * For every step of the algorithm, the hare takes two steps and the tortoise one.
     4444 * If the hare ever laps the tortoise, there must be a loop.
     4445 *
     4446 * @since 3.1.0
     4447 * @access private
     4448 *
     4449 * @param callback $callback function that accupts ( ID, callback_arg, ... ) and outputs parent_ID
     4450 * @param int $start The ID to start the loop check at
     4451 * @param array $override an array of ( ID => parent_ID, ... ) to use instead of $callback
     4452 * @param array $callback_args optional additional arguments to send to $callback
     4453 * @param bool $_return_loop Return loop members or just detect presence of loop?
     4454 *             Only set to true if you already know the given $start is part of a loop
     4455 *             (otherwise the returned array might include branches)
     4456 * @return mixed scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if $_return_loop
     4457 */
     4458function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
     4459    $tortoise = $hare = $evanescent_hare = $start;
     4460    $return = array();
     4461
     4462    // Set evanescent_hare to one past hare
     4463    // Increment hare two steps
     4464    while (
     4465        $tortoise
     4466    &&
     4467        ( $evanescent_hare = isset( $override[$hare] ) ? $override[$hare] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
     4468    &&
     4469        ( $hare = isset( $override[$evanescent_hare] ) ? $override[$evanescent_hare] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
     4470    ) {
     4471        if ( $_return_loop )
     4472            $return[$tortoise] = $return[$evanescent_hare] = $return[$hare] = true;
     4473
     4474        // tortoise got lapped - must be a loop
     4475        if ( $tortoise == $evanescent_hare || $tortoise == $hare )
     4476            return $_return_loop ? $return : $tortoise;
     4477
     4478        // Increment tortoise by one step
     4479        $tortoise = isset( $override[$tortoise] ) ? $override[$tortoise] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
     4480    }
     4481
     4482    return false;
     4483}
     4484
    43004485?>
Note: See TracChangeset for help on using the changeset viewer.