/** * Check if field is repetitive * @param type $type * @return type */ function wpcf_admin_is_repetitive($field) { if (!isset($field['data']['repetitive']) || !isset($field['type'])) { return false; } $check = intval($field['data']['repetitive']); return !empty($check) && wpcf_admin_can_be_repetitive($field['type']); }
/** * 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&wpcf_warning=' . __('Are you sure?', 'wpcf') . '&action=wpcf_ajax&wpcf_action=remove_field_from_group' . '&group_id=' . intval($form_data['group_id']) . '&field_id=' . $form_data['id']) . '&_wpnonce=' . wp_create_nonce('remove_field_from_group') : admin_url('admin-ajax.php?' . 'wpcf_ajax_callback=wpcfFieldsFormDeleteElement&wpcf_warning=' . __('Are you sure?', 'wpcf') . '&action=wpcf_ajax&wpcf_action=remove_field_from_group') . '&_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; }
/** * @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; }
/** * 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); }