Implemented namespaces
Showing
13 changed files
with
476 additions
and
471 deletions
| 1 | <?php | 1 | <?php |
| 2 | class Auth { | ||
| 3 | const REG_METH_AUTO_REG = 1; | ||
| 4 | const REG_METH_VALID_EMAIL = 2; | ||
| 5 | 2 | ||
| 6 | const FORGOT_METH_VALID_EMAIL = 1; | 3 | namespace Tz\WordPress\Tools\Auth; |
| 7 | const FORGOT_METH_RAND_PASS = 2; | ||
| 8 | 4 | ||
| 9 | const ACTION_ACTIVATE = 'activate_account'; | 5 | use \Exception, \LogicException, \InvalidArgumentException, \BadMethodCallException; |
| 10 | 6 | ||
| 11 | public static function make() { | 7 | const REG_METH_AUTO_REG = 1; |
| 12 | static $made = false; | 8 | const REG_METH_VALID_EMAIL = 2; |
| 13 | if (true === $made) { | ||
| 14 | throw new Exception('Auth has already been instantiated'); | ||
| 15 | } | ||
| 16 | $made = true; | ||
| 17 | 9 | ||
| 18 | // if _GET activate self::activate(); | 10 | const FORGOT_METH_VALID_EMAIL = 1; |
| 19 | } | 11 | const FORGOT_METH_RAND_PASS = 2; |
| 20 | 12 | ||
| 21 | /** | 13 | const ACTION_ACTIVATE = 'activate_account'; |
| 22 | * Attempts to login the user | ||
| 23 | * @param {String} $username | ||
| 24 | * @param {String} $password | ||
| 25 | * @param {Boolean} $remember | ||
| 26 | * @returns WP_User instance | ||
| 27 | * @throws LogicException If headers have already been passed | ||
| 28 | * @throws InvalidArgumentException If the authentication is invalid | ||
| 29 | */ | ||
| 30 | public static function login($username, $password, $remember = true) { | ||
| 31 | if (headers_sent()) { | ||
| 32 | throw new LogicException('Unable to login because headers have been sent'); | ||
| 33 | } | ||
| 34 | 14 | ||
| 35 | $auth = _signon(Array( | 15 | /** |
| 36 | 'user_login' => esc_sql($username) | 16 | * Attempts to login the user |
| 37 | , 'user_password' => esc_sql($password) | 17 | * @param {String} $username |
| 38 | , 'remember' => $remember | 18 | * @param {String} $password |
| 39 | )); | 19 | * @param {Boolean} $remember |
| 20 | * @returns WP_User instance | ||
| 21 | * @throws LogicException If headers have already been passed | ||
| 22 | * @throws InvalidArgumentException If the authentication is invalid | ||
| 23 | */ | ||
| 24 | function login($username, $password, $remember = true) { | ||
| 25 | if (headers_sent()) { | ||
| 26 | throw new LogicException('Unable to login because headers have been sent'); | ||
| 27 | } | ||
| 40 | 28 | ||
| 41 | $ref = new ReflectionObject($auth); | 29 | $auth = _signon(Array( |
| 42 | if ($ref->name == 'WP_User') { | 30 | 'user_login' => esc_sql($username) |
| 43 | return $auth; | 31 | , 'user_password' => esc_sql($password) |
| 44 | } | 32 | , 'remember' => $remember |
| 33 | )); | ||
| 45 | 34 | ||
| 46 | throw new InvalidArgumentException('Invalid username/password'); | 35 | if (get_class($auth) == 'WP_User') { |
| 47 | //$auth->get_error_message()); this would be nice except it links to a wp-page | 36 | return $auth; |
| 48 | } | 37 | } |
| 49 | 38 | ||
| 50 | /** | 39 | throw new InvalidArgumentException('Invalid username/password'); |
| 51 | * Attempts to log the user out | 40 | //$auth->get_error_message()); this would be nice except it links to a wp-page |
| 52 | * @returns Boolean | 41 | } |
| 53 | * @throws LogicException If HTTP headers have already been sent | ||
| 54 | */ | ||
| 55 | public static function logout() { | ||
| 56 | if (headers_sent()) { | ||
| 57 | throw new LogicException('Unable to logout because headers have been sent'); | ||
| 58 | } | ||
| 59 | |||
| 60 | _logout(); | ||
| 61 | 42 | ||
| 62 | return true; | 43 | /** |
| 44 | * Attempts to log the user out | ||
| 45 | * @returns Boolean | ||
| 46 | * @throws LogicException If HTTP headers have already been sent | ||
| 47 | */ | ||
| 48 | function logout() { | ||
| 49 | if (headers_sent()) { | ||
| 50 | throw new LogicException('Unable to logout because headers have been sent'); | ||
| 63 | } | 51 | } |
| 64 | 52 | ||
| 65 | public static function register($user_data = Array(), $registration_method) { | 53 | _logout(); |
| 66 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); | ||
| 67 | |||
| 68 | $valid = new Auth_Validation($user_data); | ||
| 69 | if (count($valid->errors) > 0) { | ||
| 70 | throw new BadMethodCallException(implode("\n", $valid->errors)); | ||
| 71 | } | ||
| 72 | 54 | ||
| 73 | array_filter($user_data, 'esc_sql'); | 55 | return true; |
| 74 | $id = (int)_insert_user($user_data); | 56 | } |
| 75 | 57 | ||
| 76 | global $wpdb; | 58 | function register($user_data = Array(), $registration_method) { |
| 77 | $wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}"); | 59 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); |
| 78 | 60 | ||
| 79 | return $id; | 61 | $valid = new Validation($user_data); |
| 62 | if (count($valid->errors) > 0) { | ||
| 63 | throw new BadMethodCallException(implode("\n", $valid->errors)); | ||
| 80 | } | 64 | } |
| 81 | 65 | ||
| 82 | public static function activate($username, $activation_key) { | 66 | array_filter($user_data, 'esc_sql'); |
| 83 | do_action(self::ACTION_ACTIVATE, $user_id); | 67 | $id = (int)_insert_user($user_data); |
| 84 | } | ||
| 85 | 68 | ||
| 86 | public static function forgot_password($username, $forgot_method) { | 69 | global $wpdb; |
| 87 | 70 | $wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}"); | |
| 88 | } | 71 | |
| 72 | return $id; | ||
| 73 | } | ||
| 74 | |||
| 75 | function activate($username, $activation_key) { | ||
| 76 | do_action(ACTION_ACTIVATE, $user_id); | ||
| 77 | } | ||
| 78 | |||
| 79 | function forgot_password($username, $forgot_method) { | ||
| 80 | |||
| 89 | } | 81 | } |
| 90 | 82 | ||
| 91 | class Auth_Validation extends Validation { | 83 | class Validation extends \Tz\Validation { |
| 92 | /** | 84 | /** |
| 93 | * @rule Not blank | 85 | * @rule Not blank |
| 94 | * @rule Valid WordPress username | 86 | * @rule Valid WordPress username | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | class Branding { | ||
| 3 | public static function make() { | ||
| 4 | add_actions('Branding_Actions'); | ||
| 5 | } | ||
| 6 | } | ||
| 7 | 2 | ||
| 8 | class Branding_Actions { | 3 | namespace Tz\WordPress\Tools\Branding; |
| 4 | use Tz\WordPress\Tools; | ||
| 5 | |||
| 6 | class Actions { | ||
| 9 | public static function admin_print_styles() { | 7 | public static function admin_print_styles() { |
| 10 | _enqueue_style('branding-style', TzTools::tools_url('css/tenzing.css', __FILE__)); | 8 | _enqueue_style('branding-style', Tools\url('css/tenzing.css', __FILE__)); |
| 11 | } | 9 | } |
| 12 | 10 | ||
| 13 | public static function admin_head() { | 11 | public static function admin_head() { |
| ... | @@ -22,10 +20,9 @@ class Branding_Actions { | ... | @@ -22,10 +20,9 @@ class Branding_Actions { |
| 22 | } | 20 | } |
| 23 | 21 | ||
| 24 | public static function login_head() { | 22 | public static function login_head() { |
| 25 | echo '<link rel="stylesheet" type="text/css" href="' . TzTools::tools_url('css/tz_login.css', __FILE__) . '" />'; | 23 | echo '<link rel="stylesheet" type="text/css" href="' . Tools\url('css/tz_login.css', __FILE__) . '" />'; |
| 26 | } | 24 | } |
| 27 | } | 25 | } |
| 28 | 26 | ||
| 29 | Branding::make(); | 27 | Tools\add_actions(__NAMESPACE__ . '\Actions'); |
| 30 | |||
| 31 | ?> | 28 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | class ClientSettings { | ||
| 3 | const CAPABILITY = 'edit_client_settings'; | ||
| 4 | const ADMIN_PAGE = 'client-settings'; | ||
| 5 | 2 | ||
| 6 | public static function make() { | 3 | namespace Tz\WordPress\Tools\ClientSettings; |
| 7 | static $made = false; | ||
| 8 | if ($made) { | ||
| 9 | throw new OverflowException('ClientSettings has already been initialized'); | ||
| 10 | } | ||
| 11 | $made = true; | ||
| 12 | 4 | ||
| 13 | $role = get_role('administrator'); | 5 | use Tz\WordPress\Tools; |
| 14 | $role->add_cap(self::CAPABILITY); | ||
| 15 | 6 | ||
| 16 | add_actions('ClientSettings_Actions'); | 7 | const CAPABILITY = 'edit_client_settings'; |
| 17 | } | 8 | const ADMIN_PAGE = 'client-settings'; |
| 18 | 9 | ||
| 19 | public static function viewOptionsPage() { | 10 | $role = get_role('administrator'); |
| 20 | } | 11 | $role->add_cap(CAPABILITY); |
| 12 | |||
| 13 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | ||
| 14 | |||
| 15 | function viewOptionsPage() { | ||
| 21 | } | 16 | } |
| 22 | 17 | ||
| 23 | // register_setting() | 18 | // register_setting() |
| ... | @@ -29,13 +24,11 @@ class ClientSettings { | ... | @@ -29,13 +24,11 @@ class ClientSettings { |
| 29 | 24 | ||
| 30 | // do_settings_section() | 25 | // do_settings_section() |
| 31 | 26 | ||
| 32 | class ClientSettings_Actions { | 27 | class Actions { |
| 33 | public static function admin_menu() { | 28 | public static function admin_menu() { |
| 34 | $display = (current_user_can('manage_options') ? 'Client Settings' : 'Settings'); | 29 | $display = (current_user_can('manage_options') ? 'Client Settings' : 'Settings'); |
| 35 | 30 | ||
| 36 | add_utility_page($display, $display, ClientSettings::CAPABILITY, ClientSettings::ADMIN_PAGE, Array('ClientSettings', 'viewOptionsPage')); | 31 | add_utility_page($display, $display, CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\viewOptionsPage'); |
| 37 | } | 32 | } |
| 38 | } | 33 | } |
| 39 | |||
| 40 | ClientSettings::make(); | ||
| 41 | ?> | 34 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | namespace Tz\WordPress\Tools\PagePermissions; | ||
| 3 | |||
| 4 | use Tz\WordPress\Tools, Tz\WordPress\Tools\ClientSettings; | ||
| 5 | |||
| 6 | use \ReflectionClass, \ReflectionException; | ||
| 7 | use \WP_Option; | ||
| 8 | use \WP_User; | ||
| 9 | |||
| 2 | /** | 10 | /** |
| 3 | * Public API | 11 | * The name of the custom field stored in a post/page |
| 12 | * @type String | ||
| 4 | */ | 13 | */ |
| 5 | class PagePermissions { | 14 | const META = 'accessible_to_roles'; |
| 6 | /** | 15 | const OPT = ''; |
| 7 | * The name of the custom field stored in a post/page | ||
| 8 | * @type String | ||
| 9 | */ | ||
| 10 | const META = 'accessible_to_roles'; | ||
| 11 | const OPT = ''; | ||
| 12 | 16 | ||
| 13 | const ELE_SEL = 'general_access'; | 17 | const ELE_SEL = 'general_access'; |
| 14 | const ELE_CUST = 'roles'; | 18 | const ELE_CUST = 'roles'; |
| 15 | const ELE_AUTH = 'message_auth'; | 19 | const ELE_AUTH = 'message_auth'; |
| 16 | const ELE_CUST_AUTH = 'message_cust_auth'; | 20 | const ELE_CUST_AUTH = 'message_cust_auth'; |
| 17 | const ELE_DENIED = 'message_cust_denied'; | 21 | const ELE_DENIED = 'message_cust_denied'; |
| 22 | |||
| 23 | /** | ||
| 24 | * Lookup value for ELE_SEL for all users | ||
| 25 | * @type Integer | ||
| 26 | */ | ||
| 27 | const OPT_ALL = 0; | ||
| 28 | /** | ||
| 29 | * Lookup value for ELE_SEL for login required | ||
| 30 | * @type Integer | ||
| 31 | */ | ||
| 32 | const OPT_AUTH = 1; | ||
| 33 | /** | ||
| 34 | * Lookup value for ELE_SEL for custom roles | ||
| 35 | * @type Integer | ||
| 36 | */ | ||
| 37 | const OPT_CUST = 2; | ||
| 18 | 38 | ||
| 19 | /** | ||
| 20 | * Lookup value for ELE_SEL for all users | ||
| 21 | * @type Integer | ||
| 22 | */ | ||
| 23 | const OPT_ALL = 0; | ||
| 24 | /** | ||
| 25 | * Lookup value for ELE_SEL for login required | ||
| 26 | * @type Integer | ||
| 27 | */ | ||
| 28 | const OPT_AUTH = 1; | ||
| 29 | /** | ||
| 30 | * Lookup value for ELE_SEL for custom roles | ||
| 31 | * @type Integer | ||
| 32 | */ | ||
| 33 | const OPT_CUST = 2; | ||
| 34 | 39 | ||
| 40 | class Vars { | ||
| 35 | /** | 41 | /** |
| 36 | * WP current user data | 42 | * WP current user data |
| 37 | * @type Array | 43 | * @type Array |
| 38 | */ | 44 | */ |
| 39 | private static $current_user = false; | 45 | public static $current_user = false; |
| 46 | } | ||
| 40 | 47 | ||
| 41 | public static function init() { | 48 | function make() { |
| 42 | if (false !== self::$current_user) { | 49 | Vars::$current_user = _get_current_user(); |
| 43 | throw new OverflowException('PagePermissions already initialized'); | 50 | } |
| 44 | } | ||
| 45 | 51 | ||
| 46 | self::$current_user = _get_current_user(); | 52 | function initAjax() { |
| 47 | } | 53 | $selected = unserialize($_POST['string_value']); |
| 54 | include(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php'); | ||
| 55 | } | ||
| 48 | 56 | ||
| 49 | public static function initAjax() { | 57 | /** |
| 50 | $selected = unserialize($_POST['string_value']); | 58 | * The key function in all of this; called by the Theme, |
| 51 | include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php'); | 59 | * this determines if the user is able to view the page. |
| 60 | * @param {Integer} $post_id | ||
| 61 | * @returns {Boolean|String} true if user can view, error message if not | ||
| 62 | * @throw InvalidArgumentException | ||
| 63 | */ | ||
| 64 | function current_user_can_view($post_id = false) { | ||
| 65 | static $settings = false; | ||
| 66 | if (false === $settings) { | ||
| 67 | $settings = new WP_Option(SETTING_NS); | ||
| 52 | } | 68 | } |
| 53 | 69 | ||
| 54 | /** | 70 | if (false === $post_id) { |
| 55 | * The key function in all of this; called by the Theme, | 71 | global $post; |
| 56 | * this determines if the user is able to view the page. | 72 | $post_id = $post->ID; |
| 57 | * @param {Integer} $post_id | 73 | } |
| 58 | * @returns {Boolean|String} true if user can view, error message if not | ||
| 59 | * @throw InvalidArgumentException | ||
| 60 | */ | ||
| 61 | public static function current_user_can_view($post_id = false) { | ||
| 62 | static $settings = false; | ||
| 63 | if (false === $settings) { | ||
| 64 | $settings = new WP_Option(PagePermissionsAdmin::SETTING_NS); | ||
| 65 | } | ||
| 66 | 74 | ||
| 67 | if (false === $post_id) { | 75 | // Meta value hasn't been set, getting settings defaults |
| 68 | global $post; | 76 | if (NULL === $data = array_shift(get_post_meta($post_id, META))) { |
| 69 | $post_id = $post->ID; | 77 | $data = Array(ELE_SEL => $settings[ELE_SEL], ELE_CUST => $settings[ELE_CUST]); |
| 70 | } | 78 | } |
| 71 | 79 | ||
| 72 | // Meta value hasn't been set, getting settings defaults | 80 | // Anyone has access, God has no limitations |
| 73 | if (NULL === $data = array_shift(get_post_meta($post_id, self::META))) { | 81 | if ($data[ELE_SEL] == OPT_ALL || is_admin()) { |
| 74 | $data = Array(self::ELE_SEL => $settings[self::ELE_SEL], self::ELE_CUST => $settings[self::ELE_CUST]); | 82 | return true; |
| 75 | } | 83 | } |
| 76 | 84 | ||
| 77 | // Anyone has access, God has no limitations | 85 | // Login required |
| 78 | if ($data[self::ELE_SEL] == self::OPT_ALL || self::is_admin()) { | 86 | if ($data[ELE_SEL] == OPT_AUTH) { |
| 87 | // User is logged in | ||
| 88 | if (is_user_logged_in()) { | ||
| 79 | return true; | 89 | return true; |
| 80 | } | 90 | } |
| 81 | 91 | ||
| 82 | // Login required | 92 | // Not logged in; return "login required" message |
| 83 | if ($data[self::ELE_SEL] == self::OPT_AUTH) { | 93 | return $settings[self::ELE_AUTH]; |
| 84 | // User is logged in | 94 | } |
| 85 | if (is_user_logged_in()) { | ||
| 86 | return true; | ||
| 87 | } | ||
| 88 | 95 | ||
| 89 | // Not logged in; return "login required" message | 96 | // Specific role required |
| 90 | return $settings[self::ELE_AUTH]; | 97 | if ($data[ELE_SEL] == OPT_CUST) { |
| 98 | // User isn't even logged in; send message | ||
| 99 | if (!is_user_logged_in()) { | ||
| 100 | return $settings[ELE_CUST_AUTH]; | ||
| 91 | } | 101 | } |
| 92 | 102 | ||
| 93 | // Specific role required | 103 | // User meets role required |
| 94 | if ($data[self::ELE_SEL] == self::OPT_CUST) { | 104 | if (isset($data[ELE_CUST][get_user_role()])) { |
| 95 | // User isn't even logged in; send message | 105 | return true; |
| 96 | if (!is_user_logged_in()) { | ||
| 97 | return $settings[self::ELE_CUST_AUTH]; | ||
| 98 | } | ||
| 99 | |||
| 100 | // User meets role required | ||
| 101 | if (isset($data[self::ELE_CUST][self::get_user_role()])) { | ||
| 102 | return true; | ||
| 103 | } | ||
| 104 | |||
| 105 | // User is logged in, but doesn't have sufficient privileges, return message | ||
| 106 | return $settings[self::ELE_DENIED]; | ||
| 107 | } | 106 | } |
| 108 | 107 | ||
| 109 | // This shouldn't happend; but just in case | 108 | // User is logged in, but doesn't have sufficient privileges, return message |
| 110 | return 'An unknown permission error has occurred'; | 109 | return $settings[ELE_DENIED]; |
| 111 | } | 110 | } |
| 112 | 111 | ||
| 113 | /** | 112 | // This shouldn't happend; but just in case |
| 114 | * @param {Integer|String} $user Username or ID of user to lookup (or false for current user) | 113 | return 'An unknown permission error has occurred'; |
| 115 | * @returns {String} $role The key of the users' role | 114 | } |
| 116 | */ | ||
| 117 | public static function get_user_role($user = false) { | ||
| 118 | if (false === $user) { | ||
| 119 | $user_data = self::$current_user; | ||
| 120 | } else { | ||
| 121 | $user_data = new WP_User($user); | ||
| 122 | } | ||
| 123 | |||
| 124 | // or should I throw an exception? | ||
| 125 | if ($user_data->ID == 0) { | ||
| 126 | return ''; | ||
| 127 | } | ||
| 128 | |||
| 129 | $user_roles = $user_data->roles; | ||
| 130 | $user_role = array_shift($user_roles); | ||
| 131 | 115 | ||
| 132 | return $user_role; | 116 | /** |
| 117 | * @param {Integer|String} $user Username or ID of user to lookup (or false for current user) | ||
| 118 | * @returns {String} $role The key of the users' role | ||
| 119 | */ | ||
| 120 | function get_user_role($user = false) { | ||
| 121 | if (false === $user) { | ||
| 122 | $user_data = Vars::$current_user; | ||
| 123 | } else { | ||
| 124 | $user_data = new WP_User($user); | ||
| 133 | } | 125 | } |
| 134 | 126 | ||
| 135 | /** | 127 | // or should I throw an exception? |
| 136 | * Determine if a user is a site administrator | 128 | if ($user_data->ID == 0) { |
| 137 | * @param {Integer|String} $user Username or ID of user to lookup (or false for current user) | 129 | return ''; |
| 138 | * @returns {Boolean} | ||
| 139 | */ | ||
| 140 | public static function is_admin($user = false) { | ||
| 141 | return (self::get_user_role($user) == 'administrator' ? true : false); | ||
| 142 | } | 130 | } |
| 143 | 131 | ||
| 144 | /** | 132 | $user_roles = $user_data->roles; |
| 145 | * Get a lookup of all the forum elements | 133 | $user_role = array_shift($user_roles); |
| 146 | * @returns {Array} An associative array of the forum elemnts name/values | ||
| 147 | */ | ||
| 148 | public static function getFieldNames() { | ||
| 149 | static $fields = false; | ||
| 150 | if (false !== $fields) { | ||
| 151 | return $fields; | ||
| 152 | } | ||
| 153 | 134 | ||
| 154 | $fields = Array(); | 135 | return $user_role; |
| 155 | $ref = new ReflectionClass(__CLASS__); | 136 | } |
| 156 | $consts = $ref->getConstants(); | ||
| 157 | foreach ($consts as $const => $value) { | ||
| 158 | if (substr($const, 0, 4) == 'ELE_') { | ||
| 159 | $fields[$const] = $value; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | 137 | ||
| 163 | return $fields; | 138 | /** |
| 164 | } | 139 | * Determine if a user is a site administrator |
| 140 | * @param {Integer|String} $user Username or ID of user to lookup (or false for current user) | ||
| 141 | * @returns {Boolean} | ||
| 142 | */ | ||
| 143 | function is_admin($user = false) { | ||
| 144 | return (get_user_role($user) == 'administrator' ? true : false); | ||
| 165 | } | 145 | } |
| 166 | 146 | ||
| 167 | /** | 147 | /** |
| 168 | * Aministration control | 148 | * Get a lookup of all the forum elements |
| 149 | * @returns {Array} An associative array of the forum elemnts name/values | ||
| 169 | */ | 150 | */ |
| 170 | class PagePermissionsAdmin { | 151 | function getFieldNames() { |
| 171 | const CAPABILITY = 'manage_page_permissions'; | 152 | static $fields = false; |
| 172 | const ADMIN_PAGE = 'page-permission-settings'; | 153 | if (false !== $fields) { |
| 173 | const SUBMIT_HOOK = 'update_def_page_permissions'; | 154 | return $fields; |
| 174 | const SETTING_NS = 'page_permission_defaults'; | 155 | } |
| 175 | 156 | ||
| 176 | public static function make() { | 157 | $fields = Array(); |
| 177 | static $made = false; | 158 | /* |
| 178 | if ($made) { | 159 | $ref = new ReflectionClass(__CLASS__); |
| 179 | throw new OverflowException('make has already beed called'); | 160 | $consts = $ref->getConstants(); |
| 161 | */ | ||
| 162 | // Need to do this since 5.3, namespace instead of Class, can't reflect namespaces | ||
| 163 | $consts = Array( | ||
| 164 | 'ELE_SEL' => 'general_access' | ||
| 165 | , 'ELE_CUST' => 'roles' | ||
| 166 | , 'ELE_AUTH' => 'message_auth' | ||
| 167 | , 'ELE_CUST_AUTH' => 'message_cust_auth' | ||
| 168 | , 'ELE_DENIED' => 'message_cust_denied' | ||
| 169 | ); | ||
| 170 | |||
| 171 | foreach ($consts as $const => $value) { | ||
| 172 | if (substr($const, 0, 4) == 'ELE_') { | ||
| 173 | $fields[$const] = $value; | ||
| 180 | } | 174 | } |
| 181 | $made = true; | 175 | } |
| 182 | 176 | ||
| 183 | TzTools::import('ClientSettings'); | 177 | return $fields; |
| 178 | } | ||
| 184 | 179 | ||
| 185 | $role = get_role('administrator'); | 180 | namespace Tz\WordPress\Tools\PagePermissions\Admin; |
| 186 | $role->add_cap(self::CAPABILITY); | ||
| 187 | 181 | ||
| 188 | add_filters('PagePermissionsAdmin_Filters'); | 182 | use Tz\WordPress\Tools; |
| 183 | use Tz\WordPress\Tools\PagePermissions; | ||
| 184 | use Tz\WordPress\Tools\ClientSettings; | ||
| 189 | 185 | ||
| 190 | if (isset($_POST[self::SUBMIT_HOOK]) && current_user_can(self::CAPABILITY)) { | 186 | use \WP_Option; |
| 191 | self::submit(); | ||
| 192 | } | ||
| 193 | 187 | ||
| 194 | add_actions('PagePermissions_Actions'); | 188 | const CAPABILITY = 'manage_page_permissions'; |
| 195 | } | 189 | const ADMIN_PAGE = 'page-permission-settings'; |
| 190 | const SUBMIT_HOOK = 'update_def_page_permissions'; | ||
| 191 | const SETTING_NS = 'page_permission_defaults'; | ||
| 192 | |||
| 193 | function make() { | ||
| 194 | Tools\import('ClientSettings'); | ||
| 195 | |||
| 196 | $role = get_role('administrator'); | ||
| 197 | $role->add_cap(CAPABILITY); | ||
| 196 | 198 | ||
| 197 | public static function viewOptionsPage() { | 199 | Tools\add_filters(__NAMESPACE__ . '\Filters'); |
| 198 | $selected = self::getOptions(); | ||
| 199 | 200 | ||
| 200 | include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'settings.php'); | 201 | if (isset($_POST[SUBMIT_HOOK]) && current_user_can(CAPABILITY)) { |
| 202 | submit(); | ||
| 201 | } | 203 | } |
| 202 | 204 | ||
| 203 | public static function viewMetaBox($post, $box_info) { | 205 | Tools\add_actions(__NAMESPACE__ . '\Actions'); |
| 204 | $selected = ($post->ID == 0 ? self::getOptions() : array_shift(get_post_meta($post->ID, PagePermissions::META))); | 206 | } |
| 205 | 207 | ||
| 206 | // If the post doesn't have the field saved get defaults | 208 | function viewOptionsPage() { |
| 207 | if (is_null($selected)) { | 209 | $selected = getOptions(); |
| 208 | $selected = self::getOptions(); | ||
| 209 | } | ||
| 210 | 210 | ||
| 211 | include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php'); | 211 | include(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'settings.php'); |
| 212 | } | 212 | } |
| 213 | 213 | ||
| 214 | /** | 214 | function viewMetaBox($post, $box_info) { |
| 215 | * Handles saving data when a post/page is saved | 215 | $selected = ($post->ID == 0 ? getOptions() : array_shift(get_post_meta($post->ID, PagePermissions\META))); |
| 216 | */ | ||
| 217 | public static function submit() { | ||
| 218 | unset($_POST[self::SUBMIT_HOOK]); | ||
| 219 | |||
| 220 | $options = self::getOptions(); | ||
| 221 | $fields = PagePermissions::getFieldNames(); | ||
| 222 | foreach ($fields as $field) { | ||
| 223 | if (isset($_POST[$field])) { | ||
| 224 | |||
| 225 | // This should probably be done via a recursive fn call or array_walk or something | ||
| 226 | if (is_array($_POST[$field])) { | ||
| 227 | $options[$field] = Array(); | ||
| 228 | foreach ($_POST[$field] as $key => $val) { | ||
| 229 | $options[$field][$key] = stripslashes($_POST[$field]); | ||
| 230 | } | ||
| 231 | } else { | ||
| 232 | // not sure if stripslashes should go here or in WP_Options | ||
| 233 | $options[$field] = stripslashes($_POST[$field]); | ||
| 234 | } | ||
| 235 | } else { | ||
| 236 | $options[$field] = ''; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | 216 | ||
| 240 | $options->save(); | 217 | // If the post doesn't have the field saved get defaults |
| 218 | if (is_null($selected)) { | ||
| 219 | $selected = getOptions(); | ||
| 241 | } | 220 | } |
| 242 | 221 | ||
| 243 | public static function getOptions() { | 222 | include(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php'); |
| 244 | static $options = false; | 223 | } |
| 245 | if (false !== $options) { | 224 | |
| 246 | return $options; | 225 | /** |
| 226 | * Handles saving data when a post/page is saved | ||
| 227 | */ | ||
| 228 | function submit() { | ||
| 229 | unset($_POST[SUBMIT_HOOK]); | ||
| 230 | |||
| 231 | $options = getOptions(); | ||
| 232 | $fields = PagePermissions\getFieldNames(); | ||
| 233 | foreach ($fields as $field) { | ||
| 234 | if (isset($_POST[$field])) { | ||
| 235 | $options[$field] = array_stripslashes($_POST[$field]); | ||
| 236 | } else { | ||
| 237 | $options[$field] = ''; | ||
| 247 | } | 238 | } |
| 239 | } | ||
| 240 | |||
| 241 | $options->save(); | ||
| 242 | } | ||
| 243 | |||
| 244 | function array_stripslashes($value) { | ||
| 245 | return (is_array($value) ? array_map(__FUNCTION__, $value) : stripslashes($value)); | ||
| 246 | } | ||
| 248 | 247 | ||
| 249 | $options = new WP_Option(self::SETTING_NS); | 248 | function getOptions() { |
| 249 | static $options = false; | ||
| 250 | if (false !== $options) { | ||
| 250 | return $options; | 251 | return $options; |
| 251 | } | 252 | } |
| 253 | |||
| 254 | $options = new WP_Option(SETTING_NS); | ||
| 255 | return $options; | ||
| 252 | } | 256 | } |
| 253 | 257 | ||
| 254 | /** | 258 | /** |
| 255 | * Each method is a handler for it's WordPress `add_action` namesake | 259 | * Each method is a handler for it's WordPress `add_action` namesake |
| 256 | */ | 260 | */ |
| 257 | class PagePermissions_Actions { | 261 | class Actions { |
| 258 | public static function init() { | 262 | public static function init() { |
| 259 | register_taxonomy(PagePermissions::META, 'attachment', Array('hierarachical' => false, 'label' => 'Page Permissions', 'query_var' => false)); | 263 | register_taxonomy(PagePermissions\META, 'attachment', Array('hierarachical' => false, 'label' => 'Page Permissions', 'query_var' => false)); |
| 260 | 264 | ||
| 261 | $file = $_SERVER['REQUEST_URI']; | 265 | $file = $_SERVER['REQUEST_URI']; |
| 262 | if ($file == '/wp-admin/media-new.php' && !PagePermissions::is_admin()) { | 266 | if ($file == '/wp-admin/media-new.php' && !PagePermissions\is_admin()) { |
| 263 | header("Location: " . $file . "?flash=0"); | 267 | header("Location: " . $file . "?flash=0"); |
| 264 | } | 268 | } |
| 265 | 269 | ||
| ... | @@ -267,49 +271,49 @@ class PagePermissions_Actions { | ... | @@ -267,49 +271,49 @@ class PagePermissions_Actions { |
| 267 | // Ideally this is in its own `edit_attachment` method...but that isn't working | 271 | // Ideally this is in its own `edit_attachment` method...but that isn't working |
| 268 | if (isset($_POST['action']) && $_POST['action'] == 'editattachment') { | 272 | if (isset($_POST['action']) && $_POST['action'] == 'editattachment') { |
| 269 | $real_id = $_POST['attachment_id']; | 273 | $real_id = $_POST['attachment_id']; |
| 270 | $current = array_shift(get_post_meta($real_id, PagePermissions::META)); | 274 | $current = array_shift(get_post_meta($real_id, PagePermissions\META)); |
| 271 | 275 | ||
| 272 | $new = Array(); | 276 | $new = Array(); |
| 273 | $new[PagePermissions::ELE_SEL] = $_POST[PagePermissions::ELE_SEL]; | 277 | $new[PagePermissions\ELE_SEL] = $_POST[PagePermissions\ELE_SEL]; |
| 274 | if (isset($_POST[PagePermissions::ELE_CUST])) { | 278 | if (isset($_POST[PagePermissions\ELE_CUST])) { |
| 275 | $new[PagePermissions::ELE_CUST] = $_POST[PagePermissions::ELE_CUST]; | 279 | $new[PagePermissions\ELE_CUST] = $_POST[PagePermissions\ELE_CUST]; |
| 276 | } else { | 280 | } else { |
| 277 | $new[PagePermissions::ELE_CUST] = Array(); | 281 | $new[PagePermissions\ELE_CUST] = Array(); |
| 278 | } | 282 | } |
| 279 | 283 | ||
| 280 | if (is_null($current)) { | 284 | if (is_null($current)) { |
| 281 | add_post_meta($real_id, PagePermissions::META, $new, true); | 285 | add_post_meta($real_id, PagePermissions\META, $new, true); |
| 282 | } else { | 286 | } else { |
| 283 | update_post_meta($real_id, PagePermissions::META, $new); | 287 | update_post_meta($real_id, PagePermissions\META, $new); |
| 284 | } | 288 | } |
| 285 | } | 289 | } |
| 286 | } | 290 | } |
| 287 | 291 | ||
| 288 | public static function admin_menu() { | 292 | public static function admin_menu() { |
| 289 | if (current_user_can(ClientSettings::CAPABILITY)) { | 293 | if (current_user_can(ClientSettings\CAPABILITY)) { |
| 290 | add_submenu_page(ClientSettings::ADMIN_PAGE, 'Permission Defaults', 'Permission Defaults', PagePermissionsAdmin::CAPABILITY, PagePermissionsAdmin::ADMIN_PAGE, Array('PagePermissionsAdmin', 'viewOptionsPage')); | 294 | add_submenu_page(ClientSettings\ADMIN_PAGE, 'Permission Defaults', 'Permission Defaults', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\viewOptionsPage'); |
| 291 | add_meta_box('page_permissions', 'Page Permissions', Array('PagePermissionsAdmin', 'viewMetaBox'), 'page', 'side', 'low'); | 295 | add_meta_box('page_permissions', 'Page Permissions', __NAMESPACE__ . '\viewMetaBox', 'page', 'side', 'low'); |
| 292 | } | 296 | } |
| 293 | } | 297 | } |
| 294 | 298 | ||
| 295 | public static function admin_print_scripts() { | 299 | public static function admin_print_scripts() { |
| 296 | $innerhtml = ''; | 300 | $innerhtml = ''; |
| 297 | if ('0' !== ($change_field = (isset($_GET['attachment_id']) ? 'attachments[' . $_GET['attachment_id'] . '][' . PagePermissions::META . ']' : '0'))) { | 301 | if ('0' !== ($change_field = (isset($_GET['attachment_id']) ? 'attachments[' . $_GET['attachment_id'] . '][' . PagePermissions\META . ']' : '0'))) { |
| 298 | $selected = array_shift(get_post_meta($_GET['attachment_id'], PagePermissions::META)); | 302 | $selected = array_shift(get_post_meta($_GET['attachment_id'], PagePermissions\META)); |
| 299 | if (is_null($selected)) { | 303 | if (is_null($selected)) { |
| 300 | $selected = PagePermissionsAdmin::getOptions(); | 304 | $selected = getOptions(); |
| 301 | } | 305 | } |
| 302 | 306 | ||
| 303 | ob_start(); | 307 | ob_start(); |
| 304 | require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php'); | 308 | require(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php'); |
| 305 | $innerhtml = ob_get_contents(); | 309 | $innerhtml = ob_get_contents(); |
| 306 | ob_end_clean(); | 310 | ob_end_clean(); |
| 307 | } | 311 | } |
| 308 | 312 | ||
| 309 | _enqueue_script('page-permissions', TzTools::tools_url('PagePermissions.js', __FILE__)); | 313 | _enqueue_script('page-permissions', Tools\url('PagePermissions.js', __FILE__)); |
| 310 | _localize_script('page-permissions', 'TzPagePermissionsData', Array( | 314 | _localize_script('page-permissions', 'TzPagePermissionsData', Array( |
| 311 | 'trigger' => PagePermissions::ELE_SEL | 315 | 'trigger' => PagePermissions\ELE_SEL |
| 312 | , 'focus' => PagePermissions::OPT_CUST | 316 | , 'focus' => PagePermissions\OPT_CUST |
| 313 | , 'change_field' => $change_field | 317 | , 'change_field' => $change_field |
| 314 | , 'innerHTML' => rawurlencode($innerhtml) | 318 | , 'innerHTML' => rawurlencode($innerhtml) |
| 315 | )); | 319 | )); |
| ... | @@ -323,42 +327,34 @@ class PagePermissions_Actions { | ... | @@ -323,42 +327,34 @@ class PagePermissions_Actions { |
| 323 | if (false === ($real_id = _is_post_revision($post_id))) { | 327 | if (false === ($real_id = _is_post_revision($post_id))) { |
| 324 | $real_id = $post_id; | 328 | $real_id = $post_id; |
| 325 | } | 329 | } |
| 326 | $current = array_shift(get_post_meta($real_id, PagePermissions::META)); | 330 | $current = array_shift(get_post_meta($real_id, PagePermissions\META)); |
| 327 | 331 | ||
| 328 | $new = Array(); | 332 | $new = Array(); |
| 329 | $new[PagePermissions::ELE_SEL] = $_POST[PagePermissions::ELE_SEL]; | 333 | $new[PagePermissions\ELE_SEL] = $_POST[PagePermissions\ELE_SEL]; |
| 330 | if (isset($_POST[PagePermissions::ELE_CUST])) { | 334 | if (isset($_POST[PagePermissions\ELE_CUST])) { |
| 331 | $new[PagePermissions::ELE_CUST] = $_POST[PagePermissions::ELE_CUST]; | 335 | $new[PagePermissions\ELE_CUST] = $_POST[PagePermissions\ELE_CUST]; |
| 332 | } else { | 336 | } else { |
| 333 | $new[PagePermissions::ELE_CUST] = Array(); | 337 | $new[PagePermissions\ELE_CUST] = Array(); |
| 334 | } | 338 | } |
| 335 | 339 | ||
| 336 | if (is_null($current)) { | 340 | if (is_null($current)) { |
| 337 | add_post_meta($real_id, PagePermissions::META, $new, true); | 341 | add_post_meta($real_id, PagePermissions\META, $new, true); |
| 338 | } else { | 342 | } else { |
| 339 | update_post_meta($real_id, PagePermissions::META, $new); | 343 | update_post_meta($real_id, PagePermissions\META, $new); |
| 340 | } | 344 | } |
| 341 | } | 345 | } |
| 342 | } | 346 | } |
| 343 | 347 | ||
| 344 | class PagePermissionsAdmin_Filters { | 348 | class Filters { |
| 345 | public static function image_upload_iframe_src($result) { | 349 | public static function flash_uploader() { |
| 346 | return $result . '&flash=0'; | 350 | return false; |
| 347 | } | ||
| 348 | |||
| 349 | public static function video_upload_iframe_src($result) { | ||
| 350 | return $result . '&flash=0'; | ||
| 351 | } | ||
| 352 | |||
| 353 | public static function audio_upload_iframe_src($result) { | ||
| 354 | return $result . '&flash=0'; | ||
| 355 | } | 351 | } |
| 356 | } | 352 | } |
| 357 | 353 | ||
| 358 | if (isset($_POST['tz_pp_ajax'])) { | 354 | if (isset($_POST['tz_pp_ajax'])) { |
| 359 | PagePermissions::initAjax(); | 355 | PagePermissions\initAjax(); |
| 360 | } else { | 356 | } else { |
| 361 | PagePermissions::init(); | 357 | PagePermissions\make(); |
| 362 | PagePermissionsAdmin::make(); | 358 | make(); |
| 363 | } | 359 | } |
| 364 | ?> | 360 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | |||
| 3 | namespace Tz\WordPress\Tools\PagePermissions; | ||
| 4 | |||
| 5 | use \WP_Roles; | ||
| 6 | use \UnderflowException; | ||
| 7 | |||
| 2 | if (!isset($selected)) { | 8 | if (!isset($selected)) { |
| 3 | throw new UnderflowException('Current Page Permission settings not provided'); | 9 | throw new UnderflowException('Current Page Permission settings not provided'); |
| 4 | } | 10 | } |
| 5 | 11 | ||
| 6 | $opt_selected = $selected[PagePermissions::ELE_SEL]; | 12 | $opt_selected = $selected[ELE_SEL]; |
| 7 | ?> | 13 | ?> |
| 8 | 14 | ||
| 9 | <select id="<?php echo PagePermissions::ELE_SEL; ?>" name="<?php echo PagePermissions::ELE_SEL; ?>"> | 15 | <select id="<?php echo ELE_SEL; ?>" name="<?php echo ELE_SEL; ?>"> |
| 10 | <option value="<?php echo PagePermissions::OPT_ALL . '"' . (PagePermissions::OPT_ALL == $opt_selected ? ' selected' : ''); ?>>Anyone</option> | 16 | <option value="<?php echo OPT_ALL . '"' . (OPT_ALL == $opt_selected ? ' selected' : ''); ?>>Anyone</option> |
| 11 | <option value="<?php echo PagePermissions::OPT_AUTH . '"' . (PagePermissions::OPT_AUTH == $opt_selected ? ' selected' : ''); ?>>Must be Logged In</option> | 17 | <option value="<?php echo OPT_AUTH . '"' . (OPT_AUTH == $opt_selected ? ' selected' : ''); ?>>Must be Logged In</option> |
| 12 | <option value="<?php echo PagePermissions::OPT_CUST . '"' . (PagePermissions::OPT_CUST == $opt_selected ? ' selected' : ''); ?>>Specific Roles</option> | 18 | <option value="<?php echo OPT_CUST . '"' . (OPT_CUST == $opt_selected ? ' selected' : ''); ?>>Specific Roles</option> |
| 13 | </select> | 19 | </select> |
| 14 | 20 | ||
| 15 | <div id="TzSpecific"> | 21 | <div id="TzSpecific"> |
| ... | @@ -20,8 +26,8 @@ | ... | @@ -20,8 +26,8 @@ |
| 20 | unset($rc, $roles['administrator']); | 26 | unset($rc, $roles['administrator']); |
| 21 | 27 | ||
| 22 | foreach ($roles as $key => $display) { | 28 | foreach ($roles as $key => $display) { |
| 23 | $checked = (isset($selected[PagePermissions::ELE_CUST][$key]) ? ' checked' : ''); | 29 | $checked = (isset($selected[ELE_CUST][$key]) ? ' checked' : ''); |
| 24 | echo '<br /><input type="checkbox" id="' . $key . '" name="' . PagePermissions::ELE_CUST . '[' . $key . ']" value="1"' . $checked . ' />'; | 30 | echo '<br /><input type="checkbox" id="' . $key . '" name="' . ELE_CUST . '[' . $key . ']" value="1"' . $checked . ' />'; |
| 25 | echo '<label for="' . $key . '">' . $display . '</label>'; | 31 | echo '<label for="' . $key . '">' . $display . '</label>'; |
| 26 | } | 32 | } |
| 27 | ?> | 33 | ?> | ... | ... |
| 1 | <?php /* | 1 | <?php |
| 2 | |||
| 3 | namespace Tz\WordPress\Tools\PagePermissions; | ||
| 4 | |||
| 5 | use Tz\WordPress\Tools\PagePermissions\Admin; | ||
| 6 | |||
| 7 | /* | ||
| 2 | Idea: Checkbox beside each textarea with option to include registration forum or not | 8 | Idea: Checkbox beside each textarea with option to include registration forum or not |
| 3 | */ ?> | 9 | */ ?> |
| 4 | 10 | ||
| 5 | |||
| 6 | <div class="wrap"> | 11 | <div class="wrap"> |
| 7 | <?php screen_icon(); ?> | 12 | <?php screen_icon(); ?> |
| 8 | <h2>Permission Defaults</h2> | 13 | <h2>Permission Defaults</h2> |
| 9 | 14 | ||
| 10 | <form method="post"> | 15 | <form method="post"> |
| 11 | <input type="hidden" name="<?php echo PagePermissionsAdmin::SUBMIT_HOOK; ?>" value="1" /> | 16 | <input type="hidden" name="<?php echo Admin\SUBMIT_HOOK; ?>" value="1" /> |
| 12 | 17 | ||
| 13 | <h3>Default Option</h3> | 18 | <h3>Default Option</h3> |
| 14 | <?php require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'form.php'); ?> | 19 | <?php require(__DIR__ . DIRECTORY_SEPARATOR . 'form.php'); ?> |
| 15 | 20 | ||
| 16 | <hr /> | 21 | <hr /> |
| 17 | 22 | ||
| ... | @@ -19,18 +24,18 @@ Idea: Checkbox beside each textarea with option to include registration forum or | ... | @@ -19,18 +24,18 @@ Idea: Checkbox beside each textarea with option to include registration forum or |
| 19 | <h6>(Messages are displayed for given criteria when page permission is not met)</h6> | 24 | <h6>(Messages are displayed for given criteria when page permission is not met)</h6> |
| 20 | 25 | ||
| 21 | <p> | 26 | <p> |
| 22 | <label for="<?php echo PagePermissions::ELE_AUTH; ?>" />"Must be Logged In" message for un-authenticated visitors</label> | 27 | <label for="<?php echo ELE_AUTH; ?>" />"Must be Logged In" message for un-authenticated visitors</label> |
| 23 | <br /><textarea id="<?php echo PagePermissions::ELE_AUTH; ?>" name="<?php echo PagePermissions::ELE_AUTH; ?>"><?php echo $selected[PagePermissions::ELE_AUTH]; ?></textarea> | 28 | <br /><textarea id="<?php echo ELE_AUTH; ?>" name="<?php echo ELE_AUTH; ?>"><?php echo $selected[ELE_AUTH]; ?></textarea> |
| 24 | </p> | 29 | </p> |
| 25 | 30 | ||
| 26 | <p> | 31 | <p> |
| 27 | <label for="<?php echo PagePermissions::ELE_CUST_AUTH; ?>" />"Only Specific Roles" message for un-authenticated visitors</label> | 32 | <label for="<?php echo ELE_CUST_AUTH; ?>" />"Only Specific Roles" message for un-authenticated visitors</label> |
| 28 | <br /><textarea id="<?php echo PagePermissions::ELE_CUST_AUTH; ?>" name="<?php echo PagePermissions::ELE_CUST_AUTH; ?>"><?php echo $selected[PagePermissions::ELE_CUST_AUTH]; ?></textarea> | 33 | <br /><textarea id="<?php echo ELE_CUST_AUTH; ?>" name="<?php echo ELE_CUST_AUTH; ?>"><?php echo $selected[ELE_CUST_AUTH]; ?></textarea> |
| 29 | </p> | 34 | </p> |
| 30 | 35 | ||
| 31 | <p> | 36 | <p> |
| 32 | <label for="<?php echo PagePermissions::ELE_DENIED; ?>" />"Only Specific Roles" message for authenticated users without sufficient privelages</label> | 37 | <label for="<?php echo ELE_DENIED; ?>" />"Only Specific Roles" message for authenticated users without sufficient privelages</label> |
| 33 | <br /><textarea id="<?php echo PagePermissions::ELE_DENIED; ?>" name="<?php echo PagePermissions::ELE_DENIED; ?>"><?php echo $selected[PagePermissions::ELE_DENIED]; ?></textarea> | 38 | <br /><textarea id="<?php echo ELE_DENIED; ?>" name="<?php echo ELE_DENIED; ?>"><?php echo $selected[ELE_DENIED]; ?></textarea> |
| 34 | </p> | 39 | </p> |
| 35 | 40 | ||
| 36 | <p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p> | 41 | <p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p> | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | class ShortCodes extends Singleton { | ||
| 3 | private static $registered = Array(); | ||
| 4 | private static $private = Array(); | ||
| 5 | 2 | ||
| 6 | public static function make() { | 3 | namespace Tz\WordPress\Tools\ShortCodes; |
| 7 | add_actions('ShortCodes_Actions'); | ||
| 8 | } | ||
| 9 | 4 | ||
| 10 | public static function registerClass($class) { | 5 | use Tz\WordPress\Tools; |
| 11 | if (!class_exists($class)) { | ||
| 12 | throw new Exception("$class does not exist"); | ||
| 13 | } | ||
| 14 | 6 | ||
| 15 | $ref = new ReflectionClass($class); | 7 | use \ReflectionClass, \ReflectionMethod, \ReflectionFunction; |
| 16 | $methods = $ref->getMethods(ReflectionMethod::IS_STATIC); | 8 | use \Exception; |
| 17 | foreach ($methods as $method) { | ||
| 18 | self::add($method->name, Array($class, $method->name)); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | 9 | ||
| 22 | public static function add($code, $callback) { | 10 | Tools\add_actions(__NAMESPACE__ . '\Actions'); |
| 23 | $fn = (function_exists('wp_add_shortcode') ? 'wp_add_shortcode' : 'add_shortcode'); | ||
| 24 | call_user_func($fn, $code, $callback); | ||
| 25 | 11 | ||
| 26 | if (is_admin()) { | 12 | if (function_exists('rename_function')) { |
| 27 | self::$registered[$code] = Array('code' => $code, 'title' => $code, 'params' => Array(), 'uses_content' => 0); | 13 | rename_function('add_shortcode', 'wp_add_shortcode'); |
| 28 | 14 | ||
| 29 | if (is_array($callback)) { | 15 | function add_shortcode() { |
| 30 | $ref = new ReflectionMethod($callback[0], $callback[1]); | 16 | $args = func_get_args(); |
| 31 | } else { | 17 | call_user_func_array('add', $args); |
| 32 | $ref = new ReflectionFunction($callback); | ||
| 33 | } | ||
| 34 | $api = $ref->getDocComment(); | ||
| 35 | $api = explode("\n", $api); | ||
| 36 | array_shift($api); | ||
| 37 | array_pop($api); | ||
| 38 | |||
| 39 | foreach ($api as $key => &$comment) { | ||
| 40 | $comment = trim($comment, ' *'); | ||
| 41 | |||
| 42 | if (substr($comment, 0, 1) == '@') { | ||
| 43 | $tag = trim(substr($comment, 1, strpos($comment, ' '))); | ||
| 44 | if (empty($tag)) { | ||
| 45 | $tag = trim($comment, '@'); | ||
| 46 | } | ||
| 47 | |||
| 48 | if (method_exists(__CLASS__, 'parseTag_' . $tag)) { | ||
| 49 | call_user_func_array(Array(__CLASS__, 'parseTag_' . $tag), Array($code, $comment)); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | 18 | } |
| 54 | } | 19 | } |
| 55 | 20 | ||
| 56 | public static function parseTag_display($code, $string) { | 21 | function add_shortcodes($class) { |
| 57 | $string = trim(str_replace('@display', '', $string)); | 22 | if (!class_exists($class)) { |
| 58 | self::$registered[$code]['title'] = $string; | 23 | throw new Exception("$class does not exist"); |
| 59 | } | 24 | } |
| 60 | 25 | ||
| 61 | public static function parseTag_param($code, $string) { | 26 | $ref = new ReflectionClass($class); |
| 62 | $regex = '.*?@param {((?:[a-z][a-z]+))(\\(.*?\\))?} (.*?) (.*?)$'; // Awww yeah! | 27 | $methods = $ref->getMethods(ReflectionMethod::IS_STATIC); |
| 63 | if ($num = preg_match_all("/" . $regex . "/is", $string, $matches)) { | 28 | foreach ($methods as $method) { |
| 64 | self::$registered[$code]['params'][] = Array('name' => $matches[3][0], 'type' => $matches[1][0], 'options' => explode(',', trim($matches[2][0], ')(')), 'desc' => $matches[4][0]); | 29 | add($method->name, Array($class, $method->name)); |
| 65 | } | ||
| 66 | } | 30 | } |
| 31 | } | ||
| 32 | |||
| 33 | function registerClass($class) { | ||
| 34 | call_user_func('add_shortcodes', $class); | ||
| 35 | } | ||
| 36 | |||
| 37 | function add($code, $callback) { | ||
| 38 | $fn = (function_exists('wp_add_shortcode') ? 'wp_add_shortcode' : 'add_shortcode'); | ||
| 39 | call_user_func($fn, $code, $callback); | ||
| 40 | |||
| 41 | if (is_admin()) { // I may want to remove this condition... | ||
| 42 | Vars::$registered[$code] = Array('code' => $code, 'title' => $code, 'params' => Array(), 'uses_content' => 0); | ||
| 67 | 43 | ||
| 68 | public static function parseTag_private($code, $string) { | 44 | if (is_array($callback)) { |
| 69 | self::$private[$code] = 1; | 45 | $ref = new ReflectionMethod($callback[0], $callback[1]); |
| 46 | } else { | ||
| 47 | $ref = new ReflectionFunction($callback); | ||
| 48 | } | ||
| 49 | $api = $ref->getDocComment(); | ||
| 50 | $api = explode("\n", $api); | ||
| 51 | array_shift($api); | ||
| 52 | array_pop($api); | ||
| 53 | |||
| 54 | foreach ($api as $key => &$comment) { | ||
| 55 | $comment = trim($comment, ' *'); | ||
| 56 | |||
| 57 | if (substr($comment, 0, 1) == '@') { | ||
| 58 | $tag = trim(substr($comment, 1, strpos($comment, ' '))); | ||
| 59 | if (empty($tag)) { | ||
| 60 | $tag = trim($comment, '@'); | ||
| 61 | } | ||
| 62 | |||
| 63 | if (function_exists('parseTag_' . $tag)) { | ||
| 64 | call_user_func_array('parseTag_' . $tag, Array($code, $comment)); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 70 | } | 68 | } |
| 69 | } | ||
| 70 | |||
| 71 | function parseTag_display($code, $string) { | ||
| 72 | $string = trim(str_replace('@display', '', $string)); | ||
| 73 | Vars::$registered[$code]['title'] = $string; | ||
| 74 | } | ||
| 71 | 75 | ||
| 72 | public static function uses_content($code) { | 76 | function parseTag_param($code, $string) { |
| 73 | self::$registered[$code]['uses_content'] = 1; | 77 | $regex = '.*?@param {((?:[a-z][a-z]+))(\\(.*?\\))?} (.*?) (.*?)$'; // Awww yeah! |
| 78 | if ($num = preg_match_all("/" . $regex . "/is", $string, $matches)) { | ||
| 79 | Vars::$registered[$code]['params'][] = Array('name' => $matches[3][0], 'type' => $matches[1][0], 'options' => explode(',', trim($matches[2][0], ')(')), 'desc' => $matches[4][0]); | ||
| 74 | } | 80 | } |
| 81 | } | ||
| 75 | 82 | ||
| 76 | public static function getRegistered() { | 83 | function parseTag_private($code, $string) { |
| 77 | $return = self::$registered; | 84 | Vars::$private[$code] = 1; |
| 78 | foreach (self::$private as $key => $one) { | 85 | } |
| 79 | unset($return[$key]); | 86 | |
| 80 | } | 87 | function uses_content($code) { |
| 88 | Vars::$registered[$code]['uses_content'] = 1; | ||
| 89 | } | ||
| 81 | 90 | ||
| 82 | return $return; | 91 | function getRegistered() { |
| 92 | $return = Vars::$registered; | ||
| 93 | foreach (Vars::$private as $key => $one) { | ||
| 94 | unset($return[$key]); | ||
| 83 | } | 95 | } |
| 84 | 96 | ||
| 85 | public static function drawMetaBox() { | 97 | return $return; |
| 98 | } | ||
| 99 | |||
| 100 | function drawMetaBox() { | ||
| 86 | ?> | 101 | ?> |
| 87 | <label for="TzShortCodeList">Tag:</label> | 102 | <label for="TzShortCodeList">Tag:</label> |
| 88 | <select id="TzShortCodeList" name="TzShortCodeList"> | 103 | <select id="TzShortCodeList" name="TzShortCodeList"> |
| 89 | <?php | 104 | <?php |
| 90 | $options = ShortCodes::getRegistered(); | 105 | $options = getRegistered(); |
| 91 | ksort($options); | 106 | ksort($options); |
| 92 | foreach ($options as $tag => $data) { | 107 | foreach ($options as $tag => $data) { |
| 93 | echo '<option value="' . $tag . '">' . $data['title'] . '</option>'; | 108 | echo '<option value="' . $tag . '">' . $data['title'] . '</option>'; |
| ... | @@ -99,42 +114,27 @@ class ShortCodes extends Singleton { | ... | @@ -99,42 +114,27 @@ class ShortCodes extends Singleton { |
| 99 | <input type="button" id="TzInsertSC" value="<?php _e('Insert into post'); ?>" /> | 114 | <input type="button" id="TzInsertSC" value="<?php _e('Insert into post'); ?>" /> |
| 100 | </p> | 115 | </p> |
| 101 | <?php | 116 | <?php |
| 102 | } | ||
| 103 | } | 117 | } |
| 104 | 118 | ||
| 105 | class ShortCodes_Actions { | 119 | class Actions { |
| 106 | public static function admin_menu() { | 120 | public static function admin_menu() { |
| 107 | add_meta_box('TzShortCodes', 'Code Helper', Array('ShortCodes', 'drawMetaBox'), 'post', 'normal'); | 121 | add_meta_box('TzShortCodes', 'Code Helper', __NAMESPACE__ . '\drawMetaBox', 'post', 'normal'); |
| 108 | add_meta_box('TzShortCodes', 'Code Helper', Array('ShortCodes', 'drawMetaBox'), 'page', 'normal'); | 122 | add_meta_box('TzShortCodes', 'Code Helper', __NAMESPACE__ . '\drawMetaBox', 'page', 'normal'); |
| 109 | } | 123 | } |
| 110 | 124 | ||
| 111 | public static function admin_print_scripts() { | 125 | public static function admin_print_scripts() { |
| 112 | if ($GLOBALS['editing']) { | 126 | if ($GLOBALS['editing']) { |
| 113 | _enqueue_script('shortcoder', TzTools::tools_url('shortcoder.js', __FILE__), Array('jquery')); | 127 | _enqueue_script('shortcoder', Tools\url('shortcoder.js', __FILE__), Array('jquery')); |
| 114 | 128 | ||
| 115 | echo "<script type=\"text/javascript\">\n/* <![CDATA[ */\n"; | 129 | echo "<script type=\"text/javascript\">\n/* <![CDATA[ */\n"; |
| 116 | echo 'var TzRegisteredShortCodes = ' . json_encode(ShortCodes::getRegistered()); | 130 | echo 'var TzRegisteredShortCodes = ' . json_encode(getRegistered()); |
| 117 | echo "\n/* ]]> */</script>\n"; | 131 | echo "\n/* ]]> */</script>\n"; |
| 118 | } | 132 | } |
| 119 | } | 133 | } |
| 120 | } | 134 | } |
| 121 | 135 | ||
| 122 | /** | 136 | class Vars { |
| 123 | * @deprecated | 137 | public static $registered = Array(); |
| 124 | */ | 138 | public static $private = Array(); |
| 125 | function add_shortcodes($class) { | ||
| 126 | call_user_func(Array('ShortCodes', 'registerClass'), $class); | ||
| 127 | } | 139 | } |
| 128 | |||
| 129 | |||
| 130 | if (function_exists('rename_function')) { | ||
| 131 | rename_function('add_shortcode', 'wp_add_shortcode'); | ||
| 132 | |||
| 133 | function add_shortcode() { | ||
| 134 | $args = func_get_args(); | ||
| 135 | call_user_func_array(Array('ShortCodes', 'add'), $args); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | ShortCodes::make(); | ||
| 140 | ?> | 140 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | |||
| 3 | namespace Tz; | ||
| 4 | |||
| 5 | use \Exception; | ||
| 6 | |||
| 2 | abstract class Validation { | 7 | abstract class Validation { |
| 3 | /** | 8 | /** |
| 4 | * Associative array of valid fields | 9 | * Associative array of valid fields |
| ... | @@ -42,4 +47,4 @@ abstract class Validation { | ... | @@ -42,4 +47,4 @@ abstract class Validation { |
| 42 | } | 47 | } |
| 43 | } | 48 | } |
| 44 | } | 49 | } |
| 45 | ?> | 50 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | /* | 2 | /* |
| 3 | Plugin Name: Tenzing Tools | 3 | Plugin Name: Tenzing Tools |
| 4 | Version: 0.3b | 4 | Version: 0.4b |
| 5 | Description: Various classes and functions to help out with stuff | 5 | Description: Various classes and functions to help out with stuff |
| 6 | Author: Tenzing | 6 | Author: Tenzing |
| 7 | */ | 7 | */ |
| 8 | 8 | ||
| 9 | TzTools::make(); | 9 | namespace Tz\WordPress\Tools; |
| 10 | 10 | ||
| 11 | class TzTools { | 11 | use \ReflectionClass, \ReflectionMethod; |
| 12 | public static function make() { | 12 | use \Exception; |
| 13 | spl_autoload_register(Array(__CLASS__, 'autoloader')); | ||
| 14 | 13 | ||
| 15 | require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'wp_functions.php'); | 14 | spl_autoload_register(__NAMESPACE__ . '\autoloader'); |
| 16 | 15 | ||
| 17 | _register_script('addEvent', self::tools_url('scripts/addEvent.js', __FILE__)); | 16 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'wp_functions.php'); |
| 18 | _register_script('xmlhttpHandler', self::tools_url('scripts/xmlhttpHandler.js', __FILE__)); | ||
| 19 | _register_script('fireEvent', self::tools_url('scripts/fireEvent.js', __FILE__)); | ||
| 20 | 17 | ||
| 21 | // This is (hopefully) getting canned in 3.0 | 18 | _register_script('addEvent', url('scripts/addEvent.js', __FILE__)); |
| 22 | add_action('widgets_init', Array('MenuWidget', 'init')); | 19 | _register_script('xmlhttpHandler', url('scripts/xmlhttpHandler.js', __FILE__)); |
| 20 | _register_script('fireEvent', url('scripts/fireEvent.js', __FILE__)); | ||
| 23 | 21 | ||
| 24 | self::import('ShortCodes'); | 22 | import('ShortCodes'); |
| 25 | if (defined('TZ_DEBUG') && TZ_DEBUG === true) { | 23 | if (defined('TZ_DEBUG') && TZ_DEBUG === true) { |
| 26 | self::import('Debug'); | 24 | import('Debug'); |
| 27 | } | ||
| 28 | } | 25 | } |
| 29 | 26 | ||
| 30 | public static function import($com) { | 27 | function import($com) { |
| 31 | $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'com' . DIRECTORY_SEPARATOR . $com . DIRECTORY_SEPARATOR; | 28 | $dir = __DIR__ . DIRECTORY_SEPARATOR . 'com' . DIRECTORY_SEPARATOR . $com . DIRECTORY_SEPARATOR; |
| 32 | $file = $dir . $com . '.php'; | 29 | $file = $dir . $com . '.php'; |
| 33 | if (is_dir($dir) && is_file($file)) { | 30 | if (is_dir($dir) && is_file($file)) { |
| 34 | require_once($file); | 31 | require_once($file); |
| 35 | } | ||
| 36 | } | 32 | } |
| 33 | } | ||
| 37 | 34 | ||
| 38 | public static function autoloader($class) { | 35 | function autoloader($class) { |
| 39 | $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php'; | ||
| 40 | if (is_file($file)) { | ||
| 41 | include($file); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | 36 | ||
| 45 | public static function tools_url($script, $base_file = false) { | 37 | $a = explode('\\', $class); |
| 46 | $base_dir = (false === $base_file ? dirname(__FILE__) : dirname($base_file)); | 38 | $class = array_pop($a); |
| 47 | $rel_path = str_replace(ABSPATH, '', $base_dir); | ||
| 48 | $script = site_url() . '/' . $rel_path . '/' . $script; | ||
| 49 | 39 | ||
| 50 | return $script; | 40 | $file = __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php'; |
| 41 | if (is_file($file)) { | ||
| 42 | include($file); | ||
| 51 | } | 43 | } |
| 52 | } | 44 | } |
| 53 | 45 | ||
| 46 | function url($script, $base_file = false) { | ||
| 47 | $base_dir = (false === $base_file ? __DIR__ : dirname($base_file)); | ||
| 48 | $rel_path = str_replace(ABSPATH, '', $base_dir); | ||
| 49 | $script = site_url() . '/' . $rel_path . '/' . $script; | ||
| 50 | |||
| 51 | return $script; | ||
| 52 | } | ||
| 53 | |||
| 54 | function tools_url() { | ||
| 55 | $args = func_get_args(); | ||
| 56 | call_user_func_array(__NAMESPACE__ . '\url', $args); | ||
| 57 | } | ||
| 58 | |||
| 54 | function add_actions($class) { | 59 | function add_actions($class) { |
| 55 | if (!class_exists($class)) { | 60 | if (!class_exists($class)) { |
| 56 | throw new Exception("$class does not exist"); | 61 | throw new Exception("$class does not exist"); | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | |||
| 2 | /** | 3 | /** |
| 3 | * WordPress strongly advises against using functions that start with "wp_" | 4 | * WordPress strongly advises against using functions that start with "wp_" |
| 4 | * as they are reserved for the core of WordPress and can change with any | 5 | * as they are reserved for the core of WordPress and can change with any | ... | ... |
-
Please register or sign in to post a comment