tz-tools.php 4.06 KB
<?php
/*
Plugin Name: Tenzing Tools
Version: 0.4b
Description: Various classes and functions to help out with stuff
Author: Tenzing
*/

namespace Tz\WordPress\Tools;

use Tz\WordPress\Tools\ShortCodes;

use ReflectionClass, ReflectionMethod;
use Exception;

    spl_autoload_register(__NAMESPACE__ . '\autoloader');

    // Code to prevent PHP from parsing Symlinks
    if (defined(__NAMESPACE__ . '\DIR')) {
        define(__NAMESPACE__ . '\OVERRIDE', 1);
    } else {
        define(__NAMESPACE__ . '\DIR', __DIR__);
    }
    define(__NAMESPACE__ . '\FILE', DIR . DIRECTORY_SEPARATOR . basename(__FILE__));

    require_once(DIR . DIRECTORY_SEPARATOR . 'wp_functions.php');

    _register_script('addEvent', url('scripts/addEvent.js', FILE));
    _register_script('xmlhttpHandler', url('scripts/xmlhttpHandler.js', FILE));
    _register_script('fireEvent', url('scripts/fireEvent.js', FILE));
    _register_script('Cookie', url('scripts/Cookie/Cookie.js', FILE));

    import('ShortCodes');
    if (defined('Tz\DEBUG') && Tz\DEBUG === true) {
        import('Debug');
    }

function import($com) {
    $dir  = DIR . DIRECTORY_SEPARATOR . 'com' . DIRECTORY_SEPARATOR . $com . DIRECTORY_SEPARATOR;
    $file = $dir . $com . '.php';
    if (is_dir($dir) && is_file($file)) {
        require_once($file);
        Vars::$loaded[$com] = 1;
    }
}

function autoloader($class) {
    $a = explode('\\', $class);
    $class = array_pop($a);

    $file = DIR . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php';
    if (is_file($file)) {
        include($file);
    }
}

function url($script, $base_file = false) {
    if (defined(__NAMESPACE__ . '\OVERRIDE')) {
        $info = Array(pathinfo(DIR), pathinfo(__DIR__));

        // This should replace link for tz-tools com components
        $base_file = str_replace(__DIR__, DIR, $base_file, $q);

        // I think this does the same...?
        $base_file = str_replace($info[1]['dirname'], $info[0]['dirname'], $base_file, $r);

        // This should replace for theme files
        $theme_dir = get_theme_root() . DIRECTORY_SEPARATOR . get_template();
        if (is_link($theme_dir)) {
            $base_file = str_replace(readlink($theme_dir), $theme_dir . DIRECTORY_SEPARATOR, $base_file);
        }

        // I probably will need to add another replace for plugins (if we use plugins w/ tz-tools, prolly won't come to think of it)
    }

    $base_dir = (false === $base_file ? DIR : dirname($base_file));
    $rel_path = str_replace(ABSPATH, '', $base_dir);
    $script   = site_url() . '/' . $rel_path . '/' . $script;

    return $script;
}

function tools_url() {
    $args = func_get_args();
    call_user_func_array(__NAMESPACE__ . '\url', $args);
}

function buffer($callback) {
    ob_start();
    call_user_func($callback);
    $b = ob_get_contents();
    ob_end_clean();

    return $b;
}

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

    $ref     = new ReflectionClass($class);
    $actions = $ref->getMethods(ReflectionMethod::IS_STATIC);
    foreach ($actions as $action) {
        add_action($action->name, Array($class, $action->name));
    }
}

function add_filters($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_filter($method->name, Array($class, $method->name));
    }
}

function add_shortcodes($class) {
    ShortCodes\add_shortcodes($class);
}

function add_settings_fields($class, $page = 'general', $section = 'default') {
    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_settings_field($method->name, ucwords(str_replace('_', ' ', $method->name)), Array($class, $method->name), $page, $section);
    }
}

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