Created
July 17, 2012 14:16
-
-
Save beweinreich/3129652 to your computer and use it in GitHub Desktop.
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 | |
global $wpdb; | |
define('WP_USE_THEMES', false); | |
require_once('../../../wp-load.php' ); | |
require_once('../../../wp-admin/includes/file.php' ); | |
$wp->init(); | |
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) { | |
$overrides = array('test_form'=>false); | |
$file = wp_handle_sideload($file_array, $overrides); | |
if ( isset($file['error']) ) | |
return new WP_Error( 'upload_error', $file['error'] ); | |
$url = $file['url']; | |
$type = $file['type']; | |
$file = $file['file']; | |
$title = preg_replace('/\.[^.]+$/', '', basename($file)); | |
$content = ''; | |
$title = ""; | |
$content = ""; | |
$title = isset($desc) ? $desc : ''; | |
// Construct the attachment array | |
$attachment = array( | |
'post_mime_type' => $type, | |
'guid' => $url, | |
'post_status' => "inherit", | |
'post_parent' => $post_id, | |
'post_title' => "upload", | |
'post_content' => $content, | |
); | |
// This should never be set as it would then overwrite an existing attachment. | |
if ( isset( $attachment['ID'] ) ) | |
unset( $attachment['ID'] ); | |
// Save the attachment metadata | |
$id = wp_insert_attachment($attachment, $file, $post_id); | |
return $id; | |
} | |
$link = mysql_connect('localhost', 'root', 'root'); | |
if (!$link) { | |
die('Not connected : ' . mysql_error()); | |
} | |
// set current db | |
$db_selected = mysql_select_db('database_name', $link); | |
if (!$db_selected) { | |
die ('Can\'t use database_name : ' . mysql_error()); | |
} | |
$result = mysql_query('SELECT * FROM ventures WHERE live = 1'); | |
if (!$result) { | |
die('Invalid query: ' . mysql_error()); | |
} | |
$venturearray = array(); | |
while ($row = mysql_fetch_assoc($result)) { | |
$venture = array('id'=>$row['id'], | |
'name'=>$row['name'], | |
'featured_image'=>$row['featured_image'], | |
'tagline'=>$row['tagline'], | |
'description'=>$row['description'], | |
'five_year_vision'=>$row['five_year_vision'], | |
'year'=>$row['year'], | |
'website'=>$row['website'], | |
'permalink'=>$row['permalink'], | |
'app_for'=>$row['app_for']); | |
$venture['user_ids'] = array(); | |
$userSQL = "SELECT useid, position FROM vens_users WHERE venid = " . $row['id']; | |
$resultuser = mysql_query($userSQL); | |
while ($rowuser = mysql_fetch_assoc($resultuser)) { | |
array_push($venture['user_ids'], $rowuser['useid']); | |
$venture['user_'.$rowuser['useid'].'_title'] = $rowuser['position']; | |
} | |
array_push($venturearray, $venture); | |
} | |
// Free the resources associated with the result set | |
// This is done automatically at the end of the script | |
mysql_free_result($result); | |
//set current db | |
$db_selected = mysql_select_db('database_name', $link); | |
if (!$db_selected) { | |
die ('Can\'t use database_name : ' . mysql_error()); | |
} | |
foreach ($venturearray as $vent) { | |
$termexist = get_term_by('slug', sanitize_title($vent['name']), 'venture'); | |
if(is_object($termexist)){ | |
$slug = sanitize_title($vent['name'].rand(0,100)); | |
} else { | |
$slug = sanitize_title($vent['name']); | |
} | |
$result = wp_insert_term( $vent['name'], "venture", array("description"=>$vent['tagline'], "slug"=>$slug) ); | |
$term_id = $result['term_id']; | |
if(!empty($vent['featured_image'])) { | |
if(substr($vent['featured_image'],0,4)=="http") { | |
$url = $vent['featured_image']; | |
} else { | |
$url = $vent['featured_image']; | |
} | |
$tmp = download_url( $url ); | |
$file_array = array( | |
'name' => basename( $url ), | |
'tmp_name' => $tmp | |
); | |
$id = media_handle_sideload($file_array, 0); | |
$venture_options['featured_image'] = $id; | |
} else { | |
$venture_options['featured_image'] = ""; | |
} | |
$venture_options['extended_description'] = $vent['description']; | |
$venture_options['website'] = $vent['website']; | |
$venture_options['fanpage'] = ''; | |
$venture_options['year'] = $vent['year']; | |
$venture_options['twitter'] = ''; | |
$venture_options['competitions'] = array(); | |
$venture_options['classifications'] = array(); | |
$venture_options['images'] = array(); | |
$venture_options['video'] = ''; | |
$venture_options['skills'] = array(); | |
$venture_options['status'] = 'approved'; | |
$venture_options['user_ids'] = $vent['user_ids']; | |
foreach($vent['user_ids'] as $user_id) { | |
$venture_options["user_".$user_id."_title"] = $vent["user_".$user_id."_title"]; | |
} | |
$result = add_option( "venture_$term_id", $venture_options ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment