-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress-widget.php
More file actions
182 lines (169 loc) · 8.18 KB
/
progress-widget.php
File metadata and controls
182 lines (169 loc) · 8.18 KB
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
<?php
/**
* @package WP_BS_Progress
* @version 1.0
*/
/*
Plugin Name: Bootstrap Progress Widget
Plugin URI:
Description: Adds a Bootstrap styled progress widget.
Author: Stephanus van Vuuren
Version: 1.0
Author URI: http://glio.co.za
*/
function wp_progress_load_plugin_css() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'progress_bootstrap', $plugin_url . 'assets/css/bootstrap.min.css' );
wp_enqueue_style( 'progress_bootstrap_theme', $plugin_url . 'assets/css/bootstrap-theme.min.css' );
}
add_action( 'wp_enqueue_scripts', 'wp_progress_load_plugin_css' );
function percent($number){
return $number * 100;
}
/////////////////////
//////progress widget
/////////////////////
class ProgressWidget extends WP_Widget {
function ProgressWidget() {
$widget_ops = array('description' => 'Progress bar widget');
$this->WP_Widget('ProgressWidget', 'Progress Widget', $widget_ops);
}
function form( $instance ) {
// Output admin widget options form
$title = null;
if (! empty( $instance['title'] ) ) {
$title = apply_filters('widget_title', $instance['title'] );
};
$description = null;
if (! empty( $instance['description'] ) ) {
$description = $instance['description'];
};
$prefix = null;
if (! empty( $instance['prefix'] ) ) {
$prefix = $instance['prefix'];
};
$target = null;
if (! empty( $instance['target'] ) ) {
$target = $instance['target'];
};
$progress = null;
if (! empty( $instance['progress'] ) ) {
$progress = $instance['progress'];
};
$style = null;
if (! empty( $instance['style'] ) ) {
$style = $instance['style'];
};
$animate = null;
if (! empty( $instance['animate'] ) ) {
$animate = $instance['animate'];
};
$showpercentage = null;
if (! empty( $instance['showpercentage'] ) ) {
$showpercentage = $instance['showpercentage'];
};
$showamounts = null;
if (! empty( $instance['showamounts'] ) ) {
$showamounts = $instance['showamounts'];
};
$morelink = null;
if (! empty( $instance['morelink'] ) ) {
$morelink = $instance['morelink'];
};
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">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 if (!empty ($title)) { echo $title; } ?>" />
<label for="<?php echo $this->get_field_id('description'); ?>">Description:</label>
<input class="widefat" id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" type="text" value="<?php echo $description; ?>" />
<label for="<?php echo $this->get_field_id('morelink'); ?>">More info link: (with http://)</label>
<input class="widefat" id="<?php echo $this->get_field_id('morelink'); ?>" name="<?php echo $this->get_field_name('morelink'); ?>" type="text" value="<?php echo $morelink; ?>" />
<label for="<?php echo $this->get_field_id('prefix'); ?>">Prefix:</label>
<input class="widefat" id="<?php echo $this->get_field_id('prefix'); ?>" name="<?php echo $this->get_field_name('prefix'); ?>" type="text" value="<?php echo $prefix; ?>" />
<label for="<?php echo $this->get_field_id('target'); ?>">Target:</label>
<input class="widefat" id="<?php echo $this->get_field_id('target'); ?>" name="<?php echo $this->get_field_name('target'); ?>" type="number" value="<?php echo $target; ?>" />
<label for="<?php echo $this->get_field_id('progress'); ?>">Progress:</label>
<input class="widefat" id="<?php echo $this->get_field_id('progress'); ?>" name="<?php echo $this->get_field_name('progress'); ?>" type="number" value="<?php echo $progress; ?>" />
<label for="<?php echo $this->get_field_id('style'); ?>">Style:</label>
<select class="widefat" id="<?php echo $this->get_field_id('style'); ?>" name="<?php echo $this->get_field_name('style'); ?>" type="number" value="<?php echo $style; ?>">
<option value="progress-bar-primary">Default</option>
<option value="progress-bar-success" <?php if ($style == 'progress-bar-success') { echo 'selected' ;} ?> >Success</option>
<option value="progress-bar-info" <?php if ($style == 'progress-bar-info') { echo 'selected' ;} ?> >Info</option>
<option value="progress-bar-warning" <?php if ($style == 'progress-bar-warning') { echo 'selected' ;} ?> >Warning</option>
<option value="progress-bar-danger" <?php if ($style == 'progress-bar-danger') { echo 'selected' ;} ?> >Danger</option>
</select>
<input class="widefat" id="<?php echo $this->get_field_id('animate'); ?>" name="<?php echo $this->get_field_name('animate'); ?>" type="checkbox" value="1" <?php checked( '1', $animate ); ?> />
<label for="<?php echo $this->get_field_id('animate'); ?>"> Animate?</label></br>
<input class="widefat" id="<?php echo $this->get_field_id('showpercentage'); ?>" name="<?php echo $this->get_field_name('showpercentage'); ?>" type="checkbox" value="1" <?php checked( '1', $showpercentage ); ?> />
<label for="<?php echo $this->get_field_id('showpercentage'); ?>"> Show Percentage?</label></br>
<input class="widefat" id="<?php echo $this->get_field_id('showamounts'); ?>" name="<?php echo $this->get_field_name('showamounts'); ?>" type="checkbox" value="1" <?php checked( '1', $showamounts ); ?> />
<label for="<?php echo $this->get_field_id('showamounts'); ?>"> Show Amounts?</label>
</p>
<?php
} //ending form creation
function update( $new_instance, $old_instance ) {
// Save widget options
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['description'] = strip_tags($new_instance['description']);
$instance['prefix'] = strip_tags($new_instance['prefix']);
$instance['target'] = strip_tags($new_instance['target']);
$instance['progress'] = strip_tags($new_instance['progress']);
$instance['style'] = strip_tags($new_instance['style']);
$instance['animate'] = strip_tags($new_instance['animate']);
$instance['showpercentage'] = strip_tags($new_instance['showpercentage']);
$instance['showamounts'] = strip_tags($new_instance['showamounts']);
$instance['morelink'] = strip_tags($new_instance['morelink']);
return $instance;
} //ending update
function widget( $args, $instance ){
// Widget output
extract($args, EXTR_SKIP);
$title = $instance['title'];
$description = $instance['description'];
$prefix = $instance['prefix'];
$target = $instance['target'];
$progress = $instance['progress'];
$style = $instance['style'];
$animate = $instance['animate'];
$showpercentage = $instance['showpercentage'];
$showamounts = $instance['showamounts'];
$morelink = $instance['morelink'];
echo $before_widget;
echo $before_title;
echo $title;
echo $after_title;
$diff = ($progress / $target);
$percentage = percent($diff);
?>
<p> <?php echo $description; ?></p>
<div class="progress progress-striped <?php if ($animate == 1) { echo 'active'; } ?>">
<div class="progress-bar <?php echo $style; ?>" role="progressbar" aria-valuenow="<?php echo $progress; ?>" aria-valuemin="0" aria-valuemax="<?php echo $target; ?>" style="width: <?php echo round($percentage) . '%'; ?>">
<?php
if ($showpercentage == 1) {
echo round($percentage) . '%';
}
?>
</div>
<?php if ($showamounts == 1) { ?>
<span class="pg-bar-progress"><i class="fa fa-angle-double-left"></i> <?php if (!empty($prefix)) { echo $prefix; } echo number_format($progress); ?></span>
<?php } ?>
</div>
<?php if ($showamounts == 1) { ?>
<div class="pg-bar-target"><?php if (!empty($prefix)) { echo $prefix; } echo number_format($target);?></div>
<?php } ?>
<?php if (!empty($morelink)) { ?>
<div class="text-right">
<a href="<?php echo $morelink; ?>">More info <i class="fa fa-info-circle"></i></a>
</div>
<?php } ?>
<?php
echo $after_widget;
} //ending widget display
}
//////////////////////////////////
function register_rghs_custom_widgets() {
register_widget( 'ProgressWidget' );
}
add_action( 'widgets_init', 'register_rghs_custom_widgets' );
?>