Esempio n. 1
0
/**
 * Form data for post edit page.
 *
 * @global object $wpdb
 *
 * @param type $field
 */
function wpcf_fields_file_meta_box_form($field)
{
    add_thickbox();
    $button_text = sprintf(__('Select %s', 'wpcf'), $field['type']);
    // Set ID
    $element_id = 'wpcf-fields-' . wpcf_unique_id(serialize(func_get_args()));
    $attachment_id = false;
    // Get attachment by guid
    global $wpdb;
    if (!empty($field['value'])) {
        $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND guid=%s", $field['value']));
    }
    // Set preview
    $preview = '';
    // TODO WPML move
    if (!wpcf_wpml_field_is_copied($field)) {
        if (!empty($attachment_id)) {
            $preview = wp_get_attachment_image($attachment_id, 'thumbnail');
        } else {
            // If external image set preview
            $file_path = parse_url($field['value']);
            if ($file_path && isset($file_path['path'])) {
                $file = pathinfo($file_path['path']);
            } else {
                $file = pathinfo($field['value']);
            }
            if (isset($file['extension']) && in_array(strtolower($file['extension']), array('jpg', 'jpeg', 'gif', 'png'))) {
                $preview = '<img alt="" src="' . $field['value'] . '" />';
            }
        }
    }
    // Set button
    // TODO WPML move
    if (!wpcf_wpml_field_is_copied($field)) {
        if (!empty($field['#attributes']['readonly']) || !empty($field['#attributes']['disabled'])) {
            $button = '';
        } else {
            $button = '<a href="javascript:void(0);"' . ' class="wpcf-fields-' . ($field['type'] == 'image' ? 'image' : 'file') . '-upload-link button-secondary"' . ' id="' . $element_id . '-upload" ' . "data-types='{\"type\":\"{$field['type']}\",\"id\":\"{$field['id']}\"}'>" . $button_text . '</a>';
        }
    } else {
        $button = '';
    }
    // Set form
    $form = array('#type' => 'textfield', '#id' => $element_id . '-upload-holder', '#name' => 'wpcf[' . $field['slug'] . ']', '#suffix' => '&nbsp;' . $button, '#after' => '<div id="' . $element_id . '-upload-holder-preview"' . ' class="wpcf-fields-file-preview">' . $preview . '</div>', '#attributes' => array('class' => 'wpcf-fields-file-textfield'));
    return $form;
}
Esempio n. 2
0
/**
 * Processes single field.
 *
 * Since Types 1.2 this function changed. It handles single form element.
 * Form element is already fetched, also meta values using class WPCF_Field.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @todo gradually remove usage of inherited fields
 * @todo Cleanup
 *
 * @staticvar array $repetitive_started
 * @param type $field_object
 * @return mixed boolean|array
 */
function wpcf_admin_post_process_field($field_object)
{
    /*
     * Since Types 1.2
     * All data we need is stored in global $wpcf
     */
    global $wpcf;
    $post = $wpcf->post;
    $field = (array) $field_object->cf;
    $context = $field_object->context;
    $invalid_fields = $wpcf->field->invalid_fields;
    if (!empty($field)) {
        /*
         * Set Unique ID
         */
        $field_id = 'wpcf-' . $field['type'] . '-' . $field['slug'] . '-' . wpcf_unique_id(serialize($field_object->__current_form_element));
        /*
         * Get inherited field
         *
         * TODO Deprecated
         *
         * Since Types 1.2 we encourage developers to completely define fields.
         */
        $inherited_field_data = false;
        if (isset($field_object->config->inherited_field_type)) {
            $_allowed = array('image' => 'file', 'numeric' => 'textfield', 'email' => 'textfield', 'phone' => 'textfield', 'url' => 'textfield');
            if (!array_key_exists($field_object->cf['type'], $_allowed)) {
                //                _deprecated_argument( 'inherited_field_type', '1.2',
                //                        'Since Types 1.2 we encourage developers to completely define fields' );
            }
            $inherited_field_data = wpcf_fields_type_action($field_object->config->inherited_field_type);
        }
        /*
         * CHECKPOINT
         * APPLY FILTERS
         *
         *
         * Moved to WPCF_Field
         * Field value should be already filtered
         *
         * Explanation:
         * When WPCF_Field::set() is called, all these properties are set.
         * WPCF_Field::$cf['value']
         * WPCF_Field::$__meta (single value from DB)
         * WPCF_Field::$meta (single or multiple values if single/repetitive)
         *
         * TODO Make sure value is already filtered and not overwritten
         */
        /*
         * Set generic values
         *
         * FUTURE BREAKPOINT
         * Since Types 1.2 we do not encourage relying on generic field data.
         * Only core fields should use this.
         *
         * TODO Open 3rd party fields dir
         */
        $_element = array('#type' => isset($field_object->config->inherited_field_type) ? $field_object->config->inherited_field_type : $field['type'], '#id' => $field_id, '#title' => $field['name'], '#name' => 'wpcf[' . $field['slug'] . ']', '#value' => isset($field['value']) ? $field['value'] : '', 'wpcf-id' => $field['id'], 'wpcf-slug' => $field['slug'], 'wpcf-type' => $field['type']);
        /*
         * TODO Add explanation about creating duplicated fields
         *
         * NOT USED YET
         *
         * Explain users that fields are added if slug is changed
         */
        wpcf_admin_add_js_settings('wpcfFieldNewInstanceWarning', __('If you change slug, new field will be created', 'wpcf'));
        /*
         * Merge with default element
         *
         * Deprecated from Types 1.2
         * Only core fields use this.
         */
        $element = array_merge($_element, $field_object->__current_form_element);
        /*
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         * TODO From this point code should be simplified.
         */
        if (isset($field['description_extra'])) {
            $element['#description'] .= wpautop($field['description_extra']);
        }
        // Set atributes #1
        if (isset($field['disable'])) {
            $field['#disable'] = $field['disable'];
        }
        if (!empty($field['disable'])) {
            $field['#attributes']['disabled'] = 'disabled';
        }
        if (!empty($field['readonly'])) {
            $field['#attributes']['readonly'] = 'readonly';
        }
        // Format description
        if (!empty($element['#description'])) {
            $element['#description'] = wpautop($element['#description']);
        }
        // Set validation element
        if (isset($field['data']['validate'])) {
            /*
             *
             *
             * TODO First two check are not needed anymore
             */
            // If array has more than one field - see which one is marked
            if ($field_object->__multiple && isset($element['#_validate_this'])) {
                $element['#validate'] = $field['data']['validate'];
            } else {
                if (!$field_object->__multiple) {
                    $element['#validate'] = $field['data']['validate'];
                }
            }
        }
        // Set atributes #2 (override)
        if (isset($field['disable'])) {
            $element['#disable'] = $field['disable'];
        }
        if (!empty($field['disable'])) {
            $element['#attributes']['disabled'] = 'disabled';
        }
        if (!empty($field['readonly'])) {
            $element['#attributes']['readonly'] = 'readonly';
            if (!empty($element['#options'])) {
                foreach ($element['#options'] as $key => $option) {
                    if (!is_array($option)) {
                        $element['#options'][$key] = array('#title' => $key, '#value' => $option);
                    }
                    $element['#options'][$key]['#attributes']['readonly'] = 'readonly';
                    if ($element['#type'] == 'select') {
                        $element['#options'][$key]['#attributes']['disabled'] = 'disabled';
                    }
                }
            }
            if ($element['#type'] == 'select') {
                $element['#attributes']['disabled'] = 'disabled';
            }
        }
        // Check if it was invalid on submit and add error message
        if ($post && !empty($invalid_fields)) {
            if (isset($invalid_fields[$element['#id']]['#error'])) {
                $element['#error'] = $invalid_fields[$element['#id']]['#error'];
            }
        }
        // TODO WPML move Set WPML locked icon
        if (wpcf_wpml_field_is_copied($field)) {
            $element['#title'] .= '<img src="' . WPCF_EMBEDDED_RES_RELPATH . '/images/locked.png" alt="' . __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf') . '" title="' . __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf') . '" style="position:relative;left:2px;top:2px;" />';
        }
        // Add repetitive class
        // TODO WPML move
        if (types_is_repetitive($field) && $context != 'post_relationship' && wpcf_wpml_field_is_copied($field)) {
            if (!empty($element['#options']) && $element['#type'] != 'select') {
                foreach ($element['#options'] as $temp_key => $temp_value) {
                    $element['#options'][$temp_key]['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
                }
            } else {
                $element['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
            }
            /*
            *
            *
            * Since Types 1.2 we allow same field values
            *
            * TODO Remove
            *
            * wpcf_admin_add_js_settings('wpcfFormRepetitiveUniqueValuesCheckText',
             '\'' . __('Warning: same values set', 'wpcf') . '\'');
            */
        }
        // Set read-only if copied by WPML
        // TODO WPML Move this to separate WPML code and use only hooks 1.1.5
        if (wpcf_wpml_field_is_copied($field)) {
            if (isset($element['#options'])) {
                foreach ($element['#options'] as $temp_key => $temp_value) {
                    if (isset($temp_value['#attributes'])) {
                        $element['#options'][$temp_key]['#attributes']['readonly'] = 'readonly';
                    } else {
                        $element['#options'][$temp_key]['#attributes'] = array('readonly' => 'readonly');
                    }
                }
            }
            if ($field['type'] == 'select') {
                if (isset($element['#attributes'])) {
                    $element['#attributes']['disabled'] = 'disabled';
                } else {
                    $element['#attributes'] = array('disabled' => 'disabled');
                }
            } else {
                if (isset($element['#attributes'])) {
                    $element['#attributes']['readonly'] = 'readonly';
                } else {
                    $element['#attributes'] = array('readonly' => 'readonly');
                }
            }
        }
        // Final filter for disabled if readonly
        if (isset($element['#attributes']['readonly']) || isset($element['#attributes']['disabled'])) {
            if (types_is_repetitive($field)) {
                $element['#name'] .= '[]';
            }
            if ($field['type'] == 'checkboxes') {
                if (isset($element['#options'])) {
                    foreach ($element['#options'] as $temp_key => $temp_value) {
                        $value = isset($temp_value['#default_value']) ? $temp_value['#default_value'] : $temp_value['#value'];
                        $_after = "<input type=\"hidden\" name=\"{$temp_value['#name']}\" value=\"{$value}\" />";
                        $temp_value['#after'] = isset($temp_value['#after']) ? $temp_value['#after'] . $_after : $_after;
                        $temp_value['#name'] = "wpcf-disabled[{$field['id']}_{$temp_value['#id']}]";
                        $temp_value['#attributes']['disabled'] = 'disabled';
                        $element['#options'][$temp_key] = $temp_value;
                    }
                }
            } else {
                if (in_array($element['#type'], array('checkbox', 'checkboxes', 'radios'))) {
                    if (isset($element['#options'])) {
                        foreach ($element['#options'] as $temp_key => $temp_value) {
                            $element['#options'][$temp_key]['#attributes']['disabled'] = 'disabled';
                        }
                    }
                    $value = isset($element['#default_value']) ? $element['#default_value'] : $element['#value'];
                    $_after = "<input type=\"hidden\" name=\"{$element['#name']}\" value=\"{$value}\" />";
                    $element['#after'] = isset($element['#after']) ? $element['#after'] . $_after : $_after;
                    $element['#attributes']['disabled'] = 'disabled';
                    $element['#name'] = "wpcf-disabled[{$field['id']}_{$element['#id']}]";
                } else {
                    $element['#attributes']['disabled'] = 'disabled';
                    if (is_array($element['#value'])) {
                        //$field['type'] == 'skype' ) {
                        $element['#value'] = array_shift($element['#value']);
                    }
                    $value = htmlentities($element['#value']);
                    $_after = "<input type=\"hidden\" name=\"{$element['#name']}\" value=\"{$value}\" />";
                    $element['#after'] = isset($element['#after']) ? $element['#after'] . $_after : $_after;
                    $element['#name'] = "wpcf-disabled[{$field['id']}_{$element['#id']}]";
                }
            }
        }
        return array('field' => $field, 'element' => $element);
    }
    return false;
}
Esempio n. 3
0
/**
 * Processes repetitive Skype fields.
 *
 * Each form element is sent separately.
 * Determine which is which and process it.
 *
 * @staticvar array $repetitive_started
 * @staticvar array $repetitive_index
 * @param type $post
 * @param string $field
 * @param type $skype_element
 * @return string
 */
function wpcf_field_skype_repetitive($element, $post, $field, $array_key)
{
    global $wpcf;
    if ($field['type'] != 'skype') {
        return $element;
    }
    switch ($array_key) {
        case 'skypename':
            // TODO WPML move
            if (wpcf_wpml_field_is_copied($field)) {
                $element['#after'] .= '<input type="hidden" name="wpcf_repetitive_copy[' . $field['id'] . '][' . $wpcf->repeater->index . ']" value="1" />';
            }
            /*
             *
             * If added via AJAX set value
             */
            if (defined('DOING_AJAX')) {
                $field['value'] = '__wpcf_repetitive_new_field';
                $element['#value'] = '';
            }
            break;
        default:
            break;
    }
    return $element;
}
Esempio n. 4
0
 /**
  * Renders element.
  *
  * Depending on element type, it calls class methods.
  *
  * @param array $element
  * @return HTML formatted output
  */
 public function renderElement($element)
 {
     $method = $element['#type'];
     if (!isset($element['#name']) && !in_array($element['#type'], array('markup', 'checkboxes'))) {
         if (!isset($element['#attributes']['name'])) {
             return '#name or #attributes[\'name\'] required!';
         } else {
             $element['#name'] = $element['#attributes']['name'];
         }
     }
     if (is_callable(array($this, $method))) {
         $custom_field_title = '';
         if (isset($element['#title']) && !empty($element['#title'])) {
             $custom_field_title = $element['#title'];
         }
         if (empty($custom_field_title) && isset($element['#name']) && !empty($element['#name'])) {
             $custom_field_title = $element['#name'];
         }
         if (!isset($element['#id'])) {
             if (isset($element['#attributes']['id'])) {
                 $element['#id'] = $element['#attributes']['id'];
             } else {
                 $_id = isset($this->_id) ? $this->_id . '-' : '';
                 $element['#id'] = "{$_id}{$element['#type']}-" . $this->_count($element['#type']) . '-' . time();
             }
         }
         if (isset($this->_errors[$element['#id']])) {
             $element['#error'] = $this->_errors[$element['#id']];
         }
         // Add JS validation
         if (!empty($element['#validate'])) {
             if (isset($element['#validate']['required']) && !empty($element['#title'])) {
                 // Asterisk
                 $element['#title'] .= '&#42;';
             }
             $element['#attributes']['data-wpt-validate'] = esc_html(self::json_encode(apply_filters('wptoolset_forms_field_js_validation_data_' . $this->_id, $element['#validate'])));
             $element['#attributes']['data-wpt-field-title'] = esc_js($custom_field_title);
         }
         if ($element['#type'] == 'radios' && !empty($element['#options'])) {
             foreach ($element['#options'] as &$option) {
                 if (!empty($option['#validate'])) {
                     $option['#attributes']['data-wpt-validate'] = esc_html(self::json_encode(apply_filters('wptoolset_forms_field_js_validation_data_' . $this->_id, $option['#validate'])));
                     $option['#attributes']['data-wpt-field-title'] = esc_js($custom_field_title);
                 }
             }
         }
         /**
          * WPML - lock CF is has option "copy from original".
          */
         if (is_admin() && function_exists('wpcf_wpml_field_is_copied') && wpcf_wpml_field_is_copied($element)) {
             $element['#title'] .= sprintf('<img src="%s/images/locked.png" alt="%s" title="%s" style="position:relative;left:2px;top:2px;" />', WPCF_EMBEDDED_RES_RELPATH, __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf'), __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf'));
             $element['#attributes']['readonly'] = true;
         }
         return $this->{$method}($element);
     }
 }
