Advertisement
Guest User

Untitled

a guest
Jul 10th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.86 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WordPress Custom Post Type Archive
  4. Author URI: http://www.gingerlime.com
  5. Description: Displays a monthly or yearly archive of posts for one specific custom post type.
  6. Version: 1.0
  7. Requires at least: 3.1
  8. Author: Yoav Aner (based on Wordpress Category Archive by Hugh Mandeville)
  9. License: GPL
  10.  
  11. == Description ==
  12.  
  13. The WordPress Custom Post Type Archive Widget Plugin displays a monthly or yearly archive of posts for one specific custom post type.
  14. It is very similar to the WordPress Archive except that it just displays one specific custom post type's posts.
  15. It can be configured to either show a listing or a pulldown of months or years with or without post counts.
  16.  
  17. == Installation ==
  18.  
  19. 1. Upload "wp-custom-post-type-archive.php" to the "/wp-content/plugins/" directory.
  20. 2. Activate the plugin through the "Plugins" menu in WordPress.
  21. 3. From the "Widgets" page in WordPress drag the "Custom Post Type Archive" widget to the "Sidebar".
  22. 4. Configure the "Custom Post Type Archive" widget controls, setting the "Title", "Custom Type", "Display Style", "Group By" and "Show Post Counts" fields.
  23.  
  24. */
  25.  
  26.  
  27. class WP_CustomType_Archive_Widget extends WP_Widget
  28. {
  29. /**
  30. * Contstructor. Set description and call parent class.
  31. */
  32. function WP_CustomType_Archive_Widget()
  33. {
  34. /* Widget settings. */
  35. $widget_ops = array("description" => "Display an archive listing of one specific custom-type.");
  36.  
  37. /* Create the widget. */
  38. $this->WP_Widget("wp-customtype-archive", "Custom Type Archive", $widget_ops);
  39. }
  40.  
  41.  
  42. /**
  43. * Creates URL for archive links to the yearly or monthly custom type listing.
  44. * The URL has the year, month and custom post type in the query string
  45. * ?m=201001&post_type=2
  46. */
  47. function create_url($interval, $year, $month, $customtype)
  48. {
  49. global $wp_rewrite;
  50. $year_month_value = $year . $month;
  51. $year_month_path = $year . "/" . $month;
  52. if ($interval == "year") {
  53. $year_month_value = $year;
  54. $year_month_path = $year . "/00";
  55. }
  56.  
  57. $url = get_option('home') . "/?m=" . $year_month_value . "&post_type=" . $customtype;
  58.  
  59. /* if ($wp_rewrite->using_permalinks()) {
  60. $cat_pos = strpos($wp_rewrite->permalink_structure, "%" . $customtype . "%");
  61. if ($cat_pos != false) {
  62. // if %category% is in the permalink structure, figure out if year is before or after it
  63. $year_pos = strpos($wp_rewrite->permalink_structure, "%year%");
  64. if ($year_pos != false) {
  65. $url = get_option('home') . "/";
  66. if ($cat_pos < $year_pos) {
  67. $url .= $customtype . "/" . $year_month_path . "/";
  68. } else {
  69. $url .= $year_month_path . "/" . $customtype . "/";
  70. }
  71. }
  72. }
  73. }
  74. */
  75. return ($url);
  76. }
  77.  
  78. /**
  79. * Display widget.
  80. */
  81. function widget($args, $instance)
  82. {
  83. extract($args);
  84.  
  85. /* User-selected settings. */
  86. //$title = apply_filters('widget_title', $instance['title'] );
  87. $title = esc_attr($instance['title']);
  88. $customtype = esc_attr($instance['customtype']);
  89. $display_style = $instance['display_style'];
  90. $interval = $instance['interval'];
  91. $show_counts = intval($instance['show_counts']);
  92. echo $before_widget;
  93. /* Title of widget (before and after defined by themes). */
  94. if ( $title ) {
  95. echo $before_title . $title . $after_title;
  96. }
  97.  
  98. // TBD: could change to call sql statement that uses group by
  99. $myposts = get_posts("numberposts=-1&offset=0&post_type=" . $customtype . "&orderby=date&order=DESC");
  100. $previous_year_month_display = "";
  101. $previous_year_month_value = "";
  102. $previous_year = "";
  103. $previous_month = "";
  104. $count = 0;
  105.  
  106. $display_format = "F Y";
  107. $compare_format = "Ym";
  108. $select_str = __("Select Month");
  109. if ($interval == "year") {
  110. $display_format = "Y";
  111. $compare_format = "Y";
  112. $select_str = __("Select Year");
  113. }
  114.  
  115. if ($display_style == "pulldown") {
  116. echo "<select name=\"wp-customtype-archive-dropdown\" onchange=\"document.location.href=this.options[this.selectedIndex].value;\">";
  117. echo " <option value=\"\">" . $select_str . "</option>";
  118. } else if ($display_style == "list") {
  119. echo "<ul>";
  120. }
  121.  
  122. foreach($myposts as $post) {
  123. $post_date = strtotime($post->post_date);
  124. $current_year_month_display = date_i18n($display_format, $post_date);
  125. $current_year_month_value = date($compare_format, $post_date);
  126. $current_year = date("Y", $post_date);
  127. $current_month = date("m", $post_date);
  128. if ($previous_year_month_value != $current_year_month_value) {
  129. if ($count > 0) {
  130. $url = $this->create_url($interval, $previous_year, $previous_month, $customtype);
  131. if ($display_style == "pulldown") {
  132. echo " <option value=\"" . $url . "\"";
  133. if ($_GET['m'] == $previous_year_month_value) {
  134. echo " selected=\"selected\" ";
  135. }
  136. echo ">" . $previous_year_month_display;
  137. if ($show_counts == 1) {
  138. echo " (" . $count . ")";
  139. }
  140. echo "</option>";
  141. } else if ($display_style == "list") {
  142. echo "<li><a href=\"". $url . "\">" . $previous_year_month_display . "</a>";
  143. if ($show_counts == 1) {
  144. echo " (" . $count . ")";
  145. }
  146. echo "</li>";
  147. } else {
  148. echo "<a href=\"". $url . "\">" . $previous_year_month_display . "</a>";
  149. if ($show_counts == 1) {
  150. echo " (" . $count . ")";
  151. }
  152. echo "<br/>";
  153. }
  154. }
  155. $count = 0;
  156. }
  157. $count++;
  158. $previous_year_month_display = $current_year_month_display;
  159. $previous_year_month_value = $current_year_month_value;
  160. $previous_year = $current_year;
  161. $previous_month = $current_month;
  162.  
  163. }
  164. if ($count > 0) {
  165. $url = $this->create_url($interval, $previous_year, $previous_month, $customtype);
  166. if ($display_style == "pulldown") {
  167. echo " <option value=\"" . $url . "\">" . $previous_year_month_display;
  168. if ($show_counts == 1) {
  169. echo " (" . $count . ")";
  170. }
  171. echo "</option>";
  172. } else if ($display_style == "list") {
  173. echo "<li><a href=\"". $url . "\">" . $previous_year_month_display . "</a>";
  174. if ($show_counts == 1) {
  175. echo " (" . $count . ")";
  176. }
  177. echo "</li>";
  178. } else {
  179. echo "<a href=\"". $url . "\">" . $previous_year_month_display . "</a>";
  180. if ($show_counts == 1) {
  181. echo " (" . $count . ")";
  182. }
  183. echo "<br/>";
  184. }
  185. }
  186. if ($display_style == "pulldown") {
  187. echo "</select>";
  188. } else if ($display_style == "list") {
  189. echo "</ul>";
  190. }
  191.  
  192.  
  193. echo $after_widget;
  194. }
  195.  
  196. /**
  197. * Called when widget control form is posted.
  198. */
  199. function update( $new_instance, $old_instance )
  200. {
  201. // global $post;
  202. if (!isset($new_instance['submit'])) {
  203. return false;
  204. }
  205.  
  206. $instance = $old_instance;
  207.  
  208. /* Strip tags (if needed) and update the widget settings. */
  209. $instance["title"] = strip_tags($new_instance["title"]);
  210. $instance["customtype"] = strip_tags($new_instance["customtype"]);
  211. $instance["display_style"] = strip_tags($new_instance["display_style"]);
  212. $instance["interval"] = strip_tags($new_instance["interval"]);
  213. $instance["show_counts"] = intval($new_instance["show_counts"]);
  214. return $instance;
  215. }
  216.  
  217.  
  218. /**
  219. * Display widget control form.
  220. * Title:
  221. * Custom Type:
  222. * Display Style: Lines | (List) | Pulldown
  223. * Group By: (Month) | Year
  224. * Show Post Counts: Yes | (No)
  225. */
  226. function form( $instance )
  227. {
  228. global $wpdb;
  229. /* Set up some default widget settings. */
  230. $defaults = array( "title" => "Archive", "customtype" => "", "display_style" => "list", "interval" => "month", "show_counts" => 1 );
  231. $instance = wp_parse_args( (array) $instance, $defaults );
  232. $title = esc_attr($instance['title']);
  233. $customtype = esc_attr($instance['customtype']);
  234. $display_style = $instance['display_style'];
  235. $interval = $instance['interval'];
  236. $show_counts = intval($instance['show_counts']);
  237.  
  238. // Title
  239. echo "<p>";
  240. echo "<label for=\"" . $this->get_field_id("title") . "\">Title:";
  241. echo "<input id=\"" . $this->get_field_id("title") . "\" " .
  242. "name=\"" . $this->get_field_name("title") . "\" " .
  243. "value=\"" . $title . "\" style=\"width:100%;\" />";
  244. echo "<label></p>";
  245.  
  246. // Custom Post Type (as string). TODO: add fancy dropdown option
  247. echo "<p>";
  248. echo "<label for=\"" . $this->get_field_id("customtype") . "\">Custom Type:";
  249. echo "<input id=\"" . $this->get_field_id("customtype") . "\" " .
  250. "name=\"" . $this->get_field_name("customtype") . "\" " .
  251. "value=\"" . $customtype . "\" style=\"width:100%;\" />";
  252. echo "<label></p>";
  253.  
  254. // Display Style: Lines, List or Pulldown
  255. echo "<p>";
  256. echo "<label for=\"" . $this->get_field_id("display_style") . "\">Display Style:<br/>";
  257. echo "<input type=\"radio\" name=\"" . $this->get_field_name("display_style") . "\" value=\"lines\"";
  258. if ($display_style == "lines") {
  259. echo " checked=\"checked\" ";
  260. }
  261. echo "> Lines ";
  262. echo "<input type=\"radio\" name=\"" . $this->get_field_name("display_style") . "\" value=\"list\"";
  263. if ($display_style == "list") {
  264. echo " checked=\"checked\" ";
  265. }
  266. echo "> List ";
  267. echo "<input type=\"radio\" name=\"" . $this->get_field_name("display_style") . "\" value=\"pulldown\"";
  268. if ($display_style == "pulldown") {
  269. echo " checked=\"checked\" ";
  270. }
  271. echo "> Pulldown";
  272. echo "</label></p>";
  273.  
  274. // Interval: Month or Year
  275. echo "<p>";
  276. echo "<label for=\"" . $this->get_field_id("interval") . "\">Group By:<br/>";
  277. echo "<input type=\"radio\" name=\"" . $this->get_field_name("interval") . "\" value=\"month\"";
  278. if ($interval != "year") {
  279. echo " checked=\"checked\" ";
  280. }
  281. echo "> Month ";
  282. echo "<input type=\"radio\" name=\"" . $this->get_field_name("interval") . "\" value=\"year\"";
  283. if ($interval == "year") {
  284. echo " checked=\"checked\" ";
  285. }
  286. echo "> Year";
  287. echo "</label></p>";
  288.  
  289. // Show Counts: Yes or No
  290. echo "<p>";
  291. echo "<label for=\"" . $this->get_field_id("show_counts") . "\">Show Post Counts:<br/>";
  292. echo "<input type=\"radio\" name=\"" . $this->get_field_name("show_counts") . "\" value=\"1\"";
  293. if ($show_counts == 1) {
  294. echo " checked=\"checked\" ";
  295. }
  296. echo "> Yes ";
  297. echo "<input type=\"radio\" name=\"" . $this->get_field_name("show_counts") . "\" value=\"0\"";
  298. if ($show_counts != 0) {
  299. echo " checked=\"checked\" ";
  300. }
  301. echo "> No";
  302. echo "</label></p>";
  303.  
  304.  
  305.  
  306. // Submit (hidden field)
  307. echo "<input type=\"hidden\" id=\"" . $this->get_field_id("submit") ."\" name=\"" .
  308. $this->get_field_name("submit") . "\" value=\"1\" />";
  309. }
  310.  
  311. /**
  312. * Register widget.
  313. */
  314. function register()
  315. {
  316. register_widget("WP_CustomType_Archive_Widget");
  317. }
  318.  
  319. }
  320.  
  321.  
  322. // widgets_init hook, calls load widget function.
  323. add_action("widgets_init", array('WP_CustomType_Archive_Widget', 'register'));
  324.  
  325. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement