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

git-svn-id: http://svn.automattic.com/wordpress/trunk@12044 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
westi 2009-10-15 21:07:00 +00:00
parent 995ff3cd3c
commit c26e33b939
3 changed files with 109 additions and 70 deletions

View File

@ -66,38 +66,22 @@
* @return array See above for description.
*/
function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
// We don't need to write to the file, so just open for reading.
$fp = fopen($plugin_file, 'r');
// Pull only the first 8kiB of the file in.
$plugin_data = fread( $fp, 8192 );
// PHP will close file handle, but we are good citizens.
fclose($fp);
preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name );
preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri );
preg_match( '|Version:(.*)|i', $plugin_data, $version );
preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
preg_match( '|Text Domain:(.*)$|mi', $plugin_data, $text_domain );
preg_match( '|Domain Path:(.*)$|mi', $plugin_data, $domain_path );
foreach ( array( 'name', 'uri', 'version', 'description', 'author_name', 'author_uri', 'text_domain', 'domain_path' ) as $field ) {
if ( !empty( ${$field} ) )
${$field} = _cleanup_header_comment(${$field}[1]);
else
${$field} = '';
}
$plugin_data = array(
'Name' => $name, 'Title' => $name, 'PluginURI' => $uri, 'Description' => $description,
'Author' => $author_name, 'AuthorURI' => $author_uri, 'Version' => $version,
'TextDomain' => $text_domain, 'DomainPath' => $domain_path
$default_headers = array(
'Name' => 'Plugin Name',
'PluginURI' => 'Plugin URI',
'Version' => 'Version',
'Description' => 'Description',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path'
);
$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
if ( $markup || $translate )
$plugin_data = _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup, $translate);
$plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
return $plugin_data;
}

View File

@ -3418,4 +3418,65 @@ function wp_scheduled_delete() {
wp_delete_comment($comment['comment_id']);
}
}
/**
* Parse the file contents to retrieve its metadata.
*
* Searches for metadata for a file, such as a plugin or theme. Each piece of
* metadata must be on its own line. For a field spanning multple lines, it
* must not have any newlines or only parts of it will be displayed.
*
* Some users have issues with opening large files and manipulating the contents
* for want is usually the first 1kiB or 2kiB. This function stops pulling in
* the file contents when it has all of the required data.
*
* The first 8kiB of the file will be pulled in and if the file data is not
* within that first 8kiB, then the author should correct their plugin file
* and move the data headers to the top.
*
* The file is assumed to have permissions to allow for scripts to read
* the file. This is not checked however and the file is only opened for
* reading.
*
* @since 2.9.0
*
* @param string $file Path to the file
* @param bool $markup If the returned data should have HTML markup applied
* @param string $context If specified adds filter hook "extra_<$context>_headers"
*/
function get_file_data( $file, $default_headers, $context = '' ) {
// We don't need to write to the file, so just open for reading.
$fp = fopen( $file, 'r' );
// Pull only the first 8kiB of the file in.
$file_data = fread( $fp, 8192 );
// PHP will close file handle, but we are good citizens.
fclose( $fp );
if( $context != '' ) {
$extra_headers = apply_filters( "extra_$context".'_headers', array() );
$extra_headers = array_flip( $extra_headers );
foreach( $extra_headers as $key=>$value ) {
$extra_headers[$key] = $key;
}
$all_headers = array_merge($extra_headers, $default_headers);
} else {
$all_headers = $default_headers;
}
foreach ( $all_headers as $field => $regex ) {
preg_match( '/' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field});
if ( !empty( ${$field} ) )
${$field} = _cleanup_header_comment( ${$field}[1] );
else
${$field} = '';
}
$file_data = compact( array_keys( $all_headers ) );
return $file_data;
}
?>

View File

@ -170,6 +170,18 @@ function get_template_directory_uri() {
* @return array Theme data.
*/
function get_theme_data( $theme_file ) {
$default_headers = array(
'Name' => 'Theme Name',
'URI' => 'Theme URI',
'Description' => 'Description',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'Version' => 'Version',
'Template' => 'Template',
'Status' => 'Status',
'Tags' => 'Tags'
);
$themes_allowed_tags = array(
'a' => array(
'href' => array(),'title' => array()
@ -185,59 +197,41 @@ function get_theme_data( $theme_file ) {
'strong' => array()
);
$theme_data = implode( '', file( $theme_file ) );
$theme_data = str_replace ( '\r', '\n', $theme_data );
if ( preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name ) )
$name = $theme = wp_kses( _cleanup_header_comment($theme_name[1]), $themes_allowed_tags );
else
$name = $theme = '';
$theme_data = get_file_data( $theme_file, $default_headers, 'theme' );
if ( preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri ) )
$theme_uri = esc_url( _cleanup_header_comment($theme_uri[1]) );
else
$theme_uri = '';
$theme_data['Name'] = $theme_data['Title'] = wp_kses( $theme_data['Name'], $themes_allowed_tags );
if ( preg_match( '|Description:(.*)$|mi', $theme_data, $description ) )
$description = wptexturize( wp_kses( _cleanup_header_comment($description[1]), $themes_allowed_tags ) );
else
$description = '';
$theme_data['URI'] = esc_url( $theme_data['URI'] );
if ( preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri ) )
$author_uri = esc_url( _cleanup_header_comment($author_uri[1]) );
else
$author_uri = '';
$theme_data['Description'] = wptexturize( wp_kses( $theme_data['Description'], $themes_allowed_tags ) );
if ( preg_match( '|Template:(.*)$|mi', $theme_data, $template ) )
$template = wp_kses( _cleanup_header_comment($template[1]), $themes_allowed_tags );
else
$template = '';
$theme_data['AuthorURI'] = esc_url( $theme_data['AuthorURI'] );
if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
$version = wp_kses( _cleanup_header_comment($version[1]), $themes_allowed_tags );
else
$version = '';
$theme_data['Template'] = wp_kses( $theme_data['Template'], $themes_allowed_tags );
if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
$status = wp_kses( _cleanup_header_comment($status[1]), $themes_allowed_tags );
else
$status = 'publish';
$theme_data['Version'] = wp_kses( $theme_data['Version'], $themes_allowed_tags );
if ( preg_match('|Tags:(.*)|i', $theme_data, $tags) )
$tags = array_map( 'trim', explode( ',', wp_kses( _cleanup_header_comment($tags[1]), array() ) ) );
if ( $theme_data['Status'] == '' )
$theme_data['Status'] = 'publish';
else
$tags = array();
$theme_data['Status'] = wp_kses( $theme_data['Status'], $themes_allowed_tags );
if ( preg_match( '|Author:(.*)$|mi', $theme_data, $author_name ) ) {
if ( empty( $author_uri ) ) {
$author = wp_kses( _cleanup_header_comment($author_name[1]), $themes_allowed_tags );
if ( $theme_data['Tags'] == '' )
$theme_data['Tags'] = array();
else
$theme_data['Tags'] = array_map( 'trim', explode( ',', wp_kses( $theme_data['Tags'], array() ) ) );
if ( $theme_data['Author'] == '' ) {
$theme_data['Author'] = __('Anonymous');
} else {
$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 ) );
if ( empty( $theme_data['AuthorURI'] ) ) {
$theme_data['Author'] = wp_kses( $theme_data['Author'], $themes_allowed_tags );
} else {
$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 ) );
}
} else {
$author = __('Anonymous');
}
return array( 'Name' => $name, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Status' => $status, 'Tags' => $tags );
return $theme_data;
}
/**