Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Text Control
- Plugin URI: http://dev.wp-plugins.org/wiki/TextControl
- Description: Take total control of how your blog formats text: Textile 1+2, Markdown, AutoP, nl2br, SmartyPant, and Texturize. Blog wide, individual posts, and defaults for comments.
- Author: Jeff Minard
- Version: 2.0b3mod
- Author URI: http://thecodepro.com/
- */
- // Hey!
- // Get outta here! All configuration is done in WordPress
- // Check the "options > Text Control Config" in your admin section
- // Arrays for the select tags we use often
- $format_select = array( 'wpautop' => 'Default (wpautop)',
- 'textile1' => 'Textile 1',
- 'textile2' => 'Textile 2',
- 'markdown' => 'Markdown',
- 'nl2br' => 'nl2br',
- 'none' => 'No Formatting'
- );
- $encoding_select = array(
- 'wptexturize' => 'Default (wptexturize)',
- 'smartypants' => 'Smarty Pants',
- 'none' => 'No Character Encoding'
- );
- function output_selects($dotext, $dochar, $format_var, $encoding_var) {
- global $format_select, $encoding_select;
- echo('<select name="' . $format_var . '">' . "\n");
- foreach ($format_select AS $formatkey => $formatval)
- echo('<option value="' . $formatkey . '"' . (($dotext == $formatkey) ? ' selected="selected"' : '') . '>' . $formatval . "</option>\n");
- echo("</select>\n");
- echo('<select name="' . $encoding_var . '">' . "\n");
- foreach ($encoding_select AS $encodingkey => $encodingval)
- echo('<option value="' . $encodingkey . '"' . (($dochar == $encodingkey) ? ' selected="selected"' : '') . '>' . $encodingval . "</option>\n");
- echo("</select>\n");
- }
- function tc_post($text) {
- global $id;
- // Check if meta data already exists, otherwise use the defaults
- $do_text = get_post_meta($id, '_tc_post_format', true);
- if (!$do_text) $do_text = get_option('tc_post_format');
- $do_char = get_post_meta($id, '_tc_post_encoding', true);
- if (!$do_char) $do_char = get_option('tc_post_encoding');
- $text = tc_post_process($text, $do_text, $do_char);
- return $text;
- }
- function tc_comment($text) {
- $do_text = get_option('tc_comment_format');
- $do_char = get_option('tc_comment_encoding');
- $text = tc_post_process($text, $do_text, $do_char);
- return $text;
- }
- function tc_post_process($text, $do_text='', $do_char='') {
- if($do_text == 'textile2') {
- require_once('text-control/textile2.php');
- $t = new Textile();
- $text = $t->process($text);
- } else if($do_text == 'textile1') {
- require_once('text-control/textile1.php');
- $text = textile($text);
- } else if($do_text == 'markdown') {
- require_once('text-control/markdown.php');
- $text = Markdown($text);
- } else if($do_text == 'wpautop') {
- $text = wpautop($text);
- } else if($do_text == 'nl2br') {
- $text = nl2br($text);
- } else if($do_text == 'none') {
- $text = $text;
- } else {
- $text = wpautop($text);
- }
- if($do_char == 'smartypants') {
- require_once('text-control/smartypants.php');
- $text = SmartyPants($text);
- } else if($do_char == 'wptexturize') {
- $text = wptexturize($text);
- } else if($do_char == 'none') {
- $text = $text;
- } else {
- $text = wptexturize($text);
- }
- return $text;
- }
- remove_filter('the_content', 'wpautop');
- remove_filter('the_content', 'wptexturize');
- add_filter('the_content', 'tc_post');
- remove_filter('the_excerpt', 'wpautop');
- remove_filter('the_excerpt', 'wptexturize');
- add_filter('the_excerpt', 'tc_post');
- remove_filter('comment_text', 'wpautop', 30);
- remove_filter('comment_text', 'wptexturize');
- add_filter('comment_text', 'tc_comment');
- function tc_post_option_page() {
- global $wpdb;
- ?>
- <div class="wrap">
- <h2>Text Control Settings</h2>
- <p>Set up the default settings for your blog text formatting.</p>
- <?php
- if( $_POST['tc_post_format'] && $_POST['tc_post_encoding'] ) {
- // set the post formatting options
- update_option('tc_post_format', $_POST['tc_post_format']);
- update_option('tc_post_encoding', $_POST['tc_post_encoding']);
- echo '<div class="updated"><p>Post/Excerpt formatting updated.</p></div>';
- }
- if( $_POST['tc_comment_format'] && $_POST['tc_comment_encoding'] ) {
- // set the comment formatting options
- update_option('tc_comment_format', $_POST['tc_comment_format']);
- update_option('tc_comment_encoding', $_POST['tc_comment_encoding']);
- echo '<div class="updated"><p>Comment formatting updated.</p></div>';
- }
- ?>
- <form method="post">
- <fieldset class="options">
- <legend>Posts & Excerpts</legend>
- <p>These will set up what the blog parses posts with by default unless over ridden on a per-post basis.</p>
- <?php
- $do_text = get_option('tc_post_format');
- $do_char = get_option('tc_post_encoding');
- output_selects($do_text, $do_char, 'tc_post_format', 'tc_post_encoding');
- ?>
- <input type="submit" value="Change Default Post/Excerpt Formatting" />
- </fieldset>
- </form>
- <form method="post">
- <fieldset class="options">
- <legend>Comments</legend>
- <p>All comments will be processed with these:</p>
- <form method="post">
- <?php
- $do_text = get_option('tc_comment_format');
- $do_char = get_option('tc_comment_encoding');
- output_selects($do_text, $do_char, 'tc_comment_format', 'tc_comment_encoding');
- ?>
- <input type="submit" value="Change Default Comment Formatting" />
- </fieldset>
- </form>
- </div>
- <?php
- }
- function tc_post_add_options() {
- add_options_page('Text Control Formatting Options', 'Text Control', 8, __FILE__, 'tc_post_option_page');
- }
- add_action('admin_menu', 'tc_post_add_options');
- add_action('edit_post', 'tc_post_edit_post');
- add_action('save_post', 'tc_post_edit_post');
- add_action('publish_post', 'tc_post_edit_post');
- add_action('publish_page', 'tc_post_edit_post');
- add_action('simple_edit_form', 'tc_post_options');
- add_action('edit_form_advanced', 'tc_post_options');
- add_action('edit_page_form', 'tc_post_options');
- function tc_post_edit_post($id) {
- if ($_POST['tc_post_i_format']) {
- $format_stored = update_post_meta($id, '_tc_post_format', $_POST['tc_post_i_format']);
- if (!$format_stored) add_post_meta($id, '_tc_post_format', $_POST['tc_post_i_format']);
- }
- if ($_POST['tc_post_i_encoding']) {
- $encode_stored = update_post_meta($id, '_tc_post_encoding', $_POST['tc_post_i_encoding']);
- if (!$encode_stored) add_post_meta($id, '_tc_post_encoding', $_POST['tc_post_i_encoding']);
- }
- }
- function tc_post_options($content) {
- $post_ID = (isset($_GET['post'])) ? intval($_GET['post']) : intval($_POST['post_ID']);
- // Check if meta data already exists, otherwise use the defaults
- $do_text = get_post_meta($post_ID, '_tc_post_format', true);
- if (!$do_text) $do_text = get_option('tc_post_format');
- $do_char = get_post_meta($post_ID, '_tc_post_encoding', true);
- if (!$do_char) $do_char = get_option('tc_post_encoding');
- ?>
- <fieldset class="options">
- <legend>Text Control</legend>
- <p>Format this post with:
- <?php
- output_selects($do_text, $do_char, 'tc_post_i_format', 'tc_post_i_encoding');
- ?>
- </p>
- </fieldset>
- <?php
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement