Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.57% |
69 / 70 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Block | |
98.57% |
69 / 70 |
|
85.71% |
6 / 7 |
10 | |
0.00% |
0 / 1 |
| name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| init | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| enqueue_block_editor_assets | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| register | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| render_login_button | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
4.00 | |||
| markup | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Block class. |
| 4 | * |
| 5 | * This is useful for registering custom gutenberg block to |
| 6 | * add `Login with Google` button in desired place. |
| 7 | * |
| 8 | * Particularly useful in FSE. |
| 9 | * |
| 10 | * @package RtCamp\GoogleLogin |
| 11 | * @since 1.2.3 |
| 12 | */ |
| 13 | |
| 14 | declare( strict_types=1 ); |
| 15 | |
| 16 | namespace RtCamp\GoogleLogin\Modules; |
| 17 | |
| 18 | use RtCamp\GoogleLogin\Utils\Helper; |
| 19 | use RtCamp\GoogleLogin\Utils\GoogleClient; |
| 20 | use RtCamp\GoogleLogin\Interfaces\Module; |
| 21 | use function RtCamp\GoogleLogin\plugin; |
| 22 | |
| 23 | /** |
| 24 | * Class Block. |
| 25 | * |
| 26 | * @package RtCamp\GoogleLogin\Modules |
| 27 | */ |
| 28 | class Block implements Module { |
| 29 | |
| 30 | /** |
| 31 | * Script handle. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | const SCRIPT_HANDLE = 'google-login-block'; |
| 36 | |
| 37 | /** |
| 38 | * Assets object. |
| 39 | * |
| 40 | * @var Assets |
| 41 | */ |
| 42 | public $assets; |
| 43 | |
| 44 | /** |
| 45 | * Google client. |
| 46 | * |
| 47 | * @var GoogleClient |
| 48 | */ |
| 49 | public $client; |
| 50 | |
| 51 | /** |
| 52 | * Module name. |
| 53 | * |
| 54 | * @return string |
| 55 | */ |
| 56 | public function name(): string { |
| 57 | return 'google_login_block'; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Block constructor. |
| 62 | * |
| 63 | * @param Assets $assets Assets object. |
| 64 | * @param GoogleClient $client Google client object. |
| 65 | */ |
| 66 | public function __construct( Assets $assets, GoogleClient $client ) { |
| 67 | $this->assets = $assets; |
| 68 | $this->client = $client; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Initialization. |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function init(): void { |
| 77 | add_action( |
| 78 | 'wp_enqueue_scripts', |
| 79 | [ $this->assets, 'register_login_styles' ] |
| 80 | ); |
| 81 | |
| 82 | add_action( |
| 83 | 'enqueue_block_editor_assets', |
| 84 | [ $this, 'enqueue_block_editor_assets' ] |
| 85 | ); |
| 86 | |
| 87 | add_action( 'init', [ $this, 'register' ] ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Enqueue block editor assets. |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | public function enqueue_block_editor_assets() { |
| 96 | $this->assets->register_login_styles(); |
| 97 | $this->assets->register_script( |
| 98 | self::SCRIPT_HANDLE, |
| 99 | 'build/js/block-button.js', |
| 100 | [ |
| 101 | 'wp-blocks', |
| 102 | 'wp-element', |
| 103 | 'wp-editor', |
| 104 | 'wp-components', |
| 105 | ], |
| 106 | filemtime( trailingslashit( plugin()->assets_dir ) . 'build/js/block-button.js' ), |
| 107 | false |
| 108 | ); |
| 109 | |
| 110 | wp_enqueue_script( self::SCRIPT_HANDLE ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Register the block. |
| 115 | * |
| 116 | * @return void |
| 117 | */ |
| 118 | public function register(): void { |
| 119 | register_block_type( |
| 120 | 'google-login/login-button', |
| 121 | [ |
| 122 | 'editor_style' => $this->assets::LOGIN_BUTTON_STYLE_HANDLE, |
| 123 | 'style' => $this->assets::LOGIN_BUTTON_STYLE_HANDLE, |
| 124 | 'render_callback' => [ $this, 'render_login_button' ], |
| 125 | 'attributes' => [ |
| 126 | 'buttonText' => [ |
| 127 | 'type' => 'string', |
| 128 | ], |
| 129 | 'forceDisplay' => [ |
| 130 | 'type' => 'boolean', |
| 131 | 'default' => false, |
| 132 | ], |
| 133 | ], |
| 134 | ] |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Render callback for block. |
| 140 | * |
| 141 | * This will output the Login with Google |
| 142 | * button if user is not logged in currently. |
| 143 | * |
| 144 | * @param string $attributes Block attributes. |
| 145 | * |
| 146 | * @return string |
| 147 | */ |
| 148 | public function render_login_button( $attributes ): string { |
| 149 | /** |
| 150 | * This filter is useful where we want to forcefully display login button, |
| 151 | * even when user is already logged-in in system. |
| 152 | * |
| 153 | * @param bool $display flag to display button. Default false. |
| 154 | * |
| 155 | * @since 1.2.3 |
| 156 | */ |
| 157 | $force_display = $attributes['forceDisplay'] ?? false; |
| 158 | if ( $force_display || ! is_user_logged_in() || apply_filters( 'rtcamp.google_login_button_display', false ) ) { |
| 159 | $markup = $this->markup( |
| 160 | [ |
| 161 | 'login_url' => $this->client->authorization_url(), |
| 162 | 'custom_btn_text' => $attributes['buttonText'] ?? false, |
| 163 | 'force_display_block' => $attributes['forceDisplay'] ?? false, |
| 164 | ] |
| 165 | ); |
| 166 | |
| 167 | ob_start(); |
| 168 | ?> |
| 169 | <div class="wp_google_login"> |
| 170 | <?php echo wp_kses_post( $markup ); ?> |
| 171 | </div> |
| 172 | <?php |
| 173 | |
| 174 | return ob_get_clean(); |
| 175 | } |
| 176 | |
| 177 | return ''; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Return markup for login button. |
| 182 | * |
| 183 | * @param array $args Arguments passed to template. |
| 184 | * |
| 185 | * @return string |
| 186 | */ |
| 187 | private function markup( array $args = [] ): string { |
| 188 | $args = wp_parse_args( |
| 189 | $args, |
| 190 | [ |
| 191 | 'login_url' => '#', |
| 192 | 'custom_btn_text' => '', |
| 193 | 'forceDisplay' => false, |
| 194 | ] |
| 195 | ); |
| 196 | |
| 197 | $template = trailingslashit( plugin()->template_dir ) . 'google-login-button.php'; |
| 198 | return Helper::render_template( $template, $args, false ); |
| 199 | } |
| 200 | } |