Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* CMB2 Theme Options
* @version 0.1.0
*/
class RTC_Admin {
/**
* Option key, and option page slug
* @var string
*/
protected $key = 'RTC_options';
/**
* Options page metabox id
* @var string
*/
protected $metabox_id = 'RTC_option_metabox';
/**
* Options Page title
* @var string
*/
protected $title = '';
/**
* Options Page hook
* @var string
*/
protected $options_page = '';
/**
* Holds an instance of the object
*
* @var RTC_Admin
*/
protected static $instance = null;
/**
* Returns the running object
*
* @return RTC_Admin
*/
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() {
// Set our title
$this->title = __( 'Tracking Codes', 'RTC' );
}
/**
* Initiate our hooks
* @since 0.1.0
*/
public function hooks() {
add_action( 'admin_init', array( $this, 'init' ) );
add_action( 'admin_menu', array( $this, 'add_options_page' ) );
add_action( 'cmb2_admin_init', array( $this, 'add_options_page_metabox' ) );
add_action( 'wp_footer', array( $this, 'get_tracking_codes' ) );
}
/**
* Register our setting to WP
* @since 0.1.0
*/
public function init() {
register_setting( $this->key, $this->key );
}
/**
* Add menu options page
* @since 0.1.0
*/
public function add_options_page() {
$this->options_page = add_menu_page( $this->title, $this->title, 'manage_options', $this->key, array( $this, 'admin_page_display' ) );
// Include CMB CSS in the head to avoid FOUC
add_action( "admin_print_styles-{$this->options_page}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
}
/**
* Admin page markup. Mostly handled by CMB2
* @since 0.1.0
*/
public function admin_page_display() {
?>
<div class="wrap cmb2-options-page <?php echo esc_attr( $this->key ); ?>">
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
<?php cmb2_metabox_form( $this->metabox_id, $this->key ); ?>
</div>
<?php
}
/**
* Add the options metabox to the array of metaboxes
* @since 0.1.0
*/
function add_options_page_metabox() {
// hook in our save notices
add_action( "cmb2_save_options-page_fields_{$this->metabox_id}", array( $this, 'settings_notices' ), 10, 2 );
$cmb = new_cmb2_box( array(
'id' => $this->metabox_id,
'hookup' => false,
'cmb_styles' => false,
'show_on' => array(
// These are important, don't remove
'key' => 'options-page',
'value' => array( $this->key ),
),
));
$group_field_id = $cmb->add_field( array(
'id' => 'tracking_codes',
'name' => 'Tracking Codes',
'type' => 'group',
'description' => __( 'Conditionally load tracking codes on pages', 'cmb2' ),
'options' => array(
'group_title' => __( 'Code {#}', 'cmb2' ), // since version 1.1.4, {#} gets replaced by row number
'add_button' => __( 'Add Another Code', 'cmb2' ),
'remove_button' => __( 'Remove Code', 'cmb2' ),
'sortable' => true, // beta
),
));
$cmb->add_group_field( $group_field_id, array(
'name' => 'Admin Label',
'id' => 'title',
'type' => 'text',
));
$cmb->add_group_field( $group_field_id, array(
'name' => 'Tracking Code',
'id' => 'tracking_code',
'type' => 'textarea_code',
));
$cmb->add_group_field( $group_field_id, array(
'name' => 'Include or Exclude?',
'description' => 'Select whether the code will be on all pages, included on the pages below, or excluded on the pages below. <br>If this is set to "All", the page select box is not applicable.',
'id' => 'include_exclude',
'type' => 'radio_inline',
'options' => [
'all' => 'All',
'include' => 'Include',
'exclude' => 'Exclude',
],
));
$cmb->add_group_field( $group_field_id, array(
'name' => __( 'Pages', 'cmb2' ),
'desc' => __( 'Drag posts from the left column to the right column to select them.<br />You may rearrange the order of the posts in the right column by dragging and dropping.', 'cmb2' ),
'id' => 'pages',
'type' => 'custom_attached_posts',
'options' => array(
'show_thumbnails' => true, // Show thumbnails on the left
'filter_boxes' => true, // Show a text box for filtering the results
'query_args' => array(
'posts_per_page' => 100,
'post_type' => 'page',
), // override the get_posts args
),
));
}
/**
* Register settings notices for display
*
* @since 0.1.0
* @param int $object_id Option key
* @param array $updated Array of updated fields
* @return void
*/
public function settings_notices( $object_id, $updated ) {
if ( $object_id !== $this->key || empty( $updated ) ) {
return;
}
add_settings_error( $this->key . '-notices', '', __( 'Settings updated.', 'RTC' ), 'updated' );
settings_errors( $this->key . '-notices' );
}
/**
* Public getter method for retrieving protected/private variables
* @since 0.1.0
* @param string $field Field to retrieve
* @return mixed Field value or exception is thrown
*/
public function __get( $field ) {
// Allowed fields to retrieve
if ( in_array( $field, array( 'key', 'metabox_id', 'title', 'options_page' ), true ) ) {
return $this->{$field};
}
throw new Exception( 'Invalid property: ' . $field );
}
}
/**
* Helper function to get/return the RTC_Admin object
* @since 0.1.0
* @return RTC_Admin object
*/
function rtc_admin() {
return RTC_Admin::get_instance();
}
/**
* Wrapper function around cmb2_get_option
* @since 0.1.0
* @param string $key Options array key
* @param mixed $default Optional default value
* @return mixed Option value
*/
function rtc_get_option( $key = '', $default = null ) {
if ( function_exists( 'cmb2_get_option' ) ) {
// Use cmb2_get_option as it passes through some key filters.
return cmb2_get_option( RTC_admin()->key, $key, $default );
}
// Fallback to get_option if CMB2 is not loaded yet.
$opts = get_option( RTC_admin()->key, $key, $default );
$val = $default;
if ( 'all' === $key ) {
$val = $opts;
} elseif ( array_key_exists( $key, $opts ) && false !== $opts[ $key ] ) {
$val = $opts[ $key ];
}
return $val;
}
// Get it started
rtc_admin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.