/**
  * Prepare Property Meta Box based on Attributes' Groups for registration.
  *
  * @since 2.0
  * @author peshkov@UD
  */
 public function get_property_meta_box($group = array(), $post = false)
 {
     $group = wp_parse_args($group, array('id' => false, 'name' => __('NO NAME', ud_get_wp_property()->domain)));
     $fields = array();
     /**
      * Get all data we need to operate with.
      */
     $attributes = ud_get_wp_property('property_stats', array());
     $geo_type_attributes = ud_get_wp_property('geo_type_attributes', array());
     $hidden_attributes = ud_get_wp_property('hidden_attributes', array());
     $inherited_attributes = ud_get_wp_property('property_inheritance', array());
     $property_stats_groups = ud_get_wp_property('property_stats_groups', array());
     $predefined_values = ud_get_wp_property('predefined_values', array());
     $descriptions = ud_get_wp_property('descriptions', array());
     $input_types = ud_get_wp_property('admin_attr_fields');
     //** Detect attributes that were taken from a range of child properties. */
     $aggregated_attributes = !empty($post->system['upwards_inherited_attributes']) ? (array) $post->system['upwards_inherited_attributes'] : array();
     /**
      * If group ID is not defined, it means that we're registering main Meta Box
      * So, here, we're adding custom fields for management!
      */
     if ($group['id'] == false) {
         /* May be add Property Parent field - 'Falls Under' */
         $field = $this->get_parent_property_field($post);
         if ($field) {
             $fields[] = $field;
         }
         /* May be add Property Type field. */
         if (!array_key_exists('property_type', $attributes)) {
             $field = $this->get_property_type_field($post);
             if ($field) {
                 $fields[] = $field;
             }
         }
         /* May be add Meta fields */
         foreach (ud_get_wp_property()->get('property_meta', array()) as $slug => $label) {
             $field = apply_filters('wpp::rwmb_meta_box::field', array_filter(array('id' => $slug, 'name' => $label, 'type' => 'textarea', 'desc' => __('Meta description.', ud_get_wp_property()->domain))), $slug, $post);
             if ($field) {
                 $fields[] = $field;
             }
         }
     }
     /**
      * Loop through all available attributes and determine if any of them must be added to current meta box.
      */
     foreach ($attributes as $slug => $label) {
         /**
          * Determine if we should add attribute's field in this meta box
          */
         //** Show ( or not ) attribute field on Edit property page for current Property. */
         if (!apply_filters('wpp::metabox::attribute::show', true, $slug, $post->ID)) {
             continue;
         }
         //* Determine if attribute is assigned to group. */
         if (!empty($property_stats_groups[$slug]) && $property_stats_groups[$slug] != $group['id']) {
             continue;
         }
         //** Do not show attribute in group's meta box if it's not assigned to groups at all */
         if (empty($property_stats_groups[$slug]) && !empty($group['id'])) {
             continue;
         }
         //* Ignore Hidden Attributes */
         if (!empty($post->property_type) && !empty($hidden_attributes[$post->property_type]) && in_array($slug, (array) $hidden_attributes[$post->property_type])) {
             continue;
         }
         /**
          * Looks like we have to add attribute's field to the current meta box.
          * So, setup data for it now.
          */
         //* HACK. If property_type is set as attribute, we register it here. */
         if ($slug == 'property_type') {
             $field = $this->get_property_type_field($post);
             if ($field) {
                 $fields[] = $field;
             }
             continue;
         }
         $attribute = Attributes::get_attribute_data($slug);
         $description = array();
         $description[] = isset($attribute['numeric']) || isset($attribute['currency']) ? __('Numbers only.', ud_get_wp_property()->domain) : '';
         $description[] = !empty($descriptions[$slug]) ? $descriptions[$slug] : '';
         /**
          * PREPARE INPUT TYPE
          * May be convert input_type to valid type.
          */
         $input_type = !empty($input_types[$slug]) ? $input_types[$slug] : 'text';
         //* Geo Attributes */
         if (in_array($slug, $geo_type_attributes)) {
             $input_type = 'wpp_readonly';
             $description[] = __('The value is being generated automatically on Google Address Validation.', ud_get_wp_property()->domain);
         }
         //* Legacy compatibility */
         if (in_array($input_type, array('input'))) {
             $input_type = 'text';
         }
         //* Legacy compatibility */
         if (in_array($input_type, array('dropdown'))) {
             $input_type = 'select';
         }
         //* Legacy compatibility */
         if (in_array($input_type, array('checkbox'))) {
             $input_type = 'wpp_checkbox';
         }
         //* Legacy compatibility */
         if (in_array($input_type, array('multi_checkbox'))) {
             $input_type = 'checkbox_list';
         }
         //* Fix currency */
         if ($input_type == 'currency') {
             $input_type = 'text';
             // HTML5 does not allow to use float, so we have to use default 'text' here
             $description[] = __('Currency.', ud_get_wp_property('domain'));
         }
         if ($input_type == 'number') {
             $input_type = 'text';
             // HTML5 does not allow to use float, so we have to use default 'text' here
         }
         //** Determine if current attribute is used by Google Address Validator. */
         if (ud_get_wp_property('configuration.address_attribute') == $slug) {
             $input_type = 'wpp_address';
             // Too obvious, I believe. -potanin@UD
             // $description[] = __( 'The value is being used by Google Address Validator to determine and prepare address to valid format. However you can set coordinates manually.', ud_get_wp_property()->domain );
         }
         //* Is current attribute inherited from parent? If so, set it as readonly!. */
         if (isset($post->post_parent) && $post->post_parent > 0 && isset($post->property_type) && !empty($inherited_attributes[$post->property_type]) && in_array($slug, $inherited_attributes[$post->property_type])) {
             $input_type = 'wpp_inherited';
             $description[] = sprintf(__('The value is inherited from Parent %s.', ud_get_wp_property()->domain), \WPP_F::property_label());
         }
         //** Is current attribute's value aggregated from child properties? If so, set it as readonly! */
         if (!empty($aggregated_attributes) && in_array($slug, $aggregated_attributes)) {
             $input_type = 'wpp_aggregated';
             $description[] = sprintf(__('The value is aggregated from Child %s.', ud_get_wp_property()->domain), \WPP_F::property_label('plural'));
         }
         //** Determine if current attribute is used by Google Address Validator. */
         if (ud_get_wp_property('configuration.address_attribute') == $slug) {
             if ($input_type == 'wpp_inherited') {
                 $input_type = 'wpp_inherited_address';
             } else {
                 $input_type = 'wpp_address';
                 //$description[] = __( 'The value is being used by Google Address Validator to determine and prepare address to valid format. However you can set coordinates manually.', ud_get_wp_property()->domain );
             }
         }
         /**
          * Check for pre-defined values
          */
         $options = array();
         if ($input_type == 'select') {
             $options[''] = __('Not Selected', ud_get_wp_property()->domain);
         }
         if (!empty($predefined_values[$slug]) && is_string($predefined_values[$slug])) {
             $_options = explode(',', trim($predefined_values[$slug]));
             foreach ($_options as $option) {
                 $option = trim(preg_replace("/\r|\n/", "", $option));
                 $options[esc_attr($option)] = apply_filters('wpp_stat_filter_' . $slug, $option);
             }
         }
         /**
          * Well, init field now.
          */
         $fields[] = apply_filters('wpp::rwmb_meta_box::field', array_filter(array('id' => $slug, 'name' => $label, 'type' => $input_type, 'desc' => implode(' ', (array) $description), 'options' => $options)), $slug, $post);
     }
     $meta_box = apply_filters('wpp::rwmb_meta_box', array('id' => !empty($group['id']) ? $group['id'] : '_general', 'title' => $group['name'], 'pages' => array('property'), 'context' => 'normal', 'priority' => 'high', 'fields' => $fields), $group, $post);
     return $meta_box;
 }
 /**
  * Return Overview Information
  *
  * @param $post
  * @return mixed|string
  */
 public function column_overview($post)
 {
     $data = '';
     $attributes = ud_get_wp_property('property_stats');
     $stat_count = 0;
     $hidden_count = 0;
     $display_stats = array();
     $meta = get_post_custom($post->ID);
     foreach ($meta as $k => $value) {
         if (!array_key_exists($k, $attributes)) {
             continue;
         }
         //** If has _ prefix it's a built-in WP key */
         if ('_' == $k[0]) {
             continue;
         }
         $attribute = Attributes::get_attribute_data($k);
         if (!$attribute['multiple']) {
             $value = $value[0];
         }
         $value = apply_filters("wpp::attribute::display", $value, $k);
         $value = apply_filters("wpp_stat_filter_{$k}", $value);
         $stat_count++;
         $stat_row_class = '';
         if ($stat_count > 5) {
             $stat_row_class = 'hidden wpp_overview_hidden_stats';
             $hidden_count++;
         }
         $display_stats[$k] = '<li class="' . $stat_row_class . '"><span class="wpp_label">' . $attributes[$k] . ':</span> <span class="wpp_value">' . $value . '</span></li>';
     }
     if (is_array($display_stats) && count($display_stats) > 0) {
         if ($stat_count > 5) {
             $display_stats['toggle_advanced'] = '<li class="wpp_show_advanced" advanced_option_class="wpp_overview_hidden_stats">' . sprintf(__('Toggle %1s more.', ud_get_wp_property()->domain), $hidden_count) . '</li>';
         }
         $data = '<ul class="wpp_overview_column_stats wpp_something_advanced_wrapper">' . implode('', $display_stats) . '</ul>';
     }
     return $data;
 }
 /**
  * Extends particular property with children data.
  * Internal method! Do not use it directly.
  *
  * @param $property
  * @param bool $cache
  * @return array
  */
 static function extend_property_with_children($property, $cache = true)
 {
     global $wpdb, $wp_properties;
     if (empty($property['ID'])) {
         return $property;
     }
     if ($cache && ($data = wp_cache_get($property['ID'], 'property_children'))) {
         // Do nothing here.
     } else {
         $data = array();
         //** Calculate variables if based off children if children exist */
         $children = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE  post_type = 'property' AND post_status = 'publish' AND post_parent = '{$property['ID']}' ORDER BY menu_order ASC ");
         if (count($children) > 0) {
             $range = array();
             //** Cycle through children and get necessary variables */
             foreach ($children as $child_id) {
                 $child_object = self::get($child_id, array('get_children' => 'false', 'load_parent' => 'false', 'load_gallery' => 'true', 'load_thumbnail' => 'true', 'cache' => 'false'));
                 $data['children'][$child_id] = $child_object;
                 //** Save child image URLs into one array for quick access */
                 if (!empty($child_object['featured_image_url'])) {
                     $data['system']['child_images'][$child_id] = $child_object['featured_image_url'];
                 }
                 //** Exclude variables from searchable attributes (to prevent ranges) */
                 $excluded_attributes = $wp_properties['geo_type_attributes'];
                 $excluded_attributes[] = $wp_properties['configuration']['address_attribute'];
                 foreach ($wp_properties['searchable_attributes'] as $searchable_attribute) {
                     $attribute_data = Attributes::get_attribute_data($searchable_attribute);
                     if (!empty($attribute_data['numeric']) || !empty($attribute_data['currency'])) {
                         if (!empty($child_object[$searchable_attribute]) && !in_array($searchable_attribute, $excluded_attributes)) {
                             $range[$searchable_attribute][] = $child_object[$searchable_attribute];
                         }
                     }
                 }
             }
             //* Cycle through every type of range (i.e. price, deposit, bathroom, etc) and fix-up the respective data arrays */
             foreach ((array) $range as $range_attribute => $range_values) {
                 //* Cycle through all values of this range (attribute), and fix any ranges that use dashes */
                 foreach ($range_values as $key => $single_value) {
                     //* Remove dollar signs */
                     $single_value = str_replace("\$", '', $single_value);
                     //* Fix ranges */
                     if (strpos($single_value, '&ndash;')) {
                         $split = explode('&ndash;', $single_value);
                         foreach ($split as $new_single_value) {
                             if (!empty($new_single_value)) {
                                 array_push($range_values, trim($new_single_value));
                             }
                         }
                         //* Unset original value with dash */
                         unset($range_values[$key]);
                     }
                 }
                 //* Remove duplicate values from this range */
                 $range[$range_attribute] = array_unique($range_values);
                 //* Sort the values in this particular range */
                 sort($range[$range_attribute]);
                 if (count($range[$range_attribute]) < 2) {
                     if (!isset($property[$range_attribute])) {
                         $property[$range_attribute] = '';
                     }
                     $data[$range_attribute] = $property[$range_attribute] . ' ( ' . $range[$range_attribute][0] . ' )';
                 }
                 if (count($range[$range_attribute]) > 1) {
                     if (!isset($property[$range_attribute])) {
                         $property[$range_attribute] = '';
                     }
                     $data[$range_attribute] = $property[$range_attribute] . ' ( ' . min($range[$range_attribute]) . " - " . max($range[$range_attribute]) . ' )';
                 }
                 //** If we end up with a range, we make a note of it */
                 if (!empty($data[$range_attribute])) {
                     $data['system']['upwards_inherited_attributes'][] = $range_attribute;
                 }
             }
         }
         wp_cache_add($property['ID'], $data, 'property_children');
     }
     $property = array_merge($property, $data);
     return $property;
 }