Skip to content

Commit e7b9d49

Browse files
authored
Merge pull request CMB2#63 from timothyjensen/develop
Bring snippets up to WordPress coding standards.
2 parents 6703cc3 + 86093e1 commit e7b9d49

File tree

2 files changed

+98
-64
lines changed

2 files changed

+98
-64
lines changed

options-and-settings-pages/genesis-cpt-archive-metabox.php

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
11
<?php
2+
23
/**
34
* CMB2 Genesis CPT Archive Metabox
45
*
56
* 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+
* }
1011
*
1112
* @version 0.1.0
1213
*/
1314
class Myprefix_Genesis_CPT_Settings_Metabox {
1415

1516
/**
16-
* Mmetabox id
17-
* @var string
18-
*/
17+
* Metabox id
18+
*
19+
* @var string
20+
*/
1921
protected $metabox_id = 'genesis-cpt-archive-settings-metabox-%1$s';
2022

2123
/**
22-
* CPT slug
23-
* @var string
24-
*/
24+
* CPT slug
25+
*
26+
* @var string
27+
*/
2528
protected $post_type = '';
2629

2730
/**
28-
* CPT slug
29-
* @var string
30-
*/
31+
* CPT slug
32+
*
33+
* @var string
34+
*/
3135
protected $admin_hook = '%1$s_page_genesis-cpt-archive-%1$s';
3236

3337
/**
34-
* Option key, and option page slug
35-
* @var string
36-
*/
38+
* Option key, and option page slug
39+
*
40+
* @var string
41+
*/
3742
protected $key = 'genesis-cpt-archive-settings-%1$s';
3843

3944
/**
@@ -55,7 +60,7 @@ class Myprefix_Genesis_CPT_Settings_Metabox {
5560
*
5661
* @since 0.1.0
5762
*
58-
* @param string $post_type Post type slug
63+
* @param string $post_type Post type slug.
5964
*
6065
* @return Myprefix_Genesis_CPT_Settings_Metabox
6166
*/
@@ -70,7 +75,10 @@ public static function get_instance( $post_type ) {
7075

7176
/**
7277
* Constructor
78+
*
7379
* @since 0.1.0
80+
*
81+
* @param string $post_type Post type slug.
7482
*/
7583
protected function __construct( $post_type ) {
7684
$this->post_type = $post_type;
@@ -81,6 +89,7 @@ protected function __construct( $post_type ) {
8189

8290
/**
8391
* Initiate our hooks
92+
*
8493
* @since 0.1.0
8594
*/
8695
public function hooks() {
@@ -92,22 +101,24 @@ public function hooks() {
92101

93102
/**
94103
* Initiate admin hooks.
95-
* @since 0.1.0
104+
*
105+
* @since 0.1.0
96106
*/
97107
public function init() {
98-
// Add custom archive support for CPT
108+
// Add custom archive support for CPT.
99109
add_post_type_support( $this->post_type, 'genesis-cpt-archives-settings' );
100110
}
101111

102112
/**
103113
* Add admin hooks.
114+
*
104115
* @since 0.1.0
105116
*/
106117
public function admin_hooks() {
107-
// Include CMB CSS in the head to avoid FOUC
118+
// Include CMB CSS in the head to avoid FOUC.
108119
add_action( "admin_print_styles-{$this->admin_hook}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
109120

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.
111122
add_filter( "sanitize_option_genesis-cpt-archive-settings-{$this->post_type}", array( $this, 'add_sanitized_values' ), 999 );
112123

113124
// Hook up our Genesis metabox.
@@ -116,6 +127,7 @@ public function admin_hooks() {
116127

117128
/**
118129
* Hook up our Genesis metabox.
130+
*
119131
* @since 0.1.0
120132
*/
121133
public function add_meta_box() {
@@ -132,7 +144,8 @@ public function add_meta_box() {
132144

133145
/**
134146
* Output our Genesis metabox.
135-
* @since 0.1.0
147+
*
148+
* @since 0.1.0
136149
*/
137150
public function output_metabox() {
138151
$cmb = $this->init_metabox();
@@ -144,7 +157,8 @@ public function output_metabox() {
144157
*
145158
* @since 0.1.0
146159
*
147-
* @param array $new_value Array of values for the setting.
160+
* @param array $new_value Array of values for the setting.
161+
*
148162
* @return array Updated array of values for the setting.
149163
*/
150164
public function add_sanitized_values( $new_value ) {
@@ -163,7 +177,7 @@ public function add_sanitized_values( $new_value ) {
163177
/**
164178
* Register our Genesis metabox and return the CMB2 instance.
165179
*
166-
* @since 0.1.0
180+
* @since 0.1.0
167181
*
168182
* @return CMB2 instance.
169183
*/
@@ -181,14 +195,13 @@ public function init_metabox() {
181195
// 'priority' => 'low', // Defaults to 'high'.
182196
'object_types' => array( $this->admin_hook ),
183197
'show_on' => array(
184-
// These are important, don't remove
198+
// These are important, don't remove.
185199
'key' => 'options-page',
186-
'value' => array( $this->key, )
200+
'value' => array( $this->key ),
187201
),
188202
), $this->key, 'options-page' );
189203

190-
// Set our CMB2 fields
191-
204+
// Set our CMB2 fields.
192205
$this->cmb->add_field( array(
193206
'name' => __( 'Test Text', 'myprefix' ),
194207
'desc' => __( 'field description (optional)', 'myprefix' ),
@@ -210,12 +223,17 @@ public function init_metabox() {
210223

211224
/**
212225
* 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
216234
*/
217235
public function __get( $field ) {
218-
// Allowed fields to retrieve
236+
// Allowed fields to retrieve.
219237
if ( 'cmb' === $field ) {
220238
return $this->init_metabox();
221239
}
@@ -232,15 +250,15 @@ public function __get( $field ) {
232250
/**
233251
* Helper function to get/return the Myprefix_Genesis_CPT_Settings_Metabox object.
234252
*
235-
* @since 0.1.0
253+
* @since 0.1.0
236254
*
237-
* @param string $post_type Post type slug
255+
* @param string $post_type Post type slug.
238256
*
239257
* @return Myprefix_Genesis_CPT_Settings_Metabox object
240258
*/
241259
function myprefix_genesis_cpt_settings( $post_type ) {
242260
return Myprefix_Genesis_CPT_Settings_Metabox::get_instance( $post_type );
243261
}
244262

245-
// Get it started
263+
// Get it started.
246264
// myprefix_genesis_cpt_settings( 'custom-post-type-slug' );

options-and-settings-pages/genesis-settings-metabox.php

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
<?php
2+
23
/**
34
* CMB2 Genesis Settings Metabox
45
*
56
* To fetch these options, use `genesis_get_option()`, e.g.
6-
*
7-
* $color = genesis_get_option( 'test_colorpicker' );
7+
* $color = genesis_get_option( 'test_colorpicker' );
88
*
99
* @version 0.1.0
1010
*/
1111
class Myprefix_Genesis_Settings_Metabox {
1212

1313
/**
14-
* Option key. Could maybe be 'genesis-seo-settings', or other section?
15-
* @var string
16-
*/
14+
* Option key. Could maybe be 'genesis-seo-settings', or other section?
15+
*
16+
* @var string
17+
*/
1718
protected $key = 'genesis-settings';
1819

1920
/**
20-
* The admin page slug.
21-
* @var string
22-
*/
21+
* The admin page slug.
22+
*
23+
* @var string
24+
*/
2325
protected $admin_page = 'genesis';
2426

2527
/**
26-
* Options page metabox id
27-
* @var string
28-
*/
28+
* Options page metabox id
29+
*
30+
* @var string
31+
*/
2932
protected $metabox_id = 'myprefix_genesis_settings';
3033

3134
/**
32-
* Admin page hook
33-
* @var string
34-
*/
35+
* Admin page hook
36+
*
37+
* @var string
38+
*/
3539
protected $admin_hook = 'toplevel_page_genesis';
3640

3741
/**
@@ -64,13 +68,15 @@ public static function get_instance() {
6468

6569
/**
6670
* Constructor
71+
*
6772
* @since 0.1.0
6873
*/
6974
protected function __construct() {
7075
}
7176

7277
/**
7378
* Initiate our hooks
79+
*
7480
* @since 0.1.0
7581
*/
7682
public function hooks() {
@@ -80,10 +86,11 @@ public function hooks() {
8086

8187
/**
8288
* Add menu options page
89+
*
8390
* @since 0.1.0
8491
*/
8592
public function admin_hooks() {
86-
// Include CMB CSS in the head to avoid FOUC
93+
// Include CMB CSS in the head to avoid FOUC.
8794
add_action( "admin_print_styles-{$this->admin_hook}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
8895

8996
// Hook into the genesis cpt setttings save and add in the CMB2 sanitized values.
@@ -96,6 +103,7 @@ public function admin_hooks() {
96103

97104
/**
98105
* Hook up our Genesis metabox.
106+
*
99107
* @since 0.1.0
100108
*/
101109
public function add_meta_box() {
@@ -112,7 +120,8 @@ public function add_meta_box() {
112120

113121
/**
114122
* Output our Genesis metabox.
115-
* @since 0.1.0
123+
*
124+
* @since 0.1.0
116125
*/
117126
public function output_metabox() {
118127
$cmb = $this->init_metabox();
@@ -124,7 +133,8 @@ public function output_metabox() {
124133
*
125134
* @since 0.1.0
126135
*
127-
* @param array $new_value Array of values for the setting.
136+
* @param array $new_value Array of values for the setting.
137+
*
128138
* @return array Updated array of values for the setting.
129139
*/
130140
public function add_sanitized_values( $new_value ) {
@@ -153,22 +163,21 @@ public function init_metabox() {
153163
}
154164

155165
$this->cmb = cmb2_get_metabox( array(
156-
'id' => $this->metabox_id,
166+
'id' => $this->metabox_id,
157167
'title' => __( 'I\'m a Genesis Settings CMB2 metabox', 'myprefix' ),
158168
'hookup' => false, // We'll handle ourselves. (add_sanitized_values())
159169
'cmb_styles' => false, // We'll handle ourselves. (admin_hooks())
160170
'context' => 'main', // Important for Genesis.
161171
// 'priority' => 'low', // Defaults to 'high'.
162172
'object_types' => array( $this->admin_hook ),
163173
'show_on' => array(
164-
// These are important, don't remove
174+
// These are important, don't remove.
165175
'key' => 'options-page',
166-
'value' => array( $this->key, )
176+
'value' => array( $this->key ),
167177
),
168178
), $this->key, 'options-page' );
169179

170-
// Set our CMB2 fields
171-
180+
// Set our CMB2 fields.
172181
$this->cmb->add_field( array(
173182
'name' => __( 'Test Text', 'myprefix' ),
174183
'desc' => __( 'field description (optional)', 'myprefix' ),
@@ -189,17 +198,22 @@ public function init_metabox() {
189198
}
190199

191200
/**
192-
* Public getter method for retrieving protected/private variables
193-
* @since 0.1.0
194-
* @param string $field Field to retrieve
195-
* @return mixed Field value or exception is thrown
201+
* Public getter method for retrieving protected/private variables.
202+
*
203+
* @since 0.1.0
204+
*
205+
* @param string $field Field to retrieve.
206+
*
207+
* @throws Exception Throws an exception if the field is invalid.
208+
*
209+
* @return mixed Field value or exception is thrown
196210
*/
197211
public function __get( $field ) {
198212
if ( 'cmb' === $field ) {
199213
return $this->init_metabox();
200214
}
201215

202-
// Allowed fields to retrieve
216+
// Allowed fields to retrieve.
203217
if ( in_array( $field, array( 'key', 'admin_page', 'metabox_id', 'admin_hook' ), true ) ) {
204218
return $this->{$field};
205219
}
@@ -211,12 +225,14 @@ public function __get( $field ) {
211225

212226
/**
213227
* Helper function to get/return the Myprefix_Genesis_Settings_Metabox object
214-
* @since 0.1.0
228+
*
229+
* @since 0.1.0
230+
*
215231
* @return Myprefix_Genesis_Settings_Metabox object
216232
*/
217233
function myprefix_genesis_settings_metabox() {
218234
return Myprefix_Genesis_Settings_Metabox::get_instance();
219235
}
220236

221-
// Get it started
237+
// Get it started.
222238
myprefix_genesis_settings_metabox();

0 commit comments

Comments
 (0)