Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
70.70% |
111 / 157 |
|
76.92% |
10 / 13 |
CRAP | |
0.00% |
0 / 1 |
Settings | |
70.70% |
111 / 157 |
|
76.92% |
10 / 13 |
28.08 | |
0.00% |
0 / 1 |
__get | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
init | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
register_settings | |
100.00% |
56 / 56 |
|
100.00% |
1 / 1 |
1 | |||
client_id_field | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
client_secret_field | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
user_registration | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
one_tap_login | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
one_tap_login_screens | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
2 | |||
whitelisted_domains | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
settings_page | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
output | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
disabled | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | /** |
3 | * Register the settings under settings page and also |
4 | * provide the interface to retrieve the settings. |
5 | * |
6 | * @package RtCamp\GoogleLogin |
7 | * @since 1.0.0 |
8 | * @author rtCamp <contact@rtcamp.com> |
9 | */ |
10 | |
11 | declare(strict_types=1); |
12 | |
13 | namespace RtCamp\GoogleLogin\Modules; |
14 | |
15 | use RtCamp\GoogleLogin\Interfaces\Module as ModuleInterface; |
16 | |
17 | /** |
18 | * Class Settings. |
19 | * |
20 | * @property string|null whitelisted_domains |
21 | * @property string|null client_id |
22 | * @property string|null client_secret |
23 | * @property bool|null registration_enabled |
24 | * @property bool|null one_tap_login |
25 | * @property string one_tap_login_screen |
26 | * |
27 | * @package RtCamp\GoogleLogin\Modules |
28 | */ |
29 | class Settings implements ModuleInterface { |
30 | |
31 | /** |
32 | * Settings values. |
33 | * |
34 | * @var array |
35 | */ |
36 | public $options; |
37 | |
38 | /** |
39 | * Getters for settings values. |
40 | * |
41 | * @var string[] |
42 | */ |
43 | private $getters = [ |
44 | 'WP_GOOGLE_LOGIN_CLIENT_ID' => 'client_id', |
45 | 'WP_GOOGLE_LOGIN_SECRET' => 'client_secret', |
46 | 'WP_GOOGLE_LOGIN_USER_REGISTRATION' => 'registration_enabled', |
47 | 'WP_GOOGLE_LOGIN_WHITELIST_DOMAINS' => 'whitelisted_domains', |
48 | 'WP_GOOGLE_ONE_TAP_LOGIN' => 'one_tap_login', |
49 | 'WP_GOOGLE_ONE_TAP_LOGIN_SCREEN' => 'one_tap_login_screen', |
50 | ]; |
51 | |
52 | /** |
53 | * Getter method. |
54 | * |
55 | * @param string $name Name of option to fetch. |
56 | */ |
57 | public function __get( string $name ) { |
58 | if ( in_array( $name, $this->getters, true ) ) { |
59 | $constant_name = array_search( $name, $this->getters, true ); |
60 | |
61 | return defined( $constant_name ) ? constant( $constant_name ) : ( $this->options[ $name ] ?? '' ); |
62 | } |
63 | |
64 | return null; |
65 | } |
66 | |
67 | /** |
68 | * Return module name. |
69 | * |
70 | * @return string |
71 | */ |
72 | public function name(): string { |
73 | return 'settings'; |
74 | } |
75 | |
76 | /** |
77 | * Initialization of module. |
78 | * |
79 | * @return void |
80 | */ |
81 | public function init(): void { |
82 | $this->options = get_option( 'wp_google_login_settings', [] ); |
83 | add_action( 'admin_init', [ $this, 'register_settings' ] ); |
84 | add_action( 'admin_menu', [ $this, 'settings_page' ] ); |
85 | } |
86 | |
87 | /** |
88 | * Register the settings, section and fields. |
89 | * |
90 | * @return void |
91 | */ |
92 | public function register_settings(): void { |
93 | register_setting( 'wp_google_login', 'wp_google_login_settings' ); |
94 | |
95 | add_settings_section( |
96 | 'wp_google_login_section', |
97 | __( 'Log in with Google Settings', 'login-with-google' ), |
98 | function () { |
99 | }, |
100 | 'login-with-google' |
101 | ); |
102 | |
103 | add_settings_field( |
104 | 'wp_google_login_client_id', |
105 | __( 'Client ID', 'login-with-google' ), |
106 | [ $this, 'client_id_field' ], |
107 | 'login-with-google', |
108 | 'wp_google_login_section', |
109 | [ 'label_for' => 'client-id' ] |
110 | ); |
111 | |
112 | add_settings_field( |
113 | 'wp_google_login_client_secret', |
114 | __( 'Client Secret', 'login-with-google' ), |
115 | [ $this, 'client_secret_field' ], |
116 | 'login-with-google', |
117 | 'wp_google_login_section', |
118 | [ 'label_for' => 'client-secret' ] |
119 | ); |
120 | |
121 | add_settings_field( |
122 | 'wp_google_allow_registration', |
123 | __( 'Create New User', 'login-with-google' ), |
124 | [ $this, 'user_registration' ], |
125 | 'login-with-google', |
126 | 'wp_google_login_section', |
127 | [ 'label_for' => 'user-registration' ] |
128 | ); |
129 | |
130 | add_settings_field( |
131 | 'wp_google_one_tap_login', |
132 | __( 'Enable One Tap Login', 'login-with-google' ), |
133 | [ $this, 'one_tap_login' ], |
134 | 'login-with-google', |
135 | 'wp_google_login_section', |
136 | [ 'label_for' => 'one-tap-login' ] |
137 | ); |
138 | |
139 | add_settings_field( |
140 | 'wp_google_one_tap_login_screen', |
141 | __( 'One Tap Login Locations', 'login-with-google' ), |
142 | [ $this, 'one_tap_login_screens' ], |
143 | 'login-with-google', |
144 | 'wp_google_login_section', |
145 | [ 'label_for' => 'one-tap-login-screen' ] |
146 | ); |
147 | |
148 | add_settings_field( |
149 | 'wp_google_whitelisted_domain', |
150 | __( 'Whitelisted Domains', 'login-with-google' ), |
151 | [ $this, 'whitelisted_domains' ], |
152 | 'login-with-google', |
153 | 'wp_google_login_section', |
154 | [ 'label_for' => 'whitelisted-domains' ] |
155 | ); |
156 | } |
157 | |
158 | /** |
159 | * Render client ID field. |
160 | * |
161 | * @return void |
162 | */ |
163 | public function client_id_field(): void { ?> |
164 | <input type='text' name='wp_google_login_settings[client_id]' id="client-id" value='<?php echo esc_attr( $this->client_id ); ?>' autocomplete="off" <?php $this->disabled( 'client_id' ); ?> /> |
165 | <p class="description"> |
166 | <?php |
167 | echo wp_kses_post( |
168 | sprintf( |
169 | '<p>%1s <a target="_blank" href="%2s">%3s</a>.</p>', |
170 | esc_html__( 'Create oAuth Client ID and Client Secret at', 'login-with-google' ), |
171 | 'https://console.developers.google.com/apis/dashboard', |
172 | 'console.developers.google.com' |
173 | ) |
174 | ); |
175 | ?> |
176 | </p> |
177 | <?php |
178 | } |
179 | |
180 | /** |
181 | * Render client secret field. |
182 | * |
183 | * @return void |
184 | */ |
185 | public function client_secret_field(): void { |
186 | ?> |
187 | <input type='password' name='wp_google_login_settings[client_secret]' id="client-secret" value='<?php echo esc_attr( $this->client_secret ); ?>' autocomplete="off" <?php $this->disabled( 'client_secret' ); ?> /> |
188 | <?php |
189 | } |
190 | |
191 | /** |
192 | * User registration field. |
193 | * |
194 | * This will tell us whether or not to create the user |
195 | * if the user does not exist on WP application. |
196 | * |
197 | * This is irrespective of registration flag present in Settings > General |
198 | * |
199 | * @return void |
200 | */ |
201 | public function user_registration(): void { |
202 | ?> |
203 | <label style='display:block;margin-top:6px;'><input <?php $this->disabled( 'registration_enabled' ); ?> type='checkbox' |
204 | name='wp_google_login_settings[registration_enabled]' |
205 | id="user-registration" <?php echo esc_attr( checked( $this->registration_enabled ) ); ?> |
206 | value='1'> |
207 | <?php esc_html_e( 'Create a new user account if it does not exist already', 'login-with-google' ); ?> |
208 | </label> |
209 | <p class="description"> |
210 | <?php |
211 | echo wp_kses_post( |
212 | sprintf( |
213 | /* translators: %1s will be replaced by page link */ |
214 | __( 'If this setting is checked, a new user will be created even if <a target="_blank" href="%1s">membership setting</a> is off.', 'login-with-google' ), |
215 | is_multisite() ? 'network/settings.php' : 'options-general.php' |
216 | ) |
217 | ); |
218 | ?> |
219 | </p> |
220 | <?php |
221 | } |
222 | |
223 | /** |
224 | * Toggle One Tap Login functionality. |
225 | * |
226 | * @return void |
227 | */ |
228 | public function one_tap_login(): void { |
229 | ?> |
230 | <label style='display:block;margin-top:6px;'><input <?php $this->disabled( 'one_tap_login' ); ?> |
231 | type='checkbox' |
232 | name='wp_google_login_settings[one_tap_login]' |
233 | id="one-tap-login" <?php echo esc_attr( checked( $this->one_tap_login ) ); ?> |
234 | value='1'> |
235 | <?php esc_html_e( 'One Tap Login', 'login-with-google' ); ?> |
236 | </label> |
237 | <?php |
238 | } |
239 | |
240 | /** |
241 | * One tap login screens. |
242 | * |
243 | * It can be enabled only for wp-login.php OR sitewide. |
244 | * |
245 | * @return void |
246 | */ |
247 | public function one_tap_login_screens(): void { |
248 | $default = $this->one_tap_login_screen ?? ''; |
249 | ?> |
250 | <label style='display:block;margin-top:6px;'><input <?php $this->disabled( 'one_tap_login' ); ?> |
251 | type='radio' |
252 | name='wp_google_login_settings[one_tap_login_screen]' |
253 | id="one-tap-login-screen-login" <?php echo esc_attr( checked( $this->one_tap_login_screen, $default ) ); ?> |
254 | value='login'> |
255 | <?php esc_html_e( 'Enable One Tap Login Only on Login Screen', 'login-with-google' ); ?> |
256 | </label> |
257 | <label style='display:block;margin-top:6px;'><input <?php $this->disabled( 'one_tap_login' ); ?> |
258 | type='radio' |
259 | name='wp_google_login_settings[one_tap_login_screen]' |
260 | id="one-tap-login-screen-sitewide" <?php echo esc_attr( checked( $this->one_tap_login_screen, 'sitewide' ) ); ?> |
261 | value='sitewide'> |
262 | <?php esc_html_e( 'Enable One Tap Login Site-wide', 'login-with-google' ); ?> |
263 | </label> |
264 | <?php |
265 | // phpcs:disable |
266 | ?> |
267 | <script type="text/javascript"> |
268 | jQuery(document).ready(function () { |
269 | var toggle = function () { |
270 | var enabled = jQuery("#one-tap-login").is(":checked"); |
271 | var tr_elem = jQuery("#one-tap-login-screen-login").parents("tr"); |
272 | if (enabled) { |
273 | tr_elem.show(); |
274 | return; |
275 | } |
276 | |
277 | tr_elem.hide(); |
278 | }; |
279 | jQuery("#one-tap-login").on('change', toggle); |
280 | toggle(); |
281 | }); |
282 | </script> |
283 | <?php |
284 | // phpcs:enable |
285 | } |
286 | |
287 | /** |
288 | * Whitelisted domains for registration. |
289 | * |
290 | * Only emails belonging to these domains would be preferred |
291 | * for registration. |
292 | * |
293 | * If left blank, all domains would be allowed. |
294 | * |
295 | * @return void |
296 | */ |
297 | public function whitelisted_domains(): void { |
298 | ?> |
299 | <input <?php $this->disabled( 'whitelisted_domains' ); ?> type='text' name='wp_google_login_settings[whitelisted_domains]' id="whitelisted-domains" value='<?php echo esc_attr( $this->whitelisted_domains ); ?>' autocomplete="off" /> |
300 | <p class="description"> |
301 | <?php echo esc_html( __( 'Add each domain comma separated', 'login-with-google' ) ); ?> |
302 | </p> |
303 | <?php |
304 | } |
305 | |
306 | /** |
307 | * Add settings sub-menu page in admin menu. |
308 | * |
309 | * @return void |
310 | */ |
311 | public function settings_page(): void { |
312 | add_options_page( |
313 | __( 'Login with Google settings', 'login-with-google' ), |
314 | __( 'Login with Google', 'login-with-google' ), |
315 | 'manage_options', |
316 | 'login-with-google', |
317 | [ $this, 'output' ] |
318 | ); |
319 | } |
320 | |
321 | /** |
322 | * Output the plugin settings. |
323 | * |
324 | * @return void |
325 | */ |
326 | public function output(): void { |
327 | ?> |
328 | <div class="wrap"> |
329 | <form action='options.php' method='post'> |
330 | <?php |
331 | settings_fields( 'wp_google_login' ); |
332 | do_settings_sections( 'login-with-google' ); |
333 | submit_button(); |
334 | ?> |
335 | </form> |
336 | </div> |
337 | <?php |
338 | } |
339 | |
340 | /** |
341 | * Outputs the disabled attribute if field needs to |
342 | * be disabled. |
343 | * |
344 | * @param string $id Input ID. |
345 | * |
346 | * @return void |
347 | */ |
348 | private function disabled( string $id ): void { |
349 | if ( empty( $id ) ) { |
350 | return; |
351 | } |
352 | |
353 | $constant_name = array_search( $id, $this->getters, true ); |
354 | |
355 | if ( false !== $constant_name ) { |
356 | if ( defined( $constant_name ) ) { |
357 | echo esc_attr( 'disabled="disabled"' ); |
358 | } |
359 | } |
360 | } |
361 | } |