/swatch/includes/widgets/widget-woo-feedback.php

https://github.com/fauverism/swatch-remix · PHP · 239 lines · 110 code · 49 blank · 80 comment · 8 complexity · 8d38c1b4dc77a703bcd5269e3f33e161 MD5 · raw file

  1. <?php
  2. /*-----------------------------------------------------------------------------------
  3. CLASS INFORMATION
  4. Description: A custom WooThemes Feedback widget.
  5. Date Created: 2011-08-18.
  6. Last Modified: 2011-08-18.
  7. Author: Matty @ WooThemes.
  8. Since: 4.5.0
  9. TABLE OF CONTENTS
  10. - function (constructor)
  11. - function widget ()
  12. - function update ()
  13. - function form ()
  14. - Register the widget on `widgets_init`.
  15. -----------------------------------------------------------------------------------*/
  16. class Woo_Widget_Feedback extends WP_Widget {
  17. /*----------------------------------------
  18. Constructor.
  19. ----------------------------------------
  20. * The constructor. Sets up the widget.
  21. ----------------------------------------*/
  22. function Woo_Widget_Feedback () {
  23. /* Widget settings. */
  24. $widget_ops = array( 'classname' => 'widget_woo_feedback', 'description' => __( 'Display your feedback in a customised widget.', 'woothemes' ) );
  25. /* Widget control settings. */
  26. $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'woo_feedback' );
  27. /* Create the widget. */
  28. $this->WP_Widget( 'woo_feedback', __('Woo - Feedback', 'woothemes' ), $widget_ops, $control_ops );
  29. /* Make sure the JavaScript for this widget loads. */
  30. add_filter( 'woo_load_feedback_js', '__return_true', 10 );
  31. } // End Constructor
  32. /*----------------------------------------
  33. widget()
  34. ----------------------------------------
  35. * Displays the widget on the frontend.
  36. ----------------------------------------*/
  37. function widget( $args, $instance ) {
  38. $html = '';
  39. extract( $args, EXTR_SKIP );
  40. /* Our variables from the widget settings. */
  41. $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base );
  42. $limit = $instance['limit']; if ( ! intval( $limit ) ) { $limit = 5; }
  43. $effect = $instance['effect'];
  44. $pagination = $instance['pagination'];
  45. $display_author = $instance['display_author'];
  46. $display_url = $instance['display_url'];
  47. $unique_id = $args['widget_id'];
  48. // Make sure our checkboxes are either true if available or false if empty.
  49. foreach ( array( 'pagination', 'display_author', 'display_url' ) as $k ) {
  50. if ( isset( $instance[$k] ) ) {
  51. if ( $instance[$k] == true ) {
  52. ${$k} = true;
  53. } else {
  54. ${$k} = false;
  55. }
  56. } else {
  57. ${$k} = false;
  58. }
  59. }
  60. /* Before widget (defined by themes). */
  61. echo $before_widget;
  62. /* Display the widget title if one was input (before and after defined by themes). */
  63. if ( $title ) {
  64. echo $before_title . $title . $after_title;
  65. } // End IF Statement
  66. /* Widget content. */
  67. // Add actions for plugins/themes to hook onto.
  68. do_action( 'woo_widget_feedback_top' );
  69. $html = '';
  70. $query_args = array(
  71. 'echo' => false
  72. );
  73. if ( $limit > 0 ) {
  74. $query_args['limit'] = $limit;
  75. }
  76. $query_args['effect'] = $effect;
  77. $query_args['pagination'] = $pagination;
  78. $query_args['display_author'] = $display_author;
  79. $query_args['display_url'] = $display_url;
  80. $html .= woo_display_feedback_entries( $query_args );
  81. echo $html;
  82. // Add actions for plugins/themes to hook onto.
  83. do_action( 'woo_widget_feedback_bottom' );
  84. /* After widget (defined by themes). */
  85. echo $after_widget;
  86. } // End widget()
  87. /*----------------------------------------
  88. update()
  89. ----------------------------------------
  90. * Function to update the settings from
  91. * the form() function.
  92. * Params:
  93. * - Array $new_instance
  94. * - Array $old_instance
  95. ----------------------------------------*/
  96. function update ( $new_instance, $old_instance ) {
  97. $instance = $old_instance;
  98. /* Strip tags for title and name to remove HTML (important for text inputs). */
  99. $instance['title'] = strip_tags( $new_instance['title'] );
  100. $instance['limit'] = esc_attr( $new_instance['limit'] );
  101. /* The select box is returning a text value, so we escape it. */
  102. $instance['effect'] = esc_attr( $new_instance['effect'] );
  103. /* The checkbox is returning a Boolean (true/false), so we check for that. */
  104. $instance['pagination'] = (bool) esc_attr( $new_instance['pagination'] );
  105. $instance['display_author'] = (bool) esc_attr( $new_instance['display_author'] );
  106. $instance['display_url'] = (bool) esc_attr( $new_instance['display_url'] );
  107. return $instance;
  108. } // End update()
  109. /*----------------------------------------
  110. form()
  111. ----------------------------------------
  112. * The form on the widget control in the
  113. * widget administration area.
  114. * Make use of the get_field_id() and
  115. * get_field_name() function when creating
  116. * your form elements. This handles the confusing stuff.
  117. * Params:
  118. * - Array $instance
  119. ----------------------------------------*/
  120. function form( $instance ) {
  121. /* Set up some default widget settings. */
  122. $defaults = array(
  123. 'title' => __( 'Feedback', 'woothemes' ),
  124. 'limit' => 5,
  125. 'effect' => 'fade',
  126. 'pagination' => false,
  127. 'display_author' => false,
  128. 'display_url' => false
  129. );
  130. $instance = wp_parse_args( (array) $instance, $defaults );
  131. ?>
  132. <!-- Widget Title: Text Input -->
  133. <p>
  134. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title (optional):', 'woothemes' ); ?></label>
  135. <input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" />
  136. </p>
  137. <!-- Widget Limit: Text Input -->
  138. <p>
  139. <label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Limit:', 'woothemes' ); ?></label>
  140. <input type="text" name="<?php echo $this->get_field_name( 'limit' ); ?>" value="<?php echo $instance['limit']; ?>" class="" size="3" id="<?php echo $this->get_field_id( 'limit' ); ?>" />
  141. </p>
  142. <!-- Widget Effect: Select Input -->
  143. <p>
  144. <label for="<?php echo $this->get_field_id( 'effect' ); ?>"><?php _e( 'Effect:', 'woothemes' ); ?></label>
  145. <select name="<?php echo $this->get_field_name( 'effect' ); ?>" class="widefat" id="<?php echo $this->get_field_id( 'effect' ); ?>">
  146. <option value="none"<?php selected( $instance['effect'], 'none' ); ?>><?php _e( 'None', 'woothemes' ); ?></option>
  147. <option value="fade"<?php selected( $instance['effect'], 'fade' ); ?>><?php _e( 'Fade', 'woothemes' ); ?></option>
  148. </select>
  149. </p>
  150. <!-- Widget Pagination: Checkbox Input -->
  151. <p>
  152. <input id="<?php echo $this->get_field_id( 'pagination' ); ?>" name="<?php echo $this->get_field_name( 'pagination' ); ?>" type="checkbox"<?php checked( $instance['pagination'], 1 ); ?> />
  153. <label for="<?php echo $this->get_field_id( 'pagination' ); ?>"><?php _e( 'Enable Pagination', 'woothemes' ); ?></label>
  154. <br /><small>(<?php _e( 'Disabled if the "limit" is 1', 'woothemes' ); ?>)</small>
  155. </p>
  156. <!-- Widget Display Author: Checkbox Input -->
  157. <p>
  158. <input id="<?php echo $this->get_field_id( 'display_author' ); ?>" name="<?php echo $this->get_field_name( 'display_author' ); ?>" type="checkbox"<?php checked( $instance['display_author'], 1 ); ?> />
  159. <label for="<?php echo $this->get_field_id( 'display_author' ); ?>"><?php _e( 'Display Author', 'woothemes' ); ?></label>
  160. </p>
  161. <!-- Widget Display URL: Checkbox Input -->
  162. <p>
  163. <input id="<?php echo $this->get_field_id( 'display_url' ); ?>" name="<?php echo $this->get_field_name( 'display_url' ); ?>" type="checkbox"<?php checked( $instance['display_url'], 1 ); ?> />
  164. <label for="<?php echo $this->get_field_id( 'display_url' ); ?>"><?php _e( 'Display URL', 'woothemes' ); ?></label>
  165. </p>
  166. <?php
  167. } // End form()
  168. } // End Class
  169. /*----------------------------------------
  170. Register the widget on `widgets_init`.
  171. ----------------------------------------
  172. * Registers this widget.
  173. ----------------------------------------*/
  174. add_action( 'widgets_init', create_function( '', 'return register_widget("Woo_Widget_Feedback");' ), 1 );
  175. ?>