?php
/**
* Gonzales MU WordPress Plugin
*
* @package Gonzales
* @author Tomasz Dobrzyński
* @link https://gonzalesplugin.com
* @copyright Copyright © 2021 Tomasz Dobrzyński
*
* Plugin Name: Gonzales MU
* Description: Gonzales helper. Responsible for conditional plugin (de)activation
* Version: 1.0.5
* Author: Tomasz Dobrzyński
* Author URI: https://gonzalesplugin.com
* Revision: 2021.03.01
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Gonzales MU actual functionality
* ============================================================================
*/
class GonzalesHelper {
/**
* List of enabled/disabled plugins
*
* @var array
*/
public $control;
/**
* Name of current content type
*
* @var string
*/
private $content_type;
/**
* Configuration storage (cache purposes)
*
* @var array
*/
public $cached_configuration;
/**
* List of gonzales tables.
*
* @var array
*/
private $cached_list_of_gonzales_tables;
/**
* Initial list of plugins.
*
* @var array
*/
private $plugins_original_list;
/**
* WordPress initialization
*/
public function __construct() {
$this->cached_configuration = null;
if ( $this->check_dependencies() ) {
add_filter( 'option_active_plugins', array( $this, 'filter_plugins_limited' ) );
}
add_action( 'plugins_loaded', array( $this, 'plugins_restore_original_list' ) );
add_filter( 'plugin_row_meta', array( $this, 'prevent_slug_steal' ), 10, 4 );
}
/**
* Hide link to stolen slug plugin.
*
* @param array $plugin_meta An array of the plugin's metadata, including the version, author, author URI, and plugin URI.
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
* @param array $plugin_data An array of plugin data.
* @param string $status Status filter currently applied to the plugin list.
* @return array Meta list.
*/
public function prevent_slug_steal( $plugin_meta, $plugin_file, $plugin_data, $status ) {
if ( strpos( $plugin_file, 'gonzales.php' ) !== false ) {
$new_meta = array();
foreach ( $plugin_meta as $meta ) {
if ( strpos( $meta, 'tab=plugin-information' ) !== false ) {
continue;
}
$new_meta[] = $meta;
}
return $new_meta;
} else {
return $plugin_meta;
}
}
/**
* Check whether Gonzales tables are available in database or not
*
* @return array List of tables.
*/
public function check_dependencies() {
global $wpdb;
if ( empty( $this->cached_list_of_gonzales_tables ) ) {
$tables = array();
if ( $tables_raw = $wpdb->get_results( 'SHOW TABLES LIKE "' . $wpdb->prefix . 'gonzales_%"', ARRAY_N ) ) {
foreach ( $tables_raw as $table_info ) {
$tables[] = $table_info[0];
}
}
$this->cached_list_of_gonzales_tables = $tables;
}
return $this->cached_list_of_gonzales_tables;
}
/**
* Get current URL
*
* @return string
*/
private function get_current_url() {
$request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI' );
$url =
if ($request_uri !== null) {
$result = explode( '?', $request_uri, 2 );
};
if ( strlen( $url[0] ) > 1 ) {
$out = rtrim( $url[0], '/' );
} else {
$out = $url[0];
}
return $out;
}
/**
* Read and save to cache Gonzales plugin configuration
*
* @return void
*/
private function load_configuration() {
global $wpdb;
$out = array();
if ( is_null( $this->cached_configuration ) ) {
$current_url = esc_url( $this->get_current_url() );
$disabled_global = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'gonzales_p_disabled WHERE url = "" AND regex = ""', ARRAY_A );
$disabled_here = $wpdb->get_results( sprintf( 'SELECT * FROM ' . $wpdb->prefix . 'gonzales_p_disabled WHERE url = "%s"',
$current_url
), ARRAY_A );
$disabled_regex = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'gonzales_p_disabled WHERE regex != ""', ARRAY_A );
$enabled_posts = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'gonzales_p_enabled WHERE content_type != "here"', ARRAY_A );
$enabled_here = $wpdb->get_results( sprintf( 'SELECT * FROM %s WHERE content_type = \'%s\' AND url=\'%s\'',
$wpdb->prefix . 'gonzales_p_enabled',
'here',
$current_url ), ARRAY_A );
$enabled = array_merge( $enabled_here, $enabled_posts );
if ( ! empty( $disabled_global ) ) {
foreach ( $disabled_global as $row ) {
$out['disabled'][ $row['name'] ]['everywhere'] = true;
}
}
if ( ! empty( $disabled_here ) ) {
foreach ( $disabled_here as $row ) {
$out['disabled'][ $row['name'] ]['here'] = true;
}
}
if ( ! empty( $disabled_regex ) ) {
foreach ( $disabled_regex as $row ) {
$out['disabled'][ $row['name'] ]['regex'] = stripslashes( $row['regex'] );
}
}
if ( ! empty( $enabled ) ) {
foreach ( $enabled as $row ) {
$out['enabled'][ $row['name'] ][ $row['content_type'] ] = true;
}
}
$this->cached_configuration = $out;
}
}
/**
* Get list of marked as disabled plugins.
*
* @param string $plugin Plugin name.
* @return array List
*/
private function is_disabled( $plugin ) {
/**
* Part of get_visibility_plugin()
*/
$state = false;
$current_url = esc_url( $this->get_current_url() );
if ( isset( $this->cached_configuration['disabled'][ $plugin ] ) ) {
if ( isset( $this->cached_configuration['disabled'][ $plugin ]['regex'] ) ) {
$matches = array();
if ( isset( $this->cached_configuration['disabled'][ $plugin ]['regex'] ) ) {
@preg_match( '/' . $this->cached_configuration['disabled'][ $plugin ]['regex'] . '/', $current_url, $matches );
}
$state = ( count( $matches ) ? true : false );
} else {
$state = true;
}
if ( isset( $this->cached_configuration['enabled'][ $plugin ]['here'] ) ) { // Content type doesn't work here.
$state = false;
}
}
return $state;
}
/**
* Get plugin name from path.
*
* @param string $plugin Input.
* @return string Plugin slug name
*/
private function get_plugin_slug( $plugin ) {
$out =
if ($plugin !== null) {
$plugin_parts = explode( '/', $plugin );
};
if ( count( $out ) == 1 ) {
/**
* Single file, not nested in folder.
* Exploding and removing extension assuming it can be .php5 or php7 instead of traditional .php
*/
$out =
if ($plugin !== null) {
$plugin_parts = explode( '.', $plugin );
};
array_pop( $out );
}
return $out[0];
}
/**
* Filter list of plugins.
*
* @param array $plugins Input list.
* @return array Filtered list
*/
public function filter_plugins_limited( $plugins ) {
$this->plugins_original_list = $plugins;
if ( is_admin() ) {
return $plugins;
}
$this->load_configuration();
if ( $plugins ) {
foreach ( $plugins as $key => $plugin ) {
if ( $this->is_disabled( $this->get_plugin_slug( $plugin ) ) ) {
unset( $plugins[ $key ] );
$this->control['disabled'][] = $plugin;
} else {
$this->control['enabled'][] = $plugin;
}
}
}
return $plugins;
}
/**
* Restores original list of plugins (just to not deactivate temporairly disabled plugin).
* Execute after plugins loading but before WP logic of plugins manipulation.
*
* @return void
*/
public function plugins_restore_original_list() {
add_filter( 'option_active_plugins', array( $this, 'filter_plugins_original' ) );
}
/**
* Restores original list of plugins.
*
* @param array $plugins Input list.
* @return array Initial list of plugins (before manipulation).
*/
public function filter_plugins_original( $plugins ) {
return ( ! empty( $this->plugins_original_list ) ) ? $this->plugins_original_list : $plugins;
}
}
$gonzales_helper = new GonzalesHelper();
Warning: Cannot modify header information - headers already sent by (output started at /home/golishi/public_html/wp-content/mu-plugins/gonzales.php:1) in /home/golishi/public_html/wp-includes/pluggable.php on line 1531
Warning: Cannot modify header information - headers already sent by (output started at /home/golishi/public_html/wp-content/mu-plugins/gonzales.php:1) in /home/golishi/public_html/wp-includes/pluggable.php on line 1534