Пример #1
0
 function wpcf_import_modules_items_taxonomies($result, $xmlstring, $items)
 {
     require_once WPCF_EMBEDDED_INC_ABSPATH . '/import-export.php';
     // transform MM ids to "normal raw ids"
     foreach ($items as $ii => $item) {
         $items[$ii] = wpcf_modman_get_submitted_id(_TAX_MODULE_MANAGER_KEY_, $item);
     }
     $result2 = wpcf_admin_import_data_from_xmlstring($xmlstring, $items, _TAX_MODULE_MANAGER_KEY_, 'modman');
     if (false === $result2 || is_wp_error($result2)) {
         return false === $result2 ? __('Error during Taxonomies import', 'wpcf') : $result2->get_error_message($result2->get_error_code());
     }
     // transform "normal raw ids" back to MM ids
     if (!empty($result2['items'])) {
         foreach ($result2['items'] as $old_id => $new_id) {
             $result2['items'][wpcf_modman_set_submitted_id(_TAX_MODULE_MANAGER_KEY_, $old_id)] = wpcf_modman_set_submitted_id(_TAX_MODULE_MANAGER_KEY_, $new_id);
             unset($result2['items'][$old_id]);
         }
     }
     return $result2;
 }
Пример #2
0
/**
 * 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')
{
    global $wpdb, $wpcf;
    /*
     * 
     * TODO Types 1.2.1
     * 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 insert data from XML
        foreach ($data->groups->group as $group) {
            $group = (array) $group;
            $_id = wpcf_modman_set_submitted_id(_GROUPS_MODULE_MANAGER_KEY_, $group['ID']);
            if (!isset($_POST['items']['groups'][$_id])) {
                continue;
            }
            $group = wpcf_admin_import_export_simplexml2array($group);
            $groups[$group['ID']] = $group;
        }
        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' => '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, $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 (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();
                }
            }
            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);
            $_id = wpcf_modman_set_submitted_id(_TYPES_MODULE_MANAGER_KEY_, $type['id']);
            if (!isset($_POST['items']['types'][$_id])) {
                continue;
            }
            $types[$type['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
        if (!empty($data->post_relationships)) {
            $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);
                break;
            }
        }
    }
    // 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) {
            $_id = wpcf_modman_set_submitted_id(_TAX_MODULE_MANAGER_KEY_, $taxonomy['id']);
            if (!isset($_POST['items']['taxonomies'][$_id])) {
                continue;
            }
            $taxonomy = wpcf_admin_import_export_simplexml2array($taxonomy);
            $taxonomies[$taxonomy['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;
}