<?php
function dacdb_debug($function_name, $atts, $options, $additional_vars, $xml)
{
    if (!$GLOBALS['fDebug']) {
        return; // Exit if debugging is not enabled
    }

    echo "<h5>function <em><strong>$function_name</strong></em> called:</h5>";

    // Display options
    echo '<h4 class="drawer">Options</h4><pre class="panel">';
    foreach ($options as $key => $value) {
        echo "<dt>\$options['$key']</dt><dd>$value</dd>";
    }
    echo '</pre>';

    // Display shortcode attributes
    if (count($atts) > 0) {
        echo '<h4 class="drawer">Shortcode Attributes</h4><pre class="panel">';
        foreach ($atts as $key => $value) {
            echo "<dt>[\$atts] $key</dt><dd>$value</dd>";
        }
        echo '</pre>';
    }

    // Display additional variables
    if (count($additional_vars) > 0) {
        echo '<h4 class="drawer">Named Variables</h4><pre class="panel">';
        foreach ($additional_vars as $key => $value) {
            // Special handling for XML to format it nicely
            if ($key == 'xml') {
                echo "<dt>\$xml</dt><dd>";
                // Format and display XML
                // ... Add XML formatting and displaying code here ...
                echo "</dd>";
            } else {
                echo "<dt>\$$key</dt><dd>$value</dd>";
            }
        }
        echo '</pre>';
    }    // Display additional variables

    if (count($xml) > 0) {
        echo '<h4 class="drawer">XML Output</h4><div class="panel">';
        foreach ($xml->element as $content) {
            $dom = new DOMDocument();
            $dom->preserveWhiteSpace = false; // Don't preserve whitespaces
            $dom->formatOutput = true; // Format output with indentation and line breaks
            $dom->loadXML($content->asXML()); // Load the XML string from SimpleXMLElement
            $formattedXml = $dom->saveXML(); // Save the formatted XML to a string
            echo '<pre>' . htmlspecialchars($formattedXml) . '</pre>';
        }
        echo '</div>';
    }
}
function dacdb_debug_shortcode($post, $atts)
{
    $pagedata = ''; // Initialize the variable to store debugging information

    // Check if the shortcode exists on the page
    if (has_shortcode($post->post_content, 'dacdb_plus')) {
        // Extract the original shortcode from the content
        $pattern = get_shortcode_regex(['dacdb_plus']);
        preg_match_all('/' . $pattern . '/s', $post->post_content, $matches);
        
        if (isset($matches[0][0])) {
            // Return the original shortcode as it appears in the content
            return '<code>' . esc_html($matches[0][0]) . '</code>';
        } else {
            // If extraction fails, fallback to showing only non-empty attributes
            $shortcode_name = 'dacdb_plus';
            $pagedata .= "<code>[$shortcode_name";
            
            // Filter out empty or default attributes
            $filtered_atts = array_filter($atts, function($value) {
                return $value !== '' && $value !== null && $value !== 0 && $value !== '0' && $value !== false && $value !== 'false';
            });
            
            foreach ($filtered_atts as $param => $value) {
                $pagedata .= " $param=\"$value\"";
            }
            $pagedata .= "]</code>";
        }
    } else {
        // Shortcode not found on the page
        $pagedata .= "<code>[dacdb_plus]</code> shortcode not found on the page";
    }

    return $pagedata;
}