<?php

namespace GriffinForms\Admin\Html\FormControls;

class RichText extends \GriffinForms\Admin\Html\FormControls\Format
{
    protected function isMessageEditor()
    {
        return in_array($this->name, array('content', 'message_content'), true);
    }

    protected function messageEditorSettings()
    {
        return array(
            // Keep TinyMCE from dropping code-mode HTML when switching tabs.
            'verify_html' => false,
            'cleanup' => false,
            'valid_elements' => '*[*]',
            'extended_valid_elements' => '*[*]',
            'custom_elements' => '*[*]',
            'valid_children' => '+*[*]',
        );
    }

    public function html()
    {
        $settings = array(
            'textarea_rows' => 25,
            'teeny' => true,
        );

        if ($this->isMessageEditor()) {
            $settings['wpautop'] = false;
            $settings['tinymce'] = $this->messageEditorSettings();
        }

        \wp_editor((string) $this->value, $this->elementId(), $settings);
    }
}
