<?php
// phpcs:disable WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL,WordPress.NamingConventions.PrefixAllGlobals -- Modal partial uses plugin-owned tables and local template variables.

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

global $wpdb;

$instant_popup_builder_table_name = $wpdb->prefix . 'instant_popup_builder';

$escaped_table_name = esc_sql($instant_popup_builder_table_name);

$instant_popup_builder_row = $wpdb->get_results($wpdb->prepare("SELECT * FROM `{$escaped_table_name}` where id=%s", $popup_id));

if ($instant_popup_builder_row && !empty($instant_popup_builder_row) && isset($instant_popup_builder_row[0])) {
    $instant_popup_builder_content = isset($instant_popup_builder_row[0]->content) ? $instant_popup_builder_row[0]->content : '';

    if (!empty($instant_popup_builder_content)) {
        $instant_popup_builder_decoded_content = json_decode($instant_popup_builder_content);

        // Validate JSON decode was successful
        if (json_last_error() === JSON_ERROR_NONE && $instant_popup_builder_decoded_content !== null) {
            // Ensure content is a string before processing
            $content_to_display = is_string($instant_popup_builder_decoded_content) ? wp_unslash($instant_popup_builder_decoded_content) : '';
            // Ensure wp_unslash result is not null
            $content_to_display = is_string($content_to_display) ? $content_to_display : '';

            if (!empty($content_to_display)) {
                echo wp_kses_post($content_to_display);
            }
        }
    }
}
