Skip to content

Instantly share code, notes, and snippets.

@atwellpub
Created October 7, 2014 23:28
Show Gist options
  • Save atwellpub/8c2de210f115749fff74 to your computer and use it in GitHub Desktop.
Save atwellpub/8c2de210f115749fff74 to your computer and use it in GitHub Desktop.
<?php
define("QUICK_CACHE_ALLOWED", false);
define("DONOTCACHEPAGE", true);
define('DONOTCACHCEOBJECT', true);
define('DONOTCDN', true);
if ( file_exists ( './../../../../wp-load.php' ) )
{
include_once ( './../../../../wp-load.php' );
}
else if ( file_exists ( './../../../../../wp-load.php' ) )
{
include_once ( './../../../../../wp-load.php' );
}
class LP_Variation_Rotation {
static $permalink_name;
static $post_id;
static $last_loaded_variation;
static $variations;
static $marker;
static $next_marker;
static $destination_url;
/**
* Executes Class
*/
public function __construct() {
self::load_variables();
//self::run_debug();
self::redirect();
}
/**
* Loads Static Variables
*/
private static function load_variables()
{
self::$permalink_name = (isset($_GET['permalink_name'])) ? sanitize_text_field($_GET['permalink_name']) : null;
self::$post_id = self::load_post_id();
self::$last_loaded_variation = ( isset( $_COOKIE['lp-loaded-variation-'.self::$permalink_name] ) ) ? $_COOKIE['lp-loaded-variation-'.self::$permalink_name] : null;
self::$variations = self::load_variations();
self::$marker = self::load_marker();
self::$next_marker = self::discover_next_variation();
self::$destination_url = self::build_destination_url();
}
/**
* Debug Information - Prints Class Variable Data
*/
static function run_debug() {
print_r($this);exit;
}
/**
* Loads the ID of the Landing Page
*/
static function load_post_id() {
global $wpdb;
$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type='landing-page'", self::$permalink_name ));
return $post_id;
}
/**
* Loads an Array of Active Variations Associated with Landing Page
*/
static function load_variations() {
$live_variations = array();
$variations_string = get_post_meta( self::$post_id , 'lp-ab-variations' , true );
$variations = explode(',',$variations_string);
$variations = array_filter($variations,'is_numeric');
/* Check the Status of Each Variation and Keep Live Ones */
foreach ($variations as $key=>$vid) {
if ($vid==0) {
$variation_status = get_post_meta( self::$post_id , 'lp_ab_variation_status' , true );
} else {
$variation_status = get_post_meta( self::$post_id , 'lp_ab_variation_status-'.$vid , true );
}
if (!is_numeric($variation_status) || $variation_status==1) {
$live_variations[] = $vid;
}
}
return $live_variations;
}
/**
* Loads Variation ID of Last Variation Loaded
*/
static function load_marker() {
$marker = get_post_meta( self::$post_id , 'lp-ab-variations-marker' , true );
if ( !is_numeric($marker) || !in_array( $marker , self::$variations ) ) {
$marker = current(self::$variations);
}
return $marker;
}
/**
* Discovers Next Variation in Line
*/
static function discover_next_variation() {
/* Set Pointer to Correct Location in Variations Array */
while ( self::$marker != current( self::$variations) ) {
next(self::$variations);
}
/* Discover the next variation in the array */
next(self::$variations);
/* If the pointer is empty then reset array */
if ( !is_numeric(current( self::$variations ) ) ) {
reset( self::$variations );
}
/* Save as Historical Data */
update_post_meta( self::$post_id , 'lp-ab-variations-marker' , current( self::$variations ) );
return current( self::$variations );
}
/**
* Builds Redirect URL & Stores Cookie Data
*/
static function build_destination_url() {
/* Load Base URL */
$url = get_permalink(self::$post_id);
$old_params = null;
/* Keep GET Params */
foreach ($_GET as $key=>$value) {
if ($key != "permalink_name"){
$old_params .= "&$key=" . $value;
}
}
/* Build Final URL and Set Memory Cookies */
$url = $url."?lp-variation-id=".self::$next_marker.$old_params;
/* Set Memory Cookies */
setcookie('lp-loaded-variation-'.self::$permalink_name , $url , time()+ 60 * 60 * 24 * 30 , "/" );
setcookie( 'lp-variation-id' , self::$next_marker , time()+3600 , "/" );
return $url;
}
/**
* Redirects to Correct Variation
*/
static function redirect() {
header("Location: ".self::$destination_url , false );
exit;
}
}
$VariationRoation = new LP_Variation_Rotation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment