Last active
January 9, 2017 16:37
-
-
Save greatislander/8ae50058643dee4f98fe25268ec2b231 to your computer and use it in GitHub Desktop.
Custom theme options in a new tab for Pressbooks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Word | |
Description: Options for Word exports. | |
Version: 1.0.0 | |
License: GPLv2 | |
*/ | |
namespace Word; | |
function add_tab( $tabs ) { | |
$tabs['word'] = '\Word\WordOptions'; | |
return $tabs; | |
} | |
add_filter( 'pb_theme_options_tabs', __NAMESPACE__ . '\\add_tab' ); | |
class WordOptions extends \Pressbooks\Options { | |
/** | |
* The value for option: pressbooks_theme_options_word_version | |
* | |
* @see upgrade() | |
* @var int | |
*/ | |
static $currentVersion = 1; | |
/** | |
* Word theme options. | |
* | |
* @var array | |
*/ | |
public $options; | |
/** | |
* Word theme defaults. | |
* | |
* @var array | |
*/ | |
public $defaults; | |
/** | |
* Constructor. | |
* | |
* @param array $options | |
*/ | |
function __construct( array $options ) { | |
$this->options = $options; | |
$this->defaults = $this->getDefaults(); | |
$this->booleans = $this->getBooleanOptions(); | |
$this->strings = $this->getStringOptions(); | |
$this->integers = $this->getIntegerOptions(); | |
$this->floats = $this->getFloatOptions(); | |
$this->predefined = $this->getPredefinedOptions(); | |
foreach ( $this->defaults as $key => $value ) { | |
if ( ! isset( $this->options[ $key ] ) ) { | |
$this->options[ $key ] = $value; | |
} | |
} | |
} | |
/** | |
* Configure the Word options tab using the settings API. | |
*/ | |
function init() { | |
$_page = $_option = 'pressbooks_theme_options_' . $this->getSlug(); | |
$_section = $this->getSlug() . '_options_section'; | |
if ( false == get_option( $_option ) ) { | |
add_option( $_option, $this->defaults ); | |
} | |
add_settings_section( | |
$_section, | |
$this->getTitle(), | |
array( $this, 'display' ), | |
$_page | |
); | |
add_settings_field( | |
'clippy', | |
__( 'Clippy', 'word' ), | |
array( $this, 'renderClippyField' ), | |
$_page, | |
$_section, | |
array( | |
__( 'Unleash an anthropomorphic paperclip upon the Pressbooks interface', 'word' ) | |
) | |
); | |
register_setting( | |
$_page, | |
$_option, | |
array( $this, 'sanitize' ) | |
); | |
} | |
/** | |
* Display the Word options tab description. | |
*/ | |
function display() { | |
echo '<p>' . __( 'These options apply to Word exports.', 'word' ) . '</p>'; | |
} | |
/** | |
* Render the Word options tab form (NOT USED). | |
*/ | |
function render() {} | |
/** | |
* Upgrade handler for Word options. | |
* | |
* @param int $version | |
*/ | |
function upgrade( $version ) { | |
if ( $version < 1 ) { | |
$this->doInitialUpgrade(); | |
} | |
} | |
/** | |
* Nothing to see here. | |
*/ | |
function doInitialUpgrade() {} | |
/** | |
* Render the clippy checkbox. | |
* @param array $args | |
*/ | |
function renderClippyField( $args ) { | |
$this->renderCheckbox( 'clippy', 'pressbooks_theme_options_' . $this->getSlug(), 'clippy', $this->options['clippy'], $args[0] ); | |
} | |
/** | |
* Get the slug for the Word options tab. | |
* | |
* @return string $slug | |
*/ | |
static function getSlug() { | |
return 'word'; | |
} | |
/** | |
* Get the localized title of the Word options tab. | |
* | |
* @return string $title | |
*/ | |
static function getTitle() { | |
return __( 'Word Options', 'word' ); | |
} | |
/** | |
* Filter the array of default values for the Word options tab. | |
* | |
* @param array $defaults | |
* @return array $defaults | |
*/ | |
static function filterDefaults( $defaults ) { | |
return $defaults; | |
} | |
/** | |
* Get an array of default values for the Word options tab. | |
* | |
* @return array $defaults | |
*/ | |
static function getDefaults() { | |
return array( | |
'clippy' => 1, | |
); | |
} | |
/** | |
* Get an array of options which return booleans. | |
* | |
* @return array $options | |
*/ | |
static function getBooleanOptions() { | |
return array( | |
'clippy', | |
); | |
} | |
/** | |
* Get an array of options which return strings. | |
* | |
* @return array $options | |
*/ | |
static function getStringOptions() { | |
return array(); | |
} | |
/** | |
* Get an array of options which return integers. | |
* | |
* @return array $options | |
*/ | |
static function getIntegerOptions() { | |
return array(); | |
} | |
/** | |
* Get an array of options which return floats. | |
* | |
* @return array $options | |
*/ | |
static function getFloatOptions() { | |
return array(); | |
} | |
/** | |
* Get an array of options which return predefined values. | |
* | |
* @return array $options | |
*/ | |
static function getPredefinedOptions() { | |
return array(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment