/**
  * Sanitize single checkboxes option definition.
  *
  * @param array $option
  * @return array Sanitized option.
  * @since 2.1
  */
 private function sanitize_single_option($option)
 {
     $option = $this->sanitize_element_isset(wpcf_ensarr($option), 'value');
     $option = $this->sanitize_element_isset($option, 'title', $option['value']);
     $option = $this->sanitize_element_isset($option, 'display_value', $option['value']);
     return $option;
 }
 /**
  * Sanitize single checkboxes option definition.
  * 
  * @param array $option
  * @return array Sanitized option.
  * @since 2.1
  */
 private function sanitize_single_option($option)
 {
     $option = $this->sanitize_element_isset(wpcf_ensarr($option), 'set_value');
     $option = $this->sanitize_element_isset($option, 'title', $option['set_value']);
     $option = $this->sanitize_element_isset($option, 'display', 'db', array('db', 'value'));
     $option = $this->sanitize_element_isset($option, 'display_value_selected');
     $option = $this->sanitize_element_isset($option, 'display_value_not_selected');
     return $option;
 }
Beispiel #3
0
 /**
  * Get taxonomies that are associated with this field group.
  *
  * @return string[] Taxonomy slugs. Empty array means that this group should be displayed with all taxonomies.
  */
 public function get_associated_taxonomies()
 {
     $postmeta = get_post_meta($this->get_id(), self::POSTMETA_ASSOCIATED_TAXONOMY, false);
     $postmeta = array_filter($postmeta, function ($value) {
         $value = trim($value);
         return !empty($value);
     });
     return wpcf_ensarr($postmeta);
 }
Beispiel #4
0
 /**
  * Add the 'number' validation if it was not already there, and activate it.
  * 
  * @param array $definition_array
  * @return array
  * @since 2.0
  */
 protected function sanitize_numeric_validation($definition_array)
 {
     // Get the original setting or a default one.
     $validation_setting = wpcf_ensarr(wpcf_getnest($definition_array, array('data', 'validate', 'number')), array('active' => true, 'message' => __('Please enter numeric data', 'wpcf')));
     // Force the activation of this validation.
     $validation_setting['active'] = true;
     // Store the setting.
     $definition_array['data']['validate']['number'] = $validation_setting;
     return $definition_array;
 }
 /**
  * @param Types_Field_Type_Definition $type
  * @return Types_Field_Type_Definition[]
  */
 public function get_possible_conversions($type)
 {
     if (!$type instanceof Types_Field_Type_Definition) {
         throw new InvalidArgumentException('Not a field type definition');
     }
     $matrix = $this->get_conversion_matrix();
     $allowed_slugs = wpcf_ensarr(wpcf_getarr($matrix, $type->get_slug()));
     $allowed_types = Types_Field_Type_Definition_Factory::get_instance()->load_multiple_definitions($allowed_slugs);
     return $allowed_types;
 }
Beispiel #6
0
 /**
  * Get taxonomies that are associated with this field group.
  *
  * @return string[] Taxonomy slugs. Empty array means that this group should be displayed with all taxonomies.
  */
 public function get_associated_taxonomies()
 {
     $postmeta = get_post_meta($this->get_id(), self::POSTMETA_ASSOCIATED_TAXONOMY, false);
     // Survive empty or whitespace taxonomy slugs (skip them). They are invalid values but
     // if we have only them, we need to return an empty array to keep the group displayed everywhere.
     foreach ($postmeta as $index => $taxonomy_slug) {
         $taxonomy_slug = trim($taxonomy_slug);
         if (empty($taxonomy_slug)) {
             unset($postmeta[$index]);
         }
     }
     return wpcf_ensarr($postmeta);
 }
