Пример #1
0
 /**
  * @param array $field_value Checkboxes field value in the "intermediate" format.
  * @return bool Whether this option is checked in the field whose value is provided.
  * @since 1.9.1
  */
 public function is_option_checked($field_value)
 {
     // Value that should be stored in database if this option is checked
     $option_value = wpcf_getnest($field_value, array($this->option_id, 0), null);
     $is_checked = null !== $option_value && $this->get_value_to_store() == $option_value;
     return $is_checked;
 }
Пример #2
0
 /**
  * @inheritdoc
  *
  * Checkbox field definition needs to contain these keys:
  *
  * - data/set_value: The value that will be saved to database when the field is checked. Default is '1'
  *
  * @param array $definition_array
  * @return array
  * @since 2.0
  */
 public function sanitize_field_definition_array($definition_array)
 {
     $definition_array = parent::sanitize_field_definition_array($definition_array);
     $set_value = wpcf_getnest($definition_array, array('data', 'set_value'), '1');
     if (!is_string($set_value) && !is_numeric($set_value)) {
         $set_value = '1';
     }
     $definition_array['data']['set_value'] = $set_value;
     return $definition_array;
 }
Пример #3
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;
 }
Пример #4
0
 /**
  * @inheritdoc
  *
  * @param array $definition_array
  * @return array
  * @since 2.1
  */
 protected function sanitize_field_definition_array_type_specific($definition_array)
 {
     $definition_array['type'] = Types_Field_Type_Definition_Factory::CHECKBOXES;
     $definition_array = $this->sanitize_element_isset($definition_array, 'save_empty', 'no', array('yes', 'no'), 'data');
     $options = wpcf_ensarr(wpcf_getnest($definition_array, array('data', 'options')));
     foreach ($options as $key => $option) {
         $options[$key] = $this->sanitize_single_option($option);
     }
     $definition_array['data']['options'] = $options;
     return $definition_array;
 }
Пример #5
0
 /**
  * @inheritdoc
  *
  * @param array $definition_array
  * @return array
  * @since 2.0
  */
 protected function sanitize_field_definition_array_type_specific($definition_array)
 {
     $definition_array['type'] = Types_Field_Type_Definition_Factory::CHECKBOX;
     $definition_array = $this->sanitize_element_isset($definition_array, 'display', 'db', array('db', 'value'), 'data');
     $definition_array = $this->sanitize_element_isset($definition_array, 'display_value_selected', '', null, 'data');
     $definition_array = $this->sanitize_element_isset($definition_array, 'display_value_not_selected', '', null, 'data');
     $definition_array = $this->sanitize_element_isset($definition_array, 'save_empty', 'no', array('yes', 'no'), 'data');
     $set_value = wpcf_getnest($definition_array, array('data', 'set_value'));
     if (!is_string($set_value) && !is_numeric($set_value)) {
         $set_value = '1';
     }
     $definition_array['data']['set_value'] = $set_value;
     return $definition_array;
 }
Пример #6
0
 /**
  * Determine value to be displayed for this option.
  *
  * @param bool $is_checked For which value should the output be rendered.
  * @return string Display value depending on option definition and field display mode 
  * @since 1.9.1
  */
 public function get_display_value($is_checked = true)
 {
     $field_definition_array = $this->field_definition->get_definition_array();
     $display_mode = wpcf_getnest($field_definition_array, array('data', 'display'), 'db');
     $display_mode = 'value' == $display_mode ? 'value' : 'db';
     if ('db' == $display_mode) {
         return $is_checked ? $this->get_value_to_store() : '';
     } else {
         if ($is_checked) {
             return wpcf_getarr($this->config, 'display_value');
         } else {
             return '';
         }
     }
 }
