PluginProbe
CodeColorer / 0.9.9
CodeColorer v0.9.9
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.9, at codecolorer.php

258 lines 9.4 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.9
7 Author: Dmytro Shteflyuk
8 Author URI: http://kpumuk.info/
9 Text Domain: codecolorer
10 Domain Path: /languages/
11 */
12 /*
13 Copyright 2006 - 2011 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.9');
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 add_filter('book_review', array('CodeColorerLoader', 'CallBeforeHighlightCodeBlock'), -1000);
99 add_filter('book_review', array('CodeColorerLoader', 'CallAfterHighlightCodeBlock'), 1000);
100
101 // Code protection filters
102 add_filter('pre_comment_content', array('CodeColorerLoader', 'CallBeforeProtectComment'), -1000);
103 add_filter('pre_comment_content', array('CodeColorerLoader', 'CallAfterProtectComment'), 1000);
104
105 return true;
106 }
107
108 function AdminInit() {
109 $plugin_dir = basename(dirname(__FILE__));
110 load_plugin_textdomain('codecolorer', false, "$plugin_dir/languages");
111
112 if (!class_exists('CodeColorerOptions')) {
113 $path = dirname(__FILE__);
114 if (!file_exists("$path/codecolorer-options.php")) return false;
115 require_once("$path/codecolorer-options.php");
116 }
117
118 // Register out options so WordPress knows about them
119 register_setting('codecolorer', 'codecolorer_css_style', '');
120 register_setting('codecolorer', 'codecolorer_css_class', '');
121 register_setting('codecolorer', 'codecolorer_lines_to_scroll', 'intval');
122 register_setting('codecolorer', 'codecolorer_width', '');
123 register_setting('codecolorer', 'codecolorer_height', '');
124 register_setting('codecolorer', 'codecolorer_rss_width', '');
125 register_setting('codecolorer', 'codecolorer_line_numbers', '');
126 register_setting('codecolorer', 'codecolorer_disable_keyword_linking', array('CodeColorerOptions', 'SanitizeBoolean'));
127 register_setting('codecolorer', 'codecolorer_tab_size', 'intval');
128 register_setting('codecolorer', 'codecolorer_theme', '');
129 register_setting('codecolorer', 'codecolorer_inline_theme', '');
130
131 // Scripts
132 if (current_user_can('edit_posts') || current_user_can('edit_pages')) {
133 // Quick tags
134 add_action('wp_print_scripts', array('CodeColorerLoader', 'RegisterQuicktag'));
135
136 // TinyMCE
137 // temporarily disabled
138 // if (get_user_option('rich_editing') == 'true') {
139 // add_filter('mce_external_plugins', array('CodeColorerLoader', 'AddTinyMCEPlugin'));
140 // add_filter('mce_buttons', array('CodeColorerLoader', 'RegisterTinyMCEButton'));
141 // }
142 }
143 }
144
145 function LoadStyles() {
146 $css_url = plugins_url(basename(dirname(__FILE__)) . '/codecolorer.css');
147 wp_register_style('codecolorer', $css_url, array(), CODECOLORER_VERSION, 'screen');
148 wp_enqueue_style('codecolorer');
149 $styles = trim(get_option('codecolorer_css_style'));
150 if (!empty($styles)) {
151 echo "<style type=\"text/css\">$styles</style>\n";
152 }
153 }
154
155 function AddPluginOptionsPage() {
156 if (function_exists('add_options_page')) {
157 add_options_page('CodeColorer', 'CodeColorer', 'manage_options', 'codecolorer.php', array('CodeColorerLoader', 'CallShowOptionsPage'));
158 }
159 }
160
161 function AddPluginActions($links) {
162 $new_links = array();
163
164 $new_links[] = '<a href="options-general.php?page=codecolorer.php">' . __('Settings', 'codecolorer') . '</a>';
165
166 return array_merge($new_links, $links);
167 }
168
169 function AddPluginLinks($links, $file) {
170 if ($file == basename(dirname(__FILE__)) . '/' . basename(__FILE__)) {
171 $links[] = '<a href="http://kpumuk.info/projects/wordpress-plugins/codecolorer/#faq">' . __('FAQ', 'codecolorer') . '</a>';
172 $links[] = '<a href="http://kpumuk.info/projects/wordpress-plugins/codecolorer/#support">' . __('Support', 'codecolorer') . '</a>';
173 }
174 return $links;
175 }
176
177 function RegisterQuicktag() {
178 if (is_admin()) {
179 wp_enqueue_script('jquery');
180 $url = plugins_url(basename(dirname(__FILE__)) . '/js/quicktags.js');
181 wp_enqueue_script('codecolorer', $url, array('jquery'), CODECOLORER_VERSION, true);
182 wp_localize_script('codecolorer', 'codeColorerL10n', array(
183 'enterLanguage' => __('Enter Language')
184 ));
185 }
186 }
187
188 function RegisterTinyMCEButton($buttons) {
189 array_push($buttons, 'separator', 'codecolorer');
190 return $buttons;
191 }
192
193 function AddTinyMCEPlugin($plugins) {
194 $url = plugins_url(basename(dirname(__FILE__)) . '/js/tinymce_plugin.js');
195 $plugins['codecolorer'] = $url;
196 return $plugins;
197 }
198
199 function CallShowOptionsPage() {
200 $cc = &CodeColorer::GetInstance();
201 if (null !== $cc) {
202 $cc->ShowOptionsPage();
203 }
204 }
205
206 function CallShowGeshiWarning() {
207 $cc = &CodeColorer::GetInstance();
208 if (null !== $cc) {
209 $cc->ShowGeshiWarning();
210 }
211 }
212
213 function CallBeforeHighlightCodeBlock($content) {
214 $cc = &CodeColorer::GetInstance();
215 if (null !== $cc) {
216 return $cc->BeforeHighlightCodeBlock($content);
217 }
218 return $content;
219 }
220
221 function CallAfterHighlightCodeBlock($content) {
222 $cc = &CodeColorer::GetInstance();
223 if (null !== $cc) {
224 return $cc->AfterHighlightCodeBlock($content);
225 }
226 return $content;
227 }
228
229 function CallBeforeProtectComment($content) {
230 $cc = &CodeColorer::GetInstance();
231 if (null !== $cc) {
232 return $cc->BeforeProtectComment($content);
233 }
234 return $content;
235 }
236
237 function CallAfterProtectComment($content) {
238 $cc = &CodeColorer::GetInstance();
239 if (null !== $cc) {
240 return $cc->AfterProtectComment($content);
241 }
242 return $content;
243 }
244
245 function Highlight($code) {
246 $cc = &CodeColorer::GetInstance();
247 if (null !== $cc) {
248 return $cc->GetCodeHighlighted($code);
249 }
250 return $code;
251 }
252 }
253
254 CodeColorerLoader::Enable();
255
256 function codecolorer_highlight($code) {
257 return CodeColorerLoader::Highlight($code);
258 }