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.

242 lines
13 KiB
PHP

<?php
function create_broadcast_post()
{
register_post_type(
'broadcast',
array(
'labels' => array(
'name' => __('Trasmissioni'),
'add_new' => __('Aggiungi Trasmissione'),
'new_item' => __('Nuova Trasmissione')
),
'public' => true,
'hierarchical' => false,
'exclude_from_search' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'menu_position' => 3,
'has_archive' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
'post-formats'
),
'menu_icon' => 'dashicons-microphone',
)
);
}
add_action('init', 'create_broadcast_post');
add_action('add_meta_boxes', 'live_meta_box_add');
function live_meta_box_add()
{
//give name of your input field
add_meta_box('orari-dirette', 'ORARI DELLA DIRETTA', 'orari_dirette_input', 'broadcast', 'normal', 'high');
add_meta_box('orari-repliche', 'ORARI DELLE REPLICHE', 'orari_repliche_input', 'broadcast', 'normal', 'high');
}
function orari_dirette_input($post)
{
$values = get_post_custom($post->ID);
$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);
?>
<input type="hidden" name="taxonomy_id" value="<?php echo $old_term['term_id'] ?>" />
<?php
for ($x=0;$x<7;$x++) :
$onair_day = isset($values['on_air_day_'.$x]) ? esc_attr($values['on_air_day_'.$x][0]) : '';
$onair_cadence = isset($values['on_air_cadence_'.$x]) ? esc_attr($values['on_air_cadence_'.$x][0]) : '';
$onair_startdate = isset($values['on_air_startdate_'.$x]) ? esc_attr($values['on_air_startdate_'.$x][0]) : '';
$onair_start = isset($values['on_air_start_'.$x]) ? esc_attr($values['on_air_start_'.$x][0]) : '';
$onair_end = isset($values['on_air_end_'.$x]) ? esc_attr($values['on_air_end_'.$x][0]) : '';
?>
<div style='width:100%;float:left;'>
<h3>Diretta n. <?php echo $x+1; ?></h3>
</div>
<div style='width:20%;float:left;'>
<select name="on_air_day_<?php echo $x; ?>" id="on_air_day_<?php echo $x; ?>">
<option <?php echo ($onair_day=='') ? 'selected' : ''; ?> value="">Seleziona un giorno</option>
<option <?php echo ($onair_day==1) ? 'selected' : ''; ?> value="1">Luned&igrave;</option>
<option <?php echo ($onair_day==2) ? 'selected' : ''; ?> value="2">Marted&igrave;</option>
<option <?php echo ($onair_day==3) ? 'selected' : ''; ?> value="3">Mercoled&igrave;</option>
<option <?php echo ($onair_day==4) ? 'selected' : ''; ?> value="4">Gioved&igrave;</option>
<option <?php echo ($onair_day==5) ? 'selected' : ''; ?> value="5">Venerd&igrave;</option>
<option <?php echo ($onair_day==6) ? 'selected' : ''; ?> value="6">Sabato</option>
<option <?php echo ($onair_day==7) ? 'selected' : ''; ?> value="7">Domenica</option>
</select>
</div>
<div style='width:20%;float:left;'>
<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; ?>" 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; ?>">
<label for="on_air_cadence_<?php echo $x; ?>">Cadenza</label>
<option <?php echo ($onair_cadence==1) ? 'selected' : ''; ?> value="1">Una volta a settimana</option>
<option <?php echo ($onair_cadence==2) ? 'selected' : ''; ?> value="2">Una volta ogni 2 settimane</option>
<option <?php echo ($onair_cadence==3) ? 'selected' : ''; ?> value="3" hidden>Una volta ogni 3 settimane</option>
<option <?php echo ($onair_cadence==4) ? 'selected' : ''; ?> value="4">Una volta ogni 4 settimane</option>
<option <?php echo ($onair_cadence==5) ? 'selected' : ''; ?> value="5">Una volta al mese</option>
</select>
</div>
<div style='width:20%;float:left;'>
<label for="on_air_start_<?php echo $x; ?>">Ora inizio</label>
<input type="time" name="on_air_start_<?php echo $x; ?>" id="on_air_start_<?php echo $x; ?>" value="<?php echo $onair_start; ?>" style="width: 40%;" />
</div>
<div style='width:20%;float:left;'>
<label for="on_air_end_<?php echo $x; ?>">Ora fine</label>
<input type="time" name="on_air_end_<?php echo $x; ?>" id="on_air_end_<?php echo $x; ?>" value="<?php echo $onair_end; ?>" style="width: 40%;" />
</div>
<div style="clear:both;"></div>
<?php
endfor;
wp_enqueue_script('add_onchange_listener', plugin_dir_url(__FILE__).'js/add_onchange_listener.js', array(), '1.2', true);
}
function orari_repliche_input($post)
{
$values = get_post_custom($post->ID);
$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 = isset($values['rerun_day_'.$x]) ? esc_attr($values['rerun_day_'.$x][0]) : '';
$rerun_start = isset($values['rerun_start_'.$x]) ? esc_attr($values['rerun_start_'.$x][0]) : '';
$rerun_end = isset($values['rerun_end_'.$x]) ? esc_attr($values['rerun_end_'.$x][0]) : '';
$rerun_cadence = isset($values['rerun_cadence_'.$x]) ? esc_attr($values['rerun_cadence_'.$x][0]) : '';
$rerun_startdate = 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>
</div>
<div style='width:20%;float:left;'>
<select name="rerun_day_<?php echo $x; ?>" id="rerun_day_<?php echo $x; ?>">
<option <?php echo ($rerun_day=='') ? 'selected' : ''; ?> value="">Seleziona un giorno</option>
<option <?php echo ($rerun_day==1) ? 'selected' : ''; ?> value="1">Luned&igrave;</option>
<option <?php echo ($rerun_day==2) ? 'selected' : ''; ?> value="2">Marted&igrave;</option>
<option <?php echo ($rerun_day==3) ? 'selected' : ''; ?> value="3">Mercoled&igrave;</option>
<option <?php echo ($rerun_day==4) ? 'selected' : ''; ?> value="4">Gioved&igrave;</option>
<option <?php echo ($rerun_day==5) ? 'selected' : ''; ?> value="5">Venerd&igrave;</option>
<option <?php echo ($rerun_day==6) ? 'selected' : ''; ?> value="6">Sabato</option>
<option <?php echo ($rerun_day==7) ? 'selected' : ''; ?> value="7">Domenica</option>
</select>
</div>
<div style='width:20%;float:left;'>
<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; ?>" 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==1) ? 'selected' : ''; ?> value="1">Una volta a settimana</option>
<option <?php echo ($rerun_cadence==2) ? 'selected' : ''; ?> value="2">Una volta ogni 2 settimane</option>
<option <?php echo ($rerun_cadence==3) ? 'selected' : ''; ?> value="3" hidden>Una volta ogni 3 settimane</option>
<option <?php echo ($rerun_cadence==4) ? 'selected' : ''; ?> value="4">Una volta ogni 4 settimane</option>
<option <?php echo ($rerun_cadence==5) ? 'selected' : ''; ?> value="3">Una volta al mese</option>
</select>
</div>
<div style='width:20%;float:left;'>
<label for="rerun_start_<?php echo $x; ?>">Ora inizio</label>
<input type="time" name="rerun_start_<?php echo $x; ?>" id="rerun_start_<?php echo $x; ?>" value="<?php echo $rerun_start; ?>" style="width: 40%;" />
</div>
<div style='width:20%;float:left;'>
<label for="rerun_end_<?php echo $x; ?>">Ora fine</label>
<input type="time" name="rerun_end_<?php echo $x; ?>" id="rerun_end_<?php echo $x; ?>" value="<?php echo $rerun_end; ?>" style="width: 40%;" />
</div>
<div style="clear:both;"></div>
<?php
endfor;
}
add_action('save_post', 'orari_dirette_save');
function orari_dirette_save($post_id)
{
// Bail if we're doing an auto save
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// if our nonce isn't there, or we can't verify it, bail
if (!isset($_POST['onair_nonce']) || !wp_verify_nonce($_POST['onair_nonce'], 'on_air_nonce')) {
return;
}
if (!isset($_POST['re_run_nonce']) || !wp_verify_nonce($_POST['re_run_nonce'], 'rerun_nonce')) {
return;
}
// if our current user can't edit this post, bail
if (!current_user_can('edit_post', $post_id)) {
return;
}
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
if (isset($_POST['on_air_start_0'])) {
$broadcast = term_exists('Podcast', 'category', 0);
$check = term_exists(intval($_POST['taxonomy_id']), 'category', $broadcast['term_id']);
if (intval($check['term_id']) > 0) {
$args = array(
'cat_ID' => intval($check['term_id']),
'taxonomy' => 'category',
'cat_name' => $_POST['post_title'],
'category_description' => '',
'category_nicename' => $_POST['post_name'],
'category_parent' => $broadcast['term_id']
);
$test = wp_insert_category($args);
} else {
$args = array(
'parent' => $broadcast['term_id'],
'slug' => strtolower(wp_specialchars(str_replace(" ", "-", $_POST['post_title']))),
);
wp_insert_term($_POST['post_title'], 'category', $args);
}
update_post_meta($post_id, 'taxonomy_id', wp_kses($_POST['taxonomy_id'], $allowed));
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_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_startdate_data)) {
check_and_correct_startdate_flag($onair_startdate_data, $onair_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));
update_post_meta($post_id, 'on_air_end_'.$x, wp_kses($_POST['on_air_end_'.$x], $allowed));
update_post_meta($post_id, 'on_air_cadence_'.$x, wp_kses($_POST['on_air_cadence_'.$x], $allowed));
update_post_meta($post_id, 'rerun_day_'.$x, $rerun_day_data);
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_startdate_'.$x, $onair_startdate_data);
update_post_meta($post_id, 'rerun_startdate_'.$x, $rerun_startdate_data);
}
}
}
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;
$startdate_dt->modify($day_diff." days");
$startdate_ptr = $startdate_dt->format("Y-m-d");
};
}