-
Notifications
You must be signed in to change notification settings - Fork 5
/
simple-google-photos-grid-widget.php
127 lines (114 loc) · 4.86 KB
/
simple-google-photos-grid-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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
include_once 'Simple_Google_Photos_Grid.php';
/**
* Widget to display Google Photos from a public album.
*/
class Simple_Google_Photos_Grid_Widget extends WP_Widget
{
/**
* Register widget with WordPress.
*/
function __construct()
{
$name = Simple_Google_Photos_Grid::name();
parent::__construct(
$name,
__( ucwords(str_replace('-', ' ', $name)), $name ),
[ 'description' => __( 'Show latest photos from a public Google Photos album', $name ), ]
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance )
{
$cache_interval = $instance['cache-interval']
? intval($instance['cache-interval'])
: Simple_Google_Photos_Grid::CACHE_INTERVAL;
$num_photos = $instance['number-photos']
? intval($instance['number-photos'])
: Simple_Google_Photos_Grid::NUMBER_PHOTOS;
$num_photos_per_row = $instance['number-photos-per-row']
? intval($instance['number-photos-per-row'])
: Simple_Google_Photos_Grid::NUMBER_PHOTOS_PER_ROW;
$grid = new Simple_Google_Photos_Grid();
$photos = $grid->get_photos($instance['album-url'], $cache_interval);
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) )
{
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
echo $grid->html($photos, $num_photos, $num_photos_per_row, $instance['album-url']);
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
* @return string|void
*/
public function form( $instance ) {
if($instance['album-url']) {
$album_url = $instance['album-url'];
$title = $instance['title'];
$cache_interval = $instance['cache-interval'];
$num_photos = $instance['number-photos'];
$num_photos_per_row = $instance['number-photos-per-row'];
}
else {
$album_url = '';
$title = '';
$cache_interval = Simple_Google_Photos_Grid::CACHE_INTERVAL;
$num_photos = Simple_Google_Photos_Grid::NUMBER_PHOTOS;
$num_photos_per_row = Simple_Google_Photos_Grid::NUMBER_PHOTOS_PER_ROW;
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></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 esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'album-url' ); ?>"><?php _e( 'Album URL:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'album-url' ); ?>" name="<?php echo $this->get_field_name( 'album-url' ); ?>" type="url" value="<?php echo esc_attr( $album_url ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'number-photos' ); ?>"><?php _e( 'Num Photos to Show:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'number-photos' ); ?>" name="<?php echo $this->get_field_name( 'number-photos' ); ?>" type="number" min="1" step="1" value="<?php echo esc_attr( $num_photos ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'number-photos-per-row' ); ?>"><?php _e( 'Num Photos Per Row:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'number-photos-per-row' ); ?>" name="<?php echo $this->get_field_name( 'number-photos-per-row' ); ?>" type="number" min="1" step="1" value="<?php echo esc_attr( $num_photos_per_row ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'cache-interval' ); ?>"><?php _e( 'Cache Interval (minutes):' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'cache-interval' ); ?>" name="<?php echo $this->get_field_name( 'cache-interval' ); ?>" type="number" min="0" step="1" value="<?php echo esc_attr( $cache_interval ); ?>">
</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 = [];
$instance['title'] = strip_tags($new_instance['title']);
$instance['cache-interval'] = intval($new_instance['cache-interval']);
$instance['number-photos'] = intval($new_instance['number-photos']);
$instance['number-photos-per-row'] = intval($new_instance['number-photos-per-row']);
$instance['album-url'] = esc_url_raw( $new_instance['album-url'], ['https'] );
return $instance;
}
}