WordPress.org

Make WordPress Core

Ticket #8964: wptrac_8964_patch5.diff

File wptrac_8964_patch5.diff, 8.9 KB (added by strider72, 9 years ago)

bugfix -- patch 4 didn't properly handle Author header in all cases

  • 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                        $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 ) );
    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

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