AI Verified

Name

Migrate BasePress -> BetterDocs: 4. Search Logs

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:

Potential vulnerability found : SQL Injection
Found on line : 12
Code : ->query("DELETE FROM $keyword_table WHERE 1")
Vulnerable line : 8
Code : $keyword_table = "{$wpdb->prefix}betterdocs_search_keyword");

Potential vulnerability found : SQL Injection
Found on line : 13
Code : ->query("DELETE FROM $log_table WHERE 1")
Vulnerable line : 9
Code : $log_table = "{$wpdb->prefix}betterdocs_search_log");

Found 2 vulnerabilities

Migrate BasePress -> BetterDocs: 4. Search Logs

 
                    
1function migrate_search_logs() {
2 global $wpdb;
3 
4 $reset = false; // this is to reset the migration process if needed
5 
6 // Variables for table names
7 $old_table = "{$wpdb->prefix}bpkb_search_logs";
8 $keyword_table = "{$wpdb->prefix}betterdocs_search_keyword";
9 $log_table = "{$wpdb->prefix}betterdocs_search_log";
10 
11 if ($reset) {
12 delete_option('last_search_sync_time');
13 $wpdb->query("DELETE FROM $keyword_table WHERE 1");
14 $wpdb->query("DELETE FROM $log_table WHERE 1");
15 }
16 
17 // Get the last sync date/time, defaulting to yesterday if not found.
18 $last_sync_time = get_option('last_search_sync_time', date('Y-m-d', strtotime('-10 years')));
19 
20 // Fetch the records from the old table after the last sync date/time.
21 $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM $old_table WHERE time >= %s", $last_sync_time), ARRAY_A);
22 
23 foreach ($results as $result) {
24 // Check if the keyword already exists in the new keyword table
25 $keyword_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM $keyword_table WHERE keyword = %s", $result['query']));
26 
27 // If not found, insert the keyword and get the new ID
28 if (!$keyword_id) {
29 $wpdb->insert($keyword_table, ['keyword' => $result['query']]);
30 $keyword_id = $wpdb->insert_id;
31 }
32 
33 // Prepare the log data
34 $log_data = [
35 'keyword_id' => $keyword_id,
36 'count' => $result['found_posts'] > 0 ? $result['hits'] : 0,
37 'not_found_count' => $result['found_posts'] <= 0 ? $result['hits'] : 0, // Deducting found_posts from hits to get not_found_count
38 'created_at' => $result['time']
39 ];
40 
41 // Insert the log data
42 $wpdb->insert($log_table, $log_data);
43 }
44 
45 // Update the last sync time to yesterday's date.
46 $yesterday = date('Y-m-d', strtotime('-1 day'));
47 update_option('last_search_sync_time', $yesterday);
48}
49 
50add_action('init', 'migrate_search_logs');

0

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

0

Get Featured Image Metadata for Current Post

Added: 9 months ago

Last Updated: 9 months ago

This creates a shortcode that returns the requested metadata item for the current posts' featured image. The item values that can be returned are alt, title, caption, description, or url.

General

AI Verified

0

changing leave a reply to leave a comment

Added: 10 months ago

Last Updated: 9 months ago

changing leave a reply to leave a comment in wordpress

General

Unverified

0

Body Class "scroll" on scroll

Added: 2 months ago

Last Updated: 13 hours 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,...