Advertisement
sscovil

WordPress Plugin Uninstall File Template

Dec 26th, 2012
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. <?php
  2. /*
  3. * Uninstall WordPress Plugin
  4. *
  5. * @package: Test Plugin
  6. */
  7.  
  8. // Define plugin options, custom post types and custom taxonomies to remove.
  9. $opt = array( 'my_options' );
  10. $cpt = array( 'book' );
  11. $tax = array( 'genre', 'writer' );
  12.  
  13.  
  14. // Call uninstall cleanup method.
  15. WP_Plugin_Janitor::cleanup( $opt, $cpt, $tax );
  16.  
  17.  
  18. // Class containing cleanup and authorize methods.
  19. class WP_Plugin_Janitor {
  20.  
  21.     public function cleanup( $opt = NULL, $cpt = NULL, $tax = NULL ) {
  22.         // Perform security checks.
  23.         if( self::authorize() == TRUE ) {
  24.        
  25.             // Remove plugin options from wp_options database table.
  26.             if( $opt ) {
  27.                 foreach( $opt as $option ) {
  28.                     delete_option( $option );
  29.                 }
  30.             }
  31.            
  32.             // Remove plugin-specific custom post type entries.
  33.             if( $cpt ) {
  34.                 $entries = get_posts( array( 'post_type' => $cpt ) );
  35.                 foreach( $entries as $entry ) {
  36.                     wp_delete_post( $entry->ID, TRUE );
  37.                 }
  38.             }
  39.            
  40.             // Remove plugin-specific custom taxonomies and terms.
  41.             if( $tax ) {
  42.                 global $wp_taxonomies;
  43.                 foreach( $tax as $taxonomy ) {
  44.                     $terms = get_terms( $taxonomy, array( 'get ' => 'all' ) ); // NOTE: get_terms doesn't seem to work during uninstall
  45.                     foreach( $terms as $term ) {
  46.                         wp_delete_term( $term->term_id, $taxonomy );
  47.                     }
  48.                     unset( $wp_taxonomies[$taxonomy] );
  49.                 }
  50.             }
  51.        
  52.         }
  53.     }
  54.    
  55.     public function authorize() {
  56.         // No direct access from outside of WordPress.
  57.         if( !function_exists( 'is_admin' ) ) {
  58.             header( 'Status: 403 Forbidden' );
  59.             header( 'HTTP/1.1 403 Forbidden' );
  60.             exit();
  61.         }
  62.        
  63.         // User must be logged in to uninstall plugin.
  64.         if( !is_user_logged_in() ) {
  65.             wp_die( 'You must be logged in to run this script.' );
  66.         }
  67.        
  68.         // User must have permission to uninstall plugin.
  69.         if( !current_user_can( 'install_plugins' ) ) {
  70.             wp_die( 'You do not have permission to run this script.' );
  71.         }
  72.        
  73.         // Authorize uninstall.
  74.         return TRUE;
  75.     }
  76. }
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement