id = 'customfields';
$this->label = __( 'Custom Fields', 'propertyhive' );
add_filter( 'propertyhive_settings_tabs_array', array( $this, 'add_settings_page' ), 15 );
add_action( 'propertyhive_sections_' . $this->id, array( $this, 'output_sections' ) );
add_action( 'propertyhive_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'propertyhive_settings_save_' . $this->id, array( $this, 'save' ) );
}
/**
* Get sections
*
* @return array
*/
public function get_sections() {
$sections = array(
'' => __( 'Custom Fields', 'propertyhive' )
);
$residential_active = false;
$residential_sales_active = false;
$residential_lettings_active = false;
$commercial_active = false;
if ( get_option( 'propertyhive_active_departments_sales' ) == 'yes' || get_option( 'propertyhive_active_departments_lettings' ) == 'yes' )
{
$residential_active = true;
if ( get_option( 'propertyhive_active_departments_sales' ) == 'yes' )
{
$residential_sales_active = true;
}
if ( get_option( 'propertyhive_active_departments_lettings' ) == 'yes' )
{
$residential_lettings_active = true;
}
}
if ( get_option( 'propertyhive_active_departments_commercial' ) == 'yes' )
{
$commercial_active = true;
}
// Residential Custom Fields
$sections[ 'availability' ] = __( 'Availabilities', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_availability', array( $this, 'custom_fields_availability_setting' ) );
if ( $residential_active )
{
$sections[ 'property-type' ] = ( $commercial_active ? __( 'Residential ', 'propertyhive' ) . ' ' : '' ) . __( 'Property Types', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_property_type', array( $this, 'custom_fields_property_type_setting' ) );
}
if ( $commercial_active )
{
$sections[ 'commercial-property-type' ] = ( $residential_active ? __( 'Commercial', 'propertyhive' ) . ' ' : '' ) . __( 'Property Types', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_commercial_property_type', array( $this, 'custom_fields_commercial_property_type_setting' ) );
}
$sections[ 'location' ] = __( 'Locations', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_location', array( $this, 'custom_fields_location_setting' ) );
if ( $residential_active )
{
$sections[ 'parking' ] = __( 'Parking', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_parking', array( $this, 'custom_fields_parking_setting' ) );
$sections[ 'outside-space' ] = __( 'Outside Spaces', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_outside_space', array( $this, 'custom_fields_outside_space_setting' ) );
}
if ( $residential_sales_active || $commercial_active )
{
$sections[ 'price-qualifier' ] = __( 'Price Qualifiers', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_price_qualifier', array( $this, 'custom_fields_price_qualifier_setting' ) );
$sections[ 'sale-by' ] = __( 'Sale By', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_sale_by', array( $this, 'custom_fields_sale_by_setting' ) );
}
if ( $residential_active )
{
$sections[ 'tenure' ] = ( $commercial_active ? __( 'Residential', 'propertyhive' ) . ' ' : '' ) . __( 'Tenures', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_tenure', array( $this, 'custom_fields_tenure_setting' ) );
}
if ( $commercial_active )
{
$sections[ 'commercial-tenure' ] = ( $residential_active ? __( 'Commercial', 'propertyhive' ) . ' ' : '' ) . __( 'Tenures', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_commercial_tenure', array( $this, 'custom_fields_commercial_tenure_setting' ) );
}
if ( $residential_lettings_active )
{
$sections[ 'furnished' ] = __( 'Furnished', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_furnished', array( $this, 'custom_fields_furnished_setting' ) );
}
// Other
$sections[ 'marketing-flag' ] = __( 'Marketing Flags', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_marketing_flag', array( $this, 'custom_fields_marketing_flag_setting' ) );
if ( get_option('propertyhive_features_type') == 'checkbox' )
{
$sections[ 'property-feature' ] = __( 'Property Features', 'propertyhive' );
add_action( 'propertyhive_admin_field_custom_fields_property_feature', array( $this, 'custom_fields_property_feature_setting' ) );
}
return $sections;
}
/**
* Get settings array
*
* @return array
*/
public function get_settings() {
global $hide_save_button;
$hide_save_button = true;
$html = '';
$sections = $this->get_sections();
//$sections = array_shift($sections); // Remove 'Custom Fields' from list of custom fields sections
$i = 0;
foreach ($sections as $key => $value)
{
if ($i > 0)
{
$html .= '
' . $value . '
';
}
++$i;
}
return apply_filters( 'propertyhive_custom_fields_settings', array(
array( 'title' => __( 'Custom Fields', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_options' ),
array(
'type' => 'html',
'title' => __( 'Custom Fields', 'propertyhive' ),
'html' => $html
),
array( 'type' => 'sectionend', 'id' => 'custom_field_options'),
));
}
/**
* Output the settings
*/
public function output() {
global $current_section;
if ( $current_section ) {
if (isset($_REQUEST['id'])) // we're either adding or editing
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
switch ($current_section)
{
case "availability": { $settings = $this->get_custom_fields_availability_setting(); break; }
case "availability-delete": { $settings = $this->get_custom_fields_delete($current_id, 'availability', __( 'Availability', 'propertyhive' )); break; }
case "property-type": { $settings = $this->get_custom_fields_property_type_setting(); break; }
case "property-type-delete": { $settings = $this->get_custom_fields_delete($current_id, 'property_type', __( 'Property Type', 'propertyhive' )); break; }
case "commercial-property-type": { $settings = $this->get_custom_fields_commercial_property_type_setting(); break; }
case "commercial-property-type-delete": { $settings = $this->get_custom_fields_delete($current_id, 'commercial_property_type', __( 'Property Type', 'propertyhive' )); break; }
case "location": { $settings = $this->get_custom_fields_location_setting(); break; }
case "location-delete": { $settings = $this->get_custom_fields_delete($current_id, 'location', __( 'Location', 'propertyhive' )); break; }
case "parking": { $settings = $this->get_custom_fields_parking_setting(); break; }
case "parking-delete": { $settings = $this->get_custom_fields_delete($current_id, 'parking', __( 'Parking', 'propertyhive' )); break; }
case "outside-space": { $settings = $this->get_custom_fields_outside_space_setting(); break; }
case "outside-space-delete": { $settings = $this->get_custom_fields_delete($current_id, 'outside_space', __( 'Outside Space', 'propertyhive' )); break; }
case "price-qualifier": { $settings = $this->get_custom_fields_price_qualifier_setting(); break; }
case "price-qualifier-delete": { $settings = $this->get_custom_fields_delete($current_id, 'price_qualifier', __( 'Price Qualifier', 'propertyhive' )); break; }
case "sale-by": { $settings = $this->get_custom_fields_sale_by_setting(); break; }
case "sale-by-delete": { $settings = $this->get_custom_fields_delete($current_id, 'sale_by', __( 'Sale By', 'propertyhive' )); break; }
case "tenure": { $settings = $this->get_custom_fields_tenure_setting(); break; }
case "tenure-delete": { $settings = $this->get_custom_fields_delete($current_id, 'tenure', __( 'Tenure', 'propertyhive' )); break; }
case "commercial-tenure": { $settings = $this->get_custom_fields_commercial_tenure_setting(); break; }
case "commercial-tenure-delete": { $settings = $this->get_custom_fields_delete($current_id, 'commercial_tenure', __( 'Tenure', 'propertyhive' )); break; }
case "furnished": { $settings = $this->get_custom_fields_furnished_setting(); break; }
case "furnished-delete": { $settings = $this->get_custom_fields_delete($current_id, 'furnished', __( 'Furnished', 'propertyhive' )); break; }
case "marketing-flag": { $settings = $this->get_custom_fields_marketing_flag_setting(); break; }
case "marketing-flag-delete": { $settings = $this->get_custom_fields_delete($current_id, 'marketing_flag', __( 'Marketing Flag', 'propertyhive' )); break; }
case "property-feature": { $settings = $this->get_custom_fields_property_feature_setting(); break; }
case "property-feature-delete": { $settings = $this->get_custom_fields_delete($current_id, 'property_feature', __( 'Property Feature', 'propertyhive' )); break; }
default: { echo 'UNKNOWN CUSTOM FIELD'; }
}
PH_Admin_Settings::output_fields( $settings );
}
else
{
global $hide_save_button;
$hide_save_button = true;
// The main custom field screen listing them in a table
$settings = $this->get_custom_fields_setting($current_section);
PH_Admin_Settings::output_fields( $settings );
}
} else {
$settings = $this->get_settings();
PH_Admin_Settings::output_fields( $settings );
}
}
/**
* Output custom fields settings.
*
* @access public
* @return void
*/
public function get_custom_fields_setting($current_section) {
$sections = $this->get_sections();
return apply_filters( 'propertyhive_custom_fields_' . $current_section . '_settings', array(
array( 'title' => $sections[$current_section], 'type' => 'title', 'desc' => '', 'id' => 'custom_fields_' . $current_section . '_options' ),
array(
'type' => 'custom_fields_' . str_replace("-", "_", $current_section),
),
array( 'type' => 'sectionend', 'id' => 'custom_fields_' . $current_section . '_options'),
));
}
/**
* Output list of availabilities
*
* @access public
* @return void
*/
public function custom_fields_availability_setting() {
global $post;
?>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Availability Option' : 'Edit Availability' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_availability_settings' ),
array(
'title' => __( 'Availability', 'propertyhive' ),
'id' => 'availability_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_availability_settings' )
);
return apply_filters( 'propertyhive_custom_field_availability_settings', $args );
}
/**
* Show residential property type add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_property_type_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'property_type';
$term_name = '';
$term_parent = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
$term_parent = $term->parent;
}
$existing_terms = array('' => '(' . __ ( 'no parent', 'propertyhive') . ')');
$args = array(
'hide_empty' => false,
'parent' => 0,
'exclude' => array($current_id)
);
$terms = get_terms( 'property_type', $args );
if ( !empty( $terms ) && !is_wp_error( $terms ) )
{
foreach ($terms as $term)
{
$existing_terms[$term->term_id] = $term->name;
}
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Property Type' : 'Edit Property Type' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_property_type_settings' ),
array(
'title' => __( 'Property Type', 'propertyhive' ),
'id' => 'property_type_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'title' => __( 'Parent', 'propertyhive' ),
'id' => 'parent_property_type_id',
'default' => $term_parent,
'options' => $existing_terms,
'type' => 'select',
'desc_tip' => false,
'desc' => ''
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_property_type_settings' )
);
return apply_filters( 'propertyhive_custom_field_property_type_settings', $args );
}
/**
* Show commercial property type add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_commercial_property_type_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'commercial_property_type';
$term_name = '';
$term_parent = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
$term_parent = $term->parent;
}
$existing_terms = array('' => '(' . __ ( 'no parent', 'propertyhive') . ')');
$args = array(
'hide_empty' => false,
'parent' => 0,
'exclude' => array($current_id)
);
$terms = get_terms( 'commercial_property_type', $args );
if ( !empty( $terms ) && !is_wp_error( $terms ) )
{
foreach ($terms as $term)
{
$existing_terms[$term->term_id] = $term->name;
}
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Property Type' : 'Edit Property Type' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_commercial_property_type_settings' ),
array(
'title' => __( 'Property Type', 'propertyhive' ),
'id' => 'commercial_property_type_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'title' => __( 'Parent', 'propertyhive' ),
'id' => 'parent_commercial_property_type_id',
'default' => $term_parent,
'options' => $existing_terms,
'type' => 'select',
'desc_tip' => false,
'desc' => ''
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_commercial_property_type_settings' )
);
return apply_filters( 'propertyhive_custom_field_commercial_property_type_settings', $args );
}
/**
* Show location add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_location_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'location';
$term_name = '';
$term_parent = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
$term_parent = $term->parent;
}
$existing_terms = array('' => '(' . __ ( 'no parent', 'propertyhive') . ')');
$args = array(
'hide_empty' => false,
'parent' => 0,
'exclude' => array($current_id)
);
$terms = get_terms( $taxonomy, $args );
if ( !empty( $terms ) && !is_wp_error( $terms ) )
{
foreach ($terms as $term)
{
$existing_terms[$term->term_id] = '- '.$term->name;
$args = array(
'hide_empty' => false,
'parent' => $term->term_id,
'exclude' => array($current_id)
);
$terms = get_terms( $taxonomy, $args );
if ( !empty( $terms ) && !is_wp_error( $terms ) )
{
foreach ($terms as $term)
{
$existing_terms[$term->term_id] = '- - '.$term->name;
}
}
}
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Location' : 'Edit Location' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_location_settings' ),
array(
'title' => __( 'Location', 'propertyhive' ),
'id' => 'location_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'title' => __( 'Parent', 'propertyhive' ),
'id' => 'parent_location_id',
'default' => $term_parent,
'options' => $existing_terms,
'type' => 'select',
'desc_tip' => false,
'desc' => ''
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_location_settings' )
);
return apply_filters( 'propertyhive_custom_field_location_settings', $args );
}
/**
* Show parking add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_parking_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'parking';
$term_name = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Parking Option' : 'Edit Parking' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_parking_settings' ),
array(
'title' => __( 'Parking', 'propertyhive' ),
'id' => 'parking_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_parking_settings' )
);
return apply_filters( 'propertyhive_custom_field_parking_settings', $args );
}
/**
* Show outside space add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_outside_space_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'outside_space';
$term_name = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Outside Space' : 'Edit Outside Space' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_outside_space_settings' ),
array(
'title' => __( 'Outside Space', 'propertyhive' ),
'id' => 'outside_space_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_outside_space_settings' )
);
return apply_filters( 'propertyhive_custom_field_outside_space_settings', $args );
}
/**
* Show custom field delete options
*
* @access public
* @return string
*/
public function get_custom_fields_delete($current_id, $taxonomy, $taxonomy_name)
{
global $save_button_text;
$save_button_text = __( 'Delete', 'propertyhive' );
//$taxonomy = 'outside_space';
//$taxonomy_name = __( 'Outside Space', 'propertyhive' );
if ( isset($_POST['confirm_removal']) && $_POST['confirm_removal'] == 1 )
{
// A term has just been deleted
global $hide_save_button, $show_cancel_button, $cancel_button_href;
$hide_save_button = TRUE;
$show_cancel_button = TRUE;
$cancel_button_href = admin_url( 'admin.php?page=ph-settings&tab=customfields§ion=' . str_replace("_", "-", $taxonomy) );
$args = array();
$args[] = array( 'title' => __( 'Successfully Deleted', 'propertyhive' ) . ' ' . $taxonomy_name, 'type' => 'title', 'desc' => '', 'id' => 'custom_field_' . $taxonomy . '_delete' );
$args[] = array(
'title' => __( 'Term Deleted', 'propertyhive' ),
'id' => '',
'html' => $taxonomy_name . __(' deleted successfully', 'propertyhive' ) . ' ' . __( 'Go Back', 'propertyhive' ) . '',
'type' => 'html',
'desc_tip' => false,
);
$args[] = array( 'type' => 'sectionend', 'id' => 'custom_field_' . $taxonomy . '_delete' );
}
else
{
$term_name = '';
if ($current_id == '')
{
die("ID not passed");
}
else
{
$term_ids = explode("-", $current_id);
$args = array();
foreach ( $term_ids as $current_id )
{
$term = get_term( $current_id, $taxonomy );
if ( is_null($term) || is_wp_error($term) )
{
die("Invalid term trying to be deleted");
}
else
{
$term_name = $term->name;
$args[] = array( 'title' => __( 'Delete', 'propertyhive' ) . ' ' . $taxonomy_name . ': ' . $term_name, 'type' => 'title', 'desc' => '', 'id' => 'custom_field_' . $taxonomy . '_' . $current_id . '_delete' );
// Get number of properties assigned to this term
$query_args = array(
'post_type' => 'property',
'nopaging' => true,
'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ),
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $current_id,
),
),
);
$property_query = new WP_Query( $query_args );
$num_properties = $property_query->found_posts;
wp_reset_postdata();
// Get number of applicants assigned to this term
$num_applicants = 0;
if ( $taxonomy == 'property_type' || $taxonomy == 'commercial_property_type' || $taxonomy == 'location' )
{
$query_args = array(
'post_type' => 'contact',
'nopaging' => true,
'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ),
'meta_query' => array(
array(
'key' => '_contact_types',
'value' => 'applicant',
'compare' => 'LIKE'
),
),
);
$applicant_query = new WP_Query( $query_args );
if ( $applicant_query->have_posts() )
{
while ( $applicant_query->have_posts() )
{
$applicant_query->the_post();
$applicant_has_taxonomy = false;
$num_applicant_profiles = get_post_meta( get_the_ID(), '_applicant_profiles', TRUE );
if ( $num_applicant_profiles == '' )
{
$num_applicant_profiles = 0;
}
if ( $num_applicant_profiles > 0 )
{
for ( $i = 0; $i < $num_applicant_profiles; ++$i )
{
$applicant_profile = get_post_meta( get_the_ID(), '_applicant_profile_' . $i, TRUE );
if ( isset($applicant_profile[$taxonomy.'s']) && is_array($applicant_profile[$taxonomy.'s']) && !empty($applicant_profile[$taxonomy.'s']) )
{
if (in_array($current_id, $applicant_profile[$taxonomy.'s']))
{
$applicant_has_taxonomy = true;
}
}
}
}
if ( $applicant_has_taxonomy )
{
++$num_applicants;
}
}
}
wp_reset_postdata();
}
if ($num_properties > 0 || $num_applicants > 0)
{
$alternative_terms = array();
$alternative_terms['none'] = '-- ' . __( 'Don\'t Reassign', 'propertyhive' ) . ' --';
$term_args = array(
'hide_empty' => false,
'exclude' => $term_ids,
'parent' => 0
);
$terms = get_terms( $taxonomy, $term_args );
if ( !empty( $terms ) && !is_wp_error( $terms ) )
{
foreach ($terms as $term)
{
$alternative_terms[$term->term_id] = $term->name;
$term_args = array(
'hide_empty' => false,
'exclude' => $term_ids,
'parent' => $term->term_id
);
$subterms = get_terms( $taxonomy, $term_args );
if ( !empty( $subterms ) && !is_wp_error( $subterms ) )
{
foreach ($subterms as $term)
{
$alternative_terms[$term->term_id] = '- ' . $term->name;
$term_args = array(
'hide_empty' => false,
'exclude' => $term_ids,
'parent' => $term->term_id
);
$subsubterms = get_terms( $taxonomy, $term_args );
if ( !empty( $subsubterms ) && !is_wp_error( $subsubterms ) )
{
foreach ($subsubterms as $term)
{
$alternative_terms[$term->term_id] = '- - ' . $term->name;
}
}
}
}
}
}
// There are properties assigned to this term
$args[] = array(
'title' => __( 'Re-assign to', 'propertyhive' ),
'id' => 'reassign_to_' . $current_id,
'default' => '',
'options' => $alternative_terms,
'type' => 'select',
'desc_tip' => false,
'desc' => __( 'There are properties or applicants that have this term assigned to them. Which, if any, term should they be reassigned to?' , 'propertyhive' )
);
}
else
{
// There are properties assigned to this term
$args[] = array(
'title' => __( 'Re-assign to', 'propertyhive' ),
'id' => 'reassign_to',
'type' => 'html',
'html' => __( 'No properties or applicants assigned to this term' , 'propertyhive' )
);
}
$args[] = array( 'type' => 'sectionend', 'id' => 'custom_field_' . $taxonomy . '_' . $current_id . '_delete' );
}
}
$args[] = array( 'title' => __( 'Confirm Removal', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_confirm_delete' );
$args[] = array(
'title' => __( 'Confirm Removal?', 'propertyhive' ),
'id' => 'confirm_removal',
'type' => 'checkbox',
'desc_tip' => false,
);
$args[] = array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
);
$args[] = array( 'type' => 'sectionend', 'id' => 'custom_field_confirm_delete' );
}
}
return apply_filters( 'propertyhive_custom_field_' . $taxonomy . '_delete', $args );
}
/**
* Show price qualifier add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_price_qualifier_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'price_qualifier';
$term_name = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Price Qualifier' : 'Edit Price Qualifier' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_price_qualifier_settings' ),
array(
'title' => __( 'Price Qualifier', 'propertyhive' ),
'id' => 'price_qualifier_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_price_qualifier_settings' )
);
return apply_filters( 'propertyhive_custom_field_price_qualifier_settings', $args );
}
/**
* Show sale by add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_sale_by_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'sale_by';
$term_name = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Sale By' : 'Edit Sale By' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_sale_by_settings' ),
array(
'title' => __( 'Sale By', 'propertyhive' ),
'id' => 'sale_by_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_sale_by_settings' )
);
return apply_filters( 'propertyhive_custom_field_sale_by_settings', $args );
}
/**
* Show tenure add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_tenure_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'tenure';
$term_name = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Tenure' : 'Edit Tenure' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_tenure_settings' ),
array(
'title' => __( 'Tenure', 'propertyhive' ),
'id' => 'tenure_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_tenure_settings' )
);
return apply_filters( 'propertyhive_custom_field_tenure_settings', $args );
}
/**
* Show commercial tenure add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_commercial_tenure_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'commercial_tenure';
$term_name = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Tenure' : 'Edit Tenure' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_commercial_tenure_settings' ),
array(
'title' => __( 'Tenure', 'propertyhive' ),
'id' => 'commercial_tenure_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_commercial_tenure_settings' )
);
return apply_filters( 'propertyhive_custom_field_commercial_tenure_settings', $args );
}
/**
* Show furnished add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_furnished_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'furnished';
$term_name = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Furnished' : 'Edit Furnished' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_furnished_settings' ),
array(
'title' => __( 'Furnished', 'propertyhive' ),
'id' => 'furnished_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_furnished_settings' )
);
return apply_filters( 'propertyhive_custom_field_furnished_settings', $args );
}
/**
* Show marketing flag add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_marketing_flag_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'marketing_flag';
$term_name = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Marketing Flag' : 'Edit Marketing Flag' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_marketing_flag_settings' ),
array(
'title' => __( 'Marketing Flag', 'propertyhive' ),
'id' => 'marketing_flag_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_marketing_flag_settings' )
);
return apply_filters( 'propertyhive_custom_field_marketing_flag_settings', $args );
}
/**
* Show property feature add/edit options
*
* @access public
* @return string
*/
public function get_custom_fields_property_feature_setting()
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
$taxonomy = 'property_feature';
$term_name = '';
if ($current_id != '')
{
$term = get_term( $current_id, $taxonomy );
$term_name = $term->name;
}
$args = array(
array( 'title' => __( ( $current_id == '' ? 'Add New Property Feature' : 'Edit Property Feature' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'custom_field_property_feature_settings' ),
array(
'title' => __( 'Property Feature', 'propertyhive' ),
'id' => 'property_feature_name',
'default' => $term_name,
'type' => 'text',
'desc_tip' => false,
),
array(
'type' => 'hidden',
'id' => 'taxonomy',
'default' => $taxonomy
),
array( 'type' => 'sectionend', 'id' => 'custom_field_property_feature_settings' )
);
return apply_filters( 'propertyhive_custom_field_property_feature_settings', $args );
}
/**
* Save settings
*/
public function save() {
global $current_section, $post;
if ( $current_section != '' )
{
if (isset($_REQUEST['id'])) // we're either adding or editing
{
$current_id = empty( $_REQUEST['id'] ) ? '' : sanitize_title( $_REQUEST['id'] );
switch ($current_section)
{
// With heirarchy
case "property-type":
case "commercial-property-type":
case "location":
{
// TODO: Validate (check for blank fields)
if ($current_id == '')
{
// Adding new term
// TODO: Check term doesn't exist already
wp_insert_term(
$_POST[$_POST['taxonomy'] . '_name'], // the term
$_POST['taxonomy'], // the taxonomy
array(
'parent' => $_POST['parent_' . $_POST['taxonomy'] . '_id']
)
);
// TODO: Check for errors returned from wp_insert_term()
}
else
{
// Editing term
wp_update_term($current_id, $_POST['taxonomy'], array(
'name' => $_POST[$_POST['taxonomy'].'_name'],
'parent' => $_POST['parent_' . $_POST['taxonomy'] . '_id']
));
// TODO: Check for errors returned from wp_update_term()
}
break;
}
// Without heirarchy
case "availability":
case "outside-space":
case "parking":
case "price-qualifier":
case "sale-by":
case "tenure":
case "commercial-tenure":
case "furnished":
case "marketing-flag":
case "property-feature":
{
// TODO: Validate (check for blank fields)
if ($current_id == '')
{
// Adding new term
// TODO: Check term doesn't exist already
wp_insert_term(
$_POST[$_POST['taxonomy'] . '_name'], // the term
$_POST['taxonomy'] // the taxonomy
);
// TODO: Check for errors returned from wp_insert_term()
}
else
{
// Editing term
wp_update_term($current_id, $_POST['taxonomy'], array(
'name' => $_POST[$_POST['taxonomy'] . '_name']
));
// TODO: Check for errors returned from wp_update_term()
}
break;
}
case "availability-delete":
case "property-type-delete":
case "commercial-property-type-delete":
case "location-delete":
case "parking-delete":
case "outside-space-delete":
case "price-qualifier-delete":
case "sale-by-delete":
case "tenure-delete":
case "furnished-delete":
case "marketing-flag-delete":
case "property-feature-delete":
{
if ( isset($_POST['confirm_removal']) && $_POST['confirm_removal'] == '1' )
{
$term_ids = explode("-", $current_id);
foreach ( $term_ids as $current_id )
{
// Update properties that have this taxonomy term set
$query_args = array(
'post_type' => 'property',
'nopaging' => true,
'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ),
'tax_query' => array(
array(
'taxonomy' => $_POST['taxonomy'],
'field' => 'id',
'terms' => $current_id,
),
),
);
$property_query = new WP_Query( $query_args );
if ( $property_query->have_posts() )
{
while ( $property_query->have_posts() )
{
$property_query->the_post();
wp_remove_object_terms( $post->ID, $current_id, $_POST['taxonomy'] );
// Re-assign to another term
if ( isset($_POST['reassign_to_' . $current_id]) && ! empty( $_POST['reassign_to_' . $current_id] ) && $_POST['reassign_to_' . $current_id] != 'none' )
{
$new_id = $_POST['reassign_to_' . $current_id];
wp_set_post_terms( $post->ID, $new_id, $_POST['taxonomy'], TRUE );
// TODO: Check for WP_ERROR
}
}
}
wp_reset_postdata();
if ( $_POST['taxonomy'] == 'property_type' || $_POST['taxonomy'] == 'commercial_property_type' || $_POST['taxonomy'] == 'location' )
{
$query_args = array(
'post_type' => 'contact',
'nopaging' => true,
'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ),
'meta_query' => array(
array(
'key' => '_contact_types',
'value' => 'applicant',
'compare' => 'LIKE'
),
),
);
$applicant_query = new WP_Query( $query_args );
if ( $applicant_query->have_posts() )
{
while ( $applicant_query->have_posts() )
{
$applicant_query->the_post();
$num_applicant_profiles = get_post_meta( get_the_ID(), '_applicant_profiles', TRUE );
if ( $num_applicant_profiles == '' )
{
$num_applicant_profiles = 0;
}
if ( $num_applicant_profiles > 0 )
{
for ( $i = 0; $i < $num_applicant_profiles; ++$i )
{
$applicant_profile = get_post_meta( get_the_ID(), '_applicant_profile_' . $i, TRUE );
if ( isset($applicant_profile[$_POST['taxonomy'].'s']) && is_array($applicant_profile[$_POST['taxonomy'].'s']) && !empty($applicant_profile[$_POST['taxonomy'].'s']) )
{
if (in_array($current_id, $applicant_profile[$_POST['taxonomy'].'s']))
{
// This profile has this term set
unset($applicant_profile[$_POST['taxonomy'].'s'][$current_id]);
if ( isset($_POST['reassign_to_' . $current_id]) && ! empty( $_POST['reassign_to_' . $current_id] ) && $_POST['reassign_to_' . $current_id] != 'none' )
{
$applicant_profile[$_POST['taxonomy'].'s'][] = $_POST['reassign_to_' . $current_id];
$applicant_profile[$_POST['taxonomy'].'s'] = array_unique($applicant_profile[$_POST['taxonomy'].'s']);
}
$applicant_profile[$_POST['taxonomy'].'s'] = array_values($applicant_profile[$_POST['taxonomy'].'s']);
update_post_meta( get_the_ID(), '_applicant_profile_' . $i, $applicant_profile );
}
}
}
}
}
}
}
wp_reset_postdata();
wp_delete_term( $current_id, $_POST['taxonomy'] );
}
}
break;
}
default: { echo 'UNKNOWN CUSTOM FIELD'; }
}
}
else
{
// Nothing to save. Should always be an id set when editing custom fields.
// Even blank ids dictate something is being added
}
}
else
{
// Nothing to save. Should always be a section when editing custom fields
}
}
}
endif;
return new PH_Settings_Custom_Fields();