bb_get_plugin_data

Parse the plugin contents to retrieve plugin's metadata.

Description

(array) bb_get_plugin_data( (string) $plugin_file, (constant) $markup = true, (bool) $translate = true ); 

The metadata of the plugin's data searches for the following in the plugin's 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.

Plugin data returned array contains the following: 'location' - Location of plugin file 'name' - Name of the plugin, must be unique. 'uri' - Plugin's web site. 'plugin_link' - Title of plugin linked to plugin's web site. 'description' - Description of what the plugin does and/or notes from the author. 'author' - The author's name 'author_uri' - The author's web site address. 'author_link' - The author's name linked to the author's web site. 'version' - The plugin version number. 'requires' - Minimum bbPress version plugin requires 'tested' - Maximum bbPress version plugin has been tested with 'text_domain' - Plugin's text domain for localization. 'domain_path' - 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.

Returns (array)

See above for description.

Parameters (3)

0. $plugin_file (string)
Path to the plugin file
1. $markup — Optional. (constant) => true
If the returned data should have HTML markup applied
2. $translate — Optional. (bool) => true
If the returned data should be translated

Usage

  1. if ( !function_exists( 'bb_get_plugin_data' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-admin/includes/functions.bb-plugin.php'; 
  3.  
  4. // Path to the plugin file 
  5. $plugin_file = ''; 
  6.  
  7. // If the returned data should have HTML markup applied 
  8. $markup = true; 
  9.  
  10. // If the returned data should be translated 
  11. $translate = true; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bb_get_plugin_data($plugin_file, $markup, $translate); 
  15.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-admin/includes/functions.bb-plugin.php  
  1. function bb_get_plugin_data( $plugin_file, $markup = true, $translate = true ) { 
  2. global $bb; 
  3.  
  4. if ( preg_match( '/^([a-z0-9_-]+)#((?:[a-z0-9\/\\_-]+.)+)(php)$/i', $plugin_file, $_matches ) ) { 
  5. $plugin_file = $bb->plugin_locations[$_matches[1]]['dir'] . $_matches[2] . $_matches[3]; 
  6.  
  7. $_directory = $bb->plugin_locations[$_matches[1]]['dir']; 
  8. $_plugin = $_matches[2] . $_matches[3]; 
  9.  
  10. if ( !$_plugin ) { 
  11. // Not likely 
  12. return false; 
  13.  
  14. if ( validate_file( $_plugin ) ) { 
  15. // $plugin has .., :, etc. 
  16. return false; 
  17.  
  18. $plugin_file = $_directory . $_plugin; 
  19. unset( $_matches, $_directory, $_plugin ); 
  20.  
  21. if ( !file_exists( $plugin_file ) ) { 
  22. // The plugin isn't there 
  23. return false; 
  24.  
  25. // We don't need to write to the file, so just open for reading. 
  26. $fp = fopen($plugin_file, 'r'); 
  27.  
  28. // Pull only the first 8kiB of the file in. 
  29. $plugin_code = fread( $fp, 8192 ); 
  30.  
  31. // PHP will close file handle, but we are good citizens. 
  32. fclose($fp); 
  33.  
  34. // Grab just the first commented area from the file 
  35. if ( !preg_match( '|/\*(.*?Plugin Name:.*?)\*/|ims', $plugin_code, $plugin_block ) ) 
  36. return false; 
  37. $plugin_data = trim( $plugin_block[1] ); 
  38.  
  39. preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name ); 
  40. preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri ); 
  41. preg_match( '|Version:(.*)|i', $plugin_data, $version ); 
  42. preg_match( '|Description:(.*)$|mi', $plugin_data, $description ); 
  43. preg_match( '|Author:(.*)$|mi', $plugin_data, $author ); 
  44. preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri ); 
  45. preg_match( '|Text Domain:(.*)$|mi', $plugin_data, $text_domain ); 
  46. preg_match( '|Domain Path:(.*)$|mi', $plugin_data, $domain_path ); 
  47. preg_match( '|Requires at least:(.*)$|mi', $plugin_data, $requires ); 
  48. preg_match( '|Tested up to:(.*)$|mi', $plugin_data, $tested ); 
  49.  
  50. // Normalise the path to the plugin 
  51. $plugin_file = str_replace( '\\', '/', $plugin_file ); 
  52.  
  53. foreach ( $bb->plugin_locations as $_name => $_data ) { 
  54. $_directory = str_replace( '\\', '/', $_data['dir'] ); 
  55. if ( 0 === strpos( $plugin_file, $_directory ) ) { 
  56. $location = array( 1 => $_name ); 
  57. break; 
  58.  
  59. $plugins_allowedtags = array('a' => array('href' => array(), 'title' => array()), 'abbr' => array('title' => array()), 'acronym' => array('title' => array()), 'code' => array(), 'em' => array(), 'strong' => array()); 
  60.  
  61. $fields = array( 
  62. 'location' => '',  
  63. 'name' => 'html',  
  64. 'uri' => 'url',  
  65. 'version' => 'text',  
  66. 'description' => 'html',  
  67. 'author' => 'html',  
  68. 'author_uri' => 'url',  
  69. 'text_domain' => '',  
  70. 'domain_path' => '',  
  71. 'requires' => 'text',  
  72. 'tested' => 'text',  
  73. ); 
  74. foreach ( $fields as $field => $san ) { 
  75. if ( !empty( ${$field} ) ) { 
  76. ${$field} = trim(${$field}[1]); 
  77. switch ( $san ) { 
  78. case 'html' : 
  79. ${$field} = bb_filter_kses( ${$field} ); 
  80. break; 
  81. case 'text' : 
  82. ${$field} = esc_html( ${$field} ); 
  83. break; 
  84. case 'url' : 
  85. ${$field} = esc_url( ${$field} ); 
  86. break; 
  87. } else { 
  88. ${$field} = ''; 
  89.  
  90. $plugin_data = compact( array_keys( $fields ) ); 
  91.  
  92. if ( $translate ) 
  93. $plugin_data = _bb_get_plugin_data_translate( $plugin_data, $plugin_file ); 
  94.  
  95. if ( $markup ) 
  96. $plugin_data['description'] = bb_autop( preg_replace( '/[\r\n]+/', "\n", trim( $plugin_data['description'] ) ) ); 
  97.  
  98. $plugin_data['plugin_link'] = ( $plugin_data['uri'] ) ? 
  99. "<a href='{$plugin_data['uri']}' title='" . esc_attr__( 'Visit plugin site' ) . "'>{$plugin_data['name']}</a>" : 
  100. $plugin_data['name']; 
  101. $plugin_data['author_link'] = ( $plugin_data['author'] && $plugin_data['author_uri'] ) ? 
  102. "<a href='{$plugin_data['author_uri']}' title='" . esc_attr__( 'Visit author homepage' ) . "'>{$plugin_data['author']}</a>" : 
  103. $plugin_data['author']; 
  104.  
  105. return $plugin_data;