<?php
  /**
 * =============================================================================
 * @package         Blue Raven (Free Version)
 * @Plugin URI      https://enterrahost.com/plugins/blue-raven
 * 
 * @Author          EnterraHost
 * @Author URI      https://enterrahost.com
 * @License         GPL-2.0-or-later
 *  
 * @Copyright       (c) 2025 EnterraHost
 * @Contents        RICH TEXT EDITOR
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - TEXT EDITOR
 * || 
 * =============================================================================
 */

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

/*
============================================================
    TEXT EDITOR
============================================================
*/

class Blue_Raven_Text_Editor {
    
    public $TEID; 
    
    public function create($text, $name) {
        $this->TEID = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 5);
        $this->load_blue_raven_text_editor_template($text, $name);
    }
    
    ## =========== Load BR Text Editor HTML =========== ##
    
    private function load_blue_raven_text_editor_template($text, $name) {
        ?>
            <div class='brtxe' id="<?php echo esc_attr($this->TEID); ?>-tools">
                <div class="brtxe-toolbar flex-left">
                    <button data-command="formatBlock" data-value="p">P</button>
                    <button data-command="formatBlock" data-value="h2">H2</button>
                    <button data-command="formatBlock" data-value="h3">H3</button>
                    <button data-command="formatBlock" data-value="h4">H4</button>
                    <span class='spacer-span-2'></span>
                    <button data-command="bold"><b>B</b></button>
                    <button data-command="italic"><i>I</i></button>
                    <button data-command="underline"><u>U</u></button>
                    <span class='spacer-span-2'></span>
                    <button data-command="insertUnorderedList">• List</button>
                    <button data-command="insertOrderedList">1. List</button>
                    <span class='spacer-span-2'></span>
                    <button data-command="justifyLeft" title="Align Left"><i class="fas fa-align-left"></i></button>
                    <button data-command="justifyCenter" title="Align Center"><i class="fas fa-align-center"></i></button>
                    <button data-command="justifyRight" title="Align Right"><i class="fas fa-align-right"></i></button>
                    <span class='spacer-span-2'></span>
                    <button data-command="createLink">Link</button>
                </div>
                
                <input type="hidden" id="<?php echo esc_html($name); ?>-input" name="<?php echo esc_html($name); ?>" value="">
                <div class="brtxe-editor <?php echo esc_attr($this->TEID); ?>-editor" contenteditable="true" id="<?php if(isset($name)){ echo esc_html($name);} ?>" >
                    <?php if(isset($text)){  echo wp_kses_post($text);  } ?>
                </div>
            </div>
        <?php
    }
}