tz-tools.php
2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?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 \ReflectionClass, \ReflectionMethod;
use \Exception;
spl_autoload_register(__NAMESPACE__ . '\autoloader');
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__));
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);
}
}
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) {
$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 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_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);
}
}
?>