b58e14b4 by Chris Boden

Modified namespacing, added new settings function

1 parent 9bde87fa
......@@ -4,7 +4,6 @@ namespace Tz\WordPress\Tools\PagePermissions;
use Tz\WordPress\Tools, Tz\WordPress\Tools\ClientSettings;
use \ReflectionClass, \ReflectionException;
use \WP_Option;
use \WP_User;
/**
......@@ -37,14 +36,6 @@ const OPT_AUTH = 1;
const OPT_CUST = 2;
class Vars {
/**
* WP current user data
* @type Array
*/
public static $current_user = false;
}
function make() {
Vars::$current_user = _get_current_user();
}
......@@ -64,7 +55,7 @@ function initAjax() {
function current_user_can_view($post_id = false) {
static $settings = false;
if (false === $settings) {
$settings = new WP_Option(SETTING_NS);
$settings = new Tools\WP_Option(SETTING_NS);
}
if (false === $post_id) {
......@@ -177,14 +168,20 @@ function getFieldNames() {
return $fields;
}
class Vars {
/**
* WP current user data
* @type Array
*/
public static $current_user = false;
}
namespace Tz\WordPress\Tools\PagePermissions\Admin;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\PagePermissions;
use Tz\WordPress\Tools\ClientSettings;
use \WP_Option;
const CAPABILITY = 'manage_page_permissions';
const ADMIN_PAGE = 'page-permission-settings';
const SUBMIT_HOOK = 'update_def_page_permissions';
......@@ -251,7 +248,7 @@ function getOptions() {
return $options;
}
$options = new WP_Option(SETTING_NS);
$options = new Tools\WP_Option(SETTING_NS);
return $options;
}
......
......@@ -4,12 +4,12 @@ namespace Tz\WordPress\Tools\ShortCodes;
use Tz\WordPress\Tools;
use WP_Option;
use \ReflectionClass, \ReflectionMethod, \ReflectionFunction;
use \Exception;
use ReflectionClass, ReflectionMethod, ReflectionFunction;
use Exception;
call_user_func(function() {
Tools\add_actions(__NAMESPACE__ . '\Actions');
Tools\add_filters(__NAMESPACE__ . '\Filters');
if (function_exists('rename_function')) {
rename_function('add_shortcode', 'wp_add_shortcode');
......@@ -19,6 +19,7 @@ use \Exception;
call_user_func_array('add', $args);
}
}
});
function add_shortcodes($class) {
if (!class_exists($class)) {
......@@ -32,6 +33,19 @@ function add_shortcodes($class) {
}
}
function getFileContents($file) {
if (!is_file($file)) {
throw new Exception("$file not found");
}
ob_start();
require($file);
$parsed_contents = ob_get_contents();
ob_end_clean();
return $parsed_contents;
}
function registerClass($class) {
call_user_func(__NAMESPACE__ . '\add_shortcodes', $class);
}
......@@ -135,6 +149,16 @@ class Actions {
}
}
class Filters {
public static function the_title($content) {
return is_admin() ? $content : do_shortcode($content);
}
public static function wp_title($content) {
return is_admin() ? $content : do_shortcode(htmlspecialchars_decode($content));
}
}
class Vars {
public static $registered = Array();
public static $private = Array();
......
<?php
namespace Tz;
use Exception;
/**
* Handles data preparation for XMLHTTP responses to xmlhttpHandler script
*
* @author Chris Boden
* @version 0.3
*/
class Ajaxdata {
private $_data = Array();
private static $exception = false;
/**
* @private
*/
final public function __destruct() {
if (self::$exception) {
return false;
}
$output = Array();
$output['status'] = 0;
$output['data'] = $this->_data;
echo json_encode($output);
}
/**
* @private
*/
final public function __set($var, $val) {
// is this going to work for arrays?
$this->_data[$var] = $val;
}
/**
* @private
*/
final public function &__get($var) {
if (isset($this->_data[$var])) {
return $this->_data[$var];
} else {
return '';
}
}
/**
* @private
* possibly obsolete
*/
final public static function ExceptionThrown() {
self::$exception = true;
}
}
/**
* Only 1 is declared at a time
* This exception is generated when doing an XHR
*
* @author Chris Boden
* @version 0.2
*/
class JSONException extends Exception {
/**
* @private
*/
public function __construct($message=NULL, $code=0) {
parent::__construct($message, $code);
// die instead?
Ajaxdata::ExceptionThrown();
}
/**
* @private
*/
public function __destruct() {
$output = Array();
$output['status'] = 1;
$output['data'] = Array('message' => $this->getMessage());
echo json_encode($output);
}
}
?>
<?php
namespace Tz;
use \Exception;
abstract class Validation {
/**
* Associative array of valid fields
* @type Array
* @public
* @read-only
*/
private $valid = Array();
/**
* Associative array if invalid fields
* @type Array
* @public
* @read-only
*/
private $errors = Array();
/**
* @param {Array} $data Associative array of data to validate
*/
final public function __construct(Array $data) {
foreach ($data as $key => $val) {
if (method_exists($this, $key)) {
try {
call_user_func(Array($this, $key), $val);
$this->valid[$key] = $val;
} catch (Exception $e) {
$this->errors[$key] = $e->getMessage();
}
}
}
}
/**
* @private
*/
final public function __get($key) {
$private = $key;
if (isset($this->$private)) {
return $this->$private;
}
}
}
?>
\ No newline at end of file
<?php
/*
* I made a boo boo
* This only works with Array variables
*/
namespace Tz\WordPress\Tools;
use ArrayAccess, Countable;
class WP_Option implements ArrayAccess, Countable {
private $_ns;
private $_data = Array();
......@@ -56,10 +57,5 @@ class WP_Option implements ArrayAccess, Countable {
array_merge($this->_data, $data);
$this->save();
}
public function dump() {
echo "Here\n";
print_r($this->_data);
}
}
?>
\ No newline at end of file
......
......@@ -20,7 +20,7 @@ use \Exception;
_register_script('fireEvent', url('scripts/fireEvent.js', __FILE__));
import('ShortCodes');
if (defined('TZ_DEBUG') && TZ_DEBUG === true) {
if (defined('Tz\DEBUG') && Tz\DEBUG === true) {
import('Debug');
}
......@@ -58,7 +58,7 @@ function tools_url() {
function add_actions($class) {
if (!class_exists($class)) {
throw new Exception("$class does not exist");
throw new Exception("{$class} does not exist");
}
$ref = new ReflectionClass($class);
......@@ -70,7 +70,7 @@ function add_actions($class) {
function add_filters($class) {
if (!class_exists($class)) {
throw new Exception("$class does not exist");
throw new Exception("{$class} does not exist");
}
$ref = new ReflectionClass($class);
......@@ -79,4 +79,16 @@ function add_filters($class) {
add_filter($method->name, Array($class, $method->name));
}
}
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)), Array($class, $method->name), $page, $section);
}
}
?>
\ No newline at end of file
......