1<?php
2/**
3 * Server-side rendering of the `core/loginout` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/loginout` block on server.
10 *
11 * @since 5.8.0
12 *
13 * @param array $attributes The block attributes.
14 *
15 * @return string Returns the login-out link or form.
16 */
17function render_block_core_loginout( $attributes ) {
18
19 /*
20 * Build the redirect URL. This current url fetching logic matches with the core.
21 *
22 * @see https://github.com/WordPress/wordpress-develop/blob/6bf62e58d21739938f3bb3f9e16ba702baf9c2cc/src/wp-includes/general-template.php#L528.
23 */
24 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
25
26 $user_logged_in = is_user_logged_in();
27
28 $classes = $user_logged_in ? 'logged-in' : 'logged-out';
29 $contents = wp_loginout(
30 isset( $attributes['redirectToCurrent'] ) && $attributes['redirectToCurrent'] ? $current_url : '',
31 false
32 );
33
34 // If logged-out and displayLoginAsForm is true, show the login form.
35 if ( ! $user_logged_in && ! empty( $attributes['displayLoginAsForm'] ) ) {
36 // Add a class.
37 $classes .= ' has-login-form';
38
39 // Get the form.
40 $contents = wp_login_form( array( 'echo' => false ) );
41 }
42
43 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
44
45 return '<div ' . $wrapper_attributes . '>' . $contents . '</div>';
46}
47
48/**
49 * Registers the `core/loginout` block on server.
50 *
51 * @since 5.8.0
52 */
53function register_block_core_loginout() {
54 register_block_type_from_metadata(
55 __DIR__ . '/loginout',
56 array(
57 'render_callback' => 'render_block_core_loginout',
58 )
59 );
60}
61add_action( 'init', 'register_block_core_loginout' );
62