forked from woocommerce/storefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorefront-functions.php
314 lines (264 loc) · 8.32 KB
/
storefront-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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* Storefront functions.
*
* @package storefront
*/
if ( ! function_exists( 'storefront_is_woocommerce_activated' ) ) {
/**
* Query WooCommerce activation
*/
function storefront_is_woocommerce_activated() {
return class_exists( 'WooCommerce' ) ? true : false;
}
}
/**
* Checks if the current page is a product archive
* @return boolean
*/
function storefront_is_product_archive() {
if ( storefront_is_woocommerce_activated() ) {
if ( is_shop() || is_product_taxonomy() || is_product_category() || is_product_tag() ) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Call a shortcode function by tag name.
*
* @since 1.4.6
*
* @param string $tag The shortcode whose function to call.
* @param array $atts The attributes to pass to the shortcode function. Optional.
* @param array $content The shortcode's content. Default is null (none).
*
* @return string|bool False on failure, the result of the shortcode on success.
*/
function storefront_do_shortcode( $tag, array $atts = array(), $content = null ) {
global $shortcode_tags;
if ( ! isset( $shortcode_tags[ $tag ] ) ) {
return false;
}
return call_user_func( $shortcode_tags[ $tag ], $atts, $content, $tag );
}
/**
* Get the content background color
* Accounts for the Storefront Designer and Storefront Powerpack content background option.
*
* @since 1.6.0
* @return string the background color
*/
function storefront_get_content_background_color() {
if ( class_exists( 'Storefront_Designer' ) ) {
$content_bg_color = get_theme_mod( 'sd_content_background_color' );
$content_frame = get_theme_mod( 'sd_fixed_width' );
}
if ( class_exists( 'Storefront_Powerpack' ) ) {
$content_bg_color = get_theme_mod( 'sp_content_frame_background' );
$content_frame = get_theme_mod( 'sp_content_frame' );
}
$bg_color = str_replace( '#', '', get_theme_mod( 'background_color' ) );
if ( class_exists( 'Storefront_Powerpack' ) || class_exists( 'Storefront_Designer' ) ) {
if ( $content_bg_color && ( 'true' == $content_frame || 'frame' == $content_frame ) ) {
$bg_color = str_replace( '#', '', $content_bg_color );
}
}
return '#' . $bg_color;
}
/**
* Apply inline style to the Storefront header.
*
* @uses get_header_image()
* @since 2.0.0
*/
function storefront_header_styles() {
$is_header_image = get_header_image();
$header_bg_image = '';
if ( $is_header_image ) {
$header_bg_image = 'url(' . esc_url( $is_header_image ) . ')';
}
$styles = array();
if ( '' !== $header_bg_image ) {
$styles['background-image'] = $header_bg_image;
}
$styles = apply_filters( 'storefront_header_styles', $styles );
foreach ( $styles as $style => $value ) {
echo esc_attr( $style . ': ' . $value . '; ' );
}
}
/**
* Apply inline style to the Storefront homepage content.
*
* @uses get_the_post_thumbnail_url()
* @since 2.2.0
*/
function storefront_homepage_content_styles() {
$featured_image = get_the_post_thumbnail_url( get_the_ID() );
$background_image = '';
if ( $featured_image ) {
$background_image = 'url(' . esc_url( $featured_image ) . ')';
}
$styles = array();
if ( '' !== $background_image ) {
$styles['background-image'] = $background_image;
}
$styles = apply_filters( 'storefront_homepage_content_styles', $styles );
foreach ( $styles as $style => $value ) {
echo esc_attr( $style . ': ' . $value . '; ' );
}
}
/**
* Adjust a hex color brightness
* Allows us to create hover styles for custom link colors
*
* @param strong $hex hex color e.g. #111111.
* @param integer $steps factor by which to brighten/darken ranging from -255 (darken) to 255 (brighten).
* @return string brightened/darkened hex color
* @since 1.0.0
*/
function storefront_adjust_color_brightness( $hex, $steps ) {
// Steps should be between -255 and 255. Negative = darker, positive = lighter.
$steps = max( -255, min( 255, $steps ) );
// Format the hex color string.
$hex = str_replace( '#', '', $hex );
if ( 3 == strlen( $hex ) ) {
$hex = str_repeat( substr( $hex, 0, 1 ), 2 ) . str_repeat( substr( $hex, 1, 1 ), 2 ) . str_repeat( substr( $hex, 2, 1 ), 2 );
}
// Get decimal values.
$r = hexdec( substr( $hex, 0, 2 ) );
$g = hexdec( substr( $hex, 2, 2 ) );
$b = hexdec( substr( $hex, 4, 2 ) );
// Adjust number of steps and keep it inside 0 to 255.
$r = max( 0, min( 255, $r + $steps ) );
$g = max( 0, min( 255, $g + $steps ) );
$b = max( 0, min( 255, $b + $steps ) );
$r_hex = str_pad( dechex( $r ), 2, '0', STR_PAD_LEFT );
$g_hex = str_pad( dechex( $g ), 2, '0', STR_PAD_LEFT );
$b_hex = str_pad( dechex( $b ), 2, '0', STR_PAD_LEFT );
return '#' . $r_hex . $g_hex . $b_hex;
}
/**
* Sanitizes choices (selects / radios)
* Checks that the input matches one of the available choices
*
* @param array $input the available choices.
* @param array $setting the setting object.
* @since 1.3.0
*/
function storefront_sanitize_choices( $input, $setting ) {
// Ensure input is a slug.
$input = sanitize_key( $input );
// Get list of choices from the control associated with the setting.
$choices = $setting->manager->get_control( $setting->id )->choices;
// If the input is a valid key, return it; otherwise, return the default.
return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}
/**
* Checkbox sanitization callback.
*
* Sanitization callback for 'checkbox' type controls. This callback sanitizes `$checked`
* as a boolean value, either TRUE or FALSE.
*
* @param bool $checked Whether the checkbox is checked.
* @return bool Whether the checkbox is checked.
* @since 1.5.0
*/
function storefront_sanitize_checkbox( $checked ) {
return ( ( isset( $checked ) && true == $checked ) ? true : false );
}
if ( ! function_exists( 'is_woocommerce_activated' ) ) {
/**
* Query WooCommerce activation
*/
function is_woocommerce_activated() {
_deprecated_function( 'is_woocommerce_activated', '2.1.6', 'storefront_is_woocommerce_activated' );
return class_exists( 'woocommerce' ) ? true : false;
}
}
/**
* Schema type
*
* @return void
*/
function storefront_html_tag_schema() {
_deprecated_function( 'storefront_html_tag_schema', '2.0.2' );
$schema = 'http://schema.org/';
$type = 'WebPage';
if ( is_singular( 'post' ) ) {
$type = 'Article';
} elseif ( is_author() ) {
$type = 'ProfilePage';
} elseif ( is_search() ) {
$type = 'SearchResultsPage';
}
echo 'itemscope="itemscope" itemtype="' . esc_attr( $schema ) . esc_attr( $type ) . '"';
}
/**
* Sanitizes the layout setting
*
* Ensures only array keys matching the original settings specified in add_control() are valid
*
* @param array $input the layout options.
* @since 1.0.3
*/
function storefront_sanitize_layout( $input ) {
_deprecated_function( 'storefront_sanitize_layout', '2.0', 'storefront_sanitize_choices' );
$valid = array(
'right' => 'Right',
'left' => 'Left',
);
if ( array_key_exists( $input, $valid ) ) {
return $input;
} else {
return '';
}
}
/**
* Storefront Sanitize Hex Color
*
* @param string $color The color as a hex.
* @todo remove in 2.1.
*/
function storefront_sanitize_hex_color( $color ) {
_deprecated_function( 'storefront_sanitize_hex_color', '2.0', 'sanitize_hex_color' );
if ( '' === $color ) {
return '';
}
// 3 or 6 hex digits, or the empty string.
if ( preg_match( '|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
return $color;
}
return null;
}
/**
* Returns true if a blog has more than 1 category.
*
* @return bool
* @todo remove in 2.1.
*/
function storefront_categorized_blog() {
_deprecated_function( 'storefront_categorized_blog', '2.0' );
if ( false === ( $all_the_cool_cats = get_transient( 'storefront_categories' ) ) ) {
// Create an array of all the categories that are attached to posts.
$all_the_cool_cats = get_categories( array(
'fields' => 'ids',
'hide_empty' => 1,
// We only need to know if there is more than one category.
'number' => 2,
) );
// Count the number of categories that are attached to the posts.
$all_the_cool_cats = count( $all_the_cool_cats );
set_transient( 'storefront_categories', $all_the_cool_cats );
}
if ( $all_the_cool_cats > 1 ) {
// This blog has more than 1 category so storefront_categorized_blog should return true.
return true;
} else {
// This blog has only 1 category so storefront_categorized_blog should return false.
return false;
}
}