pierovdfn

Entry List Alpha 2 [Flatpress]

Jul 11th, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.37 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Entry List
  4. Version: 1.0
  5. Plugin URI: http://www.vdfn.altervista.org/redirect/plugin_entrylist.html
  6. Description: This plugin add the [entrylist] tag to make the list of the entries. It needs BBCode.
  7. Author: Piero VDFN
  8. Author URI: http://www.vdfn.altervista.org/
  9. */
  10.  
  11. /**
  12.  * If a page is often visited it's better making a cache.
  13.  */
  14. define('ENTRYLIST_CACHEFILE', CACHE_DIR.'plugin_entrylist_tag.txt');
  15.  
  16. /**
  17.  * This class is the main class of the entrylist plugin.
  18.  *
  19.  * @see The description of the plugin.
  20.  */
  21. class plugin_entrylist {
  22.  
  23.     /**
  24.      * This function is the constructor.
  25.      */
  26.     function plugin_entrylist() {
  27.         add_filter('bbcode_init', array(&$this, 'hook'));
  28.         add_filter('publish_post', array(&$this, 'delete'), 10, 1);
  29.         add_filter('delete_post', array(&$this, 'delete'), 10, 1);
  30.     }
  31.  
  32.     /**
  33.      * Delete cache. It's used as callback.
  34.      *
  35.      * @return boolean: True
  36.      */
  37.     function delete() {
  38.         if(file_exists(ENTRYLIST_CACHEFILE)) {
  39.             @unlink(ENTRYLIST_CACHEFILE);
  40.         }
  41.  
  42.         return true;
  43.     }
  44.  
  45.     /**
  46.      * This function get the list of entries.
  47.      *
  48.      * @return array: The list
  49.      */
  50.     function getEntriesList() {
  51.         if(file_exists(ENTRYLIST_CACHEFILE)) {
  52.             include ENTRYLIST_CACHEFILE;
  53.             return $list;
  54.         }
  55.  
  56.         // Make the list
  57.         $list=array();
  58.  
  59.         $query=new FPDB_Query(array('count'=>-1, 'fullparse'=>false), null);
  60.         while($query->hasMore()) {
  61.             list($id, $entry)=$query->getEntry();
  62.             $date=date_from_id($id);
  63.             $list[$date['y']][$date['m']][$date['d']][$id]=$entry['subject'];
  64.         }
  65.  
  66.         system_save(ENTRYLIST_CACHEFILE, array('list'=>$list));
  67.         return $list;
  68.     }
  69.  
  70.     /**
  71.      * This function lists the entries.
  72.      *
  73.      * @param array $list: The list
  74.      * @param boolean $link: Make the link?
  75.      * @return string: The list in HTML
  76.      */
  77.     function listEntries($list, $link=true) {
  78.         global $post; // PrettyURLs wants it
  79.  
  80.         if(!is_array($list)) {
  81.             return '';
  82.         }
  83.  
  84.         $return='';
  85.         foreach($list as $id=>$subject) {
  86.             $return.='<li>';
  87.             if(is_array($subject)) {
  88.                 $return.=$this->listEntries($subject);
  89.             } else {
  90.                 if($link) {
  91.                     $oldpost=$post;
  92.                     $post=array('subject'=>$subject);
  93.                     $href=get_permalink($id);
  94.                     $post=$oldpost;
  95.                     $return.='<a href="'.$href.'">'.wp_specialchars($subject).'</a>';
  96.                 } else {
  97.                     $return.=wp_specialchars($subject);
  98.                 }
  99.             }
  100.             $return.="</li>\n";
  101.         }
  102.  
  103.         if(empty($return)) {
  104.             return '';
  105.         }
  106.  
  107.         return "<ul class=\"entrylist\">\n{$return}</ul>";
  108.     }
  109.  
  110.     /**
  111.      * This function manages the entrylist tag.
  112.      *
  113.      * @param string $action: The action
  114.      * @param array $attributes: The attributes
  115.      * @param string $content: The content of the tag
  116.      * @params array $params: The parameters
  117.      * @param mixed $node_object: Not known
  118.      * @return string: The replacement
  119.      */
  120.     function tag($action, $attributes, $content, $params, $node_object) {
  121.         if($action=='validate') {
  122.             return true;
  123.         }
  124.  
  125.         $list=(array)$this->getEntriesList();  
  126.  
  127.         // Just an year or a month or a day
  128.         if(isset($attributes['y'])) {
  129.             $y=$attributes['y'];
  130.             $list=(array)@$list[$y];
  131.             if(isset($attributes['m'])) {
  132.                 $m=$attributes['m'];
  133.                 if(strlen($m)==1) {
  134.                     $m='0'.$m;
  135.                 }
  136.                 $list=(array)@$list[$m];
  137.                 if(isset($attributes['d'])) {
  138.                     $d=$attributes['d'];
  139.                     if(strlen($d)==1) {
  140.                         $d='0'.$d;
  141.                     }
  142.                     $list=(array)@$list[$d];
  143.                 }
  144.             }
  145.         }
  146.  
  147.         // If list is empty
  148.         if(!empty($attributes['noentries']) && !count($list)) {
  149.             return $attributes['noentries'];
  150.         } elseif(!count($list)) {
  151.             return 'There aren\'t entries.';
  152.         }
  153.  
  154.         // Make the link?
  155.         $link=true;
  156.         if(isset($attributes['link'])) {
  157.             // Force boolean
  158.             $link=true && $attributes['link'];
  159.         }
  160.  
  161.         // Sort the list
  162.         $sort='asort';
  163.         if(@$attributes['sort']=='desc') {
  164.             $sort='arsort';
  165.         }
  166.         call_user_func($sort, &$list);
  167.  
  168.         // Finally make the list
  169.         $return=$this->listEntries($list, $link);
  170.         return $return;
  171.     }
  172.  
  173.     /**
  174.      * This function adds the entrylist tag to the BBCode plugin.
  175.      *
  176.      * @param object $bbcode: The bbcode instance
  177.      * @return object: $bbcode modified
  178.      */
  179.     function hook($bbcode) {
  180.         $bbcode->addCode(
  181.             'entrylist',
  182.             'callback_replace_single',
  183.             array(&$this, 'tag'),
  184.             array(
  185.                 'usecontent_param'=>array('default')
  186.             ),
  187.             'list',
  188.             array('block', 'listitem'),
  189.             array()
  190.         );
  191.         return $bbcode;
  192.     }
  193.  
  194. }
  195.  
  196. new plugin_entrylist();
Advertisement
Add Comment
Please, Sign In to add comment