NGG_Gallery_Widget

The NextCellent Gallery Widget.

Defined (1)

The class is defined in the following location(s).

/widgets/class-ngg-gallery-widget.php  
  1. class NGG_Gallery_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * Register the widget. 
  5. */ 
  6. public function __construct() { 
  7. parent::__construct( 'ngg-images', __( 'NextCellent Gallery Widget', 'nggallery' ), array( 
  8. 'classname' => 'ngg_images',  
  9. 'description' => __( 'Add recent or random images from the galleries', 'nggallery' ) 
  10. ) ); 
  11.  
  12. /** 
  13. * Sanitize widget form values as they are saved. 
  14. * @see WP_Widget::update() 
  15. * @param array $new_instance Values just sent to be saved. 
  16. * @param array $old_instance Previously saved values from database. 
  17. * @return array Updated safe values to be saved. 
  18. */ 
  19. public function update( $new_instance, $old_instance ) { 
  20. $instance = $old_instance; 
  21.  
  22. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  23. $instance['items'] = (int) $new_instance['items']; 
  24.  
  25. //Validate type 
  26. if ( $new_instance['type'] == 'random' ) { 
  27. $instance['type'] = 'random'; 
  28. } else { 
  29. $instance['type'] = 'recent'; 
  30.  
  31. //Validate show 
  32. if ( $new_instance['show'] == "thumbnail" ) { 
  33. $instance['show'] = 'thumbnail'; 
  34. } else { 
  35. $instance['show'] = 'original'; 
  36.  
  37. $instance['width'] = (int) $new_instance['width']; 
  38. $instance['height'] = (int) $new_instance['height']; 
  39.  
  40. if( $new_instance['exclude'] == 'denied' ) { 
  41. $instance['exclude'] = 'denied'; 
  42. } elseif ( $new_instance['exclude'] == 'allow' ) { 
  43. $instance['exclude'] = 'allow'; 
  44. } else { 
  45. $instance['exclude'] = 'all'; 
  46.  
  47. $temp_array = explode(", ", $new_instance['list']); 
  48. array_walk( $temp_array, 'intval' ); 
  49. $instance['list'] = implode(", ", $temp_array); 
  50.  
  51. return $instance; 
  52.  
  53. /** 
  54. * Back-end widget form. 
  55. * @see WP_Widget::form() 
  56. * @param array $instance Previously saved values from database. 
  57. * @return string Default return is 'noform'. 
  58. */ 
  59. public function form( $instance ) { 
  60.  
  61. //Defaults 
  62. $instance = wp_parse_args( (array) $instance, array( 
  63. 'title' => 'Gallery',  
  64. 'items' => '4',  
  65. 'type' => 'random',  
  66. 'show' => 'thumbnail',  
  67. 'height' => '50',  
  68. 'width' => '75',  
  69. 'exclude' => 'all',  
  70. 'list' => '',  
  71. 'webslice' => true 
  72. ) ); 
  73. $title = esc_attr( $instance['title'] ); 
  74. $items = intval( $instance['items'] ); 
  75. $height = esc_attr( $instance['height'] ); 
  76. $width = esc_attr( $instance['width'] ); 
  77.  
  78. ?> 
  79.  
  80. <p> 
  81. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'nggallery' ); ?></label> 
  82. <input id="<?php echo $this->get_field_id( 'title' ); ?>" 
  83. name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" class="widefat" 
  84. value="<?php echo $title; ?>"/> 
  85. </p> 
  86. <p> 
  87. <label for="<?php echo $this->get_field_id( 'items' ); ?>"><?php _e( 'Show:', 'nggallery' ); ?></label><br/> 
  88. <input style="width: 60px;" id="<?php echo $this->get_field_id( 'items' ); ?>" 
  89. name="<?php echo $this->get_field_name( 'items' ); ?>" type="number" min="0" 
  90. value="<?php echo $items; ?>"/> 
  91. <select id="<?php echo $this->get_field_id( 'show' ); ?>" 
  92. name="<?php echo $this->get_field_name( 'show' ); ?>"> 
  93. <option <?php selected( "thumbnail", $instance['show'] ); ?> 
  94. value="thumbnail"><?php _e( 'Thumbnails', 'nggallery' ); ?></option> 
  95. <option <?php selected( "original", $instance['show'] ); ?> 
  96. value="original"><?php _e( 'Original images', 'nggallery' ); ?></option> 
  97. </select> 
  98. </p> 
  99. <p> 
  100. <input id="<?php echo $this->get_field_id( 'type' ); ?>_random" 
  101. name="<?php echo $this->get_field_name( 'type' ); ?>" type="radio" 
  102. value="random" <?php checked( "random", $instance['type'] ); ?> /> 
  103. <label 
  104. for="<?php echo $this->get_field_id( 'type' ); ?>_random"><?php _e( 'random', 'nggallery' ); ?></label> 
  105. <br/> 
  106. <input id="<?php echo $this->get_field_id( 'type' ); ?>_recent" 
  107. name="<?php echo $this->get_field_name( 'type' ); ?>" type="radio" 
  108. value="recent" <?php checked( "recent", $instance['type'] ); ?> /> 
  109. <label 
  110. for="<?php echo $this->get_field_id( 'type' ); ?>_recent"><?php _e( 'recent added ', 'nggallery' ); ?></label> 
  111. </p> 
  112. <table> 
  113. <tr> 
  114. <td><label 
  115. for="<?php echo $this->get_field_id( 'width' ); ?>"><?php _e( 'Width:', 'nggallery' ); ?></label> 
  116. </td> 
  117. <td><input style="width: 60px; padding:3px;" id="<?php echo $this->get_field_id( 'width' ); ?>" 
  118. name="<?php echo $this->get_field_name( 'width' ); ?>" type="number" min="0" 
  119. value="<?php echo $width; ?>"/> px 
  120. </td> 
  121. </tr> 
  122. <tr> 
  123. <td><label 
  124. for="<?php echo $this->get_field_id( 'height' ); ?>"><?php _e( 'Height:', 'nggallery' ); ?></label> 
  125. </td> 
  126. <td><input style="width: 60px; padding:3px;" id="<?php echo $this->get_field_id( 'height' ); ?>" 
  127. name="<?php echo $this->get_field_name( 'height' ); ?>" type="number" min="0" 
  128. value="<?php echo $height; ?>"/> px 
  129. </td> 
  130. </tr> 
  131. </table> 
  132. <p> 
  133. <label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><?php _e( 'Select:', 'nggallery' ); ?></label> 
  134. <select id="<?php echo $this->get_field_id( 'exclude' ); ?>" 
  135. name="<?php echo $this->get_field_name( 'exclude' ); ?>" class="widefat"> 
  136. <option <?php selected( "all", $instance['exclude'] ); ?> 
  137. value="all"><?php _e( 'All galleries', 'nggallery' ); ?></option> 
  138. <option <?php selected( "denied", $instance['exclude'] ); ?> 
  139. value="denied"><?php _e( 'Only which are not listed', 'nggallery' ); ?></option> 
  140. <option <?php selected( "allow", $instance['exclude'] ); ?> 
  141. value="allow"><?php _e( 'Only which are listed', 'nggallery' ); ?></option> 
  142. </select> 
  143. </p> 
  144. <p> 
  145. <label for="<?php echo $this->get_field_id( 'list' ); ?>"><?php _e( 'Gallery ID:', 'nggallery' ); ?></label> 
  146. <input id="<?php echo $this->get_field_id( 'list' ); ?>" 
  147. name="<?php echo $this->get_field_name( 'list' ); ?>" type="text" class="widefat" 
  148. value="<?php echo $instance['list']; ?>"/> 
  149. <label class="description" 
  150. for="<?php echo $this->get_field_id( 'list' ); ?>"><?php _e( 'Gallery IDs, separated by commas.', 'nggallery' ); ?></label> 
  151. </p> 
  152.  
  153. <?php 
  154.  
  155.  
  156. /** 
  157. * Front-end display of widget. 
  158. * @see WP_Widget::widget() 
  159. * @todo Needs to be better, without the mess to call to the database, but this requires a better database API. 
  160. * @param array $args Widget arguments. 
  161. * @param array $instance Saved values from the database. 
  162. */ 
  163. public function widget( $args, $instance ) { 
  164.  
  165. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? ' ' : $instance['title'], $instance, $this->id_base ); 
  166.  
  167. global $wpdb; 
  168.  
  169. $items = min( $instance['items'], $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->nggpictures WHERE exclude != 1 " ) ); 
  170. $exclude = $instance['exclude']; 
  171. $list = $instance['list']; 
  172.  
  173. $exclude_list = ''; 
  174.  
  175. // THX to Kay Germer for the idea & addon code 
  176. if ( !empty( $list ) && $exclude != 'all' ) { 
  177. $list = explode( ', ', $list ); 
  178. // Prepare for SQL 
  179. $list = "'" . implode( "', '", $list ) . "'"; 
  180.  
  181. if ( $exclude == 'denied' ) { 
  182. $exclude_list = "AND NOT (t.gid IN ($list))"; 
  183.  
  184. if ( $exclude == 'allow' ) { 
  185. $exclude_list = "AND t.gid IN ($list)"; 
  186.  
  187. if ( $instance['type'] == 'random' ) { 
  188. $imageList = $wpdb->get_results( "SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.exclude != 1 $exclude_list ORDER by rand() limit {$items}" ); 
  189. } else { 
  190. $imageList = $wpdb->get_results( "SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.exclude != 1 $exclude_list ORDER by pid DESC limit 0, $items" ); 
  191.  
  192. echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; 
  193. echo "\n" . '<div class="ngg-widget entry-content">' . "\n"; 
  194.  
  195. if ( is_array( $imageList ) ) { 
  196. foreach ( $imageList as $image ) { 
  197. // get the URL constructor 
  198. $image = new nggImage( $image ); 
  199.  
  200. // get the effect code 
  201. $thumbcode = $image->get_thumbcode( $args['widget_id'] ); 
  202.  
  203. // enable i18n support for alttext and description 
  204. $alttext = htmlspecialchars( stripslashes( nggGallery::i18n( $image->alttext, 'pic_' . $image->pid . '_alttext' ) ) ); 
  205. $description = htmlspecialchars( stripslashes( nggGallery::i18n( $image->description, 'pic_' . $image->pid . '_description' ) ) ); 
  206.  
  207. //TODO:For mixed portrait/landscape it's better to use only the height setting, if widht is 0 or vice versa 
  208. $out = '<a href="' . $image->imageURL . '" title="' . $description . '" ' . $thumbcode . '>'; 
  209.  
  210. if ( $instance['show'] == 'original' ) { 
  211. $out .= '<img src="' . trailingslashit( home_url() ) . 'index.php?callback=image&pid=' . $image->pid . '&width=' . $instance['width'] . '&height=' . $instance['height'] . '" title="' . $alttext . '" alt="' . $alttext . '" />'; 
  212. } else { 
  213. $out .= '<img src="' . $image->thumbURL . '" width="' . $instance['width'] . '" height="' . $instance['height'] . '" title="' . $alttext . '" alt="' . $alttext . '" />'; 
  214.  
  215. echo $out . '</a>' . "\n"; 
  216.  
  217.  
  218. echo '</div>' . "\n"; 
  219. echo $args['after_widget']; 
  220.