Changeset 2420109
- Timestamp:
- 11/17/2020 02:55:19 PM (5 years ago)
- Location:
- excerpt-tools/trunk
- Files:
-
- 3 edited
-
excerpt-tools.php (modified) (15 diffs)
-
js/jquery.charcounter.js (modified) (1 diff)
-
readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
excerpt-tools/trunk/excerpt-tools.php
r958682 r2420109 8 8 Author: Marcus Downing and Zack Kakia 9 9 Author URI: http://www.bang-on.net/ 10 Version: 0. 510 Version: 0.7 11 11 */ 12 13 if (!defined('EXCERPT_TOOLS_DEBUG')) 14 define('EXCERPT_TOOLS_DEBUG', false); 12 15 13 16 $jscounter = plugins_url( 'js/jquery.charcounter.js', __FILE__ ); … … 18 21 function box_init() { 19 22 $options = get_option('e_tools'); 20 do_action('log', 'Excerpt tools: init', $options);23 if (EXCERPT_TOOLS_DEBUG) do_action('log', 'Excerpt tools: init', $options); 21 24 $title = isset($options['excerpt_title']) ? trim((string) $options['excerpt_title']) : ''; 22 25 if (empty($title) || !is_string($title)) $title = __('Excerpt', 'excerpt-tools'); 23 26 if (empty($title) || !is_string($title)) $title = 'Excerpt'; 24 do_action('log', 'Excerpt tools: Title: "%s"', $title); 27 if (EXCERPT_TOOLS_DEBUG) do_action('log', 'Excerpt tools: Title: "%s"', $title); 28 29 $excerpt_icon = isset($options['excerpt_icon']) ? trim((string) $options['excerpt_icon']) : ''; 30 if (empty($excerpt_icon)) $excerpt_icon = 'format-quote'; 31 foreach (possible_icons() as $icon => $name) { 32 if ($excerpt_icon == $icon) { 33 if (EXCERPT_TOOLS_DEBUG) do_action('log', 'Excerpt tools: Icon', $icon); 34 $title = "<i class='dashicons dashicons-$icon'></i> ".$title; 35 break; 36 } 37 } 25 38 26 39 foreach (get_post_types(array(), 'objects') as $post_type) { 27 40 if (isset($options['enable_'.$post_type->name]) && $options['enable_'.$post_type->name] == 1) { 28 do_action('log', 'Excerpt tools: Add meta box', $post_type->name, $title);29 remove_meta_box('postexcerpt', $post_type->name, ' core');41 if (EXCERPT_TOOLS_DEBUG) do_action('log', 'Excerpt tools: Init: Add meta box', $post_type->name, $title); 42 remove_meta_box('postexcerpt', $post_type->name, 'normal'); 30 43 add_meta_box('e_tools_excerpt', $title, __NAMESPACE__.'\meta_box', $post_type->name, 'normal', 'high'); 44 } 45 } 46 if (EXCERPT_TOOLS_DEBUG >= 2) { 47 global $wp_meta_boxes; 48 foreach ($wp_meta_boxes as $post_type => $value) { 49 do_action('log', 'Excerpt tools: Init: $wp_meta_boxes[%s]', $post_type, $value); 31 50 } 32 51 } … … 50 69 function init() { 51 70 $options = get_option('e_tools'); 52 if ( $options['enforce_length']) {71 if (isset($options['enforce_length']) && $options['enforce_length']) { 53 72 add_filter('wp_trim_excerpt', __NAMESPACE__.'\filter_trim_excerpt', 11, 2); 54 73 add_filter('option_relevanssi_excerpt_length', __NAMESPACE__.'\option_relevanssi_excerpt_length', 99); … … 57 76 58 77 function filter_trim_excerpt($excerpt, $raw) { 59 do_action('log', 'wp_trim_excerpt', '@filter', 'wp_trim_excerpt');78 // if (EXCERPT_TOOLS_DEBUG) do_action('log', 'Excerpt tools: wp_trim_excerpt', '@filter', 'wp_trim_excerpt'); 60 79 61 80 $options = get_option('e_tools'); … … 63 82 64 83 if (mb_strlen($excerpt) > $len) { 84 $affix = ''; 85 $r = strrpos($excerpt, '...', strlen($excerpt) - 5); 86 if ($r === false) strrpos($excerpt, '…', strlen($excerpt) - 5); 87 if ($r !== false) { 88 $affix = substr($excerpt, $r); 89 $excerpt = substr($excerpt, 0, $r); 90 } 91 92 // if (EXCERPT_TOOLS_DEBUG) do_action('log', 'Excerpt tools: trim excerpt: %s...%s', substr($excerpt, 0, 30), substr($excerpt, strlen($excerpt) - 30)); 93 65 94 $short = mb_substr($excerpt, 0, $len + 1); 66 95 $pos = mb_strrpos($short, ' '); 67 do_action('log', 'trim excerpt: excerpt length = %s; desired length = %s; found space at %s: "%s%', mb_strlen($excerpt), $len, $pos, $short);96 if (EXCERPT_TOOLS_DEBUG) do_action('log', 'Excerpt tools: trim excerpt: excerpt length = %s; desired length = %s; found space at %s: "%s%', mb_strlen($excerpt), $len, $pos, $short); 68 97 if ($pos !== false && $pos > 0) 69 98 $excerpt = mb_substr($short, 0, $pos); 70 99 else 71 100 $excerpt = $short; 101 102 $excerpt = rtrim($excerpt); 103 $excerpt = rtrim($excerpt, " :."); 104 $excerpt = $excerpt.$affix; 72 105 } 73 106 … … 77 110 function option_relevanssi_excerpt_length($length) { 78 111 $options = get_option('e_tools'); 79 $len = intval($options['excerpt_length']) ;112 $len = intval($options['excerpt_length']) - 3; // make way for an ellipsis! 80 113 if ($length > $len) $length = $len; 81 114 return $length; … … 83 116 84 117 // Draw the menu page itself 85 function settings_page() { 118 function settings_page() { 86 119 global $jscounter; 87 120 $options = get_option('e_tools'); 121 if (!isset($options['excerpt_title'])) $options['excerpt_title'] = ''; 122 if (!isset($options['excerpt_icon'])) $options['excerpt_icon'] = 'format-quote'; 88 123 89 124 ?> … … 93 128 <?php settings_fields('e_tools_options'); ?> 94 129 <div class='metabox-holder'> 130 131 <div class='stack-group'> 132 <div class='stack-block'> 133 95 134 <div class='postbox'> 96 135 97 <h3><?php _e('Options', 'excerpt-tools'); ?></h3> 98 <div class='inside'> 136 <h3><?php _e('Post types', 'excerpt-tools'); ?></h3> 99 137 <table class="form-table"> 100 138 <?php 101 102 139 $post_types = array(); 103 140 $post_type_icons = array( … … 115 152 $post_type_icons[$post_type->name] = $post_type->menu_icon; 116 153 } 117 do_action('log', 'Excerpt tools: Post types', $post_types); 118 119 $i = 0; 154 if (EXCERPT_TOOLS_DEBUG) do_action('log', 'Excerpt tools: Settings: Post types', $post_types); 155 120 156 foreach ($post_types as $key => $name) { 121 echo "<tr>"; 122 if ($i == 0) { 123 echo "<th scope='row' rowspan='".count($post_types)."'>Post types</th>"; 124 } 125 $i++; 126 echo "<td><label for='e_tools_enable_$key'>"; 127 echo "<input type='checkbox' name='e_tools[enable_$key]' id='e_tools_enable_$key' value='1' "; checked(isset($options["enable_$key"]) && intval($options["enable_$key"])); echo ">"; 157 echo "<tr><td><label for='e_tools_enable_$key'>"; 158 echo "<input type='checkbox' name='e_tools[enable_$key]' id='e_tools_enable_$key' value='1' "; checked(isset($options["enable_$key"]) && intval($options["enable_$key"])); echo "> "; 128 159 if (isset($post_type_icons[$key])) 129 echo " <i class='dashicons ${post_type_icons[$key]}'></i> ";160 echo "<i class='dashicons ${post_type_icons[$key]}'></i> "; 130 161 echo $name; 131 162 echo "</td></tr>"; … … 140 171 141 172 $enforce_length = isset($options['enforce_length']) && (boolean) $options['enforce_length']; 173 $excerpt_length_html = isset($options['excerpt_length_html']) && (boolean) $options['excerpt_length_html']; 142 174 ?> 143 175 </table> 176 </div> 177 178 </div><div class='stack-block'> 179 180 <div class='postbox'> 181 182 <h3><?php _e('Options', 'excerpt-tools'); ?></h3> 183 <div class='inside'> 184 <table class="form-table"> 144 185 <tr valign="top"> 145 186 <th scope="row"><?php _e('Excerpt Length', 'excerpt-tools'); ?></th> 146 187 <td><input type="text" name="e_tools[excerpt_length]" id='excerpt_length' value="<?php echo $length; ?>" placeholder='150' style='text-align: right; width: 4em;' /> characters 147 188 <p><label for='enforce_length'><input type='checkbox' name='e_tools[enforce_length]' id='enforce_length' <?php checked($enforce_length); ?>> 148 <?php _e('Enforce this length limit on all excerpts', 'excerpt-tools'); ?></p></td> 189 <?php _e('Enforce this length limit on all excerpts, including Relevanssi search results', 'excerpt-tools'); ?></p> 190 <p><label for='excerpt_length_html'><input type='checkbox' name='e_tools[excerpt_length_html]' id='excerpt_length_html' <?php checked($excerpt_length_html); ?>> 191 <?php _e('Exclude HTML tags from this length', 'excerpt-tools'); ?></p> 192 </td> 149 193 </tr> 150 194 … … 155 199 156 200 <tr valign="top"> 201 <th scope="row"><?php _e('Excerpt icon', 'excerpt-tools'); ?></th> 202 <td> 203 <?php 204 $excerpt_icon = $options['excerpt_icon']; 205 $possible_icons = possible_icons(); 206 foreach ($possible_icons as $icon => $name) { 207 echo "<div style='margin-bottom: 6px;'><label for='excerpt_icon-$icon'>"; 208 $checked = checked($icon, $excerpt_icon, false); 209 echo "<input type='radio' name='e_tools[excerpt_icon]' id='excerpt_icon-$icon' value='$icon' $checked> "; 210 if ($icon != 'none') echo " <i class='dashicons dashicons-$icon'></i> "; 211 echo $name."<label></div>"; 212 } 213 ?> 214 </td> 215 </tr> 216 217 <tr valign="top"> 157 218 <th scope="row"><?php _e('Excerpt description', 'excerpt-tools'); ?></th> 158 219 <td> 159 <textarea rows="2" cols="60" name="e_tools[excerpt_text]" id="excerpt_text"><?php echo $options['excerpt_text']; ?></textarea>220 <textarea rows="2" cols="60" name="e_tools[excerpt_text]" id="excerpt_text"><?php echo $options['excerpt_text']; ?></textarea> 160 221 </td> 161 222 </tr> … … 163 224 164 225 </div> 226 </div></div> 165 227 </div> 166 228 … … 177 239 178 240 241 function possible_icons() { 242 return array( 243 'format-quote' => 'Quote', 244 'editor-quote' => 'Quote (smaller)', 245 'text' => 'Text', 246 'editor-help' => 'Help', 247 'info' => 'Info', 248 'admin-comments' => 'Comment bubble', 249 'format-chat' => 'Chat bubbles', 250 'welcome-write-blog' => 'Writing', 251 'nametag' => 'Name tag', 252 'exerpt-view' => 'Excerpt view', 253 'none' => 'No icon', 254 ); 255 } 256 179 257 function meta_box($post) { 258 ?></div><div class='outside' style='padding-bottom: 8px;'><?php 259 180 260 wp_enqueue_script('jquery'); 181 261 $options = get_option('e_tools'); 182 do_action('log', 'Excerpt tools: Meta box', $options); 262 if (EXCERPT_TOOLS_DEBUG) do_action('log', 'Excerpt tools: Meta box', $options); 263 if (EXCERPT_TOOLS_DEBUG >= 2) do_action('log', 'Excerpt tools: Meta box', '@trace'); 183 264 $title = $options['excerpt_title']; 184 265 if (empty($title)) … … 189 270 $length = 150; 190 271 272 if (!empty($options['excerpt_text'])) 273 echo "<p>".$options['excerpt_text']."</p>"; 191 274 ?> 192 <div style='margin: -6px -12px 0 -12px;'> 275 193 276 <textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt" style='width: 100%; min-width: 100%; max-width: 100%; border-width: 0 0 1px 0; resize: vertical;'><?php 194 277 echo $post->post_excerpt; 195 278 ?></textarea> 196 </div>197 <?php198 if (!empty($options['excerpt_text']))199 echo "<p>".$options['excerpt_text']."</p>";200 ?>201 279 202 280 <script type="text/javascript" src="<?php echo plugins_url( 'js/jquery.charcounter.js', __FILE__ ); ?>"> </script> … … 204 282 205 283 jQuery(function($) { 284 var progress = $("<span></span>").insertBefore($("#excerpt")).css({ 285 'height': '3px', 286 'line-height': '3px', 287 'width': '0', 288 'display': 'block', 289 'background': '#2ea2cc', 290 'margin-top': '2px', 291 'margin-bottom': '2px' 292 }); 293 206 294 $("#excerpt").charCounter( <?php echo $length; ?>, { 207 295 container: "<div id='counter' class='counter' style='padding-top:5px; padding-left: 8px;'></div>", 208 296 classname: "counter", 297 callback: function (value, max) { 298 if (typeof progress !== 'undefined') { 299 var colour = '#2ea2cc'; 300 percent = Math.round(value * 10000.0 / max) / 100; 301 302 if (value == max) { 303 colour = '#e03030'; 304 } else if (percent >= 90) { 305 colour = '#f89000'; 306 } else if (percent >= 80) { 307 colour = '#ecb800'; 308 } 309 progress.css({ 310 'width': percent+'%', 311 'background': colour 312 }); 313 } 314 }, 209 315 format: "%1 characters remaining" 210 316 }); 317 318 // progress.insertAfter($("#excerpt")); 319 320 $("#e_tools_excerpt .inside").each(function () { 321 var inside = $(this); 322 if (!$.trim(inside.html())) { 323 inside.remove(); 324 } 325 }); 211 326 }); 212 327 -
excerpt-tools/trunk/js/jquery.charcounter.js
r173577 r2420109 1 1 /** 2 3 2 * 4 5 3 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com) 6 7 4 * Licensed under the MIT License: 8 9 5 * http://www.opensource.org/licenses/mit-license.php 10 11 6 * 12 13 7 */ 14 15 8 16 17 9 (function($) { 18 19 10 /** 20 21 11 * attaches a character counter to each textarea element in the jQuery object 22 23 12 * usage: $("#myTextArea").charCounter(max, settings); 24 25 13 */ 26 27 14 28 29 15 $.fn.charCounter = function (max, settings) { 30 31 16 max = max || 100; 32 33 17 settings = $.extend({ 34 35 18 container: "<span></span>", 36 37 19 classname: "charcounter", 38 39 20 format: "(%1 characters remaining)", 40 21 callback: null, 41 22 pulse: true, 42 43 23 delay: 0 44 45 24 }, settings); 46 47 25 var p, timeout; 48 49 26 50 51 27 function count(el, container) { 52 53 28 el = $(el); 54 55 29 if (el.val().length > max) { 56 57 30 el.val(el.val().substring(0, max)); 58 59 31 if (settings.pulse && !p) { 60 61 32 pulse(container, true); 62 63 33 }; 64 65 34 }; 66 67 35 if (settings.delay > 0) { 68 69 36 if (timeout) { 70 71 37 window.clearTimeout(timeout); 72 73 38 } 74 75 39 timeout = window.setTimeout(function () { 76 77 40 container.html(settings.format.replace(/%1/, (max - el.val().length))); 78 41 if (typeof settings.callback === 'function') { settings.callback(el.val().length, max); } 79 42 }, settings.delay); 80 81 43 } else { 82 83 44 container.html(settings.format.replace(/%1/, (max - el.val().length))); 84 45 if (typeof settings.callback === 'function') { settings.callback(el.val().length, max); } 85 46 } 86 87 47 }; 88 89 48 90 91 49 function pulse(el, again) { 92 93 50 if (p) { 94 95 51 window.clearTimeout(p); 96 97 52 p = null; 98 99 53 }; 100 101 54 el.animate({ opacity: 0.1 }, 100, function () { 102 103 55 $(this).animate({ opacity: 1.0 }, 100); 104 105 56 }); 106 107 57 if (again) { 108 109 58 p = window.setTimeout(function () { pulse(el) }, 200); 110 111 59 }; 112 113 60 }; 114 115 61 116 117 62 return this.each(function () { 118 119 63 var container = (!settings.container.match(/^<.+>$/)) 120 121 64 ? $(settings.container) 122 123 65 : $(settings.container) 124 125 66 .insertAfter(this) 126 127 67 .addClass(settings.classname); 128 129 68 $(this) 130 131 69 .bind("keydown", function () { count(this, container); }) 132 133 70 .bind("keypress", function () { count(this, container); }) 134 135 71 .bind("keyup", function () { count(this, container); }) 136 137 72 .bind("focus", function () { count(this, container); }) 138 139 73 .bind("mouseover", function () { count(this, container); }) 140 141 74 .bind("mouseout", function () { count(this, container); }) 142 143 75 .bind("paste", function () { 144 145 76 var me = this; 146 147 77 setTimeout(function () { count(me, container); }, 10); 148 149 78 }); 150 151 79 if (this.addEventListener) { 152 153 80 this.addEventListener('input', function () { count(this, container); }, false); 154 155 81 }; 156 157 82 count(this, container); 158 159 83 }); 160 161 84 }; 162 85 163 164 165 86 })(jQuery); -
excerpt-tools/trunk/readme.txt
r958682 r2420109 1 === Plugin Name===1 === Excerpt Tools === 2 2 Contributors: marcus.downing, zaybiz 3 3 Tags: excerpt, excerpt length, excerpt tools, jQuery, character limit 4 4 Requires at least: 2.8 5 Tested up to: 3.9.15 Tested up to: 5.5.0 6 6 Stable tag: trunk 7 7 … … 10 10 == Description == 11 11 12 A simple plugin to enhance your use of the_excerpt() function. Allows you to change the default title and description of the excerpt box, add an excerpt box to pages and show a customizable jQuery character counter with the ability to limit the amount of characters. 12 A simple plugin to enhance your use of the_excerpt() function. Allows you to change the default title and description of the excerpt box, add an excerpt box to pages and show a customizable jQuery character counter with the ability to limit the amount of characters. 13 13 14 14 The jQuery character counter from [Tom Deater](http://tomdeater.com/) provides a user friendly way to limit the amount of characters while writing. … … 29 29 == Changelog == 30 30 31 = 0.7 = 32 * Fixes for PHP 7 33 34 = 0.6 = 35 * Progress bar on excerpt area 36 * Added options for meta box icons 37 * Preserve trailing ellipsis when trimming excerpts for length 38 31 39 = 0.5 = 32 40 * Moved to a new namespace (requires PHP 5.3)
Note: See TracChangeset
for help on using the changeset viewer.