public function save_ab_test() { // make sure they're logged in and own this quiz // returns current_user_id if valid $user_id = $this->validate_user(); if (isset($_POST['enp_quiz_nonce'])) { $posted_nonce = $_POST['enp_quiz_nonce']; } //Is it a POST request? if ($_SERVER['REQUEST_METHOD'] === 'POST') { //Validate the form key if (!isset($posted_nonce) || !self::$nonce->validate($posted_nonce)) { // Form key is invalid, // return them to the page (they're probably refreshing the page) self::$message['error'][] = 'AB Test was not saved'; return false; } } $params = $_POST; $params['ab_test_updated_by'] = $user_id; $save_ab_test = new Enp_quiz_Save_ab_test(); if ($params['enp-ab-test-submit'] === 'enp-ab-test-create') { $response = $save_ab_test->save($params); } elseif ($params['enp-ab-test-submit'] === 'delete-ab-test') { $response = $save_ab_test->delete($params); } else { self::$message['error'][] = 'We\'re not sure what you want to do. Please contact us and let us know how you got to this error message.'; } self::$message = $response['message']; if (defined('DOING_AJAX') && DOING_AJAX) { $json_response = $response; $json_response = json_encode($json_response); wp_send_json($json_response); // always end ajax with exit() exit; } elseif (empty(self::$message['error']) && $response['status'] === 'success' && $response['action'] === 'insert' && isset($response['ab_test_id'])) { // successful insert, so redirect them to the embed code section of the results page // set a messages array to pass to url on redirect $url_query = http_build_query(array('enp_messages' => self::$message, 'enp_user_action' => 'ab_test_created')); // they just created a new page (quiz) so we need to redirect them to it and post our messages wp_redirect(ENP_AB_RESULTS_URL . $response['ab_test_id'] . '/?' . $url_query); exit; } return $response; }