| 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-product). |
| 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_departments() |
| 174 |
{ |
| 175 |
$departments = array( |
| 176 |
'residential-sales' => __( 'Residential Sales', 'propertyhive' ), |
| 177 |
'residential-lettings' => __( 'Residential Lettings', 'propertyhive' ), |
| 178 |
'commercial' => __( 'Commercial', 'propertyhive' ), |
| 179 |
); |
| 180 |
|
| 181 |
return apply_filters( 'propertyhive_departments', $departments ); |
| 182 |
} |
| 183 |
|
| 184 |
function get_area_units() |
| 185 |
{ |
| 186 |
$size_options = array( |
| 187 |
'sqft' => __( 'Sq Ft', 'propertyhive' ), |
| 188 |
'sqm' => __( 'Sq M', 'propertyhive' ), |
| 189 |
'acre' => __( 'Acres', 'propertyhive' ), |
| 190 |
'hectare' => __( 'Hectares', 'propertyhive' ), |
| 191 |
); |
| 192 |
|
| 193 |
// $size_options = apply_filters( 'propertyhive_commercial_size_units', $size_options ). |
| 194 |
// Above filter not in use as we need a way to add the conversion rate to sqft also |
| 195 |
// If it's a commonly used unit we could just add it for everyones benefit |
| 196 |
|
| 197 |
return $size_options; |
| 198 |
} |
| 199 |
|
| 200 |
function convert_size_to_sqft( $size, $unit = 'sqft' ) |
| 201 |
{ |
| 202 |
$size_sqft = $size; |
| 203 |
switch ( $unit ) |
| 204 |
{ |
| 205 |
case "sqm": { $size_sqft = $size * 1; break; } |
| 206 |
case "acre": { $size_sqft = $size * 43560; break; } |
| 207 |
case "hectare": { $size_sqft = $size * 107639; break; } |
| 208 |
} |
| 209 |
return $size_sqft; |
| 210 |
} |
| 211 |
|
| 212 |
function get_commercial_price_units( ) |
| 213 |
{ |
| 214 |
$price_options = array( |
| 215 |
'psqft' => __( 'Per Sq Ft', 'propertyhive' ), |
| 216 |
'psqm' => __( 'Per Sq M', 'propertyhive' ), |
| 217 |
'pacre' => __( 'Per Acre', 'propertyhive' ), |
| 218 |
'phectare' => __( 'Per Hectare', 'propertyhive' ), |
| 219 |
); |
| 220 |
|
| 221 |
return $price_options; |
| 222 |
} |