Пример #1
0
/**
 * Saves field.
 * 
 * @param type $field
 * @return type 
 */
function wpcf_admin_fields_save_field($field)
{
    if (!isset($field['name']) || !isset($field['type'])) {
        return new WP_Error('wpcf_save_field_no_name_or_type', __("Error saving field", 'wpcf'));
    }
    $field = wpcf_sanitize_field($field);
    if (empty($field['name']) || empty($field['slug'])) {
        return new WP_Error('wpcf_save_field_no_name', __("Please set name for field", 'wpcf'));
    }
    $field['id'] = $field['slug'];
    // Set field specific data
    // NOTE: This was $field['data'] = $field and seemed to work on most systems.
    // I changed it to asign via a temporary variable to fix on one system.
    $temp_field = $field;
    $field['data'] = $temp_field;
    // Unset default fields
    unset($field['data']['type'], $field['data']['slug'], $field['data']['name'], $field['data']['description'], $field['data']['user_id'], $field['data']['id'], $field['data']['data']);
    // Merge previous data (added because of outside fields)
    // @TODO Remember why
    if (wpcf_types_cf_under_control('check_outsider', $field['id'])) {
        $field_previous_data = wpcf_admin_fields_get_field($field['id']);
        if (!empty($field_previous_data['data'])) {
            $field['data'] = array_merge($field_previous_data['data'], $field['data']);
        }
    }
    $field['data'] = apply_filters('wpcf_fields_' . $field['type'] . '_meta_data', $field['data'], $field);
    // Check validation
    if (isset($field['data']['validate'])) {
        foreach ($field['data']['validate'] as $method => $data) {
            if (!isset($data['active'])) {
                unset($field['data']['validate'][$method]);
            }
        }
        if (empty($field['data']['validate'])) {
            unset($field['data']['validate']);
        }
    }
    $save_data = array();
    $save_data['id'] = $field['id'];
    $save_data['slug'] = $field['slug'];
    $save_data['type'] = $field['type'];
    $save_data['name'] = $field['name'];
    $save_data['description'] = $field['description'];
    $save_data['data'] = $field['data'];
    $save_data['data']['disabled_by_type'] = 0;
    // For radios or select
    if (!empty($field['data']['options'])) {
        foreach ($field['data']['options'] as $name => $option) {
            if (isset($option['title'])) {
                $option['title'] = $field['data']['options'][$name]['title'] = htmlspecialchars_decode($option['title']);
            }
            if (isset($option['value'])) {
                $option['value'] = $field['data']['options'][$name]['value'] = htmlspecialchars_decode($option['value']);
            }
            if (isset($option['display_value'])) {
                $option['display_value'] = $field['data']['options'][$name]['display_value'] = htmlspecialchars_decode($option['display_value']);
            }
            // For checkboxes
            if ($field['type'] == 'checkboxes' && isset($option['set_value']) && $option['set_value'] != '1') {
                $option['set_value'] = $field['data']['options'][$name]['set_value'] = htmlspecialchars_decode($option['set_value']);
            }
            if ($field['type'] == 'checkboxes' && !empty($option['display_value_selected'])) {
                $option['display_value_selected'] = $field['data']['options'][$name]['display_value_selected'] = htmlspecialchars_decode($option['display_value_selected']);
            }
            if ($field['type'] == 'checkboxes' && !empty($option['display_value_not_selected'])) {
                $option['display_value_not_selected'] = $field['data']['options'][$name]['display_value_not_selected'] = htmlspecialchars_decode($option['display_value_not_selected']);
            }
        }
    }
    // For checkboxes
    if ($field['type'] == 'checkbox' && $field['set_value'] != '1') {
        $field['set_value'] = htmlspecialchars_decode($field['set_value']);
    }
    if ($field['type'] == 'checkbox' && !empty($field['display_value_selected'])) {
        $field['display_value_selected'] = htmlspecialchars_decode($field['display_value_selected']);
    }
    if ($field['type'] == 'checkbox' && !empty($field['display_value_not_selected'])) {
        $field['display_value_not_selected'] = htmlspecialchars_decode($field['display_value_not_selected']);
    }
    // Save field
    $fields = get_option('wpcf-fields', array());
    $fields[$field['slug']] = $save_data;
    update_option('wpcf-fields', $fields);
    $field_id = $field['slug'];
    // WPML register strings
    if (function_exists('icl_register_string')) {
        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' name', $field['name']);
        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' description', $field['description']);
        // For radios or select
        if (!empty($field['data']['options'])) {
            foreach ($field['data']['options'] as $name => $option) {
                if ($name == 'default') {
                    continue;
                }
                if (isset($option['title'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' title', $option['title']);
                }
                if (isset($option['value'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' value', $option['value']);
                }
                if (isset($option['display_value'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value', $option['display_value']);
                }
                // For checkboxes
                if (isset($option['set_value']) && $option['set_value'] != '1') {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' value', $option['set_value']);
                }
                if (!empty($option['display_value_selected'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value selected', $option['display_value_selected']);
                }
                if (!empty($option['display_value_not_selected'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value not selected', $option['display_value_not_selected']);
                }
            }
        }
        if ($field['type'] == 'checkbox' && $field['set_value'] != '1') {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value', $field['set_value']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value selected', $field['display_value_selected']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_not_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value not selected', $field['display_value_not_selected']);
        }
        // Validation message
        if (!empty($field['data']['validate'])) {
            foreach ($field['data']['validate'] as $method => $validation) {
                if (!empty($validation['message'])) {
                    // Skip if it's same as default
                    $default_message = wpcf_admin_validation_messages($method);
                    if ($validation['message'] != $default_message) {
                        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' validation message ' . $method, $validation['message']);
                    }
                }
            }
        }
    }
    return $field_id;
}
 /**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  * @return type Description.
  */
 public function save()
 {
     if (!isset($_POST['wpcf']) || !isset($_POST['wpcf']['group']) || !isset($_POST['wpcf']['group']['name'])) {
         return false;
     }
     $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']);
     $group_name = wp_kses_post($_POST['wpcf']['group']['name']);
     require_once WPCF_EMBEDDED_ABSPATH . '/classes/forms.php';
     $form = new Enlimbo_Forms_Wpcf();
     if (empty($group_name)) {
         $form->triggerError();
         wpcf_admin_message(__('Group name can not be empty.', 'wpcf'), 'error');
         return $form;
     }
     $new_group = false;
     $group_slug = sanitize_title($group_name);
     // Basic check
     if (isset($_REQUEST[$this->get_id])) {
         // Check if group exists
         $post = get_post(intval($_REQUEST[$this->get_id]));
         // Name changed
         if (strtolower($group_name) != strtolower($post->post_title)) {
             // Check if already exists
             $exists = get_page_by_title($group_name, 'OBJECT', TYPES_USER_META_FIELD_GROUP_CPT_NAME);
             if (!empty($exists)) {
                 $form->triggerError();
                 wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), apply_filters('the_title', $exists->post_title)), 'error');
                 return $form;
             }
         }
         if (empty($post) || $post->post_type != TYPES_USER_META_FIELD_GROUP_CPT_NAME) {
             $form->triggerError();
             wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST[$this->get_id])), 'error');
             return $form;
         }
         $group_id = $post->ID;
     } else {
         $new_group = true;
         // Check if already exists
         $exists = get_page_by_title($group_name, 'OBJECT', TYPES_USER_META_FIELD_GROUP_CPT_NAME);
         if (!empty($exists)) {
             $form->triggerError();
             wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), apply_filters('the_title', $exists->post_title)), 'error');
             return $form;
         }
     }
     // Save fields for future use
     $fields = array();
     if (!empty($_POST['wpcf']['fields'])) {
         foreach ($_POST['wpcf']['fields'] as $key => $field) {
             $field = wpcf_sanitize_field($field);
             $field = apply_filters('wpcf_field_pre_save', $field);
             if (!empty($field['is_new'])) {
                 // Check name and slug
                 if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']), TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta')) {
                     $form->triggerError();
                     wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                     return $form;
                 }
                 if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta')) {
                     $form->triggerError();
                     wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                     return $form;
                 }
             }
             // Field ID and slug are same thing
             $field_id = wpcf_admin_fields_save_field($field, TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta');
             if (!empty($field_id)) {
                 $fields[] = $field_id;
             }
         }
     }
     // Save group
     $roles = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array();
     /**
      * Admin styles
      */
     if (isset($_POST['wpcf']['group']['admin_styles'])) {
         $admin_style = esc_html($_POST['wpcf']['group']['admin_styles']);
     }
     // Rename if needed
     if (isset($_REQUEST[$this->get_id])) {
         $_POST['wpcf']['group']['id'] = intval($_REQUEST[$this->get_id]);
     }
     $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group'], TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'user');
     // Set open fieldsets
     if ($new_group && !empty($group_id)) {
         $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true);
         if (isset($open_fieldsets[-1])) {
             $open_fieldsets[$group_id] = $open_fieldsets[-1];
             unset($open_fieldsets[-1]);
             update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets);
         }
     }
     // Rest of processes
     if (!empty($group_id)) {
         wpcf_admin_fields_save_group_fields($group_id, $fields, false, TYPES_USER_META_FIELD_GROUP_CPT_NAME);
         $this->save_group_showfor($group_id, $roles);
         /**
          * Admin styles
          */
         if (defined('TYPES_USE_STYLING_EDITOR') && TYPES_USE_STYLING_EDITOR && isset($admin_style)) {
             wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style);
         }
         $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array();
         do_action('wpcf_save_group', $_POST['wpcf']['group']);
         // admin message
         if (!wpcf_is_client()) {
             wpcf_admin_message_store(apply_filters('types_message_usermeta_saved', __('Group saved', 'wpcf'), $group_name, $new_group ? false : true), 'custom');
         }
         wp_safe_redirect(admin_url(sprintf('admin.php?page=wpcf-edit-usermeta&group_id=%d', $group_id)));
         exit;
     } else {
         wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error');
     }
 }
Пример #3
0
/**
 * Gets all fields.
 *
 * @todo Move to WPCF_Fields
 * @param bool $only_active
 * @param bool $disabled_by_type
 * @param bool $strictly_active
 * @param string $option_name
 * @param bool $use_cache
 * @param bool $clear_cache
 * @return array
 *
 * added param $use_cache by Gen (used when adding new fields to group)
 * added param $use_cache by Gen (used when adding new fields to group)
 */
function wpcf_admin_fields_get_fields($only_active = false, $disabled_by_type = false, $strictly_active = false, $option_name = 'wpcf-fields', $use_cache = true, $clear_cache = false)
{
    static $cache = array();
    if ($clear_cache) {
        $cache = array();
    }
    /**
     * Sanitize option name
     */
    switch ($option_name) {
        case 'wpcf-usermeta':
        case 'wpcf-fields':
        case WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION:
            break;
        default:
            $option_name = 'wpcf-fields';
            break;
    }
    $cache_key = md5($only_active . $disabled_by_type . $strictly_active . $option_name . $use_cache);
    if (isset($cache[$cache_key]) && $use_cache == true) {
        return $cache[$cache_key];
    }
    $required_data = array('id', 'name', 'type', 'slug');
    $fields = (array) get_option($option_name, array());
    foreach ($fields as $k => $v) {
        $failed = false;
        foreach ($required_data as $required) {
            if (!isset($v[$required])) {
                $failed = true;
                continue;
            }
            if (is_numeric($v[$required]) === true) {
                $failed = true;
                continue;
            }
        }
        if (is_numeric($k) === true || $failed) {
            unset($fields[$k]);
            continue;
        }
        // This call loads config file
        $data = wpcf_fields_type_action($v['type']);
        if (empty($data)) {
            unset($fields[$k]);
            continue;
        }
        if (isset($data['wp_version']) && wpcf_compare_wp_version($data['wp_version'], '<')) {
            unset($fields[$k]);
            continue;
        }
        if ($strictly_active) {
            if (!empty($v['data']['disabled']) || !empty($v['data']['disabled_by_type'])) {
                unset($fields[$k]);
                continue;
            }
        } else {
            if ($only_active && !empty($v['data']['disabled'])) {
                unset($fields[$k]);
                continue;
            }
            if (!$disabled_by_type && !empty($v['data']['disabled_by_type'])) {
                unset($fields[$k]);
                continue;
            }
        }
        $v['id'] = $k;
        $v['meta_key'] = wpcf_types_get_meta_prefix($v) . $k;
        $option_name_to_meta_type = array('wpcf-fields' => 'postmeta', 'wpcf-usermeta' => 'usermeta', WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION => 'termmeta');
        $v['meta_type'] = $option_name_to_meta_type[$option_name];
        $fields[$k] = wpcf_sanitize_field($v);
    }
    $cache[$cache_key] = apply_filters('types_fields', $fields);
    return $cache[$cache_key];
}
 /**
  * @param $type
  * @param array $form_data
  *
  * @return array
  */
 protected function get_field_form_data($type, $form_data = array())
 {
     /**
      * this function replace: wpcf_fields_get_field_form_data()
      */
     require_once WPCF_ABSPATH . '/includes/conditional-display.php';
     $form = array();
     /**
      * row fir field data
      */
     $table_row_typeproof = '<tr class="js-wpcf-fields-typeproof"><td><LABEL></td><td><ERROR><BEFORE><ELEMENT><AFTER></td></tr>';
     $table_row = '<tr><td><LABEL></td><td><ERROR><BEFORE><ELEMENT><AFTER></td></tr>';
     // Get field type data
     if (empty($field_data)) {
         $field_data = wpcf_fields_type_action($type);
         if (empty($field_data)) {
             return $form;
         }
     }
     // Set right ID if existing field
     if (isset($form_data['submitted_key'])) {
         $id = $form_data['submitted_key'];
     } else {
         $id = $type . '-' . rand();
     }
     // Sanitize
     $form_data = wpcf_sanitize_field($form_data);
     $required = isset($form_data['data']['validate']['required']['active']) && $form_data['data']['validate']['required']['active'] === "1" ? __('- required', 'wpcf') : '';
     $form_data['id'] = $id;
     /**
      *  Set title
      */
     $title = !empty($form_data['name']) ? $form_data['name'] : __('Untitled', 'wpcf');
     $title = sprintf('<span class="wpcf-legend-update">%s</span> <span class="description">(%s)</span> <span class="wpcf_required_data">%s</span>', $title, $field_data['title'], $required);
     // Get init data
     $field_init_data = wpcf_fields_type_action($type);
     // See if field inherits some other
     $inherited_field_data = false;
     if (isset($field_init_data['inherited_field_type'])) {
         $inherited_field_data = wpcf_fields_type_action($field_init_data['inherited_field_type']);
     }
     $form_field = array();
     /**
      * Font Awesome Icon
      */
     $icon = '';
     $icon = $this->render_field_icon($field_init_data);
     /**
      * box id & class
      */
     $closed_postboxes = $this->get_closed_postboxes();
     $clasess = array('postbox');
     // close all elements except new added fields
     if (!isset($_REQUEST['type'])) {
         $clasess[] = 'closed';
     }
     $box_id = sprintf('types-custom-field-%s', $id);
     /* Only close boxes which user closed manually
           if ( !empty($closed_postboxes) ) {
        if ( in_array($box_id, $closed_postboxes) ) {
     	   $clasess[] = 'closed';
        }
        }
        */
     /**
      * box title
      */
     $form_field['box-open'] = array('#type' => 'markup', '#markup' => sprintf('<div id="%s" class="%s"><div class="handlediv" title="%s"><br></div><h3 class="hndle ui-sortable-handle">%s%s</h3>', esc_attr($box_id), esc_attr(implode(' ', $clasess)), esc_attr__('Click to toggle', 'wpcf'), $icon, $title));
     $form_field['table-open'] = array('#type' => 'markup', '#markup' => '<table class="widefat inside js-wpcf-slugize-container">');
     // Force name and description
     $form_field['name'] = array('#type' => 'textfield', '#name' => 'name', '#attributes' => array('class' => 'widefat wpcf-forms-set-legend wpcf-forms-field-name js-wpcf-slugize-source', 'placeholder' => __('Enter field name', 'wpcf'), 'tooltip' => __('This will be the label for the field in the post editor.', 'wpcf')), '#validate' => array('required' => array('value' => true)), '#inline' => true, '#pattern' => $table_row_typeproof, '#title' => __('Field name', 'wpcf'));
     $form_field['slug'] = array('#type' => 'textfield', '#name' => 'slug', '#attributes' => array('class' => 'widefat wpcf-forms-field-slug js-wpcf-slugize', 'maxlength' => 255, 'placeholder' => __('Enter field slug', 'wpcf'), 'tooltip' => __('This is the machine name of the field.', 'wpcf')), '#validate' => array('nospecialchars' => array('value' => true)), '#inline' => true, '#pattern' => $table_row_typeproof, '#title' => __('Field slug', 'wpcf'));
     // existing field
     if (isset($form_data['submitted_key'])) {
         $form_field['slug-pre-save'] = array('#type' => 'hidden', '#name' => 'slug-pre-save');
     }
     $options = $this->get_available_types($type);
     if (empty($options)) {
         $form_field['type'] = array('#type' => 'markup', '#markup' => wpautop($type));
     } else {
         $form_field['type'] = array('#type' => 'select', '#name' => 'type', '#options' => $options, '#default_value' => $type, '#description' => __('Options for this filed will be available after group save.', 'wpcf'), '#attributes' => array('class' => 'js-wpcf-fields-type', 'data-message-after-change' => esc_attr__('Field options will be available after you save this group.', 'wpcf')));
     }
     $form_field['type']['#title'] = __('Field type', 'wpcf');
     $form_field['type']['#inline'] = true;
     $form_field['type']['#pattern'] = $table_row_typeproof;
     // If insert form callback is not provided, use generic form data
     if (function_exists('wpcf_fields_' . $type . '_insert_form')) {
         $form_field_temp = call_user_func('wpcf_fields_' . $type . '_insert_form', $form_data, 'wpcf[fields][' . $id . ']');
         if (is_array($form_field_temp)) {
             unset($form_field_temp['name'], $form_field_temp['slug']);
             /**
              * add default patter
              */
             foreach ($form_field_temp as $key => $data) {
                 if (isset($data['#pattern'])) {
                     continue;
                 }
                 $form_field_temp[$key]['#pattern'] = $table_row;
             }
             $form_field = $form_field + $form_field_temp;
         }
     }
     $form_field['description'] = array('#type' => 'textarea', '#name' => 'description', '#attributes' => array('rows' => 5, 'cols' => 1, 'placeholder' => __('Enter field description', 'wpcf'), 'class' => 'widefat', 'tooltip' => __('This optional text appears next to the field and helps users understand what this field is for.', 'wpcf')), '#inline' => true, '#pattern' => $table_row, '#title' => __('Description', 'wpcf'));
     /**
      * add placeholder field
      */
     switch ($type) {
         case 'audio':
         case 'colorpicker':
         case 'date':
         case 'email':
         case 'embed':
         case 'file':
         case 'image':
         case 'numeric':
         case 'phone':
         case 'skype':
         case 'textarea':
         case 'textfield':
         case 'url':
         case 'video':
             $form_field['placeholder'] = array('#type' => 'textfield', '#name' => 'placeholder', '#inline' => true, '#title' => __('Placeholder', 'wpcf'), '#attributes' => array('placeholder' => __('Enter placeholder', 'wpcf'), 'class' => 'widefat', 'tooltip' => __('This value is being displayed when the field is empty in the post editor.', 'wpcf')), '#pattern' => preg_replace('/<tr>/', '<tr class="wpcf-border-top">', $table_row));
             break;
     }
     /**
      * add default value
      */
     switch ($type) {
         case 'audio':
         case 'email':
         case 'embed':
         case 'file':
         case 'image':
         case 'numeric':
         case 'phone':
         case 'textfield':
         case 'url':
         case 'video':
             $form_field['user_default_value'] = array('#type' => 'textfield', '#name' => 'user_default_value', '#inline' => true, '#title' => __('Default Value', 'wpcf'), '#attributes' => array('placeholder' => __('Enter default value', 'wpcf'), 'class' => 'widefat', 'tooltip' => __('This is the initial value of the field.', 'wpcf')), '#pattern' => $table_row);
             break;
         case 'textarea':
         case 'wysiwyg':
             $form_field['user_default_value'] = array('#type' => 'textarea', '#name' => 'user_default_value', '#inline' => true, '#title' => __('Default Value', 'wpcf'), '#attributes' => array('style' => 'width:100%;margin:0 0 10px 0;', 'placeholder' => __('Enter default value', 'wpcf')), '#pattern' => $table_row);
             break;
     }
     switch ($type) {
         case 'audio':
         case 'file':
         case 'image':
         case 'embed':
         case 'url':
         case 'video':
             $form_field['user_default_value']['#validate'] = array('url' => array());
             break;
         case 'email':
             $form_field['user_default_value']['#validate'] = array('email' => array());
             break;
         case 'numeric':
             $form_field['user_default_value']['#validate'] = array('number' => array());
             break;
     }
     if (wpcf_admin_can_be_repetitive($type)) {
         $temp_warning_message = '';
         // We need to set the "repetitive" setting to a string '0' or '1', not numbers, because it will be used
         // again later in this method (which I'm not going to refactor now) and because the form renderer
         // is oversensitive.
         $is_repetitive_as_string = 1 == wpcf_getnest($form_data, array('data', 'repetitive'), '0') ? '1' : '0';
         if (!array_key_exists('data', $form_data) || !is_array($form_data['data'])) {
             $form_data['data'] = array();
         }
         $form_data['data']['repetitive'] = $is_repetitive_as_string;
         $form_field['repetitive'] = array('#type' => 'radios', '#name' => 'repetitive', '#title' => __('Single or repeating field?', 'wpcf'), '#options' => array('repeat' => array('#title' => __('Allow multiple-instances of this field', 'wpcf'), '#value' => '1', '#attributes' => array('onclick' => 'jQuery(this).parent().parent().find(\'.wpcf-cd-warning\').hide(); jQuery(this).parent().find(\'.wpcf-cd-repetitive-warning\').show();'), '#before' => '<li>', '#after' => '</li>', '#inline' => true), 'norepeat' => array('#title' => __('This field can have only one value', 'wpcf'), '#value' => '0', '#attributes' => array('onclick' => 'jQuery(this).parent().parent().find(\'.wpcf-cd-warning\').show(); jQuery(this).parent().find(\'.wpcf-cd-repetitive-warning\').hide();'), '#before' => '<li>', '#after' => '</li>', '#inline' => true)), '#default_value' => $is_repetitive_as_string, '#after' => wpcf_admin_is_repetitive($form_data) ? '<div class="wpcf-message wpcf-cd-warning wpcf-error" style="display:none;"><p>' . __("There may be multiple instances of this field already. When you switch back to single-field mode, all values of this field will be updated when it's edited.", 'wpcf') . '</p></div>' . $temp_warning_message : $temp_warning_message, '#pattern' => preg_replace('/<tr>/', '<tr class="wpcf-border-top">', $table_row), '#inline' => true, '#before' => '<ul>', '#after' => '</ul>');
     }
     /**
             /* Add validation box
     */
     $validate_function = sprintf('wpcf_fields_%s', $type);
     if (is_callable($validate_function)) {
         $form_validate = $this->form_validation('wpcf[fields][' . $id . '][validate]', call_user_func($validate_function), $form_data);
         foreach ($form_validate as $k => $v) {
             if ('hidden' != $v['#type'] && !isset($v['#pattern'])) {
                 $v['#pattern'] = $table_row;
             }
             $form_field['wpcf-' . $id . $k] = $v;
         }
     }
     /**
      * WPML Translation Preferences
      *
      * only for post meta
      *
      */
     $form_field += $this->wpml($form_data);
     // Conditional display, Relevanssi integration and other modifications can be added here.
     // Note that form_data may contain only meta_key when the field is newly
     $form_field = apply_filters('wpcf_form_field', $form_field, $form_data, $type);
     /**
      * add Remove button
      */
     $form_field['remove-field'] = array('#type' => 'markup', '#markup' => sprintf('<a href="#" class="js-wpcf-field-remove wpcf-field-remove" data-message-confirm="%s"><i class="fa fa-trash"></i> %s</a>', esc_attr__('Are you sure?', 'wpcf'), __('Remove field', 'wpcf')), '#pattern' => '<tfoot><tr><td colspan="2"><ELEMENT></td></tr></tfoot>');
     /**
      * close table
      */
     $form_field[$id . 'table-close'] = array('#type' => 'markup', '#markup' => '</table>');
     /**
      * close foldable field div
      */
     $form_field['box-close'] = array('#type' => 'markup', '#markup' => '</div>');
     // Process all form fields
     foreach ($form_field as $k => $field) {
         $name = sprintf('wpcf-%s[%s]', $id, $k);
         $form[$name] = $field;
         // Check if nested
         if (isset($field['#name']) && strpos($field['#name'], '[') === false) {
             $form[$name]['#name'] = 'wpcf[fields][' . $id . '][' . $field['#name'] . ']';
         } else {
             if (isset($field['#name']) && strpos($field['#name'], 'wpcf[fields]') === false) {
                 $form[$name]['#name'] = 'wpcf[fields][' . $id . ']' . $field['#name'];
             } else {
                 if (isset($field['#name'])) {
                     $form[$name]['#name'] = $field['#name'];
                 }
             }
         }
         if (!isset($field['#id'])) {
             $form[$name]['#id'] = $type . '-' . $field['#type'] . '-' . rand();
         }
         if (isset($field['#name']) && isset($form_data[$field['#name']])) {
             $form[$name]['#value'] = $form_data[$field['#name']];
             $form[$name]['#default_value'] = $form_data[$field['#name']];
             if (!isset($form[$name]['#pattern'])) {
                 $form[$name]['#pattern'] = $table_row;
             }
             // Check if it's in 'data'
         } else {
             if (isset($field['#name']) && isset($form_data['data'][$field['#name']])) {
                 $form[$name]['#value'] = $form_data['data'][$field['#name']];
                 $form[$name]['#default_value'] = $form_data['data'][$field['#name']];
                 if (!isset($form[$name]['#pattern'])) {
                     $form[$name]['#pattern'] = $table_row;
                 }
             }
         }
         if ($k == 'slug-pre-save') {
             $form[$name]['#value'] = $form_data['slug'];
         }
     }
     /**
      * last setup of form
      */
     if (empty($form_data) || isset($form_data['is_new'])) {
         $form['wpcf-' . $id]['is_new'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][is_new]', '#value' => '1', '#attributes' => array('class' => 'wpcf-is-new'));
     }
     // Set type
     $form['wpcf-' . $id]['type'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][type]', '#value' => $type, '#id' => $id . '-type');
     /**
      * just return this form
      */
     return $form;
 }
