Make WordPress Core

Changeset 12044


Ignore:
Timestamp:
10/15/2009 09:07:00 PM (15 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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/plugin.php

    r12042 r12044  
    6767 */
    6868function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
    69     // We don't need to write to the file, so just open for reading.
    70     $fp = fopen($plugin_file, 'r');
    71 
    72     // Pull only the first 8kiB of the file in.
    73     $plugin_data = fread( $fp, 8192 );
    74 
    75     // PHP will close file handle, but we are good citizens.
    76     fclose($fp);
    77 
    78     preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name );
    79     preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri );
    80     preg_match( '|Version:(.*)|i', $plugin_data, $version );
    81     preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
    82     preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
    83     preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
    84     preg_match( '|Text Domain:(.*)$|mi', $plugin_data, $text_domain );
    85     preg_match( '|Domain Path:(.*)$|mi', $plugin_data, $domain_path );
    86 
    87     foreach ( array( 'name', 'uri', 'version', 'description', 'author_name', 'author_uri', 'text_domain', 'domain_path' ) as $field ) {
    88         if ( !empty( ${$field} ) )
    89             ${$field} = _cleanup_header_comment(${$field}[1]);
    90         else
    91             ${$field} = '';
    92     }
    93 
    94     $plugin_data = array(
    95                 'Name' => $name, 'Title' => $name, 'PluginURI' => $uri, 'Description' => $description,
    96                 'Author' => $author_name, 'AuthorURI' => $author_uri, 'Version' => $version,
    97                 'TextDomain' => $text_domain, 'DomainPath' => $domain_path
    98                 );
     69
     70    $default_headers = array(
     71        'Name' => 'Plugin Name',
     72        'PluginURI' => 'Plugin URI',
     73        'Version' => 'Version',
     74        'Description' => 'Description',
     75        'Author' => 'Author',
     76        'AuthorURI' => 'Author URI',
     77        'TextDomain' => 'Text Domain',
     78        'DomainPath' => 'Domain Path'
     79        );
     80
     81    $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
     82
    9983    if ( $markup || $translate )
    100         $plugin_data = _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup, $translate);
     84        $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
    10185
    10286    return $plugin_data;
  • 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?>
  • trunk/wp-includes/theme.php

    r12043 r12044  
    171171 */
    172172function get_theme_data( $theme_file ) {
     173    $default_headers = array(
     174        'Name' => 'Theme Name',
     175        'URI' => 'Theme URI',
     176        'Description' => 'Description',
     177        'Author' => 'Author',
     178        'AuthorURI' => 'Author URI',
     179        'Version' => 'Version',
     180        'Template' => 'Template',
     181        'Status' => 'Status',
     182        'Tags' => 'Tags'
     183        );
     184
    173185    $themes_allowed_tags = array(
    174186        'a' => array(
     
    186198    );
    187199
    188     $theme_data = implode( '', file( $theme_file ) );
    189     $theme_data = str_replace ( '\r', '\n', $theme_data );
    190     if ( preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name ) )
    191         $name = $theme = wp_kses( _cleanup_header_comment($theme_name[1]), $themes_allowed_tags );
     200    $theme_data = get_file_data( $theme_file, $default_headers, 'theme' );
     201
     202    $theme_data['Name'] = $theme_data['Title'] = wp_kses( $theme_data['Name'], $themes_allowed_tags );
     203
     204    $theme_data['URI'] = esc_url( $theme_data['URI'] );
     205
     206    $theme_data['Description'] = wptexturize( wp_kses( $theme_data['Description'], $themes_allowed_tags ) );
     207
     208    $theme_data['AuthorURI'] = esc_url( $theme_data['AuthorURI'] );
     209
     210    $theme_data['Template'] = wp_kses( $theme_data['Template'], $themes_allowed_tags );
     211
     212    $theme_data['Version'] = wp_kses( $theme_data['Version'], $themes_allowed_tags );
     213
     214    if ( $theme_data['Status'] == '' )
     215        $theme_data['Status'] = 'publish';
    192216    else
    193         $name = $theme = '';
    194 
    195     if ( preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri ) )
    196         $theme_uri = esc_url( _cleanup_header_comment($theme_uri[1]) );
     217        $theme_data['Status'] = wp_kses( $theme_data['Status'], $themes_allowed_tags );
     218
     219    if ( $theme_data['Tags'] == '' )
     220        $theme_data['Tags'] = array();
    197221    else
    198         $theme_uri = '';
    199 
    200     if ( preg_match( '|Description:(.*)$|mi', $theme_data, $description ) )
    201         $description = wptexturize( wp_kses( _cleanup_header_comment($description[1]), $themes_allowed_tags ) );
    202     else
    203         $description = '';
    204 
    205     if ( preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri ) )
    206         $author_uri = esc_url( _cleanup_header_comment($author_uri[1]) );
    207     else
    208         $author_uri = '';
    209 
    210     if ( preg_match( '|Template:(.*)$|mi', $theme_data, $template ) )
    211         $template = wp_kses( _cleanup_header_comment($template[1]), $themes_allowed_tags );
    212     else
    213         $template = '';
    214 
    215     if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
    216         $version = wp_kses( _cleanup_header_comment($version[1]), $themes_allowed_tags );
    217     else
    218         $version = '';
    219 
    220     if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
    221         $status = wp_kses( _cleanup_header_comment($status[1]), $themes_allowed_tags );
    222     else
    223         $status = 'publish';
    224 
    225     if ( preg_match('|Tags:(.*)|i', $theme_data, $tags) )
    226         $tags = array_map( 'trim', explode( ',', wp_kses( _cleanup_header_comment($tags[1]), array() ) ) );
    227     else
    228         $tags = array();
    229 
    230     if ( preg_match( '|Author:(.*)$|mi', $theme_data, $author_name ) ) {
    231         if ( empty( $author_uri ) ) {
    232             $author = wp_kses( _cleanup_header_comment($author_name[1]), $themes_allowed_tags );
     222        $theme_data['Tags'] = array_map( 'trim', explode( ',', wp_kses( $theme_data['Tags'], array() ) ) );
     223
     224    if ( $theme_data['Author'] == '' ) {
     225        $theme_data['Author'] = __('Anonymous');
     226    } else {
     227        if ( empty( $theme_data['AuthorURI'] ) ) {
     228            $theme_data['Author'] = wp_kses( $theme_data['Author'], $themes_allowed_tags );
    233229        } else {
    234             $author = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $author_uri, __( 'Visit author homepage' ), wp_kses( _cleanup_header_comment($author_name[1]), $themes_allowed_tags ) );
     230            $theme_data['Author'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $theme_data['AuthorURI'], __( 'Visit author homepage' ), wp_kses( $theme_data['Author'], $themes_allowed_tags ) );
    235231        }
    236     } else {
    237         $author = __('Anonymous');
    238232    }
    239233
    240     return array( 'Name' => $name, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Status' => $status, 'Tags' => $tags );
     234    return $theme_data;
    241235}
    242236
Note: See TracChangeset for help on using the changeset viewer.