Skip to content

Instantly share code, notes, and snippets.

@m-torin
Last active November 5, 2017 17:13
Show Gist options
  • Save m-torin/1005106 to your computer and use it in GitHub Desktop.
Save m-torin/1005106 to your computer and use it in GitHub Desktop.
cpt
<?php
error_reporting( E_ALL & ~E_NOTICE );
function VAlpha_search( $query ) {
global $wpdb, $custom_fields;
if( is_admin() )
return;
if( ! isset( $_POST['vin'] ) )
return;
$vin = $_POST['vin'];
if( $vin ) {
if( ! isset( $_REQUEST['vin-type'] ) ) {
$vin_type = 'inventory';
} else if ( in_array( $_REQUEST['vin-type'], array( 'inventory', 'appraisal' ) ) ) {
$vin_type = $_REQUEST['vin-type'];
} else {
$vin_type = 'inventory';
}
$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID from $wpdb->posts WHERE post_type='%s' AND post_title='%s'", $vin_type, $vin ) );
if( $post_id ) {
// The VIN already exists, redirect
wp_redirect( get_permalink( $post_id ), 301 );
exit();
}
$DataOne = new DataOne();
$VinQuery = new VinQuery();
$error_page = get_option( 'VALPHA_ERROR_PAGE' ) ? get_permalink( get_option( 'VALPHA_ERROR_PAGE' ) ) : home_url( '/' );
$DataOne->build_request( $vin );
$VinQuery->build_request( $vin );
if( ! $DataOne->build_request( $vin ) && ! $VinQuery->build_request( $vin ) ) {
// All the build requests failed, redirect to error page.
wp_redirect( $error_page );
exit( );
}
$DataOne->load_results();
if( $DataOne->results && empty( $DataOne->results->decoder_error->error_code ) && empty( $DataOne->results->vin_number->vin_error->error_code ) ) {
$data = array();
foreach( $DataOne->results->vin_number->available_vehicle_styles->vehicle_style as $test ) {
$data['basic_data'][] = array_filter( (array)$test->basic_data, 'strip_empty_var' );
$data['specifications'][] = $test->specifications;
$data['features'][] = $test->features;
$data['engines'][] = $test->engines;
$data['transmissions'][] = $test->transmissions;
}
$basic_data = array_merge_special( $data['basic_data'] );
$features = array_cast_features( $data['features'] );
$specifications = array_cast_specifications( $data['specifications'] );
$fuel_efficiency_ratings = array_cast_specifications( $data['fuel_efficiency_ratings'] );
/* Custom field arrays */
$_dappcf_is_details = array();
$_dappcf_i_details = array();
$_dappcf_i_basics = array();
$_dappcf_is_fuel = array();
$_dappcf_is_mechanical = array();
$_dappcf_is_engine = array();
$_dappcf_is_enginespecs = array();
$_dappcf_is_trans = array();
$_dappcf_is_tirewheel = array();
$_dappcf_is_measure = array();
$_dappcf_is_measureint = array();
$_dappcf_is_interior = array();
$_dappcf_is_seating = array();
$_dappcf_is_media = array();
$_dappcf_is_exterior = array();
$_dappcf_is_safety = array();
$_dappcf_is_towing = array();
/**/ $_dappcf_i_options = array();
$modelyear = $basic_data['year'];
$model = $basic_data['model'];
if( ! $modelyear || ! $model ) {
return false;
}
$make = $basic_data['make'];
$modelnumber = $basic_data['model_number'];
$doors = $basic_data['doors'];
$vehicletype = $basic_data['vehicle_type'];
$body = $basic_data['body_type'];
$transition = array();
foreach( $data['transmissions'] as $transmissions ) {
foreach( $transmissions->children() as $transmission ) {
if( $transmission->type )
$transition[] = (string)$transmission->type;
}
}
$transition = array_unique( $transition );
$enginetype = $features['Engine']['Type'];
$upholstery = $features['Seats']['Upholstery'];
$_dappcf_i_basics['style'] = $basic_data['style'];
$_dappcf_i_basics['Driven Wheels'] = $specifications['Driven Wheels'];
$data['engines'] = ( ! empty ( $data['engines'] ) ) ? $data['engines'] : array();
foreach( $data['engines'] as $engines ) {
foreach( $engines->children() as $child ) {
$attrs = $child->attributes();
$_dappcf_is_details['order_code'][] = (string)$child->order_code;
$_dappcf_is_details['marketing_name'][] = (string)$child->marketing_name;
$_dappcf_i_details['engine_id'][] = (string)$attrs->id;
$_dappcf_is_fuel['fuel_induction'][] = (string)$child->fuel_induction;
$_dappcf_is_fuel['fuel_quality'][] = (string)$child->fuel_quality;
$_dappcf_is_fuel['fuel_type'][] = (string)$child->fuel_type;
$_dappcf_is_engine['brand'][] = (string)$attrs->brand;
$_dappcf_is_engine['aspiration'][] = (string)$child->aspiration;
$_dappcf_is_engine['block_type'][] = (string)$child->block_type;
$_dappcf_is_engine['cylinders'][] = (string)$child->cylinders;
$_dappcf_is_enginespecs['bore'][] = (string)$child->bore;
$_dappcf_is_enginespecs['cam_type'][] = (string)$child->cam_type;
$_dappcf_is_enginespecs['compression'][] = (string)$child->compression;
$_dappcf_is_enginespecs['max_hp'][] = (string)$child->max_hp;
$_dappcf_is_enginespecs['max_hp_at'][] = (string)$child->max_hp_at;
$_dappcf_is_enginespecs['max_torque'][] = (string)$child->max_torque;
$_dappcf_is_enginespecs['max_torque_at'][] = (string)$child->max_torque_at;
$_dappcf_is_enginespecs['oil_capacity'][] = (string)$child->oil_capacity;
$_dappcf_is_enginespecs['valve_timing'][] = (string)$child->valve_timing;
$_dappcf_is_enginespecs['valves'][] = (string)$child->valves;
$_dappcf_is_enginespecs['Compression Ratio'][] = (string)$child->compression;
$_dappcf_is_enginespecs['displacement'][] = (string)$child->displacement;
$_dappcf_is_enginespecs['stroke'][] = (string)$child->stroke;
$_dappcf_is_enginespecs['redline'][] = (string)$child->redline;
}
}
/* method 1 */
/** $data['fuel_efficiency_ratings'] = ( ! empty ( $data['fuel_efficiency_ratings'] ) ) ? $data['fuel_efficiency_ratings'] : array();
/** foreach( $data['fuel_efficiency_ratings'] as $fuel_efficiency_ratings ) {
/** foreach( $fuel_efficiency_ratings->children() as $child ) {
/** $attrs = $child->attributes();
/**
/** $_dappcf_is_fuel['mpg_city'][] = (string)$attrs->id;
/** $_dappcf_is_fuel['mpg_hwy'][] = (string)$attrs->id;
/** }
/** }
/* method 2 *
/**/ foreach( $specifications['Fuel Storage'] as $key => $value ) {
/**/ $_dappcf_is_fuel[$key] = $value;
/**/ }
/**/
foreach( $_dappcf_is_engine as $key => $value ) {
$_dappcf_is_engine[$key] = array_filter( array_unique( $_dappcf_is_engine[$key] ), 'strip_empty_var' );
}
foreach( $_dappcf_is_enginespecs as $key => $value ) {
$_dappcf_is_enginespecs[$key] = array_filter( array_unique( $_dappcf_is_enginespecs[$key] ), 'strip_empty_var' );
}
foreach( $_dappcf_is_fuel as $key => $value ) {
$_dappcf_is_fuel[$key] = array_filter( array_unique( $_dappcf_is_fuel[$key] ), 'strip_empty_var' );
}
$_dappcf_is_details['order_code'] = array_unique( $_dappcf_is_details['order_code'] );
$_dappcf_is_details['marketing_name'] = array_unique( $_dappcf_is_details['marketing_name'] );
$_dappcf_i_details['engine_id'] = array_unique( $_dappcf_i_details['engine_id'] );
$_dappcf_is_engine = array_filter( $_dappcf_is_engine, 'strip_empty_var' );
$_dappcf_is_enginespecs = array_filter( $_dappcf_is_enginespecs, 'strip_empty_var' );
$_dappcf_is_fuel = array_filter( $_dappcf_is_fuel, 'strip_empty_var' );
$_dappcf_is_engine['Horse power'] = $features['Engine']['Horse Power'];
$_dappcf_is_engine['Torque'] = $features['Engine']['Torque'];
$_dappcf_is_engine['Size'] = $features['Engine']['Size'];
$_dappcf_is_enginespecs['Engine Configuration'] = $features['Engine']['Engine Configuration'];
$_dappcf_is_enginespecs['Max Torque RPM'] = $features['Engine']['Max Torque RPM'];
$_dappcf_is_enginespecs['Variable Valve Timing'] = $features['Engine']['Variable Valve Timing'];
$_dappcf_is_enginespecs['Max HP RPM'] = $features['Engine']['Max HP RPM'];
$_dappcf_is_enginespecs['Valve Gear'] = $features['Engine']['Valve Gear'];
foreach( $transmissions->children() as $child ) {
$_dappcf_i_details['transmission_id'][] = (string)$child->attributes()->id;
$_dappcf_is_trans['type'][] = (string)$child->type;
$_dappcf_is_trans['detail_type'][] = (string)$child->detail_type;
$_dappcf_is_trans['gears'][] = (string)$child->gears;
}
$_dappcf_is_trans['Automatic type'] = $features['Transmission']['Automatic type'];
$_dappcf_is_trans['Number of speeds'] = $features['Transmission']['Number of speeds'];
$_dappcf_i_details['plant'] = $basic_data['plant'];
$_dappcf_is_details['Midyear release'] = $features['Header']['Midyear release'];
foreach( $specifications['Fuel Storage'] as $key => $value ) {
$_dappcf_is_fuel[$key] = $value;
}
/** foreach( $fuel_efficiency_ratings['mpg_city'] as $key => $value ) {
/** $_dappcf_is_fuel[$key] = $value;
/** }
/**/
foreach( $custom_fields['_dappcf_is_mechanical'] as $value ) {
if( vin_array_key_exists( $value, $features['Suspension'] ) ) {
$_dappcf_is_mechanical[$value] = $features['Suspension'][$value];
} else if( vin_array_key_exists( $value, $features['Brakes'] ) ){
$_dappcf_is_mechanical[$value] = $features['Brakes'][$value];
} else if( vin_array_key_exists( $value, $features['Convenience Features'] ) ){
$_dappcf_is_mechanical[$value] = $features['Convenience Features'][$value];
}
}
foreach( $custom_fields['_dappcf_is_tirewheel'] as $value ) {
if( vin_array_key_exists( $value, $features['Tires'] ) ) {
$_dappcf_is_tirewheel[$value] = $features['Tires'][$value];
} else if( vin_array_key_exists( $value, $specifications['Wheels and Tires'] ) ) {
$_dappcf_is_tirewheel[$value] = $specifications['Wheels and Tires'][$value];
} else if( vin_array_key_exists( $value, $features['Spare Tire'] ) ) {
$_dappcf_is_tirewheel[$value] = $features['Spare Tire'][$value];
} else if( vin_array_key_exists( $value, $features['Spare Wheel'] ) ) {
$_dappcf_is_tirewheel[$value] = $features['Spare Wheel'][$value];
} else if( vin_array_key_exists( $value, $features['Wheels'] ) ) {
$_dappcf_is_tirewheel[$value] = $features['Wheels'][$value];
}
}
foreach( $custom_fields['_dappcf_is_measure'] as $value ) {
if( vin_array_key_exists( $value, $specifications['Performance'] ) ) {
$_dappcf_is_measure[$value] = $specifications['Performance'][$value];
} else if( vin_array_key_exists( $value, $specifications['Size and Shape Measurements'] ) ) {
$_dappcf_is_measure[$value] = $specifications['Size and Shape Measurements'][$value];
} else if( vin_array_key_exists( $value, $specifications['Weight Measurements'] ) ) {
$_dappcf_is_measure[$value] = $specifications['Weight Measurements'][$value];
}
}
foreach( $custom_fields['_dappcf_is_measureint'] as $value ) {
if( vin_array_key_exists( $value, $specifications['Interior Dimensions'] ) ) {
$_dappcf_is_measureint[$value] = $specifications['Interior Dimensions'][$value];
} else if( vin_array_key_exists( $value, $specifications['Seating'] ) ) {
$_dappcf_is_measureint[$value] = $specifications['Seating'][$value];
}
}
foreach( $custom_fields['_dappcf_is_interior'] as $value ) {
if( vin_array_key_exists( $value, $features['Air Conditioning'] ) ) {
$_dappcf_is_interior[$value] = $features['Air Conditioning'][$value];
} else if( vin_array_key_exists( $value, $features['Rear Seats'] ) ) {
$_dappcf_is_interior[$value] = $features['Rear Seats'][$value];
} else if( vin_array_key_exists( $value, $features['Convenience Features'] ) ) {
$_dappcf_is_interior[$value] = $features['Convenience Features'][$value];
} else if( vin_array_key_exists( $value, $features['Instrumentation'] ) ) {
$_dappcf_is_interior[$value] = $features['Instrumentation'][$value];
} else if( vin_array_key_exists( $value, $features['Comfort Features'] ) ) {
$_dappcf_is_interior[$value] = $features['Comfort Features'][$value];
}
}
foreach( $custom_fields['_dappcf_is_seating'] as $value ) {
if( vin_array_key_exists( $value, $specifications['Seating'] ) ) {
$_dappcf_is_seating[$value] = $specifications['Seating'][$value];
} else if( vin_array_key_exists( $value, $features['Headrests'] ) ) {
$_dappcf_is_seating[$value] = $features['Headrests'][$value];
} else if( vin_array_key_exists( $value, $features['Rear Seats'] ) ) {
$_dappcf_is_seating[$value] = $features['Rear Seats'][$value];
} else if( vin_array_key_exists( $value, $features['Seatbelts'] ) ) {
$_dappcf_is_seating[$value] = $features['Seatbelts'][$value];
} else if( vin_array_key_exists( $value, $features['Seats'] ) ) {
$_dappcf_is_seating[$value] = $features['Seats'][$value];
} else if( vin_array_key_exists( $value, $features['Passenger Seat'] ) ) {
$_dappcf_is_seating[$value] = $features['Passenger Seat'][$value];
} else if( vin_array_key_exists( $value, $features['Crash Test and Other Ratings'] ) ) {
$_dappcf_is_seating[$value] = $features['Crash Test and Other Ratings'][$value];
} else if( vin_array_key_exists( $value, $features['Driver Seat'] ) ) {
$_dappcf_is_seating[$value] = $features['Driver Seat'][$value];
}
}
foreach( $custom_fields['_dappcf_is_media'] as $value ) {
if( vin_array_key_exists( $value, $features['Audio System'] ) ) {
$_dappcf_is_media[$value] = $features['Audio System'][$value];
} else if( vin_array_key_exists( $value, $features['Convenience Features'] ) ) {
$_dappcf_is_media[$value] = $features['Convenience Features'][$value];
}
}
foreach( $custom_fields['_dappcf_is_exterior'] as $value ) {
if( vin_array_key_exists( $value, $features['Windows'] ) ) {
$_dappcf_is_exterior[$value] = $features['Windows'][$value];
} else if( vin_array_key_exists( $value, $features['Lights'] ) ) {
$_dappcf_is_exterior[$value] = $features['Lights'][$value];
} else if( vin_array_key_exists( $value, $features['Locks'] ) ) {
$_dappcf_is_exterior[$value] = $features['Locks'][$value];
} else if( vin_array_key_exists( $value, $features['Mirrors'] ) ) {
$_dappcf_is_exterior[$value] = $features['Mirrors'][$value];
} else if( vin_array_key_exists( $value, $features['Comfort Features'] ) ) {
$_dappcf_is_exterior[$value] = $features['Comfort Features'][$value];
}
}
foreach( $custom_fields['_dappcf_is_safety'] as $value ) {
if( vin_array_key_exists( $value, $features['Airbags'] ) ) {
$_dappcf_is_safety[$value] = $features['Airbags'][$value];
} else if( vin_array_key_exists( $value, $features['Child Safety'] ) ) {
$_dappcf_is_safety[$value] = $features['Child Safety'][$value];
}
}
foreach( $custom_fields['_dappcf_is_towing'] as $value ) {
if( vin_array_key_exists( $value, $specifications['Weight Measurements'] ) ) {
$_dappcf_is_towing[$value] = $specifications['Weight Measurements'][$value];
} else if( vin_array_key_exists( $value, $specifications['Truck Bed'] ) ) {
$_dappcf_is_towing[$value] = $specifications['Truck Bed'][$value];
}
}
$_dappcf_is_engine['Engine immobilizer'] = $features['Security']['Engine immobilizer'];
}
if( ! $modelyear || ! $model ) {
wp_redirect( $error_page, 301 );
exit();
}
$VinQuery->load_results();
if( $VinQuery->results ) {
$results = VinQuery_to_array( $VinQuery->results->VIN->Vehicle );
$driveline = $results['Driveline'];
$milagecity = $results['Fuel Economy-city'];
$milehighway = $results['Fuel Economy-highway'];
fill_vinQuery_fields( $_dappcf_is_details, $custom_fields['_dappcf_is_details'], $results );
fill_vinQuery_fields( $_dappcf_i_details, $custom_fields['_dappcf_i_details'], $results );
fill_vinQuery_fields( $_dappcf_i_basics, $custom_fields['_dappcf_i_basics'], $results );
fill_vinQuery_fields( $_dappcf_is_fuel, $custom_fields['_dappcf_is_fuel'], $results );
fill_vinQuery_fields( $_dappcf_is_mechanical, $custom_fields['_dappcf_is_mechanical'], $results );
fill_vinQuery_fields( $_dappcf_is_engine, $custom_fields['_dappcf_is_engine'], $results );
fill_vinQuery_fields( $_dappcf_is_enginespecs, $custom_fields['_dappcf_is_enginespecs'], $results );
fill_vinQuery_fields( $_dappcf_is_trans, $custom_fields['_dappcf_is_trans'], $results );
fill_vinQuery_fields( $_dappcf_is_tirewheel, $custom_fields['_dappcf_is_tirewheel'], $results );
fill_vinQuery_fields( $_dappcf_is_measure, $custom_fields['_dappcf_is_measure'], $results );
fill_vinQuery_fields( $_dappcf_is_measureint, $custom_fields['_dappcf_is_measureint'], $results );
fill_vinQuery_fields( $_dappcf_is_interior, $custom_fields['_dappcf_is_interior'], $results );
fill_vinQuery_fields( $_dappcf_is_seating, $custom_fields['_dappcf_is_seating'], $results );
fill_vinQuery_fields( $_dappcf_is_media, $custom_fields['_dappcf_is_media'], $results );
fill_vinQuery_fields( $_dappcf_is_exterior, $custom_fields['_dappcf_is_exterior'], $results );
fill_vinQuery_fields( $_dappcf_is_safety, $custom_fields['_dappcf_is_safety'], $results );
fill_vinQuery_fields( $_dappcf_is_towing, $custom_fields['_dappcf_is_towing'], $results );
/**/ fill_vinQuery_fields( $_dappcf_is_towing, $custom_fields['_dappcf_i_options'], $results );
}
$invenspec_post = new WP_Query( array(
'posts_per_page' => 1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'dappct_i_year',
'field' => 'slug',
'terms' => $modelyear,
'operator' => 'IN'
),
array(
'taxonomy' => 'dappct_i_model',
'field' => 'slug',
'terms' => $model,
'operator' => 'IN'
)
),
'post_type' => 'invenspec'
) );
if( $invenspec_post && $invenspec_post->posts[0]->ID ) {
$invenspec_post = $invenspec_post->posts[0]->ID;
} else {
$invenspec_post = wp_insert_post( array(
'post_title' => $modelyear[0].'-'.$model[0],
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'invenspec',
) );
}
$new_vin_entry = wp_insert_post( array(
'post_title' => $vin,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => $vin_type
) );
wp_set_object_terms( $invenspec_post, $modelyear, 'dappct_i_year' );
wp_set_object_terms( $invenspec_post, $make, 'dappct_i_make' );
wp_set_object_terms( $invenspec_post, $model, 'dappct_i_model' );
wp_set_object_terms( $invenspec_post, $modelnumber, 'dappct_i_modelcode' );
wp_set_object_terms( $invenspec_post, $doors, 'dappct_i_doors' );
wp_set_object_terms( $invenspec_post, $vehicletype, 'dappct_i_vehicletype' );
wp_set_object_terms( $invenspec_post, $body, 'dappct_i_bodytype' );
wp_set_object_terms( $invenspec_post, $driveline, 'dappct_i_drivetype' );
wp_set_object_terms( $invenspec_post, $milagecity, 'dappct_i_mpgcity' );
wp_set_object_terms( $invenspec_post, $milehighway, 'dappct_i_mpghighway' );
wp_set_object_terms( $invenspec_post, $enginetype, 'dappct_i_runson' );
wp_set_object_terms( $invenspec_post, $transition, 'dappct_i_trans' );
wp_set_object_terms( $invenspec_post, $upholstery, 'dappct_i_upholstery' );
wp_set_object_terms( $new_vin_entry, $modelyear, 'dappct_i_year' );
wp_set_object_terms( $new_vin_entry, $make, 'dappct_i_make' );
wp_set_object_terms( $new_vin_entry, $model, 'dappct_i_model' );
wp_set_object_terms( $new_vin_entry, $modelnumber, 'dappct_i_modelcode' );
wp_set_object_terms( $new_vin_entry, $doors, 'dappct_i_doors' );
wp_set_object_terms( $new_vin_entry, $vehicletype, 'dappct_i_vehicletype' );
wp_set_object_terms( $new_vin_entry, $body, 'dappct_i_bodytype' );
wp_set_object_terms( $new_vin_entry, $driveline, 'dappct_i_drivetype' );
wp_set_object_terms( $new_vin_entry, $milagecity, 'dappct_i_mpgcity' );
wp_set_object_terms( $new_vin_entry, $milehighway, 'dappct_i_mpghighway' );
wp_set_object_terms( $new_vin_entry, $enginetype, 'dappct_i_runson' );
wp_set_object_terms( $new_vin_entry, $transition, 'dappct_i_trans' );
wp_set_object_terms( $new_vin_entry, $upholstery, 'dappct_i_upholstery' );
update_post_meta( $invenspec_post, '_dappcf_i_basics', $_dappcf_i_basics );
update_post_meta( $invenspec_post, '_dappcf_is_basics', $_dappcf_i_basics );
update_post_meta( $new_vin_entry, '_dappcf_i_basics', $_dappcf_i_basics );
update_post_meta( $new_vin_entry, '_dappcf_is_basics', $_dappcf_i_basics );
update_post_meta( $invenspec_post, '_dappcf_is_details', $_dappcf_is_details );
update_post_meta( $invenspec_post, '_dappcf_i_details', $_dappcf_i_details );
update_post_meta( $invenspec_post, '_dappcf_is_fuel', $_dappcf_is_fuel );
update_post_meta( $invenspec_post, '_dappcf_is_mechanical', $_dappcf_is_mechanical );
update_post_meta( $invenspec_post, '_dappcf_is_engine', $_dappcf_is_engine );
update_post_meta( $invenspec_post, '_dappcf_is_enginespecs', $_dappcf_is_enginespecs );
update_post_meta( $invenspec_post, '_dappcf_is_trans', $_dappcf_is_trans );
update_post_meta( $invenspec_post, '_dappcf_is_tirewheel', $_dappcf_is_tirewheel );
update_post_meta( $invenspec_post, '_dappcf_is_measure', $_dappcf_is_measure );
update_post_meta( $invenspec_post, '_dappcf_is_measureint', $_dappcf_is_measureint );
update_post_meta( $invenspec_post, '_dappcf_is_interior', $_dappcf_is_interior );
update_post_meta( $invenspec_post, '_dappcf_is_seating', $_dappcf_is_seating );
update_post_meta( $invenspec_post, '_dappcf_is_media', $_dappcf_is_media );
update_post_meta( $invenspec_post, '_dappcf_is_exterior', $_dappcf_is_exterior );
update_post_meta( $invenspec_post, '_dappcf_is_safety', $_dappcf_is_safety );
update_post_meta( $invenspec_post, '_dappcf_is_towing', $_dappcf_is_towing );
wp_redirect( get_permalink( $new_vin_entry ), 301 );
exit();
}
}
add_action( 'parse_request', 'VAlpha_search' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment