Make WordPress Core

Ticket #8964: wptrac_8964_patch4.diff

File wptrac_8964_patch4.diff, 8.8 KB (added by strider72, 15 years ago)

Adds hooks to both plugins and themes, new function get_file_data()

  • wp-includes/theme.php

     
    162162 * @return array Theme data.
    163163 */
    164164function get_theme_data( $theme_file ) {
     165        $default_headers = array(
     166                'Name' => 'Theme Name',
     167                'URI' => 'Plugin URI',
     168                'Description' => 'Description',
     169                'Author' => 'Author',
     170                'AuthorURI' => 'Author URI',
     171                'Version' => 'Version',
     172                'Template' => 'Template',
     173                'Status' => 'Status',
     174                'Tags' => 'Tags'
     175                );
     176
    165177        $themes_allowed_tags = array(
    166178                'a' => array(
    167179                        'href' => array(),'title' => array()
     
    177189                'strong' => array()
    178190        );
    179191
    180         $theme_data = implode( '', file( $theme_file ) );
    181         $theme_data = str_replace ( '\r', '\n', $theme_data );
    182         if ( preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name ) )
    183                 $name = $theme = wp_kses( _cleanup_header_comment($theme_name[1]), $themes_allowed_tags );
    184         else
    185                 $name = $theme = '';
     192        $theme_data = get_file_data( $theme_file, $default_headers, 'theme' );
    186193
    187         if ( preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri ) )
    188                 $theme_uri = esc_url( _cleanup_header_comment($theme_uri[1]) );
    189         else
    190                 $theme_uri = '';
     194        $theme_data['Name'] = $title = wp_kses( $theme_data['Name'], $themes_allowed_tags );
    191195
    192         if ( preg_match( '|Description:(.*)$|mi', $theme_data, $description ) )
    193                 $description = wptexturize( wp_kses( _cleanup_header_comment($description[1]), $themes_allowed_tags ) );
    194         else
    195                 $description = '';
     196        $theme_data['URI'] = esc_url( $theme_data['URI'] );
    196197
    197         if ( preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri ) )
    198                 $author_uri = esc_url( _cleanup_header_comment($author_uri[1]) );
    199         else
    200                 $author_uri = '';
     198        $theme_data['Description'] = wptexturize( wp_kses( $theme_data['Description'], $themes_allowed_tags ) );
    201199
    202         if ( preg_match( '|Template:(.*)$|mi', $theme_data, $template ) )
    203                 $template = wp_kses( _cleanup_header_comment($template[1]), $themes_allowed_tags );
    204         else
    205                 $template = '';
     200        $theme_data['AuthorURI'] = esc_url( $theme_data['AuthorURI'] );
    206201
    207         if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
    208                 $version = wp_kses( _cleanup_header_comment($version[1]), $themes_allowed_tags );
    209         else
    210                 $version = '';
     202        $theme_data['Template'] = wp_kses( $theme_data['Template'], $themes_allowed_tags );
    211203
    212         if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
    213                 $status = wp_kses( _cleanup_header_comment($status[1]), $themes_allowed_tags );
     204        $theme_data['Version'] = wp_kses( $theme_data['Version'], $themes_allowed_tags );
     205
     206        if ( $theme_data['Status'] == '' )
     207                $theme_data['Status'] = 'publish';
    214208        else
    215                 $status = 'publish';
     209                $theme_data['Status'] = wp_kses( $theme_data['Status'], $themes_allowed_tags );
    216210
    217         if ( preg_match('|Tags:(.*)|i', $theme_data, $tags) )
    218                 $tags = array_map( 'trim', explode( ',', wp_kses( _cleanup_header_comment($tags[1]), array() ) ) );
     211        if ( $theme_data['Tags'] == '' )
     212                $theme_data['Tags'] = array();
    219213        else
    220                 $tags = array();
     214                $theme_data['Tags'] = array_map( 'trim', explode( ',', wp_kses( $theme_data['Tags'], array() ) ) );
    221215
    222         if ( preg_match( '|Author:(.*)$|mi', $theme_data, $author_name ) ) {
    223                 if ( empty( $author_uri ) ) {
    224                         $author = wp_kses( _cleanup_header_comment($author_name[1]), $themes_allowed_tags );
     216        if ( $theme_data['Author'] == '' ) {
     217                $theme_data['Author'] = __('Anonymous');
     218        } else {
     219                if ( empty( $theme_data['AuthorURI'] ) ) {
     220                        $theme_data['Author'] = wp_kses( $theme_data['Author'], $themes_allowed_tags );
    225221                } else {
    226                         $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 ) );
     222                        $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 ) );
    227223                }
    228         } else {
    229                 $author = __('Anonymous');
    230224        }
     225        unset( $theme_data['AuthorURI'] );
    231226
    232         return array( 'Name' => $name, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Status' => $status, 'Tags' => $tags );
     227        return $theme_data;
    233228}
    234229
    235230/**
  • wp-includes/functions.php

     
    33953395
    33963396        update_option('wp_trash_meta', $trash_meta);
    33973397}
     3398
     3399/**
     3400 * Parse the file contents to retrieve its metadata.
     3401 *
     3402 * Searches for metadata for a file, such as a plugin or theme.  Each piece of
     3403 * metadata must be on its own line. For a field spanning multple lines, it
     3404 * must not have any newlines or only parts of it will be displayed.
     3405 *
     3406 * Some users have issues with opening large files and manipulating the contents
     3407 * for want is usually the first 1kiB or 2kiB. This function stops pulling in
     3408 * the file contents when it has all of the required data.
     3409 *
     3410 * The first 8kiB of the file will be pulled in and if the file data is not
     3411 * within that first 8kiB, then the author should correct their plugin file
     3412 * and move the data headers to the top.
     3413 *
     3414 * The file is assumed to have permissions to allow for scripts to read
     3415 * the file. This is not checked however and the file is only opened for
     3416 * reading.
     3417 *
     3418 * @since 2.9.0
     3419 *
     3420 * @param string $file Path to the file
     3421 * @param bool $markup If the returned data should have HTML markup applied
     3422 * @param bool $translate If the returned data should be translated
     3423 * @return An array of headers
     3424 */
     3425function get_file_data( $file, $default_headers, $context = '' ) {
     3426        // We don't need to write to the file, so just open for reading.
     3427        $fp = fopen( $file, 'r' );
     3428
     3429        // Pull only the first 8kiB of the file in.
     3430        $file_data = fread( $fp, 8192 );
     3431
     3432        // PHP will close file handle, but we are good citizens.
     3433        fclose( $fp );
     3434
     3435        if( $context != '' ) {
     3436                $extra_headers = apply_filters( "extra_$context".'_headers', array() );
     3437
     3438                $extra_headers = array_flip( $extra_headers );
     3439                foreach( $extra_headers as $key=>$value ) {
     3440                        $extra_headers[$key] = $key;
     3441                }
     3442                $all_headers = array_merge($extra_headers, $default_headers);
     3443        } else {
     3444                $all_headers = $default_headers;
     3445        }
     3446
     3447       
     3448        foreach ( $all_headers as $field => $regex ) {
     3449                preg_match( '/' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field});
     3450                if ( !empty( ${$field} ) )
     3451                        ${$field} = _cleanup_header_comment( ${$field}[1] );
     3452                else
     3453                        ${$field} = '';
     3454        }
     3455
     3456        $file_data = compact( array_keys( $all_headers ) );
     3457       
     3458        return $file_data;
     3459}
    33983460?>
     3461 No newline at end of file
  • wp-admin/includes/plugin.php

     
    6666 * @return array See above for description.
    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');
    7169
    72         // Pull only the first 8kiB of the file in.
    73         $plugin_data = fread( $fp, 8192 );
     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                );
    7480
    75         // PHP will close file handle, but we are good citizens.
    76         fclose($fp);
     81        $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
    7782
    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                                 );
    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;
    10387}