-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
769 lines (621 loc) · 17.5 KB
/
functions.php
File metadata and controls
769 lines (621 loc) · 17.5 KB
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
<?php
/**
* Theme functions
*
* A basic starter theme for your website management system.
*
* @package system
* @subpackage AB_Theme
* @link https://github.com/antibrand/theme
* @since 1.0.0
*/
// Namespace specificity for theme functions & filters.
namespace AB_Theme\Functions;
// Restrict direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Get plugins path
*
* Used to check for active plugins with the `is_plugin_active` function.
*
* @example The following would check for the Advanced Custom Fields plugin:
* ```
* if ( is_plugin_active( 'advanced-custom-fields/acf.php' ) ) {
* // Do stuff.
* }
* ```
*/
// If the generic website management system.
if ( defined( 'APP_INC_PATH' ) && file_exists( APP_INC_PATH . '/backend/plugin.php' ) ) {
include_once( APP_INC_PATH . '/backend/plugin.php' );
// If another system.
} elseif ( file_exists( ABSPATH . 'wp-admin/includes/plugin.php' ) ) {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
/**
* Theme functions class
*
* @since 1.0.0
* @access public
*/
final class Functions {
/**
* Return the instance of the class
*
* @since 1.0.0
* @access public
* @return object
*/
public static function get_instance() {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new self;
// Theme activation & deactivation.
$instance->activation();
// Theme dependencies.
$instance->dependencies();
}
return $instance;
}
/**
* Theme activation & deactivation functions
*
* @since 1.0.0
* @access public
* @return void
*/
private function activation() {
// Require theme activation functions.
require_once get_theme_file_path( '/includes/class-activate.php' );
// Require theme deactivation functions.
require_once get_theme_file_path( '/includes/class-deactivate.php' );
}
/**
* Constructor magic method
*
* @since 1.0.0
* @access public
* @return self
*/
public function __construct() {
// Swap html 'no-js' class with 'js'.
add_action( 'wp_head', [ $this, 'js_detect' ], 0 );
// Theme setup.
add_action( 'after_setup_theme', [ $this, 'setup' ] );
// Register widgets.
add_action( 'widgets_init', [ $this, 'widgets' ] );
// Disable custom colors in the editor.
add_action( 'after_setup_theme', [ $this, 'editor_custom_color' ] );
// Remove unpopular meta tags.
add_action( 'init', [ $this, 'head_cleanup' ] );
// Frontend scripts.
add_action( 'wp_enqueue_scripts', [ $this, 'frontend_scripts' ] );
// Admin scripts.
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
// Frontend styles.
add_action( 'wp_enqueue_scripts', [ $this, 'frontend_styles' ] );
/**
* Admin styles.
*
* Call late to override plugin styles.
*/
add_action( 'admin_enqueue_scripts', [ $this, 'admin_styles' ], 99 );
// Toolbar styles.
add_action( 'wp_enqueue_scripts', [ $this, 'toolbar_styles' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'toolbar_styles' ], 99 );
// Login styles.
add_action( 'login_enqueue_scripts', [ $this, 'login_styles' ] );
// jQuery UI fallback for HTML5 Contact Form 7 form fields.
add_filter( 'wpcf7_support_html5_fallback', '__return_true' );
// Remove WooCommerce styles.
add_filter( 'woocommerce_enqueue_styles', '__return_false' );
// Theme options page.
add_action( 'admin_menu', [ $this, 'theme_options' ] );
// Theme info page.
add_action( 'admin_menu', [ $this, 'theme_info' ] );
// User color scheme classes.
add_filter( 'body_class', [ $this, 'color_scheme_classes' ] );
}
/**
* JS Replace
*
* Replaces 'no-js' class with 'js' in the <html> element
* when JavaScript is detected.
*
* @since 1.0.0
* @access public
* @return string
*/
public function js_detect() {
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
}
/**
* Theme setup
*
* @since 1.0.0
* @access public
* @return void
*/
public function setup() {
/**
* Load domain for translation
*
* @since 1.0.0
*/
load_theme_textdomain( 'antibrand' );
/**
* Add theme support
*
* @since 1.0.0
*/
// Browser title tag support.
add_theme_support( 'title-tag' );
// Background color & image support.
add_theme_support( 'custom-background' );
// RSS feed links support.
add_theme_support( 'automatic-feed-links' );
// HTML 5 tags support.
add_theme_support( 'html5', [
'search-form',
'comment-form',
'comment-list',
'gscreenery',
'caption'
] );
/**
* Set default image sizes
*
* Define the dimensions and the crop options.
*
* @since 1.0.0
*/
// Featured image support.
add_theme_support( 'post-thumbnails' );
// Thumbnail size.
update_option( 'thumbnail_size_w', 160 );
update_option( 'thumbnail_size_h', 160 );
// update_option( 'thumbnail_crop', 1 );
// Medium size.
update_option( 'medium_size_w', 320 );
update_option( 'medium_size_h', 240 );
// update_option( 'medium_crop', 1 );
// Medium-large size.
update_option( 'medium_large_size_w', 480 );
update_option( 'medium_large_size_h', 360 );
// Large size.
update_option( 'large_size_w', 640 );
update_option( 'large_size_h', 480 );
// update_option( 'large_crop', 1 );
// Set the post thumbnail size, 16:9 HD Video.
set_post_thumbnail_size( 1280, 720, [ 'center', 'center' ] );
/**
* Add image sizes
*
* Three sizes per aspect ratio so that we
* will use srcset for responsive images.
*
* @since 1.0.0
*/
// 1:1 square.
add_image_size( __( 'large-thumbnail', 'beeline-theme' ), 240, 240, true );
add_image_size( __( 'xlarge-thumbnail', 'beeline-theme' ), 320, 320, true );
// 16:9 HD Video.
add_image_size( __( 'large-video', 'antibrand' ), 1280, 720, true );
add_image_size( __( 'medium-video', 'antibrand' ), 960, 540, true );
add_image_size( __( 'small-video', 'antibrand' ), 640, 360, true );
// 21:9 Cinemascope.
add_image_size( __( 'large-banner', 'antibrand' ), 1280, 549, true );
add_image_size( __( 'medium-banner', 'antibrand' ), 960, 411, true );
add_image_size( __( 'small-banner', 'antibrand' ), 640, 274, true );
/**
* Custom header
*/
add_theme_support( 'custom-header', apply_filters( 'ab_custom_header', [
'width' => 2048,
'height' => 878,
'flex-height' => true,
'video' => false,
'wp-head-callback' => [ $this, 'header_style' ]
] ) );
register_default_headers( [
'default-image' => [
'url' => '%s/assets/images/default-header.jpg',
'thumbnail_url' => '%s/assets/images/default-header.jpg',
'description' => __( 'Default Header Image', 'antibrand' ),
],
] );
/**
* Custom logo
*
* @since 1.0.0
*/
// Logo arguments.
$logo_args = [
'width' => 180,
'height' => 180,
'flex-width' => true,
'flex-height' => true
];
// Apply a filter to logo arguments.
$logo = apply_filters( 'ab_custom_logo', $logo_args );
// Add logo support.
add_theme_support( 'custom-logo', $logo );
/**
* Set content width.
*
* @since 1.0.0
*/
$ab_content_width = apply_filters( 'ab_content_width', 1280 );
if ( ! isset( $content_width ) ) {
$content_width = $ab_content_width;
}
// Embed sizes.
update_option( 'embed_size_w', 1280 );
update_option( 'embed_size_h', 720 );
/**
* Register theme menus.
*
* @since 1.0.0
*/
register_nav_menus( [
'main' => __( 'Main Menu', 'antibrand' ),
'footer' => __( 'Footer Menu', 'antibrand' ),
'social' => __( 'Social Menu', 'antibrand' ),
'admin' => __( 'Admin Header Menu', 'antibrand' )
] );
/**
* Add stylesheet for the content editor.
*
* @since 1.0.0
*/
add_editor_style( '/assets/css/editor.min.css', [ 'ub-admin' ], '', 'screen' );
}
/**
* Style the header image and text
*
* @since 1.0.0
* @access public
* @return string Returns an HTML style block.
*
*/
public function header_style() {
$header_text_color = get_header_textcolor();
/*
* If no custom options for text are set, let's bail.
* get_header_textcolor() options: Any hex value, 'blank' to hide text. Default: add_theme_support( 'custom-header' ).
*/
if ( get_theme_support( 'custom-header', 'default-text-color' ) === $header_text_color ) {
return;
}
// If we get this far, we have custom styles.
if ( ! display_header_text() ) {
$style = sprintf(
'<style type="text/css">%1s</style>',
'.site-title,
.site-title a,
.site-description {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
}'
);
} else {
$style = sprintf(
'<style type="text/css">%1s</style>',
'.site-title,
.site-title a,
.site-description {
color: #' . esc_attr( $header_text_color ) . ';
}'
);
}
echo $style;
}
/**
* Register widgets
*
* @since 1.0.0
* @access public
* @return void
*/
public function widgets() {
// Add customizer widget refresh support.
add_theme_support( 'customize-selective-refresh-widgets' );
// Register sidebar widget area.
register_sidebar( [
'name' => esc_html__( 'Sidebar', 'antibrand' ),
'id' => 'sidebar',
'description' => esc_html__( 'Add widgets here.', 'antibrand' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
] );
}
/**
* Theme support for disabling custom colors in the editor
*
* @since 1.0.0
* @access public
* @return bool Returns true for the color picker.
*/
public function editor_custom_color() {
$disable = add_theme_support( 'disable-custom-colors', [] );
// Apply a filter for conditionally disabling the picker.
$custom_colors = apply_filters( 'ab_editor_custom_colors', '__return_false' );
return $custom_colors;
}
/**
* Clean up meta tags from the <head>
*
* @since 1.0.0
* @access public
* @return void
*/
public function head_cleanup() {
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
}
/**
* Frontend scripts
*
* @since 1.0.0
* @access public
* @return void
*/
public function frontend_scripts() {
// Enqueue jQuery.
wp_enqueue_script( 'jquery' );
// Navigation toggle and dropdown.
wp_enqueue_script( 'theme-navigation', get_theme_file_uri( '/assets/js/navigation.min.js' ), [], null, true );
// Skip link focus, for accessibility.
wp_enqueue_script( 'antibrand-skip-link-focus-fix', get_theme_file_uri( '/assets/js/skip-link-focus-fix.min.js' ), [], null, true );
// FitVids for responsive video embeds.
wp_enqueue_script( 'ab-fitvids', get_theme_file_uri( '/assets/js/jquery.fitvids.min.js' ), [ 'jquery' ], null, true );
wp_add_inline_script( 'ab-fitvids', 'jQuery(document).ready(function($){ $( ".entry-content" ).fitVids(); });', true );
// Comments scripts.
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
/**
* Admin scripts
*
* @since 1.0.0
* @access public
* @return void
*/
public function admin_scripts() {
// Navigation toggle and dropdown.
wp_enqueue_script( 'theme-navigation', get_theme_file_uri( '/assets/js/navigation.min.js' ), [], null, true );
}
/**
* Frontend styles
*
* @since 1.0.0
* @access public
* @return void
*/
public function frontend_styles() {
// Google fonts.
// wp_enqueue_style( 'antibrand-google-fonts', 'add-url-here', [], '', 'screen' );
/**
* Theme sylesheet
*
* This enqueues the minified stylesheet compiled from SASS (.scss) files.
* The main stylesheet, in the root directory, only contains the theme header but
* it is necessary for theme activation. DO NOT delete the main stylesheet!
*/
wp_enqueue_style( 'antibrand', get_theme_file_uri( '/assets/css/style.min.css' ), [], '' );
// Add right-to-left styles if needed.
if ( is_rtl() ) {
wp_enqueue_style( 'ab-rtl', get_theme_file_uri( '/assets/css/rtl.min.css' ), [ 'antibrand' ], '' );
}
// Print styles.
wp_enqueue_style( 'ab-print', get_theme_file_uri( '/assets/css/print.min.css' ), [], '', 'print' );
}
/**
* Admin styles
*
* @since 1.0.0
* @access public
* @return void
*/
public function admin_styles() {
wp_enqueue_style( 'ab-admin', get_theme_file_uri( '/assets/css/admin.min.css' ), [], '' );
}
/**
* Toolbar styles
*
* Enqueues if user is logged in and admin bar is showing.
*
* @since 1.0.0
* @access public
* @return void
*/
public function toolbar_styles() {
if ( is_user_logged_in() && is_admin_bar_showing() ) {
wp_enqueue_style( 'ab-toolbar', get_theme_file_uri( '/assets/css/toolbar.min.css' ), [], '' );
}
}
/**
* Login styles
*
* @since 1.0.0
* @access public
* @return void
*/
public function login_styles() {
wp_enqueue_style( 'custom-login', get_theme_file_uri( '/assets/css/login.css' ), [], '', 'screen' );
}
/**
* Theme dependencies
*
* @since 1.0.0
* @access private
* @return void
*/
private function dependencies() {
require get_theme_file_path( '/includes/template-functions.php' );
require get_theme_file_path( '/includes/template-tags.php' );
require get_theme_file_path( '/includes/customizer.php' );
}
/**
* Theme options page
*
* @since 1.0.0
* @access public
* @return void
*/
public function theme_options() {
// Add a submenu page under Themes.
$this->help_theme_options = add_submenu_page(
'themes.php',
__( 'Theme Options', 'antibrand' ),
__( 'Theme Options', 'antibrand' ),
'manage_options',
'theme-options',
[ $this, 'theme_options_output' ]
);
// Add sample help tab.
add_action( 'load-' . $this->help_theme_options, [ $this, 'help_theme_options' ] );
}
/**
* Get output of the theme options page
*
* @since 1.0.0
* @access public
* @return void
*/
public function theme_options_output() {
require get_parent_theme_file_path( '/includes/theme-options-page.php' );
}
/**
* Add tabs to the about page contextual help section
*
* @since 1.0.0
* @access public
* @return void
*/
public function help_theme_options() {
// Add to the about page.
$screen = get_current_screen();
if ( $screen->id != $this->help_theme_options ) {
return;
}
// More information tab.
$screen->add_help_tab( [
'id' => 'help_theme_options_info',
'title' => __( 'More Information', 'antibrand' ),
'content' => null,
'callback' => [ $this, 'help_theme_options_info' ]
] );
// Add a help sidebar.
$screen->set_help_sidebar(
$this->help_theme_options_sidebar()
);
}
/**
* Get convert plugin help tab content
*
* @since 1.0.0
* @access public
* @return void
*/
public function help_theme_options_info() {
include_once get_theme_file_path( 'includes/partials/help-theme-options-info.php' );
}
/**
* The about page contextual tab sidebar content
*
* @since 1.0.0
* @access public
* @return string Returns the HTML of the sidebar content.
*/
public function help_theme_options_sidebar() {
$html = sprintf( '<h4>%1s</h4>', __( 'Author Credits', 'antibrand' ) );
$html .= sprintf(
'<p>%1s %2s.</p>',
__( 'This theme was created by', 'antibrand' ),
'Your Name'
);
$html .= sprintf(
'<p>%1s <br /><a href="%2s" target="_blank">%3s</a> <br />%4s</p>',
__( 'Visit', 'antibrand' ),
'https://example.com/',
'Example Site',
__( 'for more details.', 'antibrand' )
);
$html .= sprintf(
'<p>%1s</p>',
__( 'Change this sidebar to give yourself credit for the hard work you did customizing this theme.', 'antibrand' )
);
return $html;
}
/**
* Theme info page
*
* @since 1.0.0
* @access public
* @return void
*/
public function theme_info() {
// Add a submenu page under Themes.
add_submenu_page(
'themes.php',
__( 'Theme Info', 'antibrand' ),
__( 'Theme Info', 'antibrand' ),
'manage_options',
'theme-info',
[ $this, 'theme_info_output' ]
);
}
/**
* Get output of the theme info page
*
* @since 1.0.0
* @access public
* @return void
*/
public function theme_info_output() {
require get_theme_file_path( '/includes/theme-info-page.php' );
}
/**
* User color scheme classes
*
* Add a class to the body element according to
* the user's admin color scheme preference.
*
* @since 1.0.0
* @access public
* @return array Returns a modified array of body classes.
*/
public function color_scheme_classes( $classes ) {
// Add a class if user is logged in and admin bar is showing.
if ( is_user_logged_in() && is_admin_bar_showing() ) {
// Get the user color scheme option.
$scheme = get_user_option( 'admin_color' );
// Return body classes with `user-color-$scheme`.
return array_merge( $classes, array( 'user-color-' . $scheme ) );
}
// Return the unfiltered classes if user is not logged in.
return $classes;
}
}
/**
* Get an instance of the Functions class
*
* This function is useful for quickly grabbing data
* used throughout the theme.
*
* @since 1.0.0
* @access public
* @return object
*/
function ab_theme() {
$ab_theme = Functions::get_instance();
return $ab_theme;
}
// Run the Functions class.
ab_theme();