PluginProbe
Property Hive / 2.3.0
Property Hive v2.3.0
2.3.0 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 All 260 releases
propertyhive / includes / admin / ph-admin-functions.php

ph-admin-functions.php in Property Hive 2.3.0, at includes/admin/ph-admin-functions.php

338 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:set WordPress.Security.ValidatedSanitizedInput customSanitizingFunctions[] ph_clean
3 // ph_clean() recursively sanitizes text; presence, shape and unslashing checks remain separate.
4
5 /**
6 * PropertyHive Admin Functions
7 *
8 * @author PropertyHive
9 * @category Core
10 * @package PropertyHive/Admin/Functions
11 * @version 1.0.0
12 */
13
14 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
15
16 /**
17 * Get all PropertyHive screen ids
18 *
19 * @return array
20 */
21 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Legacy public global helper ph_get_screen_ids; the established callable name is part of the plugin/extension API and must remain stable.
22 function ph_get_screen_ids() {
23 return apply_filters( 'propertyhive_screen_ids', array(
24 'edit-property',
25 'property',
26 'edit-contact',
27 'contact',
28 'edit-enquiry',
29 'enquiry',
30 'edit-appraisal',
31 'appraisal',
32 'edit-viewing',
33 'viewing',
34 'edit-offer',
35 'offer',
36 'edit-sale',
37 'sale',
38 'tenancy',
39 'edit-key_date',
40 'dashboard_page_ph-installed',
41 'property-hive_page_ph-settings',
42 'admin_page_ph-generate-applicant-list',
43 'admin_page_ph-merge-duplicate-contacts',
44 ) );
45 }
46
47 /**
48 * Create a page and store the ID in an option.
49 *
50 * @access public
51 * @param mixed $slug Slug for the new page
52 * @param mixed $option Option name to store the page's ID
53 * @param string $page_title (default: '') Title for the new page
54 * @param string $page_content (default: '') Content for the new page
55 * @param int $post_parent (default: 0) Parent for the new page
56 * @return int page ID
57 */
58 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Legacy public global helper ph_create_page; the established callable name is part of the plugin/extension API and must remain stable.
59 function ph_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
60 global $wpdb;
61
62 /*$option_value = get_option( $option );
63
64 if ( $option_value > 0 && get_post( $option_value ) )
65 return -1;
66
67 $page_found = null;
68
69 if ( strlen( $page_content ) > 0 ) {
70 // Search for an existing page with the specified page content (typically a shortcode)
71 $page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->posts . " WHERE post_type='page' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
72 } else {
73 // Search for an existing page with the specified page slug
74 $page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->posts . " WHERE post_type='page' AND post_name = %s LIMIT 1;", $slug ) );
75 }
76
77 if ( $page_found ) {
78 if ( ! $option_value )
79 update_option( $option, $page_found );
80
81 return $page_found;
82 }
83
84 $page_data = array(
85 'post_status' => 'publish',
86 'post_type' => 'page',
87 'post_author' => 1,
88 'post_name' => $slug,
89 'post_title' => $page_title,
90 'post_content' => $page_content,
91 'post_parent' => $post_parent,
92 'comment_status' => 'closed'
93 );
94 $page_id = wp_insert_post( $page_data );
95
96 if ( $option )
97 update_option( $option, $page_id );
98
99 return $page_id;*/
100 }
101
102 /**
103 * Output admin fields.
104 *
105 * Loops though the PropertyHive options array and outputs each field.
106 *
107 * @param array $options Opens array to output
108 */
109 function propertyhive_admin_fields( $options ) {
110 if ( ! class_exists( 'PH_Admin_Settings' ) )
111 include 'class-ph-admin-settings.php';
112
113 PH_Admin_Settings::output_fields( $options );
114 }
115
116 /**
117 * Update all settings which are passed.
118 *
119 * @access public
120 * @param array $options
121 * @return void
122 */
123 function propertyhive_update_options( $options ) {
124 if ( ! class_exists( 'PH_Admin_Settings' ) )
125 include 'class-ph-admin-settings.php';
126
127 PH_Admin_Settings::save_fields( $options );
128 }
129
130 /**
131 * Get a setting from the settings API.
132 *
133 * @param mixed $option
134 * @return string
135 */
136 function propertyhive_settings_get_option( $option_name, $default = '' ) {
137 if ( ! class_exists( 'PH_Admin_Settings' ) )
138 include 'class-ph-admin-settings.php';
139
140 return PH_Admin_Settings::get_option( $option_name, $default );
141 }
142
143 /**
144 * Generate CSS from the less file when changing colours.
145 *
146 * @access public
147 * @return void
148 */
149 function propertyhive_compile_less_styles() {
150 /*global $propertyhive;
151
152 $colors = array_map( 'esc_attr', (array) get_option( 'propertyhive_frontend_css_colors' ) );
153 $base_file = PH()->plugin_path() . '/assets/css/propertyhive-base.less';
154 $less_file = PH()->plugin_path() . '/assets/css/propertyhive.less';
155 $css_file = PH()->plugin_path() . '/assets/css/propertyhive.css';
156
157 // Write less file
158 if ( is_writable( $base_file ) && is_writable( $css_file ) ) {
159
160 // Colours changed - recompile less
161 if ( ! class_exists( 'lessc' ) )
162 include_once( PH()->plugin_path() . '/includes/libraries/class-lessc.php' );
163 if ( ! class_exists( 'cssmin' ) )
164 include_once( PH()->plugin_path() . '/includes/libraries/class-cssmin.php' );
165
166 try {
167 // Set default if colours not set
168 if ( ! $colors['primary'] ) $colors['primary'] = '#ad74a2';
169 if ( ! $colors['secondary'] ) $colors['secondary'] = '#f7f6f7';
170 if ( ! $colors['highlight'] ) $colors['highlight'] = '#85ad74';
171 if ( ! $colors['content_bg'] ) $colors['content_bg'] = '#ffffff';
172 if ( ! $colors['subtext'] ) $colors['subtext'] = '#777777';
173
174 // Write new color to base file
175 $color_rules = "
176 @primary: " . $colors['primary'] . ";
177 @primarytext: " . ph_light_or_dark( $colors['primary'], 'desaturate(darken(@primary,50%),18%)', 'desaturate(lighten(@primary,50%),18%)' ) . ";
178
179 @secondary: " . $colors['secondary'] . ";
180 @secondarytext: " . ph_light_or_dark( $colors['secondary'], 'desaturate(darken(@secondary,60%),18%)', 'desaturate(lighten(@secondary,60%),18%)' ) . ";
181
182 @highlight: " . $colors['highlight'] . ";
183 @highlightext: " . ph_light_or_dark( $colors['highlight'], 'desaturate(darken(@highlight,60%),18%)', 'desaturate(lighten(@highlight,60%),18%)' ) . ";
184
185 @contentbg: " . $colors['content_bg'] . ";
186
187 @subtext: " . $colors['subtext'] . ";
188 ";
189
190 file_put_contents( $base_file, $color_rules );
191
192 $less = new lessc;
193 $compiled_css = $less->compileFile( $less_file );
194 $compiled_css = CssMin::minify( $compiled_css );
195
196 if ( $compiled_css )
197 file_put_contents( $css_file, $compiled_css );
198
199 } catch ( exception $ex ) {
200 wp_die( __( 'Could not compile propertyhive.less:', 'propertyhive' ) . ' ' . $ex->getMessage() );
201 }
202 }*/
203 }
204
205 function propertyhive_is_location_in_address( $property, $location )
206 {
207 $address_keywords = array( $location );
208 if ( strpos( $location, ' ' ) !== FALSE )
209 {
210 $address_keywords[] = str_replace(" ", "-", ph_clean($location));
211 }
212 if ( strpos( $location, '-' ) !== FALSE )
213 {
214 $address_keywords[] = str_replace("-", " ", ph_clean($location));
215 }
216
217 if ( strpos( $location, '.' ) !== FALSE )
218 {
219 $address_keywords[] = str_replace(".", "", ph_clean($location));
220 }
221 if ( stripos( $location, 'st ' ) !== FALSE )
222 {
223 $address_keywords[] = str_ireplace("st ", "st. ", ph_clean($location));
224 }
225
226 $location_address_fields = array(
227 '_address_street',
228 '_address_two',
229 '_address_three',
230 '_address_four',
231 '_address_postcode'
232 );
233 $location_address_fields = apply_filters( 'propertyhive_address_fields_to_query', $location_address_fields );
234
235 $address_keyword_compare = get_option( 'propertyhive_address_keyword_compare', '=' );
236 if ( $address_keyword_compare == 'polygon' )
237 {
238 $address_keyword_compare = apply_filters('propertyhive_property_match_address_keyword_compare', '=');
239 }
240
241 foreach ( $address_keywords as $address_keyword )
242 {
243 foreach ( $location_address_fields as $address_field )
244 {
245 if ( $address_field == '_address_postcode' ) { continue; } // ignore postcode as that is handled differently afterwards
246
247 if (
248 ( $address_keyword_compare == '=' && strcasecmp($address_keyword, $property->{$address_field}) == 0 )
249 ||
250 ( $address_keyword_compare == 'LIKE' && stripos($property->{$address_field}, $address_keyword) !== false )
251 )
252 {
253 return true;
254 }
255 }
256 }
257 if ( in_array('_address_postcode', $location_address_fields) )
258 {
259 if ( strlen($location) <= 4 )
260 {
261 // if location is just the first part of postcode, check if it is present at the start of the postcode
262 if ( strcasecmp($address_keyword, substr($property->_address_postcode, 0, strlen($address_keyword))) == 0 )
263 {
264 return true;
265 }
266 }
267 else
268 {
269 $postcode = ph_clean($location);
270 $postcodes = array(strtolower($postcode));
271
272 if ( preg_match('#^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW])[0-9][ABD-HJLNP-UW-Z]{2})$#i', $postcode) )
273 {
274 // UK postcode found with no space
275
276 if ( strlen($postcode) == 5 )
277 {
278 $first_part = substr($postcode, 0, 2);
279 $last_part = substr($postcode, 2, 3);
280
281 $postcodes[] = strtolower($first_part . ' ' . $last_part);
282 }
283 elseif ( strlen($postcode) == 6 )
284 {
285 $first_part = substr($postcode, 0, 3);
286 $last_part = substr($postcode, 3, 3);
287
288 $postcodes[] = strtolower($first_part . ' ' . $last_part);
289 }
290 elseif ( strlen($postcode) == 7 )
291 {
292 $first_part = substr($postcode, 0, 4);
293 $last_part = substr($postcode, 4, 3);
294
295 $postcodes[] = strtolower($first_part . ' ' . $last_part);
296 }
297 }
298
299 if ( in_array(strtolower($property->_address_postcode), $postcodes) )
300 {
301 return true;
302 }
303 }
304 }
305
306 return false;
307 }
308
309 function propertyhive_human_time_difference( $timestamp )
310 {
311 $now = time();
312
313 if ( !$timestamp )
314 {
315 return __( 'never', 'propertyhive' );
316 }
317
318 if ( $now - $timestamp > 0 )
319 {
320 return sprintf(
321 /* translators: %s: human-readable time difference (e.g. "5 minutes") */
322 __( '%s ago', 'propertyhive' ),
323 human_time_diff( $timestamp, $now )
324 );
325 }
326 elseif ( $now - $timestamp < 0 )
327 {
328 return sprintf(
329 /* translators: %s: human-readable time difference (e.g. "5 minutes") */
330 __( 'in %s', 'propertyhive' ),
331 human_time_diff( $timestamp, $now )
332 );
333 }
334 else
335 {
336 return __( 'now', 'propertyhive' );
337 }
338 }