diff --git a/uc-people.php b/uc-people.php index 129e14e..67fc1ec 100644 --- a/uc-people.php +++ b/uc-people.php @@ -1,240 +1,240 @@ - '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( 'tags', 'thumbnail', 'revisions', 'page-attributes', 'author' ), - 'has_archive' => true - ); - - 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 ) - ) - ); - 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 - ) - ); -} -add_action( 'init', 'create_custom_custom_post_type', 0 ); - -add_filter('title_save_pre', 'save_title'); -function save_title($my_post_title) { - if ($_POST['post_type'] == 'person') : - $new_title = get_field( "first_name" ).' '.get_field( "last_name" ); - $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'] == 'post_type_name') : - $new_name = strtolower(get_field( "first_name" ).'-'.get_field( "last_name" )); - $my_post_name = $new_name; - endif; - return $my_post_name; -} - -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', - 'about' => 'About', - ); - - foreach( $data as $k => $v ){ - $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 ), - ); - $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_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(){ - ?> -
-

Import People from Users CSV

-

This tool is to help people with the initial setup of "People" in their site.

-

New Site Setup

-

For new sites, use the "Export Users" tool 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.

-

Converting UUP Users to "People"

-

For sites that were using the University User Profiles plugin, use the "Export Users" tool and then import that full list here.

-
-

One Time Use

-

This tool can not be used to update information about each Person. If you upload the same .csv twice, you will end up with two records for each person.

-

Choose .csv in the "export users" specific format.

-
- - - -
- -
- '; - echo '

'; - echo '

'; - echo '

'; - echo ''; -} - - -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(); -} - + '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( 'tags', 'thumbnail', 'revisions', 'page-attributes', 'author' ), + 'has_archive' => true + ); + + 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 ) + ) + ); + 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 + ) + ); +} +add_action( 'init', 'create_custom_custom_post_type', 0 ); + +add_filter('title_save_pre', 'save_title'); +function save_title($my_post_title) { + if ($_POST['post_type'] == 'person') : + $new_title = $_POST['fields']['field_first_name'].' '.$_POST['fields']['field_first_name']; + $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'] == 'post_type_name') : + $new_name = strtolower($_POST['fields']['field_first_name'].' '.$_POST['fields']['field_first_name']); + $my_post_name = $new_name; + endif; + return $my_post_name; +} + +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', + 'about' => 'About', + ); + + foreach( $data as $k => $v ){ + $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 ), + ); + $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_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(){ + ?> +
+

Import People from Users CSV

+

This tool is to help people with the initial setup of "People" in their site.

+

New Site Setup

+

For new sites, use the "Export Users" tool 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.

+

Converting UUP Users to "People"

+

For sites that were using the University User Profiles plugin, use the "Export Users" tool and then import that full list here.

+
+

One Time Use

+

This tool can not be used to update information about each Person. If you upload the same .csv twice, you will end up with two records for each person.

+

Choose .csv in the "export users" specific format.

+
+ + + +
+ +
+ '; + echo '

'; + echo '

'; + echo '

'; + echo ''; +} + + +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(); +} + ?> \ No newline at end of file