author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
18 | 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 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 5.8.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
18 | 13 |
* @param array $attributes The block attributes. |
14 |
* |
|
15 |
* @return string Returns the login-out link or form. |
|
16 |
*/ |
|
17 |
function render_block_core_loginout( $attributes ) { |
|
18 |
||
19 |
// Build the redirect URL. |
|
20 |
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|
21 |
||
22 |
$classes = is_user_logged_in() ? 'logged-in' : 'logged-out'; |
|
23 |
$contents = wp_loginout( |
|
24 |
isset( $attributes['redirectToCurrent'] ) && $attributes['redirectToCurrent'] ? $current_url : '', |
|
25 |
false |
|
26 |
); |
|
27 |
||
28 |
// If logged-out and displayLoginAsForm is true, show the login form. |
|
29 |
if ( ! is_user_logged_in() && ! empty( $attributes['displayLoginAsForm'] ) ) { |
|
30 |
// Add a class. |
|
31 |
$classes .= ' has-login-form'; |
|
32 |
||
33 |
// Get the form. |
|
34 |
$contents = wp_login_form( array( 'echo' => false ) ); |
|
35 |
} |
|
36 |
||
37 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
|
38 |
||
39 |
return '<div ' . $wrapper_attributes . '>' . $contents . '</div>'; |
|
40 |
} |
|
41 |
||
42 |
/** |
|
19 | 43 |
* Registers the `core/loginout` block on server. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
44 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
45 |
* @since 5.8.0 |
18 | 46 |
*/ |
47 |
function register_block_core_loginout() { |
|
48 |
register_block_type_from_metadata( |
|
49 |
__DIR__ . '/loginout', |
|
50 |
array( |
|
51 |
'render_callback' => 'render_block_core_loginout', |
|
52 |
) |
|
53 |
); |
|
54 |
} |
|
55 |
add_action( 'init', 'register_block_core_loginout' ); |