Нет описания

login-button.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Premium Content Login Button Child Block.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. namespace Automattic\Jetpack\Extensions\Premium_Content;
  8. use Automattic\Jetpack\Blocks;
  9. use Jetpack_Gutenberg;
  10. require_once dirname( __DIR__ ) . '/_inc/subscription-service/include.php';
  11. const LOGIN_BUTTON_NAME = 'premium-content/login-button';
  12. /**
  13. * Registers the block for use in Gutenberg
  14. * This is done via an action so that we can disable
  15. * registration if we need to.
  16. */
  17. function register_login_button_block() {
  18. // Only load this block on WordPress.com.
  19. if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || jetpack_is_atomic_site() ) {
  20. Blocks::jetpack_register_block(
  21. LOGIN_BUTTON_NAME,
  22. array(
  23. 'render_callback' => __NAMESPACE__ . '\render_login_button_block',
  24. )
  25. );
  26. }
  27. }
  28. add_action( 'init', __NAMESPACE__ . '\register_login_button_block' );
  29. /**
  30. * Render callback.
  31. *
  32. * @param array $attributes Array containing the block attributes.
  33. * @param string $content String containing the block content.
  34. *
  35. * @return string
  36. */
  37. function render_login_button_block( $attributes, $content ) {
  38. if ( ! pre_render_checks() ) {
  39. return '';
  40. }
  41. if ( is_user_logged_in() ) {
  42. // The viewer is logged it, so they shouldn't see the login button.
  43. return '';
  44. }
  45. Jetpack_Gutenberg::load_styles_as_required( LOGIN_BUTTON_NAME );
  46. $url = subscription_service()->access_url();
  47. return preg_replace( '/(<a\b[^><]*)>/i', '$1 href="' . esc_url( $url ) . '">', $content );
  48. }