-
Notifications
You must be signed in to change notification settings - Fork 8
/
just-rwd-functions.php
115 lines (101 loc) · 4.22 KB
/
just-rwd-functions.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
<?php
/**
* Template functions to simplify using <picture> tags and dynamic mobile-friendly backgrounds.
*/
use jri\models\RwdImage;
/**
* Displays <picture> or <img> for post attachment image specifying correct responsive rules,
* which should be set through 'rwd_image_sizes' filter hook.
*
* @param WP_Post|int|null $attachment WordPress attachment object, ID or null. If null passed will take featured image of current post.
* @param string|array $size {
* Single image size name OR array
*
* @type int 0 => $size (first element should be the name of image size),
* @type string $subsize => $attachment_id ($attachment to be used to rewrite image in another resolution)
* }
* Image size name or array with custom attachment IDs for specific rwd sizes.
*
* @param string $tag Specify which tag should be used: picture|img.
* @param array $attr Additional html attributes to be used for main tag.
*/
function rwd_attachment_image( $attachment = null, $size = 'thumbnail', $tag = null, $attr = array() ) {
if( empty( $tag ) ) {
$tag = apply_filters( 'rwd_tag_type', 'picture' );
}
echo get_rwd_attachment_image( $attachment, $size, $tag, $attr );
}
/**
* Generate <picture> or <img> html for post attachment image specifying correct responsive rules,
* which should be set through 'rwd_image_sizes' filter hook.
*
* @param WP_Post|int|null $attachment WordPress attachment object, ID or null. If null passed will take featured image of current post.
* @param string|array $size {
* Single image size name OR array
*
* @type int 0 => $size (first element should be the name of image size),
* @type string $subsize => $attachment_id ($attachment to be used to rewrite image in another resolution)
* }
* @param string $tag Specify which tag should be used: picture|img.
* @param array $attr Additional html attributes to be used for main tag.
*
* @return string Generated html.
*/
function get_rwd_attachment_image( $attachment = null, $size = 'thumbnail', $tag = null, $attr = array() ) {
$rwd_image = new RwdImage( $attachment );
if( empty( $tag ) ) {
$tag = apply_filters( 'rwd_tag_type', 'picture' );
}
$size = apply_filters( 'post_thumbnail_size', $size );
if ( 'img' != $tag ) {
$html = $rwd_image->picture( $size, $attr );
} else {
$html = $rwd_image->img( $size, $attr );
}
return $html;
}
/**
* Generate css media styles for background image for specific css selector and specific rwd sizes,
* which should be set through 'rwd_image_sizes' filter hook.
*
* Generated styles are add to cache and then print them in wp_foot or by calling rwd_print_css();
*
* @param string $selector Dynamic css selector
* @param WP_Post|int|null $attachment WordPress attachment object, ID or null. If null passed will take featured image of current post.
* @param string|array $size {
* Single image size name OR array
*
* @type int 0 => $size (first element should be the name of image size),
* @type string $subsize => $attachment_id ($attachment to be used to rewrite image in another resolution)
* }
*/
function rwd_attachment_background( $selector, $attachment = null, $size = 'thumbnail' ) {
$rwd_image = new RwdImage( $attachment );
echo $rwd_image->background( $selector, $size );
}
/**
* Print styles from global cache
*/
function rwd_print_styles() {
global $rwd_background_styles;
if ( empty( $rwd_background_styles ) || ! is_array( $rwd_background_styles ) ) {
return;
}
$styles = '';
$primary_media = RwdImage::get_background_primary_sizes();
// merge with media keys to get correct sorting.
$ordered_styles = array_merge( array_flip( $primary_media ), $rwd_background_styles );
foreach ( $ordered_styles as $media => $selectors ) {
if ( empty( $selectors ) || ! is_array( $selectors ) ) {
continue;
}
$media_css = implode( ' ', $selectors );
if ( '' === $media ) {
$styles .= " $media_css ";
} else {
$styles .= " $media{ $media_css } ";
}
}
print "<style type=\"text/css\">/* rwd-background-styles */ {$styles}</style> \n";
$rwd_background_styles = array();
}