-
Notifications
You must be signed in to change notification settings - Fork 0
/
catlinks-widget.php
342 lines (281 loc) · 11.1 KB
/
catlinks-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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/*
* Shortcode definition
* @link
* @since 1.0
*
* @package catlinks-widget
* @subpackage catlinks-widget
*/
/*
The class realizes a functionality of the widget
*/
class Catlinks_Widget extends WP_Widget {
/*
Creates a widget
*/
public function __construct() {
parent::__construct(
'an_catlinks_widget', // ID of the widget
esc_html__('Categories Links Widget', 'an_catlinks_wiget'), // Widget title
array(
'description' => esc_html__('The widget allow to insert a block of the iconic links for 3 categories', 'an_catlinks_wiget'),
)
);
// enqueue scripts for media library
add_action( 'admin_enqueue_scripts', array( $this, 'allow_media_upload' ) );
// enqueue enqueue styles
add_action('wp_enqueue_scripts', array($this, 'enqueue_styles'));
// customize styles
if (!is_admin()) {
add_action('wp_head',array($this, 'customize_css'));
}
}
/*
include media library scripts & color picker
*/
public function allow_media_upload() {
wp_enqueue_script( 'media-upload' );
wp_enqueue_media();
wp_enqueue_style( 'wp-color-picker' ); // color picker
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_script('catlinks-media-upload', plugin_dir_url(__FILE__) . 'js/catlinks.js', array( 'jquery' )) ;
}
// include plugin's styles
public function enqueue_styles() {
wp_enqueue_style('catlinks-widget-style',
plugin_dir_url(__FILE__)."/css/catlinks.css",
null, null, 'all'
);
}
// css customization
function customize_css()
{
// get current widget settings
$instances = $this->get_settings();
// check settings exist
if (array_key_exists($this->number, $instances)) {
// then get instance
$instance = $instances[$this->number];
?>
<style type="text/css">
<?php
if (isset($instance['bg_color'])):
?>
.an-catlinks-container { background-color: <?php _e($instance['bg_color']); ?>; }
<?php
endif;
?>
<?php
if (isset($instance['font_color'])):
?>
.an-catlinks-element a,
.an-catlinks-element a:link,
.an-catlinks-element a:visited,
.an-catlinks-element a:active,
.an-catlinks-element a:hover{ color: <?php _e($instance['font_color']); ?>; }
<?php
endif;
?>
</style>
<?php
} // end of get instance if
} // end of customize css function
/*
The front-end part of the widget
*/
public function widget($args, $instance) {
extract( $args );
?>
<div class="an-catlinks-container">
<div class="an-catlinks-element">
<img src="<?php _e($instance['image1']); ?>" />
<a href="<?php _e(get_category_link($instance['cat1'])); ?>">
<?php _e(get_cat_name($instance['cat1'])); ?>
</a>
</div>
<div class="an-catlinks-element">
<img src="<?php _e($instance['image2']); ?>" />
<a href="<?php _e(get_category_link($instance['cat2'])); ?>">
<?php _e(get_cat_name($instance['cat2'])); ?>
</a>
</div>
<div class="an-catlinks-element">
<img src="<?php _e($instance['image3']); ?>" />
<a href="<?php _e(get_category_link($instance['cat3'])); ?>">
<?php _e(get_cat_name($instance['cat3'])); ?>
</a>
</div>
</div>
<?php
}
/*
The back-end part of the widget
*/
public function form($instance) {
// set up background color
$bg_color = "#212121";
if (isset($instance['bg_color'])) {
$bg_color = $instance['bg_color'];
}
?>
<div class="an-catlinks-settings-container">
<p>
<label for="<?php echo $this->get_field_id( 'bg_color' ); ?>" style="display:block;"><?php _e( 'Background Color:', 'an_catlinks_wiget' ); ?></label>
<input class="widefat color-picker an-catlist-bg-color-picker"
id="<?php echo $this->get_field_id( 'bg_color' ); ?>"
name="<?php echo $this->get_field_name( 'bg_color' ); ?>" type="text" value="<?php echo esc_attr( $bg_color ); ?>" />
</p>
<?php
// setup font color
$font_color = "#ffffff";
if (isset($instance['font_color'])) {
$font_color = $instance['font_color'];
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'font_color' ); ?>" style="display:block;"><?php _e( 'Font color:', 'an_catlinks_wiget' ); ?></label>
<input class="widefat color-picker"
id="<?php echo $this->get_field_id( 'font_color' ); ?>"
name="<?php echo $this->get_field_name( 'font_color' ); ?>" type="text" value="<?php echo esc_attr( $font_color ); ?>" />
</p>
<?php
// set up 1st category
$cat = 0;
if (isset($instance['cat1'])) {
$cat = $instance['cat1'];
}
?>
<!-- 1st category and image -->
<label for="<?php _e($this->get_field_id('cat1')); ?>"><?php esc_html__("Category 1", "an_catlinks_wiget"); ?></label>
<select id="<?php _e($this->get_field_id('cat1')); ?>" name="<?php _e($this->get_field_name('cat1')); ?>" class="widefat" width="100%">
<option value-"0"><?php _e('(not selected)', 'an_catlinks_wiget'); ?></option>
<?php foreach(get_terms('category','parent=0&hide_empty=0') as $category): ?>
<option <?php $cat == $category->term_id ? _e("selected") : _e(""); ?> value="<?php _e($category->term_id); ?>">
<?php _e($category->name); ?>
</option>
<?php endforeach; ?>
</select>
<?php
// set up image for the first category
$image = '';
if(isset($instance['image1']))
{
$image = $instance['image1'];
}
?>
<p class="an-catlinks-image-demo">
<label for="<?php _e($this->get_field_name( 'image1' )); ?>"><?php _e( 'Image 1:', 'an_catlinks_wiget' ); ?></label>
<input name="<?php _e($this->get_field_name( 'image1' )); ?>"
id="<?php _e($this->get_field_id( 'image1' )); ?>"
class="widefat" type="text" size="36"
value="<?php _e(esc_url( $image )); ?>" />
<img src="<?php _e(esc_url( $image )); ?>" style="max-width: 100%; height: auto; background: <?php _e($bg_color); ?>" />
<input class="upload_image_button" type="button" value="<?php _e('Upload Image','an_catlinks_wiget'); ?>" />
</p>
<!-- 2nd category and image -->
<?php
$cat = 0;
if (isset($instance['cat2'])) {
$cat = $instance['cat2'];
}
?>
<label for="<?php _e($this->get_field_id('cat2')); ?>"><?php esc_html__("Category 2", "an_catlinks_wiget"); ?></label>
<select id="<?php _e($this->get_field_id('cat2')); ?>" name="<?php _e($this->get_field_name('cat2')); ?>" class="widefat" width="100%">
<option value-"0"><?php _e('(not selected)', 'an_catlinks_wiget'); ?></option>
<?php foreach(get_terms('category','parent=0&hide_empty=0') as $category): ?>
<option <?php $cat == $category->term_id ? _e("selected") : _e(""); ?> value="<?php _e($category->term_id); ?>">
<?php _e($category->name); ?>
</option>
<?php endforeach; ?>
</select>
<?php
$image = '';
if(isset($instance['image2']))
{
$image = $instance['image2'];
}
?>
<p class="an-catlinks-image-demo">
<label for="<?php _e($this->get_field_name( 'image2' )); ?>"><?php _e( 'Image 2:', 'an_catlinks_wiget' ); ?></label>
<input name="<?php _e($this->get_field_name( 'image2' )); ?>"
id="<?php _e($this->get_field_id( 'image2' )); ?>"
class="widefat" type="text" size="36"
value="<?php _e(esc_url( $image )); ?>" />
<img src="<?php _e(esc_url( $image )); ?>" style="max-width: 100%; height: auto;background: <?php _e($bg_color); ?>" />
<input class="upload_image_button" type="button" value="<?php _e('Upload Image','an_catlinks_wiget'); ?>" />
</p>
<!-- 3rd category and image -->
<?php
$cat = 0;
if (isset($instance['cat3'])) {
$cat = $instance['cat3'];
}
?>
<label for="<?php _e($this->get_field_id('cat3')); ?>"><?php esc_html__("Category 3", "an_catlinks_wiget"); ?></label>
<select id="<?php _e($this->get_field_id('cat3')); ?>" name="<?php _e($this->get_field_name('cat3')); ?>" class="widefat" width="100%">
<option value-"0"><?php _e('(not selected)', 'an_catlinks_wiget'); ?></option>
<?php foreach(get_terms('category','parent=0&hide_empty=0') as $category): ?>
<option <?php $cat == $category->term_id ? _e("selected") : _e(""); ?> value="<?php _e($category->term_id); ?>">
<?php _e($category->name); ?>
</option>
<?php endforeach; ?>
</select>
<?php
$image = '';
if(isset($instance['image3']))
{
$image = $instance['image3'];
}
?>
<p class="an-catlinks-image-demo">
<label for="<?php _e($this->get_field_name( 'image3' )); ?>"><?php _e( 'Image 3:', 'an_catlinks_wiget' ); ?></label>
<input name="<?php _e($this->get_field_name( 'image3' )); ?>"
id="<?php _e($this->get_field_id( 'image3' )); ?>"
class="widefat" type="text" size="36"
value="<?php _e(esc_url( $image )); ?>" />
<img src="<?php _e(esc_url( $image )); ?>" style="max-width: 100%; height: auto;background: <?php _e($bg_color); ?>" />
<input class="upload_image_button" type="button" value="<?php _e('Upload Image','an_catlinks_wiget'); ?>" />
</p>
</div>
<script>
(function($) {
$(document).ready(function() {
$('.color-picker').wpColorPicker({
change: function(event, ui) {
$(this).parent().trigger('change');
if ($(this).hasClass('an-catlist-bg-color-picker')) {
$('.an-catlinks-image-demo img').css('background',$(this).val());
}
},
clear: function(event) {
$(this).parent().trigger('change');
if ($(this).hasClass('an-catlist-bg-color-picker')) {
$('.an-catlinks-image-demo img').css('background','transparent');
}
}
});
});
}
)(jQuery);
</script>
<?php
} // end of form function
/*
Update the instance of the widget
Here we define custom fields!
*/
public function update($new_instance, $old_instance) {
$instance = array();
$instance['cat1'] = ! empty( $new_instance['cat1'] ) ? intval($new_instance['cat1']) : 0;
$instance['image1'] = ! empty ($new_instance['image1']) ? $new_instance['image1'] : '';
$instance['cat2'] = ! empty( $new_instance['cat2'] ) ? intval($new_instance['cat2']) : 0;
$instance['image2'] = ! empty ($new_instance['image2']) ? $new_instance['image2'] : '';
$instance['cat3'] = ! empty( $new_instance['cat3'] ) ? intval($new_instance['cat3']) : 0;
$instance['image3'] = ! empty ($new_instance['image3']) ? $new_instance['image3'] : '';
$instance['bg_color'] = ! empty( $new_instance['bg_color'] ) ? $new_instance['bg_color'] : "#212121";
$instance['font_color'] = ! empty( $new_instance['font_color'] ) ? $new_instance['font_color'] : "#ffffff";
return $instance;
}
}
?>