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 Tz\WordPress\Tools, Tz\WordPress\Tools\ClientSettings;
use \ReflectionClass, \ReflectionException;
use \WP_Option;
use \WP_User;
/**
* Public API
* The name of the custom field stored in a post/page
* @type String
*/
class PagePermissions {
/**
* The name of the custom field stored in a post/page
* @type String
*/
const META = 'accessible_to_roles';
const OPT = '';
const META = 'accessible_to_roles';
const OPT = '';
const ELE_SEL = 'general_access';
const ELE_CUST = 'roles';
const ELE_AUTH = 'message_auth';
const ELE_CUST_AUTH = 'message_cust_auth';
const ELE_DENIED = 'message_cust_denied';
const ELE_SEL = 'general_access';
const ELE_CUST = 'roles';
const ELE_AUTH = 'message_auth';
const ELE_CUST_AUTH = 'message_cust_auth';
const ELE_DENIED = 'message_cust_denied';
/**
* Lookup value for ELE_SEL for all users
* @type Integer
*/
const OPT_ALL = 0;
/**
* Lookup value for ELE_SEL for login required
* @type Integer
*/
const OPT_AUTH = 1;
/**
* Lookup value for ELE_SEL for custom roles
* @type Integer
*/
const OPT_CUST = 2;
/**
* Lookup value for ELE_SEL for all users
* @type Integer
*/
const OPT_ALL = 0;
/**
* Lookup value for ELE_SEL for login required
* @type Integer
*/
const OPT_AUTH = 1;
/**
* Lookup value for ELE_SEL for custom roles
* @type Integer
*/
const OPT_CUST = 2;
class Vars {
/**
* WP current user data
* @type Array
*/
private static $current_user = false;
public static $current_user = false;
}
public static function init() {
if (false !== self::$current_user) {
throw new OverflowException('PagePermissions already initialized');
}
function make() {
Vars::$current_user = _get_current_user();
}
self::$current_user = _get_current_user();
}
function initAjax() {
$selected = unserialize($_POST['string_value']);
include(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
}
public static function initAjax() {
$selected = unserialize($_POST['string_value']);
include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
/**
* The key function in all of this; called by the Theme,
* this determines if the user is able to view the page.
* @param {Integer} $post_id
* @returns {Boolean|String} true if user can view, error message if not
* @throw InvalidArgumentException
*/
function current_user_can_view($post_id = false) {
static $settings = false;
if (false === $settings) {
$settings = new WP_Option(SETTING_NS);
}
/**
* The key function in all of this; called by the Theme,
* this determines if the user is able to view the page.
* @param {Integer} $post_id
* @returns {Boolean|String} true if user can view, error message if not
* @throw InvalidArgumentException
*/
public static function current_user_can_view($post_id = false) {
static $settings = false;
if (false === $settings) {
$settings = new WP_Option(PagePermissionsAdmin::SETTING_NS);
}
if (false === $post_id) {
global $post;
$post_id = $post->ID;
}
if (false === $post_id) {
global $post;
$post_id = $post->ID;
}
// Meta value hasn't been set, getting settings defaults
if (NULL === $data = array_shift(get_post_meta($post_id, META))) {
$data = Array(ELE_SEL => $settings[ELE_SEL], ELE_CUST => $settings[ELE_CUST]);
}
// Meta value hasn't been set, getting settings defaults
if (NULL === $data = array_shift(get_post_meta($post_id, self::META))) {
$data = Array(self::ELE_SEL => $settings[self::ELE_SEL], self::ELE_CUST => $settings[self::ELE_CUST]);
}
// Anyone has access, God has no limitations
if ($data[ELE_SEL] == OPT_ALL || is_admin()) {
return true;
}
// Anyone has access, God has no limitations
if ($data[self::ELE_SEL] == self::OPT_ALL || self::is_admin()) {
// Login required
if ($data[ELE_SEL] == OPT_AUTH) {
// User is logged in
if (is_user_logged_in()) {
return true;
}
// Login required
if ($data[self::ELE_SEL] == self::OPT_AUTH) {
// User is logged in
if (is_user_logged_in()) {
return true;
}
// Not logged in; return "login required" message
return $settings[self::ELE_AUTH];
}
// Not logged in; return "login required" message
return $settings[self::ELE_AUTH];
// Specific role required
if ($data[ELE_SEL] == OPT_CUST) {
// User isn't even logged in; send message
if (!is_user_logged_in()) {
return $settings[ELE_CUST_AUTH];
}
// Specific role required
if ($data[self::ELE_SEL] == self::OPT_CUST) {
// User isn't even logged in; send message
if (!is_user_logged_in()) {
return $settings[self::ELE_CUST_AUTH];
}
// User meets role required
if (isset($data[self::ELE_CUST][self::get_user_role()])) {
return true;
}
// User is logged in, but doesn't have sufficient privileges, return message
return $settings[self::ELE_DENIED];
// User meets role required
if (isset($data[ELE_CUST][get_user_role()])) {
return true;
}
// This shouldn't happend; but just in case
return 'An unknown permission error has occurred';
// User is logged in, but doesn't have sufficient privileges, return message
return $settings[ELE_DENIED];
}
/**
* @param {Integer|String} $user Username or ID of user to lookup (or false for current user)
* @returns {String} $role The key of the users' role
*/
public static function get_user_role($user = false) {
if (false === $user) {
$user_data = self::$current_user;
} else {
$user_data = new WP_User($user);
}
// or should I throw an exception?
if ($user_data->ID == 0) {
return '';
}
$user_roles = $user_data->roles;
$user_role = array_shift($user_roles);
// This shouldn't happend; but just in case
return 'An unknown permission error has occurred';
}
return $user_role;
/**
* @param {Integer|String} $user Username or ID of user to lookup (or false for current user)
* @returns {String} $role The key of the users' role
*/
function get_user_role($user = false) {
if (false === $user) {
$user_data = Vars::$current_user;
} else {
$user_data = new WP_User($user);
}
/**
* Determine if a user is a site administrator
* @param {Integer|String} $user Username or ID of user to lookup (or false for current user)
* @returns {Boolean}
*/
public static function is_admin($user = false) {
return (self::get_user_role($user) == 'administrator' ? true : false);
// or should I throw an exception?
if ($user_data->ID == 0) {
return '';
}
/**
* Get a lookup of all the forum elements
* @returns {Array} An associative array of the forum elemnts name/values
*/
public static function getFieldNames() {
static $fields = false;
if (false !== $fields) {
return $fields;
}
$user_roles = $user_data->roles;
$user_role = array_shift($user_roles);
$fields = Array();
$ref = new ReflectionClass(__CLASS__);
$consts = $ref->getConstants();
foreach ($consts as $const => $value) {
if (substr($const, 0, 4) == 'ELE_') {
$fields[$const] = $value;
}
}
return $user_role;
}
return $fields;
}
/**
* Determine if a user is a site administrator
* @param {Integer|String} $user Username or ID of user to lookup (or false for current user)
* @returns {Boolean}
*/
function is_admin($user = false) {
return (get_user_role($user) == 'administrator' ? true : false);
}
/**
* Aministration control
* Get a lookup of all the forum elements
* @returns {Array} An associative array of the forum elemnts name/values
*/
class PagePermissionsAdmin {
const CAPABILITY = 'manage_page_permissions';
const ADMIN_PAGE = 'page-permission-settings';
const SUBMIT_HOOK = 'update_def_page_permissions';
const SETTING_NS = 'page_permission_defaults';
public static function make() {
static $made = false;
if ($made) {
throw new OverflowException('make has already beed called');
function getFieldNames() {
static $fields = false;
if (false !== $fields) {
return $fields;
}
$fields = Array();
/*
$ref = new ReflectionClass(__CLASS__);
$consts = $ref->getConstants();
*/
// Need to do this since 5.3, namespace instead of Class, can't reflect namespaces
$consts = Array(
'ELE_SEL' => 'general_access'
, 'ELE_CUST' => 'roles'
, 'ELE_AUTH' => 'message_auth'
, 'ELE_CUST_AUTH' => 'message_cust_auth'
, 'ELE_DENIED' => 'message_cust_denied'
);
foreach ($consts as $const => $value) {
if (substr($const, 0, 4) == 'ELE_') {
$fields[$const] = $value;
}
$made = true;
}
TzTools::import('ClientSettings');
return $fields;
}
$role = get_role('administrator');
$role->add_cap(self::CAPABILITY);
namespace Tz\WordPress\Tools\PagePermissions\Admin;
add_filters('PagePermissionsAdmin_Filters');
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\PagePermissions;
use Tz\WordPress\Tools\ClientSettings;
if (isset($_POST[self::SUBMIT_HOOK]) && current_user_can(self::CAPABILITY)) {
self::submit();
}
use \WP_Option;
add_actions('PagePermissions_Actions');
}
const CAPABILITY = 'manage_page_permissions';
const ADMIN_PAGE = 'page-permission-settings';
const SUBMIT_HOOK = 'update_def_page_permissions';
const SETTING_NS = 'page_permission_defaults';
function make() {
Tools\import('ClientSettings');
$role = get_role('administrator');
$role->add_cap(CAPABILITY);
public static function viewOptionsPage() {
$selected = self::getOptions();
Tools\add_filters(__NAMESPACE__ . '\Filters');
include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'settings.php');
if (isset($_POST[SUBMIT_HOOK]) && current_user_can(CAPABILITY)) {
submit();
}
public static function viewMetaBox($post, $box_info) {
$selected = ($post->ID == 0 ? self::getOptions() : array_shift(get_post_meta($post->ID, PagePermissions::META)));
Tools\add_actions(__NAMESPACE__ . '\Actions');
}
// If the post doesn't have the field saved get defaults
if (is_null($selected)) {
$selected = self::getOptions();
}
function viewOptionsPage() {
$selected = getOptions();
include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
}
include(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'settings.php');
}
/**
* Handles saving data when a post/page is saved
*/
public static function submit() {
unset($_POST[self::SUBMIT_HOOK]);
$options = self::getOptions();
$fields = PagePermissions::getFieldNames();
foreach ($fields as $field) {
if (isset($_POST[$field])) {
// This should probably be done via a recursive fn call or array_walk or something
if (is_array($_POST[$field])) {
$options[$field] = Array();
foreach ($_POST[$field] as $key => $val) {
$options[$field][$key] = stripslashes($_POST[$field]);
}
} else {
// not sure if stripslashes should go here or in WP_Options
$options[$field] = stripslashes($_POST[$field]);
}
} else {
$options[$field] = '';
}
}
function viewMetaBox($post, $box_info) {
$selected = ($post->ID == 0 ? getOptions() : array_shift(get_post_meta($post->ID, PagePermissions\META)));
$options->save();
// If the post doesn't have the field saved get defaults
if (is_null($selected)) {
$selected = getOptions();
}
public static function getOptions() {
static $options = false;
if (false !== $options) {
return $options;
include(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
}
/**
* Handles saving data when a post/page is saved
*/
function submit() {
unset($_POST[SUBMIT_HOOK]);
$options = getOptions();
$fields = PagePermissions\getFieldNames();
foreach ($fields as $field) {
if (isset($_POST[$field])) {
$options[$field] = array_stripslashes($_POST[$field]);
} else {
$options[$field] = '';
}
}
$options->save();
}
function array_stripslashes($value) {
return (is_array($value) ? array_map(__FUNCTION__, $value) : stripslashes($value));
}
$options = new WP_Option(self::SETTING_NS);
function getOptions() {
static $options = false;
if (false !== $options) {
return $options;
}
$options = new WP_Option(SETTING_NS);
return $options;
}
/**
* Each method is a handler for it's WordPress `add_action` namesake
*/
class PagePermissions_Actions {
class Actions {
public static function init() {
register_taxonomy(PagePermissions::META, 'attachment', Array('hierarachical' => false, 'label' => 'Page Permissions', 'query_var' => false));
register_taxonomy(PagePermissions\META, 'attachment', Array('hierarachical' => false, 'label' => 'Page Permissions', 'query_var' => false));
$file = $_SERVER['REQUEST_URI'];
if ($file == '/wp-admin/media-new.php' && !PagePermissions::is_admin()) {
if ($file == '/wp-admin/media-new.php' && !PagePermissions\is_admin()) {
header("Location: " . $file . "?flash=0");
}
......@@ -267,49 +271,49 @@ class PagePermissions_Actions {
// Ideally this is in its own `edit_attachment` method...but that isn't working
if (isset($_POST['action']) && $_POST['action'] == 'editattachment') {
$real_id = $_POST['attachment_id'];
$current = array_shift(get_post_meta($real_id, PagePermissions::META));
$current = array_shift(get_post_meta($real_id, PagePermissions\META));
$new = Array();
$new[PagePermissions::ELE_SEL] = $_POST[PagePermissions::ELE_SEL];
if (isset($_POST[PagePermissions::ELE_CUST])) {
$new[PagePermissions::ELE_CUST] = $_POST[PagePermissions::ELE_CUST];
$new[PagePermissions\ELE_SEL] = $_POST[PagePermissions\ELE_SEL];
if (isset($_POST[PagePermissions\ELE_CUST])) {
$new[PagePermissions\ELE_CUST] = $_POST[PagePermissions\ELE_CUST];
} else {
$new[PagePermissions::ELE_CUST] = Array();
$new[PagePermissions\ELE_CUST] = Array();
}
if (is_null($current)) {
add_post_meta($real_id, PagePermissions::META, $new, true);
add_post_meta($real_id, PagePermissions\META, $new, true);
} else {
update_post_meta($real_id, PagePermissions::META, $new);
update_post_meta($real_id, PagePermissions\META, $new);
}
}
}
public static function admin_menu() {
if (current_user_can(ClientSettings::CAPABILITY)) {
add_submenu_page(ClientSettings::ADMIN_PAGE, 'Permission Defaults', 'Permission Defaults', PagePermissionsAdmin::CAPABILITY, PagePermissionsAdmin::ADMIN_PAGE, Array('PagePermissionsAdmin', 'viewOptionsPage'));
add_meta_box('page_permissions', 'Page Permissions', Array('PagePermissionsAdmin', 'viewMetaBox'), 'page', 'side', 'low');
if (current_user_can(ClientSettings\CAPABILITY)) {
add_submenu_page(ClientSettings\ADMIN_PAGE, 'Permission Defaults', 'Permission Defaults', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\viewOptionsPage');
add_meta_box('page_permissions', 'Page Permissions', __NAMESPACE__ . '\viewMetaBox', 'page', 'side', 'low');
}
}
public static function admin_print_scripts() {
$innerhtml = '';
if ('0' !== ($change_field = (isset($_GET['attachment_id']) ? 'attachments[' . $_GET['attachment_id'] . '][' . PagePermissions::META . ']' : '0'))) {
$selected = array_shift(get_post_meta($_GET['attachment_id'], PagePermissions::META));
if ('0' !== ($change_field = (isset($_GET['attachment_id']) ? 'attachments[' . $_GET['attachment_id'] . '][' . PagePermissions\META . ']' : '0'))) {
$selected = array_shift(get_post_meta($_GET['attachment_id'], PagePermissions\META));
if (is_null($selected)) {
$selected = PagePermissionsAdmin::getOptions();
$selected = getOptions();
}
ob_start();
require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
require(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
$innerhtml = ob_get_contents();
ob_end_clean();
}
_enqueue_script('page-permissions', TzTools::tools_url('PagePermissions.js', __FILE__));
_enqueue_script('page-permissions', Tools\url('PagePermissions.js', __FILE__));
_localize_script('page-permissions', 'TzPagePermissionsData', Array(
'trigger' => PagePermissions::ELE_SEL
, 'focus' => PagePermissions::OPT_CUST
'trigger' => PagePermissions\ELE_SEL
, 'focus' => PagePermissions\OPT_CUST
, 'change_field' => $change_field
, 'innerHTML' => rawurlencode($innerhtml)
));
......@@ -323,42 +327,34 @@ class PagePermissions_Actions {
if (false === ($real_id = _is_post_revision($post_id))) {
$real_id = $post_id;
}
$current = array_shift(get_post_meta($real_id, PagePermissions::META));
$current = array_shift(get_post_meta($real_id, PagePermissions\META));
$new = Array();
$new[PagePermissions::ELE_SEL] = $_POST[PagePermissions::ELE_SEL];
if (isset($_POST[PagePermissions::ELE_CUST])) {
$new[PagePermissions::ELE_CUST] = $_POST[PagePermissions::ELE_CUST];
$new[PagePermissions\ELE_SEL] = $_POST[PagePermissions\ELE_SEL];
if (isset($_POST[PagePermissions\ELE_CUST])) {
$new[PagePermissions\ELE_CUST] = $_POST[PagePermissions\ELE_CUST];
} else {
$new[PagePermissions::ELE_CUST] = Array();
$new[PagePermissions\ELE_CUST] = Array();
}
if (is_null($current)) {
add_post_meta($real_id, PagePermissions::META, $new, true);
add_post_meta($real_id, PagePermissions\META, $new, true);
} else {
update_post_meta($real_id, PagePermissions::META, $new);
update_post_meta($real_id, PagePermissions\META, $new);
}
}
}
class PagePermissionsAdmin_Filters {
public static function image_upload_iframe_src($result) {
return $result . '&flash=0';
}
public static function video_upload_iframe_src($result) {
return $result . '&flash=0';
}
public static function audio_upload_iframe_src($result) {
return $result . '&flash=0';
class Filters {
public static function flash_uploader() {
return false;
}
}
if (isset($_POST['tz_pp_ajax'])) {
PagePermissions::initAjax();
PagePermissions\initAjax();
} else {
PagePermissions::init();
PagePermissionsAdmin::make();
PagePermissions\make();
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
......