PluginProbe
WP Display Header / 3
WP Display Header v3
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 3, at wp-display-header.php

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