tz-tools.php
7.62 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?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 Exception;
use ReflectionClass;
use ReflectionMethod;
use Tz\WordPress\Tools\ShortCodes;
spl_autoload_register(__NAMESPACE__.'\autoloader');
add_action( 'login_headertext', function($title) {
if($_REQUEST['action'] == 'lostpassword') {
return "RESET PASSWORD:";
}
return "LOG IN:";
} );
add_filter( 'gettext_with_context', function($translated) {
// Use the text string exactly as it is in the translation file
if ( $translated == "← Back to %s" ) {
$translated = "Back to %s";
}
return $translated;
} );
// 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));
_register_script('Uploadify', url('scripts/uploadify/jquery.uploadify.v2.1.4.js', FILE));
_register_style('UploadifyCSS', url('scripts/uploadify/uploadify.css', 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 = [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);
}
/**
* "Cleans" data by removing double backslashes and escaped quotes. Will run recursively on arrays.
*
* @param $data array|string Data to clean
*
* @return array|string
*/
function tzClean(&$data)
{
if (is_array($data)) {
foreach ($data as $index => $child_data) {
tzClean($data[$index]);
}
} else {
$data = str_ireplace(
'\\',
'',
str_ireplace(
['\"', "\'"],
['"', "'"],
$data
)
);
}
return $data;
}
/**
* @returns {WP_User} of the currently logged in user
*/
function getCurrentUser()
{
global $current_user;
get_currentuserinfo();
return $current_user;
}
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, [$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, [$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)),
[$class, $method->name],
$page,
$section
);
}
}
function TzSuperPaginationBar(
$posts,
$pages,
$range = 2,
$before = "",
$after = "",
$show_search = false,
$show_advanced = false,
$post_type = ""
) {
$bar = '<div class="TzSuperPaginationBar">';
if ($show_search) {
$bar .= '<div class="pagination-search"><form id="TzPaginationSearch"><input type="hidden" name="post_type" value="'.$post_type.'" /><input name="search_criteria" type="input" /></form></div>';
}
if ($show_advanced) {
$bar .= '<div class="pagination-advanced"><a href="#" onclick="return false;">Advanced</a></div>';
}
$bar .= '<div class="pagination-paging">'.pagination($posts, $pages, $range, $before, $after, false).'</div>';
$bar .= '<div style="clear:both;"></div>';
$bar .= '</div>';
return $bar;
}
function pagination($posts, $pages, $range = 2, $before = '', $after = '', $echo = true)
{
$p = "";
$showitems = ($range * 2) + 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// if(is_null($pages)) {
// global $wp_query;
// $pages = $wp_query->max_num_pages;
// if (!$pages) {
// $pages = 1;
// }
// }
if (1 != $pages) {
$p .= $before;
if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) {
$p .= "<a href='".get_pagenum_link(1)."'>«</a>";
}
if ($paged > 1 && $showitems < $pages) {
$p .= "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
}
for ($i = 1; $i <= $pages; $i++) {
if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
$p .= ($paged == $i)
? "<span class='current'>".$i."</span>"
: "<a href='".get_pagenum_link(
$i
)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) {
$p .= "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
}
if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) {
$p .= "<a href='".get_pagenum_link($pages)."'>»</a>";
}
$p .= $after;
}
if ($echo) {
echo $p;
} else {
return $p;
}
}
class Vars
{
public static $loaded = [];
}
?>