| 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 |
*/ |
| 17 |
function gutenberg_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 |
if ( wp_is_block_theme() ) { |
| 43 |
$processor = new WP_HTML_Tag_Processor( $contents ); |
| 44 |
|
| 45 |
while ( $processor->next_tag( 'input' ) ) { |
| 46 |
if ( 'submit' === $processor->get_attribute( 'type' ) && 'wp-submit' === $processor->get_attribute( 'name' ) ) { |
| 47 |
$processor->add_class( 'wp-block-button__link' ); |
| 48 |
$processor->add_class( wp_theme_get_element_class_name( 'button' ) ); |
| 49 |
$contents = $processor->get_updated_html(); |
| 50 |
break; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
| 57 |
|
| 58 |
return '<div ' . $wrapper_attributes . '>' . $contents . '</div>'; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Registers the `core/loginout` block on server. |
| 63 |
* |
| 64 |
* @since 5.8.0 |
| 65 |
*/ |
| 66 |
function gutenberg_register_block_core_loginout() { |
| 67 |
register_block_type_from_metadata( |
| 68 |
__DIR__ . '/loginout', |
| 69 |
array( |
| 70 |
'render_callback' => 'gutenberg_render_block_core_loginout', |
| 71 |
) |
| 72 |
); |
| 73 |
} |
| 74 |
add_action( 'init', 'gutenberg_register_block_core_loginout', 20 ); |
| 75 |
|