/*
	Theme Name:	XTRA Child
	Theme URI:	https://xtratheme.com/
	Description:Multipurpose Theme
	Author:		Codevz
	Author URI:	https://codevz.com/
	Template:	xtra
	Version:	1.0
*/

/*
	PLEASE DO NOT edit this file, if you want add custom CSS go to Theme Options > Additional CSS
*/
function show_html_block_by_id_shortcode($atts) {
    $atts = shortcode_atts([
        'id' => 0,
    ], $atts);

    $id = intval($atts['id']);
    if (!$id) {
        return '<p>Please provide a valid Html Block ID.</p>';
    }

    $post = get_post($id);
    if (!$post || $post->post_type !== 'html-block') {
        return '<p>Html Block not found with this ID.</p>';
    }

    if ($post->post_status !== 'publish') {
        return '<p>This Html Block is not published yet.</p>';
    }

    $content = $post->post_content;
    $content = do_shortcode($content);

    return $content;
}
add_shortcode('show_html_block', 'show_html_block_by_id_shortcode');
// Add custom column to the list table
function add_shortcode_column_to_html_blocks($columns) {
    $columns['html_block_shortcode'] = 'Shortcode';
    return $columns;
}
add_filter('manage_html-block_posts_columns', 'add_shortcode_column_to_html_blocks');

// Populate the column content
function show_shortcode_in_html_block_column($column, $post_id) {
    if ($column === 'html_block_shortcode') {
        $shortcode = sprintf('[show_html_block id="%d"]', $post_id);
        ?>
        <div style="display: flex; align-items: center; gap: 8px; flex-wrap: wrap;">
            <input type="text" 
                   id="shortcode-<?php echo $post_id; ?>" 
                   value="<?php echo esc_attr($shortcode); ?>" 
                   readonly 
                   style="font-family: monospace; background: #f1f1f1; border: 1px solid #ccc; padding: 4px 6px; width: auto; min-width: 180px;"
                   onclick="this.select();">
            <button type="button" 
                    class="button button-small copy-shortcode-btn" 
                    data-post-id="<?php echo $post_id; ?>"
                    style="margin: 0;">
                Copy
            </button>
            <span id="copy-msg-<?php echo $post_id; ?>" style="font-size: 11px; color: green; display: none;">Copied ✓</span>
        </div>
        <?php
    }
}
add_action('manage_html-block_posts_custom_column', 'show_shortcode_in_html_block_column', 10, 2);

// Add JavaScript for copy functionality
function add_copy_shortcode_script_to_html_block_list() {
    $current_screen = get_current_screen();
    if ($current_screen && $current_screen->post_type === 'html-block' && $current_screen->base === 'edit') {
        ?>
        <script>
        document.addEventListener('DOMContentLoaded', function() {
            document.querySelectorAll('.copy-shortcode-btn').forEach(function(btn) {
                btn.addEventListener('click', function(e) {
                    e.preventDefault();
                    var postId = this.getAttribute('data-post-id');
                    var inputField = document.getElementById('shortcode-' + postId);
                    var msgSpan = document.getElementById('copy-msg-' + postId);
                    
                    if (inputField) {
                        inputField.select();
                        inputField.setSelectionRange(0, 99999);
                        
                        try {
                            navigator.clipboard.writeText(inputField.value).then(function() {
                                if (msgSpan) {
                                    msgSpan.style.display = 'inline';
                                    setTimeout(function() {
                                        msgSpan.style.display = 'none';
                                    }, 1500);
                                }
                            }).catch(function(err) {
                                document.execCommand('copy');
                                if (msgSpan) {
                                    msgSpan.style.display = 'inline';
                                    setTimeout(function() {
                                        msgSpan.style.display = 'none';
                                    }, 1500);
                                }
                            });
                        } catch(e) {
                            document.execCommand('copy');
                            if (msgSpan) {
                                msgSpan.style.display = 'inline';
                                setTimeout(function() {
                                    msgSpan.style.display = 'none';
                                }, 1500);
                            }
                        }
                    }
                });
            });
        });
        </script>
        <?php
    }
}
add_action('admin_footer', 'add_copy_shortcode_script_to_html_block_list');