Skip to content

Instantly share code, notes, and snippets.

@jrfnl
Last active December 31, 2015 23:59
Embed
What would you like to do?
Debugged version of the ShortcodeReference.php file from @bartee's WP plugin Shortcode Reference. Not all the fixes are done in the most efficient way, but at least it (sort of) works now and definitely works better than before.
<?php
/**
* DataContainer for shortcodes.
*
* @author Bart Stroeken
*/
class ShortcodeReference {
/**
* Shortcode
* @var string
*/
private $_shortcode;
/**
* @var ReflectionFunction
*/
private $_function_reflection;
/**
* @var string
*/
private $_filepath;
/**
* Flat DocComments.
* @var string
*/
private $_description;
/**
* @var array
*/
private $_attributes;
/**
* @var string
*/
private $_function_name;
/**
* The tags that were found in the documentation
* @var array
*/
private $_known_tags;
/**
* - function name
* - attribute(s)
* - plugin or core
*/
public function __construct($shortcode){
global $shortcode_tags;
if (!key_exists($shortcode, $shortcode_tags)){
return false;
}
$this->_shortcode = $shortcode;
$this->_function_name = $shortcode_tags[$shortcode];
if (is_string($this->_function_name) && strpos( $this->_function_name, '::' ) === false ){
$this->_function_reflection = new ReflectionFunction($this->_function_name);
} else if ( is_string($this->_function_name) && strpos( $this->_function_name, '::' ) !== false) {
$this->_function_reflection = new ReflectionMethod ($this->_function_name);
} else if (is_array($this->_function_name)) {
$this->_function_reflection = new ReflectionMethod ($this->_function_name[0],$this->_function_name[1]);
}
}
/**
* If no description for the function is found, it will parse the DocComment of the function and return it as a string.
*/
public function getDescription(){
if ($this->_description == ''){
$this->_known_tags = array();
$desc = $this->_function_reflection->getDocComment();
$parsed_desc = '';
if ($desc){
$matches = preg_split('/\n/',$desc);
$start_pattern = '/w*\/\*\*w*/';
foreach ($matches as $match) {
if (preg_match($start_pattern, $match,$submatch)){
// skip it
} else if (preg_match('/w*\*\//',$match,$submatch)){
$offset = strpos($match,'*/')-1;
$final_line = (isset($final_line) ? $final_line. trim(substr($match,0,-$offset)).'
' : trim(substr($match,0,-$offset)).'
');
if ($final_line != ''){
$parsed_desc .= $final_line;
}
} else if (preg_match('/w*\*/',$match,$submatch)){
if (preg_match('/@/',$match,$submatch)){
$offset = strpos($match,'@')+1;
$tag = trim(substr($match,$offset,strlen($match)-$offset));
$this->addTagFromString($tag);
} else {
$offset = strpos($match,'*')+1;
$parsed_desc .= trim(substr($match,$offset,strlen($match)-$offset)).'
';
}
}
}
}
if ($parsed_desc == ''){
$parsed_desc = __('No documentation found. ','Shortcode Reference');
}
$this->_description = $parsed_desc;
}
return $this->_description;
}
/**
* Will find where the targeted function is defined.
* @return string
*/
public function getReference(){
$absolute_path = $this->_function_reflection->getFileName();
$this->_filepath = $absolute_path;
$wp_dir_sep = substr( ABSPATH, -1 );
$absolute_path = str_replace( $wp_dir_sep, DIRECTORY_SEPARATOR, $absolute_path );
$WP_abs_path = str_replace( $wp_dir_sep, DIRECTORY_SEPARATOR, ABSPATH );
if (strpos($absolute_path, $WP_abs_path)>=0){
/**
* Yay, it's from Wordpress!
*/
$WP_plugin_dir = str_replace( $wp_dir_sep, DIRECTORY_SEPARATOR, WP_PLUGIN_DIR );
$WPMU_plugin_dir = str_replace( $wp_dir_sep, DIRECTORY_SEPARATOR, WPMU_PLUGIN_DIR );
if ( strpos($absolute_path, $WP_abs_path.WPINC.DIRECTORY_SEPARATOR) !== false ){
return 'WordPress function';
} else if ( strpos($absolute_path, $WP_plugin_dir.DIRECTORY_SEPARATOR) !== false || strpos($absolute_path, $WPMU_plugin_dir.DIRECTORY_SEPARATOR) !== false ){
$plugin_path = '';
if( strpos($absolute_path, $WP_plugin_dir.DIRECTORY_SEPARATOR) !== false ) {
$plugin_path = str_replace($WP_plugin_dir.DIRECTORY_SEPARATOR,'',$absolute_path);
}
else if( strpos($absolute_path, $WPMU_plugin_dir.DIRECTORY_SEPARATOR) !== false ) {
$plugin_path = str_replace($WPMU_plugin_dir.DIRECTORY_SEPARATOR,'',$absolute_path);
}
$plugin_path = explode(DIRECTORY_SEPARATOR,$plugin_path);
if( $plugin_path[0] !== '' ) {
return 'Plugin: '.$plugin_path[0];
}
else {
return '';
}
}
}
return 'PHP native';
}
/**
* Retrieve the absolute file path
*
* @return string
*/
public function getFilePath(){
return $this->_filepath;
}
/**
* Get the options for the function
*
* @return array
*/
public function getParameters(){
$options = $this->_function_reflection->getParameters();
}
/**
* Parse a string to a tag
* @param string $string
*/
private function addTagFromString($string){
$tag = explode(' ',$string);
$tagname = array_shift($tag);
$this->_known_tags[$tagname] = implode(' ',$tag);
}
/**
* Get the tags for the current documentation
*/
public function getTags(){
if (!is_array($this->_known_tags)){
$desc = $this->getDescription();
}
return $this->_known_tags;
}
/**
* Get the URL where you can find more information about the shortcode.
*
* @return url
*/
public function getUrl(){
if ( !isset($this->_url) || !$this->_url){
$is_plugin = strpos($this->getReference(),'Plugin:');
if ($this->getReference() == 'WordPress function'){
$this->_url ='http://codex.wordpress.org/index.php?title=Special:Search&search='.$this->_shortcode.'_Shortcode';
} else if ($is_plugin !== false){
$plugin_info = get_plugin_data($this->_filepath);
if( is_array($plugin_info) && $plugin_info['Name'] === '' ) {
// This was obviously not the main file, let try again
$wp_dir_sep = substr( ABSPATH, -1 );
$WP_plugin_dir = str_replace( $wp_dir_sep, DIRECTORY_SEPARATOR, WP_PLUGIN_DIR );
$WPMU_plugin_dir = str_replace( $wp_dir_sep, DIRECTORY_SEPARATOR, WPMU_PLUGIN_DIR );
$plugin_path = $this->_filepath;
$plugin_path = str_replace( $wp_dir_sep, DIRECTORY_SEPARATOR, $plugin_path );
if( strpos($plugin_path, $WP_plugin_dir.DIRECTORY_SEPARATOR) !== false ) {
$plugin_path = str_replace($WP_plugin_dir.DIRECTORY_SEPARATOR,'',$plugin_path);
$plugin_path = substr( $plugin_path, 0, strpos( $plugin_path, DIRECTORY_SEPARATOR ) );
$plugin_path = $WP_plugin_dir.DIRECTORY_SEPARATOR . $plugin_path . DIRECTORY_SEPARATOR . $plugin_path . '.php';
}
else if( strpos($plugin_path, $WPMU_plugin_dir.DIRECTORY_SEPARATOR) !== false ) {
$plugin_path = str_replace($WPMU_plugin_dir.DIRECTORY_SEPARATOR,'',$plugin_path);
$plugin_path = substr( $plugin_path, 0, strpos( $plugin_path, DIRECTORY_SEPARATOR ) );
$plugin_path = $WPMU_plugin_dir.DIRECTORY_SEPARATOR . $plugin_path . DIRECTORY_SEPARATOR . $plugin_path . '.php';
}
if( file_exists( $plugin_path ) ) {
$plugin_info = get_plugin_data($plugin_path);
}
}
if (is_array($plugin_info) && key_exists('PluginURI',$plugin_info) && $plugin_info['PluginURI'] !== ''){
/**
* If you can find the plugin-url, use that
*/
$this->_url = $plugin_info['PluginURI'];
} else if (is_array($plugin_info) && key_exists('AuthorURI',$plugin_info) && $plugin_info['AuthorURI'] !== ''){
/**
* Else use the author-URI
*/
$this->_url = $plugin_info['AuthorURI'];
} else {
/**
* If all else fails, Google is your friend
*/
$this->_url = 'http://www.google.com/search?q=Wordpress+'.$plugin_path.'+'.$this->_shortcode;
}
} else {
$this->_url = 'http://www.php.net/'.$this->_shortcode;
}
}
return $this->_url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment