Пример #1
0
function wpcf_admin_general_settings_form_submit($form)
{
    $settings = wpcf_get_settings();
    $data = $_POST['wpcf_settings'];
    foreach (array('register_translations_on_import', 'help_box') as $setting) {
        if (!isset($data[$setting])) {
            $settings[$setting] = 0;
        } else {
            $settings[$setting] = $data[$setting];
        }
    }
    update_option('wpcf_settings', $settings);
    // Credits
    // TODO Remove
    //    require_once WPCF_EMBEDDED_INC_ABSPATH . '/footer-credit.php';
    //    $option = get_option('wpcf_footer_credit', array());
    //    if (!isset($option['message'])) {
    //        $data = wpcf_footer_credit_defaults();
    //        shuffle($data);
    //        $option['message'] = rand(0, count($data));
    //    }
    //    if (!isset($_POST['show_credits'])) {
    //        update_option('wpcf_footer_credit',
    //                array('active' => 0, 'message' => $option['message']));
    //    } else {
    //        update_option('wpcf_footer_credit',
    //                array('active' => 1, 'message' => $option['message']));
    //    }
    wpcf_admin_message_store(__('Settings saved', 'wpcf'));
}
Пример #2
0
/**
 * Saves settings.
 * 
 * @param type $form 
 */
function wpcf_admin_settings_form_submit($form)
{
    $settings = wpcf_get_settings();
    $data = $_POST['wpcf_settings'];
    foreach ($settings as $setting => $value) {
        if (!isset($data[$setting])) {
            $settings[$setting] = 0;
        } else {
            $settings[$setting] = $data[$setting];
        }
    }
    update_option('wpcf_settings', $settings);
    // Credits
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/footer-credit.php';
    $option = get_option('wpcf_footer_credit', array());
    if (!isset($option['message'])) {
        $data = wpcf_footer_credit_defaults();
        shuffle($data);
        $option['message'] = rand(0, count($data));
    }
    if (!isset($_POST['show_credits'])) {
        update_option('wpcf_footer_credit', array('active' => 0, 'message' => $option['message']));
    } else {
        update_option('wpcf_footer_credit', array('active' => 1, 'message' => $option['message']));
    }
    wpcf_admin_message_store(__('Settings saved', 'wpcf'));
}
Пример #3
0
/**
 * Saves settings.
 *
 * @param type $form
 */
function wpcf_admin_general_settings_form_submit($form)
{
    if (isset($_POST['clear-cache-images']) || isset($_POST['clear-cache-images-outdated'])) {
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/image.php';
        $cache_dir = wpcf_fields_image_get_cache_directory(true);
        if (is_wp_error($cache_dir)) {
            wpcf_admin_message_store($cache_dir->get_error_message());
        } else {
            if (isset($_POST['clear-cache-images'])) {
                wpcf_fields_image_clear_cache($cache_dir, 'all');
            } else {
                wpcf_fields_image_clear_cache($cache_dir);
            }
            wpcf_admin_message_store(__('Images cache cleared', 'wpcf'));
        }
        return true;
    }
    $settings = wpcf_get_settings();
    $data = $_POST['wpcf_settings'];
    $keys = array('add_resized_images_to_library' => 'esc_html', 'help_box' => 'esc_html', 'hide_standard_custom_fields_metabox' => 'esc_html', 'images_remote' => 'intval', 'images_remote_cache_time' => 'intval', 'register_translations_on_import' => 'esc_html', 'toolset_messages' => 'intval', 'postmeta_unfiltered_html' => 'on-off', 'usermeta_unfiltered_html' => 'on-off');
    foreach ($keys as $key => $validation) {
        if (!isset($data[$key])) {
            $settings[$key] = 0;
        } else {
            switch ($validation) {
                case 'intval':
                    $settings[$key] = intval($data[$key]);
                    break;
                case 'on-off':
                    if (preg_match('/^(on|off)$/', $data[$key])) {
                        $settings[$key] = $data[$key];
                    } else {
                        $settings[$key] = 'off';
                    }
                    break;
                case 'esc_html':
                default:
                    $settings[$key] = esc_html($data[$key]);
                    break;
            }
        }
    }
    /**
     * validate hide_standard_custom_fields_metabox
     */
    if (!preg_match('/^(show|hide)$/', $settings['hide_standard_custom_fields_metabox'])) {
        $settings['hide_standard_custom_fields_metabox'] = 'show';
    }
    /**
     * update_option
     */
    update_option('wpcf_settings', $settings);
    wpcf_admin_message_store(__('Settings saved.', 'wpcf'));
}
Пример #4
0
 public static function wpcf_sanitize_usermeta_values_on_save($value)
 {
     if (current_user_can('unfiltered_html') && wpcf_get_settings('usermeta_unfiltered_html') != 'off') {
         return $value;
     }
     if (is_array($value)) {
         // Recursion
         $value = array_map(array('WPCF_Loader', 'wpcf_sanitize_usermeta_values_on_save'), $value);
     } else {
         $value = wp_filter_post_kses($value);
     }
     return $value;
 }
 /**
  * Check if user is a client, who bought Toolset
  * @return bool
  */
 function wpcf_is_client()
 {
     // for testing
     if (!WPCF_PAYED) {
         return false;
     }
     // check db stored value
     if (get_option('wpcf-is-client')) {
         $settings = wpcf_get_settings('help_box');
         // prioritise settings if available
         if ($settings) {
             switch ($settings) {
                 case 'by_types':
                 case 'all':
                     return false;
                 case 'no':
                     return true;
             }
         }
         $is_client = get_option('wpcf-is-client');
         // client
         if ($is_client === 'yes') {
             return true;
         }
         // user
         return false;
     }
     // no db stored value
     // make sure get_plugins() is available
     if (!function_exists('get_plugins')) {
         require_once ABSPATH . 'wp-admin/includes/plugin.php';
     }
     // all plugins
     $plugins = get_plugins();
     // check each plugin
     foreach ($plugins as $plugin) {
         // skip plugin that is not created by us
         if ($plugin['Author'] != 'OnTheGoSystems') {
             continue;
         }
         // check for toolset plugin and not embedded = user bought toolset
         if (preg_match("#(access|cred|layouts|module manager|views)#i", $plugin['Name']) && !preg_match('#embedded#i', $plugin['Name'])) {
             add_option('wpcf-is-client', 'yes');
             // set settings "help box" ounce to none
             $settings = get_option('wpcf_settings', array());
             $settings['help_box'] = 'no';
             update_option('wpcf_settings', $settings);
             return true;
         }
     }
     // if script comes to this point we have no option "wpcf-is-client" set
     // and also no bought toolset plugin
     add_option('wpcf-is-client', 'no');
     return false;
 }
/**
 * Clears remote image cache.
 *
 * @param type $action
 */
function wpcf_fields_image_clear_cache($cache_dir = null, $action = 'outdated')
{
    if (is_null($cache_dir)) {
        $cache_dir = wpcf_fields_image_get_cache_directory();
    }
    $refresh_time = intval(wpcf_get_settings('images_remote_cache_time'));
    if ($refresh_time == 0 && $action != 'all') {
        return true;
    }
    foreach (glob($cache_dir . DIRECTORY_SEPARATOR . "*") as $filename) {
        if ($action == 'all') {
            @unlink($filename);
        } else {
            $time_modified = filemtime($filename);
            if (time() - $time_modified > $refresh_time * 60 * 60) {
                @unlink($filename);
                // Clear resized images
                $path = pathinfo($filename);
                foreach (glob($path['dirname'] . DIRECTORY_SEPARATOR . $path['filename'] . "-*") as $resized) {
                    @unlink($resized);
                }
            }
        }
    }
}
Пример #7
0
/**
 * Init functions for post edit pages.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @param type $post
 */
function wpcf_admin_post_init($post)
{
    add_action('admin_footer', 'wpcf_admin_fields_postfields_styles');
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_group', '\'' . wp_create_nonce('group_form_collapsed') . '\'');
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_fieldset', '\'' . wp_create_nonce('form_fieldset_toggle') . '\'');
    // Get post_type
    $post_type = wpcf_admin_get_edited_post_type($post);
    /*
     *
     * This is left to maintain compatibility with older versions
     * TODO Remove
     */
    // Add items to View dropdown
    if (in_array($post_type, array('view', 'view-template', 'cred-form'))) {
        add_filter('editor_addon_menus_wpv-views', 'wpcf_admin_post_editor_addon_menus_filter');
        add_action('admin_footer', 'wpcf_admin_post_js_validation');
        wpcf_enqueue_scripts();
        wp_enqueue_script('toolset-colorbox');
        wp_enqueue_style('toolset-colorbox');
    }
    // Never show on 'Views' and 'View Templates'
    if (in_array($post_type, array('view', 'view-template'))) {
        return false;
    }
    // Add marketing box
    if (!in_array($post_type, array('post', 'page', 'cred-form')) && !defined('WPCF_RUNNING_EMBEDDED')) {
        $hide_help_box = true;
        $help_box = wpcf_get_settings('help_box');
        $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
        if ($help_box != 'no') {
            if ($help_box == 'by_types' && array_key_exists($post_type, $custom_types)) {
                $hide_help_box = false;
            }
            if (function_exists('wprc_is_logged_to_repo') && wprc_is_logged_to_repo(WPCF_REPOSITORY)) {
                $hide_help_box = true;
            }
            if ($help_box == 'all') {
                $hide_help_box = false;
            }
            if (!$hide_help_box && !defined('WPV_VERSION')) {
                add_meta_box('wpcf-marketing', __('Display Custom Content', 'wpcf'), 'wpcf_admin_post_marketing_meta_box', $post_type, 'side', 'high');
            }
        }
    }
    // Are Types active?
    $wpcf_active = false;
    // Get groups
    $groups = wpcf_admin_post_get_post_groups_fields($post);
    foreach ($groups as $group) {
        $only_preview = '';
        //If Access plugin activated
        if (function_exists('wpcf_access_register_caps')) {
            //If user can't view own profile fields
            if (!current_user_can('view_fields_in_edit_page_' . $group['slug'])) {
                continue;
            }
            //If user can modify current group in own profile
            if (!current_user_can('modify_fields_in_edit_page_' . $group['slug'])) {
                $only_preview = 1;
            }
        }
        if (!empty($group['fields']) && empty($only_preview)) {
            $wpcf_active = true;
            break;
        }
    }
    // Activate scripts
    if ($wpcf_active) {
        add_action('admin_head', 'wpcf_post_preview_warning');
        wpcf_edit_post_screen_scripts();
    }
    // Add validation
    add_action('admin_footer', 'wpcf_admin_post_js_validation');
    /*
     * TODO Review
     * This is forced because of various Child cases
     * and when field are rendered via AJAX but not registered yet.
     *
     * Basically all fields that require additional JS should be added here.
     *
     * This is a must for now.
     * These fields need init JS in various cases.
     */
    wpcf_field_enqueue_scripts('date');
    wpcf_field_enqueue_scripts('image');
    wpcf_field_enqueue_scripts('file');
    wpcf_field_enqueue_scripts('skype');
    wpcf_field_enqueue_scripts('numeric');
    do_action('wpcf_admin_post_init', $post_type, $post, $groups, $wpcf_active);
}
Пример #8
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', $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;
}
/**
 * add types configuration to debug
 */
function wpcf_get_extra_debug_info($extra_debug)
{
    $extra_debug['types'] = wpcf_get_settings();
    return $extra_debug;
}
Пример #10
0
/**
 * Init functions for post edit pages.
 * 
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 * 
 * @param type $upgrade 
 */
