<?php
/**
 * Subtitle metabox file.
 *
 * @package BlogMelody
 * @since Party Planner Bell 1.0
 */

if ( ! function_exists( 'party_planner_bell_video_url_callback' ) ) :
    /** 
     * Outputs the content of the Video Url
     */
    function party_planner_bell_video_url_callback( $post ) {
        wp_nonce_field( basename( __FILE__ ), 'party_planner_bell_nonce' );
        $video_url = get_post_meta( $post->ID, 'party-planner-bell-video-url', true );
        ?>
        <p>
         <label for="party-planner-bell-video-url" class="party-planner-bell-row-title"><?php esc_html_e( 'Video Url', 'party-planner-bell' )?></label>
         <input class="widefat" type="url" name="party-planner-bell-video-url" id="party-planner-bell-video-url" value="<?php echo esc_url( $video_url ); ?>"/>     
        </p>

        <?php
    }
endif;

if ( ! function_exists( 'party_planner_bell_video_url_save' ) ) :
    /**
     * Saves the Video Url input
     */
    function party_planner_bell_video_url_save( $post_id ) {
        // Checks save status
        $is_autosave = wp_is_post_autosave( $post_id );
        $is_revision = wp_is_post_revision( $post_id );
        $is_valid_nonce = ( isset( $_POST[ 'party_planner_bell_nonce' ] ) && wp_verify_nonce( sanitize_key( $_POST[ 'party_planner_bell_nonce' ] ), basename( __FILE__ ) ) ) ? 'true' : 'false';

        // Exits script depending on save status
        if ( $is_autosave || $is_revision || ! $is_valid_nonce ) {
            return;
        }

        // Checks for input and sanitizes/saves if needed
        if( isset( $_POST[ 'party-planner-bell-video-url' ] ) ) {
            update_post_meta( $post_id, 'party-planner-bell-video-url', sanitize_text_field( wp_unslash( $_POST[ 'party-planner-bell-video-url' ] ) ) );
        }

    }
endif;
add_action( 'save_post', 'party_planner_bell_video_url_save' );

