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
ElementorDisplayCondition.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ContentProtection; |
| 4 | |
| 5 | use Elementor\Controls_Manager; |
| 6 | use ElementorPro\Modules\DisplayConditions\Classes\Comparator_Provider as CP; |
| 7 | use ElementorPro\Modules\DisplayConditions\Classes\Comparators_Checker; |
| 8 | use ElementorPro\Modules\DisplayConditions\Conditions\Base\Condition_Base; |
| 9 | use ProfilePress\Core\Membership\Models\Customer\CustomerFactory; |
| 10 | use ProfilePress\Core\Membership\Repositories\PlanRepository; |
| 11 | |
| 12 | if ( ! defined('ABSPATH')) { |
| 13 | exit; // Exit if accessed directly |
| 14 | } |
| 15 | |
| 16 | class ElementorDisplayCondition extends Condition_Base |
| 17 | { |
| 18 | |
| 19 | public function get_name() |
| 20 | { |
| 21 | return 'profilepress_membership'; |
| 22 | } |
| 23 | |
| 24 | public function get_label() |
| 25 | { |
| 26 | return esc_html__('ProfilePress Membership', 'wp-user-avatar'); |
| 27 | } |
| 28 | |
| 29 | public function get_group() |
| 30 | { |
| 31 | return 'user'; |
| 32 | } |
| 33 | |
| 34 | public function check($args): bool |
| 35 | { |
| 36 | $user_id = get_current_user_id(); |
| 37 | |
| 38 | if (empty($args['plans'])) { |
| 39 | |
| 40 | return Comparators_Checker::check_equality( |
| 41 | $args['comparator'] == CP::COMPARATOR_IS_ONE_OF ? CP::COMPARATOR_IS : CP::COMPARATOR_IS_NOT, |
| 42 | CustomerFactory::fromUserId($user_id)->has_active_subscription(), |
| 43 | true |
| 44 | ); |
| 45 | |
| 46 | } else { |
| 47 | |
| 48 | $user_subscribed_plans = $user_id ? CustomerFactory::fromUserId($user_id)->get_active_subscriptions() : []; |
| 49 | |
| 50 | if ( ! empty($user_subscribed_plans)) { |
| 51 | $user_subscribed_plans = array_map(function ($sub) { |
| 52 | return $sub->get_plan_id(); |
| 53 | |
| 54 | }, $user_subscribed_plans); |
| 55 | } |
| 56 | |
| 57 | return Comparators_Checker::check_array_contains($args['comparator'], $user_subscribed_plans, $args['plans']); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public function get_options() |
| 62 | { |
| 63 | $comparators = CP::get_comparators( |
| 64 | [ |
| 65 | CP::COMPARATOR_IS_ONE_OF, |
| 66 | CP::COMPARATOR_IS_NONE_OF, |
| 67 | ] |
| 68 | ); |
| 69 | |
| 70 | $this->add_control( |
| 71 | 'comparator', |
| 72 | [ |
| 73 | 'type' => Controls_Manager::SELECT, |
| 74 | 'options' => $comparators, |
| 75 | 'default' => CP::COMPARATOR_IS_ONE_OF, |
| 76 | ] |
| 77 | ); |
| 78 | |
| 79 | $this->add_control( |
| 80 | 'plans', |
| 81 | [ |
| 82 | 'type' => Controls_Manager::SELECT2, |
| 83 | 'options' => $this->get_plan_options(), |
| 84 | 'multiple' => true, |
| 85 | 'default' => [], |
| 86 | ] |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return array |
| 92 | */ |
| 93 | private function get_plan_options() |
| 94 | { |
| 95 | $options = []; |
| 96 | |
| 97 | $plans = PlanRepository::init()->retrieveAll(); |
| 98 | |
| 99 | foreach ($plans as $plan) { |
| 100 | $options[$plan->get_id()] = $plan->get_name(); |
| 101 | } |
| 102 | |
| 103 | return $options; |
| 104 | } |
| 105 | } |