Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
CampTix Attendee Badges with Gravatars
<?php
/*
* Generate a CSV for InDesign with attendee info and Gravatars
*
* See http://plan.wordcamp.org/helpful-documents-and-templates/create-wordcamp-badges-with-gravatars/ for instructions
*
* input is a CSV export of CampTix attendees. Format is: "First Name","Last Name","E-mail Address","Twitter Username"
* the script downloads the attendee's Gravatars, and adds a column to the CSV with the filename of the image
* the CSV can then be used by InDesign to generate wordcamp badges with the attendee's gravatar
*
* make sure you update the input/output file paths before running
*/
$_SERVER['HTTP_HOST'] = 'example.org';
define( 'ABSPATH', '/path/to/example.org/' );
require dirname( dirname( __FILE__ ) ) . '/wp-config.php';
// Configuration
$gravatar_dir = __DIR__ . '/output/camptix-gravatar-badges/';
$original_csv = fopen( $gravatar_dir . 'badges-input.csv', 'r' ); // @todo accept filenames from cli argument
$final_csv = fopen( $gravatar_dir . 'badges-output.csv', 'w' );
$destination_directory = 'Macintosh HD:Users:ian:Desktop:wcus-badges:'; // @todo accept from cli argument
$replace_empty_twitter = false;
$missing_gravatars = array();
$output_headers = array( 'First Name', 'Last Name', 'Twitter', '@Gravatar' ); // The Gravatar column is prefixed with a @ to let InDesign know that it contains images
echo "\nRunning...";
if ( $original_csv && $final_csv ) {
$row = -1;
$gravatar_file = null;
fputcsv( $final_csv, $output_headers );
while ( ( $attendee = fgetcsv( $original_csv, 1000, "," ) ) !== FALSE ) {
$row++;
if ( $row == 0 ) {
continue; // skip header
}
foreach ( $attendee as $key => $value ) {
$attendee[ $key ] = trim( $value );
}
// Create empty badges for unknown attendees
if ( 'unknown.attendee@example.org' == $attendee[2] ) {
$attendee[0] = $attendee[1] = $attendee[2] = $attendee[3] = '';
}
// Capitalize the first and last names so they look better
// todo if there's more than one word in either field, don't do anything b/c could be something like "Ines van Essen" and they want the "van" lowercase
$attendee[0] = ucwords( $attendee[0] );
$attendee[1] = ucwords( $attendee[1] );
// Download the Gravatar
$filename = 'default.png';
if ( $attendee[2] ) {
$gravatar_source = wp_remote_get( 'http://www.gravatar.com/avatar/' . md5( strtolower( trim( $attendee[2] ) ) ) . '.jpg?s=512&default=404' );
if ( ! is_wp_error( $gravatar_source ) && 200 == $gravatar_source['response']['code'] ) {
$filename = strtolower( remove_accents( $attendee[0] . '-' . $attendee[1] ) );
$filename = sanitize_file_name( $row . '-' . $filename . '.jpg' );
$gravatar_file = fopen( $gravatar_dir . '/' . $filename, 'w' );
fwrite( $gravatar_file, $gravatar_source['body'] );
fclose( $gravatar_file );
}
}
if ( 'default.png' === $filename ) {
$missing_gravatars[] = $attendee[0] .' '. $attendee[1];
}
// Remove the e-mail address from the output file, because it's not used in the badge and InDesign complains about unused fields
unset( $attendee[2] );
// Update the final CSV
$attendee['filename'] = $destination_directory . $filename;
// If they don't have a Twitter handle, add their first name instead. Add a @ to Twitter handles to avoid confusing them with first names.
if ( empty ( $attendee[3] ) ) {
if ( $replace_empty_twitter ) {
$attendee[3] = $attendee[0];
}
} else {
// Strip out everything but the username, and prefix a @
$attendee[3] = '@' . preg_replace(
'/
(https?:\/\/)?
(twitter\.com\/)?
(@)?
/ix',
'',
$attendee[3]
);
}
fputcsv( $final_csv, $attendee );
// Output progress
if ( 0 == $row % 50 ) {
echo "\nProcessed " . $row . " attendees.";
}
// Limit to best case of ~10 iterations per second, to avoid flooding gravatar.com or draining the host server
usleep( 100000 );
}
fclose( $original_csv );
fclose( $final_csv );
$status = "\nFinished processing " . $row . " attendees.\n";
if ( $missing_gravatars ) {
$status .= "\nThe following people did not have a Gravatar:\n\n";
$status .= implode( "\n", $missing_gravatars );
}
} else {
$status = "Error: Couldn't open files.";
}
echo "\n". $status ."\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment