master
campi 2 years ago
parent 666b9a710f
commit 04305b554d

@ -42,7 +42,7 @@ function live_meta_box_add()
function orari_dirette_input($post)
{
$values = get_post_custom($post->ID);
$onair_cadence = $onair_start_date = $onair_start = $onair_end = array();
$onair_cadence = $onair_startdate = $onair_start = $onair_end = array();
wp_nonce_field('on_air_nonce', 'onair_nonce');
$broadcast = term_exists('Podcast', 'category', 0);
$old_term = term_exists($post->post_title, 'category', $broadcast);
@ -52,7 +52,7 @@ function orari_dirette_input($post)
for ($x=0;$x<7;$x++) :
$onair_day[$x] = isset($values['on_air_day_'.$x]) ? esc_attr($values['on_air_day_'.$x][0]) : '';
$onair_cadence[$x] = isset($values['on_air_cadence_'.$x]) ? esc_attr($values['on_air_cadence_'.$x][0]) : '';
$onair_start_date[$x] = isset($values['on_air_start_date_'.$x]) ? esc_attr($values['on_air_start_date_'.$x][0]) : '';
$onair_startdate[$x] = isset($values['on_air_startdate_'.$x]) ? esc_attr($values['on_air_startdate_'.$x][0]) : '';
$onair_start[$x] = isset($values['on_air_start_'.$x]) ? esc_attr($values['on_air_start_'.$x][0]) : '';
$onair_end[$x] = isset($values['on_air_end_'.$x]) ? esc_attr($values['on_air_end_'.$x][0]) : '';
$broadcast[$x] = term_exists('Podcast', 'category', 0);
@ -71,8 +71,8 @@ function orari_dirette_input($post)
</select>
</div>
<div style='width:20%;float:left;'>
<label for="onair_start_date_<?php echo $x; ?>">Data inizio</label>
<input type="date" name="on_air_start_date_<?php echo $x; ?>" id="on_air_start_date_<?php echo $x; ?>" value="<?php echo $onair_start_date[$x]; ?>" style="width: 40%;" />
<label for="onair_startdate_<?php echo $x; ?>">Data inizio</label>
<input type="date" name="on_air_startdate_<?php echo $x; ?>" id="on_air_startdate_<?php echo $x; ?>" value="<?php echo $onair_startdate[$x]; ?>" style="width: 40%;" />
</div>
<div style='width:20%;float:left;'>
<select name="on_air_cadence_<?php echo $x; ?>" id="on_air_cadence_<?php echo $x; ?>">
@ -102,14 +102,14 @@ function orari_dirette_input($post)
function orari_repliche_input($post)
{
$values = get_post_custom($post->ID);
$rerun_day = $rerun_cadence = $rerun_start_date = $rerun_start = $rerun_end = array();
$rerun_day = $rerun_cadence = $rerun_startdate = $rerun_start = $rerun_end = array();
wp_nonce_field('rerun_nonce', 're_run_nonce');
for ($x=0;$x<7;$x++) :
$rerun_day[$x] = isset($values['rerun_day_'.$x]) ? esc_attr($values['rerun_day_'.$x][0]) : '';
$rerun_start[$x] = isset($values['rerun_start_'.$x]) ? esc_attr($values['rerun_start_'.$x][0]) : '';
$rerun_end[$x] = isset($values['rerun_end_'.$x]) ? esc_attr($values['rerun_end_'.$x][0]) : '';
$rerun_cadence[$x] = isset($values['rerun_cadence_'.$x]) ? esc_attr($values['rerun_cadence_'.$x][0]) : '';
$rerun_start_date[$x] = isset($values['rerun_start_date_'.$x]) ? esc_attr($values['rerun_start_date_'.$x][0]) : '';
$rerun_startdate[$x] = isset($values['rerun_startdate_'.$x]) ? esc_attr($values['rerun_startdate_'.$x][0]) : '';
?>
<div style='width:100%;float:left;'>
<h3>Replica n. <?php echo $x+1; ?></h3>
@ -127,15 +127,18 @@ function orari_repliche_input($post)
</select>
</div>
<div style='width:20%;float:left;'>
<label for="rerun_start_date_<?php echo $x; ?>">Data inizio</label>
<input type="date" name="rerun_start_date_<?php echo $x; ?>" id="rerun_start_date_<?php echo $x; ?>" value="<?php echo $rerun_start_date[$x]; ?>" style="width: 40%;" />
<label for="rerun_startdate_<?php echo $x; ?>">Data inizio</label>
<input type="date" name="rerun_startdate_<?php echo $x; ?>" id="rerun_startdate_<?php echo $x; ?>" value="<?php echo $rerun_startdate[$x]; ?>" style="width: 40%;" />
</div>
<div style='width:20%;float:left;'>
<select name="rerun_cadence_<?php echo $x; ?>" id="rerun_cadence_<?php echo $x; ?>">
<label for="rerun_cadence_<?php echo $x; ?>">Cadenza</label>
<option <?php echo ($rerun_cadence[$x]==1) ? 'selected' : ''; ?> value="1">Una volta a settimana</option>
<option <?php echo ($rerun_cadence[$x]==2) ? 'selected' : ''; ?> value="2">Una volta ogni 2 settimane</option>
<option <?php echo ($rerun_cadence[$x]==4) ? 'selected' : ''; ?> value="4">Una volta ogni 4 settimane</option>
<option <?php echo ($rerun_cadence[$x]==3) ? 'selected' : ''; ?> value="3" type="hidden">Una volta ogni 3 settimane</option>
<option <?php echo ($rerun_cadence[$x]==4) ? 'selected' : ''; ?> value="4" type="hidden">Una volta ogni 4 settimane</option>
<option <?php echo ($rerun_cadence[$x]==5) ? 'selected' : ''; ?> value="3">Una volta al mese</option>
</select>
</div>
<div style='width:20%;float:left;'>
@ -202,14 +205,14 @@ function orari_dirette_save($post_id)
for ($x=0;$x<7;$x++) {
$onair_day_data = wp_kses($_POST['on_air_day_'.$x], $allowed);
$rerun_day_data = wp_kses($_POST['rerun_day_'.$x], $allowed);
$onair_start_date_data = wp_kses($_POST['on_air_start_date_'.$x], $allowed);
$rerun_start_date_data = wp_kses($_POST['rerun_start_date_'.$x], $allowed);
$onair_startdate_data = wp_kses($_POST['on_air_startdate_'.$x], $allowed);
$rerun_startdate_data = wp_kses($_POST['rerun_startdate_'.$x], $allowed);
if (!empty($onair_day_data) && !empty($onair_start_date_data)) {
check_and_correct_start_date_flag($onair_start_date_data, $onair_day_data);
if (!empty($onair_day_data) && !empty($onair_startdate_data)) {
check_and_correct_startdate_flag($onair_startdate_data, $onair_day_data);
}
if (!empty($rerun_day_data) && !empty($rerun_start_date_data)) {
check_and_correct_start_date_flag($rerun_start_date_data, $rerun_day_data);
if (!empty($rerun_day_data) && !empty($rerun_startdate_data)) {
check_and_correct_startdate_flag($rerun_startdate_data, $rerun_day_data);
}
update_post_meta($post_id, 'on_air_day_'.$x, $onair_day_data);
update_post_meta($post_id, 'on_air_start_'.$x, wp_kses($_POST['on_air_start_'.$x], $allowed));
@ -219,18 +222,18 @@ function orari_dirette_save($post_id)
update_post_meta($post_id, 'rerun_start_'.$x, wp_kses($_POST['rerun_start_'.$x], $allowed));
update_post_meta($post_id, 'rerun_end_'.$x, wp_kses($_POST['rerun_end_'.$x], $allowed));
update_post_meta($post_id, 'rerun_cadence_'.$x, wp_kses($_POST['rerun_cadence_'.$x], $allowed));
update_post_meta($post_id, 'on_air_start_date_'.$x, $onair_start_date_data);
update_post_meta($post_id, 'rerun_start_date_'.$x, $rerun_start_date_data);
update_post_meta($post_id, 'on_air_startdate_'.$x, $onair_startdate_data);
update_post_meta($post_id, 'rerun_startdate_'.$x, $rerun_startdate_data);
}
}
}
function check_and_correct_start_date_flag(&$start_date_ptr, $day) {
$start_date_dt = new DateTime($start_date_ptr);
$weekday_number = $start_date_dt->format("N");
function check_and_correct_startdate_flag(&$startdate_ptr, $day) {
$startdate_dt = new DateTime($startdate_ptr);
$weekday_number = $startdate_dt->format("N");
if ($day != $weekday_number) {
$day_diff = $day - $weekday_number;
$start_date_dt->modify($day_diff." days");
$start_date_ptr = $start_date_dt->format("Y-m-d");
$startdate_dt->modify($day_diff." days");
$startdate_ptr = $startdate_dt->format("Y-m-d");
};
}

@ -1,13 +1,14 @@
<?php
function have_to_run_this_week($start_date_s, $cadence_i)
function have_to_run_this_week($startdate_s, $cadence_i)
{
$today = (new DateTime())->setTime(0,0,0);
if (!empty($start_date_s)) {
$start_date = new DateTime($start_date_s);
if ($start_date > $today){
if (!empty($startdate_s)) {
$startdate = new DateTime($startdate_s);
if ($startdate > $today)
return false;
}
} else {
$cadence_i = 1;
}
$return_value;
switch($cadence_i) {
@ -18,17 +19,18 @@ function have_to_run_this_week($start_date_s, $cadence_i)
case 3: //ogni 3 settimane
case 4: //ogni 4 settimane
$modulo = 7 * $cadence_i;
$today->modify("this week {$start_date->format('l')}");
$date_difference = date_diff($today, $start_date);
$today->modify("this week {$startdate->format('l')}");
$date_difference = date_diff($today, $startdate);
$days_difference = $date_difference->days;
if (($days_difference % $modulo) < 7) {
if (($days_difference % $modulo) == 0) {
$return_value = true;
break;
}
$return_value = false;
break;
case 5: //una volta al mese
if (which_day_of_the_month($start_date) == which_day_of_the_month($today)) {
print_r($startdate);
if (which_day_of_the_month($startdate) == which_day_of_the_month($today)) {
$return_value = true;
break;
}
@ -45,7 +47,6 @@ function which_day_of_the_month($datetime)
$weekday_iterator = clone($datetime);
$weekday_iterator->modify("first $weekday_literal of this month");
$count = 1;
while($datetime != $weekday_iterator && $count < 5) {
$weekday_iterator->modify("next $weekday_literal");
$count++;
@ -67,21 +68,21 @@ function show_palinsesto($args)
$palinsesto = [];
foreach ($broadcasts as $post) {
$on_air_day = $on_air_start = $on_air_end = $on_air_cadence = $on_air_start_date = "";
$on_air_day = $on_air_start = $on_air_end = $on_air_cadence = $on_air_startdate = "";
for ($d=0;$d<7;$d++) {
$on_air_day = get_post_meta($post->ID, 'on_air_day_'.$d, true);
$on_air_start = get_post_meta($post->ID, 'on_air_start_'.$d, true);
$on_air_end = get_post_meta($post->ID, 'on_air_end_'.$d, true);
$on_air_cadence = get_post_meta($post->ID, 'on_air_cadence_'.$d, true);
$on_air_start_date = get_post_meta($post->ID, 'on_air_start_date_'.$d, true);
$on_air_startdate = get_post_meta($post->ID, 'on_air_startdate_'.$d, true);
if (empty($on_air_cadence) || empty($on_air_start_date)) {
if (empty($on_air_cadence) || empty($on_air_startdate)) {
$on_air_cadence = "1";
}
if (!empty($on_air_day)&&!empty($on_air_start)&&!empty($on_air_end)) {
if (have_to_run_this_week($on_air_start_date, (int)$on_air_cadence)) {
if (have_to_run_this_week($on_air_startdate, (int)$on_air_cadence)) {
$palinsesto[(int)$on_air_day][(string)$on_air_start] =
array(
'ID' => $post->ID,
@ -97,26 +98,21 @@ function show_palinsesto($args)
}
}
$rerun_day = $rerun_start = $rerun_end = $rerun_cadence = $rerun_start_date = "";
$rerun_day = $rerun_start = $rerun_end = $rerun_cadence = $rerun_startdate = "";
for ($d=0;$d<7;$d++) {
$rerun_day = get_post_meta($post->ID, 'rerun_day_'.$d, true);
$rerun_start = get_post_meta($post->ID, 'rerun_start_'.$d, true);
$rerun_end = get_post_meta($post->ID, 'rerun_end_'.$d, true);
$rerun_cadence = get_post_meta($post->ID, 'rerun_cadence_'.$d, true);
$rerun_start_date = get_post_meta($post->ID, 'rerun_start_date_'.$d, true);
$rerun_startdate = get_post_meta($post->ID, 'rerun_startdate_'.$d, true);
/*
echo '<pre>';
print_r($post->ID . " - " . $rerun_day ." - " . $rerun_cadence);
echo '</pre>';
*/
if (!empty($rerun_day)&&!empty($rerun_start)&&!empty($rerun_end)) {
if (empty($rerun_cadence) || empty($rerun_start_date)) {
if (empty($rerun_cadence) || empty($rerun_startdate)) {
$rerun_cadence = "1"; //default settimanale
}
if (have_to_run_this_week($rerun_start_date, (int)$rerun_cadence)) {
if (have_to_run_this_week($rerun_startdate, (int)$rerun_cadence)) {
$palinsesto[(int)$rerun_day][(string)$rerun_start] =
array(
'ID' => $post->ID,
@ -134,21 +130,10 @@ function show_palinsesto($args)
}
ksort($palinsesto);
// echo '<pre>';
// print_r($palinsesto);
// echo '</pre>';
$today = date("N");
for ($x=1;$x<=7;$x++) {
/*
echo '<pre>';
echo("$today,$x");
echo '</pre>';
*/
if ($x == $today)
$tabs .= "<li class='default-gator-tab'>".$week_days[$x-1]."</li>";
else

@ -18,56 +18,52 @@ function what_play_now($args)
/* Cerco nelle dirette */
/* Prendo dalla tabella wp_postmeta le tramissioni con diretta oggi e le repliche */
$today_onair = $wpdb->get_results("
SELECT post_id, meta_key FROM wp_postmeta
WHERE meta_key BETWEEN 'on_air_day_0' AND 'on_air_day_6'
AND meta_value = '$day'
$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'
");
$today_rerun = $wpdb->get_results("
SELECT post_id, meta_key FROM wp_postmeta
WHERE meta_key BETWEEN 'rerun_day_0' AND 'rerun_day_6'
AND meta_value = '$day'
");
/* Inserisco in un array le trasmissioni */
$broadcast_to_check = [];
foreach ($today_onair as $t) {
array_push($broadcast_to_check, (array) $t );
}
foreach ($today_rerun as $t) {
array_push($broadcast_to_check, (array) $t );
}
foreach($broadcast_to_check as $broadcast) {
foreach($today_broadcast as $broadcast) {
$broadcast = (array) $broadcast;
$post_id = $broadcast['post_id'];
$day_meta_key = $broadcast['meta_key'];
$meta_key_start = str_replace("day", "start", $day_meta_key);
$meta_key_end = str_replace("day", "end", $day_meta_key);
/* Estraggo la fascia oraria e controllo ne $now è contenuto nel range */
$start_timestring = ($wpdb->get_results("
SELECT meta_value FROM wp_postmeta
WHERE post_id = '$post_id' AND meta_key = '$meta_key_start'
"))[0]->meta_value;
$end_timestring = ($wpdb->get_results("
SELECT meta_value FROM wp_postmeta
WHERE post_id = '$post_id' AND meta_key = '$meta_key_end'
"))[0]->meta_value;
/* 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"] : "";
$start = new DateTime("$start_timestring");
$end = new DateTime("$end_timestring");
if (have_to_run_this_week($startdate_string,(int) $cadence_string)) {
$start_timestring = $broadcast_data["start"];
$end_timestring = $broadcast_data["end"];
if ($now >= $start && $now <= $end) {
$done = true;
return get_post($post_id)->post_title;
break;
$start = new DateTime("$start_timestring");
$end = new DateTime("$end_timestring");
if ($now >= $start && $now <= $end) {
$done = true;
return get_post($post_id)->post_title;
break;
}
}
}
if (!$done) {
return "Selezione musicale";
}
}
add_shortcode('whatplaynow', 'what_play_now');

Loading…
Cancel
Save