2016-10-24 13:20:42 -04:00
|
|
|
<?php // Prismatic - Enqueue Resources
|
|
|
|
|
|
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
function prismatic_enqueue() {
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
global $prismatic_options_general;
|
|
|
|
|
|
|
|
$library = (isset($prismatic_options_general['library'])) ? $prismatic_options_general['library'] : 'none';
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
if (is_admin()) {
|
2016-10-24 13:20:42 -04:00
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
$screen = get_current_screen();
|
2016-10-24 13:20:42 -04:00
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
if (!property_exists($screen, 'id')) return;
|
|
|
|
|
|
|
|
if ($screen->id === 'post' || $screen->id === 'page') {
|
|
|
|
|
|
|
|
if ($library === 'prism') {
|
|
|
|
|
|
|
|
prismatic_prism_enqueue();
|
|
|
|
|
|
|
|
} elseif ($library === 'highlight') {
|
|
|
|
|
|
|
|
prismatic_highlight_enqueue();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if ($library === 'prism') {
|
|
|
|
|
|
|
|
prismatic_prism_enqueue();
|
|
|
|
|
|
|
|
} elseif ($library === 'highlight') {
|
|
|
|
|
|
|
|
prismatic_highlight_enqueue();
|
|
|
|
|
|
|
|
}
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
function prismatic_enqueue_settings() {
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
$screen = get_current_screen();
|
|
|
|
|
|
|
|
if (!property_exists($screen, 'id')) return;
|
|
|
|
|
|
|
|
if ($screen->id === 'settings_page_prismatic') {
|
|
|
|
|
|
|
|
wp_enqueue_style('prismatic-font-icons', PRISMATIC_URL .'css/styles-font-icons.css', array(), null);
|
|
|
|
|
|
|
|
wp_enqueue_style('prismatic-settings', PRISMATIC_URL .'css/styles-settings.css', array(), null);
|
|
|
|
|
|
|
|
wp_enqueue_style('wp-jquery-ui-dialog');
|
|
|
|
|
|
|
|
$js_deps = array('jquery', 'jquery-ui-core', 'jquery-ui-dialog');
|
|
|
|
|
|
|
|
wp_enqueue_script('prismatic-settings', PRISMATIC_URL .'js/scripts-settings.js', $js_deps, null);
|
|
|
|
|
|
|
|
$data = prismatic_get_vars_admin();
|
|
|
|
|
|
|
|
wp_localize_script('prismatic-settings', 'prismatic_settings', $data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function prismatic_get_vars_admin() {
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
|
|
|
|
'reset_title' => __('Confirm Reset', 'prismatic'),
|
|
|
|
'reset_message' => __('Restore default options?', 'prismatic'),
|
|
|
|
'reset_true' => __('Yes, make it so.', 'prismatic'),
|
|
|
|
'reset_false' => __('No, abort mission.', 'prismatic'),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
function prismatic_prism_enqueue() {
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
global $prismatic_options_prism;
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
if (isset($prismatic_options_prism['singular_only']) && $prismatic_options_prism['singular_only'] && !is_singular() && !is_admin()) return;
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
$languages = prismatic_active_languages('prism');
|
|
|
|
|
|
|
|
$languages = array_filter($languages);
|
|
|
|
|
|
|
|
if (!empty($languages)) {
|
|
|
|
|
|
|
|
$theme = (isset($prismatic_options_prism['prism_theme'])) ? $prismatic_options_prism['prism_theme'] : 'default';
|
|
|
|
|
|
|
|
wp_enqueue_style('prismatic-prism', PRISMATIC_URL .'lib/prism/css/theme-'. $theme .'.css', array(), null, 'all');
|
|
|
|
|
|
|
|
wp_enqueue_script('prismatic-prism', PRISMATIC_URL .'lib/prism/js/prism-core.js', array(), null, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($prismatic_options_prism['line_highlight']) && $prismatic_options_prism['line_highlight']) {
|
|
|
|
|
|
|
|
wp_enqueue_script('prismatic-prism-line-highlight', PRISMATIC_URL .'lib/prism/js/plugin-line-highlight.js', array('prismatic-prism'), null, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($prismatic_options_prism['line_numbers']) && $prismatic_options_prism['line_numbers']) {
|
|
|
|
|
|
|
|
wp_enqueue_script('prismatic-prism-line-numbers', PRISMATIC_URL .'lib/prism/js/plugin-line-numbers.js', array('prismatic-prism'), null, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($prismatic_options_prism['show_language']) && $prismatic_options_prism['show_language']) {
|
|
|
|
|
|
|
|
wp_enqueue_script('prismatic-prism-show-language', PRISMATIC_URL .'lib/prism/js/plugin-show-language.js', array('prismatic-prism'), null, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$prefix = array('lang-', 'language-');
|
|
|
|
|
|
|
|
foreach ($languages as $language) {
|
|
|
|
|
|
|
|
$language = str_replace($prefix, '', $language);
|
|
|
|
|
|
|
|
$file = PRISMATIC_DIR . 'lib/prism/js/lang-'. $language .'.js';
|
|
|
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
|
|
|
|
wp_enqueue_script('prismatic-prism-'. $language, PRISMATIC_URL .'lib/prism/js/lang-'. $language .'.js', array('prismatic-prism'), null, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
|
|
|
|
if (is_admin()) {
|
|
|
|
|
|
|
|
// todo: once gutenberg is further developed, find a better way to add editor support
|
|
|
|
|
|
|
|
function prismatic_prism_inline_js() {
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
document.onreadystatechange = function () {
|
|
|
|
if (document.readyState == 'complete') {
|
|
|
|
Prism.highlightAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
}
|
|
|
|
add_action('admin_print_footer_scripts', 'prismatic_prism_inline_js');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-24 13:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
function prismatic_highlight_enqueue() {
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
global $prismatic_options_highlight;
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
if (isset($prismatic_options_highlight['singular_only']) && $prismatic_options_highlight['singular_only'] && !is_singular() && !is_admin()) return;
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
$always_load = (isset($prismatic_options_highlight['noprefix_classes']) && $prismatic_options_highlight['noprefix_classes']) ? true : false;
|
|
|
|
|
|
|
|
$languages = prismatic_active_languages('highlight');
|
|
|
|
|
|
|
|
$languages = array_filter($languages);
|
|
|
|
|
|
|
|
if (!empty($languages) || $always_load) {
|
|
|
|
|
|
|
|
$theme = (isset($prismatic_options_highlight['highlight_theme'])) ? $prismatic_options_highlight['highlight_theme'] : 'default';
|
|
|
|
|
|
|
|
wp_enqueue_style('prismatic-highlight', PRISMATIC_URL .'lib/highlight/css/'. $theme .'.css', array(), null, 'all');
|
|
|
|
|
|
|
|
wp_enqueue_script('prismatic-highlight', PRISMATIC_URL .'lib/highlight/js/highlight-core.js', array(), null, true);
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
$init = (isset($prismatic_options_highlight['init_javascript'])) ? $prismatic_options_highlight['init_javascript'] : '';
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
if ($init) {
|
|
|
|
|
|
|
|
wp_add_inline_script('prismatic-highlight', $init, 'after');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
if (is_admin()) {
|
|
|
|
|
|
|
|
// todo: once gutenberg is further developed, find a better way to add editor support
|
|
|
|
|
|
|
|
function prismatic_highlight_inline_js() {
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
document.onreadystatechange = function () {
|
|
|
|
if (document.readyState == 'complete') {
|
|
|
|
jQuery('pre > code').each(function() {
|
|
|
|
hljs.highlightBlock(this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
}
|
|
|
|
add_action('admin_print_footer_scripts', 'prismatic_highlight_inline_js');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-24 13:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function prismatic_active_languages($library) {
|
|
|
|
|
|
|
|
global $posts, $post;
|
|
|
|
|
2017-08-05 15:47:50 -04:00
|
|
|
$languages = array();
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
if (is_admin()) {
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
$content = $post->post_content;
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
$languages = prismatic_active_languages_loop($library, '', $content, array());
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
if (is_singular()) {
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
$excerpt = $post->post_excerpt;
|
|
|
|
|
|
|
|
$content = $post->post_content;
|
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
$comments = ($post->comment_count) ? get_comments(array('post_id' => $post->ID, 'status' => 'approve')) : array();
|
2016-10-24 13:20:42 -04:00
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
$languages = prismatic_active_languages_loop($library, $excerpt, $content, $comments);
|
2016-10-24 13:20:42 -04:00
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
} else {
|
2016-10-24 13:20:42 -04:00
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
foreach ($posts as $post) {
|
|
|
|
|
|
|
|
$excerpt = $post->post_excerpt;
|
|
|
|
|
|
|
|
$content = $post->post_content;
|
|
|
|
|
|
|
|
$comments = array();
|
|
|
|
|
|
|
|
$langs_array[] = prismatic_active_languages_loop($library, $excerpt, $content, $comments);
|
2016-10-24 13:20:42 -04:00
|
|
|
|
2018-05-08 15:06:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach($langs_array as $key => $value) {
|
|
|
|
|
|
|
|
foreach ($value as $k => $v) {
|
|
|
|
|
|
|
|
$languages[] = $v;
|
|
|
|
|
|
|
|
}
|
2016-10-24 13:20:42 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $languages;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function prismatic_active_languages_loop($library, $excerpt, $content, $comments) {
|
|
|
|
|
|
|
|
$languages = array();
|
|
|
|
|
|
|
|
$classes = ($library === 'prism') ? prismatic_prism_classes() : prismatic_highlight_classes();
|
|
|
|
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
|
|
|
|
foreach($class as $cls) {
|
|
|
|
|
|
|
|
if (strpos($excerpt, $cls) !== false) {
|
|
|
|
|
|
|
|
$languages[] = $cls;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($content, $cls) !== false) {
|
|
|
|
|
|
|
|
$languages[] = $cls;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($comments as $comment) {
|
|
|
|
|
|
|
|
if (strpos($comment->comment_content, $cls) !== false) {
|
|
|
|
|
|
|
|
$languages[] = $cls;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$languages = array_unique($languages);
|
|
|
|
|
|
|
|
return $languages;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function prismatic_prism_classes() {
|
|
|
|
|
|
|
|
$classes = array(
|
|
|
|
|
|
|
|
array(
|
|
|
|
'language-apacheconf',
|
|
|
|
'language-applescript',
|
|
|
|
'language-bash',
|
|
|
|
'language-c',
|
|
|
|
'language-csharp',
|
|
|
|
'language-cpp',
|
|
|
|
'language-clike',
|
|
|
|
'language-coffeescript',
|
|
|
|
'language-css',
|
|
|
|
'language-diff',
|
|
|
|
'language-git',
|
2018-05-08 15:06:36 -04:00
|
|
|
'language-go',
|
2017-05-20 19:58:35 -04:00
|
|
|
'language-graphql',
|
|
|
|
'language-groovy',
|
2016-10-24 13:20:42 -04:00
|
|
|
'language-http',
|
|
|
|
'language-ini',
|
|
|
|
'language-java',
|
|
|
|
'language-javascript',
|
|
|
|
'language-json',
|
2017-05-20 19:58:35 -04:00
|
|
|
'language-kotlin',
|
2017-08-05 15:47:50 -04:00
|
|
|
'language-latex',
|
|
|
|
'language-lua',
|
2016-10-24 13:20:42 -04:00
|
|
|
'language-makefile',
|
|
|
|
'language-markdown',
|
|
|
|
'language-markup',
|
|
|
|
'language-nginx',
|
|
|
|
'language-objectivec',
|
2018-05-08 15:06:36 -04:00
|
|
|
'language-pascal',
|
2016-10-24 13:20:42 -04:00
|
|
|
'language-perl',
|
|
|
|
'language-php',
|
2017-05-20 19:58:35 -04:00
|
|
|
'language-powershell',
|
2016-10-24 13:20:42 -04:00
|
|
|
'language-python',
|
|
|
|
'language-ruby',
|
|
|
|
'language-sass',
|
|
|
|
'language-scss',
|
|
|
|
'language-sql',
|
2017-05-20 19:58:35 -04:00
|
|
|
'language-swift',
|
2016-10-24 13:20:42 -04:00
|
|
|
'language-twig',
|
2018-05-08 15:06:36 -04:00
|
|
|
'language-typescript',
|
|
|
|
'language-visual-basic',
|
2017-05-20 19:58:35 -04:00
|
|
|
'language-yaml',
|
2016-10-24 13:20:42 -04:00
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
|
|
|
'lang-apacheconf',
|
|
|
|
'lang-applescript',
|
|
|
|
'lang-bash',
|
|
|
|
'lang-c',
|
|
|
|
'lang-csharp',
|
|
|
|
'lang-cpp',
|
|
|
|
'lang-clike',
|
|
|
|
'lang-coffeescript',
|
|
|
|
'lang-css',
|
|
|
|
'lang-diff',
|
|
|
|
'lang-git',
|
2018-05-08 15:06:36 -04:00
|
|
|
'lang-go',
|
2017-05-20 19:58:35 -04:00
|
|
|
'lang-graphql',
|
|
|
|
'lang-groovy',
|
2016-10-24 13:20:42 -04:00
|
|
|
'lang-http',
|
|
|
|
'lang-ini',
|
|
|
|
'lang-java',
|
|
|
|
'lang-javascript',
|
|
|
|
'lang-json',
|
2017-08-05 15:47:50 -04:00
|
|
|
'lang-kotlin',
|
|
|
|
'lang-latex',
|
|
|
|
'lang-lua',
|
2016-10-24 13:20:42 -04:00
|
|
|
'lang-makefile',
|
|
|
|
'lang-markdown',
|
|
|
|
'lang-markup',
|
|
|
|
'lang-nginx',
|
2018-05-08 15:06:36 -04:00
|
|
|
'lang-objectivec',
|
|
|
|
'lang-pascal',
|
2016-10-24 13:20:42 -04:00
|
|
|
'lang-perl',
|
|
|
|
'lang-php',
|
2017-05-20 19:58:35 -04:00
|
|
|
'lang-powershell',
|
2016-10-24 13:20:42 -04:00
|
|
|
'lang-python',
|
|
|
|
'lang-ruby',
|
|
|
|
'lang-sass',
|
|
|
|
'lang-scss',
|
|
|
|
'lang-sql',
|
2017-05-20 19:58:35 -04:00
|
|
|
'lang-swift',
|
2016-10-24 13:20:42 -04:00
|
|
|
'lang-twig',
|
2017-05-20 19:58:35 -04:00
|
|
|
'lang-typescript',
|
2018-05-08 15:06:36 -04:00
|
|
|
'lang-visual-basic',
|
2017-05-20 19:58:35 -04:00
|
|
|
'lang-yaml',
|
2016-10-24 13:20:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return $classes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function prismatic_highlight_classes() {
|
|
|
|
|
|
|
|
$classes = array(
|
|
|
|
|
|
|
|
array(
|
|
|
|
'language-'
|
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
|
|
|
'lang-',
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return $classes;
|
|
|
|
|
|
|
|
}
|