You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
5.7KB

  1. <?php // Prismatic - Core Functions
  2. if (!defined('ABSPATH')) exit;
  3. function prismatic_load_library() {
  4. global $prismatic_options_general;
  5. if (isset($prismatic_options_general['library']) && $prismatic_options_general['library'] === 'prism') {
  6. require_once PRISMATIC_DIR .'lib/prism/prism.php';
  7. } elseif (isset($prismatic_options_general['library']) && $prismatic_options_general['library'] === 'highlight') {
  8. require_once PRISMATIC_DIR .'lib/highlight/highlight.php';
  9. } elseif (isset($prismatic_options_general['library']) && $prismatic_options_general['library'] === 'plain') {
  10. require_once PRISMATIC_DIR .'lib/plain/plain.php';
  11. }
  12. }
  13. function prismatic_encode($text) {
  14. if (!is_string($text)) return $text;
  15. $output = '';
  16. $split = preg_split("/(<code[^>]*>.*<\/code>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  17. $count = count($split);
  18. for ($i = 0; $i < $count; $i++) {
  19. $content = $split[$i];
  20. if (preg_match("/^<code([^>]*)>(.*)<\/code>/Us", $content, $code)) {
  21. $atts = str_replace(array("'", "\""), "%%", $code[1]);
  22. $content = '[prismatic_encoded'. $atts .']'. base64_encode($code[2]) .'[/prismatic_encoded]';
  23. }
  24. $output .= $content;
  25. }
  26. return $output;
  27. }
  28. function prismatic_decode($text, $replace = false) {
  29. if (!is_string($text)) return $text;
  30. $output = '';
  31. $split = preg_split("/(\[prismatic_encoded.*\].*\[\/prismatic_encoded\])/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  32. $count = count($split);
  33. for ($i = 0; $i < $count; $i++) {
  34. $content = $split[$i];
  35. if (preg_match("/^\[prismatic_encoded(.*)\](.*)\[\/prismatic_encoded\]/Us", $content, $code)) {
  36. $content = base64_decode($code[2]);
  37. if ($replace) {
  38. $content = preg_replace("/\r/", "", $content);
  39. $content = preg_replace("/^\s*?\n/", "\n", $content);
  40. $content = preg_replace("/&/", "&amp;", $content);
  41. $content = preg_replace("/</", "&lt;", $content);
  42. $content = preg_replace("/>/", "&gt;", $content);
  43. }
  44. $atts = str_replace("%%", "\"", $code[1]);
  45. $content = '<code'. $atts .'>'. $content .'</code>';
  46. }
  47. $output .= $content;
  48. }
  49. return $output;
  50. }
  51. function prismatic_decode_replace($text) {
  52. return prismatic_decode($text, true);
  53. }
  54. function prismatic_check_admin($library, $filter) {
  55. $settings = 'prismatic_options_'. $library;
  56. $setting = 'filter_'. $filter;
  57. $options = prismatic_get_default_options($library);
  58. $option = isset($options[$setting]) ? $options[$setting] : false;
  59. if ($option === 'admin' || $option === 'both') return true;
  60. return false;
  61. }
  62. function prismatic_check_front($library, $filter) {
  63. $settings = 'prismatic_options_'. $library;
  64. $setting = 'filter_'. $filter;
  65. $options = prismatic_get_default_options($library);
  66. $option = isset($options[$setting]) ? $options[$setting] : false;
  67. if ($option === 'front' || $option === 'both') return true;
  68. return false;
  69. }
  70. function prismatic_add_filters() {
  71. global $prismatic_options_general;
  72. $library = (isset($prismatic_options_general['library'])) ? $prismatic_options_general['library'] : 'none';
  73. if (prismatic_check_admin($library, 'content')) {
  74. add_filter('content_save_pre', 'prismatic_encode', 33);
  75. add_filter('content_save_pre', 'prismatic_decode', 77);
  76. }
  77. if (prismatic_check_front($library, 'content')) {
  78. add_filter('the_content', 'prismatic_encode', 1);
  79. add_filter('the_content', 'prismatic_decode_replace', 3);
  80. if (function_exists('get_fields')) { // ACF
  81. add_filter('acf/load_value', 'prismatic_encode', 1);
  82. add_filter('acf/load_value', 'prismatic_decode_replace', 3);
  83. }
  84. }
  85. if (prismatic_check_admin($library, 'excerpts')) {
  86. add_filter('excerpt_save_pre', 'prismatic_encode', 33);
  87. add_filter('excerpt_save_pre', 'prismatic_decode', 77);
  88. }
  89. if (prismatic_check_front($library, 'excerpts')) {
  90. add_filter('the_excerpt', 'prismatic_encode', 1);
  91. add_filter('the_excerpt', 'prismatic_decode_replace', 99);
  92. }
  93. if (prismatic_check_admin($library, 'comments')) {
  94. add_filter('pre_comment_content', 'prismatic_encode', 3);
  95. add_filter('pre_comment_content', 'prismatic_decode', 33);
  96. add_filter('comment_save_pre', 'prismatic_encode', 33);
  97. add_filter('comment_save_pre', 'prismatic_decode', 77);
  98. }
  99. if (prismatic_check_front($library, 'comments')) {
  100. add_filter('comment_text', 'prismatic_encode', 1);
  101. add_filter('comment_text', 'prismatic_decode_replace', 99);
  102. }
  103. }
  104. function prismatic_get_default_options($section) {
  105. $options = '';
  106. if ($section === 'general') {
  107. global $prismatic_options_general;
  108. $options = $prismatic_options_general;
  109. } elseif ($section === 'prism') {
  110. global $prismatic_options_prism;
  111. $options = $prismatic_options_prism;
  112. } elseif ($section === 'highlight') {
  113. global $prismatic_options_highlight;
  114. $options = $prismatic_options_highlight;
  115. } elseif ($section === 'plain') {
  116. global $prismatic_options_plain;
  117. $options = $prismatic_options_plain;
  118. }
  119. return $options;
  120. }
  121. function prismatic_block_styles() {
  122. global $prismatic_options_general;
  123. $disable = isset($prismatic_options_general['disable_block_styles']) ? $prismatic_options_general['disable_block_styles'] : false;
  124. if ($disable) {
  125. wp_deregister_style('prismatic-blocks');
  126. wp_register_style('prismatic-blocks', false);
  127. }
  128. }
  129. function prismatic_code_shortcode($attr, $content = null) {
  130. extract(shortcode_atts(array(
  131. 'class' => '',
  132. ), $attr));
  133. $class = $class ? ' class="'. $class .'"' : '';
  134. $encode = prismatic_encode($content);
  135. $decode = prismatic_decode($encode);
  136. return '<code'. $class .'>'. $decode .'</code>';
  137. }