0) { return $pid; } $slug = $path; $cut = strrpos($slug, '/'); if (false !== $cut) { $slug = substr($slug, $cut + 1); } if ('' === $slug) { return 0; } $ids = $wpdb->get_col($wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_status = 'publish' AND post_type NOT IN ('revision','nav_menu_item','attachment') LIMIT 2", $slug )); return (1 === count($ids)) ? (int) $ids[0] : 0; } public static function backfill_page_ids($batch_size = 500, $scope = 'hot') { global $wpdb; $archive = ('archive' === $scope); $sessions = $wpdb->prefix . 'aieo_sessions' . ($archive ? '_archive' : ''); $cursor_option = $archive ? 'aieo_page_id_backfill_cursor_archive' : 'aieo_page_id_backfill_cursor'; if ((string) $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $sessions)) !== $sessions) { return array('examined' => 0, 'resolved' => 0, 'unresolved' => 0, 'remaining' => 0, 'complete' => true); } $cursor = (int) get_option($cursor_option, 0); $rows = $wpdb->get_results($wpdb->prepare( "SELECT id, page_url FROM %i WHERE event_type = 'page_view' AND (product_id IS NULL OR product_id = 0) AND page_url IS NOT NULL AND page_url <> '' AND id > %d ORDER BY id LIMIT %d", $sessions, $cursor, max(1, (int) $batch_size) ), ARRAY_A); $resolved = 0; $unresolved = 0; $seen = array(); foreach ($rows as $r) { $url = (string) $r['page_url']; $clean = strtok($url, '?'); $clean = strtok($clean, '#'); if (!array_key_exists($clean, $seen)) { $seen[$clean] = self::resolve_url_to_post_id($clean); } $pid = (int) $seen[$clean]; if ($pid > 0) { $wpdb->update($sessions, array('product_id' => $pid), array('id' => (int) $r['id']), array('%d'), array('%d')); $resolved++; } else { $unresolved++; } $cursor = max($cursor, (int) $r['id']); } update_option($cursor_option, $cursor, false); $remaining = (int) $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE event_type = 'page_view' AND (product_id IS NULL OR product_id = 0) AND page_url IS NOT NULL AND page_url <> '' AND id > %d", $sessions, $cursor )); return array( 'examined' => count($rows), 'resolved' => $resolved, 'unresolved' => $unresolved, 'remaining' => $remaining, 'complete' => (0 === $remaining), ); } }