Plugin Directory

Changeset 1391488


Ignore:
Timestamp:
04/10/2016 04:45:28 PM (10 years ago)
Author:
natereist
Message:

tagging version 1.4

Location:
protected-posts-logout-button
Files:
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • protected-posts-logout-button/tags/1.4/logout.js

    r992977 r1391488  
    66       
    77        $.post( pplb_ajax.ajaxurl, data, function( response ) {
    8                 if( response.log == 1 && typeof console == "object" && typeof console.log != 'undefined'){
    9                         console.log( response );
    10                 }
     8            if ( response.log == 1 && typeof console == "object" && typeof console.log != 'undefined' ) {
     9                console.log( response );
     10            }
    1111            if( response.status == 0 ){
    1212                // nothing hanppens
  • protected-posts-logout-button/tags/1.4/pplb_logout_button.php

    r992985 r1391488  
    44    Plugin URI: http://omfgitsnater.com/protected-posts-logout-button/
    55    Description: A plugin built to add a logout button automatically to protected posts.
    6     Version: 1.3.2
     6    Version: 1.4
    77    Author: Nate Reist
    88    Author URI: http://omfgitsnater.com
    99*/
    1010
    11 /*
    12     Add the logout button to posts which require a password and the password has been provided.
    13 */
    14 
    15 function pplb_logout_filter( $content ){
    16     global $post;
    17     $html = '';
    18    
    19     //Check if the post has a password and we are inside the loop.
    20     if ( !empty( $post->post_password) && in_the_loop() ){
    21         //Check to see if the password has been provided.
    22         if ( !post_password_required( get_the_ID() ) ) {
    23             //add the logout button to the output.
     11if ( !defined( 'PPLB_TEMPLATE_PATH' ) ) {
     12    define( 'PPLB_TEMPLATE_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) . 'templates/' );
     13}
     14
     15if ( !class_exists( 'PPLB_Plugin' ) ) {
     16    class PPLB_Plugin {
     17
     18        function __construct(){
     19            add_action('init', array( $this, 'add_pplb_filter' ), 10, 1);                       // add the filter on load.
     20
     21            add_action('admin_menu', array( $this, 'pplb_add_admin' ) );
     22            add_action('wp_enqueue_scripts', array( $this, 'pplb_logout_js' ) );                    // adds the script to the header.
     23            add_action('wp_ajax_nopriv_pplb_logout', array( $this, 'pplb_protected_logout' ) );     // logout for non-logged in wp users
     24            add_action('wp_ajax_pplb_logout', array( $this, 'pplb_protected_logout' ) );        // logout for logged in wp users
     25           
     26            add_shortcode('logout_btn', array( $this, 'pplb_logout_button' ) );                 // adds the shortcode.
     27           
     28            add_filter( 'post_password_expires', array( $this, 'pplb_change_postpass_expires' ), 10, 1 );
     29           
     30            add_action( 'admin_init',  array( $this, 'pplb_options_save' ) );
     31        }
     32       
     33        /*
     34            Add the logout button to posts which require a password and the password has been provided.
     35        */
     36       
     37        function pplb_logout_filter( $content ){
     38            global $post;
     39            $html = '';
     40           
     41            //Check if the post has a password and we are inside the loop.
     42            if ( !empty( $post->post_password) && in_the_loop() ){
     43                //Check to see if the password has been provided.
     44                if ( !post_password_required( get_the_ID() ) ) {
     45                    //add the logout button to the output.
     46                    $options = get_option('pplb_options');
     47                    $class = ( array_key_exists('pplb_button_class', $options) ) ? $options['pplb_button_class'] : '';
     48                    $html .= ' <input type="button" class="button logout '.esc_attr($class).'" value="logout">';
     49                }
     50            }
     51            return $html.$content;
     52           
     53        }
     54       
     55        /*
     56            Adds for use in wordpress shortcode or php.
     57        */
     58       
     59        function pplb_logout_button(){
     60            $qid = get_queried_object_id();
     61            $qpost = get_post($qid);
     62            $html = '';
     63            // Check if the post has a password
     64            if ( !empty( $qpost->post_password ) ) {
     65                // Check to see if the password has been provided.
     66                if(!post_password_required($qid)){
     67                    $options = get_option('pplb_options');
     68                    $class = (array_key_exists('pplb_button_class', $options)) ? $options['pplb_button_class'] : '';
     69                    $html = ' <input type="button" class="button logout '.esc_attr($class).'" value="logout">';
     70                }
     71            }
     72            return $html;
     73           
     74        }
     75       
     76        /*
     77            Ajax function to reset the cookie in wordpress.
     78        */
     79       
     80        function pplb_protected_logout(){
     81            // Set the cookie to expire ten days ago... instantly logged out.
     82            setcookie( 'wp-postpass_' . COOKIEHASH, stripslashes( '' ), time() - 864000, COOKIEPATH );
    2483            $options = get_option('pplb_options');
    25             $class = ( array_key_exists('pplb_button_class', $options) ) ? $options['pplb_button_class'] : '';
    26             $html .= ' <input type="button" class="button logout '.esc_attr($class).'" value="logout">';
    27         }
    28     }
    29     return $html.$content;
    30    
    31 }
    32 
    33 /*
    34     Adds for use in wordpress shortcode or php.
    35 */
    36 
    37 function pplb_logout_button(){
    38     $qid = get_queried_object_id();
    39     $qpost = get_post($qid);
    40     $html = '';
    41     // Check if the post has a password
    42     if ( !empty( $qpost->post_password ) ) {
    43         // Check to see if the password has been provided.
    44         if(!post_password_required($qid)){
    45             $options = get_option('pplb_options');
    46             $class = (array_key_exists('pplb_button_class', $options)) ? $options['pplb_button_class'] : '';
    47             $html = ' <input type="button" class="button logout '.esc_attr($class).'" value="logout">';
    48         }
    49     }
    50     return $html;
    51    
    52 }
    53 
    54 /*
    55     Ajax function to reset the cookie in wordpress.
    56 */
    57 
    58 function pplb_protected_logout(){
    59     // Set the cookie to expire ten days ago... instantly logged out.
    60     setcookie( 'wp-postpass_' . COOKIEHASH, stripslashes( '' ), time() - 864000, COOKIEPATH );
    61     $options = get_option('pplb_options');
    62     $pplb_alert = (array_key_exists('pplb_alert', $options)) ? $options['pplb_alert'] : 'no';
    63     $log = isset( $options['pplb_debug'] ) ? $options['pplb_debug'] : 0;
    64    
    65     $response = array(
    66         'status'    => 0,
    67         'message'   => '',
    68         'log'           => $log
    69     );
    70    
    71     if ( $pplb_alert == 'yes' ) {
    72         $response['status'] = 1;
    73         $response['message'] = stripslashes( $options['pplb_message'] );
    74     }
    75     else {
    76         $response['status'] = 0;
    77         $response['message'] = '';
    78     }
    79     wp_send_json( $response );
    80 }
    81 
    82 /*
    83     Enqueue the scripts.
    84 */
    85 function pplb_logout_js(){
    86     wp_register_script( 'pplb_logout_js', plugins_url( '/logout.js', __FILE__ ), array('jquery'), null, true );
    87     wp_enqueue_script( 'pplb_logout_js' );
    88     wp_localize_script( 'pplb_logout_js', 'pplb_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    89 }
    90 
    91 /*
    92     Filter the expiration time if necessary based upon the option.
    93 */
    94 function pplb_change_postpass_expires( $expire ){
    95     $new_expire = get_option( 'pplb_pass_expires', false );
    96     if ( $new_expire !== false && is_numeric( $new_expire ) ) {
    97         return time() + $new_expire;
    98     }
    99     else {
    100         return $expire;
     84            $pplb_alert = (array_key_exists('pplb_alert', $options)) ? $options['pplb_alert'] : 'no';
     85            $log = isset( $options['pplb_debug'] ) ? $options['pplb_debug'] : 0;
     86           
     87            $response = array(
     88                'status'    => 0,
     89                'message'   => '',
     90                'log'           => $log
     91            );
     92           
     93            if ( $pplb_alert == 'yes' ) {
     94                $response['status'] = 1;
     95                $response['message'] = stripslashes( $options['pplb_message'] );
     96            }
     97            else {
     98                $response['status'] = 0;
     99                $response['message'] = '';
     100            }
     101            wp_send_json( $response );
     102        }
     103       
     104        /*
     105            Enqueue the scripts.
     106        */
     107        function pplb_logout_js(){
     108            wp_register_script( 'pplb_logout_js', plugins_url( '/logout.js', __FILE__ ), array('jquery'), null, true );
     109            wp_enqueue_script( 'pplb_logout_js' );
     110            wp_localize_script( 'pplb_logout_js', 'pplb_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
     111        }
     112       
     113        /*
     114            Filter the expiration time if necessary based upon the option.
     115        */
     116        function pplb_change_postpass_expires( $expire ){
     117            $new_expire = get_option( 'pplb_pass_expires', false );
     118            if ( $new_expire !== false && is_numeric( $new_expire ) ) {
     119                return time() + $new_expire;
     120            }
     121            else {
     122                return $expire;
     123            }
     124        }
     125       
     126        /*
     127                Save on admin init
     128        */
     129        function pplb_options_save(){
     130                if ( isset( $_POST['pplb_action'] ) ) {
     131                //update the option.
     132                $options = array();
     133                $options['pplb_alert'] = ( array_key_exists('pplb_alert', $_POST) ) ? $_POST['pplb_alert']: 'no';
     134                $options['pplb_message'] = esc_js( $_POST['pplb_message'] );
     135                $options['pplb_debug'] = ( array_key_exists('pplb_debug', $_POST) ) ? $_POST['pplb_debug']: 0;
     136                $options['pplb_button_class'] = esc_attr($_POST['pplb_button_class']);
     137                update_option('pplb_options', $options);
     138               
     139                $expire = ( isset( $_POST['pplb_pass_expires'] ) && !empty( $_POST['pplb_pass_expires'] ) ) ? $_POST['pplb_pass_expires']: false;
     140                update_option('pplb_pass_expires', $expire );
     141               
     142                $filter = isset($_POST['pplb_button_filter']) ? $_POST['pplb_button_filter']: 'yes';
     143                update_option('pplb_button_filter', $filter);
     144                $redirect = add_query_arg( array( 'message' => 1 ) );
     145                wp_redirect( $redirect );
     146                exit();
     147            }
     148        }
     149        /*
     150            The settings page in admin
     151        */
     152        function pplb_settings_page(){
     153            include ( PPLB_TEMPLATE_PATH . 'pplb-options.php' );
     154        }
     155       
     156        /*
     157            Add the admin page
     158        */
     159        function pplb_add_admin(){
     160            add_options_page('Protected Post Logout Settings', 'Protected Post Logout', 'manage_options', 'pplb-settings-page', array( $this, 'pplb_settings_page' ) );
     161        }
     162       
     163        /*
     164            Activation hook to install the options if they haven't been installed before.
     165        */
     166        function install_pplb_options(){
     167            if ( !get_option('pplb_options') ) {
     168                $options = array(
     169                    'pplb_alert' => 'no',
     170                    'pplb_message' => 'Successfully logged out.',
     171                    'pplb_button_class' => ''
     172                );
     173                update_option('pplb_options', $options);
     174            }
     175            if ( !get_option('pplb_button_filter') ) {
     176                update_option('pplb_button_filter', 'yes');
     177            }
     178        }
     179       
     180        /*
     181            Only add the filter if the option declares it
     182        */
     183        function add_pplb_filter(){
     184            if ( !get_option( 'pplb_button_filter' ) ) {
     185                // if the option isn't set, assume we want it there.
     186                update_option('pplb_button_filter', 'yes');
     187            }
     188            $add_filter = get_option('pplb_button_filter');
     189            if ( $add_filter == 'yes' ) {
     190                add_filter('the_content', array( $this, 'pplb_logout_filter' ), 9999, 1);   // adds the button.
     191            }
     192        }
    101193    }
    102194}
    103 add_filter( 'post_password_expires','pplb_change_postpass_expires', 10, 1 );
    104 
    105 add_action( 'admin_init', 'pplb_options_save' );
    106 
    107 /*
    108         Save on admin init
    109 */
    110 function pplb_options_save(){
    111         if ( isset( $_POST['pplb_action'] ) ) {
    112         //update the option.
    113         $options = array();
    114         $options['pplb_alert'] = ( array_key_exists('pplb_alert', $_POST) ) ? $_POST['pplb_alert']: 'no';
    115         $options['pplb_message'] = esc_js( $_POST['pplb_message'] );
    116         $options['pplb_debug'] = ( array_key_exists('pplb_debug', $_POST) ) ? $_POST['pplb_debug']: 0;
    117         $options['pplb_button_class'] = esc_attr($_POST['pplb_button_class']);
    118         update_option('pplb_options', $options);
    119        
    120         $expire = ( isset( $_POST['pplb_pass_expires'] ) && !empty( $_POST['pplb_pass_expires'] ) ) ? $_POST['pplb_pass_expires']: false;
    121         update_option('pplb_pass_expires', $expire );
    122        
    123         $filter = isset($_POST['pplb_button_filter']) ? $_POST['pplb_button_filter']: 'yes';
    124         update_option('pplb_button_filter', $filter);
    125         $redirect = add_query_arg( array( 'message' => 1 ) );
    126         wp_redirect( $redirect );
    127         exit();
    128     }
     195
     196function init_pplb(){
     197    new PPLB_Plugin();
    129198}
    130 /*
    131     The settings page in admin
    132 */
    133 function pplb_settings_page(){
    134    
    135     $new_options = get_option('pplb_options');
    136     if ( isset( $_GET['message'] ) && $_GET['message'] == 1 ){
    137             ?>
    138             <div class="message updated">
    139                     <p>Settings updated</p>
    140             </div>
    141             <?php
    142     }
    143    
    144     if ( is_array($new_options ) ) {
    145        
    146         $pplb_debug = isset( $new_options['pplb_debug'] ) ? $new_options['pplb_debug'] : 0;
    147         $pplb_alert = isset( $new_options['pplb_alert'] ) ? $new_options['pplb_alert'] : 0;
    148         $pplb_message = isset( $new_options['pplb_message'] ) ? $new_options['pplb_message'] : '';
    149         $pplb_button_class = isset( $new_options['pplb_button_class'] ) ? $new_options['pplb_button_class'] : '';
    150        
    151     }
    152     ?>
    153         <div class="wrap">
    154             <h2>Protected Posts Logout Settings</h2>
    155             <p>Thanks for using this plugin.  If you encounter any errors please report them to <a href="mailto:nate@omfgitsnater.com">nate@omfgitsnater.com</a></p>
    156             <form action="" method="post">
    157                     <input type="hidden" name="pplb_action" value="update" />
    158             <table class='form-table'>
    159                 <tbody>
    160                     <tr>
    161                         <th><label>Alert user log out was successful?</label></th><td><input type="checkbox" name="pplb_alert" value="yes" <?php checked($pplb_alert, 'yes'); ?> /></td>
    162                     </tr>
    163                     <tr>
    164                         <th><label>Turn on console debugging in Javascript? ( for developers )</label></th><td><input type="checkbox" name="pplb_debug" value="1" <?php checked($pplb_debug, 1); ?> /></td>
    165                     </tr>
    166                     <tr>
    167                         <th><label>Logout Message:</label></th><td> <input type="text" name="pplb_message" value="<?php echo stripslashes($pplb_message); ?>" /></td>
    168                     </tr>
    169                     <tr>
    170                         <th><label>Button CSS class:</label></th><td> <input type="text" name="pplb_button_class" value="<?php echo stripslashes($pplb_button_class); ?>" /></td>
    171                     </tr>
    172                    
    173                     <tr>
    174                         <th><label>Automatically add button to protected pages:</label></th><td>
    175                         <select name="pplb_button_filter">
    176                             <option value='yes' <?php selected( get_option('pplb_button_filter'), 'yes'); ?>>Yes</option>
    177                             <option value='no' <?php selected( get_option('pplb_button_filter'), 'no'); ?>>No</option>
    178                         </select>
    179                         </td>
    180                     </tr>
    181                     <tr>
    182                         <?php $expire = get_option('pplb_pass_expires'); ?>
    183                         <th>
    184                             <label>Change the default cookie expire time for WordPress Protected Posts:</label>
    185                             <br />
    186                             <span class="description">In seconds, leave blank for default</span>
    187                         </th>
    188                         <td>
    189                             <input type="number" name="pplb_pass_expires" value="<?php echo $expire; ?>"> seconds = <span id="expire-human"></span>
    190                             <script type="text/javascript">
    191                                 jQuery( document ).ready(function($){
    192                                     $( 'input[name="pplb_pass_expires"]' ).change( function(){
    193                                         if( $(this).val().length == 0 ){
    194                                             $('span#expire-human').text( '10 days (default value)' )
    195                                         }
    196                                         else{
    197                                             var word = 'minutes';
    198                                             var v = $( this ).val();
    199                                             if( v > ( 24 * 60 * 60 ) ){
    200                                                 word = 'days';
    201                                             }
    202                                             else if( v > ( 60 * 60 ) ){
    203                                                 word = 'hours';
    204                                             }
    205                                            
    206                                             switch( word ){
    207                                                 case 'minutes':
    208                                                     conversion = v / 60;
    209                                                     break;
    210                                                 case 'hours':
    211                                                     conversion = v / 60 / 60;
    212                                                     break;
    213                                                 case 'days':
    214                                                     conversion = v / 60 / 60 / 24;
    215                                                     break;
    216                                                 default:
    217                                                     conversion = v;
    218                                                     break;
    219                                             }
    220                                            
    221                                             var humanReadable = conversion + ' ' + word;
    222                                        
    223                                             $('span#expire-human').text( humanReadable );
    224                                         }
    225                                     } );
    226                                     $( 'input[name="pplb_pass_expires"]' ).trigger( 'change' );
    227                                 });
    228                             </script>
    229                         </td>
    230                     </tr>
    231                 </tbody>
    232             </table>
    233             <br />
    234             <p><input type="submit" value="Update" class="button-primary" /></p>
    235             </form>
    236             <h2>Usage</h2>
    237             <p>This plugin is meant to add a logout button to a <b>single</b> protected post or page automatically.</p>
    238             <p>It does so by attaching a button to the beginning of the content using a <b>filter</b> hooked to <code>the_content</code> when it is outputting that post's content. This means it inherently will not work for archives, where Wordpress is actually running <code>the_content</code> for other posts.</p>
    239             <p>Also, if other plugins, or theme code, are manipulating the protected posts content using a <b>filter</b> as well, they may remove the button inadvertently.</p>
    240             <p>To solve this, you can now place the button via a shortcode or <code>php</code> function:</p>
    241             <p>Shortcode:</p>
    242             <style type='text/css'>
    243                 .pplb-pre{ display:block; white-space: normal; padding:20px; width:300px; background:#eee; border:1px solid #ccc; font-family: arial; }
    244             </style>
    245             <pre class='pplb-pre'>[logout_btn]</pre>
    246             <p>PHP:</p>
    247             <pre class='pplb-pre'>&lt;?php echo pplb_logout_button(); ?&gt;</pre>
    248         </div><!-- .wrap pplb -->
    249        
    250     <?php
    251 }
    252 
    253 /*
    254     Add the admin page
    255 */
    256 function pplb_add_admin(){
    257     add_options_page('Protected Post Logout Settings', 'Protected Post Logout', 'manage_options', 'pplb-settings-page', 'pplb_settings_page');
    258 }
    259 
    260 /*
    261     Activation hook to install the options if they haven't been installed before.
    262 */
    263 function install_pplb_options(){
    264     if(!get_option('pplb_options')){
    265         $options = array(
    266             'pplb_alert' => 'no',
    267             'pplb_message' => 'Successfully logged out.',
    268             'pplb_button_class' => ''
    269         );
    270         update_option('pplb_options', $options);
    271     }
    272     if(!get_option('pplb_button_filter')){
    273         update_option('pplb_button_filter', 'yes');
    274     }
    275 }
    276 
    277 /*
    278     Only add the filter if the option declares it
    279 */
    280 function add_pplb_filter(){
    281     if ( !get_option( 'pplb_button_filter' ) ) {
    282         // if the option isn't set, assume we want it there.
    283         update_option('pplb_button_filter', 'yes');
    284     }
    285     $add_filter = get_option('pplb_button_filter');
    286     if ( $add_filter == 'yes' ) {
    287         add_filter('the_content', 'pplb_logout_filter', 9999, 1);   // adds the button.
    288     }
    289 }
    290 
    291 register_activation_hook( __FILE__ , 'install_pplb_options' );      // set up options
    292 
    293 add_action('init', 'add_pplb_filter', 10, 1);                       // add the filter on load.
    294 
    295 add_action('admin_menu', 'pplb_add_admin');
    296 add_action('wp_enqueue_scripts','pplb_logout_js');                  // adds the script to the header.
    297 add_action('wp_ajax_nopriv_pplb_logout', 'pplb_protected_logout');  // logout for non-logged in wp users
    298 add_action('wp_ajax_pplb_logout', 'pplb_protected_logout');         // logout for logged in wp users
    299 
    300 add_shortcode('logout_btn','pplb_logout_button');                   // adds the shortcode.
    301 
     199
     200add_action( 'init', 'init_pplb', 9, 1 );
     201
     202register_activation_hook( __FILE__ , array( 'PPLB_Plugin', 'install_pplb_options' ) );      // set up options
    302203?>
  • protected-posts-logout-button/tags/1.4/readme.txt

    r992985 r1391488  
    44Tags: logout, password protected posts logout button, wordpress security
    55Requires at least: 2.8
    6 Tested up to: 4.0
    7 Stable tag: 1.3.2.1
     6Tested up to: 4.4.2
     7Stable tag: 1.4.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3737Well, this button is setup to be no intrusive on your theme, so it adapts to the styles that come with you theme.
    3838
    39 That being said, you can style the button as you wish using a css class you define on the settings page.
     39That being said, you can style the button as you wish using a css class you define on the settings page, or add your themes button class.
    4040
    4141= I get more than one button on my page when I am logged in to a protected post, what gives? =
     
    5151
    5252== Changelog ==
     53= 1.4.0 =
     54* Refactored code into a class
     55* Moved the options page to a template
     56* Tested with version 4.4.2
     57
    5358= 1.3.2.1 =
    5459* Moving javascript to footers
  • protected-posts-logout-button/trunk/logout.js

    r992977 r1391488  
    66       
    77        $.post( pplb_ajax.ajaxurl, data, function( response ) {
    8                 if( response.log == 1 && typeof console == "object" && typeof console.log != 'undefined'){
    9                         console.log( response );
    10                 }
     8            if ( response.log == 1 && typeof console == "object" && typeof console.log != 'undefined' ) {
     9                console.log( response );
     10            }
    1111            if( response.status == 0 ){
    1212                // nothing hanppens
  • protected-posts-logout-button/trunk/pplb_logout_button.php

    r992985 r1391488  
    44    Plugin URI: http://omfgitsnater.com/protected-posts-logout-button/
    55    Description: A plugin built to add a logout button automatically to protected posts.
    6     Version: 1.3.2
     6    Version: 1.4
    77    Author: Nate Reist
    88    Author URI: http://omfgitsnater.com
    99*/
    1010
    11 /*
    12     Add the logout button to posts which require a password and the password has been provided.
    13 */
    14 
    15 function pplb_logout_filter( $content ){
    16     global $post;
    17     $html = '';
    18    
    19     //Check if the post has a password and we are inside the loop.
    20     if ( !empty( $post->post_password) && in_the_loop() ){
    21         //Check to see if the password has been provided.
    22         if ( !post_password_required( get_the_ID() ) ) {
    23             //add the logout button to the output.
     11if ( !defined( 'PPLB_TEMPLATE_PATH' ) ) {
     12    define( 'PPLB_TEMPLATE_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) . 'templates/' );
     13}
     14
     15if ( !class_exists( 'PPLB_Plugin' ) ) {
     16    class PPLB_Plugin {
     17
     18        function __construct(){
     19            add_action('init', array( $this, 'add_pplb_filter' ), 10, 1);                       // add the filter on load.
     20
     21            add_action('admin_menu', array( $this, 'pplb_add_admin' ) );
     22            add_action('wp_enqueue_scripts', array( $this, 'pplb_logout_js' ) );                    // adds the script to the header.
     23            add_action('wp_ajax_nopriv_pplb_logout', array( $this, 'pplb_protected_logout' ) );     // logout for non-logged in wp users
     24            add_action('wp_ajax_pplb_logout', array( $this, 'pplb_protected_logout' ) );        // logout for logged in wp users
     25           
     26            add_shortcode('logout_btn', array( $this, 'pplb_logout_button' ) );                 // adds the shortcode.
     27           
     28            add_filter( 'post_password_expires', array( $this, 'pplb_change_postpass_expires' ), 10, 1 );
     29           
     30            add_action( 'admin_init',  array( $this, 'pplb_options_save' ) );
     31        }
     32       
     33        /*
     34            Add the logout button to posts which require a password and the password has been provided.
     35        */
     36       
     37        function pplb_logout_filter( $content ){
     38            global $post;
     39            $html = '';
     40           
     41            //Check if the post has a password and we are inside the loop.
     42            if ( !empty( $post->post_password) && in_the_loop() ){
     43                //Check to see if the password has been provided.
     44                if ( !post_password_required( get_the_ID() ) ) {
     45                    //add the logout button to the output.
     46                    $options = get_option('pplb_options');
     47                    $class = ( array_key_exists('pplb_button_class', $options) ) ? $options['pplb_button_class'] : '';
     48                    $html .= ' <input type="button" class="button logout '.esc_attr($class).'" value="logout">';
     49                }
     50            }
     51            return $html.$content;
     52           
     53        }
     54       
     55        /*
     56            Adds for use in wordpress shortcode or php.
     57        */
     58       
     59        function pplb_logout_button(){
     60            $qid = get_queried_object_id();
     61            $qpost = get_post($qid);
     62            $html = '';
     63            // Check if the post has a password
     64            if ( !empty( $qpost->post_password ) ) {
     65                // Check to see if the password has been provided.
     66                if(!post_password_required($qid)){
     67                    $options = get_option('pplb_options');
     68                    $class = (array_key_exists('pplb_button_class', $options)) ? $options['pplb_button_class'] : '';
     69                    $html = ' <input type="button" class="button logout '.esc_attr($class).'" value="logout">';
     70                }
     71            }
     72            return $html;
     73           
     74        }
     75       
     76        /*
     77            Ajax function to reset the cookie in wordpress.
     78        */
     79       
     80        function pplb_protected_logout(){
     81            // Set the cookie to expire ten days ago... instantly logged out.
     82            setcookie( 'wp-postpass_' . COOKIEHASH, stripslashes( '' ), time() - 864000, COOKIEPATH );
    2483            $options = get_option('pplb_options');
    25             $class = ( array_key_exists('pplb_button_class', $options) ) ? $options['pplb_button_class'] : '';
    26             $html .= ' <input type="button" class="button logout '.esc_attr($class).'" value="logout">';
    27         }
    28     }
    29     return $html.$content;
    30    
    31 }
    32 
    33 /*
    34     Adds for use in wordpress shortcode or php.
    35 */
    36 
    37 function pplb_logout_button(){
    38     $qid = get_queried_object_id();
    39     $qpost = get_post($qid);
    40     $html = '';
    41     // Check if the post has a password
    42     if ( !empty( $qpost->post_password ) ) {
    43         // Check to see if the password has been provided.
    44         if(!post_password_required($qid)){
    45             $options = get_option('pplb_options');
    46             $class = (array_key_exists('pplb_button_class', $options)) ? $options['pplb_button_class'] : '';
    47             $html = ' <input type="button" class="button logout '.esc_attr($class).'" value="logout">';
    48         }
    49     }
    50     return $html;
    51    
    52 }
    53 
    54 /*
    55     Ajax function to reset the cookie in wordpress.
    56 */
    57 
    58 function pplb_protected_logout(){
    59     // Set the cookie to expire ten days ago... instantly logged out.
    60     setcookie( 'wp-postpass_' . COOKIEHASH, stripslashes( '' ), time() - 864000, COOKIEPATH );
    61     $options = get_option('pplb_options');
    62     $pplb_alert = (array_key_exists('pplb_alert', $options)) ? $options['pplb_alert'] : 'no';
    63     $log = isset( $options['pplb_debug'] ) ? $options['pplb_debug'] : 0;
    64    
    65     $response = array(
    66         'status'    => 0,
    67         'message'   => '',
    68         'log'           => $log
    69     );
    70    
    71     if ( $pplb_alert == 'yes' ) {
    72         $response['status'] = 1;
    73         $response['message'] = stripslashes( $options['pplb_message'] );
    74     }
    75     else {
    76         $response['status'] = 0;
    77         $response['message'] = '';
    78     }
    79     wp_send_json( $response );
    80 }
    81 
    82 /*
    83     Enqueue the scripts.
    84 */
    85 function pplb_logout_js(){
    86     wp_register_script( 'pplb_logout_js', plugins_url( '/logout.js', __FILE__ ), array('jquery'), null, true );
    87     wp_enqueue_script( 'pplb_logout_js' );
    88     wp_localize_script( 'pplb_logout_js', 'pplb_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    89 }
    90 
    91 /*
    92     Filter the expiration time if necessary based upon the option.
    93 */
    94 function pplb_change_postpass_expires( $expire ){
    95     $new_expire = get_option( 'pplb_pass_expires', false );
    96     if ( $new_expire !== false && is_numeric( $new_expire ) ) {
    97         return time() + $new_expire;
    98     }
    99     else {
    100         return $expire;
     84            $pplb_alert = (array_key_exists('pplb_alert', $options)) ? $options['pplb_alert'] : 'no';
     85            $log = isset( $options['pplb_debug'] ) ? $options['pplb_debug'] : 0;
     86           
     87            $response = array(
     88                'status'    => 0,
     89                'message'   => '',
     90                'log'           => $log
     91            );
     92           
     93            if ( $pplb_alert == 'yes' ) {
     94                $response['status'] = 1;
     95                $response['message'] = stripslashes( $options['pplb_message'] );
     96            }
     97            else {
     98                $response['status'] = 0;
     99                $response['message'] = '';
     100            }
     101            wp_send_json( $response );
     102        }
     103       
     104        /*
     105            Enqueue the scripts.
     106        */
     107        function pplb_logout_js(){
     108            wp_register_script( 'pplb_logout_js', plugins_url( '/logout.js', __FILE__ ), array('jquery'), null, true );
     109            wp_enqueue_script( 'pplb_logout_js' );
     110            wp_localize_script( 'pplb_logout_js', 'pplb_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
     111        }
     112       
     113        /*
     114            Filter the expiration time if necessary based upon the option.
     115        */
     116        function pplb_change_postpass_expires( $expire ){
     117            $new_expire = get_option( 'pplb_pass_expires', false );
     118            if ( $new_expire !== false && is_numeric( $new_expire ) ) {
     119                return time() + $new_expire;
     120            }
     121            else {
     122                return $expire;
     123            }
     124        }
     125       
     126        /*
     127                Save on admin init
     128        */
     129        function pplb_options_save(){
     130                if ( isset( $_POST['pplb_action'] ) ) {
     131                //update the option.
     132                $options = array();
     133                $options['pplb_alert'] = ( array_key_exists('pplb_alert', $_POST) ) ? $_POST['pplb_alert']: 'no';
     134                $options['pplb_message'] = esc_js( $_POST['pplb_message'] );
     135                $options['pplb_debug'] = ( array_key_exists('pplb_debug', $_POST) ) ? $_POST['pplb_debug']: 0;
     136                $options['pplb_button_class'] = esc_attr($_POST['pplb_button_class']);
     137                update_option('pplb_options', $options);
     138               
     139                $expire = ( isset( $_POST['pplb_pass_expires'] ) && !empty( $_POST['pplb_pass_expires'] ) ) ? $_POST['pplb_pass_expires']: false;
     140                update_option('pplb_pass_expires', $expire );
     141               
     142                $filter = isset($_POST['pplb_button_filter']) ? $_POST['pplb_button_filter']: 'yes';
     143                update_option('pplb_button_filter', $filter);
     144                $redirect = add_query_arg( array( 'message' => 1 ) );
     145                wp_redirect( $redirect );
     146                exit();
     147            }
     148        }
     149        /*
     150            The settings page in admin
     151        */
     152        function pplb_settings_page(){
     153            include ( PPLB_TEMPLATE_PATH . 'pplb-options.php' );
     154        }
     155       
     156        /*
     157            Add the admin page
     158        */
     159        function pplb_add_admin(){
     160            add_options_page('Protected Post Logout Settings', 'Protected Post Logout', 'manage_options', 'pplb-settings-page', array( $this, 'pplb_settings_page' ) );
     161        }
     162       
     163        /*
     164            Activation hook to install the options if they haven't been installed before.
     165        */
     166        function install_pplb_options(){
     167            if ( !get_option('pplb_options') ) {
     168                $options = array(
     169                    'pplb_alert' => 'no',
     170                    'pplb_message' => 'Successfully logged out.',
     171                    'pplb_button_class' => ''
     172                );
     173                update_option('pplb_options', $options);
     174            }
     175            if ( !get_option('pplb_button_filter') ) {
     176                update_option('pplb_button_filter', 'yes');
     177            }
     178        }
     179       
     180        /*
     181            Only add the filter if the option declares it
     182        */
     183        function add_pplb_filter(){
     184            if ( !get_option( 'pplb_button_filter' ) ) {
     185                // if the option isn't set, assume we want it there.
     186                update_option('pplb_button_filter', 'yes');
     187            }
     188            $add_filter = get_option('pplb_button_filter');
     189            if ( $add_filter == 'yes' ) {
     190                add_filter('the_content', array( $this, 'pplb_logout_filter' ), 9999, 1);   // adds the button.
     191            }
     192        }
    101193    }
    102194}
    103 add_filter( 'post_password_expires','pplb_change_postpass_expires', 10, 1 );
    104 
    105 add_action( 'admin_init', 'pplb_options_save' );
    106 
    107 /*
    108         Save on admin init
    109 */
    110 function pplb_options_save(){
    111         if ( isset( $_POST['pplb_action'] ) ) {
    112         //update the option.
    113         $options = array();
    114         $options['pplb_alert'] = ( array_key_exists('pplb_alert', $_POST) ) ? $_POST['pplb_alert']: 'no';
    115         $options['pplb_message'] = esc_js( $_POST['pplb_message'] );
    116         $options['pplb_debug'] = ( array_key_exists('pplb_debug', $_POST) ) ? $_POST['pplb_debug']: 0;
    117         $options['pplb_button_class'] = esc_attr($_POST['pplb_button_class']);
    118         update_option('pplb_options', $options);
    119        
    120         $expire = ( isset( $_POST['pplb_pass_expires'] ) && !empty( $_POST['pplb_pass_expires'] ) ) ? $_POST['pplb_pass_expires']: false;
    121         update_option('pplb_pass_expires', $expire );
    122        
    123         $filter = isset($_POST['pplb_button_filter']) ? $_POST['pplb_button_filter']: 'yes';
    124         update_option('pplb_button_filter', $filter);
    125         $redirect = add_query_arg( array( 'message' => 1 ) );
    126         wp_redirect( $redirect );
    127         exit();
    128     }
     195
     196function init_pplb(){
     197    new PPLB_Plugin();
    129198}
    130 /*
    131     The settings page in admin
    132 */
    133 function pplb_settings_page(){
    134    
    135     $new_options = get_option('pplb_options');
    136     if ( isset( $_GET['message'] ) && $_GET['message'] == 1 ){
    137             ?>
    138             <div class="message updated">
    139                     <p>Settings updated</p>
    140             </div>
    141             <?php
    142     }
    143    
    144     if ( is_array($new_options ) ) {
    145        
    146         $pplb_debug = isset( $new_options['pplb_debug'] ) ? $new_options['pplb_debug'] : 0;
    147         $pplb_alert = isset( $new_options['pplb_alert'] ) ? $new_options['pplb_alert'] : 0;
    148         $pplb_message = isset( $new_options['pplb_message'] ) ? $new_options['pplb_message'] : '';
    149         $pplb_button_class = isset( $new_options['pplb_button_class'] ) ? $new_options['pplb_button_class'] : '';
    150        
    151     }
    152     ?>
    153         <div class="wrap">
    154             <h2>Protected Posts Logout Settings</h2>
    155             <p>Thanks for using this plugin.  If you encounter any errors please report them to <a href="mailto:nate@omfgitsnater.com">nate@omfgitsnater.com</a></p>
    156             <form action="" method="post">
    157                     <input type="hidden" name="pplb_action" value="update" />
    158             <table class='form-table'>
    159                 <tbody>
    160                     <tr>
    161                         <th><label>Alert user log out was successful?</label></th><td><input type="checkbox" name="pplb_alert" value="yes" <?php checked($pplb_alert, 'yes'); ?> /></td>
    162                     </tr>
    163                     <tr>
    164                         <th><label>Turn on console debugging in Javascript? ( for developers )</label></th><td><input type="checkbox" name="pplb_debug" value="1" <?php checked($pplb_debug, 1); ?> /></td>
    165                     </tr>
    166                     <tr>
    167                         <th><label>Logout Message:</label></th><td> <input type="text" name="pplb_message" value="<?php echo stripslashes($pplb_message); ?>" /></td>
    168                     </tr>
    169                     <tr>
    170                         <th><label>Button CSS class:</label></th><td> <input type="text" name="pplb_button_class" value="<?php echo stripslashes($pplb_button_class); ?>" /></td>
    171                     </tr>
    172                    
    173                     <tr>
    174                         <th><label>Automatically add button to protected pages:</label></th><td>
    175                         <select name="pplb_button_filter">
    176                             <option value='yes' <?php selected( get_option('pplb_button_filter'), 'yes'); ?>>Yes</option>
    177                             <option value='no' <?php selected( get_option('pplb_button_filter'), 'no'); ?>>No</option>
    178                         </select>
    179                         </td>
    180                     </tr>
    181                     <tr>
    182                         <?php $expire = get_option('pplb_pass_expires'); ?>
    183                         <th>
    184                             <label>Change the default cookie expire time for WordPress Protected Posts:</label>
    185                             <br />
    186                             <span class="description">In seconds, leave blank for default</span>
    187                         </th>
    188                         <td>
    189                             <input type="number" name="pplb_pass_expires" value="<?php echo $expire; ?>"> seconds = <span id="expire-human"></span>
    190                             <script type="text/javascript">
    191                                 jQuery( document ).ready(function($){
    192                                     $( 'input[name="pplb_pass_expires"]' ).change( function(){
    193                                         if( $(this).val().length == 0 ){
    194                                             $('span#expire-human').text( '10 days (default value)' )
    195                                         }
    196                                         else{
    197                                             var word = 'minutes';
    198                                             var v = $( this ).val();
    199                                             if( v > ( 24 * 60 * 60 ) ){
    200                                                 word = 'days';
    201                                             }
    202                                             else if( v > ( 60 * 60 ) ){
    203                                                 word = 'hours';
    204                                             }
    205                                            
    206                                             switch( word ){
    207                                                 case 'minutes':
    208                                                     conversion = v / 60;
    209                                                     break;
    210                                                 case 'hours':
    211                                                     conversion = v / 60 / 60;
    212                                                     break;
    213                                                 case 'days':
    214                                                     conversion = v / 60 / 60 / 24;
    215                                                     break;
    216                                                 default:
    217                                                     conversion = v;
    218                                                     break;
    219                                             }
    220                                            
    221                                             var humanReadable = conversion + ' ' + word;
    222                                        
    223                                             $('span#expire-human').text( humanReadable );
    224                                         }
    225                                     } );
    226                                     $( 'input[name="pplb_pass_expires"]' ).trigger( 'change' );
    227                                 });
    228                             </script>
    229                         </td>
    230                     </tr>
    231                 </tbody>
    232             </table>
    233             <br />
    234             <p><input type="submit" value="Update" class="button-primary" /></p>
    235             </form>
    236             <h2>Usage</h2>
    237             <p>This plugin is meant to add a logout button to a <b>single</b> protected post or page automatically.</p>
    238             <p>It does so by attaching a button to the beginning of the content using a <b>filter</b> hooked to <code>the_content</code> when it is outputting that post's content. This means it inherently will not work for archives, where Wordpress is actually running <code>the_content</code> for other posts.</p>
    239             <p>Also, if other plugins, or theme code, are manipulating the protected posts content using a <b>filter</b> as well, they may remove the button inadvertently.</p>
    240             <p>To solve this, you can now place the button via a shortcode or <code>php</code> function:</p>
    241             <p>Shortcode:</p>
    242             <style type='text/css'>
    243                 .pplb-pre{ display:block; white-space: normal; padding:20px; width:300px; background:#eee; border:1px solid #ccc; font-family: arial; }
    244             </style>
    245             <pre class='pplb-pre'>[logout_btn]</pre>
    246             <p>PHP:</p>
    247             <pre class='pplb-pre'>&lt;?php echo pplb_logout_button(); ?&gt;</pre>
    248         </div><!-- .wrap pplb -->
    249        
    250     <?php
    251 }
    252 
    253 /*
    254     Add the admin page
    255 */
    256 function pplb_add_admin(){
    257     add_options_page('Protected Post Logout Settings', 'Protected Post Logout', 'manage_options', 'pplb-settings-page', 'pplb_settings_page');
    258 }
    259 
    260 /*
    261     Activation hook to install the options if they haven't been installed before.
    262 */
    263 function install_pplb_options(){
    264     if(!get_option('pplb_options')){
    265         $options = array(
    266             'pplb_alert' => 'no',
    267             'pplb_message' => 'Successfully logged out.',
    268             'pplb_button_class' => ''
    269         );
    270         update_option('pplb_options', $options);
    271     }
    272     if(!get_option('pplb_button_filter')){
    273         update_option('pplb_button_filter', 'yes');
    274     }
    275 }
    276 
    277 /*
    278     Only add the filter if the option declares it
    279 */
    280 function add_pplb_filter(){
    281     if ( !get_option( 'pplb_button_filter' ) ) {
    282         // if the option isn't set, assume we want it there.
    283         update_option('pplb_button_filter', 'yes');
    284     }
    285     $add_filter = get_option('pplb_button_filter');
    286     if ( $add_filter == 'yes' ) {
    287         add_filter('the_content', 'pplb_logout_filter', 9999, 1);   // adds the button.
    288     }
    289 }
    290 
    291 register_activation_hook( __FILE__ , 'install_pplb_options' );      // set up options
    292 
    293 add_action('init', 'add_pplb_filter', 10, 1);                       // add the filter on load.
    294 
    295 add_action('admin_menu', 'pplb_add_admin');
    296 add_action('wp_enqueue_scripts','pplb_logout_js');                  // adds the script to the header.
    297 add_action('wp_ajax_nopriv_pplb_logout', 'pplb_protected_logout');  // logout for non-logged in wp users
    298 add_action('wp_ajax_pplb_logout', 'pplb_protected_logout');         // logout for logged in wp users
    299 
    300 add_shortcode('logout_btn','pplb_logout_button');                   // adds the shortcode.
    301 
     199
     200add_action( 'init', 'init_pplb', 9, 1 );
     201
     202register_activation_hook( __FILE__ , array( 'PPLB_Plugin', 'install_pplb_options' ) );      // set up options
    302203?>
  • protected-posts-logout-button/trunk/readme.txt

    r992985 r1391488  
    44Tags: logout, password protected posts logout button, wordpress security
    55Requires at least: 2.8
    6 Tested up to: 4.0
    7 Stable tag: 1.3.2.1
     6Tested up to: 4.4.2
     7Stable tag: 1.4.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3737Well, this button is setup to be no intrusive on your theme, so it adapts to the styles that come with you theme.
    3838
    39 That being said, you can style the button as you wish using a css class you define on the settings page.
     39That being said, you can style the button as you wish using a css class you define on the settings page, or add your themes button class.
    4040
    4141= I get more than one button on my page when I am logged in to a protected post, what gives? =
     
    5151
    5252== Changelog ==
     53= 1.4.0 =
     54* Refactored code into a class
     55* Moved the options page to a template
     56* Tested with version 4.4.2
     57
    5358= 1.3.2.1 =
    5459* Moving javascript to footers
Note: See TracChangeset for help on using the changeset viewer.