PluginProbe
CodeColorer / 0.9.7
CodeColorer v0.9.7
trunk 0.10.0 0.10.1 0.10.2 0.11.0 0.12.0 0.5.1 0.6.0 0.7.0 0.7.2 0.7.3 0.8.0 0.8.1 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.9.0 0.9.1 0.9.10 0.9.11 0.9.12 0.9.13 All 36 releases
codecolorer / codecolorer.php

codecolorer.php in CodeColorer 0.9.7, at codecolorer.php

256 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: CodeColorer
4 Plugin URI: http://kpumuk.info/projects/wordpress-plugins/codecolorer/
5 Description: This plugin allows you to insert code snippets to your posts with nice syntax highlighting powered by <a href="http://qbnz.com/highlighter/">GeSHi</a> library. After enabling this plugin visit <a href="options-general.php?page=codecolorer.php">the options page</a> to configure code style.
6 Version: 0.9.7
7 Author: Dmytro Shteflyuk
8 Author URI: http://kpumuk.info/
9 Text Domain: codecolorer
10 Domain Path: /languages/
11 */
12 /*
13 Copyright 2006 - 2009 Dmytro Shteflyuk <kpumuk@kpumuk.info>
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 */
29
30 /**
31 * Doesn't work if PHP version is not 4.0.6 or higher
32 */
33 if (version_compare(phpversion(), '4.0.6', '<')) {
34 return;
35 }
36
37 define('CODECOLORER_VERSION', '0.9.7');
38
39 /**
40 * Loader class for the CodeColorer plugin
41 */
42 class CodeColorerLoader {
43 /**
44 * Enables the CodeColorer plugin with registering all required hooks.
45 */
46 function Enable() {
47 $path = dirname(__FILE__);
48 if (!file_exists("$path/codecolorer-core.php")) return false;
49 require_once("$path/codecolorer-core.php");
50
51 // Add some default options if they don't exist
52 add_option('codecolorer_css_style', '');
53 add_option('codecolorer_css_class', '');
54 add_option('codecolorer_lines_to_scroll', 20);
55 add_option('codecolorer_width', 435);
56 add_option('codecolorer_height', 300);
57 add_option('codecolorer_rss_width', 435);
58 add_option('codecolorer_line_numbers', false);
59 add_option('codecolorer_disable_keyword_linking', false);
60 add_option('codecolorer_tab_size', 4);
61 add_option('codecolorer_theme', '');
62 add_option('codecolorer_inline_theme', '');
63
64 // Two more options to disable notifications
65 add_option('codecolorer_language_notification', true);
66 add_option('codecolorer_concurrent_notification', true);
67
68 // Admin panel initialization
69 add_action('admin_init', array('CodeColorerLoader', 'AdminInit'));
70
71 // Add plugin options page
72 add_action('admin_menu', array('CodeColorerLoader', 'AddPluginOptionsPage'));
73
74 // Load CodeColorer styles on admin pages
75 add_action('admin_print_styles', array('CodeColorerLoader', 'LoadStyles'));
76
77 // Show notice when another GeSHi library found
78 if (get_option('codecolorer_concurrent_notification')) {
79 add_action('admin_notices', array('CodeColorerLoader', 'CallShowGeshiWarning'));
80 }
81
82 // Load CodeColorer styles on regular pages
83 add_action('wp_print_styles', array('CodeColorerLoader', 'LoadStyles'));
84
85 // Add action links
86 add_action('plugin_action_links_' . plugin_basename(__FILE__), array('CodeColorerLoader', 'AddPluginActions'));
87
88 // Add meta links
89 add_filter('plugin_row_meta', array('CodeColorerLoader', 'AddPluginLinks'), 10, 2);
90
91 // Code highlighting filters
92 add_filter('the_content', array('CodeColorerLoader', 'CallBeforeHighlightCodeBlock'), -1000);
93 add_filter('the_content', array('CodeColorerLoader', 'CallAfterHighlightCodeBlock'), 1000);
94 add_filter('the_excerpt', array('CodeColorerLoader', 'CallBeforeHighlightCodeBlock'), -1000);
95 add_filter('the_excerpt', array('CodeColorerLoader', 'CallAfterHighlightCodeBlock'), 1000);
96 add_filter('comment_text', array('CodeColorerLoader', 'CallBeforeHighlightCodeBlock'), -1000);
97 add_filter('comment_text', array('CodeColorerLoader', 'CallAfterHighlightCodeBlock'), 1000);
98
99 // Code protection filters
100 add_filter('pre_comment_content', array('CodeColorerLoader', 'CallBeforeProtectComment'), -1000);
101 add_filter('pre_comment_content', array('CodeColorerLoader', 'CallAfterProtectComment'), 1000);
102
103 return true;
104 }
105
106 function AdminInit() {
107 $plugin_dir = basename(dirname(__FILE__));
108 load_plugin_textdomain('codecolorer', false, "$plugin_dir/languages");
109
110 if (!class_exists('CodeColorerOptions')) {
111 $path = dirname(__FILE__);
112 if (!file_exists("$path/codecolorer-options.php")) return false;
113 require_once("$path/codecolorer-options.php");
114 }
115
116 // Register out options so WordPress knows about them
117 register_setting('codecolorer', 'codecolorer_css_style', '');
118 register_setting('codecolorer', 'codecolorer_css_class', '');
119 register_setting('codecolorer', 'codecolorer_lines_to_scroll', 'intval');
120 register_setting('codecolorer', 'codecolorer_width', '');
121 register_setting('codecolorer', 'codecolorer_height', '');
122 register_setting('codecolorer', 'codecolorer_rss_width', '');
123 register_setting('codecolorer', 'codecolorer_line_numbers', '');
124 register_setting('codecolorer', 'codecolorer_disable_keyword_linking', array('CodeColorerOptions', 'SanitizeBoolean'));
125 register_setting('codecolorer', 'codecolorer_tab_size', 'intval');
126 register_setting('codecolorer', 'codecolorer_theme', '');
127 register_setting('codecolorer', 'codecolorer_inline_theme', '');
128
129 // Scripts
130 if (current_user_can('edit_posts') || current_user_can('edit_pages')) {
131 // Quick tags
132 add_action('wp_print_scripts', array('CodeColorerLoader', 'RegisterQuicktag'));
133
134 // TinyMCE
135 // temporarily disabled
136 // if (get_user_option('rich_editing') == 'true') {
137 // add_filter('mce_external_plugins', array('CodeColorerLoader', 'AddTinyMCEPlugin'));
138 // add_filter('mce_buttons', array('CodeColorerLoader', 'RegisterTinyMCEButton'));
139 // }
140 }
141 }
142
143 function LoadStyles() {
144 $css_url = plugins_url(basename(dirname(__FILE__)) . '/codecolorer.css');
145 wp_register_style('codecolorer', $css_url, array(), CODECOLORER_VERSION, 'screen');
146 wp_enqueue_style('codecolorer');
147 $styles = trim(get_option('codecolorer_css_style'));
148 if (!empty($styles)) {
149 echo "<style type=\"text/css\">$styles</style>\n";
150 }
151 }
152
153 function AddPluginOptionsPage() {
154 if (function_exists('add_options_page')) {
155 add_options_page('CodeColorer', 'CodeColorer', 8, 'codecolorer.php', array('CodeColorerLoader', 'CallShowOptionsPage'));
156 }
157 }
158
159 function AddPluginActions($links) {
160 $new_links = array();
161
162 $new_links[] = '<a href="options-general.php?page=codecolorer.php">' . __('Settings', 'codecolorer') . '</a>';
163
164 return array_merge($new_links, $links);
165 }
166
167 function AddPluginLinks($links, $file) {
168 if ($file == basename(dirname(__FILE__)) . '/' . basename(__FILE__)) {
169 $links[] = '<a href="http://kpumuk.info/projects/wordpress-plugins/codecolorer/#faq">' . __('FAQ', 'codecolorer') . '</a>';
170 $links[] = '<a href="http://kpumuk.info/projects/wordpress-plugins/codecolorer/#support">' . __('Support', 'codecolorer') . '</a>';
171 }
172 return $links;
173 }
174
175 function RegisterQuicktag() {
176 if (is_admin()) {
177 wp_enqueue_script('jquery');
178 $url = plugins_url(basename(dirname(__FILE__)) . '/js/quicktags.js');
179 wp_enqueue_script('codecolorer', $url, array('jquery'), CODECOLORER_VERSION, true);
180 wp_localize_script('codecolorer', 'codeColorerL10n', array(
181 'enterLanguage' => __('Enter Language')
182 ));
183 }
184 }
185
186 function RegisterTinyMCEButton($buttons) {
187 array_push($buttons, 'separator', 'codecolorer');
188 return $buttons;
189 }
190
191 function AddTinyMCEPlugin($plugins) {
192 $url = plugins_url(basename(dirname(__FILE__)) . '/js/tinymce_plugin.js');
193 $plugins['codecolorer'] = $url;
194 return $plugins;
195 }
196
197 function CallShowOptionsPage() {
198 $cc = &CodeColorer::GetInstance();
199 if (null !== $cc) {
200 $cc->ShowOptionsPage();
201 }
202 }
203
204 function CallShowGeshiWarning() {
205 $cc = &CodeColorer::GetInstance();
206 if (null !== $cc) {
207 $cc->ShowGeshiWarning();
208 }
209 }
210
211 function CallBeforeHighlightCodeBlock($content) {
212 $cc = &CodeColorer::GetInstance();
213 if (null !== $cc) {
214 return $cc->BeforeHighlightCodeBlock($content);
215 }
216 return $content;
217 }
218
219 function CallAfterHighlightCodeBlock($content) {
220 $cc = &CodeColorer::GetInstance();
221 if (null !== $cc) {
222 return $cc->AfterHighlightCodeBlock($content);
223 }
224 return $content;
225 }
226
227 function CallBeforeProtectComment($content) {
228 $cc = &CodeColorer::GetInstance();
229 if (null !== $cc) {
230 return $cc->BeforeProtectComment($content);
231 }
232 return $content;
233 }
234
235 function CallAfterProtectComment($content) {
236 $cc = &CodeColorer::GetInstance();
237 if (null !== $cc) {
238 return $cc->AfterProtectComment($content);
239 }
240 return $content;
241 }
242
243 function Highlight($code) {
244 $cc = &CodeColorer::GetInstance();
245 if (null !== $cc) {
246 return $cc->GetCodeHighlighted($code);
247 }
248 return $code;
249 }
250 }
251
252 CodeColorerLoader::Enable();
253
254 function codecolorer_highlight($code) {
255 return CodeColorerLoader::Highlight($code);
256 }