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
namespace Tz\WordPress\Tools;
use Tz\WordPress\Tools\ShortCodes;
use ReflectionClass, ReflectionMethod;
use Exception;
use ReflectionClass;
use ReflectionMethod;
use Tz\WordPress\Tools\ShortCodes;
spl_autoload_register(__NAMESPACE__ . '\autoloader');
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__));
// 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');
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));
_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('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';
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) {
function autoloader($class)
{
$a = explode('\\', $class);
$class = array_pop($a);
$file = DIR . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php';
$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__));
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);
......@@ -67,33 +70,35 @@ function url($script, $base_file = false) {
$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();
$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);
$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;
$script = site_url().'/'.$rel_path.'/'.$script;
return $script;
}
function tools_url() {
function tools_url()
{
$args = func_get_args();
call_user_func_array(__NAMESPACE__ . '\url', $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) {
function tzClean(&$data)
{
if (is_array($data)) {
foreach ($data as $index => $child_data) {
tzClean($data[$index]);
......@@ -103,8 +108,8 @@ function tzClean (&$data) {
'\\',
'',
str_ireplace(
array('\"', "\'"),
array('"', "'"),
['\"', "\'"],
['"', "'"],
$data
)
);
......@@ -116,14 +121,16 @@ function tzClean (&$data) {
/**
* @returns {WP_User} of the currently logged in user
*/
function getCurrentUser() {
function getCurrentUser()
{
global $current_user;
get_currentuserinfo();
return $current_user;
}
function buffer($callback) {
function buffer($callback)
{
ob_start();
call_user_func($callback);
$b = ob_get_contents();
......@@ -132,7 +139,8 @@ function buffer($callback) {
return $b;
}
function add_actions($class) {
function add_actions($class)
{
if (!class_exists($class)) {
throw new Exception("{$class} does not exist");
}
......@@ -140,11 +148,12 @@ function add_actions($class) {
$ref = new ReflectionClass($class);
$actions = $ref->getMethods(ReflectionMethod::IS_STATIC);
foreach ($actions as $action) {
add_action($action->name, Array($class, $action->name));
add_action($action->name, [$class, $action->name]);
}
}
function add_filters($class) {
function add_filters($class)
{
if (!class_exists($class)) {
throw new Exception("{$class} does not exist");
}
......@@ -152,15 +161,17 @@ function add_filters($class) {
$ref = new ReflectionClass($class);
$methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
foreach ($methods as $method) {
add_filter($method->name, Array($class, $method->name));
add_filter($method->name, [$class, $method->name]);
}
}
function add_shortcodes($class) {
function add_shortcodes($class)
{
ShortCodes\add_shortcodes($class);
}
function add_settings_fields($class, $page = 'general', $section = 'default') {
function add_settings_fields($class, $page = 'general', $section = 'default')
{
if (!class_exists($class)) {
throw new Exception("{$class} does not exist");
}
......@@ -168,23 +179,43 @@ function add_settings_fields($class, $page = 'general', $section = 'default') {
$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);
add_settings_field(
$method->name,
ucwords(str_replace('_', ' ', $method->name)),
[$class, $method->name],
$page,
$section
);
}
}
function TzSuperPaginationBar($pages = null, $range = 2, $before = "", $after = "",$show_search = false, $show_advanced = false, $post_type="") {
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($pages,$range,$before,$after,false).'</div>';
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($pages = null, $range = 2, $before = '', $after = '', $echo = true) {
function pagination($posts, $pages, $range = 2, $before = '', $after = '', $echo = true)
{
$p = "";
......@@ -192,38 +223,54 @@ function pagination($pages = null, $range = 2, $before = '', $after = '', $echo
$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(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)."'>&laquo;</a>";
if ($paged > 1 && $showitems < $pages) $p .= "<a href='" . get_pagenum_link($paged - 1) . "'>&lsaquo;</a>";
if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) {
$p .= "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
}
if ($paged > 1 && $showitems < $pages) {
$p .= "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</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 (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) . "'>&rsaquo;</a>";
if ($paged < $pages-1 && $paged+$range - 1 < $pages && $showitems < $pages) $p .= "<a href='" . get_pagenum_link($pages) . "'>&raquo;</a>";
if ($paged < $pages && $showitems < $pages) {
$p .= "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";
}
if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) {
$p .= "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
}
$p .= $after;
}
if ($echo) { echo $p; } else { return $p; }
if ($echo) {
echo $p;
} else {
return $p;
}
}
class Vars {
public static $loaded = Array();
class Vars
{
public static $loaded = [];
}
?>
\ No newline at end of file
......