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

    TzTools::make();

class TzTools {
    public static function make() {
        spl_autoload_register(Array(__CLASS__, 'autoloader'));

        require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'wp_functions.php');

        _register_script('addEvent', self::tools_url('scripts/addEvent.js', __FILE__));
        _register_script('xmlhttpHandler', self::tools_url('scripts/xmlhttpHandler.js', __FILE__));
        _register_script('fireEvent', self::tools_url('scripts/fireEvent.js', __FILE__));

        // This is (hopefully) getting canned in 3.0
        add_action('widgets_init', Array('MenuWidget', 'init'));

        self::import('ShortCodes');
        if (defined('TZ_DEBUG') && TZ_DEBUG === true) {
            self::import('Debug');
        }
    }

    public static function import($com) {
        $dir  = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'com' . DIRECTORY_SEPARATOR . $com . DIRECTORY_SEPARATOR;
        $file = $dir . $com . '.php';
        if (is_dir($dir) && is_file($file)) {
            require_once($file);
        }
    }

    public static function autoloader($class) {
        $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php';
        if (is_file($file)) {
            include($file);
        }
    }

    public static function tools_url($script, $base_file = false) {
        $base_dir = (false === $base_file ? dirname(__FILE__) : dirname($base_file));
        $rel_path = str_replace(ABSPATH, '', $base_dir);
        $script   = site_url() . '/' . $rel_path . '/' . $script;

        return $script;
    }
}

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 get_custom_data($name, $post_id = false) {
    if (false === $type = get_post_type($post_id)) {
        throw new InvalidArgumentException("Post {$post_id} does not exist");
    }

    $raw_data = call_user_func_array("_custom_{$type}", Array($post_id, $name));

    if (null === $raw_data) {
        return '';
    }

    return $raw_data;
}

function _custom_attachment($post_id, $custom_name) {
    if (false === ($tax_object = get_the_terms($post_id, $custom_name))) {
        return '';
    }
    $tax_data = array_shift($tax_object);

    return $tax_data->name;
}

function _custom_page($post_id, $custom_name) {
    $custom = get_post_meta($post_id, $custom_name);
    return array_shift($custom);
}

function _custom_post() {
    $args = func_get_args();
    return call_user_func_array('_custom_page', $args);
}

function _custom_revision() {
    $args = func_get_args();
    return call_user_func_array('_custom_page', $args);
}
?>