WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/15/2009 09:07:00 PM (12 years ago)
Author:
westi
Message:

Allow for plugins to enhance the number of metadata fields captured from plugin and theme headers. See #8964 props strider72.

File:
1 edited

Legend:

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

    r12042 r12044  
    34193419    }
    34203420}
     3421
     3422/**
     3423 * Parse the file contents to retrieve its metadata.
     3424 *
     3425 * Searches for metadata for a file, such as a plugin or theme.  Each piece of
     3426 * metadata must be on its own line. For a field spanning multple lines, it
     3427 * must not have any newlines or only parts of it will be displayed.
     3428 *
     3429 * Some users have issues with opening large files and manipulating the contents
     3430 * for want is usually the first 1kiB or 2kiB. This function stops pulling in
     3431 * the file contents when it has all of the required data.
     3432 *
     3433 * The first 8kiB of the file will be pulled in and if the file data is not
     3434 * within that first 8kiB, then the author should correct their plugin file
     3435 * and move the data headers to the top.
     3436 *
     3437 * The file is assumed to have permissions to allow for scripts to read
     3438 * the file. This is not checked however and the file is only opened for
     3439 * reading.
     3440 *
     3441 * @since 2.9.0
     3442 *
     3443 * @param string $file Path to the file
     3444 * @param bool $markup If the returned data should have HTML markup applied
     3445 * @param string $context If specified adds filter hook "extra_<$context>_headers"
     3446 */
     3447function get_file_data( $file, $default_headers, $context = '' ) {
     3448    // We don't need to write to the file, so just open for reading.
     3449    $fp = fopen( $file, 'r' );
     3450
     3451    // Pull only the first 8kiB of the file in.
     3452    $file_data = fread( $fp, 8192 );
     3453
     3454    // PHP will close file handle, but we are good citizens.
     3455    fclose( $fp );
     3456
     3457    if( $context != '' ) {
     3458        $extra_headers = apply_filters( "extra_$context".'_headers', array() );
     3459
     3460        $extra_headers = array_flip( $extra_headers );
     3461        foreach( $extra_headers as $key=>$value ) {
     3462            $extra_headers[$key] = $key;
     3463        }
     3464        $all_headers = array_merge($extra_headers, $default_headers);
     3465    } else {
     3466        $all_headers = $default_headers;
     3467    }
     3468
     3469   
     3470    foreach ( $all_headers as $field => $regex ) {
     3471        preg_match( '/' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field});
     3472        if ( !empty( ${$field} ) )
     3473            ${$field} = _cleanup_header_comment( ${$field}[1] );
     3474        else
     3475            ${$field} = '';
     3476    }
     3477
     3478    $file_data = compact( array_keys( $all_headers ) );
     3479   
     3480    return $file_data;
     3481}
    34213482?>
Note: See TracChangeset for help on using the changeset viewer.