Permalink
Cannot retrieve contributors at this time
uc-people/uc-people.php
Go to file<?php | |
/** | |
* Plugin Name: UC People | |
* Description: Displays information about people, a replacement for UUP. | |
* Version: 1.2 | |
* Author: Andrew Bacon and Salman Kaleem of UITS Web Dev | |
*/ | |
// If this file is called directly, abort. | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
require 'acf-export.php'; | |
//require_once(WP_CONTENT_DIR . '/ldap.php'); | |
include 'uc-people-widget.php'; | |
function create_custom_custom_post_type(){ | |
$labels = array( | |
'name' => 'People', | |
'singular_name' => 'Person', | |
'add_new' => 'New Person', | |
'add_new_item' => 'Add New Person', | |
'edit_item' => 'Edit Person', | |
'view_item' => 'View Person', | |
'search_items' => 'Search People' | |
); | |
$args = array( | |
'label' => 'People', | |
'labels' => $labels, | |
'description' => '', | |
'public' => true, | |
'menu_position' => 35, | |
'menu_icon' => 'dashicons-groups', | |
'supports' => array( 'title', 'tags', 'thumbnail', 'revisions', 'page-attributes', 'author' ), | |
'has_archive' => true, | |
'rewrite' => array( | |
'slug' => '/person' | |
) | |
); | |
register_post_type( 'person', $args ); | |
register_taxonomy( 'group', 'person', | |
array( | |
'label' => 'Groups', | |
'labels' => array( | |
'name' => 'Groups', | |
'singular_name' => 'Group', | |
'search_items' => 'Search Groups', | |
'edit_item' => 'Edit Group', | |
'view_item' => 'View Group', | |
'update_item' => 'Update Group', | |
'add_new_item' => 'Add new Group', | |
'new_item_name' => 'New Group' | |
), | |
'public' => true, | |
'hierarchical' => true, | |
'rewrite' => array('hierarchical' => true ), | |
'capabilities' => array ( | |
'manage_terms' => 'manage_categories', // Only admins and editors can manage, edit, and delete | |
'edit_terms' => 'manage_categories', | |
'delete_terms' => 'manage_categories', | |
'assign_terms' => 'edit_posts' | |
) | |
) | |
); | |
register_taxonomy( 'persontag', 'person', | |
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, | |
'capabilities' => array ( | |
'manage_terms' => 'manage_categories', // Only admins and editors can manage, edit, and delete | |
'edit_terms' => 'manage_categories', | |
'delete_terms' => 'manage_categories', | |
'assign_terms' => 'edit_posts' | |
) | |
) | |
); | |
} | |
add_action( 'init', 'create_custom_custom_post_type', 0 ); | |
// Add Order field to Add New Group Page | |
function uc_people_new_group_order_field(){ | |
?> | |
<div class="form-field"> | |
<label for="term_meta[group_order]">Order</label> | |
<input name="term_meta[group_order]" id="term_meta[group_order]" type="number" value="0"> | |
<p class="description">Enter a value for this field or leave it blank</p> | |
</div> | |
<?php | |
} | |
add_action( 'group_add_form_fields', 'uc_people_new_group_order_field', 10, 2 ); | |
// Add Order field to Edit Group page | |
function uc_people_edit_group_order_field( $term ){ | |
$term_meta = get_option( "taxonomy_{$term->term_id}" ); | |
$order = $term_meta['group_order']; | |
?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label for="term_meta[group_order]">Order</label></th> | |
<td> | |
<input type="number" name="term_meta[group_order]" id="term_meta[group_order]" value="<?php echo esc_attr( $order ) ? esc_attr( $order ) : 0; ?>"> | |
<p class="description">Enter a value for this field or leave it blank</p> | |
</td> | |
</tr> | |
<?php | |
} | |
add_action( 'group_edit_form_fields', 'uc_people_edit_group_order_field', 10, 2 ); | |
// Save Order field when editing or creating a new Group | |
function uc_people_save_group_custom_meta( $term_id ) { | |
if ( isset( $_POST['term_meta'] ) ) { | |
$term_meta = get_option( "taxonomy_{$term_id}" ); | |
$cat_keys = array_keys( $_POST['term_meta'] ); | |
foreach ( $cat_keys as $key ) { | |
if ( isset ( $_POST['term_meta'][$key] ) ) { | |
$term_meta[$key] = $_POST['term_meta'][$key]; | |
} | |
} | |
// Save the option array. | |
update_option( "taxonomy_{$term_id}", $term_meta ); | |
} | |
} | |
add_action( 'edited_group', 'uc_people_save_group_custom_meta', 10, 2 ); | |
add_action( 'create_group', 'uc_people_save_group_custom_meta', 10, 2 ); | |
add_filter('title_save_pre', 'save_title'); | |
function save_title($my_post_title) { | |
if ($_POST['post_type'] == 'person') : | |
if( isset($_POST['acf']) ){ // Set when creating new post, not set when using quick edit | |
$new_title = $_POST['acf']['field_first_name'].' '.$_POST['acf']['field_last_name']; | |
} else { | |
$new_title = get_field( 'field_first_name', $_POST['ID'] ).' '.get_field( 'field_last_name', $_POST['ID'] ); | |
} | |
$my_post_title = $new_title; | |
endif; | |
return $my_post_title; | |
} | |
add_filter('name_save_pre', 'save_name'); | |
function save_name($my_post_name) { | |
if ($_POST['post_type'] == 'person') : | |
if( isset($_POST['acf']) ){ | |
$new_name = strtolower($_POST['acf']['field_first_name'].' '.$_POST['acf']['field_last_name']); | |
} else { | |
$new_name = strtolower(get_field( 'field_first_name', $_POST['ID'] ).' '.get_field( 'field_last_name', $_POST['ID'] )); | |
$new_name = wp_unique_post_slug( $new_name, $_POST['ID'], $_POST['post_status'], 'person', $_POST['post_parent'] ); | |
} | |
$my_post_name = $new_name; | |
endif; | |
return $my_post_name; | |
} | |
if(!function_exists('processCSV')){ | |
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( $filename ){ | |
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' ); | |
} | |
$filePathInfo = pathinfo( $_FILES[$filename]['name'] ); | |
if( $filePathInfo['extension'] !== '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[$filename], array( 'test_form' => false ) ); | |
if( $uploadedFile && !isset($uploadedFile['error']) ){ | |
$data = processCSV( $uploadedFile['file'] ); | |
$fields = array( | |
'netid' => 'NetID', | |
'role' => 'Role', | |
'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', | |
'about' => 'About', | |
'url' => 'Website', | |
'author' => 'Author', | |
); | |
foreach( $data as $k => $v ){ | |
$user = username_exists( $v['NetID'] ); | |
//$postAuthorFlag = false; | |
if( $user != false && is_user_member_of_blog($v['Author'], get_current_blog_id() ) ){ | |
//$postAuthorFlag = true; | |
//$role = $v['Role']; | |
//if( $role == 'profile_user' ){ | |
$userObject = new WP_User( $v['Author'] ); | |
$userObject->remove_role('subscriber'); | |
$userObject->remove_role('profile_user'); | |
$userObject->add_role('contributornomedia'); | |
//} | |
} | |
$title = $v['First Name'].' '.$v['Last Name']; | |
$args = array( | |
'post_title' => $title, | |
'post_name' => strtolower($v['First Name']).'-'.strtolower($v['Last Name']), | |
'post_status' => 'publish', | |
'post_type' => 'person', | |
'menu_order' => ( !empty($v['Order']) ? $v['Order'] : 0 ), | |
'post_author' => ( !empty($v['Author']) ? $v['Author']: get_current_user_id() ), | |
); | |
$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) { | |
if( $l == 'netid' || $l == 'role' || $l == 'author' ){ | |
continue; | |
} | |
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_submenu_page('tools.php', '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 from Users CSV</h2> | |
<p>This tool is to help people with the initial setup of "People" in their site.</p> | |
<h3>New Site Setup</h3> | |
<p>For new sites, use the <a href="tools.php?page=export-users-to-csv">"Export Users" tool</a> to generate a template. Open that in a spreadsheet program, and fill out *all* of the information you'll want on your site, and then import that .csv here.</p> | |
<h3>Converting UUP Users to "People"</h3> | |
<p>For sites that were using the University User Profiles plugin, use the <a href="tools.php?page=export-users-to-csv">"Export Users" tool</a> and then import that full list here.</p> | |
<hr/> | |
<h2>One Time Use</h2> | |
<p>This tool can not be used to <i>update</i> information about each Person. If you upload the same .csv twice, you will end up with two records for each person.</p> | |
<p>Choose .csv in the "export users" specific format.</p> | |
<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( 'csv_file' ); | |
submit_button( 'Import People' ); | |
?> | |
</form> | |
</div> | |
<?php | |
} | |
function uc_people_lookup_metabox(){ | |
add_meta_box( 'uc_lookup', 'UConn Lookup', 'uc_people_lookup_metabox_callback', 'person', 'side', 'core' ); | |
} | |
add_action( 'add_meta_boxes', 'uc_people_lookup_metabox' ); | |
function uc_people_lookup_metabox_callback(){ | |
echo '<div id="uc_people_lookup_metabox">'; | |
echo '<p><input type="text" id="uc_people_lookup" class="form-input-tip" size="16">'; | |
echo '<input type="button" id="uc_people_lookup_search" class="button" value="Search"></p>'; | |
echo '<p class="howto"></p>'; | |
echo '</div>'; | |
} | |
function uc_acf_admin_enqueue_scripts(){ | |
wp_enqueue_script( 'ucpeoplejs', plugin_dir_url(__FILE__).'/uc-people.js', array('jquery'), '1.0' ); | |
wp_localize_script( 'ucpeoplejs', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); | |
} | |
add_action('acf/input/admin_enqueue_scripts', 'uc_acf_admin_enqueue_scripts'); | |
add_action( 'wp_ajax_uc_people_lookup', 'uc_people_lookup_callback' ); | |
function uc_people_lookup_callback(){ | |
$query = $_GET['query']; | |
$userInfo = ldap_lookup($query); | |
$userInfo['phone'] = $userInfo['uup-phone1']; | |
unset($userInfo['uup-phone1']); | |
echo wp_json_encode($userInfo); | |
wp_die(); | |
} | |
function uc_people_hide_taxonomies(){ | |
if( !current_user_can( 'manage_categories' ) ){ | |
remove_meta_box('groupdiv', 'person', 'side'); | |
remove_meta_box('tagsdiv-persontag', 'person', 'side'); | |
} | |
} | |
add_action( 'admin_menu', 'uc_people_hide_taxonomies' ); | |
?> |