Пример #5
0
/**
 * Gets all fields.
 *
 * @todo Move to WPCF_Fields
 * @param bool $only_active
 * @param bool $disabled_by_type
 * @param bool $strictly_active
 * @param string $option_name
 * @param bool $use_cache
 * @param bool $clear_cache
 * @return type
 *
 * added param $use_cache by Gen (used when adding new fields to group)
 * added param $use_cache by Gen (used when adding new fields to group)
 */
function wpcf_admin_fields_get_fields($only_active = false, $disabled_by_type = false, $strictly_active = false, $option_name = 'wpcf-fields', $use_cache = true, $clear_cache = false)
{
    static $cache = array();
    if ($clear_cache) {
        $cache = array();
    }
    $cache_key = md5($only_active . $disabled_by_type . $strictly_active . $option_name . $use_cache);
    if (isset($cache[$cache_key]) && $use_cache == true) {
        return $cache[$cache_key];
    }
    $required_data = array('id', 'name', 'type', 'slug');
    $fields = (array) get_option($option_name, array());
    foreach ($fields as $k => $v) {
        $failed = false;
        foreach ($required_data as $required) {
            if (!isset($v[$required])) {
                $failed = true;
                continue;
            }
            if (is_numeric($v[$required]) === true) {
                $failed = true;
                continue;
            }
        }
        if (is_numeric($k) === true || $failed) {
            unset($fields[$k]);
            continue;
        }
        // This call loads config file
        $data = wpcf_fields_type_action($v['type']);
        if (empty($data)) {
            unset($fields[$k]);
            continue;
        }
        if (isset($data['wp_version']) && wpcf_compare_wp_version($data['wp_version'], '<')) {
            unset($fields[$k]);
            continue;
        }
        if ($strictly_active) {
            if (!empty($v['data']['disabled']) || !empty($v['data']['disabled_by_type'])) {
                unset($fields[$k]);
                continue;
            }
        } else {
            if ($only_active && !empty($v['data']['disabled'])) {
                unset($fields[$k]);
                continue;
            }
            if (!$disabled_by_type && !empty($v['data']['disabled_by_type'])) {
                unset($fields[$k]);
                continue;
            }
        }
        $v['id'] = $k;
        $v['meta_key'] = wpcf_types_get_meta_prefix($v) . $k;
        $v['meta_type'] = $option_name == 'wpcf-fields' ? 'postmeta' : 'usermeta';
        $fields[$k] = wpcf_sanitize_field($v);
    }
    $cache[$cache_key] = apply_filters('types_fields', $fields);
    return $cache[$cache_key];
}
Пример #6
0
/**
 * Processes field form data.
 *
 * @param type $type
 * @param type $form_data
 * @return type
 */
function wpcf_fields_get_field_form_data($type, $form_data = array())
{
    // Get field type data
    $field_data = wpcf_fields_type_action($type);
    if (!empty($field_data)) {
        $form = array();
        // Set right ID if existing field
        if (isset($form_data['submitted_key'])) {
            $id = $form_data['submitted_key'];
        } else {
            $id = $type . '-' . rand();
        }
        // Sanitize
        $form_data = wpcf_sanitize_field($form_data);
        // Set remove link
        $remove_link = isset($form_data['group_id']) ? admin_url('admin-ajax.php?' . 'wpcf_ajax_callback=wpcfFieldsFormDeleteElement&amp;wpcf_warning=' . __('Are you sure?', 'wpcf') . '&amp;action=wpcf_ajax&amp;wpcf_action=remove_field_from_group' . '&amp;group_id=' . intval($form_data['group_id']) . '&amp;field_id=' . $form_data['id']) . '&amp;_wpnonce=' . wp_create_nonce('remove_field_from_group') : admin_url('admin-ajax.php?' . 'wpcf_ajax_callback=wpcfFieldsFormDeleteElement&amp;wpcf_warning=' . __('Are you sure?', 'wpcf') . '&amp;action=wpcf_ajax&amp;wpcf_action=remove_field_from_group') . '&amp;_wpnonce=' . wp_create_nonce('remove_field_from_group');
        // Set move button
        $form['wpcf-' . $id . '-control'] = array('#type' => 'markup', '#markup' => '<img src="' . WPCF_RES_RELPATH . '/images/move.png" class="wpcf-fields-form-move-field" alt="' . __('Move this field', 'wpcf') . '" /><a href="' . $remove_link . '" ' . 'class="wpcf-form-fields-delete wpcf-ajax-link">' . '<img src="' . WPCF_RES_RELPATH . '/images/delete-2.png" alt="' . __('Delete this field', 'wpcf') . '" /></a>');
        // Set fieldset
        $collapsed = wpcf_admin_fields_form_fieldset_is_collapsed('fieldset-' . $id);
        // Set collapsed on AJAX call (insert)
        $collapsed = defined('DOING_AJAX') ? false : $collapsed;
        // Set title
        $title = !empty($form_data['name']) ? $form_data['name'] : __('Untitled', 'wpcf');
        $title = '<span class="wpcf-legend-update">' . $title . '</span> - ' . sprintf(__('%s field', 'wpcf'), $field_data['title']);
        // Do not display on Usermeta Group edit screen
        if (!isset($_GET['page']) || $_GET['page'] != 'wpcf-edit-usermeta') {
            if (!empty($form_data['data']['conditional_display']['conditions'])) {
                $title .= ' ' . __('(conditional)', 'wpcf');
            }
        }
        $form['wpcf-' . $id] = array('#type' => 'fieldset', '#title' => $title, '#id' => 'fieldset-' . $id, '#collapsible' => true, '#collapsed' => $collapsed);
        // Get init data
        $field_init_data = wpcf_fields_type_action($type);
        // See if field inherits some other
        $inherited_field_data = false;
        if (isset($field_init_data['inherited_field_type'])) {
            $inherited_field_data = wpcf_fields_type_action($field_init_data['inherited_field_type']);
        }
        $form_field = array();
        // Force name and description
        $form_field['name'] = array('#type' => 'textfield', '#name' => 'name', '#attributes' => array('class' => 'wpcf-forms-set-legend wpcf-forms-field-name', 'style' => 'width:100%;margin:10px 0 10px 0;', 'placeholder' => __('Enter field name', 'wpcf')), '#validate' => array('required' => array('value' => true)), '#inline' => true);
        $form_field['slug'] = array('#type' => 'textfield', '#name' => 'slug', '#attributes' => array('class' => 'wpcf-forms-field-slug', 'style' => 'width:100%;margin:0 0 10px 0;', 'maxlength' => 255, 'placeholder' => __('Enter field slug', 'wpcf')), '#validate' => array('nospecialchars' => array('value' => true)), '#inline' => true);
        // If insert form callback is not provided, use generic form data
        if (function_exists('wpcf_fields_' . $type . '_insert_form')) {
            $form_field_temp = call_user_func('wpcf_fields_' . $type . '_insert_form', $form_data, 'wpcf[fields][' . $id . ']');
            if (is_array($form_field_temp)) {
                unset($form_field_temp['name'], $form_field_temp['slug']);
                $form_field = $form_field + $form_field_temp;
            }
        }
        $form_field['description'] = array('#type' => 'textarea', '#name' => 'description', '#attributes' => array('rows' => 5, 'cols' => 1, 'style' => 'margin:0 0 10px 0;', 'placeholder' => __('Describe this field', 'wpcf')), '#inline' => true);
        /**
         * add placeholder field
         */
        switch ($type) {
            case 'audio':
            case 'colorpicker':
            case 'date':
            case 'email':
            case 'embed':
            case 'file':
            case 'image':
            case 'numeric':
            case 'phone':
            case 'skype':
            case 'textarea':
            case 'textfield':
            case 'url':
            case 'video':
                $form_field['placeholder'] = array('#type' => 'textfield', '#name' => 'placeholder', '#inline' => true, '#title' => __('Placeholder', 'wpcf'), '#attributes' => array('style' => 'width:100%;margin:0 0 10px 0;', 'placeholder' => __('Enter placeholder', 'wpcf')));
                break;
        }
        if (wpcf_admin_can_be_repetitive($type)) {
            $temp_warning_message = '';
            $form_field['repetitive'] = array('#type' => 'radios', '#name' => 'repetitive', '#title' => __('Single or repeating field?', 'wpcf'), '#options' => array('repeat' => array('#title' => __('Allow multiple-instances of this field', 'wpcf'), '#value' => '1', '#attributes' => array('onclick' => 'jQuery(this).parent().parent().find(\'.wpcf-cd-warning\').hide(); jQuery(this).parent().find(\'.wpcf-cd-repetitive-warning\').show();')), 'norepeat' => array('#title' => __('This field can have only one value', 'wpcf'), '#value' => '0', '#attributes' => array('onclick' => 'jQuery(this).parent().parent().find(\'.wpcf-cd-warning\').show(); jQuery(this).parent().find(\'.wpcf-cd-repetitive-warning\').hide();'))), '#default_value' => isset($form_data['data']['repetitive']) ? $form_data['data']['repetitive'] : '0', '#after' => wpcf_admin_is_repetitive($form_data) ? '<div class="wpcf-message wpcf-cd-warning wpcf-error" style="display:none;"><p>' . __("There may be multiple instances of this field already. When you switch back to single-field mode, all values of this field will be updated when it's edited.", 'wpcf') . '</p></div>' . $temp_warning_message : $temp_warning_message);
        }
        // Process all form fields
        foreach ($form_field as $k => $field) {
            $form['wpcf-' . $id][$k] = $field;
            // Check if nested
            if (isset($field['#name']) && strpos($field['#name'], '[') === false) {
                $form['wpcf-' . $id][$k]['#name'] = 'wpcf[fields][' . $id . '][' . $field['#name'] . ']';
            } else {
                if (isset($field['#name'])) {
                    $form['wpcf-' . $id][$k]['#name'] = 'wpcf[fields][' . $id . ']' . $field['#name'];
                }
            }
            if (!isset($field['#id'])) {
                $form['wpcf-' . $id][$k]['#id'] = $type . '-' . $field['#type'] . '-' . rand();
            }
            if (isset($field['#name']) && isset($form_data[$field['#name']])) {
                $form['wpcf-' . $id][$k]['#value'] = $form_data[$field['#name']];
                $form['wpcf-' . $id][$k]['#default_value'] = $form_data[$field['#name']];
                // Check if it's in 'data'
            } else {
                if (isset($field['#name']) && isset($form_data['data'][$field['#name']])) {
                    $form['wpcf-' . $id][$k]['#value'] = $form_data['data'][$field['#name']];
                    $form['wpcf-' . $id][$k]['#default_value'] = $form_data['data'][$field['#name']];
                }
            }
        }
        // Set type
        $form['wpcf-' . $id]['type'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][type]', '#value' => $type, '#id' => $id . '-type');
        // Add validation box
        $form_validate = wpcf_admin_fields_form_validation('wpcf[fields][' . $id . '][validate]', call_user_func('wpcf_fields_' . $type), $form_data);
        foreach ($form_validate as $k => $v) {
            $form['wpcf-' . $id][$k] = $v;
        }
        // WPML Translation Preferences
        if (function_exists('wpml_cf_translation_preferences')) {
            $custom_field = !empty($form_data['slug']) ? wpcf_types_get_meta_prefix($form_data) . $form_data['slug'] : false;
            $suppress_errors = $custom_field == false ? true : false;
            $translatable = array('textfield', 'textarea', 'wysiwyg');
            $action = in_array($type, $translatable) ? 'translate' : 'copy';
            $form['wpcf-' . $id]['wpml-preferences'] = array('#type' => 'fieldset', '#title' => __('Translation preferences', 'wpcf'), '#collapsed' => true);
            $wpml_prefs = wpml_cf_translation_preferences($id, $custom_field, 'wpcf', false, $action, false, $suppress_errors);
            $wpml_prefs = str_replace('<span style="color:#FF0000;">', '<span class="wpcf-form-error">', $wpml_prefs);
            $form['wpcf-' . $id]['wpml-preferences']['form'] = array('#type' => 'markup', '#markup' => $wpml_prefs);
        }
        if (empty($form_data) || isset($form_data['is_new'])) {
            $form['wpcf-' . $id]['is_new'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][is_new]', '#value' => '1', '#attributes' => array('class' => 'wpcf-is-new'));
        }
        $form_data['id'] = $id;
        $form['wpcf-' . $id] = apply_filters('wpcf_form_field', $form['wpcf-' . $id], $form_data);
        return $form;
    }
    return false;
}
 /**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  *
  * @return type Description.
  */
 private function save_group_fields($group_id)
 {
     if (empty($_POST['wpcf']['fields'])) {
         delete_post_meta($group_id, '_wp_types_group_fields');
         return;
     }
     $fields = array();
     // First check all fields
     foreach ($_POST['wpcf']['fields'] as $key => $field) {
         $field = wpcf_sanitize_field($field);
         $field = apply_filters('wpcf_field_pre_save', $field);
         if (!empty($field['is_new'])) {
             // Check name and slug
             if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']))) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                 return $form;
             }
             if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']))) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                 return $form;
             }
         }
         $field['submit-key'] = $key;
         // Field ID and slug are same thing
         $field_id = wpcf_admin_fields_save_field($field);
         if (is_wp_error($field_id)) {
             $this->triggerError();
             wpcf_admin_message($field_id->get_error_message(), 'error');
             return;
         }
         if (!empty($field_id)) {
             $fields[] = $field_id;
         }
         // WPML
         /** @var string $field_id */
         if (defined('ICL_SITEPRESS_VERSION') && version_compare(ICL_SITEPRESS_VERSION, '3.2', '<')) {
             if (function_exists('wpml_cf_translation_preferences_store')) {
                 $real_custom_field_name = wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_id)) . $field_id;
                 wpml_cf_translation_preferences_store($key, $real_custom_field_name);
             }
         }
     }
     wpcf_admin_fields_save_group_fields($group_id, $fields);
 }
Пример #8
0
 private function save_filter_fields($group_id, $fields_data)
 {
     if (empty($fields_data)) {
         delete_post_meta($group_id, '_wp_types_group_fields');
         return;
     }
     $fields = array();
     // First check all fields
     foreach ($fields_data as $field_key => $field) {
         $field = wpcf_sanitize_field($field);
         $field = apply_filters('wpcf_field_pre_save', $field);
         if (!empty($field['is_new'])) {
             // Check name and slug
             if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']), WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION)) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                 return;
             }
             if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION)) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                 return;
             }
         }
         $field['submit-key'] = $field_key;
         // Field ID and slug are same thing
         $field_slug = wpcf_admin_fields_save_field($field, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
         if (is_wp_error($field_slug)) {
             $this->triggerError();
             wpcf_admin_message($field_slug->get_error_message(), 'error');
             return;
         }
         if (!empty($field_slug)) {
             $fields[] = $field_slug;
         }
         // WPML
         if (defined('ICL_SITEPRESS_VERSION') && version_compare(ICL_SITEPRESS_VERSION, '3.2', '<')) {
             if (function_exists('wpml_cf_translation_preferences_store')) {
                 $real_custom_field_name = wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_slug, false, false, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION)) . $field_slug;
                 wpml_cf_translation_preferences_store($field_key, $real_custom_field_name);
             }
         }
     }
     wpcf_admin_fields_save_group_fields($group_id, $fields, false, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
 }
