forked from seothemes/icon-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon-widget.php
378 lines (291 loc) · 10 KB
/
icon-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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* Icon Widget
*
* Icon Widget creates a new WordPress widget that displays an icon,
* title and description. Select the size, color and text-alignment
* with easy to use dropdown options.
*
* @package Icon_Widget
* @author SEO Themes <[email protected]>
* @license GPL-2.0+
* @link https://seothemes.com
* @copyright 2018 SEO Themes
*
* Plugin Name: Icon Widget
* Plugin URI: https://seothemes.com
* Description: Displays an icon widget with a title and description
* Version: 1.1.0
* Author: SEO Themes
* Author URI: https://seothemes.com
* Text Domain: icon-widget
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
* GitHub Plugin URI: https://github.com/<owner>/<repo>
*/
// Prevent direct file access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Widget class.
*/
class Icon_Widget extends WP_Widget {
/**
* Unique identifier for the widget.
*
* The variable name is used as the text domain when internationalizing strings
* of text. Its value should match the Text Domain file header in the main
* widget file.
*
* @since 1.0.0
*
* @var string
*/
protected $widget_slug = 'icon-widget';
/**
* List of available icon fonts.
*
* @var array
*/
public static $fonts = array();
/**
* Constructor
*
* Specifies the classname and description, instantiates the widget,
* loads localization files, and includes necessary stylesheets and JavaScript.
*/
public function __construct() {
// Define available icon fonts.
self::$fonts = array(
'font-awesome',
'line-awesome',
'ionicons',
'streamline',
'et-line',
);
// Load plugin text domain.
add_action( 'init', array( $this, 'widget_textdomain' ) );
parent::__construct(
$this->get_widget_slug(),
__( 'Icon', 'icon-widget' ),
array(
'classname' => 'icon_widget',
'description' => __( 'Displays an icon with a title and description.', 'icon-widget' ),
)
);
// Add settings link.
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array(
$this,
'action_links'
) );
// Register admin styles and scripts.
add_action( 'admin_print_styles', array( $this, 'register_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) );
// Register site styles and scripts.
add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_styles' ) );
}
/**
* Return the widget slug.
*
* @since 1.0.0
*
* @return Plugin slug variable.
*/
public function get_widget_slug() {
return $this->widget_slug;
}
/**
* Add settings link.
*
* @param array $links Plugin links.
*
* @return array
*/
public function action_links( $links ) {
$settings_link = array(
'<a href="' . admin_url( 'options-general.php?page=icon_widget' ) . '">Settings</a>',
);
return array_merge( $links, $settings_link );
}
/*
|--------------------------------------------------------------------------
| Widget API Functions
|--------------------------------------------------------------------------
*/
/**
* Outputs the content of the widget.
*
* @param array $args The array of form elements.
* @param array $instance The current instance of the widget.
*/
public function widget( $args, $instance ) {
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
echo $args['before_widget'];
printf( '<a href="%s">', esc_attr( $instance['url'] ));
printf( '<div class="icon-widget" style="text-align: %s">', esc_attr( $instance['align'] ) );
printf( '<i class="fa %1$s fa-%2$s" style="color:%3$s;background-color:%4$s;padding:%5$spx;border-radius:%6$spx;"></i>', esc_attr( $instance['icon'] ), esc_attr( $instance['size'] ), esc_attr( $instance['color'] ), esc_attr( $instance['bg'] ), esc_attr( $instance['padding'] ), esc_attr( $instance['radius'] ) );
echo apply_filters( 'icon_widget_line_break', true ) ? '<br>' : '';
echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'];
echo apply_filters( 'icon_widget_wpautop', true ) ? wp_kses_post( wpautop( $instance['content'] ) ) : wp_kses_post( $instance['content'] );
echo '</div></a>';
echo $args['after_widget'];
}
/**
* Process the widget's options to be saved.
*
* @param array $new_instance The new instance of values to be generated via the
* update.
* @param array $old_instance The previous instance of values before the update.
*
* @return array
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
// Update widget's old values with new incoming values.
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['content'] = wp_kses_post( $new_instance['content'] );
$instance['url'] = strip_tags( $new_instance['url'] );
$instance['icon'] = sanitize_html_class( $new_instance['icon'] );
$instance['size'] = sanitize_html_class( $new_instance['size'] );
$instance['align'] = sanitize_html_class( $new_instance['align'] );
$instance['color'] = sanitize_hex_color( $new_instance['color'] );
$instance['bg'] = sanitize_hex_color( $new_instance['bg'] );
$instance['padding'] = intval( $new_instance['padding'] );
$instance['radius'] = intval( $new_instance['radius'] );
return $instance;
}
/**
* Generates the administration form for the widget.
*
* @param array $instance The array of keys and values for the widget.
*/
public function form( $instance ) {
$defaults = apply_filters( 'icon_widget_defaults', array(
'title' => '',
'content' => '',
'url' => '#',
// Keep sub filters for backwards compat.
'icon' => apply_filters( 'icon_widget_default_icon', '\f000' ),
'size' => apply_filters( 'icon_widget_default_size', '2x' ),
'align' => apply_filters( 'icon_widget_default_align', 'left' ),
'color' => apply_filters( 'icon_widget_default_color', '#333333' ),
'bg' => '',
'padding' => '',
'radius' => '',
) );
// Define default values for your variables.
$instance = wp_parse_args( (array) $instance, $defaults );
// Store the values of the widget in their own variable.
$title = $instance['title'];
$content = $instance['content'];
$url = $instance['url'];
$icon = $instance['icon'];
$size = $instance['size'];
$align = $instance['align'];
$color = $instance['color'];
$bg = $instance['bg'];
$padding = $instance['padding'];
$radius = $instance['radius'];
// Display the admin form.
include( plugin_dir_path( __FILE__ ) . 'views/admin.php' );
}
/*
|--------------------------------------------------------------------------
| Public Functions
|--------------------------------------------------------------------------
*/
/**
* Loads the Widget's text domain for localization and translation.
*/
public function widget_textdomain() {
load_plugin_textdomain( $this->get_widget_slug(), false, plugin_dir_path( __FILE__ ) . 'languages/' );
}
/**
* Fired when the plugin is activated.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate"
* action, false if WPMU is disabled or plugin is
* activated on an individual blog.
*/
public static function activate( $network_wide ) {
// Add default icon font.
if ( ! get_option( 'icon_widget_settings' ) ) {
$defaults = apply_filters( 'icon_widget_defaults', array(
'font' => apply_filters( 'icon_widget_default_font', 'font-awesome' ),
) );
add_option( 'icon_widget_settings', $defaults );
}
}
/**
* Fired when the plugin is deactivated.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network
* Activate"action, false if WPMU is disabled or plugin
* is activated on an individual blog.
*/
public static function deactivate( $network_wide ) {
// Clean up.
delete_option( 'icon_widget_settings' );
}
/**
* Registers and enqueues admin-specific styles.
*/
public function register_admin_styles() {
if ( ! is_customize_preview() && get_current_screen()->id !== 'widgets' ) {
return;
}
wp_enqueue_style( 'bootstrap', plugins_url( 'assets/css/bootstrap.min.css', __FILE__ ), array( 'wp-color-picker' ) );
wp_enqueue_style( 'bootstrap-select', plugins_url( 'assets/css/bootstrap-select.min.css', __FILE__ ), array( 'bootstrap' ) );
// Load icon font CSS.
$this->register_widget_styles();
}
/**
* Registers and enqueues admin-specific JavaScript.
*/
public function register_admin_scripts() {
if ( ! is_customize_preview() && get_current_screen()->id !== 'widgets' ) {
return;
}
wp_enqueue_script( 'bootstrap', plugins_url( 'assets/js/bootstrap.min.js', __FILE__ ), array(
'jquery',
'wp-color-picker'
) );
wp_enqueue_script( 'bootstrap-select', plugins_url( 'assets/js/bootstrap-select.min.js', __FILE__ ), array( 'bootstrap' ) );
}
/**
* Registers and enqueues widget-specific styles.
*/
public function register_widget_styles() {
$settings = get_option( 'icon_widget_settings' );
$current_font = $settings['font'];
foreach ( self::$fonts as $font ) {
if ( $font === $current_font ) {
wp_enqueue_style( $font, plugins_url( 'assets/css/' . $font . '.min.css', __FILE__ ) );
}
}
}
}
add_action( 'widgets_init', 'icon_widget_register_widget' );
/**
* Register widget
*
* Registers the Icon Widget widget with WordPress.
*
* @since 1.0.8
*
* @return void
*/
function icon_widget_register_widget() {
register_widget( 'Icon_Widget' );
}
// Register settings.
include( plugin_dir_path( __FILE__ ) . 'includes/settings.php' );
// Add shortcode.
include( plugin_dir_path( __FILE__ ) . 'includes/shortcode.php' );
// Hooks fired when the Widget is activated and deactivated.
register_activation_hook( __FILE__, array( 'Icon_Widget', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'Icon_Widget', 'deactivate' ) );