Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 681: Adding uninstall method #716

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions includes/core/classes/class-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
protected function setup_hooks(): void {
register_activation_hook( GATHERPRESS_CORE_FILE, array( $this, 'activate_gatherpress_plugin' ) );
register_deactivation_hook( GATHERPRESS_CORE_FILE, array( $this, 'deactivate_gatherpress_plugin' ) );
register_uninstall_hook( GATHERPRESS_CORE_FILE, array( 'GatherPress\Core\Setup', 'uninstall_gatherpress_plugin' ) );

add_action( 'init', array( $this, 'load_textdomain' ), 9 );
add_action( 'init', array( $this, 'maybe_flush_rewrite_rules' ) );
Expand Down Expand Up @@ -218,6 +219,83 @@
flush_rewrite_rules();
}

/**
* Uninstalls the GatherPress plugin.
*
* @since 1.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return void
*/
public static function uninstall_gatherpress_plugin() {
global $wpdb;

Check warning on line 233 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.DirectDatabaseQuery.DirectQuery

Use of a direct database call is discouraged.

Check warning on line 233 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.DirectDatabaseQuery.NoCaching

Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().

Check warning on line 233 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.PreparedSQL.InterpolatedNotPrepared

Use placeholders and $wpdb->prepare(); found interpolated variable {$event_table} at "DROP TABLE IF EXISTS {$event_table}"

Check warning on line 233 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.DirectDatabaseQuery.SchemaChange

Attempting a database schema change is discouraged.
// Ensure the uninstallation script is called from within WordPress.

Check warning on line 234 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.DirectDatabaseQuery.DirectQuery

Use of a direct database call is discouraged.

Check warning on line 234 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.DirectDatabaseQuery.NoCaching

Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().

Check warning on line 234 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.PreparedSQL.InterpolatedNotPrepared

Use placeholders and $wpdb->prepare(); found interpolated variable {$rsvp_table} at "DROP TABLE IF EXISTS {$rsvp_table}"

Check warning on line 234 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.DirectDatabaseQuery.SchemaChange

Attempting a database schema change is discouraged.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
die;
}

// Check if the user opted to delete data on uninstall.
$settings = Settings::get_instance();
$delete_data = $settings->get_value( 'general', 'general', 'delete_data_on_uninstall' );

// If the option is not enabled, exit early to prevent data on uninstall.
if( intval( $delete_data ) !== 1 ) {
return;
}

// GatherPress-specific options and transients to be deleted.
$option_names = array(
'gatherpress_flush_rewrite_rules_flag',
'gatherpress_general',
'gatherpress_suppress_site_notification'
);

// Delete options in single-site or multisite setup.
foreach ( $option_names as $option ) {

Check warning on line 256 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.DirectDatabaseQuery.DirectQuery

Use of a direct database call is discouraged.

Check warning on line 256 in includes/core/classes/class-setup.php

View workflow job for this annotation

GitHub Actions / wp-org-plugin-guidelines

WordPress.DB.DirectDatabaseQuery.NoCaching

Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
delete_option( $option );
if ( is_multisite() ) {
delete_site_option( $option );
}
}

// Drop custom tables for events and RSVPs.
$event_table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix );
$rsvp_table = sprintf( Rsvp::TABLE_FORMAT, $wpdb->prefix );

// Drop the tables if they exist.
$wpdb->query( "DROP TABLE IF EXISTS {$event_table}" );
$wpdb->query( "DROP TABLE IF EXISTS {$rsvp_table}" );

// Delete any custom taxonomies if required (like 'gatherpress_venue', 'gatherpress_event_category').
$taxonomies = array( 'gatherpress_venue', 'gatherpress_event_category' );

foreach ( $taxonomies as $taxonomy ) {
$terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false ) );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
foreach ( $terms as $term ) {
wp_delete_term( $term->term_id, $taxonomy );
}
}
}

// Remove user meta related to GatherPress.
$meta_keys = array(
'gatherpress_event_meta',
'gatherpress_venue_meta',
'gatherpress_rsvp_meta',
);

$meta_query = implode( ', ', array_fill( 0, count( $meta_keys ), '%s' ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ( {$meta_query} )", $meta_keys ) );

// Clear rewrite rules to remove any custom rewrites added by the plugin.
flush_rewrite_rules();
}



/**
* Flush GatherPress rewrite rules if the previously added flag exists and then remove the flag.
*
Expand Down
13 changes: 13 additions & 0 deletions includes/core/classes/settings/class-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ protected function get_sections(): array {
),
),
),
'delete_data_on_uninstall' => array(
'labels' => array(
'name' => __( 'Delete Data on Uninstall', 'gatherpress' ),
),
'description' => __( 'Check this box if you want all GatherPress data and settings to be removed when the plugin is uninstalled.', 'gatherpress' ),
'field' => array(
'label' => __( 'Delete all plugin data upon uninstall', 'gatherpress' ),
'type' => 'checkbox',
'options' => array(
'default' => 0,
),
),
),
),
),
'formatting' => array(
Expand Down
Loading