f9c655e8 by Marty Penner

Format a file and make pagination more robust

1 parent 6e5c8f77
Showing 1 changed file with 117 additions and 70 deletions
...@@ -8,57 +8,60 @@ Author: Tenzing ...@@ -8,57 +8,60 @@ Author: Tenzing
8 8
9 namespace Tz\WordPress\Tools; 9 namespace Tz\WordPress\Tools;
10 10
11 use Tz\WordPress\Tools\ShortCodes;
12
13 use ReflectionClass, ReflectionMethod;
14 use Exception; 11 use Exception;
12 use ReflectionClass;
13 use ReflectionMethod;
14 use Tz\WordPress\Tools\ShortCodes;
15 15
16 spl_autoload_register(__NAMESPACE__ . '\autoloader'); 16 spl_autoload_register(__NAMESPACE__.'\autoloader');
17 17
18 // Code to prevent PHP from parsing Symlinks 18 // Code to prevent PHP from parsing Symlinks
19 if (defined(__NAMESPACE__ . '\DIR')) { 19 if (defined(__NAMESPACE__.'\DIR')) {
20 define(__NAMESPACE__ . '\OVERRIDE', 1); 20 define(__NAMESPACE__.'\OVERRIDE', 1);
21 } else { 21 } else {
22 define(__NAMESPACE__ . '\DIR', __DIR__); 22 define(__NAMESPACE__.'\DIR', __DIR__);
23 } 23 }
24 define(__NAMESPACE__ . '\FILE', DIR . DIRECTORY_SEPARATOR . basename(__FILE__)); 24 define(__NAMESPACE__.'\FILE', DIR.DIRECTORY_SEPARATOR.basename(__FILE__));
25 25
26 require_once(DIR . DIRECTORY_SEPARATOR . 'wp_functions.php'); 26 require_once(DIR.DIRECTORY_SEPARATOR.'wp_functions.php');
27 27
28 _register_script('addEvent', url('scripts/addEvent.js', FILE)); 28 _register_script('addEvent', url('scripts/addEvent.js', FILE));
29 _register_script('xmlhttpHandler', url('scripts/xmlhttpHandler.js', FILE)); 29 _register_script('xmlhttpHandler', url('scripts/xmlhttpHandler.js', FILE));
30 _register_script('fireEvent', url('scripts/fireEvent.js', FILE)); 30 _register_script('fireEvent', url('scripts/fireEvent.js', FILE));
31 _register_script('Cookie', url('scripts/Cookie/Cookie.js', FILE)); 31 _register_script('Cookie', url('scripts/Cookie/Cookie.js', FILE));
32 _register_script('Uploadify', url('scripts/uploadify/jquery.uploadify.v2.1.4.js', FILE)); 32 _register_script('Uploadify', url('scripts/uploadify/jquery.uploadify.v2.1.4.js', FILE));
33 _register_style('UploadifyCSS', url('scripts/uploadify/uploadify.css', FILE)); 33 _register_style('UploadifyCSS', url('scripts/uploadify/uploadify.css', FILE));
34 34
35 import('ShortCodes'); 35 import('ShortCodes');
36 if (defined('Tz\DEBUG') && Tz\DEBUG === true) { 36 if (defined('Tz\DEBUG') && Tz\DEBUG === true) {
37 import('Debug'); 37 import('Debug');
38 } 38 }
39 39
40 function import($com) { 40 function import($com)
41 $dir = DIR . DIRECTORY_SEPARATOR . 'com' . DIRECTORY_SEPARATOR . $com . DIRECTORY_SEPARATOR; 41 {
42 $file = $dir . $com . '.php'; 42 $dir = DIR.DIRECTORY_SEPARATOR.'com'.DIRECTORY_SEPARATOR.$com.DIRECTORY_SEPARATOR;
43 $file = $dir.$com.'.php';
43 if (is_dir($dir) && is_file($file)) { 44 if (is_dir($dir) && is_file($file)) {
44 require_once($file); 45 require_once($file);
45 Vars::$loaded[$com] = 1; 46 Vars::$loaded[$com] = 1;
46 } 47 }
47 } 48 }
48 49
49 function autoloader($class) { 50 function autoloader($class)
51 {
50 $a = explode('\\', $class); 52 $a = explode('\\', $class);
51 $class = array_pop($a); 53 $class = array_pop($a);
52 54
53 $file = DIR . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php'; 55 $file = DIR.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.$class.'.php';
54 if (is_file($file)) { 56 if (is_file($file)) {
55 include($file); 57 include($file);
56 } 58 }
57 } 59 }
58 60
59 function url($script, $base_file = false) { 61 function url($script, $base_file = false)
60 if (defined(__NAMESPACE__ . '\OVERRIDE')) { 62 {
61 $info = Array(pathinfo(DIR), pathinfo(__DIR__)); 63 if (defined(__NAMESPACE__.'\OVERRIDE')) {
64 $info = [pathinfo(DIR), pathinfo(__DIR__)];
62 65
63 // This should replace link for tz-tools com components 66 // This should replace link for tz-tools com components
64 $base_file = str_replace(__DIR__, DIR, $base_file, $q); 67 $base_file = str_replace(__DIR__, DIR, $base_file, $q);
...@@ -67,33 +70,35 @@ function url($script, $base_file = false) { ...@@ -67,33 +70,35 @@ function url($script, $base_file = false) {
67 $base_file = str_replace($info[1]['dirname'], $info[0]['dirname'], $base_file, $r); 70 $base_file = str_replace($info[1]['dirname'], $info[0]['dirname'], $base_file, $r);
68 71
69 // This should replace for theme files 72 // This should replace for theme files
70 $theme_dir = get_theme_root() . DIRECTORY_SEPARATOR . get_template(); 73 $theme_dir = get_theme_root().DIRECTORY_SEPARATOR.get_template();
71 if (is_link($theme_dir)) { 74 if (is_link($theme_dir)) {
72 $base_file = str_replace(readlink($theme_dir), $theme_dir . DIRECTORY_SEPARATOR, $base_file); 75 $base_file = str_replace(readlink($theme_dir), $theme_dir.DIRECTORY_SEPARATOR, $base_file);
73 } 76 }
74
75 // 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) 77 // 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)
76 } 78 }
77 79
78 $base_dir = (false === $base_file ? DIR : dirname($base_file)); 80 $base_dir = (false === $base_file ? DIR : dirname($base_file));
79 $rel_path = str_replace(ABSPATH, '', $base_dir); 81 $rel_path = str_replace(ABSPATH, '', $base_dir);
80 $script = site_url() . '/' . $rel_path . '/' . $script; 82 $script = site_url().'/'.$rel_path.'/'.$script;
81 83
82 return $script; 84 return $script;
83 } 85 }
84 86
85 function tools_url() { 87 function tools_url()
88 {
86 $args = func_get_args(); 89 $args = func_get_args();
87 call_user_func_array(__NAMESPACE__ . '\url', $args); 90 call_user_func_array(__NAMESPACE__.'\url', $args);
88 } 91 }
89 92
90 /** 93 /**
91 * "Cleans" data by removing double backslashes and escaped quotes. Will run recursively on arrays. 94 * "Cleans" data by removing double backslashes and escaped quotes. Will run recursively on arrays.
92 * 95 *
93 * @param $data array|string Data to clean 96 * @param $data array|string Data to clean
97 *
94 * @return array|string 98 * @return array|string
95 */ 99 */
96 function tzClean (&$data) { 100 function tzClean(&$data)
101 {
97 if (is_array($data)) { 102 if (is_array($data)) {
98 foreach ($data as $index => $child_data) { 103 foreach ($data as $index => $child_data) {
99 tzClean($data[$index]); 104 tzClean($data[$index]);
...@@ -103,8 +108,8 @@ function tzClean (&$data) { ...@@ -103,8 +108,8 @@ function tzClean (&$data) {
103 '\\', 108 '\\',
104 '', 109 '',
105 str_ireplace( 110 str_ireplace(
106 array('\"', "\'"), 111 ['\"', "\'"],
107 array('"', "'"), 112 ['"', "'"],
108 $data 113 $data
109 ) 114 )
110 ); 115 );
...@@ -116,14 +121,16 @@ function tzClean (&$data) { ...@@ -116,14 +121,16 @@ function tzClean (&$data) {
116 /** 121 /**
117 * @returns {WP_User} of the currently logged in user 122 * @returns {WP_User} of the currently logged in user
118 */ 123 */
119 function getCurrentUser() { 124 function getCurrentUser()
125 {
120 global $current_user; 126 global $current_user;
121 get_currentuserinfo(); 127 get_currentuserinfo();
122 128
123 return $current_user; 129 return $current_user;
124 } 130 }
125 131
126 function buffer($callback) { 132 function buffer($callback)
133 {
127 ob_start(); 134 ob_start();
128 call_user_func($callback); 135 call_user_func($callback);
129 $b = ob_get_contents(); 136 $b = ob_get_contents();
...@@ -132,7 +139,8 @@ function buffer($callback) { ...@@ -132,7 +139,8 @@ function buffer($callback) {
132 return $b; 139 return $b;
133 } 140 }
134 141
135 function add_actions($class) { 142 function add_actions($class)
143 {
136 if (!class_exists($class)) { 144 if (!class_exists($class)) {
137 throw new Exception("{$class} does not exist"); 145 throw new Exception("{$class} does not exist");
138 } 146 }
...@@ -140,11 +148,12 @@ function add_actions($class) { ...@@ -140,11 +148,12 @@ function add_actions($class) {
140 $ref = new ReflectionClass($class); 148 $ref = new ReflectionClass($class);
141 $actions = $ref->getMethods(ReflectionMethod::IS_STATIC); 149 $actions = $ref->getMethods(ReflectionMethod::IS_STATIC);
142 foreach ($actions as $action) { 150 foreach ($actions as $action) {
143 add_action($action->name, Array($class, $action->name)); 151 add_action($action->name, [$class, $action->name]);
144 } 152 }
145 } 153 }
146 154
147 function add_filters($class) { 155 function add_filters($class)
156 {
148 if (!class_exists($class)) { 157 if (!class_exists($class)) {
149 throw new Exception("{$class} does not exist"); 158 throw new Exception("{$class} does not exist");
150 } 159 }
...@@ -152,15 +161,17 @@ function add_filters($class) { ...@@ -152,15 +161,17 @@ function add_filters($class) {
152 $ref = new ReflectionClass($class); 161 $ref = new ReflectionClass($class);
153 $methods = $ref->getMethods(ReflectionMethod::IS_STATIC); 162 $methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
154 foreach ($methods as $method) { 163 foreach ($methods as $method) {
155 add_filter($method->name, Array($class, $method->name)); 164 add_filter($method->name, [$class, $method->name]);
156 } 165 }
157 } 166 }
158 167
159 function add_shortcodes($class) { 168 function add_shortcodes($class)
169 {
160 ShortCodes\add_shortcodes($class); 170 ShortCodes\add_shortcodes($class);
161 } 171 }
162 172
163 function add_settings_fields($class, $page = 'general', $section = 'default') { 173 function add_settings_fields($class, $page = 'general', $section = 'default')
174 {
164 if (!class_exists($class)) { 175 if (!class_exists($class)) {
165 throw new Exception("{$class} does not exist"); 176 throw new Exception("{$class} does not exist");
166 } 177 }
...@@ -168,23 +179,43 @@ function add_settings_fields($class, $page = 'general', $section = 'default') { ...@@ -168,23 +179,43 @@ function add_settings_fields($class, $page = 'general', $section = 'default') {
168 $ref = new ReflectionClass($class); 179 $ref = new ReflectionClass($class);
169 $methods = $ref->getMethods(ReflectionMethod::IS_STATIC); 180 $methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
170 foreach ($methods as $method) { 181 foreach ($methods as $method) {
171 add_settings_field($method->name, ucwords(str_replace('_', ' ', $method->name)), Array($class, $method->name), $page, $section); 182 add_settings_field(
183 $method->name,
184 ucwords(str_replace('_', ' ', $method->name)),
185 [$class, $method->name],
186 $page,
187 $section
188 );
172 } 189 }
173 } 190 }
174 191
175 function TzSuperPaginationBar($pages = null, $range = 2, $before = "", $after = "",$show_search = false, $show_advanced = false, $post_type="") { 192 function TzSuperPaginationBar(
193 $posts,
194 $pages,
195 $range = 2,
196 $before = "",
197 $after = "",
198 $show_search = false,
199 $show_advanced = false,
200 $post_type = ""
201 ) {
176 202
177 $bar = '<div class="TzSuperPaginationBar">'; 203 $bar = '<div class="TzSuperPaginationBar">';
178 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>'; } 204 if ($show_search) {
179 if ($show_advanced) { $bar .= '<div class="pagination-advanced"><a href="#" onclick="return false;">Advanced</a></div>'; } 205 $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>';
180 $bar .= '<div class="pagination-paging">'.pagination($pages,$range,$before,$after,false).'</div>'; 206 }
207 if ($show_advanced) {
208 $bar .= '<div class="pagination-advanced"><a href="#" onclick="return false;">Advanced</a></div>';
209 }
210 $bar .= '<div class="pagination-paging">'.pagination($posts, $pages, $range, $before, $after, false).'</div>';
181 $bar .= '<div style="clear:both;"></div>'; 211 $bar .= '<div style="clear:both;"></div>';
182 $bar .= '</div>'; 212 $bar .= '</div>';
183 213
184 return $bar; 214 return $bar;
185 } 215 }
186 216
187 function pagination($pages = null, $range = 2, $before = '', $after = '', $echo = true) { 217 function pagination($posts, $pages, $range = 2, $before = '', $after = '', $echo = true)
218 {
188 219
189 $p = ""; 220 $p = "";
190 221
...@@ -192,38 +223,54 @@ function pagination($pages = null, $range = 2, $before = '', $after = '', $echo ...@@ -192,38 +223,54 @@ function pagination($pages = null, $range = 2, $before = '', $after = '', $echo
192 223
193 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 224 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
194 225
195 if(is_null($pages)) { 226 // if(is_null($pages)) {
196 global $wp_query; 227 // global $wp_query;
197 $pages = $wp_query->max_num_pages; 228 // $pages = $wp_query->max_num_pages;
198 if (!$pages) { 229 // if (!$pages) {
199 $pages = 1; 230 // $pages = 1;
200 } 231 // }
201 } 232 // }
202 233
203 if (1 != $pages) { 234 if (1 != $pages) {
204 $p .= $before; 235 $p .= $before;
205 236
206 if ($paged > 2 && $paged > $range+1 && $showitems < $pages) $p .= "<a href='" . get_pagenum_link(1)."'>&laquo;</a>"; 237 if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) {
207 if ($paged > 1 && $showitems < $pages) $p .= "<a href='" . get_pagenum_link($paged - 1) . "'>&lsaquo;</a>"; 238 $p .= "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
239 }
240 if ($paged > 1 && $showitems < $pages) {
241 $p .= "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";
242 }
208 243
209 for ($i = 1; $i <= $pages; $i++) { 244 for ($i = 1; $i <= $pages; $i++) {
210 if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged-$range-1) || $pages <= $showitems)) { 245 if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
211 $p .= ($paged == $i) ? "<span class='current'>" . $i . "</span>":"<a href='" . get_pagenum_link($i) . "' class='inactive' >" . $i . "</a>"; 246 $p .= ($paged == $i)
247 ? "<span class='current'>".$i."</span>"
248 : "<a href='".get_pagenum_link(
249 $i
250 )."' class='inactive' >".$i."</a>";
212 } 251 }
213 } 252 }
214 253
215 if ($paged < $pages && $showitems < $pages) $p .= "<a href='" . get_pagenum_link($paged + 1) . "'>&rsaquo;</a>"; 254 if ($paged < $pages && $showitems < $pages) {
216 if ($paged < $pages-1 && $paged+$range - 1 < $pages && $showitems < $pages) $p .= "<a href='" . get_pagenum_link($pages) . "'>&raquo;</a>"; 255 $p .= "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";
256 }
257 if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) {
258 $p .= "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
259 }
217 260
218 $p .= $after; 261 $p .= $after;
219 } 262 }
220 263
221 if ($echo) { echo $p; } else { return $p; } 264 if ($echo) {
222 265 echo $p;
223 266 } else {
267 return $p;
268 }
224 } 269 }
225 270
226 class Vars { 271 class Vars
227 public static $loaded = Array(); 272 {
273 public static $loaded = [];
228 } 274 }
275
229 ?> 276 ?>
...\ No newline at end of file ...\ No newline at end of file
......