/**
  * Deletes all AB Tests that this quiz is a part of
  * @param $quiz_id (string/int) of the id of the quiz
  * @return $ab_test_response (array) from deleting all the ab tests (which ids were deleted, if it was successful, etc)
  */
 protected function delete_quiz_ab_tests($quiz_id, $quiz_owner)
 {
     // get all the ab test ids that this quiz is a part of
     $ab_test_ids = $this->get_all_quiz_ab_tests($quiz_id);
     // if there are any, loop through them and delete em!
     $ab_test_response = array();
     if (!empty($ab_test_ids)) {
         // open the ab test save class
         $save_ab_test = new Enp_quiz_Save_ab_test();
         foreach ($ab_test_ids as $ab_test_id) {
             $ab_test_params = array('ab_test_id' => $ab_test_id, 'ab_test_updated_by' => $quiz_owner);
             $ab_test_response[] = $save_ab_test->delete($ab_test_params);
         }
     }
     return $ab_test_response;
 }
 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;
 }