PluginProbe
WP Directory Kit / 1.2.7
WP Directory Kit v1.2.7
1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 1.4.0 1.4.1 All 35 releases
wpdirectorykit / elementor-extensions / class-hider.php

class-hider.php in WP Directory Kit 1.2.7, at elementor-extensions/class-hider.php

508 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH'))
4 exit; // Exit if accessed directly
5
6 use Elementor\Plugin;
7 use Elementor\Controls_Manager;
8 use Elementor\Modules\DynamicTags\Module;
9
10 add_action( 'elementor/init', function() {
11
12 $listing_page_id = get_option('wdk_listing_page');
13
14 if(!empty($listing_page_id))
15 {
16 new WDK_Extension_Hider();
17
18 /*
19 if(isset($_GET['post']) && $_GET['post'] == $listing_page_id)
20 {
21 new WDK_Extension_Hider();
22 }
23 elseif(!Plugin::$instance->editor->is_edit_mode())
24 {
25 new WDK_Extension_Hider();
26 }*/
27 }
28 });
29
30 /**
31 * WDK_Extension_Hider
32 *
33 * Class to extend Elementor controls functionality, adding hide feature based on specific wdk field
34 *
35 */
36
37 class WDK_Extension_Hider {
38
39 public $name = 'WDK Hider';
40
41 private $is_common = true;
42
43 private $depended_scripts = [];
44
45 private $depended_styles = [];
46
47 private $has_controls = TRUE;
48
49 public $common_sections_actions = array(
50 array(
51 'element' => 'common',
52 'action' => '_section_style',
53 ),
54 array(
55 'element' => 'section',
56 'action' => 'section_advanced',
57 ),
58 array(
59 'element' => 'column',
60 'action' => 'section_advanced',
61 ),
62 );
63
64 private $supported_elements = array(
65 'heading'
66 );
67
68 public function __construct() {
69
70 /*
71 Controls_Manager::add_tab(
72 'wdk_hider',
73 __( 'Hider', 'wpdirectorykit' )
74 );*/
75
76 $this->init();
77 }
78
79 public function init( $param = null ) {
80 // Enqueue scripts
81 add_action( 'elementor/frontend/after_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
82
83 // Enqueue styles
84 add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'enqueue_styles' ] );
85
86 // Elementor hooks
87
88 if ( $this->is_common ) {
89 // Add the advanced section required to display controls
90 $this->add_common_sections_actions();
91 }
92
93 $this->add_actions();
94 }
95
96 public static function is_enabled() {
97 return true;
98 }
99
100 public function add_script_depends( $handler ) {
101 $this->depended_scripts[] = $handler;
102 }
103
104 public function add_style_depends( $handler ) {
105 $this->depended_styles[] = $handler;
106 }
107
108 public function get_script_depends() {
109 return $this->depended_scripts;
110 }
111
112 public function enqueue_scripts() {
113 foreach ( $this->get_script_depends() as $script ) {
114 wp_enqueue_script( $script );
115 }
116 }
117
118 public function get_style_depends() {
119 return $this->depended_styles;
120 }
121
122 public static function get_description() {
123 return '';
124 }
125
126 public function enqueue_styles() {
127 foreach ( $this->get_style_depends() as $style ) {
128 wp_enqueue_style( $style );
129 }
130 }
131
132 public function _enqueue_scripts() {
133 $scripts = $this->get_script_depends();
134 if ( ! empty( $scripts ) ) {
135 foreach ( $scripts as $script ) {
136 wp_enqueue_script( $script );
137 }
138 }
139 }
140
141 public function _enqueue_styles() {
142 $styles = $this->get_style_depends();
143 if ( ! empty( $styles ) ) {
144 foreach ( $styles as $style ) {
145 wp_enqueue_style( $style );
146 }
147 }
148 }
149
150 public function enqueue_all() {
151 $this->_enqueue_styles();
152 $this->_enqueue_scripts();
153 }
154
155 public function get_low_name() {
156 return 'hider';
157 }
158
159 final public function add_common_sections( $element, $args ) {
160 $low_name = $this->get_low_name();
161 $section_name = 'wdk_section_' . $low_name . '_advanced';
162
163 if ( ! $this->has_controls ) {
164 // no need settings
165 return false;
166 }
167
168 // Check if this section exists
169 $section_exists = \Elementor\Plugin::instance()->controls_manager->get_control_from_stack( $element->get_unique_name(), $section_name );
170
171 if ( ! is_wp_error( $section_exists ) ) {
172 // We can't and should try to add this section to the stack
173 return false;
174 }
175
176 $this->get_control_section( $section_name, $element );
177 }
178
179 public function add_common_sections_actions() {
180 foreach ( $this->common_sections_actions as $action ) {
181 // Activate action for elements
182 add_action('elementor/element/' . $action['element'] . '/' . $action['action'] . '/after_section_end', function ( $element, $args ) {
183 $this->add_common_sections( $element, $args );
184 }, 10, 2);
185 }
186 }
187
188 protected function add_actions() {
189
190 // WIDGET
191 add_action( 'elementor/frontend/widget/before_render', [ $this, '_start_element' ], 10, 1 );
192 add_action( 'elementor/frontend/widget/after_render', [ $this, '_end_element' ], 10, 1 );
193
194 // SECTION
195 add_action( 'elementor/frontend/section/before_render', [ $this, '_start_element' ], 10, 1 );
196 add_action( 'elementor/frontend/section/after_render', [ $this, '_end_element' ], 10, 1 );
197
198 // COLUMN
199 add_action( 'elementor/frontend/column/before_render', [ $this, '_start_element' ], 10, 1 );
200 add_action( 'elementor/frontend/column/after_render', [ $this, '_end_element' ], 10, 1 );
201
202
203 }
204
205 protected function remove_controls( $element, $controls = null ) {
206 if ( empty( $controls ) ) {
207 return;
208 }
209
210 if ( is_array( $controls ) ) {
211 $control_id = $controls;
212
213 foreach ( $controls as $control_id ) {
214 $element->remove_control( $control_id );
215 }
216 } else {
217 $element->remove_control( $controls );
218 }
219 }
220
221 public function get_control_section( $section_name, $element ) {
222 $low_name = $this->get_low_name();
223
224 $element->start_controls_section(
225 $section_name,
226 [
227 'label' => '<span class="color-wdk icon icon-dyn-logo-wdk pull-right ml-1"></span> ' . $this->name,
228 'tab' => 'advanced',
229 ]
230 );
231
232 $WMVC = &wdk_get_instance();
233 $WMVC->model('field_m');
234 $fields_data = $WMVC->field_m->get();
235 $fields_list = array('' => esc_html__('Not Selected', 'wpdirectorykit'));
236 $order_i = 0;
237
238 $fields_list [(++$order_i).'__section'] = esc_html__('-- Section Custom fields --', 'wpdirectorykit');
239 $fields_list [(++$order_i).'__profile_have_agency'] = esc_html__('Profile page is agency', 'wpdirectorykit');
240 $fields_list [(++$order_i).'__have_agency'] = esc_html__('Listing have agency', 'wpdirectorykit');
241 $fields_list [(++$order_i).'__have_sublistings'] = esc_html__('Listing have sublistings', 'wpdirectorykit');
242 $fields_list [(++$order_i).'__have_calendar'] = esc_html__('Listing have calendar', 'wpdirectorykit');
243 $fields_list [(++$order_i).'__havent_calendar'] = esc_html__('Listing not have calendar', 'wpdirectorykit');
244 $fields_list [(++$order_i).'__idlisting'] = esc_html__('Id listing', 'wpdirectorykit');
245 $fields_list [(++$order_i).'__post_id'] = esc_html__('Post Id', 'wpdirectorykit');
246 $fields_list [(++$order_i).'__lat'] = esc_html__('Gps Lat', 'wpdirectorykit');
247 $fields_list [(++$order_i).'__lng'] = esc_html__('Gps Lng', 'wpdirectorykit');
248 $fields_list [(++$order_i).'__date'] = esc_html__('Date', 'wpdirectorykit');
249 $fields_list [(++$order_i).'__date_modified'] = esc_html__('Date Modified', 'wpdirectorykit');
250 $fields_list [(++$order_i).'__post_content'] = esc_html__('WP Content', 'wpdirectorykit');
251 $fields_list [(++$order_i).'__address'] = esc_html__('Address', 'wpdirectorykit');
252 $fields_list [(++$order_i).'__category_id'] = esc_html__('Category', 'wpdirectorykit');
253 $fields_list [(++$order_i).'__location_id'] = esc_html__('Location', 'wpdirectorykit');
254
255 foreach($fields_data as $field)
256 {
257 if(wmvc_show_data('field_type', $field) == 'SECTION') {
258 $fields_list [(++$order_i).'section__'.wmvc_show_data('idfield', $field)] = '-- '.esc_html__('Section', 'wpdirectorykit').' '.wmvc_show_data('field_label', $field).' --';
259 } else {
260 $fields_list[(++$order_i).'__'.wmvc_show_data('idfield', $field)] = '#'.wmvc_show_data('idfield', $field).' '.wmvc_show_data('field_label', $field).'['.wmvc_show_data('field_type', $field).']';
261 }
262 }
263
264 $element->add_control(
265 'wdk_related_field',
266 [
267 'label' => esc_html__('WDK Related FIeld', 'wpdirectorykit'),
268 'type' => Controls_Manager::SELECT,
269 'options' => $fields_list,
270 'default' => '',
271 'separator' => 'after',
272 ]
273 );
274
275 $element->end_controls_section();
276
277 }
278
279 public function _start_element( $element ) {
280 global $wdk_listing_id;
281 $wdkmembership_user_id = wdk_get_profile_page_id();
282
283 if(Plugin::$instance->editor->is_edit_mode() || (empty($wdk_listing_id) && empty($wdkmembership_user_id)))return;
284 //if(!in_array($element->get_name(), $this->supported_elements))return;
285
286 $WMVC = &wdk_get_instance();
287 $WMVC->model('field_m');
288
289 $settings = $element->get_settings_for_display();
290
291 if(isset($settings['wdk_related_field']) && !empty($settings['wdk_related_field']))
292 {
293 $field_id = NULL;
294 $field_value = NULL;
295 $field_empty = FALSE;
296 if(strpos($settings['wdk_related_field'], '__') !== FALSE){
297 $field_id = substr($settings['wdk_related_field'], strpos($settings['wdk_related_field'],'__')+2);
298 }
299 if(is_null($field_id))return;
300 if($field_id == 'havent_calendar' || $field_id == 'have_calendar') {
301 if(function_exists('run_wdk_bookings')) {
302 global $Winter_MVC_wdk_bookings;
303 $Winter_MVC_wdk_bookings->model('calendar_m');
304 $calendar = $Winter_MVC_wdk_bookings->calendar_m->get_by(array('post_id' => $wdk_listing_id, 'is_activated' => 1));
305 if($field_id == 'havent_calendar') {
306 if($calendar){
307 $field_empty = true;
308 }
309 } else if($field_id == 'have_calendar') {
310 if(!$calendar){
311 $field_empty = true;
312 }
313 }
314 }
315 } elseif($field_id == 'profile_have_agency') {
316 $wdkmembership_user_id = wdk_get_profile_page_id();
317 $field_empty = true;
318
319
320 if($wdkmembership_user_id) {
321 $user = wdk_get_user_data($wdkmembership_user_id);
322 if($user && wmvc_is_user_in_role($user['userdata'], 'wdk_agency')) {
323 $field_empty = false;
324 }
325
326 }
327 } elseif($field_id == 'have_agency') {
328 $field_empty = true;
329 if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) {
330 if(wdk_field_value ('user_id_editor', $wdk_listing_id)) {
331 $agent_id = wdk_field_value ('user_id_editor', $wdk_listing_id);
332 global $Winter_MVC_wdk_membership;
333 $agent_agency = $Winter_MVC_wdk_membership->agency_agent_m->get_by(array('agent_id' => $agent_id, 'status' => 'CONFIRMED'), TRUE);
334 if($agent_agency) {
335 $user = wdk_get_user_data( wmvc_show_data('agency_id', $agent_agency, 0));
336 if($user){
337 $field_empty = false;
338 }
339 }
340 }
341 }
342 } elseif($field_id == 'have_sublistings') {
343
344 $field_empty = true;
345 if(!empty(wdk_field_value('listing_related_ids', $wdk_listing_id))){
346 foreach (explode(',',wdk_field_value('listing_related_ids', $wdk_listing_id)) as $key => $child_idlisting) {
347 if(!wdk_field_value('is_activated', $child_idlisting) || !wdk_field_value('is_approved', $child_idlisting)) continue;
348
349 $field_empty = false;
350 break;
351 }
352 }
353 } elseif(wdk_field_option($field_id, 'field_type') == "SECTION") {
354
355 // get all section fields
356 $sections_data = $WMVC->field_m->get_fields_section();
357
358 if(!isset($sections_data[$field_id]) || !isset($sections_data[$field_id]['fields']))return;
359
360 // check if all subfields are empty
361 $field_empty = true;
362 foreach($sections_data[$field_id]['fields'] as $field) {
363 if(!empty(wdk_field_value(wmvc_show_data('idfield', $field), $wdk_listing_id))){
364 $field_empty = false;
365 break;
366 }
367 }
368 }
369 else
370 {
371 $field_value = wdk_field_value($field_id, $wdk_listing_id);
372
373 if(empty($field_value))$field_empty = TRUE;
374 }
375
376 if($field_empty)
377 {
378 /*
379 echo '<pre>';
380 var_dump($settings['wdk_related_field']);
381 echo '</pre>';
382 */
383
384 ob_start();
385 }
386
387 }
388 }
389
390 public function _end_element( $element ) {
391 global $wdk_listing_id;
392
393 $wdkmembership_user_id = wdk_get_profile_page_id();
394
395 if(Plugin::$instance->editor->is_edit_mode() || (empty($wdk_listing_id) && empty($wdkmembership_user_id)))return;
396 //if(!in_array($element->get_name(), $this->supported_elements))return;
397
398 $WMVC = &wdk_get_instance();
399 $WMVC->model('field_m');
400
401 $settings = $element->get_settings_for_display();
402
403 if(isset($settings['wdk_related_field']) && !empty($settings['wdk_related_field']))
404 {
405 $field_id = NULL;
406 $field_value = NULL;
407 $field_empty = FALSE;
408 if(strpos($settings['wdk_related_field'], '__') !== FALSE){
409 $field_id = substr($settings['wdk_related_field'], strpos($settings['wdk_related_field'],'__')+2);
410 }
411
412 if(is_null($field_id))return;
413 if($field_id == 'havent_calendar' || $field_id == 'have_calendar') {
414 if(function_exists('run_wdk_bookings')) {
415 global $Winter_MVC_wdk_bookings;
416 $Winter_MVC_wdk_bookings->model('calendar_m');
417 $calendar = $Winter_MVC_wdk_bookings->calendar_m->get_by(array('post_id' => $wdk_listing_id, 'is_activated' => 1));
418 if($field_id == 'havent_calendar') {
419 if($calendar){
420 $field_empty = true;
421 }
422 } else if($field_id == 'have_calendar') {
423 if(!$calendar){
424 $field_empty = true;
425 }
426 }
427 }
428 } elseif($field_id == 'have_sublistings') {
429
430 $field_empty = true;
431 if(!empty(wdk_field_value('listing_related_ids', $wdk_listing_id))){
432 foreach (explode(',',wdk_field_value('listing_related_ids', $wdk_listing_id)) as $key => $child_idlisting) {
433 if(!wdk_field_value('is_activated', $child_idlisting) || !wdk_field_value('is_approved', $child_idlisting)) continue;
434
435 $field_empty = false;
436 break;
437 }
438 }
439 } elseif($field_id == 'have_agency') {
440 $field_empty = true;
441 if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) {
442 if(wdk_field_value ('user_id_editor', $wdk_listing_id)) {
443 $agent_id = wdk_field_value ('user_id_editor', $wdk_listing_id);
444 global $Winter_MVC_wdk_membership;
445 $agent_agency = $Winter_MVC_wdk_membership->agency_agent_m->get_by(array('agent_id' => $agent_id, 'status' => 'CONFIRMED'), TRUE);
446 if($agent_agency) {
447 $user = wdk_get_user_data( wmvc_show_data('agency_id', $agent_agency, 0));
448 if($user){
449 $field_empty = false;
450 }
451 }
452 }
453 }
454 } elseif($field_id == 'profile_have_agency') {
455 $wdkmembership_user_id = wdk_get_profile_page_id();
456 $field_empty = true;
457 if($wdkmembership_user_id) {
458 $user = wdk_get_user_data($wdkmembership_user_id);
459 if($user && wmvc_is_user_in_role($user['userdata'], 'wdk_agency')) {
460 $field_empty = false;
461 }
462
463 }
464 } elseif(wdk_field_option($field_id, 'field_type') == "SECTION") {
465
466 // get all section fields
467 $sections_data = $WMVC->field_m->get_fields_section();
468
469 if(!isset($sections_data[$field_id]) || !isset($sections_data[$field_id]['fields']))return;
470
471 // check if all subfields are empty
472 $field_empty = true;
473 foreach($sections_data[$field_id]['fields'] as $field) {
474 if(!empty(wdk_field_value(wmvc_show_data('idfield', $field), $wdk_listing_id))){
475 $field_empty = false;
476 break;
477 }
478 }
479 }
480 else
481 {
482 $field_value = wdk_field_value($field_id, $wdk_listing_id);
483
484 if(empty($field_value))$field_empty = TRUE;
485 }
486
487 if($field_empty)
488 {
489 /*
490 echo '<pre>';
491 var_dump($settings['wdk_related_field']);
492 echo '</pre>';
493 */
494
495 $content = ob_get_clean();
496 }
497
498 }
499 }
500
501 }
502
503
504
505
506
507
508