Esempio n. 5
0
/**
 * From data for post edit page.
 * 
 * @param type $field 
 * @param type $data
 * @param type $field_object Field instance 
 */
function wpcf_fields_date_meta_box_form($field, $field_object = null)
{
    /*
     * Added extra fields 'hour' and 'minute'.
     * 
     * If value is not array it is assumed that DB entry is timestamp()
     * and data is converted to array.
     */
    $value = $field['value'] = wpcf_fields_date_value_get_filter($field['value'], $field_object);
    // TODO WPML Set disable_in_form or similar to true, use hook for WPML
    if (wpcf_wpml_field_is_copied($field)) {
        $attributes = array('style' => 'width:150px;');
    } else {
        $attributes = array('class' => 'wpcf-datepicker', 'style' => 'width:150px;');
    }
    /*
     * 
     * Do not forget to trigger datepicker script
     * Only trigger on AJAX call (inserting new)
     */
    $js_trigger = defined('DOING_AJAX') ? '<script type="text/javascript">wpcfFieldsDateInit(\'\');</script>' : '';
    /*
     * 
     * 
     * Set Form
     */
    $unique_id = wpcf_unique_id(serialize($field));
    $form = array();
    $form[$unique_id . '-datepicker'] = array('#type' => 'textfield', '#title' => '&nbsp;' . $field['name'], '#attributes' => $attributes, '#name' => 'wpcf[' . $field['slug'] . '][datepicker]', '#id' => 'wpcf-date-' . $field['slug'] . '-datepicker-' . $unique_id, '#value' => $value['datepicker'], '#after' => '' . $js_trigger, '#_validate_this' => true);
    // Add warning about supported timestamp
    if (!fields_date_timestamp_neg_supported()) {
        $_visible = !empty($value['datepicker']) && intval($value['timestamp']) < 0 ? '' : ' style="display:none;"';
        $form[$unique_id . '-warning'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-form-error"' . $_visible . '><p>' . __('Please enter a date after 1 January 1970', 'wpcf') . '</p></div>');
    }
    /*
     * 
     * If set 'date_and_time' add time
     */
    if (!empty($field['data']['date_and_time']) && $field['data']['date_and_time'] == 'and_time') {
        // Set parent CSS inline
        $form[$unique_id . '-datepicker']['#inline'] = true;
        $hours = 24;
        $minutes = 60;
        $options = array();
        // Hour
        for ($index = 0; $index < $hours; $index++) {
            $prefix = $index < 10 ? '0' : '';
            $options[$index] = array('#title' => $prefix . strval($index), '#value' => $index);
        }
        $form[$unique_id . 'time_hour'] = array('#type' => 'select', '#title' => __('Hour', 'wpcf'), '#inline' => true, '#before' => '<br />', '#after' => '&nbsp;&nbsp;', '#options' => $options, '#default_value' => $value['hour'], '#name' => 'wpcf[' . $field['slug'] . '][hour]', '#id' => 'wpcf-date-' . $field['slug'] . '-select-hour-' . $unique_id, '#inline' => true);
        // Minutes
        for ($index = 1; $index < $minutes; $index++) {
            $prefix = $index < 10 ? '0' : '';
            $options[$index] = array('#title' => $prefix . strval($index), '#value' => $index);
        }
        $form[$unique_id . 'time_minute'] = array('#type' => 'select', '#title' => __('Minute', 'wpcf'), '#after' => '<br /><br />', '#inline' => true, '#options' => $options, '#default_value' => $value['minute'], '#name' => 'wpcf[' . $field['slug'] . '][minute]', '#id' => 'wpcf-date-' . $field['slug'] . '-minute-' . $unique_id);
    }
    return $form;
}
/**
 * Returns HTML formatted drag button.
 * 
 * @param type $field
 * @param type $post
 * @return string 
 */
function wpcf_repetitive_drag_button($field, $post)
{
    // TODO WPML move
    if (wpcf_wpml_field_is_copied($field)) {
        return '';
    }
    return '<div class="wpcf-repetitive-drag">&nbsp;</div>';
}