|
13 | 13 | */
|
14 | 14 | class Heart_This_Popular_Posts extends WP_Widget {
|
15 | 15 |
|
16 |
| - function __construct() { |
17 |
| - parent::__construct( |
18 |
| - 'heart_this_widget', |
19 |
| - __( 'Heart This - Popular Posts', 'heart-this' ), |
| 16 | + /** |
| 17 | + * Holds widget settings defaults, populated in the child class' constructor. |
| 18 | + * |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + protected $defaults; |
| 22 | + |
| 23 | + /** |
| 24 | + * Register widget information and default settings. |
| 25 | + * |
| 26 | + * @since 0.1.0 |
| 27 | + * @access public |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function __construct( $id_base = false, $name = false, $widget_options = array(), $control_options = array() ) { |
| 31 | + $this->id_base = ( $id_base ) ? $id_base : 'heart-this-widget'; |
| 32 | + $this->name = ( $name ) ? $name : __( 'HeartThis - Popular Posts', 'heart-this' ); |
| 33 | + |
| 34 | + $this->defaults = array( |
| 35 | + 'title' => __( 'Popular Posts', 'heart-this' ), |
| 36 | + 'description' => '', |
| 37 | + 'posts' => 1, |
| 38 | + 'display_count' => 1, |
| 39 | + ); |
| 40 | + |
| 41 | + $widget_options = wp_parse_args( |
| 42 | + $widget_options, |
20 | 43 | array(
|
| 44 | + 'classname' => 'heart-this-widget heart-this-popular-posts', |
21 | 45 | 'description' => __( 'Displays your most popular posts sorted by most liked', 'heart-this' ),
|
22 | 46 | )
|
23 | 47 | );
|
| 48 | + |
| 49 | + $control_options = wp_parse_args( $control_options, array() ); |
| 50 | + |
| 51 | + parent::__construct( $this->id_base, $this->name, $widget_options, $control_options ); |
24 | 52 | }
|
25 | 53 |
|
26 |
| - function widget( $args, $instance ) { |
27 |
| - $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Popular Posts', 'heart-this' ) : $instance['title'], $instance, $this->id_base ); |
| 54 | + /** |
| 55 | + * Display the widget content. |
| 56 | + * |
| 57 | + * @since 1.0.0 |
| 58 | + * @access public |
| 59 | + * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 60 | + * @param array $instance The settings for the particular instance of the widget. |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + public function widget( $args, $instance ) { |
| 64 | + $instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 65 | + |
| 66 | + $title = apply_filters( 'widget_title', $title, $instance['title'], $this->id_base ); |
28 | 67 | $posts = empty( $instance['posts'] ) ? 1 : $instance['posts'];
|
29 | 68 |
|
30 | 69 | require HEART_THIS_DIR . 'includes/widgets/display-popular-posts.php';
|
31 | 70 | }
|
32 | 71 |
|
33 |
| - function update( $new_instance, $old_instance ) { |
34 |
| - $new_instance['title'] = wp_strip_all_tags( $new_instance['title'] ); |
35 |
| - $new_instance['description'] = strip_tags( $new_instance['description'], '<a><b><strong><i><em><span>' ); |
36 |
| - $new_instance['posts'] = is_integer( absint( $new_instance['posts'] ) ) ? absint( $new_instance['posts'] ) : 1; |
37 |
| - $new_instance['display_count'] = ( !empty( $new_instance['display_count'] ) ) ? 1 : 0; |
| 72 | + /** |
| 73 | + * Update a particular instance. |
| 74 | + * |
| 75 | + * This function should check that $new_instance is set correctly. |
| 76 | + * The newly calculated value of $instance should be returned. |
| 77 | + * If "false" is returned, the instance won't be saved/updated. |
| 78 | + * |
| 79 | + * @since 1.0.0 |
| 80 | + * @access public |
| 81 | + * @param array $new_instance New settings for this instance as input by the user via form(). |
| 82 | + * @param array $old_instance Old settings for this instance. |
| 83 | + * @return array Settings to save or bool false to cancel saving |
| 84 | + */ |
| 85 | + public function update( $new_instance, $old_instance ) { |
| 86 | + $instance = $new_instance; |
| 87 | + |
| 88 | + $instance['title'] = wp_strip_all_tags( $instance['title'] ); |
| 89 | + $instance['description'] = strip_tags( $instance['description'], '<a><b><strong><i><em><span>' ); |
| 90 | + $instance['posts'] = empty( $instance['posts'] ) ? 1 : absint( $instance['posts'] ); |
| 91 | + $instance['display_count'] = empty( $instance['display_count'] ) ? 0 : 1; |
38 | 92 |
|
39 |
| - return $new_instance; |
| 93 | + return $instance; |
40 | 94 | }
|
41 | 95 |
|
42 |
| - function form( $instance ) { |
43 |
| - $instance = wp_parse_args( (array) $instance, array( |
44 |
| - 'title' => __( 'Popular Posts', 'heart-this' ), |
45 |
| - 'description' => '', |
46 |
| - 'posts' => 1, |
47 |
| - 'display_count' => 1, |
48 |
| - ) ); |
| 96 | + /** |
| 97 | + * Output a widget field ID. |
| 98 | + * |
| 99 | + * @since 0.1.0 |
| 100 | + * @access public |
| 101 | + * @param string $name The field name to be echoed. |
| 102 | + * @return void |
| 103 | + */ |
| 104 | + public function field_id( $name ) { |
| 105 | + echo $this->get_field_id( $name ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Output a widget field name. |
| 110 | + * |
| 111 | + * @since 0.1.0 |
| 112 | + * @access public |
| 113 | + * @param string $name The field name to be echoed. |
| 114 | + * @return void |
| 115 | + */ |
| 116 | + public function field_name( $name ) { |
| 117 | + echo $this->get_field_name( $name ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Load a template to display the widget form. |
| 122 | + * |
| 123 | + * @since 0.1.0 |
| 124 | + * @access public |
| 125 | + * @param array $instance The current widget form data. |
| 126 | + * @return void |
| 127 | + */ |
| 128 | + public function form( $instance ) { |
| 129 | + $instance = wp_parse_args( (array) $instance, $this->defaults ); |
49 | 130 |
|
50 | 131 | require HEART_THIS_DIR . 'includes/widgets/form-popular-posts.php';
|
51 | 132 | }
|
|
0 commit comments