<?php
/**
 * Created by PhpStorm.
 * User: adiardana
 * Date: 13/03/19
 * Time: 17.37
 */

function bw_gt_theme_support() {
    add_theme_support( 'align-wide' );
}
add_action( 'after_setup_theme', 'bw_gt_theme_support' );

add_action('acf/init', 'bw_gt_acf_init');
function bw_gt_acf_init() {
    if( !function_exists('acf_register_block') ) return;

    foreach (glob(THEMEPATH.'/gutenberg/block/*.md') as $file) {
        $content = file_get_contents($file);
        $content = str_replace(['\r\n', '\r'], '\n', $content);
        $content = explode('=====', $content);
        $content = $content[0];
        $content = explode(PHP_EOL, $content);

        $arg = [];
        foreach ($content as $config){
            $opt = explode(':', $config);
            switch ($opt[0]){
                case 'slug':
                    if(isset($opt[1])) {
                        $arg['name'] = strtolower( str_replace(' ', '_', trim($opt[1])) );
                    }
                    break;
                case 'title':
                    if(isset($opt[1])) {
                        $arg['title'] = trim($opt[1]);
                    }
                    break;
                case 'description':
                    if(isset($opt[1])) {
                        $arg['description'] = trim($opt[1]);
                    }
                    break;
                case 'category':
                    if(isset($opt[1])) {
                        $arg['category'] = trim($opt[1]);
                    }
                    break;
                case 'icon':
                    if(isset($opt[1])) {
                        $arg['icon'] = trim($opt[1]);
                    }
                    break;
                case 'keywords':
                    if(isset($opt[1])) {
                        $arg['keywords'] = explode(',', trim($opt[1]));
                    }
                    break;
                case 'multiple':
                    if(isset($opt[1])) {
                        $multiple_opt = true;

                        if(trim($opt[1]) == 'false'){
                            $multiple_opt = false;
                        }

                        $arg['supports']['multiple'] = $multiple_opt;
                    }
                    break;
            }
        }
        if(!isset($arg['name'])) return;

        $arg['render_callback'] = 'bw_block_render_callback';

        acf_register_block($arg);
    }
}

function bw_block_render_callback($block){
    $slug = str_replace('acf/', '', $block['name']);

    if(file_exists(THEMEPATH."/gutenberg/template/{$slug}.php")){
        include( THEMEPATH."/gutenberg/template/{$slug}.php" );
    } else {
        $file = THEMEPATH."/gutenberg/template/".$slug.".php";
        $php = '<div style="background: #eee;padding: 20px;position: relative;">'.ucwords(str_replace('-',' ', $slug)).' Content<span style="position: absolute;bottom: 0; right: 10px;padding: 5px 10px;font-size: 8px;background:#ddd;">change template by editing file '.$slug.'.php</span></div>';
        if(!file_put_contents($file, $php)){
            die("Error: failed to write data to ".$slug.".php");
        }

    }
}
