tz-tools.php
4.06 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?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();
}
?>