PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
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.9.9, at features/content-restriction/class-elementor-content-restriction.php

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