-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcap-and-run.php
260 lines (222 loc) · 9.69 KB
/
cap-and-run.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/*
Plugin Name: Cap & Run
Version: 1.0
Description: Drop caps and run-ins for posts and pages.
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Author: Michael A. Alderete, Aldosoft
Author URI: http://github.com/alderete/Cap-and-Run
License: GPL2
Copyright 2010 by Michael A. Alderete
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Define the CapAndRun plugin
global $wp_version;
if ( version_compare($wp_version, "2.8", "<") ):
// Requirement for 2.8 is semi-arbitrary, gotta draw the line somewhere
exit('Cap & Run requires WordPress 2.8 or later.
<a href="http://codex.wordpress.org/Upgrading_WordPress">Please upgrade.</a>
Upgrading is a good idea, for many reasons beyond Cap & Run.');
else:
if ( ! class_exists('CapAndRun') ) {
class CapAndRun {
var $options_name = 'CapAndRun_Options';
var $options_loaded = false;
function activate_plugin() {
$set_defaults = $this->get_options();
}
function get_options() {
if ( ! $this->options_loaded ) {
// These are the default options
$car_options = array(
'add_initial_cap' => false,
'add_run_in' => 'none', // none | line | words
'run_in_words' => '5',
'add_styles_to_header' => 'no', // no | yes
'styles_to_add' => '',
);
// Now merge in the saved options
$set_options = get_option($this->options_name);
if ( ! empty($set_options) ) {
foreach($set_options as $key => $option) {
$car_options[$key] = $option;
}
}
// Make sure we save back a complete set
update_option($this->options_name, $car_options);
// Save options, so we're not writing db multiple times / page view
$this->options_loaded = $car_options;
}
return($this->options_loaded);
}
// Processing functions, run on every page view
function add_style_definitions() {
$options = $this->get_options();
if( 'yes' === $options['add_styles_to_header'] ) {
echo "<!-- Styles added by Cap & Run plugin -->\n";
echo "<style type=\"text/css\">\n";
echo $options['styles_to_add'];
echo "</style>\n";
echo "<!-- End of Cap & Run style additions -->\n\n";
}
}
function add_styling_markup($content = '') {
// error_log($content);
$options = $this->get_options();
if( !( is_single() || is_page() || is_admin() ) ||
(($options['add_initial_cap'] === false) && ($options['add_run_in'] === 'none')) ) {
return($content); // shortcut out
}
$pattern = '/<p( .*)?( class="(.*)")??( .*)?>((<[^>]*>|\s)*)((?:"|“|‘|‘|“|\')?[A-Z])(([a-zA-Z]+\s+){0,' . $options['run_in_words'] . '}?)/U';
$replacement = '<p$1 class="first-child $3"$4>$5<span title="$7" class="cap"><span>$7</span></span><span class="run-in">$8</span>';
$content = preg_replace($pattern, $replacement, $content, 1 );
/*
// Handle the drop cap
if( $options['add_initial_cap'] === true ) {
// The next three lines are copied from the Drop Caps plugin by Thomas Milburn
// http://instantsolve.net/blog/plugins/
$pattern = '/<p( .*)?( class="(.*)")??( .*)?>((<[^>]*>|\s)*)(("|“|‘|‘|“|\')?[A-Z])/U';
$replacement = '<p$1 class="first-child $3"$4>$5<span title="$7" class="cap"><span>$7</span></span>';
$content = preg_replace($pattern, $replacement, $content, 1 );
}
// Handle the run-in
if( $options['add_run_in'] === true ) {
$pattern = '//U';
$replacement = '';
$content = preg_replace($pattern, $replacement, $content, 1);
}
*/
return($content);
}
// Administrative functions, runs only for WP Admins
function add_options_page() {
if ( function_exists('add_theme_page') ) {
add_theme_page( 'Cap & Run', 'Cap & Run', 'manage_options',
basename(__FILE__), array(&$this, 'options_page') );
}
}
function options_page() {
$options = $this->get_options();
// Save a submitted options form
if ( isset($_POST['update_capandrun']) && check_admin_referer('capandrun_options_page') ) {
// TODO -- check the nonce?
$options['add_initial_cap'] = (isset($_POST['car_add_initial_cap'])) ? true : false;
if ( isset($_POST['car_add_run_in']) ) {
if ( 'line' == $_POST['car_add_run_in'] ) {
$options['add_run_in'] = 'line';
}
elseif ( 'words' == $_POST['car_add_run_in'] ) {
$options['add_run_in'] = 'words';
}
else {
$options['add_run_in'] = 'none';
}
}
if ( isset($_POST['cars_num_words']) ) {
$options['run_in_words'] = min(max(1, (int)$_POST['cars_num_words']), 10); // between 1..10
}
if ( isset($_POST['car_add_styles']) ) {
if ( 'yes' == $_POST['car_add_styles'] ) {
$options['add_styles_to_header'] = 'yes';
}
else {
$options['add_styles_to_header'] = 'no';
}
}
if ( isset($_POST['car_styles']) ) {
$options['styles_to_add'] = stripslashes($_POST['car_styles']); // TODO, better escape?
}
update_option($this->options_name, $options);
echo '<div class="updated"><p><strong>' . __('Settings updated.', 'CapAndRun') .
'</strong></p></div>';
}
// Display options form
?>
<div class="wrap">
<h2>Cap & Run Options</h2>
<p class="narrow"><strong>Cap & Run</strong> can add styles to display initial
capital letters (generally called "drop caps") and text "run-ins"
(first few words or first line) in a different text style than the
normal body copy.</p>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<h3>Drop Caps</h3>
<ul>
<li><input type="checkbox" id="car_add_initial_cap" name="car_add_initial_cap" <?php if ($options['add_initial_cap']) echo 'checked="checked"'; ?> />
<label for="car_add_initial_cap">Stylize first letter of post/page as Drop Cap</label></li>
</ul>
<h3>Text Run-In</h3>
<ul>
<li><input type="radio" id="car_add_run_in_none" name="car_add_run_in" value="none" <?php
if ('none' === $options['add_run_in']) echo 'checked="checked"'; ?> />
<label for="car_add_run_in_none">No text run-in</label></li>
<li><input type="radio" id="car_add_run_in_line" name="car_add_run_in" value="line" <?php
if ('line' === $options['add_run_in']) echo 'checked="checked"'; ?> />
<label for="car_add_run_in_line">Stylize first line of post/page as
Run In (using CSS only — more "clean")</label></li>
<li><input type="radio" id="car_add_run_in_words" name="car_add_run_in" value="words" <?php
if ('words' === $options['add_run_in']) echo 'checked="checked"'; ?> />
<label for="car_add_run_in_words">Stylize first
<input type="text" id="cars_num_words" name="cars_num_words" size="3" <?php
if ($options['run_in_words']) echo 'value="' . $options['run_in_words'] . '"';
?> /> words of post/page as Run In (using <span> + CSS — more compatible)</label></li>
</ul>
<h3>Styling</h3>
<ul>
<li><input type="radio" id="car_add_styles_no" name="car_add_styles" value="no" <?php
if ('no' === $options['add_styles_to_header']) echo 'checked="checked"'; ?> />
<label for="car_add_styles_no">Do not add styles to <header> (you must manually add styles to site CSS file)</label></li>
<li><input type="radio" id="car_add_styles_yes" name="car_add_styles" value="yes" <?php
if ('yes' === $options['add_styles_to_header']) echo 'checked="checked"'; ?> />
<label for="car_add_styles_yes">Add these styles to <header>:</label><br />
<textarea id="car_styles" name="car_styles" style="width: 60%;" rows="8"><?php
echo stripslashes($options['styles_to_add']); ?></textarea></li>
</ul>
<div class="submit">
<?php wp_nonce_field('capandrun_options_page'); ?>
<input type="submit" class="button-primary" name="update_capandrun"
value="<?php _e('Update Settings', 'CapAndRun'); ?>" />
</div>
</form>
<h3>Tests</h3>
<?php
require('tests.php');
foreach($cases as $case) {
echo "<hr />";
echo "<h4>Test Case:</h4>\n\n<p><code>" .
htmlspecialchars($case) .
"</code></p>\n\n";
echo "<h4>Result:</h4>\n\n<p><code>" .
htmlspecialchars($this->add_styling_markup($case)) .
"</code></p>\n\n";
}
?>
</div>
<?php
}
} // end class CapAndRun definition
}
endif; // end WordPress version check
// Activate the plugin
if ( class_exists('CapAndRun') ) {
$cap_and_run = new CapAndRun();
if ( function_exists('register_activation_hook') ) {
register_activation_hook(__FILE__, array(&$cap_and_run, 'activate_plugin'));
}
if ( function_exists('add_action') ) {
add_action('admin_menu', array(&$cap_and_run, 'add_options_page'));
add_action('wp_head', array(&$cap_and_run, 'add_style_definitions'));
}
if ( function_exists('add_filter') ) {
add_filter('the_content', array(&$cap_and_run, 'add_styling_markup'), 90);
}
}
?>