Changeset 1399882
- Timestamp:
- 04/20/2016 03:21:34 AM (10 years ago)
- Location:
- super-simple-post-page-restricor/trunk
- Files:
-
- 4 edited
-
README.md (modified) (2 diffs)
-
README.txt (modified) (2 diffs)
-
super-simple-post-page-restrictor-options.php (modified) (13 diffs)
-
super-simple-post-page-restrictor.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
super-simple-post-page-restricor/trunk/README.md
r951353 r1399882 39 39 - User roles selected here will **never** be able to see restricted content, regardless of whether they are logged in. 40 40 41 **Default restriction?** 42 43 - If this option is checked, posts / pages that are auto-generated should be restricted in most cases. 44 - If this option is checked, newly created posts / pages should have their 'restrict content' checkboxes checked. 45 41 46 Future Development 42 47 ------------------ … … 45 50 46 51 - Add shortcode to restrict content - content placed between start/end shortcodes would be restricted 47 - Res ctrict content in RSS feeds52 - Restrict content in RSS feeds -
super-simple-post-page-restricor/trunk/README.txt
r1332582 r1399882 1 1 === Super Simple Post / Page Restrictor === 2 Contributors: arippberger , werkpress2 Contributors: arippberger 3 3 Tags: restrict content, restrictor, super simple, user login, login, restrict 4 4 Donate link: http://alecrippberger.com 5 5 Requires at least: 3.0.1 6 Tested up to: 3.9.17 Stable tag: 1. 06 Tested up to: 4.5 7 Stable tag: 1.2 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 48 48 - User roles selected here will **never** be able to see restricted content, regardless of whether they are logged in. 49 49 50 **Default restriction?** 51 52 - If this option is checked, posts / pages that are auto-generated should be restricted in most cases. 53 - If this option is checked, newly created posts / pages should have their 'restrict content' checkboxes checked. 54 50 55 == Future Development == 51 56 -
super-simple-post-page-restricor/trunk/super-simple-post-page-restrictor-options.php
r1177974 r1399882 2 2 if ( ! class_exists( 'Super_Simple_Post_Page_Options' ) ) { 3 3 class Super_Simple_Post_Page_Options { 4 4 5 /** 5 6 * Holds the values to be used in the fields callbacks 6 7 */ 7 8 private $options; 8 9 9 10 /** 10 11 * Start up … … 15 16 add_action( 'add_meta_boxes', array( $this, 'add_post_restriction_checkbox' ) ); 16 17 add_action( 'save_post', array( $this, 'save_post_restriction_checkbox' ), 13, 2 ); 17 18 } 19 18 19 } 20 20 21 /** 21 22 * Setup checkbox meta 22 23 */ 23 24 public function setup_post_restriction_checkbox() { 24 25 } 26 25 26 } 27 27 28 /** 28 29 * Add checkbox to posts/pages which will allow users to select whether a post/page should be restricted 29 30 */ 30 31 public function add_post_restriction_checkbox() { 31 32 32 33 //get options 33 34 $this->options = get_option( 'ss_pp_restrictor_option' ); 34 35 35 36 //get post types set on settings page 36 $applicable_post_types = $this->options[ 'post_type_select'];37 37 $applicable_post_types = $this->options[ 'post_type_select' ]; 38 38 39 $post_types = get_post_types(); //get all post types 39 40 40 41 if ( is_array( $applicable_post_types ) && is_array( $post_types ) ) { 41 42 foreach ( $post_types as $post_type ) { … … 53 54 } 54 55 } 55 56 56 57 /** 57 58 * Display meta box. 58 59 */ 59 60 public function post_restriction_checkbox( $object, $box ) { 60 61 61 62 wp_nonce_field( basename( __FILE__ ), 'post_restriction_checkbox_nonce' ); 63 62 64 $checked = get_post_meta( $object->ID, 'ss_pp_restrictor_checkbox', true ); 63 //var_dump( $checked ); 65 66 $restrict_by_default = isset( $this->options[ 'restrict_by_default' ] ) ? $this->options[ 'restrict_by_default' ] : false; 67 68 $checked = $restrict_by_default && $checked === '' ? true : false; 69 64 70 ?> 65 71 <p> … … 70 76 value="1" <?php checked( $checked ); ?> /> 71 77 </p><?php 72 73 } 74 78 79 } 80 75 81 /** 76 82 * Hook to save and save $_POST variables 83 * 84 * @param $post_id 85 * @param $post 86 * 87 * @return mixed 77 88 */ 78 89 public function save_post_restriction_checkbox( $post_id, $post ) { 79 90 91 $nonce = filter_input( INPUT_POST, 'post_restriction_checkbox_nonce' ); 92 $checkbox = isset( $_POST[ 'ss_pp_restrictor_checkbox' ] ) ? filter_input( INPUT_POST, 'ss_pp_restrictor_checkbox', FILTER_SANITIZE_NUMBER_INT ) : 0; 93 80 94 //verify nonce 81 if ( ! isset( $ _POST['post_restriction_checkbox_nonce'] ) || ! wp_verify_nonce( $_POST['post_restriction_checkbox_nonce'], basename( __FILE__ ) ) ) {95 if ( ! isset( $nonce ) || ! wp_verify_nonce( $nonce, basename( __FILE__ ) ) ) { 82 96 //error_log('nonce not valid'); 83 97 return $post_id; 84 98 } 85 99 86 100 //get current post type 87 101 $post_type = get_post_type_object( $post->post_type ); 88 102 89 103 //ensure current user can edit post 90 104 if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) { 91 105 return $post_id; 92 106 } 93 107 94 108 //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 109 $new_checkbox_value = $checkbox === '1' || $checkbox === 0 ? $checkbox : ''; 110 111 update_post_meta( $post_id, 'ss_pp_restrictor_checkbox', $new_checkbox_value ); 112 113 return $post_id; 114 115 } 116 117 117 /** 118 118 * Add options page … … 128 128 ); 129 129 } 130 130 131 131 /** 132 132 * Options page callback 133 133 */ 134 134 public function create_admin_page() { 135 135 136 136 // Set class property 137 137 $this->options = get_option( 'ss_pp_restrictor_option' ); 138 138 139 139 //var_dump($this->options); 140 140 141 141 ?> 142 142 <div class="wrap"> … … 150 150 </form> 151 151 </div> 152 <?php153 } 154 152 <?php 153 } 154 155 155 /** 156 156 * Register and add settings … … 162 162 array( $this, 'sanitize' ) // Sanitize 163 163 ); 164 164 165 165 add_settings_section( 166 166 'ss_pp_restrictor_settings', // ID 167 'Super Simple Post / Page Restrictor Settings', // Title167 __( 'Super Simple Post / Page Restrictor Settings', 'ss_pp_restrictor' ), // Title 168 168 array( $this, 'print_section_info' ), // Callback 169 169 'ss_pp_restrictor' // Page 170 170 ); 171 171 172 172 //add setting for ftp server 173 173 add_settings_field( 174 174 'page_unavailable_text', // ID 175 'Page unavailable text', // Title175 __( 'Page unavailable text', 'ss_pp_restrictor' ), // Title 176 176 array( $this, 'page_unavailable_text_callback' ), // Callback 177 177 'ss_pp_restrictor', // Page … … 179 179 ); 180 180 181 182 181 add_settings_field( 183 182 'post_type_select', // ID 184 'Apply to which post types?', // Title183 __( 'Apply to which post types?', 'ss_pp_restrictor' ), // Title 185 184 array( $this, 'post_type_select_callback' ), // Callback 186 185 'ss_pp_restrictor', // Page 187 186 'ss_pp_restrictor_settings' // Section 188 187 ); 189 188 190 189 add_settings_field( 191 190 'user_role_select', // ID 192 'Never display restricted content for which user types?', // Title191 __( 'Never display restricted content for which user types?', 'ss_pp_restrictor' ), // Title 193 192 array( $this, 'user_role_select_callback' ), // Callback 194 193 'ss_pp_restrictor', // Page 195 194 'ss_pp_restrictor_settings' // Section 196 195 ); 197 198 } 199 196 197 add_settings_field( 198 'restrict_by_default', // ID 199 __( 'Check this box to restrict new posts/pages by default.', 'ss_pp_restrictor' ), // Title 200 array( $this, 'restrict_by_default_callback' ), // Callback 201 'ss_pp_restrictor', // Page 202 'ss_pp_restrictor_settings' // Section 203 ); 204 205 } 206 200 207 /** 201 208 * Sanitize each setting field as needed 202 209 * 203 * @param array $input Contains all settings fields as array keys 210 * @param $input 211 * 212 * @return array 204 213 */ 205 214 public function sanitize( $input ) { 206 215 $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 217 if ( isset( $input[ 'page_unavailable_text' ] ) ) { 218 $new_input[ 'page_unavailable_text' ] = sanitize_text_field( $input[ 'page_unavailable_text' ] ); 219 } 220 221 if ( isset( $input[ 'post_type_select' ] ) ) { 222 223 if ( is_array( $input[ 'post_type_select' ] ) ) { 224 216 225 $all_post_types = get_post_types(); 217 218 foreach ( $input[ 'post_type_select'] as $key => $value ) {219 226 227 foreach ( $input[ 'post_type_select' ] as $key => $value ) { 228 220 229 //sanitize via whitelist - if input does not exist in existing post types, set value to blank string 221 230 if ( in_array( $value, $all_post_types ) ) { 222 $new_input[ 'post_type_select'][ $key ] = $value;231 $new_input[ 'post_type_select' ][ $key ] = $value; 223 232 } else { 224 $new_input[ 'post_type_select'][ $key ] = '';233 $new_input[ 'post_type_select' ][ $key ] = ''; 225 234 } 226 235 227 236 } 228 237 } 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 238 $new_input[ 'post_type_select' ] = sanitize_text_field( $input[ 'post_type_select' ] ); 239 } 240 } 241 242 if ( isset( $input[ 'user_role_select' ] ) ) { 243 235 244 $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 245 246 if ( is_array( $input[ 'user_role_select' ] ) ) { 247 foreach ( $input[ 'user_role_select' ] as $key => $value ) { 248 240 249 //sanitize via whitelist - if input does not exist in editable roles, set value to blank string 241 250 if ( array_key_exists( $value, $editable_roles ) ) { 242 $new_input[ 'user_role_select'][ $key ] = $value;251 $new_input[ 'user_role_select' ][ $key ] = $value; 243 252 } else { 244 $new_input[ 'user_role_select'][ $key ] = '';253 $new_input[ 'user_role_select' ][ $key ] = ''; 245 254 } 246 255 247 256 } 248 257 } else { 249 $new_input['user_role_select'] = sanitize_text_field( $input['user_role_select'] ); 250 } 251 } 252 258 $new_input[ 'user_role_select' ] = sanitize_text_field( $input[ 'user_role_select' ] ); 259 } 260 } 261 262 // sanitize restrict by default 263 if ( isset( $input[ 'restrict_by_default' ] ) ) { 264 $restrict_by_default = $input[ 'restrict_by_default' ]; 265 266 if ( $restrict_by_default === '1' || $restrict_by_default === 0 || $restrict_by_default === '' ) { 267 $new_input[ 'restrict_by_default' ] = $restrict_by_default; 268 } 269 } 270 253 271 return $new_input; 254 255 } 256 272 273 } 274 257 275 /** 258 276 * Print the Section text … … 261 279 // print 'Enter your settings below:'; 262 280 } 263 281 264 282 /** 265 283 * Get the settings option array and print one of its values … … 269 287 '<textarea id="page_unavailable_text" name="ss_pp_restrictor_option[page_unavailable_text]">%s</textarea><br>' . 270 288 '<label class="small-text" for="page_unavailable_text">' . __( 'Enter the text you'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'] ) : '',289 isset( $this->options[ 'page_unavailable_text' ] ) ? esc_attr( $this->options[ 'page_unavailable_text' ] ) : '', 272 290 array( 'label_for' => 'page_unavailable_text' ) 273 291 ); 274 292 } 275 293 294 /** 295 * Callback for post type select 296 */ 276 297 public function post_type_select_callback() { 277 298 278 299 $all_post_types = get_post_types(); 279 $selected_post_types = $this->options[ 'post_type_select'];280 300 $selected_post_types = $this->options[ 'post_type_select' ]; 301 281 302 if ( is_array( $all_post_types ) ) { 282 303 echo '<select id="post_type_select" data-placeholder="' . __( 'Select some post types', 'ss_pp_restrictor' ) . '" name="ss_pp_restrictor_option[post_type_select][]" multiple>'; … … 286 307 $selected = in_array( $post_type, $selected_post_types ) ? 'selected' : ''; 287 308 } 288 309 289 310 printf( '<option value="%s" %s>%s</option>', $post_type, $selected, $post_type ); 290 311 } 291 312 echo '</select>'; 292 313 } 293 294 } 295 296 314 315 } 316 317 /** 318 * Callback for user role select 319 */ 297 320 public function user_role_select_callback() { 298 299 $selected_user_roles = isset( $this->options[ 'user_role_select'] ) ? $this->options['user_role_select'] : array();321 322 $selected_user_roles = isset( $this->options[ 'user_role_select' ] ) ? $this->options[ 'user_role_select' ] : array(); 300 323 ?> 301 324 <select id="user_role_select" … … 307 330 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 331 } 309 310 311 /*modified wp_dropdown_roles() function - copied from /wp-admin/includes/template.php */ 332 333 334 /** 335 * Callback for restrict by default 336 */ 337 public function restrict_by_default_callback() { 338 339 $restrict_by_default = isset( $this->options[ 'restrict_by_default' ] ) ? $this->options[ 'restrict_by_default' ] : false; 340 341 ?> 342 <input name="ss_pp_restrictor_option[restrict_by_default]" type="checkbox" id="restrict_by_default" 343 value="1" <?php checked( '1', $restrict_by_default ); ?> /> 344 <label class="small-text" 345 for="restrict_by_default"><?php _e( 'Check box to restrict posts/pages by default (checkbox checked and content restricted automatically on new posts).', 'ss_pp_restrictor' ); ?></label> 346 <?php 347 348 } 349 350 /** 351 * modified wp_dropdown_roles() function - copied from /wp-admin/includes/template.php 352 * 353 * @param bool $selected 354 */ 312 355 private function wp_dropdown_roles( $selected = false ) { 313 356 314 357 $output = ''; 315 358 316 359 $editable_roles = array_reverse( get_editable_roles() ); 317 360 318 361 if ( ! is_array( $selected ) ) { 319 362 $selected = array( $selected ); 320 363 } 321 322 364 323 365 foreach ( $editable_roles as $role => $details ) { 324 $name = translate_user_role( $details[ 'name'] );366 $name = translate_user_role( $details[ 'name' ] ); 325 367 if ( in_array( $role, $selected ) ) {// preselect specified role 326 368 $output .= "\n\t<option selected='selected' value='" . esc_attr( $role ) . "'>$name</option>"; … … 330 372 } 331 373 332 333 374 echo $output; 334 375 335 376 } 336 377 } 337 378 } 338 ?> -
super-simple-post-page-restricor/trunk/super-simple-post-page-restrictor.php
r1177974 r1399882 4 4 Plugin URI: https://github.com/arippberger/super-simple-post-page-restrictor 5 5 Description: Adds a super simple post / page restriction option 6 Version: 1. 16 Version: 1.2 7 7 Author: arippberger 8 8 Author URI: http://alecrippberger.com … … 26 26 if ( ! class_exists( 'Super_Simple_Page_Post_Restrictor' ) ) { 27 27 28 /** 29 * Class Super_Simple_Page_Post_Restrictor 30 */ 28 31 class Super_Simple_Page_Post_Restrictor { 29 32 33 /** 34 * @var mixed|void 35 */ 36 protected $options; 37 38 /** 39 * @var 40 */ 41 protected $admin; 42 43 /** 44 * @var 45 */ 46 protected $current_post_checkbox; 47 48 /** 49 * @var 50 */ 51 protected $page_unavailable_text; 52 53 /** 54 * Super_Simple_Page_Post_Restrictor constructor. 55 */ 30 56 public function __construct() { 31 57 … … 37 63 } 38 64 65 $this->options = get_option( 'ss_pp_restrictor_option' ); 66 39 67 } // End __construct() 40 68 69 /** 70 * Init front end 71 */ 41 72 public function init_frontend() { 42 73 … … 49 80 } // End init() 50 81 82 /** 83 * Admin includes 84 */ 51 85 public function admin_includes() { 52 86 // loads the admin settings page and adds functionality to the order admin 53 87 require_once( 'super-simple-post-page-restrictor-options.php' ); 54 $this->admin = new Super_simple_post_page_options(); 55 } 56 88 $this->admin = new Super_Simple_Post_Page_Options(); 89 } 90 91 /** 92 * Admin scripts 93 */ 57 94 public function super_simple_post_restrictor_admin_scripts() { 58 95 wp_enqueue_style( 'chosen-styles', plugin_dir_url( __FILE__ ) . '/assets/css/chosen.min.css' ); … … 60 97 wp_enqueue_script( 'chosen-init', plugin_dir_url( __FILE__ ) . '/assets/js/chosen-init.js', array( 61 98 'jquery', 62 'chosen-script' 99 'chosen-script', 63 100 ) ); 64 101 } 65 102 103 /** 104 * 105 * 106 * @param $post_object 107 */ 66 108 public function clean_post( $post_object ) { 109 110 $restricted_by_default = false; 67 111 68 112 $this->options = get_option( 'ss_pp_restrictor_option' ); … … 71 115 $this->current_post_checkbox = get_post_meta( $post_object->ID, 'ss_pp_restrictor_checkbox', true ); 72 116 117 $restrict_by_default_option = isset( $this->options[ 'restrict_by_default' ] ) ? $this->options[ 'restrict_by_default' ] : false; 118 119 if ( $this->current_post_checkbox === '' && $restrict_by_default_option ) { 120 $restricted_by_default = true; 121 } 122 73 123 //see if current post type is restricted 74 124 $restricted_post_type = false; 75 if ( is_array( $this->options[ 'post_type_select'] ) ) {76 if ( in_array( $post_object->post_type, $this->options[ 'post_type_select'] ) ) {125 if ( is_array( $this->options[ 'post_type_select' ] ) ) { 126 if ( in_array( $post_object->post_type, $this->options[ 'post_type_select' ] ) ) { 77 127 $restricted_post_type = true; 78 128 } … … 80 130 81 131 //get array of roles that may NEVER access content 82 $restricted_roles = $this->options['user_role_select'];132 $restricted_roles = isset( $this->options[ 'user_role_select' ] ) ? $this->options[ 'user_role_select' ] : array(); 83 133 84 134 //get array of current user roles … … 99 149 100 150 //if current post is restricted and user is not logged in - OR - check if current post is restricted and user can't access 101 if ( $this->current_post_checkbox && ! is_user_logged_in() && $restricted_post_type || $restricted_post_type && $this->current_post_checkbox && ! $current_user_can_access ) { 151 if ( 152 $restricted_by_default && ! is_user_logged_in() && $restricted_post_type || 153 $restricted_by_default && $restricted_post_type && ! $current_user_can_access || 154 $this->current_post_checkbox && ! is_user_logged_in() && $restricted_post_type || 155 $restricted_post_type && $this->current_post_checkbox && ! $current_user_can_access 156 ) { 102 157 add_filter( 'the_content', array( $this, 'filter_content' ) ); 103 158 add_filter( 'the_excerpt', array( $this, 'filter_excerpt' ) ); … … 106 161 } 107 162 163 /** 164 * 165 */ 108 166 public function restrict_feed() { 109 167 die( 'this is the rss_head' ); 110 add_filter( 'the_content', array( $this, 'filter_feed_content' ) ); 111 } 112 168 //add_filter( 'the_content', array( $this, 'filter_feed_content' ) ); 169 } 170 171 /** 172 * @param $content 173 * 174 * @return string 175 */ 113 176 public function filter_feed_content( $content ) { 114 global $wp_query; 177 115 178 if ( is_feed() && $this->current_post_checkbox ) { 116 $this->page_unavailable_text = $this->options[ 'page_unavailable_text'];179 $this->page_unavailable_text = $this->options[ 'page_unavailable_text' ]; 117 180 $post_content = ! empty( $this->page_unavailable_text ) ? $this->page_unavailable_text : 'This content is currently unavailable to you. '; 118 181 … … 124 187 } 125 188 189 /** 190 * @param $content 191 * 192 * @return string 193 */ 126 194 public function filter_content( $content ) { 127 195 if ( $this->current_post_checkbox ) { 128 196 129 $this->page_unavailable_text = $this->options[ 'page_unavailable_text'];197 $this->page_unavailable_text = $this->options[ 'page_unavailable_text' ]; 130 198 $post_content = ! empty( $this->page_unavailable_text ) ? $this->page_unavailable_text : 'This content is currently unavailable to you. '; 131 199 … … 137 205 } 138 206 207 /** 208 * @param $excerpt 209 * 210 * @return string 211 */ 139 212 public function filter_excerpt( $excerpt ) { 140 213 if ( $this->current_post_checkbox ) { 141 214 142 $this->page_unavailable_text = $this->options[ 'page_unavailable_text'];215 $this->page_unavailable_text = $this->options[ 'page_unavailable_text' ]; 143 216 $post_content = ! empty( $this->page_unavailable_text ) ? $this->page_unavailable_text : 'This content is currently unavailable to you. '; 144 217 … … 156 229 **/ 157 230 private function get_current_user_roles() { 158 global $wp_roles; 231 159 232 $current_user = wp_get_current_user(); 160 233 $roles = $current_user->roles; … … 173 246 global $super_simple_page_post_restrictor; 174 247 $super_simple_page_post_restrictor = new Super_Simple_Page_Post_Restrictor(); 175 176 ?>
Note: See TracChangeset
for help on using the changeset viewer.