| 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 |
* @param array $attributes The block attributes. |
| 12 |
* |
| 13 |
* @return string Returns the login-out link or form. |
| 14 |
*/ |
| 15 |
function gutenberg_render_block_core_loginout( $attributes ) { |
| 16 |
|
| 17 |
// Build the redirect URL. |
| 18 |
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 19 |
|
| 20 |
$classes = is_user_logged_in() ? 'logged-in' : 'logged-out'; |
| 21 |
$contents = wp_loginout( |
| 22 |
isset( $attributes['redirectToCurrent'] ) && $attributes['redirectToCurrent'] ? $current_url : '', |
| 23 |
false |
| 24 |
); |
| 25 |
|
| 26 |
// If logged-out and displayLoginAsForm is true, show the login form. |
| 27 |
if ( ! is_user_logged_in() && ! empty( $attributes['displayLoginAsForm'] ) ) { |
| 28 |
// Add a class. |
| 29 |
$classes .= ' has-login-form'; |
| 30 |
|
| 31 |
// Get the form. |
| 32 |
$contents = wp_login_form( array( 'echo' => false ) ); |
| 33 |
} |
| 34 |
|
| 35 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
| 36 |
|
| 37 |
return '<div ' . $wrapper_attributes . '>' . $contents . '</div>'; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Registers the `core/loginout` block on server. |
| 42 |
*/ |
| 43 |
function gutenberg_register_block_core_loginout() { |
| 44 |
register_block_type_from_metadata( |
| 45 |
__DIR__ . '/loginout', |
| 46 |
array( |
| 47 |
'render_callback' => 'gutenberg_render_block_core_loginout', |
| 48 |
) |
| 49 |
); |
| 50 |
} |
| 51 |
add_action( 'init', 'gutenberg_register_block_core_loginout', 20 ); |
| 52 |
|