| 1 |
<?php // Prismatic - Core Functions |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) exit; |
| 4 |
|
| 5 |
function prismatic_load_library() { |
| 6 |
|
| 7 |
global $prismatic_options_general; |
| 8 |
|
| 9 |
if (isset($prismatic_options_general['library']) && $prismatic_options_general['library'] === 'prism') { |
| 10 |
|
| 11 |
require_once PRISMATIC_DIR .'lib/prism/prism.php'; |
| 12 |
|
| 13 |
} elseif (isset($prismatic_options_general['library']) && $prismatic_options_general['library'] === 'highlight') { |
| 14 |
|
| 15 |
require_once PRISMATIC_DIR .'lib/highlight/highlight.php'; |
| 16 |
|
| 17 |
} elseif (isset($prismatic_options_general['library']) && $prismatic_options_general['library'] === 'plain') { |
| 18 |
|
| 19 |
require_once PRISMATIC_DIR .'lib/plain/plain.php'; |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
function prismatic_encode($text) { |
| 26 |
|
| 27 |
$output = ''; |
| 28 |
$split = preg_split("/(<code[^>]*>.*<\/code>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE); |
| 29 |
$count = count($split); |
| 30 |
|
| 31 |
for ($i = 0; $i < $count; $i++) { |
| 32 |
|
| 33 |
$content = $split[$i]; |
| 34 |
|
| 35 |
if (preg_match("/^<code([^>]*)>(.*)<\/code>/Us", $content, $code)) { |
| 36 |
|
| 37 |
$atts = str_replace(array("'", "\""), "%%", $code[1]); |
| 38 |
|
| 39 |
$content = '[prismatic_encoded'. $atts .']'. base64_encode($code[2]) .'[/prismatic_encoded]'; |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
$output .= $content; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
return $output; |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
function prismatic_decode($text, $replace = false) { |
| 52 |
|
| 53 |
$output = ''; |
| 54 |
$split = preg_split("/(\[prismatic_encoded.*\].*\[\/prismatic_encoded\])/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE); |
| 55 |
$count = count($split); |
| 56 |
|
| 57 |
for ($i = 0; $i < $count; $i++) { |
| 58 |
|
| 59 |
$content = $split[$i]; |
| 60 |
|
| 61 |
if (preg_match("/^\[prismatic_encoded(.*)\](.*)\[\/prismatic_encoded\]/Us", $content, $code)) { |
| 62 |
|
| 63 |
$content = base64_decode($code[2]); |
| 64 |
|
| 65 |
if ($replace) { |
| 66 |
|
| 67 |
$content = preg_replace("/\r/", "", $content); |
| 68 |
$content = preg_replace("/^\s*?\n/", "\n", $content); |
| 69 |
$content = preg_replace("/&/", "&", $content); |
| 70 |
$content = preg_replace("/</", "<", $content); |
| 71 |
$content = preg_replace("/>/", ">", $content); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
$atts = str_replace("%%", "\"", $code[1]); |
| 76 |
|
| 77 |
$content = '<code'. $atts .'>'. $content .'</code>'; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
$output .= $content; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
return $output; |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
function prismatic_decode_replace($text) { |
| 90 |
|
| 91 |
return prismatic_decode($text, true); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
function prismatic_check_admin($library, $filter) { |
| 96 |
|
| 97 |
$settings = 'prismatic_options_'. $library; |
| 98 |
|
| 99 |
$setting = 'filter_'. $filter; |
| 100 |
|
| 101 |
$options = prismatic_get_default_options($library); |
| 102 |
|
| 103 |
$option = isset($options[$setting]) ? $options[$setting] : false; |
| 104 |
|
| 105 |
if ($option === 'admin' || $option === 'both') return true; |
| 106 |
|
| 107 |
return false; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
function prismatic_check_front($library, $filter) { |
| 112 |
|
| 113 |
$settings = 'prismatic_options_'. $library; |
| 114 |
|
| 115 |
$setting = 'filter_'. $filter; |
| 116 |
|
| 117 |
$options = prismatic_get_default_options($library); |
| 118 |
|
| 119 |
$option = isset($options[$setting]) ? $options[$setting] : false; |
| 120 |
|
| 121 |
if ($option === 'front' || $option === 'both') return true; |
| 122 |
|
| 123 |
return false; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
function prismatic_add_filters() { |
| 128 |
|
| 129 |
global $prismatic_options_general; |
| 130 |
|
| 131 |
$library = (isset($prismatic_options_general['library'])) ? $prismatic_options_general['library'] : 'none'; |
| 132 |
|
| 133 |
if (prismatic_check_admin($library, 'content')) { |
| 134 |
|
| 135 |
add_filter('content_save_pre', 'prismatic_encode', 33); |
| 136 |
add_filter('content_save_pre', 'prismatic_decode', 77); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
if (prismatic_check_front($library, 'content')) { |
| 141 |
|
| 142 |
add_filter('the_content', 'prismatic_encode', 1); |
| 143 |
add_filter('the_content', 'prismatic_decode_replace', 3); |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
if (prismatic_check_admin($library, 'excerpts')) { |
| 148 |
|
| 149 |
add_filter('excerpt_save_pre', 'prismatic_encode', 33); |
| 150 |
add_filter('excerpt_save_pre', 'prismatic_decode', 77); |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
if (prismatic_check_front($library, 'excerpts')) { |
| 155 |
|
| 156 |
add_filter('the_excerpt', 'prismatic_encode', 1); |
| 157 |
add_filter('the_excerpt', 'prismatic_decode_replace', 99); |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
if (prismatic_check_admin($library, 'comments')) { |
| 162 |
|
| 163 |
add_filter('pre_comment_content', 'prismatic_encode', 3); |
| 164 |
add_filter('pre_comment_content', 'prismatic_decode', 33); |
| 165 |
|
| 166 |
add_filter('comment_save_pre', 'prismatic_encode', 33); |
| 167 |
add_filter('comment_save_pre', 'prismatic_decode', 77); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
if (prismatic_check_front($library, 'comments')) { |
| 172 |
|
| 173 |
add_filter('comment_text', 'prismatic_encode', 1); |
| 174 |
add_filter('comment_text', 'prismatic_decode_replace', 99); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
function prismatic_get_default_options($section) { |
| 181 |
|
| 182 |
$options = ''; |
| 183 |
|
| 184 |
if ($section === 'general') { |
| 185 |
|
| 186 |
global $prismatic_options_general; |
| 187 |
|
| 188 |
$options = $prismatic_options_general; |
| 189 |
|
| 190 |
} elseif ($section === 'prism') { |
| 191 |
|
| 192 |
global $prismatic_options_prism; |
| 193 |
|
| 194 |
$options = $prismatic_options_prism; |
| 195 |
|
| 196 |
} elseif ($section === 'highlight') { |
| 197 |
|
| 198 |
global $prismatic_options_highlight; |
| 199 |
|
| 200 |
$options = $prismatic_options_highlight; |
| 201 |
|
| 202 |
} elseif ($section === 'plain') { |
| 203 |
|
| 204 |
global $prismatic_options_plain; |
| 205 |
|
| 206 |
$options = $prismatic_options_plain; |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
return $options; |
| 211 |
|
| 212 |
} |
| 213 |
|