1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214:
<?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');
} else 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 $sticky_variations;
static $last_loaded_variation;
static $variations;
static $marker;
static $next_marker;
static $destination_url;
public function __construct() {
self::load_variables();
self::redirect();
}
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::$sticky_variations = Landing_Pages_Settings::get_setting('lp-main-landing-page-rotation-halt', false);
self::$last_loaded_variation = (isset($_COOKIE['lp-loaded-variation-' . self::$permalink_name])) ? intval($_COOKIE['lp-loaded-variation-' . self::$permalink_name]) : null;
if (self::$sticky_variations && self::$last_loaded_variation) {
self::$destination_url = self::$last_loaded_variation;
if (!isset($_GET)) {
return;
}
$begin = (strstr(self::$destination_url, '?')) ? '' : '?';
foreach ($_GET as $key => $value) {
if ($key != "permalink_name") {
$old_params .= "&$key=" . $value;
}
}
self::$destination_url = self::$destination_url . $begin . $old_params;
} else {
self::$variations = self::load_variations();
self::$marker = self::load_marker();
self::$next_marker = self::discover_next_variation();
self::$destination_url = self::build_destination_url();
}
}
static function run_debug() {
echo self::$variations . '<br>';
echo self::$marker . '<br>';
echo self::$next_marker . '<br>';
echo self::$destination_url . '<br>';
exit;
}
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;
}
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');
foreach ($variations as $key => $vid) {
$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;
}
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;
}
static function discover_next_variation() {
while (self::$marker != current(self::$variations)) {
next(self::$variations);
}
next(self::$variations);
if (!is_numeric(current(self::$variations))) {
reset(self::$variations);
}
update_post_meta(self::$post_id, 'lp-ab-variations-marker', current(self::$variations));
return current(self::$variations);
}
static function build_destination_url() {
$url = get_permalink(self::$post_id);
$old_params = null;
foreach ($_GET as $key => $value) {
if ($key != "permalink_name") {
$old_params .= "&$key=" . $value;
}
}
$url = $url . "?lp-variation-id=" . self::$next_marker . $old_params;
setcookie('lp-loaded-variation-' . self::$permalink_name, $url, time() + 60 * 60 * 24 * 30, "/");
setcookie('lp-variation-id', self::$next_marker, time() + 3600, "/");
return $url;
}
static function redirect() {
if (count(self::$variations) > 1) {
header("HTTP/1.1 302 Temporary Redirect");
} else {
header("HTTP/1.1 301 Moved Permanently");
}
header("Location: " . self::$destination_url);
exit;
}
}
new LP_Variation_Rotation;