Beispiel #7
0
 /**
  * Read the field values from $_POST.
  *
  * @return array Values in the "intermediate" format (see WPCF_Field_DataMapper_Abstract). For non-repetitive values,
  *     it will be an array with a single item.
  */
 private function read_field_values()
 {
     if (null == $this->field_values) {
         $definition = $this->field->get_definition();
         $form_data = wpcf_ensarr(wpcf_getpost('wpcf'));
         $values = wpcf_getarr($form_data, $definition->get_slug());
         // Handle single fields.
         if (!$definition->get_is_repetitive()) {
             $values = array($values);
         }
         // Map POST values to intermediate format.
         $this->field_values = array();
         $data_mapper = $definition->get_data_mapper();
         foreach ($values as $value) {
             $this->field_values[] = $data_mapper->post_to_intermediate($value, $form_data);
         }
     }
     return wpcf_ensarr($this->field_values);
 }
Beispiel #8
0
 /**
  * Retrieve an array of option definitions.
  * 
  * Allowed only for the checkboxes, radio and select field types.
  * 
  * @throws RuntimeException when the field type is invalid
  * @throws InvalidArgumentException when option definitions are corrupted
  * @return WPCF_Field_Option_Checkboxes[] An option_id => option_data array.
  * @since 1.9
  */
 public function get_field_options()
 {
     $this->check_allowed_types(array(Types_Field_Type_Definition_Factory::CHECKBOXES, Types_Field_Type_Definition_Factory::RADIO, Types_Field_Type_Definition_Factory::SELECT));
     $options_definition = wpcf_ensarr(wpcf_getnest($this->definition_array, array('data', 'options')));
     $results = array();
     // The 'default' key can be present, we have to remove it so it's not handled as another option.
     $has_default = array_key_exists('default', $options_definition);
     $default = wpcf_getarr($options_definition, 'default', 'no-default');
     if ($has_default) {
         unset($options_definition['default']);
     }
     foreach ($options_definition as $option_id => $option_config) {
         try {
             switch ($this->get_type()->get_slug()) {
                 case Types_Field_Type_Definition_Factory::RADIO:
                     $option = new WPCF_Field_Option_Radio($option_id, $option_config, $default, $this);
                     break;
                 case Types_Field_Type_Definition_Factory::SELECT:
                     $option = new WPCF_Field_Option_Select($option_id, $option_config, $default, $this);
                     break;
                 case Types_Field_Type_Definition_Factory::CHECKBOXES:
                     $option = new WPCF_Field_Option_Checkboxes($option_id, $option_config, $default);
                     break;
                 default:
                     throw new InvalidArgumentException('Invalid field type');
             }
             $results[$option_id] = $option;
         } catch (Exception $e) {
             // Corrupted data, can't do anything but skip the option.
         }
     }
     return $results;
 }
/**
 * Filter the data to be exported for custom taxonomies.
 *
 * Ensure the settings of post types associated with the taxonomy is exported correctly, even with support of legacy
 * settings.
 *
 * @param array $taxonomy_data
 * @return array Modified taxonomy data.
 * @since unknown
 */
function wpcf_fix_exported_taxonomy_assignment_to_cpt($taxonomy_data = array())
{
    $setting_name_prefix = '__types_cpt_supports_';
    $post_type_support_settings = array();
    // Associated CPTs slugs are stored as XML keys, so they can not start with a number.
    // We force a prefix on all of them on export, and restore them on import.
    $supported_post_types = wpcf_ensarr(wpcf_getarr($taxonomy_data, 'supports'));
    foreach ($supported_post_types as $post_type_slug => $is_supported) {
        $setting_name = $setting_name_prefix . $post_type_slug;
        $post_type_support_settings[$setting_name] = $is_supported ? 1 : 0;
    }
    // Here, we will also process the legacy "object_type" setting, containing supported post type slugs as array items,
    // in the samve way.
    $legacy_supported_post_type_array = wpcf_ensarr(wpcf_getarr($taxonomy_data, 'object_type'));
    foreach ($legacy_supported_post_type_array as $post_type_slug) {
        $setting_name = $setting_name_prefix . $post_type_slug;
        $post_type_support_settings[$setting_name] = 1;
    }
    // Now we need to remove this legacy setting to prevent producing invalid XML.
    unset($taxonomy_data['object_type']);
    $taxonomy_data['supports'] = $post_type_support_settings;
    return $taxonomy_data;
}
Beispiel #10
0
 /**
  * Change which groups is a field definition associated with.
  *
  * AJAX callback helper only, do not use elsewhere.
  *
  * @param string $field_slug Field definition slug.
  * @param string $domain Field domain
  * @param string[][] $groups Action-specific data passed through AJAX. Array containing a single key 'group_slugs',
  *     containing an array of field group slugs.
  *
  * @return WP_Error|WPCF_Field_Definition The updated field definition on success or an error object.
  * @since 2.0
  */
 public static function change_assignment_to_groups($field_slug, $domain, $groups)
 {
     $factory = Types_Field_Utils::get_definition_factory_by_domain($domain);
     $definition = $factory->load_field_definition($field_slug);
     if (null == $definition) {
         return new WP_Error(42, sprintf(__('Field definition for field "%s" not found in options.', 'wpcf'), sanitize_text_field($field_slug)));
     }
     $new_groups = wpcf_ensarr(wpcf_getarr($groups, 'group_slugs'));
     $associated_groups = $definition->get_associated_groups();
     $is_success = true;
     foreach ($associated_groups as $group) {
         if (!in_array($group->get_slug(), $new_groups)) {
             $is_success = $is_success && $group->remove_field_definition($definition);
         }
     }
     $group_factory = $factory->get_group_factory();
     foreach ($new_groups as $new_group_slug) {
         $new_group = $group_factory->load_field_group($new_group_slug);
         if (null != $new_group) {
             $is_success = $is_success && $new_group->add_field_definition($definition);
         } else {
             $is_success = false;
         }
     }
     if ($is_success) {
         return $definition;
     } else {
         return new WP_Error();
     }
 }
 /**
  * Get field type definitions from an array of slugs.
  *
  * If a definition cannot be loaded for a given slug, the slug is skipped without reporting an error in any other way.
  *
  * @param string[] $field_type_slugs
  * @return Types_Field_Type_Definition[]
  * @since 2.0
  */
 public function load_multiple_definitions($field_type_slugs)
 {
     $results = array();
     $field_type_slugs = wpcf_ensarr($field_type_slugs);
     foreach ($field_type_slugs as $field_type_slug) {
         $type_definition = $this->load_field_type_definition($field_type_slug);
         if (null != $type_definition) {
             $results[$field_type_slug] = $type_definition;
         }
     }
     return $results;
 }
Beispiel #12
0
 /**
  * Get taxonomies that are associated with this field group.
  *
  * @return string[] Taxonomy slugs. Empty array means that this group should be displayed with all taxonomies.
  */
 public function get_associated_taxonomies()
 {
     $postmeta = get_post_meta($this->get_id(), self::POSTMETA_ASSOCIATED_TAXONOMY, false);
     return wpcf_ensarr($postmeta);
 }
 /**
  * Make sure that the field definition array contains all necessary information.
  * 
  * Note: This is a WIP, currently it sanitizes only very specific cases. It should be extended in the future.
  *
  * @link https://git.onthegosystems.com/toolset/types/wikis/database-layer/field-definition-arrays
  * @param array $definition_array Field definition array
  * @return array Field definition array that is safe to be used even with legacy code.
  * @since 2.0
  */
 public final function sanitize_field_definition_array($definition_array)
 {
     /**
      * types_pre_sanitize_field_definition_array
      *
      * Allow for additional field definition array sanitization before the standard one runs.
      *
      * @param mixed $definition_array
      * @return array
      * @since 2.1
      */
     $definition_array = wpcf_ensarr(apply_filters('types_pre_sanitize_field_definition_array', $definition_array));
     $definition_array = $this->sanitize_field_definition_array_generic($definition_array);
     $definition_array = $this->sanitize_numeric_validation($definition_array);
     $definition_array = $this->sanitize_field_definition_array_type_specific($definition_array);
     /**
      * types_post_sanitize_field_definition_array
      *
      * Allow for additional field definition array sanitization after the standard one runs.
      *
      * @param array $definition_array
      * @return array
      * @since 2.1
      */
     $definition_array = wpcf_ensarr(apply_filters('types_post_sanitize_field_definition_array', $definition_array));
     return $definition_array;
 }
Beispiel #14
0
 /**
  * Make sure that the field definition array contains all necessary information.
  * 
  * Note: This is a WIP, currently it sanitizes only very specific cases. It should be extended in the future.
  *
  * Expected definition array structure common to all fields:
  *
  * - slug: string
  * - data: array
  *     - validate: array 
  * 
  * @param array $definition_array Field definition array
  * @return array Field definition array that is safe to be used even with legacy code.
  * @since 2.0
  */
 public function sanitize_field_definition_array($definition_array)
 {
     $definition_array['data'] = wpcf_ensarr(wpcf_getarr($definition_array, 'data'));
     $definition_array['data']['validate'] = wpcf_ensarr(wpcf_getarr($definition_array['data'], 'validate'));
     $definition_array = $this->sanitize_numeric_validation($definition_array);
     return $definition_array;
 }
 /**
  * @return array An option_id => option_data array.
  */
 public function get_field_options()
 {
     return wpcf_ensarr(wpcf_getnest($this->definition_array, array('data', 'options')));
 }
Beispiel #16
0
 private function ajax_filter_default_value($value, $currently_supported = array(), $type = false)
 {
     if ($type && isset($_REQUEST['all_fields']) && is_array($_REQUEST['all_fields'])) {
         switch ($type) {
             case 'taxonomies-for-termmeta':
                 $selected_taxonomies = wpcf_ensarr(wpcf_getnest($_REQUEST, array('all_fields', 'wpcf', 'group', 'taxonomies')));
                 if (in_array($value, array_keys($selected_taxonomies)) && true == $selected_taxonomies[$value]) {
                     return true;
                 }
                 break;
         }
         // not selected
         return false;
     }
     if (isset($_REQUEST['current'])) {
         if (is_array($_REQUEST['current']) && in_array($value, $_REQUEST['current'])) {
             return true;
         }
     } else {
         if ($currently_supported && !empty($currently_supported) && in_array($value, $currently_supported)) {
             return true;
         }
     }
     return false;
 }
/**
 * Imports data from XML.
 *
 * @param string $data
 * @param bool $redirect
 * @param string $context
 * @param array $args
 *
 * @return array
 */
function wpcf_admin_import_data($data = '', $redirect = true, $context = 'types', $args = array())
{
    global $wpdb;
    $data_installer = false;
    $return = array();
    libxml_use_internal_errors(true);
    $data = simplexml_load_string($data);
    if (!$data) {
        $return[] = array('type' => 'error', 'content' => __('Error parsing XML: ', 'wpcf') . $error->message);
        libxml_clear_errors();
        return $return;
    }
    $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);
        $return[] = array('type' => 'success', 'content' => __('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) {
                            $return[] = array('type' => 'error', 'content' => sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']));
                        } else {
                            $return[] = array('type' => 'success', 'content' => sprintf(__('Group "%s" updated', 'wpcf'), $group['post_title']));
                        }
                    } else {
                        $return[] = array('type' => 'error', 'content' => 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)) {
                        $return[] = array('type' => 'error', 'content' => sprintf(__('Group "%s" insert failed', 'wpcf'), $group['post_title']));
                    } else {
                        $return[] = array('type' => 'success', 'content' => 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;
                                        $return[] = array('type' => 'error', 'content' => __('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'));
                                    }
                                }
                            }
                        }
                        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) {
                        $return[] = array('type' => 'error', 'content' => sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete->post_title));
                    } else {
                        $return[] = array('type' => 'success', 'content' => 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) {
                        $return[] = array('type' => 'error', 'content' => sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title));
                    } else {
                        $return[] = array('type' => 'success', 'content' => sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title));
                    }
                } else {
                    $return[] = array('type' => 'error', 'content' => sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete));
                }
            }
        }
    }
    // 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();
            }
            $return[] = array('type' => 'success', 'content' => 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)) {
                $return[] = array('type' => 'success', 'content' => 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) {
                $return[] = array('type' => 'success', 'content' => 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) {
                            $return[] = array('type' => 'error', 'content' => sprintf(__('User group "%s" update failed', 'wpcf'), $group['post_title']));
                        } else {
                            /*
                                                        wpcf_admin_message_store( sprintf( __( 'User group "%s" updated',
                                                                                'wpcf' ),
                                                                        $group['post_title'] ) );
                            */
                            $return[] = array('type' => 'success', 'content' => sprintf(__('User group "%s" updated', 'wpcf'), $group['post_title']));
                        }
                    } else {
                        $return[] = array('type' => 'error', 'content' => sprintf(__('User 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)) {
                        $return[] = array('type' => 'error', 'content' => sprintf(__('User group "%s" insert failed', 'wpcf'), $group['post_title']));
                    } else {
                        $return[] = array('type' => 'success', 'content' => 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) {
                        $return[] = array('type' => 'error', 'content' => sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete->post_title));
                    } else {
                        $return[] = array('type' => 'success', 'content' => 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) {
                        $return[] = array('type' => 'error', 'content' => sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title));
                    } else {
                        $return[] = array('type' => 'success', 'content' => sprintf(__('User group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title));
                    }
                } else {
                    $return[] = array('type' => 'error', 'content' => sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete));
                }
            }
        }
    }
    // 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'];
            if (isset($field['meta_key'])) {
                $field_data['meta_key'] = $field['meta_key'];
            }
            $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();
            }
            $return[] = array('type' => 'success', 'content' => 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)) {
                $return[] = array('type' => 'success', 'content' => 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) {
                $return[] = array('type' => 'success', 'content' => sprintf(__('User field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name']));
                unset($fields_existing[$field_to_delete]);
            }
        }
    }
    update_option('wpcf-usermeta', $fields_existing);
    // Handle term field groups and field definitions outside of this mess.
    $ie_controller = Types_Import_Export::get_instance();
    $term_group_results = $ie_controller->process_field_group_import_per_domain(Types_Field_Utils::DOMAIN_TERMS, $data, 'term_groups', $overwrite_groups, $delete_groups, wpcf_ensarr(wpcf_getpost('term_groups')));
    $term_field_results = $ie_controller->process_field_definition_import_per_domain(Types_Field_Utils::DOMAIN_TERMS, $data, 'term_fields', $delete_fields, wpcf_ensarr(wpcf_getpost('term_fields')));
    $return = array_merge($return, $term_group_results, $term_field_results);
    // 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;
            $return[] = array('type' => 'success', 'content' => sprintf(__('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]);
                $return[] = array('type' => 'success', 'content' => sprintf(__('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) {
                $return[] = array('type' => 'success', 'content' => sprintf(__('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);
            $taxonomy = apply_filters('wpcf_filter_import_custom_taxonomy', $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;
            $return[] = array('type' => 'success', 'content' => sprintf(__('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]);
                $return[] = array('type' => 'success', 'content' => sprintf(__('Taxonomy "%s" deleted', 'wpcf'), $k));
            }
        }
    } else {
        if (!empty($_POST['taxonomies-to-be-deleted'])) {
            foreach ($_POST['taxonomies-to-be-deleted'] as $taxonomy_to_delete) {
                $return[] = array('type' => 'success', 'content' => sprintf(__('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);
            $return[] = array('type' => 'success', 'content' => __('Post relationships created', 'wpcf'));
        } else {
            $return[] = array('type' => 'error', 'content' => __('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'));
        }
    }
    // 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=toolset-export-import&tab=types') . '"
//-->
</script>';
        die;
    } else {
        return $return;
    }
}
 /**
  * Get array of groups that are associated with given taxonomy.
  *
  * @param string $taxonomy_slug Slug of the taxonomy
  * @return WPCF_Field_Group_Term[] Associated term field groups.
  */
 public function get_groups_by_taxonomy($taxonomy_slug)
 {
     $groups_by_taxonomies = $this->get_groups_by_taxonomies();
     return wpcf_ensarr(wpcf_getarr($groups_by_taxonomies, $taxonomy_slug));
 }
Beispiel #19
0
 /**
  * For repetitive field, get the order of individual values.
  *
  * @return array Meta IDs in the order defining the field value order.
  */
 protected function get_sort_order()
 {
     $accessor = $this->get_order_accessor();
     return wpcf_ensarr($accessor->get_raw_value());
 }
Beispiel #20
0
 /**
  * WPCF_Field_Renderer_Preview_Base constructor.
  *
  * @param WPCF_Field_Instance_Abstract $field
  * @param array $args Preview renderer settings:
  *     - maximum_item_count => Maximum count of field values that should be displayed.
  *     - maximum_item_length => Maximum length of single item.
  *     - maximum_total_length => Maximum length of output.
  *     - value_separator => Separator to be used between multiple field values.
  *     - ellipsis => Ellipsis to be added when some field values are omitted.
  *
  *     Specialized renderers may interpret the settings in a different way or add their own.
  *
  * @since 1.9.1
  */
 public function __construct($field, $args = array())
 {
     parent::__construct($field);
     $this->args = wpcf_ensarr($args);
 }
/**
 * Modify field validation rules.
 *
 * Hooked into toolset_filter_field_definition_array. Not to be used elsewhere.
 *
 * Add mandatory validation rules that have not been stored in the database but are needed by Types and toolset-forms
 * to work properly. Namely it's the URL validation for file fields. On the contrary CRED needs these validation rules
 * removed.
 *
 * @param array $field_definition A field definition array.
 * @return array
 * @since 2.2.4
 */
function wpcf_update_mandatory_validation_rules($field_definition, $ignored)
{
    // Add URL validation to file fields (containing URLs).
    //
    // This doesn't include embed files because they are more variable and the URL validation can be
    // configured on the Edit Field Group page.
    $file_fields = array('file', 'image', 'audio', 'video');
    $field_type = toolset_getarr($field_definition, 'type');
    $is_file_field = in_array($field_type, $file_fields);
    $validation_rules = wpcf_ensarr(wpcf_getnest($field_definition, array('data', 'validate')));
    $has_url2_validation = array_key_exists('url2', $validation_rules);
    if ($is_file_field) {
        unset($validation_rules['url']);
        if (!$has_url2_validation) {
            $default_validation_error_message = __('Please enter a valid URL address pointing to the file.', 'wpcf');
            $validation_error_messages = array('file' => $default_validation_error_message, 'audio' => __('Please enter a valid URL address pointing to the audio file.', 'wpcf'), 'image' => __('Please enter a valid URL address pointing to the image file.', 'wpcf'), 'video' => __('Please enter a valid URL address pointing to the video file.', 'wpcf'));
            // The url2 validation doesn't require the TLD part of the URL.
            $validation_rules['url2'] = array('active' => '1', 'message' => toolset_getarr($validation_error_messages, $field_type, $default_validation_error_message), 'suppress_for_cred' => true);
        }
        $field_definition['data']['validate'] = $validation_rules;
    }
    // On the contrary, CRED file fileds MUST NOT use this validation otherwise it won't be possible to
    // submit not changed fields on the edit form. Thus we're making sure that no such rule goes through.
    //
    // These field types come via WPToolset_Types::filterValidation().
    $cred_file_fields = array('credfile', 'credimage', 'credaudio', 'credvideo');
    $is_cred_field = in_array($field_type, $cred_file_fields);
    if ($is_cred_field) {
        unset($validation_rules['url']);
        unset($validation_rules['url2']);
        $field_definition['data']['validate'] = $validation_rules;
    }
    return $field_definition;
}