static function import_old_staff() { global $wpdb; $old_categories_table = $wpdb->prefix . 'staff_directory_categories'; $old_staff_directory_table = $wpdb->prefix . 'staff_directory'; $old_templates_table = STAFF_TEMPLATES; # # Copy old categories over first # $old_staff_categories_sql = "\n SELECT\n cat_id, name\n\n FROM\n {$old_categories_table}\n "; $old_staff_categories = $wpdb->get_results($old_staff_categories_sql); foreach ($old_staff_categories as $category) { wp_insert_term($category->name, 'staff_category'); } # # Now copy old staff members over # $old_staff = StaffDirectory::get_old_staff(); foreach ($old_staff as $staff) { $new_staff_array = array('post_title' => $staff->name, 'post_content' => $staff->bio, 'post_type' => 'staff', 'post_status' => 'publish'); $new_staff_post_id = wp_insert_post($new_staff_array); update_post_meta($new_staff_post_id, 'position', $staff->position); update_post_meta($new_staff_post_id, 'email', $staff->email_address); update_post_meta($new_staff_post_id, 'phone_number', $staff->phone_number); if (isset($staff->category)) { $old_category_sql = "\n SELECT\n cat_id, name\n\n FROM\n {$old_categories_table}\n\n WHERE\n cat_id={$staff->category}\n "; $old_category = $wpdb->get_results($old_category_sql); $new_category = get_term_by('name', $old_category[0]->name, 'staff_category'); wp_set_post_terms($new_staff_post_id, array($new_category->term_id), 'staff_category'); } if (isset($staff->photo) && $staff->photo != '') { $upload_dir = wp_upload_dir(); $upload_dir = $upload_dir['basedir']; $image_path = $upload_dir . '/staff-photos/' . $staff->photo; $filetype = wp_check_filetype($image_path); $attachment_id = wp_insert_attachment(array('post_title' => $staff->photo, 'post_content' => '', 'post_status' => 'publish', 'post_mime_type' => $filetype['type']), $image_path, $new_staff_post_id); set_post_thumbnail($new_staff_post_id, $attachment_id); } } # # Now copy templates over # $old_html_template_sql = "\n SELECT\n template_code\n\n FROM\n {$old_templates_table}\n\n WHERE\n template_name='staff_index_html'\n "; $old_html_template_results = $wpdb->get_results($old_html_template_sql); update_option('staff_directory_html_template', $old_html_template_results[0]->template_code); $old_css_template_sql = "\n SELECT\n template_code\n\n FROM\n {$old_templates_table}\n\n WHERE\n template_name='staff_index_css'\n "; $old_css_template_results = $wpdb->get_results($old_css_template_sql); update_option('staff_directory_css_template', $old_css_template_results[0]->template_code); # # Now delete the old tables # $drop_tables_sql = "\n DROP TABLE\n {$old_categories_table}, {$old_staff_directory_table}, {$old_templates_table}\n "; $wpdb->get_results($drop_tables_sql); }
<?php /* Plugin Name: Staff Directory Plugin URI: https://wordpress.org/plugins/staff-directory/ Description: Allows Wordpress to keep track of your staff directory for your website. Good for churches, small companies, etc. Version: 1.0.1 Author: Adam Tootle Author URI: http://www.adamtootle.com */ global $wpdb; $staff_directory_table = $wpdb->prefix . 'staff_directory'; define('STAFF_DIRECTORY_TABLE', $wpdb->prefix . 'staff_directory'); define('STAFF_TEMPLATES', $wpdb->prefix . 'staff_directory_templates'); define('STAFF_PHOTOS_DIRECTORY', WP_CONTENT_DIR . "/uploads/staff-photos/"); require_once dirname(__FILE__) . '/classes/staff_settings.php'; StaffSettings::setupDefaults(); require_once dirname(__FILE__) . '/classes/staff_directory.php'; require_once dirname(__FILE__) . '/classes/staff_directory_shortcode.php'; require_once dirname(__FILE__) . '/classes/staff_directory_admin.php'; StaffDirectory::register_post_types(); StaffDirectory::set_default_meta_fields_if_necessary(); StaffDirectoryAdmin::register_admin_menu_items(); StaffDirectoryShortcode::register_shortcode(); if (StaffDirectory::show_import_message()) { StaffDirectoryAdmin::register_import_old_staff_message(); } register_activation_hook(__FILE__, array('StaffDirectory', 'set_default_templates_if_necessary'));
static function import() { $did_import_old_staff = false; if (isset($_GET['import']) && $_GET['import'] == 'true') { StaffDirectory::import_old_staff(); $did_import_old_staff = true; } if (StaffDirectory::has_old_staff_table()) { ?> <h2>Staff Directory Import</h2> <p> This tool is provided to import staff from an older version of this plugin. This will copy old staff members over to the new format, but it is advised that you backup your database before proceeding. Chances are you won't need it, but it's always better to be safe than sorry! WordPress provides some <a href="https://codex.wordpress.org/Backing_Up_Your_Database" target="_blank">instructions</a> on how to backup your database. </p> <p> Once you're ready to proceed, simply use the button below to import old staff members to the newer version of the plugin. </p> <p> <a href="<?php echo get_admin_url(); ?> edit.php?post_type=staff&page=staff-directory-import&import=true" class="button button-primary">Import Old Staff</a> </p> <?php } else { ?> <?php if ($did_import_old_staff) { ?> <div class="updated"> <p> Old staff was successfully imported! You can <a href="<?php echo get_admin_url(); ?> edit.php?post_type=staff">view all staff here</a>. </p> </div> <?php } else { ?> <p> It doesn't look like you have any staff members from an older version of the plugin. You're good to go! </p> <?php } ?> <?php } }