Skip to content

Instantly share code, notes, and snippets.

@fastmover
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save fastmover/9818946 to your computer and use it in GitHub Desktop.

Select an option

Save fastmover/9818946 to your computer and use it in GitHub Desktop.
This is an object to make life easier when dealing with the Wordpress plugin Options Framework. Options Framework makes it much easier to create theme options. This is my efforts to help make it more efficient and this is still very incomplete. So far functionality covered is Color Picker, Font Styles Control, File, Tab and Small Select Box.
<?php
/**
* Created by PhpStorm.
* User: skohlmeyer
* Date: 3/27/14
* Time: 2:27 PM
*/
/*
Usage:
put this in your options.php file that you've copied over from the options framework plugin:
if(file_exists(get_stylesheet_directory() . '/options-fw.php'))
require(get_stylesheet_directory() . '/options-fw.php');
$ofw = new OptionsFW;
// This appends fonts to the font list in the font style control box.
$ofw->fonts(
array(
'Times New Roman' => 'Times New Roman',
'Georgia' => 'Georgia',
'Verdana' => 'Verdana'
)
);
// This creates a new tab on the theme options page in the admin control panel
$ofw->tab('Home Page');
// Create a color picker with a default color of black (#000000)
$ofw->color(
'home_page_color_1',
'Home page color 1',
'Color picker',
'#000000'
);
// Create a file upload box - for an image
$ofw->file(
'home_page_image',
'Home Page Image',
'Long Decription of home page image.'
);
// Font style box - font family, color, size and weight
$ofw->fontStyle(
'home_page_font',
'Home Page Font',
'Whatever long description this is for the home page font styles',
'#333333',
'15px',
'Georgia'
);
$options = $ofw->mergeOptions($options);
---------------------------------------------------------
To use this in the middle of an old options setup, simply do what you would above, but then merge the options at the proper point
$options = $ofw->mergeOptions($options);
*/
class OptionsFW {
protected $_fonts = array(
'Arial' => 'Arial',
'Helvetica' => 'Helvetica',
'sans-serif' => 'sans-serif'
);
protected $_options = array();
protected $_weights = array(
'normal' => 'Normal',
'bold' => 'Bold'
);
protected $_sizes = array( '8','10','12','14','16','18','20','22','24','26','28','30','32','34','36','38','40','42','44','48','52','72' );
public function __constructor()
{
}
/**
* Makes a Tab Control
*
* This will create a tab on the options page to separate different controls out
*
* @param string $title The title above the control
* @return null
*/
public function tab($title = null)
{
if(null === $title)
return;
$this->_options[] = array(
'name' => __($title, 'options_check'),
'type' => 'heading'
);
}
/**
* Makes Color Control Box
*
* This will return a color picker control
*
* @param string $id (REQUIRED) This is the unique ID used for accessing the control
* @param string $title The title above the control
* @param string $desc Longer description of this control that displays below the control
* @param string $defaultColor This will set the default color for the control
* @return null
*/
public function color($id = '', $title = '', $desc = '', $defaultColor = null)
{
if('' === $id)
return;
if(null === $defaultColor)
$defaultColor = '#ffffff';
$this->_options[] = array(
'name' => __($title, 'options_check'),
'desc' => __($desc, 'options_check'),
'id' => $id,
'std' => array(
'sizes' => '12px',
'faces' => 'Arial',
'styles' => 'normal',
'color' => $defaultColor
),
'type' => 'typography',
'options' => array(
'sizes' => false,
'faces' => false,
'styles' => false,
'color' => $defaultColor
)
);
}
/**
* Makes Font control box
*
* This will return a font control box with font-family, weight, size and color
*
* @param string $id (required) id is machine name used to reference this data
* @param string $title The title above the control
* @param string $desc Longer description of this control that displays below the control
* @param string $defaultColor This will set the default color for the control
* @param string $defaultSize This will set the default font-size for the control
* @param string $defaultFont This will set the default font-family for the control
* @param string $defaultStyle This will set the default font-weight for the control
* @throws Some_Exception_Class If something interesting cannot happen
* @return null
*/
public function fontStyle($id = '', $title = '', $desc = '', $defaultColor = null, $defaultSize = null, $defaultFont = null, $defaultStyle = null) {
if('' === $id)
return;
if(null === $defaultColor)
$defaultColor = '#ffffff';
if(null === $defaultSize)
$defaultSize = '12px';
if(null === $defaultFont)
$defaultFont = 'Arial';
if(null === $defaultStyle)
$defaultStyle = 'normal';
$this->_options[] = array( 'name' => __($title, 'options_check'),
'desc' => __($desc, 'options_check'),
'id' => $id,
'std' => array(
'size' => $defaultSize,
'face' => $defaultFont,
'style' => $defaultStyle,
'color' => $defaultColor
),
'type' => 'typography',
'options' => array(
'sizes' => $this->_sizes,
'faces' => $this->_fonts,
'styles' => $this->_weights,
'color' => $defaultColor
)
);
}
/**
* Makes Font control box
*
* This will return a font control box with font-family, weight, size and color
*
* @param string $id The unique id used to get the data back
* @param string $title The title above the control
* @param string $desc Longer description of this control that displays below the control
* @return null
*/
public function file($id = null, $title = null, $desc = null) {
if(null === $id)
return;
$this->_options[] = array(
'name' => __($title, 'options_check'),
'desc' => __($desc, 'options_check'),
'id' => $id,
'type' => 'upload'
);
}
/**
* Makes a select option box
*
* @param string $id The unique id used to get the data back
* @param string $title The title above the control
* @param string $desc Longer description of this control that displays below the control
* @return null
*/
public function selectSmall($id = null, $title = null, $desc = null, $options = array(), $default = '') {
if(null === $id)
return;
if(empty($options))
return;
$this->_options[] = array(
'name' => __($title, 'options_check'),
'desc' => __($desc, 'options_check'),
'id' => $id,
'std' => $default,
'type' => 'select',
'class' => 'mini',
'options' => $options
);
}
/**
* Makes a text field
*
* This returns a text input box.
*
* @param string $id The unique id used to get the data back
* @param string $title The title above the control
* @param string $desc Longer description of this control that displays below the control
* @return null
*/
public function input($id = null, $title = null, $desc = null, $default = '') {
if(null === $id)
return;
$this->_options[] = array(
'name' => __($title, 'options_check'),
'desc' => __($desc, 'options_check'),
'id' => $id,
'std' => $default,
'type' => 'text'
);
}
/**
* Makes a text area
*
* This returns a text area box.
*
* @param string $id The unique id used to get the data back
* @param string $title The title above the control
* @param string $desc Longer description of this control that displays below the control
* @return null
*/
public function textArea($id = null, $title = null, $desc = null, $default = '') {
if(null === $id)
return;
$this->_options[] = array(
'name' => __($title, 'options_check'),
'desc' => __($desc, 'options_check'),
'id' => $id,
'std' => $default,
'type' => 'textarea'
);
}
/**
* This will append usable fonts to the fonts list
*
* This will append to the list of usable fonts - in format: array('font' => 'actual font name') or an array of this: array(array('font_name' => 'Font Name'))
*
* @param array $fonts format of: array('font_name' => 'Font Name') or array(array('font_name' => 'Font Name'))
* @return null
*/
public function fonts($fonts = null)
{
if($fonts === null) {
return $this->_fonts;
}
if(is_array($fonts)) {
if( (array_keys($fonts) !== range(0, count($fonts) - 1)) ) {
foreach($fonts as $font => $fontvalue) {
if(!in_array($font, $this->_fonts)) {
$this->_fonts = array_merge($this->_fonts, array($font => $fontvalue));
}
}
} else {
$this->_fonts[] = $fonts;
}
array_multisort($this->_fonts, SORT_REGULAR);
}
}
/**
* This appends an array to the currently loaded options
*
* @param array $option The array to append to the options array
* @return null
*/
public function appendOption($option = array()) {
if(empty($option))
return;
$this->_options[] = $option;
}
/**
* This clears all currently loaded options
*
* This is useful if you're integrating this in the middle of an already built out options form
*
* @return null
*/
public function clearOptions() {
$this->_options = array();
}
/**
* Merges other options with currently loaded options
*
* This will return the merged options, if you don't specify false and 2nd parameter, this will also clear currently loaded options.
*
* @param array $options The options built outside of this object
* @param boolean $clear Whether to clear the currently loaded options after merging the two. Default: True
* @return array This returns an array of all the options
*/
public function mergeOptions($options = array(), $clear = true) {
if(empty($options))
return false;
$options = array_merge($options, $this->_options);
if($clear) {
$this->clearOptions();
}
return $options;
}
/**
* Returns all built options
*
* This will return all currently built controls with this instantiated object.
*
* @return options
*/
public function options()
{
return $this->_options;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment