WordPress.org

Make WordPress Core

Ticket #7372: plugin_data.7372.2.diff

File plugin_data.7372.2.diff, 5.9 KB (added by santosj, 7 years ago)

Further combination of functions for increased performance and decrease function stack overhead.

  • plugin.php

     
    77 */ 
    88 
    99/** 
    10  * plugin_get_contents() - Retrieve enough of the plugin file to get plugin data. 
     10 * Parse the plugin contents to retrieve plugin's metadata. 
    1111 * 
    12  * Some users have issues with opening large files and manipulating the 
    13  * contents for want is usually the first 1kiB or 2kiB. This function 
    14  * stops pulling in the plugin contents when it has all of the required 
    15  * plugin data. 
    16  * 
    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. 
    20  * 
    21  * The plugin file is assumed to have permissions to allow for scripts to 
    22  * read the file. This is not checked however and the file is only opened 
    23  * for reading. 
    24  * 
    25  * @link http://trac.wordpress.org/ticket/5651 Purpose of function. 
    26  * @since 2.7.0 
    27  * @uses plugin_has_required_fields() Checks for all of the required plugin 
    28  *              data fields. 
    29  * 
    30  * @param string $plugin_file Path to plugin file to open 
    31  * @return string Plugin file contents retrieved 
    32  */ 
    33 function plugin_get_contents($plugin_file) { 
    34  
    35         // We don't need to write to the file, so just open for reading. 
    36         $fp = fopen($plugin_file, 'r'); 
    37  
    38         // Store the contents of the plugin file here. 
    39         $contents = ''; 
    40  
    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  
    51         // PHP will close file handle, but we are good citizens 
    52         fclose($fp); 
    53         return $contents; 
    54 } 
    55  
    56 /** 
    57  * plugin_has_required_fields() - Checks plugin contents for required plugin data 
    58  * 
    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  * 
    8212 * The metadata of the plugin's data searches for the following in the plugin's 
    83  * header. 
     13 * header. All plugin data must be on its own line. For plugin description, it 
     14 * must not have any newlines or only parts of the description will be displayed 
     15 * and the same goes for the plugin data. The below is formatted for printing. 
    8416 * 
    8517 * <code> 
    8618 * /* 
     
    9224 * Version: Must be set in the plugin for WordPress 2.3+ 
    9325 * Text Domain: Optional. Unique identifier, should be same as the one used in 
    9426 *              plugin_text_domain() 
    95  * Domain Path: Optional. Only useful if the translations are located in a folder 
    96  *              above the plugin's base path. For example, if .mo files are located in 
    97  *              the locale folder then Domain Path will be "/locale/" and must have the 
    98  *              first slash. Defaults to the base folder the plugin is located in. 
     27 * Domain Path: Optional. Only useful if the translations are located in a 
     28 *              folder above the plugin's base path. For example, if .mo files are 
     29 *              located in the locale folder then Domain Path will be "/locale/" and 
     30 *              must have the first slash. Defaults to the base folder the plugin is 
     31 *              located in. 
    9932 *  * / # Remove the space to close comment 
    10033 * </code> 
    10134 * 
     
    10437 *              'Title' - Title of the plugin and the link to the plugin's web site. 
    10538 *              'Description' - Description of what the plugin does and/or notes 
    10639 *              from the author. 
    107  *              'Author' - The author's name and web site link. 
     40 *              'Author' - The author's name 
     41 *              'AuthorURI' - The authors web site address. 
    10842 *              'Version' - The plugin version number. 
     43 *              'PluginURI' - Plugin web site address. 
     44 *              'TextDomain' - Plugin's text domain for localization. 
     45 *              'DomainPath' - Plugin's relative directory path to .mo files. 
    10946 * 
     47 * Some users have issues with opening large files and manipulating the contents 
     48 * for want is usually the first 1kiB or 2kiB. This function stops pulling in 
     49 * the plugin contents when it has all of the required plugin data. 
     50 * 
     51 * The first 8kiB of the file will be pulled in and if the plugin data is not 
     52 * within that first 8kiB, then the plugin author should correct their plugin 
     53 * and move the plugin data headers to the top. 
     54 * 
     55 * The plugin file is assumed to have permissions to allow for scripts to read 
     56 * the file. This is not checked however and the file is only opened for 
     57 * reading. 
     58 * 
     59 * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations. 
     60 * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations. 
     61 * @since 1.5.0 
     62 * 
    11063 * @param string $plugin_file Path to the plugin file 
    11164 * @return array See above for description. 
    11265 */ 
    11366function get_plugin_data( $plugin_file ) { 
    114         $plugin_data = plugin_get_contents( $plugin_file ); 
     67        // We don't need to write to the file, so just open for reading. 
     68        $fp = fopen($plugin_file, 'r'); 
     69 
     70        // Pull only the first 8kiB of the file in. 
     71        $plugin_data = fread( $fp, 8192 ); 
     72 
     73        // PHP will close file handle, but we are good citizens. 
     74        fclose($fp); 
     75 
    11576        preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name ); 
    11677        preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri ); 
    11778        preg_match( '|Version:(.*)|i', $plugin_data, $version );