Changeset 37989

Show
Ignore:
Timestamp:
04/03/08 10:21:02 (4 months ago)
Author:
ketsugi
Message:

Major overhaul to Acronyms. Not stable yet! Edit functions not working properly.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • acronyms/trunk/acronyms.php

    r7253 r37989  
    33Plugin Name: Acronyms 
    44Plugin URI: http://ketsugi.com/software/wordpress/acronyms-plugin/ 
    5 Description: A plugin to wrap acronyms in posts and comments with appropriate <acronym> tags. Allows users to manage lists of acronyms through admin interface. Based on Joel Bennett's Acronym Replacer plugin (http://www.huddledmasses.org/) and Joel Pan's NP_Acronym plugin for Nucleus CMS. 
     5Description: A plugin to wrap acronyms in posts and comments with appropriate <acronym> acronyms. Allows users to manage lists of acronyms through admin interface. Based on Joel Bennett's Acronym Replacer plugin (http://www.huddledmasses.org/) and Joel Pan's NP_Acronym plugin for Nucleus CMS. 
    66Version: 1.0 
    77Author: Joel Pan 
    88Author URI: http://ketsugi.com/ 
    99 
    10   Copyright 2006  Joel Pan <spamtastic@ketsugi.com> 
     10  Copyright 2008  Joel Pan <spamtastic@ketsugi.com> 
    1111 
    1212    This program is free software; you can redistribute it and/or modify 
     
    145145                  'SVN' => 'Subversion', 
    146146                  'TIA' => 'Thanks In Advance', 
    147                   'TIFF' => 'Tagged Image File Format', 
     147                  'TIFF' => 'acronymged Image File Format', 
    148148                  'TLD' => 'Top Level Domain', 
    149149                  'TOC' => 'Table of Contents', 
     
    183183      } 
    184184       
     185      /** 
     186       * Adds the Management page to WordPress 
     187       *  
     188       * @return void 
     189       **/ 
     190      function add_pages () 
     191      { 
     192            // Add the Manage page 
     193            add_management_page(__('Manage Acronyms'), __('Acronyms'), "manage_options", __FILE__, array( 'Acronyms', 'manage_acronyms') ); 
     194      } 
     195       
     196      /** 
     197       * Manage Acronyms page 
     198       *  
     199       * @return void 
     200       **/ 
     201      function manage_acronyms () 
     202      { 
     203            $title = __('Acronyms'); 
     204            $parent_file = 'edit.php?page=acronyms.php'; 
     205            $acronyms = get_option( 'acronym_acronyms' ); 
     206 
     207            wp_reset_vars(array('action', 'acronym')); 
     208 
     209            if ( isset($_POST['deleteit']) ) 
     210                  $action = 'delete'; 
     211            else $action = $_POST['action']; 
     212 
     213            switch ($action) { 
     214                  case 'add': 
     215                        check_admin_referer('add-acronym'); 
     216                        $acronym = $_POST['acronym']; 
     217                        $fulltext = $_POST['fulltext']; 
     218                        Acronyms::update ( $acronym, $fulltext ); 
     219                        $message = 1; 
     220                        break; 
     221                  case 'edit': 
     222                        $acronym = $_POST['acronym']; 
     223                        $fulltext = $_POST['fulltext']; 
     224                        check_admin_referer('edit-acronym_' . $acronym); 
     225                        Acronyms::update ( $acronym, $fulltext ); 
     226                        $message = 3; 
     227                        break; 
     228              case 'delete': 
     229                        check_admin_referer('bulk-acronyms'); 
     230 
     231            if ( !current_user_can('manage_categories') ) 
     232                  wp_die(__('Cheatin&#8217; uh?')); 
     233 
     234                $acronyms = $_POST['delete_acronyms']; 
     235            foreach( (array) $acronyms as $acronym ) { 
     236                  Acronyms::delete ( $acronym ); 
     237                } 
     238                 
     239                if ( 1 < ( count( $acronyms ) ) ) 
     240                   $message = 6; 
     241            else $message = 2; 
     242 
     243                    break; 
     244            } 
     245 
     246            $messages[1] = __('Acronym added.'); 
     247            $messages[2] = __('Acronym deleted.'); 
     248            $messages[3] = __('Acronym updated.'); 
     249            $messages[4] = __('Acronym not added.'); 
     250            $messages[5] = __('Acronym not updated.'); 
     251            $messages[6] = __('Acronyms deleted.'); 
     252 
     253            if ( isset( $message ) ) { ?> 
     254<div id="message" class="updated fade"><p><?php echo $messages[$message] ?></p></div> 
     255<?php } 
     256            if ( 'edit' == $_GET['action'] && !empty( $_GET['acronym'] ) ) { 
     257                  Acronyms::add_acronym_form ( urldecode( $_GET['acronym'] ), urldecode( $_GET['fulltext'] ) ); 
     258                  echo '<br class="clear"/><br class="clear"/>'; 
     259            } 
     260?> 
     261<div class="wrap"> 
     262  <form id="posts-filter" action="" method="post"> 
     263  <input type="hidden" name="page" value="acronyms.php" /> 
     264        <h2><?php _e('Manage Acronyms') ?> (<a href="#addacronym"><?php _e('add new')?></a>)</h2> 
     265            <p id="post-search"> 
     266                  <input type="text" id="post-search-input" name="s" value="<?php echo attribute_escape(stripslashes($_POST['s'])); ?>" /> 
     267                  <input type="submit" value="<?php _e( 'Search acronyms' ); ?>" class="button" /> 
     268            </p> 
     269      <br class="clear" /> 
     270 
     271  <div class="tablenav"> 
     272    <div class="alignleft"> 
     273      <input type="submit" value="<?php _e('Delete') ?>" name="deleteit" class="button-secondary delete" /> 
     274      <?php wp_nonce_field('bulk-acronyms'); ?> 
     275            </div> 
     276    <br class="clear" /> 
     277  </div> 
     278  <br class="clear" /> 
     279<?php Acronyms::show_acronym_list($_POST['s']) ?> 
     280  </form> 
     281</div><?php 
     282            if ( 'edit' != $_GET['action'] || empty( $_GET['acronym'] ) ) { 
     283                  echo '<br class="clear"/><br class="clear"/>'; 
     284                  Acronyms::add_acronym_form ( '', '' ); 
     285            } 
     286      } 
     287 
    185288      /* string acronym_replace ( string $text, array $acronyms ) 
    186289       *  
    187        * Replaces known acronyms in $text with appropriate <acronym> tags. 
     290       * Replaces known acronyms in $text with appropriate <acronym> acronyms. 
    188291       * Note: acronym replacement is case-sensitive. 
    189292       */ 
     
    203306      { 
    204307?> 
    205 <style type="text/css"> 
    206 <!-- 
    207 .acronyms div#acronym_form { 
    208       padding: 1em; 
    209       border: 1px solid #ccc; 
    210       width: auto; 
    211 } 
    212  
    213 .acronyms table { 
    214       width: 100%; 
    215       border: 1px solid #ccc; 
    216 } 
    217  
    218 .acronyms table td { 
    219       padding: 0 5px; 
    220 } 
    221  
    222 .acronyms table tr.alt { 
    223       background-color: #eee; 
    224 } 
    225  
    226 .acronyms table td form { 
    227       text-align: center; 
    228 } 
    229 --> 
    230 </style> 
     308<script type='text/javascript' src='<?php bloginfo('wpurl')?>/wp-admin/js/forms.js?ver=20080317'></script> 
    231309<?php 
    232310      } 
    233311 
    234       /* void manage_acronyms () 
    235        * 
    236        * Display the admin interface to add, edit and delete acronyms 
    237        */ 
    238       function manage_acronyms () 
    239       { 
    240 ?> 
    241 <div class="wrap acronyms"> 
    242       <h2><?php _e('Manage acronyms') ?></h2> 
    243 <?php 
    244             $action = $_POST['action']; 
    245             switch ( $action ) {     
    246                   case 'delete': 
    247                         $acronym = $_POST['acronym_acronym']; 
    248                         if ( '' != $acronym) 
    249                         { 
    250                               //Check for confirmation 
    251                               if ( isset( $_POST['confirm'] ) && 'true' == $_POST['confirm']) 
    252                               { 
    253                                     Acronyms::delete( $acronym ); 
    254 ?> 
    255       <div id="message" class="updated fade"> 
    256             <p>The acronym for "<?php echo $acronym ?>" has been deleted.</p> 
    257       </div> 
    258 <?php 
    259                                     Acronyms::show_acronym_list(); 
    260 ?> 
    261 </div> 
    262 <?php 
    263                               } 
    264                               else 
    265                               { 
    266 ?> 
    267       <div id="acronym_form"> 
    268             <h3><?php _e('Delete acronym') ?></h3> 
    269             <p><?php _e('Are you sure you want to delete this acronym?') ?></p> 
    270             <form method="post" action=""> 
    271                   <input type="hidden" name="action" value="delete" /> 
    272                   <input type="hidden" name="acronym_acronym" value="<?php echo $acronym ?>" /> 
    273                   <input type="hidden" name="confirm" value="true" /> 
    274                   <input type="submit" value="<?php _e('Delete') ?>" /> 
    275             </form> 
    276             <form method="post" action=""> 
    277                   <input type="hidden" name="action" value="delete" /> 
    278       <input type="hidden" name="acronym_acronym" value="<?php echo $acronym ?>" /> 
    279       <input type="hidden" name="confirm" value="false" /> 
    280       <input type="submit" value="<?php _e('Cancel') ?>" /> 
    281             </form> 
    282       </div> 
    283 <?php 
    284                               } 
    285                         }      
    286                         break; 
    287                   case 'update': 
    288                         $acronym = $_POST['acronym_acronym']; 
    289                         $fulltext = $_POST['acronym_fulltext']; 
    290                         if ( '' != $acronym && '' != $fulltext ) 
    291                         { 
    292                               if ( str_word_count( $acronym ) > 1 ) 
    293                               { 
    294 ?> 
    295       <div id="message" class="error fade"> 
    296             <p>The acronym must be a single word.</p> 
    297       </div> 
    298 <?php 
    299                               } 
    300                               else  
    301                               { 
    302                                     Acronyms::update( $acronym, $fulltext ); 
    303 ?> 
    304       <div id="message" class="updated fade"> 
    305             <p>The acronym "<?php echo $acronym ?>" has been added or edited.</p> 
    306       </div> 
    307 <?php 
    308                               } 
    309                         } 
    310                         else 
    311                         { 
    312 ?> 
    313       <div id="message" class="error fade"> 
    314             <p>Please fill in both the acronym and full text fields.</p> 
    315       </div> 
    316 <?php 
    317                         } 
    318                         Acronyms::show_acronym_list(); 
    319 ?> 
    320 </div> 
    321 <?php 
    322                         break; 
    323                   default: 
    324                         Acronyms::show_acronym_list(); 
    325 ?> 
    326 </div> 
    327 <?php  
    328             } 
    329       } 
    330  
    331       function show_acronym_list () 
    332       { 
    333 ?> 
    334       <form method="post" action=""> 
    335             <div id="acronym_form"> 
    336                   <h3><?php _e('Add/Edit acronym') ?></h3> 
    337                   <label for="acronym_acronym"><?php _e('Acronym') ?>: </label><input type="text" id="acronym_acronym" name="acronym_acronym" /> 
    338                   <label for="acronym_fulltext"><?php _e('Full text')?>: </label><input type="text" id="acronym_fulltext" name="acronym_fulltext" /> 
    339                   <input type="hidden" name="action" value="update" /> 
    340                   <input type="submit" value="<?php _e('Add/Edit acronym') ?>" /> 
    341             </div> 
    342       </form> 
    343        
    344       <h3><?php _e('Acronym List') ?></h3> 
    345       <table cellpadding="0" cellspacing="0"> 
     312      function show_acronym_list ( $s ) 
     313      { 
     314?> 
     315      <table class="widefat"> 
     316      <thead> 
     317      <tr> 
     318    <th scope="col" class="check-column"><input type="checkbox" onclick="checkAll(document.getElementById('posts-filter'));" /></th> 
     319    <th scope="col"><?php _e('Acronym') ?></th> 
     320    <th scope="col"><?php _e('Full') ?></th> 
     321      </tr> 
     322      </thead> 
     323      <tbody id="the-list" class="list:acronym"> 
    346324<?php 
    347325            $acronyms = get_option( 'acronym_acronyms' ); 
    348326            foreach ( $acronyms as $acronym => $fulltext ) 
    349327            { 
    350 ?> 
    351             <tr<?php if (0 == $i++ % 2) echo ' class="alt"' ?>> 
    352                   <td><?php echo $acronym ?></td> 
     328                  if ( ( '' == $s ) || ( ( false !== strpos( strtolower( $acronym ), strtolower( $s ) ) ) || ( false !== strpos( strtolower( $fulltext ), strtolower( $s ) ) ) ) ) { 
     329?> 
     330            <tr<?php if (0 == $i++ % 2) echo ' class="alternate"' ?>> 
     331              <th scope="row" class="check-column"> <input type="checkbox" name="delete_acronyms[]" value="<?php echo $acronym ?>" /></th> 
     332                  <td><strong> 
     333                    <a class="row-title" href="edit.php?page=acronyms.php&amp;action=edit&amp;acronym=<?php echo urlencode( $acronym ) ?>&amp;fulltext=<?php echo urlencode( $fulltext ) ?>" title="Edit &quot;<?php echo $acronym ?>&quot;"> 
     334                      <?php echo $acronym ?> 
     335                    </a></strong></td> 
    353336                  <td><?php echo $fulltext ?></td> 
    354                   <td> 
    355                         <form method="post" action=""> 
    356                               <input type="hidden" name="action" value="delete" /> 
    357                               <input type="hidden" name="acronym_acronym" value="<?php echo $acronym ?>" /> 
    358                               <input type="submit" value="<?php _e('Delete') ?>" /> 
    359                         </form> 
    360                   </td> 
    361337            </tr> 
    362338<?php 
    363             } 
    364 ?> 
     339                  } 
     340            } 
     341?> 
     342  </tbody> 
    365343      </table> 
    366344<?php 
    367345      } 
     346       
     347      /* void add_acronym_form ( string $acronym, string $fulltext )  
     348       * 
     349       * Display the form for adding or editing acronyms 
     350       */ 
     351  function add_acronym_form ( $acronym, $fulltext ) 
     352      { 
     353            if ( ! empty($acronym) ) { 
     354                  $heading = __('Edit Acronym'); 
     355                  $submit_text = __('Edit Acronym'); 
     356                  $form = '<form name="editacronym" id="editacronym" method="post" action="" class="validate">'; 
     357                  $action = 'edit'; 
     358                  $nonce_action = 'edit-acronym_' . $acronym; 
     359            } else { 
     360                  $heading = __('Add Acronym'); 
     361                  $submit_text = __('Add Acronym'); 
     362                  $form = '<form name="addacronym" id="addacronym" method="post" action="" class="add:the-list: validate">'; 
     363                  $action = 'add'; 
     364                  $nonce_action = 'add-acronym'; 
     365            } 
     366?> 
     367<div class="wrap"> 
     368<h2><?php echo $heading ?></h2> 
     369<div id="ajax-response"></div> 
     370<?php echo $form ?> 
     371<input type="hidden" name="page" value="acronyms.php" /> 
     372<input type="hidden" name="action" value="<?php echo $action ?>" /> 
     373<?php wp_original_referer_field(true, 'previous'); wp_nonce_field($nonce_action); ?> 
     374      <table class="form-table"> 
     375            <tr class="form-field form-required"> 
     376                  <td><label for="acronym"><?php _e('Acronym') ?></label>: <input name="acronym" id="acronym" type="text" value="<?php echo attribute_escape($acronym); ?>" size="20" <?php if ( 'edit' == $action ) echo 'disabled="disabled"'; ?>/></td> 
     377                  <td><label for="fulltext"><?php _e('Full') ?></label>: <input name="fulltext" id="fulltext" type="text" value="<?php echo attribute_escape($fulltext); ?>" size="80" /></td> 
     378            </tr> 
     379      </table> 
     380<p class="submit"><input type="submit" class="button" name="submit" value="<?php echo $submit_text ?>" /></p> 
     381</form> 
     382</div> 
     383<?php } 
    368384 
    369385      /* void update ( string $acronym, string $fulltext ) 
     
    391407                  update_option( 'acronym_acronyms', $acronyms ); 
    392408            } 
    393       } 
    394  
    395       function add_pages () 
    396       { 
    397             add_management_page( 'Acronyms', 'Acronyms', 7, __FILE__, array( 'Acronyms', 'manage_acronyms' ) ); 
    398409      } 
    399410 
     
    406417if (1 == get_option( 'acronym_comments' ) )      
    407418      add_filter( 'comment_text', array( 'Acronyms', 'acronym_replace' ) ); 
    408 add_action( 'admin_head', array( 'Acronyms', 'manage_acronyms_css') ); 
    409419add_action( 'admin_menu', array( 'Acronyms', 'add_pages' ) );