Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Author:
- Description: Hides plugins from the plugins page by defined author(s)
- Domain Path: /lang
- Plugin Name: Plugin Hider
- Plugin URI:
- Text Domain: plugin-hider
- Version: 1.0.0
- */
- if( !function_exists('add_action') ) {
- header('Status: 403 Forbidden');
- header('HTTP/1.1 403 Forbidden');
- exit;
- }
- if( version_compare( PHP_VERSION, '5.0.0', '<' ) ) {
- add_action( 'admin_notices', 'wpph_version_require' );
- function wpph_version_require() {
- if( current_user_can( 'manage_options' ) )
- echo '<div class="error"><p><strong>Plugin Invisibility</strong> requires at least PHP 5.</p></div>';
- }
- return;
- }
- class WP_Plugin_Hider {
- private $store_name = 'wpph_settings';
- private $group_name = 'wpph_group';
- private $filter_num = 0;
- private $filter_max = 6;
- private $headers = array(
- 'Author',
- //'AuthorName',
- 'AuthorURI',
- //'Description',
- //'DomainPath',
- 'Name',
- //'Network',
- 'PluginURI',
- //'TextDomain',
- //'Title',
- //'Version '
- );
- private $nice_header = array(
- 'Author' => 'Author',
- //'AuthorName' => 'Author Name',
- 'AuthorURI' => 'Author URI',
- //'Description' => 'Description',
- //'DomainPath' => 'Domain Path',
- 'Name' => 'Plugin Name',
- //'Network' => 'Network',
- 'PluginURI' => 'Plugin URI',
- //'TextDomain' => 'Plugin Text Domain',
- //'Title' => 'Plugin Title',
- //'Version' => 'Plugin Version'
- );
- private $filters = array();
- private $current = array();
- public function __construct() {
- add_action( 'admin_init', array( $this, 'wpph_admin_init' ), 1000 );
- add_action( 'admin_menu', array( $this, 'wpph_admin_menu' ), 1000 );
- }
- public function wpph_admin_init() {
- if( !current_user_can( 'activate_plugins' ) )
- return;
- global $pagenow;
- // Filter to increase filter set limit
- $this->filter_max = apply_filters( 'wpph_filter_number', $this->filter_max );
- // Filter to add/remove header fields to check against
- $this->headers = apply_filters( 'wpph_headers', $this->headers );
- // The currently saved data(if any)
- $this->current = get_option( $this->store_name );
- // Register the option to hold saved data
- register_setting( $this->group_name, $this->store_name, array( $this, 'wpph_validate_input' ) );
- // Bail if not the plugins listing
- if( 'plugins.php' != $pagenow )
- return;
- // Only hook when there's filter sets configured
- if( $this->wpph_setup_sets() )
- add_filter( 'all_plugins', array( $this, 'wpph_filter_plugins' ), 10000 );
- }
- private function wpph_current_setting( $num, $name ) {
- if( !isset( $this->current['set-' . $num][$name] ) )
- return '';
- if( !empty( $this->current['set-' . $num][$name] ) )
- return $this->current['set-' . $num][$name];
- return '';
- }
- private function wpph_setup_sets() {
- foreach( $this->current as $set => $headers )
- $this->filters[$set] = array_filter( $headers );
- $this->filters = array_filter( $this->filters );
- if( !empty( $this->filters ) )
- return true;
- return false;
- }
- public function wpph_admin_menu() {
- if( !current_user_can( 'activate_plugins' ) )
- return;
- $hook = add_options_page( __( 'Plugin Hider' ), __( 'Plugin Hider' ), 'manage_options' , 'wppi', array( $this, 'wpph_plugin_page' ) );
- add_action( 'admin_print_styles-' . $hook, array( $this, 'wpph_css' ) );
- }
- public function wpph_validate_input( $input ) {
- $cleansed = array();
- foreach( $input as $set => $data )
- $cleansed['set-'.(int) substr( (string)$set, 4 )] = $data;
- return $cleansed;
- }
- public function wpph_filter_plugins( $all_plugins ) {
- // Loop over each plugin
- foreach( $all_plugins as $handle => $_plugin ) {
- // Loop over each set
- foreach( $this->filters as $data ) {
- $unset = true;
- // Loop each header
- foreach( $data as $header => $value ) {
- if( !isset( $_plugin[$header] ) ) {
- $unset = false;
- continue;
- }
- if( $_plugin[$header] != $value )
- $unset = false;
- }
- if( $unset )
- unset( $all_plugins[$handle] );
- }
- }
- return $all_plugins;
- }
- public function wpph_css() { ?>
- <style type="text/css">
- .wppi-wrapper { background-color:#f5f5f5;border:4px solid #eee;margin: .5em 0 0; }
- .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));}
- .wppi-filter { padding: .4em 1em;line-height: 2.5em;border: 1px solid #bbb;border-top: none;margin-bottom:2px }
- .wppi-filter label { white-space:nowrap; }
- .wppi-filter input { border:1px solid #ccc!important;padding:.5em;margin: 0 .6em; }
- </style>
- <?php }
- public function wpph_plugin_page() { ?>
- <div class="wrap">
- <h2><?php _e( 'Plugin Hider' ); ?></h2>
- <p class="description">
- <?php _e( 'You can specifically hide plugins based on one or many of the plugin headers, add more specificity to make exclusions less general.' ); ?>
- </p>
- <form method="post" action="options.php" >
- <?php $this->filter_sets(); ?>
- <?php settings_fields( $this->group_name ); ?>
- <p class="submit" style="clear:left">
- <input type="submit" name="submit" class="button-secondary action" value="<?php _e( 'Save' ) ?>" />
- </p>
- </form>
- </div>
- <?php }
- private function filter_sets() { ?>
- <div class="wppi-wrapper">
- <?php for( $this->filter_num; $this->filter_num < $this->filter_max; $this->filter_num++ ) : ?>
- <p class="wppi-filter-num"><strong><?php printf( 'Filter %s', $this->filter_num + 1 ); ?></strong></p>
- <div class="wppi-filter">
- <?php foreach( $this->headers as $plugin_header ) : $current = $this->wpph_current_setting( $this->filter_num, $plugin_header ); ?>
- <label for="<?php echo $this->store_name . '-set-' . $this->filter_num . '-' . $plugin_header; ?>">
- <?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 ); ?>
- </label>
- <?php endforeach; ?>
- <br style="clear:both" />
- </div>
- <?php endfor; ?>
- </div>
- <?php }
- }
- $hmc = new WP_Plugin_Hider;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement