-
Notifications
You must be signed in to change notification settings - Fork 10
/
nav-menu-roles-woocommerce-memberships.php
142 lines (114 loc) · 3.64 KB
/
nav-menu-roles-woocommerce-memberships.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Plugin Name: Nav Menu Roles + WooCommerce Memberships Bridge
* Plugin URI: http://github.com/helgatheviking/nav-menu-roles-woocommerce-memberships
* Description: Add WooCommerce Membership Plans to Nav Menu Roles
* Version: 1.0.0
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* GitHub Plugin URI: helgatheviking/nav-menu-roles-woocommerce-memberships
* GitHub Branch: master
*
* @package Nav Menu Role WooCommerce Memberships Bridge
* @category Core
* @author Kathy Darling
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/*
* Add NMR filters if both plugins are active
* @since 1.0.0
*/
function nmr_wcm_init(){
if( function_exists( 'wc_memberships' ) ){
add_filter( 'nav_menu_roles', 'nmw_wcm_new_roles' );
add_filter( 'nav_menu_roles_item_visibility', 'nmw_wcm_item_visibility', 10, 2 );
}
}
add_action( 'plugins_loaded', 'nmr_wcm_init', 20 );
/*
* Add custom roles to Nav Menu Roles menu options
*
* @param array $roles An array of all available roles, by default is global $wp_roles
* @return array
* @since 1.0.0
*/
function nmw_wcm_new_roles( $roles ){
return array_merge( $roles, nmr_wcm_get_roles_wrapper() );
}
/*
* Change visibilty of each menu item
* NMR settings can be "in" (all logged in), "out" (all logged out) or an array of specific roles
*
* @param bool $visible
* @param object $item The menu item object. Nav Menu Roles adds its info to $item->roles
* @return boolean
* @since 1.0.0
*/
function nmw_wcm_item_visibility( $visible, $item ){
if( ! $visible && isset( $item->roles ) && is_array( $item->roles ) ){
// Get the plugin-specific roles for this menu item.
$roles = nmr_wcm_get_relevant_roles_wrapper( $item->roles );
if( count( $roles ) > 0 ) {
// Only need to look through the relevant roles.
foreach( $roles as $role ) {
// Test if the current user has the specific plan membership.
if ( nmr_wcm_current_user_can_wrapper( $role ) ){
$visible = true;
break;
} else {
$visible = false;
}
}
}
}
return $visible;
}
/*-----------------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------------*/
/*
* Get the plugin-specific "roles" returned in an array, with ID => Name key pairs
*
* @return array
* @since 1.1.0
*/
function nmr_wcm_get_roles_wrapper(){
$roles = array();
$plans = wc_memberships_get_membership_plans();
if( ! empty( $plans ) ) {
foreach( $plans as $plan ){
$roles['wc_membership_' . $plan->id] = $plan->name;
}
}
return $roles;
}
/*
* Get the plugin-specific "roles" relevant to this menu item
*
* @return array
* @since 1.1.0
*/
function nmr_wcm_get_relevant_roles_wrapper( $roles = array() ){
return preg_grep( '/^wc_membership_*/', $roles );
}
/*
* Check the current user has plugin-specific level capability
*
* @param string $role_id | The ID of the "role" with a plugin-specific prefix
* @return bool
* @since 1.1.0
*/
function nmr_wcm_current_user_can_wrapper( $role_id = false ) {
$user_id = get_current_user_id();
if( ! $user_id || ! $role_id ) {
return false;
}
$role_id = str_replace( 'wc_membership_', '', $role_id );
return wc_memberships_is_user_active_member( $user_id, $role_id );
}