Example #1
0
function wpcf7_ajax_json_echo()
{
    global $wpcf7_contact_form;
    $echo = '';
    if (isset($_POST['_wpcf7'])) {
        $id = (int) $_POST['_wpcf7'];
        $unit_tag = $_POST['_wpcf7_unit_tag'];
        if ($wpcf7_contact_form = wpcf7_contact_form($id)) {
            $validation = $wpcf7_contact_form->validate();
            $items = array('mailSent' => false, 'into' => '#' . $unit_tag, 'captcha' => null);
            $items = apply_filters('wpcf7_ajax_json_echo', $items);
            if (!$validation['valid']) {
                // Validation error occured
                $invalids = array();
                foreach ($validation['reason'] as $name => $reason) {
                    $invalids[] = array('into' => 'span.wpcf7-form-control-wrap.' . $name, 'message' => $reason);
                }
                $items['message'] = $wpcf7_contact_form->message('validation_error');
                $items['invalids'] = $invalids;
            } elseif (!$wpcf7_contact_form->accepted()) {
                // Not accepted terms
                $items['message'] = $wpcf7_contact_form->message('accept_terms');
            } elseif ($wpcf7_contact_form->akismet()) {
                // Spam!
                $items['message'] = $wpcf7_contact_form->message('akismet_says_spam');
                $items['spam'] = true;
            } elseif ($wpcf7_contact_form->mail()) {
                $items['mailSent'] = true;
                $items['message'] = $wpcf7_contact_form->message('mail_sent_ok');
                $on_sent_ok = $wpcf7_contact_form->additional_setting('on_sent_ok', false);
                if (!empty($on_sent_ok)) {
                    $on_sent_ok = array_map('wpcf7_strip_quote', $on_sent_ok);
                } else {
                    $on_sent_ok = null;
                }
                $items['onSentOk'] = $on_sent_ok;
                do_action_ref_array('wpcf7_mail_sent', array(&$wpcf7_contact_form));
            } else {
                $items['message'] = $wpcf7_contact_form->message('mail_sent_ng');
            }
            // remove upload files
            foreach ((array) $wpcf7_contact_form->uploaded_files as $name => $path) {
                @unlink($path);
            }
            $wpcf7_contact_form = null;
        }
    }
    $echo = wpcf7_json($items);
    if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
        @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
        echo $echo;
    } else {
        @header('Content-Type: text/html; charset=' . get_option('blog_charset'));
        echo '<textarea>' . $echo . '</textarea>';
    }
}
Example #2
0
function wpcf7_json($items)
{
    if (is_array($items)) {
        if (empty($items)) {
            return 'null';
        }
        $keys = array_keys($items);
        $all_int = true;
        foreach ($keys as $key) {
            if (!is_int($key)) {
                $all_int = false;
                break;
            }
        }
        if ($all_int) {
            $children = array();
            foreach ($items as $item) {
                $children[] = wpcf7_json($item);
            }
            return '[' . join(', ', $children) . ']';
        } else {
            // Object
            $children = array();
            foreach ($items as $key => $item) {
                $key = esc_js((string) $key);
                if (preg_match('/[^a-zA-Z]/', $key)) {
                    $key = '"' . $key . '"';
                }
                $children[] = $key . ': ' . wpcf7_json($item);
            }
            return '{ ' . join(', ', $children) . ' }';
        }
    } elseif (is_numeric($items)) {
        return (string) $items;
    } elseif (is_bool($items)) {
        return $items ? '1' : '0';
    } elseif (is_null($items)) {
        return 'null';
    } else {
        return '"' . esc_js((string) $items) . '"';
    }
}