AJAX
3 months ago
Admin_Page
3 months ago
Click_Tracking
5 months ago
ColumnOptions
6 months ago
Cron
8 months ago
Custom_WordPress_Columns
8 months ago
Data_Pruning
5 months ago
Date_Picker
3 months ago
Date_Range
5 months ago
Ecommerce
5 months ago
Email_Reports
3 months ago
Examiner
3 months ago
Favicon
5 months ago
Form_Submissions
5 months ago
Integrations
3 months ago
Interval
1 year ago
Journey
5 months ago
Migrations
5 months ago
Models
5 months ago
Overview
3 months ago
Public_API
5 months ago
Rows
3 months ago
Statistics
3 months ago
Tables
3 months ago
Utils
5 months ago
Views
5 months ago
WooCommerceOrderMetaBox
5 months ago
ActivationLifecycle.php
3 months ago
Admin_Bar_Stats.php
3 months ago
Appearance.php
1 year ago
Campaign_Builder.php
3 months ago
Capability_Manager.php
3 months ago
Chart.php
3 months ago
Chart_Data.php
1 year ago
Click_Tracking.php
3 months ago
ComplianzIntegration.php
3 months ago
Cron_Job.php
5 months ago
Cron_Manager.php
5 months ago
Current_Traffic_Finder.php
1 year ago
Dashboard_Options.php
6 months ago
Dashboard_Widget.php
2 years ago
Database.php
8 months ago
Database_Manager.php
5 months ago
Empty_Report_Option.php
2 years ago
Env.php
6 months ago
Examiner_Config.php
11 months ago
FetchFaviconsJob.php
5 months ago
Filters.php
3 months ago
Geo_Database_Health_Check_Job.php
8 months ago
Geo_Database_Manager.php
6 months ago
Geoposition.php
1 year ago
Icon_Directory.php
6 months ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
10 months ago
Independent_Analytics.php
5 months ago
Interrupt.php
3 months ago
Known_Referrers.php
6 months ago
MainWP.php
1 year ago
Map.php
8 months ago
Map_Data.php
1 year ago
Migration_Fixer_Job.php
5 months ago
Patch.php
1 year ago
Payload_Validator.php
8 months ago
Plugin_Conflict_Detector.php
6 months ago
Plugin_Group.php
6 months ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
3 months ago
Quick_Stats.php
3 months ago
REST_API.php
3 months ago
Real_Time.php
3 months ago
Report.php
1 year ago
Report_Finder.php
6 months ago
Report_Options_Parser.php
8 months ago
Resource_Identifier.php
8 months ago
Settings.php
3 months ago
Sort_Configuration.php
8 months ago
Tables.php
8 months ago
Track_Resource_Changes.php
1 year ago
View_Counter.php
6 months ago
Views_Over_Time_Finder.php
1 year ago
VisitorSaltRefreshInterval.php
6 months ago
WP_Option_Cache_Bust.php
2 years ago
Database_Manager.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Custom_WordPress_Columns\Views_Column; |
| 6 | use IAWP\Utils\Dir; |
| 7 | use IAWPSCOPED\Illuminate\Support\Collection; |
| 8 | /** @internal */ |
| 9 | class Database_Manager |
| 10 | { |
| 11 | public function reset_analytics() : void |
| 12 | { |
| 13 | // Empty all analytics tables while preserving config tables |
| 14 | $this->get_tables()->where('type', 'analytics')->each(function ($table) { |
| 15 | global $wpdb; |
| 16 | $wpdb->query('TRUNCATE ' . $table['name']); |
| 17 | }); |
| 18 | // Recreate the saved reports |
| 19 | \IAWP\Report_Finder::insert_default_reports(); |
| 20 | \IAWP\Report_Finder::insert_default_user_journey_reports(); |
| 21 | $this->delete_overview_report_data(); |
| 22 | $this->delete_all_post_meta(); |
| 23 | } |
| 24 | public function delete_all_data() : void |
| 25 | { |
| 26 | $this->delete_all_iawp_options(); |
| 27 | $this->delete_all_iawp_user_metadata(); |
| 28 | $this->delete_all_iawp_tables(); |
| 29 | $this->delete_all_post_meta(); |
| 30 | $this->delete_cached_favicons(); |
| 31 | } |
| 32 | public function delete_all_iawp_options() : void |
| 33 | { |
| 34 | global $wpdb; |
| 35 | $options = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->options} WHERE option_name LIKE %s", 'iawp_%')); |
| 36 | foreach ($options as $option) { |
| 37 | \delete_option($option->option_name); |
| 38 | } |
| 39 | } |
| 40 | public function delete_overview_report_data() : void |
| 41 | { |
| 42 | global $wpdb; |
| 43 | $options = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->options} WHERE option_name LIKE %s", 'iawp_module_%')); |
| 44 | foreach ($options as $option) { |
| 45 | \delete_option($option->option_name); |
| 46 | } |
| 47 | } |
| 48 | public function delete_all_iawp_user_metadata() : void |
| 49 | { |
| 50 | global $wpdb; |
| 51 | $metadata = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->usermeta} WHERE meta_key LIKE %s", 'iawp_%')); |
| 52 | foreach ($metadata as $metadata) { |
| 53 | \delete_user_meta($metadata->user_id, $metadata->meta_key); |
| 54 | } |
| 55 | } |
| 56 | public function delete_cached_favicons() : void |
| 57 | { |
| 58 | try { |
| 59 | Dir::delete(\IAWPSCOPED\iawp_upload_path_to('iawp-favicons/')); |
| 60 | } catch (\Throwable $error) { |
| 61 | } |
| 62 | } |
| 63 | public function delete_all_iawp_tables() : void |
| 64 | { |
| 65 | $this->get_tables()->each(function ($table) { |
| 66 | global $wpdb; |
| 67 | $wpdb->query('DROP TABLE ' . $table['name']); |
| 68 | }); |
| 69 | } |
| 70 | public function get_tables() : Collection |
| 71 | { |
| 72 | global $wpdb; |
| 73 | $rows = $wpdb->get_results($wpdb->prepare("SELECT table_name AS name FROM information_schema.tables WHERE TABLE_SCHEMA = %s AND table_name LIKE %s", $wpdb->dbname, $wpdb->prefix . 'independent_analytics_%')); |
| 74 | $config_tables = [$wpdb->prefix . 'independent_analytics_campaign_urls', $wpdb->prefix . 'independent_analytics_link_rules']; |
| 75 | $tables = Collection::make($rows)->map(function ($row) use($config_tables) { |
| 76 | return ['name' => $row->name, 'type' => \in_array($row->name, $config_tables) ? 'config' : 'analytics']; |
| 77 | }); |
| 78 | return $tables; |
| 79 | } |
| 80 | public function delete_all_post_meta() : void |
| 81 | { |
| 82 | \delete_post_meta_by_key(Views_Column::$meta_key); |
| 83 | } |
| 84 | public static function register_actions() : void |
| 85 | { |
| 86 | \add_action('iawp_reset_analytics', function () { |
| 87 | $manager = new self(); |
| 88 | $manager->reset_analytics(); |
| 89 | }); |
| 90 | \add_action('iawp_reset_database', function () { |
| 91 | $db_version = \intval(\get_option('iawp_db_version', '0')); |
| 92 | $database_manager = new \IAWP\Database_Manager(); |
| 93 | $database_manager->delete_all_iawp_options(); |
| 94 | $database_manager->delete_all_iawp_user_metadata(); |
| 95 | $database_manager->delete_all_iawp_tables(); |
| 96 | $database_manager->delete_cached_favicons(); |
| 97 | \IAWP\Capability_Manager::reset_capabilities(); |
| 98 | \update_option('iawp_db_version', $db_version); |
| 99 | }); |
| 100 | \add_action('iawp_reset_meta', function () { |
| 101 | $db_version = \intval(\get_option('iawp_db_version', '0')); |
| 102 | $database_manager = new \IAWP\Database_Manager(); |
| 103 | $database_manager->delete_all_iawp_options(); |
| 104 | $database_manager->delete_all_iawp_user_metadata(); |
| 105 | $database_manager->delete_all_post_meta(); |
| 106 | \IAWP\Capability_Manager::reset_capabilities(); |
| 107 | \update_option('iawp_db_version', $db_version); |
| 108 | }); |
| 109 | } |
| 110 | } |
| 111 |