WordPress Access Control - Version 4.0.0

Version Description

  • December 13, 2013 =

  • Rewrote the Nav Walker classes to be more minimalistic and to defer actual functionality to the originally configured walker, allowing for better compatability with themes or plugins that use their own nav walkers

  • Code style updates

  • Fixing some PHP warnings/notices

Download this release

Release Info

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

Code changes from version 3.1.4 to 4.0.0

documentation/assets/css/readme.css CHANGED
File without changes
documentation/assets/images/default_interface.png CHANGED
File without changes
documentation/assets/images/htmlstructure.png CHANGED
File without changes
documentation/assets/images/members_interface.png CHANGED
File without changes
documentation/assets/images/nonmembers_interface.png CHANGED
File without changes
documentation/assets/images/white-grad-active.png CHANGED
File without changes
documentation/assets/images/white-grad.png CHANGED
File without changes
documentation/assets/images/wordpress-logo.png CHANGED
File without changes
documentation/index.html CHANGED
File without changes
languages/wordpress-access-control.pot CHANGED
File without changes
lib/Walker.php ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Walker class for nav menus. We extend Walker_Nav_Menu in case anything type
5
+ * checks the walker class. We take the original walker as a constructor arg and
6
+ * defer method calls to the original walker if the menu item is visible.
7
+ *
8
+ * This new approach should allow us to be compatible with code or themes that
9
+ * implement their own walker classes.
10
+ *
11
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
12
+ */
13
+ class WpacSecureWalker extends Walker_Nav_Menu
14
+ {
15
+ /**
16
+ * @var boolean
17
+ */
18
+ protected $in_private = false;
19
+
20
+ /**
21
+ * @var integer
22
+ */
23
+ protected $private_depth = 0;
24
+
25
+ /**
26
+ * @var Walker_Nav_Menu
27
+ */
28
+ protected $original_walker;
29
+
30
+ /**
31
+ * Constructor.
32
+ *
33
+ * @param Walker_Nav_Menu $original_walker
34
+ */
35
+ public function __construct($original_walker)
36
+ {
37
+ if (empty($original_walker)) {
38
+ $original_walker = new Walker_Nav_Menu;
39
+ }
40
+
41
+ $this->original_walker = $original_walker;
42
+ $this->sync_vars_with_original_walker();
43
+ }
44
+
45
+ /**
46
+ * Sync public variables to the original walker.
47
+ */
48
+ public function sync_vars_with_original_walker()
49
+ {
50
+ $this->original_walker->tree_type = $this->tree_type;
51
+ $this->original_walker->db_fields = $this->db_fields;
52
+ $this->original_walker->max_pages = $this->max_pages;
53
+ }
54
+
55
+ /**
56
+ * {@inheritdoc}
57
+ */
58
+ public function display_element($element, &$children_elements, $max_depth, $depth, $args, &$output)
59
+ {
60
+ $this->sync_vars_with_original_walker();
61
+
62
+ if (WordPressAccessControl::check_conditions($element->object_id) || get_option('wpac_show_in_menus', 'with_access') == 'always') {
63
+ $this->original_walker->display_element($element, $children_elements, $max_depth, $depth, $args, $output);
64
+ } else {
65
+ $children_elements[$element->{$this->db_fields['id']}] = array();
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Divert all other method calls directly to the original walker class.
71
+ *
72
+ * @param string $method
73
+ * @param array $arguments
74
+ * @return mixed
75
+ */
76
+ public function __call($method, $arguments)
77
+ {
78
+ $this->sync_vars_with_original_walker();
79
+ return call_user_func_array(array($this->original_walker, $method), $arguments);
80
+ }
81
+
82
+ /**
83
+ * Divert all property access requests to the original walker class.
84
+ *
85
+ * @param string $var
86
+ * @return mixed
87
+ */
88
+ public function __get($var)
89
+ {
90
+ return $this->original_walker->{$var};
91
+ }
92
+
93
+ /**
94
+ * Divert all property access requests to the original walker class.
95
+ *
96
+ * @param string $var
97
+ * @return mixed
98
+ */
99
+ public function __set($var, $value)
100
+ {
101
+ $this->original_walker->{$var} = $value;
102
+ }
103
+
104
+ /**
105
+ * Divert all property access requests to the original walker class.
106
+ *
107
+ * @param string $var
108
+ * @return mixed
109
+ */
110
+ public function __isset($var)
111
+ {
112
+ return isset($this->original_walker->{$var});
113
+ }
114
+ }
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: brandon.wamboldt
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=Y398QNA6FM9TA
4
  Tags: members, only, plugin, restricted, access, menus, 3.3, wp_nav_menu, nonmembers
5
- Requires at least: 2.9
6
- Tested up to: 3.3
7
- Stable tag: 3.1.4
8
 
9
  Restrict pages, posts, custom post types, menus and widgets to members, nonmembers or specific roles and still add to navigation
10
 
@@ -20,6 +20,10 @@ By default, you can add Members only pages to your menus, and users who cannot
20
 
21
  Additionally, you have the ability to customize search pages, completely hiding posts/pages from search results if a user can't access them, showing search results without an excerpt, or showing search results normally.
22
 
 
 
 
 
23
  **New Features in 3.1**
24
 
25
  * You can add member only versions of each menu on your site (Only when using WordPress menus)
@@ -60,9 +64,15 @@ Yes, this is a new feature in 3.1.3. Use the syntax [members role="administrator
60
 
61
  == Changelog ==
62
 
 
 
 
 
 
 
63
  = 3.1.4 - August 30, 2012 =
64
 
65
- * Added not/or keywords to the role option of the members shortcode, syntax: [members role="not:guest,banned"][/members] and [members role="or:editor,author,contributor"] (Thanks to Dave Amphlett for the idea)
66
 
67
  = 3.1.3 - August 30, 2012 =
68
 
2
  Contributors: brandon.wamboldt
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=Y398QNA6FM9TA
4
  Tags: members, only, plugin, restricted, access, menus, 3.3, wp_nav_menu, nonmembers
5
+ Requires at least: 3.0
6
+ Tested up to: 3.8
7
+ Stable tag: 4.0.0
8
 
9
  Restrict pages, posts, custom post types, menus and widgets to members, nonmembers or specific roles and still add to navigation
10
 
20
 
21
  Additionally, you have the ability to customize search pages, completely hiding posts/pages from search results if a user can't access them, showing search results without an excerpt, or showing search results normally.
22
 
23
+ **New Features in 4.0**
24
+
25
+ Enhanced theme compatiblity! We now have a much smaller impact on sites due to a new way of implementing our nav walker code. This should significantly improve compatability with third party themes.
26
+
27
  **New Features in 3.1**
28
 
29
  * You can add member only versions of each menu on your site (Only when using WordPress menus)
64
 
65
  == Changelog ==
66
 
67
+ = 4.0.0 - December 13, 2013 =
68
+
69
+ * Rewrote the Nav Walker classes to be more minimalistic and to defer actual functionality to the originally configured walker, allowing for better compatability with themes or plugins that use their own nav walkers
70
+ * Code style updates
71
+ * Fixing some PHP warnings/notices
72
+
73
  = 3.1.4 - August 30, 2012 =
74
 
75
+ * Added not/or keywords to the role option of the members shortcode, syntax: [members role="not:guest,banned"][/members] and [members role="or:editor,author,contributor"](Thanks to Dave Amphlett for the idea)
76
 
77
  = 3.1.3 - August 30, 2012 =
78
 
templates/options.php CHANGED
File without changes
wordpress-access-control.php CHANGED
@@ -4,836 +4,652 @@
4
  * Plugin URI: http://brandonwamboldt.ca/plugins/members-only-menu-plugin/
5
  * Author: Brandon Wamboldt
6
  * Author URI: http://brandonwamboldt.ca/
7
- * Version: 3.1.4
8
  * Description: This plugin is a powerful tool which gives you fine grained control over your pages and posts (and custom post types), allowing you to restrict a page, post, or custom post type to members, non-members, or even specific roles. You can customize how these pages and posts show up in search results, where users are directed when they visit them, and much more. <strong>You can even make your entire blog members only!</strong>.
9
  */
10
 
11
- add_action( 'after_setup_theme', array( 'WordPressAccessControl', 'load_custom_widgets' ) );
12
- add_action( 'wp', array( 'WordPressAccessControl', 'check_for_members_only' ) );
13
- add_action( 'wp', array( 'WordPressAccessControl', 'check_for_nonmembers_only' ) );
14
- add_action( 'init', array( 'WordPressAccessControl', 'add_wpac_nav_menus' ) );
15
- add_action( 'admin_init', array( 'WordPressAccessControl', 'admin_init' ) );
16
- add_action( 'admin_menu', array( 'WordPressAccessControl', 'admin_menu' ) );
17
- add_action( 'add_meta_boxes', array( 'WordPressAccessControl', 'add_wp_access_meta_boxes' ) );
18
- add_action( 'save_post', array( 'WordPressAccessControl', 'save_postdata' ) );
19
- add_action( 'manage_pages_custom_column', array( 'WordPressAccessControl', 'show_column' ) );
20
-
21
- add_filter( 'get_pages', array( 'WordPressAccessControl', 'get_pages' ) );
22
- add_filter( 'manage_edit-page_columns', array( 'WordPressAccessControl', 'add_column' ) );
23
- add_filter( 'the_excerpt', array( 'WordPressAccessControl', 'remove_excerpt' ) );
24
- add_filter( 'the_content', array( 'WordPressAccessControl', 'remove_excerpt' ) );
25
- add_filter( 'plugin_row_meta', array( 'WordPressAccessControl', 'plugin_row_meta' ), 10, 5 );
26
- add_filter( 'posts_join_paged', array( 'WordPressAccessControl', 'posts_join_paged' ), 10, 2 );
27
- add_filter( 'posts_where_paged', array( 'WordPressAccessControl', 'posts_where_paged' ), 10, 2 );
28
- add_filter( 'wp_nav_menu_args', array( 'WordPressAccessControl', 'wp_nav_menu_members_only' ) );
29
-
30
- add_shortcode( 'member', array( 'WordPressAccessControl', 'shortcode_members' ) );
31
- add_shortcode( 'members', array( 'WordPressAccessControl', 'shortcode_members' ) );
32
- add_shortcode( 'non-members', array( 'WordPressAccessControl', 'shortcode_nonmembers' ) );
33
- add_shortcode( 'nonmembers', array( 'WordPressAccessControl', 'shortcode_nonmembers' ) );
34
- add_shortcode( 'non-member', array( 'WordPressAccessControl', 'shortcode_nonmembers' ) );
35
- add_shortcode( 'nonmember', array( 'WordPressAccessControl', 'shortcode_nonmembers' ) );
36
-
37
- // We check to see if the class exists because it isn't part of pre WordPress 3 systems
38
- if ( class_exists( 'Walker_Nav_Menu' ) ) {
39
- add_filter( 'wp_nav_menu_args', array( 'WordPressAccessControl', 'wp_nav_menu_args' ) );
40
-
41
- /**
42
- * A custom walker that checks our conditions to make sure the user has
43
- * permission to view the page linked to from the menu item
44
- *
45
- * @author Brandon Wamboldt
46
- * @package WordPress
47
- * @subpackage WordPressAccessControl
48
- */
49
- class WPAC_Nav_Menu_Walker extends Walker_Nav_Menu
50
- {
51
- var $in_private = false;
52
- var $private_lvl = 0;
53
-
54
- function start_lvl( & $output, $depth )
55
- {
56
- if ( ! $this->in_private ) {
57
- $indent = str_repeat( ' ', $depth );
58
- $output .= "\n$indent<ul class=\"sub-menu\"><li style='display:none;'></li>\n";
59
- }
60
- }
61
-
62
- function end_lvl( & $output, $depth )
63
- {
64
- if ( ! $this->in_private ) {
65
- $indent = str_repeat( ' ', $depth );
66
- $output .= "$indent</ul>\n";
67
- }
68
- }
69
-
70
- function start_el( & $output, $item, $depth, $args )
71
- {
72
- global $wp_query;
73
-
74
- if ( WordPressAccessControl::check_conditions( $item->object_id ) || get_option( 'wpac_show_in_menus', 'with_access' ) == 'always' ) {
75
- if ( ! $this->in_private ) {
76
- $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
77
-
78
- $class_names = $value = '';
79
-
80
- $classes = empty( $item->classes ) ? array() : (array) $item->classes;
81
-
82
- $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
83
- $class_names = ' class="' . esc_attr( $class_names ) . '"';
84
-
85
- $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
86
-
87
- $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
88
- $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
89
- $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
90
- $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
91
-
92
- $item_output = $args->before;
93
- $item_output .= '<a'. $attributes .'>';
94
- $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
95
- $item_output .= '</a>';
96
- $item_output .= $args->after;
97
-
98
- $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
99
- }
100
- } else {
101
- $this->in_private = true;
102
- $this->private_lvl++;
103
- }
104
- }
105
-
106
- function end_el( & $output, $item, $depth )
107
- {
108
- if ( WordPressAccessControl::check_conditions( $item->object_id ) || get_option( 'wpac_show_in_menus', 'with_access' ) == 'always' ) {
109
- if ( ! $this->in_private ) {
110
- $output .= "</li>\n";
111
- }
112
- } else {
113
- $this->private_lvl--;
114
-
115
- if ( $this->private_lvl == 0 ) {
116
- $this->in_private = false;
117
- }
118
- }
119
- }
120
- }
121
- }
122
-
123
- // We check to see if the class exists because it may get removed in the future
124
- if ( class_exists( 'Walker_Page' ) ) {
125
- add_filter( 'wp_page_menu_args', array( 'WordPressAccessControl' , 'wp_page_menu_args' ) );
126
-
127
- /**
128
- * A custom walker that checks our conditions to make sure the user has
129
- * permission to view the page linked to from the menu item
130
- *
131
- * @author Brandon Wamboldt
132
- * @package WordPress
133
- * @subpackage WordPressAccessControl
134
- */
135
- class WPAC_Page_Walker extends Walker_Page
136
- {
137
- var $in_private = false;
138
- var $private_lvl = 0;
139
-
140
- function start_lvl( & $output, $depth )
141
- {
142
- if ( ! $this->in_private ) {
143
- $indent = str_repeat( ' ', $depth );
144
- $output .= "\n$indent<ul class=\"sub-menu\"><li style='display:none;'></li>\n";
145
- }
146
- }
147
-
148
- function end_lvl( & $output, $depth )
149
- {
150
- if ( ! $this->in_private ) {
151
- $indent = str_repeat( ' ', $depth );
152
- $output .= "$indent</ul>\n";
153
- }
154
- }
155
-
156
- function start_el( & $output, $page, $depth, $args, $current_page )
157
- {
158
- global $wp_query;
159
-
160
- if ( WordPressAccessControl::check_conditions( $page->ID ) ) {
161
- if ( ! $this->in_private ) {
162
- $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
163
-
164
- extract( $args, EXTR_SKIP );
165
-
166
- $css_class = array( 'page_item', 'page-item-' . $page->ID );
167
-
168
- if ( $current_page != '' ) {
169
- $_current_page = get_page( $current_page );
170
-
171
- if ( isset( $_current_page->ancestors ) && in_array( $page->ID, (array) $_current_page->ancestors ) ) {
172
- $css_class[] = 'current_page_ancestor';
173
- }
174
-
175
- if ( $page->ID == $current_page ) {
176
- $css_class[] = 'current_page_item';
177
- } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
178
- $css_class[] = 'current_page_parent';
179
- }
180
- } elseif ( $page->ID == get_option('page_for_posts') ) {
181
- $css_class[] = 'current_page_parent';
182
- }
183
-
184
- $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page ) );
185
-
186
- $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link( $page->ID ) . '" title="' . esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $page->post_title, $page->ID ) ) ) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';
187
-
188
- if ( ! empty( $show_date ) ) {
189
- if ( 'modified' == $show_date ) {
190
- $time = $page->post_modified;
191
- } else {
192
- $time = $page->post_date;
193
- }
194
-
195
- $output .= ' ' . mysql2date( $date_format, $time );
196
- }
197
- }
198
- } else {
199
- $this->in_private = true;
200
- $this->private_lvl++;
201
- }
202
- }
203
-
204
- function end_el( & $output, $page, $depth )
205
- {
206
- if ( WordPressAccessControl::check_conditions( $page->ID ) ) {
207
- if ( ! $this->in_private ) {
208
- $output .= "</li>\n";
209
- }
210
- } else {
211
- $this->private_lvl--;
212
-
213
- if ( $this->private_lvl == 0 ) {
214
- $this->in_private = false;
215
- }
216
- }
217
- }
218
- }
219
- }
220
 
221
  /**
222
  * The main plugin
223
  *
224
- * @author Brandon Wamboldt
225
- * @package WordPress
226
- * @subpackage WordPressAccessControl
227
  */
228
  class WordPressAccessControl
229
  {
230
- function admin_init()
231
- {
232
- wp_enqueue_style( 'wpac-style', plugin_dir_url( __FILE__ ) . 'public/css/wpac.css' );
233
-
234
- // Load translation files
235
- if ( ! load_plugin_textdomain( 'wpac' ) ) {
236
- load_plugin_textdomain( 'wpac', false, 'wordpress-access-control/languages/' );
237
- }
238
- }
239
-
240
- function admin_menu()
241
- {
242
- add_options_page( 'WordPress Access Control Settings', 'Members Only', 'manage_options', 'wpac-options', array( 'WordPressAccessControl', 'options_page' ) );
243
- }
244
-
245
- function options_page()
246
- {
247
- // Save options?
248
- if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'update' ) {
249
- if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'wpac_options_save' ) ) {
250
- $admin_error = __( 'Security check failed, please try again', 'wpac' );
251
- } else {
252
-
253
- // Make the blog members only
254
- if ( isset( $_REQUEST['wpac_members_only_blog'] ) && $_REQUEST['wpac_members_only_blog'] == 'yes' ) {
255
- update_option( 'wpac_members_only_blog', true );
256
- } else {
257
- update_option( 'wpac_members_only_blog', false );
258
- }
259
-
260
- // Members blog redirect link
261
- if ( isset( $_REQUEST['wpac_members_blog_redirect'] ) ) {
262
- update_option( 'wpac_members_blog_redirect', esc_url( $_REQUEST['wpac_members_blog_redirect'] ) );
263
- } else {
264
- delete_option( 'wpac_members_blog_redirect' );
265
- }
266
-
267
- // Support custom post types
268
- if ( isset( $_REQUEST['wpac_custom_post_types'] ) && is_array( $_REQUEST['wpac_custom_post_types'] ) ) {
269
- update_option( 'wpac_custom_post_types', $_REQUEST['wpac_custom_post_types'] );
270
- } else {
271
- delete_option( 'wpac_custom_post_types' );
272
- }
273
-
274
- // Override per-page permissions
275
- if ( isset( $_REQUEST['wpac_always_accessible_by'] ) && is_array( $_REQUEST['wpac_always_accessible_by'] ) ) {
276
- $wpac_always_accessible_by = array();
277
-
278
- foreach ( $_REQUEST['wpac_always_accessible_by'] as $role ) {
279
- $wpac_always_accessible_by[] = $role;
280
- }
281
-
282
- update_option( 'wpac_always_accessible_by', $wpac_always_accessible_by );
283
- } else {
284
- update_option( 'wpac_always_accessible_by', array() );
285
- }
286
-
287
- // Show in the menu settings
288
- if ( isset( $_REQUEST['wpac_show_in_menus'] ) && $_REQUEST['wpac_show_in_menus'] == 'always' ) {
289
- update_option( 'wpac_show_in_menus', 'always' );
290
- } else {
291
- update_option( 'wpac_show_in_menus', 'with_access' );
292
- }
293
-
294
- // Default post state setting
295
- if ( isset( $_REQUEST['default_post_state'] ) && in_array( $_REQUEST['default_post_state'], array( 'public', 'members', 'nonmembers' ) ) ) {
296
- update_option( 'wpac_default_post_state', $_REQUEST['default_post_state'] );
297
- } else {
298
- update_option( 'wpac_default_post_state', 'public' );
299
- }
300
-
301
- // Default post roles
302
- if ( isset( $_REQUEST['wpac_posts_default_restricted_to'] ) && is_array( $_REQUEST['wpac_posts_default_restricted_to'] ) ) {
303
- update_option( 'wpac_posts_default_restricted_to', $_REQUEST['wpac_posts_default_restricted_to'] );
304
- } else {
305
- update_option( 'wpac_posts_default_restricted_to', array() );
306
- }
307
-
308
- // Default page state setting
309
- if ( isset( $_REQUEST['default_page_state'] ) && in_array( $_REQUEST['default_page_state'], array( 'public', 'members', 'nonmembers' ) ) ) {
310
- update_option( 'wpac_default_page_state', $_REQUEST['default_page_state'] );
311
- } else {
312
- update_option( 'wpac_default_page_state', 'public' );
313
- }
314
-
315
- // Default page roles
316
- if ( isset( $_REQUEST['wpac_pages_default_restricted_to'] ) && is_array( $_REQUEST['wpac_pages_default_restricted_to'] ) ) {
317
- update_option( 'wpac_pages_default_restricted_to', $_REQUEST['wpac_pages_default_restricted_to'] );
318
- } else {
319
- update_option( 'wpac_pages_default_restricted_to', array() );
320
- }
321
-
322
- // Default redirect
323
- if ( isset( $_REQUEST['wpac_default_members_redirect'] ) && ! empty( $_REQUEST['wpac_default_members_redirect'] ) ) {
324
- update_option( 'wpac_default_members_redirect', $_REQUEST['wpac_default_members_redirect'] );
325
- } else {
326
- update_option( 'wpac_default_members_redirect', '' );
327
- }
328
-
329
- // Search Options
330
- if ( isset( $_REQUEST['show_posts_in_search'] ) && $_REQUEST['show_posts_in_search'] == 'yes' ) {
331
- update_option( 'wpac_show_posts_in_search', true );
332
- } else {
333
- update_option( 'wpac_show_posts_in_search', false );
334
- }
335
-
336
- if ( isset( $_REQUEST['show_post_excerpts_in_search'] ) && $_REQUEST['show_post_excerpts_in_search'] == 'yes' ) {
337
- update_option( 'wpac_show_post_excerpts_in_search', true );
338
- } else {
339
- update_option( 'wpac_show_post_excerpts_in_search', false );
340
- }
341
-
342
- if ( isset( $_REQUEST['show_pages_in_search'] ) && $_REQUEST['show_pages_in_search'] == 'yes' ) {
343
- update_option( 'wpac_show_pages_in_search', true );
344
- } else {
345
- update_option( 'wpac_show_pages_in_search', false );
346
- }
347
-
348
- if ( isset( $_REQUEST['show_page_excerpts_in_search'] ) && $_REQUEST['show_page_excerpts_in_search'] == 'yes' ) {
349
- update_option( 'wpac_show_page_excerpts_in_search', true );
350
- } else {
351
- update_option( 'wpac_show_page_excerpts_in_search', false );
352
- }
353
-
354
- // Custom excerpts
355
- if ( isset( $_REQUEST['page_excerpt_text'] ) ) {
356
- update_option( 'wpac_page_excerpt_text', $_REQUEST['page_excerpt_text'] );
357
- }
358
-
359
- if ( isset( $_REQUEST['post_excerpt_text'] ) ) {
360
- update_option( 'wpac_post_excerpt_text', $_REQUEST['post_excerpt_text'] );
361
- }
362
-
363
- $admin_message = '<strong>' . __( 'Settings Saved.') . '</strong>';
364
- }
365
- }
366
-
367
- include( dirname( __FILE__ ) . '/templates/options.php' );
368
- }
369
-
370
- function add_column( $columns )
371
- {
372
- $columns['wpac'] = '<img src="' . plugin_dir_url( __FILE__ ) . 'public/images/lock.png" alt="Access" />';
373
- return $columns;
374
- }
375
-
376
- function show_column( $name )
377
- {
378
- global $post;
379
-
380
- switch ( $name ) {
381
- case 'wpac':
382
- if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) ) {
383
- echo '<img src="' . plugin_dir_url( __FILE__ ) . 'public/images/lock.png" alt="Members Only" title="Members Only" />';
384
- } else if ( get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true ) ) {
385
- echo '<img src="' . plugin_dir_url( __FILE__ ) . 'public/images/unlock.png" alt="Non-members Only" title="Non-members Only" />';
386
- }
387
-
388
- break;
389
- }
390
- }
391
-
392
- function check_conditions( $page_id )
393
- {
394
- global $wp_roles, $current_user;
395
-
396
- if ( is_user_logged_in() ) {
397
- get_currentuserinfo();
398
-
399
- $allowed_roles = maybe_unserialize( get_post_meta( $page_id, '_wpac_restricted_to', true ) );
400
- $override_roles = maybe_unserialize( get_option( 'wpac_always_accessible_by', array( 0 => 'administrator' ) ) );
401
-
402
- if ( empty( $allowed_roles ) ) {
403
- $allowed_roles_tmp = $wp_roles->get_names();
404
- $allowed_roles = array();
405
-
406
- foreach ( $allowed_roles_tmp as $role => $garbage ) {
407
- $allowed_roles[] = $role;
408
- }
409
- }
410
-
411
- $intersections = array_intersect( $current_user->roles, $allowed_roles );
412
- $override_intersections = array_intersect( $current_user->roles, $override_roles );
413
-
414
- if ( empty( $intersections ) && empty( $override_intersections ) ) {
415
- $role = false;
416
- } else {
417
- $role = true;
418
- }
419
- } else {
420
- $role = true;
421
- }
422
-
423
- $is_members_only = get_post_meta( $page_id, '_wpac_is_members_only', true );
424
- $is_nonmembers_only = get_post_meta( $page_id, '_wpac_is_nonmembers_only', true );
425
-
426
- if ( ( is_user_logged_in() && $is_members_only && $role ) || ( ! is_user_logged_in() && $is_nonmembers_only ) || ( ! $is_members_only && ! $is_nonmembers_only ) ) {
427
- return true;
428
- } else {
429
- return false;
430
- }
431
- }
432
-
433
- function check_for_members_only()
434
- {
435
- global $post;
436
-
437
- if ( is_admin() ) return;
438
-
439
- // Check for a members only blog
440
- $blog_is_members_only = get_option( 'wpac_members_only_blog', false );
441
-
442
- if ( $blog_is_members_only && ! is_user_logged_in() ) {
443
- $redirect_to = get_option( 'wpac_members_blog_redirect', wp_login_url( $_SERVER['REQUEST_URI'] ) );
444
-
445
- if ( empty( $redirect_to ) ) {
446
- $redirect_to = wp_login_url( $_SERVER['REQUEST_URI'] );
447
- }
448
-
449
- header( 'Location: ' . add_query_arg( 'redirect_to', $_SERVER['REQUEST_URI'], $redirect_to ) );
450
- exit();
451
- }
452
-
453
- if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
454
- if ( is_singular() ) {
455
- $redirect_to = get_post_meta( $post->ID, '_wpac_members_redirect_to', true );
456
-
457
- if ( empty( $redirect_to ) ) {
458
- header( 'Location: ' . get_bloginfo( 'wpurl' ) . '/wp-login.php?redirect_to=' . $_SERVER['REQUEST_URI'] );
459
- } else {
460
- header( 'Location: ' . add_query_arg( 'redirect_to', $_SERVER['REQUEST_URI'], $redirect_to ) );
461
- }
462
-
463
- exit();
464
- }
465
- }
466
- }
467
-
468
- function check_for_nonmembers_only()
469
- {
470
- if ( is_admin() ) return;
471
-
472
- global $post;
473
-
474
- if ( get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
475
- $redirect_to = get_post_meta( $post->ID, '_wpac_nonmembers_redirect_to', true );
476
-
477
- if ( empty( $redirect_to ) ) {
478
- header( 'Location: ' . get_bloginfo( 'wpurl' ) . '/index.php' );
479
- } else {
480
- header( 'Location: ' . $redirect_to );
481
- }
482
-
483
- exit();
484
- }
485
- }
486
-
487
- function remove_excerpt( $excerpt )
488
- {
489
- if ( is_admin() || is_single() ) return $excerpt;
490
-
491
- global $post;
492
-
493
- if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
494
- $show_excerpt = get_post_meta( $post->ID, '_wpac_show_excerpt_in_search', true );
495
-
496
- if ( ! is_singular() && $show_excerpt != 'yes' ) {
497
- if ( $post->post_type == 'post' ) {
498
- $excerpt = get_option( 'wpac_post_excerpt_text', __( 'To view the contents of this post, you must be authenticated and have the required access level.', 'wpac' ) );
499
- } else {
500
- $excerpt = get_option( 'wpac_page_excerpt_text', __( 'To view the contents of this page, you must be authenticated and have the required access level.', 'wpac' ) );
501
- }
502
-
503
- return wpautop( $excerpt );
504
- } else {
505
- return $excerpt;
506
- }
507
- }
508
-
509
- return $excerpt;
510
- }
511
-
512
- function posts_join_paged( $join, $query )
513
- {
514
- global $wpdb;
515
-
516
- if ( is_admin() ) return $join;
517
-
518
- if ( ! is_singular() && ! is_admin() ) {
519
- $join .= " LEFT JOIN {$wpdb->postmeta} PMWPAC3 ON PMWPAC3.post_id = {$wpdb->posts}.ID AND PMWPAC3.meta_key = '_wpac_show_in_search' LEFT JOIN {$wpdb->postmeta} PMWPAC ON PMWPAC.post_id = {$wpdb->posts}.ID AND PMWPAC.meta_key = '_wpac_is_members_only' LEFT JOIN {$wpdb->postmeta} PMWPAC2 ON PMWPAC2.post_id = {$wpdb->posts}.ID AND PMWPAC2.meta_key = '_wpac_is_nonmembers_only' ";
520
- }
521
-
522
- return $join;
523
- }
524
-
525
- function posts_where_paged( $where, $query )
526
- {
527
- global $wpdb;
528
-
529
- if ( is_admin() ) return $where;
530
-
531
- if ( ! is_singular() && ! is_admin() ) {
532
- if ( ! is_user_logged_in() ) {
533
- $where .= " AND ( PMWPAC.meta_value IS NULL OR PMWPAC3.meta_value = '1' OR PMWPAC.meta_value != 'true' ) ";
534
- } else {
535
- $where .= " AND ( PMWPAC2.meta_value IS NULL OR PMWPAC2.meta_value != 'true' ) ";
536
- }
537
- }
538
-
539
- return $where;
540
- }
541
-
542
- function remove_restricted_posts( $posts, $query = false )
543
- {
544
- return $posts;
545
- echo $query->request;exit();
546
-
547
- if ( ! is_singular() && ! is_admin() ) {
548
- $new_posts = array();
549
-
550
- foreach ( $posts as $post ) {
551
- // The page is members only and we do NOT have access to it
552
- if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
553
- if ( get_post_meta( $post->ID, '_wpac_show_in_search', true ) != 1 ) {
554
- continue;
555
- }
556
- }
557
-
558
- // The page is non-members only and we do NOT have access to it
559
- if ( get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
560
- if ( get_post_meta( $post->ID, '_wpac_show_in_search', true ) != 1 ) {
561
- continue;
562
- }
563
- }
564
-
565
- // Remove results where the search term is in a [member] or [nonmember] tag and we don't have access
566
- $st = get_search_query();
567
- $content = do_shortcode( $post->post_content );
568
-
569
-
570
- if ( ! empty( $st ) && is_search() && ! stristr( $content, $st ) ) {
571
- continue;
572
- }
573
-
574
- $new_posts[] = $post;
575
- }
576
-
577
- $query->found_posts = count( $new_posts );
578
- $query->max_num_pages = ceil( count( $new_posts ) / $query->query_vars['posts_per_page'] );
579
- $query->posts = $new_posts;
580
- $query->post_count = count( $new_posts );
581
-
582
- return $new_posts;
583
- }
584
-
585
- return $posts;
586
- }
587
-
588
- /**
589
- * Add our custom meta box to all support post types
590
- *
591
- * @since WordPress Access Control 2.0
592
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
593
- */
594
- function add_wp_access_meta_boxes()
595
- {
596
- $enabled_post_types = get_option( 'wpac_custom_post_types', array() );
597
- $supported_pages = apply_filters( 'wp_access_supported_pages', array_merge( array( 'page', 'post' ), $enabled_post_types ) );
598
-
599
- foreach ( $supported_pages as $page_type ) {
600
- add_meta_box( 'wpac_controls_meta', 'WordPress Access Control', array( 'WordPressAccessControl', 'add_wpac_meta_box' ), $page_type, 'side', 'high' );
601
- }
602
- }
603
-
604
- /**
605
- * Display the various per-page/per-post options in the sidebar
606
- *
607
- * @since WordPress Access Control 2.0
608
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
609
- */
610
- function add_wpac_meta_box()
611
- {
612
- global $post;
613
-
614
- $is_members_only = get_post_meta( $post->ID, '_wpac_is_members_only', true );
615
- $is_nonmembers_only = get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true );
616
-
617
- if ( $post->post_type == 'post' ) {
618
- $wpac_default_post_state = get_option( 'wpac_default_post_state', 'public' );
619
- } else {
620
- $wpac_default_post_state = get_option( 'wpac_default_page_state', 'public' );
621
- }
622
-
623
- if ( $is_members_only == 'true' || ( $post->post_status == 'auto-draft' && $wpac_default_post_state == 'members' ) ) {
624
- $is_members_only = 'checked="checked"';
625
- }
626
-
627
- if ( $is_nonmembers_only == 'true' || ( $post->post_status == 'auto-draft' && $wpac_default_post_state == 'nonmembers' ) ) {
628
- $is_nonmembers_only = 'checked="checked"';
629
- }
630
-
631
- $wpac_show_in_search = get_post_meta( $post->ID, '_wpac_show_in_search', true );
632
- $wpac_show_excerpt_in_search = get_post_meta( $post->ID, '_wpac_show_excerpt_in_search', true );
633
- $pass_to_children = get_post_meta( $post->ID, '_wpac_pass_to_children', true );
634
-
635
- include( dirname( __FILE__ ) . '/templates/meta_box.php' );
636
- }
637
-
638
- /**
639
- * Saves our custom meta data for the post being saved
640
- *
641
- * @since WordPress Access Control 2.0
642
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
643
- */
644
- function save_postdata( $post_id )
645
- {
646
- // Security check
647
- if ( ! isset( $_POST['members_only_nonce'] ) ) {
648
- return $post_id;
649
- }
650
-
651
- if ( ! wp_verify_nonce( $_POST['members_only_nonce'], 'members_only' ) ) {
652
- return $post_id;
653
- }
654
-
655
- // Meta data isn't transmitted during autosaves so don't do anything
656
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
657
- return $post_id;
658
- }
659
-
660
- if ( isset( $_POST['members_only'] ) && $_POST['members_only'] == 'true' ) {
661
- update_post_meta( $post_id, '_wpac_is_members_only', 'true' );
662
-
663
- if ( isset( $_POST['wpac_restricted_to'] ) && ! empty( $_POST['wpac_restricted_to'] ) ) {
664
- update_post_meta( $post_id, '_wpac_restricted_to', serialize( $_POST['wpac_restricted_to'] ) );
665
- } else {
666
- delete_post_meta( $post_id, '_wpac_restricted_to' );
667
- }
668
- } else {
669
- delete_post_meta( $post_id, '_wpac_is_members_only' );
670
- }
671
-
672
- if ( isset( $_POST['nonmembers_only'] ) && $_POST['nonmembers_only'] == 'true' ) {
673
- update_post_meta( $post_id, '_wpac_is_nonmembers_only', 'true' );
674
- } else {
675
- delete_post_meta( $post_id, '_wpac_is_nonmembers_only' );
676
- }
677
-
678
- if ( isset( $_POST['wpac_members_redirect_to'] ) ) {
679
- update_post_meta( $post_id, '_wpac_members_redirect_to', $_POST['wpac_members_redirect_to'] );
680
- }
681
-
682
- if ( isset( $_POST['wpac_show_in_search'] ) ) {
683
- update_post_meta( $post_id, '_wpac_show_in_search', 1 );
684
- } else {
685
- update_post_meta( $post_id, '_wpac_show_in_search', 0 );
686
- }
687
-
688
- if ( isset( $_POST['wpac_show_excerpt_in_search'] ) ) {
689
- update_post_meta( $post_id, '_wpac_show_excerpt_in_search', 1 );
690
- } else {
691
- update_post_meta( $post_id, '_wpac_show_excerpt_in_search', 0 );
692
- }
693
-
694
- if ( isset( $_POST['wpac_nonmembers_redirect_to'] ) ) {
695
- update_post_meta( $post_id, '_wpac_nonmembers_redirect_to', $_POST['wpac_nonmembers_redirect_to'] );
696
- }
697
-
698
- if ( isset( $_POST['pass_to_children'] ) && $_POST['pass_to_children'] == 'true' ) {
699
- update_post_meta( $post_id, '_wpac_pass_to_children', 'true' );
700
-
701
- $original_id = $post_id;
702
- $is_revision = wp_is_post_revision( $original_id );
703
-
704
- if ( $is_revision ) {
705
- $original_id = $is_revision;
706
- }
707
-
708
- $children = get_pages( array( 'child_of' => $original_id ) );
709
-
710
- foreach ( $children as $child ) {
711
- WordPressAccessControl::save_postdata( $child->ID );
712
- }
713
- } else {
714
- delete_post_meta( $post_id, '_wpac_pass_to_children' );
715
- }
716
-
717
- return $post_id;
718
- }
719
-
720
- /**
721
- * Replaces the default WordPress nav menu walker with our own version
722
- *
723
- * @param array $args The arguments passed to the wp_nav_menu function
724
- *
725
- * @return array The original arguments, with our custom walker instead of the default
726
- *
727
- * @since WordPress Access Control 1.1
728
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
729
- */
730
- function wp_nav_menu_args( $args )
731
- {
732
- $args['walker'] = new WPAC_Nav_Menu_Walker();
733
- return $args;
734
- }
735
-
736
- /**
737
- * Prevents the code from degrading terribly when no nav menu is available
738
- *
739
- * @param array $args The arguments passed to the wp_nav_menu function
740
- *
741
- * @return array The original arguments, possible with our custom walker instead of the default
742
- *
743
- * @since WordPress Access Control 1.2
744
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
745
- */
746
- function wp_page_menu_args( $args )
747
- {
748
- // Only remove the walker if it is ours
749
- if ( isset( $args['walker'] ) && get_class( $args['walker']) == 'WPAC_Nav_Menu_Walker' ) {
750
- $args['walker'] = new WPAC_Page_Walker();
751
- }
752
-
753
- return $args;
754
- }
755
-
756
- /**
757
- * This hooks in at a higher level to make sure functions like wp_list_pages
758
- * won't return members only pages.
759
- *
760
- * @param array $pages
761
- * @return array
762
- *
763
- * @since WordPress Access Control 1.6.4
764
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
765
- */
766
- function get_pages( $pages )
767
- {
768
- // Don't affect the display of pages when viewing the list in the admin
769
- if ( is_admin() ) {
770
- return $pages;
771
- }
772
-
773
- // Whether or not the user is logged in
774
- $auth = is_user_logged_in();
775
-
776
- // Go through each page, remove ones we can't see
777
- foreach ( $pages as $key => $page ) {
778
- $is_members_only = get_post_meta( $page->ID, '_wpac_is_members_only', true );
779
- $is_nonmembers_only = get_post_meta( $page->ID, '_wpac_is_nonmembers_only', true );
780
-
781
- if ( $is_members_only && ! $auth ) {
782
- unset( $pages[$key] );
783
- }
784
-
785
- if ( $is_nonmembers_only && $auth ) {
786
- unset( $pages[$key] );
787
- }
788
- }
789
-
790
- return $pages;
791
- }
792
-
793
- /**
794
- * Adds links to the plugin row to view the options page or view the plugin
795
- * documentation.
796
- *
797
- * @param array $plugin_meta The links to display in the plugin row
798
- * @param string $plugin_file The directory name and filename of the plugin (wordpress-access-control/wordpress-access-control.php)
799
- * @param array $plugin_data
800
- * @param string $status The status of the plugin (active or inactive)
801
- *
802
- * @return array The $plugin_meta array of links for this plugin
803
- *
804
- * @uses plugin_dir_url() to get the URL of the plugin directory since that is where the documentation is
805
- *
806
- * @since WordPress Access Control 3.0
807
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
808
- */
809
- function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status )
810
- {
811
- if ( $plugin_file == str_replace( '.php', '/', basename( __FILE__ ) ) . basename( __FILE__ ) ) {
812
- $plugin_meta[] = '<a href="options-general.php?page=wpac-options">' . __( 'Visit options page', 'wpac' ) . '</a>';
813
- $plugin_meta[] = '<a href="' . plugin_dir_url( __FILE__ ) . 'documentation/index.html">' . __( 'Plugin Documentation', 'wpac' ) . '</a>';
814
- }
815
-
816
- return $plugin_meta;
817
- }
818
-
819
-
820
- /**
821
- * Hides content for users that are not logged in and displays content for
822
- * users who are logged in. Calls do_shortcode on the content to allow
823
- * nested shortcodes.
824
- *
825
- * @uses is_user_logged_in() to determine if the user is logged in or not
826
- * @uses wpautop() to format the text in the shortcodes
827
- * @uses do_shortcode() to execute nested shortcodes in our shortcode
828
- *
829
- * @since WordPress Access Control 3.0
830
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
831
- */
832
- function shortcode_members( $attributes, $content = NULL )
833
- {
834
- global $current_user, $post;
835
-
836
- if ( is_user_logged_in() ) {
837
  if (isset($attributes['role'])) {
838
 
839
  // Determine the syntax (Singular role, OR syntax or NOT syntax)
@@ -867,103 +683,85 @@ class WordPressAccessControl
867
  }
868
  }
869
 
870
- return wpautop( do_shortcode( $content ) );
871
- }
872
-
873
- return '';
874
- }
875
-
876
- /**
877
- * Hides content for users that are logged in and displays content for users
878
- * who are not logged in. Calls do_shortcode on the content to allow nested
879
- * shortcodes.
880
- *
881
- * @uses is_user_logged_in() to determine if the user is logged in or not
882
- * @uses wpautop() to format the text in the shortcodes
883
- * @uses do_shortcode() to execute nested shortcodes in our shortcode
884
- *
885
- * @since WordPress Access Control 3.0
886
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
887
- */
888
- function shortcode_nonmembers( $attributes, $content = NULL )
889
- {
890
- global $post;
891
-
892
- if ( ! is_user_logged_in() ) {
893
- return wpautop( do_shortcode( $content ) );
894
- }
895
-
896
- return '';
897
- }
898
-
899
- /*
900
- * Loads a file that will override certain WordPress default widgets with
901
- * a slightly modified version that allows admins to select it's visibility
902
- * (Members only or non members only).
903
- *
904
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
905
- * @since WordPress Access Control 3.1
906
- */
907
- function load_custom_widgets()
908
- {
909
- require( dirname( __FILE__ ) . '/default-widgets.php' );
910
- }
911
-
912
- /**
913
- * Adds our member only menus for each regular menu
914
- *
915
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
916
- * @since WordPress Access Control 3.1
917
- */
918
- function add_wpac_nav_menus()
919
- {
920
- global $_wp_registered_nav_menus;
921
-
922
- $original = (array) $_wp_registered_nav_menus;
923
-
924
- foreach ( $original as $location => $label ) {
925
- $_wp_registered_nav_menus[$location . '_wpac'] = $label . ' - Members Only';
926
- }
927
- }
928
-
929
- /**
930
- * Checks to see if a members only version of the current menu is available
931
- * and will load that instead of the normal menu.
932
- *
933
- * @param array $args The arguments passed to the wp_nav_menu function
934
- *
935
- * @return array The original arguments, possible with a new value for theme_location
936
- *
937
- * @uses is_user_logged_in() to determine if the user is logged in
938
- * @uses get_nav_menu_locations() to get a list of valid theme locations
939
- * @uses wp_get_nav_menu_object() to get our custom members only menu
940
- *
941
- * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
942
- * @since WordPress Access Control 3.1
943
- */
944
- function wp_nav_menu_members_only( $args )
945
- {
946
- // Don't do anything if the user isn't logged in
947
- if ( is_user_logged_in() ) {
948
-
949
- // See if the theme even passed a proper menu ID
950
- if ( ! empty( $args['theme_location'] ) ) {
951
-
952
- // Member only menus end in _wpac
953
- $theme_location = $args['theme_location'] . '_wpac';
954
-
955
- // Get the nav menu based on the theme_location
956
- if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $theme_location ] ) ) {
957
- $menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
958
-
959
- // Only use the member only menu if it's not empty
960
- if ( $menu && $menu->count > 0 ) {
961
- $args['theme_location'] = $theme_location;
962
- }
963
- }
964
- }
965
- }
966
-
967
- return $args;
968
- }
969
  }
