562c32d1 by Chris Boden

Overhaul of this library; organization, addition of ClientSettings, PagePermissions. refs #534

1 parent 7a88afc4
1 <?php
2 class ClientSettings {
3 const CAPABILITY = 'edit_client_settings';
4 const ADMIN_PAGE = 'client-settings';
5
6 public static function make() {
7 static $made = false;
8 if ($made) {
9 throw new OverflowException('ClientSettings has already been initialized');
10 }
11 $made = true;
12
13 $role = get_role('administrator');
14 $role->add_cap(self::CAPABILITY);
15
16 add_actions('ClientSettings_Actions');
17 }
18
19 public static function viewOptionsPage() {
20
21 }
22 }
23
24 // register_setting()
25 // settings_fields()
26
27 // add_settings_section()
28 // add_settings_field()
29 // settings_fields()
30
31 // do_settings_section()
32
33 class ClientSettings_Actions {
34 public static function admin_menu() {
35 $display = (current_user_can('manage_options') ? 'Client Settings' : 'Settings');
36
37 add_utility_page($display, $display, ClientSettings::CAPABILITY, ClientSettings::ADMIN_PAGE, Array('ClientSettings', 'viewOptionsPage'));
38 }
39 }
40
41 ClientSettings::make();
42 ?>
...\ No newline at end of file ...\ No newline at end of file
1 var TzPagePermissions = function() {
2 var oSel;
3 var $select;
4 var $roles;
5
6 var init = function($) {
7 oSel = document.getElementById(TzPagePermissionsData.trigger);
8 if (!oSel) {
9 return;
10 }
11
12 $roles = $('#TzSpecific');
13 $(oSel).change(checkSetting);
14
15 checkSetting();
16 }
17
18 var checkSetting = function() {
19 var iVal = oSel.options[oSel.selectedIndex].value;
20 if (iVal == TzPagePermissionsData.focus) {
21 $roles.show();
22 } else {
23 $roles.hide();
24 }
25 }
26
27 jQuery(document).ready(init);
28 }();
...\ No newline at end of file ...\ No newline at end of file
1 <?php
2 /**
3 * Public API
4 */
5 class PagePermissions {
6 const META = 'accessible_to_roles';
7 const OPT = '';
8
9 const ELE_SEL = 'general_access';
10 const ELE_CUST = 'roles';
11 const ELE_AUTH = 'message_auth';
12 const ELE_CUST_AUTH = 'message_cust_auth';
13 const ELE_DENIED = 'message_cust_denied';
14
15 const OPT_ALL = 0;
16 const OPT_AUTH = 1;
17 const OPT_CUST = 2;
18
19 private static $current_user = false;
20
21 public static function init() {
22 if (false !== self::$current_user) {
23 throw new OverflowException('PagePermissions already initialized');
24 }
25
26 self::$current_user = _get_current_user();
27 }
28
29 /**
30 * @param {Integer} $post_id
31 * @returns Boolean
32 * @throw InvalidArgumentException
33 */
34 public static function current_user_can_view($post_id = false) {
35 static $settings = false;
36 if (false === $settings) {
37 $settings = new WP_Option(PagePermissionsAdmin::SETTING_NS);
38 }
39
40 if (false === $post_id) {
41 global $post;
42 $post_id = $post->ID;
43 }
44
45 // Meta value hasn't been set, assume public page (maybe should go off WP_Option default instead though...
46 if ('' === $data = get_custom_data(self::META, $post_id)) {
47 $data = Array(self::ELE_SEL => $settings[self::ELE_SEL], self::ELE_CUST => $settings[self::ELE_CUST]);
48 }
49
50 // Anyone has access, God has no limitations
51 if ($data[self::ELE_SEL] == self::OPT_ALL || is_site_admin()) {
52 return true;
53 }
54
55 // Login required and user is logged in
56 if ($data[self::ELE_SEL] == self::OPT_AUTH) {
57 if (is_user_logged_in()) {
58 return true;
59 }
60
61 return $settings[self::ELE_AUTH];
62 }
63
64 // Specific role required and user meets it
65 if ($data[self::ELE_SEL] == self::OPT_CUST) {
66 if (!is_user_logged_in()) {
67 return $settings[self::ELE_CUST_AUTH];
68 }
69
70 if (isset($data[self::ELE_CUST][self::get_user_role()])) {
71 return true;
72 }
73
74 return $settings[self::ELE_DENIED];
75 }
76
77 return 'An unknown permission error has occurred';
78 }
79
80 /**
81 * @param {Integer|String} $user Username or ID of user to lookup (or false for current user)
82 * @returns {String} $role The key of the users' role
83 */
84 public static function get_user_role($user = false) {
85 if (false === $user) {
86 $user_data = self::$current_user;
87 } else {
88 $user_data = new WP_User($user);
89 }
90
91 // or should I throw an exception?
92 if ($user_data->ID == 0) {
93 return '';
94 }
95
96 $user_roles = $user_data->roles;
97 $user_role = array_shift($user_roles);
98
99 return $user_role;
100 }
101
102 public static function getFieldNames() {
103 static $fields = false;
104 if (false !== $fields) {
105 return $fields;
106 }
107
108 $fields = Array();
109 $ref = new ReflectionClass(__CLASS__);
110 $consts = $ref->getConstants();
111 foreach ($consts as $const => $value) {
112 if (substr($const, 0, 4) == 'ELE_') {
113 $fields[$const] = $value;
114 }
115 }
116
117 return $fields;
118 }
119 }
120
121 class PagePermissionsAdmin {
122 const CAPABILITY = 'manage_page_permissions';
123 const ADMIN_PAGE = 'page-permission-settings';
124 const SUBMIT_HOOK = 'update_def_page_permissions';
125 const SETTING_NS = 'page_permission_defaults';
126
127 public static function make() {
128 static $made = false;
129 if ($made) {
130 throw new OverflowException('make has already beed called');
131 }
132 $made = true;
133
134 TzTools::import('ClientSettings');
135
136 $role = get_role('administrator');
137 $role->add_cap(self::CAPABILITY);
138
139 if (isset($_POST[self::SUBMIT_HOOK]) && current_user_can(self::CAPABILITY)) {
140 self::submit();
141 }
142
143 add_actions('PagePermissions_Actions');
144 }
145
146 public static function viewOptionsPage() {
147 $selected = self::getOptions();
148
149 include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'settings.php');
150 }
151
152 public static function viewMetaBox($post, $box_info) {
153 $selected = ($post->ID == 0 ? self::getOptions() : get_custom_data(PagePermissions::META, $post->ID));
154
155 if (empty($selected)) {
156 $selected = self::getOptions();
157 }
158
159 include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
160 }
161
162 public static function submit() {
163 unset($_POST[self::SUBMIT_HOOK]);
164
165 $options = self::getOptions();
166 $fields = PagePermissions::getFieldNames();
167 foreach ($fields as $field) {
168 if (isset($_POST[$field])) {
169 // not sure if stripslashes should go here or in WP_Options
170 $options[$field] = stripslashes($_POST[$field]);
171 } else {
172 $options[$field] = '';
173 }
174 }
175
176 $options->save();
177 }
178
179 private static function getOptions() {
180 static $options = false;
181 if (false !== $options) {
182 return $options;
183 }
184
185 $options = new WP_Option(self::SETTING_NS);
186 return $options;
187 }
188 }
189
190 class PagePermissions_Actions {
191 public static function admin_menu() {
192 if (current_user_can(ClientSettings::CAPABILITY)) {
193 add_submenu_page(ClientSettings::ADMIN_PAGE, 'Permission Defaults', 'Permission Defaults', PagePermissionsAdmin::CAPABILITY, PagePermissionsAdmin::ADMIN_PAGE, Array('PagePermissionsAdmin', 'viewOptionsPage'));
194 add_meta_box('page_permissions', 'Page Permissions', Array('PagePermissionsAdmin', 'viewMetaBox'), 'page', 'side', 'low');
195 }
196 }
197
198 public static function admin_print_scripts() {
199 _enqueue_script('page-permissions', plugins_url('PagePermissions.js', __FILE__));
200 _localize_script('page-permissions', 'TzPagePermissionsData', Array('trigger' => PagePermissions::ELE_SEL, 'focus' => PagePermissions::OPT_CUST));
201 }
202
203 public static function save_post($post_id) {
204 if (false === ($real_id = _is_post_revision($post_id))) {
205 $real_id = $post_id;
206 }
207 $current = get_custom_data(PagePermissions::META, $real_id);
208
209 $new = Array();
210 $new[PagePermissions::ELE_SEL] = $_POST[PagePermissions::ELE_SEL];
211 if (isset($_POST[PagePermissions::ELE_CUST])) {
212 $new[PagePermissions::ELE_CUST] = $_POST[PagePermissions::ELE_CUST];
213 } else {
214 $new[PagePermissions::ELE_CUST] = Array();
215 }
216
217 if (empty($current)) {
218 add_post_meta($real_id, PagePermissions::META, $new, true);
219 } else {
220 update_post_meta($real_id, PagePermissions::META, $new);
221 }
222 }
223 }
224
225 PagePermissions::init();
226 PagePermissionsAdmin::make();
227 ?>
...\ No newline at end of file ...\ No newline at end of file
1 <?php
2 if (!isset($selected)) {
3 throw new UnderflowException('Current Page Permission settings not provided');
4 }
5
6 $opt_selected = $selected[PagePermissions::ELE_SEL];
7 ?>
8
9 <select id="<?php echo PagePermissions::ELE_SEL; ?>" name="<?php echo PagePermissions::ELE_SEL; ?>">
10 <option value="<?php echo PagePermissions::OPT_ALL . '"' . (PagePermissions::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>
12 <option value="<?php echo PagePermissions::OPT_CUST . '"' . (PagePermissions::OPT_CUST == $opt_selected ? ' selected' : ''); ?>>Only Specific Users</option>
13 </select>
14
15 <div id="TzSpecific">
16 <?php
17 $rc = new WP_Roles();
18 $roles = $rc->role_names;
19 ksort($roles);
20 unset($rc, $roles['administrator']);
21
22 foreach ($roles as $key => $display) {
23 $checked = (isset($selected[PagePermissions::ELE_CUST][$key]) ? ' checked' : '');
24 echo '<br /><input type="checkbox" id="' . $key . '" name="' . PagePermissions::ELE_CUST . '[' . $key . ']" value="1"' . $checked . ' />';
25 echo '<label for="' . $key . '">' . $display . '</label>';
26 }
27 ?>
28 </div>
...\ No newline at end of file ...\ No newline at end of file
1 <?php require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'form.php'); ?>
...\ No newline at end of file ...\ No newline at end of file
1 <?php /*
2 Idea: Checkbox beside each textarea with option to include registration forum or not
3 */ ?>
4
5
6 <div class="wrap">
7 <?php screen_icon(); ?>
8 <h2>Permission Defaults</h2>
9
10 <form method="post">
11 <input type="hidden" name="<?php echo PagePermissionsAdmin::SUBMIT_HOOK; ?>" value="1" />
12
13 <h3>Defaults</h3>
14 <?php require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'form.php'); ?>
15
16 <hr />
17
18 <h3>Messages</h3>
19
20 <p>
21 <label for="<?php echo PagePermissions::ELE_AUTH; ?>" />"Must be Logged In" message for un-authenticated visitors</label>
22 <br /><textarea id="<?php echo PagePermissions::ELE_AUTH; ?>" name="<?php echo PagePermissions::ELE_AUTH; ?>"><?php echo $selected[PagePermissions::ELE_AUTH]; ?></textarea>
23 </p>
24
25 <p>
26 <label for="<?php echo PagePermissions::ELE_CUST_AUTH; ?>" />"Only Specific Users" message for un-authenticated visitors</label>
27 <br /><textarea id="<?php echo PagePermissions::ELE_CUST_AUTH; ?>" name="<?php echo PagePermissions::ELE_CUST_AUTH; ?>"><?php echo $selected[PagePermissions::ELE_CUST_AUTH]; ?></textarea>
28 </p>
29
30 <p>
31 <label for="<?php echo PagePermissions::ELE_DENIED; ?>" />"Only Specific Users" message for authenticated users without sufficient privelages</label>
32 <br /><textarea id="<?php echo PagePermissions::ELE_DENIED; ?>" name="<?php echo PagePermissions::ELE_DENIED; ?>"><?php echo $selected[PagePermissions::ELE_DENIED]; ?></textarea>
33 </p>
34
35 <p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p>
36 </form>
37 </div>
...\ No newline at end of file ...\ No newline at end of file
...@@ -14,7 +14,6 @@ Author: Tenzing ...@@ -14,7 +14,6 @@ Author: Tenzing
14 14
15 class TzTools { 15 class TzTools {
16 public static function load() { 16 public static function load() {
17 // set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));
18 spl_autoload_register(Array(__CLASS__, 'autoloader')); 17 spl_autoload_register(Array(__CLASS__, 'autoloader'));
19 18
20 require_once(dirname(__FILE__) . '/wp_functions.php'); 19 require_once(dirname(__FILE__) . '/wp_functions.php');
...@@ -26,8 +25,16 @@ class TzTools { ...@@ -26,8 +25,16 @@ class TzTools {
26 add_action('widgets_init', Array('MenuWidget', 'init')); 25 add_action('widgets_init', Array('MenuWidget', 'init'));
27 } 26 }
28 27
28 public static function import($com) {
29 $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'com' . DIRECTORY_SEPARATOR . $com . DIRECTORY_SEPARATOR;
30 $file = $dir . $com . '.php';
31 if (is_dir($dir) && is_file($file)) {
32 require_once($file);
33 }
34 }
35
29 public static function autoloader($class) { 36 public static function autoloader($class) {
30 $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . $class . '.php'; 37 $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php';
31 if (is_file($file)) { 38 if (is_file($file)) {
32 include($file); 39 include($file);
33 } 40 }
...@@ -69,4 +76,74 @@ function add_filters($class) { ...@@ -69,4 +76,74 @@ function add_filters($class) {
69 add_filter($method->name, Array($class, $method->name)); 76 add_filter($method->name, Array($class, $method->name));
70 } 77 }
71 } 78 }
79
80 function get_custom_data($name, $post_id = false) {
81 if (false === $type = get_post_type($post_id)) {
82 throw new InvalidArgumentException("Post {$post_id} does not exist");
83 }
84
85 $raw_data = call_user_func_array("_custom_{$type}", Array($post_id, $name));
86
87 if (null === $raw_data) {
88 return '';
89 }
90
91 return $raw_data;
92
93
94
95 /* @deprecated
96 if (is_array($raw_data)) {
97 return $raw_data;
98 }
99
100
101
102 return $raw_data
103
104 /* @deprecated
105 if (null === $maybe_data = json_decode($raw_data, true)) {
106 return $raw_data;
107 } else {
108 return $maybe_data;
109 }
110 */
111 }
112
113 function _custom_attachment($post_id, $custom_name) {
114 if (false === ($tax_object = get_the_terms($post_id, $custom_name))) {
115 return '';
116 }
117 $tax_data = array_shift($tax_object);
118
119 return $tax_data->name;
120 }
121
122 function _custom_page($post_id, $custom_name) {
123 $custom = get_post_meta($post_id, $custom_name);
124 return array_shift($custom);
125
126 // @deprecated
127 static $custom = Array();
128
129 if (!isset($custom[$post_id])) {
130 $custom[$post_id] = get_post_custom($post_id);
131
132 if (!isset($custom[$post_id][$custom_name])) {
133 $custom[$post_id][$custom_name] = Array('');
134 }
135 }
136
137 return array_shift($custom[$post_id][$custom_name]);
138 }
139
140 function _custom_post() {
141 $args = func_get_args();
142 return call_user_func_array('_custom_page', $args);
143 }
144
145 function _custom_revision() {
146 $args = func_get_args();
147 return call_user_func_array('_custom_page', $args);
148 }
72 ?> 149 ?>
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -128,4 +128,9 @@ function _logout() { ...@@ -128,4 +128,9 @@ function _logout() {
128 $params = func_get_args(); 128 $params = func_get_args();
129 return call_user_func_array('wp' . __FUNCTION__, $params); 129 return call_user_func_array('wp' . __FUNCTION__, $params);
130 } 130 }
131
132 function _is_post_revision() {
133 $params = func_get_args();
134 return call_user_func_array('wp' . __FUNCTION__, $params);
135 }
131 ?> 136 ?>
......