PluginProbe
WP Directory Kit / 1.2.3
WP Directory Kit v1.2.3
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.3, at elementor-extensions/class-hider.php

453 lines 14.7 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).'__have_sublistings'] = esc_html__('Listing have sublistings', 'wpdirectorykit');
240 $fields_list [(++$order_i).'__have_calendar'] = esc_html__('Listing have calendar', 'wpdirectorykit');
241 $fields_list [(++$order_i).'__havent_calendar'] = esc_html__('Listing not have calendar', 'wpdirectorykit');
242 $fields_list [(++$order_i).'__idlisting'] = esc_html__('Id listing', 'wpdirectorykit');
243 $fields_list [(++$order_i).'__post_id'] = esc_html__('Post Id', 'wpdirectorykit');
244 $fields_list [(++$order_i).'__lat'] = esc_html__('Gps Lat', 'wpdirectorykit');
245 $fields_list [(++$order_i).'__lng'] = esc_html__('Gps Lng', 'wpdirectorykit');
246 $fields_list [(++$order_i).'__date'] = esc_html__('Date', 'wpdirectorykit');
247 $fields_list [(++$order_i).'__date_modified'] = esc_html__('Date Modified', 'wpdirectorykit');
248 $fields_list [(++$order_i).'__post_content'] = esc_html__('WP Content', 'wpdirectorykit');
249 $fields_list [(++$order_i).'__address'] = esc_html__('Address', 'wpdirectorykit');
250 $fields_list [(++$order_i).'__category_id'] = esc_html__('Category', 'wpdirectorykit');
251 $fields_list [(++$order_i).'__location_id'] = esc_html__('Location', 'wpdirectorykit');
252
253 foreach($fields_data as $field)
254 {
255 if(wmvc_show_data('field_type', $field) == 'SECTION') {
256 $fields_list [(++$order_i).'section__'.wmvc_show_data('idfield', $field)] = '-- '.esc_html__('Section', 'wpdirectorykit').' '.wmvc_show_data('field_label', $field).' --';
257 } else {
258 $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).']';
259 }
260 }
261
262 $element->add_control(
263 'wdk_related_field',
264 [
265 'label' => esc_html__('WDK Related FIeld', 'wpdirectorykit'),
266 'type' => Controls_Manager::SELECT,
267 'options' => $fields_list,
268 'default' => '',
269 'separator' => 'after',
270 ]
271 );
272
273 $element->end_controls_section();
274
275 }
276
277 public function _start_element( $element ) {
278 global $wdk_listing_id;
279
280 if(Plugin::$instance->editor->is_edit_mode() || empty($wdk_listing_id))return;
281 //if(!in_array($element->get_name(), $this->supported_elements))return;
282
283 $WMVC = &wdk_get_instance();
284 $WMVC->model('field_m');
285
286 $settings = $element->get_settings_for_display();
287
288 if(isset($settings['wdk_related_field']) && !empty($settings['wdk_related_field']))
289 {
290 $field_id = NULL;
291 $field_value = NULL;
292 $field_empty = FALSE;
293 if(strpos($settings['wdk_related_field'], '__') !== FALSE){
294 $field_id = substr($settings['wdk_related_field'], strpos($settings['wdk_related_field'],'__')+2);
295 }
296
297 if(is_null($field_id))return;
298
299 if($field_id == 'havent_calendar' || $field_id == 'have_calendar') {
300 if(function_exists('run_wdk_bookings')) {
301 global $Winter_MVC_wdk_bookings;
302 $Winter_MVC_wdk_bookings->model('calendar_m');
303 $calendar = $Winter_MVC_wdk_bookings->calendar_m->get_by(array('post_id' => $wdk_listing_id));
304 if($field_id == 'havent_calendar') {
305 if($calendar){
306 $field_empty = true;
307 }
308 } else if($field_id == 'have_calendar') {
309 if(!$calendar){
310 $field_empty = true;
311 }
312 }
313 }
314 } elseif($field_id == 'have_sublistings') {
315
316 $field_empty = true;
317 if(!empty(wdk_field_value('listing_related_ids', $wdk_listing_id))){
318 foreach (explode(',',wdk_field_value('listing_related_ids', $wdk_listing_id)) as $key => $child_idlisting) {
319 if(!wdk_field_value('is_activated', $child_idlisting) || !wdk_field_value('is_approved', $child_idlisting)) continue;
320
321 $field_empty = false;
322 break;
323 }
324 }
325 } elseif(wdk_field_option($field_id, 'field_type') == "SECTION") {
326
327 // get all section fields
328 $sections_data = $WMVC->field_m->get_fields_section();
329
330 if(!isset($sections_data[$field_id]) || !isset($sections_data[$field_id]['fields']))return;
331
332 // check if all subfields are empty
333 $field_empty = true;
334 foreach($sections_data[$field_id]['fields'] as $field) {
335 if(!empty(wdk_field_value(wmvc_show_data('idfield', $field), $wdk_listing_id))){
336 $field_empty = false;
337 break;
338 }
339 }
340 }
341 else
342 {
343 $field_value = wdk_field_value($field_id, $wdk_listing_id);
344
345 if(empty($field_value))$field_empty = TRUE;
346 }
347
348 if($field_empty)
349 {
350 /*
351 echo '<pre>';
352 var_dump($settings['wdk_related_field']);
353 echo '</pre>';
354 */
355
356 ob_start();
357 }
358
359 }
360 }
361
362 public function _end_element( $element ) {
363 global $wdk_listing_id;
364
365 if(Plugin::$instance->editor->is_edit_mode() || empty($wdk_listing_id))return;
366 //if(!in_array($element->get_name(), $this->supported_elements))return;
367
368 $WMVC = &wdk_get_instance();
369 $WMVC->model('field_m');
370
371 $settings = $element->get_settings_for_display();
372
373 if(isset($settings['wdk_related_field']) && !empty($settings['wdk_related_field']))
374 {
375 $field_id = NULL;
376 $field_value = NULL;
377 $field_empty = FALSE;
378 if(strpos($settings['wdk_related_field'], '__') !== FALSE){
379 $field_id = substr($settings['wdk_related_field'], strpos($settings['wdk_related_field'],'__')+2);
380 }
381
382 if(is_null($field_id))return;
383 if($field_id == 'havent_calendar' || $field_id == 'have_calendar') {
384 if(function_exists('run_wdk_bookings')) {
385 global $Winter_MVC_wdk_bookings;
386 $Winter_MVC_wdk_bookings->model('calendar_m');
387 $calendar = $Winter_MVC_wdk_bookings->calendar_m->get_by(array('post_id' => $wdk_listing_id));
388 if($field_id == 'havent_calendar') {
389 if($calendar){
390 $field_empty = true;
391 }
392 } else if($field_id == 'have_calendar') {
393 if(!$calendar){
394 $field_empty = true;
395 }
396 }
397 }
398 } elseif($field_id == 'have_sublistings') {
399
400 $field_empty = true;
401 if(!empty(wdk_field_value('listing_related_ids', $wdk_listing_id))){
402 foreach (explode(',',wdk_field_value('listing_related_ids', $wdk_listing_id)) as $key => $child_idlisting) {
403 if(!wdk_field_value('is_activated', $child_idlisting) || !wdk_field_value('is_approved', $child_idlisting)) continue;
404
405 $field_empty = false;
406 break;
407 }
408 }
409 } elseif(wdk_field_option($field_id, 'field_type') == "SECTION") {
410
411 // get all section fields
412 $sections_data = $WMVC->field_m->get_fields_section();
413
414 if(!isset($sections_data[$field_id]) || !isset($sections_data[$field_id]['fields']))return;
415
416 // check if all subfields are empty
417 $field_empty = true;
418 foreach($sections_data[$field_id]['fields'] as $field) {
419 if(!empty(wdk_field_value(wmvc_show_data('idfield', $field), $wdk_listing_id))){
420 $field_empty = false;
421 break;
422 }
423 }
424 }
425 else
426 {
427 $field_value = wdk_field_value($field_id, $wdk_listing_id);
428
429 if(empty($field_value))$field_empty = TRUE;
430 }
431
432 if($field_empty)
433 {
434 /*
435 echo '<pre>';
436 var_dump($settings['wdk_related_field']);
437 echo '</pre>';
438 */
439
440 $content = ob_get_clean();
441 }
442
443 }
444 }
445
446 }
447
448
449
450
451
452
453