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 / admin / ph-admin-functions.php

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

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