<?php

$context = array();
$context['theme'] = get_template_directory_uri();
$context['url'] = home_url();
if ($_POST) {
    $phone = $_POST['phone'];
    $code = $_POST['code'];
    $password = $_POST['password'];
    if (!$phone) {
        $context['phoneError'] = '请输入11位手机号';
    } else if (!$code) {
        $context['codeError'] = '请输入验证码';
    } else if (!$password) {
        $context['passwordError'] = '请输入密码';
    } else {
        $search_code = dao::search_tospur_verify($phone, $code);
        if ($code == $search_code) {
            $result = wp_create_user($phone, $password);
            if (is_integer($result)) {
                dao::update_tospur_verify($phone);
                $creds['user_login'] = $phone;
                $creds['user_password'] = $password;
                $creds['remember'] = true;
                echo '注册成功';
                $user = wp_signon($creds, false);
                if (!is_wp_error($user)) {
                    echo '自动登录，跳转页面';
                }
            } else {
                $context['phoneError'] = '抱歉，用户名已存在';
            }
        } else {
            $context['codeError'] = '请输入正确的验证码';
        }
    }
}
Timber::render('register.html', $context);

?>