Plugin Directory

Changeset 2420109


Ignore:
Timestamp:
11/17/2020 02:55:19 PM (5 years ago)
Author:
marcus.downing
Message:

Excerpt Tools: updates for PHP 7

Location:
excerpt-tools/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • excerpt-tools/trunk/excerpt-tools.php

    r958682 r2420109  
    88 Author: Marcus Downing and Zack Kakia
    99 Author URI: http://www.bang-on.net/
    10  Version: 0.5
     10 Version: 0.7
    1111 */
     12
     13if (!defined('EXCERPT_TOOLS_DEBUG'))
     14  define('EXCERPT_TOOLS_DEBUG', false);
    1215
    1316$jscounter = plugins_url( 'js/jquery.charcounter.js', __FILE__ );
     
    1821function box_init() {
    1922  $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);
    2124  $title = isset($options['excerpt_title']) ? trim((string) $options['excerpt_title']) : '';
    2225  if (empty($title) || !is_string($title)) $title = __('Excerpt', 'excerpt-tools');
    2326  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>&nbsp; ".$title;
     35      break;
     36    }
     37  }
    2538
    2639  foreach (get_post_types(array(), 'objects') as $post_type) {
    2740    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');
    3043      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);
    3150    }
    3251  }
     
    5069function init() {
    5170  $options = get_option('e_tools');
    52   if ($options['enforce_length']) {
     71  if (isset($options['enforce_length']) && $options['enforce_length']) {
    5372    add_filter('wp_trim_excerpt', __NAMESPACE__.'\filter_trim_excerpt', 11, 2);
    5473    add_filter('option_relevanssi_excerpt_length', __NAMESPACE__.'\option_relevanssi_excerpt_length', 99);
     
    5776
    5877function 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');
    6079
    6180  $options = get_option('e_tools');
     
    6382
    6483  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
    6594    $short = mb_substr($excerpt, 0, $len + 1);
    6695    $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);
    6897    if ($pos !== false && $pos > 0)
    6998      $excerpt = mb_substr($short, 0, $pos);
    7099    else
    71100      $excerpt = $short;
     101
     102    $excerpt = rtrim($excerpt);
     103    $excerpt = rtrim($excerpt, " :.");
     104    $excerpt = $excerpt.$affix;
    72105  }
    73106
     
    77110function option_relevanssi_excerpt_length($length) {
    78111  $options = get_option('e_tools');
    79   $len = intval($options['excerpt_length']);
     112  $len = intval($options['excerpt_length']) - 3; // make way for an ellipsis!
    80113  if ($length > $len) $length = $len;
    81114  return $length;
     
    83116
    84117// Draw the menu page itself
    85 function settings_page() { 
     118function settings_page() {
    86119  global $jscounter;
    87120  $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';
    88123
    89124?>
     
    93128    <?php settings_fields('e_tools_options'); ?>
    94129  <div class='metabox-holder'>
     130
     131    <div class='stack-group'>
     132      <div class='stack-block'>
     133
    95134  <div class='postbox'>
    96135
    97   <h3><?php _e('Options', 'excerpt-tools'); ?></h3>
    98   <div class='inside'>
     136  <h3><?php _e('Post types', 'excerpt-tools'); ?></h3>
    99137   <table class="form-table">
    100138      <?php
    101 
    102139        $post_types = array();
    103140        $post_type_icons = array(
     
    115152            $post_type_icons[$post_type->name] = $post_type->menu_icon;
    116153        }
    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
    120156        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 "> &nbsp;";
    128159          if (isset($post_type_icons[$key]))
    129             echo " &nbsp;<i class='dashicons ${post_type_icons[$key]}'></i>&nbsp; ";
     160            echo "<i class='dashicons ${post_type_icons[$key]}'></i>&nbsp; ";
    130161          echo $name;
    131162          echo "</td></tr>";
     
    140171
    141172        $enforce_length = isset($options['enforce_length']) && (boolean) $options['enforce_length'];
     173        $excerpt_length_html = isset($options['excerpt_length_html']) && (boolean) $options['excerpt_length_html'];
    142174      ?>
    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">
    144185      <tr valign="top">
    145186        <th scope="row"><?php _e('Excerpt Length', 'excerpt-tools'); ?></th>
    146187        <td><input type="text" name="e_tools[excerpt_length]" id='excerpt_length' value="<?php echo $length; ?>" placeholder='150' style='text-align: right; width: 4em;' /> &nbsp;characters
    147188          <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>
    149193      </tr>
    150194     
     
    155199     
    156200      <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>&nbsp;";
     210              if ($icon != 'none') echo "&nbsp; <i class='dashicons dashicons-$icon'></i> &nbsp; ";
     211              echo $name."<label></div>";
     212            }
     213          ?>
     214        </td>
     215      </tr>
     216     
     217      <tr valign="top">
    157218        <th scope="row"><?php _e('Excerpt description', 'excerpt-tools'); ?></th>
    158219        <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>
    160221        </td>
    161222      </tr>
     
    163224   
    164225  </div>
     226    </div></div>
    165227</div>
    166228
     
    177239
    178240
     241function 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
    179257function meta_box($post) {
     258  ?></div><div class='outside' style='padding-bottom: 8px;'><?php
     259
    180260  wp_enqueue_script('jquery');
    181261  $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');
    183264  $title = $options['excerpt_title'];
    184265  if (empty($title))
     
    189270    $length = 150;
    190271
     272  if (!empty($options['excerpt_text']))
     273    echo "<p>".$options['excerpt_text']."</p>";
    191274  ?>
    192   <div style='margin: -6px -12px 0 -12px;'>
     275
    193276    <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
    194277      echo $post->post_excerpt;
    195278    ?></textarea>
    196   </div>
    197   <?php
    198   if (!empty($options['excerpt_text']))
    199     echo "<p>".$options['excerpt_text']."</p>";
    200   ?>
    201279
    202280  <script type="text/javascript"  src="<?php echo plugins_url( 'js/jquery.charcounter.js', __FILE__ ); ?>"> </script>
     
    204282
    205283    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
    206294      $("#excerpt").charCounter( <?php  echo $length; ?>, {
    207295        container: "<div id='counter' class='counter' style='padding-top:5px; padding-left: 8px;'></div>",
    208296        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        },
    209315        format: "%1 characters remaining"
    210316      });
     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      });
    211326    });
    212327
  • excerpt-tools/trunk/js/jquery.charcounter.js

    r173577 r2420109  
    11/**
    2 
    32 *
    4 
    53 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
    6 
    74 * Licensed under the MIT License:
    8 
    95 * http://www.opensource.org/licenses/mit-license.php
    10 
    116 *
    12 
    137 */
    14 
    158 
    16 
    179(function($) {
    18 
    1910    /**
    20 
    2111     * attaches a character counter to each textarea element in the jQuery object
    22 
    2312     * usage: $("#myTextArea").charCounter(max, settings);
    24 
    2513     */
    26 
    2714   
    28 
    2915    $.fn.charCounter = function (max, settings) {
    30 
    3116        max = max || 100;
    32 
    3317        settings = $.extend({
    34 
    3518            container: "<span></span>",
    36 
    3719            classname: "charcounter",
    38 
    3920            format: "(%1 characters remaining)",
    40 
     21            callback: null,
    4122            pulse: true,
    42 
    4323            delay: 0
    44 
    4524        }, settings);
    46 
    4725        var p, timeout;
    48 
    4926       
    50 
    5127        function count(el, container) {
    52 
    5328            el = $(el);
    54 
    5529            if (el.val().length > max) {
    56 
    5730                el.val(el.val().substring(0, max));
    58 
    5931                if (settings.pulse && !p) {
    60 
    6132                    pulse(container, true);
    62 
    6333                };
    64 
    6534            };
    66 
    6735            if (settings.delay > 0) {
    68 
    6936                if (timeout) {
    70 
    7137                    window.clearTimeout(timeout);
    72 
    7338                }
    74 
    7539                timeout = window.setTimeout(function () {
    76 
    7740                    container.html(settings.format.replace(/%1/, (max - el.val().length)));
    78 
     41                    if (typeof settings.callback === 'function') { settings.callback(el.val().length, max); }
    7942                }, settings.delay);
    80 
    8143            } else {
    82 
    8344                container.html(settings.format.replace(/%1/, (max - el.val().length)));
    84 
     45                if (typeof settings.callback === 'function') { settings.callback(el.val().length, max); }
    8546            }
    86 
    8747        };
    88 
    8948       
    90 
    9149        function pulse(el, again) {
    92 
    9350            if (p) {
    94 
    9551                window.clearTimeout(p);
    96 
    9752                p = null;
    98 
    9953            };
    100 
    10154            el.animate({ opacity: 0.1 }, 100, function () {
    102 
    10355                $(this).animate({ opacity: 1.0 }, 100);
    104 
    10556            });
    106 
    10757            if (again) {
    108 
    10958                p = window.setTimeout(function () { pulse(el) }, 200);
    110 
    11159            };
    112 
    11360        };
    114 
    11561       
    116 
    11762        return this.each(function () {
    118 
    11963            var container = (!settings.container.match(/^<.+>$/))
    120 
    12164                ? $(settings.container)
    122 
    12365                : $(settings.container)
    124 
    12566                    .insertAfter(this)
    126 
    12767                    .addClass(settings.classname);
    128 
    12968            $(this)
    130 
    13169                .bind("keydown", function () { count(this, container); })
    132 
    13370                .bind("keypress", function () { count(this, container); })
    134 
    13571                .bind("keyup", function () { count(this, container); })
    136 
    13772                .bind("focus", function () { count(this, container); })
    138 
    13973                .bind("mouseover", function () { count(this, container); })
    140 
    14174                .bind("mouseout", function () { count(this, container); })
    142 
    14375                .bind("paste", function () {
    144 
    14576                    var me = this;
    146 
    14777                    setTimeout(function () { count(me, container); }, 10);
    148 
    14978                });
    150 
    15179            if (this.addEventListener) {
    152 
    15380                this.addEventListener('input', function () { count(this, container); }, false);
    154 
    15581            };
    156 
    15782            count(this, container);
    158 
    15983        });
    160 
    16184    };
    16285
    163 
    164 
    16586})(jQuery);
  • excerpt-tools/trunk/readme.txt

    r958682 r2420109  
    1 === Plugin Name ===
     1=== Excerpt Tools ===
    22Contributors: marcus.downing, zaybiz
    33Tags: excerpt, excerpt length, excerpt tools, jQuery, character limit
    44Requires at least: 2.8
    5 Tested up to: 3.9.1
     5Tested up to: 5.5.0
    66Stable tag: trunk
    77
     
    1010== Description ==
    1111
    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. 
     12A 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.
    1313
    1414The jQuery character counter from [Tom Deater](http://tomdeater.com/)  provides a user friendly way to limit the amount of characters while writing.
     
    2929== Changelog ==
    3030
     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
    3139= 0.5 =
    3240* Moved to a new namespace (requires PHP 5.3)
Note: See TracChangeset for help on using the changeset viewer.