Frontend
3 months ago
views
1 year ago
CapabilityCheck.php
3 years ago
ConditionCallbacks.php
3 years ago
ConditionalBlocksIntegration.php
3 years ago
ContentConditions.php
7 months ago
ElementorDisplayCondition.php
1 month ago
ElementorRestriction.php
3 years ago
Init.php
1 month ago
NavMenuProtection.php
3 years ago
SettingsPage.php
1 year ago
WPListTable.php
1 year ago
index.php
5 years ago
NavMenuProtection.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ContentProtection; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Customer\CustomerFactory; |
| 6 | use ProfilePress\Core\Membership\Repositories\PlanRepository; |
| 7 | |
| 8 | class NavMenuProtection |
| 9 | { |
| 10 | public function __construct() |
| 11 | { |
| 12 | add_action('plugins_loaded', [$this, 'init'], 20); |
| 13 | } |
| 14 | |
| 15 | public function init() |
| 16 | { |
| 17 | if (class_exists('\Nav_Menu_Roles')) { |
| 18 | add_filter('nav_menu_roles', [$this, 'new_roles']); |
| 19 | add_filter('nav_menu_roles_item_visibility', [$this, 'item_visibility'], 10, 2); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * Add custom roles to Nav Menu Roles menu options |
| 25 | * |
| 26 | * @param array $roles An array of all available roles, by default is global $wp_roles |
| 27 | * @return array |
| 28 | */ |
| 29 | function new_roles($roles) |
| 30 | { |
| 31 | return array_merge($this->get_roles_wrapper(), $roles); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /* |
| 36 | * Change visibility of each menu item. |
| 37 | * |
| 38 | * NMR settings can be "in" (all logged in), "out" (all logged out) or an array of specific roles |
| 39 | * |
| 40 | * @param bool $visible |
| 41 | * @param object $item The menu item object. Nav Menu Roles adds its info to $item->roles |
| 42 | * @return boolean |
| 43 | */ |
| 44 | function item_visibility($visible, $item) |
| 45 | { |
| 46 | if ( ! $visible && isset($item->roles) && is_array($item->roles)) { |
| 47 | |
| 48 | // Get the plugin-specific roles for this menu item. |
| 49 | $roles = $this->get_relevant_roles_wrapper($item->roles); |
| 50 | |
| 51 | if (count($roles) > 0) { |
| 52 | |
| 53 | // Only need to look through the relevant roles. |
| 54 | foreach ($roles as $role) { |
| 55 | |
| 56 | // Test if the current user has the specific plan membership. |
| 57 | if ($this->current_user_can_wrapper($role)) { |
| 58 | $visible = true; |
| 59 | break; |
| 60 | } else { |
| 61 | $visible = false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
| 69 | return $visible; |
| 70 | } |
| 71 | |
| 72 | /*-----------------------------------------------------------------------------------*/ |
| 73 | /* Helper Functions */ |
| 74 | /*-----------------------------------------------------------------------------------*/ |
| 75 | |
| 76 | /* |
| 77 | * Get the plugin-specific "roles" returned in an array, with ID => Name key pairs |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | function get_roles_wrapper() |
| 82 | { |
| 83 | $roles = array(); |
| 84 | |
| 85 | $plans = PlanRepository::init()->retrieveAll(); |
| 86 | |
| 87 | if ( ! empty($plans)) { |
| 88 | |
| 89 | foreach ($plans as $plan) { |
| 90 | $roles['ppress_membership_' . $plan->id] = sprintf('%s (ProfilePress %s)', $plan->name, esc_html__('Plan', 'wp-user-avatar')); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $roles; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Get the plugin-specific "roles" relevant to this menu item |
| 99 | * |
| 100 | * @return array |
| 101 | */ |
| 102 | function get_relevant_roles_wrapper($roles = array()) |
| 103 | { |
| 104 | return preg_grep('/^ppress_membership_*/', $roles); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Check the current user has plugin-specific level capability |
| 109 | * |
| 110 | * @param string $role_id | The ID of the "role" with a plugin-specific prefix |
| 111 | * |
| 112 | * @return bool |
| 113 | */ |
| 114 | function current_user_can_wrapper($role_id = false) |
| 115 | { |
| 116 | $user_id = get_current_user_id(); |
| 117 | |
| 118 | if ( ! $user_id || ! $role_id) return false; |
| 119 | |
| 120 | $role_id = str_replace('ppress_membership_', '', $role_id); |
| 121 | |
| 122 | return CustomerFactory::fromUserId($user_id)->has_active_subscription($role_id); |
| 123 | } |
| 124 | |
| 125 | public static function get_instance() |
| 126 | { |
| 127 | static $instance = null; |
| 128 | |
| 129 | if (is_null($instance)) { |
| 130 | $instance = new self(); |
| 131 | } |
| 132 | |
| 133 | return $instance; |
| 134 | } |
| 135 | } |
| 136 |