PageRenderTime 25ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/cap-and-run.php

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