Skip to content

Commit b3b4978

Browse files
committed
Cleaned up widget code a bit
Related to #8
1 parent 49d518a commit b3b4978

File tree

2 files changed

+108
-27
lines changed

2 files changed

+108
-27
lines changed

includes/widgets/class-popular-posts.php

+100-19
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,120 @@
1313
*/
1414
class Heart_This_Popular_Posts extends WP_Widget {
1515

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,
2043
array(
44+
'classname' => 'heart-this-widget heart-this-popular-posts',
2145
'description' => __( 'Displays your most popular posts sorted by most liked', 'heart-this' ),
2246
)
2347
);
48+
49+
$control_options = wp_parse_args( $control_options, array() );
50+
51+
parent::__construct( $this->id_base, $this->name, $widget_options, $control_options );
2452
}
2553

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 );
2867
$posts = empty( $instance['posts'] ) ? 1 : $instance['posts'];
2968

3069
require HEART_THIS_DIR . 'includes/widgets/display-popular-posts.php';
3170
}
3271

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;
3892

39-
return $new_instance;
93+
return $instance;
4094
}
4195

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 );
49130

50131
require HEART_THIS_DIR . 'includes/widgets/form-popular-posts.php';
51132
}

includes/widgets/form-popular-posts.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010

1111
?>
1212
<p>
13-
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'heart-this' ); ?></label>
14-
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $instance['title']; ?>" />
13+
<label for="<?php $this->field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'heart-this' ); ?></label>
14+
<input class="widefat" id="<?php $this->field_id( 'title' ); ?>" name="<?php $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $instance['title']; ?>" />
1515
</p>
1616
<p>
17-
<label for="<?php echo $this->get_field_id( 'description' ); ?>"><?php esc_html_e( 'Description:', 'heart-this' ); ?></label>
18-
<input class="widefat" id="<?php echo $this->get_field_id( 'description' ); ?>" name="<?php echo $this->get_field_name( 'description' ); ?>" type="text" value="<?php echo $instance['description']; ?>" />
17+
<label for="<?php $this->field_id( 'description' ); ?>"><?php esc_html_e( 'Description:', 'heart-this' ); ?></label>
18+
<input class="widefat" id="<?php $this->field_id( 'description' ); ?>" name="<?php $this->get_field_name( 'description' ); ?>" type="text" value="<?php echo $instance['description']; ?>" />
1919
</p>
2020
<p>
21-
<label for="<?php echo $this->get_field_id( 'posts' ); ?>"><?php esc_html_e( 'Posts:', 'heart-this' ); ?></label>
22-
<input id="<?php echo $this->get_field_id( 'posts' ); ?>" name="<?php echo $this->get_field_name( 'posts' ); ?>" type="number" min="1" value="<?php echo $instance['posts']; ?>" size="3" />
21+
<label for="<?php $this->field_id( 'posts' ); ?>"><?php esc_html_e( 'Posts:', 'heart-this' ); ?></label>
22+
<input id="<?php $this->field_id( 'posts' ); ?>" name="<?php $this->get_field_name( 'posts' ); ?>" type="number" min="1" value="<?php echo $instance['posts']; ?>" size="3" />
2323
</p>
2424
<p>
25-
<input id="<?php echo $this->get_field_id( 'display_count' ); ?>" name="<?php echo $this->get_field_name( 'display_count' ); ?>" type="checkbox" value="1" <?php checked( $instance['display_count'] ); ?>>
26-
<label for="<?php echo $this->get_field_id( 'display_count' ); ?>"><?php esc_html_e( 'Display like counts', 'heart-this' ); ?></label>
25+
<input id="<?php $this->field_id( 'display_count' ); ?>" name="<?php $this->get_field_name( 'display_count' ); ?>" type="checkbox" value="1" <?php checked( $instance['display_count'] ); ?>>
26+
<label for="<?php $this->field_id( 'display_count' ); ?>"><?php esc_html_e( 'Display like counts', 'heart-this' ); ?></label>
2727
</p>

0 commit comments

Comments
 (0)