This repository has been archived by the owner on Feb 7, 2021. It is now read-only.
/
functions.php
executable file
·403 lines (368 loc) · 14.7 KB
/
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/**
* Twenty'em WordPress Framework.
*
* WARNING: This file is part of Twenty'em WordPress Framework.
* DO NOT edit this file under any circumstances. Do all your modifications in the form of a child theme.
*
* @package WordPress
* @subpackage Twenty'em
* @author RogerTM
* @license license.txt
* @link https://themingisprose.com/twenty-em
* @since Twenty'em 1.0
*/
/**
* Twenty'em Helpers Functions.
*/
/**
* Helper. Register and Enqueue Bootstrap jQuery Plugins
*
* @param $plugin Required. String. Plugin name and extension (IE: transition.js)
* @param $script Optional. String. Additional script if needed by the plugin
* @param $script_src Optional. String. Required if $script, the $script source address
* @param $deps Optional. Array. Array of the handles of all the registered scripts that this script depends on.
* @param $transition Optional. Bool. Enqueue util.js Bootstrap plugin for simple transition effects
* @param $in_footer Optional. Bool. If true, the script is placed before the </body> end tag
*
* @since Twenty'em 1.0
*/
function t_em_register_bootstrap_plugin( $plugin, $script = '', $script_src = '', $deps = array(), $util = true, $in_footer = true ){
$deps = array_merge( $deps, array( 'jquery' ) );
if ( WP_DEBUG ) :
$handle = ( file_exists( T_EM_THEME_DIR_BOOTSTRAP_PATH .'/js/'. $plugin .'.js' ) ) ? $plugin .'.js' : $plugin .'.min.js';
$util = ( $util && file_exists( T_EM_THEME_DIR_BOOTSTRAP_PATH .'/js/'. 'util.js' ) ) ? 'util.js' : 'util.min.js';
else :
$handle = ( file_exists( T_EM_THEME_DIR_BOOTSTRAP_PATH .'/js/'. $plugin .'.min.js' ) ) ? $plugin .'.min.js' : $plugin .'.js';
$util = ( $util && file_exists( T_EM_THEME_DIR_BOOTSTRAP_PATH .'/js/'. 'util.min.js' ) ) ? 'util.min.js' : 'util.js';
endif;
if ( $util ) :
array_push( $deps, $util );
wp_register_script( $util, T_EM_THEME_DIR_BOOTSTRAP_URL.'/js/'. $util, array( 'jquery' ), t_em_theme( 'Version' ), $in_footer );
endif;
wp_register_script( $handle, T_EM_THEME_DIR_BOOTSTRAP_URL .'/js/'. $handle, $deps, t_em_theme( 'Version' ), $in_footer );
wp_enqueue_script( $handle );
if ( $script ) :
wp_register_script( $script, $script_src, array( 'jquery' ), t_em_theme( 'Version' ), $in_footer );
wp_enqueue_script( $script );
endif;
}
/**
* Get javascript files
* This functions loads minify scripts if the site is running online, otherwise (WP_DEBUG == true),
* loads the beautify script.
* @param string $handle Script handle. If the file is named my.script.min.js, $handle = 'my.script'
* Both files should exists: my.script.min.js and my.script.js
* @param string $path File path. If is a child theme set this parameter to T_EM_CHILD_THEME_DIR_PATH .'/path/to/js-dir/'
* @param string $url File URL. If is a child theme set this parameter to T_EM_CHILD_THEME_DIR_URL .'/url/to/js-dir/'
* @return string File URL. False if file does not exist
*
* @since Twenty'em 1.2
*/
function t_em_get_js( $handle, $path = T_EM_THEME_DIR_JS_PATH, $url = T_EM_THEME_DIR_JS_URL ){
if ( WP_DEBUG ) :
$file = ( file_exists( $path .'/'. $handle .'.js' ) ) ? $handle .'.js' : $handle .'.min.js';
else :
$file = ( file_exists( $path .'/'. $handle .'.min.js' ) ) ? $handle .'.min.js' : $handle .'.js';
endif;
if ( file_exists( $path .'/'. $file ) ) :
$file_url = $url .'/'. $file;
return $file_url;
endif;
return false;
}
/**
* Get css files
* This functions loads minify css file if the site is running online, otherwise (WP_DEBUG == true),
* loads the beautify css.
* @param string $handle Script handle. If the file is named my.css.min.js, $handle = 'my.css'
* Both files should exists: my.css.min.js and my.css.js
* @param string $path File path. If is a child theme set this parameter to T_EM_CHILD_THEME_DIR_PATH .'/path/to/css-dir/'
* @param string $url File URL. If is a child theme set this parameter to T_EM_CHILD_THEME_DIR_URL .'/url/to/css-dir/'
* @return string File URL. False if file does not exist
*
* @since Twenty'em 1.2
*/
function t_em_get_css( $handle, $path = T_EM_THEME_DIR_CSS_PATH, $url = T_EM_THEME_DIR_CSS_URL ){
if ( WP_DEBUG ) :
$file = ( file_exists( $path .'/'. $handle .'.css' ) ) ? $handle .'.css' : $handle .'.min.css';
else :
$file = ( file_exists( $path .'/'. $handle .'.min.css' ) ) ? $handle .'.min.css' : $handle .'.css';
endif;
if ( file_exists( $path .'/'. $file ) ) :
$file_url = $url .'/'. $file;
return $file_url;
endif;
return false;
}
/**
* Add Bootstrap CSS Classes dynamically.
*
* @param string $section Required. The name of the section that Bootstrap CSS Classes are needed.
*
* @since Twenty'em 1.0
*/
function t_em_breakpoint( $section ){
$breakpoint = array();
/** Main Content, Content, Sidebar and Sidebar Alt */
$layout_set = t_em( 'layout_set' );
$one_column = in_array( $layout_set, array( 'one-column' ) );
$two_column = in_array( $layout_set,
array( 'two-columns-content-right',
'two-columns-content-left' ) );
$three_column = in_array( $layout_set,
array( 'three-columns-content-left',
'three-columns-content-right',
'three-columns-content-middle' ) );
// #main-content
if ( 'main-content' == $section ) :
$breakpoint[] = 'row';
endif;
// #content and three-column or one-column
if ( 'content' == $section && $three_column ) :
$breakpoint[] = t_em_grid( '6' );
elseif ( 'content' == $section && $one_column ) :
$breakpoint[] = t_em_grid( '12' );
endif;
// #content && #main-content One column templates
if ( 'content-one-column' == $section ) :
$breakpoint[] = t_em_grid( '12' );
$breakpoint[] = 'one-column';
endif;
// #sidebar and three-column
if ( 'sidebar' == $section && $three_column ) :
$breakpoint[] = t_em_grid( '3' );
$breakpoint[] = 'widget-area';
endif;
// #sidebar-alt and three-column
if ( 'sidebar-alt' == $section && $three_column ) :
$breakpoint[] = t_em_grid( '3' );
$breakpoint[] = 'widget-area';
endif;
// #content and two-column
if ( 'content' == $section && $two_column && ! ( is_home() && t_em( 'front_page_set' ) != 'wp-front-page' ) ) :
$breakpoint[] = t_em_grid( '8' );
endif;
// #content and !front_page_set['wp-front-page']
if ( 'content' == $section && is_front_page() && t_em( 'front_page_set' ) != 'wp-front-page' ) :
$breakpoint[] = t_em_grid( '12' );
endif;
// #sidebar and two-column
if ( 'sidebar' == $section && $two_column ) :
$breakpoint[] = t_em_grid( '4' );
$breakpoint[] = 'widget-area';
endif;
/** Static Header Content and Image */
if ( 'static-header' == $section ) :
$static_header_img = ( ! empty ( t_em( 'static_header_img_src' ) ) ) ? '1' : '0';
$static_header_content = ( ! empty ( t_em( 'static_header_headline' ) )
|| ! empty ( t_em( 'static_header_content' ) )
|| ! empty ( t_em( 'static_header_primary_button_text' ) )
|| ! empty ( t_em( 'static_header_secondary_button_text' ) )
) ? '1' : '0';
$total_static_header = array_sum( array( $static_header_img, $static_header_content ) );
$cols = 12 / $total_static_header;
$breakpoint[] = t_em_grid( $cols );
endif;
/** Front Page Widgets Area */
if ( 'primary-featured-widget-area' == $section ) :
$breakpoint[] = t_em_grid( '12', 'md' );
endif;
// Classes are needed for secondaries widgets only (two, three and four).
if ( 'secondary-featured-widget-area' == $section ) :
$widget_two = ( ! empty ( t_em( 'headline_text_widget_two' ) ) || ! empty ( t_em( 'content_text_widget_two' ) ) ) ? '1' : '0' ;
$widget_three = ( ! empty ( t_em( 'headline_text_widget_three' ) ) || ! empty ( t_em( 'content_text_widget_three' ) ) ) ? '1' : '0' ;
$widget_four = ( ! empty ( t_em( 'headline_text_widget_four' ) ) || ! empty ( t_em( 'content_text_widget_four' ) ) ) ? '1' : '0' ;
$total_widgets = array_sum( array( $widget_two, $widget_three, $widget_four ) );
$cols = 12 / $total_widgets;
$breakpoint[] = t_em_grid( $cols, 'md' );
endif;
/** Footer Widgets Area */
if ( 'footer-widget-area' == $section ) :
$one_widget_footer = ( 'no-footer-widget' != t_em( 'footer_set' ) ) ? '1' : '0';
$two_widget_footer = ( in_array( t_em( 'footer_set' ),
array( 'two-footer-widget', 'three-footer-widget', 'four-footer-widget' )
) ) ? '1' : '0';
$three_widget_footer = ( in_array( t_em( 'footer_set' ),
array( 'three-footer-widget', 'four-footer-widget' )
) ) ? '1' : '0';
$four_widget_footer = ( in_array( t_em( 'footer_set' ),
array( 'four-footer-widget' )
) ) ? '1' : '0';
$total_widgets = array_sum( array( $one_widget_footer, $two_widget_footer, $three_widget_footer, $four_widget_footer ) );
$cols = 12 / $total_widgets;
$breakpoint[] = t_em_grid( $cols );
endif;
/**
* Filter the list of CSS Bootstrap classes on the current template.
* The dynamic portion of the hook name, "$section", refers to the parameter $section where this
* functions is called.
*
* @param array Bootstrap CSS classes
* @since Twenty'em 1.0
*/
$classes = apply_filters( "t_em_filter_breakpoint_{$section}", $breakpoint );
$classes = array_unique( $classes );
echo 'class="' . implode( ' ', $classes ) . '"';
}
/**
* Grid breakpoint
* @param string $cols Columns
* @param string $breakpoint breakpoint. Default 'lg'
*
* @since Twenty'em 1.2
*/
function t_em_grid( $cols, $breakpoint = '' ){
/**
* Filter the default breakpoint
* @param string $breakpoint Default breakpoint
*
* @since Twenty'em 1.2
*/
$bp = ( ! $breakpoint ) ? apply_filters( 't_em_filter_default_breakpoint', 'lg' ) : $breakpoint;
$sep = ( $bp ) ? '-' : null;
$class = 'col-'. $bp . $sep . $cols;
return $class;
}
/**
* Container or Container Fluid, that's the meollo
* @param bool $echo. Echo or return the result
* @return string .container or .container-fluid class, set by the user in the admin panel Layout Options
*
* @since Twenty'em 1.2
*/
function t_em_container( $echo = true ){
$container = ( t_em( 'layout_fluid_width' ) ) ? 'container-fluid' : 'container';
if ( $echo )
echo $container;
else
return $container;
}
/**
* Display featured image in posts archives when "Display the Excerpt" option is
* activated in admin theme option page.
*
* @param int $width Require Thumbnail width.
* @param int $height Require Thumbnail height.
* @param boolean $link Optional The image will be linkable or not. Default: true.
* @param string $class Optional CSS class.
* @param int $post_id Optional. Post ID. Default is ID of the global $post.
*
* @return string HTML "img" tag
*
* @since Twenty'em 1.0
* @since Twenty'em 1.2 Deleted "figure" and "figcaption" wrapped element
* @since Twenty'em 1.3.2 Show only the featured post thumbnail if exists
*/
function t_em_featured_post_thumbnail( $width, $height, $link = true, $class = null, $post_id = 0 ){
$post_id = absint( $post_id );
if ( ! $post_id )
$post_id = get_the_ID();
$attachment_id = get_post_thumbnail_id( $post_id );
$open_link = ( $link ) ? '<a href="'. get_permalink( $post_id ) .'" rel="bookmark">' : null;
$close_link = ( $link ) ? '</a>' : null;
/**
* Filter default image to show if the current post has no image associated to it
* @param null $default_post_thumbnail. URL of the default post thumbnail
*
* @since Twenty'em 1.0
*/
$default_thumbnail = apply_filters( 't_em_filter_default_post_thumbnail', null );
$thumbnail = ( t_em_image_resize( $width, $height, $attachment_id ) ) ? t_em_image_resize( $width, $height, $attachment_id ) : $default_thumbnail;
if ( $thumbnail ) :
echo $open_link; ?>
<img class="featured-post-thumbnail <?php echo $class ?>" alt="<?php echo get_the_title( $post_id ); ?>" src="<?php echo $thumbnail ?>" width="<?php echo $width ?>" height="<?php echo $height ?>"/>
<?php echo $close_link;
endif;
}
/**
* Resize images on the fly
*
* @param int $width Required. New image width
* @param int $height Required. New image height
* @param int $attachment_id Required. Attachment ID
* @param bool $crop Optional. Crop image. Default true
*
* @return string|null URL of the new image.
*
* @since Twenty'em 1.0
* @since Twenty'em 1.3.2 Change parameter $post_id to $attachment_id
*/
function t_em_image_resize( $width, $height, $attachment_id, $crop = true ){
$image_path = get_attached_file( $attachment_id );
if ( ! $image_path )
return;
// Image data
$image_url = wp_get_attachment_image_src( $attachment_id, 'full' );
$image_src = $image_url[0]; // Original image
$image_edit = wp_get_image_editor( $image_url[0] );
$image_info = pathinfo( $image_path );
// Image dimensions
$image_size = getimagesize( $image_path );
// If Width and Height are bigger than original size we use the original image
if ( $width >= $image_size[0] && $height >= $image_size[1] ) :
$image_thumbnail = $image_src;
else :
// Set dimensions
$max_width = ( $width >= $image_size[0] ) ? $image_size[0] : $width;
$max_height = ( $height >= $image_size[1] ) ? $image_size[1] : $height;
// Image to be saved
$image_to_save = $image_info['dirname'] .'/'. $image_info['filename'] .'-'. $max_width .'x'. $max_height .'.'. $image_info['extension'];
// Create the new image
if ( ! file_exists( $image_to_save ) ) :
if ( ! is_wp_error( $image_edit ) ) :
$image_edit->resize( $max_width, $max_height, $crop );
$image_edit->save( $image_to_save );
endif;
endif;
$image_thumbnail = str_replace( $image_info['filename'], $image_info['filename'] .'-'. $max_width .'x'. $max_height, $image_src );
endif;
return $image_thumbnail;
}
/**
* Helper. Wrap paragraphs into <p> ...</p> tags, and clean empty lines
* @param string $paragraph Require Paragraph to be wrapped into <p> ...</p> tags
* @return string
*
* @since Twenty'em 1.0
*/
function t_em_wrap_paragraph( $paragraph ){
$wrap_paragraph = explode( "\n", $paragraph );
$i = 0;
$ps = count($wrap_paragraph) - 1;
while ( $i <= $ps ) :
$p[$i] = "<p>" . $wrap_paragraph[$i] . "</p>";
$clean_paragraph[$i] = str_replace( "<p>\r</p>", "", $p[$i] );
$i++;
endwhile;
return implode( "", $clean_paragraph );
}
/**
* Get the resume of every post
* @param int $post_id Post ID. Default 0, the current post
* @param bool $echo Return the value or print it on screen. Default to 'true'
* @return string The 'post_excerpt' field or the first $trim words of the 'post_content' fields
* if 'post_excerpt' is empty
*
* @since Twenty'em 1.2.2
*/
function t_em_get_post_excerpt( $post_id = 0, $echo = true ){
global $post;
$post_id = absint( $post_id );
if ( ! $post_id )
$post_id = $post->ID;
$excerpt = get_post_field( 'post_excerpt', $post_id );
$content = get_post_field( 'post_content', $post_id );
$trim = t_em( 'excerpt_length' );
if ( ! empty( $excerpt ) ) :
$resume = $excerpt;
else :
$resume = wp_trim_words( $content, $trim );
endif;
if ( $echo ) :
echo strip_shortcodes( $resume );
else :
return strip_shortcodes( $resume );
endif;
}
?>