* * Copyright 2008-2011 Crowd Favorite, Ltd. All rights reserved. * Released under the GPL license * http://www.opensource.org/licenses/gpl-license.php * * Based on code found in WordPress Importer plugin */ function dtd_importer_enqueue() { if (isset($_REQUEST['import']) && $_REQUEST['import'] == 'kipling_dtd_xml') { wp_enqueue_style('anno-importer', trailingslashit(get_template_directory_uri()) . 'plugins/annotum-importers/importer.css'); } } add_action('admin_print_styles', 'dtd_importer_enqueue'); function add_xml_import_link_to_admin_menu() { /** * Add an entry to the Article admin menu to import XML (Kipling) files */ add_submenu_page( 'edit.php?post_type=article', _x('XML Import', 'Admin menu title', 'anno'), _x('XML Import', 'Admin menu title', 'anno'), 'edit_posts', 'admin.php?import=kipling_dtd_xml'); } add_action('admin_menu', 'add_xml_import_link_to_admin_menu'); if (!defined('WP_LOAD_IMPORTERS')) return; if ( !class_exists('Knol_Import')) { $class_knol_importer = trailingslashit(TEMPLATEPATH).'plugins/annotum-importers/knol-importer.php'; if (file_exists($class_knol_importer)) { require $class_knol_importer; } } if (!class_exists('DTD_Importer')) { class DTD_Import extends Knol_Import { var $import_slug = 'kipling_dtd_xml'; // Author meta data is stored here. We don't get this data from Knols var $authors_meta = array(); function DTD_Import() {} /** * Parses the XML file and prepares us for the task of processing parsed data * * @param string $file Path to the WXR file for importing */ function import_start( $file ) { if (!is_file($file)) { echo '

' . __( 'Sorry, there has been an error.', 'anno' ) . '
'; echo __( 'The file does not exist, please try again.', 'anno' ) . '

'; $this->footer(); die(); } $import_data = $this->parse( $file ); if ( is_wp_error( $import_data ) ) { echo '

' . __( 'Sorry, there has been an error.', 'anno' ) . '
'; echo esc_html( $import_data->get_error_message() ) . '

'; $this->footer(); die(); } // Validation error likely if (isset($import_data['status']) && $import_data['status'] == 'error') { echo '

'.__('There were the following errors while trying to validate your XML. Please correct them and try again.', 'anno').'

'; foreach ($import_data['errors'] as $error) { $error_lines[] = $error['line']; echo '
'.$error['fullMessage'].'.
'; } $this->output_XML($import_data['content'], $error_lines); $this->footer(); die(); } $this->version = $import_data['version']; $this->get_authors_from_import( $import_data ); $this->posts = $import_data['posts']; $this->terms = $import_data['terms']; $this->categories = $import_data['categories']; $this->tags = $import_data['tags']; $this->base_url = esc_url( $import_data['base_url'] ); wp_defer_term_counting( true ); wp_defer_comment_counting( true ); do_action( 'import_start' ); } /** * Retrieve authors and author meta from parse XML file. Process meta data. * * @param array $import_data Data returned by the parser */ function get_authors_from_import($import_data) { // No fallback options available if (!empty($import_data['authors'])) { $this->authors = $import_data['authors']; } if (!empty($import_data['authors_meta'])) { $this->authors_meta = $import_data['authors_meta']; $this->process_user_meta(); } } /** * Process post meta for created users * */ function process_user_meta() { // Only perform on newly created users foreach ($this->created_users as $old_id => $wp_id) { if (!empty($this->authors_meta[$old_id]) && is_array($this->authors_meta[$old_id])) { foreach ($this->authors_meta[$old_id] as $key => $value) { $value = trim($value); if (!empty($value)) { if ($key == 'bio') { $key == 'descriptions'; } else { $key = '_anno_'.$key; } // We only have single rows per key update_user_meta($wp_id, $key, $value); } } } } } /** * Parse a WXR file * * @param string $file Path to WXR file for parsing * @return array Information gathered from the WXR file */ function parse($file) { $parser = new Kipling_DTD_Parser($file); return $parser->parse($file); } // Display import page title function header() { echo '
'; screen_icon(); echo '

' . __( 'Kipling DTD XML Import', 'anno' ) . '

'; } // Close div.wrap function footer() { echo '
'; } /** * Display introductory text and file upload form */ function greet() { $url = 'admin.php?import=kipling_dtd_xml'; if ($this->_filesystem_init($url)) { echo '
'; echo '

'.__( 'Howdy! Upload your Kipling DTD XML file and we’ll import the articles, keywords, subjects, and users into this site.', 'anno' ).'

'; echo '

'.__( 'Choose a Kipling DTD XML (.xml) file to upload, then click Upload file and import.', 'anno' ).'

'; $this->import_upload_form('admin.php?import=kipling_dtd_xml&step=1'); echo '
'; } } function import_upload_form($action) { $bytes = apply_filters('import_upload_size_limit', wp_max_upload_size()); $size = size_format( $bytes ); $upload_dir = wp_upload_dir(); if (!empty($upload_dir['error'])) : ?>

()

_filesystem_hidden_fields(); ?>
'.$line_number.'.'.esc_html($xml_line).''; $line_number++; } } } } function anno_dtd_importer_init() { /** * Knol Importer object for registering the import callback * @global DTD_Import $dtd_import */ $GLOBALS['dtd_import'] = new DTD_Import(); register_importer('kipling_dtd_xml', 'Kipling DTD XML', __('Import articles, keywords, subjects and users from a Kipling DTD XML file.', 'anno'), array( $GLOBALS['dtd_import'], 'dispatch') ); } add_action('admin_init', 'anno_dtd_importer_init'); ?>