function wpcf_admin_post_init($post = false)
{
    global $wpcf;
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_group', '\'' . wp_create_nonce('group_form_collapsed') . '\'');
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_fieldset', '\'' . wp_create_nonce('form_fieldset_toggle') . '\'');
    // Get post_type
    if ($post) {
        $post_type = get_post_type($post);
    } else {
        if (!isset($_GET['post_type'])) {
            $post_type = 'post';
        } else {
            if (in_array($_GET['post_type'], get_post_types(array('show_ui' => true)))) {
                $post_type = $_GET['post_type'];
            } else {
                return false;
            }
        }
    }
    // Set global $wpcf post object
    // CHECKPOINT
    $wpcf->post = $post;
    $wpcf->post_types->set($post_type);
    // Add items to View dropdown
    if (in_array($post_type, array('view', 'view-template'))) {
        add_filter('editor_addon_menus_wpv-views', 'wpcf_admin_post_editor_addon_menus_filter');
        add_action('admin_footer', 'wpcf_admin_post_js_validation');
        wpcf_enqueue_scripts();
    }
    // Never show on 'Views' and 'View Templates'
    // CRED should pass this check
    if (in_array($post_type, array('view', 'view-template'))) {
        return false;
    }
    // Add marketing box
    if (!in_array($post_type, array('post', 'page', 'cred-form')) && !defined('WPCF_RUNNING_EMBEDDED')) {
        $hide_help_box = true;
        $help_box = wpcf_get_settings('help_box');
        $custom_types = get_option('wpcf-custom-types', array());
        if ($help_box != 'no') {
            if ($help_box == 'by_types' && array_key_exists($post_type, $custom_types)) {
                $hide_help_box = false;
            }
            if (function_exists('wprc_is_logged_to_repo') && wprc_is_logged_to_repo(WPCF_REPOSITORY)) {
                $hide_help_box = true;
            }
            if ($help_box == 'all') {
                $hide_help_box = false;
            }
            if (!$hide_help_box) {
                add_meta_box('wpcf-marketing', __('Display Custom Content', 'wpcf'), 'wpcf_admin_post_marketing_meta_box', $post_type, 'side', 'high');
            }
        }
    }
    // Are Types active?
    $wpcf_active = false;
    // Get groups
    $groups = wpcf_admin_post_get_post_groups_fields($post);
    foreach ($groups as $group) {
        if (!empty($group['fields'])) {
            $wpcf_active = true;
            // Process fields
            $group['fields'] = wpcf_admin_post_process_fields($post, $group['fields'], true);
        }
        // Specially for CRED
        /*
         * 
         * TODO Setting some specific for CRED is wrong
         * Use hooks
         */
        if (!in_array($post_type, array('cred-form'))) {
            // Add meta boxes
            add_meta_box($group['slug'], wpcf_translate('group ' . $group['id'] . ' name', $group['name']), 'wpcf_admin_post_meta_box', $post_type, $group['meta_box_context'], 'high', $group);
        }
    }
    // Activate scripts
    if ($wpcf_active) {
        wp_enqueue_script('wpcf-fields-post', WPCF_EMBEDDED_RES_RELPATH . '/js/fields-post.js', array('jquery'), WPCF_VERSION);
        wp_enqueue_script('wpcf-form-validation', WPCF_EMBEDDED_RES_RELPATH . '/js/' . 'jquery-form-validation/jquery.validate.min.js', array('jquery'), WPCF_VERSION);
        wp_enqueue_script('wpcf-form-validation-additional', WPCF_EMBEDDED_RES_RELPATH . '/js/' . 'jquery-form-validation/additional-methods.min.js', array('jquery'), WPCF_VERSION);
        wp_enqueue_style('wpcf-fields-basic', WPCF_EMBEDDED_RES_RELPATH . '/css/basic.css', array(), WPCF_VERSION);
        wp_enqueue_style('wpcf-fields-post', WPCF_EMBEDDED_RES_RELPATH . '/css/fields-post.css', array('wpcf-fields-basic'), WPCF_VERSION);
        wpcf_enqueue_scripts();
    }
    // Add validation
    // TODO Move to wpcf_enqueue_scripts()
    add_action('admin_footer', 'wpcf_admin_post_js_validation');
    do_action('wpcf_admin_post_init', $post_type, $post, $groups, $wpcf_active);
}
Пример #11
0
/**
 * 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;
    }
}
 public function update_toolset_messages()
 {
     $settings = wpcf_get_settings();
     if (array_key_exists('value', $_POST) && 'checked' == $_POST['value']) {
         if (!add_option($this->option_disable, '1', '', 'no')) {
             update_option($this->option_disable, '1');
         }
         $settings['toolset_messages'] = true;
     } else {
         delete_option($this->option_disable);
         $settings['toolset_messages'] = false;
     }
     update_option('wpcf_settings', $settings);
     echo '<div class="updated"><p>';
     _e('Toolset Messages state saved!', 'wpcf');
     echo '</p></div>';
     die;
 }
/**
 * Init functions for post edit pages.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @param type $post
 */
function wpcf_admin_post_init($post)
{
    add_action('admin_footer', 'wpcf_admin_fields_postfields_styles');
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_group', '\'' . wp_create_nonce('group_form_collapsed') . '\'');
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_fieldset', '\'' . wp_create_nonce('form_fieldset_toggle') . '\'');
    // Get post_type
    $post_type = wpcf_admin_get_edited_post_type($post);
    /*
     *
     * This is left to maintain compatibility with older versions
     * TODO Remove
     */
    // Add items to View dropdown
    if (in_array($post_type, array('view', 'view-template', 'cred-form', 'cred-user-form'))) {
        add_filter('editor_addon_menus_wpv-views', 'wpcf_admin_post_editor_addon_menus_filter');
        add_action('admin_footer', 'wpcf_admin_post_js_validation');
        wpcf_enqueue_scripts();
        wp_enqueue_script('toolset-colorbox');
        wp_enqueue_style('toolset-colorbox');
    }
    // Never show on 'Views' and 'Content Templates'
    if (in_array($post_type, array('view', 'view-template'))) {
        return false;
    }
    /**
     * remove custom field WordPress metabox
     */
    if ('hide' == wpcf_get_settings('hide_standard_custom_fields_metabox')) {
        foreach (array('normal', 'advanced', 'side') as $context) {
            remove_meta_box('postcustom', $post_type, $context);
        }
    }
    // Add marketing box
    if (!wpcf_is_client() && !in_array($post_type, array('post', 'page', 'cred-form', 'cred-user-form')) && !defined('WPCF_RUNNING_EMBEDDED')) {
        $settings_help_box = wpcf_get_settings('help_box');
        $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
        if ($settings_help_box == 'all' || array_key_exists($post_type, $custom_types)) {
            $displaying_custom_content = (include WPCF_ABSPATH . '/marketing/displaying-custom-content/title-content.php');
            add_meta_box('add_box_howto', $displaying_custom_content['title'], 'wpcf_admin_post_marketing_displaying_custom_content', $post_type, 'side', 'high');
        }
    }
    // Are Types active?
    $wpcf_active = false;
    // Get groups
    $groups = wpcf_admin_post_get_post_groups_fields($post);
    foreach ($groups as $group) {
        $only_preview = '';
        //If Access plugin activated
        if (function_exists('wpcf_access_register_caps')) {
            //If user can't view own profile fields
            if (!current_user_can('view_fields_in_edit_page_' . $group['slug'])) {
                continue;
            }
            //If user can modify current group in own profile
            if (!current_user_can('modify_fields_in_edit_page_' . $group['slug'])) {
                $only_preview = 1;
            }
        }
        if (!empty($group['fields']) && empty($only_preview)) {
            $wpcf_active = true;
            break;
        }
    }
    // Activate scripts
    if ($wpcf_active) {
        add_action('admin_head', 'wpcf_post_preview_warning');
        wpcf_edit_post_screen_scripts();
    }
    // Add validation
    add_action('admin_footer', 'wpcf_admin_post_js_validation');
    /*
     * TODO Review
     * This is forced because of various Child cases
     * and when field are rendered via AJAX but not registered yet.
     *
     * Basically all fields that require additional JS should be added here.
     *
     * This is a must for now.
     * These fields need init JS in various cases.
     */
    wpcf_field_enqueue_scripts('date');
    wpcf_field_enqueue_scripts('image');
    wpcf_field_enqueue_scripts('file');
    wpcf_field_enqueue_scripts('skype');
    wpcf_field_enqueue_scripts('numeric');
    do_action('wpcf_admin_post_init', $post_type, $post, $groups, $wpcf_active);
}
Пример #14
0
function wpcf_admin_general_settings_form_submit($form)
{
    $settings = wpcf_get_settings();
    $data = $_POST['wpcf_settings'];
    foreach (array('register_translations_on_import', 'help_box') as $setting) {
        if (!isset($data[$setting])) {
            $settings[$setting] = 0;
        } else {
            // @todo Add sanitization here
            $settings[$setting] = $data[$setting];
        }
    }
    update_option('wpcf_settings', $settings);
    wpcf_admin_message_store(__('Settings saved', 'wpcf'));
}
Пример #15
0
/**
 * View function.
 * 
 * @param type $params 
 */
function wpcf_fields_image_view($params)
{
    $output = '';
    $alt = false;
    $title = false;
    $class = array();
    // Get image data
    $image_data = wpcf_fields_image_get_data($params['field_value']);
    // Display error to admin only
    if (!empty($image_data['error'])) {
        if (current_user_can('administrator')) {
            return '<div style="padding:10px;background-color:Red;color:#FFFFFF;">' . 'Types: ' . $image_data['error'] . '</div>';
        }
        return $params['field_value'];
    }
    //if (!$image_data['is_outsider']) {
    //    if (current_user_can('administrator')) {
    //        return '<div style="padding:10px;background-color:Red;color:#FFFFFF;">'
    //                . 'Types: ' . sprintf(__('Directory %s not writable', 'wpcf'),
    //                        $image_data['abspath']) . '</div>';
    //    }
    //    return $params['field_value'];
    //}
    // Set alt
    if (isset($params['alt'])) {
        $alt = $params['alt'];
    }
    // Set title
    if (isset($params['title'])) {
        $title = $params['title'];
    }
    // Set attachment class
    if (!empty($params['size'])) {
        $class[] = 'attachment-' . $params['size'];
    }
    // Set align class
    if (!empty($params['align']) && $params['align'] != 'none') {
        $class[] = 'align' . $params['align'];
    }
    // Pre-configured size (use WP function)
    if ($image_data['is_attachment'] && !empty($params['size'])) {
        $output = wp_get_attachment_image($image_data['is_attachment'], $params['size'], false, array('class' => implode(' ', $class), 'alt' => $alt, 'title' => $title));
    } else {
        // Custom size
        $width = !empty($params['width']) ? intval($params['width']) : null;
        $height = !empty($params['height']) ? intval($params['height']) : null;
        $crop = !empty($params['proportional']) && $params['proportional'] == 'true' ? false : true;
        // Check if image is outsider
        if (!$image_data['is_outsider']) {
            $resized_image = wpcf_fields_image_resize_image($params['field_value'], $width, $height, 'relpath', false, $crop);
            if (!$resized_image) {
                $resized_image = $params['field_value'];
            } else {
                // Add to library
                $image_abspath = wpcf_fields_image_resize_image($params['field_value'], $width, $height, 'abspath', false, $crop);
                $add_to_library = wpcf_get_settings('add_resized_images_to_library');
                if ($add_to_library) {
                    global $wpdb;
                    $attachment_exists = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\r\r\n    WHERE post_type = 'attachment' AND guid=%s", $resized_image));
                    if (empty($attachment_exists)) {
                        // Add as attachment
                        $wp_filetype = wp_check_filetype(basename($image_abspath), null);
                        $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($image_abspath)), 'post_content' => '', 'post_status' => 'inherit', 'guid' => $resized_image);
                        global $post;
                        $attach_id = wp_insert_attachment($attachment, $image_abspath, $post->ID);
                        // you must first include the image.php file
                        // for the function wp_generate_attachment_metadata() to work
                        require_once ABSPATH . "wp-admin" . '/includes/image.php';
                        $attach_data = wp_generate_attachment_metadata($attach_id, $image_abspath);
                        wp_update_attachment_metadata($attach_id, $attach_data);
                    }
                }
            }
        } else {
            $resized_image = $params['field_value'];
        }
        $output = '<img alt="';
        $output .= $alt !== false ? $alt : $resized_image;
        $output .= '" title="';
        $output .= $title !== false ? $title : $resized_image;
        $output .= '"';
        $output .= !empty($params['onload']) ? ' onload="' . $params['onload'] . '"' : '';
        $output .= !empty($class) ? ' class="' . implode(' ', $class) . '"' : '';
        $output .= ' src="' . $resized_image . '" />';
    }
    return $output;
}
Пример #16
0
/**
 * 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;
    }
}