Completed
Pull Request — develop (#1328)
by Naveen
02:57
created

Admin_User_Option::connect_hook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Admin;
4
/**
5
 * @since 3.30.0
6
 * @author Naveen Muthusamy <[email protected]>
7
 */
8
class Admin_User_Option {
9
10
	const WORDLIFT_ADMIN = 'wl_is_wordlift_admin';
11
12
	public static function is_wordlift_admin() {
13
		return intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1;
14
	}
15
16
	public function connect_hook() {
17
		add_action( 'wordlift_user_settings_page', array( $this, 'render_checkbox' ) );
18
		add_action( 'edit_user_profile_update', array( $this, 'save_checkbox' ) );
19
		add_action( 'personal_options_update', array( $this, 'save_checkbox' ) );
20
	}
21
22
	public function save_checkbox() {
23
		if ( ! isset( $_POST[ self::WORDLIFT_ADMIN ] ) || ! current_user_can( 'manage_options' ) ) {
24
			return;
25
		}
26
		update_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, 1 );
27
	}
28
29
	public function render_checkbox() {
30
		if ( ! current_user_can( 'manage_options' ) ) {
31
			return;
32
		}
33
		$is_checked = intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1;
34
		echo $this->get_checkbox( $is_checked );
35
	}
36
37
	public static function get_checkbox( $is_checked ) {
38
		$checked    = $is_checked ? "checked='checked'" : '';
39
		$admin_text = __( 'Wordlift Admin', 'wordlift' );
40
41
		return "<tr><th>$admin_text</th><td><input type='checkbox' name='wl_is_wordlift_admin' $checked></td></tr>";
42
	}
43
44
}