<?php
/*
    File: Gutenberg/Block-Editor Functions
    Comments: -//-
*/
if( ! defined( 'ABSPATH' ) ) exit;

new blocks();
class blocks {

    /**
     * Holds the blocks plugins metadata
     */
    public $plugins = array(
        'gutenberg' => array(
            'name'                  => 'Gutenberg',
            'slug'                  => 'gutenberg',
            'required'              => true,
            'version'               => '16.9.0',
            'force_activation'      => true,
            'force_deactivation'    => true,
            'external_url'          => 'https://wordpress.org/plugins/gutenberg/',
            'default'               => true
        ),
        'greyd_blocks' => array(
            'name'                  => 'GREYD.Blocks',
            'slug'                  => 'greyd_blocks',
            'source'                => 'https://update.greyd.io/review/plugins/greyd_blocks/greyd_blocks.zip',
            'required'              => true,
            'version'               => '1.8.2',
            'force_activation'      => true,
            'force_deactivation'    => true,
            'default'               => true
        )
    );

    public function __construct() {
        // add theme supports for blocks
        add_action( 'after_setup_theme', array($this, 'theme_setup_blocks'), 3 );
        // modify required plugins
        add_filter( 'greyd_required_plugins', array($this, 'mod_required_plugins') );
        
        // modify update message
        add_action( "in_plugin_update_message-gutenberg/gutenberg.php", array($this, 'gutenberg_update_message'), 10, 2 );

        // add error notice when gutenberg makes problems...
        add_action( "admin_notices", array( $this, 'gutenberg_incompatiblity_notice' ) );
        add_action( "network_admin_notices", array( $this, 'gutenberg_incompatiblity_notice' ) );
    }

    public function theme_setup_blocks() {
        // remove_theme_support( 'block-templates' );
        // add_theme_support( 'block-templates' );
        remove_theme_support( 'widgets-block-editor' );
        // add_theme_support( 'wp-block-styles' ); 
        add_theme_support( 'responsive-embeds' );
        add_theme_support( 'custom-spacing' ); 
        add_theme_support( 'custom-units' );
        // add_theme_support( 'custom-line-height' );
        // add_theme_support( 'align-wide' );
        // add_theme_support( 'align-full' );
        
        add_theme_support( 'editor-color-palette', $this->get_color_palette() );
        add_theme_support( 'editor-gradient-presets', $this->get_gradient_presets() );
        add_theme_support( 'editor-font-sizes', $this->get_font_sizes() );

    }
    
