PluginProbe
Property Hive / trunk
Property Hive vtrunk
2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 1.4.63 All 259 releases
propertyhive / includes / class-ph-elementor.php

class-ph-elementor.php in Property Hive trunk, at includes/class-ph-elementor.php

447 lines 13.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
7 class PH_Elementor {
8
9 public function __construct()
10 {
11 add_action( 'plugins_loaded', array( $this, 'setup_propertyhive_elementor_functionality' ) );
12 add_action( 'elementor/query/onmarketpropertyquery', array( $this, 'elementor_query_on_market_only' ) );
13 add_action( 'elementor/query/onmarketsalespropertyquery', array( $this, 'elementor_query_on_market_sales_only' ) );
14 add_action( 'elementor/query/onmarketlettingspropertyquery', array( $this, 'elementor_query_on_market_lettings_only' ) );
15 add_action( 'elementor/query/onmarketcommercialpropertyquery', array( $this, 'elementor_query_on_market_commercial_only' ) );
16 add_action( 'elementor/query/featuredpropertyquery', array( $this, 'elementor_query_featured_only' ) );
17 add_filter( 'elementor/query/query_args', array( $this, 'elementor_query_args' ), 10, 2 );
18
19 add_action( 'propertyhive_elementor_widget_property_image_controls', array( $this, 'elementor_widget_property_image_controls' ), 10 );
20 add_action( 'propertyhive_elementor_widget_property_image_render_after', array( $this, 'elementor_widget_property_image_render_after' ), 10, 2 );
21 }
22
23 public function elementor_query_args( $query_vars, $widget )
24 {
25 // Check to see if this is the right Widget.
26 $settings = $widget->get_data( 'settings' );
27
28 if ( isset($settings['post_query_query_id']) && $settings['post_query_query_id'] == 'onmarketpropertyquery' )
29 {
30 $ordering = PH()->query->get_search_results_ordering_args();
31
32 $query_vars['orderby'] = $ordering['orderby'] . ' post_title';
33 $query_vars['order'] = $ordering['order'];
34 if ( isset( $ordering['meta_key'] ) )
35 {
36 $query_vars['meta_key'] = $ordering['meta_key'];
37 }
38 }
39
40 return $query_vars;
41 }
42
43 public function setup_propertyhive_elementor_functionality()
44 {
45 add_filter( 'elementor_pro/utils/get_public_post_types', array( $this, 'register_public_post_type' ) );
46
47 add_action( 'elementor/elements/categories_registered', array( $this, 'add_elementor_widget_category' ) );
48
49 add_filter( 'elementor/image_size/get_attachment_image_html', array( $this, 'portfolio_use_property_image_url'), 10, 4 );
50
51 add_action( 'elementor/preview/enqueue_scripts', array( 'PH_Frontend_Scripts', 'load_scripts' ) );
52 add_action( 'elementor/preview/enqueue_scripts', array( $this, 'load_elementor_scripts' ) );
53
54 if ( did_action( 'elementor/loaded' ) )
55 {
56 // Widgets
57 add_action( 'init', array( $this, 'register_widgets' ) );
58 }
59 }
60
61 public function elementor_query_on_market_only( $query )
62 {
63 PH()->query->property_query( $query );
64
65 // Set the custom post type
66 $query->set( 'post_type', [ 'property' ] );
67
68 // ensure a department is set as not set by default from property_query
69 $meta_query = $query->get('meta_query');
70
71 $new_meta_query = $meta_query;
72 $department_found = false;
73 foreach ( $meta_query as $key => $value )
74 {
75 if ( isset($value['key']) && $value['key'] == '_department' )
76 {
77 $department_found = true;
78 break;
79 }
80 }
81
82 if ( !$department_found )
83 {
84 $new_meta_query[] = array(
85 'key' => '_department',
86 'value' => get_option( 'propertyhive_primary_department', 'residential-sales' )
87 );
88 }
89
90 $query->set( 'meta_query', $new_meta_query );
91 }
92
93 public function elementor_query_on_market_sales_only( $query )
94 {
95 $original_department = isset($_REQUEST['department']) ? $_REQUEST['department'] : '';
96
97 $_GET['department'] = 'residential-sales';
98 $_REQUEST['department'] = 'residential-sales';
99
100 PH()->query->property_query( $query );
101
102 $_GET['department'] = $original_department;
103 $_REQUEST['department'] = $original_department;
104
105 // Set the custom post type
106 $query->set( 'post_type', [ 'property' ] );
107
108 // ensure a department is set as not set by default from property_query
109 $new_meta_query = $this->remove_department_from_query( $query );
110
111 $new_meta_query[] = array(
112 'key' => '_department',
113 'value' => 'residential-sales'
114 );
115
116 $query->set( 'meta_query', $new_meta_query );
117 }
118
119 public function elementor_query_on_market_lettings_only( $query )
120 {
121 $original_department = isset($_REQUEST['department']) ? $_REQUEST['department'] : '';
122
123 $_GET['department'] = 'residential-lettings';
124 $_REQUEST['department'] = 'residential-lettings';
125
126 PH()->query->property_query( $query );
127
128 $_GET['department'] = $original_department;
129 $_REQUEST['department'] = $original_department;
130
131 // Set the custom post type
132 $query->set( 'post_type', [ 'property' ] );
133
134 // ensure a department is set as not set by default from property_query
135 $new_meta_query = $this->remove_department_from_query( $query );
136
137 $new_meta_query[] = array(
138 'key' => '_department',
139 'value' => 'residential-lettings'
140 );
141
142 $query->set( 'meta_query', $new_meta_query );
143 }
144
145 public function elementor_query_on_market_commercial_only( $query )
146 {
147 $original_department = isset($_REQUEST['department']) ? $_REQUEST['department'] : '';
148
149 $_GET['department'] = 'commercial';
150 $_REQUEST['department'] = 'commercial';
151
152 PH()->query->property_query( $query );
153
154 $_GET['department'] = $original_department;
155 $_REQUEST['department'] = $original_department;
156
157 // Set the custom post type
158 $query->set( 'post_type', [ 'property' ] );
159
160 // ensure a department is set as not set by default from property_query
161 $new_meta_query = $this->remove_department_from_query( $query );
162
163 $new_meta_query[] = array(
164 'key' => '_department',
165 'value' => 'commercial'
166 );
167
168 $query->set( 'meta_query', $new_meta_query );
169 }
170
171 public function elementor_query_featured_only( $query )
172 {
173 $original_orderby = $query->get( 'orderby' );
174 $original_order = $query->get( 'order' );
175
176 PH()->query->property_query( $query );
177
178 // Set the custom post type
179 $query->set( 'post_type', [ 'property' ] );
180
181 $meta_query = $this->remove_department_from_query( $query );
182
183 $new_meta_query = $meta_query;
184
185 $new_meta_query[] = array(
186 'key' => '_featured',
187 'value' => 'yes'
188 );
189
190 $query->set( 'meta_query', $new_meta_query );
191
192 $query->set( 'orderby', $original_orderby );
193 $query->set( 'order', $original_order );
194 }
195
196 private function remove_department_from_query( $query )
197 {
198 $new_meta_query = array();
199
200 $meta_query = $query->get('meta_query');
201
202 foreach ( $meta_query as $key => $value )
203 {
204 if ( isset($value['key']) && $value['key'] == '_department' )
205 {
206 // remove department
207 }
208 else
209 {
210 $new_meta_query[$key] = $value;
211 }
212 }
213
214 return $new_meta_query;
215 }
216
217 public function load_elementor_scripts()
218 {
219 $suffix = '';
220 $assets_path = str_replace( array( 'http:', 'https:' ), '', PH()->plugin_url() ) . '/assets/';
221
222 wp_enqueue_script( 'propertyhive_fancybox' );
223 wp_enqueue_style( 'propertyhive_fancybox_css' );
224
225 wp_enqueue_script( 'flexslider', $assets_path . 'js/flexslider/jquery.flexslider' . $suffix . '.js', array( 'jquery' ), '2.2.2', true );
226 wp_enqueue_script( 'flexslider-init', $assets_path . 'js/flexslider/jquery.flexslider.init' . $suffix . '.js', array( 'jquery','flexslider' ), PH_VERSION, true );
227 wp_enqueue_style( 'flexslider_css', $assets_path . 'css/flexslider.css' );
228
229 $api_key = get_option('propertyhive_google_maps_api_key');
230 wp_register_script('googlemaps', '//maps.googleapis.com/maps/api/js?' . ( ( $api_key != '' && $api_key !== FALSE ) ? 'key=' . $api_key : '' ), false, '3');
231 wp_enqueue_script('googlemaps');
232
233 wp_enqueue_script( 'propertyhive_elementor', $assets_path . 'js/elementor/elementor.js', array( 'jquery','flexslider' ), PH_VERSION, true );
234 }
235
236 public function add_elementor_widget_category( $elements_manager )
237 {
238 $elements_manager->add_category(
239 'property-hive',
240 [
241 'title' => __( 'Property Hive', 'propertyhive' ),
242 'icon' => 'fa fa-home',
243 ]
244 );
245 }
246
247 public function register_widgets()
248 {
249 $widgets = array(
250 'Property Search Form',
251 'Property Tabbed Details',
252 'Property Images',
253 'Property Image',
254 'Property Gallery',
255 'Property Address Name Number',
256 'Property Address Street',
257 'Property Address Line 2',
258 'Property Address Town City',
259 'Property Address County',
260 'Property Address Postcode',
261 'Property Address Full',
262 );
263
264 if ( taxonomy_exists('location') )
265 {
266 $args = array(
267 'taxonomy' => 'location',
268 'hide_empty' => false,
269 'parent' => 0,
270 'number' => 1,
271 'fields' => 'ids',
272 );
273 $terms = get_terms( $args );
274
275 if ( !empty( $terms ) && !is_wp_error( $terms ) )
276 {
277 $widgets[] = 'Property Location';
278 }
279 }
280
281 $widgets = array_merge( $widgets, array(
282 'Property Price',
283 'Property Price Qualifier',
284 'Property Features',
285 'Property Summary Description',
286 'Property Full Description',
287 'Property Actions',
288 'Property Meta',
289 'Property Availability',
290 'Property Type',
291 'Property Bedrooms',
292 'Property Bathrooms',
293 'Property Reception Rooms',
294 'Property Reference Number',
295 'Property Floor Area',
296 'Property Tenure',
297 'Property Council Tax Band',
298 'Property Let Available Date',
299 'Property Deposit',
300 'Property Furnished',
301 'Property Marketing Flag',
302 'Property Map',
303 'Property Map Link',
304 'Property Street View',
305 'Property Floorplans',
306 'Property Floorplans Link',
307 'Property EPCs',
308 'Property EPCs Link',
309 'Property Enquiry Form',
310 'Property Enquiry Form Link',
311 'Property Brochures Link',
312 'Property Embedded Virtual Tours',
313 'Property Virtual Tours Link',
314 'Property Office Name',
315 'Property Office Telephone Number',
316 'Property Office Email Address',
317 'Property Office Address',
318 'Property Negotiator Name',
319 'Property Negotiator Telephone Number',
320 'Property Negotiator Email Address',
321 'Property Negotiator Photo',
322 'Property Not On Market Message',
323 'Back To Search',
324 'Property Search Result Count',
325 'Property Search Order',
326 ) );
327
328 $current_settings = get_option( 'propertyhive_template_assistant', array() );
329
330 $custom_fields = ( (isset($current_settings['custom_fields'])) ? $current_settings['custom_fields'] : array() );
331
332 foreach ( $custom_fields as $custom_field )
333 {
334 if ( substr($custom_field['meta_box'], 0, 9) == 'property_' || substr($custom_field['meta_box'], 0, 7) == 'office_' )
335 {
336 $widgets[] = 'Property Additional Field';
337 break;
338 }
339 }
340
341 $widgets = apply_filters( 'propertyhive_elementor_widgets', $widgets );
342
343 foreach ( $widgets as $widget )
344 {
345 $widget_dir = 'elementor-widgets';
346 $widget_dir = apply_filters( 'propertyhive_elementor_widget_directory', dirname(__FILE__) . "/" . $widget_dir, $widget );
347 if ( file_exists( $widget_dir . "/" . sanitize_title($widget) . ".php" ) )
348 {
349 require_once( $widget_dir . "/" . sanitize_title($widget) . ".php" );
350 $class_name = '\Elementor_' . str_replace(" ", "_", $widget) . '_Widget';
351 \Elementor\Plugin::instance()->widgets_manager->register( new $class_name() );
352 }
353 }
354 }
355
356 public function register_public_post_type( $post_types ) {
357
358 if ( isset($post_types['property']) )
359 {
360 return $post_types;
361 }
362
363 $post_types['property'] = __( 'Property', 'propertyhive' );
364
365 return $post_types;
366 }
367
368 public function portfolio_use_property_image_url( $html, $settings, $image_size_key, $image_key )
369 {
370 global $post;
371
372 if ( !isset($settings['posts_post_type']) || ( isset($settings['posts_post_type']) && $settings['posts_post_type'] != 'property' ) )
373 {
374 return $html;
375 }
376
377 // We're viewing a property
378
379 if ( get_option('propertyhive_images_stored_as', '') != 'urls' )
380 {
381 return $html;
382 }
383
384 // Images are stored as URLs
385
386 $images = get_post_meta( $post->ID, '_photo_urls', TRUE );
387
388 if ( empty($images) )
389 {
390 return $html;
391 }
392
393 $image_src = $images[0]['url'];
394
395 $image_class = ! empty( $settings['hover_animation'] ) ? 'elementor-animation-' . $settings['hover_animation'] : '';
396 $image_class_html = ! empty( $image_class ) ? ' class="' . $image_class . '"' : '';
397
398 $html = sprintf( '<img src="%s" title="" alt=""%s />', esc_attr( $image_src ), $image_class_html );
399 return $html;
400 }
401
402 public function elementor_widget_property_image_controls( $widget )
403 {
404 $widget->add_control(
405 'show_flag',
406 [
407 'label' => __( 'Show Availability Flag', 'propertyhive' ),
408 'type' => \Elementor\Controls_Manager::SWITCHER,
409 'label_on' => __( 'Yes', 'propertyhive' ),
410 'label_off' => __( 'No', 'propertyhive' ),
411 'return_value' => 'yes',
412 'default' => '',
413 ]
414 );
415
416 $widget->add_control(
417 'flag_note',
418 [
419 'label' => __( '', 'propertyhive' ),
420 'type' => \Elementor\Controls_Manager::RAW_HTML,
421 'raw' => __( 'The flag shown will take its colour and position settings from the <a href="' . admin_url('admin.php?page=ph-settings&tab=template-assistant&section=flags') . '" target="_blank">Template Assistant Flags</a> settings area', 'propertyhive' ),
422 'condition' => [
423 'show_flag' => 'yes',
424 ],
425 ]
426 );
427 }
428
429 public function elementor_widget_property_image_render_after( $settings, $property )
430 {
431 global $property;
432
433 if ( isset($settings['show_flag']) && $settings['show_flag'] == 'yes' )
434 {
435 $flag = propertyhive_get_flag();
436
437 if ( $flag != '' )
438 {
439 $current_settings = get_option( 'propertyhive_template_assistant', array() );
440
441 echo '<div class="flag flag-' . sanitize_title($flag) . '" style="position:absolute; text-transform:uppercase; font-size:13px; box-sizing:border-box; padding:7px 20px; ' . $current_settings['flag_position'] . '; color:' . $current_settings['flag_text_color'] . '; background:' . $current_settings['flag_bg_color'] . ';">' . $flag . '</div>';
442 }
443 }
444 }
445 }
446
447 new PH_Elementor();