Skip to content

Instantly share code, notes, and snippets.

@hakre
Forked from mikeschinkel/special-background.php
Created August 28, 2010 09:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakre/554944 to your computer and use it in GitHub Desktop.
Save hakre/554944 to your computer and use it in GitHub Desktop.
<?php
/**
* Special Background Wordpress Plugin
*
* @-wp-header Plugin Name: Special Background
* @-wp-header Plugin URI: http://wordpress.stackexchange.com/questions/972/
* @-wp-header Description: Example to show how to add a special background using exiting background admin page in core.
* @-wp-header Version: 0.2
* @-wp-header Author: Mike Schinkel, hakre
* @-wp-header Author URI: http://mikeschinkel.com/custom-wordpress-plugins/
*/
return SpecialBackgroundPlugin::bootstrap();
class SpecialBackgroundPlugin {
private $title = 'Special Background';
private $slug = 'special-background';
private $accessLevel = 'manage_options';
private $parentPage = 'themes.php';
private $optionPrefix = 'mods_';
/** Theme Modification
* @var SpecialBackgroundMod
*/
private $mod;
/** Option Swapper
* @var SpecialBackgroundOptionSwapper
*/
private $swapper;
static $instance;
public function isAdminPage() {
return $this->parentPage === $GLOBALS['pagenow']
&& isset($_GET['page'])
&& $this->slug === $_GET['page'];
}
/**
* Plugin Bootstrap
*
* @return SpecialBackgroundPlugin
*/
public static function bootstrap() {
(self::$instance === null) &&
(self::$instance = new self());
return self::$instance;
}
/**
* Plugin Constructor
*/
public function __construct() {
$this->title = __($this->title);
add_filter('admin_menu', array($this, 'admin_menu'));
add_filter('admin_init', array($this, 'admin_init'));
$optionName = $this->optionPrefix . get_current_theme();
$this->swapper = new SpecialBackgroundOptionSwapper($optionName, $this);
}
public function admin_init() {
if ($this->isAdminPage()) {
$optionName = $this->optionPrefix . get_current_theme();
'mods_' . get_current_theme();
$this->mod = new SpecialBackgroundMod($optionName);
wp_enqueue_script('custom-background');
wp_enqueue_style ('farbtastic');
}
$hook = sprintf('load-appearance_page_%s', $this->slug);
foreach(array(
'admin_load' => 10,
'take_action' => 49,
'handle_upload' => 49,
) as $func => $priority)
add_action($hook, array($GLOBALS['custom_background'], $func), $priority);
}
public function admin_menu() {
$title = $this->title;
$slug = $this->slug;
add_theme_page($title, $title,'edit_theme_options', $slug, array($this, 'admin_page'));
}
public function admin_page() {
if (!$this->isAdminPage())
return;
if (!current_user_can($this->accessLevel))
return;
$title = $this->title;
$GLOBALS['parent_file'] = $this->parentPage;
$GLOBALS['submenu_file'] = $this->parentPage.'?page=special-background';
$GLOBALS['title'] = $title; // FIXME goes nowhere so far, check and/or remove
require_once(ABSPATH . 'wp-admin/admin-header.php');
ob_start();
$GLOBALS['custom_background']->admin_page();
echo preg_replace('#<h2>([^<]+)</h2>#', "<h2>{$title}</h2>", ob_get_clean());
}
} // class
/**
* SpecialBackground Theme Modification Class
*/
class SpecialBackgroundMod {
/**
* Theme Mod Properties Prefix
*
* @var string
*/
private $prefix = 'background_';
/**
* Theme Mod Properties
* @var array
*/
private $properties = array(
'image',
'image_thumb',
'repeat',
'position_x',
'attachment',
'color',
);
private $wpFilterPrefix = 'theme_mod_';
private $funcPrefix = 'background_';
private $optionName;
public function __construct($optionName) {
$this->optionName = $optionName;
$this->AddFilter();
}
/**
* AddFilter - Register all theme modification properties
*/
public function AddFilter() {
$wp_prefix = $this->wpFilterPrefix . $this->prefix;
$my_prefix = $this->funcPrefix;
foreach($this->properties as $property)
add_filter($wp_prefix.$property, array($this, $my_prefix.$property));
}
/**
* undefined function "hook"
*
* @param string $name
* @param array $arguments
*/
public function __call($name , $arguments) {
$funcPrefix = $this->funcPrefix;
$properties = $this->properties;
if (
substr($name,0,strlen($funcPrefix))!==$funcPrefix
|| !in_array(($attr = substr($name, strlen($funcPrefix))), $properties)
) throw new BadMethodCallException(sprintf('Invalid Method: %s::%s.', __CLASS__, $name));
// get theme property value from option store
$defaults = $arguments[0];
$mods = get_option($this->optionName);
$key = 'special_background_'.$attr;
$defaults = !empty($mods[$key]) ? $mods[$key] : '';
return $defaults;
}
} // class
/**
* OptionSwapper Class
*
* Transparently hook into Backgrouind Picture
* Form Option processing Stream.
*/
class SpecialBackgroundOptionSwapper {
private $option;
/**
* @var SpecialBackgroundPlugin */
private $plugin;
public function __construct($option, SpecialBackgroundPlugin $plugin) {
$this->option = $option;
$this->plugin = $plugin;
add_filter('pre_update_option_'. $option, array($this, 'pre_update_option'), 10, 2);
}
public function pre_update_option($vnew, $vold) {
static $times = 0;
if (!empty($_POST) && $this->plugin->isAdminPage()) {
$key = null;
if ( (isset($_POST['action']) && 'save' === $_POST['action'])
||isset($_POST['reset-background'])
||isset($_POST['remove-background'])) {
$values = array(
'image',
'image_thumb',
);
!empty($values[$times]) && ($key = $values[$times]);
} else {
$values = array(
array('background-repeat', 'repeat'),
array('background-position-x','position_x'),
array('background-attachment','attachment'),
array('background-color','color'),
);
!empty($values[$times]) && ($value = $values[$times])
&& isset($_POST[$value[0]]) && ($key = $value[1]);
}
if (null !== $key) {
$vnew["special_background_{$key}"] = $vnew["background_{$key}"];
$vnew["background_{$key}"] = $vold["background_{$key}"];
}
}
$times++;
return $vnew;
}
} //class
#EOF;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment