|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Widget to insert a FooGallery |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
if ( ! class_exists( 'FooGallery_Widget_Init' ) ) { |
|
7
|
|
|
class FooGallery_Widget_Init |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
public function __construct() |
|
10
|
|
|
{ |
|
11
|
|
|
add_action('widgets_init', array($this, 'register_widget')); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function register_widget() |
|
15
|
|
|
{ |
|
16
|
|
|
register_widget('FooGallery_Widget'); |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
if ( ! class_exists( 'FooGallery_Widget' ) ) { |
|
22
|
|
|
class FooGallery_Widget extends WP_Widget |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Sets up the widgets name etc |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
$widget_ops = array( |
|
31
|
|
|
'classname' => 'foogallery_widget', |
|
32
|
|
|
'description' => __('Insert a FooGallery', 'foogallery'), |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
parent::__construct('foogallery_widget', 'FooGallery', $widget_ops); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Outputs the content of the widget |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $args |
|
43
|
|
|
* @param array $instance |
|
44
|
|
|
*/ |
|
45
|
|
|
public function widget($args, $instance) |
|
46
|
|
|
{ |
|
47
|
|
|
// outputs the content of the widget |
|
48
|
|
|
echo $args['before_widget']; |
|
49
|
|
|
if (!empty($instance['title'])) { |
|
50
|
|
|
echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title']; |
|
51
|
|
|
} |
|
52
|
|
|
//output the gallery here |
|
53
|
|
|
$foogallery_id = isset( $instance['foogallery_id'] ) ? intval( $instance['foogallery_id'] ) : 0; |
|
54
|
|
|
if ( $foogallery_id > 0 ) { |
|
55
|
|
|
foogallery_render_gallery( $foogallery_id ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
echo $args['after_widget']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Outputs the options form on admin |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $instance The widget options |
|
66
|
|
|
* @return string|void |
|
67
|
|
|
*/ |
|
68
|
|
|
public function form($instance) |
|
69
|
|
|
{ |
|
70
|
|
|
// outputs the options form on admin |
|
71
|
|
|
$title = !empty($instance['title']) ? $instance['title'] : __('New title', 'foogallery'); |
|
72
|
|
|
$foogallery_id = !empty($instance['foogallery_id']) ? intval($instance['foogallery_id']) : 0; |
|
73
|
|
|
$galleries = foogallery_get_all_galleries(); |
|
74
|
|
|
?> |
|
75
|
|
|
<p> |
|
76
|
|
|
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> |
|
77
|
|
|
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" |
|
78
|
|
|
name="<?php echo $this->get_field_name('title'); ?>" type="text" |
|
79
|
|
|
value="<?php echo esc_attr($title); ?>"> |
|
80
|
|
|
</p> |
|
81
|
|
|
<p> |
|
82
|
|
|
<label for="<?php echo $this->get_field_id('foogallery_id'); ?>"><?php _e('Select Gallery:', 'foogallery'); ?></label> |
|
83
|
|
|
<select class="widefat" id="<?php echo $this->get_field_id('foogallery_id'); ?>" |
|
84
|
|
|
name="<?php echo $this->get_field_name('foogallery_id'); ?>" |
|
85
|
|
|
value="<?php echo esc_attr($title); ?>"> |
|
86
|
|
|
<?php foreach ( $galleries as $gallery ) {?> |
|
87
|
|
|
<option <?php echo $gallery->ID == $foogallery_id ? 'selected="selected"' : ''; ?> value="<?php echo $gallery->ID; ?>"><?php echo $gallery->name . ' [' . $gallery->ID . ']'; ?></option> |
|
88
|
|
|
<?php } ?> |
|
89
|
|
|
</select> |
|
90
|
|
|
</p> |
|
91
|
|
|
<?php |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Processing widget options on save |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $new_instance The new options |
|
99
|
|
|
* @param array $old_instance The previous options |
|
100
|
|
|
* @return array|mixed |
|
101
|
|
|
*/ |
|
102
|
|
|
public function update($new_instance, $old_instance) |
|
103
|
|
|
{ |
|
104
|
|
|
// processes widget options to be saved |
|
105
|
|
|
foreach ($new_instance as $key => $value) { |
|
106
|
|
|
$updated_instance[$key] = sanitize_text_field($value); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $updated_instance; |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.