Plugin Directory

Changeset 1177974


Ignore:
Timestamp:
06/10/2015 03:51:07 AM (11 years ago)
Author:
arippberger
Message:

plugin cleanup and added translation support

Location:
super-simple-post-page-restricor/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • super-simple-post-page-restricor/trunk/super-simple-post-page-restrictor-options.php

    r951353 r1177974  
    11<?php
    2 if ( !class_exists( 'Super_Simple_Post_Page_Options' ) ) {
    3     class Super_Simple_Post_Page_Options {
    4         /**
    5          * Holds the values to be used in the fields callbacks
    6          */
    7         private $options;
    8 
    9         /**
    10          * Start up
    11          */
    12         public function __construct() {
    13             add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
    14             add_action( 'admin_init', array( $this, 'page_init' ) );
    15             add_action( 'add_meta_boxes', array( $this, 'add_post_restriction_checkbox' ) );
    16             add_action( 'save_post', array( $this, 'save_post_restriction_checkbox' ), 13, 2 );
    17 
    18         }
    19 
    20         /**
    21         * Setup checkbox meta
    22         */
    23         public function setup_post_restriction_checkbox() {
    24 
    25         }
    26 
    27         /**
    28         * Add checkbox to posts/pages which will allow users to select whether a post/page should be restricted
    29         */
    30         public function add_post_restriction_checkbox() {
    31 
    32             //get options
    33             $this->options = get_option( 'ss_pp_restrictor_option' );
    34 
    35             //get post types set on settings page
    36             $applicable_post_types = $this->options['post_type_select'];
    37 
    38             $post_types = get_post_types(); //get all post types
    39 
    40             if ( is_array($applicable_post_types) && is_array($post_types) ) {
    41                 foreach ( $post_types as $post_type ) {
    42                     if ( in_array( $post_type, $applicable_post_types ) ) {
    43                         add_meta_box(
    44                             'ss_pp_restriction_checkbox', // Unique ID
    45                             esc_html__( 'Restrict Post?', 'ss_pp_restrictor' ),
    46                             array( $this, 'post_restriction_checkbox' ),
    47                             $post_type,
    48                             'side',
    49                             'default'
    50                         );
    51                     }
    52                 }
    53             }
    54         }
    55 
    56         /**
    57         * Display meta box.
    58         */
    59         public function post_restriction_checkbox( $object, $box ) {
    60 
    61           wp_nonce_field( basename( __FILE__ ), 'post_restriction_checkbox_nonce' );
    62           $checked = get_post_meta( $object->ID, 'ss_pp_restrictor_checkbox', true );
    63           //var_dump( $checked );
    64           ?>
    65           <p>
    66             <label class="small-text" for="post_restriction_checkbox"><?php _e( 'Restrict post/page content to logged-in users?', 'ss_pp_restrictor' ); ?></label>
    67             <br />
    68             <input type="checkbox" name="ss_pp_restrictor_checkbox" id="ss_pp_restrictor_checkbox" value="1" <?php checked( $checked ); ?> />
    69           </p><?php
    70 
    71         }
    72 
    73         /**
    74         * Hook to save and save $_POST variables
    75         */
    76         public function save_post_restriction_checkbox( $post_id, $post ) {
    77 
    78             // error_log(print_r($post_id));
    79             // error_log(print_r($post));
    80 
    81             //verify nonce
    82             if ( !isset( $_POST['post_restriction_checkbox_nonce'] ) || !wp_verify_nonce( $_POST['post_restriction_checkbox_nonce'], basename( __FILE__ ) ) ) {
    83                 //error_log('nonce not valid');
    84                 return $post_id;
    85             }
    86 
    87             //get current post type
    88             $post_type = get_post_type_object( $post->post_type );
    89 
    90             //ensure current user can edit post
    91             if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) {
    92                 //error_log('user cannot edit post');
    93                 return $post_id;
    94             }
    95 
    96             //new checkbox value
    97             $new_checkbox_value = ( isset( $_POST[ 'ss_pp_restrictor_checkbox' ] ) ? filter_var( $_POST[ 'ss_pp_restrictor_checkbox' ], FILTER_SANITIZE_NUMBER_INT ) : '' );
    98 
    99             //get old checkbox value
    100             $checkbox_value = get_post_meta( $post_id, 'ss_pp_restrictor_checkbox', true );
    101 
    102             //if new value added and there is no current value
    103             if ( $new_checkbox_value && '' == $checkbox_value ) {
    104 
    105                 add_post_meta( $post_id, 'ss_pp_restrictor_checkbox', $new_checkbox_value, true );
    106 
    107             } else if ( $new_checkbox_value && $new_checkbox_value != $checkbox_value ) { //if new checkbox value submitted and it doesn't match old
    108 
    109                 update_post_meta( $post_id, 'ss_pp_restrictor_checkbox', $new_checkbox_value );
    110 
    111             } else if ( '' == $new_checkbox_value && $checkbox_value ) { //if new checkbox value is empty and old exists, delete new
    112 
    113                 delete_post_meta( $post_id, 'ss_pp_restrictor_checkbox', $checkbox_value );
    114 
    115             }
    116 
    117         }
    118 
    119         /**
    120          * Add options page
    121          */
    122         public function add_plugin_page() {
    123             // This page will be under "Settings"
    124             add_options_page(
    125                 'Settings Admin',
    126                 'Super Simple Post / Page Restrictor',
    127                 'manage_options',
    128                 'ss_pp_restrictor',
    129                 array( $this, 'create_admin_page' )
    130             );
    131         }
    132 
    133         /**
    134          * Options page callback
    135          */
    136         public function create_admin_page() {
    137 
    138             // Set class property
    139             $this->options = get_option( 'ss_pp_restrictor_option' );
    140 
    141             //var_dump($this->options);
    142 
    143             ?>
    144             <div class="wrap">
    145                 <form method="post" action="options.php">
    146                 <?php
    147                     // This prints out all hidden setting fields
    148                     settings_fields( 'ss_pp_restrictor_option_group' );
    149                     do_settings_sections( 'ss_pp_restrictor' );
    150                     submit_button();
    151                 ?>
    152                 </form>
    153             </div>
    154             <?php
    155         }
    156 
    157         /**
    158          * Register and add settings
    159          */
    160         public function page_init() {
    161             register_setting(
    162                 'ss_pp_restrictor_option_group', // Option group
    163                 'ss_pp_restrictor_option', // Option name
    164                 array( $this, 'sanitize' ) // Sanitize
    165             );
    166 
    167             add_settings_section(
    168                 'ss_pp_restrictor_settings', // ID
    169                 'Super Simple Post / Page Restrictor Settings', // Title
    170                 array( $this, 'print_section_info' ), // Callback
    171                 'ss_pp_restrictor' // Page
    172             );
    173 
    174             //add setting for ftp server
    175             add_settings_field(
    176                 'page_unavailable_text', // ID
    177                 'Page unavailable text', // Title
    178                 array( $this, 'page_unavailable_text_callback' ), // Callback
    179                 'ss_pp_restrictor', // Page
    180                 'ss_pp_restrictor_settings' // Section
    181             );
    182 
    183 
    184             add_settings_field(
    185                 'post_type_select', // ID
    186                 'Apply to which post types?', // Title
    187                 array( $this, 'post_type_select_callback' ), // Callback
    188                 'ss_pp_restrictor', // Page
    189                 'ss_pp_restrictor_settings' // Section
    190             );
    191 
    192             add_settings_field(
    193                 'user_role_select', // ID
    194                 'Never display restricted content for which user types?', // Title
    195                 array( $this, 'user_role_select_callback' ), // Callback
    196                 'ss_pp_restrictor', // Page
    197                 'ss_pp_restrictor_settings' // Section
    198             );
    199 
    200         }
    201 
    202         /**
    203          * Sanitize each setting field as needed
    204          *
    205          * @param array $input Contains all settings fields as array keys
    206          */
    207         public function sanitize( $input ) {
    208             $new_input = array();
    209 
    210             if( isset( $input['page_unavailable_text'] ) ) {
    211                 $new_input['page_unavailable_text'] = sanitize_text_field( $input['page_unavailable_text'] );
    212             }
    213 
    214             if( isset( $input['post_type_select'] ) ) {
    215 
    216                 if ( is_array( $input['post_type_select'] ) ) {
    217 
    218                     $all_post_types = get_post_types();
    219 
    220                     foreach ($input['post_type_select'] as $key => $value ) {
     2if ( ! class_exists( 'Super_Simple_Post_Page_Options' ) ) {
     3    class Super_Simple_Post_Page_Options {
     4        /**
     5         * Holds the values to be used in the fields callbacks
     6         */
     7        private $options;
     8
     9        /**
     10         * Start up
     11         */
     12        public function __construct() {
     13            add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
     14            add_action( 'admin_init', array( $this, 'page_init' ) );
     15            add_action( 'add_meta_boxes', array( $this, 'add_post_restriction_checkbox' ) );
     16            add_action( 'save_post', array( $this, 'save_post_restriction_checkbox' ), 13, 2 );
     17
     18        }
     19
     20        /**
     21         * Setup checkbox meta
     22         */
     23        public function setup_post_restriction_checkbox() {
     24
     25        }
     26
     27        /**
     28         * Add checkbox to posts/pages which will allow users to select whether a post/page should be restricted
     29         */
     30        public function add_post_restriction_checkbox() {
     31
     32            //get options
     33            $this->options = get_option( 'ss_pp_restrictor_option' );
     34
     35            //get post types set on settings page
     36            $applicable_post_types = $this->options['post_type_select'];
     37
     38            $post_types = get_post_types(); //get all post types
     39
     40            if ( is_array( $applicable_post_types ) && is_array( $post_types ) ) {
     41                foreach ( $post_types as $post_type ) {
     42                    if ( in_array( $post_type, $applicable_post_types ) ) {
     43                        add_meta_box(
     44                            'ss_pp_restriction_checkbox', // Unique ID
     45                            esc_html__( 'Restrict Post?', 'ss_pp_restrictor' ),
     46                            array( $this, 'post_restriction_checkbox' ),
     47                            $post_type,
     48                            'side',
     49                            'default'
     50                        );
     51                    }
     52                }
     53            }
     54        }
     55
     56        /**
     57         * Display meta box.
     58         */
     59        public function post_restriction_checkbox( $object, $box ) {
     60
     61            wp_nonce_field( basename( __FILE__ ), 'post_restriction_checkbox_nonce' );
     62            $checked = get_post_meta( $object->ID, 'ss_pp_restrictor_checkbox', true );
     63            //var_dump( $checked );
     64            ?>
     65            <p>
     66            <label class="small-text"
     67                   for="post_restriction_checkbox"><?php _e( 'Restrict post/page content to logged-in users?', 'ss_pp_restrictor' ); ?></label>
     68            <br/>
     69            <input type="checkbox" name="ss_pp_restrictor_checkbox" id="ss_pp_restrictor_checkbox"
     70                   value="1" <?php checked( $checked ); ?> />
     71            </p><?php
     72
     73        }
     74
     75        /**
     76         * Hook to save and save $_POST variables
     77         */
     78        public function save_post_restriction_checkbox( $post_id, $post ) {
     79
     80            //verify nonce
     81            if ( ! isset( $_POST['post_restriction_checkbox_nonce'] ) || ! wp_verify_nonce( $_POST['post_restriction_checkbox_nonce'], basename( __FILE__ ) ) ) {
     82                //error_log('nonce not valid');
     83                return $post_id;
     84            }
     85
     86            //get current post type
     87            $post_type = get_post_type_object( $post->post_type );
     88
     89            //ensure current user can edit post
     90            if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
     91                return $post_id;
     92            }
     93
     94            //new checkbox value
     95            $new_checkbox_value = ( isset( $_POST['ss_pp_restrictor_checkbox'] ) ? filter_var( $_POST['ss_pp_restrictor_checkbox'], FILTER_SANITIZE_NUMBER_INT ) : '' );
     96
     97            //get old checkbox value
     98            $checkbox_value = get_post_meta( $post_id, 'ss_pp_restrictor_checkbox', true );
     99
     100            //if new value added and there is no current value
     101            if ( $new_checkbox_value && '' == $checkbox_value ) {
     102
     103                add_post_meta( $post_id, 'ss_pp_restrictor_checkbox', $new_checkbox_value, true );
     104
     105            } else if ( $new_checkbox_value && $new_checkbox_value != $checkbox_value ) { //if new checkbox value submitted and it doesn't match old
     106
     107                update_post_meta( $post_id, 'ss_pp_restrictor_checkbox', $new_checkbox_value );
     108
     109            } else if ( '' == $new_checkbox_value && $checkbox_value ) { //if new checkbox value is empty and old exists, delete new
     110
     111                delete_post_meta( $post_id, 'ss_pp_restrictor_checkbox', $checkbox_value );
     112
     113            }
     114
     115        }
     116
     117        /**
     118         * Add options page
     119         */
     120        public function add_plugin_page() {
     121            // This page will be under "Settings"
     122            add_options_page(
     123                'Settings Admin',
     124                'Super Simple Post / Page Restrictor',
     125                'manage_options',
     126                'ss_pp_restrictor',
     127                array( $this, 'create_admin_page' )
     128            );
     129        }
     130
     131        /**
     132         * Options page callback
     133         */
     134        public function create_admin_page() {
     135
     136            // Set class property
     137            $this->options = get_option( 'ss_pp_restrictor_option' );
     138
     139            //var_dump($this->options);
     140
     141            ?>
     142            <div class="wrap">
     143                <form method="post" action="options.php">
     144                    <?php
     145                    // This prints out all hidden setting fields
     146                    settings_fields( 'ss_pp_restrictor_option_group' );
     147                    do_settings_sections( 'ss_pp_restrictor' );
     148                    submit_button();
     149                    ?>
     150                </form>
     151            </div>
     152        <?php
     153        }
     154
     155        /**
     156         * Register and add settings
     157         */
     158        public function page_init() {
     159            register_setting(
     160                'ss_pp_restrictor_option_group', // Option group
     161                'ss_pp_restrictor_option', // Option name
     162                array( $this, 'sanitize' ) // Sanitize
     163            );
     164
     165            add_settings_section(
     166                'ss_pp_restrictor_settings', // ID
     167                'Super Simple Post / Page Restrictor Settings', // Title
     168                array( $this, 'print_section_info' ), // Callback
     169                'ss_pp_restrictor' // Page
     170            );
     171
     172            //add setting for ftp server
     173            add_settings_field(
     174                'page_unavailable_text', // ID
     175                'Page unavailable text', // Title
     176                array( $this, 'page_unavailable_text_callback' ), // Callback
     177                'ss_pp_restrictor', // Page
     178                'ss_pp_restrictor_settings' // Section
     179            );
     180
     181
     182            add_settings_field(
     183                'post_type_select', // ID
     184                'Apply to which post types?', // Title
     185                array( $this, 'post_type_select_callback' ), // Callback
     186                'ss_pp_restrictor', // Page
     187                'ss_pp_restrictor_settings' // Section
     188            );
     189
     190            add_settings_field(
     191                'user_role_select', // ID
     192                'Never display restricted content for which user types?', // Title
     193                array( $this, 'user_role_select_callback' ), // Callback
     194                'ss_pp_restrictor', // Page
     195                'ss_pp_restrictor_settings' // Section
     196            );
     197
     198        }
     199
     200        /**
     201         * Sanitize each setting field as needed
     202         *
     203         * @param array $input Contains all settings fields as array keys
     204         */
     205        public function sanitize( $input ) {
     206            $new_input = array();
     207
     208            if ( isset( $input['page_unavailable_text'] ) ) {
     209                $new_input['page_unavailable_text'] = sanitize_text_field( $input['page_unavailable_text'] );
     210            }
     211
     212            if ( isset( $input['post_type_select'] ) ) {
     213
     214                if ( is_array( $input['post_type_select'] ) ) {
     215
     216                    $all_post_types = get_post_types();
     217
     218                    foreach ( $input['post_type_select'] as $key => $value ) {
    221219
    222220                        //sanitize via whitelist - if input does not exist in existing post types, set value to blank string
    223                         if ( in_array( $value, $all_post_types ) ) {
    224                             $new_input['post_type_select'][ $key ] = $value;
    225                         } else {
    226                             $new_input['post_type_select'][ $key ] = '';
    227                         }
    228                        
    229                     }
    230                 } else {
    231                     $new_input['post_type_select'] = sanitize_text_field( $input['post_type_select'] );
    232                 }
    233             }
    234 
    235             if( isset( $input['user_role_select'] ) ) {
    236 
    237                 $editable_roles = array_reverse( get_editable_roles() );
    238                
    239                 if ( is_array( $input['user_role_select'] ) ) {
    240                     foreach ($input['user_role_select'] as $key => $value ) {
    241                        
    242                         //sanitize via whitelist - if input does not exist in editable roles, set value to blank string
    243                         if ( array_key_exists( $value, $editable_roles ) ) {
    244                             $new_input['user_role_select'][ $key ] = $value;
    245                         } else {
    246                             $new_input['user_role_select'][ $key ] = '';
    247                         }
    248                        
    249                     }
    250                 } else {
    251                     $new_input['user_role_select'] = sanitize_text_field( $input['user_role_select'] );
    252                 }
    253             }
    254 
    255             return $new_input;
    256 
    257         }
    258 
    259         /**
    260          * Print the Section text
    261          */
    262         public function print_section_info() {
    263             // print 'Enter your settings below:';
    264         }
    265 
    266         /**
    267          * Get the settings option array and print one of its values
    268          */
    269         public function page_unavailable_text_callback() {
    270             printf(
    271                 '<textarea id="page_unavailable_text" name="ss_pp_restrictor_option[page_unavailable_text]">%s</textarea><br>' .
    272                 '<label class="small-text" for="page_unavailable_text">Enter the text you&apos;d like to display when content is restricted.<br>Defaults to "This content is currently unavailable to you".</label>',
    273                 isset( $this->options['page_unavailable_text'] ) ? esc_attr( $this->options['page_unavailable_text']) : '',
    274                 array( 'label_for' => 'page_unavailable_text')
    275             );
    276         }
    277 
    278         public function post_type_select_callback() {
    279 
    280             $all_post_types = get_post_types();
    281             $selected_post_types = $this->options['post_type_select'];
    282 
    283             if ( is_array( $all_post_types ) ) {
    284                 echo '<select id="post_type_select" data-placeholder="Select some post types" name="ss_pp_restrictor_option[post_type_select][]" multiple>';
    285                 foreach ( $all_post_types as $key => $post_type ) {
    286                     $selected = '';
    287                     if ( is_array( $selected_post_types ) ) {
    288                         $selected = in_array( $post_type, $selected_post_types ) ? 'selected' : '';
    289                     }
    290 
    291                     printf('<option value="%s" %s>%s</option>', $post_type, $selected, $post_type);
    292                 }
    293                 echo '</select>';
    294             }
    295 
    296         }
    297 
    298 
    299         public function user_role_select_callback() {
    300 
    301             $selected_user_roles = isset($this->options['user_role_select']) ? $this->options['user_role_select'] : array();
    302             ?>
    303             <select id="user_role_select" data-placeholder="Select some user roles" name="ss_pp_restrictor_option[user_role_select][]" multiple>';
    304               <?php $this->wp_dropdown_roles( $selected_user_roles );?>
    305             </select><br>
    306             <label class="small-text" for="user_role_select">Selected user roles will never be able to see restricted content - even when logged in.</label><?php
    307         }
    308 
    309 
    310 
    311         /*modified wp_dropdown_roles() function - copied from /wp-admin/includes/template.php */
    312         private function wp_dropdown_roles( $selected = false ) {
    313 
    314             $output = '';
    315 
    316             $editable_roles = array_reverse( get_editable_roles() );
    317 
    318             //error_log(print_r($editable_roles, true));
    319 
    320             if ( !is_array( $selected ) ) {
    321               $selected = array ( $selected );
    322             }
    323 
    324 
    325             foreach ( $editable_roles as $role => $details ) {
    326               $name = translate_user_role($details['name'] );
    327               if ( in_array( $role, $selected ) ) {// preselect specified role
    328                 $output .= "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
    329               }
    330               else {
    331                 $output .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
    332               }
    333             }
    334 
    335 
    336             echo $output;
    337 
    338         }
    339     }
     221                        if ( in_array( $value, $all_post_types ) ) {
     222                            $new_input['post_type_select'][ $key ] = $value;
     223                        } else {
     224                            $new_input['post_type_select'][ $key ] = '';
     225                        }
     226
     227                    }
     228                } else {
     229                    $new_input['post_type_select'] = sanitize_text_field( $input['post_type_select'] );
     230                }
     231            }
     232
     233            if ( isset( $input['user_role_select'] ) ) {
     234
     235                $editable_roles = array_reverse( get_editable_roles() );
     236
     237                if ( is_array( $input['user_role_select'] ) ) {
     238                    foreach ( $input['user_role_select'] as $key => $value ) {
     239
     240                        //sanitize via whitelist - if input does not exist in editable roles, set value to blank string
     241                        if ( array_key_exists( $value, $editable_roles ) ) {
     242                            $new_input['user_role_select'][ $key ] = $value;
     243                        } else {
     244                            $new_input['user_role_select'][ $key ] = '';
     245                        }
     246
     247                    }
     248                } else {
     249                    $new_input['user_role_select'] = sanitize_text_field( $input['user_role_select'] );
     250                }
     251            }
     252
     253            return $new_input;
     254
     255        }
     256
     257        /**
     258         * Print the Section text
     259         */
     260        public function print_section_info() {
     261            // print 'Enter your settings below:';
     262        }
     263
     264        /**
     265         * Get the settings option array and print one of its values
     266         */
     267        public function page_unavailable_text_callback() {
     268            printf(
     269                '<textarea id="page_unavailable_text" name="ss_pp_restrictor_option[page_unavailable_text]">%s</textarea><br>' .
     270                '<label class="small-text" for="page_unavailable_text">' . __( 'Enter the text you&apos;d like to display when content is restricted.', 'ss_pp_restrictor' ) . '<br>' . __( 'Defaults to "This content is currently unavailable to you".', 'ss_pp_restrictor' ) . '</label>',
     271                isset( $this->options['page_unavailable_text'] ) ? esc_attr( $this->options['page_unavailable_text'] ) : '',
     272                array( 'label_for' => 'page_unavailable_text' )
     273            );
     274        }
     275
     276        public function post_type_select_callback() {
     277
     278            $all_post_types      = get_post_types();
     279            $selected_post_types = $this->options['post_type_select'];
     280
     281            if ( is_array( $all_post_types ) ) {
     282                echo '<select id="post_type_select" data-placeholder="' . __( 'Select some post types', 'ss_pp_restrictor' ) . '" name="ss_pp_restrictor_option[post_type_select][]" multiple>';
     283                foreach ( $all_post_types as $key => $post_type ) {
     284                    $selected = '';
     285                    if ( is_array( $selected_post_types ) ) {
     286                        $selected = in_array( $post_type, $selected_post_types ) ? 'selected' : '';
     287                    }
     288
     289                    printf( '<option value="%s" %s>%s</option>', $post_type, $selected, $post_type );
     290                }
     291                echo '</select>';
     292            }
     293
     294        }
     295
     296
     297        public function user_role_select_callback() {
     298
     299            $selected_user_roles = isset( $this->options['user_role_select'] ) ? $this->options['user_role_select'] : array();
     300            ?>
     301            <select id="user_role_select"
     302                    data-placeholder="<?php _e( 'Select some user roles', 'ss_pp_restrictor' ); ?>"
     303                    name="ss_pp_restrictor_option[user_role_select][]" multiple>';
     304                <?php $this->wp_dropdown_roles( $selected_user_roles ); ?>
     305            </select><br>
     306            <label class="small-text"
     307                   for="user_role_select"><?php _e( 'Selected user roles will never be able to see restricted content - even when logged in.', 'ss_pp_restrictor' ); ?></label><?php
     308        }
     309
     310
     311        /*modified wp_dropdown_roles() function - copied from /wp-admin/includes/template.php */
     312        private function wp_dropdown_roles( $selected = false ) {
     313
     314            $output = '';
     315
     316            $editable_roles = array_reverse( get_editable_roles() );
     317
     318            if ( ! is_array( $selected ) ) {
     319                $selected = array( $selected );
     320            }
     321
     322
     323            foreach ( $editable_roles as $role => $details ) {
     324                $name = translate_user_role( $details['name'] );
     325                if ( in_array( $role, $selected ) ) {// preselect specified role
     326                    $output .= "\n\t<option selected='selected' value='" . esc_attr( $role ) . "'>$name</option>";
     327                } else {
     328                    $output .= "\n\t<option value='" . esc_attr( $role ) . "'>$name</option>";
     329                }
     330            }
     331
     332
     333            echo $output;
     334
     335        }
     336    }
    340337}
    341338?>
  • super-simple-post-page-restricor/trunk/super-simple-post-page-restrictor.php

    r1033497 r1177974  
    2424    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2525*/
    26 if ( !class_exists( 'Super_Simple_Page_Post_Restrictor' ) ) {
     26if ( ! class_exists( 'Super_Simple_Page_Post_Restrictor' ) ) {
    2727
    2828    class Super_Simple_Page_Post_Restrictor {
     
    4242
    4343            //hook main function
    44             //add_action( 'pre_get_posts', array( $this, 'dump_query' ) );
    4544            add_action( 'the_post', array( $this, 'clean_post' ), 1 );
    4645
    4746            //enqueue scripts and styles
    4847            add_action( 'admin_enqueue_scripts', array( $this, 'super_simple_post_restrictor_admin_scripts' ) );
    49 
    50             //restrict feed content
    51             // add_action('rss_head', array( $this, 'restrict_feed' ) );
    52             // add_action('rss2_head', array( $this, 'restrict_feed' ) );
    5348
    5449        } // End init()
     
    6055        }
    6156
    62         public function super_simple_post_restrictor_admin_scripts(){
     57        public function super_simple_post_restrictor_admin_scripts() {
    6358            wp_enqueue_style( 'chosen-styles', plugin_dir_url( __FILE__ ) . '/assets/css/chosen.min.css' );
    6459            wp_enqueue_script( 'chosen-script', plugin_dir_url( __FILE__ ) . '/assets/js/chosen.jquery.min.js', array( 'jquery' ), '1.1.0', true );
    65             wp_enqueue_script( 'chosen-init', plugin_dir_url( __FILE__ ) . '/assets/js/chosen-init.js', array( 'jquery', 'chosen-script') );
     60            wp_enqueue_script( 'chosen-init', plugin_dir_url( __FILE__ ) . '/assets/js/chosen-init.js', array(
     61                'jquery',
     62                'chosen-script'
     63            ) );
    6664        }
    6765
    6866        public function clean_post( $post_object ) {
    69 
    70             //var_dump($post_object);
    7167
    7268            $this->options = get_option( 'ss_pp_restrictor_option' );
     
    7874            $restricted_post_type = false;
    7975            if ( is_array( $this->options['post_type_select'] ) ) {
    80                 if ( in_array(  $post_object->post_type, $this->options['post_type_select'] ) ) {
     76                if ( in_array( $post_object->post_type, $this->options['post_type_select'] ) ) {
    8177                    $restricted_post_type = true;
    8278                }
     
    9490            //loop through current user roles and check if any roles are in restricted roles array
    9591            if ( is_array( $current_user_roles ) ) { //first check if is array (settings need to be set)
    96                 foreach ($current_user_roles as $key => $role) {
    97                     if ( is_array( $restricted_roles ) && in_array( $role, $restricted_roles) ) { //if restricted roles is set (is_array()) and current role is in restricted roles
     92                foreach ( $current_user_roles as $key => $role ) {
     93                    if ( is_array( $restricted_roles ) && in_array( $role, $restricted_roles ) ) { //if restricted roles is set (is_array()) and current role is in restricted roles
    9894                        //restrict access
    9995                        $current_user_can_access = false;
     
    10399
    104100            //if current post is restricted and user is not logged in - OR - check if current post is restricted and user can't access
    105             if ( $this->current_post_checkbox && !is_user_logged_in() && $restricted_post_type || $restricted_post_type && $this->current_post_checkbox && !$current_user_can_access ) {
    106                 add_filter('the_content', array( $this, 'filter_content' ) );
    107                 add_filter('the_excerpt', array( $this, 'filter_excerpt' ) );
    108                 // add_action('rss_head', array( $this, 'restrict_feed' ) );
    109                 // add_action('rss2_head', array( $this, 'restrict_feed' ) );
    110             } else {
    111 
     101            if ( $this->current_post_checkbox && ! is_user_logged_in() && $restricted_post_type || $restricted_post_type && $this->current_post_checkbox && ! $current_user_can_access ) {
     102                add_filter( 'the_content', array( $this, 'filter_content' ) );
     103                add_filter( 'the_excerpt', array( $this, 'filter_excerpt' ) );
    112104            }
    113105
    114106        }
    115    
    116         public function restrict_feed(){
    117             die('this is the rss_head');
    118             add_filter('the_content', array( $this, 'filter_feed_content' ) );
     107
     108        public function restrict_feed() {
     109            die( 'this is the rss_head' );
     110            add_filter( 'the_content', array( $this, 'filter_feed_content' ) );
    119111        }
    120112
    121         public function filter_feed_content($content) {
     113        public function filter_feed_content( $content ) {
    122114            global $wp_query;
    123             if (is_feed() && $this->current_post_checkbox) {
     115            if ( is_feed() && $this->current_post_checkbox ) {
    124116                $this->page_unavailable_text = $this->options['page_unavailable_text'];
    125                 $post_content = !empty( $this->page_unavailable_text ) ? $this->page_unavailable_text : 'This content is currently unavailable to you. ';
    126                 return $post_content;
    127             } else {
    128                 return $content;
    129             }
    130            
    131         }       
    132        
    133         public function filter_content($content) {
    134             if ( $this->current_post_checkbox ) {
     117                $post_content                = ! empty( $this->page_unavailable_text ) ? $this->page_unavailable_text : 'This content is currently unavailable to you. ';
    135118
    136                 $this->page_unavailable_text = $this->options['page_unavailable_text'];
    137                 $post_content = !empty( $this->page_unavailable_text ) ? $this->page_unavailable_text : 'This content is currently unavailable to you. ';
    138119                return $post_content;
    139120            } else {
     
    143124        }
    144125
    145         public function filter_excerpt($excerpt) {
     126        public function filter_content( $content ) {
    146127            if ( $this->current_post_checkbox ) {
    147128
    148129                $this->page_unavailable_text = $this->options['page_unavailable_text'];
    149                 $post_content = !empty( $this->page_unavailable_text ) ? $this->page_unavailable_text : 'This content is currently unavailable to you. ';
     130                $post_content                = ! empty( $this->page_unavailable_text ) ? $this->page_unavailable_text : 'This content is currently unavailable to you. ';
     131
     132                return $post_content;
     133            } else {
     134                return $content;
     135            }
     136
     137        }
     138
     139        public function filter_excerpt( $excerpt ) {
     140            if ( $this->current_post_checkbox ) {
     141
     142                $this->page_unavailable_text = $this->options['page_unavailable_text'];
     143                $post_content                = ! empty( $this->page_unavailable_text ) ? $this->page_unavailable_text : 'This content is currently unavailable to you. ';
     144
    150145                return $post_content;
    151146            } else {
     
    156151
    157152        /**
    158          * Returns the role of the current user. 
     153         * Returns the role of the current user.
    159154         *
    160155         * @return array containing the names of the current users role(s)
     
    163158            global $wp_roles;
    164159            $current_user = wp_get_current_user();
    165             $roles = $current_user->roles;
     160            $roles        = $current_user->roles;
    166161
    167162            $translated_roles = array();
    168                
    169             foreach ($roles as $key => $role) {
    170                 //$translated_roles[] = isset($wp_roles->role_names[$role]) ? translate_user_role($wp_roles->role_names[$role] ) : $role;
     163
     164            foreach ( $roles as $key => $role ) {
    171165                $translated_roles[] = $role;
    172166            }
Note: See TracChangeset for help on using the changeset viewer.