<?php
/*
Plugin Name: Postology
Description: Boost reader engagement with Postology! Add an attractive widget at the top of each blog article, allowing users to summarize key points in just a few bullets.
Version: 1.3
Author: Postology
Text Domain: postology
Domain Path: /languages
License:     GPL-2.0-or-later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

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

// Check if Pro version is active
if ( ! function_exists( 'is_plugin_active' ) ) {
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$pro_plugin = 'postology-pro/postology-plugin.php';
if ( is_plugin_active( $pro_plugin ) ) {
    return; // Do nothing if Pro plugin is active
}

/**
 * Include the settings page
 */
require_once plugin_dir_path(__FILE__) . '/includes/settings-page.php';

class Postology {
    /**
     * Constructor to initialize hooks
     */
    public function __construct() {
        // Activation hook to create analytics database table
        register_activation_hook(__FILE__, array('Postology', 'on_activation'));

        // Enqueue scripts and styles
        add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));

        // Add the Postology box to the content
        add_filter('the_content', array($this, 'add_box'));

        // AJAX handlers for clicks and generating summary
        add_action('wp_ajax_generate_summary', array($this, 'ajax_handler'));
        add_action('wp_ajax_nopriv_generate_summary', array($this, 'ajax_handler'));
    }
    /**
     * Activation routine for the Pro plugin
     */
    public static function on_activation() {
        // Continue with other activation tasks (e.g., creating tables).
        self::create_user();
    }

    /**
     * Enqueue scripts and styles
     */
    public function enqueue_scripts() {
        $widget_visibility = get_option('postology_widget_visibility', true);
        if (!$widget_visibility) {
            return; // Do not enqueue the scripts if the widget is not visible.
        }
        
        wp_enqueue_style('postology-style', plugin_dir_url(__FILE__) . '/assets/css/postology-style.css', array(), '1.0.0');
        wp_enqueue_script('postology-script', plugin_dir_url(__FILE__) . '/assets/js/postology.js', array('jquery'), '1.0.0', true);

        wp_localize_script('postology-script', 'postologySettings', array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('postology_nonce'),
            'selectors' => get_option('postology_selectors', 'main, .post-content, .entry-content, .content, .article, .blog-post, .post, .single-post'),
            'find_elements' => get_option('postology_find_elements', 'p, h2, h3, h4, h5, h6'),
            'excluded_classes' => get_option('postology_excluded_classes', '.related-posts, .ads-section, .banner, .comments'), // Passing excluded classes
            'number_of_bullets' => get_option('postology_number_of_bullets', 2),
            'summary_heading_text' => get_option('postology_summary_heading_text', 'AI-generated article summary'),
        ));
    }

    /**
     * Add the Postology box to the content
     */
    public function add_box($content) {
        // Retrieve the setting value; if false then widget should not display.
        $widget_visibility = get_option('postology_widget_visibility', true);

        // If the user has disabled the widget, return the original content.
        if (!$widget_visibility) {
            return $content;
        }

        if (is_single()) {
            $excluded_patterns = get_option('postology_excluded_urls', '');
            $current_url = wp_parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
            $excluded_patterns_array = array_filter(array_map('trim', explode("\n", $excluded_patterns)));

            foreach ($excluded_patterns_array as $pattern) {
                if (strpos($current_url, $pattern) !== false) {
                    return $content; // Skip adding the widget if URL matches
                }
            }

            // If not excluded, add the Postology box
            $heading_text = get_option('postology_heading_text', 'Get an AI-generated summary of this article.');
            $button_text = get_option('postology_button_text', 'Generate Summary');
            $powered_by_option = get_option('postology_display_powered_by', "no");

            ob_start();
            ?>
            <postology-el
                data-heading-text="<?php echo esc_attr( $heading_text ); ?>"
                data-button-text="<?php echo esc_attr( $button_text ); ?>"
                data-powered-by="<?php echo esc_attr( $powered_by_option ); ?>"
            >
                <div id="postology-box" style="<?php echo esc_attr($this->get_style('main_container')); ?>">
                    <div class="postology-header" style="<?php echo esc_attr($this->get_style('header_text')); ?>">
                        <svg width="29" height="31" viewBox="0 0 29 31" fill="none" xmlns="http://www.w3.org/2000/svg"
                        class="postology-box-icon">
                            <g filter="url(#filter0_dd_5_106)">
                            <path class="fill-path" d="M20.5398 10.7576C20.4742 10.7576 20.4185 10.7445 20.3725 10.7183C20.3266 10.6921 20.2905 10.6462 20.2643 10.5807L19.6936 9.24382L18.2569 8.61472C18.1913 8.5885 18.1454 8.55246 18.1192 8.50659C18.0929 8.46072 18.0798 8.40502 18.0798 8.33948C18.0798 8.27395 18.0929 8.21825 18.1192 8.17238C18.1454 8.12651 18.1913 8.09046 18.2569 8.06425L19.6936 7.45481L20.2643 6.17694C20.2905 6.1114 20.3266 6.06553 20.3725 6.03932C20.4185 6.01311 20.4742 6 20.5398 6C20.6054 6 20.6612 6.01311 20.7071 6.03932C20.753 6.06553 20.7891 6.1114 20.8153 6.17694L21.386 7.45481L22.8227 8.06425C22.8883 8.09046 22.9342 8.12651 22.9604 8.17238C22.9867 8.21825 22.9998 8.27395 22.9998 8.33948C22.9998 8.40502 22.9867 8.46072 22.9604 8.50659C22.9342 8.55246 22.8883 8.5885 22.8227 8.61472L21.386 9.24382L20.8153 10.5807C20.7891 10.6462 20.753 10.6921 20.7071 10.7183C20.6612 10.7445 20.6054 10.7576 20.5398 10.7576ZM20.5398 22.4747C20.4873 22.4747 20.4349 22.4616 20.3824 22.4354C20.3299 22.4092 20.2905 22.3633 20.2643 22.2977L19.6936 21.0199L18.2766 20.4104C18.211 20.3842 18.1651 20.3482 18.1389 20.3023C18.1126 20.2564 18.0995 20.2007 18.0995 20.1352C18.0995 20.0697 18.1126 20.014 18.1389 19.9681C18.1651 19.9222 18.211 19.8862 18.2766 19.86L19.6936 19.2505L20.2643 17.894C20.2905 17.8285 20.3266 17.7826 20.3725 17.7564C20.4185 17.7302 20.4742 17.7171 20.5398 17.7171C20.6054 17.7171 20.6612 17.7302 20.7071 17.7564C20.753 17.7826 20.7891 17.8285 20.8153 17.894L21.386 19.2505L22.803 19.86C22.8686 19.8862 22.9145 19.9222 22.9408 19.9681C22.967 20.014 22.9801 20.0697 22.9801 20.1352C22.9801 20.2007 22.967 20.2564 22.9408 20.3023C22.9145 20.3482 22.8686 20.3842 22.803 20.4104L21.386 21.0199L20.8153 22.2977C20.7891 22.3633 20.7497 22.4092 20.6973 22.4354C20.6448 22.4616 20.5923 22.4747 20.5398 22.4747ZM11.9003 19.0736C11.7953 19.0736 11.6937 19.0441 11.5953 18.9851C11.4969 18.9261 11.4214 18.8442 11.369 18.7394L10.0898 16.0067L7.33456 14.7681C7.2296 14.7157 7.1476 14.6404 7.08856 14.5421C7.02952 14.4438 7 14.3422 7 14.2373C7 14.1325 7.02952 14.0309 7.08856 13.9326C7.1476 13.8343 7.2296 13.759 7.33456 13.7065L10.0898 12.468L11.369 9.75497C11.4214 9.63701 11.4969 9.54854 11.5953 9.48957C11.6937 9.43059 11.7953 9.4011 11.9003 9.4011C12.0053 9.4011 12.1069 9.43059 12.2053 9.48957C12.3037 9.54854 12.3792 9.63046 12.4317 9.73531L13.7305 12.468L16.4661 13.7065C16.5841 13.759 16.6727 13.8343 16.7317 13.9326C16.7908 14.0309 16.8203 14.1325 16.8203 14.2373C16.8203 14.3422 16.7908 14.4438 16.7317 14.5421C16.6727 14.6404 16.5841 14.7157 16.4661 14.7681L13.7305 16.0067L12.4317 18.7394C12.3792 18.8573 12.3037 18.9425 12.2053 18.9949C12.1069 19.0474 12.0053 19.0736 11.9003 19.0736Z"
                            style="<?php echo esc_attr($this->get_style('icon_container')); ?>" />
                            </g>
                            <defs>
                            <filter id="filter0_dd_5_106" x="0.633232" y="0.438226" width="28.0017" height="29.5741" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
                            <feFlood flood-opacity="0" result="BackgroundImageFix"/>
                            <feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>
                            <feOffset dx="-1.31726" dy="2.48816"/>
                            <feGaussianBlur stdDeviation="2.52475"/>
                            <feComposite in2="hardAlpha" operator="out"/>
                            <feColorMatrix type="matrix" values="0 0 0 0 0.0506089 0 0 0 0 0.0159861 0 0 0 0 0.191833 0 0 0 0.25 0"/>
                            <feBlend mode="normal" in2="BackgroundImageFix" result="effect1_dropShadow_5_106"/>
                            <feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>
                            <feOffset dx="1.24408" dy="-1.1709"/>
                            <feGaussianBlur stdDeviation="2.19544"/>
                            <feComposite in2="hardAlpha" operator="out"/>
                            <feColorMatrix type="matrix" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0.35 0"/>
                            <feBlend mode="normal" in2="effect1_dropShadow_5_106" result="effect2_dropShadow_5_106"/>
                            <feBlend mode="normal" in="SourceGraphic" in2="effect2_dropShadow_5_106" result="shape"/>
                            </filter>
                            </defs>
                        </svg>
                        <?php echo esc_html($heading_text); ?>
                    </div>
                    <button id="generate-summary" aria-label="<?php echo esc_html($button_text); ?>" style="<?php echo esc_attr($this->get_style('button_text')); ?>">
                        <?php echo esc_html($button_text); ?>
                    </button>
                </div>
                <div id="summary-content" style="<?php echo esc_attr($this->get_style('inner_container')); ?>">
                    <section id="summary-title" style="<?php echo esc_attr($this->get_style('summary_heading_text')); ?>">
                        <svg width="29" height="31" viewBox="0 0 29 31" fill="none" xmlns="http://www.w3.org/2000/svg"
                        class="postology-box-icon">
                            <g filter="url(#filter0_dd_5_106)">
                            <path class="fill-path" d="M20.5398 10.7576C20.4742 10.7576 20.4185 10.7445 20.3725 10.7183C20.3266 10.6921 20.2905 10.6462 20.2643 10.5807L19.6936 9.24382L18.2569 8.61472C18.1913 8.5885 18.1454 8.55246 18.1192 8.50659C18.0929 8.46072 18.0798 8.40502 18.0798 8.33948C18.0798 8.27395 18.0929 8.21825 18.1192 8.17238C18.1454 8.12651 18.1913 8.09046 18.2569 8.06425L19.6936 7.45481L20.2643 6.17694C20.2905 6.1114 20.3266 6.06553 20.3725 6.03932C20.4185 6.01311 20.4742 6 20.5398 6C20.6054 6 20.6612 6.01311 20.7071 6.03932C20.753 6.06553 20.7891 6.1114 20.8153 6.17694L21.386 7.45481L22.8227 8.06425C22.8883 8.09046 22.9342 8.12651 22.9604 8.17238C22.9867 8.21825 22.9998 8.27395 22.9998 8.33948C22.9998 8.40502 22.9867 8.46072 22.9604 8.50659C22.9342 8.55246 22.8883 8.5885 22.8227 8.61472L21.386 9.24382L20.8153 10.5807C20.7891 10.6462 20.753 10.6921 20.7071 10.7183C20.6612 10.7445 20.6054 10.7576 20.5398 10.7576ZM20.5398 22.4747C20.4873 22.4747 20.4349 22.4616 20.3824 22.4354C20.3299 22.4092 20.2905 22.3633 20.2643 22.2977L19.6936 21.0199L18.2766 20.4104C18.211 20.3842 18.1651 20.3482 18.1389 20.3023C18.1126 20.2564 18.0995 20.2007 18.0995 20.1352C18.0995 20.0697 18.1126 20.014 18.1389 19.9681C18.1651 19.9222 18.211 19.8862 18.2766 19.86L19.6936 19.2505L20.2643 17.894C20.2905 17.8285 20.3266 17.7826 20.3725 17.7564C20.4185 17.7302 20.4742 17.7171 20.5398 17.7171C20.6054 17.7171 20.6612 17.7302 20.7071 17.7564C20.753 17.7826 20.7891 17.8285 20.8153 17.894L21.386 19.2505L22.803 19.86C22.8686 19.8862 22.9145 19.9222 22.9408 19.9681C22.967 20.014 22.9801 20.0697 22.9801 20.1352C22.9801 20.2007 22.967 20.2564 22.9408 20.3023C22.9145 20.3482 22.8686 20.3842 22.803 20.4104L21.386 21.0199L20.8153 22.2977C20.7891 22.3633 20.7497 22.4092 20.6973 22.4354C20.6448 22.4616 20.5923 22.4747 20.5398 22.4747ZM11.9003 19.0736C11.7953 19.0736 11.6937 19.0441 11.5953 18.9851C11.4969 18.9261 11.4214 18.8442 11.369 18.7394L10.0898 16.0067L7.33456 14.7681C7.2296 14.7157 7.1476 14.6404 7.08856 14.5421C7.02952 14.4438 7 14.3422 7 14.2373C7 14.1325 7.02952 14.0309 7.08856 13.9326C7.1476 13.8343 7.2296 13.759 7.33456 13.7065L10.0898 12.468L11.369 9.75497C11.4214 9.63701 11.4969 9.54854 11.5953 9.48957C11.6937 9.43059 11.7953 9.4011 11.9003 9.4011C12.0053 9.4011 12.1069 9.43059 12.2053 9.48957C12.3037 9.54854 12.3792 9.63046 12.4317 9.73531L13.7305 12.468L16.4661 13.7065C16.5841 13.759 16.6727 13.8343 16.7317 13.9326C16.7908 14.0309 16.8203 14.1325 16.8203 14.2373C16.8203 14.3422 16.7908 14.4438 16.7317 14.5421C16.6727 14.6404 16.5841 14.7157 16.4661 14.7681L13.7305 16.0067L12.4317 18.7394C12.3792 18.8573 12.3037 18.9425 12.2053 18.9949C12.1069 19.0474 12.0053 19.0736 11.9003 19.0736Z"
                            style="<?php echo esc_attr($this->get_style('icon_container')); ?>" />
                            </g>
                            <defs>
                            <filter id="filter0_dd_5_106" x="0.633232" y="0.438226" width="28.0017" height="29.5741" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
                            <feFlood flood-opacity="0" result="BackgroundImageFix"/>
                            <feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>
                            <feOffset dx="-1.31726" dy="2.48816"/>
                            <feGaussianBlur stdDeviation="2.52475"/>
                            <feComposite in2="hardAlpha" operator="out"/>
                            <feColorMatrix type="matrix" values="0 0 0 0 0.0506089 0 0 0 0 0.0159861 0 0 0 0 0.191833 0 0 0 0.25 0"/>
                            <feBlend mode="normal" in2="BackgroundImageFix" result="effect1_dropShadow_5_106"/>
                            <feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>
                            <feOffset dx="1.24408" dy="-1.1709"/>
                            <feGaussianBlur stdDeviation="2.19544"/>
                            <feComposite in2="hardAlpha" operator="out"/>
                            <feColorMatrix type="matrix" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0.35 0"/>
                            <feBlend mode="normal" in2="effect1_dropShadow_5_106" result="effect2_dropShadow_5_106"/>
                            <feBlend mode="normal" in="SourceGraphic" in2="effect2_dropShadow_5_106" result="shape"/>
                            </filter>
                            </defs>
                        </svg>
                        <p><?php echo esc_html(get_option('postology_summary_heading_text', 'AI-generated article summary')); ?></p>
                    </section>
                    <div id="summary-content-placeholder" style="<?php echo esc_attr($this->get_style('summary_background')); ?>">
                        <div id="loading-placeholder">
                            <div class="loading-line"></div>
                            <div class="loading-line"></div>
                            <div class="loading-line"></div>
                            <div class="loading-line"></div>
                        </div>
                    </div>                    
                    <!-- "Powered by Postology" hidden initially -->
                    <?php if ($powered_by_option == "on"): ?>
                        <div class="postology-powered-by" style="<?php echo esc_attr($this->get_style('powered_by')); ?>">Postology</div>
                    <?php endif; ?>
                </div>
            </postology-el>
            <?php
            $postology_box = ob_get_clean();

            return $postology_box . $content;
        }
        return $content;
    }


    /**
     * AJAX handler for generating the summary
     */
    public function ajax_handler() {
        // Verify the nonce for security.
        check_ajax_referer('postology_nonce', 'nonce');

        // Get the article content from the AJAX request.
        $article_content = isset($_POST['content']) ? sanitize_textarea_field(wp_unslash($_POST['content'])) : '';

        // Retrieve settings from the database.
        $model              = get_option('postology_model', 'gpt-4o-mini');
        $number_of_bullets  = intval(2);
        $max_tokens         = intval(get_option('postology_max_tokens', 1000));
        $bullet_point_length = get_option('postology_bullet_point_length', 'short');
        $summary_tone       = get_option('postology_summary_tone', 'informative');
        $summary_language   = get_option('postology_summary_language', 'default');

        // Determine the length description based on the user's selection.
        $length_descriptions = array(
            'short'  => 'brief',
            'medium' => 'concise',
            'long'   => 'detailed',
        );
        $length_description = isset($length_descriptions[$bullet_point_length]) ? $length_descriptions[$bullet_point_length] : 'concise';

        // Capitalize the tone for use in the prompt.
        $summary_tone = ucfirst(sanitize_text_field($summary_tone));

        // Determine the language to use in the summary.
        if ('default' === $summary_language) {
            // Attempt to detect the post's language.
            $post_language = get_bloginfo('language'); // e.g., 'en-US'
            $language_code = substr($post_language, 0, 2);
            $language_map  = array(
                'en' => 'English',
                'es' => 'Spanish',
                'fr' => 'French',
                'de' => 'German',
                'it' => 'Italian',
                'pt' => 'Portuguese',
                'zh' => 'Chinese',
                'ja' => 'Japanese',
            );
            $summary_language = isset($language_map[$language_code]) ? $language_map[$language_code] : 'English';
        } else {
            // Use the user-selected language, capitalize it for the prompt.
            $summary_language = ucfirst(sanitize_text_field($summary_language));
        }

        // Build the prompt for the API.
        $prompt = sprintf(
            "Please summarize the following article into %d bullet points, providing %s descriptions for each point in a %s tone. The summary should be written in %s. Do not include any bold text, headings, colons, or special formatting; only provide plain text bullet points. Each bullet point should be a complete sentence. Focus only on the main article content below and ignore any unrelated sections such as navigation menus, advertisements, related posts, footers, headers, or comments.\n\nMain article content:\n%s",
            $number_of_bullets,
            $length_description,
            $summary_tone,
            $summary_language,
            $article_content
        );

        // Prepare the data for the API request.
        $data = array(
            'model'    => sanitize_text_field($model),
            'messages' => array(
                array(
                    'role'    => 'system',
                    'content' => sprintf("You are a helpful assistant that summarizes articles into bullet points using a %s tone.", strtolower($summary_tone)),
                ),
                array(
                    'role'    => 'user',
                    'content' => $prompt,
                ),
            ),
            'max_tokens'  => $max_tokens,
            'temperature' => 0.5,
        );

        $client_domain = site_url(); // Gets the site's URL, e.g., https://example.com
        // $endpoint = 'http://localhost/postology/wp-json/credit-manager/chat';
        $endpoint = 'https://postology-pro.com/wp-json/credit-manager/chat';

        // Include the license key in the data
        $data['client_domain'] = $client_domain;

        $headers = array(
            'Content-Type'  => 'application/json',
        );

        // Make the API request
        $response = wp_remote_post(
            $endpoint,
            array(
                'headers' => $headers,
                'body'    => wp_json_encode($data),
                'timeout' => 60,
            )
        );

        // Handle the response
        if (is_wp_error($response)) {
            wp_send_json_error('Error: ' . esc_html($response->get_error_message()));
        } else {
            $body   = wp_remote_retrieve_body($response);
            $result = json_decode($body, true);

            if (isset($result["openai_response"]['choices'][0]['message']['content'])) {
                $summary = $result["openai_response"]['choices'][0]['message']['content'];
                $bullets = explode("\n", $summary);
                $output  = '<ul style="' . esc_attr($this->get_style('bullet_points_text')) . '">';

                foreach ($bullets as $bullet) {
                    $bullet = trim($bullet, "- \t\n\r\0\x0B");
                    if ($bullet) {
                        $output .= '<li>' . esc_html($bullet) . '</li>';
                    }
                }

                $output .= '</ul>';
                
                wp_send_json_success($output);
            } else {
                // Handle possible API error message.
                if (isset($result['error']['message'])) {
                    wp_send_json_error('Error from API: ' . esc_html($result['error']['message']));
                } else {
                    wp_send_json_error('No summary available.');
                }
            }
        }

        wp_die(); // All AJAX handlers should die when finished.
    }

    /**
     * Helper function to get inline styles for different elements
     */
    public function get_style($element) {
        $styles = array();

        switch ($element) {
            case 'powered_by':
                $styles['color'] = get_option('postology_header_font_color', '#000000');
                break;
            case 'header_text':
                $styles['font-size'] = get_option('postology_header_font_size', '14px');
                $styles['font-family'] = get_option('postology_header_font_family', 'inherit');
                $styles['font-weight'] = get_option('postology_header_font_weight', 'bold');
                $styles['color'] = get_option('postology_header_font_color', '#000000');
                $styles['line-height'] = get_option('postology_header_line_height', '1.5');
                break;
            case 'summary_heading_text':
                $styles['font-size'] = get_option('postology_summary_heading_font_size', '16px');
                $styles['font-family'] = get_option('postology_summary_heading_font_family', 'inherit');
                $styles['font-weight'] = get_option('postology_summary_heading_font_weight', 'bold');
                $styles['color'] = get_option('postology_summary_heading_font_color', '#000000');
                $styles['line-height'] = get_option('postology_summary_heading_line_height', '1.5');
                break;
            case 'summary_background':
                $styles['background-color'] = get_option('postology_summary_background_color', '#a3a3a3');
                break;
            case 'button_text':
                $styles['font-size'] = get_option('postology_button_font_size', '14px');
                $styles['font-family'] = get_option('postology_button_font_family', 'inherit');
                $styles['font-weight'] = get_option('postology_button_font_weight', '600');
                $styles['color'] = get_option('postology_button_font_color', '#000000');
                $styles['line-height'] = get_option('postology_button_line_height', '1.5');
                $styles['background-color'] = get_option('postology_button_background_color', '#FFFFFF');
                $styles['border'] = '1px solid ' . get_option('postology_button_border_color', '#E2E8F0');
                $styles['border-radius'] = get_option('postology_button_border_radius', '9px');
                break;
            case 'bullet_points_text':
                $styles['font-size'] = get_option('postology_bullet_points_font_size', '16px');
                $styles['font-family'] = get_option('postology_bullet_points_font_family', 'inherit');
                $styles['font-weight'] = get_option('postology_bullet_points_font_weight', 'normal');
                $styles['color'] = get_option('postology_bullet_points_font_color', '#000000');
                $styles['line-height'] = get_option('postology_bullet_points_line_height', '1.5');
                break;
            case 'main_container':
                $styles['background-color'] = get_option('postology_main_container_background_color', '#FFFFFF');
                $styles['border-radius'] = get_option('postology_main_container_border_radius', '9px');
                $styles['border'] = '0px solid ' . get_option('postology_main_container_border_color', '#000000');
                $styles['box-shadow'] = get_option('postology_main_container_border_shadow', '3px 12px 15px 3px rgba(0, 0, 0, 0.1)');
                break;
            case 'inner_container':
                $styles['background-color'] = get_option('postology_inner_container_background_color', '#FFFFFF');
                $styles['border-radius'] = get_option('postology_inner_container_border_radius', '9px');
                $styles['border'] = '0px solid ' . get_option('postology_inner_container_border_color', '#000000');
                $styles['box-shadow'] = get_option('postology_inner_container_border_shadow', '3px 12px 15px 3px rgba(0, 0, 0, 0.1)');
                break;
            case 'icon_container':
                $styles['fill'] = get_option('postology_box_icon_color', '#0088FF');
                break;
            default:
                break;
        }

        $inline_style = '';
        foreach ($styles as $property => $value) {
            if (!empty($value)) {
                $inline_style .= $property . ': ' . esc_attr($value) . '; ';
            }
        }
        return $inline_style;
    }

    // 
    public static function create_user() {
        $client_domain = site_url(); // Gets the site's URL, e.g., https://example.com
        // $endpoint = 'http://localhost/postology/wp-json/credit-manager/chat';
        $endpoint = 'https://postology-pro.com/wp-json/credit-manager/create-free-user';

        // Prepare the data for the API request.
        $data = array();
        // Include the license key in the data
        $data['client_domain'] = $client_domain;

        $headers = array(
            'Content-Type'  => 'application/json',
        );

        // Make the API request
        $response = wp_remote_post(
            $endpoint,
            array(
                'headers' => $headers,
                'body'    => wp_json_encode($data),
                'timeout' => 60,
            )
        );
    }
}


// Instantiate the Postology class
new Postology();
