Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Merge pull request #1 from szk11001/master
Browse files Browse the repository at this point in the history
Initial Commit
  • Loading branch information
jmr06005 committed Jul 14, 2015
2 parents a82ce86 + dc754c0 commit 9c1e98d
Showing 1 changed file with 172 additions and 0 deletions.
172 changes: 172 additions & 0 deletions uc-people.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php
/**
* Plugin Name: UC People
* Description: Displays information about people, a replacement for UUP.
* Version: 1.0
* Author: Andrew Bacon and Salman Kaleem of UITS Web Dev
*/

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}

function create_custom_custom_post_type(){
$labels = array(
'name' => '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 '<div class="error notice is-dismissible below-h2" id="message"><p>Please upload a CSV file</p><button class="notice-dismiss" type="button"><span class="screen-reader-text">Dismiss this notice.</span></button></div>';
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 '<div class="updated notice is-dismissible below-h2" id="message"><p>Users have been successfully imported</p><button class="notice-dismiss" type="button"><span class="screen-reader-text">Dismiss this notice.</span></button></div>';
} 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(){
?>
<div class="wrap">
<h2>Import People</h2>
<form action="" method="POST" enctype="multipart/form-data" name="ucp-import">
<?php
wp_nonce_field( 'ucp_import_people_nonce', '_wpnonce_ucp_import_people_nonce' );
?>
<input type="file" name="csv_file" />
<?php
importFile();
submit_button( 'Import' );
?>
</form>

</div>
<?php
}

0 comments on commit 9c1e98d

Please sign in to comment.