Created
July 17, 2015 13:46
-
-
Save mgratch/9c468e9952d7d05287e2 to your computer and use it in GitHub Desktop.
Convert Data from https://github.com/farinspace/wpalchemy and https://metabox.io/ from 2012
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Convert Meta Fields | |
* Description: batch convert metadata for metroinvestments update | |
* Author: Marc Gratch | |
* Author URI: http://marcgratch.com | |
* Version: 0.1.0 | |
*/ | |
/** | |
* Start by collecting all the IDs for all the post_type that needs to be updated | |
* @return array | |
*/ | |
function get_all_ids(){ | |
$args = array ( | |
'post_type' => 'mi_listings', | |
'posts_per_page' => -1 | |
); | |
$all_posts = new WP_Query($args); | |
$ids_to_store = array(); | |
while ( $all_posts->have_posts() ){ | |
$all_posts->the_post(); | |
$ids_to_store[] = get_the_ID(); | |
} | |
return $ids_to_store; | |
} | |
/** | |
* Loop through All of the IDs from get_all_ids() and determine if the meta_value and meta_key need to be updated. | |
* This function also checks if the new keys exit and deletes them first. | |
*/ | |
function get_all_the_meta_keys(){ | |
$all_ids_array = get_all_ids(); | |
global $wpdb; | |
$new_val = null; | |
/* | |
* Loop through the post ids | |
*/ | |
foreach($all_ids_array as $post_id){ | |
$this_posts_meta = get_post_meta($post_id); | |
$all_meta_keys = get_post_custom_keys($post_id); | |
$attachment_ids = array(); | |
foreach($all_meta_keys as $k){ | |
if(strpos($k,'lwi_')===0){ | |
$wpdb->delete( $wpdb->postmeta, array( 'meta_key' => $k, 'post_id'=>$post_id), array( '%s','%d' ) ); | |
} | |
elseif($k == '_custom_availability_meta_fields'){ | |
$wpdb->delete( $wpdb->postmeta, array( 'meta_key' => $k, 'post_id'=>$post_id), array( '%s','%d' ) ); | |
} | |
elseif($k == '_lwi_basement'){ | |
$wpdb->delete( $wpdb->postmeta, array( 'meta_key' => $k, 'post_id'=>$post_id), array( '%s','%d' ) ); | |
} | |
} | |
/* | |
* loop through the meta_keys | |
*/ | |
foreach ($this_posts_meta as $key => $val){ | |
if (strpos($key,'_lwi') === 0 || strpos($key,'_slideShowImage') === 0 || strpos($key,'_availability') === 0){ | |
$new_keys = str_replace('_lwi','lwi',$key); | |
$new_keys = str_replace('_slideShowImage','lwi_slideshowimage',$new_keys); | |
$new_keys = strtolower($new_keys); | |
if (strpos($key,'lwi_location_lat') !== false){ | |
$new_keys = "lwi_location_latitude"; | |
} | |
elseif (strpos($key,'_slideShowImage') !== false){ | |
$attachment_ids[] = $val; | |
} | |
elseif (strpos($key,'_availability_availability') !== false){ | |
$new_keys = "availability_availability"; | |
} | |
elseif (strpos($key,'_availability_are_available') !== false){ | |
$new_keys = "availability_are_available"; | |
} | |
elseif (strpos($key,'lwi_location_long') !== false){ | |
$new_keys = "lwi_location_longitude"; | |
} | |
elseif (strpos($key,'lwi_street_address') !== false){ | |
$new_keys = "lwi_address_street_address"; | |
} | |
elseif (strpos($key,'lwi_state_address') !== false){ | |
$new_keys = "lwi_address_state"; | |
} | |
elseif (strpos($key,'lwi_city_address') !== false){ | |
$new_keys = "lwi_address_city"; | |
} | |
elseif (strpos($key,'lwi_zip_address') !== false){ | |
$new_keys = "lwi_address_zip"; | |
} | |
elseif (strpos($key,'lwi_location_coords') !== false){ | |
$new_keys = "lwi_location"; | |
$coords_array = explode(",", $val[0]); | |
$latitude = $coords_array[0]; | |
$longitude = $coords_array[1]; | |
$final_array = array("latitude" => $latitude,"longitude" => $longitude); | |
$new_val = maybe_serialize($final_array); | |
} | |
elseif (strpos($key,'lwi_location_address') !== false){ | |
$new_keys = "lwi_address"; | |
$address_array = explode(",", $val[0]); | |
$street = $address_array[0]; | |
$city = $address_array[1]; | |
$state = $address_array[2]; | |
$zip = $address_array[3]; | |
$final_array = array( | |
"address-1" => $street, | |
"address-2" => "", | |
"city" => $city, | |
"state" => $state, | |
"zip" => $zip | |
); | |
$new_val = maybe_serialize($final_array); | |
} | |
elseif (strpos($key,'lwi_leaseTerm') !== false){ | |
$new_keys = "lwi_leaseterm"; | |
$new_val = ucwords($val[0]); | |
} | |
elseif (strpos($key,'lwi_washer_dryer') !== false){ | |
$new_keys = "lwi_washer_dryer"; | |
$uppercse_array = array(); | |
$split_val = explode(' ',$val[0]); | |
foreach($split_val as $word){ | |
if ($word !== 'on' && $word !== 'the' && $word !== 'in' && $word !== 'to'){ | |
$uppercse_array[] = ucwords($word); | |
} | |
else{ | |
$uppercse_array[] = $word; | |
} | |
} | |
$new_val = implode(' ',$uppercse_array); | |
} | |
elseif (strpos($key,'lwi_pets_allowed') !== false){ | |
$new_keys = "lwi_pets_allowed"; | |
$uppercse_array = array(); | |
$split_val = explode(' ',$val[0]); | |
foreach($split_val as $word){ | |
if ($word !== 'on' && $word !== 'the' && $word !== 'in' && $word !== 'to'){ | |
$uppercse_array[] = ucwords($word); | |
} | |
else{ | |
$uppercse_array[] = $word; | |
} | |
} | |
$new_val = implode(' ',$uppercse_array); | |
} | |
elseif (strpos($key,'lwi_utilities') !== false){ | |
$new_keys = "lwi_utilities"; | |
$uppercse_array = array(); | |
$split_val = explode(' ',$val[0]); | |
foreach($split_val as $word){ | |
if ($word !== 'on' && $word !== 'the' && $word !== 'in' && $word !== 'to'){ | |
$uppercse_array[] = ucwords($word); | |
} | |
else{ | |
$uppercse_array[] = $word; | |
} | |
} | |
$new_val = implode(' ',$uppercse_array); | |
} | |
elseif (strpos($key,'lwi_parking') !== false){ | |
$new_keys = "lwi_parking"; | |
$uppercse_array = array(); | |
$split_val = explode(' ',$val[0]); | |
foreach($split_val as $word){ | |
if ($word !== 'on' && $word !== 'the' && $word !== 'in' && $word !== 'to'){ | |
$uppercse_array[] = ucwords($word); | |
} | |
else{ | |
$uppercse_array[] = $word; | |
} | |
} | |
$new_val = implode(' ',$uppercse_array); | |
} | |
/* | |
* if the newly formatted meta_key Does NOT exist in the DB, lets update the old Rows with the new keys/value | |
*/ | |
if ($new_val !== null && !empty($new_val)){ | |
$wpdb->query($wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE post_id = %d AND meta_key = %s",$new_val,$post_id,$key)); | |
$wpdb->query($wpdb->prepare("UPDATE $wpdb->postmeta SET meta_key = %s WHERE post_id = %d AND meta_key = %s",$new_keys,$post_id,$key)); | |
} | |
else{ | |
$wpdb->query($wpdb->prepare("UPDATE $wpdb->postmeta SET meta_key = %s WHERE post_id = %d AND meta_key = %s",$new_keys,$post_id,$key)); | |
} | |
$new_val = null; | |
} | |
} | |
/* | |
* If the post relates to multiple attachments create an associative array of the all the attachments for Pods | |
*/ | |
if (!empty($attachment_ids)){ | |
$i = 0; | |
$final_array = array(); | |
$attachment_id_array = $attachment_ids[0]; | |
$table_name = $wpdb->prefix . 'podsrel'; | |
$podsrel_ids = $wpdb->get_results("SELECT ID FROM $table_name"); | |
$max_podsrel_id = 0; | |
$related_field_id = 0; | |
$pod_id = 7; | |
$field_id = 25; | |
$related_field_id = 0; | |
foreach($podsrel_ids as $podrel_id){ | |
if($podrel_id->ID > $max_podsrel_id){ | |
$max_podsrel_id = $podrel_id->ID; | |
} | |
} | |
foreach ($attachment_id_array as $key => $val){ | |
$max_podsrel_id++; | |
$val = intval($val,10); | |
$final_array[] = $val; | |
$wpdb->insert( | |
$wpdb->prefix . 'podsrel', | |
array( | |
'id' => $max_podsrel_id, | |
'pod_id' => $pod_id, | |
'field_id' => $field_id, | |
'item_id' => $post_id, | |
'related_pod_id' => $related_field_id, | |
'related_field_id' => $related_field_id, | |
'related_item_id' => $val, | |
'weight' => $i | |
), | |
array( | |
'%d', | |
'%d', | |
'%d', | |
'%d', | |
'%d', | |
'%d', | |
'%d', | |
'%d' | |
) | |
); | |
$i++; | |
} | |
$serialized_array = maybe_serialize($final_array); | |
$meta_key = '_pods_lwi_slideshowimage'; | |
$wpdb->insert( | |
$wpdb->postmeta, | |
array( | |
'post_id' => $post_id, | |
'meta_key' => $meta_key, | |
'meta_value' => $serialized_array | |
), | |
array( | |
'%d', | |
'%s', | |
'%s' | |
) | |
); | |
} | |
} | |
deactivate_plugins( plugin_basename( __FILE__ ) ); | |
} | |
add_action( 'shutdown', 'get_all_the_meta_keys' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment