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