a659717b by Chris Boden

Added mostly functioning taxanomy attachment handling for PagePermissions. refs #534

1 parent d156e2e7
...@@ -3,7 +3,37 @@ var TzPagePermissions = function() { ...@@ -3,7 +3,37 @@ var TzPagePermissions = function() {
3 var $select; 3 var $select;
4 var $roles; 4 var $roles;
5 5
6 var oHidden = false;
7
6 var init = function($) { 8 var init = function($) {
9 if (TzPagePermissionsData.change_field != '0') {
10 var oChange = document.getElementById(TzPagePermissionsData.change_field);
11 var sVal = oChange.value;
12 var oContainer = oChange.parentNode;
13 oContainer.removeChild(oChange);
14 oContainer.innerHTML = decodeURIComponent(TzPagePermissionsData.innerHTML);
15
16 oHidden = document.createElement('input');
17 oHidden.type = 'hidden';
18 oHidden.value = sVal;
19 oHidden.id = TzPagePermissionsData.change_field;
20 oHidden.name = TzPagePermissionsData.change_field;
21
22 oContainer.appendChild(oHidden);
23
24 var $Sel = $('#' + TzPagePermissionsData.trigger);
25 $Sel.change(updateHidden);
26
27 /*
28 var Els = document.getElementsByTagName('form')[0].elements;
29 for (var i = 0, iLen = Els.length; i < iLen; i++) {
30 if ($(Els[i]).parent(oContainer)) {
31 // console.log(Els[i]);
32 }
33 }
34 */
35 }
36
7 oSel = document.getElementById(TzPagePermissionsData.trigger); 37 oSel = document.getElementById(TzPagePermissionsData.trigger);
8 if (!oSel) { 38 if (!oSel) {
9 return; 39 return;
...@@ -15,6 +45,10 @@ var TzPagePermissions = function() { ...@@ -15,6 +45,10 @@ var TzPagePermissions = function() {
15 checkSetting(); 45 checkSetting();
16 } 46 }
17 47
48 var updateHidden = function() {
49 oHidden.value = oSel.options[oSel.selectedIndex].value;
50 }
51
18 var checkSetting = function() { 52 var checkSetting = function() {
19 var iVal = oSel.options[oSel.selectedIndex].value; 53 var iVal = oSel.options[oSel.selectedIndex].value;
20 if (iVal == TzPagePermissionsData.focus) { 54 if (iVal == TzPagePermissionsData.focus) {
......
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
3 * Public API 3 * Public API
4 */ 4 */
5 class PagePermissions { 5 class PagePermissions {
6 /**
7 * The name of the custom field stored in a post/page
8 * @type String
9 */
6 const META = 'accessible_to_roles'; 10 const META = 'accessible_to_roles';
7 const OPT = ''; 11 const OPT = '';
8 12
...@@ -12,10 +16,26 @@ class PagePermissions { ...@@ -12,10 +16,26 @@ class PagePermissions {
12 const ELE_CUST_AUTH = 'message_cust_auth'; 16 const ELE_CUST_AUTH = 'message_cust_auth';
13 const ELE_DENIED = 'message_cust_denied'; 17 const ELE_DENIED = 'message_cust_denied';
14 18
19 /**
20 * Lookup value for ELE_SEL for all users
21 * @type Integer
22 */
15 const OPT_ALL = 0; 23 const OPT_ALL = 0;
24 /**
25 * Lookup value for ELE_SEL for login required
26 * @type Integer
27 */
16 const OPT_AUTH = 1; 28 const OPT_AUTH = 1;
29 /**
30 * Lookup value for ELE_SEL for custom roles
31 * @type Integer
32 */
17 const OPT_CUST = 2; 33 const OPT_CUST = 2;
18 34
35 /**
36 * WP current user data
37 * @type Array
38 */
19 private static $current_user = false; 39 private static $current_user = false;
20 40
21 public static function init() { 41 public static function init() {
...@@ -26,9 +46,16 @@ class PagePermissions { ...@@ -26,9 +46,16 @@ class PagePermissions {
26 self::$current_user = _get_current_user(); 46 self::$current_user = _get_current_user();
27 } 47 }
28 48
49 public static function initAjax() {
50 $selected = unserialize($_POST['string_value']);
51 include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
52 }
53
29 /** 54 /**
55 * The key function in all of this; called by the Theme,
56 * this determines if the user is able to view the page.
30 * @param {Integer} $post_id 57 * @param {Integer} $post_id
31 * @returns Boolean 58 * @returns {Boolean|String} true if user can view, error message if not
32 * @throw InvalidArgumentException 59 * @throw InvalidArgumentException
33 */ 60 */
34 public static function current_user_can_view($post_id = false) { 61 public static function current_user_can_view($post_id = false) {
...@@ -42,7 +69,7 @@ class PagePermissions { ...@@ -42,7 +69,7 @@ class PagePermissions {
42 $post_id = $post->ID; 69 $post_id = $post->ID;
43 } 70 }
44 71
45 // Meta value hasn't been set, assume public page (maybe should go off WP_Option default instead though... 72 // Meta value hasn't been set, getting settings defaults
46 if ('' === $data = get_custom_data(self::META, $post_id)) { 73 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]); 74 $data = Array(self::ELE_SEL => $settings[self::ELE_SEL], self::ELE_CUST => $settings[self::ELE_CUST]);
48 } 75 }
...@@ -52,28 +79,34 @@ class PagePermissions { ...@@ -52,28 +79,34 @@ class PagePermissions {
52 return true; 79 return true;
53 } 80 }
54 81
55 // Login required and user is logged in 82 // Login required
56 if ($data[self::ELE_SEL] == self::OPT_AUTH) { 83 if ($data[self::ELE_SEL] == self::OPT_AUTH) {
84 // User is logged in
57 if (is_user_logged_in()) { 85 if (is_user_logged_in()) {
58 return true; 86 return true;
59 } 87 }
60 88
89 // Not logged in; return "login required" message
61 return $settings[self::ELE_AUTH]; 90 return $settings[self::ELE_AUTH];
62 } 91 }
63 92
64 // Specific role required and user meets it 93 // Specific role required
65 if ($data[self::ELE_SEL] == self::OPT_CUST) { 94 if ($data[self::ELE_SEL] == self::OPT_CUST) {
95 // User isn't even logged in; send message
66 if (!is_user_logged_in()) { 96 if (!is_user_logged_in()) {
67 return $settings[self::ELE_CUST_AUTH]; 97 return $settings[self::ELE_CUST_AUTH];
68 } 98 }
69 99
100 // User meets role required
70 if (isset($data[self::ELE_CUST][self::get_user_role()])) { 101 if (isset($data[self::ELE_CUST][self::get_user_role()])) {
71 return true; 102 return true;
72 } 103 }
73 104
105 // User is logged in, but doesn't have sufficient privileges, return message
74 return $settings[self::ELE_DENIED]; 106 return $settings[self::ELE_DENIED];
75 } 107 }
76 108
109 // This shouldn't happend; but just in case
77 return 'An unknown permission error has occurred'; 110 return 'An unknown permission error has occurred';
78 } 111 }
79 112
...@@ -99,10 +132,19 @@ class PagePermissions { ...@@ -99,10 +132,19 @@ class PagePermissions {
99 return $user_role; 132 return $user_role;
100 } 133 }
101 134
135 /**
136 * Determine if a user is a site administrator
137 * @param {Integer|String} $user Username or ID of user to lookup (or false for current user)
138 * @returns {Boolean}
139 */
102 public static function is_admin($user = false) { 140 public static function is_admin($user = false) {
103 return (self::get_user_role($user) == 'administrator' ? true : false); 141 return (self::get_user_role($user) == 'administrator' ? true : false);
104 } 142 }
105 143
144 /**
145 * Get a lookup of all the forum elements
146 * @returns {Array} An associative array of the forum elemnts name/values
147 */
106 public static function getFieldNames() { 148 public static function getFieldNames() {
107 static $fields = false; 149 static $fields = false;
108 if (false !== $fields) { 150 if (false !== $fields) {
...@@ -122,6 +164,9 @@ class PagePermissions { ...@@ -122,6 +164,9 @@ class PagePermissions {
122 } 164 }
123 } 165 }
124 166
167 /**
168 * Aministration control
169 */
125 class PagePermissionsAdmin { 170 class PagePermissionsAdmin {
126 const CAPABILITY = 'manage_page_permissions'; 171 const CAPABILITY = 'manage_page_permissions';
127 const ADMIN_PAGE = 'page-permission-settings'; 172 const ADMIN_PAGE = 'page-permission-settings';
...@@ -140,6 +185,8 @@ class PagePermissionsAdmin { ...@@ -140,6 +185,8 @@ class PagePermissionsAdmin {
140 $role = get_role('administrator'); 185 $role = get_role('administrator');
141 $role->add_cap(self::CAPABILITY); 186 $role->add_cap(self::CAPABILITY);
142 187
188 add_filters('PagePermissionsAdmin_Filters');
189
143 if (isset($_POST[self::SUBMIT_HOOK]) && current_user_can(self::CAPABILITY)) { 190 if (isset($_POST[self::SUBMIT_HOOK]) && current_user_can(self::CAPABILITY)) {
144 self::submit(); 191 self::submit();
145 } 192 }
...@@ -156,6 +203,7 @@ class PagePermissionsAdmin { ...@@ -156,6 +203,7 @@ class PagePermissionsAdmin {
156 public static function viewMetaBox($post, $box_info) { 203 public static function viewMetaBox($post, $box_info) {
157 $selected = ($post->ID == 0 ? self::getOptions() : get_custom_data(PagePermissions::META, $post->ID)); 204 $selected = ($post->ID == 0 ? self::getOptions() : get_custom_data(PagePermissions::META, $post->ID));
158 205
206 // If the post doesn't have the field saved get defaults
159 if (empty($selected)) { 207 if (empty($selected)) {
160 $selected = self::getOptions(); 208 $selected = self::getOptions();
161 } 209 }
...@@ -163,15 +211,27 @@ class PagePermissionsAdmin { ...@@ -163,15 +211,27 @@ class PagePermissionsAdmin {
163 include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php'); 211 include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
164 } 212 }
165 213
214 /**
215 * Handles saving data when a post/page is saved
216 */
166 public static function submit() { 217 public static function submit() {
167 unset($_POST[self::SUBMIT_HOOK]); 218 unset($_POST[self::SUBMIT_HOOK]);
168 219
169 $options = self::getOptions(); 220 $options = self::getOptions();
170 $fields = PagePermissions::getFieldNames(); 221 $fields = PagePermissions::getFieldNames();
171 foreach ($fields as $field) { 222 foreach ($fields as $field) {
172 if (isset($_POST[$field])) { 223 if (isset($_POST[$field])) {
173 // not sure if stripslashes should go here or in WP_Options 224
174 $options[$field] = stripslashes($_POST[$field]); 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 }
175 } else { 235 } else {
176 $options[$field] = ''; 236 $options[$field] = '';
177 } 237 }
...@@ -180,7 +240,7 @@ class PagePermissionsAdmin { ...@@ -180,7 +240,7 @@ class PagePermissionsAdmin {
180 $options->save(); 240 $options->save();
181 } 241 }
182 242
183 private static function getOptions() { 243 public static function getOptions() {
184 static $options = false; 244 static $options = false;
185 if (false !== $options) { 245 if (false !== $options) {
186 return $options; 246 return $options;
...@@ -191,7 +251,19 @@ class PagePermissionsAdmin { ...@@ -191,7 +251,19 @@ class PagePermissionsAdmin {
191 } 251 }
192 } 252 }
193 253
254 /**
255 * Each method is a handler for it's WordPress `add_action` namesake
256 */
194 class PagePermissions_Actions { 257 class PagePermissions_Actions {
258 public static function init() {
259 register_taxonomy(PagePermissions::META, 'attachment', Array('hierarachical' => false, 'label' => 'Page Permissions', 'query_var' => false));
260
261 $file = $_SERVER['REQUEST_URI'];
262 if ($file == '/wp-admin/media-new.php' && !PagePermissions::is_admin()) {
263 header("Location: " . $file . "?flash=0");
264 }
265 }
266
195 public static function admin_menu() { 267 public static function admin_menu() {
196 if (current_user_can(ClientSettings::CAPABILITY)) { 268 if (current_user_can(ClientSettings::CAPABILITY)) {
197 add_submenu_page(ClientSettings::ADMIN_PAGE, 'Permission Defaults', 'Permission Defaults', PagePermissionsAdmin::CAPABILITY, PagePermissionsAdmin::ADMIN_PAGE, Array('PagePermissionsAdmin', 'viewOptionsPage')); 269 add_submenu_page(ClientSettings::ADMIN_PAGE, 'Permission Defaults', 'Permission Defaults', PagePermissionsAdmin::CAPABILITY, PagePermissionsAdmin::ADMIN_PAGE, Array('PagePermissionsAdmin', 'viewOptionsPage'));
...@@ -200,11 +272,36 @@ class PagePermissions_Actions { ...@@ -200,11 +272,36 @@ class PagePermissions_Actions {
200 } 272 }
201 273
202 public static function admin_print_scripts() { 274 public static function admin_print_scripts() {
275 $innerhtml = '';
276 if ('0' !== ($change_field = (isset($_GET['attachment_id']) ? 'attachments[' . $_GET['attachment_id'] . '][' . PagePermissions::META . ']' : '0'))) {
277 $selected = get_custom_data(PagePermissions::META, $_GET['attachment_id']);
278 if (empty($selected)) {
279 $selected = PagePermissionsAdmin::getOptions();
280 }
281
282 ob_start();
283 require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
284 $innerhtml = ob_get_contents();
285 ob_end_clean();
286 }
287
203 _enqueue_script('page-permissions', plugins_url('PagePermissions.js', __FILE__)); 288 _enqueue_script('page-permissions', plugins_url('PagePermissions.js', __FILE__));
204 _localize_script('page-permissions', 'TzPagePermissionsData', Array('trigger' => PagePermissions::ELE_SEL, 'focus' => PagePermissions::OPT_CUST)); 289 _localize_script('page-permissions', 'TzPagePermissionsData', Array(
290 'trigger' => PagePermissions::ELE_SEL
291 , 'focus' => PagePermissions::OPT_CUST
292 , 'change_field' => $change_field
293 , 'innerHTML' => rawurlencode($innerhtml)
294 ));
295
296 //attachments[304][accessible_to_roles]
297 //a:2:{s:14:"general_access";s:1:"1";s:5:"roles";a:1:{s:6:"editor";s:1:"1";}}
205 } 298 }
206 299
207 public static function save_post($post_id) { 300 public static function save_post($post_id) {
301 if ($_POST['action'] == 'autosave') {
302 return;
303 }
304
208 if (false === ($real_id = _is_post_revision($post_id))) { 305 if (false === ($real_id = _is_post_revision($post_id))) {
209 $real_id = $post_id; 306 $real_id = $post_id;
210 } 307 }
...@@ -226,6 +323,24 @@ class PagePermissions_Actions { ...@@ -226,6 +323,24 @@ class PagePermissions_Actions {
226 } 323 }
227 } 324 }
228 325
229 PagePermissions::init(); 326 class PagePermissionsAdmin_Filters {
230 PagePermissionsAdmin::make(); 327 public static function image_upload_iframe_src($result) {
328 return $result . '&flash=0';
329 }
330
331 public static function video_upload_iframe_src($result) {
332 return $result . '&flash=0';
333 }
334
335 public static function audio_upload_iframe_src($result) {
336 return $result . '&flash=0';
337 }
338 }
339
340 if (isset($_POST['tz_pp_ajax'])) {
341 PagePermissions::initAjax();
342 } else {
343 PagePermissions::init();
344 PagePermissionsAdmin::make();
345 }
231 ?> 346 ?>
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
9 <select id="<?php echo PagePermissions::ELE_SEL; ?>" name="<?php echo PagePermissions::ELE_SEL; ?>"> 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> 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> 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> 12 <option value="<?php echo PagePermissions::OPT_CUST . '"' . (PagePermissions::OPT_CUST == $opt_selected ? ' selected' : ''); ?>>Specific Roles</option>
13 </select> 13 </select>
14 14
15 <div id="TzSpecific"> 15 <div id="TzSpecific">
......