forked from jeherve/color-posts
/
functions.jeherve-get-color.php
154 lines (130 loc) · 3.82 KB
/
functions.jeherve-get-color.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Add Theme support for Tonesque
*
* @uses add_theme_support()
*
* @since 1.0
*/
function colorposts_call_tonesque() {
add_theme_support( 'tonesque' );
}
add_action( 'after_setup_theme', 'colorposts_call_tonesque', 9 );
/**
* Get an image from a post
*
* @uses Jetpack_PostImages::get_image( $post_id ) to get the source of an image in a post, apply_filters()
*
* @since 1.0
*
* @return string $the_image the image source
*/
function colorposts_get_post_image() {
$post_id = get_the_ID();
if ( class_exists( 'Jetpack_PostImages' ) ) {
$the_image = Jetpack_PostImages::get_image( $post_id );
if ( ! empty( $the_image['src'] ) ) {
$the_image = $the_image['src'];
} else {
$the_image = apply_filters( 'jetpack_open_graph_image_default', "http://wordpress.com/i/blank.jpg" );
}
}
$the_image = apply_filters( 'colorposts_image_output', $the_image );
return esc_url( $the_image );
}
/**
* Build CSS from Tonesque
*
* @uses get_the_ID(), is_single(), get_post_meta(), colorposts_get_post_image(), update_post_meta(), apply_filters()
*
* @since 1.0
*/
function colorposts_build_css() {
$post_id = get_the_ID();
// Only add color if a single page is displayed
if ( is_singular() ) {
// Grab color from post meta
$tonesque = get_post_meta( $post_id, '_post_colors', true );
// No color? Let's get one
if ( empty( $tonesque ) ) {
$post_image = colorposts_get_post_image( $post_id );
$tonesque = new Tonesque( $post_image );
$tonesque = array(
'color' => $tonesque->color(),
'contrast' => $tonesque->contrast(),
);
if ( $tonesque['color'] ) {
update_post_meta( $post_id, '_post_colors', $tonesque );
} else {
return;
}
}
// Add the CSS to our page
extract( $tonesque );
if ( empty( $color ) || empty( $contrast ) ) {
return;
} else {
$custom_css = "\n<style type='text/css'>\n";
$colors_css = "body { background: #{$color} !important; }";
/**
* Filters the CSS inserted in the head.
*
* @since 1.1
*
* @param string $colors_css CSS code.
* @param string $color HEX color code, without the hashtag.
* @param string $contrast Contrast matching the post. Either black or white. Defined in RGB.
*/
$custom_css .= apply_filters( 'colorposts_css_output', $colors_css, $color, $contrast );
$custom_css .= "\n</style>\n";
/**
* Filters the complete CSS output, including the style tags.
*
* @since 1.2
*
* @param string $custom_css Custom CSS output.
* @param string $color HEX color code, without the hashtag.
* @param string $contrast Contrast matching the post. Either black or white. Defined in RGB.
*/
echo apply_filters( 'colorposts_css_tag', $custom_css, $color, $contrast );
}
} // End is_single()
}
add_action( 'wp_head', 'colorposts_build_css' );
/**
* Add a theme-color meta tag to the head
*
* This meta tag allows Android to change the color of the address bar.
* This will now match the post color.
*
* @since 1.2
*
* @return string $custom_css Custom CSS output and our extra color tag if the color is set.
*/
function colorposts_color_meta_tag( $custom_css, $color, $contrast ) {
// Bail early if no color is set.
if ( ! $color || empty( $color ) ) {
return $custom_css;
}
// Create our tag.
$color_tag = sprintf(
'<meta name="theme-color" content="#%1$s" />%2$s',
$color,
"\n"
);
// Add the tag below the Custom CSS
return $custom_css . $color_tag;
}
add_filter( 'colorposts_css_tag', 'colorposts_color_meta_tag', 10, 3 );
/**
* Flush out the post meta used in colorposts_build_css().
*
* @uses delete_post_meta()
* @param int $post_id The ID of the saved post.
*
* @since 1.0
*/
function colorposts_post_meta_flusher( $post_id ) {
delete_post_meta( $post_id, '_post_colors' );
}
add_action( 'save_post', 'colorposts_post_meta_flusher' );