-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.php
More file actions
75 lines (70 loc) · 2.73 KB
/
widget.php
File metadata and controls
75 lines (70 loc) · 2.73 KB
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
<?php
/**
* Extend widget CLass.
*/
class Selected_Posts_Widget_Widget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('Selected Posts Widget', 'selected-posts-widget' ) );
}
function widget( $args, $instance ) {
include plugin_dir_path( __FILE__ ) . '/widget-template.php';
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Selected Posts', 'selected-posts-widget' );
$posts = ! empty( $instance['posts'] ) ? $instance['posts'] : '';
$post_arr = array_unique( explode( ' ', $posts ) );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( esc_attr( 'Title:' ) ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'posts' ) ); ?>"><?php _e( esc_attr( 'Select Post', 'selected-posts-widget' ) ); ?></label>
<div class="post-suggestion-wrap">
<input class="widefat post-suggestion" id="<?php echo esc_attr( $this->get_field_id( 'posts' ) ); ?>" type="text" placeholder="<?php _e('Slowly type for suggestions', 'selected-posts-widget'); ?>" autocomplete="off" />
<input type="hidden" name="<?php echo esc_attr( $this->get_field_name( 'posts' ) ); ?>" value="<?php echo $posts; ?>" />
<div class="selected-posts">
<div data-template style="display:none">
<div data-id="{id}">
<span>{title}</span>
<span class="remove">×</span>
</div>
</div>
<?php foreach( (array) $post_arr as $id ): ?>
<?php if( !empty( $id) ): ?>
<div data-id="<?php echo $id; ?>">
<span><?php echo get_the_title( $id ); ?></span>
<span class="remove">×</span>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['posts'] = ( ! empty( $new_instance['posts'] ) ) ? trim(strip_tags( $new_instance['posts'] )) : '';
return $instance;
}
}