0

请告诉如何检索短代码的可用属性。

我有可用的简码,但它不会显示属性名称

global $shortcode_tags;
print_r($shortcode_tags);

有人告诉我使用 PHP 反射 API ..

获得短代码属性的工作插件代码:

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)){
            $this->_function_reflection = new ReflectionFunction($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.= 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;
        if (strpos($absolute_path, ABSPATH)>=0){
            /**
             * Yay, it's from Wordpress!
             */
            $relative_path = str_replace(ABSPATH,'',$absolute_path);
            $is_native = strpos($relative_path, 'wp-includes/');
            $is_plugin = strpos($relative_path, 'wp-content/plugins/');
            if ($is_native !== false){
                return 'WordPress function';
            } else if ($is_plugin !== false){
                $plugin_path = explode('/',str_replace('wp-content/plugins/','',$relative_path));
                return 'Plugin: '.$plugin_path[0];
            }
        }
        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 (!$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) && key_exists('PluginURI',$plugin_info)){
                    /**
                     * 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)){
                    /**
                     * 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;
    }
}
4

1 回答 1

2

短代码的属性是动态设置的。可以创建任何属性并将其传递给函数。除非创建了跟踪它们的新/自定义方法并由您想要其属性的短代码使用,否则无法获取它们。

http://codex.wordpress.org/Shortcode_API

编辑:您似乎不想要简码的属性,而是想要它的描述和详细信息。

如果您有插件,只需打开它并在记事本中找到您想要的行。

于 2013-07-22T05:11:20.580 回答