/** * admin_init hook. */ function wpcf_embedded_admin_init_hook() { // Add callbacks for post edit pages add_action('load-post.php', 'wpcf_admin_post_page_load_hook'); add_action('load-post-new.php', 'wpcf_admin_post_page_load_hook'); // Add callback for 'media-upload.php' add_filter('get_media_item_args', 'wpcf_get_media_item_args_filter'); // Add save_post callback add_action('save_post', 'wpcf_admin_save_post_hook', 10, 2); // Render messages wpcf_show_admin_messages(); // Render JS settings add_action('admin_head', 'wpcf_admin_render_js_settings'); // Media insert code if (isset($_GET['wpcf-fields-media-insert']) || isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'wpcf-fields-media-insert=1')) { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/file.php'; // Add types button add_filter('attachment_fields_to_edit', 'wpcf_fields_file_attachment_fields_to_edit_filter', 10, 2); // Add JS add_action('admin_head', 'wpcf_fields_file_media_admin_head'); // Filter media TABs add_filter('media_upload_tabs', 'wpcf_fields_file_media_upload_tabs_filter'); } register_post_type('wp-types-group', array('public' => false, 'label' => 'Types Groups', 'can_export' => false)); add_filter('icl_custom_fields_to_be_copied', 'wpcf_custom_fields_to_be_copied', 10, 2); // WPML editor filters add_filter('icl_editor_cf_name', 'wpcf_icl_editor_cf_name_filter'); add_filter('icl_editor_cf_description', 'wpcf_icl_editor_cf_description_filter', 10, 2); add_filter('icl_editor_cf_style', 'wpcf_icl_editor_cf_style_filter', 10, 2); // Initialize translations if (function_exists('icl_register_string') && defined('WPML_ST_VERSION') && !get_option('wpcf_strings_translation_initialized', false)) { wpcf_admin_bulk_string_translation(); update_option('wpcf_strings_translation_initialized', 1); } }
/** * admin_init hook. */ function wpcf_embedded_admin_init_hook() { // Add callbacks for post edit pages add_action('load-post.php', 'wpcf_admin_edit_screen_load_hook'); add_action('load-post-new.php', 'wpcf_admin_edit_screen_load_hook'); // Meta boxes hook add_action('add_meta_boxes', 'wpcf_admin_add_meta_boxes', 10, 2); // Add callback for 'media-upload.php' add_filter('get_media_item_args', 'wpcf_get_media_item_args_filter'); // Add save_post callback add_action('save_post', 'wpcf_admin_save_post_hook', 10, 2); // Add Media callback add_action('add_attachment', 'wpcf_admin_save_attachment_hook', 10); add_action('add_attachment', 'wpcf_admin_add_attachment_hook', 10); add_action('edit_attachment', 'wpcf_admin_save_attachment_hook', 10); // Render messages wpcf_show_admin_messages(); // Render JS settings add_action('admin_head', 'wpcf_admin_render_js_settings'); // Media insert code if (isset($_GET['context']) && $_GET['context'] == 'wpcf-fields-media-insert' || isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'context=wpcf-fields-media-insert') !== false) { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/file.php'; // Add types button add_filter('attachment_fields_to_edit', 'wpcf_fields_file_attachment_fields_to_edit_filter', PHP_INT_MAX, 2); // Filter media TABs add_filter('media_upload_tabs', 'wpcf_fields_file_media_upload_tabs_filter'); } register_post_type(TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, array('public' => false, 'label' => 'Types Groups', 'can_export' => false)); register_post_type(TYPES_USER_META_FIELD_GROUP_CPT_NAME, array('public' => false, 'label' => 'Types User Groups', 'can_export' => false)); register_post_type(TYPES_TERM_META_FIELD_GROUP_CPT_NAME, array('public' => false, 'label' => 'Types Term Groups', 'can_export' => false)); add_filter('icl_custom_fields_to_be_copied', 'wpcf_custom_fields_to_be_copied', 10, 2); // WPML editor filters add_filter('icl_editor_cf_name', 'wpcf_icl_editor_cf_name_filter'); add_filter('icl_editor_cf_description', 'wpcf_icl_editor_cf_description_filter', 10, 2); add_filter('icl_editor_cf_style', 'wpcf_icl_editor_cf_style_filter', 10, 2); // Initialize translations if (function_exists('icl_register_string') && defined('WPML_ST_VERSION') && !get_option('wpcf_strings_translation_initialized', false)) { wpcf_admin_bulk_string_translation(); update_option('wpcf_strings_translation_initialized', 1); } }
/** * Custom Import function for Module Manager. * * Import selected items given by xmlstring. * * @global type $wpdb * @global type $iclTranslationManagement * @param type $data * @param type $_type * @return \WP_Error|boolean */ function wpcf_admin_import_data_from_xmlstring($data = '', $_type = 'types', $context = 'types') { global $wpdb, $wpcf; /* * * TODO Types 1.3 * Merge with wpcf_admin_import_data() */ $result = array('updated' => 0, 'new' => 0, 'failed' => 0, 'errors' => array()); libxml_use_internal_errors(true); $data = simplexml_load_string($data); if (!$data) { echo '<div class="message error"><p>' . __('Error parsing XML', 'wpcf') . '</p></div>'; foreach (libxml_get_errors() as $error) { return new WP_Error('error_parsing_xml', __('Error parsing XML', 'wpcf') . ' ' . $error->message); } libxml_clear_errors(); return false; } $errors = array(); $imported = false; // Process groups if (!empty($data->groups) && 'groups' == $_type) { $imported = true; $groups = array(); // Set Groups insert data from XML foreach ($data->groups->group as $group) { $group = (array) $group; // TODO 1.2.1 Remove // $_id = wpcf_modman_set_submitted_id( _GROUPS_MODULE_MANAGER_KEY_, // $group['ID'] ); $_id = $group['__types_id']; // If Types check if exists in $_POST if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['groups'][$_id])) { continue; } } $group = wpcf_admin_import_export_simplexml2array($group); $group['add'] = true; $group['update'] = false; $groups[$_id] = $group; } // Insert groups foreach ($groups as $group) { $post = array('post_status' => $group['post_status'], 'post_type' => 'wp-types-group', 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\n WHERE post_title = %s AND post_type = %s", $group['post_title'], 'wp-types-group')); // Update (may be forced by bulk action) if ($group['update'] || !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; /* * * Compare checksum to see if updated */ $_checksum = $wpcf->import->checksum('group', $post_to_update, $group['checksum']); $group_wp_id = wp_update_post($post); if (!$group_wp_id) { $errors[] = new WP_Error('group_update_failed', sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title'])); $result['errors'][] = sprintf(__('Group %s update failed', 'wpcf'), $group['post_title']); $result['failed'] += 1; } else { if (!$_checksum) { $result['updated'] += 1; } else { } } } else { $errors[] = new WP_Error('group_update_failed', sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title'])); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { $errors[] = new WP_Error('group_insert_failed', sprintf(__('Group "%s" insert failed', 'wpcf'), $group['post_title'])); $result['errors'][] = sprintf(__('Group %s insert failed', 'wpcf'), $group['post_title']); $result['failed'] += 1; } else { $result['new'] += 1; } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { update_post_meta($group_wp_id, $meta_key, maybe_unserialize($meta_value)); } } $group_check[] = $group_wp_id; if (!empty($post_to_update)) { $group_check[] = $post_to_update; } } } // Process fields if (!empty($data->fields)) { $fields_existing = wpcf_admin_fields_get_fields(); $fields = array(); $fields_check = array(); // Set insert data from XML foreach ($data->fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); $fields[$field['id']] = $field; } // Insert fields foreach ($fields as $field_id => $field) { // If Types check if exists in $_POST // TODO Regular import do not have structure like this if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['groups']['__fields__' . $field['slug']])) { continue; } } if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $_new_field = !isset($fields_existing[$field_id]); if ($_new_field) { $result['new'] += 1; } else { $_checksum = $wpcf->import->checksum('field', $fields_existing[$field_id]['slug'], $field['checksum']); if (!$_checksum) { $result['updated'] += 1; } } $field_data = array(); $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); foreach (array('id', 'name', 'type', 'slug', 'meta_key', 'meta_type') as $key) { if (array_key_exists($key, $field)) { $field_data[$key] = $field[$key]; } } $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } } update_option('wpcf-fields', $fields_existing); } } // Process types if (!empty($data->types) && 'types' == $_type) { $imported = true; $types_existing = get_option('wpcf-custom-types', array()); $types = array(); $types_check = array(); // Set insert data from XML foreach ($data->types->type as $type) { $type = (array) $type; $type = wpcf_admin_import_export_simplexml2array($type); // TODO 1.2.1 Remove // $_id = wpcf_modman_get_submitted_id( _TYPES_MODULE_MANAGER_KEY_, // $type['id'] ); $_id = strval($type['__types_id']); // If Types check if exists in $_POST if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['types'][$_id])) { continue; } } $types[$_id] = $type; } // Insert types foreach ($types as $type_id => $type) { if (isset($type['add']) && !$type['add']) { continue; } if (isset($types_existing[$type_id])) { /* * * Compare checksum to see if updated */ $_checksum = $wpcf->import->checksum('custom_post_type', $type_id, $type['checksum']); if (!$_checksum) { $result['updated'] += 1; } } else { $result['new'] += 1; } /* * Set type */ unset($type['add'], $type['update'], $type['checksum']); $types_existing[$type_id] = $type; $types_check[] = $type_id; } update_option('wpcf-custom-types', $types_existing); // Add relationships /** EMERSON: Restore Types relationships when importing modules */ if (!empty($data->post_relationships)) { $relationship_existing = get_option('wpcf_post_relationship', array()); /** * be sure, $relationship_existing is a array! */ if (!is_array($relationship_existing)) { $relationship_existing = array(); } $relationship = json_decode($data->post_relationships->data, true); if (is_array($relationship)) { $relationship = array_merge($relationship_existing, $relationship); update_option('wpcf_post_relationship', $relationship); } } } // Process taxonomies if (!empty($data->taxonomies) && 'taxonomies' == $_type) { $imported = true; $taxonomies_existing = get_option('wpcf-custom-taxonomies', array()); $taxonomies = array(); $taxonomies_check = array(); // Set insert data from XML foreach ($data->taxonomies->taxonomy as $taxonomy) { // TODO 1.2.1 Remove // $_id = wpcf_modman_get_submitted_id( _TAX_MODULE_MANAGER_KEY_, // $taxonomy['__types_id'] ); $_id = strval($taxonomy->__types_id); // If Types check if exists in $_POST if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['taxonomies'][$_id])) { continue; } } $taxonomy = wpcf_admin_import_export_simplexml2array($taxonomy); $taxonomies[$_id] = $taxonomy; } // Insert taxonomies foreach ($taxonomies as $taxonomy_id => $taxonomy) { if (isset($taxonomy['add']) && !$taxonomy['add'] && !$overwrite_tax) { continue; } if (isset($taxonomies_existing[$taxonomy_id])) { /* * * Compare checksum to see if updated */ $_checksum = $wpcf->import->checksum('custom_taxonomy', $taxonomy_id, $taxonomy['checksum']); if (!$_checksum) { $result['updated'] += 1; } } else { $result['new'] += 1; } // Set tax unset($taxonomy['add'], $taxonomy['update'], $taxonomy['checksum']); $taxonomies_existing[$taxonomy_id] = $taxonomy; $taxonomies_check[] = $taxonomy_id; } update_option('wpcf-custom-taxonomies', $taxonomies_existing); } if ($imported) { // WPML bulk registration // TODO WPML move if (wpcf_get_settings('register_translations_on_import')) { wpcf_admin_bulk_string_translation(); } // Flush rewrite rules wpcf_init_custom_types_taxonomies(); flush_rewrite_rules(); } return $result; }
/** * Imports data from XML. * * @global object $wpdb * */ function wpcf_admin_import_data($data = '', $redirect = true, $context = 'types', $args = array()) { global $wpdb; $data_installer = false; libxml_use_internal_errors(true); $data = simplexml_load_string($data); if (!$data) { echo '<div class="message error"><p>' . __('Error parsing XML', 'wpcf') . '</p></div>'; foreach (libxml_get_errors() as $error) { echo '<div class="message error"><p>' . $error->message . '</p></div>'; } libxml_clear_errors(); return false; } $overwrite_settings = isset($_POST['overwrite-settings']); $overwrite_groups = isset($_POST['overwrite-groups']); $overwrite_fields = isset($_POST['overwrite-fields']); $overwrite_types = isset($_POST['overwrite-types']); $overwrite_tax = isset($_POST['overwrite-tax']); $delete_groups = isset($_POST['delete-groups']); $delete_fields = isset($_POST['delete-fields']); $delete_types = isset($_POST['delete-types']); $delete_tax = isset($_POST['delete-tax']); if ('wpvdemo' == $context && !empty($args)) { /** * allow overwrite */ $overwrite_groups = true; $overwrite_fields = true; $overwrite_types = true; $overwrite_tax = true; include_once dirname(__FILE__) . '/classes/class.types.data.installer.php'; $data_installer = new Types_Data_Installer($data, $args); $data = $data_installer->wpvdemo(); } /** * process settings */ if ($overwrite_settings && isset($data->settings)) { $wpcf_settings = wpcf_get_settings(); foreach (wpcf_admin_import_export_simplexml2array($data->settings) as $key => $value) { $wpcf_settings[$key] = $value; } wpcf_save_settings($wpcf_settings); wpcf_admin_message_store(__('Settings are updated.', 'wpcf')); } // Process groups $groups_check = array(); if (!empty($data->groups)) { $groups = array(); // Set insert data from XML foreach ($data->groups->group as $group) { $group = wpcf_admin_import_export_simplexml2array($group); $groups[$group['ID']] = $group; } // Set insert data from POST if (!empty($_POST['groups'])) { foreach ($_POST['groups'] as $group_id => $group) { if (empty($groups[$group_id])) { continue; } $groups[$group_id]['add'] = !empty($group['add']); $groups[$group_id]['update'] = isset($group['update']) && $group['update'] == 'update' ? true : false; } } else { foreach ($groups as $group_id => $group) { $groups[$group_id]['add'] = true; $groups[$group_id]['update'] = false; } } // Insert groups $show_import_fail_version_message = true; foreach ($groups as $group_id => $group) { $post = array('post_status' => $group['post_status'], 'post_type' => TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); /** * preserve slug */ if (array_key_exists('__types_id', $group)) { $post['post_name'] = $group['__types_id']; } if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", $group['post_title'], TYPES_CUSTOM_FIELD_GROUP_CPT_NAME)); // Update (may be forced by bulk action) if ($group['update'] || $overwrite_groups && !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; $group_wp_id = wp_update_post($post); if (!$group_wp_id) { wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" updated', 'wpcf'), $group['post_title'])); } } else { wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { wpcf_admin_message_store(sprintf(__('Group "%s" insert failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" added', 'wpcf'), $group['post_title'])); } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { if ('_wpcf_conditional_display' == $meta_key) { if (!empty($meta_value)) { $meta_value = wpcf_admin_import_export_simplexml2array($meta_value); if (!is_array($meta_value)) { $meta_value = array(); if ($show_import_fail_version_message) { $show_import_fail_version_message = false; wpcf_admin_message_store(__('The Types settings were not fully imported because it contained unsecured data. You should re-export your Types settings using the latest version of Types', 'wpcf'), 'error'); } } } } update_post_meta($group_wp_id, $meta_key, $meta_value); } } $groups_check[] = $group_wp_id; if (!empty($post_to_update)) { $groups_check[] = $post_to_update; } } } // Delete groups (forced, set in bulk actions) } if ($delete_groups) { $groups_to_delete = get_posts(array('post_type' => TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'post_status' => 'any', 'posts_per_page' => -1)); if (!empty($groups_to_delete)) { foreach ($groups_to_delete as $group_to_delete) { if (!in_array($group_to_delete->ID, $groups_check)) { $deleted = wp_delete_post($group_to_delete->ID, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete->post_title)); } } } } } else { // If not forced, look in POST if (!empty($_POST['groups-to-be-deleted'])) { foreach ($_POST['groups-to-be-deleted'] as $group_to_delete) { $group_to_delete_post = get_post($group_to_delete); if (!empty($group_to_delete_post) && $group_to_delete_post->post_type == TYPES_CUSTOM_FIELD_GROUP_CPT_NAME) { $deleted = wp_delete_post($group_to_delete, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title)); } } else { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete), 'error'); } } } } // Process fields $fields_check = array(); $fields_existing = wpcf_admin_fields_get_fields(); if (!empty($data->fields)) { $fields = array(); // Set insert data from XML foreach ($data->fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); // Set if submitted in 'types' context if ($context == 'types') { // Process only if marked if (isset($_POST['fields'][$field['id']])) { $fields[$field['id']] = $field; } } else { $fields[$field['id']] = $field; } } // Set insert data from POST if (!empty($_POST['fields'])) { foreach ($_POST['fields'] as $field_id => $field) { if (empty($fields[$field_id])) { continue; } $fields[$field_id]['add'] = !empty($field['add']); $fields[$field_id]['update'] = isset($field['update']) && $field['update'] == 'update' ? true : false; } } // Insert fields foreach ($fields as $field_id => $field) { if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $field_data = array(); $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); foreach (array('id', 'name', 'type', 'slug', 'meta_key', 'meta_type') as $key) { if (array_key_exists($key, $field)) { $field_data[$key] = $field[$key]; } } $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } wpcf_admin_message_store(sprintf(__('Field "%s" added/updated', 'wpcf'), $field['name'])); } } // Delete fields if ($delete_fields) { foreach ($fields_existing as $k => $v) { if (!empty($v['data']['controlled'])) { continue; } if (!in_array($k, $fields_check)) { wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$k]['name'])); unset($fields_existing[$k]); } } } else { if (!empty($_POST['fields-to-be-deleted'])) { foreach ($_POST['fields-to-be-deleted'] as $field_to_delete) { wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name'])); unset($fields_existing[$field_to_delete]); } } } update_option('wpcf-fields', $fields_existing); // Process user groups //print_r($data->user_groups);exit; $groups_check = array(); if (!empty($data->user_groups) && isset($data->user_groups->group)) { $groups = array(); // Set insert data from XML foreach ($data->user_groups->group as $group) { $group = wpcf_admin_import_export_simplexml2array($group); $groups[$group['ID']] = $group; } // Set insert data from POST if (!empty($_POST['user_groups'])) { foreach ($_POST['user_groups'] as $group_id => $group) { if (empty($groups[$group_id])) { continue; } $groups[$group_id]['add'] = !empty($group['add']); $groups[$group_id]['update'] = isset($group['update']) && $group['update'] == 'update' ? true : false; } } else { foreach ($groups as $group_id => $group) { $groups[$group_id]['add'] = true; $groups[$group_id]['update'] = false; } } // Insert groups foreach ($groups as $group_id => $group) { $post = array('post_status' => $group['post_status'], 'post_type' => TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", $group['post_title'], TYPES_USER_META_FIELD_GROUP_CPT_NAME)); // Update (may be forced by bulk action) if ($group['update'] || $overwrite_groups && !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; $group_wp_id = wp_update_post($post); if (!$group_wp_id) { wpcf_admin_message_store(sprintf(__('User group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" updated', 'wpcf'), $group['post_title'])); } } else { wpcf_admin_message_store(sprintf(__('User group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { wpcf_admin_message_store(sprintf(__('User group "%s" insert failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" added', 'wpcf'), $group['post_title'])); } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { update_post_meta($group_wp_id, $meta_key, wpcf_admin_import_export_simplexml2array($meta_value)); } } $groups_check[] = $group_wp_id; if (!empty($post_to_update)) { $groups_check[] = $post_to_update; } } } } // Delete groups (forced, set in bulk actions) if ($delete_groups) { $groups_to_delete = get_posts(array('post_type' => TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'post_status' => 'any', 'posts_per_page' => -1)); if (!empty($groups_to_delete)) { foreach ($groups_to_delete as $group_to_delete) { if (!in_array($group_to_delete->ID, $groups_check)) { $deleted = wp_delete_post($group_to_delete->ID, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" deleted', 'wpcf'), $group_to_delete->post_title)); } } } } } else { // If not forced, look in POST if (!empty($_POST['user-groups-to-be-deleted'])) { foreach ($_POST['user-groups-to-be-deleted'] as $group_to_delete) { $group_to_delete_post = get_post($group_to_delete); if (!empty($group_to_delete_post) && $group_to_delete_post->post_type == TYPES_USER_META_FIELD_GROUP_CPT_NAME) { $deleted = wp_delete_post($group_to_delete, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title)); } } else { wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete), 'error'); } } } } // Process fields $fields_existing = wpcf_admin_fields_get_fields(false, false, false, 'wpcf-usermeta'); $fields_check = array(); if (!empty($data->user_fields)) { $fields = array(); // Set insert data from XML foreach ($data->user_fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); // Set if submitted in 'types' context if ($context == 'types') { // Process only if marked if (isset($_POST['user_fields'][$field['id']])) { $fields[$field['id']] = $field; } } else { $fields[$field['id']] = $field; } } // Set insert data from POST if (!empty($_POST['user_fields'])) { foreach ($_POST['user_fields'] as $field_id => $field) { if (empty($fields[$field_id])) { continue; } $fields[$field_id]['add'] = !empty($field['add']); $fields[$field_id]['update'] = isset($field['update']) && $field['update'] == 'update' ? true : false; } } // Insert fields foreach ($fields as $field_id => $field) { if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $field_data = array(); $field_data['id'] = $field['id']; $field_data['name'] = $field['name']; $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['type'] = $field['type']; $field_data['slug'] = $field['slug']; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } wpcf_admin_message_store(sprintf(__('User field "%s" added/updated', 'wpcf'), $field['name'])); } } // Delete fields if ($delete_fields) { foreach ($fields_existing as $k => $v) { if (!empty($v['data']['controlled'])) { continue; } if (!in_array($k, $fields_check)) { wpcf_admin_message_store(sprintf(__('User field "%s" deleted', 'wpcf'), $fields_existing[$k]['name'])); unset($fields_existing[$k]); } } } else { if (!empty($_POST['user-fields-to-be-deleted'])) { foreach ($_POST['user-fields-to-be-deleted'] as $field_to_delete) { wpcf_admin_message_store(sprintf(__('User field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name'])); unset($fields_existing[$field_to_delete]); } } } update_option('wpcf-usermeta', $fields_existing); // Process types $types_existing = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); $types_check = array(); if (!empty($data->types) && isset($data->types->type)) { $types = array(); // Set insert data from XML foreach ($data->types->type as $type) { $type = wpcf_admin_import_export_simplexml2array($type); // Set if submitted in 'types' context if ($context == 'types') { if (isset($_POST['types'][$type['id']])) { $types[$type['id']] = $type; } } else { $types[$type['id']] = $type; } } // Set insert data from POST if (!empty($_POST['types'])) { foreach ($_POST['types'] as $type_id => $type) { if (empty($types[$type_id])) { continue; } $types[$type_id]['add'] = !empty($type['add']); $types[$type_id]['update'] = isset($type['update']) && $type['update'] == 'update' ? true : false; } } // Insert types foreach ($types as $type_id => $type) { if (isset($type['add']) && !$type['add'] && !$overwrite_types) { continue; } unset($type['add'], $type['update']); $types_existing[$type_id] = $type; $types_check[] = $type_id; wpcf_admin_message_store(sprintf(__('Custom post type "%s" added/updated', 'wpcf'), $type_id)); } } // Delete types if ($delete_types) { foreach ($types_existing as $k => $v) { if (!in_array($k, $types_check)) { unset($types_existing[$k]); wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), esc_html($k))); } } } else { if (!empty($_POST['types-to-be-deleted'])) { foreach ($_POST['types-to-be-deleted'] as $type_to_delete) { wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), $types_existing[$type_to_delete]['labels']['name'])); unset($types_existing[$type_to_delete]); } } } update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $types_existing); // Process taxonomies $taxonomies_existing = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); $taxonomies_check = array(); if (!empty($data->taxonomies) && isset($data->taxonomies->taxonomy)) { $taxonomies = array(); // Set insert data from XML foreach ($data->taxonomies->taxonomy as $taxonomy) { $taxonomy = wpcf_admin_import_export_simplexml2array($taxonomy); // Set if submitted in 'types' context if ($context == 'types') { if (isset($_POST['taxonomies'][$taxonomy['id']])) { $taxonomies[$taxonomy['id']] = $taxonomy; } } else { $taxonomies[$taxonomy['id']] = $taxonomy; } } // Set insert data from POST if (!empty($_POST['taxonomies'])) { foreach ($_POST['taxonomies'] as $taxonomy_id => $taxonomy) { if (empty($taxonomies[$taxonomy_id])) { continue; } $taxonomies[$taxonomy_id]['add'] = !empty($taxonomy['add']); $taxonomies[$taxonomy_id]['update'] = isset($taxonomy['update']) && $taxonomy['update'] == 'update' ? true : false; } } // Insert taxonomies foreach ($taxonomies as $taxonomy_id => $taxonomy) { if (isset($taxonomy['add']) && !$taxonomy['add'] && !$overwrite_tax) { continue; } unset($taxonomy['add'], $taxonomy['update']); $taxonomies_existing[$taxonomy_id] = $taxonomy; $taxonomies_check[] = $taxonomy_id; wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" added/updated', 'wpcf'), $taxonomy_id)); } } /** * reset TOOLSET_EDIT_LAST */ if ($data_installer) { $data_installer->reset_toolset_edit_last(); } // Delete taxonomies if ($delete_tax) { foreach ($taxonomies_existing as $k => $v) { if (!in_array($k, $taxonomies_check)) { unset($taxonomies_existing[$k]); wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $k)); } } } else { if (!empty($_POST['taxonomies-to-be-deleted'])) { foreach ($_POST['taxonomies-to-be-deleted'] as $taxonomy_to_delete) { wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $taxonomies_existing[$taxonomy_to_delete]['labels']['name'])); unset($taxonomies_existing[$taxonomy_to_delete]); } } } update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxonomies_existing); // Add relationships if (!empty($data->post_relationships) && !empty($_POST['post_relationship'])) { $relationship_existing = get_option('wpcf_post_relationship', array()); /** * be sure, $relationship_existing is a array! */ if (!is_array($relationship_existing)) { $relationship_existing = array(); } $relationship = json_decode($data->post_relationships->data, true); if (is_array($relationship)) { $relationship = array_merge($relationship_existing, $relationship); update_option('wpcf_post_relationship', $relationship); wpcf_admin_message_store(__('Post relationships created', 'wpcf')); } else { wpcf_admin_message_store(__('Post relationships settings were not imported because it contained unsecured data. You should re-export your Types settings using the latest version of Types', 'wpcf'), 'error'); } } // WPML bulk registration if (wpcf_get_settings('register_translations_on_import')) { wpcf_admin_bulk_string_translation(); } // Flush rewrite rules wpcf_init_custom_types_taxonomies(); flush_rewrite_rules(); if ($redirect) { echo '<script type="text/javascript"> <!-- window.location = "' . admin_url('admin.php?page=wpcf-import-export') . '" //--> </script>'; die; } }
/** * Imports data from XML. */ function wpcf_admin_import_data($data = '', $redirect = true) { global $wpdb; libxml_use_internal_errors(true); $data = simplexml_load_string($data); if (!$data) { echo '<div class="message error"><p>' . __('Error parsing XML', 'wpcf') . '</p></div>'; foreach (libxml_get_errors() as $error) { echo '<div class="message error"><p>' . $error->message . '</p></div>'; } libxml_clear_errors(); return false; } $overwrite_groups = isset($_POST['overwrite-groups']); $overwrite_fields = isset($_POST['overwrite-fields']); $overwrite_types = isset($_POST['overwrite-types']); $overwrite_tax = isset($_POST['overwrite-tax']); $delete_groups = isset($_POST['delete-groups']); $delete_fields = isset($_POST['delete-fields']); $delete_types = isset($_POST['delete-types']); $delete_tax = isset($_POST['delete-tax']); // Process groups if (!empty($data->groups)) { $groups = array(); // Set insert data from XML foreach ($data->groups->group as $group) { $group = wpcf_admin_import_export_simplexml2array($group); $groups[$group['ID']] = $group; } // Set insert data from POST if (!empty($_POST['groups'])) { foreach ($_POST['groups'] as $group_id => $group) { if (empty($groups[$group_id])) { continue; } $groups[$group_id]['add'] = !empty($group['add']); $groups[$group_id]['update'] = isset($group['update']) && $group['update'] == 'update' ? true : false; } } else { foreach ($groups as $group_id => $group) { $groups[$group_id]['add'] = true; $groups[$group_id]['update'] = false; } } // Insert groups $groups_check = array(); foreach ($groups as $group_id => $group) { $post = array('post_status' => $group['post_status'], 'post_type' => 'wp-types-group', 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\n WHERE post_title = %s AND post_type = %s", $group['post_title'], 'wp-types-group')); // Update (may be forced by bulk action) if ($group['update'] || $overwrite_groups && !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; $group_wp_id = wp_update_post($post); if (!$group_wp_id) { wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" updated', 'wpcf'), $group['post_title'])); } } else { wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { wpcf_admin_message_store(sprintf(__('Group "%s" insert failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" added', 'wpcf'), $group['post_title'])); } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { update_post_meta($group_wp_id, $meta_key, $meta_value); } } $group_check[] = $group_wp_id; if (!empty($post_to_update)) { $group_check[] = $post_to_update; } } } // Delete groups (forced, set in bulk actions) if ($delete_groups) { $groups_to_delete = get_posts('post_type=wp-types-group&status=null'); if (!empty($groups_to_delete)) { foreach ($groups_to_delete as $group_to_delete) { if (!in_array($group_to_delete->ID, $group_check)) { $deleted = wp_delete_post($group_to_delete->ID, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete->post_title)); } } } } } else { // If not forced, look in POST if (!empty($_POST['groups-to-be-deleted'])) { foreach ($_POST['groups-to-be-deleted'] as $group_to_delete) { $group_to_delete_post = get_post($group_to_delete); if (!empty($group_to_delete_post) && $group_to_delete_post->post_type == 'wp-types-group') { $deleted = wp_delete_post($group_to_delete, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title)); } } else { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete), 'error'); } } } } } // Process fields if (!empty($data->fields)) { $fields_existing = wpcf_admin_fields_get_fields(); $fields = array(); $fields_check = array(); // Set insert data from XML foreach ($data->fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); $fields[$field['id']] = $field; } // Set insert data from POST if (!empty($_POST['fields'])) { foreach ($_POST['fields'] as $field_id => $field) { if (empty($fields[$field_id])) { continue; } $fields[$field_id]['add'] = !empty($field['add']); $fields[$field_id]['update'] = isset($field['update']) && $field['update'] == 'update' ? true : false; } } // Insert fields foreach ($fields as $field_id => $field) { if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $field_data = array(); $field_data['id'] = $field['id']; $field_data['name'] = $field['name']; $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['type'] = $field['type']; $field_data['slug'] = $field['slug']; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } wpcf_admin_message_store(sprintf(__('Field "%s" added/updated', 'wpcf'), $field['name'])); } // Delete fields if ($delete_fields) { foreach ($fields_existing as $k => $v) { if (!empty($v['data']['controlled'])) { continue; } if (!in_array($k, $fields_check)) { wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$k]['name'])); unset($fields_existing[$k]); } } } else { if (!empty($_POST['fields-to-be-deleted'])) { foreach ($_POST['fields-to-be-deleted'] as $field_to_delete) { wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name'])); unset($fields_existing[$field_to_delete]); } } } update_option('wpcf-fields', $fields_existing); } // Process types if (!empty($data->types)) { $types_existing = get_option('wpcf-custom-types', array()); $types = array(); $types_check = array(); // Set insert data from XML foreach ($data->types->type as $type) { $type = wpcf_admin_import_export_simplexml2array($type); $types[$type['id']] = $type; } // Set insert data from POST if (!empty($_POST['types'])) { foreach ($_POST['types'] as $type_id => $type) { if (empty($types[$type_id])) { continue; } $types[$type_id]['add'] = !empty($type['add']); $types[$type_id]['update'] = isset($type['update']) && $type['update'] == 'update' ? true : false; } } // Insert types foreach ($types as $type_id => $type) { if (isset($type['add']) && !$type['add'] && !$overwrite_types) { continue; } unset($type['add'], $type['update']); $types_existing[$type_id] = $type; $types_check[] = $type_id; wpcf_admin_message_store(sprintf(__('Custom post type "%s" added/updated', 'wpcf'), $type_id)); } // Delete types if ($delete_types) { foreach ($types_existing as $k => $v) { if (!in_array($k, $types_check)) { unset($types_existing[$k]); wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), esc_html($k))); } } } else { if (!empty($_POST['types-to-be-deleted'])) { foreach ($_POST['types-to-be-deleted'] as $type_to_delete) { wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), $types_existing[$type_to_delete]['labels']['name'])); unset($types_existing[$type_to_delete]); } } } update_option('wpcf-custom-types', $types_existing); } // Process taxonomies if (!empty($data->taxonomies)) { $taxonomies_existing = get_option('wpcf-custom-taxonomies', array()); $taxonomies = array(); $taxonomies_check = array(); // Set insert data from XML foreach ($data->taxonomies->taxonomy as $taxonomy) { $taxonomy = wpcf_admin_import_export_simplexml2array($taxonomy); $taxonomies[$taxonomy['id']] = $taxonomy; } // Set insert data from POST if (!empty($_POST['taxonomies'])) { foreach ($_POST['taxonomies'] as $taxonomy_id => $taxonomy) { if (empty($taxonomies[$taxonomy_id])) { continue; } $taxonomies[$taxonomy_id]['add'] = !empty($taxonomy['add']); $taxonomies[$taxonomy_id]['update'] = isset($taxonomy['update']) && $taxonomy['update'] == 'update' ? true : false; } } // Insert taxonomies foreach ($taxonomies as $taxonomy_id => $taxonomy) { if (isset($taxonomy['add']) && !$taxonomy['add'] && !$overwrite_tax) { continue; } unset($taxonomy['add'], $taxonomy['update']); $taxonomies_existing[$taxonomy_id] = $taxonomy; $taxonomies_check[] = $taxonomy_id; wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" added/updated', 'wpcf'), $taxonomy_id)); } // Delete taxonomies if ($delete_tax) { foreach ($taxonomies_existing as $k => $v) { if (!in_array($k, $taxonomies_check)) { unset($taxonomies_existing[$k]); wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $k)); } } } else { if (!empty($_POST['taxonomies-to-be-deleted'])) { foreach ($_POST['taxonomies-to-be-deleted'] as $taxonomy_to_delete) { wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $taxonomies_existing[$taxonomy_to_delete]['labels']['name'])); unset($taxonomies_existing[$taxonomy_to_delete]); } } } update_option('wpcf-custom-taxonomies', $taxonomies_existing); } // Add relationships if (!empty($data->post_relationships) && !empty($_POST['post_relationship'])) { $relationship_existing = get_option('wpcf_post_relationship', array()); foreach ($data->post_relationships->post_relationship as $relationship) { $relationship = unserialize($relationship); $relationship = array_merge($relationship_existing, $relationship); update_option('wpcf_post_relationship', $relationship); wpcf_admin_message_store(__('Post relationships created', 'wpcf')); break; } } // WPML bulk registration if (wpcf_get_settings('register_translations_on_import')) { wpcf_admin_bulk_string_translation(); } // Flush rewrite rules wpcf_init_custom_types_taxonomies(); flush_rewrite_rules(); if ($redirect) { echo '<script type="text/javascript"> <!-- window.location = "' . admin_url('admin.php?page=wpcf-import-export') . '" //--> </script>'; die; } }