-
Notifications
You must be signed in to change notification settings - Fork 9
/
tl-widget.php
109 lines (97 loc) · 3.84 KB
/
tl-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Widget class
*
* @package The_Loops
* @since 0.1
*/
class TL_Widget extends WP_Widget {
function __construct() {
parent::__construct( 'tl_widget', __( 'The Loops' ), array( 'description' => __( 'Use this widget to add one of your loops as a widget.' ) ) );
}
function widget( $args, $instance ) {
extract( $args );
$instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $before_widget;
if ( $instance['title'] )
echo $before_title . $instance['title'] . $after_title;
$args = array(
'offset' => $instance['offset'],
'posts_per_page' => $instance['posts_per_page']
);
echo tl_display_loop( $instance['loop_id'], $instance['template'], $args, 'widget' );
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['loop_id'] = absint( $new_instance['loop_id'] );
$instance['offset'] = absint( $new_instance['offset'] );
$instance['posts_per_page'] = absint( $new_instance['posts_per_page'] );
$instance['template'] = $new_instance['template'];
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
function form( $instance ) {
$defaults = array(
'loop_id' => 0,
'offset' => 0,
'posts_per_page' => get_option( 'posts_per_page' ) / 2,
'template' => 'List of titles',
'title' => ''
);
$instance = wp_parse_args( (array) $instance, $defaults );
$loop_id = $instance['loop_id'];
$posts_per_page = $instance['posts_per_page'];
$offset = $instance['offset'];
$template = esc_attr( $instance['template'] );
$title = esc_attr( $instance['title'] );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'the-loops' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('loop_id'); ?>"><?php _e('Loop:', 'the-loops' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('loop_id'); ?>" name="<?php echo $this->get_field_name('loop_id'); ?>">
<?php
$loops = tl_get_loops();
foreach ( $loops as $loop ) {
$selected = selected( $loop_id, $loop->ID, false );
echo "<option value='$loop->ID'$selected>$loop->post_title</option>";
}
?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('posts_per_page'); ?>"><?php _e( 'Number of items:', 'the-loops' ); ?></label>
<input id="<?php echo $this->get_field_id('posts_per_page'); ?>" name="<?php echo $this->get_field_name('posts_per_page'); ?>" type="text" value="<?php echo $posts_per_page; ?>" class="small-text" />
</p>
<p>
<label for="<?php echo $this->get_field_id('offset'); ?>"><?php _e( 'Offset:', 'the-loops' ); ?></label>
<input id="<?php echo $this->get_field_id('offset'); ?>" name="<?php echo $this->get_field_name('offset'); ?>" type="text" value="<?php echo $offset; ?>" class="small-text" />
</p>
<p>
<label for="<?php echo $this->get_field_id('template'); ?>"><?php _e( 'Template:', 'the-loops' ); ?></label>
<select id="<?php echo $this->get_field_id('template'); ?>" name="<?php echo $this->get_field_name('template'); ?>" class="widefat">
<?php
$loop_templates = tl_get_loop_templates();
foreach ( $loop_templates as $name => $file ) {
$selected = selected( $name, $template, false );
echo "<option value='". esc_attr( $name ) ."'$selected>{$name}</option>";
}
?>
</select>
</p>
<?php
}
}
/**
* Register widget
*
* @package The_Loops
* @since 0.1
*/
function tl_widget_init() {
register_widget('TL_Widget');
}
add_action( 'widgets_init', 'tl_widget_init' );