AI Verified

Name

Migrate BasePress -> BetterDocs: 2. Analytics

About

<p>Use these to migrate data from BasePress to BetterDocs, or clone them and customize to migrate to some other structure.</p> <p>Import all 4 snippets, then run them in order. This will handle docs, tags, categorys, meta, analytics, and search logs. It handles feedback as well, but BasePress didn't keep it logged over time, so you get one lump sum inserted to the current date for each doc.</p> <ol> <li><a href="https://codesnippets.cloud/snippet/Code-Atlantic/migrate-basepress-to-betterdocs-step-1">Migrate Docs, Categories, Tags &amp; Meta.</a></li> <li><a href="https://codesnippets.cloud/snippet/Code-Atlantic/migrate-basepress-to-betterdocs-step-2">Analytics</a></li> <li><a href="https://codesnippets.cloud/snippet/Code-Atlantic/migrate-basepress-to-betterdocs-step-3">Feedback</a></li> <li><a href="https://codesnippets.cloud/snippet/Code-Atlantic/migrate-basepress-to-betterdocs-step-4">Search Logs</a></li> </ol>

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

<p>Use these to migrate data from BasePress to BetterDocs, or clone them and customize to migrate to some other structure.</p> <p>Import all 4 snippets, then run them in order. This will handle docs, tags, categorys, meta, analytics, and search logs. It handles feedback as well, but BasePress didn't keep it logged over time, so you get one lump sum inserted to the current date for each doc.</p> <ol> <li><a href="https://codesnippets.cloud/snippet/Code-Atlantic/migrate-basepress-to-betterdocs-step-1">Migrate Docs, Categories, Tags &amp; Meta.</a></li> <li><a href="https://codesnippets.cloud/snippet/Code-Atlantic/migrate-basepress-to-betterdocs-step-2">Analytics</a></li> <li><a href="https://codesnippets.cloud/snippet/Code-Atlantic/migrate-basepress-to-betterdocs-step-3">Feedback</a></li> <li><a href="https://codesnippets.cloud/snippet/Code-Atlantic/migrate-basepress-to-betterdocs-step-4">Search Logs</a></li> </ol>

Codevault

Code-Atlantic

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Website/ Profile URL:

https://code-atlantic.com

Snippet Source:

https://danieliser.com

History

Last modified:

14/10/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Migrate BasePress -> BetterDocs: 2. Analytics

 
                    
1function migrate_hits_table(){
2 global $wpdb;
3 
4 $reset = false;
5 
6 // Variables for table names
7 $old_table = "{$wpdb->prefix}bpkb_views_logs";
8 $new_table = "{$wpdb->prefix}betterdocs_analytics";
9 
10 // Field mapping array (reversed to avoid duplicate keys)
11 $field_mapping = [
12 'post_id' => 'post_id',
13 'impressions' => 'hits',
14 'unique_visit' => 'hits',
15 'created_at' => 'time'
16 ];
17 
18 if ( $reset ) {
19 delete_option( 'last_knowledgebase_sync_time' );
20 $wpdb->get_results("DELETE FROM $new_table WHERE true");
21 }
22 
23 // Get the last sync date/time, defaulting to yesterday if not found.
24 $last_sync_time = get_option('last_knowledgebase_sync_time', date('Y-m-d', strtotime('-')));
25 
26 // Fetch the records from the old table after the last sync date/time.
27 $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM $old_table WHERE time >= %s", $last_sync_time), ARRAY_A);
28 
29 $processed_posts = get_option('processed_knowledgebase_posts', []);
30 
31 foreach ($results as $result) {
32 // Check if the old post id exists in the processed_posts array.
33 if (isset($processed_posts[$result['post_id']])) {
34 $new_post_id = $processed_posts[$result['post_id']];
35 } else {
36 continue; // Skip if not found in the processed_posts array.
37 }
38 
39 // Prepare data using the field mapping.
40 $data = ['post_id' => $new_post_id]; // Setting post_id first since it requires mapping
41 foreach ($field_mapping as $new_field => $old_field) {
42 if ($old_field === 'post_id') continue; // Skip post_id, we've already handled it
43 $data[$new_field] = $result[$old_field];
44 }
45 
46 // Insert the data
47 $wpdb->insert($new_table, $data);
48 }
49 
50 // Update the last sync time to yesterday's date.
51 $yesterday = date('Y-m-d', strtotime('-1 day'));
52 update_option('last_knowledgebase_sync_time', $yesterday);
53}
54 
55add_action( 'init', 'migrate_hits_table', 10 );

0

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

General

AI Verified

0

Add Widget Function

Added: 3 months ago

Last Updated: 3 months ago

General

AI Verified

0

Fluen Forms Login

Added: 1 month ago

Last Updated: 1 month ago

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

0

Migrate BasePress -> BetterDocs: 2. Analytics

Added: 4 months ago

Last Updated: 4 months ago

<p>Use these to migrate data from BasePress to BetterDocs, or clone them and customize to migrate to some other structure.</p> <p>Import all 4 snippets, then run them in order. This will handle docs,...

General

AI Verified

0

Migrate BasePress -> BetterDocs: 4. Search Logs

Added: 4 months ago

Last Updated: 4 months ago

<p>Use these to migrate data from BasePress to BetterDocs, or clone them and customize to migrate to some other structure.</p> <p>Import all 4 snippets, then run them in order. This will handle docs,...

General

AI Verified

0

Migrate BasePress -> BetterDocs: 3. Feedback

Added: 4 months ago

Last Updated: 4 months ago

<p>Use these to migrate data from BasePress to BetterDocs, or clone them and customize to migrate to some other structure.</p> <p>Import all 4 snippets, then run them in order. This will handle docs,...