Plugin Directory

Changeset 1769844


Ignore:
Timestamp:
11/17/2017 11:57:22 PM (8 years ago)
Author:
pkwooster
Message:

Updated readme

Location:
simple-access-control/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • simple-access-control/trunk/readme.txt

    r1666626 r1769844  
    44Tags: post, page, menu, security
    55Requires at least: 3.0
    6 Tested up to: 4.7.5
    7 Stable tag: 1.5.1
     6Tested up to: 4.9
     7Stable tag: 1.6.0
    88
    99A very simple plugin that hides specific pages, posts and menu items from users based on their logged in staus.
     
    1919* The locked status is shown in the admin page and post lists
    2020* 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
    2223
    23 *Changes seen by users*
     24*Features seen by users*
    2425
    2526* Locked pages and posts are not displayed
    2627* Locked items are removed from standard and custom menus
    2728* 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
    2830* A login/logout link is displayed in the mesage
    2931* Loggedin Text widgets are not visible
     
    4143
    4244== 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
    4349= 1.5.1 =
    4450* set version number
  • simple-access-control/trunk/simple-access-control.php

    r1666626 r1769844  
    44Plugin URI: http://devondev.com/blog/simple-access-control/
    55Description: Allows authors to restrict access to pages and posts to logged in users.
    6 Version: 1.5.1
     6Version: 1.6.0
    77Author: Peter Wooster
    88Author URI: http://www.devondev.com/
     
    4141add_filter('get_pages', 'sac_filter_pages');
    4242add_filter('the_posts', 'sac_filter_pages');
     43add_action('template_redirect', 'sac_redirect');
    4344
    4445
     
    111112}
    112113
     114/*
     115 * check for direct access to page or post
     116 * and produce 404 if requested
     117 */
     118function 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
    113135/**
    114136 * set the text for pages that should not be displayed, also turns off comments
     
    151173 * @param type $post_id of the content
    152174 * @param type $menu true if this is for a menu
    153  * @return type true if restricted
     175 * @return type true if not restricted
    154176 */
    155177function sac_allowed($post_id, $menu=false, $value=null) {
    156     global $current_user;
    157178    if(is_admin())return true;
    158179   
    159     $logged_in = 0 < $current_user->ID;
     180    $logged_in = is_user_logged_in();
    160181    if(!$value)$value = get_post_meta($post_id, 'sac_locked', true);
     182error_log("sac_allowed, post_id=$post_id, value=$value, logged_in=$logged_in");
    161183    if(!$value)return true;
    162     $va = sac_clean_value($value,true);
     184
     185    $va = sac_clean_value($value);
    163186    $value = $va[0];
    164187    $showMenu = $va[1];
     
    169192    if($value == 'not logged in' && $logged_in)$restrict=true;
    170193    if($value == 'logged in' && !$logged_in)$restrict= true;
     194    error_log ("post_id=$post_id, restrict=$restrict");
    171195    return !$restrict;
    172196}
    173197
     198/*
     199 * sac_force_404
     200 * determine if a 404 error should be returned instead of the notice
     201 */
     202function 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}
    174207
    175208/* =========================================================================
     
    217250    global $post;
    218251    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));
    220253        echo $value;
    221254    }
     
    247280    $type= ucwords($post->post_type);
    248281    $value = get_post_meta($id, 'sac_locked', true);
    249     $va = sac_clean_value($value, true);
     282    $va = sac_clean_value($value);
    250283    $users = $va[0];
    251284    $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>";
    254288    $sel_all = $sel_li = $sel_nli = '';
    255289    if($users == 'logged in')$sel_li = 'selected';
     
    257291    else $sel_all = 'selected';
    258292    if($menu == 'show menu')$checked='checked'; else $checked='';
     293    if($force == 'yes')$forced='checked'; else $forced='';
    259294   
    260295    $html = <<<QEND
     
    268303    <input type="checkbox" $checked name="sac_showmenu"/>
    269304    </p>
    270  
     305    <p><strong>Force 404 on direct access</strong>
     306    <input type="checkbox" $forced name="sac_force404"/>
     307    </p>
    271308QEND;
    272309    echo $html;   
     
    279316function sac_save_meta($post_id) {
    280317    $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));
    282319   
    283320    if (isset($_POST['sac_locked']))$newUsers = $_POST['sac_locked']; else $newUsers = '';
    284321    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
     335function sac_clean_value($value) {
    294336    if (!$value)$value = 'all,';
    295337    if($value == 'yes')$value='logged in,';
    296     $value = explode(',', $value);
    297     if(1 == count($value))$value[1]= '';
     338    $value = explode(',', $value . ',,,');
    298339    $users = $value[0];
    299     $menu = $value[1];
     340    $menu = $value[1];
     341    $force = $value[2];
    300342    if($users != 'logged in' && $users != 'not logged in')$users = 'all';
    301343    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);
    306346}
    307347
     
    320360    }
    321361}
    322 
    323 
    324 
    325 
    326362
    327363/* =========================================================================
     
    418454 * Logged In Text widget class
    419455 *
    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 */
     462require_once ABSPATH . '/wp-includes/widgets/class-wp-widget-text.php';
     463
     464/*
     465 * construct the widget by extending the Text Widget
     466 */
     467class Logggedin_Widget_Text extends WP_Widget_Text {
    423468    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;
    427475    }
    428476
     477/*
     478 * Only show the widget when logged in
     479 */       
    429480    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);
    441482    }
    442483}
Note: See TracChangeset for help on using the changeset viewer.