    public static function get_color_palette() {
        return array(

            // Dunkel
            array(
                'name' => __('Dunkel - Sehr dunkel', 'core'),
                'slug' => 'color-31',
                'color' => get_theme_mod('color31', '#202221'),
            ),
            array(
                'name' => __('Dunkel - Dunkel', 'core'),
                'slug' => 'color-21',
                'color' => get_theme_mod('color21', '#393d3d'),
            ),
            array(
                'name' => __('Dunkel - Leicht dunkel', 'core'),
                'slug' => 'color-32',
                'color' => get_theme_mod('color32', '#777777'),
            ),

            // Hell
            array(
                'name' => __('Hell - Leicht hell', 'core'),
                'slug' => 'color-22',
                'color' => get_theme_mod('color22', '#cfcfcf'),
            ),
            array(
                'name' => __('Hell - Hell', 'core'),
                'slug' => 'color-33',
                'color' => get_theme_mod('color33', '#ececec'),
            ),
            array(
                'name' => __('Hell - Sehr hell', 'core'),
                'slug' => 'color-23',
                'color' => get_theme_mod('color23', '#ffffff'),
            ),
            
            // Branding
            array(
                'name' => __('Branding - Primär', 'core'),
                'slug' => 'color-11',
                'color' => get_theme_mod('color11', '#f0c432'),
            ),
            array(
                'name' => __('Branding - Sekundär', 'core'),
                'slug' => 'color-12',
                'color' => get_theme_mod('color12', '#fbe853'),
            ),
            array(
                'name' => __('Branding - Alternative', 'core'),
                'slug' => 'color-13',
                'color' => get_theme_mod('color13', '#ffd64e'),
            ),

            // Grundfarben
            array(
                'name' => __('Grundfarben - Schwarz', 'core'),
                'slug' => 'color-61',
                'color' => get_theme_mod('color61', '#000000'),
            ),
            array(
                'name' => __('Grundfarben - Weiß', 'core'),
                'slug' => 'color-62',
                'color' => get_theme_mod('color62', '#ffffff'),
            ),
            array(
                'name' => __('Grundfarben - Transparenz', 'core'),
                'slug' => 'color-63',
                'color' => get_theme_mod('color63', 'transparent'),
            ),

            // Signalfarben
            array(
                'name' => __('Signalfarben - Hinweis dunkel', 'core'),
                'slug' => 'color-41',
                'color' => get_theme_mod('color41', '#31708f'),
            ),
            array(
                'name' => __('Signalfarben - Hinweis hell', 'core'),
                'slug' => 'color-51',
                'color' => get_theme_mod('color51', '#cdeef7'),
            ),
            array(
                'name' => __('Signalfarben - Erfolg dunkel', 'core'),
                'slug' => 'color-42',
                'color' => get_theme_mod('color42', '#328566'),
            ),
            array(
                'name' => __('Signalfarben - Erfolg hell', 'core'),
                'slug' => 'color-52',
                'color' => get_theme_mod('color52', '#daf5de'),
            ),
            array(
                'name' => __('Signalfarben - Fehler dunkel', 'core'),
                'slug' => 'color-43',
                'color' => get_theme_mod('color43', '#b11500'),
            ),
            array(
                'name' => __('Signalfarben - Fehler hell', 'core'),
                'slug' => 'color-53',
                'color' => get_theme_mod('color53', '#ffe4e4'),
            ),

        );
    }
    public static function get_gradient_presets() {
        $color11 = get_theme_mod('color11', '#f0c432'); // primary
        $color12 = get_theme_mod('color12', '#fbe853'); // secondary
        $color13 = get_theme_mod('color13', '#ffd64e'); // alternate
        $color31 = get_theme_mod('color31', '#202221'); // very dark
        $color21 = get_theme_mod('color21', '#393d3d'); // dark
        $color32 = get_theme_mod('color32', '#777777'); // slightly dark
        $color22 = get_theme_mod('color22', '#cfcfcf'); // slightly bright
        $color33 = get_theme_mod('color33', '#ececec'); // bright
        $color23 = get_theme_mod('color23', '#ffffff'); // very bright
        $color61 = get_theme_mod('color61', '#000000'); // black
        $color62 = get_theme_mod('color62', '#ffffff'); // white
        $color63 = get_theme_mod('color63', 'transparent'); // transparent

        return array(
            array(
                "name"     => __( "Primär zu Sekundär", "core" ),
                "gradient" => "linear-gradient(130deg,{$color11} 0%,{$color12} 100%)",
                "slug"     => "color11-to-color12"
            ),
            array(
                "name"     => __( "Primär zu Sekundär zu Alternativ", "core" ),
                "gradient" => "linear-gradient(130deg,{$color11} 0%,{$color12} 50%,{$color13} 100%)",
                "slug"     => "color11-to-color13"
            ),
            array(
                "name"     => __( "Sekundär zu Alternativ", "core" ),
                "gradient" => "linear-gradient(130deg,{$color12} 0%,{$color13} 100%)",
                "slug"     => "color12-to-color13"
            ),
            array(
                "name"     => __( "Primär zu Sekundär zu Alternativ zu Sehr dunkel", "core" ),
                "gradient" => "linear-gradient(130deg,{$color11} 0%,{$color12} 33%,{$color13} 66%,{$color31} 100%)",
                "slug"     => "color11-to-color31"
            ),
            array(
                "name"     => __( "Schrittweißer Branding Verlauf", "core" ),
                "gradient" => "linear-gradient(130deg,{$color11} 0%,{$color12} 50%,{$color11} 50%,{$color12} 75%,{$color13} 75%,{$color12} 100%)",
                "slug"     => "branding-steps"
            ),
            array(
                "name"     => __( "Experimenteller Branding Verlauf", "core" ),
                "gradient" => "linear-gradient(130deg,{$color13} 50%,{$color12} 50%,{$color12} 60%,{$color11} 60%,{$color11} 70%,{$color12} 80%,{$color13} 80%,{$color13} 90%,{$color11} 90%)",
                "slug"     => "branding-experiment"
            ),

            array(
                "name"     => __( "Weiß zu Hell", "core" ),
                "gradient" => "linear-gradient(130deg,{$color62} 0%,{$color22} 100%)",
                "slug"     => "color62-to-color22"
            ),
            array(
                "name"     => __( "Hell zu Leicht dunkel", "core" ),
                "gradient" => "linear-gradient(130deg,{$color23} 0%,{$color22} 50%,{$color32} 100%)",
                "slug"     => "color23-to-color32"
            ),
            array(
                "name"     => __( "Weiß zu Dunkel", "core" ),
                "gradient" => "linear-gradient(130deg,{$color62} 0%,{$color33} 33%,{$color32} 66%,{$color21} 100%)",
                "slug"     => "color62-to-color21"
            ),
            array(
                "name"     => __( "Hell zu Sehr dunkel", "core" ),
                "gradient" => "linear-gradient(130deg,{$color33} 0%,{$color21} 66%,{$color31} 100%)",
                "slug"     => "color33-to-color31"
            ),
            array(
                "name"     => __( "Schrittweiße Dunkler", "core" ),
                "gradient" => "linear-gradient(130deg,{$color23} 0%,{$color22} 50%,{$color33} 50%,{$color22} 75%,{$color32} 75%,{$color21} 100%)",
                "slug"     => "darken-steps"
            ),
            array(
                "name"     => __( "Transparent zu Weiß", "core" ),
                "gradient" => "linear-gradient(180deg,{$color63} 0%,{$color62} 100%)",
                "slug"     => "color63-to-color62"
            ),

            array(
                'name'     => __( 'Primär nach Sekundär', 'core' ),
                'gradient' => 'linear-gradient(145deg,'.$color11.' 0%,'.$color12.' 100%)',
                'slug'     => 'color-11-to-color-12-se'
            ),
            array(
                'name'     => __( 'Primär nach Sekundär', 'core' ),
                'gradient' => 'linear-gradient(202deg,'.$color11.' 0%,'.$color12.' 100%)',
                'slug'     => 'color-11-to-color-12-sw'
            ),
            array(
                'name'     => __( 'Primär nach Sekundär', 'core' ),
                'gradient' => 'linear-gradient(29deg,'.$color11.' 0%,'.$color12.' 100%)',
                'slug'     => 'color-11-to-color-12-ne'
            ),

            array(
                'name'     => __( 'Sehr dunkel nach Transparenz', 'core' ),
                'gradient' => 'linear-gradient(180deg,'.$color31.' 0%,'.$color63.' 100%)',
                'slug'     => 'color-31-to-color-63-s'
            ),
            array(
                'name'     => __( 'Dunkel nach Sehr dunkel', 'core' ),
                'gradient' => 'linear-gradient(29deg,'.$color21.' 0%,'.$color31.' 100%)',
                'slug'     => 'color-21-to-color-31-ne'
            ),
            array(
                'name'     => __( 'Leicht dunkel nach Sehr hell', 'core' ),
                'gradient' => 'linear-gradient(45deg,'.$color32.' 0%,'.$color23.' 100%)',
                'slug'     => 'color-32-to-color-23-ne'
            ),

            array(
                'name'     => __( 'Hell nach Weiß', 'core' ),
                'gradient' => 'linear-gradient(0deg,'.$color33.' 0%,'.$color62.' 100%)',
                'slug'     => 'color-33-to-color-62-n'
            ),
            array(
                'name'     => __( 'Hell nach Sehr hell', 'core' ),
                'gradient' => 'linear-gradient(180deg,'.$color33.' 49.99%,'.$color23.' 50%)',
                'slug'     => 'color-33-to-color-23-s'
            ),

        );
    }
    public static function get_font_sizes() {
        return array(

            array(
                'name' => __('H1', 'core'),
                'slug' => 'h-1',
                'size' => get_theme_mod('font_size', 16)*get_theme_mod('h1_font_size', 2)
            ),
            array(
                'name' => __('H2', 'core'),
                'slug' => 'h-2',
                'size' => get_theme_mod('font_size', 16)*get_theme_mod('h2_font_size', 1.8)
            ),
            array(
                'name' => __('H3', 'core'),
                'slug' => 'h-3',
                'size' => get_theme_mod('font_size', 16)*get_theme_mod('h3_font_size', 1.3)
            ),
            array(
                'name' => __('H4', 'core'),
                'slug' => 'h-4',
                'size' => get_theme_mod('font_size', 16)*get_theme_mod('h4_font_size', 1.3)
            ),
            array(
                'name' => __('H5', 'core'),
                'slug' => 'h-5',
                'size' => get_theme_mod('font_size', 16)*get_theme_mod('h5_font_size', 1)
            ),
            array(
                'name' => __('H6', 'core'),
                'slug' => 'h-6',
                'size' => get_theme_mod('font_size', 16)*get_theme_mod('h6_font_size', 1)
            ),
            array(
                'name' => __('Text', 'core'),
                'slug' => 'txt',
                'size' => get_theme_mod('font_size', 16)
            ),

        );
    }

