-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
179 lines (131 loc) · 5.19 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
<?php
/* Don't edit this file. Use a child theme instead. */
namespace Kntnt_Canvas;
new class {
private $ns;
private $theme_uri;
private $theme_dir;
private $theme;
private $theme_style;
private $theme_script;
private $theme_textdomain;
private $theme_domainpath;
private $child_theme = null;
private $child_theme_style = null;
private $child_theme_script = null;
private $child_theme_textdomain = null;
private $child_theme_domainpath = null;
public function __construct() {
$this->ns = strtolower( strtr( __NAMESPACE__, '_', '-' ) );
$this->theme_dir = get_template_directory();
$this->theme_uri = get_template_directory_uri();
$this->child_theme_dir = get_stylesheet_directory();
$this->child_theme_uri = get_stylesheet_directory_uri();
add_action( 'after_setup_theme', [ $this, 'setup' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] );
add_action( 'init', [ $this, 'enqueue_core_block_css' ] );
add_action( 'init', [ $this, 'register_block_style' ] );
add_action( 'init', [ $this, 'register_block_pattern_categories' ] );
}
public function setup() {
$this->theme = wp_get_theme( $this->ns );
$this->theme_style = "$this->theme_uri/assets/theme.css";
$this->theme_script = "$this->theme_uri/assets/theme.min.js";
$this->theme_textdomain = $this->theme->get( 'TextDomain' );
$this->theme_domainpath = $this->theme_dir . $this->theme->get( 'DomainPath' );
if ( $this->child_theme_dir != $this->theme_dir ) {
$this->child_theme = wp_get_theme();
$this->child_theme_style = file_exists( "$this->child_theme_dir/assets/theme.css" ) ? "$this->child_theme_uri/assets/theme.css" : "$this->child_theme_uri/style.css";
$this->child_theme_script = file_exists( "$this->child_theme_dir/assets/theme.min.js" ) ? "$this->child_theme_uri/assets/theme.min.js" : ( file_exists( "$this->child_theme_dir/assets/theme.js" ) ? "$this->child_theme_uri/assets/theme.js" : null );
$this->child_theme_textdomain = $this->child_theme->get( 'TextDomain' );
$this->child_theme_domainpath = $this->child_theme_dir . $this->child_theme->get( 'DomainPath' );
}
// Support translation.
load_theme_textdomain( $this->theme_textdomain, $this->theme_domainpath );
if ( $this->child_theme_textdomain ) {
load_theme_textdomain( $this->child_theme_textdomain, $this->child_theme_domainpath );
}
// Enqueue editor styles.
$editor_styles = [ $this->theme_style ];
if ( $this->child_theme_style ) {
$editor_styles[] = $this->child_theme_style;
}
add_editor_style( $editor_styles );
// Remove core block patterns.
remove_theme_support( 'core-block-patterns' );
}
public function enqueue_assets() {
wp_enqueue_style( $this->ns, $this->theme_style, [], $this->theme->version );
if ( $this->child_theme_style ) {
wp_enqueue_style( $this->child_theme->stylesheet, $this->child_theme_style, [ $this->ns ], $this->child_theme->version );
}
wp_enqueue_script( $this->ns, $this->theme_script, [], $this->theme->version, [ 'strategy' => 'async', 'in_footer' => true ] );
if ( $this->child_theme_script ) {
$args = apply_filters( 'kntnt_canvas_child_script_args', [ 'strategy' => 'async', 'in_footer' => true ] );
wp_enqueue_script( $this->child_theme->stylesheet, $this->child_theme_script, [ $this->ns ], $args );
}
}
public function enqueue_core_block_css() {
$files = glob( "$this->theme_dir/assets/core-block-css/*" );
foreach ( $files as $file ) {
$block_slug = basename( $file, '.css' );
$block_name = 'core/' . substr( $block_slug, 5 );
$block_file = "assets/core-block-css/$block_slug.css";
wp_enqueue_block_style( $block_name, [
'handle' => "kntnt-canvas-$block_slug",
'src' => get_theme_file_uri( $block_file ),
'path' => get_theme_file_path( $block_file ),
] );
}
}
public function register_block_style() {
$block_styles = [
'core/separator' => [
[
'name' => 'wide-dotted',
'label' => 'Wide Dotted',
],
[
'name' => 'wide-dashed',
'label' => 'Wide Dashed',
],
[
'name' => 'wide-double',
'label' => 'Wide Double',
],
[
'name' => 'hidden',
'label' => 'Hidden',
],
],
];
foreach ( $block_styles as $name => $styles ) {
foreach ( $styles as $style ) {
register_block_style( $name, $style );
}
}
}
public function register_block_pattern_categories() {
$pattern_categories = [
'hero' => [
'label' => __( 'Hero sections', 'kntnt-canvas' ),
'description' => __( 'Introductory section at top of page.', 'kntnt-canvas' ),
],
'heading' => [
'label' => __( 'Heading sections', 'kntnt-canvas' ),
'description' => __( 'Introductory section at top of posts.', 'kntnt-canvas' ),
],
'heading_parts' => [
'label' => __( 'Heading parts', 'kntnt-canvas' ),
'description' => __( 'Patterns used to build headings.', 'kntnt-canvas' ),
],
'theme' => [
'label' => __( 'Theme patters', 'kntnt-canvas' ),
'description' => __( 'Patters used by the theme.', 'kntnt-canvas' ),
],
];
foreach ( $pattern_categories as $slug => $category ) {
register_block_pattern_category( $slug, $category );
}
}
};