WordPress.org

Make WordPress Core

Ticket #7372: plugin_data.7372.diff

File plugin_data.7372.diff, 3.8 KB (added by santosj, 10 years ago)

Performance improvements for #7372

  • plugin.php

     
    77 */
    88
    99/**
    10  * plugin_get_contents() - Retrieve enough of the plugin file to get plugin data.
     10 * Retrieve enough of the plugin file to get plugin data.
    1111 *
    1212 * Some users have issues with opening large files and manipulating the
    1313 * contents for want is usually the first 1kiB or 2kiB. This function
    1414 * stops pulling in the plugin contents when it has all of the required
    1515 * plugin data.
    1616 *
    17  * It also adds a little bit of padding for fudging the version info
    18  * and to make sure that we grab absolutely everything, in case of a
    19  * long description.
     17 * The first 8kiB of the file will be pulled in and if the plugin data is not
     18 * within that first 8kiB, then the plugin author should correct their plugin
     19 * and move the plugin data headers to the top.
    2020 *
    2121 * The plugin file is assumed to have permissions to allow for scripts to
    2222 * read the file. This is not checked however and the file is only opened
    2323 * for reading.
    2424 *
    2525 * @link http://trac.wordpress.org/ticket/5651 Purpose of function.
     26 * @link http://trac.wordpress.org/ticket/7372 Optimization of function.
    2627 * @since 2.7.0
    27  * @uses plugin_has_required_fields() Checks for all of the required plugin
    28  *              data fields.
    2928 *
    3029 * @param string $plugin_file Path to plugin file to open
    3130 * @return string Plugin file contents retrieved
     
    3534        // We don't need to write to the file, so just open for reading.
    3635        $fp = fopen($plugin_file, 'r');
    3736
    38         // Store the contents of the plugin file here.
    39         $contents = '';
     37        // Pull only the first 8kiB of the file in.
     38        $contents = fread( $fp, 8192 );
    4039
    41         // Keep reading the contents of the file until End of File is
    42         // reached, or we grabbed all of the required plugin data.
    43         while( !feof($fp) && !plugin_has_required_fields($contents) )
    44                 $contents .= fread( $fp, 1024 );
    45 
    46         // Make sure that any padding is adding for long descriptions
    47         // and grabbing any optional plugin data, not checked for.
    48         if( !feof($fp) )
    49                 $contents .= fread( $fp, 512 );
    50 
    5140        // PHP will close file handle, but we are good citizens
    5241        fclose($fp);
    5342        return $contents;
    5443}
    5544
    5645/**
    57  * plugin_has_required_fields() - Checks plugin contents for required plugin data
     46 * Parse the plugin contents to retrieve plugin's metadata.
    5847 *
    59  * @since 2.7.0
    60  * @usedby plugin_get_contents()
    61  *
    62  * @param string $plugin_contents Plugin file contents
    63  * @return bool Whether contents has all plugin data.
    64  */
    65 function plugin_has_required_fields($plugin_contents) {
    66         $hasName = stripos($plugin_contents, 'plugin name:');
    67         $hasPluginURI = stripos($plugin_contents, 'plugin uri:');
    68         $hasDescription = stripos($plugin_contents, 'description:');
    69         $hasAuthor = stripos($plugin_contents, 'author:');
    70         $hasAuthorURI = stripos($plugin_contents, 'author uri:');
    71 
    72         if( false !== $hasName && false !== $hasPluginURI && false !== $hasDescription &&
    73                 false !== $hasAuthor && false !== $hasAuthorURI)
    74                 return true;
    75 
    76         return false;
    77 }
    78 
    79 /**
    80  * get_plugin_data() - Parse the plugin contents to retrieve plugin's metadata
    81  *
    8248 * The metadata of the plugin's data searches for the following in the plugin's
    8349 * header.
    8450 *
     
    10470 *              'Title' - Title of the plugin and the link to the plugin's web site.
    10571 *              'Description' - Description of what the plugin does and/or notes
    10672 *              from the author.
    107  *              'Author' - The author's name and web site link.
     73 *              'Author' - The author's name
     74 *              'AuthorURI' - The authors web site address.
    10875 *              'Version' - The plugin version number.
     76 *              'PluginURI' - Plugin web site address.
     77 *              'TextDomain' - Plugin's text domain for localization.
     78 *              'DomainPath' - Plugin's relative directory path to .mo files.
    10979 *
    11080 * @param string $plugin_file Path to the plugin file
    11181 * @return array See above for description.