Skip to content

Instantly share code, notes, and snippets.

@gwin
Created July 12, 2016 09:54
Show Gist options
  • Save gwin/57180f7d6420e6281c30d3f28b724f41 to your computer and use it in GitHub Desktop.
Save gwin/57180f7d6420e6281c30d3f28b724f41 to your computer and use it in GitHub Desktop.
function wpadverts_mal_save_advert( $post_ID, $post, $update ) {
if( !isset( $_POST ) || empty( $_POST ) ) {
return;
}
include_once ADVERTS_PATH . '/includes/class-form.php';
$form = new Adverts_Form;
$form->load( Adverts::instance()->get( "form_mal" ) );
$form->bind( stripslashes_deep( $_POST ) );
$values = $form->get_values();
$map = array_keys( wpadverts_mal_mapping() );
$map[] = "_adverts_mal_place_id";
$map[] = "_adverts_mal_latitude";
$map[] = "_adverts_mal_longitude";
foreach( $map as $meta_key ) {
if( isset( $values[$meta_key] ) ) {
$meta_value = $values[$meta_key];
} else {
$meta_value = "";
}
if( isset( $_POST[$meta_key] ) ) {
adverts_save_single( $post_ID, $meta_key, $meta_value );
}
}
$lat = null;
$lng = null;
if( isset( $values["_adverts_mal_latitude"] ) ) {
$lat = $values["_adverts_mal_latitude"];
} elseif( isset( $_POST["_adverts_mal_latitude"] ) ) {
$lat = "";
}
if( isset( $values["_adverts_mal_longitude"] ) ) {
$lng = $values["_adverts_mal_longitude"];
} elseif( isset( $_POST["_adverts_mal_longitude"] ) ) {
$lng = "";
}
if( $lat !== null && $lng !== null) {
if( ! empty( $lat ) && ! empty( $lng ) ) {
wpadverts_mal_save_latlng( $post_ID, $lat, $lng );
} else {
wpadverts_mal_delete_latlng( $post_ID );
}
}
$taxo_hierarchy = array();
$config = adverts_config( "mal.component" );
foreach( $config as $component_name => $component_data ) {
if( $component_data["taxonomy_level"] >= 0 ) {
$taxo_hierarchy[absint( $component_data["taxonomy_level"] ) ] = $component_name;
}
}
$mapping = wpadverts_mal_mapping( true );
$index = 0;
$parent = null;
while( array_key_exists( $index, $taxo_hierarchy ) ) {
$field_name = $mapping[ $taxo_hierarchy[$index] ];
if( ! isset( $values[$field_name] ) ) {
$index++;
continue;
}
$term = $values[$field_name];
$t = term_exists( $term, 'advert_location', $parent );
if( ! $t ) {
// create term
$term_args = array(
'description' => '',
'slug' => sanitize_title( $term ),
'parent' => $parent
);
$new_term = wp_insert_term( $term, 'advert_location', $term_args );
$parent = $new_term['term_id'];
} elseif( is_array( $t ) ) {
// set parent
$parent = $t['term_id'];
} else {
$parent = $t;
}
$index++;
}
if( ! is_null( $parent ) ) {
wp_set_post_terms( $post_ID, $parent, 'advert_location' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment