Last active
December 17, 2015 03:09
-
-
Save colegeissinger/5541496 to your computer and use it in GitHub Desktop.
Read a CSV file and export it to an importable XML file for WordPress. Code is setup to import "exhibits" for Maker Faire.
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 | |
/** | |
* Allows us to convert CSV/Spread Sheets and generate an XML file for the import of WordPress. | |
* | |
* Save this file into the root of your WordPress install to run. Other wise, modify the file path for the wp-load.php. | |
* | |
* EG. Use this URL to generate the XML. Or use cURL to save the file. | |
* http://localhost/import-csv-makerfaire.php?file_name=BazBizMF13_Vendor-IMPORT.csv&url=http://localhost/imports/ | |
* curl "http://localhost/import-csv-makerfaire.php?file_name=BazBizMF13_Vendor-IMPORT.csv&url=http://localhost/imports/" -o "makerfaire-exibit-import.xml" | |
* | |
* @version 1.1 | |
*/ | |
// Load the WP bootstrap. TODO: update to auto grab relative URL instead of hard coding it... :/ | |
require( 'wp-load.php' ); | |
/** | |
* Set our variables. | |
* | |
* $file_name is required. | |
* $url defaults to 'http://localhost/', if you wish to point this to another directory, you can do 'http://localhost/imports/'. | |
*/ | |
$file_name = ( ! empty( $_REQUEST['file_name'] ) ? $_REQUEST['file_name'] : null ); | |
$url = ( ! empty( $_REQUEST['url'] ) ? $_REQUEST['url'] : null ); | |
// Check that we have passed the $file_name through a query string. If not, kill the script and send an error. | |
if ( empty( $file_name ) ) { | |
echo "Error! File Name required! Use '?file_name=spread-sheet-name-here.csv' at the end of the URL.\n"; // Add the \n for proper console rendering. | |
die(); | |
} | |
/** | |
* Change the names of categories be dashed, rather then have spaces. | |
* @param String $str The name of the category | |
* @return String | |
* | |
* @version 1.0 | |
* @since 1.0 | |
*/ | |
function mf_convert_to_dashes( $str ) { | |
// Convert the white space in the string to dashes | |
$dashed = str_replace( ' ', '-', $str ); | |
return $dashed; | |
} | |
/** | |
* The mother ship. This function will open and read a CSV file and convert it to a useable array. | |
* By default the function will locate the file in the root of the local server. | |
* @param String $file_name The string containing the name of the CSV file. | |
* @param String $url The URL where to find the CSV file. | |
* @return Array | |
* | |
* @version 1.0 | |
* @since 1.0 | |
*/ | |
function mf_get_the_file( $file_name, $url ) { | |
// Check if the $url variable is empty. If so, set a default string. | |
if ( empty( $url ) ) | |
$url = 'http://localhost/'; | |
// Create an empty array so we can add another array inside. | |
$exibits = array(); | |
// Use fopen() to read the file. NOTE, make sure spreadsheet is converted to a CSV file. | |
if ( ( $file = fopen( $url . $file_name, 'r' ) ) !== false ) { | |
// Loop through every row using a comma delimmited separator. | |
while ( ( $data = fgetcsv( $file, 0, ',' ) ) !== false ) { | |
// Setup our array containing all of the values in the CSV. | |
// NOTE: There were a lot of extra things not visable in the original spreadsheet? | |
$exibit = array( | |
'project_id' => $data[1], | |
'disable_auto_resonder' => $data[2], | |
'status' => $data[3], | |
'form_type' => $data[4], | |
'project_name' => $data[5], | |
'public_description' => $data[6], | |
'private_description' => $data[7], | |
'cats' => $data[8], | |
'project_photo' => $data[10], | |
'project_website' => $data[11], | |
'project_video' => $data[13], | |
'sales' => $data[21], | |
'sales_details' => $data[22], | |
'placement' => $data[23], | |
'email' => $data[24], | |
'group_name' => $data[25], | |
); | |
$exibits[] = $exibit; | |
} | |
// Close the read connect | |
fclose( $file ); | |
} | |
// Send back the array of arrays :P | |
return $exibits; | |
} | |
/** | |
* Generates the actual XML. Pretty stright forward, just dump the XML striaght into the document... | |
* @param Array $exibits | |
* @return XML | |
* | |
* @version 1.1 | |
* @since 1.0 | |
*/ | |
function mf_generate_xml( $exibits ) { | |
// Loop through the results from mf_get_the_file and use array_slice() to remove the first array as we don't need it. | |
foreach ( array_slice( $exibits, 1 ) as $exibit ) { | |
echo "\t<item>\n"; | |
echo "\t\t<title>" . wp_specialchars( $exibit['project_name'] ) . "</title>\n"; | |
echo "\t\t<pubdate>" . date( r ) . "</pubdate>\n"; | |
echo "\t\t<dc:creator>makemagazine</dc:creator>\n"; | |
echo "\t\t" . '<content:encoded><![CDATA[{"form_type":"' . $exibit['form_type'] . '","maker_faire":"2013_bayarea","uid":"","tags":"","cats":"' . $exibit['cats'] . '","project_name":"' . ent2ncr( esc_html( $exibit['project_name'] ) ) . '","private_description":"' . ent2ncr( esc_html( $exibit['private_description'] ) ) . '","public_description":"' . ent2ncr( esc_html( $exibit['public_description'] ) ) . '","project_photo":"' . $exibit['project_photo'] . '","project_photo_thumb":"","project_website":"' . $exibit['project_website'] . '","project_video":"' . $exibit['project_video'] . '","food":"","food_details":"","sales":"' . $exibit['sales'] . '","sales_details":"' . $exibit['sales_details'] . '","booth_size":"","booth_size_details":"","tables_chairs":"","tables_chairs_details":"","layout":"","activity":"","placement":"' . $exibit['placement'] . '","booth_location":"","booth_options":"","lighting":"","noise":"","power":"","what_are_you_powering":"","amps":"","amps_details":"","internet":"","radio":"","radio_frequency":"","radio_details":"","fire":"","hands_on":"","safety_details":"","email":"' . $exibit['email'] . '","name":"","maker":"","maker_name":"","maker_email":"","maker_photo":"","maker_photo_thumb":"","maker_bio":"","m_maker_name":[""],"m_maker_email":[""],"m_maker_photo":[""],"m_maker_photo_thumb":"","m_maker_bio":[""],"m_maker_gigyaid":[""],"group_name":"' . $exibit['group_name'] . '","group_bio":"","group_photo":"","group_photo_thumb":"","group_website":"","phone1":"","phone1_type":"","phone2":"","phone2_type":"","private_address":"","private_address2":"","private_city":"","private_state":"","private_zip":"","private_country":"","org_type":"","large_non_profit":"","supporting_documents":"","references":"","referrals":"","hear_about":"","first_time":"","anything_else":""}]]></content:encoded>' . "\n"; | |
echo "\t\t<wp:post_date>" . date( Y-m-d ) . "</wp:post_date>\n"; | |
echo "\t\t<wp:comment_status>closed</wp:comment_status>\n"; | |
echo "\t\t<wp:ping_status>closed</wp:ping_status>\n"; | |
echo "\t\t<wp:post_name>" . strtolower( preg_replace( array( '/[^a-z0-9\- ]/i', '/[ \-]+/' ), array( '', '-' ), $exibit['project_name'] ) ) . "</wp:post_name>\n"; | |
echo "\t\t<wp:status>accepted</wp:status>\n"; | |
echo "\t\t<wp:post_type>mf_form</wp:post_type>\n"; | |
echo "\t\t<wp:post_parent>0</wp:post_parent>\n"; | |
echo "\t\t<wp:menu_order>0</wp:menu_order>\n"; | |
echo "\t\t<wp:post_password></wp:post_password>\n"; | |
echo "\t\t<wp:is_sticky>0</wp:is_sticky>\n"; | |
echo "\t\t<category domain=\"group\" nicename=\"bazaar-bizarre\"><![CDATA[Bazaar Bizarre]]></category>\n"; | |
echo "\t\t<category domain=\"category\" nicename=\"" . strtolower( preg_replace( array( '/[^a-z0-9\- ]/i', '/[ \-]+/' ), array( '', '-' ), $exibit['cats'] ) ) . "\"><![CDATA[{$exibit['cats']}]]></category>\n"; | |
echo "\t\t<category domain=\"location\" nicename=\"bazaar-bizarre\"><![CDATA[Bazaar Bizarre]]></category>\n"; | |
echo "\t\t<wp:postmeta>\n"; | |
echo "\t\t\t<wp:meta_key>_ef_editorial_meta_checkbox_email-notifications</wp:meta_key>\n"; | |
echo "\t\t\t<wp:meta_value><![CDATA[0]]></wp:meta_value>\n"; | |
echo "\t\t</wp:postmeta>\n"; | |
echo "\t</item>\n"; | |
} | |
} | |
// Spit out the header with the correct content type. | |
header("Content-type: text/xml; charset=utf-8"); | |
// Echo this out first, or else XML errors will occur. | |
echo '<?xml version="1.0" encoding="UTF-8" ?>'; | |
?> | |
<rss version="2.0" xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="http://wordpress.org/export/1.2/"> | |
<channel> | |
<title>Maker Faire Exhibits</title> | |
<link>http://localhost</link> | |
<description></description> | |
<pubDate><?php date( 'l jS \of F Y h:i:s A' ); ?></pubDate> | |
<language>en-US</language> | |
<wp:wxr_version>1.2</wp:wxr_version> | |
<wp:base_site_url>http://localhost</wp:base_site_url> | |
<wp:base_blog_url>http://localhost</wp:base_blog_url> | |
<?php mf_generate_xml( mf_get_the_file( $file_name, $url ) ); // Spit out the XML! ?> | |
</channel> | |
</rss> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment