/** * AJAX Call Handler * @since 1.2 * @version 1.2.1 */ public function ajax_call_video_points() { // We must be logged in if (!is_user_logged_in()) { die; } // Security check_ajax_referer('mycred-video-points', 'token'); // We are only interested in handling our own point type // We can not die here since then the hook will not fire for the correct point type. if (!isset($_POST['type']) || $this->mycred_type != sanitize_key($_POST['type'])) { return; } // Get user id $user_id = get_current_user_id(); // Decode the key giving us the video shortcode setup // This will prevent users from manipulating the shortcode output $setup = mycred_verify_token($_POST['setup'], 5); if ($setup === false) { die(0); } list($source, $video_id, $amount, $logic, $interval) = $setup; // Required if (empty($source) || empty($video_id)) { die; } // Prep $amount = $this->core->number($amount); $interval = abs($interval / 1000); // Get playback details $actions = sanitize_text_field($_POST['video_a']); $seconds = abs($_POST['video_b']); $duration = abs($_POST['video_c']); $state = absint($_POST['video_d']); // Apply Leniency $leniency = $duration * ($this->prefs['leniency'] / 100); $leniency = floor($leniency); $watched = $seconds + $leniency; $status = 'silence'; switch ($logic) { // Award points when video starts case 'play': if ($state == 1) { if (!$this->has_entry('watching_video', '', $user_id, $video_id, $this->mycred_type)) { // Execute $this->core->add_creds('watching_video', $user_id, $amount, $this->prefs['log'], 0, $video_id, $this->mycred_type); $status = 'added'; } else { $status = 'max'; } } break; // Award points when video is viewed in full // Award points when video is viewed in full case 'full': // Check for skipping or if we watched more (with leniency) then the video length if (!preg_match('/22/', $actions, $matches) || $watched >= $duration) { if ($state == 0) { if (!$this->has_entry('watching_video', '', $user_id, $video_id, $this->mycred_type)) { // Execute $this->core->add_creds('watching_video', $user_id, $amount, $this->prefs['log'], 0, $video_id, $this->mycred_type); $status = 'added'; } else { $status = 'max'; } } } break; // Award points in intervals // Award points in intervals case 'interval': // The maximum points a video can earn you $num_intervals = floor($duration / $interval); $max = abs($num_intervals * $amount); $users_log = $this->get_users_video_log($video_id, $user_id); // Film is playing and we just started if ($state == 1 && $users_log === NULL) { // Add points without using mycred_add to prevent // notifications from being sent as this amount will change. $this->core->update_users_balance($user_id, $amount); $this->core->add_to_log('watching_video', $user_id, $amount, $this->prefs['log'], 0, $video_id, $this->mycred_type); $status = 'added'; } elseif ($state == 1 && isset($users_log->creds) && $users_log->creds + $amount <= $max) { $this->update_creds($users_log->id, $user_id, $users_log->creds + $amount); $this->core->update_users_balance($user_id, $amount); $amount = $users_log->creds + $amount; $status = 'added'; } elseif ($state == 0 && isset($users_log->creds) && $users_log->creds + $amount <= $max) { $this->update_creds($users_log->id, $user_id, $users_log->creds + $amount); $this->core->update_users_balance($user_id, $amount); $amount = $users_log->creds + $amount; $status = 'max'; // If enabled, add notification if (function_exists('mycred_add_new_notice')) { if ($amount < 0) { $color = '<'; } else { $color = '>'; } $message = str_replace('%amount%', $amount, $this->prefs['template']); if (!empty($message)) { mycred_add_new_notice(array('user_id' => $user_id, 'message' => $message, 'color' => $color)); } } } break; } wp_send_json(array('status' => $status, 'video_id' => $video_id, 'amount' => $amount, 'duration' => $duration, 'seconds' => $seconds, 'watched' => $watched, 'actions' => $actions, 'state' => $state, 'logic' => $logic, 'interval' => $interval)); }
function mycred_catch_exchange_requests() { if (!isset($_POST['mycred_exchange']['nonce']) || !wp_verify_nonce($_POST['mycred_exchange']['nonce'], 'mycred-exchange')) { return; } // Decode token $token = mycred_verify_token($_POST['mycred_exchange']['token'], 5); if ($token === false) { return; } global $mycred_exchange; list($from, $to, $user_id, $rate, $min) = $token; // Check point types $types = mycred_get_types(); if (!array_key_exists($from, $types) || !array_key_exists($to, $types)) { $mycred_exchange = array('success' => false, 'message' => __('Point types not found.', 'mycred')); return; } $user_id = get_current_user_id(); // Check for exclusion $mycred_from = mycred($from); if ($mycred_from->exclude_user($user_id)) { $mycred_exchange = array('success' => false, 'message' => sprintf(__('You are excluded from using %s.', 'mycred'), $mycred_from->plural())); return; } // Check balance $balance = $mycred_from->get_users_balance($user_id, $from); if ($balance < $mycred_from->number($min)) { $mycred_exchange = array('success' => false, 'message' => __('Your balance is too low to use this feature.', 'mycred')); return; } // Check for exclusion $mycred_to = mycred($to); if ($mycred_to->exclude_user($user_id)) { $mycred_exchange = array('success' => false, 'message' => sprintf(__('You are excluded from using %s.', 'mycred'), $mycred_to->plural())); return; } // Prep Amount $amount = abs($_POST['mycred_exchange']['amount']); $amount = $mycred_from->number($amount); // Make sure we are sending more then minimum if ($amount < $min) { $mycred_exchange = array('success' => false, 'message' => sprintf(__('You must exchange at least %s!', 'mycred'), $mycred_from->format_creds($min))); return; } // Make sure we have enough points if ($amount > $balance) { $mycred_exchange = array('success' => false, 'message' => __('Insufficient Funds. Please try a lower amount.', 'mycred')); return; } // Let others decline $reply = apply_filters('mycred_decline_exchange', false, compact('from', 'to', 'user_id', 'rate', 'min', 'amount')); if ($reply === false) { $mycred_from->add_creds('exchange', $user_id, 0 - $amount, sprintf(__('Exchange from %s', 'mycred'), $mycred_from->plural()), 0, array('from' => $from, 'rate' => $rate, 'min' => $min), $from); $exchanged = $mycred_to->number($amount * $rate); $mycred_to->add_creds('exchange', $user_id, $exchanged, sprintf(__('Exchange to %s', 'mycred'), $mycred_to->plural()), 0, array('to' => $to, 'rate' => $rate, 'min' => $min), $to); $mycred_exchange = array('success' => true, 'message' => sprintf(__('You have successfully exchanged %s into %s.', 'mycred'), $mycred_from->format_creds($amount), $mycred_to->format_creds($exchanged))); } else { $mycred_exchange = array('success' => false, 'message' => $reply); return; } }