Plugin Directory

Changeset 608028


Ignore:
Timestamp:
10/04/2012 09:16:40 PM (13 years ago)
Author:
johnleblanc
Message:

Added category support by request

Location:
docs-auto-tags/trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • docs-auto-tags/trunk/docs-auto-tags.php

    r466444 r608028  
    44Plugin URI: http://wordpress.org/extend/plugins/docs-auto-tags/
    55Description: Assigns tags to posts containing specific text strings, handy for filtering within the loop. See query_posts() in the Codex for including/excluding posts by tag.
    6 Version: 0.5
     6Version: 0.6
    77Author: John LeBlanc
    88Author URI: http://johnleblanc.com
     
    3333
    3434
    35 // set up constants
    36 if ( !defined('JPL_DOCS_AUTOTAGS_OPTION_NAME') )
    37     define(JPL_DOCS_AUTOTAGS_OPTION_NAME, 'docs_autotags_patterns');
    38 
    39 
    4035// internationalization
    4136if( !load_plugin_textdomain( 'docs-auto-tags', ABSPATH . WP_CONTENT_DIR . '/languages/') )
     
    4742function jpl_docs_autotags_options_init() {
    4843    register_setting( 'jpl_docs_autotags_options', 'docs_autotags' );
     44    register_setting( 'jpl_docs_autocategories_options', 'docs_autocategories' );
    4945}
    5046
     
    5551    $post = get_post($post_id);
    5652    if ( intval($post->ID) > 0 ) {
    57         $options = get_option( 'docs_autotags' );
    58         if ( count($options) > 0 ) {
    59             foreach ( $options as $pattern => $tags ) {
     53        // append tags
     54        $tag_options = get_option( 'docs_autotags' );
     55        if ( count($tag_options) > 0 ) {
     56            foreach ( $tag_options as $pattern => $tags ) {
    6057                if ( strpos($post->post_content, $pattern) )
    6158                    wp_set_post_tags( $post->ID, $tags, TRUE ); // TRUE means append
     59            }
     60        }
     61        // append categories
     62        $category_options = get_option( 'docs_autocategories' );
     63        if ( count($category_options) > 0 ) {
     64            foreach ( $category_options as $pattern => $categories ) {
     65                if ( strpos($post->post_content, $pattern) )
     66                    wp_set_object_terms( $post->ID, 'category', $categories, TRUE ); // TRUE means append
    6267            }
    6368        }
     
    7580
    7681    <div class="wrap">
     82
    7783        <h2><?php _e("Doc's Auto-Tags") ?></h2>
    78         <p><?php _e('Posts that include the (case-sensitive) string patterns defined below will be assigned the specified tag(s) when saved.') ?></p>
     84
     85        <p>Assign tags and/or categories to posts containing specific text strings.  Tags and/or categories are assigned when the post is saved, so new rules apply only to posts that are added or updated after those rules are added.</p>
     86
    7987        <form method="post" action="">
    80             <?php settings_fields( 'docs_autotags_options' ); ?>
     88
     89        <?php settings_fields( 'docs_autotags_options' ); ?>
     90        <?php settings_fields( 'docs_autocategories_options' ); ?>
     91
     92        <fieldset style="padding: 5px 20px 20px; margin: 10px 0; border: 1px #ccc solid">
     93            <legend style="font-size:150%"><?php _e('Tags') ?></legend>
     94            <p><?php _e('Posts that include the (case-sensitive) string patterns defined below will be assigned the specified tag(s) when saved.') ?></p>
    8195
    8296            <?php
    8397            // receive post data
    84             if ( isset($_POST['pattern']) ) {
     98            if ( isset($_POST['tag-pattern']) ) {
    8599                $i = 0;
    86                 while ( $i <= count( $_POST['pattern'] ) ) {
    87                     if ( strlen( trim($_POST['pattern'][$i]) ) > 0 && strlen( trim($_POST['tags'][$i]) ) > 0 )
    88                         $options[$_POST['pattern'][$i]] = $_POST['tags'][$i];
     100                while ( $i <= count( $_POST['tag-pattern'] ) ) {
     101                    if ( strlen( trim($_POST['tag-pattern'][$i]) ) > 0 && strlen( trim($_POST['tags'][$i]) ) > 0 )
     102                        $tag_options[$_POST['tag-pattern'][$i]] = $_POST['tags'][$i];
    89103                    $i++;
    90104                }
    91                 update_option( 'docs_autotags', $options );
     105                update_option( 'docs_autotags', $tag_options );
    92106            }
    93107            ?>
    94108
    95             <?php $options = get_option( 'docs_autotags' ); ?>
     109            <?php $tag_options = get_option( 'docs_autotags' ); ?>
    96110
    97111            <?php $i=0; ?>
    98             <?php if ( is_array($options) && count($options) > 0 ): ?>
    99             <?php foreach ( $options as $pattern => $tags ) : ?>
    100                 <div id="rule-<?php echo $i ?>">
     112            <?php if ( is_array($tag_options) && count($tag_options) > 0 ): ?>
     113            <?php foreach ( $tag_options as $pattern => $tags ) : ?>
     114                <div id="tag-rule-<?php echo $i ?>">
    101115                    <p>
    102                         <label for="pattern[<?php echo $i ?>]"><?php _e('Pattern') ?></label> <input name="pattern[<?php echo $i ?>]" value="<?php echo $pattern ?>" />
    103                         <label for="tags[<?php echo $i ?>]"><?php _e('Tags') ?></label> <input class="suggest" name="tags[<?php echo $i ?>]" value="<?php echo $tags ?>" />
    104                         <a href="#" class="delete" onclick="jQuery('#rule-<?php echo $i ?>').remove();return false">delete</a>
     116                        <label for="tag-pattern[<?php echo $i ?>]"><?php _e('Pattern') ?></label> <input name="tag-pattern[<?php echo $i ?>]" value="<?php echo $pattern ?>" />
     117                        <label for="tags[<?php echo $i ?>]"><?php _e('Tags') ?></label> <input class="suggest-tag" name="tags[<?php echo $i ?>]" value="<?php echo $tags ?>" />
     118                        <a href="#" class="delete" onclick="jQuery('#tag-rule-<?php echo $i ?>').remove();return false">delete</a>
    105119                    </p>
    106120                </div>
     
    109123            <?php endif; ?>
    110124
    111             <div id="rule-new">
     125            <div id="tag-rule-new">
    112126                <p>
    113                     <label for="pattern[<?php echo $i ?>]"><?php _e('Pattern') ?></label> <input name="pattern[<?php echo $i ?>]" value="" />
    114                     <label for="tags[<?php echo $i ?>]"><?php _e('Tags') ?></label> <input class="suggest" name="tags[<?php echo $i ?>]" value="" />
     127                    <label for="tag-pattern[<?php echo $i ?>]"><?php _e('Pattern') ?></label> <input name="tag-pattern[<?php echo $i ?>]" value="" />
     128                    <label for="tags[<?php echo $i ?>]"><?php _e('Tags') ?></label> <input class="suggest-tag" name="tags[<?php echo $i ?>]" value="" />
    115129                </p>
    116130            </div>
    117 
    118             <p class="submit">
    119                 <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
    120             </p>
     131        </fieldset>
     132
     133        <fieldset style="padding: 5px 20px 20px; margin: 10px 0; border: 1px #ccc solid">
     134            <legend style="font-size:150%"><?php _e('Categories') ?></legend>
     135            <p><?php _e('Posts that include the (case-sensitive) string patterns defined below will be assigned the specified category(s) when saved.') ?></p>
     136
     137            <?php
     138            // receive post data
     139            if ( isset($_POST['category-pattern']) ) {
     140                $i = 0;
     141                while ( $i <= count( $_POST['category-pattern'] ) ) {
     142                    if ( strlen( trim($_POST['category-pattern'][$i]) ) > 0 && strlen( trim($_POST['categories'][$i]) ) > 0 )
     143                        $category_options[$_POST['category-pattern'][$i]] = $_POST['categories'][$i];
     144                    $i++;
     145                }
     146                update_option( 'docs_autocategories', $category_options );
     147            }
     148            ?>
     149
     150            <?php $category_options = get_option( 'docs_autocategories' ); ?>
     151
     152            <?php $i=0; ?>
     153            <?php if ( is_array($category_options) && count($category_options) > 0 ): ?>
     154            <?php foreach ( $category_options as $pattern => $categories ) : ?>
     155                <div id="category-rule-<?php echo $i ?>">
     156                    <p>
     157                        <label for="category-pattern[<?php echo $i ?>]"><?php _e('Pattern') ?></label> <input name="category-pattern[<?php echo $i ?>]" value="<?php echo $pattern ?>" />
     158                        <label for="categories[<?php echo $i ?>]"><?php _e('Categories') ?></label> <input class="suggest-category" name="categories[<?php echo $i ?>]" value="<?php echo $categories ?>" />
     159                        <a href="#" class="delete" onclick="jQuery('#category-rule-<?php echo $i ?>').remove();return false">delete</a>
     160                    </p>
     161                </div>
     162                <?php $i++; ?>
     163            <?php endforeach; ?>
     164            <?php endif; ?>
     165
     166            <div id="category-rule-new">
     167                <p>
     168                    <label for="category-pattern[<?php echo $i ?>]"><?php _e('Pattern') ?></label> <input name="category-pattern[<?php echo $i ?>]" value="" />
     169                    <label for="categories[<?php echo $i ?>]"><?php _e('Categories') ?></label> <input class="suggest-category" name="categories[<?php echo $i ?>]" value="" />
     170                </p>
     171            </div>
     172        </fieldset>
     173
     174        <p class="submit">
     175            <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
     176        </p>
    121177        </form>
    122178    </div>
     
    131187    wp_enqueue_script( 'suggest' );
    132188}
     189
    133190
    134191add_action('admin_head', 'jpl_docs_autotag_admin_head');
     
    137194    <script type="text/javascript">
    138195        jQuery(function() {
    139             jQuery('.suggest').suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=post_tag", {multiple:true, multipleSep: ","});
     196            jQuery('.suggest-tag').suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=post_tag", {multiple:true, multipleSep: ","});
     197            jQuery('.suggest-category').suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=category", {multiple:true, multipleSep: ","});
    140198        });
    141199    </script>
  • docs-auto-tags/trunk/readme.txt

    r466444 r608028  
    44Tags: tags
    55Requires at least: 2.8
    6 Tested up to: 3.1.1
     6Tested up to: 3.4.2
    77Stable tag: 1.0
    88
    9 Assigns tags to posts containing specific text strings, handy for filtering within the loop.
     9Assigns tags and/or categories to posts containing specific text strings, handy for filtering within the loop.
    1010
    1111== Description ==
     
    19191. Configure your settings within the 'Tools' area in the Dashboard
    2020
     21== Screenshots ==
     22
     231. Configuration interface where text strings and corresponding tags (and/or categories) are defined: Tools -> Doc's Auto-Tags
     24
    2125== Changelog ==
     26
     27= 0.6 =
     28Added Category support by request.
    2229
    2330= 0.5 =
     
    4350== Frequently Asked Questions ==
    4451
    45 Great, my newly saved posts are being auto-tagged.  How do I filter posts by tag within the WordPress Loop?
     52= Great, my newly saved posts are being auto-tagged.  How do I filter posts by tag within the WordPress Loop? =
    4653
    4754An example of query_posts():
Note: See TracChangeset for help on using the changeset viewer.