ShortCodes.php 4.69 KB
<?php

namespace Tz\WordPress\Tools\ShortCodes;

use Tz\WordPress\Tools;

use ReflectionClass, ReflectionMethod, ReflectionFunction;
use Exception;

    call_user_func(function() {
        Tools\add_actions(__NAMESPACE__ . '\Actions');
        Tools\add_filters(__NAMESPACE__ . '\Filters');

        if (function_exists('rename_function')) {
            rename_function('add_shortcode', 'wp_add_shortcode');

            function add_shortcode() {
                $args = func_get_args();
                call_user_func_array('add', $args);
            }
        }
    });

function add_shortcodes($class) {
    if (!class_exists($class)) {
        throw new Exception("$class does not exist");
    }

    $ref     = new ReflectionClass($class);
    $methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
    foreach ($methods as $method) {
        add($method->name, Array($class, $method->name));
    }
}

function getFileContents($file) {
    if (!is_file($file)) {
        throw new Exception("$file not found");
    }

    ob_start();
    require($file);
    $parsed_contents = ob_get_contents();
    ob_end_clean();

    return $parsed_contents;
}

function registerClass($class) {
    call_user_func(__NAMESPACE__ . '\add_shortcodes', $class);
}

function add($code, $callback) {
    $fn = (function_exists('wp_add_shortcode') ? 'wp_add_shortcode' : 'add_shortcode');
    call_user_func($fn, $code, $callback);

    if (is_admin()) { // I may want to remove this condition...
        Vars::$registered[$code] = Array('code' => $code, 'title' => $code, 'params' => Array(), 'uses_content' => 0);

        if (is_array($callback)) {
            $ref = new ReflectionMethod($callback[0], $callback[1]);
        } else {
            $ref = new ReflectionFunction($callback);
        }
        $api = $ref->getDocComment();
        $api = explode("\n", $api);
        array_shift($api);
        array_pop($api);

        foreach ($api as $key => &$comment) {
            $comment = trim($comment, ' *');

            if (substr($comment, 0, 1) == '@') {
                $tag = trim(substr($comment, 1, strpos($comment, ' ')));
                if (empty($tag)) {
                    $tag = trim($comment, '@');
                }

                if (function_exists('parseTag_' . $tag)) {
                    call_user_func_array('parseTag_' . $tag, Array($code, $comment));
                }
            }
        }
    }
}

function parseTag_display($code, $string) {
    $string = trim(str_replace('@display', '', $string));
    Vars::$registered[$code]['title'] = $string;
}

function parseTag_param($code, $string) {
    $regex = '.*?@param {((?:[a-z][a-z]+))(\\(.*?\\))?} (.*?) (.*?)$';  // Awww yeah!
    if ($num  = preg_match_all("/" . $regex . "/is", $string, $matches)) {
        Vars::$registered[$code]['params'][] = Array('name' => $matches[3][0], 'type' => $matches[1][0], 'options' => explode(',', trim($matches[2][0], ')(')), 'desc' => $matches[4][0]);
    }
}

function parseTag_private($code, $string) {
    Vars::$private[$code] = 1;
}

function uses_content($code) {
    Vars::$registered[$code]['uses_content'] = 1;
}

function getRegistered() {
    $return = Vars::$registered;
    foreach (Vars::$private as $key => $one) {
        unset($return[$key]);
    }

    return $return;
}

function drawMetaBox() {
?>
<label for="TzShortCodeList">Tag:</label>
<select id="TzShortCodeList" name="TzShortCodeList">
<?php
    $options = getRegistered();
    ksort($options);
    foreach ($options as $tag => $data) {
        echo '<option value="' . $tag . '">' . $data['title'] . '</option>';
    }
?>
</select>

<p class="submit">
  <input type="button" id="TzInsertSC" value="<?php _e('Insert into post'); ?>" />
</p>
<?php
}

class Actions {
    public static function admin_menu() {
        add_meta_box('TzShortCodes', 'Code Helper', __NAMESPACE__ . '\drawMetaBox', 'post', 'normal');
        add_meta_box('TzShortCodes', 'Code Helper', __NAMESPACE__ .  '\drawMetaBox', 'page', 'normal');
    }

    public static function admin_print_scripts() {
        if ($GLOBALS['editing']) {
            _enqueue_script('shortcoder', Tools\url('shortcoder.js', __FILE__), Array('jquery'));

            echo "<script type=\"text/javascript\">\n/* <![CDATA[ */\n";
            echo 'var TzRegisteredShortCodes = ' . json_encode(getRegistered());
            echo "\n/* ]]> */</script>\n";
        }
    }
}

class Filters {
    public static function the_title($content) {
        return is_admin() ? $content : do_shortcode($content);
    }

    public static function wp_title($content) {
        return is_admin() ? $content : do_shortcode(htmlspecialchars_decode($content));
    }
}

class Vars {
    public static $registered = Array();
    public static $private    = Array();
}
?>