function wpsc_validate_form($form_args, &$validated_array = false)
{
    if (!is_array($validated_array)) {
        $validated_array =& $_POST;
    }
    $error = new WP_Error();
    $a =& $error;
    if (!isset($form_args['fields'])) {
        $valid = null;
    } else {
        $valid = true;
    }
    $form = $form_args['fields'];
    foreach ($form as $props) {
        // Handle custom fields.
        if (!isset($props['fields'])) {
            $props['fields'] = $props;
        }
        foreach ($props['fields'] as $prop) {
            if (empty($prop['rules'])) {
                continue;
            }
            $prop = _wpsc_populate_field_default_args($prop);
            $field = $prop['name'];
            $rules = $prop['rules'];
            if (is_string($rules)) {
                $rules = explode('|', $rules);
            }
            $value = wpsc_submitted_value($field, '', $validated_array);
            foreach ($rules as $rule) {
                if (function_exists($rule)) {
                    $value = call_user_func($rule, $value);
                    continue;
                }
                if (preg_match('/([^\\[]+)\\[([^\\]]+)\\]/', $rule, $matches)) {
                    $rule = $matches[1];
                    $matched_field = $matches[2];
                    $matched_value = wpsc_submitted_value($matched_field, null, $validated_array);
                    $matched_props = isset($form[$matched_field]) ? $form[$matched_field] : array();
                    $error = apply_filters("wpsc_validation_rule_{$rule}", $error, $value, $field, $prop, $matched_field, $matched_value, $matched_props);
                } else {
                    $error = apply_filters("wpsc_validation_rule_{$rule}", $error, $value, $field, $prop);
                }
                if (count($error->get_error_codes())) {
                    break;
                }
            }
        }
        _wpsc_set_submitted_value($field, $value, $validated_array);
    }
    if (count($error->get_error_messages())) {
        $valid = $error;
    }
    return apply_filters('wpsc_validate_form', $valid);
}
Exemple #2
0
function _wpsc_convert_checkout_shipping_form_args($args)
{
    global $wpsc_shipping_modules;
    $calculator = WPSC_Shipping_Calculator::get_instance();
    $submitted_value = wpsc_submitted_value('wpsc_shipping_option');
    $active_shipping_id = $calculator->active_shipping_id;
    foreach ($calculator->sorted_quotes as $module_name => $quotes) {
        $radios = array('type' => 'radios', 'title' => $wpsc_shipping_modules[$module_name]->name, 'options' => array(), 'name' => 'wpsc_shipping_option');
        foreach ($quotes as $option => $cost) {
            $id = $calculator->ids[$module_name][$option];
            $checked = empty($submitted_value) ? $active_shipping_id == $id : $submitted_value == $id;
            $radios['options'][] = array('title' => $option, 'value' => $id, 'description' => wpsc_format_currency($cost), 'checked' => $checked);
        }
        $args['fields'][] = $radios;
    }
    // automatically select the cheapest option by default
    if (empty($active_shipping_id) && empty($submitted_value)) {
        $args['fields'][0]['options'][0]['checked'] = true;
    }
    return $args;
}