diff --git a/uc-people.php b/uc-people.php new file mode 100644 index 0000000..13b9746 --- /dev/null +++ b/uc-people.php @@ -0,0 +1,172 @@ + 'Contacts', + 'singular_name' => 'Contact', + 'add_new' => 'New Contact', + 'add_new_item' => 'Add New Contact', + 'edit_item' => 'Edit Contact', + 'view_item' => 'View Contact', + 'search_items' => 'Search Contacts' + ); + + $args = array( + 'label' => 'Contacts', + 'labels' => $labels, + 'description' => '', + 'public' => true, + 'menu_position' => 5, + 'menu_icon' => null, + 'supports' => array( 'tags', 'thumbnail', 'revisions', 'page-attributes' ), + 'has_archive' => true + ); + + register_post_type( 'contact', $args ); + + register_taxonomy( 'team', 'contact', + array( + 'label' => 'Teams', + 'labels' => array( + 'name' => 'Teams', + 'singular_name' => 'Team', + 'search_items' => 'Search Teams', + 'edit_item' => 'Edit ', + 'view_item' => 'Team.viewitem', + 'update_item' => 'Update Team', + 'add_new_item' => 'Add new Team', + 'new_item_name' => 'Team.newitemname' + ), + 'public' => true, + 'hierarchical' => true, + 'rewrite' => array('hierarchical' => true ) + ) + ); + register_taxonomy( 'tag', 'contact', + array( + 'label' => 'Tags', + 'labels' => array( + 'name' => 'Tags', + 'singular_name' => 'Tag', + 'search_items' => 'Search Tags', + 'edit_item' => 'Edit ', + 'view_item' => 'Tag.viewitem', + 'update_item' => 'Update Tag', + 'add_new_item' => 'Add new tags', + 'new_item_name' => 'Tag.newitemname' + ), + 'public' => true, + 'hierarchical' => false + ) + ); +} +add_action( 'init', 'create_custom_custom_post_type', 0 ); + +function processCSV($file){ + $data = array(); + + $fh = fopen($file, 'r'); + if( $fh == false ){ + return; + } + $header = fgetcsv($fh); + + while( $line = fgetcsv($fh) ) { + $data[] = array_combine($header, $line); + } + + fclose($fh); + + return $data; +} + +function importFile(){ + if( isset( $_POST['_wpnonce_ucp_import_people_nonce'] ) ){ + check_admin_referer( 'ucp_import_people_nonce', '_wpnonce_ucp_import_people_nonce' ); + + if ( ! function_exists( 'wp_handle_upload' ) ) { + require_once( ABSPATH . 'wp-admin/includes/file.php' ); + } + + if( $_FILES['csv_file']['type'] !== 'text/csv' ){ + echo '

Please upload a CSV file

'; + return; + } + + $uploadedFile = wp_handle_upload( $_FILES['csv_file'], array( 'test_form' => false ) ); + + if( $uploadedFile && !isset($uploadedFile['error']) ){ + $data = processCSV( $uploadedFile['file'] ); + + $fields = array( + 'first_name' => 'First Name', + 'last_name' => 'Last Name', + 'title' => 'Title', + 'about' => 'Biographical Info', + 'email' => 'Email Address', + 'phone' => 'Phone 1', + 'phone_(alternate)' => 'Phone 2', + 'fax' => 'Fax', + 'mailing_address' => 'Mailing Address', + 'office_location' => 'Location', + 'office_hours' => 'Office Hours', + 'courses' => 'Courses', + ); + + foreach( $data as $k => $v ){ + $title = $v['First Name'].' '.$v['Last Name']; + $args = array( + 'post_title' => $title, + 'post_name' => 'testing-contact', + 'post_status' => 'publish', + 'post_type' => 'contact', + 'menu_order' => ( !empty($v['Order']) ? $v['Order'] : 0 ), + ); + $id = wp_insert_post( $args, true ); + wp_set_object_terms( $id, explode(' ', $v['Tags']), 'tag' ); + set_post_thumbnail( $id, intval($v['Profile Image']) ); + foreach ($fields as $l => $w) { + update_field($l, $v[$w], $id); + } + } + echo '

Users have been successfully imported

'; + } else { + echo $uploadedFile['error']; + } + } +} + +function uc_people_settings_page_menu(){ + add_users_page( 'Import People', 'Import People', 'manage_options', 'uc-import-people', 'uc_people_settings_page' ); +} +add_action('admin_menu', 'uc_people_settings_page_menu'); + +function uc_people_settings_page(){ + ?> +
+

Import People

+
+ + + +
+ +
+