Пример #7
0
 /**
  * @inheritdoc
  *
  * @param array $definition_array
  * @return array
  * @since 2.1
  */
 protected function sanitize_field_definition_array_type_specific($definition_array)
 {
     $definition_array['type'] = Types_Field_Type_Definition_Factory::SELECT;
     $options = wpcf_ensarr(wpcf_getnest($definition_array, array('data', 'options')));
     foreach ($options as $key => $option) {
         if ('default' == $key) {
             continue;
         }
         $options[$key] = $this->sanitize_single_option($option);
     }
     $default_option = wpcf_getarr($options, 'default');
     if (!is_string($default_option) || !array_key_exists($default_option, $options)) {
         $default_option = 'no-default';
     }
     $options['default'] = $default_option;
     $definition_array['data']['options'] = $options;
     return $definition_array;
 }
 /**
  * Check a slug for conflict with slugs used for post type permalink rewriting.
  *
  * @param string $value Value to check.
  * @param string $exclude_id Post type slug to exclude from checking.
  *
  * @return array|bool Conflict information (an associative array with conflicting_id, message) or false when
  *     there's no conflict.
  * @since 2.1
  */
 private function check_slug_conflicts_in_post_type_rewrite_rules($value, $exclude_id)
 {
     // Merge currently registered post types (which might include some from other plugins) and
     // Types settings (which might include deactivated post types).
     $post_type_settings = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
     if (!is_array($post_type_settings)) {
         return false;
     }
     $post_type_settings = array_merge($post_type_settings, get_post_types(array(), 'objects'));
     foreach ($post_type_settings as $post_type) {
         // Read information from the post type object or Types settings
         if (is_object($post_type)) {
             $slug = $post_type->name;
             $is_permalink_rewriting_enabled = (bool) wpcf_getarr($post_type->rewrite, 'enabled');
             $rewrite_slug = wpcf_getarr($post_type->rewrite, 'slug');
             $is_custom_slug_used = !empty($rewrite_slug);
         } else {
             $slug = wpcf_getarr($post_type, 'slug');
             $is_permalink_rewriting_enabled = (bool) wpcf_getnest($post_type, array('rewrite', 'enabled'));
             $is_custom_slug_used = wpcf_getnest($post_type, array('rewrite', 'custom')) == 'custom';
             $rewrite_slug = wpcf_getnest($post_type, array('rewrite', 'slug'));
         }
         if ($slug == $exclude_id) {
             continue;
         }
         if ($is_permalink_rewriting_enabled) {
             $conflict_candidate = $is_custom_slug_used ? $rewrite_slug : $slug;
             if ($conflict_candidate == $value) {
                 $conflict = array('conflicting_id' => $slug, 'message' => sprintf(__('The same value is already used in permalink rewrite rules for the custom post type "%s". Using it again can cause issues with permalinks.', 'wpcf'), esc_html($slug)));
                 return $conflict;
             }
         }
     }
     // No conflicts found.
     return false;
 }
Пример #9
0
 /**
  * Try to convert a taxonomy slug to a label.
  *
  * @param string $slug Taxonomy slug.
  * @param string $label_name One of the available labels of the taxonomy.
  *
  * @link https://codex.wordpress.org/Function_Reference/get_taxonomies Taxonomy object description.
  *
  * @return string Selected taxonomy label or slug if the label was not found.
  * @since 1.9
  */
 public static function taxonomy_slug_to_label($slug, $label_name = 'name')
 {
     $all_taxonomies = self::get_all_taxonomies();
     $taxonomy_display_name = wpcf_getnest($all_taxonomies, array($slug, 'labels', $label_name), $slug);
     return $taxonomy_display_name;
 }
 /**
  * @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;
 }
 /**
  * @return array An option_id => option_data array.
  */
 public function get_field_options()
 {
     return wpcf_ensarr(wpcf_getnest($this->definition_array, array('data', 'options')));
 }
Пример #12
0
/**
 * Check if field is repetitive.
 *
 * @deprecated Use types_is_repetitive instead.
 * @param array $field Field definition array.
 * @return bool
 */
function wpcf_admin_is_repetitive($field)
{
    $field_type = wpcf_getarr($field, 'type', '');
    $is_repetitive = (int) wpcf_getnest($field, array('data', 'repetitive'), 0);
    return $is_repetitive && !empty($field_type) && wpcf_admin_can_be_repetitive($field_type);
}
Пример #13
0
/**
 * 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;
}
Пример #14
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, '#attributes' => array('class' => 'js-wpcf-slugize-container'));
        // 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 js-wpcf-slugize-source', '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 js-wpcf-slugize', '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;
        }
        /**
         * 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('style' => 'width:100%;margin:0 0 10px 0;', 'placeholder' => __('Enter default value', 'wpcf')));
                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')));
                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)) {
            // 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;
            $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' => $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);
        }
        // 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
         *
         * only for post meta
         *
         */
        if (isset($form_data['meta_type']) && 'postmeta' == $form_data['meta_type'] && 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;
}
Пример #15
0
 /**
  * Save field group data from $_POST to database when the form is submitted.
  */
 protected function save()
 {
     if (!$this->is_there_something_to_save()) {
         return;
     }
     $wpcf_data = wpcf_getpost('wpcf', null);
     // check incoming $_POST data
     $group_id = wpcf_getnest($_POST, array('wpcf', 'group', 'id'), null);
     if (null === $group_id) {
         // probably can be 0, which is valid
         $this->verification_failed_and_die(1);
     }
     // nonce verification
     $nonce_name = $this->get_nonce_action($group_id);
     $nonce = wpcf_getpost('wpcf_save_group_nonce');
     if (!wp_verify_nonce($nonce, $nonce_name)) {
         $this->verification_failed_and_die(2);
     }
     // save group data to the database
     $group_id = wpcf_admin_fields_save_group(wpcf_getarr($wpcf_data, 'group', array()), WPCF_Field_Group_Term::POST_TYPE, 'term');
     $field_group = $this->load_field_group($group_id);
     if (null == $field_group) {
         return;
     }
     // Why are we doing this?!
     $_REQUEST[$this->get_id] = $group_id;
     // save taxonomies
     $taxonomies_post = wpcf_getnest($wpcf_data, array('group', 'taxonomies'), array());
     $field_group->update_associated_taxonomies($taxonomies_post);
     $this->save_filter_fields($group_id, wpcf_getarr($wpcf_data, 'fields', array()));
     // promo message
     if (!wpcf_is_client()) {
         wpcf_admin_message_store(apply_filters('types_message_term_fields_saved', __('Group saved', 'wpcf'), 0, isset($_GET['group_id']) ? true : false), 'custom');
     }
     // Redirect to edit page so we stay on it even if user reloads it
     // and to present admin notices
     wp_safe_redirect(esc_url_raw(add_query_arg(array('page' => WPCF_Page_Edit_Termmeta::PAGE_NAME, $this->get_id => $group_id), admin_url('admin.php'))));
     die;
 }
Пример #16
0
 /**
  * Determines whether the field should display both time and date or date only.
  *
  * Allowed field type: date.
  *
  * @throws RuntimeException
  * @return string 'date'|'date_and_time' (note that for 'date_and_time' the actual value stored is 'and_time',
  *     we're translating it to sound more sensible)
  * @since 1.9.1
  */
 public function get_datetime_option()
 {
     $this->check_allowed_types(Types_Field_Type_Definition_Factory::DATE);
     $value = wpcf_getnest($this->definition_array, array('data', 'date_and_time'));
     return 'and_time' == $value ? 'date_and_time' : 'date';
 }
Пример #17
0
 /**
  * Create an export object for this field definition, including checksums and annotations.
  * 
  * @return array
  * @since 2.1
  */
 public function get_export_object()
 {
     $data = $this->get_definition_array();
     // legacy filter
     $data = apply_filters('wpcf_export_field', $data);
     $ie_controller = Types_Import_Export::get_instance();
     $data = $this->add_checksum_to_export_object($data);
     $data = $ie_controller->annotate_object($data, $this->get_name(), $this->get_slug());
     // Export WPML TM setting for this field's translation, if available.
     $wpml_tm_settings = apply_filters('wpml_setting', null, 'translation-management');
     $custom_field_translation_setting = wpcf_getnest($wpml_tm_settings, array('custom_fields_translation', $this->get_meta_key()), null);
     if (null !== $custom_field_translation_setting) {
         $data[self::XML_KEY_WPML_ACTION] = $custom_field_translation_setting;
     }
     return $data;
 }