This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bon-toolkit-core-functions.php
575 lines (434 loc) · 17.8 KB
/
bon-toolkit-core-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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
/**
* Get template part (for templates like the loop).
*
* @access public
* @param mixed $slug
* @param string $name (default: '')
* @return void
*/
function bon_toolkit_get_template_part( $slug, $name = '' ) {
global $bontoolkit;
$template = '';
// Look in yourtheme/slug-name.php and yourtheme/bon-toolkit/slug-name.php
if ( $name )
$template = locate_template( array ( "{$slug}-{$name}.php", "{$bontoolkit->template_url}{$slug}-{$name}.php" ) );
// Get default slug-name.php
if ( !$template && $name && file_exists( $bontoolkit->plugin_path() . "/templates/{$slug}-{$name}.php" ) )
$template = $bontoolkit->plugin_path() . "/templates/{$slug}-{$name}.php";
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/bon-toolkit/slug.php
if ( !$template )
$template = locate_template( array ( "{$slug}.php", "{$bontoolkit->template_url}{$slug}.php" ) );
if ( $template )
load_template( $template, false );
}
if( !function_exists('bon_toolkit_get_prefix') ) {
function bon_toolkit_get_prefix() {
global $bon, $bontoolkit;
if(function_exists('bon_get_prefix')) {
$prefix = bon_get_prefix();
} else {
$prefix = $bontoolkit->prefix;
}
return $prefix;
}
}
if( !function_exists('bon_toolkit_get_builder_suffix') ) {
function bon_toolkit_get_builder_suffix() {
global $bontoolkit;
return $bontoolkit->suffix;
}
}
if( !function_exists('bon_toolkit_get_categories') ) {
/**
* @return array
* @param string $name
* @param string $parent
*/
function bon_toolkit_get_categories( $name, $parent = '' ){
if( empty($parent) ){
$get_category = get_categories( array( 'taxonomy' => $name, 'hide_empty' => 0 ));
$category_list = array();
$category_list['all'] = 'All';
if( !empty($get_category) ){
foreach( $get_category as $category ){
$category_list[$category->slug] = $category->name;
}
}
return $category_list;
} else {
$parent_id = get_term_by('slug', $parent, $category_name);
$get_category = get_categories( array( 'taxonomy' => $name, 'child_of' => $parent_id->term_id, 'hide_empty' => 0 ));
$category_list = array( '0' => $parent );
if( !empty($get_category) ){
foreach( $get_category as $category ){
$category_list[$category->slug] = $category->name;
}
}
return $category_list;
}
}
}
if( !function_exists('bon_toolkit_get_post_id_lists')) {
/**
* @return array
* @param string $post_type
*/
function bon_toolkit_get_post_id_lists( $post_type, $numberposts = 100 ){
$posts = get_posts(array('post_type' => $post_type, 'numberposts'=> $numberposts));
$posts_title = array();
if(!empty($posts)) {
foreach ($posts as $post) {
$posts_title[$post->ID] = $post->post_title;
}
}
return $posts_title;
}
}
if( !function_exists('bon_toolkit_default_widget_args') ) {
function bon_toolkit_default_widget_args() {
if(function_exists('bon_get_default_widget_args') ) {
return bon_get_default_widget_args();
} else {
$defaults = array(
'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-wrap widget-inside">',
'after_widget' => '</div></div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
);
return $defaults;
}
}
}
if( !function_exists('bon_toolkit_process_contact_form') ) {
add_action('wp_ajax_process_contact_form','bon_toolkit_process_contact_form');
add_action('wp_ajax_nopriv_process_contact_form','bon_toolkit_process_contact_form');
function bon_toolkit_process_contact_form() {
if(!isset($_POST) || empty($_POST)) {
$return_data['value'] = __('Cannot send email to destination. No parameter receive form AJAX call.','bon-toolkit');
die ( json_encode($return_data) );
}
$name = esc_html( $_POST['name'] );
if(empty($name)) {
$return_data['value'] = __('Please enter your name.','bon-toolkit');
die ( json_encode($return_data) );
}
$email = sanitize_email( $_POST['email'] );
if(empty($email)){
$return_data['value'] = __('Please enter a valid email address.','bon-toolkit');
die ( json_encode($return_data) );
}
$phone = '';
if( isset( $_POST['phone'] ) ) {
$phone = sanitize_text_field( $_POST['phone'] );
}
$subject = esc_html( $_POST['subject'] );
$messages = esc_textarea( $_POST['messages'] );
if(empty($messages)){
$return_data['value'] = 'Please enter your messages.';
die ( json_encode($return_data) );
}
if(function_exists('akismet_http_post') && trim(get_option('wordpress_api_key')) != '' ) {
global $akismet_api_host, $akismet_api_port;
$c['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
$c['blog'] = get_option('home');
$c['comment_author'] = $name;
$c['comment_author_email'] = $email;
$c['comment_content'] = $messages;
$query_string = '';
foreach ( $c as $key => $data ) {
if( is_string($data) )
$query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
}
$response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
if ( 'true' == $response[1] ) { // Akismet says it's SPAM
$return_data['value'] = __('Cheatin Huh?!', 'bon-toolkit');
die( json_encode($return_data) );
}
}
$receiver = $_POST['receiver'];
$body = "You have received a new contact form message via ".get_bloginfo('name')." \n";
$body .= 'Name : ' . $name . " \n";
$body .= 'Email : ' . $email . " \n";
if( !empty( $phone ) ) {
$body .= 'Phone :' . $phone . " \n";
}
$body .= 'Subject : ' . $subject . " \n";
$body .= 'Message : ' . $messages;
$header = "From: " . $name . " <" . $email . "> \r\n";
$header .= "Reply-To: " . $email;
$subject_email = "[".get_bloginfo('name')." Contact Form] ".$subject;
if( wp_mail($receiver, $subject_email , $body, $header) ){
$return_data['success'] = '1';
$return_data['value'] = __('Email was sent successfully.','bon-toolkit');
die( json_encode($return_data) );
} else {
$return_data['value'] = __('There is an error sending email.','bon-toolkit');
die( json_encode($return_data) );
}
}
}
if( !function_exists('bon_toolkit_filter_builder_class') ) {
function bon_toolkit_filter_builder_class($class) {
$class = str_replace('span', 'column-span-', $class);
return 'bon-toolkit-col '.$class;
}
add_filter('bon_toolkit_builder_render_column_class', 'bon_toolkit_filter_builder_class', 1);
}
if( !function_exists('bon_toolkit_builder_row_class') ) {
function bon_toolkit_builder_row_class($args) {
$args[] = 'bon-toolkit-column-grid';
$args[] = 'column-grid-12';
return $args;
}
add_filter('bon_tookit_builder_render_row_class', 'bon_toolkit_builder_row_class', 1);
}
if( !function_exists('bon_toolkit_check_options') ) {
function bon_toolkit_check_options($options) {
global $bontoolkit;
$bon_toolkit_options = get_option($bontoolkit->option_name);
if( isset($bon_toolkit_options[$options]) && $bon_toolkit_options[$options] == true) {
return true;
}
return false;
}
}
if( !function_exists('bon_toolkit_video') ) {
function bon_toolkit_video($args) {
if( isset($GLOBALS['bon_toolkit_video_count']) )
$GLOBALS['bon_toolkit_video_count']++;
else
$GLOBALS['bon_toolkit_video_count'] = 0;
$defaults = array(
'embed' => '',
'm4v' => '',
'ogv' => '',
'poster' => '',
'id' => $GLOBALS['bon_toolkit_video_count'],
'echo' => true,
'desc' => '',
);
$args = wp_parse_args( $args, $defaults);
extract($args);
global $bontoolkit;
$awe = BON_TOOLKIT_FONT_AWESOME;
$o = '<div class="bon-toolkit-video">';
if(!empty($embed)) {
$o .= '<div class="bon-toolkit-video-embed">';
$embed_code = wp_oembed_get($embed);
$o .= $embed_code;
$o .= '</div>';
} else if(!empty ($m4v) && !empty($ogv)) {
$o .= '<div id="jp-"'.$id.' class="bon-toolkit-jplayer jp-jplayer jp-jplayer-video" data-poster="'.$poster.'" data-m4v="'.$m4v.'" data-ogv="'.$ogv.'"></div>';
$o .= '<div class="jp-video-container">
<div class="jp-video">
<div class="jp-type-single">
<div id="jp-interface-'.$id.'" class="jp-interface">
<div class="jp-controls">
<div class="jp-play" tabindex="1">
<span class="'.$awe.'play icon"></span>
</div>
<div class="jp-pause" tabindex="1">
<span class="'.$awe.'pause icon"></span>
</div>
<div class="jp-progress-container">
<div class="jp-progress">
<div class="jp-seek-bar">
<div class="jp-play-bar"></div>
</div>
</div>
</div>
<div class="jp-mute" tabindex="1"><span class="'.$awe.'volume-up icon"></span></div>
<div class="jp-unmute" tabindex="1"><span class="'.$awe.'volume-off icon"></span></div>
<div class="jp-volume-bar-container">
<div class="jp-volume-bar">
<div class="jp-volume-bar-value"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>';
} else {
$o .= '<p>'. __('Error Loading Video', 'bon-toolkit') . '</p>';
}
if($desc != '') {
$o .= '<div class="bon-toolkit-video-desc">' . $desc . '</div>';
}
$o .= '</div>';
if($echo === true) {
echo $o;
} else {
return $o;
}
}
}
/**
* Return an array of available widgets
* @since 1.0
* @uses bon_sort_name_callback();
* @access public
* @return array()
*/
if( !function_exists('bon_toolkit_wp_widget_lists') ) {
function bon_toolkit_wp_widget_lists() {
global $wp_registered_widgets;
$sort = $wp_registered_widgets;
usort( $sort, 'bon_toolkit_sort_name_callback' );
$done = array();
$return = array();
foreach ( $sort as $widget ) {
if ( in_array( $widget['callback'], $done, true ) ) // We already showed this multi-widget
continue;
$done[] = $widget['callback'];
$return[get_class($widget['callback'][0])] = $widget['callback'][0]->name;
}
return $return;
}
}
/**
* Callback for sortname
* @since 1.0
* @access public
* @return array()
*/
if( !function_exists('bon_toolkit_sort_name_callback') ) {
function bon_toolkit_sort_name_callback( $a, $b ) {
return strnatcasecmp( $a['name'], $b['name'] );
}
}
if( !function_exists('bon_toolkit_get_contact_form') ) {
function bon_toolkit_get_contact_form($email = '', $color = '') {
$color = ( $color != '' ) ? $color : 'blue';
if(empty($email) || !is_email($email)) {
return __('Failed rendering contact form. Please provide a correct <strong>Email Address</strong>.','bon-toolkit');
}
$o = apply_filters('bon_toolkit_get_contact_form', '', $email, $color);
if($o != '')
return $o;
$o .= '<form class="bon-builder-contact-forms"><div class="contact-form-wrapper">';
$o .= '<div class="contact-form-input">';
$o .= '<label for="name">'.__('Your Name', 'bon-toolkit').'</label>';
$o .= '<input type="text" value="" name="name" class="name required" placeholder="'.__('Name', 'bon').'" />';
$o .= '<div class="contact-form-error">'.__('Please enter your name.','bon-toolkit').'</div>';
$o .= '</div>';
$o .= '<div class="contact-form-input">';
$o .= '<label for="email-address">'.__('Email Address', 'bon-toolkit').'</label>';
$o .= '<input type="email" value="" name="email" class="email-address required" placeholder="'.__('Email', 'bon').'" />';
$o .= '<div class="contact-form-error">'.__('Please enter valid email address.','bon-toolkit').'</div>';
$o .= '</div>';
$o .= '<div class="contact-form-input">';
$o .= '<label for="subject">'.__('Subject', 'bon-toolkit').'</label>';
$o .= '<input type="text" value="" name="subject" class="subject" placeholder="'.__('Subject', 'bon').'" />';
$o .= '</div>';
$o .= '<div class="contact-form-input">';
$o .= '<label for="messages">'.__('Your Messages', 'bon-toolkit').'</label>';
$o .= '<textarea name="messages" class="messages required" placeholder="'.__('Messages', 'bon').'"></textarea>';
$o .= '<div class="contact-form-error">'.__('Please enter your messages.','bon-toolkit').'</div>';
$o .= '</div>';
$o .= '<input type="hidden" name="receiver" value="'.$email.'" />';
$o .= '<div class="contact-form-input">';
$o .= '<button type="submit" class="contact-form-submit bon-toolkit-button round-corner flat '.$color.'" name="submit">'.__('Send Message','bon-toolkit').'</button>';
$o .= '<span class="contact-form-ajax-loader"><img src="'.trailingslashit( BON_TOOLKIT_IMAGES ).'loader.gif" alt="loading..." /></span>';
$o .= '</div>';
$o .= '</div><div class="sending-result"><div class="green bon-toolkit-alert"></div></div></form>';
return $o;
}
}
function bon_toolkit_post_custom_bg() {
if(!current_theme_supports( 'bon-custom-post-bg' )) {
return;
}
$post_types = get_theme_support( 'bon-custom-post-bg' );
$post_types = $post_types[0];
if(current_theme_supports( 'bon-custom-post-bg' ) && is_array($post_types) && class_exists('BON_Metabox')) {
$prefix = bon_toolkit_get_prefix();
if(is_admin()) {
$mb = new BON_Metabox();
$fields = array(
array(
'label' => __('Background Image', 'bon-toolkit'),
'desc' => __('Upload a custom background image for this post. Once image has been uploaded, click Insert into Post.', 'bon-toolkit'),
'type' => 'image',
'id' => $prefix . 'custom_bg_image',
),
array(
'label' => __('Background Repeat', 'bon-toolkit'),
'desc' => __('Select the preferred repeat for the uploaded image.', 'bon-toolkit'),
'type' => 'select',
'options' => array(
'no-repeat' => __('No Repeat', 'bon-toolkit'),
'repeat-x' => __('Repeat Horizontal', 'bon-toolkit'),
'repeat-y' => __('Repeat Vertical', 'bon-toolkit'),
'repeat' => __('Repeat', 'bon-toolkit'),
),
'id' => $prefix . 'custom_bg_repeat',
),
array(
'label' => __('Background Position', 'bon-toolkit'),
'desc' => __('Select the background position for the uploaded image.', 'bon-toolkit'),
'type' => 'select',
'options' => array(
'left' => __('Left', 'bon-toolkit'),
'right' => __('Right', 'bon-toolkit'),
'center' => __('Center', 'bon-toolkit'),
),
'id' => $prefix . 'custom_bg_position',
),
array(
'label' => __('Background Cover', 'bon-toolkit'),
'desc' => __('If browser supported, the background image should cover the block.', 'bon-toolkit'),
'type' => 'select',
'options' => array(
'true' => __('Yes', 'bon-toolkit'),
'false' => __('No', 'bon-toolkit'),
),
'id' => $prefix . 'custom_bg_cover',
),
array(
'label' => __('Background Color', 'bon-toolkit'),
'desc' => __('Select a custom background color for this post.', 'bon-toolkit'),
'type' => 'color',
'id' => $prefix . 'custom_bg_color',
),
);
$mb->create_box('bon-custom-background', __('Custom Background','bon'), $fields, $post_types, 'normal', 'high');
}
}
}
add_action( 'init', 'bon_toolkit_post_custom_bg', 20 );
function bon_toolkit_filter_custom_bg() {
if(!current_theme_supports( 'bon-custom-post-bg' )) {
return;
}
$post_types = get_theme_support( 'bon-custom-post-bg' );
$post_types = $post_types[0];
if(is_array($post_types) && current_theme_supports('bon-custom-post-bg')) {
if( is_singular($post_types) ) {
$prefix = bon_toolkit_get_prefix();
$bg_image = get_post_meta( get_the_ID(), "{$prefix}custom_bg_image", true );
$bg_image = wp_get_attachment_url($bg_image);
$bg_repeat = get_post_meta( get_the_ID(), "{$prefix}custom_bg_repeat", true );
$bg_color = get_post_meta( get_the_ID(), "{$prefix}custom_bg_color", true );
$bg_position = get_post_meta( get_the_ID(), "{$prefix}custom_bg_position", true );
$bg_cover = get_post_meta( get_the_ID(), "{$prefix}custom_bg_cover", true );
if(empty($bg_image) && empty($bg_color)) {
return;
}
$current_post_type = get_post_type();
$id = get_the_ID();
$selector = apply_filters( 'bon_toolkit_custom_bg_selector', '.custom-post-background' );
$bg_attachment = 'scroll';
$bg_cover_style = '';
if('true' == $bg_cover) {
$bg_attachment = 'fixed';
$bg_cover_style = '-webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;';
}
$style = "<style type='text/css' id='custom-post-background-css'> .singular-{$current_post_type}-{$id} {$selector} { background: url({$bg_image}) {$bg_repeat} {$bg_position} {$bg_color} {$bg_attachment}; {$bg_cover_style} }</style>";
echo $style;
}
}
}
add_action('wp_head', 'bon_toolkit_filter_custom_bg', 1000);
?>