1 <?php |
1 <?php |
2 /* |
2 /* |
3 Plugin Name: AJAX Calendar |
3 Plugin Name: AJAX Calendar |
4 Plugin URI: http://urbangiraffe.com/plugins/ajax-calendar/ |
4 Plugin URI: http://urbangiraffe.com/plugins/ajax-calendar/ |
5 Description: A version of the WordPress calendar that uses AJAX to allow the user to step through the months without updating the page. Additionally, a click on the 'expand' link shows all the posts within that month, inside the calendar. Caching of content can be enabled to increase speed. |
5 Description: A version of the WordPress calendar that uses AJAX to allow the user to step through the months without updating the page. |
6 Version: 2.4.9 |
6 Version: 2.5.1 |
7 Author: John Godley |
7 Author: John Godley |
8 Author URI: http://urbangiraffe.com |
8 Author URI: http://urbangiraffe.com |
9 */ |
9 */ |
10 |
10 |
11 include_once (dirname (__FILE__).'/plugin.php'); |
11 class AJAX_Calendar_Widget extends WP_Widget { |
12 include_once (dirname (__FILE__).'/models/widget.php'); |
12 var $category_ids = array(); |
13 |
13 |
14 if (!class_exists ('AJAX_Calendar')) |
14 function AJAX_Calendar_Widget() { |
15 { |
15 $widget_ops = array( 'classname' => 'ajax_calendar_widget', 'description' => __( 'AJAX Powered Calendar', 'ajax-calendar' ) ); |
16 class AJAX_Calendar extends AJAX_Calendar_Plugin |
16 $control_ops = array( 'width' => 300, 'height' => 300 ); |
17 { |
|
18 function AJAX_Calendar () |
|
19 { |
|
20 $this->register_plugin ('ajax-calendar', __FILE__); |
|
21 |
17 |
22 if (is_admin ()) |
18 $this->WP_Widget( 'ajax-calendar', __( 'AJAX Calendar', 'ajax-calendar' ), $widget_ops, $control_ops ); |
23 { |
19 |
24 $this->add_action ('publish_post', 'cache_remove'); |
20 add_action( 'template_redirect', array( &$this, 'template_redirect' ) ); |
25 $this->add_action ('save_post', 'cache_remove'); |
21 } |
26 $this->add_action ('delete_post', 'cache_remove'); |
22 |
|
23 function template_redirect() { |
|
24 if ( is_date() && isset( $_GET['ajax'] ) && $_GET['ajax'] == 'true' ) { |
|
25 $settings = $this->get_settings(); |
|
26 $settings = $settings[$this->number]; |
|
27 |
|
28 $instance = wp_parse_args( $settings, array( 'title' => __( 'AJAX Calendar', 'ajax-calendar' ), 'category_id' => '' ) ); |
|
29 $this->category_ids = array_filter( explode( ',', $instance['category_id'] ) ); |
|
30 |
|
31 echo $this->get_calendar(); |
|
32 die(); |
27 } |
33 } |
28 else |
34 } |
29 $this->add_action ('wp_head'); |
35 |
|
36 /** |
|
37 * Display the widget |
|
38 * |
|
39 * @param string $args Widget arguments |
|
40 * @param string $instance Widget instance |
|
41 * @return void |
|
42 **/ |
|
43 function widget( $args, $instance ) { |
|
44 extract( $args ); |
|
45 |
|
46 $instance = wp_parse_args( (array)$instance, array( 'title' => __( 'AJAX Calendar', 'ajax-calendar' ), 'category_id' => '' ) ); |
|
47 $title = apply_filters( 'widget_title', $instance['title'] ); |
|
48 $category_id = $instance['category_id']; |
30 |
49 |
31 $this->widget = new AJAX_Calendar_Widget ('AJAX Calendar'); |
50 $this->category_ids = array_filter( explode( ',', $category_id ) ); |
|
51 |
|
52 echo $before_widget; |
|
53 |
|
54 if ( $title ) |
|
55 echo $before_title . stripslashes( $title ) . $after_title; |
|
56 |
|
57 echo $this->get_calendar(); |
|
58 |
|
59 // MicroAJAX: http://www.blackmac.de/index.php?/archives/31-Smallest-JavaScript-AJAX-library-ever!.html |
|
60 ?> |
|
61 <script type="text/javascript"> |
|
62 function show_micro_ajax(response){document.getElementById('wp-calendar').innerHTML=response;} |
|
63 function microAjax(url,cF){this.bF=function(caller,object){return function(){return caller.apply(object,new Array(object));}}; |
|
64 this.sC=function(object){if(this.r.readyState==4){this.cF(this.r.responseText);}}; |
|
65 this.gR=function(){if(window.ActiveXObject) |
|
66 return new ActiveXObject('Microsoft.XMLHTTP');else if(window.XMLHttpRequest) |
|
67 return new XMLHttpRequest();else return false;}; |
|
68 if(arguments[2])this.pb=arguments[2];else this.pb="";this.cF=cF;this.url=url;this.r=this.gR();if(this.r){this.r.onreadystatechange=this.bF(this.sC,this);if(this.pb!=""){this.r.open("POST",url,true);this.r.setRequestHeader('Content-type','application/x-www-form-urlencoded');this.r.setRequestHeader('Connection','close');}else{this.r.open("GET",url,true);} |
|
69 this.r.send(this.pb);}} |
|
70 </script> |
|
71 <?php |
|
72 // After |
|
73 echo $after_widget; |
32 } |
74 } |
|
75 |
|
76 function get_calendar() { |
|
77 global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; |
33 |
78 |
34 function show ($categories = '') |
79 add_filter( 'query', array( &$this, 'modify_calendar_query' ) ); |
35 { |
80 |
36 include_once (dirname (__FILE__).'/models/calendar.php'); |
81 $text = get_calendar( true, false ); |
|
82 |
|
83 remove_filter( 'query', array( &$this, 'modify_calendar_query' ) ); |
|
84 |
|
85 $text = str_replace( '<td colspan="3" id="next"><a', '<td colspan="3" id="next"><a onclick="microAjax(this.href + \'?ajax=true\',show_micro_ajax); return false"', $text ); |
|
86 $text = str_replace( '<td colspan="3" id="prev"><a', '<td colspan="3" id="prev"><a onclick="microAjax(this.href + \'?ajax=true\',show_micro_ajax); return false"', $text ); |
|
87 return $text; |
|
88 } |
|
89 |
|
90 function modify_calendar_query( $query ) { |
|
91 if ( !empty( $this->category_ids ) ) { |
|
92 global $wpdb; |
|
93 |
|
94 $query = str_replace( 'WHERE', "LEFT JOIN {$wpdb->prefix}term_relationships ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id INNER JOIN {$wpdb->prefix}term_taxonomy ON ({$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id AND {$wpdb->prefix}term_taxonomy.taxonomy='category') WHERE", $query ); |
|
95 if ( strpos( $query, 'ORDER' ) !== false ) |
|
96 $query = str_replace( "ORDER", "AND {$wpdb->prefix}term_taxonomy.term_id IN (".implode (',', $this->category_ids ).') ORDER', $query ); |
|
97 else |
|
98 $query .= "AND {$wpdb->prefix}term_taxonomy.term_id IN (".implode (',', $this->category_ids ).')'; |
|
99 } |
37 |
100 |
38 $this->render ('calendar', array ('calendar' => new Calendar ($this->url (), $categories))); |
101 return $query; |
39 } |
102 } |
|
103 |
|
104 /** |
|
105 * Display config interface |
|
106 * |
|
107 * @param string $instance Widget instance |
|
108 * @return void |
|
109 **/ |
|
110 function form( $instance ) { |
|
111 $instance = wp_parse_args( (array)$instance, array( 'title' => __( 'AJAX Calendar', 'ajax-calendar' ), 'category_id' => '' ) ); |
40 |
112 |
41 function wp_head () |
113 $title = stripslashes( $instance['title'] ); |
42 { |
114 $category_id = $instance['category_id']; |
43 $this->render ('head'); |
115 |
|
116 ?> |
|
117 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'ajax-calendar' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p> |
|
118 <p><label for="<?php echo $this->get_field_id( 'category_id' ); ?>"><?php _e( 'Category IDs:', 'ajax-calendar' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'category_id' ); ?>" name="<?php echo $this->get_field_name( 'category_id' ); ?>" type="text" value="<?php echo esc_attr( $category_id ); ?>" /></label></p> |
|
119 <?php |
44 } |
120 } |
|
121 |
|
122 /** |
|
123 * Save widget data |
|
124 * |
|
125 * @param string $new_instance |
|
126 * @param string $old_instance |
|
127 * @return void |
|
128 **/ |
|
129 function update( $new_instance, $old_instance ) { |
|
130 $instance = $old_instance; |
|
131 $new_instance = wp_parse_args( (array)$new_instance, array( 'title' => __( 'AJAX Calendar', 'ajax-calendar' ), 'category_id' => '' ) ); |
45 |
132 |
46 function cache_remove ($id) |
133 $instance['title'] = wp_filter_nohtml_kses( $new_instance['title'] ); |
47 { |
134 $instance['category_id'] = implode( ',', array_filter( array_map( 'intval', explode( ',', $new_instance['category_id'] ) ) ) ); |
48 $post = wp_get_single_post ($id); |
135 |
49 |
136 return $instance; |
50 $postmonth = substr ($post->post_date, 5, 2); |
|
51 $postyear = substr ($post->post_date, 0, 4); |
|
52 $modmonth = substr ($post->post_modified, 5, 2); |
|
53 $modyear = substr ($post->post_modified, 0, 4); |
|
54 |
|
55 // Delete the cache files for the two dates, just in case something has changed |
|
56 for ($x = 0; $x < 2; $x++) |
|
57 { |
|
58 wp_cache_delete ($postyear."_".$postmonth."_".$x); |
|
59 wp_cache_delete ($modyear."_".$modmonth."_".$x); |
|
60 } |
|
61 } |
|
62 |
|
63 function &get () |
|
64 { |
|
65 static $instance; |
|
66 |
|
67 if (!isset ($instance)) |
|
68 { |
|
69 $c = __CLASS__; |
|
70 $instance = new $c; |
|
71 } |
|
72 |
|
73 return $instance; |
|
74 } |
137 } |
75 } |
138 } |
76 |
139 |
77 |
140 function register_ajax_calendar_widget() { |
78 function ajax_calendar ($categories = '') |
141 register_widget( 'AJAX_Calendar_Widget' ); |
79 { |
|
80 $calendar = AJAX_Calendar::get (); |
|
81 $calendar->show ($categories); |
|
82 } |
|
83 } |
142 } |
84 |
143 |
|
144 add_action( 'widgets_init', 'register_ajax_calendar_widget' ); |
85 |
145 |
86 $ajax_calendar_plugin = AJAX_Calendar::get (); |
146 function ajax_calendar ($categories = '') { |
87 |
147 // $calendar = AJAX_Calendar::get (); |
88 ?> |
148 // $calendar->show ( $categories ); |
|
149 } |