PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.1
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.1
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / features / content-restriction / class-elementor-content-restriction.php

class-elementor-content-restriction.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.16.1, at features/content-restriction/class-elementor-content-restriction.php

367 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 use Elementor\Controls_Manager;
7
8 class WPPB_Elementor {
9 private static $_instance = null;
10 public $locations = array(
11 array(
12 'element' => 'common',
13 'action' => '_section_style',
14 ),
15 array(
16 'element' => 'section',
17 'action' => 'section_advanced',
18 ),
19 array(
20 'element' => 'container',
21 'action' => 'section_layout',
22 )
23 );
24 public $section_name = 'wppb_section_visibility_settings';
25
26 /**
27 * Register plugin action hooks and filters
28 */
29 public function __construct() {
30 // Register new section to display restriction controls
31 $this->register_sections();
32
33 // Setup controls
34 $this->register_controls();
35
36 // Filter widget content
37 add_filter( 'elementor/widget/render_content', array( $this, 'widget_render' ), 10, 2 );
38
39 // Filter sections display & add custom messages
40 add_action( 'elementor/frontend/section/should_render', array( $this, 'section_render' ), 10, 2 );
41 add_action( 'elementor/frontend/section/after_render', array( $this, 'section_custom_messages' ), 10, 2 );
42
43 // Filter container display & add custom messages
44 add_action( 'elementor/frontend/container/should_render', array( $this, 'section_render' ), 10, 2 );
45 add_action( 'elementor/frontend/container/after_render', array( $this, 'section_custom_messages' ), 10, 2 );
46
47 // Filter Elementor `the_content` hook
48 add_action( 'elementor/frontend/the_content', array( $this, 'filter_elementor_templates' ), 20 );
49
50 // Disable Element Cache when Content Restriction rules are setup for an element
51 add_filter( 'elementor/element/is_dynamic_content', array( $this, 'are_content_restriction_rules_setup' ), 20, 3 );
52 }
53
54 /**
55 *
56 * Ensures only one instance of the class is loaded or can be loaded.
57 *
58 * @return WPPB_Elementor An instance of the class.
59 */
60 public static function instance() {
61 if ( is_null( self::$_instance ) )
62 self::$_instance = new self();
63
64 return self::$_instance;
65 }
66
67 private function register_sections() {
68 foreach( $this->locations as $where ) {
69 add_action( 'elementor/element/'.$where['element'].'/'.$where['action'].'/after_section_end', array( $this, 'add_section' ), 10, 2 );
70 }
71 }
72
73 // Register controls to sections and widgets
74 private function register_controls() {
75 foreach( $this->locations as $where )
76 add_action('elementor/element/'.$where['element'].'/'.$this->section_name.'/before_section_end', array( $this, 'add_controls' ), 10, 2 );
77 }
78
79 public function add_section( $element, $args ) {
80 $exists = \Elementor\Plugin::instance()->controls_manager->get_control_from_stack( $element->get_unique_name(), $this->section_name );
81
82 if( !is_wp_error( $exists ) )
83 return false;
84
85 $element->start_controls_section(
86 $this->section_name, array(
87 'tab' => Controls_Manager::TAB_ADVANCED,
88 'label' => __( 'Profile Builder Content Restriction', 'profile-builder' )
89 )
90 );
91
92 $element->end_controls_section();
93 }
94
95 // Define controls
96 public function add_controls( $element, $args ) {
97 $element_type = $element->get_type();
98
99 $element->add_control(
100 'wppb_restriction_loggedin_users', array(
101 'label' => __( 'Restrict to logged in users', 'profile-builder' ),
102 'type' => Controls_Manager::SWITCHER,
103 'description' => __( 'Allow only logged in users to see this content.', 'profile-builder' ),
104 'default' => '',
105 'condition' => array(
106 'wppb_restriction_loggedout_users!' => 'yes'
107 )
108 )
109 );
110
111 $element->add_control(
112 'wppb_restriction_loggedout_users', array(
113 'label' => __( 'Restrict to logged out users', 'profile-builder' ),
114 'type' => Controls_Manager::SWITCHER,
115 'description' => __( 'Allow only logged out users to see this content.', 'profile-builder' ),
116 'default' => '',
117 'condition' => array(
118 'wppb_restriction_loggedin_users!' => 'yes'
119 )
120 )
121 );
122
123 $element->add_control(
124 'wppb_restriction_user_roles_heading', array(
125 'label' => __( 'Restrict by User Roles', 'profile-builder' ),
126 'type' => Controls_Manager::HEADING,
127 'separator' => 'before',
128 'condition' => array(
129 'wppb_restriction_loggedout_users!' => 'yes'
130 )
131 )
132 );
133
134 $element->add_control(
135 'wppb_restriction_user_roles', array(
136 'type' => Controls_Manager::SELECT2,
137 'options' => wp_roles()->get_names(),
138 'multiple' => 'true',
139 'label_block' => 'true',
140 'description' => __( 'Allow users which have the specified role to see this content.', 'profile-builder' ),
141 'condition' => array(
142 'wppb_restriction_loggedout_users!' => 'yes'
143 )
144 )
145 );
146
147 $element->add_control(
148 'wppb_restriction_custom_messages_heading', array(
149 'label' => __( 'Restriction Messages', 'profile-builder' ),
150 'type' => Controls_Manager::HEADING,
151 'separator' => 'before',
152 )
153 );
154
155 $element->add_control(
156 'wppb_restriction_default_messages', array(
157 'label' => __( 'Enable Restriction Messages', 'profile-builder' ),
158 'type' => Controls_Manager::SWITCHER,
159 'description' => __( 'Replace hidden content with the default messages from PB -> Settings -> Content Restriction, a custom message or an Elementor Template.', 'profile-builder' ),
160 )
161 );
162
163 $element->add_control(
164 'wppb_restriction_custom_messages', array(
165 'label' => __( 'Enable Custom Messages', 'profile-builder' ),
166 'type' => Controls_Manager::SWITCHER,
167 'description' => __( 'Add a custom message or template.', 'profile-builder' ),
168 'condition' => array(
169 'wppb_restriction_default_messages' => 'yes'
170 )
171 )
172 );
173
174 $element->add_control(
175 'wppb_restriction_custom_messages_type', array(
176 'label' => __( 'Content type', 'profile-builder' ),
177 'type' => Controls_Manager::CHOOSE,
178 'options' => array(
179 'text' => array(
180 'title' => __( 'Text', 'profile-builder' ),
181 'icon' => 'fa fa-align-left',
182 ),
183 'template' => array(
184 'title' => __( 'Template', 'profile-builder' ),
185 'icon' => 'fa fa-th-large',
186 )
187 ),
188 'default' => 'text',
189 'condition' => array(
190 'wppb_restriction_default_messages' => 'yes',
191 'wppb_restriction_custom_messages' => 'yes'
192 ),
193 )
194 );
195
196 //DCE_HELPER::get_all_template()
197 $element->add_control(
198 'wppb_restriction_fallback_template', array(
199 'type' => Controls_Manager::SELECT2,
200 'options' => $this->get_elementor_templates(),
201 'label' => __( 'Select Template', 'profile-builder' ),
202 'default' => '',
203 'label_block' => 'true',
204 'condition' => array(
205 'wppb_restriction_default_messages' => 'yes',
206 'wppb_restriction_custom_messages' => 'yes',
207 'wppb_restriction_custom_messages_type' => 'template'
208 ),
209 )
210 );
211
212 $element->add_control(
213 'wppb_restriction_fallback_text', array(
214 'type' => Controls_Manager::WYSIWYG,
215 'default' => '',
216 'condition' => array(
217 'wppb_restriction_default_messages' => 'yes',
218 'wppb_restriction_custom_messages' => 'yes',
219 'wppb_restriction_custom_messages_type' => 'text'
220 ),
221 )
222 );
223
224 }
225
226 // Verifies if element is hidden
227 public function is_hidden( $element ) {
228 $settings = $element->get_settings();
229
230 if( is_user_logged_in() && $settings['wppb_restriction_loggedout_users'] === 'yes' ) {
231 return true;
232 }
233
234 if( !empty( $settings['wppb_restriction_user_roles'] ) && is_user_logged_in() ) {
235
236 $user_data = get_userdata( get_current_user_id() );
237
238 foreach( $settings['wppb_restriction_user_roles'] as $restriction_role ) {
239 foreach( $user_data->roles as $user_role ) {
240 if( $user_role == $restriction_role ) {
241 return false;
242 }
243 }
244 }
245
246 return true;
247 } else if ( !is_user_logged_in() && (
248 ( $settings['wppb_restriction_loggedin_users'] == 'yes' ) || ( !empty( $settings['wppb_restriction_user_roles'] ) )
249 ) ) {
250
251 return true;
252 }
253
254 return false;
255 }
256
257 // Retrieves custom element message or the default message from PB settings
258 private function get_custom_message( $element ) {
259 $settings = $element->get_settings();
260
261 if( $settings['wppb_restriction_default_messages'] != 'yes' )
262 return false;
263
264 if( $settings['wppb_restriction_custom_messages'] == 'yes' ) {
265
266 if( $settings['wppb_restriction_custom_messages_type'] == 'text' )
267 return $settings['wppb_restriction_fallback_text'];
268 elseif( $settings['wppb_restriction_custom_messages_type'] == 'template' ) {
269 return $this->render_template( $settings['wppb_restriction_fallback_template'] );
270 }
271 } else {
272 if( is_user_logged_in() )
273 return wppb_content_restriction_process_content_message( 'logged_in', get_current_user_id() );
274 else
275 return wppb_content_restriction_process_content_message( 'logged_out', get_current_user_id() );
276 }
277 }
278
279 // Widget display & custom messages
280 public function widget_render( $content, $widget ) {
281 if( $this->is_hidden( $widget ) ) {
282
283 if( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
284 $widget->add_render_attribute( '_wrapper', 'class', 'wppb-visibility-hidden' );
285
286 return $content;
287 }
288
289 if( $message = $this->get_custom_message( $widget ) ) {
290 return $message;
291 }
292
293 return '<style>' . $widget->get_unique_selector() . '{display:none !important}</style>';
294 }
295
296 return $content;
297 }
298
299 // Section display
300 public function section_render( $should_render, $element ) {
301 if( $this->is_hidden( $element ) === true )
302 return false;
303
304 return $should_render;
305 }
306
307 // Section custom messages
308 public function section_custom_messages( $element ) {
309 if( $this->is_hidden( $element ) && ( $message = $this->get_custom_message( $element ) ) ) {
310
311 $element->add_render_attribute(
312 '_wrapper', 'class', array(
313 'elementor-section',
314 )
315 );
316
317 $element->before_render();
318 echo $message;//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
319 $element->after_render();
320 }
321 }
322
323 // Render an Elementor template based on ID
324 // Based on Elementor Pro template shortcode
325 public function render_template( $id ) {
326 return Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $id, true );
327 }
328
329 // Retrieve defined Elementor templates
330 private function get_elementor_templates() {
331 $templates = array();
332
333 foreach( \Elementor\Plugin::instance()->templates_manager->get_source('local')->get_items() as $template ) {
334 $templates[$template['template_id']] = $template['title'] . ' (' . $template['type'] . ')';
335 }
336
337 return $templates;
338 }
339
340 public function filter_elementor_templates( $content ) {
341 $document = \Elementor\Plugin::$instance->documents->get_current();
342
343 return wppb_content_restriction_filter_content( $content, $document->get_post() );
344 }
345
346 public function are_content_restriction_rules_setup( $is_dynamic_content, $data, $element ){
347
348 if( empty( $data['settings'] ) )
349 return $is_dynamic_content;
350
351 if( isset( $data['settings']['wppb_restriction_loggedin_users'] ) && $data['settings']['wppb_restriction_loggedin_users'] == 'yes' )
352 return true;
353
354 if( isset( $data['settings']['wppb_restriction_loggedout_users'] ) && $data['settings']['wppb_restriction_loggedout_users'] == 'yes' )
355 return true;
356
357 if( isset( $data['settings']['wppb_restriction_user_roles'] ) && !empty( $data['settings']['wppb_restriction_user_roles'] ) )
358 return true;
359
360 return $is_dynamic_content;
361
362 }
363 }
364
365 // Instantiate Plugin Class
366 WPPB_Elementor::instance();
367