3c4bb773 by Chris Boden

Implemented namespaces

1 parent 943a8b83
<?php
class Auth {
const REG_METH_AUTO_REG = 1;
const REG_METH_VALID_EMAIL = 2;
const FORGOT_METH_VALID_EMAIL = 1;
const FORGOT_METH_RAND_PASS = 2;
namespace Tz\WordPress\Tools\Auth;
const ACTION_ACTIVATE = 'activate_account';
use \Exception, \LogicException, \InvalidArgumentException, \BadMethodCallException;
public static function make() {
static $made = false;
if (true === $made) {
throw new Exception('Auth has already been instantiated');
}
$made = true;
const REG_METH_AUTO_REG = 1;
const REG_METH_VALID_EMAIL = 2;
// if _GET activate self::activate();
}
const FORGOT_METH_VALID_EMAIL = 1;
const FORGOT_METH_RAND_PASS = 2;
/**
* Attempts to login the user
* @param {String} $username
* @param {String} $password
* @param {Boolean} $remember
* @returns WP_User instance
* @throws LogicException If headers have already been passed
* @throws InvalidArgumentException If the authentication is invalid
*/
public static function login($username, $password, $remember = true) {
if (headers_sent()) {
throw new LogicException('Unable to login because headers have been sent');
}
const ACTION_ACTIVATE = 'activate_account';
$auth = _signon(Array(
'user_login' => esc_sql($username)
, 'user_password' => esc_sql($password)
, 'remember' => $remember
));
/**
* Attempts to login the user
* @param {String} $username
* @param {String} $password
* @param {Boolean} $remember
* @returns WP_User instance
* @throws LogicException If headers have already been passed
* @throws InvalidArgumentException If the authentication is invalid
*/
function login($username, $password, $remember = true) {
if (headers_sent()) {
throw new LogicException('Unable to login because headers have been sent');
}
$ref = new ReflectionObject($auth);
if ($ref->name == 'WP_User') {
return $auth;
}
$auth = _signon(Array(
'user_login' => esc_sql($username)
, 'user_password' => esc_sql($password)
, 'remember' => $remember
));
throw new InvalidArgumentException('Invalid username/password');
//$auth->get_error_message()); this would be nice except it links to a wp-page
if (get_class($auth) == 'WP_User') {
return $auth;
}
/**
* Attempts to log the user out
* @returns Boolean
* @throws LogicException If HTTP headers have already been sent
*/
public static function logout() {
if (headers_sent()) {
throw new LogicException('Unable to logout because headers have been sent');
}
_logout();
throw new InvalidArgumentException('Invalid username/password');
//$auth->get_error_message()); this would be nice except it links to a wp-page
}
return true;
/**
* Attempts to log the user out
* @returns Boolean
* @throws LogicException If HTTP headers have already been sent
*/
function logout() {
if (headers_sent()) {
throw new LogicException('Unable to logout because headers have been sent');
}
public static function register($user_data = Array(), $registration_method) {
require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
$valid = new Auth_Validation($user_data);
if (count($valid->errors) > 0) {
throw new BadMethodCallException(implode("\n", $valid->errors));
}
_logout();
array_filter($user_data, 'esc_sql');
$id = (int)_insert_user($user_data);
return true;
}
global $wpdb;
$wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}");
function register($user_data = Array(), $registration_method) {
require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
return $id;
$valid = new Validation($user_data);
if (count($valid->errors) > 0) {
throw new BadMethodCallException(implode("\n", $valid->errors));
}
public static function activate($username, $activation_key) {
do_action(self::ACTION_ACTIVATE, $user_id);
}
array_filter($user_data, 'esc_sql');
$id = (int)_insert_user($user_data);
public static function forgot_password($username, $forgot_method) {
}
global $wpdb;
$wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}");
return $id;
}
function activate($username, $activation_key) {
do_action(ACTION_ACTIVATE, $user_id);
}
function forgot_password($username, $forgot_method) {
}
class Auth_Validation extends Validation {
class Validation extends \Tz\Validation {
/**
* @rule Not blank
* @rule Valid WordPress username
......
<?php
class Branding {
public static function make() {
add_actions('Branding_Actions');
}
}
class Branding_Actions {
namespace Tz\WordPress\Tools\Branding;
use Tz\WordPress\Tools;
class Actions {
public static function admin_print_styles() {
_enqueue_style('branding-style', TzTools::tools_url('css/tenzing.css', __FILE__));
_enqueue_style('branding-style', Tools\url('css/tenzing.css', __FILE__));
}
public static function admin_head() {
......@@ -22,10 +20,9 @@ class Branding_Actions {
}
public static function login_head() {
echo '<link rel="stylesheet" type="text/css" href="' . TzTools::tools_url('css/tz_login.css', __FILE__) . '" />';
echo '<link rel="stylesheet" type="text/css" href="' . Tools\url('css/tz_login.css', __FILE__) . '" />';
}
}
Branding::make();
Tools\add_actions(__NAMESPACE__ . '\Actions');
?>
\ No newline at end of file
......
<?php
class ClientSettings {
const CAPABILITY = 'edit_client_settings';
const ADMIN_PAGE = 'client-settings';
public static function make() {
static $made = false;
if ($made) {
throw new OverflowException('ClientSettings has already been initialized');
}
$made = true;
namespace Tz\WordPress\Tools\ClientSettings;
$role = get_role('administrator');
$role->add_cap(self::CAPABILITY);
use Tz\WordPress\Tools;
add_actions('ClientSettings_Actions');
}
const CAPABILITY = 'edit_client_settings';
const ADMIN_PAGE = 'client-settings';
public static function viewOptionsPage() {
}
$role = get_role('administrator');
$role->add_cap(CAPABILITY);
Tools\add_actions(__NAMESPACE__ . '\Actions');
function viewOptionsPage() {
}
// register_setting()
......@@ -29,13 +24,11 @@ class ClientSettings {
// do_settings_section()
class ClientSettings_Actions {
class Actions {
public static function admin_menu() {
$display = (current_user_can('manage_options') ? 'Client Settings' : 'Settings');
add_utility_page($display, $display, ClientSettings::CAPABILITY, ClientSettings::ADMIN_PAGE, Array('ClientSettings', 'viewOptionsPage'));
add_utility_page($display, $display, CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\viewOptionsPage');
}
}
ClientSettings::make();
?>
\ No newline at end of file
......
<?php
namespace Tz\WordPress\Tools\PagePermissions;
use \WP_Roles;
use \UnderflowException;
if (!isset($selected)) {
throw new UnderflowException('Current Page Permission settings not provided');
}
$opt_selected = $selected[PagePermissions::ELE_SEL];
$opt_selected = $selected[ELE_SEL];
?>
<select id="<?php echo PagePermissions::ELE_SEL; ?>" name="<?php echo PagePermissions::ELE_SEL; ?>">
<option value="<?php echo PagePermissions::OPT_ALL . '"' . (PagePermissions::OPT_ALL == $opt_selected ? ' selected' : ''); ?>>Anyone</option>
<option value="<?php echo PagePermissions::OPT_AUTH . '"' . (PagePermissions::OPT_AUTH == $opt_selected ? ' selected' : ''); ?>>Must be Logged In</option>
<option value="<?php echo PagePermissions::OPT_CUST . '"' . (PagePermissions::OPT_CUST == $opt_selected ? ' selected' : ''); ?>>Specific Roles</option>
<select id="<?php echo ELE_SEL; ?>" name="<?php echo ELE_SEL; ?>">
<option value="<?php echo OPT_ALL . '"' . (OPT_ALL == $opt_selected ? ' selected' : ''); ?>>Anyone</option>
<option value="<?php echo OPT_AUTH . '"' . (OPT_AUTH == $opt_selected ? ' selected' : ''); ?>>Must be Logged In</option>
<option value="<?php echo OPT_CUST . '"' . (OPT_CUST == $opt_selected ? ' selected' : ''); ?>>Specific Roles</option>
</select>
<div id="TzSpecific">
......@@ -20,8 +26,8 @@
unset($rc, $roles['administrator']);
foreach ($roles as $key => $display) {
$checked = (isset($selected[PagePermissions::ELE_CUST][$key]) ? ' checked' : '');
echo '<br /><input type="checkbox" id="' . $key . '" name="' . PagePermissions::ELE_CUST . '[' . $key . ']" value="1"' . $checked . ' />';
$checked = (isset($selected[ELE_CUST][$key]) ? ' checked' : '');
echo '<br /><input type="checkbox" id="' . $key . '" name="' . ELE_CUST . '[' . $key . ']" value="1"' . $checked . ' />';
echo '<label for="' . $key . '">' . $display . '</label>';
}
?>
......
<?php require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'form.php'); ?>
\ No newline at end of file
<?php require(__DIR__ . DIRECTORY_SEPARATOR . 'form.php'); ?>
\ No newline at end of file
......
<?php /*
<?php
namespace Tz\WordPress\Tools\PagePermissions;
use Tz\WordPress\Tools\PagePermissions\Admin;
/*
Idea: Checkbox beside each textarea with option to include registration forum or not
*/ ?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Permission Defaults</h2>
<form method="post">
<input type="hidden" name="<?php echo PagePermissionsAdmin::SUBMIT_HOOK; ?>" value="1" />
<input type="hidden" name="<?php echo Admin\SUBMIT_HOOK; ?>" value="1" />
<h3>Default Option</h3>
<?php require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'form.php'); ?>
<?php require(__DIR__ . DIRECTORY_SEPARATOR . 'form.php'); ?>
<hr />
......@@ -19,18 +24,18 @@ Idea: Checkbox beside each textarea with option to include registration forum or
<h6>(Messages are displayed for given criteria when page permission is not met)</h6>
<p>
<label for="<?php echo PagePermissions::ELE_AUTH; ?>" />"Must be Logged In" message for un-authenticated visitors</label>
<br /><textarea id="<?php echo PagePermissions::ELE_AUTH; ?>" name="<?php echo PagePermissions::ELE_AUTH; ?>"><?php echo $selected[PagePermissions::ELE_AUTH]; ?></textarea>
<label for="<?php echo ELE_AUTH; ?>" />"Must be Logged In" message for un-authenticated visitors</label>
<br /><textarea id="<?php echo ELE_AUTH; ?>" name="<?php echo ELE_AUTH; ?>"><?php echo $selected[ELE_AUTH]; ?></textarea>
</p>
<p>
<label for="<?php echo PagePermissions::ELE_CUST_AUTH; ?>" />"Only Specific Roles" message for un-authenticated visitors</label>
<br /><textarea id="<?php echo PagePermissions::ELE_CUST_AUTH; ?>" name="<?php echo PagePermissions::ELE_CUST_AUTH; ?>"><?php echo $selected[PagePermissions::ELE_CUST_AUTH]; ?></textarea>
<label for="<?php echo ELE_CUST_AUTH; ?>" />"Only Specific Roles" message for un-authenticated visitors</label>
<br /><textarea id="<?php echo ELE_CUST_AUTH; ?>" name="<?php echo ELE_CUST_AUTH; ?>"><?php echo $selected[ELE_CUST_AUTH]; ?></textarea>
</p>
<p>
<label for="<?php echo PagePermissions::ELE_DENIED; ?>" />"Only Specific Roles" message for authenticated users without sufficient privelages</label>
<br /><textarea id="<?php echo PagePermissions::ELE_DENIED; ?>" name="<?php echo PagePermissions::ELE_DENIED; ?>"><?php echo $selected[PagePermissions::ELE_DENIED]; ?></textarea>
<label for="<?php echo ELE_DENIED; ?>" />"Only Specific Roles" message for authenticated users without sufficient privelages</label>
<br /><textarea id="<?php echo ELE_DENIED; ?>" name="<?php echo ELE_DENIED; ?>"><?php echo $selected[ELE_DENIED]; ?></textarea>
</p>
<p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p>
......
<?php
class ShortCodes extends Singleton {
private static $registered = Array();
private static $private = Array();
public static function make() {
add_actions('ShortCodes_Actions');
}
namespace Tz\WordPress\Tools\ShortCodes;
public static function registerClass($class) {
if (!class_exists($class)) {
throw new Exception("$class does not exist");
}
use Tz\WordPress\Tools;
$ref = new ReflectionClass($class);
$methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
foreach ($methods as $method) {
self::add($method->name, Array($class, $method->name));
}
}
use \ReflectionClass, \ReflectionMethod, \ReflectionFunction;
use \Exception;
public static function add($code, $callback) {
$fn = (function_exists('wp_add_shortcode') ? 'wp_add_shortcode' : 'add_shortcode');
call_user_func($fn, $code, $callback);
Tools\add_actions(__NAMESPACE__ . '\Actions');
if (is_admin()) {
self::$registered[$code] = Array('code' => $code, 'title' => $code, 'params' => Array(), 'uses_content' => 0);
if (function_exists('rename_function')) {
rename_function('add_shortcode', 'wp_add_shortcode');
if (is_array($callback)) {
$ref = new ReflectionMethod($callback[0], $callback[1]);
} else {
$ref = new ReflectionFunction($callback);
}
$api = $ref->getDocComment();
$api = explode("\n", $api);
array_shift($api);
array_pop($api);
foreach ($api as $key => &$comment) {
$comment = trim($comment, ' *');
if (substr($comment, 0, 1) == '@') {
$tag = trim(substr($comment, 1, strpos($comment, ' ')));
if (empty($tag)) {
$tag = trim($comment, '@');
}
if (method_exists(__CLASS__, 'parseTag_' . $tag)) {
call_user_func_array(Array(__CLASS__, 'parseTag_' . $tag), Array($code, $comment));
}
}
}
function add_shortcode() {
$args = func_get_args();
call_user_func_array('add', $args);
}
}
public static function parseTag_display($code, $string) {
$string = trim(str_replace('@display', '', $string));
self::$registered[$code]['title'] = $string;
function add_shortcodes($class) {
if (!class_exists($class)) {
throw new Exception("$class does not exist");
}
public static function parseTag_param($code, $string) {
$regex = '.*?@param {((?:[a-z][a-z]+))(\\(.*?\\))?} (.*?) (.*?)$'; // Awww yeah!
if ($num = preg_match_all("/" . $regex . "/is", $string, $matches)) {
self::$registered[$code]['params'][] = Array('name' => $matches[3][0], 'type' => $matches[1][0], 'options' => explode(',', trim($matches[2][0], ')(')), 'desc' => $matches[4][0]);
}
$ref = new ReflectionClass($class);
$methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
foreach ($methods as $method) {
add($method->name, Array($class, $method->name));
}
}
function registerClass($class) {
call_user_func('add_shortcodes', $class);
}
function add($code, $callback) {
$fn = (function_exists('wp_add_shortcode') ? 'wp_add_shortcode' : 'add_shortcode');
call_user_func($fn, $code, $callback);
if (is_admin()) { // I may want to remove this condition...
Vars::$registered[$code] = Array('code' => $code, 'title' => $code, 'params' => Array(), 'uses_content' => 0);
public static function parseTag_private($code, $string) {
self::$private[$code] = 1;
if (is_array($callback)) {
$ref = new ReflectionMethod($callback[0], $callback[1]);
} else {
$ref = new ReflectionFunction($callback);
}
$api = $ref->getDocComment();
$api = explode("\n", $api);
array_shift($api);
array_pop($api);
foreach ($api as $key => &$comment) {
$comment = trim($comment, ' *');
if (substr($comment, 0, 1) == '@') {
$tag = trim(substr($comment, 1, strpos($comment, ' ')));
if (empty($tag)) {
$tag = trim($comment, '@');
}
if (function_exists('parseTag_' . $tag)) {
call_user_func_array('parseTag_' . $tag, Array($code, $comment));
}
}
}
}
}
function parseTag_display($code, $string) {
$string = trim(str_replace('@display', '', $string));
Vars::$registered[$code]['title'] = $string;
}
public static function uses_content($code) {
self::$registered[$code]['uses_content'] = 1;
function parseTag_param($code, $string) {
$regex = '.*?@param {((?:[a-z][a-z]+))(\\(.*?\\))?} (.*?) (.*?)$'; // Awww yeah!
if ($num = preg_match_all("/" . $regex . "/is", $string, $matches)) {
Vars::$registered[$code]['params'][] = Array('name' => $matches[3][0], 'type' => $matches[1][0], 'options' => explode(',', trim($matches[2][0], ')(')), 'desc' => $matches[4][0]);
}
}
public static function getRegistered() {
$return = self::$registered;
foreach (self::$private as $key => $one) {
unset($return[$key]);
}
function parseTag_private($code, $string) {
Vars::$private[$code] = 1;
}
function uses_content($code) {
Vars::$registered[$code]['uses_content'] = 1;
}
return $return;
function getRegistered() {
$return = Vars::$registered;
foreach (Vars::$private as $key => $one) {
unset($return[$key]);
}
public static function drawMetaBox() {
return $return;
}
function drawMetaBox() {
?>
<label for="TzShortCodeList">Tag:</label>
<select id="TzShortCodeList" name="TzShortCodeList">
<?php
$options = ShortCodes::getRegistered();
$options = getRegistered();
ksort($options);
foreach ($options as $tag => $data) {
echo '<option value="' . $tag . '">' . $data['title'] . '</option>';
......@@ -99,42 +114,27 @@ class ShortCodes extends Singleton {
<input type="button" id="TzInsertSC" value="<?php _e('Insert into post'); ?>" />
</p>
<?php
}
}
class ShortCodes_Actions {
class Actions {
public static function admin_menu() {
add_meta_box('TzShortCodes', 'Code Helper', Array('ShortCodes', 'drawMetaBox'), 'post', 'normal');
add_meta_box('TzShortCodes', 'Code Helper', Array('ShortCodes', 'drawMetaBox'), 'page', 'normal');
add_meta_box('TzShortCodes', 'Code Helper', __NAMESPACE__ . '\drawMetaBox', 'post', 'normal');
add_meta_box('TzShortCodes', 'Code Helper', __NAMESPACE__ . '\drawMetaBox', 'page', 'normal');
}
public static function admin_print_scripts() {
if ($GLOBALS['editing']) {
_enqueue_script('shortcoder', TzTools::tools_url('shortcoder.js', __FILE__), Array('jquery'));
_enqueue_script('shortcoder', Tools\url('shortcoder.js', __FILE__), Array('jquery'));
echo "<script type=\"text/javascript\">\n/* <![CDATA[ */\n";
echo 'var TzRegisteredShortCodes = ' . json_encode(ShortCodes::getRegistered());
echo 'var TzRegisteredShortCodes = ' . json_encode(getRegistered());
echo "\n/* ]]> */</script>\n";
}
}
}
/**
* @deprecated
*/
function add_shortcodes($class) {
call_user_func(Array('ShortCodes', 'registerClass'), $class);
class Vars {
public static $registered = Array();
public static $private = Array();
}
if (function_exists('rename_function')) {
rename_function('add_shortcode', 'wp_add_shortcode');
function add_shortcode() {
$args = func_get_args();
call_user_func_array(Array('ShortCodes', 'add'), $args);
}
}
ShortCodes::make();
?>
\ No newline at end of file
......
<?php
namespace Tz;
use Exception;
/**
* Handles data preparation for XMLHTTP responses to xmlhttpHandler script
*
......
<?php
namespace Tz;
use \Exception;
abstract class Validation {
/**
* Associative array of valid fields
......@@ -42,4 +47,4 @@ abstract class Validation {
}
}
}
?>
?>
\ No newline at end of file
......
......@@ -62,4 +62,4 @@ echo "Here\n";
print_r($this->_data);
}
}
?>
?>
\ No newline at end of file
......
<?php
/*
Plugin Name: Tenzing Tools
Version: 0.3b
Version: 0.4b
Description: Various classes and functions to help out with stuff
Author: Tenzing
*/
TzTools::make();
namespace Tz\WordPress\Tools;
class TzTools {
public static function make() {
spl_autoload_register(Array(__CLASS__, 'autoloader'));
use \ReflectionClass, \ReflectionMethod;
use \Exception;
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'wp_functions.php');
spl_autoload_register(__NAMESPACE__ . '\autoloader');
_register_script('addEvent', self::tools_url('scripts/addEvent.js', __FILE__));
_register_script('xmlhttpHandler', self::tools_url('scripts/xmlhttpHandler.js', __FILE__));
_register_script('fireEvent', self::tools_url('scripts/fireEvent.js', __FILE__));
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'wp_functions.php');
// This is (hopefully) getting canned in 3.0
add_action('widgets_init', Array('MenuWidget', 'init'));
_register_script('addEvent', url('scripts/addEvent.js', __FILE__));
_register_script('xmlhttpHandler', url('scripts/xmlhttpHandler.js', __FILE__));
_register_script('fireEvent', url('scripts/fireEvent.js', __FILE__));
self::import('ShortCodes');
if (defined('TZ_DEBUG') && TZ_DEBUG === true) {
self::import('Debug');
}
import('ShortCodes');
if (defined('TZ_DEBUG') && TZ_DEBUG === true) {
import('Debug');
}
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);
}
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);
}
}
public static function autoloader($class) {
$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php';
if (is_file($file)) {
include($file);
}
}
function autoloader($class) {
public static function tools_url($script, $base_file = false) {
$base_dir = (false === $base_file ? dirname(__FILE__) : dirname($base_file));
$rel_path = str_replace(ABSPATH, '', $base_dir);
$script = site_url() . '/' . $rel_path . '/' . $script;
$a = explode('\\', $class);
$class = array_pop($a);
return $script;
$file = __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php';
if (is_file($file)) {
include($file);
}
}
function url($script, $base_file = false) {
$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);
}
function add_actions($class) {
if (!class_exists($class)) {
throw new Exception("$class does not exist");
......
<?php
/**
* WordPress strongly advises against using functions that start with "wp_"
* as they are reserved for the core of WordPress and can change with any
......