PluginProbe
WP Display Header / 4
WP Display Header v4
9 8 trunk 2.0.0 2.0.1 2.1.0 2.2.0 3 4 5 6 7
wp-display-header / wp-display-header.php

wp-display-header.php in WP Display Header 4, at wp-display-header.php

622 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /** wp-display-header.php
3 *
4 * Plugin Name: WP Display Header
5 * Plugin URI: http://en.wp.obenland.it/wp-display-header/?utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-display-header
6 * Description: This plugin lets you specify a header image for each post and taxonomy/author archive page individually, from your default headers and custom headers.
7 * Version: 4
8 * Author: Konstantin Obenland
9 * Author URI: http://en.wp.obenland.it/?utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-display-header
10 * Text Domain: wp-display-header
11 * Domain Path: /lang
12 * License: GPLv2
13 */
14
15
16 if ( ! class_exists( 'Obenland_Wp_Plugins_v301' ) ) {
17 require_once 'obenland-wp-plugins.php';
18 }
19
20 register_activation_hook( __FILE__, array(
21 'Obenland_Wp_Display_Header',
22 'activation'
23 ) );
24
25
26 /**
27 * Class Obenland_Wp_Display_Header
28 */
29 class Obenland_Wp_Display_Header extends Obenland_Wp_Plugins_v301 {
30
31 ///////////////////////////////////////////////////////////////////////////
32 // METHODS, PUBLIC
33 ///////////////////////////////////////////////////////////////////////////
34
35 /**
36 * Constructor.
37 *
38 * @author Konstantin Obenland
39 * @since 1.0 - 23.03.2011
40 * @access public
41 *
42 * @return Obenland_Wp_Display_Header
43 */
44 public function __construct() {
45 parent::__construct( array(
46 'textdomain' => 'wp-display-header',
47 'plugin_path' => __FILE__,
48 'donate_link_id' => 'MWUA92KA2TL6Q',
49 ) );
50
51 load_plugin_textdomain( 'wp-display-header', false, 'wp-display-header/lang' );
52
53 $this->hook( 'init' );
54 }
55
56
57 /**
58 * Checks if the current theme supports custom header functionality and bails
59 * if it does not. The plugin will stay deactivated.
60 *
61 * @author Konstantin Obenland
62 * @since 1.0 - 23.03.2011
63 * @access public
64 * @static
65 */
66 public static function activation() {
67 load_plugin_textdomain( 'wp-display-header', false, 'wp-display-header/lang' );
68
69 if ( ! current_theme_supports( 'custom-header' ) ) {
70 wp_die( esc_html__( 'Your current theme does not support Custom Headers.', 'wp-display-header' ), '', array(
71 'back_link' => true,
72 ) );
73 }
74
75 if ( version_compare( get_bloginfo( 'version' ), '3.2', '<' ) ) {
76 wp_die( esc_html__( 'WP Display Headers requires WordPress version 3.2 or later.', 'wp-display-header' ), '', array(
77 'back_link' => true,
78 ) );
79 }
80 }
81
82
83 /**
84 * Hooks in all the hooks :)
85 *
86 * @author Konstantin Obenland
87 * @since 1.5.3 - 24.02.2012
88 * @access public
89 */
90 public function init() {
91
92 $this->hook( 'theme_mod_header_image' );
93 $this->hook( 'add_meta_boxes' );
94
95 // Save info.
96 $this->hook( 'save_post' );
97 $this->hook( 'edit_term' );
98 $this->hook( 'personal_options_update', 'update_user' );
99 $this->hook( 'edit_user_profile_update', 'update_user' );
100
101 // Styles
102 $this->hook( 'admin_init', 'register_scripts_styles', 9 ); // Set priority to 9, so they can easily be deregistered.
103 $this->hook( 'admin_enqueue_scripts', 'admin_print_styles' );
104
105 // Edit forms.
106 foreach ( get_taxonomies( array( 'show_ui' => true ) ) as $_tax ) {
107 $this->hook( "{$_tax}_edit_form", 'edit_form', 9 ); // Let's make us a bit more important than we are.
108 }
109
110 $this->hook( 'admin_init', 'add_settings_field' );
111 $this->hook( 'show_user_profile', 'edit_form' );
112 $this->hook( 'show_user_profile', 'edit_form' );
113 }
114
115
116 /**
117 * Returns the header url.
118 *
119 * Returns the default header when we are on the blog page, the header
120 * settings page or no specific header was defined for that post. Can be
121 * filtered!
122 *
123 * @author Konstantin Obenland
124 * @since 1.0 - 23.03.2011
125 * @access public
126 *
127 * @param string $header_url The header url as saved in the theme mods.
128 *
129 * @return string
130 */
131 public function theme_mod_header_image( $header_url ) {
132
133 if ( is_category() OR is_tag() OR is_tax() ) {
134 $active_header = $this->get_active_tax_header();
135 } else if ( is_author() ) {
136 $active_header = $this->get_active_author_header();
137 } else if ( is_singular() ) {
138 $active_header = $this->get_active_post_header();
139 }
140
141 if ( isset( $active_header ) AND $active_header ) {
142 $header_url = $active_header;
143 }
144
145 return $header_url;
146 }
147
148
149 /**
150 * Adds the header post meta box.
151 *
152 * @author Konstantin Obenland
153 * @since 1.0 - 23.03.2011
154 * @access public
155 *
156 * @param string $post_type
157 */
158 public function add_meta_boxes( $post_type ) {
159 add_meta_box( 'wp-display-header', esc_html__( 'Header' ), array(
160 $this,
161 'display_meta_box',
162 ), $post_type, 'normal', 'high' );
163 }
164
165
166 /**
167 * Registers the stylesheet.
168 *
169 * The stylesheets can easily be deregistered be calling
170 * <code>wp_deregister_style( 'wp-display-header' );</code> on the
171 * admin_init hook.
172 *
173 * @author Konstantin Obenland
174 * @since 1.5 - 22.01.2012
175 * @access public
176 */
177 public function register_scripts_styles() {
178 $plugin_data = get_plugin_data( __FILE__, false, false );
179 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
180
181 wp_register_style(
182 $this->textdomain,
183 plugins_url( "/css/{$this->textdomain}{$suffix}.css", __FILE__ ),
184 array(),
185 $plugin_data['Version']
186 );
187 }
188
189
190 /**
191 * Enqueues the CSS so the Header meta box looks nice :)
192 *
193 * @author Konstantin Obenland
194 * @since 1.0 - 23.03.2011
195 * @access public
196 *
197 * @param string $screen Name of the current admin screen.
198 */
199 public function admin_print_styles( $screen ) {
200 if ( in_array( $screen, array( 'post-new.php', 'post.php', 'term.php', 'edit-tags.php', 'profile.php', 'user-edit.php' ) ) ) {
201 wp_enqueue_style( $this->textdomain );
202 }
203 }
204
205
206 /**
207 * Registers the setting and the settings field if it does not already
208 * exist.
209 *
210 * @author Konstantin Obenland
211 * @since 1.0 - 22.01.2012
212 * @access public
213 */
214 public function add_settings_field() {
215 add_settings_section(
216 $this->textdomain,
217 esc_html__( 'Header' ),
218 array(
219 $this,
220 'settings_section_callback',
221 ),
222 $this->textdomain
223 );
224
225 add_settings_field(
226 $this->textdomain,
227 esc_html__( 'Choose Header', 'wp-display-header' ),
228 array(
229 $this,
230 'header_selection_callback',
231 ),
232 $this->textdomain,
233 $this->textdomain
234 );
235 }
236
237
238 /**
239 * Adds a settings section to the category edit screen.
240 *
241 * @author Konstantin Obenland
242 * @since 1.0 - 22.01.2012
243 * @access public
244 */
245 public function edit_form() {
246 do_settings_sections( $this->textdomain );
247 }
248
249
250 /**
251 * Renders the content of the post meta box.
252 *
253 * @author Konstantin Obenland
254 * @since 1.0 - 22.01.2012
255 * @access public
256 *
257 * @param WP_Post $post
258 */
259 public function display_meta_box( $post ) {
260 $active = $this->get_active_post_header( $post->ID, true );
261 $this->header_selection_form( $active );
262 }
263
264
265 /**
266 * Echos out a description at the top of the section (between heading and
267 * fields).
268 *
269 * @author Konstantin Obenland
270 * @since 1.0 - 22.01.2012
271 * @access public
272 */
273 public function settings_section_callback() {
274 if ( 'profile' == get_current_screen()->base ) {
275 esc_html_e( 'Select a header image for the author page.', 'wp-display-header' );
276 } elseif ( in_array( get_current_screen()->base, array( 'edit-tags', 'term' ) ) ) {
277 esc_html_e( 'Select a header image for the taxonomy archive page.', 'wp-display-header' );
278 }
279 }
280
281
282 /**
283 * Displays the settings field HTML.
284 *
285 * @author Konstantin Obenland
286 * @since 1.0 - 22.01.2012
287 * @access public
288 */
289 public function header_selection_callback() {
290 $active = '';
291
292 switch ( get_current_screen()->base ) {
293 case 'profile':
294 $active = get_user_meta( $GLOBALS['profileuser']->ID, $this->textdomain, true );
295 break;
296
297 case 'edit-tags':
298 case 'term':
299 global $tag;
300 if ( $active = get_option( 'wpdh_tax_meta', '' ) ) {
301 $active = isset( $active[ $tag->term_taxonomy_id ] ) ? $active[ $tag->term_taxonomy_id ] : '';
302 }
303 break;
304 }
305
306 // If no header set yet, get default header.
307 if ( ! $active ) {
308 $active = get_theme_mod( 'header_image' );
309 }
310
311 $this->header_selection_form( $active );
312 }
313
314
315 /**
316 * Saves the selected header for this post.
317 *
318 * @author Konstantin Obenland
319 * @since 1.0 - 23.03.2011
320 * @access public
321 *
322 * @param int $post_ID
323 *
324 * @return int Post ID
325 */
326 public function save_post( $post_ID ) {
327 if ( ( ! defined( 'DOING_AUTOSAVE' ) OR ! DOING_AUTOSAVE ) AND
328 isset( $_POST[ $this->textdomain ] ) AND
329 wp_verify_nonce( $_POST["{$this->textdomain}-nonce"], $this->textdomain )
330 ) {
331
332 if ( isset( $_POST['wpdh-reset-header'] ) ) {
333 delete_post_meta( $post_ID, '_wpdh_display_header' );
334
335 } else {
336 $value = in_array( $_POST[ $this->textdomain ], array(
337 'random',
338 'remove-header',
339 ) ) ? $_POST[ $this->textdomain ] : esc_url_raw( $_POST[ $this->textdomain ] );
340 update_post_meta( $post_ID, '_wpdh_display_header', $value );
341 }
342 }
343
344 return $post_ID;
345 }
346
347
348 /**
349 * Sanitizes the settings field input.
350 *
351 * @author Konstantin Obenland
352 * @since 2.0.0 - 12.03.2012
353 * @access public
354 *
355 * @param int $term_id
356 * @param string $tt_id
357 *
358 * @return int Term ID
359 */
360 public function edit_term( $term_id, $tt_id ) {
361 if ( ( ! defined( 'DOING_AUTOSAVE' ) OR ! DOING_AUTOSAVE ) AND
362 isset( $_POST[ $this->textdomain ] ) AND
363 wp_verify_nonce( $_POST["{$this->textdomain}-nonce"], $this->textdomain )
364 ) {
365 $term_meta = get_option( 'wpdh_tax_meta', array() );
366
367 if ( isset( $_POST['wpdh-reset-header'] ) ) {
368 unset( $term_meta[ $tt_id ] );
369 } else {
370 $term_meta[ $tt_id ] = in_array( $_POST[ $this->textdomain ], array(
371 'random',
372 'remove-header',
373 ) ) ? $_POST[ $this->textdomain ] : esc_url_raw( $_POST[ $this->textdomain ] );
374 }
375
376 update_option( 'wpdh_tax_meta', $term_meta );
377 }
378
379 return $term_id;
380 }
381
382
383 /**
384 * Sanitizes the settings field input.
385 *
386 * @author Konstantin Obenland
387 * @since 2.0.0 - 12.03.2012
388 * @access public
389 *
390 * @param int $user_id
391 *
392 * @return int User ID
393 */
394 public function update_user( $user_id ) {
395 if ( ( ! defined( 'DOING_AUTOSAVE' ) OR ! DOING_AUTOSAVE ) AND
396 isset( $_POST[ $this->textdomain ] ) AND
397 wp_verify_nonce( $_POST["{$this->textdomain}-nonce"], $this->textdomain )
398 ) {
399
400 if ( isset( $_POST['wpdh-reset-header'] ) ) {
401 delete_user_meta( $user_id, $this->textdomain );
402 } else {
403 $value = in_array( $_POST[ $this->textdomain ], array(
404 'random',
405 'remove-header',
406 ) ) ? $_POST[ $this->textdomain ] : esc_url_raw( $_POST[ $this->textdomain ] );
407 update_user_meta( $user_id, $this->textdomain, $value );
408 }
409 }
410
411 return $user_id;
412 }
413
414
415 ///////////////////////////////////////////////////////////////////////////
416 // METHODS, PROTECTED
417 ///////////////////////////////////////////////////////////////////////////
418
419 /**
420 * Displays the settings field HTML.
421 *
422 * @author Konstantin Obenland
423 * @since 1.0 - 22.01.2012
424 * @access protected
425 *
426 * @param string $active
427 */
428 protected function header_selection_form( $active = '' ) {
429 $headers = $this->get_headers();
430
431 if ( empty( $headers ) ) {
432 printf(
433 __( 'The are no headers available. Please <a href="%s">upload a header image</a>!', 'wp-display-header' ),
434 add_query_arg( array( 'page' => 'custom-header' ), admin_url( 'themes.php' ) )
435 );
436
437 return;
438 }
439
440 foreach ( array_keys( $headers ) as $header ) {
441 foreach ( array( 'url', 'thumbnail_url' ) as $url ) {
442 $headers[ $header ][ $url ] = sprintf(
443 $headers[ $header ][ $url ],
444 get_template_directory_uri(),
445 get_stylesheet_directory_uri()
446 );
447 }
448 }
449
450 wp_nonce_field( 'wp-display-header', 'wp-display-header-nonce' );
451 ?>
452 <div class="available-headers">
453 <div class="random-header">
454 <p>
455 <label>
456 <input name="wp-display-header" type="radio" value="random" <?php checked( 'random', $active ); ?> />
457 <?php _e( '<strong>Random:</strong> Show a different image on each page.', 'wp-display-header' ); ?>
458 </label>
459 </p>
460 <p>
461 <label>
462 <input name="wp-display-header" type="radio" value="remove-header" <?php checked( 'remove-header', $active ); ?> />
463 <?php _e( '<strong>None:</strong> Show no header image.', 'wp-display-header' ); ?>
464 </label>
465 </p>
466 </div>
467 <?php
468 foreach ( $headers as $header_key => $header ) :
469 $header_url = $header['url'];
470 $header_thumbnail = $header['thumbnail_url'];
471 $header_desc = isset( $header['description'] ) ? $header['description'] : '';
472 ?>
473 <div class="default-header">
474 <label>
475 <input name="wp-display-header" type="radio" value="<?php echo esc_attr( $header_url ); ?>" <?php checked( $header_url, $active ); ?> />
476 <img width="230" src="<?php echo esc_url( $header_thumbnail ); ?>" alt="<?php echo esc_attr( $header_desc ); ?>" title="<?php echo esc_attr( $header_desc ); ?>"/>
477 </label>
478 </div>
479 <?php endforeach; ?>
480 <div class="clear"></div>
481
482 <?php submit_button( esc_html__( 'Restore Original Header Image', 'wp-display-header' ), 'button', 'wpdh-reset-header', false ); ?>
483 <span class="description"><?php esc_html_e( 'This will restore the original header image. You will not be able to restore any customizations.', 'wp-display-header' ) ?></span>
484 </div>
485 <?php
486 }
487
488
489 /**
490 * Returns all registered headers.
491 *
492 * If there are uploaded headers via the WP Save Custom Header Plugin, they
493 * will be loaded, too.
494 *
495 * @author Konstantin Obenland
496 * @since 1.0 - 23.03.2011
497 * @access public
498 * @global $_wp_default_headers
499 *
500 * @return array
501 */
502 protected function get_headers() {
503 global $_wp_default_headers;
504 $headers = array_merge( (array) $_wp_default_headers, get_uploaded_header_images() );
505
506 return (array) apply_filters( 'wpdh_get_headers', $headers );
507 }
508
509
510 /**
511 * Determines the active header for the post and returns the url.
512 *
513 * The $raw variable is necessary so that the 'random' option stays
514 * selected in post edit screens.
515 *
516 * @author Konstantin Obenland
517 * @since 2.0.0 - 12.03.2012
518 * @access protected
519 *
520 * @param int $post_ID
521 * @param boolean $raw
522 *
523 * @return string
524 */
525 protected function get_active_post_header( $post_ID = 0, $raw = false ) {
526 if ( ! $post_ID ) {
527 $post_ID = get_post()->ID;
528 }
529
530 $active = get_post_meta( $post_ID, '_wpdh_display_header', true );
531
532 return apply_filters( 'wpdh_get_active_post_header', $this->get_active_header( $active, $raw ) );
533 }
534
535
536 /**
537 * Determines the active header for the category and returns the url.
538 *
539 * The $raw variable is necessary so that the 'random' option stays
540 * selected in post edit screens.
541 *
542 * @author Konstantin Obenland
543 * @since 2.0.0 - 12.03.2012
544 * @access protected
545 *
546 * @return string
547 */
548 protected function get_active_tax_header() {
549 if ( $active = get_option( 'wpdh_tax_meta', false ) ) {
550 $tt_id = get_queried_object()->term_taxonomy_id;
551 $active = isset( $active[ $tt_id ] ) ? $active[ $tt_id ] : '';
552 }
553
554 return apply_filters( 'wpdh_get_active_tax_header', $this->get_active_header( $active ) );
555 }
556
557
558 /**
559 * Determines the active header for the author and returns the url.
560 *
561 * The $raw variable is necessary so that the 'random' option stays
562 * selected in post edit screens.
563 *
564 * @author Konstantin Obenland
565 * @since 2.0.0 - 12.03.2012
566 * @access protected
567 *
568 * @return string
569 */
570 protected function get_active_author_header() {
571 $active = get_user_meta( get_queried_object()->ID, $this->textdomain, true );
572
573 return apply_filters( 'wpdh_get_active_author_header', $this->get_active_header( $active ) );
574 }
575
576
577 /**
578 * Determines the active header for the post and returns the url.
579 *
580 * The $raw variable is necessary so that the 'random' option stays
581 * selected in post edit screens.
582 *
583 * @author Konstantin Obenland
584 * @since 1.0 - 23.03.2011
585 * @access public
586 *
587 * @param string $header Header URL
588 * @param boolean $raw
589 *
590 * @return string
591 */
592 protected function get_active_header( $header, $raw = false ) {
593 if ( 'random' == $header AND ! $raw ) {
594 $headers = $this->get_headers();
595 $header = sprintf(
596 $headers[ array_rand( $headers ) ]['url'],
597 get_template_directory_uri(),
598 get_stylesheet_directory_uri()
599 );
600 }
601
602 return apply_filters( 'wpdh_get_active_header', $header );
603 }
604 } // End of class Obenland_Wp_Display_Header.
605
606
607 /**
608 * Instantiates the class if current theme supports Custom Headers.
609 *
610 * @author Konstantin Obenland
611 * @since 1.2 - 03.05.2011
612 */
613 function Obenland_wpdh_instantiate() {
614 if ( current_theme_supports( 'custom-header' ) ) {
615 new Obenland_Wp_Display_Header;
616 }
617 }
618 add_action( 'init', 'Obenland_wpdh_instantiate', 1 );
619
620
621 /* End of file wp-display-header.php */
622 /* Location: ./wp-content/plugins/wp-display-header/wp-display-header.php */