PluginProbe
WP-Members Membership Plugin / trunk
WP-Members Membership Plugin vtrunk
trunk 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.4.2 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.4.9.1 3.4.9.2 3.4.9.3 3.4.9.4 3.4.9.5 3.4.9.6 3.4.9.7 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 All 34 releases
wp-members / includes / class-wp-members-menus.php

class-wp-members-menus.php in WP-Members Membership Plugin trunk, at includes/class-wp-members-menus.php

356 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The WP_Members Menus Class.
4 *
5 * Allows for handling of menu items based on user access. Limit menu itmes
6 * to logged in only, logged out only, or based on membership access.
7 *
8 * Modified from Nav Menu Roles by Kathy Darling (https://www.kathyisawesome.com/)
9 * https://wordpress.org/plugins/nav-menu-roles/
10 *
11 * @package WP-Members
12 * @subpackage WP_Members Menus Object Class
13 * @since 3.3.0
14 */
15
16 // Exit if accessed directly.
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit();
19 }
20
21 class WP_Members_Menus {
22
23 /**
24 * Meta for storing item settings.
25 *
26 * @since 3.3.0
27 * @access public
28 * @var string
29 */
30 public $post_meta = "_wpmem_item_settings";
31
32 /**
33 * Nonce name
34 *
35 * @since 3.3.0
36 * @access public
37 * @var string
38 */
39 public $nonce_name = "wpmem_nav_menu_nonce";
40
41 /**
42 * Nonce field
43 *
44 * @since 3.3.0
45 * @access public
46 * @var string
47 */
48 public $nonce_field = "wpmem_nav_menu_nonce";
49
50 /**
51 * Initialize WP_Members_Menus.
52 *
53 * @since 1.3.0
54 */
55 public function __construct(){
56 $this->load_hooks();
57 }
58
59 /**
60 * Loads hooks.
61 *
62 * @since 3.3.0
63 */
64 public function load_hooks() {
65
66 global $wp_version;
67 if ( version_compare( $wp_version, '5.4', '<' ) ) {
68 add_filter( 'wp_edit_nav_menu_walker', array( $this, 'edit_nav_menu_walker' ) );
69 }
70
71 add_action( 'wp_update_nav_menu_item', array( $this, 'update_nav_menu_item' ), 10, 2 );
72 add_filter( 'wp_setup_nav_menu_item', array( $this, 'setup_nav_menu_item' ) );
73 add_action( 'admin_enqueue_scripts' , array( $this, 'enqueue_scripts' ) );
74
75 add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'nav_menu_item_fields' ), 5, 4 );
76 // add_action( 'wp_nav_menu_item_custom_fields_customize_template', array( $this, 'nav_menu_item_fields' ), 5 ); // @todo Work this out.
77
78 add_action( 'wpmem_nav_menu_logged_in_criteria', array( $this, 'add_product_criteria' ) );
79
80 // Handles removing front end menu items.
81 if ( ! is_admin() ) {
82 add_filter( 'wp_get_nav_menu_items', array( $this, 'exclude_menu_items' ), 20 );
83 }
84 }
85
86 /**
87 * Override the Admin Menu Walker
88 *
89 * @since 3.3.0
90 *
91 * @global object $wpmem
92 *
93 * @return object WP_Members_Walker_Nav_Menu
94 */
95 public function edit_nav_menu_walker( $walker ) {
96 global $wpmem;
97 require_once( $wpmem->path . 'includes/walkers/class-wp-members-walker-nav-menu.php' );
98 return 'WP_Members_Walker_Nav_Menu';
99 }
100
101 /**
102 * Add fields to hook added in Walker
103 *
104 * @since 3.3.0
105 *
106 * @global object $wpmem
107 *
108 * @param string $item_id
109 * @param object $item
110 * @param $depth
111 * @param array $args
112 */
113 public function nav_menu_item_fields( $item_id, $item, $depth, $args ) {
114
115 $restrictions = get_post_meta( $item->ID, $this->post_meta, true );
116
117 $logged_in_out = '';
118 if ( is_array( $restrictions ) || $restrictions == 'in' ) {
119 $logged_in_out = 'in';
120 } else if ( $restrictions == 'out' ){
121 $logged_in_out = 'out';
122 }
123
124 $hidden = $logged_in_out == 'in' ? '' : 'display: none;';
125 ?>
126 <input type="hidden" name="<?php echo $this->nonce_name; ?>" value="<?php echo wp_create_nonce( $this->nonce_field ); ?>" />
127
128 <div class="field-wpmem_nav_menu wpmem_logged_in_out_field description-wide" style="margin: 5px 0;">
129 <span class="description"><?php _e( "Display", 'wp-members' ); ?></span>
130 <br />
131
132 <input type="hidden" class="nav-menu-id" value="<?php echo $item->ID ;?>" />
133
134 <div class="wpmem-logged-in" style="float: left; width: 35%;">
135 <input type="radio" class="wpmem-logged-in-out" name="wpmem_logged_in_out[<?php echo $item->ID ;?>]" id="wpmem_logged_in-for-<?php echo $item->ID ;?>" <?php checked( 'in', $logged_in_out ); ?> value="in" />
136 <label for="wpmem_logged_in-for-<?php echo $item->ID ;?>">
137 <?php _e( 'Logged In Users', 'wp-members'); ?>
138 </label>
139 </div>
140
141 <div class="wpmem-logged-in" style="float: left; width: 35%;">
142 <input type="radio" class="wpmem-logged-in-out" name="wpmem_logged_in_out[<?php echo $item->ID ;?>]" id="wpmem_logged_out-for-<?php echo $item->ID ;?>" <?php checked( 'out', $logged_in_out ); ?> value="out" />
143 <label for="wpmem_logged_out-for-<?php echo $item->ID ;?>">
144 <?php _e( 'Logged Out Users', 'wp-members'); ?>
145 </label>
146 </div>
147
148 <div class="wpmem-logged-in" style="float: left; width: 30%;">
149 <input type="radio" class="wpmem-logged-in-out" name="wpmem_logged_in_out[<?php echo $item->ID ;?>]" id="wpmem_all_users-for-<?php echo $item->ID ;?>" <?php checked( '', $logged_in_out ); ?> value="" />
150 <label for="wpmem_all_users-for-<?php echo $item->ID ;?>">
151 <?php _e( 'All Users', 'wp-members'); ?>
152 </label>
153 </div>
154
155 </div>
156
157 <?php if ( wpmem_is_enabled( 'enable_products' ) ) {
158 $display_products = wpmem_get_memberships();
159 $checked_products = ( isset( $restrictions['products'] ) && is_array( $restrictions['products'] ) ) ? $restrictions['products'] : false;
160 ?>
161 <div class="field-wpmem_nav_menu wpmem_nav_menu_field description-wide" style="margin: 5px 0; <?php echo $hidden;?>">
162 <?php if ( empty( $display_products ) ) {
163 $add_product_url = esc_url( admin_url( 'post-new.php?post_type=wpmem_product' ) );
164 ?>
165 <span class="description"><?php echo sprintf( esc_html__( "%sAdd membership products%s to restrict menu to a membership", 'wp-members' ), '<a href="' . $add_product_url . '">', '</a>' ); ?></span>
166 <?php } else { ?>
167 <span class="description"><?php echo esc_html__( "Restrict menu item to a membership product", 'wp-members' ); ?></span>
168 <br />
169 <?php
170
171 $i = 1;
172
173 /* Loop through each of the available memberships. */
174 foreach ( $display_products as $key => $product ) {
175
176 /* If the memberships has been selected, make sure it's checked. */
177 $checked = checked( true, ( is_array( $checked_products ) && in_array( $key, $checked_products ) ), false );
178 ?>
179
180 <div class="wpmem-product-input" style="float: left; width: 33.3%; margin: 2px 0;">
181 <input type="checkbox" name="wpmem_product[<?php echo $item->ID ;?>][<?php echo $i; ?>]" id="wpmem_product-<?php echo $key; ?>-for-<?php echo $item->ID ;?>" <?php echo $checked; ?> value="<?php echo $key; ?>" />
182 <label for="wpmem_product-<?php echo $key; ?>-for-<?php echo $item->ID ;?>">
183 <?php echo esc_html( $product['title'] ); ?>
184 <?php $i++; ?>
185 </label>
186 </div>
187
188 <?php }
189 } ?>
190 </div>
191 <?php } ?>
192 <?php
193 }
194
195 /**
196 * Enqueues javascript.
197 *
198 * @since 3.3.0
199 *
200 * @global object $wpmem
201 */
202 public function enqueue_scripts( $hook ) {
203 global $wpmem;
204 if ( $hook == 'nav-menus.php' ){
205 wp_enqueue_script( 'wp-members-menus', $wpmem->url . 'assets/js/wpmem-nav-menu' . wpmem_get_suffix() . '.js', array( 'jquery' ), $wpmem->version, true );
206 }
207 }
208
209 /**
210 * Save custom menu fields.
211 *
212 * @since 3.3.0
213 *
214 * @global object $wpmem
215 */
216 public function update_nav_menu_item( $menu_id, $menu_item_db_id ) {
217 $product_names = array();
218 foreach ( wpmem_get_memberships() as $key => $value ) {
219 $product_names[ $key ] = $value['title'];
220 }
221
222 // Verify this came from our screen and with proper authorization.
223 if ( ! isset( $_POST[ $this->nonce_name ] ) || ! wp_verify_nonce( $_POST[ $this->nonce_name ], $this->nonce_field ) ){
224 return;
225 }
226
227 $saved_data = false;
228
229 if ( isset( $_POST['wpmem_logged_in_out'][ $menu_item_db_id ] ) && 'in' == $_POST['wpmem_logged_in_out'][ $menu_item_db_id ] && isset( $_POST['wpmem_product'][ $menu_item_db_id ] ) ) {
230
231 $custom_fields = array();
232
233 foreach( (array) $_POST['wpmem_product'][ $menu_item_db_id ] as $product ) {
234
235 if ( array_key_exists ( $product, $product_names ) ) {
236 $custom_fields['products'][] = sanitize_text_field( $product );
237 }
238 }
239 if ( ! empty ( $custom_fields ) ) {
240 $saved_data = $custom_fields;
241 }
242 } elseif ( isset( $_POST['wpmem_logged_in_out'][ $menu_item_db_id ] ) && in_array( $_POST['wpmem_logged_in_out'][ $menu_item_db_id ], array( 'in', 'out' ) ) ) {
243 $saved_data = sanitize_text_field( $_POST['wpmem_logged_in_out'][ $menu_item_db_id ] );
244 }
245
246 if ( $saved_data ) {
247 update_post_meta( $menu_item_db_id, $this->post_meta, $saved_data );
248 } else {
249 delete_post_meta( $menu_item_db_id, $this->post_meta );
250 }
251 }
252
253 /**
254 * Add custom field to $item object.
255 *
256 * @since 3.3.0
257 *
258 * @global object $wpmem
259 *
260 * @param object $menu_item
261 * @return object $menu_item
262 */
263 public function setup_nav_menu_item( $menu_item ) {
264 global $wpmem;
265 if ( is_object( $menu_item ) && isset( $menu_item->ID ) ) {
266
267 $restrictions = get_post_meta( $menu_item->ID, $this->post_meta, true );
268
269 // If there are any restrictions, set them in the menu item.
270 if ( ! empty( $restrictions ) ) {
271 $menu_item->restrictions = $restrictions;
272 }
273 }
274 return $menu_item;
275 }
276
277 /**
278 * Exclude menu items based on custom criteria.
279 *
280 * @since 3.3.0
281 *
282 * @global object $wpmem
283 *
284 * @param array $items
285 * @return array $items
286 */
287 public function exclude_menu_items( $items ) {
288 global $wpmem;
289 $hide_children_of = array();
290
291 if ( ! empty( $items ) ) {
292
293 // Iterate and remove set items.
294 foreach ( $items as $key => $item ) {
295
296 $visible = true;
297
298 // Hide any item that is the child of a hidden item.
299 if ( isset( $item->menu_item_parent ) && in_array( $item->menu_item_parent, $hide_children_of ) ) {
300 $visible = false;
301 }
302
303 // Check items that have products.
304 if ( $visible && isset( $item->restrictions ) ) {
305
306 // Check all logged in, all logged out, or role.
307 switch( $item->restrictions ) {
308 case 'in' :
309 $visible = ( is_user_logged_in() ) ? true : false;
310 break;
311 case 'out' :
312 $visible = ( ! is_user_logged_in() ) ? true : false;
313 break;
314 default:
315 if ( 1 == $wpmem->enable_products ) {
316 $visible = false;
317 if ( is_array( $item->restrictions ) && ! empty( $item->restrictions ) ) {
318 foreach ( $item->restrictions['products'] as $product ) {
319 if ( wpmem_user_has_access( $product ) ) {
320 $visible = true;
321 }
322 }
323 }
324 } else {
325 $visible = true;
326 }
327 break;
328 }
329 }
330
331 /**
332 * Filters item visibility value.
333 *
334 * @since 3.3.0
335 *
336 * @param bool $visible
337 * @param object $item
338 */
339 $visible = apply_filters( 'wpmem_menu_item_visibility', $visible, $item );
340
341 // Unset non-visible item.
342 if ( ! $visible ) {
343 if ( isset( $item->ID ) ) {
344 $hide_children_of[] = $item->ID;
345 }
346 unset( $items[ $key ] ) ;
347 }
348
349 }
350
351 }
352
353 return $items;
354 }
355
356 }