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
|
<?php /** * Widget to insert a FooGallery */
if ( ! class_exists( 'FooGallery_Widget_Init' ) ) { class FooGallery_Widget_Init { public function __construct() { add_action('widgets_init', array($this, 'register_widget')); }
public function register_widget() { register_widget('FooGallery_Widget'); } } }
if ( ! class_exists( 'FooGallery_Widget' ) ) { class FooGallery_Widget extends WP_Widget {
/** * Sets up the widgets name etc */ public function __construct() { $widget_ops = array( 'classname' => 'foogallery_widget', 'description' => __('Insert a FooGallery', 'foogallery'), );
parent::__construct('foogallery_widget', 'FooGallery', $widget_ops); }
/** * Outputs the content of the widget * * @param array $args * @param array $instance */ public function widget($args, $instance) { // outputs the content of the widget echo $args['before_widget']; if (!empty($instance['title'])) { echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title']; } //output the gallery here $foogallery_id = isset( $instance['foogallery_id'] ) ? intval( $instance['foogallery_id'] ) : 0; if ( $foogallery_id > 0 ) { foogallery_render_gallery( $foogallery_id ); }
echo $args['after_widget']; }
/** * Outputs the options form on admin * * @param array $instance The widget options * @return string|void */ public function form($instance) { // outputs the options form on admin $title = !empty($instance['title']) ? $instance['title'] : __('New title', 'foogallery'); $foogallery_id = !empty($instance['foogallery_id']) ? intval($instance['foogallery_id']) : 0; $galleries = foogallery_get_all_galleries(); ?> <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('foogallery_id'); ?>"><?php _e('Select Gallery:', 'foogallery'); ?></label> <select class="widefat" id="<?php echo $this->get_field_id('foogallery_id'); ?>" name="<?php echo $this->get_field_name('foogallery_id'); ?>" value="<?php echo esc_attr($title); ?>"> <?php foreach ( $galleries as $gallery ) {?> <option <?php echo $gallery->ID == $foogallery_id ? 'selected="selected"' : ''; ?> value="<?php echo $gallery->ID; ?>"><?php echo $gallery->name . ' [' . $gallery->ID . ']'; ?></option> <?php } ?> </select> </p> <?php }
/** * Processing widget options on save * * @param array $new_instance The new options * @param array $old_instance The previous options * @return array|mixed */ public function update($new_instance, $old_instance) { // processes widget options to be saved foreach ($new_instance as $key => $value) { $updated_instance[$key] = sanitize_text_field($value); }
return $updated_instance; } } }
|