-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
158 lines (140 loc) · 4.43 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
<?php
/**
* TumblrThemeGarden theme functions.
*
* @package TumblrThemeGarden
*/
defined( 'ABSPATH' ) || exit;
use CupcakeLabs\TumblrThemeGarden\Plugin;
/**
* Returns the plugin's main class instance.
*
* @since 1.0.0
* @version 1.0.0
*
* @return Plugin
*/
function ttgarden_get_plugin_instance(): Plugin {
return Plugin::get_instance();
}
/**
* Returns the plugin's slug.
*
* @since 1.0.0
* @version 1.0.0
*
* @return string
*/
function ttgarden_get_plugin_slug(): string {
return sanitize_key( TTGARDEN_METADATA['TextDomain'] );
}
/**
* Returns an array with meta information for a given asset path. First, it checks for an .asset.php file in the same directory
* as the given asset file whose contents are returns if it exists. If not, it returns an array with the file's last modified
* time as the version and the main stylesheet + any extra dependencies passed in as the dependencies.
*
* @since 1.0.0
* @version 1.0.0
*
* @param string $asset_path The path to the asset file.
* @param array|null $extra_dependencies Any extra dependencies to include in the returned meta.
*
* @return array|null
*/
function ttgarden_get_asset_meta( string $asset_path, ?array $extra_dependencies = null ): ?array {
if ( ! file_exists( $asset_path ) || ! str_starts_with( $asset_path, TTGARDEN_PATH ) ) {
return null;
}
$asset_path_info = pathinfo( $asset_path );
if ( file_exists( $asset_path_info['dirname'] . '/' . $asset_path_info['filename'] . '.php' ) ) {
$asset_meta = require $asset_path_info['dirname'] . '/' . $asset_path_info['filename'] . '.php';
$asset_meta += array( 'dependencies' => array() ); // Ensure 'dependencies' key exists.
} else {
$asset_meta = array(
'dependencies' => array(),
'version' => filemtime( $asset_path ),
);
if ( false === $asset_meta['version'] ) { // Safeguard against filemtime() returning false.
$asset_meta['version'] = TTGARDEN_METADATA['Version'];
}
}
if ( is_array( $extra_dependencies ) ) {
$asset_meta['dependencies'] = array_merge( $asset_meta['dependencies'], $extra_dependencies );
$asset_meta['dependencies'] = array_unique( $asset_meta['dependencies'] );
}
return $asset_meta;
}
/**
* We need a custom do_shortcode implementation because do_shortcodes_in_html_tags()
* is run before running reguular shortcodes, which means that things like link hrefs
* get populated before they even have context.
*
* @param string $content The content to parse.
*
* @return string The parsed content.
*/
function ttgarden_do_shortcode( $content ): string {
global $shortcode_tags;
static $pattern = null;
// Avoid generating this multiple times.
if ( null === $pattern ) {
$pattern = get_shortcode_regex( array_keys( $shortcode_tags ) );
}
$content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content );
// Always restore square braces so we don't break things like <!--[if IE ]>.
$content = unescape_invalid_shortcodes( $content );
return $content;
}
/**
* Gets the current parse context.
* Used for informing data tags of their context.
* Also used for storing data to pass between tags.
*
* @return array|null|string The current parse context.
*/
function ttgarden_get_parse_context() {
global $ttgarden_parse_context;
return $ttgarden_parse_context;
}
/**
* Sets the global parse context.
*
* @param string $key The key to set.
* @param mixed $value The value to set.
*
* @return void
*/
function ttgarden_set_parse_context( $key, $value ): void {
global $ttgarden_parse_context;
$ttgarden_parse_context = array( $key => $value );
}
/**
* Normalizes a theme option name.
*
* @param string $name The name to normalize.
*
* @return string The normalized name.
*/
function ttgarden_normalize_option_name( $name ): string {
return strtolower(
str_replace(
array_merge(
array( ' ' ),
TTGARDEN_OPTIONS
),
'',
$name
)
);
}
/**
* Gets the Tumblr Theme Garden regex.
*
* @return string The Tumblr Theme Garden regex.
*/
function ttgarden_get_tumblr_regex(): string {
return '/\{([a-zA-Z0-9][a-zA-Z0-9\\-\/=" ]*|font\:[a-zA-Z0-9 ]+|text\:[a-zA-Z0-9 ]+|select\:[a-zA-Z0-9 ]+|image\:[a-zA-Z0-9 ]+|color\:[a-zA-Z0-9 ]+|RGBcolor\:[a-zA-Z0-9 ]+|lang\:[a-zA-Z0-9- ]+|[\/]?block\:[a-zA-Z0-9]+( [a-zA-Z0-9=" ]+)*)\}/i';
}
// Include tag and block hydration functions for each Tumblr Theme tag|block.
require TTGARDEN_PATH . 'includes/block-functions.php';
require TTGARDEN_PATH . 'includes/tag-functions.php';