Example #1
0
/**
 * Main init hook.
 */
function wpcf_embedded_init()
{
    $locale = get_locale();
    load_textdomain('wpcf', WPCF_EMBEDDED_ABSPATH . '/locale/types-' . $locale . '.mo');
    if (!defined('WPV_VERSION')) {
        load_textdomain('wpv-views', WPCF_EMBEDDED_ABSPATH . '/locale/locale-views/views-' . $locale . '.mo');
    }
    // Define necessary constants if plugin is not present
    if (!defined('WPCF_VERSION')) {
        define('WPCF_VERSION', '1.0.4');
        define('WPCF_META_PREFIX', 'wpcf-');
        define('WPCF_EMBEDDED_RELPATH', icl_get_file_relpath(__FILE__));
    } else {
        define('WPCF_EMBEDDED_RELPATH', WPCF_RELPATH . '/embedded');
    }
    define('WPCF_EMBEDDED_INC_RELPATH', WPCF_EMBEDDED_RELPATH . '/includes');
    define('WPCF_EMBEDDED_RES_RELPATH', WPCF_EMBEDDED_RELPATH . '/resources');
    if (is_admin()) {
        require_once WPCF_EMBEDDED_ABSPATH . '/admin.php';
        wpcf_embedded_admin_init_hook();
    } else {
        require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
    }
    wpcf_init_custom_types_taxonomies();
    if (defined('DOING_AJAX')) {
        require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
    }
    wpcf_embedded_check_import();
}
Example #2
0
 /**
  * Compare checksums of types and import accordingly
  */
 public function sync()
 {
     $index = array();
     $configs = $this->getConfigs();
     $current = get_option('types-manager-index', array());
     $_POST['overwrite-groups'] = $_POST['overwrite-types'] = $_POST['overwrite-fields'] = $_POST['overwrite-tax'] = 1;
     $import = array();
     foreach ($configs as $export_type => $types) {
         if (empty($types['__key'])) {
             continue;
         }
         $__key = $types['__key'];
         foreach ($types as $id => $data) {
             if ($id == '__key') {
                 continue;
             }
             $index[$export_type][$id] = md5(serialize($data));
             if (!isset($current[$export_type][$id]) || $current[$export_type][$id] != $index[$export_type][$id]) {
                 // Import function checks for this
                 $_POST[$export_type][$id] = array('add' => true, 'update' => true);
                 $import[$export_type]['__key'] = $__key;
                 $import[$export_type][$id] = $data;
             }
         }
     }
     if (count($import)) {
         require_once WPCF_INC_ABSPATH . '/fields.php';
         require_once WPCF_INC_ABSPATH . '/import-export.php';
         $xml = self::toXml($import, 'types');
         foreach ($import as $export_type => $_) {
             wpcf_admin_import_data($xml, false, $export_type);
         }
         wpcf_init_custom_types_taxonomies();
         flush_rewrite_rules();
         update_option('types-manager-index', $index);
     }
     unset($_POST['overwrite-groups'], $_POST['overwrite-types'], $_POST['overwrite-fields'], $_POST['overwrite-tax']);
 }
/**
 * 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;
    }
}
Example #5
0
/**
 * Main init hook.
 *
 * All rest of init processes are continued here.
 * Sets locale, constants, includes...
 *
 * @todo Make sure plugin AND embedded code are calling this function on 'init'
 * @todo Test priorities
 */
