/**
  * 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 wpml($form_data)
 {
     $form = array();
     if (true && isset($form_data['meta_type']) && isset($form_data['type']) && isset($form_data['id']) && '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($form_data['type'], $translatable) ? 'translate' : 'copy';
         $wpml_prefs = wpml_cf_translation_preferences($form_data['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-' . $form_data['id'] . '-wpml-preferences'] = array('#title' => __('Translation preferences', 'wpcf'), '#type' => 'markup', '#markup' => $wpml_prefs, '#pattern' => '<tr><td><LABEL></td><td><ERROR><BEFORE><ELEMENT><AFTER></td></tr>');
     }
     return $form;
 }
Example #2
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)) {
        //        require_once $filename;
        $form = array();
        // Set right ID if existing field
        if (isset($form_data['submitted_key'])) {
            $id = $form_data['submitted_key'];
        } else {
            $id = $type . '-' . rand();
        }
        // 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']);
        $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', 'style' => 'width:100%;margin:10px 0 10px 0;'), '#validate' => array('required' => array('value' => true)), '#inline' => true, '#value' => __('Enter field name', 'wpcf'));
        if (empty($form_data['name'])) {
            $form_field['name']['#attributes']['onclick'] = 'if (jQuery(this).val() == \'' . __('Enter field name', 'wpcf') . '\') { jQuery(this).val(\'\'); }';
            $form_field['name']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Enter field name', 'wpcf') . '\') }';
        }
        $form_field['slug'] = array('#type' => 'textfield', '#name' => 'slug', '#attributes' => array('style' => 'width:100%;margin:0 0 10px 0;'), '#validate' => array('nospecialchars' => array('value' => true)), '#inline' => true, '#value' => __('Enter field slug', 'wpcf'));
        if (empty($form_data['slug'])) {
            $form_field['slug']['#attributes']['onclick'] = 'if (jQuery(this).val() == \'' . __('Enter field slug', 'wpcf') . '\') { jQuery(this).val(\'\'); }';
            $form_field['slug']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Enter field slug', 'wpcf') . '\') }';
        }
        // 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;'), '#inline' => true, '#value' => __('Describe this field', 'wpcf'));
        if (empty($form_data['description'])) {
            $form_field['description']['#attributes']['onfocus'] = 'if (jQuery(this).val() == \'' . __('Describe this field', 'wpcf') . '\') { jQuery(this).val(\'\'); }';
            $form_field['description']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Describe this field', 'wpcf') . '\') }';
        }
        // 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;
            $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);
            $form['wpcf-' . $id]['wpml-preferences']['form'] = array('#type' => 'markup', '#markup' => wpml_cf_translation_preferences($id, $custom_field, 'wpcf', false, $action));
        }
        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');
        }
        return $form;
    }
    return false;
}
/**
 * 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;
}