/** * Get user's coupon application. * * @since 1.0.0 * * @param int $user_id The user id. * @param int $membership_id The membership id. * @return MS_Addon_Coupon_Model The coupon model object. */ public static function get_application($user_id, $membership_id) { $key = self::get_transient_name($user_id, $membership_id); $transient = MS_Factory::get_transient($key); $coupon = null; if (is_array($transient) && !empty($transient['id'])) { $the_id = intval($transient['id']); $coupon = MS_Factory::load('MS_Addon_Coupon_Model', $the_id); $coupon->coupon_message = $transient['message']; } else { $coupon = MS_Factory::load('MS_Addon_Coupon_Model'); } return apply_filters('ms_addon_coupon_model_get_application', $coupon, $user_id, $membership_id); }
/** * Search for orphaned relationships and remove them. * * We write a custom SQL query for this, as solving it with a meta-query * structure is very performance intense and requires at least two queries * and a loop... * * For additional performance we will only do this check once every hour. * * Note: We cannot use the hook 'delete_user' to do this, because in * Multisite users are deleted via the Main network admin; however, there * we do not have access to the site data; especially if Plugin is not * network enabled... * * @todo Change this to use WP-Cron instead of own implementation... * * @since 1.0.0 * @internal */ public static function clean_db() { $timestamp = absint(MS_Factory::get_transient('ms_member_clean_db')); $elapsed = time() - $timestamp; if ($elapsed > 3600) { // Last check is longer than 1 hour ago. Check again. MS_Factory::set_transient('ms_member_clean_db', time(), 3600); } else { // Last check was within past hour. Do nothing yet... return; } global $wpdb; // Find all Relationships that have no post-author. $sql = "\n\t\tSELECT p.ID\n\t\tFROM {$wpdb->posts} p\n\t\tWHERE p.post_type=%s\n\t\tAND NOT EXISTS (\n\t\t\tSELECT 1\n\t\t\tFROM {$wpdb->users} u\n\t\t\tWHERE u.ID = p.post_author\n\t\t);\n\t\t"; $sql = $wpdb->prepare($sql, MS_Model_Relationship::get_post_type()); // Delete these Relationships! $items = $wpdb->get_results($sql); foreach ($items as $item) { $junk = MS_Factory::load('MS_Model_Relationship', $item->ID); $junk->delete(); } }
/** * Verfies the admin token in the $_GET collection * * $_GET['ms_token'] must match the current ms_token * $_POST['confirm'] must have value 'yes' * * @since 1.0.0 * @internal * * @param string $action Like a nonce, this is the action to execute. * @return bool */ private static function verify_token($action) { if (!self::valid_user()) { return false; } if (empty($_GET['ms_token'])) { return false; } $get_token = $_GET['ms_token']; if (empty($_POST['confirm'])) { return false; } if ('yes' != $_POST['confirm']) { return false; } $one_time_key = MS_Factory::get_transient('ms_one_time_key-' . $action); MS_Factory::delete_transient('ms_one_time_key-' . $action); if (empty($one_time_key)) { return false; } // We verify the current and the previous beat $plain_token_1 = $action . '-' . date('B') . ':' . get_current_user_id() . '-' . $one_time_key; $plain_token_2 = $action . '-' . (date('B') - 1) . ':' . get_current_user_id() . '-' . $one_time_key; if (wp_verify_nonce($get_token, $plain_token_1)) { return true; } if (wp_verify_nonce($get_token, $plain_token_2)) { return true; } return false; }
/** * Creates or updates the coupon specified by the function parameter. * * @since 1.0.0 * @internal * * @param array $coupon_data The object containing all details for Stripe. */ public function create_or_update_coupon($coupon_data) { $item_id = $coupon_data['id']; $all_items = MS_Factory::get_transient('ms_stripeplan_plans'); $all_items = lib2()->array->get($all_items); if (!isset($all_items[$item_id]) || !is_a($all_items[$item_id], 'M2_Stripe_Coupon')) { try { $item = M2_Stripe_Coupon::retrieve($item_id); } catch (Exception $e) { // If the coupon does not exist then stripe will throw an Exception. $item = false; } $all_items[$item_id] = $item; } else { $item = $all_items[$item_id]; } /* * Stripe can only update the coupon-name, so we have to delete and * recreate the coupon manually. */ if ($item && is_a($item, 'M2_Stripe_Coupon')) { $item->delete(); $all_items[$item_id] = false; } $item = M2_Stripe_Coupon::create($coupon_data); $all_items[$item_id] = $item; MS_Factory::set_transient('ms_stripeplan_coupons', $all_items, HOUR_IN_SECONDS); }
/** * Creates or updates a single coupon on Stripe. * * This function is called when the gateway is activated and after a * coupon was saved to database. * * @since 1.0.0 */ public function update_stripe_data_coupon($coupon) { if (!$this->active) { return false; } $this->_api->set_gateway($this); $settings = MS_Plugin::instance()->settings; $duration = 'once'; $percent_off = null; $amount_off = null; if (MS_Addon_Coupon_Model::TYPE_VALUE == $coupon->discount_type) { $amount_off = absint($coupon->discount * 100); } else { $percent_off = $coupon->discount; } $coupon_data = array('id' => self::get_the_id($coupon->id, 'coupon'), 'duration' => $duration, 'amount_off' => $amount_off, 'percent_off' => $percent_off, 'currency' => $settings->currency); // Check if the plan needs to be updated. $serialized_data = json_encode($coupon_data); $temp_key = substr('ms-stripe-' . $coupon_data['id'], 0, 45); $temp_data = MS_Factory::get_transient($temp_key); if ($temp_data != $serialized_data) { MS_Factory::set_transient($temp_key, $serialized_data, HOUR_IN_SECONDS); $this->_api->create_or_update_coupon($coupon_data); } }
/** * Get user's invitation application. * * @since 1.0.0 * * @param int $user_id The user id. * @param int $membership_id The membership id. * @return MS_Addon_Invitation_Model The invitation model object. */ public static function get_application($user_id, $membership_id) { $key = self::get_transient_name($user_id, $membership_id); $transient = MS_Factory::get_transient($key); $invitation = null; if (is_array($transient) && !empty($transient['id'])) { $the_id = intval($transient['id']); $invitation = MS_Factory::load('MS_Addon_Invitation_Model', $the_id); $invitation->invitation_message = $transient['message']; } else { $invitation = MS_Factory::load('MS_Addon_Invitation_Model'); } if ($invitation->is_valid()) { $invitation->invitation_message = __('Invitation code is correct.', 'membership2'); } return apply_filters('ms_addon_invitation_model_get_application', $invitation, $user_id, $membership_id); }