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 */
1314class 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 */
241259function 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' );
0 commit comments