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

399 lines 11.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).'__idlisting'] = esc_html__('Id listing', 'wpdirectorykit');
240 $fields_list [(++$order_i).'__post_id'] = esc_html__('Post Id', 'wpdirectorykit');
241 $fields_list [(++$order_i).'__lat'] = esc_html__('Gps Lat', 'wpdirectorykit');
242 $fields_list [(++$order_i).'__lng'] = esc_html__('Gps Lng', 'wpdirectorykit');
243 $fields_list [(++$order_i).'__date'] = esc_html__('Date', 'wpdirectorykit');
244 $fields_list [(++$order_i).'__date_modified'] = esc_html__('Date Modified', 'wpdirectorykit');
245 $fields_list [(++$order_i).'__post_content'] = esc_html__('WP Content', 'wpdirectorykit');
246 $fields_list [(++$order_i).'__address'] = esc_html__('Address', 'wpdirectorykit');
247 $fields_list [(++$order_i).'__category_id'] = esc_html__('Category', 'wpdirectorykit');
248 $fields_list [(++$order_i).'__location_id'] = esc_html__('Location', 'wpdirectorykit');
249
250 foreach($fields_data as $field)
251 {
252 if(wmvc_show_data('field_type', $field) == 'SECTION') {
253 $fields_list [(++$order_i).'section__'.wmvc_show_data('idfield', $field)] = '-- '.esc_html__('Section', 'wpdirectorykit').' '.wmvc_show_data('field_label', $field).' --';
254 } else {
255 $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).']';
256 }
257 }
258
259 $element->add_control(
260 'wdk_related_field',
261 [
262 'label' => esc_html__('WDK Related FIeld', 'wpdirectorykit'),
263 'type' => Controls_Manager::SELECT,
264 'options' => $fields_list,
265 'default' => '',
266 'separator' => 'after',
267 ]
268 );
269
270 $element->end_controls_section();
271
272 }
273
274 public function _start_element( $element ) {
275 global $wdk_listing_id;
276
277 if(Plugin::$instance->editor->is_edit_mode() || empty($wdk_listing_id))return;
278 //if(!in_array($element->get_name(), $this->supported_elements))return;
279
280 $WMVC = &wdk_get_instance();
281 $WMVC->model('field_m');
282
283 $settings = $element->get_settings_for_display();
284
285 if(isset($settings['wdk_related_field']) && !empty($settings['wdk_related_field']))
286 {
287 $field_id = NULL;
288 $field_value = NULL;
289 $field_empty = FALSE;
290 if(strpos($settings['wdk_related_field'], '__') !== FALSE){
291 $field_id = substr($settings['wdk_related_field'], strpos($settings['wdk_related_field'],'__')+2);
292 }
293
294 if(is_null($field_id))return;
295
296 if(wdk_field_option($field_id, 'field_type') == "SECTION") {
297
298 // get all section fields
299 $sections_data = $WMVC->field_m->get_fields_section();
300
301 if(!isset($sections_data[$field_id]) || !isset($sections_data[$field_id]['fields']))return;
302
303 // check if all subfields are empty
304 $field_empty = true;
305 foreach($sections_data[$field_id]['fields'] as $field) {
306 if(!empty(wdk_field_value(wmvc_show_data('idfield', $field), $wdk_listing_id))){
307 $field_empty = false;
308 break;
309 }
310 }
311 }
312 else
313 {
314 $field_value = wdk_field_value($field_id, $wdk_listing_id);
315
316 if(empty($field_value))$field_empty = TRUE;
317 }
318
319 if($field_empty)
320 {
321 /*
322 echo '<pre>';
323 var_dump($settings['wdk_related_field']);
324 echo '</pre>';
325 */
326
327 ob_start();
328 }
329
330 }
331 }
332
333 public function _end_element( $element ) {
334 global $wdk_listing_id;
335
336 if(Plugin::$instance->editor->is_edit_mode() || empty($wdk_listing_id))return;
337 //if(!in_array($element->get_name(), $this->supported_elements))return;
338
339 $WMVC = &wdk_get_instance();
340 $WMVC->model('field_m');
341
342 $settings = $element->get_settings_for_display();
343
344 if(isset($settings['wdk_related_field']) && !empty($settings['wdk_related_field']))
345 {
346 $field_id = NULL;
347 $field_value = NULL;
348 $field_empty = FALSE;
349 if(strpos($settings['wdk_related_field'], '__') !== FALSE){
350 $field_id = substr($settings['wdk_related_field'], strpos($settings['wdk_related_field'],'__')+2);
351 }
352
353 if(is_null($field_id))return;
354
355 if(wdk_field_option($field_id, 'field_type') == "SECTION") {
356
357 // get all section fields
358 $sections_data = $WMVC->field_m->get_fields_section();
359
360 if(!isset($sections_data[$field_id]) || !isset($sections_data[$field_id]['fields']))return;
361
362 // check if all subfields are empty
363 $field_empty = true;
364 foreach($sections_data[$field_id]['fields'] as $field) {
365 if(!empty(wdk_field_value(wmvc_show_data('idfield', $field), $wdk_listing_id))){
366 $field_empty = false;
367 break;
368 }
369 }
370 }
371 else
372 {
373 $field_value = wdk_field_value($field_id, $wdk_listing_id);
374
375 if(empty($field_value))$field_empty = TRUE;
376 }
377
378 if($field_empty)
379 {
380 /*
381 echo '<pre>';
382 var_dump($settings['wdk_related_field']);
383 echo '</pre>';
384 */
385
386 $content = ob_get_clean();
387 }
388
389 }
390 }
391
392 }
393
394
395
396
397
398
399