This repository has been archived by the owner on Nov 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.php
222 lines (194 loc) · 5.89 KB
/
index.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
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
<?php
/**
* Plugin Name: Code Syntax Block
* Plugin URI: https://github.com/mkaz/code-syntax-block
* Description: A plugin to extend Gutenberg code block with syntax highlighting
* Version: 3.2.1
* Author: Marcus Kazmierczak
* Author URI: https://mkaz.blog/
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: code-syntax-block
*
* @package Code_Syntax_Block
*/
// version added, used in URL
const MKAZ_CODE_SYNTAX_BLOCK_VERSION = '3.2.1';
const MKAZ_CODE_SYNTAX_DEFAULT_SCHEME = 'prism-a11y-dark';
const MKAZ_CODE_SYNTAX_COLOR_SCHEMES = array(
'prism-a11y-dark' => 'A11y Dark',
'prism-ghcolors' => 'GitHub (Light)',
'prism-nord' => 'Nord',
'prism-onedark' => 'One Dark'
);
require dirname( __FILE__ ) . '/prism-languages.php';
require dirname( __FILE__ ) . '/settings.php';
/**
* Enqueue assets for editor portion of Gutenberg
*/
add_action( 'enqueue_block_editor_assets', function() {
$asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php' );
$editor_style_path = 'assets/blocks.editor.css';
// Prism Languages - write out as JavaScript array that makes
// it available to show in the editor
$languages = mkaz_code_syntax_block_get_supported_languages();
// Block.
wp_enqueue_script(
'mkaz-code-syntax',
plugins_url( 'build/index.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version']
);
// Enqueue view style.
wp_enqueue_style(
'mkaz-code-syntax-editor-css',
plugins_url( $editor_style_path, __FILE__ ),
[],
filemtime( plugin_dir_path( __FILE__ ) . $editor_style_path )
);
$default_lang = get_option( 'mkaz-code-syntax-default-lang', '' );
wp_add_inline_script(
'mkaz-code-syntax',
implode(
"\n",
array(
'const mkaz_code_syntax_languages = ' . json_encode( $languages ) . ';',
'const mkaz_code_syntax_default_lang = ' . json_encode( $default_lang ) . ';',
)
),
'before'
);
} );
/**
* Enqueue assets for frontend, not editor.
*/
add_action( 'wp_enqueue_scripts', function() {
global $posts;
/**
* Filter forces loading assets even if no block detected.
*
* @since 1.2.4
*
*/
$force_load = apply_filters( 'mkaz_code_syntax_force_loading', false );
// if not forcing the loading of assets check if the block
// is found and if no block skip loading assets
if ( ! $force_load ) {
if ( empty( $posts ) ) {
return;
}
$found_block = array_reduce( $posts, function($found, $post) {
return $found || has_block( 'code', $post );
}, false );
if ( ! $found_block ) {
return;
}
}
// Files.
$prism_js_path = 'assets/prism/prism.js';
$prism_settings_path = 'assets/prism/prism-settings.js';
// Enqueue prism style.
wp_enqueue_style(
'mkaz-code-syntax-prism-css',
mkaz_prism_theme_css(),
[],
mkaz_prism_theme_css_ver()
);
// Enqueue prism script.
wp_enqueue_script(
'mkaz-code-syntax-prism-js',
plugins_url( $prism_js_path, __FILE__ ),
[], // No dependencies.
filemtime( plugin_dir_path( __FILE__ ) . $prism_js_path ),
true // In footer.
);
// save the plugin path
wp_localize_script('mkaz-code-syntax-prism-js', 'prism_settings', array(
'pluginUrl' => plugin_dir_url(__FILE__),
));
} );
/**
* Locate a given resource URL in the active theme or with the default.
*
* @param boolean $rtnPath True returns path, default false returns URL
*/
function mkaz_prism_theme_css( $rtnPath = false ) {
$default_path = '/assets/prism-a11y-dark.css';
/**
* Site option overrides theme, because the site option can be set by the user.
* However, we will want the theme to be able to override the default.
* @since 2.0.0
*/
$option = get_option( 'mkaz-code-syntax-color-scheme', 'prism-a11y-dark' );
// confirm file exists
if ( $option ) {
$option_rel_path = '/assets/' . $option . '.css';
$option_file_path = plugin_dir_path( __FILE__ ) . $option_rel_path;
if ( file_exists( $option_file_path ) ) {
$default_path = $option_rel_path;
}
}
/**
* Filter the theme directory path used for overriding css path
*
* @fix 2.0.3 - Hardcode value to assets/prism/prism.css instead of default
* In 2.0.0 the default path was changed to a11y and support site option,
* but previous themes and docs instruct to look for this path.
*
* @since 0.8.2
*
* @param string $path Path to the file to override, relative to the theme
*/
$css_rel_path = apply_filters( 'mkaz_prism_css_path', "/assets/prism/prism.css" );
$theme_file_path = get_stylesheet_directory() . $css_rel_path;
if ( file_exists( $theme_file_path ) ) {
$prism_css_path = $theme_file_path;
$prism_css_url = get_stylesheet_directory_uri() . $css_rel_path;
}
else {
$prism_css_path = plugin_dir_path( __FILE__ ) . $default_path;
$prism_css_url = plugins_url( $default_path, __FILE__ );
}
if ( $rtnPath ) {
return $prism_css_path;
}
/**
* Filter the URL of the Syntax Highlighting colors.
* Use this filter to define your own color set.
*
* @since 0.8.1
*
* @param string $prism_css_url Absolute URL of the default CSS file you want to enqueue.
*/
return apply_filters( 'mkaz_prism_css_url', $prism_css_url );
}
/**
* Return version or timestamp for theme_css to be used in enqueue version,
* otherwise plugin version used for cache busting CSS enqueue.
*
* @return string
*
*/
function mkaz_prism_theme_css_ver() {
// Check if full url is being used, if so return version.
$prism_css_url = apply_filters( 'mkaz_prism_css_url', false );
if ( $prism_css_url ) {
return MKAZ_CODE_SYNTAX_BLOCK_VERSION;
}
$css_path = mkaz_prism_theme_css( true );
if ( file_exists( $css_path ) ) {
return filemtime( $css_path );
}
return MKAZ_CODE_SYNTAX_BLOCK_VERSION;
}
// extend code tag to allow lang attribute
add_filter( 'wp_kses_allowed_html', function( $tags ) {
if ( isset( $tags['code'] ) && is_array( $tags['code'] ) ) {
$tags['code']['lang'] = array();
} else {
$tags['code'] = array(
'lang' => array(),
);
}
return $tags;
}, 10, 2);