public
Last active — forked from mikeschinkel/special-background.php

  • Download Gist
special-background.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
<?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;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.