/** * Verify the visitor if the form was submitted. * * @since 0.1.0 * * @return void */ public function verify() { if (!isset($_POST['av-nonce']) || !wp_verify_nonce($_POST['av-nonce'], 'verify-age')) { return; } $redirect_url = remove_query_arg(array('age-verified', 'verify-error'), wp_get_referer()); $is_verified = false; $error = 1; // Catch-all in case something goes wrong $input_type = av_get_input_type(); switch ($input_type) { case 'checkbox': if (isset($_POST['av_verify_confirm']) && (int) $_POST['av_verify_confirm'] == 1) { $is_verified = true; } else { $error = 2; } // Didn't check the box break; default: if (checkdate((int) $_POST['av_verify_m'], (int) $_POST['av_verify_d'], (int) $_POST['av_verify_y'])) { $age = av_get_visitor_age($_POST['av_verify_y'], $_POST['av_verify_m'], $_POST['av_verify_d']); if ($age >= av_get_minimum_age()) { $is_verified = true; } else { $error = 3; } // Not old enough } else { $error = 4; // Invalid date } break; } $is_verified = apply_filters('av_passed_verify', $is_verified); if ($is_verified == true) { do_action('av_was_verified'); if (isset($_POST['av_verify_remember'])) { $cookie_duration = time() + av_get_cookie_duration() * 60; } else { $cookie_duration = 0; } setcookie('age-verified', 1, $cookie_duration, COOKIEPATH, COOKIE_DOMAIN, false); wp_redirect(esc_url_raw($redirect_url) . '?age-verified=' . wp_create_nonce('age-verified')); exit; } else { do_action('av_was_not_verified'); wp_redirect(esc_url_raw(add_query_arg('verify-error', $error, $redirect_url))); exit; } }
/** * Returns the all-important verification form. * You can filter this if you like. * * @since 0.1 * @return string */ function av_get_verify_form() { $input_type = av_get_input_type(); $submit_button_label = apply_filters('av_form_submit_label', __('Enter Site »', 'age-verify')); $form = ''; $form .= '<form id="av_verify_form" action="' . esc_url(home_url('/')) . '" method="post">'; /* Parse the errors, if any */ $error = isset($_GET['verify-error']) ? $_GET['verify-error'] : false; if ($error) { // Catch-all error $error_string = apply_filters('av_error_text_general', __('Sorry, something must have gone wrong. Please try again', 'age-verify')); // Visitor didn't check the box (only for the simple checkbox form) if ($error == 2) { $error_string = apply_filters('av_error_text_not_checked', __('Check the box to confirm your age before continuing', 'age-verify')); } // Visitor isn't old enough if ($error == 3) { $error_string = apply_filters('av_error_text_too_young', __('Sorry, it doesn\'t look like you\'re old enough', 'age-verify')); } // Visitor entered an invalid date if ($error == 4) { $error_string = apply_filters('av_error_text_bad_date', __('Please enter a valid date', 'age-verify')); } $form .= '<p class="error">' . esc_html($error_string) . '</p>'; } do_action('av_form_before_inputs'); // Add a sweet nonce. So sweet. $form .= wp_nonce_field('verify-age', 'av-nonce'); switch ($input_type) { // If set to date dropdowns case 'dropdowns': $form .= '<p><select name="av_verify_m" id="av_verify_m">'; foreach (range(1, 12) as $month) { $month_name = date('F', mktime(0, 0, 0, $month, 1)); $form .= '<option value="' . $month . '">' . $month_name . '</option>'; } $form .= '</select> - <select name="av_verify_d" id="av_verify_d">'; foreach (range(1, 31) as $day) { $form .= '<option value="' . $day . '">' . esc_html(zeroise($day, 2)) . '</option>'; } $form .= '</select> - <select name="av_verify_y" id="av_verify_y">'; foreach (range(1910, date('Y')) as $year) { $selected = $year == date('Y') ? 'selected="selected"' : ''; $form .= '<option value="' . $year . '" ' . $selected . '>' . $year . '</option>'; } $form .= '</select></p>'; break; // If set to date inputs // If set to date inputs case 'inputs': $form .= '<p><input type="text" name="av_verify_m" id="av_verify_m" maxlength="2" value="" placeholder="MM" /> - <input type="text" name="av_verify_d" id="av_verify_d" maxlength="2" value="" placeholder="DD" /> - <input type="text" name="av_verify_y" id="av_verify_y" maxlength="4" value="" placeholder="YYYY" /></p>'; break; // If just a simple checkbox // If just a simple checkbox case 'checkbox': $form .= '<p><label for="av_verify_confirm"><input type="checkbox" name="av_verify_confirm" id="av_verify_confirm" value="1" /> '; $form .= esc_html(sprintf(apply_filters('av_confirm_text', __('I am at least %s years old', 'age-verify')), av_get_minimum_age())) . '</label></p>'; break; } do_action('av_form_after_inputs'); $form .= '<p class="submit"><label for="av_verify_remember"><input type="checkbox" name="av_verify_remember" id="av_verify_remember" value="1" /> ' . esc_html__('Remember me', 'age-verify') . '</label> '; $form .= '<input type="submit" name="av_verify" id="av_verify" value="' . esc_attr($submit_button_label) . '" /></p>'; $form .= '</form>'; return apply_filters('av_verify_form', $form); }