| 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( $property_ids ); |
| 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 ( empty( $_COOKIE['propertyhive_recently_viewed'] ) ) |
| 96 |
$viewed_properties = array(); |
| 97 |
else |
| 98 |
$viewed_properties = (array) explode( '|', $_COOKIE['propertyhive_recently_viewed'] ); |
| 99 |
|
| 100 |
if ( ! in_array( $post->ID, $viewed_properties ) ) |
| 101 |
$viewed_properties[] = $post->ID; |
| 102 |
|
| 103 |
if ( sizeof( $viewed_properties ) > 15 ) |
| 104 |
array_shift( $viewed_properties ); |
| 105 |
|
| 106 |
// Store for session only |
| 107 |
ph_setcookie( 'propertyhive_recently_viewed', implode( '|', $viewed_properties ) ); |
| 108 |
|
| 109 |
// Track in database |
| 110 |
if ( !is_user_logged_in() || ( is_user_logged_in() && !current_user_can('manage_propertyhive') ) ) |
| 111 |
{ |
| 112 |
// User isn't logged in |
| 113 |
|
| 114 |
$view_counts = get_post_meta( $post->ID, '_view_statistics', TRUE ); |
| 115 |
|
| 116 |
if ( $view_counts == '' || !is_array($view_counts) ) |
| 117 |
{ |
| 118 |
$view_counts = array(); |
| 119 |
} |
| 120 |
|
| 121 |
if ( !isset($view_counts[date("Y-m-d")]) ) |
| 122 |
{ |
| 123 |
$view_counts[date("Y-m-d")] = 0; |
| 124 |
} |
| 125 |
|
| 126 |
++$view_counts[date("Y-m-d")]; |
| 127 |
|
| 128 |
update_post_meta( $post->ID, '_view_statistics', $view_counts ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
add_action( 'template_redirect', 'ph_track_property_view', 20 ); |
| 133 |
|
| 134 |
function get_property_map( $args = array() ) |
| 135 |
{ |
| 136 |
global $property; |
| 137 |
|
| 138 |
if ( $property->latitude != '' && $property->latitude != '0' && $property->longitude != '' && $property->longitude != '0' ) |
| 139 |
{ |
| 140 |
$api_key = get_option('propertyhive_google_maps_api_key'); |
| 141 |
wp_register_script('googlemaps', '//maps.googleapis.com/maps/api/js?' . ( ( $api_key != '' && $api_key !== FALSE ) ? 'key=' . $api_key : '' ), false, '3'); |
| 142 |
wp_enqueue_script('googlemaps'); |
| 143 |
|
| 144 |
echo '<div id="property_map_canvas" style="height:' . str_replace( "px", "", ( ( isset($args['height']) && !empty($args['height']) ) ? $args['height'] : '400' ) ) . 'px"></div>'; |
| 145 |
?> |
| 146 |
<script> |
| 147 |
|
| 148 |
// We declare vars globally so developers can access them |
| 149 |
var property_map; // Global declaration of the map |
| 150 |
var property_marker; // Global declaration of the marker |
| 151 |
|
| 152 |
function initialize_property_map() { |
| 153 |
|
| 154 |
var myLatlng = new google.maps.LatLng(<?php echo $property->latitude; ?>, <?php echo $property->longitude; ?>); |
| 155 |
var map_options = { |
| 156 |
zoom: <?php echo ( ( isset($args['zoom']) && !empty($args['zoom']) ) ? $args['zoom'] : '14' ); ?>, |
| 157 |
center: myLatlng, |
| 158 |
mapTypeId: google.maps.MapTypeId.ROADMAP, |
| 159 |
scrollwheel: <?php echo ( ( isset($args['scrollwheel']) && ($args['scrollwheel'] === 'false' || $args['scrollwheel'] === FALSE) ) ? 'false' : 'true' ); ?> |
| 160 |
} |
| 161 |
<?php |
| 162 |
if ( class_exists( 'PH_Map_Search' ) ) |
| 163 |
{ |
| 164 |
$map_add_on_settings = get_option( 'propertyhive_map_search', array() ); |
| 165 |
|
| 166 |
if ( isset($map_add_on_settings['style_js']) && trim($map_add_on_settings['style_js']) != '' ) |
| 167 |
{ |
| 168 |
echo 'map_options.styles = ' . trim($map_add_on_settings['style_js']) . ';'; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
do_action( 'propertyhive_property_map_options' ); |
| 173 |
?> |
| 174 |
property_map = new google.maps.Map(document.getElementById("property_map_canvas"), map_options); |
| 175 |
|
| 176 |
var myLatlng = new google.maps.LatLng(<?php echo $property->latitude; ?>, <?php echo $property->longitude; ?>); |
| 177 |
|
| 178 |
var marker_options = { |
| 179 |
map: property_map, |
| 180 |
position: myLatlng |
| 181 |
}; |
| 182 |
|
| 183 |
<?php do_action( 'propertyhive_property_map_marker_options' ); ?> |
| 184 |
|
| 185 |
property_marker = new google.maps.Marker(marker_options); |
| 186 |
} |
| 187 |
|
| 188 |
if(window.addEventListener) { |
| 189 |
window.addEventListener('load', initialize_property_map); |
| 190 |
}else{ |
| 191 |
window.attachEvent('onload', initialize_property_map); |
| 192 |
} |
| 193 |
|
| 194 |
</script> |
| 195 |
<?php |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
function get_property_street_view( $args = array() ) |
| 200 |
{ |
| 201 |
global $property; |
| 202 |
|
| 203 |
if ( $property->latitude != '' && $property->latitude != '0' && $property->longitude != '' && $property->longitude != '0' ) |
| 204 |
{ |
| 205 |
$api_key = get_option('propertyhive_google_maps_api_key'); |
| 206 |
wp_register_script('googlemaps', '//maps.googleapis.com/maps/api/js?' . ( ( $api_key != '' && $api_key !== FALSE ) ? 'key=' . $api_key : '' ), false, '3'); |
| 207 |
wp_enqueue_script('googlemaps'); |
| 208 |
|
| 209 |
echo '<div id="property_street_view_canvas" style="height:' . str_replace( "px", "", ( ( isset($args['height']) && !empty($args['height']) ) ? $args['height'] : '400' ) ) . 'px"></div>'; |
| 210 |
?> |
| 211 |
<script> |
| 212 |
|
| 213 |
// We declare vars globally so developers can access them |
| 214 |
var property_street_view; // Global declaration of the map |
| 215 |
|
| 216 |
function initialize_property_street_view() { |
| 217 |
|
| 218 |
var myLatlng = new google.maps.LatLng(<?php echo $property->latitude; ?>, <?php echo $property->longitude; ?>); |
| 219 |
var map_options = { |
| 220 |
center: myLatlng |
| 221 |
} |
| 222 |
|
| 223 |
<?php do_action( 'propertyhive_property_street_view_map_options' ); ?> |
| 224 |
|
| 225 |
property_street_view = new google.maps.Map(document.getElementById("property_street_view_canvas"), map_options); |
| 226 |
|
| 227 |
var streetViewOptions = { |
| 228 |
position: myLatlng, |
| 229 |
pov: { |
| 230 |
heading: 90, |
| 231 |
pitch: 0, |
| 232 |
zoom: 0 |
| 233 |
} |
| 234 |
}; |
| 235 |
|
| 236 |
<?php do_action( 'propertyhive_property_street_view_options' ); ?> |
| 237 |
|
| 238 |
var streetView = new google.maps.StreetViewPanorama(document.getElementById("property_street_view_canvas"), streetViewOptions); |
| 239 |
streetView.setVisible(true); |
| 240 |
} |
| 241 |
|
| 242 |
if(window.addEventListener) { |
| 243 |
window.addEventListener('load', initialize_property_street_view); |
| 244 |
}else{ |
| 245 |
window.attachEvent('onload', initialize_property_street_view); |
| 246 |
} |
| 247 |
|
| 248 |
</script> |
| 249 |
<?php |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
add_filter( 'get_post_metadata', function ( $value, $post_id, $meta_key, $single ) |
| 254 |
{ |
| 255 |
static $is_recursing = false; // Used to prevent infinite loop |
| 256 |
|
| 257 |
// Only filter if we're not recursing and if it is a post thumbnail ID |
| 258 |
if ( ! $is_recursing && $meta_key === '_thumbnail_id' && get_post_type( $post_id ) == 'property' ) |
| 259 |
{ |
| 260 |
$is_recursing = true; |
| 261 |
|
| 262 |
$value = get_post_thumbnail_id( $post_id ); |
| 263 |
|
| 264 |
$is_recursing = false; |
| 265 |
|
| 266 |
if ( $value == '' ) // If we haven't already get a thumbnail ID (i.e. in the case where someone has added theme support) |
| 267 |
{ |
| 268 |
$photos = get_post_meta( $post_id, '_photos', TRUE ); |
| 269 |
if ( is_array($photos) && !empty($photos) ) |
| 270 |
{ |
| 271 |
$photos = array_filter( $photos ); |
| 272 |
$value = $photos[0]; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if ( ! $single ) |
| 277 |
{ |
| 278 |
$value = array( $value ); |
| 279 |
} |
| 280 |
} |
| 281 |
return $value; |
| 282 |
}, 10, 4); |