tz-tools.php
3.76 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
141
142
143
144
145
146
147
148
149
<?php
/*
Plugin Name: Tenzing Tools
Version: 0.1
Description: Various classes to help out with stuff
Author: Tenzing
*/
if (version_compare(PHP_VERSION, '5.2.2') !== 1) {
die('PHP version 5.2.2 or greater is required');
}
TzTools::load();
class TzTools {
public static function load() {
spl_autoload_register(Array(__CLASS__, 'autoloader'));
require_once(dirname(__FILE__) . '/wp_functions.php');
_register_script('addEvent', plugins_url('addEvent.js', __FILE__));
_register_script('xmlhttpHandler', plugins_url('xmlhttpHandler.js', __FILE__));
_register_script('fireEvent', plugins_url('fireEvent.js', __FILE__));
add_action('widgets_init', Array('MenuWidget', 'init'));
}
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);
}
}
}
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_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_shortcode($method->name, Array($class, $method->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;
/* @deprecated
if (is_array($raw_data)) {
return $raw_data;
}
return $raw_data
/* @deprecated
if (null === $maybe_data = json_decode($raw_data, true)) {
return $raw_data;
} else {
return $maybe_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);
// @deprecated
static $custom = Array();
if (!isset($custom[$post_id])) {
$custom[$post_id] = get_post_custom($post_id);
if (!isset($custom[$post_id][$custom_name])) {
$custom[$post_id][$custom_name] = Array('');
}
}
return array_shift($custom[$post_id][$custom_name]);
}
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);
}
?>