<?php 

/**
 * @package GetOmekaData
 */

 /*
Plugin Name: Get Omeka Data
Description: Add Omeka API data and inject using wordpress blocks
Version: 1.1.1
Author: IBICT | André Togawa
 */

defined( 'ABSPATH' ) or die( 'Hi there!  I\'m just a plugin, not much I can do when called directly.' );


if (!class_exists('GetOmekaData')){

    class GetOmekaData{ 

        public $pluginName;

    // == On Plugin running == //    
        function __construct(){        
            $this->pluginName = plugin_basename( __FILE__ );
            //add_action ('init', array($this, 'custom_post_type'));
            add_action( 'admin_init', array($this,'settingsOptions'));
            add_action( 'init', array($this,'getod_gutenberg_blocks'));
            add_action( 'wp_enqueue_scripts', array($this,'load_js_css'));

        }

        function load_js_css() {
            $plugin_url = plugin_dir_url( __FILE__ );        
            wp_enqueue_style( 'style1', $plugin_url . '/inc/mystyle.css' );
            wp_enqueue_script('jquery');
            wp_enqueue_script( 'test-js', $plugin_url . 'js/on_page_load.js'); 
            
        }
        
    // == Gutenberg Functions ==//

        function getod_gutenberg_blocks(){
            wp_register_script( 'getod-blocks-js',  plugins_url('/build/index.js', __FILE__ ), array('wp-blocks', 'wp-block-editor', 'wp-components'));
            

           // register_block_type( 'getod/getod-image', array('editor_script' => 'getod-blocks-js', 'render_callback' => array($this,'GetOD_image_html')));
            register_block_type( 'getod/getod-video', array('editor_script' => 'getod-blocks-js', 'render_callback' => array($this,'GetOD_video_html')));
            register_block_type( 'getod/getod-audio', array('editor_script' => 'getod-blocks-js', 'render_callback' => array($this,'GetOD_audio_html')));
            register_block_type( 'getod/getod-listfullfields', array('editor_script' => 'getod-blocks-js', 'render_callback' => array($this,'GetOD_fullfields_html')));
        }

        function GetOD_fullfields_html($attributes){
            
            if ($attributes['GetOD_list_full_fields']){
                return '
                <span class="getod getod-fullfield getod-fullfield-' . $attributes['GetOD_list_full_fields'] . ' getod-empty dot-pulse"'. 
                'metadata_id="' . $attributes['GetOD_list_full_fields'] . '"'.
                'metadata_css="'. (isset($attributes['GetOD_ff_css']) ? $attributes['GetOD_ff_css'] : "") .'"'.
                'getod_image_description="'. (isset($attributes['GetOD_ff_image_description']) ? $attributes['GetOD_ff_image_description'] : "") .'"'.
                'getod_image_source="' . (isset($attributes['GetOD_ff_image_source']) ? $attributes['GetOD_ff_image_source'] : "") . '"'.
                'getod_image_alt="' . (isset($attributes['GetOD_ff_image_alt']) ? $attributes['GetOD_ff_image_alt'] : "") . '"'.
                'getod_fullfield_elementohtml="' . (isset($attributes['GetOD_elementoHtml']) ? $attributes['GetOD_elementoHtml'] : "") . '"'.
                'getod_image_creator="' . (isset($attributes['GetOD_ff_image_creator']) ? $attributes['GetOD_ff_image_creator'] : "") . '"'.
                'getod_image_date="' . (isset($attributes['GetOD_ff_image_date']) ? $attributes['GetOD_ff_image_date'] : "") . '"'.
                'getod_image_accessRights="' . (isset($attributes['GetOD_ff_image_accessRights']) ? $attributes['GetOD_ff_image_accessRights'] : "") . '"'.
                'getod_image_rightsHolder="' . (isset($attributes['GetOD_ff_image_rightsHolder']) ? $attributes['GetOD_ff_image_rightsHolder'] : "") . '"'.
                'getod_fullfield_show_image="' . (isset($attributes['toggle_img']) ? $attributes['toggle_img'] ? 'true':'false' : "") . '"'.
                'getod_fullfield_show_text="' . (isset($attributes['toggle_text']) ? $attributes['toggle_text'] ? 'true':'false' : "") . '"'. 
                'getod_fullfield_show_video="' . (isset($attributes['toggle_video']) ? $attributes['toggle_video'] ? 'true':'false' : "") . '"'. 
                'getod_fullfield_show_audio="' . (isset($attributes['toggle_audio']) ? $attributes['toggle_audio'] ? 'true':'false' : "") . '"'. 
                'getod_fullfield_show_url="' . (isset($attributes['toggle_url']) ? $attributes['toggle_url'] ? 'true':'false' : "") . '"'.  
                'getod_block ="getod-full-fields">             
                </span>'; 
            }  
            
        }


        function GetOD_image_html($attributes){
            if ($attributes['GetOD_image']){
                return '
                <figure class="getod getod-image getod-image-' . $attributes['GetOD_image'] . ' getod-empty dot-pulse"'. 
                'metadata_id="' . $attributes['GetOD_image'] . '"'.
                'getod_image_description="'. (isset($attributes['GetOD_image_description']) ? $attributes['GetOD_image_description'] : "") .'"'.
                'getod_image_source="' . (isset($attributes['GetOD_image_source']) ? $attributes['GetOD_image_source'] : "") . '"'.
                'getod_image_alt="' . (isset($attributes['GetOD_image_alt']) ? $attributes['GetOD_image_alt'] : "") . '"'.
                'getod_image_creator="' . (isset($attributes['GetOD_image_creator']) ? $attributes['GetOD_image_creator'] : "") . '"'.
                'getod_image_date="' . (isset($attributes['GetOD_image_date']) ? $attributes['GetOD_image_date'] : "") . '"'.
                'getod_image_accessRights="' . (isset($attributes['GetOD_image_accessRights']) ? $attributes['GetOD_image_accessRights'] : "") . '"'.
                'getod_image_rightsHolder="' . (isset($attributes['GetOD_image_rightsHolder']) ? $attributes['GetOD_image_rightsHolder'] : "") . '"'.    
                'getod_block ="getod-image">               
                </figure>'; 
            }   
            
        }

        function GetOD_video_html($attributes){
            if ($attributes['GetOD_video']){
                return '<div class="getod getod-video getod-video-' . $attributes['GetOD_video'] . 'getod-empty dot-pulse" metadata_id="' . $attributes['GetOD_video'] . '"getod_block ="getod-video"></div>'; 
            }        
        }
        function GetOD_audio_html($attributes){
            if ($attributes['GetOD_audio']){
                return '<div class="getod getod-audio getod-audio-' . $attributes['GetOD_audio']  . 'getod-empty dot-pulse" metadata_id="' . $attributes['GetOD_audio'] . '" getod_block ="getod-audio"></div>'; 
            }        
        }



    // == Add Admin page == //
        public function add_admin_pages(){
            add_menu_page( 'GetOD', 'GetOD', 'manage_options', 'GetOD_plugin', array($this,'admin_index'),'dashicons-rest-api', 110);
        }

    // == Plugin admin page == //
        public function admin_index(){
            require_once plugin_dir_path( __FILE__ ) . 'inc/get_od_admin_page.php';
        }

        function settingsOptions(){
            add_settings_section( 'getod_first_section', null, null, 'GetOD' );

            //criando pagina com os dados para pagina
                add_settings_field( 'getod_url_api', 'Url da API', array($this,'paginaHTML'), 'GetOD', 'getod_first_section');
                register_setting( 'getod_url', 'getod_url_api', array('sanitize_callback'=>'sanitize_text_field') );
        }

        function paginaHTML(){?>
         
            <input type="text" name="getod_url_api" value="<?php echo esc_attr( get_option('getod_url_api') ) ?>">
   
           <?php }

    // == On Plugin Activation == //
        function activate (){
            require_once plugin_dir_path( __FILE__ ) . 'inc/get_od_activate.php';
            GetODActivate::activate();
        }

    // == Register on Acivation == //
        function register(){            
            add_action( 'admin_menu', array( $this, 'add_admin_pages') ); // Add Admin button to menu

            add_action ('admin_enqueue_scripts', array($this,'enqueue')); // add css page on admin side 
        }
        function enqueue(){
            // enqueue all our scripts
            wp_enqueue_style( 'mypluginstyle', plugins_url('/inc/mystyle.css', __FILE__ ) );            
    
        }


    }

    if ( class_exists( 'GetOmekaData')){
        $GetOmekaData = new GetOmekaData();
        $GetOmekaData->register();
    }
    
    // activation
    register_activation_hook( __FILE__, array($GetOmekaData, 'activate'));
    
    // deactivation
    require_once plugin_dir_path( __FILE__ ) . 'inc/get_od_deactivate.php';
    register_deactivation_hook( __FILE__, array('GetODDeactivate', 'deactivate'));

}