Jack-Dane / odoo-wp-plugin

Odoo WordPress Plugin
GNU General Public License v3.0
8 stars 4 forks source link

Do not delete data and tables from the database on deactivate plugin. Do it only when uninstall. #33

Closed AlbertParera closed 3 months ago

AlbertParera commented 4 months ago

Every time the plugin is deactivated (for maintenance reasons for example), all data and tables from plugin database are dropped, being necessary to reconfigure all again (connection, mappings...). It's not quite user-friendly who can lost his data after deactivation. I suggest adding a new file called uninstall.php:

<?php

// If uninstall.php is not called by WordPress, die.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    die;
}

// Remove custom options.
$options = array(
    'odoo_conn_db_version',
);

foreach ( $options as $option ) {
    if ( get_option( $option ) ) {
        delete_option( $option );
        delete_site_option( $option ); // For Multisite.
    } 
}

// Drop all custom tables from database.
global $wpdb, $table_prefix;

$tables_to_drop = array(
    'odoo_conn_errors',
    'odoo_conn_form_mapping',
    'odoo_conn_form',
    'odoo_conn_connection',
);

foreach ( $tables_to_drop as $table ) {
    $table_name = $table_prefix . $table;
    $sql        = "DROP TABLE IF EXISTS $table_name";

    $wpdb->query( $sql );
}

and remove the following code in odoo_conn.php file:

require_once 'deactivation.php';
register_deactivation_hook( __FILE__, 'odoo_conn_deactivation_function' );

In a hypothetical case it would NOT be necessary to use register_uninstall_hook( 'uninstall.php', 'odoo_conn_uninstall_function' ); Simply using the uninstall.php file is enough.

and remove deactivation.php file too.

Jack-Dane commented 4 months ago

Thanks for the heads up.

The documentation supports this bug ticket, I will implement this change later today.

On deactivation, plugins can run a routine to remove temporary data such as cache and temp files and directories.

The deactivation hook is sometimes confused with the uninstall hook. The uninstall hook is best suited to delete all data permanently such as deleting plugin options and custom tables, etc.

https://developer.wordpress.org/plugins/plugin-basics/activation-deactivation-hooks/