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.

179 lines
4.2KB

  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. $output = '';
  15. $split = preg_split("/(<code[^>]*>.*<\/code>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  16. $count = count($split);
  17. for ($i = 0; $i < $count; $i++) {
  18. $content = $split[$i];
  19. if (preg_match("/^<code([^>]*)>(.*)<\/code>/Us", $content, $code)) {
  20. $atts = str_replace(array("'", "\""), "%%", $code[1]);
  21. $content = '[prismatic_encoded'. $atts .']'. base64_encode($code[2]) .'[/prismatic_encoded]';
  22. }
  23. $output .= $content;
  24. }
  25. return $output;
  26. }
  27. function prismatic_decode($text, $replace = false) {
  28. $output = '';
  29. $split = preg_split("/(\[prismatic_encoded.*\].*\[\/prismatic_encoded\])/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  30. $count = count($split);
  31. for ($i = 0; $i < $count; $i++) {
  32. $content = $split[$i];
  33. if (preg_match("/^\[prismatic_encoded(.*)\](.*)\[\/prismatic_encoded\]/Us", $content, $code)) {
  34. $content = base64_decode($code[2]);
  35. if ($replace) {
  36. $content = preg_replace("/\r/", "", $content);
  37. $content = preg_replace("/^\s*?\n/", "\n", $content);
  38. $content = preg_replace("/&/", "&amp;", $content);
  39. $content = preg_replace("/</", "&lt;", $content);
  40. $content = preg_replace("/>/", "&gt;", $content);
  41. }
  42. $atts = str_replace("%%", "\"", $code[1]);
  43. $content = '<code'. $atts .'>'. $content .'</code>';
  44. }
  45. $output .= $content;
  46. }
  47. return $output;
  48. }
  49. function prismatic_decode_replace($text) {
  50. return prismatic_decode($text, true);
  51. }
  52. function prismatic_check_admin($library, $filter) {
  53. $settings = 'prismatic_options_'. $library;
  54. global ${$settings};
  55. $setting = 'filter_'. $filter;
  56. $option = isset(${$settings}[$setting]) ? ${$settings}[$setting] : false;
  57. if ($option === 'admin' || $option === 'both') return true;
  58. return false;
  59. }
  60. function prismatic_check_front($library, $filter) {
  61. $settings = 'prismatic_options_'. $library;
  62. global ${$settings};
  63. $setting = 'filter_'. $filter;
  64. $option = isset(${$settings}[$setting]) ? ${$settings}[$setting] : false;
  65. if ($option === 'front' || $option === 'both') return true;
  66. return false;
  67. }
  68. function prismatic_add_filters() {
  69. global $prismatic_options_general;
  70. $library = (isset($prismatic_options_general['library'])) ? $prismatic_options_general['library'] : 'none';
  71. if (prismatic_check_admin($library, 'content')) {
  72. add_filter('content_save_pre', 'prismatic_encode', 33);
  73. add_filter('content_save_pre', 'prismatic_decode', 77);
  74. }
  75. if (prismatic_check_front($library, 'content')) {
  76. add_filter('the_content', 'prismatic_encode', 1);
  77. add_filter('the_content', 'prismatic_decode_replace', 99);
  78. }
  79. if (prismatic_check_admin($library, 'excerpts')) {
  80. add_filter('excerpt_save_pre', 'prismatic_encode', 33);
  81. add_filter('excerpt_save_pre', 'prismatic_decode', 77);
  82. }
  83. if (prismatic_check_front($library, 'excerpts')) {
  84. add_filter('the_excerpt', 'prismatic_encode', 1);
  85. add_filter('the_excerpt', 'prismatic_decode_replace', 99);
  86. }
  87. if (prismatic_check_admin($library, 'comments')) {
  88. add_filter('pre_comment_content', 'prismatic_encode', 3);
  89. add_filter('pre_comment_content', 'prismatic_decode', 33);
  90. add_filter('comment_save_pre', 'prismatic_encode', 33);
  91. add_filter('comment_save_pre', 'prismatic_decode', 77);
  92. }
  93. if (prismatic_check_front($library, 'comments')) {
  94. add_filter('comment_text', 'prismatic_encode', 1);
  95. add_filter('comment_text', 'prismatic_decode_replace', 99);
  96. }
  97. }