HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/video-rental/wp-content/plugins/video-management.php
<?php
/**
 * Plugin Name: Video Management
 * Description: Adds a Video Management menu to the admin area.
 * Version: 1.0
 * Author: Linta
 */

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

// Register custom post type
add_action('init', function () {
    register_post_type('vm_video', [
        'labels' => [
            'name' => 'Videos',
            'singular_name' => 'Video'
        ],
        'public' => false,
        'show_ui' => true,
        'menu_icon' => 'dashicons-format-video',
        'supports' => ['title'],
    ]);
});

// Add meta box
add_action('add_meta_boxes', function () {
    add_meta_box('vm_video_fields', 'Video Details', 'vm_video_fields_callback', 'vm_video', 'normal', 'high');
});

// Meta box callback
function vm_video_fields_callback($post)
{
    $trailer_link = get_post_meta($post->ID, 'vm_trailer_link', true);
    $vm_main_video_url = get_post_meta($post->ID, 'vm_main_video_url', true);
    $description = get_post_meta($post->ID, 'vm_description', true);
    $price = get_post_meta($post->ID, 'vm_price', true);
    $rental = get_post_meta($post->ID, 'vm_rental', true);
    $thumb = get_post_meta($post->ID, 'vm_video_thumbnail', true);
    ?>

    <p><label>Trailer Link (Cloudflare):</label><br>
        <input type="text" name="vm_trailer_link" value="<?php echo esc_attr($trailer_link); ?>" style="width:100%;" />
    </p>
    <p><label>Video URL:</label><br>
        <input type="text" name="vm_main_video_url"
            value="<?php echo esc_attr(get_post_meta($post->ID, 'vm_main_video_url', true)); ?>" style="width:100%;" />
    </p>

    <p><label>Description:</label></p>
    <?php
    wp_editor($description, 'vm_description', [
        'textarea_name' => 'vm_description',
        'media_buttons' => false,
        'textarea_rows' => 8,
        'editor_class' => 'vm_description_editor',
    ]);
    ?>

    <p><label>Price ($):</label><br>
        <input type="number" name="vm_price" value="<?php echo esc_attr($price); ?>" step="0.01" min="0" />
    </p>

    <p><label>Rental Period (in days):</label><br>
        <input type="number" name="vm_rental" value="<?php echo esc_attr($rental); ?>" min="0" />
    </p>

    <p><label>Thumbnail:</label><br>
        <input type="hidden" id="vm_video_thumbnail" name="vm_video_thumbnail" value="<?php echo esc_url($thumb); ?>" />
        <button type="button" class="button" id="vm_upload_thumb_btn">Upload Thumbnail</button>
        <button type="button" class="button" id="vm_remove_thumb_btn"
            style="display: <?php echo $thumb ? 'inline-block' : 'none'; ?>;">Remove</button><br><br>
        <img id="vm_thumb_preview" src="<?php echo esc_url($thumb); ?>"
            style="max-width:150px; <?php echo $thumb ? '' : 'display:none;'; ?>" />
    </p>

    <script>
        jQuery(document).ready(function ($) {
            let mediaUploader;
            $('#vm_upload_thumb_btn').on('click', function (e) {
                e.preventDefault();
                if (mediaUploader) {
                    mediaUploader.open();
                    return;
                }

                mediaUploader = wp.media({
                    title: 'Select Video Thumbnail',
                    button: { text: 'Use this image' },
                    multiple: false
                });

                mediaUploader.on('select', function () {
                    const attachment = mediaUploader.state().get('selection').first().toJSON();
                    $('#vm_video_thumbnail').val(attachment.url);
                    $('#vm_thumb_preview').attr('src', attachment.url).show();
                    $('#vm_remove_thumb_btn').show();
                });

                mediaUploader.open();
            });

            $('#vm_remove_thumb_btn').on('click', function (e) {
                e.preventDefault();
                $('#vm_video_thumbnail').val('');
                $('#vm_thumb_preview').hide();
                $(this).hide();
            });
        });
    </script>
    <?php
}

// Save meta fields
add_action('save_post', function ($post_id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;

    $fields = [
        'vm_trailer_link',
        'vm_main_video_url',
        'vm_description',
        'vm_price',
        'vm_rental',
        'vm_video_thumbnail',
    ];

    foreach ($fields as $field) {
        if (isset($_POST[$field])) {
            // update_post_meta($post_id, $field, sanitize_text_field($_POST[$field]));
            $value = ($field === 'vm_description') ? wp_kses_post($_POST[$field]) : sanitize_text_field($_POST[$field]);
            update_post_meta($post_id, $field, $value);
        }
    }
});

// Enqueue media uploader
add_action('admin_enqueue_scripts', function ($hook) {
    $screen = get_current_screen();
    if ($screen->post_type === 'vm_video') {
        wp_enqueue_media();
    }
});