Advertisement
Guest User

WP Plugin Hider - t31os

a guest
Sep 26th, 2012
1,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.25 KB | None | 0 0
  1. <?php
  2. /*
  3.     Author:
  4.     Description: Hides plugins from the plugins page by defined author(s)
  5.     Domain Path: /lang
  6.     Plugin Name: Plugin Hider
  7.     Plugin URI:
  8.     Text Domain: plugin-hider
  9.     Version: 1.0.0
  10. */
  11. if( !function_exists('add_action') ) {
  12.     header('Status: 403 Forbidden');
  13.     header('HTTP/1.1 403 Forbidden');
  14.     exit;
  15. }
  16. if( version_compare( PHP_VERSION, '5.0.0', '<' ) ) {
  17.     add_action( 'admin_notices', 'wpph_version_require' );
  18.     function wpph_version_require() {
  19.         if( current_user_can( 'manage_options' ) )
  20.             echo '<div class="error"><p><strong>Plugin Invisibility</strong> requires at least PHP 5.</p></div>';
  21.     }
  22.     return;
  23. }
  24.  
  25. class WP_Plugin_Hider {
  26.    
  27.     private $store_name = 'wpph_settings';
  28.     private $group_name = 'wpph_group';
  29.     private $filter_num = 0;
  30.     private $filter_max = 6;
  31.     private $headers = array(
  32.         'Author',
  33.         //'AuthorName',
  34.         'AuthorURI',
  35.         //'Description',
  36.         //'DomainPath',
  37.         'Name',
  38.         //'Network',
  39.         'PluginURI',
  40.         //'TextDomain',
  41.         //'Title',
  42.         //'Version '
  43.     );
  44.     private $nice_header = array(
  45.         'Author' => 'Author',
  46.         //'AuthorName' => 'Author Name',
  47.         'AuthorURI' => 'Author URI',
  48.         //'Description' => 'Description',
  49.         //'DomainPath' => 'Domain Path',
  50.         'Name' => 'Plugin Name',
  51.         //'Network' => 'Network',
  52.         'PluginURI' => 'Plugin URI',
  53.         //'TextDomain' => 'Plugin Text Domain',
  54.         //'Title' => 'Plugin Title',
  55.         //'Version' => 'Plugin Version'
  56.     );
  57.     private $filters = array();
  58.     private $current = array();
  59.  
  60.     public function __construct() {
  61.         add_action( 'admin_init', array( $this, 'wpph_admin_init' ), 1000 );
  62.         add_action( 'admin_menu', array( $this, 'wpph_admin_menu' ), 1000 );
  63.     }
  64.     public function wpph_admin_init() {
  65.        
  66.         if( !current_user_can( 'activate_plugins' ) )
  67.             return;
  68.        
  69.         global $pagenow;
  70.        
  71.         // Filter to increase filter set limit
  72.         $this->filter_max  = apply_filters( 'wpph_filter_number', $this->filter_max );
  73.         // Filter to add/remove header fields to check against
  74.         $this->headers     = apply_filters( 'wpph_headers',       $this->headers );
  75.  
  76.         // The currently saved data(if any)
  77.         $this->current     = get_option( $this->store_name );
  78.         // Register the option to hold saved data
  79.         register_setting( $this->group_name, $this->store_name, array( $this, 'wpph_validate_input' ) );
  80.        
  81.         // Bail if not the plugins listing
  82.         if( 'plugins.php' != $pagenow )
  83.             return;
  84.        
  85.         // Only hook when there's filter sets configured
  86.         if( $this->wpph_setup_sets() )
  87.             add_filter( 'all_plugins', array( $this, 'wpph_filter_plugins' ), 10000 );
  88.     }
  89.     private function wpph_current_setting( $num, $name ) {
  90.         if( !isset( $this->current['set-' . $num][$name] ) )
  91.             return '';
  92.         if( !empty( $this->current['set-' . $num][$name] ) )
  93.             return $this->current['set-' . $num][$name];
  94.         return '';
  95.     }
  96.     private function wpph_setup_sets() {   
  97.         foreach( $this->current as $set => $headers )
  98.             $this->filters[$set] = array_filter( $headers );
  99.         $this->filters = array_filter( $this->filters );
  100.        
  101.         if( !empty( $this->filters ) )
  102.             return true;
  103.         return false;
  104.     }
  105.     public function wpph_admin_menu() {
  106.        
  107.         if( !current_user_can( 'activate_plugins' ) )
  108.             return;
  109.        
  110.         $hook = add_options_page( __( 'Plugin Hider' ), __( 'Plugin Hider' ), 'manage_options' , 'wppi', array( $this, 'wpph_plugin_page' ) );
  111.         add_action( 'admin_print_styles-' . $hook, array( $this, 'wpph_css' ) );
  112.     }
  113.     public function wpph_validate_input( $input ) {
  114.         $cleansed = array();
  115.         foreach( $input as $set => $data )
  116.             $cleansed['set-'.(int) substr( (string)$set, 4 )] = $data;
  117.         return $cleansed;
  118.     }
  119.     public function wpph_filter_plugins( $all_plugins ) {
  120.         // Loop over each plugin
  121.         foreach( $all_plugins as $handle => $_plugin ) {
  122.             // Loop over each set
  123.             foreach( $this->filters as $data ) {
  124.                 $unset = true;
  125.                 // Loop each header
  126.                 foreach( $data as $header => $value ) {
  127.                     if( !isset( $_plugin[$header] ) ) {
  128.                         $unset = false;
  129.                         continue;
  130.                     }
  131.                     if( $_plugin[$header] != $value )
  132.                         $unset = false;
  133.                 }
  134.                 if( $unset )
  135.                     unset( $all_plugins[$handle] );
  136.             }
  137.         }
  138.         return $all_plugins;
  139.     }
  140.     public function wpph_css() { ?>
  141.         <style type="text/css">
  142.         .wppi-wrapper { background-color:#f5f5f5;border:4px solid #eee;margin: .5em 0 0; }
  143.         .wppi-filter-num { border: 1px solid #aaa;text-shadow: #fff 1px 1px 1px;margin: 0;line-height:2em;color:#666;padding: .3em 1em;background: #ddd;background:-moz-linear-gradient(bottom,  #ddd,  #eee);background:-webkit-gradient(linear, left bottom, left top, from(#ddd), to(#eee));}
  144.         .wppi-filter { padding: .4em 1em;line-height: 2.5em;border: 1px solid #bbb;border-top: none;margin-bottom:2px }
  145.         .wppi-filter label { white-space:nowrap; }
  146.         .wppi-filter input { border:1px solid #ccc!important;padding:.5em;margin: 0 .6em; }
  147.         </style>
  148.     <?php }
  149.     public function wpph_plugin_page() { ?>
  150.    
  151.     <div class="wrap">
  152.         <h2><?php _e( 'Plugin Hider' ); ?></h2>
  153.         <p class="description">
  154.             <?php _e( 'You can specifically hide plugins based on one or many of the plugin headers, add more specificity to make exclusions less general.' ); ?>
  155.         </p>
  156.         <form method="post" action="options.php" >
  157.             <?php $this->filter_sets(); ?>
  158.             <?php settings_fields( $this->group_name ); ?>
  159.             <p class="submit" style="clear:left">
  160.                 <input type="submit" name="submit" class="button-secondary action" value="<?php _e( 'Save' ) ?>" />
  161.             </p>
  162.         </form>
  163.     </div>
  164.    
  165.     <?php }
  166.     private function filter_sets() { ?>
  167.        
  168.         <div class="wppi-wrapper">
  169.        
  170.         <?php for( $this->filter_num; $this->filter_num < $this->filter_max; $this->filter_num++ ) :    ?>
  171.            
  172.             <p class="wppi-filter-num"><strong><?php printf( 'Filter %s', $this->filter_num + 1 ); ?></strong></p>
  173.             <div class="wppi-filter">
  174.                 <?php foreach( $this->headers as $plugin_header ) : $current = $this->wpph_current_setting( $this->filter_num, $plugin_header ); ?>
  175.                 <label for="<?php echo $this->store_name . '-set-' . $this->filter_num . '-' . $plugin_header; ?>">
  176.                     <?php echo $this->nice_header[$plugin_header]; printf( ' <input type="text" name="%s[%s][%s]" value="%s" />', $this->store_name, 'set-' . $this->filter_num, $plugin_header, $current ); ?>
  177.                 </label>
  178.                 <?php endforeach; ?>
  179.                 <br style="clear:both" />
  180.             </div>
  181.  
  182.         <?php endfor; ?>
  183.        
  184.         </div>
  185.        
  186.     <?php }
  187. }
  188.  
  189. $hmc = new WP_Plugin_Hider;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement