PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/autonav-widget.php

https://bitbucket.org/wlindley/autonav
PHP | 260 lines | 221 code | 7 blank | 32 comment | 4 complexity | 436e047e5260a5a02137fa9b3e082c3f MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: AutoNav Widget
  4. Version: 1.5.10
  5. Plugin URI: http://www.wlindley.com/website/autonav/
  6. Description: Adds a sidebar widget to display attachments or navigational tables/lists.
  7. Author: William Lindley
  8. Author URI: http://www.wlindley.com/
  9. License: GPL2
  10. */
  11. /* Copyright 2013-2015 William Lindley (email : wlindley -at- wlindley -dot- com)
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. if (!class_exists('WL_Config_Form')) {
  25. include 'wl-config-form.php';
  26. }
  27. if (!function_exists('get_filter')) {
  28. function get_filter($tag) {
  29. # Returns the current state of the given WordPress filter.
  30. global $wp_filter;
  31. return $wp_filter[$tag];
  32. }
  33. function set_filter($tag, $saved) {
  34. # Sets the given WordPress filter to a state saved by get_filter.
  35. remove_all_filters($tag);
  36. foreach ($saved as $priority => $func_list) {
  37. foreach ($func_list as $func_name => $func_args) {
  38. add_filter($tag,$func_args['function'], $priority, $func_args['accepted_args']);
  39. }
  40. }
  41. }
  42. }
  43. class AutoNavWidget extends WP_Widget
  44. {
  45. /**
  46. * Declares the HierPageWidget class.
  47. *
  48. */
  49. function AutonavWidget(){
  50. $widget_ops = array('classname' => 'widget_autonav', 'description' => __( "AutoNav Navigation and Attachment Widget") );
  51. $control_ops = array('width' => 300, 'height' => 300);
  52. $this->WP_Widget('autonav', __('AutoNav'), $widget_ops, $control_ops);
  53. }
  54. # Specific to WP_Widget -derived
  55. function form_item($options, $option_name, $html_tags) {
  56. print '<p style="text-align:right;">';
  57. if (strlen($html_tags['title'])) {
  58. foreach (array('style','class') as $p) {
  59. if (!empty($html_tags["title.$p"])) {
  60. $extra_tags .= " $p=\"" . $html_tags["title.$p"] . '"';
  61. }
  62. }
  63. print '<label for="'.$html_tags['id'].'"' . $extra_tags .
  64. '>' . __($html_tags['title']) . '</label>';
  65. unset($html_tags['title']);
  66. }
  67. return $html_tags;
  68. }
  69. function form_item_close($options, $option_name, $html_tags) {
  70. print '</p>';
  71. return $html_tags;
  72. }
  73. function form_html($instance) {
  74. $option_menu = $this->known_params(1);
  75. $conf_menu = new WL_Config_Form;
  76. $conf_menu -> set_callbacks(array('item_pre' => array($this, 'form_item'),
  77. 'item_post' => array($this, 'form_item_close')));
  78. foreach ($option_menu as $option_name => $html_tags) {
  79. if ($html_tags['type'] != 'checkbox') {
  80. $html_tags['value'] = htmlspecialchars($instance[$option_name]);
  81. }
  82. $fieldname = strlen($html_tags['name']) ? $html_tags['name'] : $option_name;
  83. $html_tags['name'] = $this->get_field_name($fieldname);
  84. $html_tags['id'] = $this->get_field_id($option_name);
  85. $html_tags['style'] = 'width:150px;';
  86. $html_tags['class'] = 'srcs_widget_form_element';
  87. if (!array_key_exists('type',$html_tags)) {
  88. $html_tags['type'] = 'text';
  89. }
  90. $conf_menu->form_item($instance, $option_name, $html_tags);
  91. }
  92. }
  93. /**
  94. * Displays the Widget
  95. *
  96. */
  97. function widget($args, $instance){
  98. $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
  99. $known_params = $this->known_params(0);
  100. foreach ($known_params as $param) {
  101. if (strlen($instance[$param])) {
  102. $page_options[$param] = $instance[$param];
  103. }
  104. }
  105. print $args['before_widget'];
  106. if ( $title )
  107. print "{$args['before_title']}{$title}{$args['after_title']}";
  108. # $was_filter = get_filter('autonav_create_list_item');
  109. # remove_all_filters('autonav_create_list_item');
  110. /* Set filters according to user specifications. ~~~~ future */
  111. $autonav_args = array();
  112. $display_options = array($page_options['display']);
  113. foreach ($page_options as $option => $option_value) {
  114. if (substr($option, 0, 1) == '_') {
  115. $display_options[] = substr($option, 1);
  116. } else {
  117. $autonav_args[$option] = $option_value;
  118. }
  119. }
  120. $autonav_args['display'] = join(',', $display_options);
  121. print autonav_wl_shortcode($autonav_args);
  122. print $args['after_widget'];
  123. /* Restore filter state */
  124. # set_filter('autonav_create_list_item', $was_filter );
  125. }
  126. function known_params ($options = 0) {
  127. $option_menu = array('title' => array('title' => 'Title:'),
  128. 'display' => array('title' => 'What to display?',
  129. 'type' => 'radio',
  130. 'choices' => array('list' => 'List: child pages of current page.<br>',
  131. 'images' => 'Table: child pages of current page<br>',
  132. 'attached' => 'Table: images attached to current post/page<br>',
  133. 'attachments' => 'Table: from the "Attachments" plugin<br>',
  134. 'posts,list' => 'List: Pages/Posts per <em>postid</em><br>',
  135. 'posts' => 'Table: Pages/Posts per <em>postid</em>')),
  136. 'postid' => array('title' => 'Post ID (see Readme):'),
  137. 'orderby' => array('title' => 'Sort field:',
  138. 'text' => '<br>Comma-separated list: <em>post_title, menu_order, post_date, post_modified, ID, post_author, post_name, postmash, meta:</em>custom_field_name'),
  139. 'order' => array('title' => 'Sort direction:',
  140. 'type' => 'radio',
  141. 'choices' => array( 'asc' => 'Ascending<br>', 'desc' => 'Descending<br>',
  142. 'rand' => 'Random')),
  143. 'columns' => array('title' => 'Table Columns:'),
  144. 'combine' => array('type'=>'radio',
  145. 'title' => 'Combine Tables?',
  146. 'choices' => array(0 => 'All', 1 => 'None', 2 => 'Full rows only')),
  147. 'size' => array('title' => 'Thumbnail Size:'),
  148. 'crop' => array('type'=>'radio',
  149. 'title' => 'Thumbnail cropping',
  150. 'choices' => array(0 => 'Fit',
  151. 1 => 'Crop from center.',
  152. 2 => 'Crop from upper-left.',
  153. 3 => 'Crop from top middle.')),
  154. 'caption' => array('title' => 'Table Caption:'),
  155. 'pics_only' => array('title' => 'Only pages with pictures:',
  156. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  157. '_thumb' => array('title' => 'Force page/post thumbnails',
  158. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  159. '_nothumb' => array('title' => 'Suppress page/post thumbnails',
  160. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  161. '_title' => array('title' => 'Force page/post titles',
  162. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  163. '_notitle' => array('title' => 'Suppress page/post titles',
  164. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  165. '_excerpt' => array('title' => 'Display "manual excerpt" (see FAQ)',
  166. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  167. '_noexcerpt' => array('title' => 'Suppress manual excerpt',
  168. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  169. '_plain' => array('title' => 'Use <em>div</em> instead of list',
  170. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  171. '_siblings' => array('title' => 'Display siblings of current page',
  172. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  173. '_family' => array('title' => 'Display page\'s family (children, grandchildren...)',
  174. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  175. '_self' => array('title' => 'Include the current page<br>(with siblings, or family)',
  176. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  177. '_image' => array('title' => 'Posts link to full-size of thumbnail',
  178. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  179. '_page' => array('title' => 'Attachments link to attachment page',
  180. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  181. '_nolink' => array('title' => 'Disable links entirely',
  182. 'type' => 'checkbox', 'checked' => 1, 'value' => '1'),
  183. 'exclude' => array('title' => 'Exclude pages:',
  184. 'desc' => 'List of page IDs to exclude'),
  185. 'imgrel' => array('title' => 'Image relation tag:'),
  186. 'meta_key' => array('title' => 'Meta Key:'),
  187. 'meta_value' => array('title' => 'Meta-key Value:',
  188. 'desc' => 'for selecting pages by custom fields'),
  189. 'authors' => array('title' => 'Authors:'),
  190. 'post_status' => array('title' => 'Post status:',
  191. 'desc' => '(default: publish)'),
  192. 'target' => array('title' => 'Target frame to open link:'),
  193. );
  194. return ($options ? $option_menu : array_keys($option_menu));
  195. }
  196. /**
  197. * Saves the widget's settings.
  198. *
  199. */
  200. function update($new_instance, $old_instance){
  201. $instance = $old_instance;
  202. $known_params = $this->known_params();
  203. unset($instance['menu_order']);
  204. foreach ($known_params as $param) {
  205. $instance[$param] = strip_tags(stripslashes($new_instance[$param]));
  206. }
  207. $instance['sort_order'] = strtolower($instance['sort_order']);
  208. return $instance;
  209. }
  210. /**
  211. * Creates the edit form for the widget.
  212. *
  213. */
  214. function form($instance){
  215. $instance = wp_parse_args( (array) $instance, array('title'=>'') );
  216. if (empty($instance['orderby'])) {
  217. $instance['orderby'] = 'menu_order';
  218. }
  219. $this->form_html($instance);
  220. }
  221. }// END class
  222. /**
  223. * Register this widget.
  224. *
  225. * Calls 'widgets_init' action after the widget has been registered.
  226. */
  227. function AutoNavWidgetInit() {
  228. register_widget('AutoNavWidget');
  229. }
  230. add_action('widgets_init', 'AutoNavWidgetInit');