/** * Hooks into save_post function and saves additional property data * * @todo Add some sort of custom capability so not only admins can make properties as featured. i.e. Agents can make their own properties featured. * @since 1.04 * @param null $post_id * @return null */ function save_property($post_id = null) { global $wp_properties, $wp_version; $_wpnonce = (version_compare($wp_version, '3.5', '>=') ? 'update-post_' : 'update-property_') . $post_id; if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], $_wpnonce) || $_POST['post_type'] !== 'property') { return $post_id; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } $update_data = $_REQUEST['wpp_data']['meta']; //** Neccessary meta data which is required by Supermap Premium Feature. Should be always set even the Supermap disabled. peshkov@UD */ if (empty($_REQUEST['exclude_from_supermap'])) { if (!metadata_exists('post', $post_id, 'exclude_from_supermap')) { $update_data['exclude_from_supermap'] = 'false'; } } if (!isset($update_data['latitude']) || (double) $update_data['latitude'] == 0) { $update_data['latitude'] = ''; } if (!isset($update_data['longitude']) || (double) $update_data['longitude'] == 0) { $update_data['longitude'] = ''; } /* get old coordinates and location */ $old_lat = get_post_meta($post_id, 'latitude', true); $old_lng = get_post_meta($post_id, 'longitude', true); $geo_data = array('old_coordinates' => empty($old_lat) || empty($old_lng) ? "" : array('lat' => $old_lat, 'lng' => $old_lng), 'old_location' => !empty($wp_properties['configuration']['address_attribute']) ? get_post_meta($post_id, $wp_properties['configuration']['address_attribute'], true) : ''); // die( '<pre>' . $post_id . print_r( $update_data, true ) . '</pre>' ); foreach ($update_data as $meta_key => $meta_value) { $attribute_data = UsabilityDynamics\WPP\Attributes::get_attribute_data($meta_key); $meta_value = html_entity_decode($meta_value); $meta_value = stripslashes($meta_value); /* Handle logic for featured property. */ if ($meta_key == 'featured') { //* Only admins can mark properties as featured. */ if (!current_user_can('manage_options')) { //** But be sure that meta 'featured' exists at all */ if (!metadata_exists('post', $post_id, $meta_key)) { $meta_value = 'false'; } else { continue; } } do_action('wpp::toggle_featured', $meta_value, $post_id); } //* Remove certain characters */ if (isset($attribute_data['currency']) || isset($attribute_data['numeric'])) { $meta_value = str_replace(array("\$", ","), '', $meta_value); } //* Overwrite old post meta allowing only one value */ delete_post_meta($post_id, $meta_key); add_post_meta($post_id, $meta_key, $meta_value); } //* Check if property has children */ $children = get_children(array('post_parent' => $post_id, 'post_type' => 'property')); //* Write any data to children properties that are supposed to inherit things */ //* 1) Go through all children */ foreach ((array) $children as $child_id => $child_data) { //* Determine child property_type */ $child_property_type = get_post_meta($child_id, 'property_type', true); //* Check if child's property type has inheritence rules, and if meta_key exists in inheritance array */ if (isset($wp_properties['property_inheritance'][$child_property_type]) && is_array($wp_properties['property_inheritance'][$child_property_type])) { foreach ($wp_properties['property_inheritance'][$child_property_type] as $i_meta_key) { $parent_meta_value = get_post_meta($post_id, $i_meta_key, true); //* inheritance rule exists for this property_type for this meta_key */ update_post_meta($child_id, $i_meta_key, $parent_meta_value); } } } $_gpid = WPP_F::maybe_set_gpid($post_id); do_action('save_property', $post_id, array('children' => $children, 'gpid' => $_gpid, 'update_data' => $update_data, 'geo_data' => $geo_data)); /** * Flush all object caches related to current property */ \UsabilityDynamics\WPP\Property_Factory::flush_cache($post_id); /** * Flush WP-Property caches */ \WPP_F::clear_cache(); }
/** * Updates parent ID. * Determines if parent exists and it doesn't have own parent. * * @param integer $parent_id * @param integer $post_id * * @return int * @author peshkov@UD * @since 1.37.5 */ public static function update_parent_id($parent_id, $post_id) { global $wpdb, $wp_properties; $parent_id = !empty($parent_id) ? $parent_id : 0; $post = get_post($parent_id); if (!$post) { $parent_id = 0; } else { if ($post->post_parent > 0) { if (empty($wp_properties['configuration']['allow_parent_deep_depth']) || $wp_properties['configuration']['allow_parent_deep_depth'] != 'true') { $parent_id = 0; } } } if ($parent_id == 0) { $wpdb->query("UPDATE {$wpdb->posts} SET post_parent=0 WHERE ID={$post_id}"); } update_post_meta($post_id, 'parent_gpid', WPP_F::maybe_set_gpid($parent_id)); return $parent_id; }
/** * This function takes all your properties and exports it as an XML feed * * @todo Improve efficiency of function, times out quickly for feeds of 500 properties. memory_limit and set_time_limit should be removed once efficiency is improved * */ public function wpp_export_properties() { global $wp_properties; ini_set('memory_limit', -1); $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; // Set a new path set_include_path(get_include_path() . PATH_SEPARATOR . WPP_Path . 'lib/third-party/XML/'); // Include our necessary libaries require_once 'Serializer.php'; require_once 'Unserializer.php'; $api_key = \WPP_F::get_api_key(); $taxonomies = $wp_properties['taxonomies']; // If the API key isn't valid, we quit if (!isset($_REQUEST['api']) || $_REQUEST['api'] != $api_key) { die(__('Invalid API key.', ud_get_wp_property()->domain)); } if (isset($_REQUEST['limit'])) { $per_page = $_REQUEST['limit']; $starting_row = 0; } if (isset($_REQUEST['per_page'])) { $per_page = $_REQUEST['per_page']; } if (isset($_REQUEST['starting_row'])) { $starting_row = $_REQUEST['starting_row']; } if (isset($_REQUEST['property_type'])) { $property_type = $_REQUEST['property_type']; } else { $property_type = 'all'; } if (strtolower($_REQUEST['format']) == 'xml') { $xml_format = true; } else { $xml_format = false; } $wpp_query['query']['pagi'] = $starting_row . '--' . $per_page; $wpp_query['query']['sort_by'] = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : 'post_date'; $wpp_query['query']['sort_order'] = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'ASC'; $wpp_query['query']['property_type'] = $property_type; $wpp_query['query'] = apply_filters('wpp::xml::export::query', $wpp_query['query']); $wpp_query = \WPP_F::get_properties($wpp_query['query'], true); $results = $wpp_query['results']; if (count($results) == 0) { die(__('No published properties.', ud_get_wp_property()->domain)); } if ($xml_format) { } else { } $properties = array(); foreach ($results as $count => $id) { //** Reserve time on every iteration. */ set_time_limit(0); $property = \WPP_F::get_property($id, "return_object=false&load_parent=false&get_children=false"); if ($property['post_parent'] && !$property['parent_gpid']) { $property['parent_gpid'] = \WPP_F::maybe_set_gpid($property['post_parent']); } // Unset unnecessary data unset($property['comment_count'], $property['post_modified_gmt'], $property['comment_status'], $property['post_password'], $property['guid'], $property['filter'], $property['post_author'], $property['permalink'], $property['ping_status'], $property['post_modified'], $property['post_mime_type']); // Set unique site ID $property['wpp_unique_id'] = md5($api_key . $property['ID']); //** Get taxonomies */ if ($taxonomies) { foreach ($taxonomies as $taxonomy_slug => $taxonomy_data) { if ($these_terms = wp_get_object_terms($property['ID'], $taxonomy_slug, array('fields' => 'names'))) { $property['taxonomies'][$taxonomy_slug] = $these_terms; } } } $fixed_property = array(); foreach ($property as $meta_key => $meta_value) { // Maybe Unserialize $meta_value = maybe_unserialize($meta_value); if (is_array($meta_value) || is_object($meta_value)) { $fixed_property[$meta_key] = $meta_value; continue; } $fixed_property[$meta_key] = $meta_value; } $properties[$id] = $fixed_property; } $properties = apply_filters('wpp::xml::export::data', $properties); if ($xml_format) { $result = $this->convert_to_xml($properties, apply_filters('wpp::xml::export::serializer_options', array())); /** Deprecated. peshkov@UD $result = json_encode( $properties ); $result = WPP_F::json_to_xml( $result, apply_filters( 'wpp::xml::export::serializer_options', array() ) ); //*/ if (!$result) { die(__('There is an Error on trying to create XML feed.', ud_get_wp_property()->domain)); } header('Content-type: text/xml'); header('Content-Disposition: inline; filename="wpp_xml_data.xml"'); } else { $result = json_encode($properties); header('Content-type: application/json'); header('Content-Disposition: inline; filename="wpp_xml_data.json"'); } header("Cache-Control: no-cache"); header("Pragma: no-cache"); die($result); }
/** * Returns property * * @since 1.11 * @todo Code pertaining to displaying data should be migrated to prepare_property_for_display() like :$real_value = nl2br($real_value); * @todo Fix the long dashes - when in latitude or longitude it breaks it when using static map * */ public static function get($id, $args = false) { global $wp_properties; if (is_object($id) && isset($id->ID)) { $id = $id->ID; } $id = trim($id); extract($args = wp_parse_args($args, array('get_children' => 'true', 'return_object' => 'false', 'load_gallery' => 'true', 'load_thumbnail' => 'true', 'load_parent' => 'true', 'cache' => 'true')), EXTR_SKIP); $get_children = isset($get_children) && $get_children === 'true' ? true : false; $return_object = isset($return_object) && $return_object === 'true' ? true : false; $load_gallery = isset($load_gallery) && $load_gallery === 'true' ? true : false; $load_thumbnail = isset($load_thumbnail) && $load_thumbnail === 'true' ? true : false; $load_parent = isset($load_parent) && $load_parent === 'true' ? true : false; $cache = isset($cache) && $cache === 'true' ? true : false; if ($cache && ($property = wp_cache_get($id))) { // Do nothing here since we already have data from cache! } else { $property = array(); $post = get_post($id, ARRAY_A); if ($post['post_type'] != 'property') { return false; } //** Figure out what all the editable attributes are, and get their keys */ $editable_keys = array_keys(array_merge((array) $wp_properties['property_meta'], (array) $wp_properties['property_stats'])); //** Load all meta keys for this object */ if ($keys = get_post_custom($id)) { foreach ($keys as $key => $value) { $attribute = Attributes::get_attribute_data($key); if (!$attribute['multiple']) { $value = $value[0]; } $keyt = trim($key); //** If has _ prefix it's a built-in WP key */ if ('_' == $keyt[0]) { continue; } // Fix for boolean values switch ($value) { case 'true': $real_value = true; //** Converts all "true" to 1 */ break; case 'false': $real_value = false; break; default: $real_value = $value; break; } // Handle keys with multiple values if (count($value) > 1) { $property[$key] = $value; } else { $property[$key] = $real_value; } } } $property = array_merge($property, $post); //** Make sure certain keys were not messed up by custom attributes */ $property['system'] = array(); $property['gallery'] = array(); $property['wpp_gpid'] = \WPP_F::maybe_set_gpid($id); $property['permalink'] = get_permalink($id); //** Make sure property_type stays as slug, or it will break many things: (widgets, class names, etc) */ if (!empty($property['property_type'])) { $property['property_type_label'] = get_property_type($id); } //** If phone number is not set but set globally, we load it into property array here */ if (empty($property['phone_number']) && !empty($wp_properties['configuration']['phone_number'])) { $property['phone_number'] = $wp_properties['configuration']['phone_number']; } //* Get rid of all empty values */ foreach ($property as $key => $item) { //** Don't blank keys starting w/ post_ - this should be converted to use get_attribute_data() to check where data is stored for better check - potanin@UD */ if (strpos($key, 'post_') === 0) { continue; } if (empty($item)) { unset($property[$key]); } } wp_cache_add($id, $property); } /* * Load parent if exists and inherit Parent's atttributes. */ if ($load_parent) { $property = self::extend_property_with_parent($property, $cache); } /* * Load Children and their attributes */ if ($get_children) { $property = self::extend_property_with_children($property, $cache); } /* * Figure out what the thumbnail is, and load all sizes */ if ($load_thumbnail) { $property = array_merge($property, self::get_thumbnail($id, $cache)); } /* * Load all attached images and their sizes */ if ($load_gallery) { $gallery = self::get_images($id, $cache); $property['gallery'] = !empty($gallery) ? $gallery : false; } if (is_array($property)) { ksort($property); } $property = apply_filters('wpp_get_property', $property, $args); //** Convert to object */ if ($return_object) { $property = \WPP_F::array_to_object($property); } return $property; }