loginout.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/loginout` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/loginout` block on server.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string Returns the login-out link or form.
  13. */
  14. function render_block_core_loginout( $attributes ) {
  15. // Build the redirect URL.
  16. $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  17. $classes = is_user_logged_in() ? 'logged-in' : 'logged-out';
  18. $contents = wp_loginout(
  19. isset( $attributes['redirectToCurrent'] ) && $attributes['redirectToCurrent'] ? $current_url : '',
  20. false
  21. );
  22. // If logged-out and displayLoginAsForm is true, show the login form.
  23. if ( ! is_user_logged_in() && ! empty( $attributes['displayLoginAsForm'] ) ) {
  24. // Add a class.
  25. $classes .= ' has-login-form';
  26. // Get the form.
  27. $contents = wp_login_form( array( 'echo' => false ) );
  28. }
  29. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  30. return '<div ' . $wrapper_attributes . '>' . $contents . '</div>';
  31. }
  32. /**
  33. * Registers the `core/loginout` block on server.
  34. */
  35. function register_block_core_loginout() {
  36. register_block_type_from_metadata(
  37. __DIR__ . '/loginout',
  38. array(
  39. 'render_callback' => 'render_block_core_loginout',
  40. )
  41. );
  42. }
  43. add_action( 'init', 'register_block_core_loginout' );