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

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