-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathgenesis-settings-metabox.php
More file actions
238 lines (207 loc) · 5.18 KB
/
genesis-settings-metabox.php
File metadata and controls
238 lines (207 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* CMB2 Genesis Settings Metabox
*
* To fetch these options, use `genesis_get_option()`, e.g.
* $color = genesis_get_option( 'test_colorpicker' );
*
* @version 0.1.0
*/
class Myprefix_Genesis_Settings_Metabox {
/**
* Option key. Could maybe be 'genesis-seo-settings', or other section?
*
* @var string
*/
protected $key = 'genesis-settings';
/**
* The admin page slug.
*
* @var string
*/
protected $admin_page = 'genesis';
/**
* Options page metabox id
*
* @var string
*/
protected $metabox_id = 'myprefix_genesis_settings';
/**
* Admin page hook
*
* @var string
*/
protected $admin_hook = 'toplevel_page_genesis';
/**
* Holds an instance of CMB2
*
* @var CMB2
*/
protected $cmb = null;
/**
* Holds an instance of the object
*
* @var Myprefix_Genesis_Settings_Metabox
*/
protected static $instance = null;
/**
* Returns the running object
*
* @return Myprefix_Genesis_Settings_Metabox
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
self::$instance->hooks();
}
return self::$instance;
}
/**
* Constructor
*
* @since 0.1.0
*/
protected function __construct() {
}
/**
* Initiate our hooks
*
* @since 0.1.0
*/
public function hooks() {
add_action( 'admin_menu', array( $this, 'admin_hooks' ) );
add_action( 'cmb2_admin_init', array( $this, 'init_metabox' ) );
}
/**
* Add menu options page
*
* @since 0.1.0
*/
public function admin_hooks() {
// Include CMB CSS in the head to avoid FOUC.
add_action( "admin_print_styles-{$this->admin_hook}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
// Hook into the genesis cpt setttings save and add in the CMB2 sanitized values.
add_filter( "sanitize_option_{$this->key}", array( $this, 'add_sanitized_values' ), 999 );
// Hook up our Genesis metabox.
add_action( 'genesis_theme_settings_metaboxes', array( $this, 'add_meta_box' ) );
}
/**
* Hook up our Genesis metabox.
*
* @since 0.1.0
*/
public function add_meta_box() {
$cmb = $this->init_metabox();
add_meta_box(
$cmb->cmb_id,
$cmb->prop( 'title' ),
array( $this, 'output_metabox' ),
$this->admin_hook,
$cmb->prop( 'context' ),
$cmb->prop( 'priority' )
);
}
/**
* Output our Genesis metabox.
*
* @since 0.1.0
*/
public function output_metabox() {
$cmb = $this->init_metabox();
$cmb->show_form( $cmb->object_id(), $cmb->object_type() );
}
/**
* If saving the cpt settings option, add the CMB2 sanitized values.
*
* @since 0.1.0
*
* @param array $new_value Array of values for the setting.
*
* @return array Updated array of values for the setting.
*/
public function add_sanitized_values( $new_value ) {
if ( ! empty( $_POST ) ) {
$cmb = $this->init_metabox();
$new_value = array_merge(
$new_value,
$cmb->get_sanitized_values( $_POST )
);
}
return $new_value;
}
/**
* Register our Genesis metabox and return the CMB2 instance.
*
* @since 0.1.0
*
* @return CMB2 instance.
*/
public function init_metabox() {
if ( null !== $this->cmb ) {
return $this->cmb;
}
$this->cmb = cmb2_get_metabox( array(
'id' => $this->metabox_id,
'title' => __( 'I\'m a Genesis Settings CMB2 metabox', 'myprefix' ),
'hookup' => false, // We'll handle ourselves. (add_sanitized_values())
'cmb_styles' => false, // We'll handle ourselves. (admin_hooks())
'context' => 'main', // Important for Genesis.
// 'priority' => 'low', // Defaults to 'high'.
'object_types' => array( $this->admin_hook ),
'show_on' => array(
// These are important, don't remove.
'key' => 'options-page',
'value' => array( $this->key ),
),
), $this->key, 'options-page' );
// Set our CMB2 fields.
$this->cmb->add_field( array(
'name' => __( 'Test Text', 'myprefix' ),
'desc' => __( 'field description (optional)', 'myprefix' ),
'id' => 'test_text',
'type' => 'text',
// 'default' => 'Default Text',
) );
$this->cmb->add_field( array(
'name' => __( 'Test Color Picker', 'myprefix' ),
'desc' => __( 'field description (optional)', 'myprefix' ),
'id' => 'test_colorpicker',
'type' => 'colorpicker',
'default' => '#bada55',
) );
return $this->cmb;
}
/**
* Public getter method for retrieving protected/private variables.
*
* @since 0.1.0
*
* @param string $field Field to retrieve.
*
* @throws Exception Throws an exception if the field is invalid.
*
* @return mixed Field value or exception is thrown
*/
public function __get( $field ) {
if ( 'cmb' === $field ) {
return $this->init_metabox();
}
// Allowed fields to retrieve.
if ( in_array( $field, array( 'key', 'admin_page', 'metabox_id', 'admin_hook' ), true ) ) {
return $this->{$field};
}
throw new Exception( 'Invalid property: ' . $field );
}
}
/**
* Helper function to get/return the Myprefix_Genesis_Settings_Metabox object
*
* @since 0.1.0
*
* @return Myprefix_Genesis_Settings_Metabox object
*/
function myprefix_genesis_settings_metabox() {
return Myprefix_Genesis_Settings_Metabox::get_instance();
}
// Get it started.
myprefix_genesis_settings_metabox();