<?php

// Feature Meta
function audio_player_bn_custom_meta_feature() {
    add_meta_box( 'bn_meta', __( 'Custom fields for Features', 'audio-player' ), 'audio_player_meta_callback_feature', 'post', 'normal', 'high' );
}

if (is_admin()){
  add_action('admin_menu', 'audio_player_bn_custom_meta_feature');
}

function audio_player_meta_callback_feature( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'audio_player_feature_meta_nonce' );
    $bn_stored_meta = get_post_meta( $post->ID );
    $button_url = get_post_meta( $post->ID, 'audio_player_button_url', true );
    $episodes1 = get_post_meta( $post->ID, 'audio_player_episode_1', true );
    $text1 = get_post_meta( $post->ID, 'audio_player_text_1', true );
    $button_uel_2 = get_post_meta( $post->ID, 'audio_player_button_url_2', true );
    $episodes_2 = get_post_meta( $post->ID, 'audio_player_episode_2', true );
    $text_2 = get_post_meta( $post->ID, 'audio_player_text_2', true );
    ?>
    <div id="feature">
        <table id="list">
            <tbody id="the-list" data-wp-lists="list:meta">
                <tr id="meta-1">
                    <td class="left">
                        <?php esc_html_e( 'Button Url', 'audio-player' )?>
                    </td>
                    <td class="left">
                        <input type="text" name="audio_player_button_url" id="audio_player_button_url" value="<?php echo esc_attr($button_url); ?>" />
                    </td>
                </tr>
                <tr id="meta-2">
                    <td class="left">
                        <?php esc_html_e( 'Episodes', 'audio-player' )?>
                    </td>
                    <td class="left">
                        <input type="text" name="audio_player_episode_1" id="audio_player_episode_1" value="<?php echo esc_attr($episodes1); ?>" />
                    </td>
                </tr>
                <tr id="meta-3">
                    <td class="left">
                        <?php esc_html_e( 'Text', 'audio-player' )?>
                    </td>
                    <td class="left">
                        <input type="text" name="audio_player_text_1" id="audio_player_text_1" value="<?php echo esc_attr($text1); ?>" />
                    </td>
                </tr>
                <tr id="meta-4">
                    <td class="left">
                        <?php esc_html_e( 'Button Url', 'audio-player' )?>
                    </td>
                    <td class="left">
                        <input type="text" name="audio_player_button_url_2" id="audio_player_button_url_2" value="<?php echo esc_attr($button_uel_2); ?>" />
                    </td>
                </tr>
                <tr id="meta-5">
                    <td class="left">
                        <?php esc_html_e( 'Episodes', 'audio-player' )?>
                    </td>
                    <td class="left">
                        <input type="text" name="audio_player_episode_2" id="audio_player_episode_2" value="<?php echo esc_attr($episodes_2); ?>" />
                    </td>
                </tr>
                <tr id="meta-8">
                    <td class="left">
                        <?php esc_html_e( 'Text', 'audio-player' )?>
                    </td>
                    <td class="left">
                        <input type="text" name="audio_player_text_2" id="audio_player_text_2" value="<?php echo esc_attr($text_2); ?>" />
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <?php
}

/* Saves the custom meta input */
function audio_player_bn_metadesig_save( $post_id ) {
    if (!isset($_POST['audio_player_feature_meta_nonce']) || !wp_verify_nonce( strip_tags( wp_unslash( $_POST['audio_player_feature_meta_nonce']) ), basename(__FILE__))) {
        return;
    }

    if (!current_user_can('edit_post', $post_id)) {
        return;
    }

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    // Save
    if( isset( $_POST[ 'audio_player_button_url' ] ) ) {
        update_post_meta( $post_id, 'audio_player_button_url', strip_tags( wp_unslash( $_POST[ 'audio_player_button_url' ]) ) );
    }
    if( isset( $_POST[ 'audio_player_episode_1' ] ) ) {
        update_post_meta( $post_id, 'audio_player_episode_1', strip_tags( wp_unslash( $_POST[ 'audio_player_episode_1' ]) ) );
    }
    if( isset( $_POST[ 'audio_player_text_1' ] ) ) {
        update_post_meta( $post_id, 'audio_player_text_1', strip_tags( wp_unslash( $_POST[ 'audio_player_text_1' ]) ) );
    }
    if( isset( $_POST[ 'audio_player_button_url_2' ] ) ) {
        update_post_meta( $post_id, 'audio_player_button_url_2', strip_tags( wp_unslash( $_POST[ 'audio_player_button_url_2' ]) ) );
    }
    if( isset( $_POST[ 'audio_player_episode_2' ] ) ) {
        update_post_meta( $post_id, 'audio_player_episode_2', strip_tags( wp_unslash( $_POST[ 'audio_player_episode_2' ]) ) );
    }
    if( isset( $_POST[ 'audio_player_text_2' ] ) ) {
        update_post_meta( $post_id, 'audio_player_text_2', strip_tags( wp_unslash( $_POST[ 'audio_player_text_2' ]) ) );
    }

}
add_action( 'save_post', 'audio_player_bn_metadesig_save' );