Passed
Branch master (50908e)
by Stiofan
07:01
created

uninstall.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Fired when the plugin is uninstalled.
4
 *
5
 * @link       https://wpinvoicing.com
6
 * @since      1.0.0
7
 *
8
 * @package    Invoicing
9
 */
10
11
// If uninstall not called from WordPress, then exit.
12
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
13
    exit;
14
}
15
16
global $wpdb, $wp_version;
17
18
$remove_data = get_option( 'wpinv_remove_data_on_invoice_unistall' );
19
20
/*
21
 * Only remove ALL product and page data if WPINV_REMOVE_ALL_DATA constant is set to true in user's
22
 * wp-config.php. This is to prevent data loss when deleting the plugin from the backend
23
 * and to ensure only the site owner can perform this action.
24
 */
25
if ( defined( 'WPINV_REMOVE_ALL_DATA' ) ) {
26
    $remove_data = true === WPINV_REMOVE_ALL_DATA ? true : false;
0 ignored issues
show
The constant WPINV_REMOVE_ALL_DATA was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
}
28
29
if ( $remove_data ) {
30
    // Load Invoicing file.
31
    include_once( 'invoicing.php' );
32
33
    // Roles + caps.
34
    include_once( dirname( __FILE__ ) . '/includes/admin/install.php' );
35
    wpinv_remove_admin_caps();
36
    
37
    $settings = get_option( 'wpinv_settings' );
38
    
39
    // Delete pages.
40
    $wpi_pages = array( 'checkout_page', 'success_page', 'failure_page', 'invoice_history_page', 'quote_history_page', 'invoice_subscription_page' );
41
    foreach ( $wpi_pages as $page ) {
42
        if ( !empty( $page ) && !empty( $settings[ $page ] ) ) {
43
            wp_delete_post( $settings[ $page ], true );
44
        }
45
    }
46
    
47
    // Delete posts + data.
48
    $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type IN ( 'wpi_invoice', 'wpi_item', 'wpi_discount', 'wpi_quote' );" );
49
    $wpdb->query( "DELETE meta FROM {$wpdb->postmeta} meta LEFT JOIN {$wpdb->posts} posts ON posts.ID = meta.post_id WHERE posts.ID IS NULL;" );
50
    
51
    // Delete comments.
52
    $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_type LIKE 'wpinv_note';" );
53
    $wpdb->query( "DELETE meta FROM {$wpdb->commentmeta} meta LEFT JOIN {$wpdb->comments} comments ON comments.comment_ID = meta.comment_id WHERE comments.comment_ID IS NULL;" );
54
    
55
    // Delete user meta.
56
    $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE '%_wpinv_%' OR meta_key LIKE '%_wpi_invoice%' OR meta_key LIKE '%_wpi_item%' OR meta_key LIKE '%_wpi_discount%' OR meta_key LIKE '_wpi_stripe%' OR meta_key LIKE '%_wpi_quote%';" );
57
    
58
    // Cleanup Cron Schedule
59
    wp_clear_scheduled_hook( 'wp_session_garbage_collection' );
60
    wp_clear_scheduled_hook( 'wpinv_register_schedule_event_twicedaily' );
61
    
62
    // Delete options.
63
    $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'wpinv_%' OR option_name LIKE '_wpinv_%' OR option_name LIKE '\_transient\_wpinv\_%';" );
64
    
65
    // Clear any cached data that has been removed
66
    wp_cache_flush();
67
}