Created
December 2, 2014 18:16
-
-
Save nhaskins/335d678dfa69c0aaa1a8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* The template for displaying all pages | |
* | |
* This is the template that displays all pages by default. | |
* Please note that this is the WordPress construct of pages and that | |
* other 'pages' on your WordPress site will use a different template. | |
* | |
* @package WordPress | |
* @subpackage Twenty_Fourteen | |
* @since Twenty Fourteen 1.0 | |
*/ | |
?> | |
<?php | |
class ABImport { | |
function parseCSV($filepath){ | |
$array = $fields = array(); $i = 0; | |
$handle = @fopen($filepath, "r"); | |
if ($handle) { | |
while (($row = fgetcsv($handle, 4096)) !== false) { | |
if (empty($fields)) { | |
$fields = $row; | |
continue; | |
} | |
foreach ($row as $k=>$value) { | |
$array[$i][$k] = $value; | |
} | |
$i++; | |
} | |
if (!feof($handle)) { | |
echo "Error: unexpected fgets() fail\n"; | |
} | |
fclose($handle); | |
} | |
return $array; | |
} | |
private function scrub($s){ | |
if(isset($s) && $s){ | |
$s = trim($s); | |
$s = strtolower($s); | |
return $s; | |
} | |
} //end scrubg | |
private function add_check($listing){ | |
//looks at a listing, and decideds if it will be added or not | |
if(get_page_by_title($listing[0], ARRAY_A, 'ablisting')){return false;} | |
return strlen(trim($listing[0])); //check if there's a name desc | |
} | |
private function desc_masher($d){ | |
$d = array_filter($d); //rm blank elements | |
if(!count($d)){return '';} //no desc at all | |
return implode('<br/><br/>', $d); | |
} | |
private function add_cats($d){ | |
$output = array(); //hold cat id's | |
$tax = 'ablisting_category'; | |
if( !$d[1] || !strlen(trim($d[1])) ){return;} | |
$d[1] = $this->scrub($d[1]); | |
//handle the main category | |
$pid = wp_insert_term($d[1], $tax); | |
//interpret the return | |
if(is_wp_error($pid)){ | |
$pid = $pid->error_data['term_exists']; | |
} else { | |
//term insert ok, deal with arr | |
$pid = $pid['term_id']; | |
} | |
$output[] = $pid; | |
//handle the subcategory | |
if(strlen(trim($d[2]))){ | |
$d[2] = $this->scrub($d[2]); | |
//insert with a subcat | |
$sub = wp_insert_term($d[2], $tax, array('parent'=>$pid)); | |
delete_option($tax."_children"); // clear the cache | |
if(is_wp_error($sub)){ | |
$sub = $sub->error_data['term_exists']; | |
} else { | |
$sub = $sub['term_id']; | |
} | |
$output[] = $sub; | |
} | |
return array_map('intval', $output); | |
} //end add_cats | |
function add_abListings($d, $index = 0) { | |
//given a set of data, generate the listings and cats | |
if(!$d){return;} //no data, no dice | |
if($index){$d = array_slice($d, $index);} | |
foreach($d as $x){ | |
if(!$this->add_check($x)){continue;} //check if it's worthy | |
$desc = $this->desc_masher(array($x[3], $x[4], $x[6])); | |
$fields = array( | |
"name" => $x[0], | |
"content" => $desc, | |
"website" => $x[5], | |
"phone" => $x[7], | |
"email" => $x[8], | |
"street_address" => $x[9], | |
"city" => $x[10], | |
"state" => $x[11], | |
"zipcode" => $x[12], | |
"status" => "publish" | |
); | |
$cids = $this->add_cats($x); //handle categories | |
$id = pods('ablisting')->add($fields); | |
if(count($cids) && $id){ | |
wp_set_object_terms($id, $cids, 'ablisting_category'); | |
delete_option("ablisting_category_children"); // clear the cache | |
} | |
} | |
} //end add_abListing | |
} | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
set_time_limit(0); | |
ini_set('memory_limit', '512M'); | |
$a = new ABImport; | |
$data = $a->parseCSV('/var/chroot/home/content/22/8280222/html/data.csv'); | |
$a->add_abListings($data); | |
//$a->add_abListings($data, 1146); | |
echo 'done @ '.time(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment