WordPress.org

Make WordPress Core

Ticket #5651: 4048.2.r6603.diff

File 4048.2.r6603.diff, 3.3 KB (added by darkdragon, 6 years ago)

Second try after suggestions, still based off of r6603 and fixes version info

  • plugin.php

     
    11<?php 
     2/** 
     3 * WordPress Plugin Administration API 
     4 * 
     5 * @package WordPress 
     6 * @subpackage Administration 
     7 */ 
    28 
     9/** 
     10 * plugin_get_contents() - Retrieve just the plugin data in the plugin file. 
     11 * 
     12 * Some users have issues with pulling the contents of the entire file, so 
     13 * instead of pulling the entire contents, we just pull in the amount we need. 
     14 * 
     15 * @link http://trac.wordpress.org/ticket/5651 Ticket for this functions being. 
     16 * 
     17 * @since 2.5.0 
     18 * @uses plugin_has_required_fields() Checks for all required plugin data fields. 
     19 * 
     20 * @param string $plugin_file Path to plugin file 
     21 * @return string File contents retrieved  
     22 */ 
     23function plugin_get_contents($plugin_file) { 
     24        // We don't need to write to the file, so just open it for reading 
     25        // only. Hopefully, the file isn't binary. 
     26        $fp = fopen($plugin_file, 'r'); 
     27 
     28        // Store the contents of the plugin file here 
     29        $contents = ''; 
     30 
     31        // Keep reading the contents of the file until End of File 
     32        // is reached or we grabbed all we need. 
     33        while( !feof($fp) && !plugin_has_required_fields($contents) )  
     34                $contents .= fread( $fp, 1024 ); 
     35 
     36        // Make sure we pick up any padding for long descriptions and 
     37        // version info. 
     38        if( !feof($fp) ) 
     39                $contents .= fread( $fp, 512 ); 
     40 
     41        // PHP will close file handle, but we are good citizens 
     42        fclose($fp); 
     43        return $contents; 
     44} 
     45 
     46/** 
     47 * plugin_has_required_fields() - Checks the contents of the string for required Plugin Data 
     48 * 
     49 * @since 2.5.0 
     50 * @usedby plugin_get_contents() 
     51 * 
     52 * @param string $plugin_contents Contents of the plugin file to check 
     53 * @return bool Whether all required Plugin Data items are in contents 
     54 */ 
     55function plugin_has_required_fields($plugin_contents) { 
     56        $hasName = stripos($plugin_contents, 'Plugin Name:'); 
     57        $hasPluginURI = stripos($plugin_contents, 'Plugin URI:'); 
     58        $hasDescription = stripos($plugin_contents, ''); 
     59        $hasAuthor = stripos($plugin_contents, ''); 
     60        $hasAuthorURI = stripos($plugin_contents, ''); 
     61         
     62        if( false !== $hasName && false !== $hasPluginURI && 
     63                false !== $hasDescription && false !== $hasAuthor && 
     64                false !== $hasAuthorURI) 
     65                return true; 
     66 
     67        return false; 
     68} 
     69 
     70/** 
     71 * get_plugin_data() - Parse the plugin contents to retrieve the plugin's data 
     72 * 
     73 * While this function has only existed since WordPress 2.5, the functionality 
     74 * has existed in WordPress for longer. 
     75 * 
     76 * Plugin data contains the following: 
     77 *              'Name' - Name of the plugin, should be unique. 
     78 *              'Title' - Title of the plugin and the link to the plugin's web site. 
     79 *              'Description' - Description of what the plugin does and/or notes from the author. 
     80 *              'Author' - The author's web site and name. 
     81 *              'Version' - The version of the plugin in the file. 
     82 * 
     83 * @since 1.5.0 
     84 * 
     85 * @param string $plugin_file Full Plugin Path 
     86 * @return array Plugin's data 
     87 */ 
    388function get_plugin_data( $plugin_file ) { 
    4         $plugin_data = implode( '', file( $plugin_file )); 
     89        $plugin_data = plugin_get_contents($plugin_file); 
    590        preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $plugin_name ); 
    691        preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $plugin_uri ); 
    792        preg_match( '|Description:(.*)$|mi', $plugin_data, $description );