You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.1 KiB
PHTML

2 years ago
<?php
/*
La funzione what_play_now funzione da per scontato che non ci siano trasmissioni con piu' di una diretta
a settimana nello stesso giorno.
*/
function what_play_now($args)
{
2 years ago
global $wpdb;
2 years ago
/* Oggi espresso come $day (giorno della settimana 1-7 a partire da lunedì) e $now oggetto DateTime completo */
$day = date("N");
$now = new DateTime("NOW");
$done = false;
/* Cerco nelle dirette */
/* Prendo dalla tabella wp_postmeta le tramissioni con diretta oggi e le repliche */
2 years ago
$today_broadcast = $wpdb->get_results("
SELECT post_id, meta_key FROM wp_postmeta WHERE (
(meta_key BETWEEN 'on_air_day_0' AND 'on_air_day_6') OR
(meta_key BETWEEN 'rerun_day_0' AND 'rerun_day_6'))
AND meta_value = '$day'
");
2 years ago
2 years ago
foreach($today_broadcast as $broadcast) {
$broadcast = (array) $broadcast;
$post_id = $broadcast['post_id'];
2 years ago
/* Estraggo tutti i metadati del post e li metto in un array associativo $broadcast_data*/
$meta_key_day = $broadcast['meta_key'];
$meta_key = str_replace("day", "%", $meta_key_day);
$meta_key = str_replace("_", "\\_", $meta_key);
$metadata_query = $wpdb->get_results("
SELECT meta_key, meta_value FROM wp_postmeta
WHERE post_id = '$post_id'
AND meta_key LIKE '$meta_key'
");
$broadcast_data = [];
foreach ($metadata_query as $meta) {
$key_exploded = explode("_", $meta->meta_key);
$key = $key_exploded[count($key_exploded) - 2];
$broadcast_data[$key] = $meta->meta_value;
}
$cadence_string = (isset($broadcast_data["cadence"])) ? $broadcast_data["cadence"] : "1";
$startdate_string = (isset($broadcast_data["startdate"])) ? $broadcast_data["startdate"] : "";
if (have_to_run_this_week($startdate_string,(int) $cadence_string)) {
$start_timestring = $broadcast_data["start"];
$end_timestring = $broadcast_data["end"];
2 years ago
$start = new DateTime("$start_timestring");
$end = new DateTime("$end_timestring");
2 years ago
if ($now >= $start && $now <= $end) {
$done = true;
return get_post($post_id)->post_title;
break;
}
2 years ago
}
}
2 years ago
2 years ago
if (!$done) {
return "Selezione musicale";
}
}
add_shortcode('whatplaynow', 'what_play_now');
?>