'en_US', 'pl' => 'pl_PL', 'ru' => 'ru_RU', ); private const LOCALE_COOKIE = 'smilia_demo_locale'; private const DENIED_CAPS = array( 'install_plugins', 'update_plugins', 'delete_plugins', 'edit_plugins', 'upload_plugins', 'install_themes', 'update_themes', 'delete_themes', 'edit_themes', 'upload_themes', 'update_core', 'update_languages', 'install_languages', 'create_users', 'delete_users', 'edit_users', 'promote_users', 'remove_users', 'add_users', 'export', 'import', 'edit_files', 'unfiltered_upload', 'export_others_personal_data', 'erase_others_personal_data', 'manage_privacy_options', ); private const WRITE_SQL = '/^\s*(?:INSERT|UPDATE|DELETE|REPLACE|ALTER|CREATE|DROP|TRUNCATE|RENAME|GRANT|REVOKE|LOCK|LOAD)\b/i'; public static function enabled(): bool { return defined( 'SMILIA_DEMO' ) && SMILIA_DEMO; } public function register(): void { if ( ! self::enabled() ) { return; } add_filter( 'query', array( $this, 'guard_query' ), PHP_INT_MAX ); add_filter( 'user_has_cap', array( $this, 'limit_caps' ), PHP_INT_MAX ); add_filter( 'rest_pre_dispatch', array( $this, 'guard_rest' ), 0, 3 ); add_action( 'admin_init', array( $this, 'guard_admin' ), 0 ); add_action( 'template_redirect', array( $this, 'redirect_front' ), 0 ); add_filter( 'rest_authentication_errors', array( $this, 'guard_rest_auth' ), PHP_INT_MAX ); add_filter( 'allow_password_reset', '__return_false' ); add_filter( 'show_password_fields', '__return_false' ); add_filter( 'pre_wp_mail', '__return_false' ); add_filter( 'pre_option_blog_public', '__return_zero' ); add_action( 'admin_notices', array( $this, 'notice' ) ); add_action( 'enqueue_block_editor_assets', array( $this, 'quiet_editor_errors' ) ); add_filter( 'login_message', array( $this, 'login_message' ) ); add_filter( 'lost_password_html_link', '__return_empty_string' ); add_action( 'login_form_login', array( $this, 'auto_login' ) ); add_filter( 'get_user_metadata', array( $this, 'user_locale' ), 10, 3 ); add_action( 'init', array( $this, 'remember_locale' ), 0 ); foreach ( array( 'lostpassword', 'retrievepassword', 'rp', 'resetpass', 'register' ) as $action ) { add_action( 'login_form_' . $action, array( $this, 'deny' ) ); } } public static function install(): int { global $wpdb; $admin = get_role( 'administrator' ); if ( ! $admin ) { return 0; } remove_role( self::ROLE ); add_role( self::ROLE, __( 'Демо-просмотр', 'smilia-core' ), array_diff_key( $admin->capabilities, array_flip( self::DENIED_CAPS ) ) ); $user_id = wp_insert_user( array( 'user_login' => self::LOGIN, 'user_pass' => wp_generate_password( 64, true, true ), 'user_email' => self::LOGIN . '@smilia.invalid', 'display_name' => 'Drill and Fill', 'role' => self::ROLE, 'locale' => 'en_US', ) ); if ( is_wp_error( $user_id ) ) { return 0; } $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET post_author = %d", $user_id ) ); return (int) $user_id; } public function guard_query( $query ) { if ( ! is_string( $query ) || ! preg_match( self::WRITE_SQL, $query ) || $this->login_request() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { return $query; } global $wpdb; if ( false !== strpos( $query, $wpdb->options ) && preg_match( "/'_(?:site_)?transient_/", $query ) ) { return $query; } return ''; } public function limit_caps( array $allcaps ): array { foreach ( self::DENIED_CAPS as $cap ) { unset( $allcaps[ $cap ] ); } return $allcaps; } public function guard_rest( $result, $server, $request ) { if ( in_array( $request->get_method(), array( 'GET', 'HEAD', 'OPTIONS' ), true ) ) { return $result; } return new WP_Error( 'smilia_demo', self::message(), array( 'status' => 403 ) ); } public function redirect_front(): void { $public = defined( 'SMILIA_PUBLIC_URL' ) ? (string) SMILIA_PUBLIC_URL : ''; if ( '' === $public ) { status_header( 404 ); exit; } $uri = isset( $_SERVER['REQUEST_URI'] ) ? '/' . ltrim( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/' ) : '/'; wp_redirect( untrailingslashit( $public ) . $uri, 301, 'Smilia' ); exit; } public function guard_rest_auth( $result ) { if ( is_wp_error( $result ) || is_user_logged_in() ) { return $result; } return new WP_Error( 'smilia_demo', self::message(), array( 'status' => 401 ) ); } public function guard_admin(): void { $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET'; if ( wp_doing_ajax() ) { $action = isset( $_REQUEST['action'] ) ? sanitize_key( wp_unslash( $_REQUEST['action'] ) ) : ''; if ( in_array( $action, array( 'heartbeat', 'rest-nonce' ), true ) ) { return; } wp_send_json_error( array( 'message' => self::message() ), 403 ); } $writes_on_load = array( 'post-new.php', 'media-new.php', 'erase-personal-data.php', 'export-personal-data.php' ); if ( in_array( $GLOBALS['pagenow'] ?? '', $writes_on_load, true ) ) { $this->deny(); } $read = in_array( $method, array( 'GET', 'HEAD' ), true ) && ! isset( $_REQUEST['_wpnonce'] ) && ! isset( $_REQUEST['_ajax_nonce'] ); if ( ! $read ) { $this->deny(); } } public function auto_login(): void { $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET'; $user = get_user_by( 'login', self::LOGIN ); if ( 'GET' !== $method || isset( $_GET['loggedout'] ) || ! $user ) { return; } wp_set_current_user( $user->ID ); wp_set_auth_cookie( $user->ID, false, true ); $redirect = isset( $_GET['redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) : ''; $redirect = wp_validate_redirect( $redirect, '' ); wp_safe_redirect( '' !== $redirect ? $redirect : admin_url() ); exit; } public function user_locale( $value, $object_id, $meta_key ) { $locale = self::requested_locale(); if ( 'locale' !== $meta_key || '' === $locale ) { return $value; } $user = get_userdata( (int) $object_id ); return $user && self::LOGIN === $user->user_login ? array( $locale ) : $value; } public function remember_locale(): void { $code = isset( $_GET['locale'] ) ? sanitize_key( wp_unslash( $_GET['locale'] ) ) : ''; if ( isset( self::LOCALES[ $code ] ) && ! headers_sent() ) { setcookie( self::LOCALE_COOKIE, $code, 0, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, true, true ); } } private static function requested_locale(): string { $code = isset( $_GET['locale'] ) ? sanitize_key( wp_unslash( $_GET['locale'] ) ) : ''; if ( ! isset( self::LOCALES[ $code ] ) ) { $code = isset( $_COOKIE[ self::LOCALE_COOKIE ] ) ? sanitize_key( wp_unslash( $_COOKIE[ self::LOCALE_COOKIE ] ) ) : ''; } return self::LOCALES[ $code ] ?? ''; } public function deny(): void { wp_die( esc_html( self::message() ), esc_html__( 'Демо-доступ', 'smilia-core' ), array( 'response' => 403, 'back_link' => true ) ); } public function quiet_editor_errors(): void { $blank = array( ' ' ); $data = array( 'Updating failed.' => $blank, 'Publishing failed.' => $blank, 'Scheduling failed.' => $blank, ); wp_add_inline_script( 'wp-editor', 'wp.i18n.setLocaleData( ' . wp_json_encode( $data ) . ' );' ); wp_add_inline_script( 'wp-edit-post', "wp.data.dispatch( 'core/preferences' ).set( 'core/edit-post', 'welcomeGuide', false );" ); } public function notice(): void { printf( '
%s