function wpcf_embedded_init()
{
    global $types_instances, $wp_current_filter;
    // Record hook
    $types_instances['hook'] = $wp_current_filter;
    $types_instances['init_queued'] = '#' . did_action('init');
    $types_instances['init_priority'] = TYPES_INIT_PRIORITY;
    $types_instances['forced_embedded'] = defined('TYPES_LOAD_EMBEDDED') && TYPES_LOAD_EMBEDDED;
    // Loader
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/loader.php';
    do_action('wpcf_before_init');
    do_action('types_before_init');
    // Define necessary constants if plugin is not present
    // This ones are skipped if used as embedded code!
    if (!defined('WPCF_VERSION')) {
        define('WPCF_VERSION', '1.6.4');
        define('WPCF_META_PREFIX', 'wpcf-');
    }
    // If forced embedded mode use path to __FILE__
    if (defined('TYPES_LOAD_EMBEDDED') && TYPES_LOAD_EMBEDDED || !defined('WPCF_RELPATH')) {
        define('WPCF_EMBEDDED_RELPATH', wpcf_get_file_url(__FILE__, false));
    } else {
        define('WPCF_EMBEDDED_RELPATH', WPCF_RELPATH . '/embedded');
    }
    // Define embedded paths
    define('WPCF_EMBEDDED_INC_RELPATH', WPCF_EMBEDDED_RELPATH . '/includes');
    define('WPCF_EMBEDDED_RES_RELPATH', WPCF_EMBEDDED_RELPATH . '/resources');
    // TODO INCLUDES!
    //
    // Please add all required includes here
    // Since Types 1.2 we can consider existing code as core.
    // All new functionalities should be added as includes HERE
    // and marked with @since Types $version.
    //
    // Thanks!
    //
    // Basic
    /*
     *
     * Mind class extensions queue
     */
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/fields.php';
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/field.php';
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/usermeta_field.php';
    // Added by Gen, usermeta fields class
    // Repeater
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/repeater.php';
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/usermeta_repeater.php';
    // Added by Gen, usermeta repeater class
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/repetitive-fields-ordering.php';
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/repetitive-usermetafields-ordering.php';
    // Relationship
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/relationship.php';
    // Conditional
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/conditional.php';
    // API
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/api.php';
    // Validation
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/validation.php';
    // Post Types
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/class.wpcf-post-types.php';
    // Import Export
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/class.wpcf-import-export.php';
    // Module manager
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/module-manager.php';
    // WPML specific code
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/wpml.php';
    // CRED specific code.
    if (defined('CRED_FE_VERSION')) {
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/cred.php';
    }
    /*
     *
     *
     * TODO This is a must for now.
     * See if any fields need to be loaded.
     *
     * 1. Checkboxes - may be missing when submitted
     */
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/checkbox.php';
    /*
     *
     *
     * Use this call to load basic scripts and styles if necesary
     * wpcf_enqueue_scripts();
     */
    require_once WPCF_EMBEDDED_ABSPATH . '/usermeta-init.php';
    // Include frontend or admin code
    if (is_admin()) {
        require_once WPCF_EMBEDDED_ABSPATH . '/admin.php';
        /*
         * TODO Check if called twice
         *
         * Watch this! This is actually called twice everytime
         * in both modes (plugin or embedded)
         */
        wpcf_embedded_admin_init_hook();
    } else {
        require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
    }
    global $wpcf;
    // TODO since Types 1.2 Continue adding new functionalities HERE
    /*
     * Consider code already there as core.
     * Use hooks to add new functionalities
     *
     * Introduced new global object $wpcf
     * Holds useful objects like:
     * $wpcf->field - Field object (base item object)
     * $wpcf->repeater - Repetitive field object
     */
    // Set debugging
    if (!defined('WPCF_DEBUG')) {
        define('WPCF_DEBUG', false);
    } else {
        if (WPCF_DEBUG) {
            wp_enqueue_script('jquery');
        }
    }
    $wpcf->debug = new stdClass();
    require WPCF_EMBEDDED_INC_ABSPATH . '/debug.php';
    add_action('wp_footer', 'wpcf_debug', PHP_INT_MAX);
    add_action('admin_footer', 'wpcf_debug', PHP_INT_MAX);
    // Set field object
    $wpcf->field = new WPCF_Field();
    // Set fields object
    $wpcf->fields = new WPCF_Fields();
    // Set usermeta field object
    $wpcf->usermeta_field = new WPCF_Usermeta_Field();
    // Set repeater object
    $wpcf->usermeta_repeater = new WPCF_Usermeta_Repeater();
    // Set repeater object
    $wpcf->repeater = new WPCF_Repeater();
    // Set relationship object
    $wpcf->relationship = new WPCF_Relationship();
    // Set conditional object
    $wpcf->conditional = new WPCF_Conditional();
    // Set validate object
    $wpcf->validation = new WPCF_Validation();
    // Set import export objects
    $wpcf->import = new WPCF_Import_Export();
    $wpcf->export = new WPCF_Import_Export();
    // Set post object
    $wpcf->post = new stdClass();
    // Set post types object
    $wpcf->post_types = new WPCF_Post_Types();
    // Define exceptions - privileged plugins and their data
    $wpcf->toolset_post_types = array('view', 'view-template', 'cred-form');
    // 'attachment' = Media
    //
    $wpcf->excluded_post_types = array('revision', 'view', 'view-template', 'cred-form', 'nav_menu_item', 'mediapage');
    // Init loader
    WPCF_Loader::init();
    // Init custom types and taxonomies
    wpcf_init_custom_types_taxonomies();
    /*
     * TODO Check why we enabled this
     *
     * I think because of CRED or Views using Types admin functions on frontend
     * Does this need review?
     */
    if (defined('DOING_AJAX')) {
        require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
    }
    // Check if import/export request is going on
    wpcf_embedded_check_import();
    do_action('types_after_init');
    do_action('wpcf_after_init');
}
Example #6
0
/**
 * Main init hook.
 * 
 * All rest of init processes are continued here.
 * Sets locale, constants, includes...
 * 
 * @todo Make sure plugin AND embedded code are calling this function on 'init'
 * @todo Test priorities
 */
function wpcf_embedded_init()
{
    do_action('wpcf_before_init');
    // Set locale
    $locale = get_locale();
    load_textdomain('wpcf', WPCF_EMBEDDED_ABSPATH . '/locale/types-' . $locale . '.mo');
    if (!defined('WPV_VERSION')) {
        load_textdomain('wpv-views', WPCF_EMBEDDED_ABSPATH . '/locale/locale-views/views-' . $locale . '.mo');
    }
    // Define necessary constants if plugin is not present
    // This ones are skipped if used as embedded code!
    if (!defined('WPCF_VERSION')) {
        define('WPCF_VERSION', '1.2');
        define('WPCF_META_PREFIX', 'wpcf-');
        define('WPCF_EMBEDDED_RELPATH', icl_get_file_relpath(__FILE__));
    } else {
        // Otherwise if plugin code - just define embedded paths
        define('WPCF_EMBEDDED_RELPATH', WPCF_RELPATH . '/embedded');
    }
    // Define embedded paths
    define('WPCF_EMBEDDED_INC_RELPATH', WPCF_EMBEDDED_RELPATH . '/includes');
    define('WPCF_EMBEDDED_RES_RELPATH', WPCF_EMBEDDED_RELPATH . '/resources');
    // TODO INCLUDES!
    //
    // Please add all required includes here
    // Since Types 1.2 we can consider existing code as core.
    // All new functionalities should be added as includes HERE
    // and marked with @since Types $version.
    //
    // Thanks!
    //
    // Basic
    /*
     * 
     * Mind class extensions queue
     */
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/fields.php';
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/field.php';
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/class.wpcf-template.php';
    // Repeater
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/repeater.php';
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/repetitive-fields-ordering.php';
    // Relationship
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/relationship.php';
    // Conditional
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/conditional.php';
    // API
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/api.php';
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/api.php';
    // Validation
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/validation.php';
    // Post Types
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/class.wpcf-post-types.php';
    // Import Export
    require_once WPCF_EMBEDDED_ABSPATH . '/classes/class.wpcf-import-export.php';
    // Incubator
    require_once WPCF_EMBEDDED_ABSPATH . '/incubator/index.php';
    // Module manager
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/module-manager.php';
    // WPML specific code
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/wpml.php';
    /*
     * 
     * 
     * TODO This is a must for now.
     * See if any fields need to be loaded.
     */
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/checkbox.php';
    /*
     * 
     * 
     * Use this call to load basic scripts and styles if necesary
     * wpcf_enqueue_scripts();
     */
    // Include frontend or admin code
    if (is_admin()) {
        require_once WPCF_EMBEDDED_ABSPATH . '/admin.php';
        /*
         * TODO Check if called twice
         * 
         * Watch this! This is actually called twice everytime
         * in both modes (plugin or embedded)
         */
        wpcf_embedded_admin_init_hook();
    } else {
        require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
    }
    global $wpcf;
    // TODO since Types 1.2 Continue adding new functionalities HERE
    /*
     * Consider code already there as core.
     * Use hooks to add new functionalities
     * 
     * Introduced new global object $wpcf
     * Holds useful objects like:
     * $wpcf->field - Field object (base item object)
     * $wpcf->repeater - Repetitive field object
     */
    // Set field object
    $wpcf->field = new WPCF_Field();
    // Set fields object
    $wpcf->fields = new WPCF_Fields();
    // Set template object
    $wpcf->template = new WPCF_Template();
    // Set repeater object
    $wpcf->repeater = new WPCF_Repeater();
    // Set relationship object
    $wpcf->relationship = new WPCF_Relationship();
    // Set conditional object
    $wpcf->conditional = new WPCF_Conditional();
    // Set validate object
    $wpcf->validation = new WPCF_Validation();
    // Set import export objects
    $wpcf->import = new WPCF_Import_Export();
    $wpcf->export = new WPCF_Import_Export();
    // Set API object
    $wpcf->api = new WPCF_Api();
    // Set post object
    $wpcf->post = new stdClass();
    // Set post types object
    $wpcf->post_types = new WPCF_Post_Types();
    // Define exceptions - privileged plugins and their data
    $wpcf->toolset_post_types = array('view', 'view-template', 'cred-form');
    $wpcf->excluded_post_types = array('revision', 'view', 'view-template', 'cred-form', 'nav_menu_item', 'attachment', 'mediapage');
    // Init custom types and taxonomies
    wpcf_init_custom_types_taxonomies();
    /*
     * TODO Check why we enabled this
     * 
     * I think because of CRED or Views using Types admin functions on frontend
     * Does this need review?
     */
    if (defined('DOING_AJAX')) {
        require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
    }
    // Check if import/export request is going on
    wpcf_embedded_check_import();
    // Set debugging
    if (!defined('WPCF_DEBUG')) {
        define('WPCF_DEBUG', false);
    }
    if (WPCF_DEBUG) {
        $wpcf->debug = new stdClass();
        require WPCF_INC_ABSPATH . '/debug.php';
        add_action('wp_footer', 'wpcf_debug', 1.0E+32);
        add_action('admin_footer', 'wpcf_debug', 9.999999999999999E+28);
    }
    do_action('wpcf_after_init');
}
/**
 * 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;
    }
}