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: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278:
<?php
class CTA_Ajax_Listeners {
public function __construct() {
self::load_hooks();
}
public static function load_hooks() {
add_action( 'wp_ajax_wp_cta_clear_all_cta_stats', array(__CLASS__, 'clear_all_stats'));
add_action( 'wp_ajax_wp_cta_clear_stats_action', array(__CLASS__, 'clear_stats'));
add_action( 'wp_ajax_wp_cta_clear_variation_stats', array(__CLASS__, 'clear_variation_stats'));
add_action('wp_ajax_wp_cta_record_impressions', array(__CLASS__, 'record_impression'));
add_action('wp_ajax_nopriv_wp_cta_record_impressions', array(__CLASS__, 'record_impression'));
add_action('wp_ajax_wp_cta_record_conversion', array(__CLASS__, 'record_conversion'));
add_action('wp_ajax_nopriv_wp_cta_record_conversion', array(__CLASS__, 'record_conversion'));
add_action( 'wp_ajax_wp_wp_call_to_action_meta_save', array(__CLASS__, 'save_meta'));
add_action( 'wp_ajax_nopriv_cta_get_variation', array(__CLASS__, 'serve_varition'));
add_action( 'wp_ajax_cta_get_variation', array(__CLASS__, 'serve_varition'));
}
public static function clear_all_stats() {
$ctas = get_posts( array(
'post_type' => 'wp-call-to-action',
'posts_per_page' => -1
));
foreach ($ctas as $cta) {
$variations = CTA_Variations::get_variations( $cta->ID );
foreach ($variations as $vid=> $variation){
CTA_Variations::set_impression_count($cta->ID , $vid);
}
}
header('HTTP/1.1 200 OK');
exit;
}
public static function clear_stats() {
$cta_id = intval($_POST['page_id']);
$variations = CTA_Variations::get_variations( $cta_id );
foreach ($variations as $vid=> $variation){
CTA_Variations::set_impression_count($cta_id , $vid , 0);
CTA_Variations::set_conversion_count($cta_id , $vid , 0);
}
header('HTTP/1.1 200 OK');
exit;
}
public static function clear_variation_stats() {
$cta_id = intval($_POST['page_id']);
$vid = intval($_POST['variation']);
CTA_Variations::set_impression_count($cta_id , $vid , 0);
CTA_Variations::set_conversion_count($cta_id , $vid , 0);
header('HTTP/1.1 200 OK');
exit;
}
public static function record_impression() {
global $wpdb;
global $user_ID;
if (!isset($_POST['cta_impressions'])) {
return;
}
$do_not_track = apply_filters('inbound_analytics_stop_track', false );
if ( $do_not_track ) {
return;
}
foreach ( $_POST['cta_impressions'] as $cta_id => $vid ) {
do_action('wp_cta_record_impression', array(
'page_id' =>(int) $_POST['page_id'],
'cta_id' =>(int) $cta_id,
'variation_id' => (int) $vid
));
}
header('HTTP/1.1 200 OK');
exit;
}
public static function record_conversion() {
global $wpdb;
global $user_ID;
$cta_id = trim(intval($_POST['cta_id']));
$variation_id = trim(intval($_POST['variation_id']));
$do_not_track = apply_filters('inbound_analytics_stop_track', false );
if ( $do_not_track ) {
return;
}
do_action('wp_cta_record_conversion', $cta_id, $variation_id);
print $cta_id;
header('HTTP/1.1 200 OK');
exit;
}
public static function save_meta() {
global $wpdb;
if ( !wp_verify_nonce( $_POST['nonce'], "wp-call-to-action-meta-nonce")) {
exit("Wrong nonce");
}
$new_meta_val = $_POST['new_meta_val'];
$meta_id = sanitize_text_field($_POST['meta_id']);
$post_id = intval($_POST['page_id']);
if ($meta_id === "main_title") {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = $new_meta_val;
wp_update_post( $my_post );
}
if ($meta_id === "the_content") {
$title_save = get_post_meta($post_id, "wp-cta-main-headline", true);
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_content'] = $new_meta_val;
wp_update_post( $my_post );
add_post_meta( $post_id, "wp-cta-main-headline", $title_save, true ) or update_post_meta( $post_id, "wp-cta-main-headline", $title_save );
} else {
add_post_meta( $post_id, $meta_id, $new_meta_val, true ) or update_post_meta( $post_id, $meta_id, $new_meta_val );
}
header('HTTP/1.1 200 OK');
exit;
}
public static function serve_varition() {
if ( !isset($_REQUEST['cta_id']) || !is_numeric($_REQUEST['cta_id']) ) {
echo 'x';
exit;
} else {
$cta_id = intval($_REQUEST['cta_id']);
}
$variations = CTA_Variations::get_variations($cta_id );
$variation_marker = get_post_meta( $cta_id , '_cta_ab_variation_marker', true );
if (!is_numeric($variation_marker)) {
$variation_marker = 0;
}
if ($variations) {
foreach ($variations as $vid => $variation ) {
if (!isset($variation['status']) || $variation['status'] == 'active' ){
$live_variations[] = $vid;
}
}
}
if (!$live_variations) {
echo 'x';
exit;
}
if (count($live_variations)==1) {
echo $live_variations[0];
exit;
}
$keys_as_values = array_flip($live_variations);
reset($keys_as_values);
if (!isset($keys_as_values[$variation_marker])) {
$variation_marker = reset($keys_as_values);
}
$i = 0;
if ( key($keys_as_values) != $variation_marker) {
while ((next($keys_as_values) != $variation_marker )){
if ($i>100) {
break;
}
$i++;
}
}
$key = next($keys_as_values);
$variation_marker = $live_variations[$key];
if (!$variation_marker) {
$variation_marker = reset($keys_as_values);
}
update_post_meta( $cta_id , '_cta_ab_variation_marker', $variation_marker);
echo $variation_marker;
exit;
}
}
new CTA_Ajax_Listeners();