Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 313 lines (266 sloc) 10.184 kb
e3fb7cf2 »
2012-09-03 Loads of fun, new stuff. Plus, random backgrounds.
1 <?php
2 /**
3 * Random Custom Background - A script for handling random backgrounds.
4 *
5 * This script was created to make it simple for theme developers to set a random background for
6 * their theme instead of just a single background. The script is just an extension of the WordPress
7 * 'custom-background' theme feature. It allows the user to select a permanent background, but if
8 * no user background is set, the random background is shown.
9 *
10 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License as published by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
15 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * @package RandomCustomBackground
18 * @version 0.1.0 - Alpha
19 * @author Justin Tadlock <justin@justintadlock.com>
20 * @copyright Copyright (c) 2012, Justin Tadlock
21 * @link http://justintadlock.com
22 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23 */
24
25 /**
26 * Generates a random custom background and filters the 'theme_mod_background_* hooks to
27 * overwrite the theme's set background.
28 *
29 * @todo Sanitize. Sanitize. Sanitize.
30 *
31 * @since 0.1.0
32 */
33 class Random_Custom_Background {
34
35 /**
36 * The background color property.
37 *
38 * @since 0.1.0
39 * @access public
40 * @var string
41 */
42 public $color = '';
43
44 /**
45 * The background image property.
46 *
47 * @since 0.1.0
48 * @access public
49 * @var string
50 */
51 public $image = '';
52
53 /**
54 * The background repeat property. Allowed: 'no-repeat', 'repeat', 'repeat-x', 'repeat-y'.
55 *
56 * @since 0.1.0
57 * @access public
58 * @var string
59 */
60 public $repeat = 'repeat';
61
62 /**
63 * The vertical value of the background position property. Allowed: 'top', 'bottom', 'center'.
64 *
65 * @since 0.1.0
66 * @access public
67 * @var string
68 */
69 public $position_y = 'top';
70
71 /**
72 * The horizontal value of the background position property. Allowed: 'left', 'right', 'center'.
73 *
74 * @since 0.1.0
75 * @access public
76 * @var string
77 */
78 public $position_x = 'left';
79
80 /**
81 * The background attachment property. Allowed: 'scroll', 'fixed'.
82 *
83 * @since 0.1.0
84 * @access public
85 * @var string
86 */
87 public $attachment = 'scroll';
88
89 /**
90 * Constructor method. Sets up the random background feature.
91 *
92 * @since 0.1.0
93 * @access public
94 * @return void
95 */
96 public function __construct() {
97
98 /* The theme should actually support the custom background feature. */
99 if ( !current_theme_supports( 'custom-background' ) )
100 add_theme_support( 'custom-background', array( 'wp-head-callback' => '__return_false' ) );
101
102 /* Generate a random background. */
103 $this->generate_random_background();
104
105 /* Get the user-selected background image. */
106 $image = get_theme_mod( 'background_image' );
107
108 /* Filter the background color. */
109 add_filter( 'theme_mod_background_color', array( &$this, 'background_color' ) );
110
111 /* Filter the background image. */
112 add_filter( 'theme_mod_background_image', array( &$this, 'background_image' ) );
113
114 /**
115 * If no background image is set by the user, set the properties related to the background
116 * image. The script must overwrite these values completely if we're adding a random
117 * background because the user can't clear these values.
118 */
119 if ( empty( $image ) && !empty( $this->image ) ) {
120 add_filter( 'theme_mod_background_repeat', array( &$this, 'background_repeat' ) );
121 add_filter( 'theme_mod_background_attachment', array( &$this, 'background_attachment' ) );
122 add_filter( 'theme_mod_background_position_x', array( &$this, 'background_position_x' ) );
123 add_filter( 'theme_mod_background_position_y', array( &$this, 'background_position_y' ) );
124 }
125
126 /* Get the custom background arguments. */
127 $supports = get_theme_support( 'custom-background' );
128
129 /* If '__return_false' is the wp_head callback, roll our own. */
130 if ( '__return_false' == $supports[0]['wp-head-callback'] )
131 add_action( 'wp_head', array( &$this, 'custom_background_callback' ) );
132 }
133
134 /**
135 * Generates a random background image from the theme's random images set. Themes should
136 * add a second parameter to register their backgrounds (an array of background arrays).
137 * add_theme_support( 'random-custom-background', $backgrounds ).
138 *
139 * Supported background arguments: 'image', 'color', 'repeat', 'position_x', 'position_y', 'attachment'.
140 *
141 * @since 0.1.0
142 * @access public
143 * @return void
144 */
145 public function generate_random_background() {
146
147 /* Get the theme-supported random background array. */
148 $supports = get_theme_support( 'random-custom-background' );
149
150 /* If no backgrounds are set, return. */
151 if ( !isset( $supports[0] ) || !is_array( $supports[0] ) )
152 return;
153
154 /* Set the backgrounds to the $backgrounds variable. */
155 $backgrounds = $supports[0];
156
157 /* Generate a random background from the given set of backgrounds. */
158 srand( (double) microtime() * 1000000 );
159 $random = rand( 0, count( $backgrounds ) - 1 );
160 $args = $backgrounds[$random];
161
162 /* Set the background properties. */
163 $this->image = !empty( $args['image'] ) ? $args['image'] : $this->image;
164 $this->color = !empty( $args['color'] ) ? $args['color'] : $this->color;
165 $this->repeat = !empty( $args['repeat'] ) ? $args['repeat'] : $this->repeat;
166 $this->position_y = !empty( $args['position_y'] ) ? $args['position_y'] : $this->position_y;
167 $this->position_x = !empty( $args['position_x'] ) ? $args['position_x'] : $this->position_x;
168 $this->attachment = !empty( $args['attachment'] ) ? $args['attachment'] : $this->attachment;
169 }
170
171 /**
172 * Sets the background color. Right now, we must respect the user's color setting because
173 * there's no way for the user to remove it. Thus, there's no way for the script to know
174 * whether the user intends to use their custom background color or the randomly-
175 * generated color.
176 *
177 * @todo Update script once users are allowed to remove background color.
178 * @link http://core.trac.wordpress.org/ticket/21059
179 *
180 * @since 0.1.0
181 * @access public
182 * @param string $color The background color property.
183 * @return string
184 */
185 public function background_color( $color ) {
186
187 /* Only return random color if the user hasn't chosen a color. */
188 return empty( $color ) ? $this->color : $color;
189 }
190
191 /**
192 * Sets the background image URL.
193 *
194 * @since 0.1.0
195 * @access public
196 * @param string $image The background image property.
197 * @return string
198 */
199 public function background_image( $image ) {
200
201 /* Only return the random image if the user hasn't chosen an image. */
202 return empty( $image ) ? $this->image : $image;
203 }
204
205 /**
206 * Sets the background repeat property. Only exectued if using a random background.
207 *
208 * @since 0.1.0
209 * @access public
210 * @param string $repeat The background repeat property.
211 * @return string
212 */
213 public function background_repeat( $repeat ) {
214 return $this->repeat;
215 }
216
217 /**
218 * Sets the background vertical position. This isn't technically supported in WordPress (as of 3.5).
219 * This method is only executed if using a random background and the custom_background_callback()
220 * method is executed (themes can also use it in custom callbacks).
221 *
222 * @since 0.1.0
223 * @access public
224 * @param string $position_y The background vertical position.
225 * @return string
226 */
227 public function background_position_y( $position_y ) {
228 return $this->position_y;
229 }
230
231 /**
232 * Sets the background horizontal position. Only exectued if using a random background.
233 *
234 * @since 0.1.0
235 * @access public
236 * @param string $position_x The background horizontal position.
237 * @return string
238 */
239 public function background_position_x( $position_x ) {
240 return $this->position_x;
241 }
242
243 /**
244 * Sets the background attachment property. Only exectued if using a random background.
245 *
246 * @since 0.1.0
247 * @access public
248 * @param string $url The background attachment property.
249 * @return string
250 */
251 public function background_attachment( $attachment ) {
252 return $this->attachment;
253 }
254
255 /**
256 * Outputs the custom background style in the header. This function is only executed if the value
257 * of the 'wp-head-callback' for the 'custom-background' feature is set to '__return_false'.
258 *
259 * @since 0.1.0
260 * @access public
261 * @return void
262 */
263 public function custom_background_callback() {
264
265 /* Get the background image. */
266 $image = set_url_scheme( get_background_image() );
267
268 /* Get the background color. */
269 $color = get_background_color();
270
271 /* If there is no image or color, bail. */
272 if ( empty( $image ) && empty( $color ) )
273 return;
274
275 /* Set the background color. */
276 $style = $color ? "background-color: #{$color};" : '';
277
278 /* If there's a background image, add it. */
279 if ( $image ) {
280
281 /* Background image. */
282 $style .= " background-image: url('{$image}');";
283
284 /* Background repeat. */
285 $repeat = get_theme_mod( 'background_repeat', 'repeat' );
286 $repeat = in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) ? $repeat : 'repeat';
287
288 $style .= " background-repeat: {$repeat};";
289
290 /* Background position. */
291 $position_y = get_theme_mod( 'background_position_y', 'top' );
292 $position_y = in_array( $position_y, array( 'top', 'center', 'bottom' ) ) ? $position_y : 'top';
293
294 $position_x = get_theme_mod( 'background_position_x', 'left' );
295 $position_x = in_array( $position_x, array( 'center', 'right', 'left' ) ) ? $position_x : 'left';
296
297 $style .= " background-position: {$position_y} {$position_x};";
298
299 /* Background attachment. */
300 $attachment = get_theme_mod( 'background_attachment', 'scroll' );
301 $attachment = in_array( $attachment, array( 'fixed', 'scroll' ) ) ? $attachment : 'scroll';
302
303 $style .= " background-attachment: $attachment;";
304 }
305
306 /* Output the custom background style. */
307 echo "\n" . '<style type="text/css" id="custom-background-css">body.custom-background{ ' . trim( $style ) . '; }</style>' . "\n";
308 }
309 }
310
311 new Random_Custom_Background();
312
313 ?>
Something went wrong with that request. Please try again.