0 ) { setcookie( Changes::COOKIE, $id, (int) $draft['expires'], COOKIEPATH ?: '/', COOKIE_DOMAIN ?: '', is_ssl(), true ); } else { return; } wp_safe_redirect( remove_query_arg( Changes::QUERY ) ); exit; } public static function device(): void { $device = (string) ( $_COOKIE[ self::DEVICE_COOKIE ] ?? '' ); if ( is_admin() || ! in_array( $device, [ 'mobile', 'desktop' ], true ) || ! current_user_can( 'edit_theme_options' ) ) { return; } self::$device = $device; add_filter( 'rooster/device/class', static fn (): string => self::$device, 99 ); \Rooster\Core\DeviceClass::reset(); nocache_headers(); add_action( 'wp_body_open', [ self::class, 'banner' ] ); } public static function overlay(): void { if ( is_admin() ) { return; } $id = isset( $_COOKIE[ Changes::COOKIE ] ) ? preg_replace( '/[^a-zA-Z0-9]/', '', (string) $_COOKIE[ Changes::COOKIE ] ) : ''; if ( '' === $id ) { return; } $draft = Changes::get_draft( $id ); if ( ! $draft || (int) $draft['user'] !== get_current_user_id() ) { return; } self::$draft = $draft; nocache_headers(); add_filter( 'wp_robots', static fn ( $robots ) => array_merge( (array) $robots, [ 'noindex' => true, 'nofollow' => true ] ) ); if ( ! has_action( 'wp_body_open', [ self::class, 'banner' ] ) ) { add_action( 'wp_body_open', [ self::class, 'banner' ] ); } foreach ( $draft['ops'] as $op ) { $args = (array) ( $op['args'] ?? [] ); switch ( $op['type'] ?? '' ) { case 'settings': \Rooster\Core\Setup::override( (string) $args['group'], (array) $args['values'] ); break; case 'option': add_filter( 'option_' . (string) $args['name'], static fn () => $args['value'] ); break; case 'pins': add_filter( 'rooster/templates/pins', static fn () => (array) $args['pins'] ); break; case 'global_styles': add_filter( 'wp_theme_json_data_user', static function ( $theme_json ) use ( $args ) { $data = $theme_json->get_data(); return new \WP_Theme_JSON_Data( Changes::styles( is_array( $data ) ? $data : [], $args ), 'custom' ); } ); break; } } } public static function active(): bool { return null !== self::$draft; } public static function banner(): void { $parts = []; if ( self::$draft ) { $parts[] = esc_html__( 'Previewing a draft change:', 'ai-eshop-optimizer' ) . ' ' . esc_html( (string) self::$draft['summary'] ) . ''; } if ( '' !== self::$device ) { $parts[] = esc_html( 'mobile' === self::$device ? __( 'Phone page', 'ai-eshop-optimizer' ) : __( 'Computer page', 'ai-eshop-optimizer' ) ); } if ( ! $parts ) { return; } printf( '
%s %s
', implode( ' ยท ', $parts ), esc_url( add_query_arg( Changes::QUERY, 'off', home_url( '/' ) ) ), esc_html__( 'Stop previewing', 'ai-eshop-optimizer' ) ); } }