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: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364:
<?php
class Leads_Batch_Processor {
static $leads;
public function __construct(){
self::load_hooks();
}
public static function load_hooks(){
add_action( 'admin_menu' , array( __CLASS__ , 'init_listener') , 30);
}
public static function init_listener() {
if ( !get_option('leads_batch_processing' , false )) {
return;
}
add_submenu_page(
'edit.php?post_type=wp-lead',
__( 'RESUME DATA MIGRATION', 'inbound-pro' ),
__( 'RESUME DATA MIGRATION', 'inbound-pro' ),
'manage_options',
'leads-batch-processing',
array( __CLASS__ , 'process_batches' )
);
if ( ( !isset($_GET['page']) || $_GET['page'] != 'leads-batch-processing' ) && !get_transient('batch_processing_started') ) {
set_transient('batch_processing_started' , true , 1 * HOUR_IN_SECONDS );
header('Location: ' . admin_url('edit.php?post_type=wp-lead&page=leads-batch-processing'));
exit;
}
}
public static function get_leads( $args ) {
$args = array(
'post_type' => 'wp-lead',
'posts_per_page' => $args['posts_per_page'],
'offset' => $args['offset'] * $args['posts_per_page'],
'post_status' => 'any',
'orderby' => 'date',
'order' => 'DESC',
);
self::$leads = get_posts( $args );
}
public static function isJson($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
public static function rebuild_funnel( $array ) {
$dates_array = array();
foreach ( $array as $page_id => $dates ) {
foreach ($dates as $date) {
$date = date("c", strtotime($date));
$dates_array[$date] = $page_id;
}
}
ksort($dates_array);
$corrected_funnel = array();
foreach ($dates_array as $datetime=>$page_id) {
$corrected_funnel[] = strval($page_id);
}
return array_values(array_unique($corrected_funnel));
}
public static function process_batches() {
$jobs = get_option('leads_batch_processing');
echo '<h1>' . __( 'Processing Batches!' , 'inbound-pro' ) .'</h1>';
echo '<div class="wrap">';
$args = array_shift($jobs);
if (isset($args['method'])) {
call_user_func(
array(__ClASS__, $args['method']),
$args
);
} else {
echo 'There was an error. No method found. Please see arg dump for debugging';
echo '<br>';
print_r($args);
}
echo '</div>';
}
public static function delete_flag( $args ) {
$jobs = get_option('leads_batch_processing');
unset($jobs[$args['method']]);
if ($jobs) {
update_option('leads_batch_processing', $jobs);
return true;
} else {
delete_option('leads_batch_processing');
return false;
}
}
public static function import_events_table_112015( $args ) {
$total = wp_count_posts('wp-lead');
$pages = ceil( $total->publish / $args['posts_per_page'] );
self::get_leads( $args );
echo sprintf( __( '%s of %s steps complete. Please wait...' , 'inbound-pro' ) , $args['offset'] , $pages );
if (!self::$leads || $args['offset'] > $pages ) {
$has_more_jobs = self::delete_flag( $args );
echo '<br>';
_e( 'All done!' , 'inbound-pro' );
if ($has_more_jobs) {
?>
<script type="text/javascript">
document.location.href = "edit.php?post_type=wp-lead&page=leads-batch-processing";
</script>
<?php
}
exit;
}
echo '<br><br>';
echo '<img src="'.admin_url('images/spinner-2x.gif').'">';
foreach (self::$leads as $ID => $lead) {
$conversion_data = get_post_meta( $lead->ID , 'wpleads_conversion_data', true);
if ($conversion_data) :
$conversion_data = json_decode($conversion_data, true);
foreach ($conversion_data as $entry) {
if ( !isset($entry['id']) || !$entry['id'] ) {
continue;
}
$post_type = get_post_type( $entry['id'] );
if ($post_type == 'wp-call-to-action') {
continue;
}
Inbound_Events::store_event(array(
'event_name' => 'inbound_form_submission',
'page_id' => (isset($entry['id']) ? $entry['id'] : ''),
'variation_id' => (isset($entry['variation']) ? $entry['variation'] : ''),
'lead_id' => $lead->ID,
'datetime' => (isset($entry['datetime']) ? $entry['datetime'] : null)
));
}
endif;
$cta_clicks = get_post_meta($lead->ID, 'call_to_action_clicks', true);
if ($cta_clicks):
$cta_clicks = json_decode($cta_clicks, true);
foreach ($cta_clicks as $entry) {
Inbound_Events::store_event(array(
'event_name' => 'inbound_cta_click',
'cta_id' => (isset($entry['id']) ? $entry['id'] : ''),
'variation_id' => (isset($entry['variation']) ? $entry['variation'] : ''),
'lead_id' => $lead->ID,
'datetime' => (isset($entry['datetime']) ? $entry['datetime'] : null)
));
}
endif;
$custom_events = get_post_meta($lead->ID, 'inbound_custom_events', true);
if ($custom_events):
$custom_events = json_decode($custom_events, true);
foreach ($custom_events as $entry) {
$date_raw = new DateTime($entry['datetime']);
$clean_date = $date_raw->format('Y-m-d H:i:s');
Inbound_Events::store_event(array(
'event_name' => $entry['event_type'],
'cta_id' => (isset($entry['id']) ? $entry['id'] : ''),
'variation_id' => (isset($entry['variation']) ? $entry['variation'] : ''),
'lead_id' => $lead->ID,
'session_id' => (isset($entry['tracking_id']) ? $entry['tracking_id'] : ''),
'datetime' => $clean_date
));
}
endif;
}
$args['offset'] = $args['offset'] + 1;
$jobs = get_option('leads_batch_processing');
$jobs[$args['method']] = $args;
update_option('leads_batch_processing' , $jobs , false );
?>
<script type="text/javascript">
document.location.href = "edit.php?post_type=wp-lead&page=leads-batch-processing";
</script>
<?php
}
public static function import_event_data_07132016( $args ) {
$total = wp_count_posts('wp-lead');
$pages = ceil( $total->publish / $args['posts_per_page'] );
self::get_leads( $args );
echo sprintf( __( '%s of %s steps complete. Please wait...' , 'inbound-pro' ) , $args['offset'] , $pages );
Inbound_Events::create_page_views_table();
if (!self::$leads || $args['offset'] > $pages ) {
$has_more_jobs = self::delete_flag( $args );
echo '<br>';
_e( 'All done!' , 'inbound-pro' );
if ($has_more_jobs) {
?>
<script type="text/javascript">
document.location.href = "edit.php?post_type=wp-lead&page=leads-batch-processing";
</script>
<?php
}
exit;
}
echo '<br><br>';
echo '<img src="'.admin_url('images/spinner-2x.gif').'">';
foreach (self::$leads as $ID => $lead) {
$page_views = get_post_meta($lead->ID, 'page_views', true);
if (!$page_views) {
continue;
}
$session_id = uniqid();
$page_views = json_decode($page_views, true);
if (!is_array($page_views)) {
continue;
}
foreach ($page_views as $page_id => $times) {
if (!$page_id || !is_numeric($page_id) ) {
continue;
}
foreach ($times as $key => $time) {
Inbound_Events::store_page_view(array(
'page_id' => $page_id,
'variation_id' => 0,
'session_id' => $session_id,
'lead_id' => $lead->ID,
'lead_uid' => '',
'datetime' => $time
));
}
}
}
$args['offset'] = $args['offset'] + 1;
$jobs = get_option('leads_batch_processing');
$jobs[$args['method']] = $args;
update_option('leads_batch_processing' , $jobs , false );
?>
<script type="text/javascript">
document.location.href = "edit.php?post_type=wp-lead&page=leads-batch-processing";
</script>
<?php
}
}
new Leads_Batch_Processor();