function survey_submit_question_ajax_callback() { check_ajax_referer('survey_submit_question_nonce', 'security'); //Make sure they're logged in with the appropriate permissions. if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } //Set up the basic structure for the question array. $question = array('qtype' => '', 'qtext' => '', 'answers' => array()); //This array will keep track of all of the answer ids being edited. $edit = array(); //Fill in the array properly using the posted data. foreach ($_POST['question'] as $posted) { //If the name is answer then it's a brand new answer and not being edited. if ($posted['name'] == 'answer') { $question['answers'][] = $posted['value']; } elseif (strstr($posted['name'], '-answer') !== FALSE) { $answer_id = strstr($posted['name'], '-answer', TRUE); $question['answers'][$answer_id] = $posted['value']; $edit[] = $answer_id; } else { $question[$posted['name']] = $posted['value']; } } //If the question is being edited, then this will use the id of that question to create the qobject. if (isset($question['survey_edit'])) { $qobject = new question(intval($question['survey_edit'])); $qobject->edit_question($question['qtext']); $qobject->edit_type($question['qtype']); $qobject->edit_dependency($question['depquestion'], $question['depanswer']); } else { $qobject = new question(FALSE, $question['qtype'], $question['qtext'], $question['depquestion'], $question['depanswer']); } switch ($question['qtype']) { case question::truefalse: case question::shortanswer: case question::longanswer: //These question types don't have answers. break; default: foreach ($question['answers'] as $answer_id => $answer) { //If the answer id is in the array, then it must be edited, otherwise it's a new answer. if (in_array($answer_id, $edit)) { $qobject->edit_answer($answer_id, $answer); } else { $qobject->add_answer($answer); } } } //Don't add a new question to the survey list if it's being edited. if (!isset($question['survey_edit'])) { $survey = new survey(intval($_POST['survey'])); $survey->add_qobject($qobject); } echo "Success!"; die; // this is required to return a proper result }