<?php
/*
Plugin Name: Dynamic Video for Divi Posts
Description: Dynamically embeds a video into your blog posts by using a custom field. When editing a post, a new meta box labeled "Video URL" appears. Enter a video URL (e.g., a YouTube link) into this box, and then use the [dynamic_video] shortcode in your Divi Builder template. The video is automatically embedded using WordPress’s oEmbed functionality. Optional feature to display featured image when no video URL is provided.
Version: 1.0.2
Author: hyperspective
Author URI: https://hyperspective.com
Plugin URI: https://wordpress.org/plugins/dynamic-video-for-divi-posts/
License: GPL2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Display the video or featured image via shortcode.
 *
 * @param array $atts Shortcode attributes.
 * @return string Embedded video HTML, featured image HTML, or empty string.
 */
function dynvid_display_video_shortcode( $atts ) {
    global $post;
    $video_url = get_post_meta( $post->ID, '_dynvid_video_url', true );
    
    if ( ! empty( $video_url ) ) {
        // Embed the video using WordPress's oEmbed functionality.
        return wp_oembed_get( esc_url( $video_url ) );
    } elseif ( get_option( 'dynvid_use_featured_image', 'no' ) === 'yes' ) {
        // If no video URL and featured image option is enabled, display featured image
        if ( has_post_thumbnail( $post->ID ) ) {
            return get_the_post_thumbnail( $post->ID, 'full', array( 'class' => 'dynvid-featured-image' ) );
        }
    }
    return '';
}
add_shortcode( 'dynamic_video', 'dynvid_display_video_shortcode' );

/**
 * Add meta box for video URL in post editor.
 */
function dynvid_add_video_metabox() {
    add_meta_box(
        'dynvid_video_metabox',
        esc_html__( 'Video URL', 'dynamic-video-for-divi-posts' ),
        'dynvid_video_url_metabox_callback',
        'post',
        'side',
        'default'
    );
}
add_action( 'add_meta_boxes', 'dynvid_add_video_metabox' );

/**
 * Render the video URL meta box.
 *
 * @param WP_Post $post The current post object.
 */
function dynvid_video_url_metabox_callback( $post ) {
    wp_nonce_field( 'dynvid_video_url_save', 'dynvid_video_url_nonce' );
    $video_url = get_post_meta( $post->ID, '_dynvid_video_url', true );
    ?>
    <p>
        <label for="dynvid_video_url"><?php esc_html_e( 'Enter the video URL (e.g., YouTube):', 'dynamic-video-for-divi-posts' ); ?></label>
    </p>
    <p>
        <input type="text" name="dynvid_video_url" id="dynvid_video_url" value="<?php echo esc_attr( $video_url ); ?>" style="width:100%;" />
    </p>
    <?php
}

/**
 * Save the video URL meta when a post is saved.
 *
 * @param int $post_id The ID of the post being saved.
 */
function dynvid_save_video_url_meta( $post_id ) {
    if ( ! isset( $_POST['dynvid_video_url_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['dynvid_video_url_nonce'] ) ), 'dynvid_video_url_save' ) ) {
        return;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    if ( isset( $_POST['post_type'] ) && 'post' === $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }
    if ( ! isset( $_POST['dynvid_video_url'] ) ) {
        return;
    }
    $video_url = sanitize_text_field( wp_unslash( $_POST['dynvid_video_url'] ) );
    update_post_meta( $post_id, '_dynvid_video_url', esc_url_raw( $video_url ) );
}
add_action( 'save_post', 'dynvid_save_video_url_meta' );

/**
 * Add an admin submenu page for plugin details and settings.
 */
function dynvid_add_admin_menu() {
    add_submenu_page(
        'options-general.php',
        esc_html__( 'Dynamic Video for Divi Posts Details', 'dynamic-video-for-divi-posts' ),
        esc_html__( 'Dynamic Video for Divi Posts', 'dynamic-video-for-divi-posts' ),
        'manage_options',
        'dynvid_plugin_details_page',
        'dynvid_plugin_details_page'
    );
}
add_action( 'admin_menu', 'dynvid_add_admin_menu' );

/**
 * Register settings for the plugin.
 */
function dynvid_register_settings() {
    register_setting( 'dynvid_settings_group', 'dynvid_use_featured_image', array(
        'type' => 'string',
        'sanitize_callback' => 'sanitize_text_field',
        'default' => 'no'
    ));
}
add_action( 'admin_init', 'dynvid_register_settings' );

/**
 * Render the plugin details admin page.
 */
function dynvid_plugin_details_page() {
    ?>
    <div class="wrap">
        <h1><?php esc_html_e( 'Dynamic Video for Divi Posts Details', 'dynamic-video-for-divi-posts' ); ?></h1>
        <p><?php esc_html_e( 'This plugin dynamically embeds a video into your blog posts using a custom field. When editing a post, enter a video URL (e.g., a YouTube link) in the Video URL meta box, and then use the [dynamic_video] shortcode in your Divi Builder template to display the video.', 'dynamic-video-for-divi-posts' ); ?></p>
        
        <form method="post" action="options.php">
            <?php 
            settings_fields( 'dynvid_settings_group' ); 
            do_settings_sections( 'dynvid_settings_group' );
            $use_featured_image = get_option( 'dynvid_use_featured_image', 'no' );
            ?>
            <h2><?php esc_html_e( 'Settings', 'dynamic-video-for-divi-posts' ); ?></h2>
            <table class="form-table">
                <tr>
                    <th scope="row"><?php esc_html_e( 'Use Featured Image Fallback', 'dynamic-video-for-divi-posts' ); ?></th>
                    <td>
                        <input type="checkbox" name="dynvid_use_featured_image" value="yes" <?php checked( $use_featured_image, 'yes' ); ?> />
                        <?php esc_html_e( 'Display featured image when no video URL is provided', 'dynamic-video-for-divi-posts' ); ?>
                    </td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>

        <h2><?php esc_html_e( 'Usage Instructions', 'dynamic-video-for-divi-posts' ); ?></h2>
        <ol>
            <li><?php esc_html_e( 'Edit a post and enter the video URL in the meta box on the right.', 'dynamic-video-for-divi-posts' ); ?></li>
            <li><?php esc_html_e( 'In your Divi Builder template, add a Code or Text module and insert the shortcode: [dynamic_video]', 'dynamic-video-for-divi-posts' ); ?></li>
            <li><?php esc_html_e( 'The video will be embedded automatically using WordPress’s oEmbed functionality.', 'dynamic-video-for-divi-posts' ); ?></li>
            <li><?php esc_html_e( 'If no video URL is provided and the featured image option is enabled, the post\'s featured image will be displayed instead.', 'dynamic-video-for-divi-posts' ); ?></li>
        </ol>
    </div>
    <?php
}