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
ElementorRestriction.php
275 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ContentProtection; |
| 4 | |
| 5 | use Elementor\Controls_Manager; |
| 6 | use Elementor\Element_Base; |
| 7 | use Elementor\Widget_Base as Widget_Base; |
| 8 | use ProfilePress\Core\Membership\Models\Customer\CustomerFactory; |
| 9 | use ProfilePress\Core\Membership\Repositories\PlanRepository; |
| 10 | |
| 11 | class ElementorRestriction |
| 12 | { |
| 13 | public function __construct() |
| 14 | { |
| 15 | add_action('elementor/element/common/_section_style/after_section_end', [$this, 'register_section']); |
| 16 | add_action('elementor/element/section/section_advanced/after_section_end', [$this, 'register_section']); |
| 17 | add_action('elementor/element/column/section_advanced/after_section_end', [$this, 'register_section']); |
| 18 | add_action('elementor/element/container/section_layout/after_section_end', [$this, 'register_section']); |
| 19 | |
| 20 | add_action('elementor/element/common/ppress_section/before_section_end', array($this, 'register_controls'), 10, 2); |
| 21 | add_action('elementor/element/section/ppress_section/before_section_end', array($this, 'register_controls'), 10, 2); |
| 22 | add_action('elementor/element/column/ppress_section/before_section_end', array($this, 'register_controls'), 10, 2); |
| 23 | add_action('elementor/element/container/ppress_section/before_section_end', array($this, 'register_controls'), 10, 2); |
| 24 | |
| 25 | |
| 26 | add_filter('elementor/frontend/widget/should_render', [$this, 'should_render'], PHP_INT_MAX - 10, 2); |
| 27 | add_filter('elementor/frontend/section/should_render', [$this, 'should_render'], PHP_INT_MAX - 10, 2); |
| 28 | add_filter('elementor/frontend/column/should_render', [$this, 'should_render'], PHP_INT_MAX - 10, 2); |
| 29 | add_filter('elementor/frontend/container/should_render', [$this, 'should_render'], PHP_INT_MAX - 10, 2); |
| 30 | |
| 31 | // determine whether to replace widget's content with "Content Restricted" alert message or not |
| 32 | add_filter('elementor/widget/render_content', [$this, 'maybe_render_restricted_message'], 10, 2); |
| 33 | } |
| 34 | |
| 35 | public function register_section($element) |
| 36 | { |
| 37 | $element->start_controls_section('ppress_section', [ |
| 38 | 'label' => __('ProfilePress Content Restriction', 'wp-user-avatar'), |
| 39 | 'tab' => Controls_Manager::TAB_ADVANCED, |
| 40 | ]); |
| 41 | |
| 42 | $element->end_controls_section(); |
| 43 | } |
| 44 | |
| 45 | public function register_controls($element, $args) |
| 46 | { |
| 47 | if (is_a($element, '\Elementor\Core\DocumentTypes\Post')) return; |
| 48 | |
| 49 | $element->add_control( |
| 50 | 'ppress_visibility', |
| 51 | [ |
| 52 | 'label' => __('Target Audience', 'wp-user-avatar'), |
| 53 | 'type' => Controls_Manager::SELECT, |
| 54 | 'default' => 'everyone', |
| 55 | 'options' => [ |
| 56 | 'everyone' => __('Everyone', 'wp-user-avatar'), |
| 57 | 'loggedin' => __('Logged-in Users', 'wp-user-avatar'), |
| 58 | 'loggedout' => __('Logged-out Users', 'wp-user-avatar'), |
| 59 | ], |
| 60 | 'multiple' => false, |
| 61 | 'label_block' => true, |
| 62 | ] |
| 63 | ); |
| 64 | |
| 65 | $element->add_control( |
| 66 | 'ppress_membership_plans', |
| 67 | [ |
| 68 | 'label' => __('Restrict to Membership Plans', 'wp-user-avatar'), |
| 69 | 'type' => Controls_Manager::SELECT2, |
| 70 | 'options' => (function () { |
| 71 | |
| 72 | $plans = PlanRepository::init()->retrieveAll(); |
| 73 | |
| 74 | $result = []; |
| 75 | foreach ($plans as $plan) { |
| 76 | $result[$plan->id] = $plan->name; |
| 77 | } |
| 78 | |
| 79 | return $result; |
| 80 | })(), |
| 81 | 'multiple' => true, |
| 82 | 'label_block' => true, |
| 83 | 'condition' => [ |
| 84 | 'ppress_visibility' => ['loggedin'], |
| 85 | ], |
| 86 | ] |
| 87 | ); |
| 88 | |
| 89 | $element->add_control( |
| 90 | 'ppress_user_roles', |
| 91 | [ |
| 92 | 'label' => __('Restrict to User Roles', 'wp-user-avatar'), |
| 93 | 'type' => Controls_Manager::SELECT2, |
| 94 | 'options' => (function () { |
| 95 | |
| 96 | $all_roles = ppress_get_editable_roles(false); |
| 97 | |
| 98 | $result = []; |
| 99 | foreach ($all_roles as $key => $value) { |
| 100 | $result[$key] = $value['name']; |
| 101 | } |
| 102 | |
| 103 | return $result; |
| 104 | })(), |
| 105 | 'multiple' => true, |
| 106 | 'label_block' => true, |
| 107 | 'condition' => [ |
| 108 | 'ppress_visibility' => ['loggedin'], |
| 109 | ], |
| 110 | ] |
| 111 | ); |
| 112 | |
| 113 | // additional controls for Widget based elements only |
| 114 | if ($element instanceof Widget_Base) { |
| 115 | |
| 116 | $element->add_control( |
| 117 | 'ppress_alternate_content', |
| 118 | [ |
| 119 | 'label' => __('Alternate Content', 'wp-user-avatar'), |
| 120 | 'description' => __('The message to show when the main content is restricted.', 'wp-user-avatar'), |
| 121 | 'type' => Controls_Manager::SELECT, |
| 122 | 'default' => 'nothing', |
| 123 | 'options' => [ |
| 124 | 'nothing' => __('Show Nothing', 'wp-user-avatar'), |
| 125 | 'global' => __('Global Restrict Access Message', 'wp-user-avatar'), |
| 126 | 'custom' => __('Custom Message', 'wp-user-avatar'), |
| 127 | ], |
| 128 | 'multiple' => false, |
| 129 | 'label_block' => true, |
| 130 | 'condition' => [ |
| 131 | 'ppress_visibility' => ['loggedin', 'loggedout'] |
| 132 | ], |
| 133 | ] |
| 134 | ); |
| 135 | |
| 136 | $element->add_control( |
| 137 | 'ppress_custom_message', |
| 138 | [ |
| 139 | 'type' => Controls_Manager::WYSIWYG, |
| 140 | 'label' => __('Custom Message', 'wp-user-avatar'), |
| 141 | 'description' => __('This message will be shown as an alternate of your content', 'wp-user-avatar'), |
| 142 | 'default' => __('This content is restricted!', 'wp-user-avatar'), |
| 143 | 'condition' => [ |
| 144 | 'ppress_alternate_content' => 'custom', |
| 145 | 'ppress_visibility' => ['loggedin', 'loggedout'] |
| 146 | ], |
| 147 | ] |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param Element_Base $element |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | private function can_access($element) |
| 158 | { |
| 159 | if (is_admin() || \Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | $visibility = $element->get_settings('ppress_visibility'); |
| 164 | $roles = $element->get_settings('ppress_user_roles'); |
| 165 | $membership_plans = $element->get_settings('ppress_membership_plans'); |
| 166 | |
| 167 | if (empty($visibility) || 'everyone' == $visibility) return true; |
| 168 | |
| 169 | if ($visibility == 'loggedout' && ! is_user_logged_in()) return true; |
| 170 | |
| 171 | if ($visibility == 'loggedout' && is_user_logged_in()) return false; |
| 172 | |
| 173 | // if we got here, rule is for logged in users. return false of not logged in. |
| 174 | if ( ! is_user_logged_in()) return false; |
| 175 | |
| 176 | if (empty($roles) && empty($membership_plans)) return true; |
| 177 | |
| 178 | if ( ! empty($membership_plans)) { |
| 179 | |
| 180 | $customer = CustomerFactory::fromUserId(get_current_user_id()); |
| 181 | |
| 182 | $membership_plans = array_map('absint', $membership_plans); |
| 183 | |
| 184 | foreach ($membership_plans as $plan_id) { |
| 185 | if ($customer->has_active_subscription($plan_id)) return true; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if ( ! empty($roles)) { |
| 190 | |
| 191 | $user_roles = wp_get_current_user()->roles; |
| 192 | |
| 193 | if ( ! empty(array_intersect($roles, $user_roles))) return true; |
| 194 | } |
| 195 | |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param Element_Base $widget |
| 201 | * |
| 202 | * @return bool |
| 203 | */ |
| 204 | private function is_alternate_message_active($widget) |
| 205 | { |
| 206 | $setting = $widget->get_settings('ppress_alternate_content'); |
| 207 | |
| 208 | return in_array($setting, ['custom', 'global']); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @param bool $should_render |
| 213 | * @param Element_Base $widget |
| 214 | * |
| 215 | * @return bool |
| 216 | */ |
| 217 | public function should_render($should_render, $widget) |
| 218 | { |
| 219 | if ( ! $this->is_alternate_message_active($widget) && ! $this->can_access($widget)) { |
| 220 | $should_render = false; |
| 221 | } |
| 222 | |
| 223 | return $should_render; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Renders the "Content restricted" message instead of the widget's content. |
| 228 | * |
| 229 | * Applies if widget should be hidden from members and "Content restricted" message option is enabled. |
| 230 | * |
| 231 | * @param string $widget_content Elementor Widget content |
| 232 | * @param Widget_Base $widget Elementor Widget object |
| 233 | * |
| 234 | * @return string |
| 235 | * |
| 236 | */ |
| 237 | public function maybe_render_restricted_message($widget_content, $widget) |
| 238 | { |
| 239 | if ( |
| 240 | $this->is_alternate_message_active($widget) && |
| 241 | ! $this->can_access($widget) |
| 242 | ) { |
| 243 | |
| 244 | $message = $widget->get_settings('ppress_custom_message'); |
| 245 | if (empty($message)) { |
| 246 | $message = esc_html__('This content is restricted!', 'wp-user-avatar'); |
| 247 | } |
| 248 | |
| 249 | if ('global' == $widget->get_settings('ppress_alternate_content')) { |
| 250 | |
| 251 | $message = ppress_settings_by_key( |
| 252 | 'global_restricted_access_message', |
| 253 | esc_html__('You are unauthorized to view this page.', 'wp-user-avatar'), |
| 254 | true |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | return do_shortcode(wpautop($message)); |
| 259 | } |
| 260 | |
| 261 | return $widget_content; |
| 262 | } |
| 263 | |
| 264 | public static function get_instance() |
| 265 | { |
| 266 | static $instance = null; |
| 267 | |
| 268 | if (is_null($instance)) { |
| 269 | $instance = new self(); |
| 270 | } |
| 271 | |
| 272 | return $instance; |
| 273 | } |
| 274 | } |
| 275 |