-
Notifications
You must be signed in to change notification settings - Fork 4
/
us-debt-clock-widget.php
340 lines (269 loc) · 11.7 KB
/
us-debt-clock-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
<?php
/*
Plugin Name: U.S. Debt Clock Widget
Plugin URI: https://github.com/ChrisHardie/us-debt-clock-widget
Description: Display the U.S. national debt in a widget
Author: Chris Hardie
Version: 1.6
Author URI: https://chrishardie.com/
License: GPL2
*/
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
defined( 'ABSPATH' ) || die( "Please don't try to run this file directly." );
class Debtclock_Widget extends WP_Widget {
/**
* Register the widget with WordPress
*/
public function __construct() {
parent::__construct(
'us_debtclock_widget', // base id
esc_html__( 'U.S. Debt Clock Widget', 'us_debtclock_widget_domain' ), // name
array(
'description' => __( 'A widget to display the U.S. national debt.', 'us_debtclock_widget_domain' ),
)
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
$debt_info = JCH_Debtclock::get_debt();
$debt_delta = '';
// If we can't get a value for the current debt info, at least display something.
if ( ! $debt_info ) {
$debt_amount = 'UNAVAILABLE';
$debt_amount_formatted = 'UNAVAILABLE';
} elseif ( is_numeric( $debt_info['current_debt'] ) ) {
$debt_amount = $debt_info['current_debt'];
// For maximum effect let's display the big number, with commas, no cents.
$debt_amount_formatted = number_format( $debt_amount );
if ( $instance['animate_p'] ) {
// Calculate how much the debt increased per second on average between the two timestamps
$time_delta = ( $debt_info['current_date'] - $debt_info['previous_date'] );
$debt_delta = ( ( $debt_info['current_debt'] - $debt_info['previous_debt'] ) / $time_delta );
wp_enqueue_script( 'jquery' );
}
} else {
$debt_amount = 'INVALID';
$debt_amount_formatted = 'INVALID';
}
// Output the widget content
echo wp_kses_post( $args['before_widget'] );
// Title
if ( ! empty( $instance['title'] ) ) {
echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'] );
}
// User-defined introduction
if ( ! empty( $instance['introduction'] ) ) {
echo '<div class="us_debtclock_widget_introduction">';
echo esc_html( $instance['introduction'] );
echo '</div>';
}
// Actual debt amount, formatted (maybe replaced by JS actions below)
echo '<div id="debtclock_amount" class="us_debtclock_widget_amount">$'
. esc_html( $debt_amount_formatted ) . '</div>';
// If they want moving numbers and we're starting with a real number...
if ( ! empty( $debt_amount )
&& ! empty( $debt_delta )
&& is_numeric( $debt_amount )
&& is_numeric( $debt_delta )
&& $instance['animate_p'] ) {
// Increment the amount by the per-second delta we calculated earlier
echo "<script type='text/javascript'>";
echo '
var INTERVAL = 1; // refresh interval in seconds
var INCREMENT = ' . esc_js( $debt_delta ) . '; // increase per tick
var START_VALUE = ' . esc_js( $debt_amount ) . "; // initial value when it's the start date
var count = 0;
jQuery(document).ready(function() {
var msInterval = INTERVAL * 1000;
var now = new Date();
count = START_VALUE;
window.setInterval( function(){
count += INCREMENT;
count_formatted = count.toFixed(0).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, \"$1,\");
jQuery('#debtclock_amount').html(\"$\" + count_formatted);
}, msInterval);
});
";
echo '</script>';
}
if ( ! is_numeric( $debt_amount ) ) {
echo '<div id="debtclock_error" class="us_debtclock_widget_error">There was a
problem fetching the data, please try again later.</div>';
}
// Give credit where it's due?
if ( $instance['show_credit_p'] ) {
echo '<p class="us_debtclock_widget_credit">
<a class="us_debtclock_widget_credit_link" target="_blank" href=" ' . esc_html( $debt_info['url'] ) . '">Source</a>
</p>';
}
echo wp_kses_post( $args['after_widget'] );
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ! empty( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : '';
$instance['introduction'] = ! empty( $new_instance['introduction'] ) ? wp_strip_all_tags( $new_instance['introduction'] ) : '';
$instance['animate_p'] = ! empty( $new_instance['animate_p'] ) ? 1 : 0;
$instance['show_credit_p'] = ! empty( $new_instance['show_credit_p'] ) ? 1 : 0;
return $instance;
}
/**
* Back-end form to manage a widget's options in wp-admin
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
* @return string
*/
public function form( $instance ) {
if ( isset( $instance['title'] ) ) {
$title = $instance['title'];
} else {
$title = esc_html__( 'U.S. National Debt', 'us_debtclock_widget_domain' );
}
if ( isset( $instance['introduction'] ) ) {
$introduction = $instance['introduction'];
} else {
$introduction = esc_html__( 'The current U.S. national debt:', 'us_debtclock_widget_domain' );
}
// Will the value of the debt be animated to show change over time?
$animate_p = isset( $instance['animate_p'] ) ? (bool) $instance['animate_p'] : false;
// Should we show the source of the debt data?
$show_credit_p = isset( $instance['show_credit_p'] ) ? (bool) $instance['show_credit_p'] : true;
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
type="text" value="<?php echo esc_html( esc_attr( $title ) ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'introduction' ) ); ?>"><?php esc_html_e( 'Introduction Text:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'introduction' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'introduction' ) ); ?>"
type="text" value="<?php echo esc_attr( $introduction ); ?>">
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked( $animate_p ); ?> id="<?php echo esc_attr( $this->get_field_id( 'animate_p' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'animate_p' ) ); ?>" />
<label for="<?php echo esc_attr( $this->get_field_id( 'animate_p' ) ); ?>"><?php esc_html_e( 'Animate dollar amount with estimated change over time?' ); ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked( $show_credit_p ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_credit_p' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'show_credit_p' ) ); ?>" />
<label for="<?php echo esc_attr( $this->get_field_id( 'show_credit_p' ) ); ?>"><?php esc_html_e( 'Include credit link to data source?' ); ?></label>
</p>
<?php
return true;
}
}
class JCH_Debtclock {
public function __construct() {
add_action( 'init', array( $this, 'init' ), 1 );
register_activation_hook( __FILE__, array( $this, 'us_debtclock_widget_activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'us_debtclock_widget_deactivation' ) );
}
public function init() {
// If the widget is active, enqueue needed CSS
if ( is_active_widget( false, false, 'us_debtclock_widget' ) ) {
add_action( 'wp_head', array( &$this, 'add_styles_and_scripts' ) );
}
}
/**
* On plugin activation, schedule a twice-daily update of the debt data from the source.
*/
public function us_debtclock_widget_activation() {
if ( ! wp_next_scheduled( 'us_debtclock_widget_event_hook' ) ) {
wp_schedule_event( time(), 'twicedaily', 'us_debtclock_widget_event_hook' );
}
add_action( 'us_debtclock_widget_event_hook', 'get_debt' );
}
/**
* On plugin deactivation, remove all functions from the scheduled action hook.
*/
public function us_debtclock_widget_deactivation() {
wp_clear_scheduled_hook( 'us_debtclock_widget_event_hook' );
delete_transient( 'us_debtclock_widget_info' );
}
public function add_styles_and_scripts() {
wp_enqueue_style( 'debtclock_widget_style', plugins_url( 'style.css', __FILE__ ) );
}
/**
* Actually fetch the debt info from the remote source
*/
public static function get_debt() {
global $us_debtclock_widget_info; // Check if it's in the runtime cache
if ( empty( $us_debtclock_widget_info ) ) {
$us_debtclock_widget_info = get_transient( 'us_debtclock_widget_info' ); // Check database
}
if ( ! empty( $us_debtclock_widget_info['current_debt'] ) ) {
return $us_debtclock_widget_info;
}
delete_transient( 'us_debtclock_widget_info' );
$debt_feed_url = 'https://treasurydirect.gov/NP_WS/debt/feeds/recent';
$us_debtclock_widget_info['url'] = 'https://fiscaldata.treasury.gov/datasets/debt-to-the-penny/';
$debt_feed_contents = fetch_feed( $debt_feed_url );
if ( is_wp_error( $debt_feed_contents ) ) {
return false;
}
// Get the most recent number and the one right before, for possible animation
$max_items = $debt_feed_contents->get_item_quantity( 2 );
list( $recent_debt, $previous_debt ) = $debt_feed_contents->get_items( 0, $max_items );
if ( ! ( is_object( $recent_debt ) && is_object( $previous_debt ) ) ) {
return false;
}
// Format timestamps as seconds since epoch for easier delta calculation in animation
$us_debtclock_widget_info['current_date'] = $recent_debt->get_date( 'U' );
$us_debtclock_widget_info['previous_date'] = $previous_debt->get_date( 'U' );
// <em>Debt Held by the Public:</em> 14,822,172,493,990.24<br /><em>Intragovernmental Holdings:</em> 5,652,677,544,564.30<br /><em>Total Public Debt Outstanding:</em> 20,474,850,038,554.541
$us_debtclock_widget_info['current_debt'] = (int) str_replace( ',', '', preg_replace( '/^.*Total Public Debt Outstanding:<\/em> (\S+)$/', '\1', $recent_debt->get_content() ) );
$us_debtclock_widget_info['previous_debt'] = (int) str_replace( ',', '', preg_replace( '/^.*Total Public Debt Outstanding:<\/em> (\S+)$/', '\1', $previous_debt->get_content() ) );
// If it doesn't have the field data requested, something went wrong
if ( ! $us_debtclock_widget_info['current_debt'] ) {
return false;
}
// If it's a real number, put it in a transient for later use
if ( is_numeric( $us_debtclock_widget_info['current_debt'] ) ) {
set_transient( 'us_debtclock_widget_info', $us_debtclock_widget_info, 12 * HOUR_IN_SECONDS ); // Store in database for up to 12 hours
} else {
return false;
}
return $us_debtclock_widget_info;
}
}
$jch_debtclock_widget = new JCH_Debtclock();
add_action(
'widgets_init',
function() {
register_widget( 'Debtclock_Widget' );
}
);
?>