mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-23 01:27:36 +01:00
Performance improvments to get_plugin_data() from santosj. see #7372
git-svn-id: http://svn.automattic.com/wordpress/trunk@8402 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
a0b5e9133d
commit
0ece55eb7d
@ -7,80 +7,12 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* plugin_get_contents() - Retrieve enough of the plugin file to get plugin data.
|
||||
*
|
||||
* 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 plugin contents when it has all of the required
|
||||
* plugin data.
|
||||
*
|
||||
* It also adds a little bit of padding for fudging the version info
|
||||
* and to make sure that we grab absolutely everything, in case of a
|
||||
* long description.
|
||||
*
|
||||
* The plugin 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.
|
||||
*
|
||||
* @link http://trac.wordpress.org/ticket/5651 Purpose of function.
|
||||
* @since 2.7.0
|
||||
* @uses plugin_has_required_fields() Checks for all of the required plugin
|
||||
* data fields.
|
||||
*
|
||||
* @param string $plugin_file Path to plugin file to open
|
||||
* @return string Plugin file contents retrieved
|
||||
*/
|
||||
function plugin_get_contents($plugin_file) {
|
||||
|
||||
// We don't need to write to the file, so just open for reading.
|
||||
$fp = fopen($plugin_file, 'r');
|
||||
|
||||
// Store the contents of the plugin file here.
|
||||
$contents = '';
|
||||
|
||||
// Keep reading the contents of the file until End of File is
|
||||
// reached, or we grabbed all of the required plugin data.
|
||||
while( !feof($fp) && !plugin_has_required_fields($contents) )
|
||||
$contents .= fread( $fp, 1024 );
|
||||
|
||||
// Make sure that any padding is adding for long descriptions
|
||||
// and grabbing any optional plugin data, not checked for.
|
||||
if( !feof($fp) )
|
||||
$contents .= fread( $fp, 512 );
|
||||
|
||||
// PHP will close file handle, but we are good citizens
|
||||
fclose($fp);
|
||||
return $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* plugin_has_required_fields() - Checks plugin contents for required plugin data
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @usedby plugin_get_contents()
|
||||
*
|
||||
* @param string $plugin_contents Plugin file contents
|
||||
* @return bool Whether contents has all plugin data.
|
||||
*/
|
||||
function plugin_has_required_fields($plugin_contents) {
|
||||
$hasName = stripos($plugin_contents, 'plugin name:');
|
||||
$hasPluginURI = stripos($plugin_contents, 'plugin uri:');
|
||||
$hasDescription = stripos($plugin_contents, 'description:');
|
||||
$hasAuthor = stripos($plugin_contents, 'author:');
|
||||
$hasAuthorURI = stripos($plugin_contents, 'author uri:');
|
||||
|
||||
if( false !== $hasName && false !== $hasPluginURI && false !== $hasDescription &&
|
||||
false !== $hasAuthor && false !== $hasAuthorURI)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* get_plugin_data() - Parse the plugin contents to retrieve plugin's metadata
|
||||
* Parse the plugin contents to retrieve plugin's metadata.
|
||||
*
|
||||
* The metadata of the plugin's data searches for the following in the plugin's
|
||||
* header.
|
||||
* header. All plugin data must be on its own line. For plugin description, it
|
||||
* must not have any newlines or only parts of the description will be displayed
|
||||
* and the same goes for the plugin data. The below is formatted for printing.
|
||||
*
|
||||
* <code>
|
||||
* /*
|
||||
@ -92,10 +24,11 @@ function plugin_has_required_fields($plugin_contents) {
|
||||
* Version: Must be set in the plugin for WordPress 2.3+
|
||||
* Text Domain: Optional. Unique identifier, should be same as the one used in
|
||||
* plugin_text_domain()
|
||||
* Domain Path: Optional. Only useful if the translations are located in a folder
|
||||
* above the plugin's base path. For example, if .mo files are located in
|
||||
* the locale folder then Domain Path will be "/locale/" and must have the
|
||||
* first slash. Defaults to the base folder the plugin is located in.
|
||||
* Domain Path: Optional. Only useful if the translations are located in a
|
||||
* folder above the plugin's base path. For example, if .mo files are
|
||||
* located in the locale folder then Domain Path will be "/locale/" and
|
||||
* must have the first slash. Defaults to the base folder the plugin is
|
||||
* located in.
|
||||
* * / # Remove the space to close comment
|
||||
* </code>
|
||||
*
|
||||
@ -104,14 +37,42 @@ function plugin_has_required_fields($plugin_contents) {
|
||||
* 'Title' - Title of the plugin and the link to the plugin's web site.
|
||||
* 'Description' - Description of what the plugin does and/or notes
|
||||
* from the author.
|
||||
* 'Author' - The author's name and web site link.
|
||||
* 'Author' - The author's name
|
||||
* 'AuthorURI' - The authors web site address.
|
||||
* 'Version' - The plugin version number.
|
||||
* 'PluginURI' - Plugin web site address.
|
||||
* 'TextDomain' - Plugin's text domain for localization.
|
||||
* 'DomainPath' - Plugin's relative directory path to .mo files.
|
||||
*
|
||||
* 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 plugin contents when it has all of the required plugin data.
|
||||
*
|
||||
* The first 8kiB of the file will be pulled in and if the plugin data is not
|
||||
* within that first 8kiB, then the plugin author should correct their plugin
|
||||
* and move the plugin data headers to the top.
|
||||
*
|
||||
* The plugin 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.
|
||||
*
|
||||
* @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
|
||||
* @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param string $plugin_file Path to the plugin file
|
||||
* @return array See above for description.
|
||||
*/
|
||||
function get_plugin_data( $plugin_file ) {
|
||||
$plugin_data = plugin_get_contents( $plugin_file );
|
||||
// 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 );
|
||||
|
Loading…
Reference in New Issue
Block a user