<?php

require_once('const.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['error'] = '请输入11位手机号';
    } else if (!$code) {
        $context['error'] = '请输入验证码';
    } else if (!$password) {
        $context['error'] = '请输入密码';
    } else {
        $search_code = TospurDao::search_tospur_verify($phone, $code);
        if ($code == $search_code) {
            $result = wp_create_user($phone, $password);
            if (is_integer($result)) {
                TospurDao::update_tospur_verify($phone);
                $creds['user_login'] = $phone;
                $creds['user_password'] = $password;
                $creds['remember'] = true;
                $user = wp_signon($creds, false);
                if (!is_wp_error($user)) {
                    wp_redirect($const_my_page);
                }
            } else {
                $context['error'] = '抱歉，用户名已存在';
            }
        } else {
            $context['error'] = '验证码错误';
        }
    }
}
Timber::render('register.html', $context);

?>