function ym_firesale_ppp_packs() { global $wpdb; // do all packs! foreach (ym_get_bundles() as $pack) { $pack_id = $pack->id; // get base data from the table $sql = 'SELECT original_cost FROM ' . $wpdb->ym_app_ppp_pack . ' WHERE pack_id = ' . $pack_id; $fire_original_cost = $wpdb->get_var($sql); $base_cost = number_format($pack->cost, 2); $fire_original_cost = $fire_original_cost ? $fire_original_cost : 0; $is_currently_firesale = $fire_original_cost ? TRUE : FALSE; if ($ym_firesale_id = ym_firesale_exists($pack_id, YM_APP_TYPE_PACK)) { // a fire sale exists $tier_data = ym_firesale_get_current_tier($ym_firesale_id); $tier_data = $tier_data[0]; if (!$fire_original_cost) { // starting a firesale $sql = 'INSERT INTO ' . $wpdb->ym_app_ppp_pack . '(pack_id, original_cost) VALUES (' . $pack_id . ', ' . $fire_original_cost . ')'; $wpdb->query($sql); ym_firesale_log(array('doing' => 'StartingPricingModel', 'packId' => $pack_id, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data)); ym_fire_sale_start($tier_data->fire_id); } // cost update if ($base_price != $tier_data->fire_price) { if ($tier_data) { // packs stores in pence/cents base unit $pack->cost = str_replace('.', '', $tier_data->fire_price); ym_firesale_log(array('doing' => 'TierChange', 'packId' => $pack_id, 'newPrice' => $tier_data->fire_price, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data)); ym_firesale_tier_log($tier_data->fire_tier_id); ym_fire_tier_start($tier_data->fire_tier_id); } else { // no tier $pack->cost = str_replace('.', '', $fire_original_cost); $sql = 'DELETE FROM ' . $wpdb->ym_app_ppp_pack . ' WHERE pack_id = ' . $pack_id; $wpdb->query($sql); ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data)); } } } else { if ($is_currently_firesale == 1) { // firesale needs resetting $pack->cost = $fire_original_cost; ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price)); } } // update the ppp pack // $all_packs[$key] = $pack; $pack->cost = number_format($pack->cost, 2, '.', '') * 100; //store in pence $sql = 'UPDATE ' . $wpdb->prefix . 'ym_post_pack SET cost = ' . $pack->cost . ' WHERE id = ' . $pack->id; $wpdb->query($sql); } }
function ym_firesale_subs() { // get all subs $packs = ym_get_packs(); foreach ($packs as $key => $pack) { $pack_id = $pack['id']; $base_price = $pack['cost']; $fire_original_cost = isset($pack['pre_firesale_cost']) ? $pack['pre_firesale_cost'] : FALSE; $is_currently_firesale = isset($pack['on_fire_sale']) ? $pack['on_fire_sale'] : FALSE; if ($ym_firesale_id = ym_firesale_exists($pack_id, YM_APP_TYPE_SUB)) { // a fire sale exists for this pack $tier_data = ym_firesale_get_current_tier($ym_firesale_id); $tier_data = $tier_data[0]; if (!$fire_original_cost) { // starting a fire sale $pack['pre_firesale_cost'] = $base_price; ym_firesale_log(array('doing' => 'StartingPricingModel', 'packId' => $pack_id, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data)); ym_fire_sale_start($tier_data->fire_id); } // check for tier change if ($base_price != $tier_data->fire_price) { if ($tier_data) { $pack['cost'] = $tier_data->fire_price; $pack['on_fire_sale'] = TRUE; ym_firesale_log(array('doing' => 'TierChange', 'packId' => $pack_id, 'newPrice' => $tier_data->fire_price, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data)); ym_firesale_tier_log($tier_data->fire_tier_id); ym_fire_tier_start($tier_data->fire_tier_id); } else { // no tier $pack['cost'] = $fire_original_cost; unset($pack['pre_firesale_cost']); unset($pack['on_fire_sale']); ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data)); } } } else { if ($is_currently_firesale == 1) { // firesale needs resetting as it has ended $pack['cost'] = $fire_original_cost; unset($pack['pre_firesale_cost']); unset($pack['on_fire_sale']); ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price)); } } // store the pack $packs[$key] = $pack; } // store the packs $obj_packs = get_option('ym_packs'); $obj_packs->packs = $packs; update_option('ym_packs', $obj_packs); }
function ym_firesale_ppp($button_code, $code, $post_id) { if (ym_post_is_purchasable()) { // post cane be bought, continue $post_id = get_the_ID(); // current post data $base_price = get_post_meta($post_id, '_ym_post_purchasable_cost', TRUE); $fire_original_cost = get_post_meta($post_id, '_ym_fire_original_cost', TRUE); $is_currently_firesale = get_post_meta($post_id, '_ym_fire_active', TRUE); if ($ym_firesale_id = ym_firesale_exists($post_id, YM_APP_TYPE_POST)) { // a fire sale exists for this ppp $tier_data = ym_firesale_get_current_tier($ym_firesale_id); $tier_data = $tier_data[0]; // check the post meta is correct if (!get_post_meta($post_id, '_ym_fire_original_cost')) { // no base cost // post is starting to go on firesale // store the original cost update_post_meta($post_id, '_ym_fire_original_cost', $base_price); ym_firesale_log(array('doing' => 'StartingPricingModel', 'postId' => $post_id, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data)); ym_fire_sale_start($tier_data->fire_id); } // check cost update if needed if ($base_price != $tier_data->fire_price) { if ($tier_data) { update_post_meta($post_id, '_ym_post_purchasable_cost', $tier_data->fire_price); update_post_meta($post_id, '_ym_fire_active', TRUE); ym_firesale_log(array('doing' => 'TierChange', 'postId' => $post_id, 'newPrice' => $tier_data->fire_price, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data)); ym_firesale_tier_log($tier_data->fire_tier_id); ym_fire_tier_start($tier_data->fire_tier_id); } else { // no tier update_post_meta($post_id, '_ym_post_purchasable_cost', $fire_original_cost); delete_post_meta($post_id, '_ym_fire_original_cost'); update_post_meta($post_id, '_ym_fire_active', false); ym_firesale_log(array('doing' => 'EndPricingModel', 'postId' => $post_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data)); } } } else { if ($is_currently_firesale == 1) { // firesale needs resetting as it has ended update_post_meta($post_id, '_ym_post_purchasable_cost', $fire_original_cost); delete_post_meta($post_id, '_ym_fire_original_cost'); update_post_meta($post_id, '_ym_fire_active', false); ym_firesale_log(array('doing' => 'EndPricingModel', 'postId' => $post_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price)); } } } return $button_code; }
function ym_fire_shortcode_parse($args, $content, $tag) { global $firetypes, $wpdb; switch ($tag) { case 'app_counter': $html = ''; // $html = '<p>Pricing Models</p>'; if (isset($args['pid'])) { // get pid counter $targets[] = ym_firesale_get_single($args['pid']); } else { $targets = ym_firesale_get_all_enabled(); } // get all counters foreach ($targets as $fire) { if ($fire->fire_enable) { // get current tier $tier = ym_firesale_get_current_tier($fire->fire_id); $tier = $tier[0]; // left till expire // ignore type 2 as type 2 on actovates nbecome types 1 //what $what = $firetypes[$fire->fire_type]; $which = $fire->fire_type_id; // how long if ($tier->fire_limit_by) { //$tier->fire_tier_started $left = $tier->fire_limit_var - time(); // hours $hours = $left / 3600; list($hours, $left) = explode('.', $hours); $left = $hours . ' Hours Left'; } else { // get sales if ($fire->fire_type) { // subs $left = $tier->fire_limit_var - ym_fire_sales_subscription_since($tier->fire_tier_started, $which); } else { // ppp $left = $tier->fire_limit_var - ym_fire_sales_ppp_since($tier->fire_tier_started, $which); } $left .= ' Sales Left'; } if ($fire->fire_type) { $pack = ym_get_pack($which); $what = $pack['account_type']; $link = '?ym_subscribe=1&ud=1'; } else { // what is the post title $what = 'SELECT post_title FROM ' . $wpdb->posts . ' WHERE ID = ' . $which; $what = $wpdb->get_var($what); $link = get_permalink($which); } $html .= '<p><a href="' . $link . '"><strong>' . $what . '</strong></a> ' . $left . '</p>'; } } return $html; default: // not defined } }