1
1
<?php
2
+
2
3
/**
3
4
* CMB2 Genesis CPT Archive Metabox
4
5
*
5
6
* To fetch these options, use `genesis_get_cpt_option()`, e.g.
6
- * // In CPT archive template:
7
- * if ( genesis_has_post_type_archive_support() ) {
8
- * $color = genesis_get_cpt_option( 'test_colorpicker' );
9
- * }
7
+ * // In CPT archive template:
8
+ * if ( genesis_has_post_type_archive_support() ) {
9
+ * $color = genesis_get_cpt_option( 'test_colorpicker' );
10
+ * }
10
11
*
11
12
* @version 0.1.0
12
13
*/
13
14
class Myprefix_Genesis_CPT_Settings_Metabox {
14
15
15
16
/**
16
- * Mmetabox id
17
- * @var string
18
- */
17
+ * Metabox id
18
+ *
19
+ * @var string
20
+ */
19
21
protected $ metabox_id = 'genesis-cpt-archive-settings-metabox-%1$s ' ;
20
22
21
23
/**
22
- * CPT slug
23
- * @var string
24
- */
24
+ * CPT slug
25
+ *
26
+ * @var string
27
+ */
25
28
protected $ post_type = '' ;
26
29
27
30
/**
28
- * CPT slug
29
- * @var string
30
- */
31
+ * CPT slug
32
+ *
33
+ * @var string
34
+ */
31
35
protected $ admin_hook = '%1$s_page_genesis-cpt-archive-%1$s ' ;
32
36
33
37
/**
34
- * Option key, and option page slug
35
- * @var string
36
- */
38
+ * Option key, and option page slug
39
+ *
40
+ * @var string
41
+ */
37
42
protected $ key = 'genesis-cpt-archive-settings-%1$s ' ;
38
43
39
44
/**
@@ -55,7 +60,7 @@ class Myprefix_Genesis_CPT_Settings_Metabox {
55
60
*
56
61
* @since 0.1.0
57
62
*
58
- * @param string $post_type Post type slug
63
+ * @param string $post_type Post type slug.
59
64
*
60
65
* @return Myprefix_Genesis_CPT_Settings_Metabox
61
66
*/
@@ -70,7 +75,10 @@ public static function get_instance( $post_type ) {
70
75
71
76
/**
72
77
* Constructor
78
+ *
73
79
* @since 0.1.0
80
+ *
81
+ * @param string $post_type Post type slug.
74
82
*/
75
83
protected function __construct ( $ post_type ) {
76
84
$ this ->post_type = $ post_type ;
@@ -81,6 +89,7 @@ protected function __construct( $post_type ) {
81
89
82
90
/**
83
91
* Initiate our hooks
92
+ *
84
93
* @since 0.1.0
85
94
*/
86
95
public function hooks () {
@@ -92,22 +101,24 @@ public function hooks() {
92
101
93
102
/**
94
103
* Initiate admin hooks.
95
- * @since 0.1.0
104
+ *
105
+ * @since 0.1.0
96
106
*/
97
107
public function init () {
98
- // Add custom archive support for CPT
108
+ // Add custom archive support for CPT.
99
109
add_post_type_support ( $ this ->post_type , 'genesis-cpt-archives-settings ' );
100
110
}
101
111
102
112
/**
103
113
* Add admin hooks.
114
+ *
104
115
* @since 0.1.0
105
116
*/
106
117
public function admin_hooks () {
107
- // Include CMB CSS in the head to avoid FOUC
118
+ // Include CMB CSS in the head to avoid FOUC.
108
119
add_action ( "admin_print_styles- {$ this ->admin_hook }" , array ( 'CMB2_hookup ' , 'enqueue_cmb_css ' ) );
109
120
110
- // Hook into the genesis cpt setttings save and add in the CMB2 sanitized values.
121
+ // Hook into the genesis cpt settings save and add in the CMB2 sanitized values.
111
122
add_filter ( "sanitize_option_genesis-cpt-archive-settings- {$ this ->post_type }" , array ( $ this , 'add_sanitized_values ' ), 999 );
112
123
113
124
// Hook up our Genesis metabox.
@@ -116,6 +127,7 @@ public function admin_hooks() {
116
127
117
128
/**
118
129
* Hook up our Genesis metabox.
130
+ *
119
131
* @since 0.1.0
120
132
*/
121
133
public function add_meta_box () {
@@ -132,7 +144,8 @@ public function add_meta_box() {
132
144
133
145
/**
134
146
* Output our Genesis metabox.
135
- * @since 0.1.0
147
+ *
148
+ * @since 0.1.0
136
149
*/
137
150
public function output_metabox () {
138
151
$ cmb = $ this ->init_metabox ();
@@ -144,7 +157,8 @@ public function output_metabox() {
144
157
*
145
158
* @since 0.1.0
146
159
*
147
- * @param array $new_value Array of values for the setting.
160
+ * @param array $new_value Array of values for the setting.
161
+ *
148
162
* @return array Updated array of values for the setting.
149
163
*/
150
164
public function add_sanitized_values ( $ new_value ) {
@@ -163,7 +177,7 @@ public function add_sanitized_values( $new_value ) {
163
177
/**
164
178
* Register our Genesis metabox and return the CMB2 instance.
165
179
*
166
- * @since 0.1.0
180
+ * @since 0.1.0
167
181
*
168
182
* @return CMB2 instance.
169
183
*/
@@ -181,14 +195,13 @@ public function init_metabox() {
181
195
// 'priority' => 'low', // Defaults to 'high'.
182
196
'object_types ' => array ( $ this ->admin_hook ),
183
197
'show_on ' => array (
184
- // These are important, don't remove
198
+ // These are important, don't remove.
185
199
'key ' => 'options-page ' ,
186
- 'value ' => array ( $ this ->key , )
200
+ 'value ' => array ( $ this ->key ),
187
201
),
188
202
), $ this ->key , 'options-page ' );
189
203
190
- // Set our CMB2 fields
191
-
204
+ // Set our CMB2 fields.
192
205
$ this ->cmb ->add_field ( array (
193
206
'name ' => __ ( 'Test Text ' , 'myprefix ' ),
194
207
'desc ' => __ ( 'field description (optional) ' , 'myprefix ' ),
@@ -210,12 +223,17 @@ public function init_metabox() {
210
223
211
224
/**
212
225
* Public getter method for retrieving protected/private variables
213
- * @since 0.1.0
214
- * @param string $field Field to retrieve
215
- * @return mixed Field value or exception is thrown
226
+ *
227
+ * @since 0.1.0
228
+ *
229
+ * @param string $field Field to retrieve.
230
+ *
231
+ * @throws Exception Throws an exception if the field is invalid.
232
+ *
233
+ * @return mixed Field value or exception is thrown
216
234
*/
217
235
public function __get ( $ field ) {
218
- // Allowed fields to retrieve
236
+ // Allowed fields to retrieve.
219
237
if ( 'cmb ' === $ field ) {
220
238
return $ this ->init_metabox ();
221
239
}
@@ -232,15 +250,15 @@ public function __get( $field ) {
232
250
/**
233
251
* Helper function to get/return the Myprefix_Genesis_CPT_Settings_Metabox object.
234
252
*
235
- * @since 0.1.0
253
+ * @since 0.1.0
236
254
*
237
- * @param string $post_type Post type slug
255
+ * @param string $post_type Post type slug.
238
256
*
239
257
* @return Myprefix_Genesis_CPT_Settings_Metabox object
240
258
*/
241
259
function myprefix_genesis_cpt_settings ( $ post_type ) {
242
260
return Myprefix_Genesis_CPT_Settings_Metabox::get_instance ( $ post_type );
243
261
}
244
262
245
- // Get it started
263
+ // Get it started.
246
264
// myprefix_genesis_cpt_settings( 'custom-post-type-slug' );
0 commit comments