4
  * Plugin URI: http://brandonwamboldt.ca/plugins/members-only-menu-plugin/
5
  * Author: Brandon Wamboldt
6
  * Author URI: http://brandonwamboldt.ca/
7
+ * Version: 4.0.0
8
  * Description: This plugin is a powerful tool which gives you fine grained control over your pages and posts (and custom post types), allowing you to restrict a page, post, or custom post type to members, non-members, or even specific roles. You can customize how these pages and posts show up in search results, where users are directed when they visit them, and much more. <strong>You can even make your entire blog members only!</strong>.
9
  */
10
 
11
+ require dirname(__FILE__) . '/lib/Walker.php';
12
+
13
+ add_action('after_setup_theme', array('WordPressAccessControl', 'load_custom_widgets'));
14
+ add_action('wp', array('WordPressAccessControl', 'check_for_members_only'));
15
+ add_action('wp', array('WordPressAccessControl', 'check_for_nonmembers_only'));
16
+ add_action('init', array('WordPressAccessControl', 'add_wpac_nav_menus'));
17
+ add_action('admin_init', array('WordPressAccessControl', 'admin_init'));
18
+ add_action('admin_menu', array('WordPressAccessControl', 'admin_menu'));
19
+ add_action('add_meta_boxes', array('WordPressAccessControl', 'add_wp_access_meta_boxes'));
20
+ add_action('save_post', array('WordPressAccessControl', 'save_postdata'));
21
+ add_action('manage_pages_custom_column', array('WordPressAccessControl', 'show_column'));
22
+
23
+ add_filter('get_pages', array('WordPressAccessControl', 'get_pages'));
24
+ add_filter('manage_edit-page_columns', array('WordPressAccessControl', 'add_column'));
25
+ add_filter('the_excerpt', array('WordPressAccessControl', 'remove_excerpt'));
26
+ add_filter('the_content', array('WordPressAccessControl', 'remove_excerpt'));
27
+ add_filter('plugin_row_meta', array('WordPressAccessControl', 'plugin_row_meta'), 10, 5);
28
+ add_filter('posts_join_paged', array('WordPressAccessControl', 'posts_join_paged'), 10, 2);
29
+ add_filter('posts_where_paged', array('WordPressAccessControl', 'posts_where_paged'), 10, 2);
30
+ add_filter('wp_nav_menu_args', array('WordPressAccessControl', 'wp_nav_menu_members_only'));
31
+ add_filter('wp_nav_menu_args', array('WordPressAccessControl', 'wp_nav_menu_args'));
32
+ add_filter('wp_page_menu_args', array('WordPressAccessControl' , 'wp_page_menu_args'));
33
+
34
+ add_shortcode('member', array('WordPressAccessControl', 'shortcode_members'));
35
+ add_shortcode('members', array('WordPressAccessControl', 'shortcode_members'));
36
+ add_shortcode('non-members', array('WordPressAccessControl', 'shortcode_nonmembers'));
37
+ add_shortcode('nonmembers', array('WordPressAccessControl', 'shortcode_nonmembers'));
38
+ add_shortcode('non-member', array('WordPressAccessControl', 'shortcode_nonmembers'));
39
+ add_shortcode('nonmember', array('WordPressAccessControl', 'shortcode_nonmembers'));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  /**
42
  * The main plugin
43
  *
44
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
 
 
45
  */
46
  class WordPressAccessControl
47
  {
48
+ public static function admin_init()
49
+ {
50
+ wp_enqueue_style('wpac-style', plugin_dir_url(__FILE__) . 'public/css/wpac.css');
51
+
52
+ // Load translation files
53
+ if (!load_plugin_textdomain('wpac')) {
54
+ load_plugin_textdomain('wpac', false, 'wordpress-access-control/languages/');
55
+ }
56
+ }
57
+
58
+ public static function admin_menu()
59
+ {
60
+ add_options_page('WordPress Access Control Settings', 'Members Only', 'manage_options', 'wpac-options', array('WordPressAccessControl', 'options_page'));
61
+ }
62
+
63
+ public static function options_page()
64
+ {
65
+ // Save options?
66
+ if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'update') {
67
+ if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'wpac_options_save')) {
68
+ $admin_error = __('Security check failed, please try again', 'wpac');
69
+ } else {
70
+
71
+ // Make the blog members only
72
+ if (isset($_REQUEST['wpac_members_only_blog']) && $_REQUEST['wpac_members_only_blog'] == 'yes') {
73
+ update_option('wpac_members_only_blog', true);
74
+ } else {
75
+ update_option('wpac_members_only_blog', false);
76
+ }
77
+
78
+ // Members blog redirect link
79
+ if (isset($_REQUEST['wpac_members_blog_redirect'])) {
80
+ update_option('wpac_members_blog_redirect', esc_url($_REQUEST['wpac_members_blog_redirect']));
81
+ } else {
82
+ delete_option('wpac_members_blog_redirect');
83
+ }
84
+
85
+ // Support custom post types
86
+ if (isset($_REQUEST['wpac_custom_post_types']) && is_array($_REQUEST['wpac_custom_post_types'])) {
87
+ update_option('wpac_custom_post_types', $_REQUEST['wpac_custom_post_types']);
88
+ } else {
89
+ delete_option('wpac_custom_post_types');
90
+ }
91
+
92
+ // Override per-page permissions
93
+ if (isset($_REQUEST['wpac_always_accessible_by']) && is_array($_REQUEST['wpac_always_accessible_by'])) {
94
+ $wpac_always_accessible_by = array();
95
+
96
+ foreach ($_REQUEST['wpac_always_accessible_by'] as $role) {
97
+ $wpac_always_accessible_by[] = $role;
98
+ }
99
+
100
+ update_option('wpac_always_accessible_by', $wpac_always_accessible_by);
101
+ } else {
102
+ update_option('wpac_always_accessible_by', array());
103
+ }
104
+
105
+ // Show in the menu settings
106
+ if (isset($_REQUEST['wpac_show_in_menus']) && $_REQUEST['wpac_show_in_menus'] == 'always') {
107
+ update_option('wpac_show_in_menus', 'always');
108
+ } else {
109
+ update_option('wpac_show_in_menus', 'with_access');
110
+ }
111
+
112
+ // Default post state setting
113
+ if (isset($_REQUEST['default_post_state']) && in_array($_REQUEST['default_post_state'], array('public', 'members', 'nonmembers'))) {
114
+ update_option('wpac_default_post_state', $_REQUEST['default_post_state']);
115
+ } else {
116
+ update_option('wpac_default_post_state', 'public');
117
+ }
118
+
119
+ // Default post roles
120
+ if (isset($_REQUEST['wpac_posts_default_restricted_to']) && is_array($_REQUEST['wpac_posts_default_restricted_to'])) {
121
+ update_option('wpac_posts_default_restricted_to', $_REQUEST['wpac_posts_default_restricted_to']);
122
+ } else {
123
+ update_option('wpac_posts_default_restricted_to', array());
124
+ }
125
+
126
+ // Default page state setting
127
+ if (isset($_REQUEST['default_page_state']) && in_array($_REQUEST['default_page_state'], array('public', 'members', 'nonmembers'))) {
128
+ update_option('wpac_default_page_state', $_REQUEST['default_page_state']);
129
+ } else {
130
+ update_option('wpac_default_page_state', 'public');
131
+ }
132
+
133
+ // Default page roles
134
+ if (isset($_REQUEST['wpac_pages_default_restricted_to']) && is_array($_REQUEST['wpac_pages_default_restricted_to'])) {
135
+ update_option('wpac_pages_default_restricted_to', $_REQUEST['wpac_pages_default_restricted_to']);
136
+ } else {
137
+ update_option('wpac_pages_default_restricted_to', array());
138
+ }
139
+
140
+ // Default redirect
141
+ if (isset($_REQUEST['wpac_default_members_redirect']) && !empty($_REQUEST['wpac_default_members_redirect'])) {
142
+ update_option('wpac_default_members_redirect', $_REQUEST['wpac_default_members_redirect']);
143
+ } else {
144
+ update_option('wpac_default_members_redirect', '');
145
+ }
146
+
147
+ // Search Options
148
+ if (isset($_REQUEST['show_posts_in_search']) && $_REQUEST['show_posts_in_search'] == 'yes') {
149
+ update_option('wpac_show_posts_in_search', true);
150
+ } else {
151
+ update_option('wpac_show_posts_in_search', false);
152
+ }
153
+
154
+ if (isset($_REQUEST['show_post_excerpts_in_search']) && $_REQUEST['show_post_excerpts_in_search'] == 'yes') {
155
+ update_option('wpac_show_post_excerpts_in_search', true);
156
+ } else {
157
+ update_option('wpac_show_post_excerpts_in_search', false);
158
+ }
159
+
160
+ if (isset($_REQUEST['show_pages_in_search']) && $_REQUEST['show_pages_in_search'] == 'yes') {
161
+ update_option('wpac_show_pages_in_search', true);
162
+ } else {
163
+ update_option('wpac_show_pages_in_search', false);
164
+ }
165
+
166
+ if (isset($_REQUEST['show_page_excerpts_in_search']) && $_REQUEST['show_page_excerpts_in_search'] == 'yes') {
167
+ update_option('wpac_show_page_excerpts_in_search', true);
168
+ } else {
169
+ update_option('wpac_show_page_excerpts_in_search', false);
170
+ }
171
+
172
+ // Custom excerpts
173
+ if (isset($_REQUEST['page_excerpt_text'])) {
174
+ update_option('wpac_page_excerpt_text', $_REQUEST['page_excerpt_text']);
175
+ }
176
+
177
+ if (isset($_REQUEST['post_excerpt_text'])) {
178
+ update_option('wpac_post_excerpt_text', $_REQUEST['post_excerpt_text']);
179
+ }
180
+
181
+ $admin_message = '<strong>' . __('Settings Saved.') . '</strong>';
182
+ }
183
+ }
184
+
185
+ include(dirname(__FILE__) . '/templates/options.php');
186
+ }
187
+
188
+ public static function add_column($columns)
189
+ {
190
+ $columns['wpac'] = '<img src="' . plugin_dir_url(__FILE__) . 'public/images/lock.png" alt="Access" />';
191
+ return $columns;
192
+ }
193
+
194
+ public static function show_column($name)
195
+ {
196
+ global $post;
197
+
198
+ switch ($name) {
199
+ case 'wpac':
200
+ if (get_post_meta($post->ID, '_wpac_is_members_only', true)) {
201
+ echo '<img src="' . plugin_dir_url(__FILE__) . 'public/images/lock.png" alt="Members Only" title="Members Only" />';
202
+ } else if (get_post_meta($post->ID, '_wpac_is_nonmembers_only', true)) {
203
+ echo '<img src="' . plugin_dir_url(__FILE__) . 'public/images/unlock.png" alt="Non-members Only" title="Non-members Only" />';
204
+ }
205
+
206
+ break;
207
+ }
208
+ }
209
+
210
+ public static function check_conditions($page_id)
211
+ {
212
+ global $wp_roles, $current_user;
213
+
214
+ if (is_user_logged_in()) {
215
+ get_currentuserinfo();
216
+
217
+ $allowed_roles = maybe_unserialize(get_post_meta($page_id, '_wpac_restricted_to', true));
218
+ $override_roles = maybe_unserialize(get_option('wpac_always_accessible_by', array(0 => 'administrator')));
219
+
220
+ if (empty($allowed_roles)) {
221
+ $allowed_roles_tmp = $wp_roles->get_names();
222
+ $allowed_roles = array();
223
+
224
+ foreach ($allowed_roles_tmp as $role => $garbage) {
225
+ $allowed_roles[] = $role;
226
+ }
227
+ }
228
+
229
+ $intersections = array_intersect($current_user->roles, $allowed_roles);
230
+ $override_intersections = array_intersect($current_user->roles, $override_roles);
231
+
232
+ if (empty($intersections) && empty($override_intersections)) {
233
+ $role = false;
234
+ } else {
235
+ $role = true;
236
+ }
237
+ } else {
238
+ $role = true;
239
+ }
240
+
241
+ $is_members_only = get_post_meta($page_id, '_wpac_is_members_only', true);
242
+ $is_nonmembers_only = get_post_meta($page_id, '_wpac_is_nonmembers_only', true);
243
+
244
+ return ((is_user_logged_in() && $is_members_only && $role) || (!is_user_logged_in() && $is_nonmembers_only) || (!$is_members_only && !$is_nonmembers_only));
245
+ }
246
+
247
+ public static function check_for_members_only()
248
+ {
249
+ global $post;
250
+
251
+ if (is_admin()) return;
252
+
253
+ // Check for a members only blog
254
+ $blog_is_members_only = get_option('wpac_members_only_blog', false);
255
+
256
+ if ($blog_is_members_only && !is_user_logged_in()) {
257
+ $redirect_to = get_option('wpac_members_blog_redirect', wp_login_url($_SERVER['REQUEST_URI']));
258
+
259
+ if (empty($redirect_to)) {
260
+ $redirect_to = wp_login_url($_SERVER['REQUEST_URI']);
261
+ }
262
+
263
+ header('Location: ' . add_query_arg('redirect_to', $_SERVER['REQUEST_URI'], $redirect_to));
264
+ exit();
265
+ }
266
+
267
+ if (get_post_meta($post->ID, '_wpac_is_members_only', true) && !WordPressAccessControl::check_conditions($post->ID)) {
268
+ if (is_singular()) {
269
+ $redirect_to = get_post_meta($post->ID, '_wpac_members_redirect_to', true);
270
+
271
+ if (empty($redirect_to)) {
272
+ header('Location: ' . get_bloginfo('wpurl') . '/wp-login.php?redirect_to=' . $_SERVER['REQUEST_URI']);
273
+ } else {
274
+ header('Location: ' . add_query_arg('redirect_to', $_SERVER['REQUEST_URI'], $redirect_to));
275
+ }
276
+
277
+ exit();
278
+ }
279
+ }
280
+ }
281
+
282
+ public static function check_for_nonmembers_only()
283
+ {
284
+ if (is_admin()) return;
285
+
286
+ global $post;
287
+
288
+ if (get_post_meta($post->ID, '_wpac_is_nonmembers_only', true) && !WordPressAccessControl::check_conditions($post->ID)) {
289
+ $redirect_to = get_post_meta($post->ID, '_wpac_nonmembers_redirect_to', true);
290
+
291
+ if (empty($redirect_to)) {
292
+ header('Location: ' . get_bloginfo('wpurl') . '/index.php');
293
+ } else {
294
+ header('Location: ' . $redirect_to);
295
+ }
296
+
297
+ exit();
298
+ }
299
+ }
300
+
301
+ public static function remove_excerpt($excerpt)
302
+ {
303
+ if (is_admin() || is_single()) return $excerpt;
304
+
305
+ global $post;
306
+
307
+ if (get_post_meta($post->ID, '_wpac_is_members_only', true) && !WordPressAccessControl::check_conditions($post->ID)) {
308
+ $show_excerpt = get_post_meta($post->ID, '_wpac_show_excerpt_in_search', true);
309
+
310
+ if (!is_singular() && $show_excerpt != 'yes') {
311
+ if ($post->post_type == 'post') {
312
+ $excerpt = get_option('wpac_post_excerpt_text', __('To view the contents of this post, you must be authenticated and have the required access level.', 'wpac'));
313
+ } else {
314
+ $excerpt = get_option('wpac_page_excerpt_text', __('To view the contents of this page, you must be authenticated and have the required access level.', 'wpac'));
315
+ }
316
+
317
+ return wpautop($excerpt);
318
+ } else {
319
+ return $excerpt;
320
+ }
321
+ }
322
+
323
+ return $excerpt;
324
+ }
325
+
326
+ public static function posts_join_paged($join, $query)
327
+ {
328
+ global $wpdb;
329
+
330
+ if (is_admin()) return $join;
331
+
332
+ if (!is_singular() && !is_admin()) {
333
+ $join .= " LEFT JOIN {$wpdb->postmeta} PMWPAC3 ON PMWPAC3.post_id = {$wpdb->posts}.ID AND PMWPAC3.meta_key = '_wpac_show_in_search' LEFT JOIN {$wpdb->postmeta} PMWPAC ON PMWPAC.post_id = {$wpdb->posts}.ID AND PMWPAC.meta_key = '_wpac_is_members_only' LEFT JOIN {$wpdb->postmeta} PMWPAC2 ON PMWPAC2.post_id = {$wpdb->posts}.ID AND PMWPAC2.meta_key = '_wpac_is_nonmembers_only' ";
334
+ }
335
+
336
+ return $join;
337
+ }
338
+
339
+ public static function posts_where_paged($where, $query)
340
+ {
341
+ global $wpdb;
342
+
343
+ if (is_admin()) return $where;
344
+
345
+ if (!is_singular() && !is_admin()) {
346
+ if (!is_user_logged_in()) {
347
+ $where .= " AND (PMWPAC.meta_value IS NULL OR PMWPAC3.meta_value = '1' OR PMWPAC.meta_value != 'true') ";
348
+ } else {
349
+ $where .= " AND (PMWPAC2.meta_value IS NULL OR PMWPAC2.meta_value != 'true') ";
350
+ }
351
+ }
352
+
353
+ return $where;
354
+ }
355
+
356
+ public static function remove_restricted_posts($posts, $query = false)
357
+ {
358
+ return $posts;
359
+ echo $query->request;exit();
360
+
361
+ if (!is_singular() && !is_admin()) {
362
+ $new_posts = array();
363
+
364
+ foreach ($posts as $post) {
365
+ // The page is members only and we do NOT have access to it
366
+ if (get_post_meta($post->ID, '_wpac_is_members_only', true) && !WordPressAccessControl::check_conditions($post->ID)) {
367
+ if (get_post_meta($post->ID, '_wpac_show_in_search', true) != 1) {
368
+ continue;
369
+ }
370
+ }
371
+
372
+ // The page is non-members only and we do NOT have access to it
373
+ if (get_post_meta($post->ID, '_wpac_is_nonmembers_only', true) && !WordPressAccessControl::check_conditions($post->ID)) {
374
+ if (get_post_meta($post->ID, '_wpac_show_in_search', true) != 1) {
375
+ continue;
376
+ }
377
+ }
378
+
379
+ // Remove results where the search term is in a [member] or [nonmember] tag and we don't have access
380
+ $st = get_search_query();
381
+ $content = do_shortcode($post->post_content);
382
+
383
+
384
+ if (!empty($st) && is_search() && !stristr($content, $st)) {
385
+ continue;
386
+ }
387
+
388
+ $new_posts[] = $post;
389
+ }
390
+
391
+ $query->found_posts = count($new_posts);
392
+ $query->max_num_pages = ceil(count($new_posts) / $query->query_vars['posts_per_page']);
393
+ $query->posts = $new_posts;
394
+ $query->post_count = count($new_posts);
395
+
396
+ return $new_posts;
397
+ }
398
+
399
+ return $posts;
400
+ }
401
+
402
+ /**
403
+ * Add our custom meta box to all support post types
404
+ *
405
+ * @since WordPress Access Control 2.0
406
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
407
+ */
408
+ public static function add_wp_access_meta_boxes()
409
+ {
410
+ $enabled_post_types = get_option('wpac_custom_post_types', array());
411
+ $supported_pages = apply_filters('wp_access_supported_pages', array_merge(array('page', 'post'), $enabled_post_types));
412
+
413
+ foreach ($supported_pages as $page_type) {
414
+ add_meta_box('wpac_controls_meta', 'WordPress Access Control', array('WordPressAccessControl', 'add_wpac_meta_box'), $page_type, 'side', 'high');
415
+ }
416
+ }
417
+
418
+ /**
419
+ * Display the various per-page/per-post options in the sidebar
420
+ *
421
+ * @since WordPress Access Control 2.0
422
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
423
+ */
424
+ public static function add_wpac_meta_box()
425
+ {
426
+ global $post;
427
+
428
+ $is_members_only = get_post_meta($post->ID, '_wpac_is_members_only', true);
429
+ $is_nonmembers_only = get_post_meta($post->ID, '_wpac_is_nonmembers_only', true);
430
+
431
+ if ($post->post_type == 'post') {
432
+ $wpac_default_post_state = get_option('wpac_default_post_state', 'public');
433
+ } else {
434
+ $wpac_default_post_state = get_option('wpac_default_page_state', 'public');
435
+ }
436
+
437
+ if ($is_members_only == 'true' || ($post->post_status == 'auto-draft' && $wpac_default_post_state == 'members')) {
438
+ $is_members_only = 'checked="checked"';
439
+ }
440
+
441
+ if ($is_nonmembers_only == 'true' || ($post->post_status == 'auto-draft' && $wpac_default_post_state == 'nonmembers')) {
442
+ $is_nonmembers_only = 'checked="checked"';
443
+ }
444
+
445
+ $wpac_show_in_search = get_post_meta($post->ID, '_wpac_show_in_search', true);
446
+ $wpac_show_excerpt_in_search = get_post_meta($post->ID, '_wpac_show_excerpt_in_search', true);
447
+ $pass_to_children = get_post_meta($post->ID, '_wpac_pass_to_children', true);
448
+
449
+ include(dirname(__FILE__) . '/templates/meta_box.php');
450
+ }
451
+
452
+ /**
453
+ * Saves our custom meta data for the post being saved
454
+ *
455
+ * @since WordPress Access Control 2.0
456
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
457
+ */
458
+ public static function save_postdata($post_id)
459
+ {
460
+ // Security check
461
+ if (!isset($_POST['members_only_nonce'])) {
462
+ return $post_id;
463
+ }
464
+
465
+ if (!wp_verify_nonce($_POST['members_only_nonce'], 'members_only')) {
466
+ return $post_id;
467
+ }
468
+
469
+ // Meta data isn't transmitted during autosaves so don't do anything
470
+ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
471
+ return $post_id;
472
+ }
473
+
474
+ if (isset($_POST['members_only']) && $_POST['members_only'] == 'true') {
475
+ update_post_meta($post_id, '_wpac_is_members_only', 'true');
476
+
477
+ if (isset($_POST['wpac_restricted_to']) && !empty($_POST['wpac_restricted_to'])) {
478
+ update_post_meta($post_id, '_wpac_restricted_to', serialize($_POST['wpac_restricted_to']));
479
+ } else {
480
+ delete_post_meta($post_id, '_wpac_restricted_to');
481
+ }
482
+ } else {
483
+ delete_post_meta($post_id, '_wpac_is_members_only');
484
+ }
485
+
486
+ if (isset($_POST['nonmembers_only']) && $_POST['nonmembers_only'] == 'true') {
487
+ update_post_meta($post_id, '_wpac_is_nonmembers_only', 'true');
488
+ } else {
489
+ delete_post_meta($post_id, '_wpac_is_nonmembers_only');
490
+ }
491
+
492
+ if (isset($_POST['wpac_members_redirect_to'])) {
493
+ update_post_meta($post_id, '_wpac_members_redirect_to', $_POST['wpac_members_redirect_to']);
494
+ }
495
+
496
+ if (isset($_POST['wpac_show_in_search'])) {
497
+ update_post_meta($post_id, '_wpac_show_in_search', 1);
498
+ } else {
499
+ update_post_meta($post_id, '_wpac_show_in_search', 0);
500
+ }
501
+
502
+ if (isset($_POST['wpac_show_excerpt_in_search'])) {
503
+ update_post_meta($post_id, '_wpac_show_excerpt_in_search', 1);
504
+ } else {
505
+ update_post_meta($post_id, '_wpac_show_excerpt_in_search', 0);
506
+ }
507
+
508
+ if (isset($_POST['wpac_nonmembers_redirect_to'])) {
509
+ update_post_meta($post_id, '_wpac_nonmembers_redirect_to', $_POST['wpac_nonmembers_redirect_to']);
510
+ }
511
+
512
+ if (isset($_POST['pass_to_children']) && $_POST['pass_to_children'] == 'true') {
513
+ update_post_meta($post_id, '_wpac_pass_to_children', 'true');
514
+
515
+ $original_id = $post_id;
516
+ $is_revision = wp_is_post_revision($original_id);
517
+
518
+ if ($is_revision) {
519
+ $original_id = $is_revision;
520
+ }
521
+
522
+ $children = get_pages(array('child_of' => $original_id));
523
+
524
+ foreach ($children as $child) {
525
+ WordPressAccessControl::save_postdata($child->ID);
526
+ }
527
+ } else {
528
+ delete_post_meta($post_id, '_wpac_pass_to_children');
529
+ }
530
+
531
+ return $post_id;
532
+ }
533
+
534
+ /**
535
+ * Replaces the default WordPress nav menu walker with our own version
536
+ *
537
+ * @param array $args The arguments passed to the wp_nav_menu function
538
+ *
539
+ * @return array The original arguments, with our custom walker instead of the default
540
+ *
541
+ * @since WordPress Access Control 1.1
542
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
543
+ */
544
+ public static function wp_nav_menu_args($args)
545
+ {
546
+ $args['walker'] = new WpacSecureWalker($args['walker']);
547
+ return $args;
548
+ }
549
+
550
+ /**
551
+ * Prevents the code from degrading terribly when no nav menu is available
552
+ *
553
+ * @param array $args The arguments passed to the wp_nav_menu function
554
+ *
555
+ * @return array The original arguments, possible with our custom walker instead of the default
556
+ *
557
+ * @since WordPress Access Control 1.2
558
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
559
+ */
560
+ public static function wp_page_menu_args($args)
561
+ {
562
+ // Only remove the walker if it is ours
563
+ if (isset($args['walker']) && get_class($args['walker']) == 'WpacSecureWalker') {
564
+ $args['walker'] = new WpacSecureWalker(new Walker_Page());
565
+ } else {
566
+ $args['walker'] = new WpacSecureWalker($args['walker']);
567
+ }
568
+
569
+ return $args;
570
+ }
571
+
572
+ /**
573
+ * This hooks in at a higher level to make sure functions like wp_list_pages
574
+ * won't return members only pages.
575
+ *
576
+ * @param array $pages
577
+ * @return array
578
+ *
579
+ * @since WordPress Access Control 1.6.4
580
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
581
+ */
582
+ public static function get_pages($pages)
583
+ {
584
+ // Don't affect the display of pages when viewing the list in the admin
585
+ if (is_admin()) {
586
+ return $pages;
587
+ }
588
+
589
+ // Whether or not the user is logged in
590
+ $auth = is_user_logged_in();
591
+
592
+ // Go through each page, remove ones we can't see
593
+ foreach ($pages as $key => $page) {
594
+ $is_members_only = get_post_meta($page->ID, '_wpac_is_members_only', true);
595
+ $is_nonmembers_only = get_post_meta($page->ID, '_wpac_is_nonmembers_only', true);
596
+
597
+ if ($is_members_only && !$auth) {
598
+ unset($pages[$key]);
599
+ }
600
+
601
+ if ($is_nonmembers_only && $auth) {
602
+ unset($pages[$key]);
603
+ }
604
+ }
605
+
606
+ return $pages;
607
+ }
608
+
609
+ /**
610
+ * Adds links to the plugin row to view the options page or view the plugin
611
+ * documentation.
612
+ *
613
+ * @param array $plugin_meta The links to display in the plugin row
614
+ * @param string $plugin_file The directory name and filename of the plugin (wordpress-access-control/wordpress-access-control.php)
615
+ * @param array $plugin_data
616
+ * @param string $status The status of the plugin (active or inactive)
617
+ *
618
+ * @return array The $plugin_meta array of links for this plugin
619
+ *
620
+ * @uses plugin_dir_url() to get the URL of the plugin directory since that is where the documentation is
621
+ *
622
+ * @since WordPress Access Control 3.0
623
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
624
+ */
625
+ public static function plugin_row_meta($plugin_meta, $plugin_file, $plugin_data, $status)
626
+ {
627
+ if ($plugin_file == str_replace('.php', '/', basename(__FILE__)) . basename(__FILE__)) {
628
+ $plugin_meta[] = '<a href="options-general.php?page=wpac-options">' . __('Visit options page', 'wpac') . '</a>';
629
+ $plugin_meta[] = '<a href="' . plugin_dir_url(__FILE__) . 'documentation/index.html">' . __('Plugin Documentation', 'wpac') . '</a>';
630
+ }
631
+
632
+ return $plugin_meta;
633
+ }
634
+
635
+
636
+ /**
637
+ * Hides content for users that are not logged in and displays content for
638
+ * users who are logged in. Calls do_shortcode on the content to allow
639
+ * nested shortcodes.
640
+ *
641
+ * @uses is_user_logged_in() to determine if the user is logged in or not
642
+ * @uses wpautop() to format the text in the shortcodes
643
+ * @uses do_shortcode() to execute nested shortcodes in our shortcode
644
+ *
645
+ * @since WordPress Access Control 3.0
646
+ * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
647
+ */
648
+ public static function shortcode_members($attributes, $content = NULL)
649
+ {
650
+ global $current_user, $post;
651
+
652
+ if (is_user_logged_in()) {
 
 
653
  if (isset($attributes['role'])) {
654
 
655
  // Determine the syntax (Singular role, OR syntax or NOT syntax)
683
  }
684
  }
685
 
686
+ return wpautop(do_shortcode($content));
687
+ }
688
+
689
+ return '';
690
+ }
691
+
692
+ /**
693
+ * Hides content for users that are logged in and displays content for users
694
+ * who are not logged in. Calls do_shortcode on the content to allow nested
695
+ * shortcodes.
696
+ *
697
+ * @param array $attributes
698
+ * @param string $content
699
+ */
700
+ public static function shortcode_nonmembers($attributes, $content = NULL)
701
+ {
702
+ global $post;
703
+
704
+ if (!is_user_logged_in()) {
705
+ return wpautop(do_shortcode($content));
706
+ }
707
+
708
+ return '';
709
+ }
710
+
711
+ /**
712
+ * Loads a file that will override certain WordPress default widgets with
713
+ * a slightly modified version that allows admins to select it's visibility
714
+ * (Members only or non members only).
715
+ */
716
+ public static function load_custom_widgets()
717
+ {
718
+ require dirname(__FILE__) . '/default-widgets.php';
719
+ }
720
+
721
+ /**
722
+ * Adds our member only menus for each regular menu
723
+ */
724
+ public static function add_wpac_nav_menus()
725
+ {
726
+ global $_wp_registered_nav_menus;
727
+
728
+ $original = (array) $_wp_registered_nav_menus;
729
+
730
+ foreach ($original as $location => $label) {
731
+ $_wp_registered_nav_menus[$location . '_wpac'] = $label . ' - Members Only';
732
+ }
733
+ }
734
+
735
+ /**
736
+ * Checks to see if a members only version of the current menu is available
737
+ * and will load that instead of the normal menu.
738
+ *
739
+ * @param array $args The arguments passed to the wp_nav_menu function
740
+ * @return array The original arguments, possible with a new value for theme_location
741
+ */
742
+ public static function wp_nav_menu_members_only($args)
743
+ {
744
+ // Don't do anything if the user isn't logged in
745
+ if (is_user_logged_in()) {
746
+
747
+ // See if the theme even passed a proper menu ID
748
+ if (!empty($args['theme_location'])) {
749
+
750
+ // Member only menus end in _wpac
751
+ $theme_location = $args['theme_location'] . '_wpac';
752
+
753
+ // Get the nav menu based on the theme_location
754
+ if (($locations = get_nav_menu_locations()) && isset($locations[ $theme_location ])) {
755
+ $menu = wp_get_nav_menu_object($locations[ $theme_location ]);
756
+
757
+ // Only use the member only menu if it's not empty
758
+ if ($menu && $menu->count > 0) {
759
+ $args['theme_location'] = $theme_location;
760
+ }
761
+ }
762
+ }
763
+ }
764
+
765
+ return $args;
766
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
767
  }