Skip to:
Content

bbPress.org

Changeset 2051


Ignore:
Timestamp:
04/08/2009 09:04:38 PM (17 years ago)
Author:
mdawaffe
Message:

updates to bb_get_plugin_data(): parse plugin data block even if after PHPDoc block. Allow translation of plugin data. Imported from WP

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bb-admin/includes/functions.bb-plugin.php

    r2011 r2051  
    8686}
    8787
    88 // Output sanitized for display
    89 function bb_get_plugin_data( $plugin_file )
    90 {
     88/**
     89 * Parse the plugin contents to retrieve plugin's metadata.
     90 *
     91 * The metadata of the plugin's data searches for the following in the plugin's
     92 * header. All plugin data must be on its own line. For plugin description, it
     93 * must not have any newlines or only parts of the description will be displayed
     94 * and the same goes for the plugin data. The below is formatted for printing.
     95 *
     96 * <code>
     97 * /*
     98 * Plugin Name: Name of Plugin
     99 * Plugin URI: Link to plugin information
     100 * Description: Plugin Description
     101 * Author: Plugin author's name
     102 * Author URI: Link to the author's web site
     103 * Version: Must be set
     104 * Requires at least: Optional.  Minimum bbPress version this plugin requires
     105 * Tested up to: Optional. Maximum bbPress version this plugin has been tested with
     106 * Text Domain: Optional. Unique identifier, should be same as the one used in
     107 *      load_plugin_textdomain()
     108 * Domain Path: Optional. Only useful if the translations are located in a
     109 *      folder above the plugin's base path. For example, if .mo files are
     110 *      located in the locale folder then Domain Path will be "/locale/" and
     111 *      must have the first slash. Defaults to the base folder the plugin is
     112 *      located in.
     113 *  * / # You must remove the space to close comment (the space is here only for documentation purposes).
     114 * </code>
     115 *
     116 * Plugin data returned array contains the following:
     117 *      'location' - Location of plugin file
     118 *      'name' - Name of the plugin, must be unique.
     119 *      'uri' - Plugin's web site.
     120 *      'plugin_link' - Title of plugin linked to plugin's web site.
     121 *      'description' - Description of what the plugin does and/or notes
     122 *      from the author.
     123 *      'author' - The author's name
     124 *      'author_uri' - The author's web site address.
     125 *      'author_link' - The author's name linked to the author's web site.
     126 *      'version' - The plugin version number.
     127 *      'requires' - Minimum bbPress version plugin requires
     128 *      'tested' - Maximum bbPress version plugin has been tested with
     129 *      'text_domain' - Plugin's text domain for localization.
     130 *      'domain_path' - Plugin's relative directory path to .mo files.
     131 *
     132 * Some users have issues with opening large files and manipulating the contents
     133 * for want is usually the first 1kiB or 2kiB. This function stops pulling in
     134 * the plugin contents when it has all of the required plugin data.
     135 *
     136 * The first 8kiB of the file will be pulled in and if the plugin data is not
     137 * within that first 8kiB, then the plugin author should correct their plugin
     138 * and move the plugin data headers to the top.
     139 *
     140 * The plugin file is assumed to have permissions to allow for scripts to read
     141 * the file. This is not checked however and the file is only opened for
     142 * reading.
     143 *
     144 * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
     145 * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
     146 * @since 1.5.0
     147 *
     148 * @param string $plugin_file Path to the plugin file
     149 * @param bool $markup If the returned data should have HTML markup applied
     150 * @param bool $translate If the returned data should be translated
     151 * @return array See above for description.
     152 */
     153function bb_get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
    91154    global $bb;
    92155
     
    116179    }
    117180
    118     $plugin_code = implode( '', file( $plugin_file ) );
    119 
    120     // Grab just the first commented area from the file
    121     if ( !preg_match( '|/\*(.*)\*/|msU', $plugin_code, $plugin_block ) ) {
    122         return false;
    123     }
    124 
    125     $plugin_data = trim( $plugin_block[1] );
    126 
    127     if ( !preg_match( '/Plugin Name:(.*)/i', $plugin_data, $plugin_name ) ) {
    128         return false;
    129     }
    130 
    131     preg_match( '/Plugin URI:(.*)/i', $plugin_data, $plugin_uri );
    132     preg_match( '/Description:(.*)/i', $plugin_data, $description );
    133     preg_match( '/Author:(.*)/i', $plugin_data, $author_name );
    134     preg_match( '/Author URI:(.*)/i', $plugin_data, $author_uri );
    135 
    136     if ( preg_match( '/Requires at least:(.*)/i', $plugin_data, $requires ) ) {
    137         $requires = wp_specialchars( trim( $requires[1] ) );
    138     } else {
    139         $requires = '';
    140     }
    141     if ( preg_match( '/Tested up to:(.*)/i', $plugin_data, $tested ) ) {
    142         $tested = wp_specialchars( trim( $tested[1] ) );
    143     } else {
    144         $tested = '';
    145     }
    146     if ( preg_match( '/Version:(.*)/i', $plugin_data, $version ) ) {
    147         $version = wp_specialchars( trim( $version[1] ) );
    148     } else {
    149         $version = '';
    150     }
    151 
    152     $plugin_name = wp_specialchars( trim( $plugin_name[1] ) );
    153 
    154     if ( $plugin_uri ) {
    155         $plugin_uri = clean_url( trim( $plugin_uri[1] ) );
    156     } else {
    157         $plugin_uri = '';
    158     }
    159     if ( $author_name ) {
    160         $author_name = wp_specialchars( trim( $author_name[1] ) );
    161     } else {
    162         $author_name = '';
    163     }
    164     if ( $author_uri ) {
    165         $author_uri = clean_url( trim( $author_uri[1] ) );
    166     } else {
    167         $author_uri = '';
    168     }
    169 
    170     if ( $description ) {
    171         $description = trim( $description[1] );
    172         $description = bb_encode_bad( $description );
    173         $description = bb_code_trick( $description );
    174         $description = force_balance_tags( $description );
    175         $description = bb_filter_kses( $description );
    176         $description = bb_autop( $description );
    177     } else {
    178         $description = '';
    179     }
     181    // We don't need to write to the file, so just open for reading.
     182    $fp = fopen($plugin_file, 'r');
     183
     184    // Pull only the first 8kiB of the file in.
     185    $plugin_data = fread( $fp, 8192 );
     186
     187    // PHP will close file handle, but we are good citizens.
     188    fclose($fp);
     189
     190    preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name );
     191    preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri );
     192    preg_match( '|Version:(.*)|i', $plugin_data, $version );
     193    preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
     194    preg_match( '|Author:(.*)$|mi', $plugin_data, $author );
     195    preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
     196    preg_match( '|Text Domain:(.*)$|mi', $plugin_data, $text_domain );
     197    preg_match( '|Domain Path:(.*)$|mi', $plugin_data, $domain_path );
     198    preg_match( '|Requires at least:(.*)$|mi', $plugin_data, $requires );
     199    preg_match( '|Tested up to:(.*)$|mi', $plugin_data, $tested );
    180200
    181201    // Normalise the path to the plugin
     
    190210    }
    191211
    192     $r = array(
    193         'location' => $location,
    194         'name' => $plugin_name,
    195         'uri' => $plugin_uri,
    196         'description' => $description,
    197         'author' => $author_name,
    198         'author_uri' => $author_uri,
    199         'requires' => $requires,
    200         'tested' => $tested,
    201         'version' => $version
     212    $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
     213
     214    $fields = array(
     215        'location' => '',
     216        'name' => 'html',
     217        'uri' => 'url',
     218        'version' => 'text',
     219        'description' => 'html',
     220        'author' => 'html',
     221        'author_uri' => 'url',
     222        'text_domain' => '',
     223        'domain_path' => '',
     224        'requires' => 'text',
     225        'tested' => 'text',
    202226    );
    203 
    204     $r['plugin_link'] = ( $plugin_uri ) ?
    205         "<a href='$plugin_uri' title='" . attribute_escape( __('Visit plugin homepage') ) . "'>$plugin_name</a>" :
    206         $plugin_name;
    207     $r['author_link'] = ( $author_name && $author_uri ) ?
    208         "<a href='$author_uri' title='" . attribute_escape( __('Visit author homepage') ) . "'>$author_name</a>" :
    209         $author_name;
    210 
    211     return $r;
     227    foreach ( $fields as $field => $san ) {
     228        if ( !empty( ${$field} ) ) {
     229            ${$field} = trim(${$field}[1]);
     230            switch ( $san ) {
     231            case 'html' :
     232                ${$field} = bb_filter_kses( ${$field} );
     233                break;
     234            case 'text' :
     235                ${$field} = wp_specialchars(  ${$field} );
     236                break;
     237            case 'url' :
     238                ${$field} = clean_url( ${$field} );
     239                break;
     240            }
     241        } else {
     242            ${$field} = '';
     243        }
     244    }
     245
     246    $plugin_data = compact( array_keys( $fields ) );
     247
     248    if ( $translate )
     249        $plugin_data = _bb_get_plugin_data_translate( $plugin_data );
     250
     251    if ( $markup )
     252        $plugin_data['description'] = bb_autop( $plugin_data['description'] );
     253
     254    $plugin_data['plugin_link'] = ( $plugin_data['uri'] ) ?
     255        "<a href='{$plugin_data['plugin_uri']}' title='" . attribute_escape( __('Visit plugin homepage') ) . "'>{$plugin_data['name']}</a>" :
     256        $plugin_data['name'];
     257    $plugin_data['author_link'] = ( $plugin_data['author'] && $plugin_data['author_uri'] ) ?
     258        "<a href='{$plugin_data['author_uri']}' title='" . attribute_escape( __('Visit author homepage') ) . "'>{$plugin_data['author']}</a>" :
     259        $plugin_data['author'];
     260
     261    return $plugin_data;
     262}
     263
     264function _bb_get_plugin_data_translate( $plugin_data ) {
     265    //Translate fields
     266    if( !empty($plugin_data['text_domain']) ) {
     267        if( ! empty( $plugin_data['domain_path'] ) )
     268            load_plugin_textdomain($plugin_data['text_domain'], dirname($plugin_file). $plugin_data['domain_path']);
     269        else
     270            load_plugin_textdomain($plugin_data['text_domain'], dirname($plugin_file));
     271
     272        foreach ( array('name', 'plugin_url', 'description', 'author', 'author_uri', 'version') as $field )
     273            $plugin_data[$field] = translate($plugin_data[$field], $plugin_data['text_domain']);
     274    }
     275
     276    return $plugin_data;
    212277}
    213278
Note: See TracChangeset for help on using the changeset viewer.