WordPress Access Control - Version 2.1

Version Description

  • Added an icon on the page list dialog that shows Non-member or Member only statuses
  • Bug Fix: Fixed a PHP4 bug by replacing self with the full class name thanks to itpixie
  • Bug Fix: Fixed a bug where pages with roles set could lock an administrator out (Thanks to evlapix for reporting)
  • Bug Fix: After logging in you are now redirected back to the page you were trying to access
Download this release

Release Info

Developer brandon.wamboldt
Plugin Icon wp plugin WordPress Access Control
Version 2.1
Comparing to
See all releases

Code changes from version 2.0 to 2.1

public/css/wpac.css ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @CHARSET "UTF-8";
2
+
3
+ .column-wpac {
4
+ width:50px;
5
+ }
6
+
7
+ .column-wpac img {
8
+ width:20px;
9
+ height:20px;
10
+ }
11
+
12
+ .column-wpac img:hover {
13
+ cursor:help;
14
+ }
public/images/lock.png ADDED
Binary file
public/images/unlock.png ADDED
Binary file
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
4
  Tags: members, only, plugin, restricted, access, menus, 3.0, wp_nav_menu, nonmembers
5
  Requires at least: 2.9
6
  Tested up to: 3.1
7
- Stable tag: 2.0
8
 
9
  Restrict pages to members, nonmembers or specific roles and still add to navigation
10
 
@@ -41,6 +41,12 @@ This was caused by PHP4, which is now supported in version 1.4
41
 
42
  == Changelog ==
43
 
 
 
 
 
 
 
44
  = 2.0 =
45
  * Added the ability to mark pages as non-members only
46
  * Added the ability to restrict pages to specific roles
4
  Tags: members, only, plugin, restricted, access, menus, 3.0, wp_nav_menu, nonmembers
5
  Requires at least: 2.9
6
  Tested up to: 3.1
7
+ Stable tag: 2.1
8
 
9
  Restrict pages to members, nonmembers or specific roles and still add to navigation
10
 
41
 
42
  == Changelog ==
43
 
44
+ = 2.1 =
45
+ * Added an icon on the page list dialog that shows Non-member or Member only statuses
46
+ * Bug Fix: Fixed a PHP4 bug by replacing self with the full class name thanks to `itpixie`
47
+ * Bug Fix: Fixed a bug where pages with roles set could lock an administrator out (Thanks to `evlapix` for reporting)
48
+ * Bug Fix: After logging in you are now redirected back to the page you were trying to access
49
+
50
  = 2.0 =
51
  * Added the ability to mark pages as non-members only
52
  * Added the ability to restrict pages to specific roles
