<?php
/**
 * Plugin Name:       Key Points
 * Description:       Example block scaffolded with Create Block tool.
 * Requires at least: 6.6
 * Requires PHP:      7.2
 * Version:           0.1.0
 * Author:            The WordPress Contributors
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       key-points
 *
 * @package CreateBlock
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Enqueue block assets for the editor
function enqueue_key_points_block_assets() {
    wp_enqueue_script(
        'key-points-editor-script', // Handle for the block script
        plugin_dir_url(__FILE__) . 'block.js', // Path to the block JS file
        ['wp-blocks', 'wp-element', 'wp-editor'], // Dependencies
        filemtime(plugin_dir_path(__FILE__) . 'block.js'), // Cache-busting
        true // Load script in the footer
    );
}
add_action('enqueue_block_editor_assets', 'enqueue_key_points_block_assets');

// Render callback function to display the key points
// Render callback function to display the key points
// Render callback function to display the key points
function render_key_points_block($attributes, $content) {
    // Ensure we're in the correct post context
    if (!is_single()) {
        return null;  // Only render on single post pages
    }

    // Get the current post ID (Ensure the correct context)
    $post_id = get_the_ID();

    // Log for debugging purposes
    error_log('Current Post ID: ' . $post_id);

    // Get the key points stored as a serialized string in post meta
    $key_points_serialized = get_post_meta($post_id, 'key_points', true);

    // Log the serialized data
    error_log('Key Points Serialized Data: ' . var_export($key_points_serialized, true)); // Log to debug

    // Unserialize the data into an array
    $key_points = !empty($key_points_serialized) ? unserialize($key_points_serialized) : [];

    // Log the unserialized data
    error_log('Key Points Unserialized Data: ' . var_export($key_points, true)); // Log to debug

    // If no key points are available, return an error message
    if (empty($key_points)) {
        return null;
    }

    // Check if the headingTag attribute is set and wrap the heading accordingly
    $heading_tag = !empty($attributes['headingTag']) ? $attributes['headingTag'] : 'h2'; // Default to h2 if not provided
    $heading_font_size = !empty($attributes['headingFontSizeLarge']) ? $attributes['headingFontSizeLarge'] : '24px'; // Default size if not provided
    $points_font_size = !empty($attributes['pointsFontSizeLarge']) ? $attributes['pointsFontSizeLarge'] : '16px'; // Default size if not provided
	$list_style_type = !empty($attributes['listStyleType']) ? $attributes['listStyleType'] : 'disc'; // Default size if not provided

    // Create the heading with font size applied
    $heading = "<{$heading_tag} style='font-size: " . esc_attr($heading_font_size) . ";'>" . esc_html($attributes['heading']) . "</{$heading_tag}>";

    // Create the <ul> list and populate it with the key points
    $output = $heading;  // Start with the heading
	$output .= '<ul id="lcp-key-points" style="list-style-type: ' . esc_attr($list_style_type) . ';">';
    foreach ($key_points as $point) {
        // Avoid empty points
        if (!empty($point)) {
            $output .= '<li style="font-size: ' . esc_attr($points_font_size) . ';">' . esc_html($point) . '</li>';
        }
    }
    $output .= '</ul>';

    return $output;
}



// Register the block
function register_key_points_block() {
    register_block_type(__DIR__ . '/build', [
        'editor_script' => 'key-points-editor-script', // Script for the editor
        'render_callback' => 'render_key_points_block', // PHP callback for frontend rendering
    ]);
}
add_action('init', 'register_key_points_block');
