PluginProbe
Prismatic / 2.2
Prismatic v2.2
trunk 1.3 1.4 1.5 1.6 1.6.1 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.9.1 3.0 3.1 3.1.1 3.2 3.2.1 All 40 releases
prismatic / inc / resources-enqueue.php

resources-enqueue.php in Prismatic 2.2, at inc/resources-enqueue.php

472 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // Prismatic - Enqueue Resources
2
3 if (!defined('ABSPATH')) exit;
4
5 function prismatic_enqueue() {
6
7 global $prismatic_options_general;
8
9 $library = (isset($prismatic_options_general['library'])) ? $prismatic_options_general['library'] : 'none';
10
11 if (is_admin()) {
12
13 $screen = get_current_screen();
14
15 if (!property_exists($screen, 'id')) return;
16
17 if ($screen->id === 'post' || $screen->id === 'page') {
18
19 if ($library === 'prism') {
20
21 prismatic_prism_enqueue();
22
23 } elseif ($library === 'highlight') {
24
25 prismatic_highlight_enqueue();
26
27 }
28
29 }
30
31 } else {
32
33 if ($library === 'prism') {
34
35 prismatic_prism_enqueue();
36
37 } elseif ($library === 'highlight') {
38
39 prismatic_highlight_enqueue();
40
41 }
42
43 }
44
45 }
46
47 function prismatic_enqueue_settings() {
48
49 $screen = get_current_screen();
50
51 if (!property_exists($screen, 'id')) return;
52
53 if ($screen->id === 'settings_page_prismatic') {
54
55 wp_enqueue_style('prismatic-font-icons', PRISMATIC_URL .'css/styles-font-icons.css', array(), null);
56
57 wp_enqueue_style('prismatic-settings', PRISMATIC_URL .'css/styles-settings.css', array(), null);
58
59 wp_enqueue_style('wp-jquery-ui-dialog');
60
61 $js_deps = array('jquery', 'jquery-ui-core', 'jquery-ui-dialog');
62
63 wp_enqueue_script('prismatic-settings', PRISMATIC_URL .'js/scripts-settings.js', $js_deps, null);
64
65 $data = prismatic_get_vars_admin();
66
67 wp_localize_script('prismatic-settings', 'prismatic_settings', $data);
68
69 }
70
71 }
72
73 function prismatic_enqueue_buttons() {
74
75 $screen = get_current_screen();
76
77 if (!property_exists($screen, 'id')) return;
78
79 if ($screen->id === 'post' || $screen->id === 'page') {
80
81 wp_enqueue_style('prismatic-buttons', PRISMATIC_URL .'css/styles-buttons.css', array(), null);
82
83 }
84
85 }
86
87 function prismatic_get_vars_admin() {
88
89 $data = array(
90
91 'reset_title' => __('Confirm Reset', 'prismatic'),
92 'reset_message' => __('Restore default options?', 'prismatic'),
93 'reset_true' => __('Yes, make it so.', 'prismatic'),
94 'reset_false' => __('No, abort mission.', 'prismatic'),
95
96 );
97
98 return $data;
99
100 }
101
102 function prismatic_prism_enqueue() {
103
104 global $prismatic_options_prism;
105
106 if (isset($prismatic_options_prism['singular_only']) && $prismatic_options_prism['singular_only'] && !is_singular() && !is_admin()) return;
107
108 $languages = prismatic_active_languages('prism');
109
110 $languages = array_filter($languages);
111
112 if (!empty($languages)) {
113
114 $theme = (isset($prismatic_options_prism['prism_theme'])) ? $prismatic_options_prism['prism_theme'] : 'default';
115
116 wp_enqueue_style('prismatic-prism', PRISMATIC_URL .'lib/prism/css/theme-'. $theme .'.css', array(), null, 'all');
117
118 wp_enqueue_script('prismatic-prism', PRISMATIC_URL .'lib/prism/js/prism-core.js', array(), null, true);
119
120
121
122 if (isset($prismatic_options_prism['line_highlight']) && $prismatic_options_prism['line_highlight']) {
123
124 wp_enqueue_script('prismatic-prism-line-highlight', PRISMATIC_URL .'lib/prism/js/plugin-line-highlight.js', array('prismatic-prism'), null, true);
125
126 }
127
128 if (isset($prismatic_options_prism['line_numbers']) && $prismatic_options_prism['line_numbers']) {
129
130 wp_enqueue_script('prismatic-prism-line-numbers', PRISMATIC_URL .'lib/prism/js/plugin-line-numbers.js', array('prismatic-prism'), null, true);
131
132 }
133
134 if (isset($prismatic_options_prism['show_language']) && $prismatic_options_prism['show_language']) {
135
136 wp_enqueue_script('prismatic-prism-show-language', PRISMATIC_URL .'lib/prism/js/plugin-show-language.js', array('prismatic-prism'), null, true);
137
138 }
139
140
141
142 $prefix = array('lang-', 'language-');
143
144 foreach ($languages as $language) {
145
146 $language = str_replace($prefix, '', $language);
147
148 $file = PRISMATIC_DIR . 'lib/prism/js/lang-'. $language .'.js';
149
150 if (file_exists($file)) {
151
152 wp_enqueue_script('prismatic-prism-'. $language, PRISMATIC_URL .'lib/prism/js/lang-'. $language .'.js', array('prismatic-prism'), null, true);
153
154 }
155
156 }
157
158
159 if (is_admin()) {
160
161 // todo: once gutenberg is further developed, find a better way to add editor support
162
163 function prismatic_prism_inline_js() {
164
165 ?>
166
167 <script type="text/javascript">
168 document.onreadystatechange = function () {
169 if (document.readyState == 'complete') {
170 Prism.highlightAll();
171 }
172 }
173 </script>
174
175 <?php
176
177 }
178 add_action('admin_print_footer_scripts', 'prismatic_prism_inline_js');
179
180 }
181
182 }
183
184 }
185
186 function prismatic_highlight_enqueue() {
187
188 global $prismatic_options_highlight;
189
190 if (isset($prismatic_options_highlight['singular_only']) && $prismatic_options_highlight['singular_only'] && !is_singular() && !is_admin()) return;
191
192 $always_load = (isset($prismatic_options_highlight['noprefix_classes']) && $prismatic_options_highlight['noprefix_classes']) ? true : false;
193
194 $languages = prismatic_active_languages('highlight');
195
196 $languages = array_filter($languages);
197
198 if (!empty($languages) || $always_load) {
199
200 $theme = (isset($prismatic_options_highlight['highlight_theme'])) ? $prismatic_options_highlight['highlight_theme'] : 'default';
201
202 wp_enqueue_style('prismatic-highlight', PRISMATIC_URL .'lib/highlight/css/'. $theme .'.css', array(), null, 'all');
203
204 wp_enqueue_script('prismatic-highlight', PRISMATIC_URL .'lib/highlight/js/highlight-core.js', array(), null, true);
205
206 $init = (isset($prismatic_options_highlight['init_javascript'])) ? $prismatic_options_highlight['init_javascript'] : '';
207
208 if ($init) {
209
210 wp_add_inline_script('prismatic-highlight', $init, 'after');
211
212 }
213
214 if (is_admin()) {
215
216 // todo: once gutenberg is further developed, find a better way to add editor support
217
218 function prismatic_highlight_inline_js() {
219
220 ?>
221
222 <script type="text/javascript">
223 document.onreadystatechange = function () {
224 if (document.readyState == 'complete') {
225 jQuery('pre > code').each(function() {
226 hljs.highlightBlock(this);
227 });
228 }
229 }
230 </script>
231
232 <?php
233
234 }
235 add_action('admin_print_footer_scripts', 'prismatic_highlight_inline_js');
236
237 }
238
239 }
240
241 }
242
243 function prismatic_active_languages($library) {
244
245 global $posts, $post;
246
247 $languages = array();
248
249 if (is_admin()) {
250
251 $content = $post->post_content;
252
253 $languages = prismatic_active_languages_loop($library, '', $content, array());
254
255 } else {
256
257 if (is_singular()) {
258
259 $excerpt = $post->post_excerpt;
260
261 $content = $post->post_content;
262
263 $comments = ($post->comment_count) ? get_comments(array('post_id' => $post->ID, 'status' => 'approve')) : array();
264
265 $languages = prismatic_active_languages_loop($library, $excerpt, $content, $comments);
266
267 } else {
268
269 foreach ($posts as $post) {
270
271 $excerpt = $post->post_excerpt;
272
273 $content = $post->post_content;
274
275 $comments = array();
276
277 $langs_array[] = prismatic_active_languages_loop($library, $excerpt, $content, $comments);
278
279 }
280
281 if (!empty($langs_array) && is_array($langs_array)) {
282
283 foreach($langs_array as $key => $value) {
284
285 foreach ($value as $k => $v) {
286
287 $languages[] = $v;
288
289 }
290
291 }
292
293 }
294
295 }
296
297 }
298
299 return $languages;
300
301 }
302
303 function prismatic_active_languages_loop($library, $excerpt, $content, $comments) {
304
305 $languages = array();
306
307 $classes = ($library === 'prism') ? prismatic_prism_classes() : prismatic_highlight_classes();
308
309 foreach ($classes as $class) {
310
311 foreach($class as $cls) {
312
313 if (strpos($excerpt, $cls) !== false) {
314
315 $languages[] = $cls;
316
317 }
318
319 if (strpos($content, $cls) !== false) {
320
321 $languages[] = $cls;
322
323 }
324
325 foreach ($comments as $comment) {
326
327 if (strpos($comment->comment_content, $cls) !== false) {
328
329 $languages[] = $cls;
330
331 }
332
333 }
334
335 }
336
337 }
338
339 $languages = array_unique($languages);
340
341 return $languages;
342
343 }
344
345 function prismatic_prism_classes() {
346
347 $classes = array(
348
349 array(
350 'language-apacheconf',
351 'language-applescript',
352 'language-arduino',
353 'language-bash',
354 'language-c',
355 'language-csharp',
356 'language-cpp',
357 'language-clike',
358 'language-coffeescript',
359 'language-css',
360 'language-d',
361 'language-dart',
362 'language-diff',
363 'language-git',
364 'language-go',
365 'language-graphql',
366 'language-groovy',
367 'language-http',
368 'language-ini',
369 'language-java',
370 'language-javascript',
371 'language-json',
372 'language-jsx',
373 'language-kotlin',
374 'language-latex',
375 'language-lua',
376 'language-makefile',
377 'language-markdown',
378 'language-markup',
379 'language-nginx',
380 'language-objectivec',
381 'language-pascal',
382 'language-perl',
383 'language-php',
384 'language-powershell',
385 'language-python',
386 'language-ruby',
387 'language-sass',
388 'language-scala',
389 'language-scss',
390 'language-sql',
391 'language-swift',
392 'language-tsx',
393 'language-twig',
394 'language-typescript',
395 'language-visual-basic',
396 'language-yaml',
397 ),
398
399 array(
400 'lang-apacheconf',
401 'lang-applescript',
402 'lang-arduino',
403 'lang-bash',
404 'lang-c',
405 'lang-csharp',
406 'lang-cpp',
407 'lang-clike',
408 'lang-coffeescript',
409 'lang-css',
410 'lang-d',
411 'lang-dart',
412 'lang-diff',
413 'lang-git',
414 'lang-go',
415 'lang-graphql',
416 'lang-groovy',
417 'lang-http',
418 'lang-ini',
419 'lang-java',
420 'lang-javascript',
421 'lang-json',
422 'lang-jsx',
423 'lang-kotlin',
424 'lang-latex',
425 'lang-lua',
426 'lang-makefile',
427 'lang-markdown',
428 'lang-markup',
429 'lang-nginx',
430 'lang-objectivec',
431 'lang-pascal',
432 'lang-perl',
433 'lang-php',
434 'lang-powershell',
435 'lang-python',
436 'lang-ruby',
437 'lang-sass',
438 'lang-scala',
439 'lang-scss',
440 'lang-sql',
441 'lang-swift',
442 'lang-tsx',
443 'lang-twig',
444 'lang-typescript',
445 'lang-visual-basic',
446 'lang-yaml',
447 )
448
449 );
450
451 return $classes;
452
453 }
454
455 function prismatic_highlight_classes() {
456
457 $classes = array(
458
459 array(
460 'language-'
461 ),
462
463 array(
464 'lang-',
465 )
466
467 );
468
469 return $classes;
470
471 }
472