templates/meta_box.php ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <input type="hidden" name="members_only_nonce" id="members_only_nonce" value="<?php echo wp_create_nonce('members_only'); ?>" />
2
+
3
+ <div id="members_only_container">
4
+ <input type="checkbox" name="members_only" id="members_only" value="true" <?php echo $is_members_only; ?> />
5
+ <?php _e('Only accessible by members?'); ?>
6
+
7
+ <div id="members_only_options" class="hide-if-js">
8
+ <br /><strong>Only Accessible By (Defaults to everyone): </strong><br />
9
+
10
+ <?php
11
+ global $wp_roles;
12
+ $roles = $wp_roles->get_names();
13
+ $checked_roles = (array)maybe_unserialize(get_post_meta($post->ID, '_wpac_restricted_to', true));
14
+
15
+ foreach ($roles as $role => $label) {
16
+ if (in_array($role, $checked_roles)) {
17
+ echo '<input type="checkbox" name="wpac_restricted_to[]" checked="checked" value="' . $role . '" /> ' . $label . '<br />';
18
+ } else {
19
+ echo '<input type="checkbox" name="wpac_restricted_to[]" value="' . $role . '" /> ' . $label . '<br />';
20
+ }
21
+ }
22
+
23
+ $redirect_to = get_post_meta($post->ID, '_wpac_members_redirect_to', true);
24
+ ?>
25
+
26
+ <br /><strong>Redirect To (Leave empty for login page): </strong><br />
27
+ <input type="text" name="wpac_members_redirect_to" value="<?php echo $redirect_to; ?>" />
28
+ </div>
29
+ </div>
30
+
31
+ <div id="nonmembers_only_container">
32
+ <input type="checkbox" name="nonmembers_only" id="nonmembers_only" value="true" <?php echo $is_nonmembers_only; ?> />
33
+ <?php _e('Only accessible by non-members?'); ?>
34
+
35
+ <div id="nonmembers_only_options" class="hide-if-js">
36
+ <?php
37
+ $redirect_to = get_post_meta($post->ID, '_wpac_nonmembers_redirect_to', true);
38
+ ?>
39
+
40
+ <br /><strong>Redirect To (Leave empty for home page): </strong><br />
41
+ <input type="text" name="wpac_nonmembers_redirect_to" value="<?php echo $redirect_to; ?>" />
42
+ </div>
43
+ </div>
44
+
45
+ <script>
46
+ //$j = jQuery.noConflict();
47
+
48
+ jQuery('#members_only').change(function() {
49
+ if (jQuery(this).attr('checked') == true) {
50
+ jQuery('#nonmembers_only_container').hide('fast');
51
+ jQuery('#members_only_options').show('fast');
52
+ } else {
53
+ jQuery('#nonmembers_only_container').show('fast');
54
+ jQuery('#members_only_options').hide('fast');
55
+ }
56
+ });
57
+
58
+ jQuery('#nonmembers_only').change(function() {
59
+ if (jQuery(this).attr('checked') == true) {
60
+ jQuery('#members_only_container').hide('fast');
61
+ jQuery('#nonmembers_only_options').show('fast');
62
+ } else {
63
+ jQuery('#members_only_container').show('fast');
64
+ jQuery('#nonmembers_only_options').hide('fast');
65
+ }
66
+ });
67
+
68
+ if (jQuery('#nonmembers_only').attr('checked') == true) {
69
+ jQuery('#members_only_container').hide('fast');
70
+ jQuery('#nonmembers_only_options').show();
71
+ } else if (jQuery('#members_only').attr('checked') == true) {
72
+ jQuery('#nonmembers_only_container').hide('fast');
73
+ jQuery('#members_only_options').show();
74
+ }
75
+ </script>
wordpress-access-control.php CHANGED
@@ -4,16 +4,19 @@
4
  * Plugin URI: http://brandonwamboldt.ca/plugins/members-only-menu-plugin/
5
  * Author: Brandon Wamboldt
6
  * Author URI: http://brandonwamboldt.ca/
7
- * Version: 2.0
8
  * Description: Allows you to limit access to pages to members, non-members or specific roles, having unauthorized users redirected to the login page or a specified URL. Pages can still be added to WordPress navigation menus and will only show up for users with the required access.
9
  */
10
 
11
  add_action( 'wp', array( 'WordPressAccessControl', 'check_for_members_only' ) );
12
  add_action( 'wp', array( 'WordPressAccessControl', 'check_for_nonmembers_only' ) );
 
13
  add_action( 'add_meta_boxes', array( 'WordPressAccessControl', 'add_wp_access_meta_boxes' ) );
14
  add_action( 'save_post', array( 'WordPressAccessControl', 'save_postdata' ) );
 
15
 
16
  add_filter( 'get_pages', array( 'WordPressAccessControl', 'get_pages' ) );
 
17
 
18
  // We check to see if the class exists because it isn't part of pre WordPress 3 systems
