Advertisement
Guest User

Untitled

a guest
May 17th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.70 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Grid Archives
  4. Plugin URI: http://blog.samsonis.me/tag/grid-archives/
  5. Version: 1.0.2
  6. Author: <a href="http://blog.samsonis.me/">Samson Wu</a>
  7. Description: Grid Archives offers a grid style archives page for WordPress.
  8.  
  9. **************************************************************************
  10.  
  11. Copyright (C) 2008 Samson Wu
  12.  
  13. This program is free software: you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation, either version 3 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program. If not, see <http://www.gnu.org/licenses/>.
  25.  
  26. **************************************************************************
  27. */
  28.  
  29. define('GRID_ARCHIVES_VERSION', '1.0.2');
  30.  
  31. /**
  32. * Guess the wp-content and plugin urls/paths
  33. */
  34. // Pre-2.6 compatibility
  35. if ( ! defined( 'WP_CONTENT_URL' ) )
  36. define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
  37. if ( ! defined( 'WP_CONTENT_DIR' ) )
  38. define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
  39. if ( ! defined( 'WP_PLUGIN_URL' ) )
  40. define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
  41. if ( ! defined( 'WP_PLUGIN_DIR' ) )
  42. define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
  43.  
  44.  
  45. define('GRID_ARCHIVES_POSTS_TRANSIENT_KEY', 'grid_archives_posts');
  46. define('GRID_ARCHIVES_OPTION_NAME', 'grid_archives_options');
  47.  
  48.  
  49. if (!class_exists("GridArchives")) {
  50. class GridArchives {
  51. var $options;
  52.  
  53. function GridArchives() {
  54. $this->plugin_url = WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__));
  55.  
  56. add_action('wp_print_styles', array(&$this, 'load_styles'));
  57. add_shortcode('grid_archives', array(&$this, 'display_archives'));
  58.  
  59. // admin menu
  60. add_action('admin_menu', array(&$this, 'grid_archives_settings'));
  61.  
  62. // invalidate cache
  63. add_action('save_post', array(&$this, 'delete_cache'));
  64. add_action('edit_post', array(&$this, 'delete_cache'));
  65. add_action('delete_post', array(&$this, 'delete_cache'));
  66.  
  67. register_activation_hook(__FILE__, array(&$this, 'install'));
  68. }
  69.  
  70. // Grab all posts and filter them into an array
  71. private function get_posts() {
  72. // If we have a non-expire cached copy of the filtered posts array, use that instead
  73. if($posts = get_transient(GRID_ARCHIVES_POSTS_TRANSIENT_KEY)) {
  74. return $posts;
  75. }
  76.  
  77. // Get a simple array of all posts
  78. $rawposts = get_posts('numberposts=-1');
  79.  
  80. // Trim some memory
  81. foreach ( $rawposts as $key => $rawpost )
  82. $rawposts[$key]->post_content = $this->get_excerpt($rawposts[$key]->post_content, $this->options['post_content_max_len']);
  83.  
  84. // Loop through each post and sort it into a structured array
  85. foreach( $rawposts as $key => $post ) {
  86. $posts[ mysql2date('Y.m', $post->post_date) ][] = $post;
  87.  
  88. $rawposts[$key] = null;
  89. }
  90. $rawposts = null; // More memory cleanup
  91.  
  92. // Store the results into the WordPress transient, expires in 1 day (24 hours)
  93. set_transient(GRID_ARCHIVES_POSTS_TRANSIENT_KEY, $posts, 60*60*24);
  94. return $posts;
  95. }
  96.  
  97. private function compose_html($posts, $monthly_summaries) {
  98. $post_date_format = $this->options['post_date_format'];
  99. if($post_date_format === 'custom'){
  100. $post_date_format = $this->options['post_date_format_custom'];
  101. }
  102. $month_date_format = $this->options['month_date_format'];
  103. if($month_date_format === 'custom'){
  104. $month_date_format = $this->options['month_date_format_custom'];
  105. }
  106. $html = '<div id="grid_archives" class="grid_archives_column">'
  107. . '<ul>';
  108. foreach ($posts as $yearmonth => $monthly_posts) {
  109. list($year, $month) = explode('.', $yearmonth);
  110. $html .= '<li class="ga_year_month">'
  111. . '<a href="' . get_month_link( $year, $month ) . '" title="Monthly Archives: ' . $yearmonth . '">'. mysql2date($month_date_format, date('Y-m-d H:i:s', strtotime($year . '-' . $month))) . '</a>';
  112. if(!empty($monthly_summaries[$yearmonth])){
  113. $html .= '<span class="ga_monthly_summary">“' . $monthly_summaries[$yearmonth] . '”';
  114. }else {
  115. $html .= '<span class="ga_monthly_summary">' . $this->options['default_monthly_summary'];
  116. }
  117. $html .= '</span></li>';
  118. foreach ($monthly_posts as $post) {
  119. $html .= '<li class="ga_post">'
  120. . '<div class="ga_post_main">'
  121. . '<a href="' . get_permalink( $post->ID ) . '" title="' . $post->post_title . '">' . $this->get_excerpt($post->post_title, $this->options['post_title_max_len']) . '</a>'
  122. . '<p>' . $post->post_content . '</p>'
  123. . '</div>';
  124. if(!$this->options['post_date_not_display']){
  125. $html .= '<p class="ga_post_date">' . mysql2date($post_date_format, $post->post_date) . '</p>';
  126. }
  127. $html .= '</li>';
  128. }
  129. }
  130. $html .= '</ul>' . '</div>';
  131. return $html;
  132. }
  133.  
  134. private function get_excerpt($text, $length = 90) {
  135. if (!$length || mb_strlen($text, 'utf8') <= $length)
  136. return $text;
  137.  
  138. $text = strip_tags($text);
  139. $text = preg_replace('/\(\(([^\)]*?)\)\)/', '(${1})', $text);
  140. $text = preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', $text);
  141.  
  142. $text = mb_substr($text, 0, $length, 'utf8') . " ...";
  143. return $text;
  144. }
  145.  
  146. private function parse_summaries($str) {
  147. $summaries = array();
  148. foreach (explode("\n", trim($str)) as $line) {
  149. if(strpos($line, '##') !== FALSE){
  150. list($yearmonth, $summary) = array_map('trim', explode("##", $line, 2));
  151. if (!empty($yearmonth)){
  152. $summaries[$yearmonth] = stripslashes($summary);
  153. }
  154. }
  155. }
  156. return $summaries;
  157. }
  158.  
  159. private function get_options() {
  160. $options = array('post_title_max_len' => 60, 'post_content_max_len' => 90, 'post_date_not_display' => false, 'post_date_format' => 'j M Y', 'post_date_format_custom' => 'j M Y', 'month_date_format' => 'Y.m', 'month_date_format_custom' => 'Y.m', 'post_hovered_highlight' => true, 'monthly_summary_hovered_rotate' => true, 'custom_css_styles' => '', 'default_monthly_summary' => '“... ...”', 'monthly_summaries' => "2010.09##It was AWESOME!\n2010.08##Anyone who has never made a mistake has never tried anything new.");
  161. $saved_options = get_option(GRID_ARCHIVES_OPTION_NAME);
  162.  
  163. if (!empty($saved_options)) {
  164. foreach ($saved_options as $key => $option)
  165. $options[$key] = $option;
  166. }
  167.  
  168. if ($saved_options != $options) {
  169. update_option(GRID_ARCHIVES_OPTION_NAME, $options);
  170. }
  171. return $options;
  172. }
  173.  
  174. function handle_grid_archives_settings() {
  175. if (!current_user_can('manage_options')) {
  176. wp_die( __('You do not have sufficient permissions to access this page.') );
  177. }
  178.  
  179. $options = $this->get_options();
  180.  
  181. if (isset($_POST['submit'])) {
  182. check_admin_referer('grid-archives-nonce');
  183.  
  184. $options = array();
  185.  
  186. $options['post_title_max_len'] = (int)$_POST['post_title_max_len'];
  187. $options['post_content_max_len'] = (int)$_POST['post_content_max_len'];
  188. $options['post_date_not_display'] = isset($_POST['post_date_not_display']) ? (boolean)$_POST['post_date_not_display'] : false;
  189. $options['post_date_format'] = $_POST['post_date_format'];
  190. $options['post_date_format_custom'] = stripslashes($_POST['post_date_format_custom']);
  191.  
  192. $options['month_date_format'] = $_POST['month_date_format'];
  193. $options['month_date_format_custom'] = stripslashes($_POST['month_date_format_custom']);
  194.  
  195. $options['post_hovered_highlight'] = isset($_POST['post_hovered_highlight']) ? (boolean)$_POST['post_hovered_highlight'] : false;
  196. $options['monthly_summary_hovered_rotate'] = isset($_POST['monthly_summary_hovered_rotate']) ? (boolean)$_POST['monthly_summary_hovered_rotate'] : false;
  197.  
  198. $options['custom_css_styles'] = stripslashes($_POST['custom_css_styles']);
  199.  
  200. $options['default_monthly_summary'] = htmlspecialchars(stripslashes($_POST['default_monthly_summary']));
  201. $options['monthly_summaries'] = htmlspecialchars(stripslashes($_POST['monthly_summaries']));
  202.  
  203. update_option(GRID_ARCHIVES_OPTION_NAME, $options);
  204.  
  205. $this->delete_cache();
  206. echo '<div class="updated" id="message"><p>Settings saved.</p></div>';
  207. }
  208. include_once("grid-archives-options.php");
  209. }
  210.  
  211. function display_archives($atts){
  212. // $this->options = $this->get_options();
  213. $posts = $this->get_posts();
  214. $monthly_summaries = $this->parse_summaries($this->options['monthly_summaries']);
  215. return $this->compose_html($posts, $monthly_summaries);
  216. }
  217.  
  218. function grid_archives_settings() {
  219. add_options_page('Grid Archives Settings', 'Grid Archives', 'manage_options', 'grid-archives-settings', array(&$this, 'handle_grid_archives_settings'));
  220. }
  221.  
  222. function load_styles(){
  223. $this->options = $this->get_options();
  224.  
  225. $css_url = $this->plugin_url . '/grid-archives.css';
  226. wp_register_style('grid_archives', $css_url, array(), GRID_ARCHIVES_VERSION, 'screen');
  227. wp_enqueue_style('grid_archives');
  228.  
  229. if($this->options['post_hovered_highlight'] || $this->options['monthly_summary_hovered_rotate']) {
  230. $effect_css_url = $this->plugin_url . '/grid-archives-effect-css.php';
  231. wp_register_style('grid_archives_effect', $effect_css_url, array(), GRID_ARCHIVES_VERSION, 'screen');
  232. wp_enqueue_style('grid_archives_effect');
  233. }
  234.  
  235. $custom_css_styles = trim($this->options['custom_css_styles']);
  236. if(!empty($custom_css_styles)) {
  237. $custom_css_url = $this->plugin_url . '/grid-archives-custom-css.php';
  238. wp_register_style('grid_archives_custom', $custom_css_url, array(), GRID_ARCHIVES_VERSION, 'screen');
  239. wp_enqueue_style('grid_archives_custom');
  240. }
  241. }
  242.  
  243. function delete_cache() {
  244. delete_transient(GRID_ARCHIVES_POSTS_TRANSIENT_KEY);
  245. }
  246.  
  247. function install() {
  248. $this->options = $this->get_options();
  249. }
  250. }
  251. }
  252.  
  253. if (class_exists("GridArchives")) {
  254. $grid_archives = new GridArchives();
  255. }
  256.  
  257. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement