SergeyBiryukov

Sort Recently Active Plugins

Jan 30th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.02 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Sort Recently Active Plugins
  4. Plugin URI:
  5. Description: Sorts plugins in Recently Active list by deactivation date.
  6. Version: 1.0
  7. Author: Sergey Biryukov
  8. Author URI: http://profiles.wordpress.org/sergeybiryukov/
  9. */
  10.  
  11. class Sort_Recently_Active_Plugins {
  12.  
  13.     public function __construct() {
  14.         add_action( 'admin_head-plugins.php', array( $this, 'sort_plugins_by_deactivation_date' ) );
  15.         add_action( 'deactivated_plugin',     array( $this, 'store_deactivation_time' ) );
  16.         add_action( 'deleted_plugin',         array( $this, 'clear_deactivation_time' ) );
  17.  
  18.         register_uninstall_hook( __FILE__, array( 'Sort_Recently_Active_Plugins', 'uninstall' ) );
  19.     }
  20.  
  21.     public function sort_plugins_by_deactivation_date() {
  22.         global $wp_list_table, $status;
  23.  
  24.         if ( 'recently_activated' === $status ) {
  25.             uksort( $wp_list_table->items, array( $this, '_order_callback' ) );
  26.         }
  27.     }
  28.  
  29.     private function _order_callback( $a, $b ) {
  30.         global $wp_list_table;
  31.  
  32.         $times = get_option( 'sort_recently_active_plugins', array() );
  33.  
  34.         $a_time = isset( $times[ $a ] ) ? $times[ $a ] : '';
  35.         $b_time = isset( $times[ $b ] ) ? $times[ $b ] : '';
  36.  
  37.         if ( $a_time && $b_time ) {
  38.             return strcmp( $b_time, $a_time );
  39.         } elseif ( $a_time && ! $b_time ) {
  40.             return -1;
  41.         } elseif ( ! $a_time && $b_time ) {
  42.             return 1;
  43.         } else {
  44.             return strcasecmp( $wp_list_table->items[ $a ]['Name'], $wp_list_table->items[ $b ]['Name'] );
  45.         }
  46.     }
  47.  
  48.     public function store_deactivation_time( $plugin ) {
  49.         $times = get_option( 'sort_recently_active_plugins', array() );
  50.  
  51.         $times[ $plugin ] = current_time( 'mysql' );
  52.  
  53.         update_option( 'sort_recently_active_plugins', $times );
  54.     }
  55.  
  56.     public function clear_deactivation_time( $plugin ) {
  57.         $times = get_option( 'sort_recently_active_plugins', array() );
  58.  
  59.         unset( $times[ $plugin ] );
  60.  
  61.         update_option( 'sort_recently_active_plugins', $times );
  62.     }
  63.  
  64.     public static function uninstall() {
  65.         delete_option( 'sort_recently_active_plugins' );
  66.     }
  67.  
  68. }
  69.  
  70. new Sort_Recently_Active_Plugins;
Advertisement
Add Comment
Please, Sign In to add comment