Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Uninstall WordPress Plugin
- *
- * @package: Test Plugin
- */
- // Define plugin options, custom post types and custom taxonomies to remove.
- $opt = array( 'my_options' );
- $cpt = array( 'book' );
- $tax = array( 'genre', 'writer' );
- // Call uninstall cleanup method.
- WP_Plugin_Janitor::cleanup( $opt, $cpt, $tax );
- // Class containing cleanup and authorize methods.
- class WP_Plugin_Janitor {
- public function cleanup( $opt = NULL, $cpt = NULL, $tax = NULL ) {
- // Perform security checks.
- if( self::authorize() == TRUE ) {
- // Remove plugin options from wp_options database table.
- if( $opt ) {
- foreach( $opt as $option ) {
- delete_option( $option );
- }
- }
- // Remove plugin-specific custom post type entries.
- if( $cpt ) {
- $entries = get_posts( array( 'post_type' => $cpt ) );
- foreach( $entries as $entry ) {
- wp_delete_post( $entry->ID, TRUE );
- }
- }
- // Remove plugin-specific custom taxonomies and terms.
- if( $tax ) {
- global $wp_taxonomies;
- foreach( $tax as $taxonomy ) {
- $terms = get_terms( $taxonomy, array( 'get ' => 'all' ) ); // NOTE: get_terms doesn't seem to work during uninstall
- foreach( $terms as $term ) {
- wp_delete_term( $term->term_id, $taxonomy );
- }
- unset( $wp_taxonomies[$taxonomy] );
- }
- }
- }
- }
- public function authorize() {
- // No direct access from outside of WordPress.
- if( !function_exists( 'is_admin' ) ) {
- header( 'Status: 403 Forbidden' );
- header( 'HTTP/1.1 403 Forbidden' );
- exit();
- }
- // User must be logged in to uninstall plugin.
- if( !is_user_logged_in() ) {
- wp_die( 'You must be logged in to run this script.' );
- }
- // User must have permission to uninstall plugin.
- if( !current_user_can( 'install_plugins' ) ) {
- wp_die( 'You do not have permission to run this script.' );
- }
- // Authorize uninstall.
- return TRUE;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement