<?php
/**
 * Description: Get .png images of rules from server.
 *
 * @package     midi-Synth
 * @category    module
 * @author      Oleg Klenitsky
 * @copyright   2024 Oleg Klenitsky
 * @license     GPL-2.0-or-later
 */

if ( ! defined( "ABSPATH" ) ) {
	exit();
}

/**
 * Get .png images of rules from server.
 * @return [type] [description]
 */
function midiSynth_getRulesImgs(){
	WP_Filesystem();
	global $wp_filesystem;

	// security check.
	if(array_key_exists("nonce", $_POST)){
		$_midiSynth_nonce = filter_input( INPUT_POST, "nonce", FILTER_DEFAULT );
		if ( false === wp_verify_nonce( $_midiSynth_nonce, 'midiSynth' ) ) {
			wp_die( 'Security check failed!' );
    }
	}else{
		wp_die( "midiSynth: nonce missing." );
	}
	if( $_midiSynth_nonce !== wp_create_nonce( "midiSynth" ) ){
		wp_die( "midiSynth: nonce is incorrect." );
	}

	$dirRules = plugin_dir_path( __FILE__ ) . "rules";
	$filelist = $wp_filesystem->dirlist($dirRules);

	foreach ( $filelist as $file_path ) {
		$image_url = plugin_dir_url( __FILE__ ) . "rules/" . $file_path['name'];
    echo '<img src="' . esc_url( $image_url ) . '" alt="' . esc_attr( basename( $file_path['name'] ) ) . '" width="350" style="border: 1px solid black;">';
    echo("|");
  }

	wp_die();
}
