| 1 |
<?php |
| 2 |
/** |
| 3 |
* PropertyHive Core Functions |
| 4 |
* |
| 5 |
* General core functions available on both the front-end and admin. |
| 6 |
* |
| 7 |
* @author PropertyHive |
| 8 |
* @category Core |
| 9 |
* @package PropertyHive/Functions |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
// Include core functions |
| 16 |
include( 'ph-formatting-functions.php' ); |
| 17 |
include( 'ph-conditional-functions.php' ); |
| 18 |
include( 'ph-term-functions.php' ); |
| 19 |
include( 'ph-page-functions.php' ); |
| 20 |
include( 'ph-property-functions.php' ); |
| 21 |
|
| 22 |
/** |
| 23 |
* Get template part (for templates like the single-property). |
| 24 |
* |
| 25 |
* @access public |
| 26 |
* @param mixed $slug |
| 27 |
* @param string $name (default: '') |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
function ph_get_template_part( $slug, $name = '' ) { |
| 31 |
$template = ''; |
| 32 |
|
| 33 |
// Look in yourtheme/slug-name.php and yourtheme/propertyhive/slug-name.php |
| 34 |
if ( $name ) { |
| 35 |
$template = locate_template( array( "{$slug}-{$name}.php", PH()->template_path() . "{$slug}-{$name}.php" ) ); |
| 36 |
} |
| 37 |
|
| 38 |
// Get default slug-name.php |
| 39 |
if ( ! $template && $name && file_exists( PH()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) { |
| 40 |
$template = PH()->plugin_path() . "/templates/{$slug}-{$name}.php"; |
| 41 |
} |
| 42 |
|
| 43 |
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/propertyhive/slug.php |
| 44 |
if ( ! $template ) { |
| 45 |
$template = locate_template( array( "{$slug}.php", PH()->template_path() . "{$slug}.php" ) ); |
| 46 |
} |
| 47 |
|
| 48 |
// Allow 3rd party plugin filter template file from their plugin |
| 49 |
$template = apply_filters( 'ph_get_template_part', $template, $slug, $name ); |
| 50 |
|
| 51 |
if ( $template ) { |
| 52 |
load_template( $template, false ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get other templates (e.g. property images) passing attributes and including the file. |
| 58 |
* |
| 59 |
* @access public |
| 60 |
* @param string $template_name |
| 61 |
* @param array $args (default: array()) |
| 62 |
* @param string $template_path (default: '') |
| 63 |
* @param string $default_path (default: '') |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
function ph_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) { |
| 67 |
if ( $args && is_array( $args ) ) { |
| 68 |
extract( $args ); |
| 69 |
} |
| 70 |
|
| 71 |
$located = ph_locate_template( $template_name, $template_path, $default_path ); |
| 72 |
|
| 73 |
if ( ! file_exists( $located ) ) { |
| 74 |
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' ); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
do_action( 'propertyhive_before_template_part', $template_name, $template_path, $located, $args ); |
| 79 |
|
| 80 |
include( $located ); |
| 81 |
|
| 82 |
do_action( 'propertyhive_after_template_part', $template_name, $template_path, $located, $args ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Locate a template and return the path for inclusion. |
| 87 |
* |
| 88 |
* This is the load order: |
| 89 |
* |
| 90 |
* yourtheme / $template_path / $template_name |
| 91 |
* yourtheme / $template_name |
| 92 |
* $default_path / $template_name |
| 93 |
* |
| 94 |
* @access public |
| 95 |
* @param string $template_name |
| 96 |
* @param string $template_path (default: '') |
| 97 |
* @param string $default_path (default: '') |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
function ph_locate_template( $template_name, $template_path = '', $default_path = '' ) { |
| 101 |
if ( ! $template_path ) { |
| 102 |
$template_path = PH()->template_path(); |
| 103 |
} |
| 104 |
|
| 105 |
if ( ! $default_path ) { |
| 106 |
$default_path = PH()->plugin_path() . '/templates/'; |
| 107 |
} |
| 108 |
|
| 109 |
// Look within passed path within the theme - this is priority |
| 110 |
$template = locate_template( |
| 111 |
array( |
| 112 |
trailingslashit( $template_path ) . $template_name, |
| 113 |
$template_name |
| 114 |
) |
| 115 |
); |
| 116 |
|
| 117 |
// Get default template |
| 118 |
if ( ! $template ) { |
| 119 |
$template = $default_path . $template_name; |
| 120 |
} |
| 121 |
|
| 122 |
// Return what we found |
| 123 |
return apply_filters('propertyhive_locate_template', $template, $template_name, $template_path); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Set a cookie - wrapper for setcookie using WP constants |
| 128 |
* |
| 129 |
* @param string $name Name of the cookie being set |
| 130 |
* @param string $value Value of the cookie |
| 131 |
* @param integer $expire Expiry of the cookie |
| 132 |
* @param string $secure Whether the cookie should be served only over https |
| 133 |
*/ |
| 134 |
function ph_setcookie( $name, $value, $expire = 0, $secure = false ) { |
| 135 |
if ( ! headers_sent() ) { |
| 136 |
return setcookie( $name, $value, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure ); |
| 137 |
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 138 |
trigger_error( "Cookie cannot be set - headers already sent", E_USER_NOTICE ); |
| 139 |
} |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get an image size. |
| 145 |
* |
| 146 |
* @param mixed $image_size |
| 147 |
* @return array |
| 148 |
*/ |
| 149 |
function ph_get_image_size( $image_size ) { |
| 150 |
if ( is_array( $image_size ) ) { |
| 151 |
$width = isset( $image_size[0] ) ? $image_size[0] : '300'; |
| 152 |
$height = isset( $image_size[1] ) ? $image_size[1] : '300'; |
| 153 |
$crop = isset( $image_size[2] ) ? $image_size[2] : 1; |
| 154 |
|
| 155 |
$size = array( |
| 156 |
'width' => $width, |
| 157 |
'height' => $height, |
| 158 |
'crop' => $crop |
| 159 |
); |
| 160 |
|
| 161 |
$image_size = $width . '_' . $height; |
| 162 |
} else { |
| 163 |
$size = array( |
| 164 |
'width' => '300', |
| 165 |
'height' => '300', |
| 166 |
'crop' => 1 |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
return $size; |
| 171 |
} |
| 172 |
|
| 173 |
function ph_get_custom_departments( $active_only = true ) |
| 174 |
{ |
| 175 |
$return = array(); |
| 176 |
|
| 177 |
$custom_departments = get_option( 'propertyhive_custom_departments', array() ); |
| 178 |
|
| 179 |
if ( is_array($custom_departments) && !empty($custom_departments) ) |
| 180 |
{ |
| 181 |
foreach ( $custom_departments as $key => $custom_department ) |
| 182 |
{ |
| 183 |
if ( !$active_only || ( $active_only && get_option('propertyhive_active_departments_' . $key) == 'yes' ) ) |
| 184 |
{ |
| 185 |
$return[$key] = $custom_department; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return $return; |
| 191 |
} |
| 192 |
|
| 193 |
function ph_get_custom_department_based_on( $department ) |
| 194 |
{ |
| 195 |
$custom_departments = get_option( 'propertyhive_custom_departments', array() ); |
| 196 |
|
| 197 |
if ( isset($custom_departments[$department]['based_on']) ) |
| 198 |
{ |
| 199 |
return $custom_departments[$department]['based_on']; |
| 200 |
} |
| 201 |
|
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
function ph_get_departments( $raw = false ) |
| 206 |
{ |
| 207 |
$departments = array( |
| 208 |
'residential-sales' => __( 'Residential Sales', 'propertyhive' ), |
| 209 |
'residential-lettings' => __( 'Residential Lettings', 'propertyhive' ), |
| 210 |
'commercial' => __( 'Commercial', 'propertyhive' ), |
| 211 |
); |
| 212 |
|
| 213 |
return $raw ? $departments : apply_filters( 'propertyhive_departments', $departments ); |
| 214 |
} |
| 215 |
|
| 216 |
function ph_get_viewing_statuses() |
| 217 |
{ |
| 218 |
$viewing_statuses = array( |
| 219 |
'pending' => __( 'Pending', 'propertyhive' ), |
| 220 |
'confirmed' => '- ' . __( 'Confirmed', 'propertyhive' ), |
| 221 |
'unconfirmed' => '- ' . __( 'Awaiting Confirmation', 'propertyhive' ), |
| 222 |
'carried_out' => __( 'Carried Out', 'propertyhive' ), |
| 223 |
'awaiting_feedback' => '- ' . __( 'Awaiting Feedback', 'propertyhive' ), |
| 224 |
'feedback_passed_on' => '- ' . __( 'Feedback Passed On', 'propertyhive' ), |
| 225 |
'feedback_not_passed_on' => '- ' . __( 'Feedback Not Passed On', 'propertyhive' ), |
| 226 |
'cancelled' => __( 'Cancelled', 'propertyhive' ), |
| 227 |
'no_show' => __( 'No Show', 'propertyhive' ), |
| 228 |
); |
| 229 |
|
| 230 |
return $viewing_statuses; |
| 231 |
} |
| 232 |
|
| 233 |
function add_viewing_status_meta_query( $meta_query, $selected_status ) |
| 234 |
{ |
| 235 |
switch ( $selected_status ) |
| 236 |
{ |
| 237 |
case "confirmed": |
| 238 |
{ |
| 239 |
$meta_query[] = array( |
| 240 |
'key' => '_status', |
| 241 |
'value' => 'pending', |
| 242 |
); |
| 243 |
$meta_query[] = array( |
| 244 |
'key' => '_all_confirmed', |
| 245 |
'value' => 'yes', |
| 246 |
); |
| 247 |
break; |
| 248 |
} |
| 249 |
case "unconfirmed": |
| 250 |
{ |
| 251 |
$meta_query[] = array( |
| 252 |
'key' => '_status', |
| 253 |
'value' => 'pending', |
| 254 |
); |
| 255 |
$meta_query[] = array( |
| 256 |
'relation' => 'OR', |
| 257 |
array( |
| 258 |
'key' => '_all_confirmed', |
| 259 |
'value' => '', |
| 260 |
), |
| 261 |
array( |
| 262 |
'key' => '_all_confirmed', |
| 263 |
'compare' => 'NOT EXISTS', |
| 264 |
) |
| 265 |
); |
| 266 |
break; |
| 267 |
} |
| 268 |
case "awaiting_feedback": |
| 269 |
{ |
| 270 |
$meta_query[] = array( |
| 271 |
'key' => '_status', |
| 272 |
'value' => 'carried_out', |
| 273 |
); |
| 274 |
$meta_query[] = array( |
| 275 |
'key' => '_feedback_status', |
| 276 |
'value' => '', |
| 277 |
); |
| 278 |
break; |
| 279 |
} |
| 280 |
case "feedback_passed_on": |
| 281 |
{ |
| 282 |
$meta_query[] = array( |
| 283 |
'key' => '_status', |
| 284 |
'value' => 'carried_out', |
| 285 |
); |
| 286 |
$meta_query[] = array( |
| 287 |
'key' => '_feedback_status', |
| 288 |
'value' => array('interested', 'not_interested'), |
| 289 |
'compare' => 'IN' |
| 290 |
); |
| 291 |
$meta_query[] = array( |
| 292 |
'key' => '_feedback_passed_on', |
| 293 |
'value' => 'yes', |
| 294 |
); |
| 295 |
break; |
| 296 |
} |
| 297 |
case "feedback_not_passed_on": |
| 298 |
{ |
| 299 |
$meta_query[] = array( |
| 300 |
'key' => '_status', |
| 301 |
'value' => 'carried_out', |
| 302 |
); |
| 303 |
$meta_query[] = array( |
| 304 |
'key' => '_feedback_status', |
| 305 |
'value' => array('interested', 'not_interested'), |
| 306 |
'compare' => 'IN' |
| 307 |
); |
| 308 |
$meta_query[] = array( |
| 309 |
'key' => '_feedback_passed_on', |
| 310 |
'value' => '', |
| 311 |
); |
| 312 |
break; |
| 313 |
} |
| 314 |
default: |
| 315 |
{ |
| 316 |
$meta_query[] = array( |
| 317 |
'key' => '_status', |
| 318 |
'value' => sanitize_text_field( $selected_status ), |
| 319 |
); |
| 320 |
} |
| 321 |
} |
| 322 |
return $meta_query; |
| 323 |
} |
| 324 |
|
| 325 |
function ph_get_offer_statuses() |
| 326 |
{ |
| 327 |
$offer_statuses = array( |
| 328 |
'pending' => __( 'Pending', 'propertyhive' ), |
| 329 |
'accepted' => __( 'Accepted', 'propertyhive' ), |
| 330 |
'declined' => __( 'Declined', 'propertyhive' ), |
| 331 |
); |
| 332 |
|
| 333 |
return $offer_statuses; |
| 334 |
} |
| 335 |
|
| 336 |
function ph_get_sale_statuses() |
| 337 |
{ |
| 338 |
$sale_statuses = array( |
| 339 |
'current' => __( 'Current', 'propertyhive' ), |
| 340 |
'exchanged' => __( 'Exchanged', 'propertyhive' ), |
| 341 |
'completed' => __( 'Completed', 'propertyhive' ), |
| 342 |
'fallen_through' => __( 'Fallen Through', 'propertyhive' ), |
| 343 |
); |
| 344 |
|
| 345 |
return $sale_statuses; |
| 346 |
} |
| 347 |
|
| 348 |
function ph_get_enquiry_statuses() |
| 349 |
{ |
| 350 |
$enquiry_statuses = array( |
| 351 |
'open' => __( 'Open', 'propertyhive' ), |
| 352 |
'closed' => __( 'Closed', 'propertyhive' ), |
| 353 |
); |
| 354 |
|
| 355 |
return $enquiry_statuses; |
| 356 |
} |
| 357 |
|
| 358 |
function get_area_units() |
| 359 |
{ |
| 360 |
$size_options = array( |
| 361 |
'sqft' => __( 'Sq Ft', 'propertyhive' ), |
| 362 |
'sqm' => __( 'Sq M', 'propertyhive' ), |
| 363 |
'acre' => __( 'Acres', 'propertyhive' ), |
| 364 |
'hectare' => __( 'Hectares', 'propertyhive' ), |
| 365 |
); |
| 366 |
|
| 367 |
// $size_options = apply_filters( 'propertyhive_commercial_size_units', $size_options ). |
| 368 |
// Above filter not in use as we need a way to add the conversion rate to sqft also |
| 369 |
// If it's a commonly used unit we could just add it for everyones benefit |
| 370 |
|
| 371 |
return $size_options; |
| 372 |
} |
| 373 |
|
| 374 |
function convert_size_to_sqft( $size, $unit = 'sqft' ) |
| 375 |
{ |
| 376 |
$size_sqft = $size; |
| 377 |
|
| 378 |
if ( $size_sqft != '' ) |
| 379 |
{ |
| 380 |
switch ( $unit ) |
| 381 |
{ |
| 382 |
case "sqm": { $size_sqft = $size * 10.7639; break; } |
| 383 |
case "acre": { $size_sqft = $size * 43560; break; } |
| 384 |
case "hectare": { $size_sqft = $size * 107639; break; } |
| 385 |
} |
| 386 |
} |
| 387 |
return $size_sqft; |
| 388 |
} |
| 389 |
|
| 390 |
function get_commercial_price_units( ) |
| 391 |
{ |
| 392 |
$price_options = array( |
| 393 |
'psqft' => __( 'Per Sq Ft', 'propertyhive' ), |
| 394 |
'psqm' => __( 'Per Sq M', 'propertyhive' ), |
| 395 |
'pacre' => __( 'Per Acre', 'propertyhive' ), |
| 396 |
'phectare' => __( 'Per Hectare', 'propertyhive' ), |
| 397 |
); |
| 398 |
|
| 399 |
return $price_options; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Get ordinal suffix (st, nd, rd etc) for any number |
| 404 |
* |
| 405 |
* @param int $number |
| 406 |
* @param bool $return_number Include $n in the string returned |
| 407 |
* @return string $number including its ordinal suffix |
| 408 |
*/ |
| 409 |
function ph_ordinal_suffix( $number, $return_words = true, $return_number = true ) |
| 410 |
{ |
| 411 |
$n_last = $number % 100; |
| 412 |
if ( ($n_last > 10 && $n_last << 14) || $number == 0 ) |
| 413 |
{ |
| 414 |
$suffix = 'th'; |
| 415 |
$number_in_words = substr($number, -1) . 'th'; |
| 416 |
} |
| 417 |
else |
| 418 |
{ |
| 419 |
switch( substr($number, -1) ) |
| 420 |
{ |
| 421 |
case '1': |
| 422 |
$suffix = 'st'; |
| 423 |
$number_in_words = 'First'; |
| 424 |
break; |
| 425 |
case '2': |
| 426 |
$suffix = 'nd'; |
| 427 |
$number_in_words = 'Second'; |
| 428 |
break; |
| 429 |
case '3': |
| 430 |
$suffix = 'rd'; |
| 431 |
$number_in_words = 'Third'; |
| 432 |
break; |
| 433 |
case '4': |
| 434 |
$suffix = 'th'; |
| 435 |
$number_in_words = 'Fourth'; |
| 436 |
break; |
| 437 |
case '5': |
| 438 |
$suffix = 'th'; |
| 439 |
$number_in_words = 'Fifth'; |
| 440 |
break; |
| 441 |
default: |
| 442 |
$suffix = 'th'; |
| 443 |
$number_in_words = substr($number, -1) . 'th'; |
| 444 |
break; |
| 445 |
} |
| 446 |
} |
| 447 |
return $return_words ? $number_in_words : ( $return_number ? $number . $suffix : $suffix ); |
| 448 |
} |