Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

222 рядки
5.0KB

  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. $output = '';
  30. $split = preg_split("/(\[prismatic_encoded.*\].*\[\/prismatic_encoded\])/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  31. $count = count($split);
  32. for ($i = 0; $i < $count; $i++) {
  33. $content = $split[$i];
  34. if (preg_match("/^\[prismatic_encoded(.*)\](.*)\[\/prismatic_encoded\]/Us", $content, $code)) {
  35. $content = base64_decode($code[2]);
  36. if ($replace) {
  37. $content = preg_replace("/\r/", "", $content);
  38. $content = preg_replace("/^\s*?\n/", "\n", $content);
  39. $content = preg_replace("/&/", "&amp;", $content);
  40. $content = preg_replace("/</", "&lt;", $content);
  41. $content = preg_replace("/>/", "&gt;", $content);
  42. }
  43. $atts = str_replace("%%", "\"", $code[1]);
  44. $content = '<code'. $atts .'>'. $content .'</code>';
  45. }
  46. $output .= $content;
  47. }
  48. return $output;
  49. }
  50. function prismatic_decode_replace($text) {
  51. return prismatic_decode($text, true);
  52. }
  53. function prismatic_check_admin($library, $filter) {
  54. $settings = 'prismatic_options_'. $library;
  55. $setting = 'filter_'. $filter;
  56. $options = prismatic_get_default_options($library);
  57. $option = isset($options[$setting]) ? $options[$setting] : false;
  58. if ($option === 'admin' || $option === 'both') return true;
  59. return false;
  60. }
  61. function prismatic_check_front($library, $filter) {
  62. $settings = 'prismatic_options_'. $library;
  63. $setting = 'filter_'. $filter;
  64. $options = prismatic_get_default_options($library);
  65. $option = isset($options[$setting]) ? $options[$setting] : false;
  66. if ($option === 'front' || $option === 'both') return true;
  67. return false;
  68. }
  69. function prismatic_add_filters() {
  70. global $prismatic_options_general;
  71. $library = (isset($prismatic_options_general['library'])) ? $prismatic_options_general['library'] : 'none';
  72. if (prismatic_check_admin($library, 'content')) {
  73. add_filter('content_save_pre', 'prismatic_encode', 33);
  74. add_filter('content_save_pre', 'prismatic_decode', 77);
  75. }
  76. if (prismatic_check_front($library, 'content')) {
  77. add_filter('the_content', 'prismatic_encode', 1);
  78. add_filter('the_content', 'prismatic_decode_replace', 3);
  79. if (function_exists('get_fields')) { // ACF
  80. add_filter('acf/load_value', 'prismatic_encode', 1);
  81. add_filter('acf/load_value', 'prismatic_decode_replace', 3);
  82. }
  83. }
  84. if (prismatic_check_admin($library, 'excerpts')) {
  85. add_filter('excerpt_save_pre', 'prismatic_encode', 33);
  86. add_filter('excerpt_save_pre', 'prismatic_decode', 77);
  87. }
  88. if (prismatic_check_front($library, 'excerpts')) {
  89. add_filter('the_excerpt', 'prismatic_encode', 1);
  90. add_filter('the_excerpt', 'prismatic_decode_replace', 99);
  91. }
  92. if (prismatic_check_admin($library, 'comments')) {
  93. add_filter('pre_comment_content', 'prismatic_encode', 3);
  94. add_filter('pre_comment_content', 'prismatic_decode', 33);
  95. add_filter('comment_save_pre', 'prismatic_encode', 33);
  96. add_filter('comment_save_pre', 'prismatic_decode', 77);
  97. }
  98. if (prismatic_check_front($library, 'comments')) {
  99. add_filter('comment_text', 'prismatic_encode', 1);
  100. add_filter('comment_text', 'prismatic_decode_replace', 99);
  101. }
  102. }
  103. function prismatic_get_default_options($section) {
  104. $options = '';
  105. if ($section === 'general') {
  106. global $prismatic_options_general;
  107. $options = $prismatic_options_general;
  108. } elseif ($section === 'prism') {
  109. global $prismatic_options_prism;
  110. $options = $prismatic_options_prism;
  111. } elseif ($section === 'highlight') {
  112. global $prismatic_options_highlight;
  113. $options = $prismatic_options_highlight;
  114. } elseif ($section === 'plain') {
  115. global $prismatic_options_plain;
  116. $options = $prismatic_options_plain;
  117. }
  118. return $options;
  119. }