-
Notifications
You must be signed in to change notification settings - Fork 0
/
backgroundimage.php
222 lines (207 loc) · 6.95 KB
/
backgroundimage.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* @package Custom
* @author Sébastien Chandonay www.seb-c.com / Cyril Tissot www.cyriltissot.com
* License: GPL2
* Text Domain: custom
*
* Copyright 2016 Sébastien Chandonay (email : please contact me from my website)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
defined('ABSPATH') or die("Go Away!");
/**
* REQUIREMENTS
*/
require_once (CUSTOM_PLUGIN_PATH.'/'.CUSTOM_PLUGIN_TOOLS_FOLDER.BACKGROUNDIMAGE_TOOL_NAME.'/custom-fields/backgroundimage.php');
require_once (CUSTOM_PLUGIN_PATH.'/'.CUSTOM_PLUGIN_TOOLS_FOLDER.BACKGROUNDIMAGE_TOOL_NAME.'/inc/customizer.php');
if (!function_exists("backgroundimage_get_url")):
/**
* retrieve backgroundimage url
* @param string $id_post - current post if null
*/
function backgroundimage_get_url($id_post = null){
$url_backgroundimage = "";
if (empty($id_post) || !is_numeric($id_post)){
$_queried_post = get_queried_object();
if (is_home()){ // blog page
$id_post = get_option('page_for_posts');
}else if (!is_404() && $_queried_post){
$id_post = $_queried_post->ID;
}
}
if ($id_post && (is_single() || is_page() || is_home())){
$url_backgroundimage = get_post_meta($id_post, BACKGROUNDIMAGE_URL, true);
}
if (empty($url_backgroundimage)){
$url_backgroundimage = get_theme_mod('backgroundimage_image');
}
return $url_backgroundimage;
}
endif;
if (!function_exists("backgroundimage_is_default")):
/**
* retrieve backgroundimage url
* @param string $id_post - current post if null
*/
function backgroundimage_is_default($id_post = null){
$default = true;
if (empty($id_post) || !is_numeric($id_post)){
$_queried_post = get_queried_object();
if (is_home()){ // blog page
$id_post = get_option('page_for_posts');
}else if (!is_404() && $_queried_post){
$id_post = $_queried_post->ID;
}
}
// background image
if ($id_post && (is_single() || is_page() || is_home())){
$post_meta = get_post_meta($id_post, BACKGROUNDIMAGE_URL, true);
if (!empty($post_meta)){
$default = false;
}
}
return $default;
}
endif;
if (!function_exists("backgroundimage_get_color")):
/**
* retrieve backgroundimage color
* @param string $id_post - current post if null
*/
function backgroundimage_get_color($id_post = null){
$background_color_code = "";
if (empty($id_post) || !is_numeric($id_post)){
$_queried_post = get_queried_object();
if (is_home()){ // blog page
$id_post = get_option('page_for_posts');
}else if (!is_404() && $_queried_post){
$id_post = $_queried_post->ID;
}
}
if ($id_post && (is_single() || is_page() || is_home())){
$background_color_code = get_post_meta($id_post, BACKGROUNDCOLOR_CODE, true);
if (!empty($background_color_code)){
$background_color_code = hex_to_rgb($background_color_code, true);
}
}
return $background_color_code;
}
endif;
if (!function_exists("backgroundimage_get_color_opacity")):
/**
* retrieve backgroundimage color's opacity
* @param string $id_post - current post if null
*/
function backgroundimage_get_color_opacity($id_post = null){
$background_color_opacity = "";
if (empty($id_post) || !is_numeric($id_post)){
$_queried_post = get_queried_object();
if (is_home()){ // blog page
$id_post = get_option('page_for_posts');
}else if (!is_404() && $_queried_post){
$id_post = $_queried_post->ID;
}
}
if ($id_post && (is_single() || is_page() || is_home())){
$background_color_opacity = get_post_meta($id_post, BACKGROUNDCOLOR_OPACITY, true);
}
if (empty($background_color_opacity))
$background_color_opacity = 0;
if ($background_color_opacity == 100){
$background_color_opacity = "1";
}else{
$background_color_opacity = "0.".$background_color_opacity;
}
return $background_color_opacity;
}
endif;
if (!function_exists("custom_backgroundimage")):
/**
* Display background image/color/opacity
* @param boolean $auto_insert : boolean - do not specify true if you use this method manualy in your template theme
*/
function custom_backgroundimage($auto_insert = false){
$id_post = null;
if (is_home()){ // blog page
$id_post = get_option('page_for_posts');
}else if (!is_404()){
$_queried_post = get_queried_object();
if ($_queried_post)
$id_post = $_queried_post->ID;
}
// background image
$url_backgroundimage = backgroundimage_get_url($id_post);
// background color
$background_color_code = backgroundimage_get_color($id_post);
// background color opacity
$background_color_opacity = backgroundimage_get_color_opacity($id_post);
// default ?
$class = "";
if (backgroundimage_is_default($id_post))
$class = "default";
if (!empty($url_backgroundimage)){
$variable_styles = "";
if ($auto_insert)
$variable_styles = " position: fixed; z-index: -100;";
else
$variable_styles = " position: absolute;";
?>
<div id="tool-backgroundimage" class="<?php echo $class; ?>" style="background: url('<?php echo $url_backgroundimage; ?>') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
-ms-background-size: cover;
background-size: cover;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
<?php echo $variable_styles; ?>">
<?php
if (!empty($background_color_code)){
?>
<div id="tool-backgroundimage-color"
style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(<?php echo $background_color_code; ?>, <?php echo $background_color_opacity; ?>);"></div>
<?php } ?>
</div>
<?php
}else if (!empty($background_color_code)){
$variable_styles = "";
if ($auto_insert)
$variable_styles = " position: fixed; z-index: -100;";
else
$variable_styles = " position: absolute;";
?>
<div id="tool-backgroundimage-color"
style="top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(<?php echo $background_color_code; ?>, <?php echo $background_color_opacity; ?>);<?php echo $variable_styles; ?>"></div>
<?php
}
}
endif;
if (!function_exists("custom_backgroundimage_autoinsert")):
/**
* Display background image/color/opacity
* @param boolean $auto_insert : boolean - do not specify true if you use this method manualy in your template theme
*/
function custom_backgroundimage_autoinsert($auto_insert = false){
$backgroundimage_autoinsert = custom_get_option('tool-backgroundimage-auto-insert');
if (!empty($backgroundimage_autoinsert) && $backgroundimage_autoinsert == 'on'){
custom_backgroundimage(true);
}
}
add_action('wp_footer', 'custom_backgroundimage_autoinsert');
endif;