Plugin Directory

Changeset 958672


Ignore:
Timestamp:
08/01/2014 01:51:32 PM (12 years ago)
Author:
marcus.downing
Message:

Release 0.5 (and tag 0.4 correctly)

Location:
excerpt-tools
Files:
2 edited
10 copied

Legend:

Unmodified
Added
Removed
  • excerpt-tools/tags/0.4/excerpt-tools.php

    r926472 r958672  
    22 /*
    33 Plugin Name: Excerpt Tools
     4 Plugin URI: http://www.bang-on.net/
    45 Description: Customize your excerpts. Allows you to limit the length of excerpts with a jQuery character counter, display a custom title and description for the excerpt box and show the excerpt box on pages.
    56 Author: Marcus Downing and Zack Kakia
     7 Author URI: http://www.bang-on.net/
    68 Version: 0.4
    79 */
  • excerpt-tools/tags/0.5/excerpt-tools.php

    r926472 r958672  
    11<?php
     2
     3namespace Bang\excerpt_tools;
    24 /*
    35 Plugin Name: Excerpt Tools
     6 Plugin URI: http://www.bang-on.net/
    47 Description: Customize your excerpts. Allows you to limit the length of excerpts with a jQuery character counter, display a custom title and description for the excerpt box and show the excerpt box on pages.
    58 Author: Marcus Downing and Zack Kakia
    6  Version: 0.4
     9 Author URI: http://www.bang-on.net/
     10 Version: 0.5
    711 */
    812
    913$jscounter = plugins_url( 'js/jquery.charcounter.js', __FILE__ );
    1014
    11 add_action('admin_init', 'e_tools_box_init');
     15add_action('admin_init', __NAMESPACE__.'\box_init');
    1216
    1317// Checks options to see if custom excerpts are turned on. if so, adds them
    14 function e_tools_box_init() {
     18function box_init() {
    1519  $options = get_option('e_tools');
    1620  do_action('log', 'Excerpt tools: init', $options);
     
    2125
    2226  foreach (get_post_types(array(), 'objects') as $post_type) {
    23     if ($options['enable_'.$post_type->name] == 1) {
     27    if (isset($options['enable_'.$post_type->name]) && $options['enable_'.$post_type->name] == 1) {
    2428      do_action('log', 'Excerpt tools: Add meta box', $post_type->name, $title);
    2529      remove_meta_box('postexcerpt', $post_type->name, 'core');
    26       add_meta_box('e_tools_excerpt', $title, 'e_tools_meta_box', $post_type->name, 'normal', 'high');
     30      add_meta_box('e_tools_excerpt', $title, __NAMESPACE__.'\meta_box', $post_type->name, 'normal', 'high');
    2731    }
    2832  }
    2933}
    3034 
    31 add_action('admin_init', 'e_tools_init');
    32 add_action('admin_menu', 'e_tools_add_page');
     35add_action('init', __NAMESPACE__.'\init');
     36add_action('admin_init', __NAMESPACE__.'\admin_init');
     37add_action('admin_menu', __NAMESPACE__.'\add_page');
    3338
    3439// Init plugin options to white list our options
    35 function e_tools_init() {
     40function admin_init() {
    3641   register_setting('e_tools_options', 'e_tools');
    3742}
    3843
    3944// Add menu page
    40 function e_tools_add_page() {
    41    add_options_page('Excerpt Options', 'Excerpt Tools', 'manage_options', 'e_tools_handler', 'e_tools_page');
     45function add_page() {
     46   add_options_page('Excerpt Options', 'Excerpt Tools', 'manage_options', 'e_tools_handler', __NAMESPACE__.'\settings_page');
     47}
     48
     49// Check if we need to adjust all excerpts
     50function init() {
     51  $options = get_option('e_tools');
     52  if ($options['enforce_length']) {
     53    add_filter('wp_trim_excerpt', __NAMESPACE__.'\filter_trim_excerpt', 11, 2);
     54    add_filter('option_relevanssi_excerpt_length', __NAMESPACE__.'\option_relevanssi_excerpt_length', 99);
     55  }
     56}
     57
     58function filter_trim_excerpt($excerpt, $raw) {
     59  do_action('log', 'wp_trim_excerpt', '@filter', 'wp_trim_excerpt');
     60
     61  $options = get_option('e_tools');
     62  $len = intval($options['excerpt_length']);
     63
     64  if (mb_strlen($excerpt) > $len) {
     65    $short = mb_substr($excerpt, 0, $len + 1);
     66    $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);
     68    if ($pos !== false && $pos > 0)
     69      $excerpt = mb_substr($short, 0, $pos);
     70    else
     71      $excerpt = $short;
     72  }
     73
     74  return $excerpt;
     75}
     76
     77function option_relevanssi_excerpt_length($length) {
     78  $options = get_option('e_tools');
     79  $len = intval($options['excerpt_length']);
     80  if ($length > $len) $length = $len;
     81  return $length;
    4282}
    4383
    4484// Draw the menu page itself
    45 function e_tools_page() {
     85function settings_page() {
    4686  global $jscounter;
    4787  $options = get_option('e_tools');
     
    85125          $i++;
    86126          echo "<td><label for='e_tools_enable_$key'>";
    87           echo "<input type='checkbox' name='e_tools[enable_$key]' id='e_tools_enable_$key' value='1' "; checked('1', $options["enable_$key"]); echo ">";
     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 ">";
    88128          if (isset($post_type_icons[$key]))
    89129            echo " &nbsp;<i class='dashicons ${post_type_icons[$key]}'></i>&nbsp; ";
     
    98138            $length = $len;
    99139        }
     140
     141        $enforce_length = isset($options['enforce_length']) && (boolean) $options['enforce_length'];
    100142      ?>
    101143     
    102144      <tr valign="top">
    103145        <th scope="row"><?php _e('Excerpt Length', 'excerpt-tools'); ?></th>
    104         <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</td>
     146        <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
     147          <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>
    105149      </tr>
    106150     
     
    133177
    134178
    135 function e_tools_meta_box($post) {
     179function meta_box($post) {
    136180  wp_enqueue_script('jquery');
    137181  $options = get_option('e_tools');
  • excerpt-tools/tags/0.5/readme.txt

    r926472 r958672  
    20203. Configure your options under the "Excerpt Tools" item in the Settings menu.
    2121
     22=== Requirements ===
     23
     24Note that from version 0.5, this plugin requires PHP version 5.3 or above.
     25
    2226==Screenshots==
    2327
     
    2630
    2731== Changelog ==
     32
     33= 0.5 =
     34* Moved to a new namespace (requires PHP 5.3)
     35* Added option to enforce excerpt length
     36* Set excerpt length with Relevanssi as well
    2837
    2938= 0.4 =
  • excerpt-tools/trunk/excerpt-tools.php

    r926472 r958672  
    11<?php
     2
     3namespace Bang\excerpt_tools;
    24 /*
    35 Plugin Name: Excerpt Tools
     6 Plugin URI: http://www.bang-on.net/
    47 Description: Customize your excerpts. Allows you to limit the length of excerpts with a jQuery character counter, display a custom title and description for the excerpt box and show the excerpt box on pages.
    58 Author: Marcus Downing and Zack Kakia
    6  Version: 0.4
     9 Author URI: http://www.bang-on.net/
     10 Version: 0.5
    711 */
    812
    913$jscounter = plugins_url( 'js/jquery.charcounter.js', __FILE__ );
    1014
    11 add_action('admin_init', 'e_tools_box_init');
     15add_action('admin_init', __NAMESPACE__.'\box_init');
    1216
    1317// Checks options to see if custom excerpts are turned on. if so, adds them
    14 function e_tools_box_init() {
     18function box_init() {
    1519  $options = get_option('e_tools');
    1620  do_action('log', 'Excerpt tools: init', $options);
     
    2125
    2226  foreach (get_post_types(array(), 'objects') as $post_type) {
    23     if ($options['enable_'.$post_type->name] == 1) {
     27    if (isset($options['enable_'.$post_type->name]) && $options['enable_'.$post_type->name] == 1) {
    2428      do_action('log', 'Excerpt tools: Add meta box', $post_type->name, $title);
    2529      remove_meta_box('postexcerpt', $post_type->name, 'core');
    26       add_meta_box('e_tools_excerpt', $title, 'e_tools_meta_box', $post_type->name, 'normal', 'high');
     30      add_meta_box('e_tools_excerpt', $title, __NAMESPACE__.'\meta_box', $post_type->name, 'normal', 'high');
    2731    }
    2832  }
    2933}
    3034 
    31 add_action('admin_init', 'e_tools_init');
    32 add_action('admin_menu', 'e_tools_add_page');
     35add_action('init', __NAMESPACE__.'\init');
     36add_action('admin_init', __NAMESPACE__.'\admin_init');
     37add_action('admin_menu', __NAMESPACE__.'\add_page');
    3338
    3439// Init plugin options to white list our options
    35 function e_tools_init() {
     40function admin_init() {
    3641   register_setting('e_tools_options', 'e_tools');
    3742}
    3843
    3944// Add menu page
    40 function e_tools_add_page() {
    41    add_options_page('Excerpt Options', 'Excerpt Tools', 'manage_options', 'e_tools_handler', 'e_tools_page');
     45function add_page() {
     46   add_options_page('Excerpt Options', 'Excerpt Tools', 'manage_options', 'e_tools_handler', __NAMESPACE__.'\settings_page');
     47}
     48
     49// Check if we need to adjust all excerpts
     50function init() {
     51  $options = get_option('e_tools');
     52  if ($options['enforce_length']) {
     53    add_filter('wp_trim_excerpt', __NAMESPACE__.'\filter_trim_excerpt', 11, 2);
     54    add_filter('option_relevanssi_excerpt_length', __NAMESPACE__.'\option_relevanssi_excerpt_length', 99);
     55  }
     56}
     57
     58function filter_trim_excerpt($excerpt, $raw) {
     59  do_action('log', 'wp_trim_excerpt', '@filter', 'wp_trim_excerpt');
     60
     61  $options = get_option('e_tools');
     62  $len = intval($options['excerpt_length']);
     63
     64  if (mb_strlen($excerpt) > $len) {
     65    $short = mb_substr($excerpt, 0, $len + 1);
     66    $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);
     68    if ($pos !== false && $pos > 0)
     69      $excerpt = mb_substr($short, 0, $pos);
     70    else
     71      $excerpt = $short;
     72  }
     73
     74  return $excerpt;
     75}
     76
     77function option_relevanssi_excerpt_length($length) {
     78  $options = get_option('e_tools');
     79  $len = intval($options['excerpt_length']);
     80  if ($length > $len) $length = $len;
     81  return $length;
    4282}
    4383
    4484// Draw the menu page itself
    45 function e_tools_page() {
     85function settings_page() {
    4686  global $jscounter;
    4787  $options = get_option('e_tools');
     
    85125          $i++;
    86126          echo "<td><label for='e_tools_enable_$key'>";
    87           echo "<input type='checkbox' name='e_tools[enable_$key]' id='e_tools_enable_$key' value='1' "; checked('1', $options["enable_$key"]); echo ">";
     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 ">";
    88128          if (isset($post_type_icons[$key]))
    89129            echo " &nbsp;<i class='dashicons ${post_type_icons[$key]}'></i>&nbsp; ";
     
    98138            $length = $len;
    99139        }
     140
     141        $enforce_length = isset($options['enforce_length']) && (boolean) $options['enforce_length'];
    100142      ?>
    101143     
    102144      <tr valign="top">
    103145        <th scope="row"><?php _e('Excerpt Length', 'excerpt-tools'); ?></th>
    104         <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</td>
     146        <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
     147          <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>
    105149      </tr>
    106150     
     
    133177
    134178
    135 function e_tools_meta_box($post) {
     179function meta_box($post) {
    136180  wp_enqueue_script('jquery');
    137181  $options = get_option('e_tools');
  • excerpt-tools/trunk/readme.txt

    r926472 r958672  
    20203. Configure your options under the "Excerpt Tools" item in the Settings menu.
    2121
     22=== Requirements ===
     23
     24Note that from version 0.5, this plugin requires PHP version 5.3 or above.
     25
    2226==Screenshots==
    2327
     
    2630
    2731== Changelog ==
     32
     33= 0.5 =
     34* Moved to a new namespace (requires PHP 5.3)
     35* Added option to enforce excerpt length
     36* Set excerpt length with Relevanssi as well
    2837
    2938= 0.4 =
Note: See TracChangeset for help on using the changeset viewer.