    /**
     * Modify or add required & recommended plugins for the SUITE.
     * 
     * @filter 'greyd_required_plugins'
     * 
     * @param array $plugins   Array of all plugins.
     */
    public function mod_required_plugins($plugins) {
        if (is_greyd_blocks()) {
            // remove vc
            for ($i=count($plugins)-1; $i>-1; $i--) {
                if ($plugins[$i]['slug'] == 'js_composer') {
                    unset($plugins[$i]);
                    // break;
                }
            }
            foreach( $this->plugins as $plugin ) {
                $plugins[] = $plugin;
            }
        }
        return $plugins;
    }
    
    /**
     * Modify Gutenberg update notification
     */
    public function gutenberg_update_message( $plugin_data, $new_data ) {
        
        $new_version        = isset($new_data->new_version) ? esc_attr($new_data->new_version) : esc_attr($plugin_data['new_version']);
        $supported_version  = $this->plugins['gutenberg']['version'];

        $is_supported = version_compare( $new_version, $supported_version, '<=' );

        if ( !$is_supported ) {
            echo "</p><br>
            <style> p._icon_warning::before { content: \"\\f534\"; } </style>
            <p class='_icon_warning'>".
                __("Diese Gutenberg Version wurde noch nicht mit deiner aktuellen Version der GREYD.SUITE getestet. Nach einem Update kann es zu Fehlern kommen.", "core" ).
                " <a href='https://downloads.wordpress.org/plugin/gutenberg.{$supported_version}.zip'>".sprintf(
                    __("Aktuell unterstütze Gutenberg Version (%s) herunterladen", 'core'),
                    $supported_version
                )."</a>";
        }
        else {
            echo "</p><br>
            <style> p._icon_check::before { content: \"\\f485\"; color: currentColor; } </style>
            <p class='_icon_check'>".__("Diese Gutenberg Version ist mit deiner installierten Version der GREYD.SUITE getestet und kompatibel.", "core" );
        }
    }
    
	/**
	 * Add an incompatiblity notice
	 */
	public function gutenberg_incompatiblity_notice() {

		$screen = get_current_screen();

		if ( !$screen || !isset( $screen->id ) ) return;

		if (
			$screen->id === 'plugins'
			|| $screen->id === 'plugin-install'
			|| $screen->id === 'plugins-network'
			|| $screen->id === 'plugin-install-network'
			|| $screen->id === 'update-core'
			|| $screen->id === 'update-core-network'
			|| $screen->id === 'themes'
			|| $screen->id === 'themes-network'
			|| strpos( $screen->id, 'greyd' ) !== false
		) {

			if( ! function_exists( 'get_plugin_data' ) ) {
				require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
			}
			$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/gutenberg/gutenberg.php' );

			if ( !$plugin_data || !isset( $plugin_data['Version'] ) ) return;

			$current_version    = esc_attr($plugin_data['Version']);
			$supported_version  = $this->plugins['gutenberg']['version'];

			$is_incompatible = version_compare( $current_version, $supported_version, '>' );
			if ( $is_incompatible ) {
				echo '<div class="notice notice-error">';
				echo '<p>';
				echo '<strong>'.__( 'Achtung', 'core' ).'</strong>';
				echo '</p>';
				echo '<p>';
				echo sprintf(
						__( "Deine aktuell installierte Gutenberg Version %s ist noch nicht mit der GREYD.SUITE getestet. In der Vergangenheit kam es immer wieder zu Inkompatibilitäten und Problemen mit neuen Versionen durch Breaking Changes. Sei deshalb vorsichtig und installiere im Zeifel die bestätigt getestete Version: %s", 'core'),
						'<code>'.$current_version.'</code>',
						'<a href="https://downloads.wordpress.org/plugin/gutenberg.'.$supported_version.'.zip">'.sprintf(
							__("Aktuell unterstütze Gutenberg Version (%s) herunterladen", "core"),
							$supported_version
						).'</a>'
						);
				echo '</p>';
				echo '</div>';
			}
		}
	}
}