From 9d64f9f4158159c2b2fb1ccfbbcebdf379be1582 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Thu, 1 Nov 2018 15:39:58 +0100 Subject: [PATCH] Add uninstall hook to remove ticks and tickers --- includes/class-wpliveticker2-system.php | 33 +++++++++++++++++++++++++ wp-liveticker2.php | 1 + 2 files changed, 34 insertions(+) diff --git a/includes/class-wpliveticker2-system.php b/includes/class-wpliveticker2-system.php index 1247345..330489c 100644 --- a/includes/class-wpliveticker2-system.php +++ b/includes/class-wpliveticker2-system.php @@ -39,4 +39,37 @@ class WPLiveticker2_System extends WPLiveticker2 { add_option( self::OPTION, $defaults ); } } + + /** + * Plugin uninstall handler. + * + * @return void + */ + public static function uninstall() { + // Delete all ticks. + $ticks = new WP_Query( array( 'post_type' => 'wplt2_tick' ) ); + foreach ( $ticks->get_posts() as $tick ) { + wp_delete_post( $tick->ID, true ); + } + + // Temporarily register taxonomy to delete it. + register_taxonomy( 'wplt2_ticker', array( 'wplt2_tick' ) ); + + // Delete tickers. + $tickers = get_terms( + array( + 'taxonomy' => 'wplt2_ticker', + 'hide_empty' => false, + ) + ); + foreach ( $tickers as $ticker ) { + wp_delete_term( $ticker->term_id, 'wplt2_ticker' ); + } + + // Unregister taxonomy again. + unregister_taxonomy( 'wplt2_ticker' ); + + // Delete the option. + delete_option( self::OPTION ); + } } diff --git a/wp-liveticker2.php b/wp-liveticker2.php index ae03933..070bfcc 100644 --- a/wp-liveticker2.php +++ b/wp-liveticker2.php @@ -44,6 +44,7 @@ define( 'WPLT2_BASENAME', plugin_basename( __FILE__ ) ); add_action( 'init', array( 'WPLiveticker2', 'register_types' ) ); add_action( 'plugins_loaded', array( 'WPLiveticker2', 'init' ) ); register_activation_hook( WPLT2_FILE, array( 'WPLiveticker2_System', 'activate' ) ); +register_uninstall_hook( WPLT2_FILE, array( 'WPLiveticker2_System', 'uninstall' ) ); // Allow shortcodes in widgets. add_filter( 'widget_text', 'do_shortcode' );