Completed
Push — master ( 82dd5a...1d4857 )
by Leon
02:41
created

CMB2_Meta_Box_Settings::user_options()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 7
c 2
b 1
f 0
nc 3
nop 0
dl 0
loc 13
rs 8.8571
1
<?php
2
/**
3
 * CMB2 Meta Box Settings.
4
 *
5
 * @since  0.1.0
6
 *
7
 * @category  WordPress_Plugin
8
 * @package   CMB2 Admin Extension
9
 * @author    twoelevenjay
10
 * @license   GPL-2.0+
11
 */
12
13
if ( ! class_exists( 'CMB2_Meta_Box_Settings' ) ) {
14
15
	/**
16
	 * Class CMB2_Meta_Box_Settings.
17
	 */
18
	class CMB2_Meta_Box_Settings {
19
20
		/**
21
		 * Field prefix.
22
		 *
23
		 * @var string
24
		 */
25
		private $prefix = '_cmb2_';
26
27
		/**
28
		 * Settings key, and option page slug.
29
		 *
30
		 * @var string
31
		 */
32
		private $settings_key = '_cmb2_settings';
33
34
		/**
35
		 * Settings page metabox id.
36
		 *
37
		 * @var string
38
		 */
39
		private $settings_metabox_id = '_cmb2_settings_metabox';
40
41
		/**
42
		 * Settings Page hook.
43
		 *
44
		 * @var string
45
		 */
46
		protected $settings_page = '';
47
48
		/**
49
		 * Initiate CMB2 Admin Extension object.
50
		 *
51
		 * @todo For now plugin will use one main object, will consider 3 seperate objects in the future.
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
52
		 * @todo Comment.
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
53
		 *
54
		 * @since 0.0.1
55
		 */
56
		public function __construct() {
57
58
			add_action( 'admin_init', array( $this, 'register_settings' ) );
59
			add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
60
			add_action( 'cmb2_init', array( $this, 'init_cmb2_settings_page' ) );
61
		}
62
63
		/**
64
		 * Set up the plugin settings page.
65
		 *
66
		 * @since  0.0.1
67
		 */
68
		public function add_settings_page() {
69
70
			if ( cmb2ae_metabox()->is_cmb2_allowed() ) {
71
				$this->settings_page = add_submenu_page( 'edit.php?post_type=meta_box', __( 'CMB2 Settings', 'cmb2-admin-extension' ), __( 'CMB2 Settings', 'cmb2-admin-extension' ), 'edit_posts', $this->settings_key, array( $this, 'settings_page' ) );
72
				add_action( "admin_print_styles-{$this->settings_page}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
73
			}
74
75
		}
76
77
		/**
78
		 * Register the setting.
79
		 */
80
		public function register_settings() {
81
82
			register_setting( $this->settings_key, $this->settings_key );
83
84
		}
85
86
		/**
87
		 * Plugin settings page call back.
88
		 *
89
		 * @since  0.0.1
90
		 */
91
		public function settings_page() {
92
93
			?>
94
			<div class="wrap cmb2-options-page <?php echo esc_attr( $this->settings_key ); ?>">
95
				<h2><?php echo esc_html__( 'CMB2 Settings', 'cmb2-admin-extension' ); ?></h2>
96
				<?php cmb2_metabox_form( $this->settings_metabox_id, $this->settings_key, array( 'disable_styles' => false ) ); ?>
97
			</div>
98
			<?php
99
100
		}
101
102
		/**
103
		 * Get users for the soptions on the ettings page.
104
		 *
105
		 * @since  0.0.6
106
		 */
107
		public function user_options() {
108
109
			$users = get_users();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
110
			$user_options = array();
111
			foreach ( $users as $user ) {
112
113
				if ( user_can( $user, 'update_plugins' ) || user_can( $user, 'install_plugins' ) || user_can( $user, 'delete_plugins' ) || user_can( $user, 'edit_theme_options' ) ) {
114
					$user_options[ $user->ID ] = $user->display_name;
115
				}
116
			}
117
118
			return $user_options;
119
		}
120
121
122
		/**
123
		 * This function needs documentation.
124
		 *
125
		 * @todo Document.
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
126
		 */
127
		public function init_cmb2_settings_page() {
128
129
			$prefix = $this->prefix;
130
131
			$cmb_settings = new_cmb2_box( array(
132
				'id'      => $this->settings_metabox_id,
133
				'hookup'  => false,
134
				'show_on' => array(
135
					'key'   => 'options-page',
136
					'value' => array( $this->settings_key ),
137
				),
138
			) );
139
140
			$cmb_settings->add_field( array(
141
				'name'    => __( 'Users', 'cmb2-admin-extension' ),
142
				'desc'    => __( 'Check the users to grant access to this plugin and the CMB2 plugin. Leave unchecked to grant access to all users.', 'cmb2-admin-extension' ),
143
				'id'      => $prefix . 'user_multicheckbox',
144
				'type'    => 'multicheck',
145
				'options' => $this->user_options(),
146
				'inline'  => true,
147
			) );
148
149
		}
150
	}
151
152
	$cmb2_meta_box_settings = new CMB2_Meta_Box_Settings();
153
}
154