Completed
Pull Request — develop (#1328)
by Naveen
03:36
created

Admin_User_Option   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A is_wordlift_admin() 0 3 1
A connect_hook() 0 5 1
A save_checkbox() 0 13 3
A render_checkbox() 0 7 2
A get_checkbox() 0 6 2
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
24
		if ( ! current_user_can( 'manage_options' ) ) {
25
			return;
26
		}
27
28
		if ( ! isset( $_POST[ self::WORDLIFT_ADMIN ] ) ) {
29
			delete_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN );
30
31
			return;
32
		}
33
		update_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, 1 );
34
	}
35
36
	public function render_checkbox() {
37
		if ( ! current_user_can( 'manage_options' ) ) {
38
			return;
39
		}
40
		$is_checked = intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1;
41
		echo $this->get_checkbox( $is_checked );
42
	}
43
44
	public static function get_checkbox( $is_checked ) {
45
		$checked    = $is_checked ? "checked='checked'" : '';
46
		$admin_text = __( 'Wordlift Admin', 'wordlift' );
47
48
		return "<tr><th>$admin_text</th><td><input type='checkbox' name='wl_is_wordlift_admin' $checked></td></tr>";
49
	}
50
51
}