Skip to content

Instantly share code, notes, and snippets.

@nikitasinelnikov
Created January 15, 2021 13:09
Show Gist options
  • Save nikitasinelnikov/91efa184d5d2a2c0b052747b58f8ac11 to your computer and use it in GitHub Desktop.
Save nikitasinelnikov/91efa184d5d2a2c0b052747b58f8ac11 to your computer and use it in GitHub Desktop.
Must User plugin for checking the conflicts
<?php
// if REMOTE_ADDR is empty or not correct you may use HTTP_X_FORWARDED_FOR or HTTP_CF_CONNECTING_IP
if ( isset( $_SERVER['REMOTE_ADDR'] ) && '{here your current IP}' == $_SERVER['REMOTE_ADDR'] ) {
if ( ! class_exists( 'UM_TEST_BAR' ) ) {
class UM_TEST_BAR {
var $plugin_dir;
var $plugin_url;
var $redirect_url = '';
/**
* constructor
**/
function __construct() {
$this->save_settings();
//setup proper directories
if ( defined( 'WP_CONTENT_URL' ) && defined( 'WP_CONTENT_DIR' ) && file_exists( WP_CONTENT_DIR . '/mu-plugins/um-test-bar.php' ) ) {
$this->plugin_dir = WP_CONTENT_DIR . '/mu-plugins/um-test-bar-includes/';
$this->plugin_url = WP_CONTENT_URL . '/mu-plugins/um-test-bar-includes/';
}
//allow plugins
add_filter( 'option_active_plugins', array( &$this, 'modify_plugins' ), 1 );
//change theme
add_filter( 'stylesheet', array (&$this, 'get_stylesheet' ), 1 );
add_filter( 'template', array( &$this, 'get_template' ), 1 );
//render bar
add_action( 'admin_head', array( &$this, 'render_bar' ), 99 );
add_action( 'admin_init', array( &$this, 'do_redirect' ), 99 );
}
function do_redirect() {
if ( !empty( $this->redirect_url ) ) {
wp_redirect( $this->redirect_url );
exit;
}
}
/*
* disable plugins for deny access from current plan
*/
function save_settings() {
if ( isset( $_POST['um_test_bar_form_submit'] ) ) {
update_option( 'um_test_bar', $_POST['um_test_bar_settings'] );
} elseif ( isset( $_REQUEST['um_test_bar_reset'] ) ) {
if( isset( $_SERVER['HTTP_REFERER'] )&& !empty( $_SERVER['HTTP_REFERER'] ) ) {
$current_url = $_SERVER['HTTP_REFERER'];
} else {
$schema = is_ssl() ? 'https://' : 'http://';
$current_url = $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = str_replace( 'um_test_bar_reset=1', '', $current_url );
}
delete_option( 'um_test_bar' );
$this->redirect_url = $current_url;
} elseif ( isset( $_REQUEST['um_test_bar_backup'] ) ) {
if( isset( $_SERVER['HTTP_REFERER'] )&& !empty( $_SERVER['HTTP_REFERER'] ) ) {
$current_url = $_SERVER['HTTP_REFERER'];
} else {
$schema = is_ssl() ? 'https://' : 'http://';
$current_url = $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = str_replace( 'um_test_bar_backup=1', '', $current_url );
}
$active_plugins = get_option( 'active_plugins' );
update_option( 'um_test_bar_backup', $active_plugins );
$this->redirect_url = $current_url;
} elseif ( isset( $_REQUEST['um_test_bar_restore'] ) ) {
if( isset( $_SERVER['HTTP_REFERER'] )&& !empty( $_SERVER['HTTP_REFERER'] ) ) {
$current_url = $_SERVER['HTTP_REFERER'];
} else {
$schema = is_ssl() ? 'https://' : 'http://';
$current_url = $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = str_replace( 'um_test_bar_restore=1', '', $current_url );
}
$backup_plugins = get_option( 'um_test_bar_backup' );
update_option( 'active_plugins', $backup_plugins );
$this->redirect_url = $current_url;
} elseif ( isset( $_REQUEST['um_test_bar_delete'] ) ) {
if( isset( $_SERVER['HTTP_REFERER'] )&& !empty( $_SERVER['HTTP_REFERER'] ) ) {
$current_url = $_SERVER['HTTP_REFERER'];
} else {
$schema = is_ssl() ? 'https://' : 'http://';
$current_url = $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = str_replace( 'um_test_bar_delete=1', '', $current_url );
}
delete_option( 'um_test_bar_backup' );
$this->redirect_url = $current_url;
}
}
/*
* activate some plugins
*/
function modify_plugins( $value ) {
$um_test_bar = get_option( 'um_test_bar', array() );
if ( isset( $um_test_bar['_plugins'] ) && is_array( $um_test_bar['_plugins'] ) && count( $um_test_bar['_plugins'] ) ) {
if ( in_array( '__none__', $um_test_bar['_plugins'] ) ) {
return array();
}
$value = $um_test_bar['_plugins'];
}
return $value;
}
/*
* change theme
*/
function get_stylesheet( $stylesheet ) {
$um_test_bar = get_option( 'um_test_bar', array() );
if ( isset( $um_test_bar['_theme'] ) && !empty( $um_test_bar['_theme'] ) ) {
$theme = wp_get_theme( $um_test_bar['_theme'] );
if ( empty( $theme ) )
return $stylesheet;
if ( isset($theme['Status'] ) && $theme['Status'] != 'publish' )
return $stylesheet;
return $theme['Stylesheet'];
}
return $stylesheet;
}
/*
* change theme
*/
function get_template( $template ) {
$um_test_bar = get_option( 'um_test_bar', array() );
if ( isset( $um_test_bar['_theme'] ) && !empty( $um_test_bar['_theme'] ) ) {
$theme = wp_get_theme( $um_test_bar['_theme'] );
if ( empty( $theme ) )
return $template;
if ( isset($theme['Status'] ) && $theme['Status'] != 'publish' )
return $template;
return $theme['Template'];
}
return $template;
}
/*
* render test bar
*/
function render_bar( $value ) {
$backup_plugins = get_option( 'um_test_bar_backup' );
$um_test_bar = get_option( 'um_test_bar', array() );
$active_plugins = get_option( 'active_plugins' );
$all_plugins = get_plugins();
$active_theme = get_option( 'template' );
$themes = wp_get_themes();
?>
<style type="text/css">
#um_test_bar_block {
background-color: #FAFAFA;
border: 1px solid #E5E5E5;
width: 300px;
position: fixed;
z-index: 9999999999999999999;
right: 0;
top: 63px;
}
</style>
<div id="um_test_bar_block">
<form name="um_test_bar_form" id="um_test_bar_form" method="post" >
<div style="margin: 5px 0 5px 10px;" >
<label for="um_test_bar_settings_plugins" >
Plugins:
<br />
<select name="um_test_bar_settings[_plugins][]" id="um_test_bar_settings_plugins" multiple="multiple" style="height: 200px; width: 290px;" >
<option value="__none__">None</option>
<?php
if ( isset( $um_test_bar['_plugins'] ) ) {
foreach( $all_plugins as $key => $value ) {
$selected = ( is_array( $um_test_bar['_plugins'] ) && in_array( $key, $um_test_bar['_plugins'] ) ) ? 'selected' : '';
echo '<option value="' . $key . '" ' . $selected . ' >' . $value['Name'] . '</option>';
}
} else {
foreach( $all_plugins as $key => $value ) {
$selected = ( is_array( $active_plugins ) && in_array( $key, $active_plugins ) ) ? 'selected' : '';
echo '<option value="' . $key . '" ' . $selected . ' >' . $value['Name'] . '</option>';
}
}
?>
</select>
</label>
<br>
<label for="um_test_bar_settings_theme" >
Theme:
<br />
<select name="um_test_bar_settings[_theme]" id="um_test_bar_settings_theme">
<?php
if ( isset( $um_test_bar['_theme'] ) ) {
foreach( $themes as $key => $value ) {
$selected = ( $um_test_bar['_theme'] == $key ) ? 'selected' : '';
echo '<option value="' . $key . '" ' . $selected . ' >' . $value['Name'] . '</option>';
}
} else {
foreach( $themes as $key => $value ) {
$selected = ( $active_theme == $key ) ? 'selected' : '';
echo '<option value="' . $key . '" ' . $selected . ' >' . $value['Name'] . '</option>';
}
}
?>
</select>
</label>
<br>
<br>
<input type="submit" class="button-primary" value="Submit" name="um_test_bar_form_submit">
<a href="<?php echo add_query_arg( array( 'um_test_bar_reset' => '1') ) ?>">Reset</a>
<?php if ( empty( $backup_plugins ) ) { ?>
&nbsp;
<a href="<?php echo add_query_arg( array( 'um_test_bar_backup' => '1') ) ?>">Backup</a>
<?php } else { ?>
&nbsp;
<a href="<?php echo add_query_arg( array( 'um_test_bar_restore' => '1') ) ?>">Restore</a>
&nbsp;
<a href="<?php echo add_query_arg( array( 'um_test_bar_delete' => '1') ) ?>" style="color: red;">Delete Backup</a>
<?php } ?>
</div>
</form>
</div>
<?php
return $value;
}
//end of class
}
}
$um_test_bar = new UM_TEST_BAR();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment