| 1 |
<?php |
| 2 |
/** |
| 3 |
* PropertyHive Property Functions |
| 4 |
* |
| 5 |
* Functions for property specific things. |
| 6 |
* |
| 7 |
* @author BISOTALL |
| 8 |
* @category Core |
| 9 |
* @package PropertyHive/Functions |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* Main function for returning properties, uses the PH_Property_Factory class. |
| 15 |
* |
| 16 |
* @param mixed $the_property Post object or post ID of the property. |
| 17 |
* @param array $args (default: array()) Contains all arguments to be used to get this property. |
| 18 |
* @return PH_Property |
| 19 |
*/ |
| 20 |
function get_property( $the_property = false, $args = array() ) { |
| 21 |
return new PH_Property( $the_property ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Function that returns an array containing the IDs of the featured properties. |
| 26 |
* |
| 27 |
* @access public |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
function ph_get_featured_property_ids() { |
| 31 |
|
| 32 |
// Load from cache |
| 33 |
$featured_property_ids = get_transient( 'ph_featured_properties' ); |
| 34 |
|
| 35 |
// Valid cache found |
| 36 |
if ( false !== $featured_property_ids ) |
| 37 |
return $featured_property_ids; |
| 38 |
|
| 39 |
$featured = get_posts( array( |
| 40 |
'post_type' => 'property', |
| 41 |
'posts_per_page' => -1, |
| 42 |
'post_status' => 'publish', |
| 43 |
'meta_query' => array( |
| 44 |
array( |
| 45 |
'key' => '_on_market', |
| 46 |
'value' => 'yes' |
| 47 |
), |
| 48 |
array( |
| 49 |
'key' => '_featured', |
| 50 |
'value' => 'yes' |
| 51 |
) |
| 52 |
), |
| 53 |
'fields' => 'id=>parent' |
| 54 |
) ); |
| 55 |
|
| 56 |
$featured_property_ids = array_keys( $featured ); |
| 57 |
|
| 58 |
set_transient( 'ph_featured_properties', $featured_property_ids, YEAR_IN_SECONDS ); |
| 59 |
|
| 60 |
return $featured_property_ids; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get the placeholder image URL for properties |
| 65 |
* |
| 66 |
* @access public |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
function ph_placeholder_img_src() { |
| 70 |
return apply_filters( 'propertyhive_placeholder_img_src', PH()->plugin_url() . '/assets/images/placeholder.png' ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the placeholder image |
| 75 |
* |
| 76 |
* @access public |
| 77 |
* @return string |
| 78 |
*/ |
| 79 |
function ph_placeholder_img( $size = 'thumbnail' ) { |
| 80 |
$dimensions = ph_get_image_size( $size ); |
| 81 |
|
| 82 |
return apply_filters('propertyhive_placeholder_img', '<img src="' . ph_placeholder_img_src() . '" alt="Placeholder" width="' . esc_attr( $dimensions['width'] ) . '" class="property-placeholder wp-post-image" height="' . esc_attr( $dimensions['height'] ) . '" />' ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Track property views |
| 87 |
*/ |
| 88 |
function ph_track_property_view() { |
| 89 |
if ( ! is_singular( 'property' ) ) |
| 90 |
return; |
| 91 |
|
| 92 |
global $post; |
| 93 |
|
| 94 |
// Track in cookie |
| 95 |
if ( apply_filters( 'propertyhive_store_in_recently_viewed_cookie', true ) ) |
| 96 |
{ |
| 97 |
if ( empty( $_COOKIE['propertyhive_recently_viewed'] ) ) |
| 98 |
$viewed_properties = array(); |
| 99 |
else |
| 100 |
$viewed_properties = (array) explode( '|', $_COOKIE['propertyhive_recently_viewed'] ); |
| 101 |
|
| 102 |
if ( ! in_array( $post->ID, $viewed_properties ) ) |
| 103 |
$viewed_properties[] = $post->ID; |
| 104 |
|
| 105 |
if ( sizeof( $viewed_properties ) > 15 ) |
| 106 |
array_shift( $viewed_properties ); |
| 107 |
|
| 108 |
// Store for session only |
| 109 |
ph_setcookie( 'propertyhive_recently_viewed', implode( '|', $viewed_properties ) ); |
| 110 |
} |
| 111 |
|
| 112 |
// Track in database |
| 113 |
if ( !is_user_logged_in() || ( is_user_logged_in() && !current_user_can('manage_propertyhive') ) ) |
| 114 |
{ |
| 115 |
// User isn't logged in |
| 116 |
|
| 117 |
$view_counts = get_post_meta( $post->ID, '_view_statistics', TRUE ); |
| 118 |
|
| 119 |
if ( $view_counts == '' || !is_array($view_counts) ) |
| 120 |
{ |
| 121 |
$view_counts = array(); |
| 122 |
} |
| 123 |
|
| 124 |
if ( !isset($view_counts[date("Y-m-d")]) ) |
| 125 |
{ |
| 126 |
$view_counts[date("Y-m-d")] = 0; |
| 127 |
} |
| 128 |
|
| 129 |
++$view_counts[date("Y-m-d")]; |
| 130 |
|
| 131 |
update_post_meta( $post->ID, '_view_statistics', $view_counts ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
add_action( 'template_redirect', 'ph_track_property_view', 20 ); |
| 136 |
|
| 137 |
function get_property_map( $args = array() ) |
| 138 |
{ |
| 139 |
global $property; |
| 140 |
|
| 141 |
if ( $property->latitude != '' && $property->latitude != '0' && $property->longitude != '' && $property->longitude != '0' ) |
| 142 |
{ |
| 143 |
$api_key = get_option('propertyhive_google_maps_api_key'); |
| 144 |
wp_register_script('googlemaps', '//maps.googleapis.com/maps/api/js?' . ( ( $api_key != '' && $api_key !== FALSE ) ? 'key=' . $api_key : '' ), false, '3'); |
| 145 |
wp_enqueue_script('googlemaps'); |
| 146 |
|
| 147 |
$id_suffix = ( ( isset($args['id']) && $args['id'] != '' ) ? '_' . $args['id'] : '' ); |
| 148 |
|
| 149 |
echo '<div id="property_map_canvas' . $id_suffix . '" style="height:' . str_replace( "px", "", ( ( isset($args['height']) && !empty($args['height']) ) ? $args['height'] : '400' ) ) . 'px"></div>'; |
| 150 |
?> |
| 151 |
<script> |
| 152 |
|
| 153 |
// We declare vars globally so developers can access them |
| 154 |
var property_map<?php echo $id_suffix; ?>; // Global declaration of the map |
| 155 |
var property_marker<?php echo $id_suffix; ?>; // Global declaration of the marker |
| 156 |
|
| 157 |
function initialize_property_map<?php echo $id_suffix; ?>() { |
| 158 |
|
| 159 |
var myLatlng = new google.maps.LatLng(<?php echo $property->latitude; ?>, <?php echo $property->longitude; ?>); |
| 160 |
var map_options = { |
| 161 |
zoom: <?php echo ( ( isset($args['zoom']) && !empty($args['zoom']) ) ? $args['zoom'] : '14' ); ?>, |
| 162 |
center: myLatlng, |
| 163 |
mapTypeId: google.maps.MapTypeId.ROADMAP, |
| 164 |
scrollwheel: <?php echo ( ( isset($args['scrollwheel']) && ($args['scrollwheel'] === 'false' || $args['scrollwheel'] === FALSE) ) ? 'false' : 'true' ); ?> |
| 165 |
} |
| 166 |
<?php |
| 167 |
if ( class_exists( 'PH_Map_Search' ) ) |
| 168 |
{ |
| 169 |
$map_add_on_settings = get_option( 'propertyhive_map_search', array() ); |
| 170 |
|
| 171 |
if ( isset($map_add_on_settings['style_js']) && trim($map_add_on_settings['style_js']) != '' ) |
| 172 |
{ |
| 173 |
echo 'map_options.styles = ' . trim($map_add_on_settings['style_js']) . ';'; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
do_action( 'propertyhive_property_map_options' ); |
| 178 |
?> |
| 179 |
property_map<?php echo $id_suffix; ?> = new google.maps.Map(document.getElementById("property_map_canvas<?php echo $id_suffix; ?>"), map_options); |
| 180 |
|
| 181 |
var myLatlng = new google.maps.LatLng(<?php echo $property->latitude; ?>, <?php echo $property->longitude; ?>); |
| 182 |
|
| 183 |
var marker_options = { |
| 184 |
map: property_map<?php echo $id_suffix; ?>, |
| 185 |
position: myLatlng |
| 186 |
}; |
| 187 |
|
| 188 |
<?php |
| 189 |
if ( class_exists( 'PH_Map_Search' ) ) |
| 190 |
{ |
| 191 |
$map_add_on_settings = get_option( 'propertyhive_map_search', array() ); |
| 192 |
|
| 193 |
if ( isset($map_add_on_settings['icon_type']) && $map_add_on_settings['icon_type'] == 'custom_single' && isset($map_add_on_settings['custom_icon_attachment_id']) && $map_add_on_settings['custom_icon_attachment_id'] != '' ) |
| 194 |
{ |
| 195 |
$marker_icon_url = wp_get_attachment_url( $map_add_on_settings['custom_icon_attachment_id'] ); |
| 196 |
if ( $marker_icon_url !== FALSE ) |
| 197 |
{ |
| 198 |
echo 'marker_options.icon = \'' . $marker_icon_url . '\';'; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
?> |
| 203 |
|
| 204 |
<?php do_action( 'propertyhive_property_map_marker_options' ); ?> |
| 205 |
|
| 206 |
property_marker<?php echo $id_suffix; ?> = new google.maps.Marker(marker_options); |
| 207 |
} |
| 208 |
|
| 209 |
if(window.addEventListener) { |
| 210 |
window.addEventListener('load', initialize_property_map<?php echo $id_suffix; ?>); |
| 211 |
}else{ |
| 212 |
window.attachEvent('onload', initialize_property_map<?php echo $id_suffix; ?>); |
| 213 |
} |
| 214 |
|
| 215 |
</script> |
| 216 |
<?php |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
function get_property_street_view( $args = array() ) |
| 221 |
{ |
| 222 |
global $property; |
| 223 |
|
| 224 |
if ( $property->latitude != '' && $property->latitude != '0' && $property->longitude != '' && $property->longitude != '0' ) |
| 225 |
{ |
| 226 |
$api_key = get_option('propertyhive_google_maps_api_key'); |
| 227 |
wp_register_script('googlemaps', '//maps.googleapis.com/maps/api/js?' . ( ( $api_key != '' && $api_key !== FALSE ) ? 'key=' . $api_key : '' ), false, '3'); |
| 228 |
wp_enqueue_script('googlemaps'); |
| 229 |
|
| 230 |
echo '<div id="property_street_view_canvas" style="height:' . str_replace( "px", "", ( ( isset($args['height']) && !empty($args['height']) ) ? $args['height'] : '400' ) ) . 'px"></div>'; |
| 231 |
?> |
| 232 |
<script> |
| 233 |
|
| 234 |
// We declare vars globally so developers can access them |
| 235 |
var property_street_view; // Global declaration of the map |
| 236 |
|
| 237 |
function initialize_property_street_view() { |
| 238 |
|
| 239 |
var myLatlng = new google.maps.LatLng(<?php echo $property->latitude; ?>, <?php echo $property->longitude; ?>); |
| 240 |
var map_options = { |
| 241 |
center: myLatlng |
| 242 |
} |
| 243 |
|
| 244 |
<?php do_action( 'propertyhive_property_street_view_map_options' ); ?> |
| 245 |
|
| 246 |
property_street_view = new google.maps.Map(document.getElementById("property_street_view_canvas"), map_options); |
| 247 |
|
| 248 |
var streetViewOptions = { |
| 249 |
position: myLatlng, |
| 250 |
pov: { |
| 251 |
heading: 90, |
| 252 |
pitch: 0, |
| 253 |
zoom: 0 |
| 254 |
} |
| 255 |
}; |
| 256 |
|
| 257 |
<?php do_action( 'propertyhive_property_street_view_options' ); ?> |
| 258 |
|
| 259 |
var streetView = new google.maps.StreetViewPanorama(document.getElementById("property_street_view_canvas"), streetViewOptions); |
| 260 |
streetView.setVisible(true); |
| 261 |
} |
| 262 |
|
| 263 |
if(window.addEventListener) { |
| 264 |
window.addEventListener('load', initialize_property_street_view); |
| 265 |
}else{ |
| 266 |
window.attachEvent('onload', initialize_property_street_view); |
| 267 |
} |
| 268 |
|
| 269 |
</script> |
| 270 |
<?php |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
add_filter( 'get_post_metadata', function ( $value, $post_id, $meta_key, $single ) |
| 275 |
{ |
| 276 |
static $is_recursing = false; // Used to prevent infinite loop |
| 277 |
|
| 278 |
// Only filter if we're not recursing and if it is a post thumbnail ID |
| 279 |
if ( ! $is_recursing && $meta_key === '_thumbnail_id' && get_post_type( $post_id ) == 'property' ) |
| 280 |
{ |
| 281 |
$is_recursing = true; |
| 282 |
|
| 283 |
$value = get_post_thumbnail_id( $post_id ); |
| 284 |
|
| 285 |
$is_recursing = false; |
| 286 |
|
| 287 |
if ( $value == '' ) // If we haven't already get a thumbnail ID (i.e. in the case where someone has added theme support) |
| 288 |
{ |
| 289 |
$photos = get_post_meta( $post_id, '_photos', TRUE ); |
| 290 |
if ( is_array($photos) && !empty($photos) ) |
| 291 |
{ |
| 292 |
$photos = array_filter( $photos ); |
| 293 |
$value = $photos[0]; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if ( ! $single ) |
| 298 |
{ |
| 299 |
$value = array( $value ); |
| 300 |
} |
| 301 |
} |
| 302 |
return $value; |
| 303 |
}, 10, 4); |