tz-tools.php 7.21 KB
<?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');

// 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)."'>&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 ($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;
    }
}

class Vars
{
    public static $loaded = [];
}

?>