Changeset 1769844
- Timestamp:
- 11/17/2017 11:57:22 PM (8 years ago)
- Location:
- simple-access-control/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (3 diffs)
-
simple-access-control.php (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
simple-access-control/trunk/readme.txt
r1666626 r1769844 4 4 Tags: post, page, menu, security 5 5 Requires at least: 3.0 6 Tested up to: 4. 7.57 Stable tag: 1. 5.16 Tested up to: 4.9 7 Stable tag: 1.6.0 8 8 9 9 A very simple plugin that hides specific pages, posts and menu items from users based on their logged in staus. … … 19 19 * The locked status is shown in the admin page and post lists 20 20 * a "Loggedin Text" widget is available that only displays text to logged in users 21 * You can set a flag causing the menu to display even when filtered 21 * You can set an option to display the menu even when filtered 22 * You can set an option to force a 404 Not Found error on direct access to a restricted page 22 23 23 * Changes seen by users*24 *Features seen by users* 24 25 25 26 * Locked pages and posts are not displayed 26 27 * Locked items are removed from standard and custom menus 27 28 * A message asking you to log in is displayed if you access it directly using its address or using the next and previous links 29 * A 404 Not Found error may be displayed on access to a restricted page 28 30 * A login/logout link is displayed in the mesage 29 31 * Loggedin Text widgets are not visible … … 41 43 42 44 == Changelog == 45 = 1.6.0 = 46 * fix fatal error extending WP_Widget, now extends WP_Widget_Text 47 * add option to give a 404 Not Found error on direct access to a restricted page or post 48 43 49 = 1.5.1 = 44 50 * set version number -
simple-access-control/trunk/simple-access-control.php
r1666626 r1769844 4 4 Plugin URI: http://devondev.com/blog/simple-access-control/ 5 5 Description: Allows authors to restrict access to pages and posts to logged in users. 6 Version: 1. 5.16 Version: 1.6.0 7 7 Author: Peter Wooster 8 8 Author URI: http://www.devondev.com/ … … 41 41 add_filter('get_pages', 'sac_filter_pages'); 42 42 add_filter('the_posts', 'sac_filter_pages'); 43 add_action('template_redirect', 'sac_redirect'); 43 44 44 45 … … 111 112 } 112 113 114 /* 115 * check for direct access to page or post 116 * and produce 404 if requested 117 */ 118 function sac_redirect() { 119 global $post; 120 $pid = $post->ID; 121 $allowed = sac_allowed($post->ID); 122 error_log("allowed=$allowed"); 123 if($allowed)return; 124 125 if (sac_force_404($post->ID)) { 126 error_log("forcing 404"); 127 status_header( 404 ); 128 nocache_headers(); 129 include( get_query_template( '404' ) ); 130 die(); 131 } 132 } 133 134 113 135 /** 114 136 * set the text for pages that should not be displayed, also turns off comments … … 151 173 * @param type $post_id of the content 152 174 * @param type $menu true if this is for a menu 153 * @return type true if restricted175 * @return type true if not restricted 154 176 */ 155 177 function sac_allowed($post_id, $menu=false, $value=null) { 156 global $current_user;157 178 if(is_admin())return true; 158 179 159 $logged_in = 0 < $current_user->ID;180 $logged_in = is_user_logged_in(); 160 181 if(!$value)$value = get_post_meta($post_id, 'sac_locked', true); 182 error_log("sac_allowed, post_id=$post_id, value=$value, logged_in=$logged_in"); 161 183 if(!$value)return true; 162 $va = sac_clean_value($value,true); 184 185 $va = sac_clean_value($value); 163 186 $value = $va[0]; 164 187 $showMenu = $va[1]; … … 169 192 if($value == 'not logged in' && $logged_in)$restrict=true; 170 193 if($value == 'logged in' && !$logged_in)$restrict= true; 194 error_log ("post_id=$post_id, restrict=$restrict"); 171 195 return !$restrict; 172 196 } 173 197 198 /* 199 * sac_force_404 200 * determine if a 404 error should be returned instead of the notice 201 */ 202 function sac_force_404($post_id) { 203 $value = get_post_meta($post_id, 'sac_locked', true); 204 $va = sac_clean_value($value); 205 return $va[2]; 206 } 174 207 175 208 /* ========================================================================= … … 217 250 global $post; 218 251 if($name == $key) { 219 $value = sac_clean_value(get_post_meta($post->ID, $key , true));252 $value = sac_clean_value(get_post_meta($post->ID, $key)); 220 253 echo $value; 221 254 } … … 247 280 $type= ucwords($post->post_type); 248 281 $value = get_post_meta($id, 'sac_locked', true); 249 $va = sac_clean_value($value , true);282 $va = sac_clean_value($value); 250 283 $users = $va[0]; 251 284 $menu= $va[1]; 252 253 echo "<p>locked=$value, users=$users, menu=$menu</p>"; 285 $force = $va[2]; 286 287 // echo "<p>locked=$value, users=$users, menu=$menu</p>"; 254 288 $sel_all = $sel_li = $sel_nli = ''; 255 289 if($users == 'logged in')$sel_li = 'selected'; … … 257 291 else $sel_all = 'selected'; 258 292 if($menu == 'show menu')$checked='checked'; else $checked=''; 293 if($force == 'yes')$forced='checked'; else $forced=''; 259 294 260 295 $html = <<<QEND … … 268 303 <input type="checkbox" $checked name="sac_showmenu"/> 269 304 </p> 270 305 <p><strong>Force 404 on direct access</strong> 306 <input type="checkbox" $forced name="sac_force404"/> 307 </p> 271 308 QEND; 272 309 echo $html; … … 279 316 function sac_save_meta($post_id) { 280 317 $field = 'sac_locked'; 281 $old = sac_clean_value(get_post_meta($post_id, $field , true));318 $old = sac_clean_value(get_post_meta($post_id, $field)); 282 319 283 320 if (isset($_POST['sac_locked']))$newUsers = $_POST['sac_locked']; else $newUsers = ''; 284 321 if (isset($_POST['sac_showmenu']))$newMenu = 'show menu'; else $newMenu = ''; 285 $new = sac_clean_value("$newUsers,$newMenu"); 286 287 if ($old != $new) { 288 if ($new == 'all')delete_post_meta($post_id, $field); 289 else update_post_meta($post_id, $field, $new); 290 } 291 } 292 293 function sac_clean_value($value, $asArray = false) { 322 if (isset($_POST['sac_force404']))$force404= 'yes'; else $force404 = ''; 323 324 $new = sac_clean_value("$newUsers,$newMenu,$force404"); 325 326 $newV= implode(',', $new); 327 $oldV = implode(',', $old); 328 error_log("oldV=$old; newV=$new"); 329 if ($oldV != $newV) { 330 if ($new[0] == 'all')delete_post_meta($post_id, $field); 331 else update_post_meta($post_id, $field, $newV); 332 } 333 } 334 335 function sac_clean_value($value) { 294 336 if (!$value)$value = 'all,'; 295 337 if($value == 'yes')$value='logged in,'; 296 $value = explode(',', $value); 297 if(1 == count($value))$value[1]= ''; 338 $value = explode(',', $value . ',,,'); 298 339 $users = $value[0]; 299 $menu = $value[1]; 340 $menu = $value[1]; 341 $force = $value[2]; 300 342 if($users != 'logged in' && $users != 'not logged in')$users = 'all'; 301 343 if($menu != 'show menu')$menu = ''; 302 if($asArray)return array($users, $menu); 303 else { 304 if($menu) return "$users,$menu"; else return $users; 305 } 344 if($force != 'yes')$force = ''; 345 return array($users, $menu, $force); 306 346 } 307 347 … … 320 360 } 321 361 } 322 323 324 325 326 362 327 363 /* ========================================================================= … … 418 454 * Logged In Text widget class 419 455 * 420 * @since 2.8.0 421 */ 422 class Logggedin_Widget_Text extends WP_Widget { 456 * @since 4.0 457 */ 458 459 /* 460 * this code now requires the Text Widget code 461 */ 462 require_once ABSPATH . '/wp-includes/widgets/class-wp-widget-text.php'; 463 464 /* 465 * construct the widget by extending the Text Widget 466 */ 467 class Logggedin_Widget_Text extends WP_Widget_Text { 423 468 function __construct() { 424 $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML when logged in')); 425 $control_ops = array('width' => 400, 'height' => 350); 426 parent::__construct('litext', __('Loggedin Text'), $widget_ops, $control_ops); 469 parent::__construct(); 470 $this->id_base = 'litext'; 471 $this->name = __( 'Loggedin Text!' ); 472 $this->option_name = 'widget_' . $this->id_base; 473 $this->widget_options['description'] = __( 'Text or HTML that shows when the user is logged-in', 'simple-access-control' ); 474 $this->control_options['id_base'] = $this->id_base; 427 475 } 428 476 477 /* 478 * Only show the widget when logged in 479 */ 429 480 function widget( $args, $instance ) { 430 global $current_user; 431 if(0 == $current_user->ID ) return; // no widget if not logged in 432 WP_Widget_Text::widget($args, $instance); 433 } 434 435 function update( $new_instance, $old_instance ) { 436 return WP_Widget_Text::update( $new_instance, $old_instance ); 437 } 438 439 function form( $instance ) { 440 WP_Widget_Text::form($instance); 481 if(is_user_logged_in() ) parent::widget($args, $instance); 441 482 } 442 483 }
Note: See TracChangeset
for help on using the changeset viewer.