Repository navigation
Expand file tree
/
Copy pathfunctions.php
More file actions
225 lines (192 loc) · 5.94 KB
/
Copy pathfunctions.php
File metadata and controls
225 lines (192 loc) · 5.94 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
<?php
/**
* This file adds functions to the Kindling WordPress theme.
*
* @package kindling
* @author Matchbox Design Group
* @license GNU General Public License v2 or later
* @link https://github.com/matchboxdesigngroup/kindling
*/
namespace Kindling;
/**
* Set up theme defaults and register various WordPress features.
*
* @since 4.0.0
*
* @return void
*/
function setup() {
/**
* Make theme available for translation.
* - Text domain: 'kindling'
* - Translation files location: /languages (e.g. /languages/kindling.pot, .mo, .po)
*/
load_theme_textdomain( 'kindling', get_template_directory() . '/languages' );
// Enqueue editor styles and fonts.
add_editor_style( 'style.css' );
// Remove core block patterns.
remove_theme_support( 'core-block-patterns' );
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\setup' );
/**
* Enqueue styles.
*
* @since Kindling 4.0.0
*
* @return void
*/
function enqueue_styles() {
wp_enqueue_style(
sanitize_title( __NAMESPACE__ ),
get_parent_theme_file_uri( 'style.css' ),
[],
filemtime( get_parent_theme_file_path( 'style.css' ) )
);
wp_enqueue_style(
'kindling/styles',
get_theme_file_uri( 'assets/css/styles.css' ),
[],
filemtime( get_theme_file_path( 'assets/css/styles.css' ) )
);
}
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\enqueue_styles' );
/**
* Registers block patterns for the theme.
*
* @since 4.0.0
*
* @return void
*/
function register_theme_patterns() {
// Check if register_block_pattern exists (WordPress 5.5+).
if ( ! function_exists( 'register_block_pattern' ) ) {
return;
}
$patterns_dir = get_stylesheet_directory() . '/patterns/';
if ( ! is_dir( $patterns_dir ) ) {
return;
}
$pattern_files = glob( $patterns_dir . '*.php' );
if ( ! $pattern_files ) {
return;
}
foreach ( $pattern_files as $file ) {
$raw_content = file_get_contents( $file );
if ( ! $raw_content ) {
continue;
}
// Extract the doc block header lines and content.
$header = [];
$content = '';
// Split file lines.
$lines = preg_split( '/\r\n|\r|\n/', $raw_content );
// We'll parse from the start until we hit the first non-comment line or closing PHP tag.
$in_header = false;
foreach ( $lines as $line ) {
// Trim whitespace.
$trimmed = trim( $line );
// Detect start of header doc block: `/**` or `/*`
if ( preg_match( '/^\/\*\*/', $trimmed ) ) {
$in_header = true;
continue;
}
// Detect end of doc block: `*/`
if ( preg_match( '/\*\//', $trimmed ) ) {
$in_header = false;
continue;
}
if ( $in_header ) {
// Example line: ` * Title: My Pattern`.
// Remove leading ` *`.
$line_str = preg_replace( '/^(\s*\*\s?)/', '', $trimmed );
// Now see if it matches "Key: Value".
if ( strpos( $line_str, ':' ) !== false ) {
$parts = explode( ':', $line_str, 2 );
$key = sanitize_text_field( trim( $parts[0] ) ); // e.g. "Title".
$val = sanitize_text_field( trim( $parts[1] ) ); // e.g. "My Pattern".
$header[ strtolower( $key ) ] = $val;
}
} else {
// Once doc block is finished, the rest is content.
$content .= $line . "\n";
}
}
// Map doc block keys to pattern registration array
// e.g. Title => 'title', Slug => 'slug', Categories => 'categories'.
$pattern_args = [
'title' => $header['title'] ?? null,
'slug' => $header['slug'] ?? null,
'categories' => [],
'content' => '',
'blockTypes' => [],
'postTypes' => [],
'inserter' => true,
];
// If "Categories: cat1, cat2", convert to array.
if ( ! empty( $header['categories'] ) ) {
// E.g. "kindling/media, kindling/featured, kindling/gallery"
$cats = array_map( 'trim', explode( ',', $header['categories'] ) );
$pattern_args['categories'] = $cats;
}
// If "Block Types: core/paragraph, core/quote", etc.
if ( ! empty( $header['block types'] ) ) {
$blocks = array_map( 'trim', explode( ',', $header['block types'] ) );
$pattern_args['blockTypes'] = $blocks;
}
// If "Post Types: post, page".
if ( ! empty( $header['post types'] ) ) {
$posts = array_map( 'trim', explode( ',', $header['post types'] ) );
$pattern_args['postTypes'] = $posts;
}
// If "Inserter: false" or "Inserter: true".
if ( isset( $header['inserter'] ) ) {
$pattern_args['inserter'] = ( 'true' === strtolower( $header['inserter'] ) );
}
// 'Slug' should follow the format 'namespace/pattern-slug' like 'kindling/instagram-grid'.
$name = $pattern_args['slug'] ? sanitize_title( $pattern_args['slug'] ) : null;
// Bail if we don't have a name or title.
if ( ! $pattern_args['title'] || ! $name ) {
continue;
}
// Now that $content is fully built, remove any stray PHP tags:
$content = str_replace( '<?php', '', $content );
$content = str_replace( '?>', '', $content );
// The remaining file content is block markup.
$pattern_args['content'] = $content;
// Use 'Slug: namespace-pattern-slug' as 'name' => 'namespace/pattern-slug' or extract a prefix.
$pattern_name = ( false === strpos( $name, '/' ) )
? 'theme/' . $name // fallback.
: $name;
// Register the pattern.
register_block_pattern(
$pattern_name,
[
'title' => $pattern_args['title'],
'categories' => $pattern_args['categories'],
'blockTypes' => $pattern_args['blockTypes'],
'postTypes' => $pattern_args['postTypes'],
'inserter' => $pattern_args['inserter'],
'content' => $pattern_args['content'],
]
);
}
}
add_action( 'init', __NAMESPACE__ . '\register_theme_patterns' );
/**
* Enqueue block styles.
*
* @since Kindling 4.0.0
*
* @return void
*/
function enqueue_block_styles() {
wp_enqueue_block_style(
'kindling/grid-item',
[
'handle' => 'kindling-block-grid-item',
'src' => get_theme_file_uri( 'assets/css/block-styles/kindling-grid-system.css' ),
'path' => get_theme_file_path( 'assets/css/block-styles/kindling-grid-system.css' ),
]
);
}
add_action( 'init', __NAMESPACE__ . '\enqueue_block_styles' );