| 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_get_vars_admin() { |
| 74 |
|
| 75 |
$data = array( |
| 76 |
|
| 77 |
'reset_title' => __('Confirm Reset', 'prismatic'), |
| 78 |
'reset_message' => __('Restore default options?', 'prismatic'), |
| 79 |
'reset_true' => __('Yes, make it so.', 'prismatic'), |
| 80 |
'reset_false' => __('No, abort mission.', 'prismatic'), |
| 81 |
|
| 82 |
); |
| 83 |
|
| 84 |
return $data; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
function prismatic_prism_enqueue() { |
| 89 |
|
| 90 |
global $prismatic_options_prism; |
| 91 |
|
| 92 |
if (isset($prismatic_options_prism['singular_only']) && $prismatic_options_prism['singular_only'] && !is_singular() && !is_admin()) return; |
| 93 |
|
| 94 |
$languages = prismatic_active_languages('prism'); |
| 95 |
|
| 96 |
$languages = array_filter($languages); |
| 97 |
|
| 98 |
if (!empty($languages)) { |
| 99 |
|
| 100 |
$theme = (isset($prismatic_options_prism['prism_theme'])) ? $prismatic_options_prism['prism_theme'] : 'default'; |
| 101 |
|
| 102 |
wp_enqueue_style('prismatic-prism', PRISMATIC_URL .'lib/prism/css/theme-'. $theme .'.css', array(), null, 'all'); |
| 103 |
|
| 104 |
wp_enqueue_script('prismatic-prism', PRISMATIC_URL .'lib/prism/js/prism-core.js', array(), null, true); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if (isset($prismatic_options_prism['line_highlight']) && $prismatic_options_prism['line_highlight']) { |
| 109 |
|
| 110 |
wp_enqueue_script('prismatic-prism-line-highlight', PRISMATIC_URL .'lib/prism/js/plugin-line-highlight.js', array('prismatic-prism'), null, true); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
if (isset($prismatic_options_prism['line_numbers']) && $prismatic_options_prism['line_numbers']) { |
| 115 |
|
| 116 |
wp_enqueue_script('prismatic-prism-line-numbers', PRISMATIC_URL .'lib/prism/js/plugin-line-numbers.js', array('prismatic-prism'), null, true); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
if (isset($prismatic_options_prism['show_language']) && $prismatic_options_prism['show_language']) { |
| 121 |
|
| 122 |
wp_enqueue_script('prismatic-prism-show-language', PRISMATIC_URL .'lib/prism/js/plugin-show-language.js', array('prismatic-prism'), null, true); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
$prefix = array('lang-', 'language-'); |
| 129 |
|
| 130 |
foreach ($languages as $language) { |
| 131 |
|
| 132 |
$language = str_replace($prefix, '', $language); |
| 133 |
|
| 134 |
$file = PRISMATIC_DIR . 'lib/prism/js/lang-'. $language .'.js'; |
| 135 |
|
| 136 |
if (file_exists($file)) { |
| 137 |
|
| 138 |
wp_enqueue_script('prismatic-prism-'. $language, PRISMATIC_URL .'lib/prism/js/lang-'. $language .'.js', array('prismatic-prism'), null, true); |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
if (is_admin()) { |
| 146 |
|
| 147 |
// todo: once gutenberg is further developed, find a better way to add editor support |
| 148 |
|
| 149 |
function prismatic_prism_inline_js() { |
| 150 |
|
| 151 |
?> |
| 152 |
|
| 153 |
<script type="text/javascript"> |
| 154 |
document.onreadystatechange = function () { |
| 155 |
if (document.readyState == 'complete') { |
| 156 |
Prism.highlightAll(); |
| 157 |
} |
| 158 |
} |
| 159 |
</script> |
| 160 |
|
| 161 |
<?php |
| 162 |
|
| 163 |
} |
| 164 |
add_action('admin_print_footer_scripts', 'prismatic_prism_inline_js'); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
function prismatic_highlight_enqueue() { |
| 173 |
|
| 174 |
global $prismatic_options_highlight; |
| 175 |
|
| 176 |
if (isset($prismatic_options_highlight['singular_only']) && $prismatic_options_highlight['singular_only'] && !is_singular() && !is_admin()) return; |
| 177 |
|
| 178 |
$always_load = (isset($prismatic_options_highlight['noprefix_classes']) && $prismatic_options_highlight['noprefix_classes']) ? true : false; |
| 179 |
|
| 180 |
$languages = prismatic_active_languages('highlight'); |
| 181 |
|
| 182 |
$languages = array_filter($languages); |
| 183 |
|
| 184 |
if (!empty($languages) || $always_load) { |
| 185 |
|
| 186 |
$theme = (isset($prismatic_options_highlight['highlight_theme'])) ? $prismatic_options_highlight['highlight_theme'] : 'default'; |
| 187 |
|
| 188 |
wp_enqueue_style('prismatic-highlight', PRISMATIC_URL .'lib/highlight/css/'. $theme .'.css', array(), null, 'all'); |
| 189 |
|
| 190 |
wp_enqueue_script('prismatic-highlight', PRISMATIC_URL .'lib/highlight/js/highlight-core.js', array(), null, true); |
| 191 |
|
| 192 |
$init = (isset($prismatic_options_highlight['init_javascript'])) ? $prismatic_options_highlight['init_javascript'] : ''; |
| 193 |
|
| 194 |
if ($init) { |
| 195 |
|
| 196 |
wp_add_inline_script('prismatic-highlight', $init, 'after'); |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
if (is_admin()) { |
| 201 |
|
| 202 |
// todo: once gutenberg is further developed, find a better way to add editor support |
| 203 |
|
| 204 |
function prismatic_highlight_inline_js() { |
| 205 |
|
| 206 |
?> |
| 207 |
|
| 208 |
<script type="text/javascript"> |
| 209 |
document.onreadystatechange = function () { |
| 210 |
if (document.readyState == 'complete') { |
| 211 |
jQuery('pre > code').each(function() { |
| 212 |
hljs.highlightBlock(this); |
| 213 |
}); |
| 214 |
} |
| 215 |
} |
| 216 |
</script> |
| 217 |
|
| 218 |
<?php |
| 219 |
|
| 220 |
} |
| 221 |
add_action('admin_print_footer_scripts', 'prismatic_highlight_inline_js'); |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
function prismatic_active_languages($library) { |
| 230 |
|
| 231 |
global $posts, $post; |
| 232 |
|
| 233 |
$languages = array(); |
| 234 |
|
| 235 |
if (is_admin()) { |
| 236 |
|
| 237 |
$content = $post->post_content; |
| 238 |
|
| 239 |
$languages = prismatic_active_languages_loop($library, '', $content, array()); |
| 240 |
|
| 241 |
} else { |
| 242 |
|
| 243 |
if (is_singular()) { |
| 244 |
|
| 245 |
$excerpt = $post->post_excerpt; |
| 246 |
|
| 247 |
$content = $post->post_content; |
| 248 |
|
| 249 |
$comments = ($post->comment_count) ? get_comments(array('post_id' => $post->ID, 'status' => 'approve')) : array(); |
| 250 |
|
| 251 |
$languages = prismatic_active_languages_loop($library, $excerpt, $content, $comments); |
| 252 |
|
| 253 |
} else { |
| 254 |
|
| 255 |
foreach ($posts as $post) { |
| 256 |
|
| 257 |
$excerpt = $post->post_excerpt; |
| 258 |
|
| 259 |
$content = $post->post_content; |
| 260 |
|
| 261 |
$comments = array(); |
| 262 |
|
| 263 |
$langs_array[] = prismatic_active_languages_loop($library, $excerpt, $content, $comments); |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
if (!empty($langs_array) && is_array($langs_array)) { |
| 268 |
|
| 269 |
foreach($langs_array as $key => $value) { |
| 270 |
|
| 271 |
foreach ($value as $k => $v) { |
| 272 |
|
| 273 |
$languages[] = $v; |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
return $languages; |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
function prismatic_active_languages_loop($library, $excerpt, $content, $comments) { |
| 290 |
|
| 291 |
$languages = array(); |
| 292 |
|
| 293 |
$classes = ($library === 'prism') ? prismatic_prism_classes() : prismatic_highlight_classes(); |
| 294 |
|
| 295 |
foreach ($classes as $class) { |
| 296 |
|
| 297 |
foreach($class as $cls) { |
| 298 |
|
| 299 |
if (strpos($excerpt, $cls) !== false) { |
| 300 |
|
| 301 |
$languages[] = $cls; |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
if (strpos($content, $cls) !== false) { |
| 306 |
|
| 307 |
$languages[] = $cls; |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
foreach ($comments as $comment) { |
| 312 |
|
| 313 |
if (strpos($comment->comment_content, $cls) !== false) { |
| 314 |
|
| 315 |
$languages[] = $cls; |
| 316 |
|
| 317 |
} |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
} |
| 324 |
|
| 325 |
$languages = array_unique($languages); |
| 326 |
|
| 327 |
return $languages; |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
function prismatic_prism_classes() { |
| 332 |
|
| 333 |
$classes = array( |
| 334 |
|
| 335 |
array( |
| 336 |
'language-apacheconf', |
| 337 |
'language-applescript', |
| 338 |
'language-bash', |
| 339 |
'language-c', |
| 340 |
'language-csharp', |
| 341 |
'language-cpp', |
| 342 |
'language-clike', |
| 343 |
'language-coffeescript', |
| 344 |
'language-css', |
| 345 |
'language-diff', |
| 346 |
'language-git', |
| 347 |
'language-go', |
| 348 |
'language-graphql', |
| 349 |
'language-groovy', |
| 350 |
'language-http', |
| 351 |
'language-ini', |
| 352 |
'language-java', |
| 353 |
'language-javascript', |
| 354 |
'language-json', |
| 355 |
'language-kotlin', |
| 356 |
'language-latex', |
| 357 |
'language-lua', |
| 358 |
'language-makefile', |
| 359 |
'language-markdown', |
| 360 |
'language-markup', |
| 361 |
'language-nginx', |
| 362 |
'language-objectivec', |
| 363 |
'language-pascal', |
| 364 |
'language-perl', |
| 365 |
'language-php', |
| 366 |
'language-powershell', |
| 367 |
'language-python', |
| 368 |
'language-ruby', |
| 369 |
'language-sass', |
| 370 |
'language-scss', |
| 371 |
'language-sql', |
| 372 |
'language-swift', |
| 373 |
'language-twig', |
| 374 |
'language-typescript', |
| 375 |
'language-visual-basic', |
| 376 |
'language-yaml', |
| 377 |
), |
| 378 |
|
| 379 |
array( |
| 380 |
'lang-apacheconf', |
| 381 |
'lang-applescript', |
| 382 |
'lang-bash', |
| 383 |
'lang-c', |
| 384 |
'lang-csharp', |
| 385 |
'lang-cpp', |
| 386 |
'lang-clike', |
| 387 |
'lang-coffeescript', |
| 388 |
'lang-css', |
| 389 |
'lang-diff', |
| 390 |
'lang-git', |
| 391 |
'lang-go', |
| 392 |
'lang-graphql', |
| 393 |
'lang-groovy', |
| 394 |
'lang-http', |
| 395 |
'lang-ini', |
| 396 |
'lang-java', |
| 397 |
'lang-javascript', |
| 398 |
'lang-json', |
| 399 |
'lang-kotlin', |
| 400 |
'lang-latex', |
| 401 |
'lang-lua', |
| 402 |
'lang-makefile', |
| 403 |
'lang-markdown', |
| 404 |
'lang-markup', |
| 405 |
'lang-nginx', |
| 406 |
'lang-objectivec', |
| 407 |
'lang-pascal', |
| 408 |
'lang-perl', |
| 409 |
'lang-php', |
| 410 |
'lang-powershell', |
| 411 |
'lang-python', |
| 412 |
'lang-ruby', |
| 413 |
'lang-sass', |
| 414 |
'lang-scss', |
| 415 |
'lang-sql', |
| 416 |
'lang-swift', |
| 417 |
'lang-twig', |
| 418 |
'lang-typescript', |
| 419 |
'lang-visual-basic', |
| 420 |
'lang-yaml', |
| 421 |
) |
| 422 |
|
| 423 |
); |
| 424 |
|
| 425 |
return $classes; |
| 426 |
|
| 427 |
} |
| 428 |
|
| 429 |
function prismatic_highlight_classes() { |
| 430 |
|
| 431 |
$classes = array( |
| 432 |
|
| 433 |
array( |
| 434 |
'language-' |
| 435 |
), |
| 436 |
|
| 437 |
array( |
| 438 |
'lang-', |
| 439 |
) |
| 440 |
|
| 441 |
); |
| 442 |
|
| 443 |
return $classes; |
| 444 |
|
| 445 |
} |
| 446 |
|