<?php 

namespace Webien\Setup;


class PluginCheck 
{
    #Route: http://webien.test/wp-json/haus/plugin_status

    #initialize
    static function init()
    {}

    static function custom_rewrite_rule() {
        $allowedURIs = array('/haus-status');
        $currentURI = $_SERVER['REQUEST_URI'];
        if (in_array($currentURI, $allowedURIs)) {
            #Redirect to the desired url
            wp_redirect('/wp-json/haus/wpdata');
            exit;
        }
    }

    #Register the Route with getPlugins() as callback
    function post_register_route(){
        register_rest_route( 'haus', '/wpdata', array(
            'methods' => 'GET',
            'callback' => array( $this, 'getData' ),
            'show_in_index' => false,
            'permission_callback' => '__return_true',
        ));
    }
    
    function getData() {
        #Get the wordpress version
        $wordpress_version = get_bloginfo( 'version' );

        #Get all the plugins even the deactivated ones
        $all_plugins = get_plugins();

        #Get the active plugins
        $activePlugins = get_option('active_plugins'); // simple array of active plugins

        #Get mu-plugins
        $mu_plugins = get_mu_plugins();

        #Display the wordpress version
        echo "Wordpress Version", " ", $wordpress_version;
        echo("\n");

        #Get PHP version
        echo("\n");
        echo("PHP Version ");
        echo(phpversion());

        #Check if the plugin list is NULL or 0
        if (!isset($all_plugins)) {
            return "No plugins available";
        } else {
            #Make the arrays for the values
            $plugin_names = array();
            $plugin_version = array();
            $plugin_activated = array();

            #Loop down to names and value -> $value
            foreach ($all_plugins as $key => $value) {
                if (in_array($key, $activePlugins)) {
                    $plugin_activated[] = $value['Name'];
                }

                #Insert the data Name and version into the array
                $plugin_names[] = $value['Name'];
                $plugin_version[] = $value['Version'];

                #Combine the name and verison arrays
                $plugin_output_data = array_combine($plugin_names, $plugin_version,);
            }
            #Output
            #Return/Print the result our output
            #\n for space
            echo("\n");
            echo("\n");
            echo("Available Plugins: ");
            echo("\n");
            print_r($plugin_output_data);

            #Activated
            echo("\n");
            echo("Activated Plugins" . "\n");
            print_r($plugin_activated);
            echo("\n");
        }

        #Check if the mu-plugin list is NULL or 0
        if (!isset($mu_plugins)) {
            echo("No Mu-plugins available");
        } else {
            #mu-plugins
            $mu_plugin_names = array();

            #Loop $mu_plugins then get the Name
            foreach ($mu_plugins as $plugins) {
                $mu_plugin_names[] = $plugins['Name'];
            }
            #Output the mu-plugins
            echo("Mu-Plugins" . "\n");
            print_r($mu_plugin_names);
        }
        
    }

    #function with add_action 
    static function register_route() {
        $plugin_check = new PluginCheck();
        add_action( 'rest_api_init', array( $plugin_check, 'post_register_route' ) );

        #for rewrite
        add_action('init', array( $plugin_check, 'custom_rewrite_rule'));
    }
}

#Route class with add_action
PluginCheck::register_route();  