/** * Process import file * * This parses a file and triggers importation of its widgets. * * @since 0.3 * @param string $file Path to .wie file uploaded * @global string $wie_import_results */ function wie_process_import_file($file) { global $wie_import_results; // File exists? if (!file_exists($file)) { wp_die(__('Import file could not be found. Please try again.', 'cumulo'), '', array('back_link' => true)); } // Get file contents and decode $data = file_get_contents($file); $data = json_decode($data); // Import the widget data // Make results available for display on import/export page $wie_import_results = wie_import_data($data); }
/** * Import widgets and fix invalid data */ public function import_widgets() { // get the widgets importer if (!function_exists('wie_import_data')) { include_once get_template_directory() . '/lib/vendor/importer/widgets-importer.php'; } // get the widget data and import it $widget_data = $this->demo_data_path . 'widgets.wie'; if (file_exists($widget_data)) { $data = json_decode(file_get_contents($widget_data)); /* * Modify the sidebar data to assign new category mappings */ foreach ($data as $sidebar => $sidebar_data) { // only process if there are widgets if (!is_array($sidebar_data) && !is_object($sidebar_data)) { continue; } foreach ($sidebar_data as $widget => $widget_data) { // only process if there are widgets if (!is_array($sidebar_data) && !is_object($sidebar_data)) { continue; } // process the widget data foreach ($widget_data as $key => $value) { // only remapping the categories if (in_array($key, array('cats', 'category', 'cat', 'categories')) && (is_array($value) or is_object($value))) { $processed = array(); foreach ($value as $k => $v) { $processed[$k] = $v; // perhaps the value is a category id if (!empty($v) && is_numeric($v) && !empty($this->wp_import->processed_terms[$v])) { @($processed[$v] = $this->wp_import->processed_terms[$v]); } // bunyad recent tabbed has it flipped - key is the category id if (!empty($k) && strstr($widget, 'bunyad-tabbed-recent-widget') && !empty($this->wp_import->processed_terms[$k])) { @($processed[$k] = $this->wp_import->processed_terms[$k]); } } // update main data $data->{$sidebar}->{$widget}->{$key} = $processed; } else { if (is_object($value)) { $data->{$sidebar}->{$widget}->{$key} = (array) $value; } } // custom menu item? remap to the correct taxonomy if ($key == 'nav_menu' && !empty($this->wp_import->processed_terms[$value])) { $data->{$sidebar}->{$widget}->{$key} = $this->wp_import->processed_terms[$value]; } } // end process widget data } // end process sidebars } // end main data modification loop ob_start(); wie_import_data($data); $widget_result = ob_get_clean(); } return $widget_result; /* * Fix Bunyad "Tabs" widget */ /*$bunyad_tabbed_recent = maybe_unserialize(get_option('widget_bunyad-tabbed-recent-widget')); if (is_array($bunyad_tabbed_recent)) { // convert stdObject to arrays for nested arrays converted to stdObject foreach ($bunyad_tabbed_recent as $key => $value) { foreach ($value as $sub_key => $val) { if (!is_object($val) && !is_array($val)) { continue; } $val = (array) $val; $bunyad_tabbed_recent[$key][$sub_key] = array_combine( array_map('intval', array_keys($val)), array_values($val) ); } } update_option('widget_bunyad-tabbed-recent-widget', $bunyad_tabbed_recent); }*/ }
protected function do_import_widgets($cfg) { $file = $cfg['file']; // Get file contents and decode $data = json_decode(file_get_contents($file)); if (empty($data)) { throw new Exception('[diw501] Widgets file data parsing error. Please contact support.'); } $import_results = wie_import_data($data); $report_html = ''; foreach ($import_results as $area_key => $area_details) { if (empty($area_details['widgets'])) { continue; } $widgets_info = array(); foreach ($area_details['widgets'] as $widget_details) { //if $widget_details['message_type'] == 'warning' $widgets_info[] = sprintf("%s - %s", !empty($widget_details['title']) && 'No Title' != $widget_details['title'] ? sprintf('%s [ %s ]', $widget_details['name'], $widget_details['title']) : $widget_details['name'], $widget_details['message']); } $report_html .= sprintf('<div class="widgets-report"><div class="widgets-report__title">%s</div><div class="widgets-report__list">%s</div></div>', $area_details['name'], join('<br>', $widgets_info)); } return $report_html; }
function om_ajax_import_tool() { if (!current_user_can('manage_options')) { die; } if (get_magic_quotes_gpc()) { $_POST = stripslashes_deep($_POST); } if (!isset($_POST['om_action'])) { die; } switch ($_POST['om_action']) { case 'start': $data = array('error' => 0); if (!file_exists($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['wordpress_xml'])) { $data['error'] = 1; wp_send_json($data); } if (!class_exists('WXR_Parser')) { require $GLOBALS['omImportTool']['path'] . 'includes/parsers.php'; } $parser = new WXR_Parser(); $import_data = $parser->parse($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['wordpress_xml']); unset($parser); if (is_wp_error($import_data)) { $data['error'] = 1; wp_send_json($data); } $data['common'] = array('base_url' => esc_url($import_data['base_url'])); $data['attachments'] = array(); $author = (int) get_current_user_id(); foreach ($import_data['posts'] as $post) { if ('attachment' == $post['post_type']) { $post_parent = (int) $post['post_parent']; $postdata = array('import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], 'post_status' => $post['status'], 'post_name' => $post['post_name'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], 'post_type' => $post['post_type'], 'post_password' => $post['post_password']); $remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; // try to use _wp_attached file for upload folder placement to ensure the same location as the export site // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() $postdata['upload_date'] = $post['post_date']; if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { if ($meta['key'] == '_wp_attached_file') { if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches)) { $postdata['upload_date'] = $matches[0]; } break; } } } $postdata['postmeta'] = $post['postmeta']; $data['attachments'][] = array('data' => $postdata, 'remote_url' => $remote_url); } } $data['last_attachment_index'] = -1; $variables_dump = get_option(OM_THEME_PREFIX . 'import_process_data'); if (!empty($variables_dump) && is_array($variables_dump)) { if (isset($variables_dump['last_attachment_index'])) { $data['last_attachment_index'] = $variables_dump['last_attachment_index']; } } wp_send_json($data); break; case 'process_attachments': $ret = array('error' => 0); if (isset($_POST['data']['attachments'])) { if (!defined('WP_LOAD_IMPORTERS')) { define('WP_LOAD_IMPORTERS', true); } if (!class_exists('WP_Import')) { // if WP importer doesn't exist $wp_import = $GLOBALS['omImportTool']['path'] . 'includes/wordpress-importer.php'; include $wp_import; } if (class_exists('WP_Importer') && class_exists('WP_Import')) { // check for main import class and wp import class $importer = new WP_Import(); $importer->base_url = $_POST['data']['common']['base_url']; $importer->fetch_attachments = true; $variables_dump = get_option(OM_THEME_PREFIX . 'import_process_data'); if (!empty($variables_dump) && is_array($variables_dump)) { $importer->post_orphans = $variables_dump['post_orphans']; $importer->processed_posts = $variables_dump['processed_posts']; $importer->url_remap = $variables_dump['url_remap']; } $last_attachment_index = $_POST['data']['first_attachment_index']; foreach ($_POST['data']['attachments'] as $attachment) { $post = $attachment['data']; $importer->post_orphans[intval($post['import_id'])] = (int) $post['post_parent']; $post['post_parent'] = 0; $post_id = $importer->process_attachment($post, $attachment['remote_url']); if (is_wp_error($post_id)) { continue; } $importer->processed_posts[intval($post['import_id'])] = (int) $post_id; // add/update post meta if (!empty($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { $key = $meta['key']; $value = false; if ('_edit_last' == $key) { continue; } if ($key) { // export gets meta straight from the DB so could have a serialized string if (!$value) { $value = maybe_unserialize($meta['value']); } add_post_meta($post_id, $key, $value); } } } $variables_dump['last_attachment_index'] = $last_attachment_index; $last_attachment_index++; } $variables_dump['post_orphans'] = $importer->post_orphans; $variables_dump['processed_posts'] = $importer->processed_posts; $variables_dump['url_remap'] = $importer->url_remap; update_option(OM_THEME_PREFIX . 'import_process_data', $variables_dump); } } wp_send_json($ret); break; case 'process_other': $ret = array('error' => 0); if (!file_exists($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['wordpress_xml'])) { $ret['error'] = 1; wp_send_json($ret); } if (!defined('WP_LOAD_IMPORTERS')) { define('WP_LOAD_IMPORTERS', true); } if (!class_exists('WP_Import')) { // if WP importer doesn't exist $wp_import = $GLOBALS['omImportTool']['path'] . 'includes/wordpress-importer.php'; include $wp_import; } if (class_exists('WP_Importer') && class_exists('WP_Import')) { // check for main import class and wp import class // Content $importer = new WP_Import(); $importer->fetch_attachments = false; $variables_dump = get_option(OM_THEME_PREFIX . 'import_process_data'); if (!empty($variables_dump) && is_array($variables_dump)) { $importer->post_orphans = $variables_dump['post_orphans']; $importer->processed_posts = $variables_dump['processed_posts']; $importer->url_remap = $variables_dump['url_remap']; } add_filter('wp_import_post_meta', 'om_import_modify_meta'); ob_start(); $importer->import($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['wordpress_xml']); ob_end_clean(); om_import_menu_meta($importer->processed_menu_items); update_option(OM_THEME_PREFIX . 'import_process_data', false); // Menus to locations $locations = get_theme_mod('nav_menu_locations'); $menus = wp_get_nav_menus(); if ($menus) { foreach ($menus as $menu) { if (isset($GLOBALS['omImportTool']['menus'][$menu->name])) { $locations[$GLOBALS['omImportTool']['menus'][$menu->name]] = $menu->term_id; } } } set_theme_mod('nav_menu_locations', $locations); // set menus to locations // Import Theme Options if (file_exists($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['theme_options_dat'])) { $s = trim(file_get_contents($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['theme_options_dat'])); $options = @unserialize($s); if (is_array($options)) { $wp_upload_dir = wp_upload_dir(); if (isset($options['options'][OM_THEME_PREFIX . "default_title_bg_img"])) { $options['options'][OM_THEME_PREFIX . 'default_title_bg_img'] = str_replace($GLOBALS['omImportTool']['uploads_replace_dir'], $wp_upload_dir['baseurl'], $options['options'][OM_THEME_PREFIX . 'default_title_bg_img']); } om_options_do_import_data($options); } } // Widgets if (file_exists($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['widgets_dat'])) { if (!function_exists('wie_available_widgets')) { require $GLOBALS['omImportTool']['path'] . 'includes/widgets-widgets.php'; } if (!function_exists('wie_import_data')) { require $GLOBALS['omImportTool']['path'] . 'includes/widgets-import.php'; } $data = json_decode(file_get_contents($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['widgets_dat'])); wie_import_data($data); } // Layer Slider if (isset($GLOBALS['lsPluginVersion']) || defined('LS_PLUGIN_VERSION')) { if ($GLOBALS['omImportTool']['layer_slider_dat']) { om_ls_import_sliders($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['layer_slider_dat']); } } // RevSlider if (class_exists('RevSlider')) { if (!empty($GLOBALS['omImportTool']['rev_slider_dat'])) { foreach ($GLOBALS['omImportTool']['rev_slider_dat'] as $file) { if ($file) { om_rev_import_slider($GLOBALS['omImportTool']['path'] . $file); } } } } // Set reading options $front_page = $GLOBALS['omImportTool']['reading']['front_page_title'] ? get_page_by_title($GLOBALS['omImportTool']['reading']['front_page_title']) : false; $posts_page = $GLOBALS['omImportTool']['reading']['posts_page_title'] ? get_page_by_title($GLOBALS['omImportTool']['reading']['posts_page_title']) : false; if ($front_page || $posts_page) { update_option('show_on_front', 'page'); if ($front_page) { update_option('page_on_front', $front_page->ID); } if ($posts_page) { update_option('page_for_posts', $posts_page->ID); } } } wp_send_json($ret); break; } }