forked from jeffreyvr/tailpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
173 lines (141 loc) · 4.05 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
<?php
/**
* Enqueue scripts.
*/
function tailpress_enqueue_scripts() {
$theme = wp_get_theme();
wp_enqueue_script( 'jquery' );
wp_enqueue_style( 'tailpress', tailpress_get_mix_compiled_asset_url( 'css/app.css' ), array(), $theme->get( 'Version' ) );
wp_enqueue_script( 'tailpress', tailpress_get_mix_compiled_asset_url( 'js/app.js' ), array( 'jquery' ), $theme->get( 'Version' ) );
}
add_action( 'wp_enqueue_scripts', 'tailpress_enqueue_scripts' );
/**
* Get mix compiled asset.
*
* @param string $path The path to the asset.
*
* @return string
*/
function tailpress_get_mix_compiled_asset_url( $path ) {
$path = '/' . $path;
$stylesheet_dir_uri = get_stylesheet_directory_uri();
$stylesheet_dir_path = get_stylesheet_directory();
if ( ! file_exists( $stylesheet_dir_path . '/mix-manifest.json' ) ) {
return $stylesheet_dir_uri . $path;
}
$mix_file_path = file_get_contents( $stylesheet_dir_path . '/mix-manifest.json' );
$manifest = json_decode( $mix_file_path, true );
$asset_path = ! empty( $manifest[ $path ] ) ? $manifest[ $path ] : $path;
return $stylesheet_dir_uri . $asset_path;
}
/**
* Get data from the tailpress.json file.
*
* @param mixed $key The key to retrieve.
*
* @return mixed|null
*/
function tailpress_get_data( $key = null ) {
$config = json_decode( file_get_contents( get_stylesheet_directory() . '/tailpress.json' ), true );
if ( $key === null ) {
return filter_var_array( $config, FILTER_SANITIZE_STRING );
}
$option = filter_var( $config[ $key ], FILTER_SANITIZE_STRING );
return $option ?? null;
}
/**
* Theme setup.
*/
function tailpress_setup() {
// Let WordPress manage the document title.
add_theme_support( 'title-tag' );
// This theme uses wp_nav_menu() in one location.
register_nav_menus(
array(
'primary' => __( 'Primary Menu', 'tailpress' ),
)
);
// Switch default core markup for search form, comment form, and comments
// to output valid HTML5.
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
)
);
// Adding Thumbnail basic support.
add_theme_support( 'post-thumbnails' );
// Block editor.
add_theme_support( 'align-wide' );
add_theme_support( 'wp-block-styles' );
add_theme_support( 'editor-styles' );
add_editor_style();
$tailpress = tailpress_get_data();
$colors = array_map(
function ( $color, $hex ) {
return array(
'name' => ucfirst( $color ),
'slug' => $color,
'color' => $hex,
);
},
array_keys( $tailpress['colors'] ),
$tailpress['colors']
);
$font_sizes = array_map(
function ( $size, $px ) {
return array(
'name' => ucfirst( $size ),
'size' => $px,
'slug' => $size,
);
},
array_keys( $tailpress['fontSizes'] ),
$tailpress['fontSizes']
);
add_theme_support( 'editor-color-palette', $colors );
add_theme_support( 'editor-font-sizes', $font_sizes );
}
add_action( 'after_setup_theme', 'tailpress_setup' );
/**
* Adds option 'li_class' to 'wp_nav_menu'.
*
* @param string $classes String of classes.
* @param mixed $item The curren item.
* @param WP_Term $args Holds the nav menu arguments.
*
* @return array
*/
function tailpress_nav_menu_add_li_class( $classes, $item, $args, $depth ) {
if ( isset( $args->li_class ) ) {
$classes[] = $args->li_class;
}
if ( isset( $args->{"li_class_$depth"} ) ) {
$classes[] = $args->{"li_class_$depth"};
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'tailpress_nav_menu_add_li_class', 10, 4 );
/**
* Adds option 'submenu_class' to 'wp_nav_menu'.
*
* @param string $classes String of classes.
* @param mixed $item The curren item.
* @param WP_Term $args Holds the nav menu arguments.
*
* @return array
*/
function tailpress_nav_menu_add_submenu_class( $classes, $args, $depth ) {
if ( isset( $args->submenu_class ) ) {
$classes[] = $args->submenu_class;
}
if ( isset( $args->{"submenu_class_$depth"} ) ) {
$classes[] = $args->{"submenu_class_$depth"};
}
return $classes;
}
add_filter( 'nav_menu_submenu_css_class', 'tailpress_nav_menu_add_submenu_class', 10, 3 );