Modified namespacing, added new settings function
Showing
6 changed files
with
65 additions
and
174 deletions
| ... | @@ -4,7 +4,6 @@ namespace Tz\WordPress\Tools\PagePermissions; | ... | @@ -4,7 +4,6 @@ namespace Tz\WordPress\Tools\PagePermissions; |
| 4 | use Tz\WordPress\Tools, Tz\WordPress\Tools\ClientSettings; | 4 | use Tz\WordPress\Tools, Tz\WordPress\Tools\ClientSettings; |
| 5 | 5 | ||
| 6 | use \ReflectionClass, \ReflectionException; | 6 | use \ReflectionClass, \ReflectionException; |
| 7 | use \WP_Option; | ||
| 8 | use \WP_User; | 7 | use \WP_User; |
| 9 | 8 | ||
| 10 | /** | 9 | /** |
| ... | @@ -37,14 +36,6 @@ const OPT_AUTH = 1; | ... | @@ -37,14 +36,6 @@ const OPT_AUTH = 1; |
| 37 | const OPT_CUST = 2; | 36 | const OPT_CUST = 2; |
| 38 | 37 | ||
| 39 | 38 | ||
| 40 | class Vars { | ||
| 41 | /** | ||
| 42 | * WP current user data | ||
| 43 | * @type Array | ||
| 44 | */ | ||
| 45 | public static $current_user = false; | ||
| 46 | } | ||
| 47 | |||
| 48 | function make() { | 39 | function make() { |
| 49 | Vars::$current_user = _get_current_user(); | 40 | Vars::$current_user = _get_current_user(); |
| 50 | } | 41 | } |
| ... | @@ -64,7 +55,7 @@ function initAjax() { | ... | @@ -64,7 +55,7 @@ function initAjax() { |
| 64 | function current_user_can_view($post_id = false) { | 55 | function current_user_can_view($post_id = false) { |
| 65 | static $settings = false; | 56 | static $settings = false; |
| 66 | if (false === $settings) { | 57 | if (false === $settings) { |
| 67 | $settings = new WP_Option(SETTING_NS); | 58 | $settings = new Tools\WP_Option(SETTING_NS); |
| 68 | } | 59 | } |
| 69 | 60 | ||
| 70 | if (false === $post_id) { | 61 | if (false === $post_id) { |
| ... | @@ -177,14 +168,20 @@ function getFieldNames() { | ... | @@ -177,14 +168,20 @@ function getFieldNames() { |
| 177 | return $fields; | 168 | return $fields; |
| 178 | } | 169 | } |
| 179 | 170 | ||
| 171 | class Vars { | ||
| 172 | /** | ||
| 173 | * WP current user data | ||
| 174 | * @type Array | ||
| 175 | */ | ||
| 176 | public static $current_user = false; | ||
| 177 | } | ||
| 178 | |||
| 180 | namespace Tz\WordPress\Tools\PagePermissions\Admin; | 179 | namespace Tz\WordPress\Tools\PagePermissions\Admin; |
| 181 | 180 | ||
| 182 | use Tz\WordPress\Tools; | 181 | use Tz\WordPress\Tools; |
| 183 | use Tz\WordPress\Tools\PagePermissions; | 182 | use Tz\WordPress\Tools\PagePermissions; |
| 184 | use Tz\WordPress\Tools\ClientSettings; | 183 | use Tz\WordPress\Tools\ClientSettings; |
| 185 | 184 | ||
| 186 | use \WP_Option; | ||
| 187 | |||
| 188 | const CAPABILITY = 'manage_page_permissions'; | 185 | const CAPABILITY = 'manage_page_permissions'; |
| 189 | const ADMIN_PAGE = 'page-permission-settings'; | 186 | const ADMIN_PAGE = 'page-permission-settings'; |
| 190 | const SUBMIT_HOOK = 'update_def_page_permissions'; | 187 | const SUBMIT_HOOK = 'update_def_page_permissions'; |
| ... | @@ -251,7 +248,7 @@ function getOptions() { | ... | @@ -251,7 +248,7 @@ function getOptions() { |
| 251 | return $options; | 248 | return $options; |
| 252 | } | 249 | } |
| 253 | 250 | ||
| 254 | $options = new WP_Option(SETTING_NS); | 251 | $options = new Tools\WP_Option(SETTING_NS); |
| 255 | return $options; | 252 | return $options; |
| 256 | } | 253 | } |
| 257 | 254 | ... | ... |
| ... | @@ -4,21 +4,22 @@ namespace Tz\WordPress\Tools\ShortCodes; | ... | @@ -4,21 +4,22 @@ namespace Tz\WordPress\Tools\ShortCodes; |
| 4 | 4 | ||
| 5 | use Tz\WordPress\Tools; | 5 | use Tz\WordPress\Tools; |
| 6 | 6 | ||
| 7 | use WP_Option; | 7 | use ReflectionClass, ReflectionMethod, ReflectionFunction; |
| 8 | use Exception; | ||
| 8 | 9 | ||
| 9 | use \ReflectionClass, \ReflectionMethod, \ReflectionFunction; | 10 | call_user_func(function() { |
| 10 | use \Exception; | 11 | Tools\add_actions(__NAMESPACE__ . '\Actions'); |
| 12 | Tools\add_filters(__NAMESPACE__ . '\Filters'); | ||
| 11 | 13 | ||
| 12 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | 14 | if (function_exists('rename_function')) { |
| 15 | rename_function('add_shortcode', 'wp_add_shortcode'); | ||
| 13 | 16 | ||
| 14 | if (function_exists('rename_function')) { | 17 | function add_shortcode() { |
| 15 | rename_function('add_shortcode', 'wp_add_shortcode'); | 18 | $args = func_get_args(); |
| 16 | 19 | call_user_func_array('add', $args); | |
| 17 | function add_shortcode() { | 20 | } |
| 18 | $args = func_get_args(); | ||
| 19 | call_user_func_array('add', $args); | ||
| 20 | } | 21 | } |
| 21 | } | 22 | }); |
| 22 | 23 | ||
| 23 | function add_shortcodes($class) { | 24 | function add_shortcodes($class) { |
| 24 | if (!class_exists($class)) { | 25 | if (!class_exists($class)) { |
| ... | @@ -32,6 +33,19 @@ function add_shortcodes($class) { | ... | @@ -32,6 +33,19 @@ function add_shortcodes($class) { |
| 32 | } | 33 | } |
| 33 | } | 34 | } |
| 34 | 35 | ||
| 36 | function getFileContents($file) { | ||
| 37 | if (!is_file($file)) { | ||
| 38 | throw new Exception("$file not found"); | ||
| 39 | } | ||
| 40 | |||
| 41 | ob_start(); | ||
| 42 | require($file); | ||
| 43 | $parsed_contents = ob_get_contents(); | ||
| 44 | ob_end_clean(); | ||
| 45 | |||
| 46 | return $parsed_contents; | ||
| 47 | } | ||
| 48 | |||
| 35 | function registerClass($class) { | 49 | function registerClass($class) { |
| 36 | call_user_func(__NAMESPACE__ . '\add_shortcodes', $class); | 50 | call_user_func(__NAMESPACE__ . '\add_shortcodes', $class); |
| 37 | } | 51 | } |
| ... | @@ -135,6 +149,16 @@ class Actions { | ... | @@ -135,6 +149,16 @@ class Actions { |
| 135 | } | 149 | } |
| 136 | } | 150 | } |
| 137 | 151 | ||
| 152 | class Filters { | ||
| 153 | public static function the_title($content) { | ||
| 154 | return is_admin() ? $content : do_shortcode($content); | ||
| 155 | } | ||
| 156 | |||
| 157 | public static function wp_title($content) { | ||
| 158 | return is_admin() ? $content : do_shortcode(htmlspecialchars_decode($content)); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 138 | class Vars { | 162 | class Vars { |
| 139 | public static $registered = Array(); | 163 | public static $registered = Array(); |
| 140 | public static $private = Array(); | 164 | public static $private = Array(); | ... | ... |
lib/Ajaxdata.php
deleted
100644 → 0
| 1 | <?php | ||
| 2 | |||
| 3 | namespace Tz; | ||
| 4 | |||
| 5 | use Exception; | ||
| 6 | |||
| 7 | /** | ||
| 8 | * Handles data preparation for XMLHTTP responses to xmlhttpHandler script | ||
| 9 | * | ||
| 10 | * @author Chris Boden | ||
| 11 | * @version 0.3 | ||
| 12 | */ | ||
| 13 | class Ajaxdata { | ||
| 14 | private $_data = Array(); | ||
| 15 | private static $exception = false; | ||
| 16 | |||
| 17 | /** | ||
| 18 | * @private | ||
| 19 | */ | ||
| 20 | final public function __destruct() { | ||
| 21 | if (self::$exception) { | ||
| 22 | return false; | ||
| 23 | } | ||
| 24 | |||
| 25 | $output = Array(); | ||
| 26 | $output['status'] = 0; | ||
| 27 | $output['data'] = $this->_data; | ||
| 28 | |||
| 29 | echo json_encode($output); | ||
| 30 | } | ||
| 31 | |||
| 32 | /** | ||
| 33 | * @private | ||
| 34 | */ | ||
| 35 | final public function __set($var, $val) { | ||
| 36 | // is this going to work for arrays? | ||
| 37 | $this->_data[$var] = $val; | ||
| 38 | } | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @private | ||
| 42 | */ | ||
| 43 | final public function &__get($var) { | ||
| 44 | if (isset($this->_data[$var])) { | ||
| 45 | return $this->_data[$var]; | ||
| 46 | } else { | ||
| 47 | return ''; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * @private | ||
| 53 | * possibly obsolete | ||
| 54 | */ | ||
| 55 | final public static function ExceptionThrown() { | ||
| 56 | self::$exception = true; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Only 1 is declared at a time | ||
| 62 | * This exception is generated when doing an XHR | ||
| 63 | * | ||
| 64 | * @author Chris Boden | ||
| 65 | * @version 0.2 | ||
| 66 | */ | ||
| 67 | class JSONException extends Exception { | ||
| 68 | /** | ||
| 69 | * @private | ||
| 70 | */ | ||
| 71 | public function __construct($message=NULL, $code=0) { | ||
| 72 | parent::__construct($message, $code); | ||
| 73 | // die instead? | ||
| 74 | Ajaxdata::ExceptionThrown(); | ||
| 75 | } | ||
| 76 | |||
| 77 | /** | ||
| 78 | * @private | ||
| 79 | */ | ||
| 80 | public function __destruct() { | ||
| 81 | $output = Array(); | ||
| 82 | $output['status'] = 1; | ||
| 83 | $output['data'] = Array('message' => $this->getMessage()); | ||
| 84 | |||
| 85 | echo json_encode($output); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | ?> |
lib/Validation.php
deleted
100644 → 0
| 1 | <?php | ||
| 2 | |||
| 3 | namespace Tz; | ||
| 4 | |||
| 5 | use \Exception; | ||
| 6 | |||
| 7 | abstract class Validation { | ||
| 8 | /** | ||
| 9 | * Associative array of valid fields | ||
| 10 | * @type Array | ||
| 11 | * @public | ||
| 12 | * @read-only | ||
| 13 | */ | ||
| 14 | private $valid = Array(); | ||
| 15 | |||
| 16 | /** | ||
| 17 | * Associative array if invalid fields | ||
| 18 | * @type Array | ||
| 19 | * @public | ||
| 20 | * @read-only | ||
| 21 | */ | ||
| 22 | private $errors = Array(); | ||
| 23 | |||
| 24 | /** | ||
| 25 | * @param {Array} $data Associative array of data to validate | ||
| 26 | */ | ||
| 27 | final public function __construct(Array $data) { | ||
| 28 | foreach ($data as $key => $val) { | ||
| 29 | if (method_exists($this, $key)) { | ||
| 30 | try { | ||
| 31 | call_user_func(Array($this, $key), $val); | ||
| 32 | $this->valid[$key] = $val; | ||
| 33 | } catch (Exception $e) { | ||
| 34 | $this->errors[$key] = $e->getMessage(); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @private | ||
| 42 | */ | ||
| 43 | final public function __get($key) { | ||
| 44 | $private = $key; | ||
| 45 | if (isset($this->$private)) { | ||
| 46 | return $this->$private; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | <?php | 1 | <?php |
| 2 | /* | 2 | |
| 3 | * I made a boo boo | 3 | namespace Tz\WordPress\Tools; |
| 4 | * This only works with Array variables | 4 | |
| 5 | */ | 5 | use ArrayAccess, Countable; |
| 6 | |||
| 6 | class WP_Option implements ArrayAccess, Countable { | 7 | class WP_Option implements ArrayAccess, Countable { |
| 7 | private $_ns; | 8 | private $_ns; |
| 8 | private $_data = Array(); | 9 | private $_data = Array(); |
| ... | @@ -56,10 +57,5 @@ class WP_Option implements ArrayAccess, Countable { | ... | @@ -56,10 +57,5 @@ class WP_Option implements ArrayAccess, Countable { |
| 56 | array_merge($this->_data, $data); | 57 | array_merge($this->_data, $data); |
| 57 | $this->save(); | 58 | $this->save(); |
| 58 | } | 59 | } |
| 59 | |||
| 60 | public function dump() { | ||
| 61 | echo "Here\n"; | ||
| 62 | print_r($this->_data); | ||
| 63 | } | ||
| 64 | } | 60 | } |
| 65 | ?> | 61 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -20,7 +20,7 @@ use \Exception; | ... | @@ -20,7 +20,7 @@ use \Exception; |
| 20 | _register_script('fireEvent', url('scripts/fireEvent.js', __FILE__)); | 20 | _register_script('fireEvent', url('scripts/fireEvent.js', __FILE__)); |
| 21 | 21 | ||
| 22 | import('ShortCodes'); | 22 | import('ShortCodes'); |
| 23 | if (defined('TZ_DEBUG') && TZ_DEBUG === true) { | 23 | if (defined('Tz\DEBUG') && Tz\DEBUG === true) { |
| 24 | import('Debug'); | 24 | import('Debug'); |
| 25 | } | 25 | } |
| 26 | 26 | ||
| ... | @@ -58,7 +58,7 @@ function tools_url() { | ... | @@ -58,7 +58,7 @@ function tools_url() { |
| 58 | 58 | ||
| 59 | function add_actions($class) { | 59 | function add_actions($class) { |
| 60 | if (!class_exists($class)) { | 60 | if (!class_exists($class)) { |
| 61 | throw new Exception("$class does not exist"); | 61 | throw new Exception("{$class} does not exist"); |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | $ref = new ReflectionClass($class); | 64 | $ref = new ReflectionClass($class); |
| ... | @@ -70,7 +70,7 @@ function add_actions($class) { | ... | @@ -70,7 +70,7 @@ function add_actions($class) { |
| 70 | 70 | ||
| 71 | function add_filters($class) { | 71 | function add_filters($class) { |
| 72 | if (!class_exists($class)) { | 72 | if (!class_exists($class)) { |
| 73 | throw new Exception("$class does not exist"); | 73 | throw new Exception("{$class} does not exist"); |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | $ref = new ReflectionClass($class); | 76 | $ref = new ReflectionClass($class); |
| ... | @@ -79,4 +79,16 @@ function add_filters($class) { | ... | @@ -79,4 +79,16 @@ function add_filters($class) { |
| 79 | add_filter($method->name, Array($class, $method->name)); | 79 | add_filter($method->name, Array($class, $method->name)); |
| 80 | } | 80 | } |
| 81 | } | 81 | } |
| 82 | |||
| 83 | function add_settings_fields($class, $page = 'general', $section = 'default') { | ||
| 84 | if (!class_exists($class)) { | ||
| 85 | throw new Exception("{$class} does not exist"); | ||
| 86 | } | ||
| 87 | |||
| 88 | $ref = new ReflectionClass($class); | ||
| 89 | $methods = $ref->getMethods(ReflectionMethod::IS_STATIC); | ||
| 90 | foreach ($methods as $method) { | ||
| 91 | add_settings_field($method->name, ucwords(str_replace('_', ' ', $method->name)), Array($class, $method->name), $page, $section); | ||
| 92 | } | ||
| 93 | } | ||
| 82 | ?> | 94 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment