Last active
August 29, 2015 14:01
-
-
Save ringmaster/df5da3126344dc7782fc to your computer and use it in GitHub Desktop.
archives.plugin.php that takes into account the current post maybe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, $theme ); | |
Cache::set( $this->cache[$num_month], $monthly_archives ); | |
} | |
} | |
else { | |
$monthly_archives = $this->get_monthly_archives( $num_month, $detail_view, $theme ); | |
$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, $theme = null ) | |
{ | |
$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 ) { | |
$display_month = ""; | |
$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, 1) ); | |
break; | |
case 'A': // Abbreviation | |
$display_month = date( 'M', mktime( 0, 0, 0, $yr_mth->month, 1) ); | |
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' ); | |
if(isset($theme) && isset($theme->post) && $theme->post->id == $post->id && $theme->request->display_post) { | |
$monthly_archives .= "<li class=\"bold\">{$day}"; | |
$monthly_archives .= ( empty( $this->config['delimiter'] ) ? " " : htmlspecialchars( $this->config['delimiter'] ) ); | |
$monthly_archives .= "{$post->title}</li>\n"; | |
} | |
else { | |
$monthly_archives .= "<li>{$day}"; | |
$monthly_archives .= ( empty( $this->config['delimiter'] ) ? " " : 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; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment