Pastoid

The page you are looking at now is at this URL: http://pastoid.com/a50

This paste was last updated on July 14, 2009 at 2:50 pm.

archives.plugin.xmlraw

<?xml version="1.0" encoding="utf-8" ?>
<pluggable type="plugin">
        <name>RN Monthly Archives</name>
        <license url="http://www.apache.org/licenses/LICENSE-2.0.html">Apache Software License 2.0</license>
 
        <author url="http://blog.tinyau.net">Raman Ng (a.k.a. tinyau)</author>
 
        <version>1.3.1</version>
        <url>http://habariproject.org/</url>
        <description><![CDATA[Show monthly archives]]></description>
 
        <copyright>2009</copyright>
</pluggable>

Toggle wordwrap

archives.plugin.phpraw

<?php
 
class MonthlyArchives extends Plugin
{
 
        private $config = array();
        private $class_name = '';
        private $cache = array();
        private $default_options = array (
                'display_month' => 'F',
                'show_count' => 'Y',
                'delimiter' => ''
                );
 
        // Set the default options
        public function action_plugin_activation( $file )
        {
                if ( realpath( $file ) == __FILE__ ) {
                        $this->class_name = strtolower( get_class( $this ) );
                        $this->cache_name = Site::get_url( 'host' ) . ':' . $this->class_name;
                        foreach ( $this->default_options as $name => $value ) {
                                $current_value = Options::get( $this->class_name . '__' . $name );
                                if ( !isset( $current_value) ) {
                                        Options::set( $this->class_name . '__' . $name, $value );
                                }
                        }
                }
        }
 
        public function action_plugin_deactivation( $file )
        {
                if ( realpath( $file ) == __FILE__ ) {
                        Cache::expire( $this->cache_name );
                }
        }
 
        public function action_init()
        {
                $this->class_name = strtolower( get_class( $this ) );
                $this->cache_name = Site::get_url( 'host' ) . ':' . $this->class_name;
                foreach ( $this->default_options as $name => $unused ) {
                        $this->config[$name] = Options::get( $this->class_name . '__' . $name );
                }
        }
 
        public function filter_plugin_config( $actions, $plugin_id )
        {
                if ( $plugin_id == $this->plugin_id() ) {
                        $actions[] = _t( 'Configure' );
                        $actions[] = _t( 'Clear Cache' );
                }
                return $actions;
        }
 
        public function action_plugin_ui( $plugin_id, $action )
        {
                if ( $plugin_id == $this->plugin_id() ) {
                        switch ( $action ) {
                                case _t( 'Configure' ):
                                        $ui = new FormUI( $this->class_name );
 
                                        $display_month = $ui->append( 'select', 'display_month', 'option:' . $this->class_name . '__display_month', _t( 'Month displayed as' ) );
 
                                        $display_month->options = array( '' => '', 'F' => 'Full name', 'A' => 'Abbreviation', 'N' => 'Number' );
 
                                        $show_count = $ui->append( 'select', 'show_count', 'option:' . $this->class_name . '__show_count', _t( 'Show Monthly Entries Count?' ) );
                                        $show_count->options = array( '' => '', 'Y' => 'Yes', 'N' => 'No' );
                                        $show_count->add_validator( 'validate_required' );
 
                                        $delimiter = $ui->append( 'text', 'delimiter', 'option:' . $this->class_name . '__delimiter', _t( 'Delimiter to separate day and post title in detail view (optional)' ) );
 
                                        $ui->append( 'submit', 'save', _t( 'Save' ) );
                                        $ui->set_option( 'success_message', _t( 'Configuration saved' ) );
 
                                        $ui->on_success( array( $this, 'updated_config' ) );
                                        $ui->out();
                                        break;
                                case _t( 'Clear Cache' ):
                                        Cache::expire( $this->cache_name );
                                        echo '<p>' . _t( 'Cache has been cleared.' ) . '</p>';
                                        break;
                        }
                }
        }
 
        public function updated_config( $ui )
        {
                $ui->save();
                Cache::expire( $this->cache_name );
                return false;
        }
 
        public function theme_monthly_archives( $theme, $num_month = 0, $detail_view = 'Y' )
        {
                if ( array_key_exists( $num_month, $this->cache ) ) {
                        if ( Cache::has( $this->cache[$num_month] ) ) {
                                $monthly_archives = Cache::get( $this->cache[$num_month] );
                        }
                        else {
                                $monthly_archives = $this->get_monthly_archives( $num_month, $detail_view );
                                Cache::set( $this->cache[$num_month], $monthly_archives );
                        }
                }
                else {
                        $monthly_archives = $this->get_monthly_archives( $num_month, $detail_view );
                        $this->cache[$num_month] = Site::get_url( 'host' ) . ':' . $this->class_name . ':' . $num_month;
                        Cache::set( $this->cache[$num_month], $monthly_archives );
                }
 
                return $monthly_archives;
        }
 
        public function action_post_update_status( $post, $old_value, $new_value )
        {
                if ( ( Post::status_name( $old_value ) == 'published' && Post::status_name( $new_value ) != 'published' ) ||
                          ( Post::status_name( $old_value ) != 'published' && Post::status_name( $new_value ) == 'published' ) ) {
                        Cache::expire( $this->cache_name );
                }
        }
 
        public function action_post_update_slug( $post, $old_value, $new_value )
        {
                if ( Post::status_name( $post->status ) == 'published' ) {
                        Cache::expire( $this->cache_name );
                }
        }
 
        public function action_post_update_title( $post, $old_value, $new_value )
        {
                if ( Post::status_name( $post->status ) == 'published' ) {
                        Cache::expire( $this->cache_name );
                }
        }
 
        public function action_post_delete_after( $post )
        {
                if ( Post::status_name( $post->status ) == 'published' ) {
                        Cache::expire( $this->cache_name );
                }
        }
 
        private function get_monthly_archives( $num_month, $detail_view )
        {
                $post_type = Post::type( 'entry' );
                $post_status = Post::status( 'published' );
                $monthly_archives = '';
 
                $limit = ( empty( $num_month ) ? '' : "LIMIT {$num_month}" );
                $sql = "
                        SELECT YEAR(FROM_UNIXTIME(pubdate)) AS year, MONTH(FROM_UNIXTIME(pubdate)) AS month,
                        COUNT(p.id) AS cnt
                        FROM {posts} p
                        WHERE p.content_type = {$post_type}
                        AND p.status = {$post_status}
                        GROUP BY year, month
                        ORDER BY year DESC, month DESC
                        {$limit}";
                $yr_mths = DB::get_results( $sql );
 
                $monthly_archives.= "<ul class=\"archive-month\">\n";
                foreach ( $yr_mths as $yr_mth ) {
                        $month = substr( '0' . $yr_mth->month, -2, 2 );
                        switch ( $this->config['display_month'] ) {
                                case 'F': // Full name
                                        $display_month = date( 'F', mktime( 0, 0, 0, $yr_mth->month ) );
                                        break;
                                case 'A': // Abbreviation
                                        $display_month = date( 'M', mktime( 0, 0, 0, $yr_mth->month ) );
                                        break;
                                case 'N': // Number
                                        $display_month = $month;
                                        break;
                        }
                        $monthly_archives.= '<li><a href="' . URL::get( 'display_entries_by_date', array( 'year' => $yr_mth->year, 'month' => $month ) ) . '" title="View entries in ' . "{$display_month} {$yr_mth->year}". '">' . "{$display_month} {$yr_mth->year}</a>";
                        if ( 'Y' == $this->config['show_count'] ) {
                                $monthly_archives.= " ({$yr_mth->cnt})";
                        }
 
                        if ( 'N' == strtoupper( $detail_view ) ) {
                                $monthly_archives.= "</li>\n";
                        }
                        elseif ( 'Y' == strtoupper( $detail_view ) ) {
                                // Show post title as well
                                $posts = Posts::get( array(
                                        'content_type' => $post_type,
                                        'status' => $post_status,
                                        'year' => $yr_mth->year,
                                        'month' => $yr_mth->month,
                                        'nolimit' => 1,
                                        ) );
                                if ( $posts ) {
                                        $monthly_archives.= "\n<ul class=\"archive-entry\">\n";
                                        foreach ( $posts as $post ) {
                                                $day = $post->pubdate->format( 'd' );
 
                                                $monthly_archives.= "<li>{$day}";
                                                $monthly_archives.= ( empty( $this->config['delimiter'] ) ? "&nbsp;&nbsp;" : htmlspecialchars( $this->config['delimiter'] ) );
                                                $monthly_archives.= "<a href=\"{$post->permalink}\">{$post->title}</a></li>\n";
                                        }
                                        $monthly_archives.= "</ul>\n";
                                }
                                $monthly_archives.= "</li>\n";
                        }
                }
                $monthly_archives.= "</ul>\n";
 
                return $monthly_archives;
        }
}
?>

Toggle wordwrap

Referring DomainHits
Unknown Referer 165
pastoid.com 2
Is this paste spam?
<Hide