-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrivendell-now-and-next.php
More file actions
338 lines (273 loc) · 10.6 KB
/
Copy pathrivendell-now-and-next.php
File metadata and controls
338 lines (273 loc) · 10.6 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
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
<?php
/**
* Plugin Name: Rivendell Now&Next collector and browser
* Plugin URI: https://github.com/RadioCampusFrance/rivendell-now-and-next-for-wordpress
* Description: Collects "Now & Next" signals from RDAirPlay (Rivendell's automation software), stores the playlist and lets the user browse the past playlist.
* Version: 1.0
* Author: Martin Kirchgessner
* Author URI: https://github.com/martinkirch
* License: GPLv2
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class RivendellNowAndNext {
const SCHEMA_VERSION = "1.0";
const OPTION_DB_VERSION = "rivendell_now_and_next_db_version";
const OPTION_KEY = "rivendell_now_and_next_key";
const OPTION_KEEP_N_DAYS = "rivendell_now_and_next_keep_n_days";
static function table_name () {
global $wpdb;
return $wpdb->prefix . "rivendell_playlist";
}
private static $instance;
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof RivendellNowAndNext ) ) {
self::$instance = new RivendellNowAndNext();
}
return self::$instance;
}
public function __construct () {
register_activation_hook( __FILE__, array ( $this, 'install' ) );
add_action( 'init', array ( $this, 'wp_init') );
add_action( 'admin_post_nopriv_rivendell_now_and_next_store', array ( $this, 'store') );
add_action( 'admin_post_rivendell_now_and_next_store', array ( $this, 'store') );
add_filter( 'page_template', array ( $this, 'playlist_page') , 99 );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
function install () {
$installed_version = get_option( self::OPTION_DB_VERSION );
if ( $installed_version != self::SCHEMA_VERSION ) {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = self::table_name();
$sql = "CREATE TABLE $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
artist text NOT NULL,
title text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
update_option( self::OPTION_DB_VERSION, self::SCHEMA_VERSION );
}
if ( !url_to_postid('playlist') ) {
wp_insert_post( array(
'post_title' => 'playlist',
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'page'
) );
}
}
function wp_init () {
add_option( self::OPTION_KEY );
add_option( self::OPTION_KEEP_N_DAYS );
add_option( self::OPTION_DB_VERSION );
//TODO load_plugin_textdomain('rivendell-now-and-next', false, basename( dirname( __FILE__ ) ) . '/lang'
}
function admin_menu () {
// This page will be under "Settings"
add_options_page(
"Rivendell's playlist settings",
'Rivendell',
'manage_options',
'rivendell_settings',
array( $this, 'options_page' )
);
}
function admin_init () {
register_setting( 'rivendell_settings', self::OPTION_KEY );
register_setting( 'rivendell_settings', self::OPTION_KEEP_N_DAYS,
array(
'type' => 'integer',
'sanitize_callback' => array( $this, 'sanitize_keep_n_days' ),
) );
add_settings_section(
'capture_script_parameters', // ID
'Now & Next capture parameters', // Title
array( $this, 'empty_cb' ), // Callback
'rivendell_settings' // Page
);
add_settings_field(
'key', // ID
'Security Key', // Title
array( $this, 'settings_cb_key' ), // Callback
'rivendell_settings', // Page
'capture_script_parameters' // Section
);
add_settings_field(
'keep_n_days', // ID
'Days before erasing playlist', // Title
array( $this, 'settings_cb_keep_n_days' ), // Callback
'rivendell_settings', // Page
'capture_script_parameters' // Section
);
}
function empty_cb ( $args ) {
}
function settings_cb_key ( $args ) {
$key = get_option( self::OPTION_KEY );
printf('<input type="text" id="key" class="large-text" name="%s" value="%s">',
self::OPTION_KEY, esc_attr( $key ));
}
function settings_cb_keep_n_days ( $args ) {
$current = get_option( self::OPTION_KEEP_N_DAYS );
if ( $current === false ){
$current = 7;
}
printf('<input type="text" id="keep_n_days" name="%s" value="%s">',
self::OPTION_KEEP_N_DAYS, esc_attr( $current ));
}
function sanitize_keep_n_days( $input ) {
if ( preg_match('/[0-9]+/', $input ) ){
return (int) $input;
} else {
add_settings_error(
'not_a_number',
'validationError',
'Please the number of days before erasing playlist items',
'error');
}
return null;
}
function options_page () {
if ( !current_user_can( 'manage_options' ) ) {
return;
}
echo '<div class="wrap">';
echo '<h1>'.esc_html( get_admin_page_title() ).'</h1>';
echo '<form method="post" action="options.php">';
settings_fields( 'rivendell_settings' );
do_settings_sections( 'rivendell_settings' );
submit_button( "Save", 'primary', 'submit' );
echo '</form>';
echo '</div>';
}
/**
* POST request to /wp-admin/admin-post.php?action=rivendell_now_and_next_store should include data:
* - "key" that matches the plugin configured key
* - "artisttitle" structured as "ARTIST___TITLE" (that's 3 underscores)
*/
function store () {
$given_key = stripslashes(@$_POST['key']);
$key = get_option( self::OPTION_KEY );
if ( empty($key) ) {
print "The secret key is not configured. Please set a (long) one in Wordpress' parameters\n";
return;
} elseif ( $key != $given_key ) {
return;
}
$raw_id = stripslashes(@$_POST['artisttitle']);
$matches = explode( '___', $raw_id );
$artist = @$matches[0];
$title = @$matches[1];
if ( empty( $artist ) && empty( $title ) ) {
return;
}
global $wpdb;
$table_name = self::table_name();
$previous = $wpdb->get_row( "
SELECT *
FROM $table_name
ORDER BY time DESC
LIMIT 1
");
if ( $previous->artist == $artist && $previous->title == $title ) {
print "Already posted, skipping\n";
return;
}
$days_before_erasure = get_option( self::OPTION_KEEP_N_DAYS );
$erase_before = new DateTime("-${days_before_erasure}days");
$wpdb->query( $wpdb->prepare("
DELETE FROM $table_name
WHERE time <= %s
", $erase_before->format("Y-m-d H:i:s") ) );
$entries = $wpdb->insert($table_name, array(
'time' => current_time( 'mysql' ),
'artist' => $artist,
'title' => $title
));
if ( $wpdb->insert_id ) {
print "OK\n";
}
}
function playlist_page ( $page_template ) {
if ( is_page( 'playlist' ) ) {
add_filter( 'the_content', array ( $this, 'list_entries'), 1 );
}
return $page_template;
}
function list_entries ( $content ) {
global $wp_current_filter;
// Don't add to get_the_excerpt because it's too early and strips tags (adding to the_excerpt is allowed)
if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
return $content;
}
$before = @$_GET['before'];
if ( !empty($before) ) {
if ( !preg_match('/\A[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\z/', $before ) ){
$before = false;
}
}
global $wpdb;
$table_name = self::table_name();
$available_hours = $wpdb->get_results( "
SELECT DISTINCT DATE(time) as day, HOUR(time) as hour
FROM $table_name
ORDER BY time DESC
");
$content .= "<a name='rivendellplaylist'/> </a>\n";
$content .= "<form class='rivendell-playlist' action='#rivendellplaylist'>\n";
$content .= "Titres diffusés vers <select name='before' onchange='this.form.submit()'>>\n";
foreach ( $available_hours as $entry ) {
if ( strlen( $entry->hour ) == 1) {
$hour = '0'.$entry->hour;
} else {
$hour = $entry->hour;
}
$timestamp = strtotime($entry->day.' '.$hour.':00:00');
$display = strftime("%H:%M, %A %e %B %Y", $timestamp);
$value = $entry->day.' '.$hour.':59:59';
if ( $value == $before ){
$selected = 'selected';
} else {
$selected = '';
}
$content .= "<option value='$value' $selected>$display</option>\n";
}
$content .= "</select><input type='submit' value='OK'/></form>\n";
if ( $before ){
$where = "time <= '$before'";
} else {
$where = "1";
}
$entries = $wpdb->get_results( "
SELECT *
FROM $table_name
WHERE $where
ORDER BY time DESC
LIMIT 20
");
$content .= "<ul class='rivendell-playlist'>\n";
$previous_day = null;
foreach ( $entries as $entry ) {
# TODO maybe need setlocale(LC_TIME, 'fr_FR.utf8','fra');
$timestamp = strtotime($entry->time);
$day = strftime("%A %e %B %Y", $timestamp);
if ( $day != $previous_day ) {
$previous_day = $day;
$content .= "</ul>\n<h2 class='rivendell-playlist-day'>$day</h2>\n<ul class='rivendell-playlist'>\n";
}
$time = substr($entry->time, 11, 5);
# TIP: in CSS you can select artist with "li.rivendell-playlist span:nth-of-type(2)"
$content .= "<li class='rivendell-playlist'><span>$time</span> <span>$entry->artist</span>: <span>$entry->title</span></li>\n";
}
$content .= "</ul>\n";
return $content;
}
}
$rivendell_now_and_next = RivendellNowAndNext::instance();