Пример #1
0
function task_change_stage()
{
    global $wpdb;
    $post_id = $_POST['post_id'];
    // Post id posted from ajax function
    $user_id = $_POST['user_id'];
    // User id posted from ajax function
    $status = (int) $_POST['status'];
    // Task's status posted from ajax function
    $page_id = $_POST['page_id'];
    // Page id posted from ajax function
    $admin_name = $_POST['admin_name'];
    $undo = $_POST['undo'];
    // Boolean which determines if the button clicked is an undo button or not (True or False)
    $pass = $_POST['pass'];
    // Contains the user-entered admin password
    $repeat_button = $_POST['repeat'];
    // Boolean which determines if the task is repeatable or not (True or False)
    $update_percent = $_POST['update_percent'];
    // Float which is used to modify values saved to database
    $chain_name = $_POST['chain_name'];
    // String which is used to display next task in a quest chain
    $next_post_id_in_chain = $_POST['next_post_id_in_chain'];
    // Integer which is used to display next task in a quest chain
    $last_in_chain = $_POST['last_in_chain'];
    // Boolean which determines if the current quest is last in chain
    $go_table_ind = $wpdb->prefix . 'go';
    $task_count = $wpdb->get_var("SELECT `count` FROM " . $go_table_ind . " WHERE post_id = {$post_id} AND uid = {$user_id}");
    $custom_fields = get_post_custom($post_id);
    // Just gathering some data about this task with its post id
    $rewards = unserialize($custom_fields['go_presets'][0]);
    // Array of rewards
    $mastery_active = !$custom_fields['go_mta_task_mastery'][0];
    // whether or not the mastery stage is active
    $repeat = $custom_fields['go_mta_task_repeat'][0];
    // Whether or not you can repeat the task
    $e_admin_lock = unserialize($custom_fields['go_mta_encounter_admin_lock'][0]);
    $e_is_locked = $e_admin_lock[0];
    if ($e_is_locked === 'true') {
        $e_pass_lock = $e_admin_lock[1];
    }
    $a_admin_lock = unserialize($custom_fields['go_mta_accept_admin_lock'][0]);
    $a_is_locked = $a_admin_lock[0];
    if ($a_is_locked === 'true') {
        $a_pass_lock = $a_admin_lock[1];
    }
    $c_admin_lock = unserialize($custom_fields['go_mta_completion_admin_lock'][0]);
    $c_is_locked = $c_admin_lock[0];
    if ($c_is_locked === 'true') {
        $c_pass_lock = $c_admin_lock[1];
    }
    $m_admin_lock = unserialize($custom_fields['go_mta_mastery_admin_lock'][0]);
    $m_is_locked = $m_admin_lock[0];
    if ($m_is_locked === 'true') {
        $m_pass_lock = $m_admin_lock[1];
    }
    $r_admin_lock = unserialize($custom_fields['go_mta_repeat_admin_lock'][0]);
    $r_is_locked = $r_admin_lock[0];
    if ($r_is_locked === 'true') {
        $r_pass_lock = $r_admin_lock[1];
    }
    if (!empty($pass)) {
        if ($status == 4) {
            $temp_status = $status;
        } else {
            $temp_status = $status - 1;
        }
        switch ($temp_status) {
            case 1:
                $pass_lock = $e_pass_lock;
                break;
            case 2:
                $pass_lock = $a_pass_lock;
                break;
            case 3:
                $pass_lock = $c_pass_lock;
                break;
            case 4:
                if ($repeat === 'on') {
                    $pass_lock = $r_pass_lock;
                } else {
                    $pass_lock = $m_pass_lock;
                }
                break;
        }
        if (!empty($pass_lock) && $pass !== $pass_lock) {
            echo 0;
            die;
        }
    }
    $test_e_active = $custom_fields['go_mta_test_encounter_lock'][0];
    $test_a_active = $custom_fields['go_mta_test_accept_lock'][0];
    $test_c_active = $custom_fields['go_mta_test_completion_lock'][0];
    if ($test_e_active) {
        $test_e_returns = $custom_fields['go_mta_test_encounter_lock_loot'][0];
        $test_e_array = $custom_fields['go_mta_test_lock_encounter'][0];
        $test_e_uns = unserialize($test_e_array);
        $test_e_num = $test_e_uns[3];
        $test_e_all_questions = $test_e_uns[0];
        $test_e_all_types = $test_e_uns[2];
        $test_e_all_inputs = $test_e_uns[1];
        $test_e_all_input_num = $test_e_uns[4];
        $test_e_all_answers = array();
        $test_e_all_keys = array();
        for ($i = 0; $i < count($test_e_all_inputs); $i++) {
            if (!empty($test_e_all_inputs[$i][0])) {
                $answer_e_temp = implode("###", $test_e_all_inputs[$i][0]);
                $test_e_all_answers[] = $answer_e_temp;
            }
            if (!empty($test_e_all_inputs[$i][1])) {
                $key_e_temp = implode("###", $test_e_all_inputs[$i][1]);
                $test_e_all_keys[] = $key_e_temp;
            }
        }
    }
    $encounter_upload = $custom_fields['go_mta_encounter_upload'][0];
    if ($test_a_active) {
        $test_a_returns = $custom_fields['go_mta_test_accept_lock_loot'][0];
        $test_a_array = $custom_fields['go_mta_test_lock_accept'][0];
        $test_a_uns = unserialize($test_a_array);
        $test_a_num = $test_a_uns[3];
        $test_a_all_questions = $test_a_uns[0];
        $test_a_all_types = $test_a_uns[2];
        $test_a_all_inputs = $test_a_uns[1];
        $test_a_all_input_num = $test_a_uns[4];
        $test_a_all_answers = array();
        $test_a_all_keys = array();
        for ($i = 0; $i < count($test_a_all_inputs); $i++) {
            if (!empty($test_a_all_inputs[$i][0])) {
                $answer_a_temp = implode("###", $test_a_all_inputs[$i][0]);
                $test_a_all_answers[] = $answer_a_temp;
            }
            if (!empty($test_a_all_inputs[$i][1])) {
                $key_a_temp = implode("###", $test_a_all_inputs[$i][1]);
                $test_a_all_keys[] = $key_a_temp;
            }
        }
    }
    $accept_upload = $custom_fields['go_mta_accept_upload'][0];
    if ($test_c_active) {
        $test_c_returns = $custom_fields['go_mta_test_completion_lock_loot'][0];
        $test_c_array = $custom_fields['go_mta_test_lock_completion'][0];
        $test_c_uns = unserialize($test_c_array);
        $test_c_num = $test_c_uns[3];
        $test_c_all_questions = $test_c_uns[0];
        $test_c_all_types = $test_c_uns[2];
        $test_c_all_inputs = $test_c_uns[1];
        $test_c_all_input_num = $test_c_uns[4];
        $test_c_all_answers = array();
        $test_c_all_keys = array();
        for ($i = 0; $i < count($test_c_all_inputs); $i++) {
            if (!empty($test_c_all_inputs[$i][0])) {
                $answer_c_temp = implode("###", $test_c_all_inputs[$i][0]);
                $test_c_all_answers[] = $answer_c_temp;
            }
            if (!empty($test_c_all_inputs[$i][1])) {
                $key_c_temp = implode("###", $test_c_all_inputs[$i][1]);
                $test_c_all_keys[] = $key_c_temp;
            }
        }
    }
    $completion_message = $custom_fields['go_mta_complete_message'][0];
    // Completion Message
    $completion_upload = $custom_fields['go_mta_completion_upload'][0];
    if ($mastery_active) {
        $test_m_active = $custom_fields['go_mta_test_mastery_lock'][0];
        $test_m_returns = $custom_fields['go_mta_test_mastery_lock_loot'][0];
        if ($test_m_active) {
            $test_m_array = $custom_fields['go_mta_test_lock_mastery'][0];
            $test_m_uns = unserialize($test_m_array);
            $test_m_num = $test_m_uns[3];
            $test_m_all_questions = $test_m_uns[0];
            $test_m_all_types = $test_m_uns[2];
            $test_m_all_inputs = $test_m_uns[1];
            $test_m_all_input_num = $test_m_uns[4];
            $test_m_all_answers = array();
            $test_m_all_keys = array();
            for ($i = 0; $i < count($test_m_all_inputs); $i++) {
                if (!empty($test_m_all_inputs[$i][0])) {
                    $answer_m_temp = implode("###", $test_m_all_inputs[$i][0]);
                    $test_m_all_answers[] = $answer_m_temp;
                }
                if (!empty($test_m_all_inputs[$i][1])) {
                    $key_m_temp = implode("###", $test_m_all_inputs[$i][1]);
                    $test_m_all_keys[] = $key_m_temp;
                }
            }
        }
        $mastery_message = $custom_fields['go_mta_mastery_message'][0];
        $mastery_upload = $custom_fields['go_mta_mastery_upload'][0];
        if ($repeat == 'on') {
            if ($custom_fields['go_mta_repeat_amount'][0]) {
                $repeat_amount = $custom_fields['go_mta_repeat_amount'][0];
            } else {
                $repeat_amount = 0;
            }
            $repeat_message = $custom_fields['go_mta_repeat_message'][0];
            $repeat_upload = $custom_fields['go_mta_repeat_upload'][0];
        }
    }
    $description = $custom_fields['go_mta_quick_desc'][0];
    $points_array = $rewards['points'];
    $currency_array = $rewards['currency'];
    $bonus_currency = $rewards['bonus_currency'];
    // Array of badge switch and badges associated with a stage
    // E.g. array(true, array(263, 276)) means that stage has badges (true) and the badge IDs are 263 and 276
    $stage_badges = array(unserialize($custom_fields['go_mta_stage_one_badge'][0]), unserialize($custom_fields['go_mta_stage_two_badge'][0]), unserialize($custom_fields['go_mta_stage_three_badge'][0]), unserialize($custom_fields['go_mta_stage_four_badge'][0]), unserialize($custom_fields['go_mta_stage_five_badge'][0]));
    // Stage Stuff
    $content_post = get_post($post_id);
    $task_content = $content_post->post_content;
    if ($task_content == '') {
        $accept_message = $custom_fields['go_mta_accept_message'][0];
        // Completion Message
    } else {
        $accept_message = $content_post->post_content;
    }
    $table_name_go = $wpdb->prefix . "go";
    // Tests failed.
    if (isset($_SESSION['test_encounter_fail_count'])) {
        $e_fail_count = $_SESSION['test_encounter_fail_count'];
    } else {
        $e_fail_count = 0;
    }
    if (isset($_SESSION['test_accept_fail_count'])) {
        $a_fail_count = $_SESSION['test_accept_fail_count'];
    } else {
        $a_fail_count = 0;
    }
    if (isset($_SESSION['test_completion_fail_count'])) {
        $c_fail_count = $_SESSION['test_completion_fail_count'];
    } else {
        $c_fail_count = 0;
    }
    if (isset($_SESSION['test_mastery_fail_count'])) {
        $m_fail_count = $_SESSION['test_mastery_fail_count'];
    } else {
        $m_fail_count = 0;
    }
    // Tests passed.
    if (isset($_SESSION['test_encounter_passed'])) {
        $e_passed = $_SESSION['test_encounter_passed'];
    } else {
        $e_passed = 0;
    }
    if (isset($_SESSION['test_accept_passed'])) {
        $a_passed = $_SESSION['test_accept_passed'];
    } else {
        $a_passed = 0;
    }
    if (isset($_SESSION['test_completion_passed'])) {
        $c_passed = $_SESSION['test_completion_passed'];
    } else {
        $c_passed = 0;
    }
    if (isset($_SESSION['test_mastery_passed'])) {
        $m_passed = $_SESSION['test_mastery_passed'];
    } else {
        $m_passed = 0;
    }
    // if the button pressed IS the repeat button...
    if ($repeat_button == 'on') {
        if ($undo == 'true' || $undo === true) {
            if ($task_count > 0) {
                go_add_post($user_id, $post_id, $status, -floor($points_array[$status - 1] + $update_percent * $points_array[$status - 1]), -floor($currency_array[$status - 1] + $update_percent * $currency_array[$status - 1]), -floor($bonus_currency[$status - 1] + $update_percent * $bonus_currency[$status - 1]), $page_id, $repeat_button, -1, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null);
            } else {
                go_add_post($user_id, $post_id, $status - 1, -floor($points_array[$status - 1] + $update_percent * $points_array[$status - 1]), -floor($currency_array[$status - 1] + $update_percent * $currency_array[$status - 1]), -floor($bonus_currency[$status - 1] + $update_percent * $bonus_currency[$status - 1]), $page_id, $repeat_button, 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null);
                if ($stage_badges[$status][0] == 'true') {
                    foreach ($stage_badges[$status][1] as $badge_id) {
                        go_remove_badge($user_id, $badge_id);
                    }
                }
            }
        } else {
            // if repeat is on and undo is not hit...
            go_add_post($user_id, $post_id, $status, floor($points_array[$status - 1] + $update_percent * $points_array[$status - 1]), floor($currency_array[$status - 1] + $update_percent * $currency_array[$status - 1]), floor($bonus_currency[$status - 1] + $update_percent * $bonus_currency[$status - 1]), $page_id, $repeat_button, 1, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null);
            if ($stage_badges[$status][0] == 'true') {
                foreach ($stage_badges[$status][1] as $badge_id) {
                    do_shortcode("[go_award_badge id='{$badge_id}' repeat='off' uid='{$user_id}']");
                }
            }
        }
        // if the button pressed is NOT the repeat button...
    } else {
        $db_status = (int) $wpdb->get_var("SELECT `status` FROM " . $table_name_go . " WHERE uid = {$user_id} AND post_id = {$post_id}");
        if ($db_status == 0 || $db_status < $status) {
            if ($undo == 'true' || $undo === true) {
                if ($task_count > 0) {
                    go_add_post($user_id, $post_id, $status, -floor($points_array[$status - 1] + $update_percent * $points_array[$status - 1]), -floor($currency_array[$status - 1] + $update_percent * $currency_array[$status - 1]), -floor($bonus_currency[$status - 1] + $update_percent * $bonus_currency[$status - 1]), $page_id, $repeat_button, -1, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null);
                } else {
                    go_add_post($user_id, $post_id, $status - 2, -floor($points_array[$status - 2] + $update_percent * $points_array[$status - 2]), -floor($currency_array[$status - 2] + $update_percent * $currency_array[$status - 2]), -floor($bonus_currency[$status - 2] + $update_percent * $bonus_currency[$status - 2]), $page_id, $repeat_button, 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null);
                    if ($stage_badges[$status - 2][0] == 'true') {
                        foreach ($stage_badges[$status - 2][1] as $badge_id) {
                            go_remove_badge($user_id, $badge_id);
                        }
                    }
                }
            } else {
                go_add_post($user_id, $post_id, $status, floor($points_array[$status - 1] + $update_percent * $points_array[$status - 1]), floor($currency_array[$status - 1] + $update_percent * $currency_array[$status - 1]), floor($bonus_currency[$status - 1] + $update_percent * $bonus_currency[$status - 1]), $page_id, $repeat_button, 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null);
                if ($stage_badges[$status - 1][0] == 'true') {
                    foreach ($stage_badges[$status - 1][1] as $badge_id) {
                        do_shortcode("[go_award_badge id='{$badge_id}' repeat='off' uid='{$user_id}']");
                    }
                }
            }
        }
    }
    // redefine the status and task_count because they have been updated as soon as the above go_add_post() calls are made.
    $status = $wpdb->get_var("SELECT `status` FROM " . $table_name_go . " WHERE uid = {$user_id} AND post_id = {$post_id}");
    $task_count = $wpdb->get_var("SELECT `count` FROM " . $go_table_ind . " WHERE post_id = {$post_id} AND uid = {$user_id}");
    switch ($status) {
        case 0:
            $db_task_stage_upload_var = 'e_uploaded';
            break;
        case 1:
            $db_task_stage_upload_var = 'a_uploaded';
            break;
        case 2:
            $db_task_stage_upload_var = 'c_uploaded';
            break;
        case 3:
            $db_task_stage_upload_var = 'm_uploaded';
            break;
        case 4:
            $db_task_stage_upload_var = 'r_uploaded';
            break;
    }
    if (!empty($db_task_stage_upload_var)) {
        $is_uploaded = $wpdb->get_var("SELECT {$db_task_stage_upload_var} FROM {$go_table_ind} WHERE uid = {$user_id} AND post_id = {$post_id}");
    } else {
        $is_uploaded = 0;
    }
    // The switch iterates through every value of status until it finds a value that matches a case.  So, if $status = 2, case 1 will
    // be skipped and case 2 will be output.  NOTE:  Without the 'break' statement after each case, the switch would recursively output
    // each case beyond the current value of $status.  Ex: if there are no 'break' statments in any of the cases and $status = 1,
    // every case 1 will be output and so will ever case after it, until it hits the end of the switch.
    switch ($status) {
        case 1:
            echo '<div id="new_content">' . '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div>';
            if ($test_e_active) {
                echo "<p id='go_test_error_msg' style='color: red;'></p>";
                if ($test_e_num > 1) {
                    for ($i = 0; $i < $test_e_num; $i++) {
                        echo do_shortcode("[go_test type='" . $test_e_all_types[$i] . "' question='" . $test_e_all_questions[$i] . "' possible_answers='" . $test_e_all_answers[$i] . "' key='" . $test_e_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_e_num . "']");
                    }
                    echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                } else {
                    echo do_shortcode("[go_test type='" . $test_e_all_types[0] . "' question='" . $test_e_all_questions[0] . "' possible_answers='" . $test_e_all_answers[0] . "' key='" . $test_e_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                }
            }
            if ($encounter_upload) {
                echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
            }
            echo "<p id='go_stage_error_msg' style='display: none; color: red;'></p>";
            if ($e_is_locked === 'true' && !empty($e_pass_lock)) {
                echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
            }
            echo "<button id='go_button' status='2' onclick='task_stage_change(this);'";
            if ($e_is_locked === 'true' && empty($e_pass_lock)) {
                echo "admin_lock='true'";
            }
            echo ">" . go_return_options('go_second_stage_button') . "</button></div>";
            break;
        case 2:
            echo '<div id="new_content">' . '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div>';
            if ($test_a_active) {
                echo "<p id='go_test_error_msg' style='color: red;'></p>";
                if ($test_a_num > 1) {
                    for ($i = 0; $i < $test_a_num; $i++) {
                        echo do_shortcode("[go_test type='" . $test_a_all_types[$i] . "' question='" . $test_a_all_questions[$i] . "' possible_answers='" . $test_a_all_answers[$i] . "' key='" . $test_a_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_a_num . "']");
                    }
                    echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                } else {
                    echo do_shortcode("[go_test type='" . $test_a_all_types[0] . "' question='" . $test_a_all_questions[0] . "' possible_answers='" . $test_a_all_answers[0] . "' key='" . $test_a_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                }
            }
            if ($accept_upload) {
                echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
            }
            echo "<p id='go_stage_error_msg' style='display: none; color: red;'></p>";
            if ($a_is_locked === 'true' && !empty($a_pass_lock)) {
                echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
            }
            echo "<button id='go_button' status='3' onclick='task_stage_change(this);'";
            if ($a_is_locked === 'true' && empty($a_pass_lock)) {
                echo "admin_lock='true'";
            }
            echo '>' . go_return_options('go_third_stage_button') . '</button> <button id="go_back_button" onclick="task_stage_change(this);" undo="true">Undo</button></div>';
            break;
        case 3:
            echo '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div>' . '<div id="new_content"><div class="go_stage_message">' . do_shortcode(wpautop($completion_message)) . '</div>';
            if ($mastery_active) {
                if ($test_c_active) {
                    echo "<p id='go_test_error_msg' style='color: red;'></p>";
                    if ($test_c_num > 1) {
                        for ($i = 0; $i < $test_c_num; $i++) {
                            echo do_shortcode("[go_test type='" . $test_c_all_types[$i] . "' question='" . $test_c_all_questions[$i] . "' possible_answers='" . $test_c_all_answers[$i] . "' key='" . $test_c_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_c_num . "']");
                        }
                        echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    } else {
                        echo do_shortcode("[go_test type='" . $test_c_all_types[0] . "' question='" . $test_c_all_questions[0] . "' possible_answers='" . $test_c_all_answers[0] . "' key='" . $test_c_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    }
                }
                if ($completion_upload) {
                    echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                }
                echo "<p id='go_stage_error_msg' style='display: none; color: red;'></p>";
                if ($c_is_locked === 'true' && !empty($c_pass_lock)) {
                    echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
                }
                echo "<button id='go_button' status='4' onclick='task_stage_change(this);'";
                if ($c_is_locked === 'true' && empty($c_pass_lock)) {
                    echo "admin_lock='true'";
                }
                echo '>' . go_return_options('go_fourth_stage_button') . '</button> 
				<button id="go_back_button" onclick="task_stage_change(this);" undo="true">Undo</button>';
                if ($next_post_id_in_chain != 0 && $last_in_chain !== 'true') {
                    echo '<div class="go_chain_message">Next ' . strtolower(go_return_options('go_tasks_name')) . ' in ' . $chain_name . ': <a href="' . get_permalink($next_post_id_in_chain) . '">' . get_the_title($next_post_id_in_chain) . '</a></div>';
                } else {
                    echo '<div class="go_chain_message">' . $custom_fields['go_mta_final_chain_message'][0] . '</div>';
                }
                echo "</div>";
            } else {
                if ($test_c_active) {
                    echo "<p id='go_test_error_msg' style='color: red;'></p>";
                    if ($test_c_num > 1) {
                        for ($i = 0; $i < $test_c_num; $i++) {
                            echo do_shortcode("[go_test type='" . $test_c_all_types[$i] . "' question='" . $test_c_all_questions[$i] . "' possible_answers='" . $test_c_all_answers[$i] . "' key='" . $test_c_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_c_num . "']");
                        }
                        echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    } else {
                        echo do_shortcode("[go_test type='" . $test_c_all_types[0] . "' question='" . $test_c_all_questions[0] . "' possible_answers='" . $test_c_all_answers[0] . "' key='" . $test_c_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    }
                }
                if ($completion_upload) {
                    echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                }
                echo '<span id="go_button" status="4" style="display:none;"></span><button id="go_back_button" onclick="task_stage_change(this);" undo="true">Undo</button>';
                if ($next_post_id_in_chain != 0 && $last_in_chain !== 'true') {
                    echo '<div class="go_chain_message">Next ' . strtolower(go_return_options('go_tasks_name')) . ' in ' . $chain_name . ': <a href="' . get_permalink($next_post_id_in_chain) . '">' . get_the_title($next_post_id_in_chain) . '</a></div>';
                } else {
                    echo '<div class="go_chain_message">' . $custom_fields['go_mta_final_chain_message'][0] . '</div>';
                }
                echo "</div>";
            }
            break;
        case 4:
            echo '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div><div class="go_stage_message">' . do_shortcode(wpautop($completion_message)) . '</div><div id="new_content"><div class="go_stage_message">' . do_shortcode(wpautop($mastery_message)) . '</div>';
            // if the task can be repeated...
            if ($repeat == 'on') {
                // if the number of times that the page has been repeated is less than the total amount of repeats allowed OR if the
                // total repeats allowed is equal to zero (infinte amount allowed)...
                if ($task_count < $repeat_amount || $repeat_amount == 0) {
                    if ($task_count == 0) {
                        if ($test_m_active) {
                            echo "<p id='go_test_error_msg' style='color: red;'></p>";
                            if ($test_m_num > 1) {
                                for ($i = 0; $i < $test_m_num; $i++) {
                                    echo do_shortcode("[go_test type='" . $test_m_all_types[$i] . "' question='" . $test_m_all_questions[$i] . "' possible_answers='" . $test_m_all_answers[$i] . "' key='" . $test_m_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_m_num . "']");
                                }
                                echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                            } else {
                                echo do_shortcode("[go_test type='" . $test_m_all_types[0] . "' question='" . $test_m_all_questions[0] . "' possible_answers='" . $test_m_all_answers[0] . "' key='" . $test_m_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                            }
                        }
                        if ($mastery_upload) {
                            echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                        }
                        echo '
							<div id="repeat_quest">
								<div id="go_repeat_clicked" style="display:none;"><div class="go_stage_message">' . do_shortcode(wpautop($repeat_message)) . "</div><p id='go_stage_error_msg' style='display: none; color: red;'></p>";
                        if ($m_is_locked === 'true' && !empty($m_pass_lock)) {
                            echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
                        }
                        echo "<button id='go_button' status='4' onclick='go_repeat_hide(this);' repeat='on'";
                        if ($m_is_locked === 'true' && empty($m_pass_lock)) {
                            echo "admin_lock='true'";
                        }
                        echo '>' . go_return_options('go_fourth_stage_button') . " Again" . '</button>
									<button id="go_back_button" onclick="task_stage_change(this);" undo="true">Undo</button>
								</div>
								<div id="go_repeat_unclicked">
									<button id="go_button" status="4" onclick="go_repeat_replace();">' . get_option('go_fifth_stage_button') . '</button>
									<button id="go_back_button" onclick="task_stage_change(this);" undo="true">Undo</button>
								</div>
							</div>
						';
                    } else {
                        if ($repeat_upload) {
                            echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                        }
                        echo '
							<div id="repeat_quest">
								<div id="go_repeat_clicked" style="display:none;"><div class="go_stage_message">' . do_shortcode(wpautop($repeat_message)) . "</div><p id='go_stage_error_msg' style='display: none; color: red;'></p>";
                        if ($r_is_locked === 'true' && !empty($r_pass_lock)) {
                            echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
                        }
                        echo "<button id='go_button' status='4' onclick='go_repeat_hide(this);' repeat='on'";
                        if ($r_is_locked === 'true' && empty($r_pass_lock)) {
                            echo "admin_lock='true'";
                        }
                        echo '>' . go_return_options('go_fourth_stage_button') . " Again" . '</button>
									<button id="go_back_button" onclick="task_stage_change(this);" undo="true">Undo</button>
								</div>
								<div id="go_repeat_unclicked">
									<button id="go_button" status="4" onclick="go_repeat_replace();">' . get_option('go_fifth_stage_button') . '</button>
									<button id="go_back_button" onclick="task_stage_change(this);" undo="true">Undo</button>
								</div>
							</div>
						';
                    }
                } else {
                    echo '<span id="go_button" status="4" repeat="on" style="display:none;"></span><button id="go_back_button" onclick="task_stage_change(this);" undo="true">Undo</button>';
                }
            } else {
                if ($test_m_active) {
                    echo "<p id='go_test_error_msg' style='color: red;'></p>";
                    if ($test_m_num > 1) {
                        for ($i = 0; $i < $test_m_num; $i++) {
                            echo do_shortcode("[go_test type='" . $test_m_all_types[$i] . "' question='" . $test_m_all_questions[$i] . "' possible_answers='" . $test_m_all_answers[$i] . "' key='" . $test_m_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_m_num . "']");
                        }
                        echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    } else {
                        echo do_shortcode("[go_test type='" . $test_m_all_types[0] . "' question='" . $test_m_all_questions[0] . "' possible_answers='" . $test_m_all_answers[0] . "' key='" . $test_m_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    }
                }
                if ($mastery_upload) {
                    echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                }
                echo '<span id="go_button" status="4" repeat="on" style="display:none;"></span><button id="go_back_button" onclick="task_stage_change(this);" undo="true">Undo</button>';
            }
            if ($next_post_id_in_chain != 0 && $last_in_chain !== 'true') {
                echo '<div class="go_chain_message"><p>Next ' . strtolower(go_return_options('go_tasks_name')) . ' in ' . $chain_name . ': <a href="' . get_permalink($next_post_id_in_chain) . '">' . get_the_title($next_post_id_in_chain) . '</a></div>';
            } else {
                echo '<div class="go_chain_message">' . $custom_fields['go_mta_final_chain_message'][0] . '</div>';
            }
            echo '</div>';
    }
    die;
}
Пример #2
0
function go_stats_move_stage()
{
    global $wpdb;
    $go_table_name = "{$wpdb->prefix}go";
    if (!empty($_POST['user_id'])) {
        $user_id = $_POST['user_id'];
    } else {
        $user_id = get_current_user_id();
    }
    $current_rank = get_user_meta($user_id, 'go_rank', true);
    $task_id = $_POST['task_id'];
    $status = $_POST['status'];
    $count = $_POST['count'];
    $message = $_POST['message'];
    $custom_fields = get_post_custom($task_id);
    $rewards = unserialize($custom_fields['go_presets'][0]);
    $current_status = $wpdb->get_var($wpdb->prepare("SELECT status FROM {$go_table_name} WHERE uid=%d AND post_id=%d", $user_id, $task_id));
    $page_id = $wpdb->get_var($wpdb->prepare("SELECT page_id FROM {$go_table_name} WHERE uid=%d AND post_id=%d", $user_id, $task_id));
    $changed = array('type' => 'json', 'points' => 0, 'currency' => 0, 'bonus_currency' => 0);
    if ($status == 1) {
        $current_rewards = $wpdb->get_results($wpdb->prepare("SELECT points, currency, bonus_currency FROM {$go_table_name} WHERE uid=%d AND post_id=%d", $user_id, $task_id));
        go_task_abandon($user_id, $task_id, $current_rewards[0]->points, $current_rewards[0]->currency, $current_rewards[0]->bonus_currency);
        $changed['points'] = -$current_rewards[0]->points;
        $changed['currency'] = -$current_rewards[0]->currency;
        $changed['bonus_currency'] = -$current_rewards[0]->bonus_currency;
        $current_points = go_return_points($user_id);
        $updated_rank = get_user_meta($user_id, 'go_rank', true);
        if ($current_rank[0][0] != $updated_rank[0][0]) {
            $changed['current_points'] = $current_points;
            $changed['rank'] = $updated_rank[0][0];
            $changed['rank_points'] = $updated_rank[0][1];
            $changed['next_rank_points'] = $updated_rank[1][1];
        }
        $changed['abandon'] = 'true';
    } else {
        for ($count; $count > 0; $count--) {
            go_add_post($user_id, $task_id, $current_status, -$rewards['points'][$current_status], -$rewards['currency'][$current_status], -$rewards['bonus_currency'][$current_status], null, $page_id, 'on', -1, null, null, null, null);
            $changed['points'] += -$rewards['points'][$current_status];
            $changed['currency'] += -$rewards['currency'][$current_status];
            $changed['bonus_currency'] += -$rewards['bonus_currency'][$current_status];
        }
        while ($current_status != $status) {
            if ($current_status > $status) {
                $current_status--;
                go_add_post($user_id, $task_id, $current_status, -$rewards['points'][$current_status], -$rewards['currency'][$current_status], -$rewards['bonus_currency'][$current_status], null, $page_id, null, null, null, null, null, null);
                $changed['points'] += -$rewards['points'][$current_status];
                $changed['currency'] += -$rewards['currency'][$current_status];
                $changed['bonus_currency'] += -$rewards['bonus_currency'][$current_status];
            } elseif ($current_status < $status) {
                $current_status++;
                $current_count = $wpdb->get_var($wpdb->prepare("SELECT count FROM {$go_table_name} WHERE uid=%d AND post_id=%d", $user_id, $task_id));
                if ($current_status == 5 && $current_count == 0) {
                    go_add_post($user_id, $task_id, $current_status - 1, $rewards['points'][$current_status - 1], $rewards['currency'][$current_status - 1], $rewards['bonus_currency'][$current_status - 1], null, $page_id, 'on', 1, null, null, null, null);
                    $changed['points'] += $rewards['points'][$current_status - 1];
                    $changed['currency'] += $rewards['currency'][$current_status - 1];
                    $changed['bonus_currency'] += $rewards['bonus_currency'][$current_status - 1];
                } elseif ($current_status < 5) {
                    go_add_post($user_id, $task_id, $current_status, $rewards['points'][$current_status - 1], $rewards['currency'][$current_status - 1], $rewards['bonus_currency'][$current_status - 1], null, $page_id, null, null, null, null, null, null);
                    $changed['points'] += $rewards['points'][$current_status - 1];
                    $changed['currency'] += $rewards['currency'][$current_status - 1];
                    $changed['bonus_currency'] += $rewards['bonus_currency'][$current_status - 1];
                }
            }
        }
        if ($message === 'See me') {
            go_message_user($user_id, $message . ' about, <a href="' . get_permalink($task_id) . '" style="display: inline-block; text-decoration: underline; padding: 0px; margin: 0px;">' . get_the_title($task_id) . '</a>, please.');
        } else {
            go_message_user($user_id, 'RE: <a href="' . get_permalink($task_id) . '">' . get_the_title($task_id) . '</a> ' . $message);
        }
        $current_points = go_return_points($user_id);
        $updated_rank = get_user_meta($user_id, 'go_rank', true);
        if ($current_rank[0][0] != $updated_rank[0][0]) {
            $changed['current_points'] = $current_points;
            $changed['rank'] = $updated_rank[0][0];
            $changed['rank_points'] = $updated_rank[0][1];
            $changed['next_rank_points'] = $updated_rank[1][1];
        }
    }
    echo json_encode($changed);
    die;
}
Пример #3
0
function task_change_stage()
{
    global $wpdb;
    $unix_now = current_time('timestamp');
    // Current unix timestamp
    $post_id = $_POST['post_id'];
    // Post id posted from ajax function
    $user_id = $_POST['user_id'];
    // User id posted from ajax function
    $status = (int) $_POST['status'];
    // Task's status posted from ajax function
    $page_id = $_POST['page_id'];
    // Page id posted from ajax function
    $admin_name = $_POST['admin_name'];
    $undo = $_POST['undo'];
    // Boolean which determines if the button clicked is an undo button or not (True or False)
    $pass = $_POST['pass'];
    // Contains the user-entered admin password
    $url = $_POST['url'];
    // Contains user-entered url
    $repeat_button = $_POST['repeat'];
    // Boolean which determines if the task is repeatable or not (True or False)
    $points_array = $_POST['points'];
    // Serialized array of points rewarded for each stage
    $currency_array = $_POST['currency'];
    // Serialized array of currency rewarded for each stage
    $bonus_currency_array = $_POST['bonus_currency'];
    // Serialized array of bonus currency awarded for each stage
    $date_update_percent = $_POST['date_update_percent'];
    // Float which is used to modify values saved to database
    $chain_name = $_POST['chain_name'];
    // String which is used to display next task in a quest chain
    $next_post_id_in_chain = $_POST['next_post_id_in_chain'];
    // Integer which is used to display next task in a quest chain
    $last_in_chain = $_POST['last_in_chain'];
    // Boolean which determines if the current quest is last in chain
    $final_chain_message = $_POST['final_chain_message'];
    $number_of_stages = $_POST['number_of_stages'];
    // Integer with number of stages in the task
    $go_table_name = "{$wpdb->prefix}go";
    $task_count = $wpdb->get_var("SELECT `count` FROM {$go_table_name} WHERE post_id = {$post_id} AND uid = {$user_id}");
    $custom_fields = get_post_custom($post_id);
    // Just gathering some data about this task with its post id
    $mastery_active = !empty($custom_fields['go_mta_task_mastery'][0]) ? !$custom_fields['go_mta_task_mastery'][0] : true;
    // whether or not the mastery stage is active
    $repeat = !empty($custom_fields['go_mta_task_repeat'][0]) ? $custom_fields['go_mta_task_repeat'][0] : '';
    // Whether or not you can repeat the task
    $e_admin_lock = !empty($custom_fields['go_mta_encounter_admin_lock'][0]) ? unserialize($custom_fields['go_mta_encounter_admin_lock'][0]) : null;
    if (!empty($e_admin_lock)) {
        $e_is_locked = $e_admin_lock[0];
        if ($e_is_locked === 'true') {
            $e_pass_lock = $e_admin_lock[1];
        }
    }
    $a_admin_lock = !empty($custom_fields['go_mta_accept_admin_lock'][0]) ? unserialize($custom_fields['go_mta_accept_admin_lock'][0]) : null;
    if (!empty($a_admin_lock)) {
        $a_is_locked = $a_admin_lock[0];
        if ($a_is_locked === 'true') {
            $a_pass_lock = $a_admin_lock[1];
        }
    }
    $c_admin_lock = !empty($custom_fields['go_mta_completion_admin_lock'][0]) ? unserialize($custom_fields['go_mta_completion_admin_lock'][0]) : null;
    if (!empty($c_admin_lock)) {
        $c_is_locked = $c_admin_lock[0];
        if ($c_is_locked === 'true') {
            $c_pass_lock = $c_admin_lock[1];
        }
    }
    $m_admin_lock = !empty($custom_fields['go_mta_mastery_admin_lock'][0]) ? unserialize($custom_fields['go_mta_mastery_admin_lock'][0]) : null;
    if (!empty($m_admin_lock)) {
        $m_is_locked = $m_admin_lock[0];
        if ($m_is_locked === 'true') {
            $m_pass_lock = $m_admin_lock[1];
        }
    }
    $r_admin_lock = !empty($custom_fields['go_mta_repeat_admin_lock'][0]) ? unserialize($custom_fields['go_mta_repeat_admin_lock'][0]) : null;
    if (!empty($r_admin_lock)) {
        $r_is_locked = $r_admin_lock[0];
        if ($r_is_locked === 'true') {
            $r_pass_lock = $r_admin_lock[1];
        }
    }
    $e_url_is_locked = !empty($custom_fields['go_mta_encounter_url_key'][0]) ? true : false;
    $a_url_is_locked = !empty($custom_fields['go_mta_accept_url_key'][0]) ? true : false;
    $c_url_is_locked = !empty($custom_fields['go_mta_completion_url_key'][0]) ? true : false;
    $m_url_is_locked = !empty($custom_fields['go_mta_mastery_url_key'][0]) ? true : false;
    if (!empty($pass) || "0" === $pass) {
        if ($status == 4) {
            $temp_status = $status;
        } else {
            $temp_status = $status - 1;
        }
        switch ($temp_status) {
            case 1:
                $pass_lock = $e_pass_lock;
                break;
            case 2:
                $pass_lock = $a_pass_lock;
                break;
            case 3:
                $pass_lock = $c_pass_lock;
                break;
            case 4:
                if ($repeat === 'on') {
                    $pass_lock = $r_pass_lock;
                } else {
                    $pass_lock = $m_pass_lock;
                }
                break;
        }
        if (!empty($pass_lock) && $pass !== $pass_lock) {
            echo 0;
            die;
        }
    }
    $test_e_active = !empty($custom_fields['go_mta_test_encounter_lock'][0]) ? $custom_fields['go_mta_test_encounter_lock'][0] : false;
    $test_a_active = !empty($custom_fields['go_mta_test_accept_lock'][0]) ? $custom_fields['go_mta_test_accept_lock'][0] : false;
    $test_c_active = !empty($custom_fields['go_mta_test_completion_lock'][0]) ? $custom_fields['go_mta_test_completion_lock'][0] : false;
    if ($test_e_active) {
        $test_e_array = go_get_test_meta_content($custom_fields, 'encounter');
        $test_e_returns = $test_e_array[0];
        $test_e_num = $test_e_array[1];
        $test_e_all_questions = $test_e_array[2][0];
        $test_e_all_types = $test_e_array[2][1];
        $test_e_all_answers = $test_e_array[2][2];
        $test_e_all_keys = $test_e_array[2][3];
    }
    $encounter_upload = !empty($custom_fields['go_mta_encounter_upload'][0]) ? $custom_fields['go_mta_encounter_upload'][0] : false;
    if ($test_a_active) {
        $test_a_array = go_get_test_meta_content($custom_fields, 'accept');
        $test_a_returns = $test_a_array[0];
        $test_a_num = $test_a_array[1];
        $test_a_all_questions = $test_a_array[2][0];
        $test_a_all_types = $test_a_array[2][1];
        $test_a_all_answers = $test_a_array[2][2];
        $test_a_all_keys = $test_a_array[2][3];
    }
    $accept_upload = !empty($custom_fields['go_mta_accept_upload'][0]) ? $custom_fields['go_mta_accept_upload'][0] : false;
    if ($test_c_active) {
        $test_c_array = go_get_test_meta_content($custom_fields, 'completion');
        $test_c_returns = $test_c_array[0];
        $test_c_num = $test_c_array[1];
        $test_c_all_questions = $test_c_array[2][0];
        $test_c_all_types = $test_c_array[2][1];
        $test_c_all_answers = $test_c_array[2][2];
        $test_c_all_keys = $test_c_array[2][3];
    }
    $completion_message = !empty($custom_fields['go_mta_complete_message'][0]) ? $custom_fields['go_mta_complete_message'][0] : '';
    $completion_upload = !empty($custom_fields['go_mta_completion_upload'][0]) ? $custom_fields['go_mta_completion_upload'][0] : false;
    if ($mastery_active) {
        $test_m_active = !empty($custom_fields['go_mta_test_mastery_lock'][0]) ? $custom_fields['go_mta_test_mastery_lock'][0] : false;
        $bonus_loot = !empty($custom_fields['go_mta_mastery_bonus_loot'][0]) ? unserialize($custom_fields['go_mta_mastery_bonus_loot'][0]) : null;
        if ($test_m_active) {
            $test_m_array = go_get_test_meta_content($custom_fields, 'mastery');
            $test_m_returns = $test_m_array[0];
            $test_m_num = $test_m_array[1];
            $test_m_all_questions = $test_m_array[2][0];
            $test_m_all_types = $test_m_array[2][1];
            $test_m_all_answers = $test_m_array[2][2];
            $test_m_all_keys = $test_m_array[2][3];
        }
        $mastery_message = !empty($custom_fields['go_mta_mastery_message'][0]) ? $custom_fields['go_mta_mastery_message'][0] : '';
        $mastery_upload = !empty($custom_fields['go_mta_mastery_upload'][0]) ? $custom_fields['go_mta_mastery_upload'][0] : false;
        if ($repeat == 'on') {
            $repeat_amount = !empty($custom_fields['go_mta_repeat_amount'][0]) ? $custom_fields['go_mta_repeat_amount'][0] : 0;
            $repeat_message = !empty($custom_fields['go_mta_repeat_message'][0]) ? $custom_fields['go_mta_repeat_message'][0] : '';
            $repeat_upload = !empty($custom_fields['go_mta_repeat_upload'][0]) ? $custom_fields['go_mta_repeat_upload'][0] : false;
        }
    }
    $description = !empty($custom_fields['go_mta_quick_desc'][0]) ? $custom_fields['go_mta_quick_desc'][0] : '';
    // Array of badge switch and badges associated with a stage
    // E.g. array( true, array( 263, 276 ) ) means that stage has badges (true) and the badge IDs are 263 and 276
    $stage_badges = array(!empty($custom_fields['go_mta_stage_one_badge'][0]) ? unserialize($custom_fields['go_mta_stage_one_badge'][0]) : null, !empty($custom_fields['go_mta_stage_two_badge'][0]) ? unserialize($custom_fields['go_mta_stage_two_badge'][0]) : null, !empty($custom_fields['go_mta_stage_three_badge'][0]) ? unserialize($custom_fields['go_mta_stage_three_badge'][0]) : null, !empty($custom_fields['go_mta_stage_four_badge'][0]) ? unserialize($custom_fields['go_mta_stage_four_badge'][0]) : null, !empty($custom_fields['go_mta_stage_five_badge'][0]) ? unserialize($custom_fields['go_mta_stage_five_badge'][0]) : null);
    // Stage Stuff
    $content_post = get_post($post_id);
    $task_content = $content_post->post_content;
    if ($task_content == '') {
        $accept_message = !empty($custom_fields['go_mta_accept_message'][0]) ? $custom_fields['go_mta_accept_message'][0] : '';
    } else {
        $accept_message = $content_post->post_content;
    }
    // Tests failed.
    if (isset($_SESSION['test_encounter_fail_count'])) {
        $e_fail_count = $_SESSION['test_encounter_fail_count'];
    } else {
        $e_fail_count = 0;
    }
    if (isset($_SESSION['test_accept_fail_count'])) {
        $a_fail_count = $_SESSION['test_accept_fail_count'];
    } else {
        $a_fail_count = 0;
    }
    if (isset($_SESSION['test_completion_fail_count'])) {
        $c_fail_count = $_SESSION['test_completion_fail_count'];
    } else {
        $c_fail_count = 0;
    }
    if (isset($_SESSION['test_mastery_fail_count'])) {
        $m_fail_count = $_SESSION['test_mastery_fail_count'];
    } else {
        $m_fail_count = 0;
    }
    // Tests passed.
    if (isset($_SESSION['test_encounter_passed'])) {
        $e_passed = $_SESSION['test_encounter_passed'];
    } else {
        $e_passed = 0;
    }
    if (isset($_SESSION['test_accept_passed'])) {
        $a_passed = $_SESSION['test_accept_passed'];
    } else {
        $a_passed = 0;
    }
    if (isset($_SESSION['test_completion_passed'])) {
        $c_passed = $_SESSION['test_completion_passed'];
    } else {
        $c_passed = 0;
    }
    if (isset($_SESSION['test_mastery_passed'])) {
        $m_passed = $_SESSION['test_mastery_passed'];
    } else {
        $m_passed = 0;
    }
    $db_status = (int) $wpdb->get_var("SELECT `status` FROM {$go_table_name} WHERE uid = {$user_id} AND post_id = {$post_id}");
    $future_switches = !empty($custom_fields['go_mta_time_filters'][0]) ? unserialize($custom_fields['go_mta_time_filters'][0]) : null;
    //determine which future date modifier is on, if any
    $date_picker = !empty($custom_fields['go_mta_date_picker'][0]) && unserialize($custom_fields['go_mta_date_picker'][0]) ? array_filter(unserialize($custom_fields['go_mta_date_picker'][0])) : false;
    // If there are dates in the date picker
    if (!empty($date_picker) && (!empty($future_switches) && $future_switches['calendar'] == 'on')) {
        $dates = $date_picker['date'];
        $times = $date_picker['time'];
        $percentages = $date_picker['percent'];
        $past_dates = array();
        foreach ($dates as $key => $date) {
            if ($unix_now >= strtotime($date) + strtotime($times[$key], 0)) {
                $past_dates[$key] = abs($unix_now - strtotime($date));
            }
        }
        if (!empty($past_dates)) {
            asort($past_dates);
            $date_update_percent = (double) ((100 - $percentages[key($past_dates)]) / 100);
        } else {
            $date_update_percent = 1;
        }
        ?>
		<script type='text/javascript'>
			var num_of_stages = parseInt( <?php 
        echo $number_of_stages;
        ?>
 );
			var date_modifier = parseFloat( <?php 
        echo $date_update_percent;
        ?>
 );
			for ( i = 1; i <= num_of_stages; i++ ) {
				var stage_points = jQuery( '#go_stage_' + i + '_points' );
				var stage_currency = jQuery( '#go_stage_' + i + '_currency' );
				var stage_bonus_currency = jQuery( '#go_stage_' + i + '_bonus_currency' );
				if ( stage_points.length && ! stage_points.hasClass( 'go_updated' ) ) {
					stage_points.html( Math.floor( parseInt( stage_points.html() ) * date_modifier ) ).addClass( 'go_updated' );
				}
				if ( stage_currency.length && ! stage_currency.hasClass( 'go_updated' ) ) {
					stage_currency.html( Math.floor( parseInt( stage_currency.html() ) * date_modifier ) ).addClass( 'go_updated' );
				}
				if ( stage_bonus_currency.length && ! stage_bonus_currency.hasClass( 'go_updated' ) ) {
					stage_bonus_currency.html( Math.floor( parseInt( stage_bonus_currency.html() ) * date_modifier ) ).addClass( 'go_updated' );
				}
			}
		</script>
		<?php 
    } else {
        $date_update_percent = 1;
    }
    $future_modifier = !empty($custom_fields['go_mta_time_modifier'][0]) ? unserialize($custom_fields['go_mta_time_modifier'][0]) : null;
    if (!empty($future_modifier) && (!empty($future_switches['future']) && $future_switches['future'] == 'on') && !($future_modifier['days'] == 0 && $future_modifier['hours'] == 0 && $future_modifier['minutes'] == 0 && $future_modifier['seconds'] == 0)) {
        $user_timers = get_user_meta($user_id, 'go_timers');
        $accept_timestamp_db = strtotime(str_replace('@', ' ', $wpdb->get_var("SELECT timestamp FROM {$wpdb->prefix}go WHERE uid='{$user_id}' AND post_id='{$post_id}'")));
        $accept_timestamp = !empty($user_timers[0][$post_id]) ? $user_timers[0][$post_id] : (!empty($accept_timestamp_db) ? $accept_timestamp_db : ($status == 2 ? $unix_now : 0));
        $days = (int) $future_modifier['days'];
        $hours = (int) $future_modifier['hours'];
        $minutes = (int) $future_modifier['minutes'];
        $seconds = (int) $future_modifier['seconds'];
        $future_time = strtotime("{$days} days", 0) + strtotime("{$hours} hours", 0) + strtotime("{$minutes} minutes", 0) + strtotime("{$seconds} seconds", 0) + $accept_timestamp;
        if ($status == 2 || ($undo == 'true' || $undo === true) && $status >= 2 && $db_status < 4) {
            go_task_timer($post_id, $user_id, $future_modifier);
        } elseif ($status > 2) {
            ?>
          	<script type='text/javascript'>
				jQuery( '#go_future_notification' ).hide();
				jQuery( '#go_task_timer' ).remove();
			</script>
            <?php 
        }
        if ($unix_now >= $future_time) {
            $future_update_percent = (double) ((100 - $future_modifier['percentage']) / 100);
        } else {
            $future_update_percent = 1;
        }
    } else {
        $future_update_percent = 1;
    }
    $complete_stage = $undo ? $status - 1 : $status;
    if (!empty($future_switches['calendar']) && $future_switches['calendar'] == 'on') {
        $update_percent = $date_update_percent;
    } elseif (!empty($future_switches['future']) && $future_switches['future'] == 'on' && $complete_stage == 3 && $db_status < 4) {
        $update_percent = $future_update_percent;
    } else {
        $update_percent = 1;
    }
    // if the button pressed IS the repeat button...
    if ($repeat_button == 'on') {
        if ($undo == 'true' || $undo === true) {
            if ($task_count > 0) {
                go_add_post($user_id, $post_id, $status, -floor($update_percent * $points_array[$status]), -floor($update_percent * $currency_array[$status]), -floor($update_percent * $bonus_currency_array[$status]), null, $page_id, $repeat_button, -1, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed);
                if ($bonus_loot[0]) {
                    if (!empty($bonus_loot[1])) {
                        foreach ($bonus_loot[1] as $store_item => $on) {
                            if ($on === 'on') {
                                $store_custom_fields = get_post_custom($store_item);
                                $store_cost = !empty($store_custom_fields['go_mta_store_cost'][0]) ? unserialize($store_custom_fields['go_mta_store_cost'][0]) : array();
                                $currency = $store_cost[0] < 0 ? $store_cost[0] : 0;
                                $points = $store_cost[1] < 0 ? $store_cost[1] : 0;
                                $bonus_currency = $store_cost[2] < 0 ? $store_cost[2] : 0;
                                $penalty = $store_cost[3] > 0 ? $store_cost[3] : 0;
                                $minutes = $store_cost[4] < 0 ? $store_cost[4] : 0;
                                $user_id = get_current_user_id();
                                $received = $wpdb->query($wpdb->prepare("SELECT * FROM {$go_table_name}  WHERE uid = %d AND status = %d AND gifted = %d AND post_id = %d AND reason = 'Quest' OR reason = 'Bonus' ORDER BY timestamp DESC, reason DESC, id DESC LIMIT 1", $user_id, -1, 0, $store_item));
                                if ($received) {
                                    go_update_totals($user_id, $points, $currency, $bonus_currency, 0, $minutes, null, false, true);
                                }
                                $wpdb->query($wpdb->prepare("DELETE FROM {$go_table_name}  WHERE uid = %d AND status = %d AND gifted = %d AND post_id = %d AND reason = 'Quest' OR reason = 'Bonus' ORDER BY timestamp DESC, reason DESC, id DESC LIMIT 1", $user_id, -1, 0, $store_item));
                            }
                        }
                    }
                }
            } else {
                go_add_post($user_id, $post_id, $status - 1, -floor($update_percent * $points_array[$status - 1]), -floor($update_percent * $currency_array[$status - 1]), -floor($update_percent * $bonus_currency_array[$status - 1]), null, $page_id, $repeat_button, 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed);
                if ($bonus_loot[0]) {
                    if (!empty($bonus_loot[1])) {
                        foreach ($bonus_loot[1] as $store_item => $on) {
                            if ($on === 'on') {
                                $store_custom_fields = get_post_custom($store_item);
                                $store_cost = !empty($store_custom_fields['go_mta_store_cost'][0]) ? unserialize($store_custom_fields['go_mta_store_cost'][0]) : array();
                                $currency = $store_cost[0] < 0 ? $store_cost[0] : 0;
                                $points = $store_cost[1] < 0 ? $store_cost[1] : 0;
                                $bonus_currency = $store_cost[2] < 0 ? $store_cost[2] : 0;
                                $penalty = $store_cost[3] > 0 ? $store_cost[3] : 0;
                                $minutes = $store_cost[4] < 0 ? $store_cost[4] : 0;
                                $loot_reason = $bonus_loot[2][$store_item] * 10 > 999 ? 'quest' : 'bonus';
                                $user_id = get_current_user_id();
                                $received = $wpdb->query($wpdb->prepare("SELECT * FROM {$go_table_name}  WHERE uid = %d AND status = %d AND gifted = %d AND post_id = %d AND reason = 'Quest' OR reason = 'Bonus' ORDER BY timestamp DESC, reason DESC, id DESC LIMIT 1", $user_id, -1, 0, $store_item));
                                if ($received) {
                                    go_update_totals($user_id, $points, $currency, $bonus_currency, 0, $minutes, null, $loot_reason, true, false);
                                }
                                $wpdb->query($wpdb->prepare("DELETE FROM {$go_table_name}  WHERE uid = %d AND status = %d AND gifted = %d AND post_id = %d AND reason = 'Quest' OR reason = 'Bonus' ORDER BY timestamp DESC, reason DESC, id DESC LIMIT 1", $user_id, -1, 0, $store_item));
                            }
                        }
                    }
                }
                if ($stage_badges[$status][0] == 'true') {
                    foreach ($stage_badges[$status][1] as $badge_id) {
                        go_remove_badge($user_id, $badge_id);
                    }
                }
            }
        } else {
            // if repeat is on and undo is not hit...
            go_add_post($user_id, $post_id, $status, floor($update_percent * $points_array[$status]), floor($update_percent * $currency_array[$status]), floor($update_percent * $bonus_currency_array[$status]), null, $page_id, $repeat_button, 1, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, $url);
            if ($stage_badges[$status][0] == 'true') {
                foreach ($stage_badges[$status][1] as $badge_id) {
                    do_shortcode("[go_award_badge id='{$badge_id}' repeat='off' uid='{$user_id}']");
                }
            }
        }
        // if the button pressed is NOT the repeat button...
    } else {
        $db_status = (int) $wpdb->get_var("SELECT `status` FROM {$go_table_name} WHERE uid = {$user_id} AND post_id = {$post_id}");
        if ($db_status == 0 || $db_status < $status) {
            if ($undo == 'true' || $undo === true) {
                if ($task_count > 0) {
                    go_add_post($user_id, $post_id, $status, -floor($update_percent * $points_array[$status - 1]), -floor($update_percent * $currency_array[$status - 1]), -floor($update_percent * $bonus_currency_array[$status - 1]), null, $page_id, $repeat_button, -1, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed);
                } else {
                    go_add_post($user_id, $post_id, $status - 2, -floor($update_percent * $points_array[$status - 2]), -floor($update_percent * $currency_array[$status - 2]), -floor($update_percent * $bonus_currency_array[$status - 2]), null, $page_id, $repeat_button, 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed);
                    if ($stage_badges[$status - 2][0] == 'true') {
                        foreach ($stage_badges[$status - 2][1] as $badge_id) {
                            go_remove_badge($user_id, $badge_id);
                        }
                    }
                }
            } else {
                $update_time = $status == 2 ? true : false;
                go_add_post($user_id, $post_id, $status, floor($update_percent * $points_array[$status - 1]), floor($update_percent * $currency_array[$status - 1]), floor($update_percent * $bonus_currency_array[$status - 1]), null, $page_id, $repeat_button, 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, $url, $update_time);
                if ($stage_badges[$status - 1][0] == 'true') {
                    foreach ($stage_badges[$status - 1][1] as $badge_id) {
                        do_shortcode("[go_award_badge id='{$badge_id}' repeat='off' uid='{$user_id}']");
                    }
                }
            }
        }
    }
    // redefine the status and task_count because they have been updated as soon as the above go_add_post() calls are made.
    $status = $wpdb->get_var("SELECT `status` FROM {$go_table_name} WHERE uid = {$user_id} AND post_id = {$post_id}");
    $task_count = $wpdb->get_var("SELECT `count` FROM {$go_table_name} WHERE post_id = {$post_id} AND uid = {$user_id}");
    if ($undo == "false") {
        if ($task_count == 1) {
            go_record_stage_time($post_id, 5);
        } else {
            go_record_stage_time($post_id, $status);
        }
    }
    switch ($status) {
        case 0:
            $db_task_stage_upload_var = 'e_uploaded';
            break;
        case 1:
            $db_task_stage_upload_var = 'a_uploaded';
            break;
        case 2:
            $db_task_stage_upload_var = 'c_uploaded';
            break;
        case 3:
            $db_task_stage_upload_var = 'm_uploaded';
            break;
        case 4:
            $db_task_stage_upload_var = 'r_uploaded';
            break;
    }
    if (!empty($db_task_stage_upload_var)) {
        $is_uploaded = $wpdb->get_var("SELECT {$db_task_stage_upload_var} FROM {$go_table_name} WHERE uid = {$user_id} AND post_id = {$post_id}");
    } else {
        $is_uploaded = 0;
    }
    // Controls output of the page, after the #go_button has been pressed for the first time while viewing the page.
    switch ($status) {
        case 1:
            echo '<div id="new_content">' . '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div>';
            if ($test_e_active) {
                echo "<p id='go_test_error_msg' style='color: red;'></p>";
                if ($test_e_num > 1) {
                    for ($i = 0; $i < $test_e_num; $i++) {
                        echo do_shortcode("[go_test type='" . $test_e_all_types[$i] . "' question='" . $test_e_all_questions[$i] . "' possible_answers='" . $test_e_all_answers[$i] . "' key='" . $test_e_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_e_num . "']");
                    }
                    echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                } else {
                    echo do_shortcode("[go_test type='" . $test_e_all_types[0] . "' question='" . $test_e_all_questions[0] . "' possible_answers='" . $test_e_all_answers[0] . "' key='" . $test_e_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                }
            }
            if ($encounter_upload) {
                echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
            }
            echo "<p id='go_stage_error_msg' style='display: none; color: red;'></p>";
            if ($e_is_locked === 'true' && !empty($e_pass_lock)) {
                echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
            } elseif ($e_url_is_locked === true) {
                echo "<input id='go_url_key' type='url' placeholder='Enter Url'/></br>";
            }
            echo "<button id='go_button' status='2' onclick='task_stage_change( this );'";
            if ($e_is_locked === 'true' && empty($e_pass_lock)) {
                echo "admin_lock='true'";
            }
            echo ">" . go_return_options('go_second_stage_button') . "</button>\n\t\t\t<button id='go_abandon_task' onclick='go_task_abandon();this.disabled = true;'>" . get_option('go_abandon_stage_button', 'Abandon') . "</button></div>" . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
            break;
        case 2:
            echo '<div id="new_content">' . '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div>';
            if ($test_a_active) {
                echo "<p id='go_test_error_msg' style='color: red;'></p>";
                if ($test_a_num > 1) {
                    for ($i = 0; $i < $test_a_num; $i++) {
                        echo do_shortcode("[go_test type='" . $test_a_all_types[$i] . "' question='" . $test_a_all_questions[$i] . "' possible_answers='" . $test_a_all_answers[$i] . "' key='" . $test_a_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_a_num . "']");
                    }
                    echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                } else {
                    echo do_shortcode("[go_test type='" . $test_a_all_types[0] . "' question='" . $test_a_all_questions[0] . "' possible_answers='" . $test_a_all_answers[0] . "' key='" . $test_a_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                }
            }
            if ($accept_upload) {
                echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
            }
            echo "<p id='go_stage_error_msg' style='display: none; color: red;'></p>";
            if ($a_is_locked === 'true' && !empty($a_pass_lock)) {
                echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
            } elseif ($a_url_is_locked === true) {
                echo "<input id='go_url_key' type='url' placeholder='Enter Url'/></br>";
            }
            echo "<button id='go_button' status='3' onclick='task_stage_change( this );'";
            if ($a_is_locked === 'true' && empty($a_pass_lock)) {
                echo "admin_lock='true'";
            }
            echo '>' . go_return_options('go_third_stage_button') . '</button> <button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button></div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
            break;
        case 3:
            echo '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div>' . '<div id="new_content"><div class="go_stage_message">' . do_shortcode(wpautop($completion_message)) . '</div>';
            if ($mastery_active) {
                if ($test_c_active) {
                    echo "<p id='go_test_error_msg' style='color: red;'></p>";
                    if ($test_c_num > 1) {
                        for ($i = 0; $i < $test_c_num; $i++) {
                            echo do_shortcode("[go_test type='" . $test_c_all_types[$i] . "' question='" . $test_c_all_questions[$i] . "' possible_answers='" . $test_c_all_answers[$i] . "' key='" . $test_c_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_c_num . "']");
                        }
                        echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    } else {
                        echo do_shortcode("[go_test type='" . $test_c_all_types[0] . "' question='" . $test_c_all_questions[0] . "' possible_answers='" . $test_c_all_answers[0] . "' key='" . $test_c_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    }
                }
                if ($completion_upload) {
                    echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                }
                echo "<p id='go_stage_error_msg' style='display: none; color: red;'></p>";
                if ($c_is_locked === 'true' && !empty($c_pass_lock)) {
                    echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
                } elseif ($c_url_is_locked === true) {
                    echo "<input id='go_url_key' type='url' placeholder='Enter Url'/></br>";
                }
                echo "<button id='go_button' status='4' onclick='task_stage_change( this );'";
                if ($c_is_locked === 'true' && empty($c_pass_lock)) {
                    echo "admin_lock='true'";
                }
                echo '>' . go_return_options('go_fourth_stage_button') . '</button> 
				<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>';
                if ($next_post_id_in_chain != 0 && $last_in_chain !== 'true') {
                    echo '<div class="go_chain_message">Next ' . strtolower(go_return_options('go_tasks_name')) . ' in ' . $chain_name . ': <a href="' . get_permalink($next_post_id_in_chain) . '">' . get_the_title($next_post_id_in_chain) . '</a></div>';
                } else {
                    echo '<div class="go_chain_message">' . $final_chain_message . '</div>';
                }
                echo "</div>" . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
            } else {
                if ($test_c_active) {
                    echo "<p id='go_test_error_msg' style='color: red;'></p>";
                    if ($test_c_num > 1) {
                        for ($i = 0; $i < $test_c_num; $i++) {
                            echo do_shortcode("[go_test type='" . $test_c_all_types[$i] . "' question='" . $test_c_all_questions[$i] . "' possible_answers='" . $test_c_all_answers[$i] . "' key='" . $test_c_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_c_num . "']");
                        }
                        echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    } else {
                        echo do_shortcode("[go_test type='" . $test_c_all_types[0] . "' question='" . $test_c_all_questions[0] . "' possible_answers='" . $test_c_all_answers[0] . "' key='" . $test_c_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    }
                }
                if ($completion_upload) {
                    echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                }
                echo '<span id="go_button" status="4" style="display:none;"></span><button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>';
                if ($next_post_id_in_chain != 0 && $last_in_chain !== 'true') {
                    echo '<div class="go_chain_message">Next ' . strtolower(go_return_options('go_tasks_name')) . ' in ' . $chain_name . ': <a href="' . get_permalink($next_post_id_in_chain) . '">' . get_the_title($next_post_id_in_chain) . '</a></div>';
                } else {
                    echo '<div class="go_chain_message">' . $final_chain_message . '</div>';
                }
                echo "</div>" . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
            }
            break;
        case 4:
            echo '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div><div class="go_stage_message">' . do_shortcode(wpautop($completion_message)) . '</div><div id="new_content"><div class="go_stage_message">' . do_shortcode(wpautop($mastery_message)) . '</div>';
            // if the task can be repeated...
            if ($repeat == 'on') {
                // if the number of times that the page has been repeated is less than the total amount of repeats allowed OR if the
                // total repeats allowed is equal to zero (infinte amount allowed)...
                if ($task_count < $repeat_amount || $repeat_amount == 0) {
                    if ($task_count == 0) {
                        if ($test_m_active) {
                            echo "<p id='go_test_error_msg' style='color: red;'></p>";
                            if ($test_m_num > 1) {
                                for ($i = 0; $i < $test_m_num; $i++) {
                                    echo do_shortcode("[go_test type='" . $test_m_all_types[$i] . "' question='" . $test_m_all_questions[$i] . "' possible_answers='" . $test_m_all_answers[$i] . "' key='" . $test_m_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_m_num . "']");
                                }
                                echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                            } else {
                                echo do_shortcode("[go_test type='" . $test_m_all_types[0] . "' question='" . $test_m_all_questions[0] . "' possible_answers='" . $test_m_all_answers[0] . "' key='" . $test_m_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                            }
                        }
                        if ($mastery_upload) {
                            echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                        }
                        echo '
							<div id="repeat_quest">
								<div id="go_repeat_clicked" style="display:none;"><div class="go_stage_message">' . do_shortcode(wpautop($repeat_message)) . "</div><p id='go_stage_error_msg' style='display: none; color: red;'></p>";
                        if ($m_is_locked === 'true' && !empty($m_pass_lock)) {
                            echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
                        } elseif ($m_url_is_locked === true) {
                            echo "<input id='go_url_key' type='url' placeholder='Enter Url'/></br>";
                        }
                        echo "<button id='go_button' status='4' onclick='go_repeat_hide( this );' repeat='on'";
                        if ($m_is_locked === 'true' && empty($m_pass_lock)) {
                            echo "admin_lock='true'";
                        }
                        echo '>' . go_return_options('go_fourth_stage_button') . " Again" . '</button>
									<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>
								</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "") . '<div id="go_repeat_unclicked">
									<button id="go_button" status="4" onclick="go_repeat_replace();">' . get_option('go_fifth_stage_button') . '</button>
									<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>
								</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "") . '</div>
						';
                    } else {
                        if ($repeat_upload) {
                            echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                        }
                        echo '
							<div id="repeat_quest">
								<div id="go_repeat_clicked" style="display:none;"><div class="go_stage_message">' . do_shortcode(wpautop($repeat_message)) . "</div><p id='go_stage_error_msg' style='display: none; color: red;'></p>";
                        if ($r_is_locked === 'true' && !empty($r_pass_lock)) {
                            echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
                        }
                        echo "<button id='go_button' status='4' onclick='go_repeat_hide( this );' repeat='on'";
                        if ($r_is_locked === 'true' && empty($r_pass_lock)) {
                            echo "admin_lock='true'";
                        }
                        echo '>' . go_return_options('go_fourth_stage_button') . " Again" . '</button>
									<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>
								</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "") . '<div id="go_repeat_unclicked">
									<button id="go_button" status="4" onclick="go_repeat_replace();">' . get_option('go_fifth_stage_button') . '</button>
									<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>
								</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "") . '</div>
						';
                    }
                } else {
                    echo '<span id="go_button" status="4" repeat="on" style="display:none;"></span><button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
                }
            } else {
                if ($test_m_active) {
                    echo "<p id='go_test_error_msg' style='color: red;'></p>";
                    if ($test_m_num > 1) {
                        for ($i = 0; $i < $test_m_num; $i++) {
                            echo do_shortcode("[go_test type='" . $test_m_all_types[$i] . "' question='" . $test_m_all_questions[$i] . "' possible_answers='" . $test_m_all_answers[$i] . "' key='" . $test_m_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_m_num . "']");
                        }
                        echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    } else {
                        echo do_shortcode("[go_test type='" . $test_m_all_types[0] . "' question='" . $test_m_all_questions[0] . "' possible_answers='" . $test_m_all_answers[0] . "' key='" . $test_m_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    }
                }
                if ($mastery_upload) {
                    echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                }
                $mastered = (array) get_user_meta($user_id, 'mastered_tasks', true);
                $user_id = get_current_user_id();
                echo !empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "";
                if ($bonus_loot[0]) {
                    if (!empty($bonus_loot[1])) {
                        foreach ($bonus_loot[1] as $store_item => $on) {
                            if ($on === 'on') {
                                $random_number = rand(1, 999);
                                $store_custom_fields = get_post_custom($store_item);
                                $store_cost = !empty($store_custom_fields['go_mta_store_cost'][0]) ? unserialize($store_custom_fields['go_mta_store_cost'][0]) : array();
                                $currency = $store_cost[0] < 0 ? -$store_cost[0] : 0;
                                $points = $store_cost[1] < 0 ? -$store_cost[1] : 0;
                                $bonus_currency = $store_cost[2] < 0 ? -$store_cost[2] : 0;
                                $penalty = $store_cost[3] > 0 ? -$store_cost[3] : 0;
                                $minutes = $store_cost[4] < 0 ? -$store_cost[4] : 0;
                                $loot_reason = $bonus_loot[2][$store_item] * 10 > 999 ? 'Quest' : 'Bonus';
                                if ($random_number < $bonus_loot[2][$store_item] * 10) {
                                    if (!in_array($post_id, $mastered) && $loot_reason == 'Bonus') {
                                        go_add_post($user_id, $store_item, -1, $points, $currency, $bonus_currency, $minutes, null, 'off', 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null, false, $loot_reason, 'bonus', false, false);
                                        echo "Congrats, " . do_shortcode('[go_get_displayname]') . "!  You received an item: <a href='#' onclick='go_lb_opener({$store_item})'>" . get_the_title($store_item) . "</a></br>";
                                    } elseif ($loot_reason == 'Quest') {
                                        go_add_post($user_id, $store_item, -1, $points, $currency, $bonus_currency, $minutes, null, 'off', 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null, false, $loot_reason, 'quest', false, false);
                                        echo "Congrats, " . do_shortcode('[go_get_displayname]') . "!  You received an item: <a href='#' onclick='go_lb_opener({$store_item})'>" . get_the_title($store_item) . "</a></br>";
                                    }
                                }
                            }
                        }
                    }
                    if (!in_array($post_id, $mastered)) {
                        $mastered[] = $post_id;
                        update_user_meta($user_id, 'mastered_tasks', $mastered);
                    }
                    echo "</br>";
                }
                add_user_meta($user_id, 'ever_mastered', $post_id, true);
                echo '<span id="go_button" status="4" repeat="on" style="display:none;"></span><button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>';
            }
            if ($next_post_id_in_chain != 0 && $last_in_chain !== 'true') {
                echo '<div class="go_chain_message"><p>Next ' . strtolower(go_return_options('go_tasks_name')) . ' in ' . $chain_name . ': <a href="' . get_permalink($next_post_id_in_chain) . '">' . get_the_title($next_post_id_in_chain) . '</a></div>';
            } else {
                echo '<div class="go_chain_message">' . $final_chain_message . '</div>';
            }
            echo '</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
    }
    die;
}
Пример #4
0
function go_buy_item()
{
    global $wpdb;
    $go_table_name = $wpdb->prefix . "go";
    $post_id = $_POST["the_id"];
    $qty = $_POST['qty'];
    $current_purchase_count = $_POST['purchase_count'];
    if (isset($_POST['recipient']) && !empty($_POST['recipient']) && $_POST['recipient'] != '') {
        $recipient = $_POST['recipient'];
        $recipient_id = $wpdb->get_var('SELECT id FROM ' . $wpdb->users . ' WHERE display_name="' . $recipient . '"');
        $recipient_purchase_count = $wpdb->get_var("SELECT count FROM {$table_name_go} WHERE post_id={$post_id} AND uid={$recipient_id} LIMIT 1");
    }
    $user_id = get_current_user_id();
    $custom_fields = get_post_custom($post_id);
    $sending_receipt = $custom_fields['go_mta_store_receipt'][0];
    $store_cost = unserialize($custom_fields['go_mta_store_cost'][0]);
    if (!empty($store_cost)) {
        $req_currency = $store_cost[0];
        $req_points = $store_cost[1];
        $req_bonus_currency = $store_cost[2];
        $req_penalty = $store_cost[3];
        $req_minutes = $store_cost[4];
    }
    $penalty = $custom_fields['go_mta_penalty_switch'];
    $store_limit = unserialize($custom_fields['go_mta_store_limit'][0]);
    $is_limited = (bool) $store_limit[0];
    if ($is_limited) {
        $limit = (int) $store_limit[1];
    }
    $store_filter = unserialize($custom_fields['go_mta_store_filter'][0]);
    $is_filtered = (bool) $store_filter[0];
    if ($is_filtered) {
        $req_rank = $store_filter[1];
        $bonus_filter = $store_filter[2];
    }
    $store_exchange = unserialize($custom_fields['go_mta_store_exchange'][0]);
    $is_exchangeable = (bool) $store_exchange[0];
    if ($is_exchangeable) {
        $exchange_currency = $store_exchange[1];
        $exchange_points = $store_exchange[2];
        $exchange_bonus_currency = $store_exchange[3];
    }
    $item_url = $custom_fields['go_mta_store_item_url'][0];
    $badge_id = $custom_fields['go_mta_badge_id'][0];
    $store_focus = $custom_fields['go_mta_store_focus'][0];
    $is_focused = (bool) $store_focus[0];
    if ($is_focused) {
        $item_focus = $store_focus[1];
    }
    $repeat = 'off';
    $cur_currency = go_return_currency($user_id);
    $cur_points = go_return_points($user_id);
    $cur_bonus_currency = go_return_bonus_currency($user_id);
    $cur_penalty = go_return_penalty($user_id);
    $cur_minutes = go_return_minutes($user_id);
    $enough_currency = check_values($req_currency, $cur_currency);
    $enough_points = check_values($req_points, $cur_points);
    $enough_bonus_currency = check_values($req_bonus_currency, $cur_bonus_currency);
    $enough_penalty = check_values($req_penalty, $cur_penalty);
    $enough_minutes = check_values($req_minutes, $cur_minutes);
    $within_limit = true;
    if (!empty($limit)) {
        $qty_diff = $limit - $current_purchase_count - $qty;
        if ($qty_diff < 0) {
            $within_limit = false;
        }
    }
    if (($enough_currency && $enough_bonus_currency && $enough_points && $enough_minutes || $penalty) && $within_limit) {
        if ($is_focused && !empty($item_focus)) {
            $user_focuses = (array) get_user_meta($user_id, 'go_focus', true);
            $user_focuses[] = $item_focus;
            update_user_meta($user_id, 'go_focus', $user_focuses);
        }
        if ($recipient_id) {
            go_message_user($recipient_id, get_userdata($user_id)->display_name . " has purchased {$qty} <a href='javascript:;' onclick='go_lb_opener({$post_id})'>" . get_the_title($post_id) . "</a> for you.");
            if ($exchange_currency || $exchange_points || $exchange_bonus_currency) {
                go_add_post($recipient_id, $post_id, -1, $exchange_points, $exchange_currency, $exchange_bonus_currency, null, null, $repeat);
                go_add_bonus_currency($recipient_id, $exchange_bonus_currency, get_userdata($user_id)->display_name . " purchase of {$qty} " . get_the_title($post_id) . ".");
            } else {
                go_add_post($recipient_id, $post_id, -1, 0, 0, 0, null, $repeat);
            }
            go_add_post($user_id, $post_id, -1, -$req_points, -$req_currency, -$req_bonus_currency, -$req_minutes, null, $repeat);
            $wpdb->query($wpdb->prepare("UPDATE {$go_table_name} SET reason = 'Gifted' WHERE uid = %d AND status = %d AND gifted = %d AND post_id = %d ORDER BY timestamp DESC, reason DESC, id DESC LIMIT 1", $user_id, -1, 0, $post_id));
        } else {
            go_add_post($user_id, $post_id, -1, -$req_points, -$req_currency, -$req_bonus_currency, -$req_minutes, null, $repeat);
            if (!empty($req_penalty)) {
                go_add_penalty($user_id, -$req_penalty, get_the_title($post_id));
            }
        }
        if (!empty($badge_id)) {
            if ($recipient_id) {
                do_shortcode('[go_award_badge id="' . $badge_id . '" repeat = "off" uid="' . $recipient_id . '"]');
            } else {
                do_shortcode('[go_award_badge id="' . $badge_id . '" repeat = "off" uid="' . $user_id . '"]');
            }
        }
        if (!empty($item_url) && isset($item_url)) {
            $item_hyperlink = '<a target="_blank" href="' . $item_url . '">Grab your loot!</a>';
            echo $item_hyperlink;
        } else {
            echo "Purchased";
        }
        if ($sending_receipt === 'true') {
            $receipt = go_mail_item_reciept($user_id, $post_id, $req_currency, $req_points, $req_bonus_currency, $req_minutes, $qty, $recipient_id);
            if (!empty($receipt)) {
                echo $receipt;
            }
        }
    } else {
        $currency_name = go_return_options('go_currency_name');
        $points_name = go_return_options('go_points_name');
        $bonus_currency_name = go_return_options('go_bonus_currency_name');
        $minutes_name = go_return_options('go_minutes_name');
        $enough_array = array($currency_name => $enough_currency, $points_name => $enough_points, $bonus_currency_name => $enough_bonus_currency, $minutes_name => $enough_minutes);
        $errors = array();
        foreach ($enough_array as $key => $enough) {
            if (!$enough) {
                $errors[] = $key;
            }
        }
        if (!empty($errors)) {
            $errors = implode(', ', $errors);
            echo 'Need more ' . substr($errors, 0, strlen($errors));
        }
        if ($is_limited && !$within_limit) {
            $qty_diff *= -1;
            echo "You've attempted to purchase " . ($qty_diff == 1 ? '1 item' : "{$qty_diff} items") . " greater than the purchase limit.";
        }
    }
    die;
}
Пример #5
0
function go_stats_move_stage()
{
    global $wpdb;
    $go_table_name = "{$wpdb->prefix}go";
    if (!empty($_POST['user_id'])) {
        $user_id = $_POST['user_id'];
    } else {
        $user_id = get_current_user_id();
    }
    $current_rank = get_user_meta($user_id, 'go_rank', true);
    $task_id = $_POST['task_id'];
    $status = $_POST['status'];
    $count = $_POST['count'];
    $message = $_POST['message'];
    $custom_fields = get_post_custom($task_id);
    $date_picker = !empty($custom_fields['go_mta_date_picker'][0]) && unserialize($custom_fields['go_mta_date_picker'][0]) ? array_filter(unserialize($custom_fields['go_mta_date_picker'][0])) : null;
    $rewards = unserialize($custom_fields['go_presets'][0]);
    $current_status = $wpdb->get_var($wpdb->prepare("SELECT status FROM {$go_table_name} WHERE uid=%d AND post_id=%d", $user_id, $task_id));
    $page_id = $wpdb->get_var($wpdb->prepare("SELECT page_id FROM {$go_table_name} WHERE uid=%d AND post_id=%d", $user_id, $task_id));
    $changed = array('type' => 'json', 'points' => 0, 'currency' => 0, 'bonus_currency' => 0);
    if (!empty($date_picker)) {
        $dates = $date_picker['date'];
        $percentages = $date_picker['percent'];
        $unix_today = strtotime(date('Y-m-d'));
        $past_dates = array();
        foreach ($dates as $key => $date) {
            if ($unix_today >= strtotime($date)) {
                $past_dates[$key] = abs($unix_today - strtotime($date));
            }
        }
        if (!empty($past_dates)) {
            asort($past_dates);
            $update_percent = (double) ($percentages[key($past_dates)] / 100);
        } else {
            $update_percent = 1;
        }
    } else {
        $update_percent = 1;
    }
    if ($status == 1) {
        $current_rewards = $wpdb->get_results($wpdb->prepare("SELECT points, currency, bonus_currency FROM {$go_table_name} WHERE uid=%d AND post_id=%d", $user_id, $task_id));
        go_task_abandon($user_id, $task_id, $current_rewards[0]->points, $current_rewards[0]->currency, $current_rewards[0]->bonus_currency * $update_percent);
        $changed['points'] = -$current_rewards[0]->points;
        $changed['currency'] = -$current_rewards[0]->currency;
        $changed['bonus_currency'] = -$current_rewards[0]->bonus_currency;
        $current_points = go_return_points($user_id);
        $updated_rank = get_user_meta($user_id, 'go_rank', true);
        if ($current_rank[0][0] != $updated_rank[0][0]) {
            $changed['current_points'] = $current_points;
            $changed['rank'] = $updated_rank[0][0];
            $changed['rank_points'] = $updated_rank[0][1];
            $changed['next_rank_points'] = $updated_rank[1][1];
        }
        $changed['abandon'] = 'true';
        if ($message === 'See me') {
            go_message_user($user_id, $message . ' about, <a href="' . get_permalink($task_id) . '" style="display: inline-block; text-decoration: underline; padding: 0px; margin: 0px;">' . get_the_title($task_id) . '</a>, please.');
        } else {
            go_message_user($user_id, 'RE: <a href="' . get_permalink($task_id) . '">' . get_the_title($task_id) . '</a> ' . $message);
        }
    } else {
        for ($count; $count > 0; $count--) {
            go_add_post($user_id, $task_id, $current_status, floor(-$rewards['points'][$current_status] * $update_percent), floor(-$rewards['currency'][$current_status] * $update_percent), floor(-$rewards['bonus_currency'][$current_status] * $update_percent), null, $page_id, 'on', -1, null, null, null, null);
            $changed['points'] += floor(-$rewards['points'][$current_status] * $update_percent);
            $changed['currency'] += floor(-$rewards['currency'][$current_status] * $update_percent);
            $changed['bonus_currency'] += floor(-$rewards['bonus_currency'][$current_status] * $update_percent);
        }
        while ($current_status != $status) {
            if ($current_status > $status) {
                $current_status--;
                go_add_post($user_id, $task_id, $current_status, floor(-$rewards['points'][$current_status] * $update_percent), floor(-$rewards['currency'][$current_status] * $update_percent), floor(-$rewards['bonus_currency'][$current_status] * $update_percent), null, $page_id, null, null, null, null, null, null);
                $changed['points'] += floor(-$rewards['points'][$current_status] * $update_percent);
                $changed['currency'] += floor(-$rewards['currency'][$current_status] * $update_percent);
                $changed['bonus_currency'] += floor(-$rewards['bonus_currency'][$current_status] * $update_percent);
            } elseif ($current_status < $status) {
                $current_status++;
                $current_count = $wpdb->get_var($wpdb->prepare("SELECT count FROM {$go_table_name} WHERE uid=%d AND post_id=%d", $user_id, $task_id));
                if ($current_status == 5 && $current_count == 0) {
                    go_add_post($user_id, $task_id, $current_status - 1, floor($rewards['points'][$current_status - 1] * $update_percent), floor($rewards['currency'][$current_status - 1] * $update_percent), floor($rewards['bonus_currency'][$current_status - 1] * $update_percent), null, $page_id, 'on', 1, null, null, null, null);
                    $changed['points'] += floor($rewards['points'][$current_status - 1] * $update_percent);
                    $changed['currency'] += floor($rewards['currency'][$current_status - 1] * $update_percent);
                    $changed['bonus_currency'] += floor($rewards['bonus_currency'][$current_status - 1] * $update_percent);
                } elseif ($current_status < 5) {
                    go_add_post($user_id, $task_id, $current_status, floor($rewards['points'][$current_status - 1] * $update_percent), floor($rewards['currency'][$current_status - 1] * $update_percent), floor($rewards['bonus_currency'][$current_status - 1] * $update_percent), null, $page_id, null, null, null, null, null, null);
                    $changed['points'] += floor($rewards['points'][$current_status - 1] * $update_percent);
                    $changed['currency'] += floor($rewards['currency'][$current_status - 1] * $update_percent);
                    $changed['bonus_currency'] += floor($rewards['bonus_currency'][$current_status - 1] * $update_percent);
                }
            }
        }
        if ($message === 'See me') {
            go_message_user($user_id, $message . ' about, <a href="' . get_permalink($task_id) . '" style="display: inline-block; text-decoration: underline; padding: 0px; margin: 0px;">' . get_the_title($task_id) . '</a>, please.');
        } else {
            go_message_user($user_id, 'RE: <a href="' . get_permalink($task_id) . '">' . get_the_title($task_id) . '</a> ' . $message);
        }
        $current_points = go_return_points($user_id);
        $updated_rank = get_user_meta($user_id, 'go_rank', true);
        if ($current_rank[0][0] != $updated_rank[0][0]) {
            $changed['current_points'] = $current_points;
            $changed['rank'] = $updated_rank[0][0];
            $changed['rank_points'] = $updated_rank[0][1];
            $changed['next_rank_points'] = $updated_rank[1][1];
        }
    }
    echo json_encode($changed);
    die;
}