Skip to content

Commit

Permalink
Put bootstrap in a function to keep variables out of global space
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed Oct 30, 2015
1 parent ac13de7 commit 304b2e4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
90 changes: 51 additions & 39 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,63 @@
* @link http://webdevstudios.com
*/

if ( is_admin() ) {
/**
* Fires on the admin side when CMB2 is included/loaded.
*
* In most cases, this should be used to add metaboxes. See example-functions.php
*/
do_action( 'cmb2_admin_init' );
}

/**
* Fires when CMB2 is included/loaded
*
* Can be used to add metaboxes if needed on the front-end or needed on the front and backend.
* Function to encapsulate the CMB2 bootstrap process.
* @since 2.1.3
* @return void
*/
do_action( 'cmb2_init' );
function cmb2_bootstrap() {

/**
* For back-compat. Does the dirtywork of instantiating all the
* CMB2 instances for the cmb2_meta_boxes filter
* @since 2.0.2
*/
$all_meta_boxes_config = apply_filters( 'cmb2_meta_boxes', array() );
foreach ( (array) $all_meta_boxes_config as $meta_box_config ) {
new CMB2( $meta_box_config );
}
if ( is_admin() ) {
/**
* Fires on the admin side when CMB2 is included/loaded.
*
* In most cases, this should be used to add metaboxes. See example-functions.php
*/
do_action( 'cmb2_admin_init' );

/**
* Fires after all CMB2 instances are created
*/
do_action( 'cmb2_init_before_hookup' );
} else {
/**
* Fires when CMB2 is included/loaded
*
* Can be used to add metaboxes if needed on the front-end (or the front and backend).
*/
do_action( 'cmb2_init' );
}

/**
* Get all created metaboxes, and instantiate CMB2_hookup
* on metaboxes which require it.
* @since 2.0.2
*/
foreach ( CMB2_Boxes::get_all() as $cmb ) {
if ( $cmb->prop( 'hookup' ) ) {
$hookup = new CMB2_hookup( $cmb );
/**
* For back-compat. Does the dirty-work of instantiating all the
* CMB2 instances for the cmb2_meta_boxes filter
* @since 2.0.2
*/
$cmb_config_arrays = apply_filters( 'cmb2_meta_boxes', array() );
foreach ( (array) $cmb_config_arrays as $cmb_config ) {
new CMB2( $cmb_config );
}
}

/**
* Fires after CMB2 initiation process has been completed
*/
do_action( 'cmb2_after_init' );
/**
* Fires after all CMB2 instances are created
*/
do_action( 'cmb2_init_before_hookup' );

/**
* Get all created metaboxes, and instantiate CMB2_hookup
* on metaboxes which require it.
* @since 2.0.2
*/
foreach ( CMB2_Boxes::get_all() as $cmb ) {
if ( $cmb->prop( 'hookup' ) ) {
$hookup = new CMB2_hookup( $cmb );
}
if ( $cmb->prop( 'show_in_rest' ) ) {
add_action( 'rest_api_init', array( new CMB2_REST( $cmb ), 'register_fields' ) );
}
}

/**
* Fires after CMB2 initiation process has been completed
*/
do_action( 'cmb2_after_init' );
}

// End. That's it, folks! //
1 change: 1 addition & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public function include_cmb() {

// Kick the whole thing off
require_once 'bootstrap.php';
cmb2_bootstrap();
}

/**
Expand Down

0 comments on commit 304b2e4

Please sign in to comment.