<?php
/*
    Extensions for classic WP Editor (Tinymce)
*/
namespace vc;

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

new editor;
class editor {

    /*
        General constructor
    */
    public function __construct() {
        
        if ( !is_admin() ) return;
        
        // exit if vc is not installed  
        if (!helper::is_active_plugin('js_composer/js_composer.php')) return;    

        add_action( 'init', array(  $this, 'setup_tinymce' ) );
    }
    
    /*
        Define plugins
    */
    public static function get_tinymce_plugins() {
        $uri = get_template_directory_uri().'/assets/js/tinymce/';
        return [
            'areaPlaceholder'   => $uri.'areaPlaceholder.js',
            'uppercase'         => $uri.'uppercase.js',
            'fontFamilyPicker'  => $uri.'fontFamilyPicker.js',
            'fontSizePicker'    => $uri.'fontSizePicker.js',
            'colorPicker'       => $uri.'colorPicker.js'
        ];
    }
    
    public function setup_tinymce() {

        // Check if the logged in WordPress User can edit Posts or Pages
        if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) return;

        // Check if the logged in WordPress User has the Visual Editor enabled
        if ( get_user_option( 'rich_editing' ) !== 'true' ) return;
        
        // apply functions
        add_filter( 'mce_external_plugins', array($this, 'add_plugins') );
        add_filter( 'tiny_mce_before_init', array($this, 'modify_options') );
        add_action( 'wp_tiny_mce_init', array($this, 'add_inline_css') );
    }
    
    /**
     *  Modify tinymce setup
     *  
     *  source: https://developer.wordpress.org/reference/hooks/tiny_mce_before_init/
     */
    public function modify_options ( $options ) {
        $colors = \vc\helper::get_colors_array('tinymce');
        
        // modify default colorpicker
        if ($colors !== false) {
            $options['textcolor_map'] = json_encode( $colors );
            $options['textcolor_cols'] = 9;
        }
        
        for ($i=1; $i <= 4; $i++) {
            $toolbar = isset($options['toolbar'.$i]) ? $options['toolbar'.$i] : '';
            if ( !empty($toolbar) ) {
                // replace default controls
                $toolbar = str_replace( [
                    'fontselect', 'fontsizeselect', 'italic', 'forecolor', 'backcolor', ',wp_more'
                ], [
                    'fontFamilyPicker', 'fontSizePicker', 'italic,uppercase', 'foreColorPicker', 'backColorPicker', ''
                ], $toolbar);
                
                // reorder default
                if ( !is_plugin_active('tinymce-advanced/tinymce-advanced.php') ) {
                    $toolbar = str_replace( [
                        'foreColorPicker', 'formatselect', 'link', 'strikethrough'
                    ], [
                        '', 'formatselect,fontSizePicker', 'foreColorPicker,backColorPicker,link', 'fontFamilyPicker,strikethrough'
                    ], $toolbar);
                }
                $options['toolbar'.$i] = $toolbar;
            }
        }
        // $options['plugins'] .= ',uppercase,fontSizePicker,fontFamilyPicker,colorPicker,areaPlaceholder';
        // debug( $options );
        return $options;
    }
    
    /**
     *  Add tinymce plugins
     *  
     *  source: https://codex.wordpress.org/Plugin_API/Filter_Reference/mce_external_plugins
     */
    public function add_plugins ( $plugins ) {
        return array_merge( $plugins, self::get_tinymce_plugins() );
    }
    
    /**
     *  Add inline css to properly preview the custom classes used inside the plugins
     *  
     *  this function is called via the core hook 'wp_tiny_mce_init' ( source-file: /wp-includes/class-wp-editor.php )
     *  it executes right before the script that calls tinymce.init() is build
     *  the tinymce-parameter 'content_style' is added ( source: https://www.tiny.cloud/docs-4x/configure/content-appearance/#content_style )
     *  this parameter enqueues inline-css and is currently not used by wordpress
     *  
     *  as of WordPress 5.5, there is no other (cleaner) way to do this
     */
    public function add_inline_css () {
        $styles = \vc\helper::get_tinymce_styles();
        echo "
        <script>
            if ( typeof tinyMCEPreInit.mceInit === 'object' && typeof tinyMCEPreInit.mceInit.content === 'object' ) {
                tinyMCEPreInit.mceInit.content['content_style'] = '$styles';
            }
        </script>
        ";
    }
}