19
  if ( class_exists( 'Walker_Nav_Menu' ) ) {
@@ -208,6 +211,33 @@ if ( class_exists( 'Walker_Page' ) ) {
208
  */
209
  class WordPressAccessControl
210
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  function check_conditions( $page_id )
212
  {
213
  global $wp_roles, $current_user;
@@ -249,13 +279,15 @@ class WordPressAccessControl
249
 
250
  function check_for_members_only()
251
  {
 
 
252
  global $post;
253
 
254
- if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) && !self::check_conditions( $post->ID ) ) {
255
  $redirect_to = get_post_meta( $post->ID, '_wpac_members_redirect_to', true );
256
 
257
  if ( empty( $redirect_to ) ) {
258
- header( 'Location: ' . get_bloginfo( 'wpurl' ) . '/wp-login.php' );
259
  } else {
260
  header( 'Location: ' . $redirect_to );
261
  }
@@ -266,9 +298,11 @@ class WordPressAccessControl
266
 
267
  function check_for_nonmembers_only()
268
  {
 
 
269
  global $post;
270
 
271
- if ( get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true ) && ! self::check_conditions( $post->ID ) ) {
272
  $redirect_to = get_post_meta( $post->ID, '_wpac_nonmembers_redirect_to', true );
273
 
274
  if ( empty( $redirect_to ) ) {
4
  * Plugin URI: http://brandonwamboldt.ca/plugins/members-only-menu-plugin/
5
  * Author: Brandon Wamboldt
6
  * Author URI: http://brandonwamboldt.ca/
7
+ * Version: 2.1
8
  * Description: Allows you to limit access to pages to members, non-members or specific roles, having unauthorized users redirected to the login page or a specified URL. Pages can still be added to WordPress navigation menus and will only show up for users with the required access.
9
  */
10
 
11
  add_action( 'wp', array( 'WordPressAccessControl', 'check_for_members_only' ) );
12
  add_action( 'wp', array( 'WordPressAccessControl', 'check_for_nonmembers_only' ) );
13
+ add_action( 'admin_init', array( 'WordPressAccessControl', 'admin_init' ) );
14
  add_action( 'add_meta_boxes', array( 'WordPressAccessControl', 'add_wp_access_meta_boxes' ) );
15
  add_action( 'save_post', array( 'WordPressAccessControl', 'save_postdata' ) );
16
+ add_action( 'manage_pages_custom_column', array( 'WordPressAccessControl', 'show_column' ) );
17
 
18
  add_filter( 'get_pages', array( 'WordPressAccessControl', 'get_pages' ) );
19
+ add_filter( 'manage_edit-page_columns', array( 'WordPressAccessControl', 'add_column' ) );
20
 
21
  // We check to see if the class exists because it isn't part of pre WordPress 3 systems
22
  if ( class_exists( 'Walker_Nav_Menu' ) ) {
211
  */
212
  class WordPressAccessControl
213
  {
214
+ function admin_init()
215
+ {
216
+ wp_enqueue_style( 'wpac-style', plugin_dir_url( __FILE__ ) . 'public/css/wpac.css' );
217
+ }
218
+
219
+ function add_column( $columns )
220
+ {
221
+ $columns['wpac'] = '<img src="' . plugin_dir_url( __FILE__ ) . 'public/images/lock.png" alt="Access" />';
222
+ return $columns;
223
+ }
224
+
225
+ function show_column( $name )
226
+ {
227
+ global $post;
228
+
229
+ switch ( $name ) {
230
+ case 'wpac':
231
+ if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) ) {
232
+ echo '<img src="' . plugin_dir_url( __FILE__ ) . 'public/images/lock.png" alt="Members Only" title="Members Only" />';
233
+ } else if ( get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true ) ) {
234
+ echo '<img src="' . plugin_dir_url( __FILE__ ) . 'public/images/unlock.png" alt="Non-members Only" title="Non-members Only" />';
235
+ }
236
+
237
+ break;
238
+ }
239
+ }
240
+
241
  function check_conditions( $page_id )
242
  {
243
  global $wp_roles, $current_user;
279
 
280
  function check_for_members_only()
281
  {
282
+ if ( is_admin() ) return;
283
+
284
  global $post;
285
 
286
+ if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
287
  $redirect_to = get_post_meta( $post->ID, '_wpac_members_redirect_to', true );
288
 
289
  if ( empty( $redirect_to ) ) {
290
+ header( 'Location: ' . get_bloginfo( 'wpurl' ) . '/wp-login.php?redirect_to=' . $_SERVER['REQUEST_URI'] );
291
  } else {
292
  header( 'Location: ' . $redirect_to );
293
  }
298
 
299
  function check_for_nonmembers_only()
300
  {
301
+ if ( is_admin() ) return;
302
+
303
  global $post;
304
 
305
+ if ( get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
306
  $redirect_to = get_post_meta( $post->ID, '_wpac_nonmembers_redirect_to', true );
307
 
308
  if ( empty( $redirect_to ) ) {