Пример #9
0
/**
 * Saves field.
 * Modified by Gen, 13.02.2013
 *
 * @param array $field Field data
 * @param string $post_type
 * @param string $option_name
 *
 * @return string|WP_Error Field slug or an error object.
 */
function wpcf_admin_fields_save_field($field, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, $option_name = 'wpcf-fields')
{
    if (!isset($field['name']) || !isset($field['type'])) {
        return new WP_Error('wpcf_save_field_no_name_or_type', __("Error saving field", 'wpcf'));
    }
    $field = wpcf_sanitize_field($field);
    if (empty($field['name']) || empty($field['slug'])) {
        return new WP_Error('wpcf_save_field_no_name', __("Please set name for field", 'wpcf'));
    }
    $field['id'] = $field['slug'];
    // Set field specific data
    // NOTE: This was $field['data'] = $field and seemed to work on most systems.
    // I changed it to asign via a temporary variable to fix on one system.
    $temp_field = $field;
    $field['data'] = $temp_field;
    // Unset default fields
    unset($field['data']['type'], $field['data']['slug'], $field['data']['name'], $field['data']['description'], $field['data']['user_id'], $field['data']['id'], $field['data']['data']);
    // Merge previous data (added because of outside fields)
    // @TODO Remember why
    if (wpcf_types_cf_under_control('check_outsider', $field['id'], $post_type, $option_name)) {
        $field_previous_data = wpcf_admin_fields_get_field($field['id'], false, true, false, $option_name);
        if (!empty($field_previous_data['data'])) {
            $field['data'] = array_merge($field_previous_data['data'], $field['data']);
        }
    }
    $field['data'] = apply_filters('wpcf_fields_' . $field['type'] . '_meta_data', $field['data'], $field);
    // Check validation
    if (isset($field['data']['validate'])) {
        foreach ($field['data']['validate'] as $method => $data) {
            if (!isset($data['active'])) {
                unset($field['data']['validate'][$method]);
            }
        }
        if (empty($field['data']['validate'])) {
            unset($field['data']['validate']);
        }
    }
    $save_data = array();
    $save_data['id'] = $field['id'];
    $save_data['slug'] = $field['slug'];
    $save_data['type'] = $field['type'];
    $save_data['name'] = $field['name'];
    $save_data['description'] = $field['description'];
    $save_data['data'] = $field['data'];
    $save_data['data']['disabled_by_type'] = 0;
    // For radios or select
    if (!empty($field['data']['options'])) {
        foreach ($field['data']['options'] as $name => $option) {
            if (isset($option['title'])) {
                $option['title'] = $field['data']['options'][$name]['title'] = htmlspecialchars_decode($option['title']);
            }
            if (isset($option['value'])) {
                $option['value'] = $field['data']['options'][$name]['value'] = htmlspecialchars_decode($option['value']);
            }
            if (isset($option['display_value'])) {
                $option['display_value'] = $field['data']['options'][$name]['display_value'] = htmlspecialchars_decode($option['display_value']);
            }
            // For checkboxes
            if ($field['type'] == 'checkboxes' && isset($option['set_value']) && $option['set_value'] != '1') {
                $option['set_value'] = $field['data']['options'][$name]['set_value'] = htmlspecialchars_decode($option['set_value']);
            }
            if ($field['type'] == 'checkboxes' && !empty($option['display_value_selected'])) {
                $option['display_value_selected'] = $field['data']['options'][$name]['display_value_selected'] = htmlspecialchars_decode($option['display_value_selected']);
            }
            if ($field['type'] == 'checkboxes' && !empty($option['display_value_not_selected'])) {
                $option['display_value_not_selected'] = $field['data']['options'][$name]['display_value_not_selected'] = htmlspecialchars_decode($option['display_value_not_selected']);
            }
        }
    }
    // For checkboxes
    if ('checkbox' == $field['type'] && isset($field['set_value']) && $field['set_value'] != '1') {
        $field['set_value'] = htmlspecialchars_decode($field['set_value']);
    }
    if ($field['type'] == 'checkbox' && !empty($field['display_value_selected'])) {
        $field['display_value_selected'] = htmlspecialchars_decode($field['display_value_selected']);
    }
    if ($field['type'] == 'checkbox' && !empty($field['display_value_not_selected'])) {
        $field['display_value_not_selected'] = htmlspecialchars_decode($field['display_value_not_selected']);
    }
    // Save field
    $fields = get_option($option_name, array());
    // prevent erasing saved conditional display data (it's controlled via ajax)
    if (isset($fields[$field['slug']]['data']['conditional_display']) && !empty($fields[$field['slug']]['data']['conditional_display'])) {
        $save_data['data']['conditional_display'] = $fields[$field['slug']]['data']['conditional_display'];
    }
    $fields[$field['slug']] = $save_data;
    update_option($option_name, $fields);
    $field_id = $field['slug'];
    // WPML register strings
    if (function_exists('icl_register_string')) {
        if (isset($_POST['wpml_cf_translation_preferences'][$field_id])) {
            $__wpml_action = intval($_POST['wpml_cf_translation_preferences'][$field_id]);
        } else {
            $__wpml_action = wpcf_wpml_get_action_by_type($field['type']);
        }
        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' name', $field['name']);
        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' description', $field['description']);
        // For radios or select
        if (!empty($field['data']['options'])) {
            foreach ($field['data']['options'] as $name => $option) {
                if ($name == 'default') {
                    continue;
                }
                if (isset($option['title'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' title', $option['title']);
                }
                if ($__wpml_action === 2) {
                    if (isset($option['value'])) {
                        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' value', $option['value']);
                    }
                }
                if (isset($option['display_value'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value', $option['display_value']);
                }
                // For checkboxes
                if (isset($option['set_value']) && $option['set_value'] != '1') {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' value', $option['set_value']);
                }
                if (!empty($option['display_value_selected'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value selected', $option['display_value_selected']);
                }
                if (!empty($option['display_value_not_selected'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value not selected', $option['display_value_not_selected']);
                }
            }
        }
        if ($field['type'] == 'checkbox' && $field['set_value'] != '1') {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value', $field['set_value']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value selected', $field['display_value_selected']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_not_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value not selected', $field['display_value_not_selected']);
        }
        // Validation message
        if (!empty($field['data']['validate'])) {
            foreach ($field['data']['validate'] as $method => $validation) {
                if (!empty($validation['message'])) {
                    // Skip if it's same as default
                    $default_message = wpcf_admin_validation_messages($method);
                    if ($validation['message'] != $default_message) {
                        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' validation message ' . $method, $validation['message']);
                    }
                }
            }
        }
    }
    /**
     * WPML update translation status
     */
    if (isset($save_data['data']) && isset($save_data['data']['submit-key'])) {
        if (isset($_POST['wpml_cf_translation_preferences'][$save_data['data']['submit-key']])) {
            do_action('wpml_config', array('section' => 'custom-fields', 'key' => wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($save_data['slug'])) . $save_data['slug'], 'value' => intval($_POST['wpml_cf_translation_preferences'][$save_data['data']['submit-key']]), 'read_only' => true));
        }
    }
    return $field_id;
}
Пример #10
0
/**
 * Saves field.
 *
 * Note: This is (probably) intended only for saving field definition data on the Edit Field Group pages.
 * 
 * @param array $field Field data
 * @param string $post_type
 * @param string $option_name
 *
 * @return string|WP_Error Field slug or an error object.
 */
function wpcf_admin_fields_save_field($field, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, $option_name = 'wpcf-fields')
{
    if (!isset($field['name']) || !isset($field['type'])) {
        return new WP_Error('wpcf_save_field_no_name_or_type', __("Error saving field", 'wpcf'));
    }
    $field = wpcf_sanitize_field($field);
    if (empty($field['name']) || empty($field['slug'])) {
        return new WP_Error('wpcf_save_field_no_name', __("Please set name for field", 'wpcf'));
    }
    $field['id'] = $field['slug'];
    // Set field specific data
    // NOTE: This was $field['data'] = $field and seemed to work on most systems.
    // I changed it to asign via a temporary variable to fix on one system.
    $temp_field = $field;
    $field['data'] = $temp_field;
    // Unset default fields
    // fixme These lines effectively erases all new values in "data", does anyone know why?
    // fixme This function needs SERIOUS review.
    unset($field['data']['type'], $field['data']['slug'], $field['data']['name'], $field['data']['description'], $field['data']['user_id'], $field['data']['id'], $field['data']['data']);
    // Merge previous data (added because of outside fields)
    // @TODO Remember why
    if (wpcf_types_cf_under_control('check_outsider', $field['id'], $post_type, $option_name)) {
        $field_previous_data = wpcf_admin_fields_get_field($field['id'], false, true, false, $option_name);
        if (!empty($field_previous_data['data'])) {
            $field['data'] = array_merge($field_previous_data['data'], $field['data']);
        }
    }
    $field['data'] = apply_filters('wpcf_fields_' . $field['type'] . '_meta_data', $field['data'], $field);
    // Check validation
    if (isset($field['data']['validate'])) {
        foreach ($field['data']['validate'] as $method => $data) {
            if (!isset($data['active'])) {
                unset($field['data']['validate'][$method]);
            }
        }
        if (empty($field['data']['validate'])) {
            unset($field['data']['validate']);
        }
    }
    $save_data = array();
    $save_data['id'] = $field['id'];
    $save_data['slug'] = $field['slug'];
    $save_data['type'] = $field['type'];
    $save_data['name'] = $field['name'];
    $save_data['description'] = $field['description'];
    $save_data['data'] = $field['data'];
    $save_data['data']['disabled_by_type'] = 0;
    // For radios or select
    if (!empty($field['data']['options'])) {
        foreach ($field['data']['options'] as $name => $option) {
            if (isset($option['title'])) {
                $option['title'] = $field['data']['options'][$name]['title'] = htmlspecialchars_decode($option['title']);
            }
            if (isset($option['value'])) {
                $option['value'] = $field['data']['options'][$name]['value'] = htmlspecialchars_decode($option['value']);
            }
            if (isset($option['display_value'])) {
                $option['display_value'] = $field['data']['options'][$name]['display_value'] = htmlspecialchars_decode($option['display_value']);
            }
            // For checkboxes
            if ($field['type'] == 'checkboxes' && isset($option['set_value']) && $option['set_value'] != '1') {
                $option['set_value'] = $field['data']['options'][$name]['set_value'] = htmlspecialchars_decode($option['set_value']);
            }
            if ($field['type'] == 'checkboxes' && !empty($option['display_value_selected'])) {
                $option['display_value_selected'] = $field['data']['options'][$name]['display_value_selected'] = htmlspecialchars_decode($option['display_value_selected']);
            }
            if ($field['type'] == 'checkboxes' && !empty($option['display_value_not_selected'])) {
                $option['display_value_not_selected'] = $field['data']['options'][$name]['display_value_not_selected'] = htmlspecialchars_decode($option['display_value_not_selected']);
            }
        }
    }
    // For checkboxes
    if ('checkbox' == $field['type'] && isset($field['set_value']) && $field['set_value'] != '1') {
        $field['set_value'] = htmlspecialchars_decode($field['set_value']);
    }
    if ($field['type'] == 'checkbox' && !empty($field['display_value_selected'])) {
        $field['display_value_selected'] = htmlspecialchars_decode($field['display_value_selected']);
    }
    if ($field['type'] == 'checkbox' && !empty($field['display_value_not_selected'])) {
        $field['display_value_not_selected'] = htmlspecialchars_decode($field['display_value_not_selected']);
    }
    // Save field
    $fields = get_option($option_name, array());
    // prevent erasing saved conditional display data (it's controlled via ajax)
    if (isset($fields[$field['slug']]['data']['conditional_display']) && !empty($fields[$field['slug']]['data']['conditional_display'])) {
        $save_data['data']['conditional_display'] = $fields[$field['slug']]['data']['conditional_display'];
    }
    // on changing a field slug
    if (isset($field['slug-pre-save']) && $field['slug'] != $field['slug-pre-save']) {
        // takeover conditions on renamed slug
        if (isset($fields[$field['slug-pre-save']]['data']['conditional_display']) && !empty($fields[$field['slug-pre-save']]['data']['conditional_display'])) {
            $save_data['data']['conditional_display'] = $fields[$field['slug-pre-save']]['data']['conditional_display'];
        }
        // field is assigned ONLY to current group
        $belongs_to_groups = wpcf_admin_fields_get_groups_by_field($field['slug-pre-save'], $post_type);
        if (isset($_GET['group_id']) && isset($fields[$field['slug-pre-save']]) && count($belongs_to_groups) == 1 && isset($belongs_to_groups[$_GET['group_id']])) {
            global $wpdb;
            // as it's the only group with this field we can update the slug for all items
            $wpdb->update($wpdb->postmeta, array('meta_key' => 'wpcf-' . $field['slug']), array('meta_key' => 'wpcf-' . $field['slug-pre-save']), array('%s'), array('%s'));
            // change slug in field conditions
            foreach ($fields as $key => &$single_field) {
                if (isset($single_field['data']['conditional_display']['conditions'])) {
                    foreach ($single_field['data']['conditional_display']['conditions'] as &$single_condition) {
                        if ($single_condition['field'] == $field['slug-pre-save']) {
                            $single_condition['field'] = $field['slug'];
                        }
                    }
                }
            }
            // get all group conditions which contain the old slug
            $groups_conditions = $wpdb->get_results("\n                SELECT post_id, meta_value\n                FROM   {$wpdb->postmeta}\n                WHERE  meta_key = '_wpcf_conditional_display'\n                AND    meta_value LIKE '%" . $field['slug-pre-save'] . "%'\n\t        ");
            if ($groups_conditions) {
                foreach ($groups_conditions as $group_condition) {
                    $meta_value = unserialize($group_condition->meta_value);
                    // proper proof for slug
                    if (isset($meta_value['conditions'])) {
                        foreach ($meta_value['conditions'] as &$single_condition) {
                            if ($single_condition['field'] == $field['slug-pre-save']) {
                                // update conditions slug
                                $single_condition['field'] = $field['slug'];
                                update_post_meta($group_condition->post_id, '_wpcf_conditional_display', $meta_value);
                                break;
                            }
                        }
                    }
                }
            }
            // delete field
            wpcf_admin_fields_delete_field($field['slug-pre-save'], $post_type, $option_name);
            unset($fields[$field['slug-pre-save']]);
            // update items by post type
        } elseif (isset($_GET['group_id'])) {
            switch ($post_type) {
                case 'wp-types-group':
                    $group = Types_Field_Group_Post_Factory::load($_GET['group_id']);
                    break;
                case 'wp-types-term-group':
                    $group = Types_Field_Group_Term_Factory::load($_GET['group_id']);
                    break;
                case 'wp-types-user-group':
                    $group = Types_Field_Group_User_Factory::load($_GET['group_id']);
                    break;
            }
            if (null != $group) {
                $assigned_types = $group->get_assigned_to_types();
                if (!empty($assigned_types)) {
                    $items = $group->get_assigned_to_items();
                    if (is_array($items) && !empty($items)) {
                        global $wpdb;
                        foreach ($items as $item) {
                            $wpdb->update($wpdb->postmeta, array('meta_key' => 'wpcf-' . $field['slug']), array('meta_key' => 'wpcf-' . $field['slug-pre-save'], 'post_id' => $item->ID), array('%s', '%d'), array('%s'));
                        }
                    }
                }
            }
        }
    }
    $fields[$field['slug']] = $save_data;
    update_option($option_name, $fields);
    $field_id = $field['slug'];
    // WPML register strings
    if (function_exists('icl_register_string')) {
        if (isset($_POST['wpml_cf_translation_preferences'][$field_id])) {
            $__wpml_action = intval($_POST['wpml_cf_translation_preferences'][$field_id]);
        } else {
            $__wpml_action = wpcf_wpml_get_action_by_type($field['type']);
        }
        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' name', $field['name']);
        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' description', $field['description']);
        // For radios or select
        if (!empty($field['data']['options'])) {
            foreach ($field['data']['options'] as $name => $option) {
                if ($name == 'default') {
                    continue;
                }
                if (isset($option['title'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' title', $option['title']);
                }
                if ($__wpml_action === 2) {
                    if (isset($option['value'])) {
                        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' value', $option['value']);
                    }
                }
                if (isset($option['display_value'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value', $option['display_value']);
                }
                // For checkboxes
                if (isset($option['set_value']) && $option['set_value'] != '1') {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' value', $option['set_value']);
                }
                if (!empty($option['display_value_selected'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value selected', $option['display_value_selected']);
                }
                if (!empty($option['display_value_not_selected'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value not selected', $option['display_value_not_selected']);
                }
            }
        }
        if ($field['type'] == 'checkbox' && $field['set_value'] != '1') {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value', $field['set_value']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value selected', $field['display_value_selected']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_not_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value not selected', $field['display_value_not_selected']);
        }
        // Validation message
        if (!empty($field['data']['validate'])) {
            foreach ($field['data']['validate'] as $method => $validation) {
                if (!empty($validation['message'])) {
                    // Skip if it's same as default
                    $default_message = wpcf_admin_validation_messages($method);
                    if ($validation['message'] != $default_message) {
                        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' validation message ' . $method, $validation['message']);
                    }
                }
            }
        }
    }
    /**
     * WPML update translation status
     */
    if (isset($save_data['data']) && isset($save_data['data']['submit-key'])) {
        if (isset($_POST['wpml_cf_translation_preferences'][$save_data['data']['submit-key']])) {
            do_action('wpml_config', array('section' => 'custom-fields', 'key' => wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($save_data['slug'])) . $save_data['slug'], 'value' => intval($_POST['wpml_cf_translation_preferences'][$save_data['data']['submit-key']]), 'read_only' => true));
        }
    }
    return $field_id;
}
Пример #11
0
/**
 * Gets field by ID.
 * 
 * @global type $wpdb
 * @param type $field_id
 * @param type $only_active
 * @return type 
 */
function wpcf_admin_fields_get_field($field_id, $only_active = false, $disabled_by_type = false, $strictly_active = false)
{
    $fields = wpcf_admin_fields_get_fields($only_active, $disabled_by_type, $strictly_active);
    if (!empty($fields[$field_id])) {
        $data = wpcf_fields_type_action($fields[$field_id]['type']);
        if (isset($data['wp_version']) && wpcf_compare_wp_version($data['wp_version'], '<')) {
            return array();
        }
        $fields[$field_id]['id'] = $field_id;
        return wpcf_sanitize_field($fields[$field_id]);
    }
    return array();
}