<?php

    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    class WPH_component_security_vulnerabilities
        {
            protected $use_hooks = true;
            
            function __construct( $use_hooks = true )
                {                    
                    $this->use_hooks = $use_hooks;
                    
                    if ( $this->use_hooks ) 
                        {
                            add_action ( 'init', array ( $this, 'init') );
                        }
                }
                
            function init()
                {
   
                    if ( is_admin() &&  current_user_can ( 'manage_options' ) )
                        $this->scan_ajax();
                }       
                
            function admin_enqueue_scripts( )
                {
                    wp_register_style('wph-vulnerabilities', WPH_URL . '/assets/css/wph-vulnerabilities.css', array(), WPH_CORE_VERSION );
                    wp_enqueue_style( 'wph-vulnerabilities');
            
                    wp_register_style('wph-graphs', WPH_URL . '/assets/css/graph.css', array(), WPH_CORE_VERSION );
                    wp_enqueue_style( 'wph-graphs');
                    
                    wp_register_style('wph-styles', WPH_URL . '/assets/css/wph.css', array(), WPH_CORE_VERSION );
                    wp_enqueue_style( 'wph-styles');
                    
                    wp_enqueue_script('jquery.tipsy.js', WPH_URL . '/assets/js/jquery.tipsy.js' ); 
                    
                    wp_enqueue_script( 'jquery');
                    wp_register_script('wph', WPH_URL . '/assets/js/wph.js', array(), WPH_CORE_VERSION );
                    
                    // Localize the script with new data
                    $translation_array = array(
                                                'init_scan_vulnerabilities' =>  __( 'Initializing the Vulnerability Scan', 'wp-hide-security-enhancer' ),
                                                
                                                'init_scan_failure'         =>  __( 'Failed to initialize the Vulnerability Scan', 'wp-hide-security-enhancer' ),
                                                'all_items_processed'       =>  __( 'All items processed', 'wp-hide-security-enhancer' ),
                                                'processing_item'           =>  __( 'Processing plugin', 'wp-hide-security-enhancer' ),
                                                'error_processing_item'     =>  __( 'Error processing AJAX for plugin', 'wp-hide-security-enhancer' ),
                                        );
                    wp_localize_script( 'wph', 'wph_vars', $translation_array );
                    
                    wp_enqueue_script( 'wph');
                }
 
                
            function scan_ajax()
                {
                    add_action( 'wp_ajax_wph_site_scan_vulnerabilities_start',          array ( $this, 'wp_ajax_wph_site_scan_vulnerabilities_start' ) );
                    add_action( 'wp_ajax_wph_site_scan_vulnerabilities_single_item',    array ( $this, 'wp_ajax_wph_site_scan_vulnerabilities_single_item' ) );

                }

                
                                 
            
            function interface_html()
                {
                    
                    $site_vulnerabilities_scan  =   $this->get_site_vulnerabilities_scan();
                                                     
                    ?>

                        <div id="security-scan" class="min-h-screen bg-background">
                                
                                <header>
                                    <div class="container">
                                        <div class="items">
                                            <div>
                                                <h1><span class="dashicons dashicons-shield"></span> <?php _e( "Vulnerabilities Scan", 'wp-hide-security-enhancer' ) ?></h1>
                                                <p class="text-muted-foreground text-xs"><b><?php esc_html_e( "AI-Powered Vulnerability Scan", 'wp-hide-security-enhancer' ) ?></b><br /><?php esc_html_e( "Built on insights from our", 'wp-hide-security-enhancer' ) ?> <b><?php esc_html_e( "proprietary AI-engineered", 'wp-hide-security-enhancer' ) ?></b>, <?php esc_html_e( "trained with advanced models and enriched by CVE records and other trusted vulnerability sources. It checks your site’s plugins against a comprehensive database of known security risks.", 'wp-hide-security-enhancer' ) ?></p>
                                            </div>
                                        </div>
                                        <div class="items">
                                            <span class="dashicons dashicons-clock"></span>
                                            <span>Last scan: <?php 
                                                if ( ! isset ( $site_vulnerabilities_scan['last_scan'] ) )
                                                    _e( 'Unavailable', 'wp-hide-security-enhancer' );
                                                    else
                                                    echo date( "Y-m-d H:i:s", $site_vulnerabilities_scan['last_scan'] );  ?></span>
                                        </div>
                                    </div>
                                </header>

                                <main class="container">
                                    
                                    
                                    <?php  $this->render_overview( $site_vulnerabilities_scan ); ?>
                                    
                                    
                                    <div id="all-scann-items">
                                        <?php 
                                                if ( ! isset ( $site_vulnerabilities_scan['last_scan'] ) )
                                                    { ?>
                                        <h2 class="info"><?php _e( "You're about to run your first scan. Click Start Scan to get started.", 'wp-hide-security-enhancer' ) ?></h2>
                                        <?php } 
                                        
                                        if ( $this->scan_hash_changed() )
                                            {
                                            ?>
                                                <h2 class="info"><?php _e( "One or more changes have occurred in your plugin list. Please initiate a new scan.", 'wp-hide-security-enhancer' ) ?></h2>
                                                <?php    
                                            }
                                        
                                        ?>
                                        
                                        <div class="gc_top">
                                            <h2 class="info"><?php _e( 'Scan Results', 'wp-hide-security-enhancer' ) ?></h2>
                                        </div>

                                        <div class="gc-area">                                            
                                            <div id="scann-items"><?php 
                                                if (  isset ( $site_vulnerabilities_scan['last_scan'] ) )
                                                    {
                                                        if ( ! $this->scan_hash_changed() )
                                                            {
                                                        
                                                                $site_plugins   =   $this->get_site_plugins();
                            
                                                                foreach ( $site_plugins as  $site_plugin )
                                                                    {
                                                                        $response   =   $this->get_plugin_report( $site_plugin['slug'] );
                                                                        echo $response['item_html'];
                                                                    }
                                                            }
                                                    
                                                    }   
                                                        
                                            ?></div> 
                                        </div>
                                    </div>
                                </main>
                            </div>
      

                    <?php
                    
                }
                
            
            public function render_overview( $site_vulnerabilities_scan, $context = '' )
                {
                    
                    $overview   =   $this->get_overview_data();
                    
                    ?>
                    <svg xmlns="http://www.w3.org/2000/svg" style="display:none">
                                    <symbol id="icon-shield-check" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"></path>
                                        <path d="m9 12 2 2 4-4"></path>
                                    </symbol>
                                    <symbol id="icon-shield-x" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"></path>
                                        <path d="m14.5 9.5-5 5"></path>
                                        <path d="m9.5 9.5 5 5"></path>
                                    </symbol>
                                </svg>
                    <div id="scan_overview" class="single-column">
                 
                            <div class="wph_results">

                                            <div id="wph-scan-score" class="four-columns">
                                                <div class="score-item">
                                                    <div class="icon passed">
                                                        <svg class="text-success" width="24" height="24"><use href="#icon-shield-check"/></svg>
                                                    </div>
                                                    <div class="total-plugins">
                                                        <span><?php echo $overview['total_plugins'] ?></span>
                                                        <p><?php _e( 'Total Plugins', 'wp-hide-security-enhancer' ); ?></p>
                                                    </div>
                                                </div>
                                                <div class="score-item">
                                                    <div class="icon passed">
                                                        <svg class="text-success" width="24" height="24"><use href="#icon-shield-check"/></svg>
                                                    </div>
                                                    <div class="scaned">
                                                        <span><?php echo $overview['scaned'] ?></span>
                                                        <p><?php _e( 'Scaned Plugins', 'wp-hide-security-enhancer' ); ?></p>
                                                    </div>
                                                </div>
                                                <div class="score-item">
                                                    <div class="icon passed">
                                                        <svg class="text-success" width="24" height="24"><use href="#icon-shield-check"/></svg>
                                                    </div>
                                                    <div class="passed">
                                                        <span><?php echo $overview['passed'] ?></span>
                                                        <p><?php _e( 'Passed', 'wp-hide-security-enhancer' ); ?></p>
                                                    </div>
                                                </div>
                                                <div class="score-item ">
                                                    <div class="icon failed">
                                                        <svg class="text-destructive" width="24" height="24"><use href="#icon-shield-x"/></svg>
                                                    </div>
                                                    <div class="vulnerable">
                                                        <span><?php echo $overview['vulnerable'] ?></span>
                                                        <p><?php _e( 'Vulnerables', 'wp-hide-security-enhancer' ); ?></p>
                                                    </div>
                                                </div>
                                            </div>
                                            <button id="wph-site-scan-button" class="button" tabindex="0" onClick="WPH.vulnerabilities_site_scan( '<?php echo esc_attr ( wp_create_nonce( 'wph/site_scan_vulnerabilities') ) ?>')">
                                                <span class="dashicons dashicons-search"></span>
                                                <?php _e( 'Start Scan', 'wp-hide-security-enhancer' ); ?>
                                            </button>
                                            <p class="actions">
                                                <span class="working"></span>
                                                <span class="spinner" style="visibility: hidden;"></span>
                                            </p>             
                            </div>
                        </div>
                   
                    <?php
                    
                }
                
            private function render_item( $scan_item_id, $scan_item_data, $response  )
                {
                    $site_scan  =   (array)get_site_option('wph/site_scan');
                    
                    $wph_site_scan_ignore   =   isset ( $site_scan['ignore'] ) ?    (array)$site_scan['ignore'] :   array();
                    
                    $ignore_html    =   '';
                    
                    if ( in_array ( $scan_item_id, $wph_site_scan_ignore ) )
                        $ignore_html    =   '<span class="status-ignore">' . __( 'Ignored', 'security-scan-interface' ) .'</span>';
                       
                    ?>
                        <div id="item-<?php echo $scan_item_id ?>" class="gc <?php if ( $response->status ) echo 'item-pass'; else echo 'item-fail' ?>">
                            <div class="gc-header status-<?php if ( $response->status ) echo 'pass'; else echo 'fail' ?>">
                                <div class="gc-item">
                                    <span class="dashicons dashicon-circle dashicons-<?php if ( $response->status ) echo 'yes'; else echo 'no' ?>"></span>
                                    <div>
                                        <h3><?php echo $scan_item_data['title'] ?></h3>
                                        <p class="issue_info"><?php echo __( 'Current Plugin Version', 'security-scan-interface' ); echo ': ' . $response->info; ?></p>
                                        <p class="issue_info"><?php echo __( 'Known historical vulnerabilities for this plugin', 'security-scan-interface' ); echo ': <b>';
                                            
                                            if ( $response->known_vulnerabilities < 1 )
                                                echo '0';
                                                else
                                                _e( 'Multiple', 'security-scan-interface' )
                                                 ?></b></p>
                                    </div>
                                </div>
                                <div class="gc-item toggle">
                                    <span class="spinner"></span>
                                    <?php echo $ignore_html ?>
                                    <span class="status status-<?php if ( $response->status ) echo 'pass'; else echo 'fail' ?>"><?php if ( $response->status ) esc_html_e ( 'Passed', 'security-scan-interface' ); else esc_html_e ( 'Vulnerable', 'security-scan-interface' ); ?></span>
                                    <div class="wph-toggle">
                                      <span class="dashicons <?php if ( $response->status ) { echo 'dashicons-arrow-down-alt2';} else { echo 'dashicons-arrow-up-alt2';} ?>"></span>
                                    </div>
                                </div>
                            </div>
                            <div class="gc-body" <?php if ( $response->status ) { echo 'style="display: none;"';} ?>>
                                <div class="gc-item">
                                    <div class="issue_description"><?php 
                                        
                                        if ( is_array ( $response->description )    &&  count ( $response->description )    >   0 ) 
                                            {
                                                foreach ( $response->description    as  $description )
                                                    {
                                                        echo $description;
                                                    }   
                                            } ?></div>
                                    
                                    <?php
                                        if ( count ( (array)$response->actions ) > 0 )
                                        {
                                            ?><div class="issue_actions"><?php
                                        foreach ( $response->actions    as  $action_type =>  $action )
                                            {
                                                echo  " " . $this->get_action_html( $action_type, $action, $scan_item_id );  
                                            }
                                            ?></div><?php
                                        }
                                    ?>
             
                                </div>
                            </div>
                        </div>

                    <?php
                    
                }
            
            function get_site_vulnerabilities_scan()
                {
                    $site_vulnerabilities_scan  =   get_site_option('wph/site_vulnerabilities_scan');   
                    
                    if ( ! is_array ( $site_vulnerabilities_scan ) )
                        $site_vulnerabilities_scan  =   array();
                                        
                    return $site_vulnerabilities_scan;   
                }
                
            
            /**
            * Check if anything changed in the list of plugins and or installed version
            * 
            */
            function scan_hash_changed()
                {
                    $site_vulnerabilities_scan  =   $this->get_site_vulnerabilities_scan();
                    $site_plugins               =   $this->get_site_plugins();
                    
                    $json_site_plugins          =   json_encode ( $site_plugins );   
                    
                    if ( isset (  $site_vulnerabilities_scan['hash'] )  &&  md5 ( $json_site_plugins )  !== $site_vulnerabilities_scan['hash']  )
                        return TRUE;
                        
                    return FALSE;
                      
                }    
            
            
            function get_overview_data()    
                {
                    $site_plugins   =   $this->get_site_plugins();
                    $site_vulnerabilities_scan  =   $this->get_site_vulnerabilities_scan();
                    
                    $overview   =   array();
                    
                    $overview['total_plugins']   =  count ( $site_plugins );
                    
                    if ( ! isset ( $site_vulnerabilities_scan['last_scan'] ) ||  ! isset ( $site_vulnerabilities_scan['results'] )  ||  count ( $site_vulnerabilities_scan['results'] ) < 1 )
                        {
                            $overview['scaned']         =  0;
                            $overview['passed']         =  0;
                            $overview['vulnerable']     =  0;
                            
                            return $overview;        
                        }
                    
                    $scanned_plugins    =   array();
                    foreach ( $site_vulnerabilities_scan['results'] as  $result )
                        {
                            if ( ! isset ( $scanned_plugins[ $result['plugin'] ] ) )
                                $scanned_plugins[ $result['plugin'] ]   =   0;
                                
                            if ( isset ( $result['cve'] ) &&    count ( $result['cve'] ) > 0 )
                                $scanned_plugins[ $result['plugin'] ]   =   $scanned_plugins[ $result['plugin'] ] + 1;
                        }
                    $overview['scaned'] =   count ( $scanned_plugins );
                    
                    $overview['passed']         =  0;
                    foreach ( $scanned_plugins as   $scanned_plugin =>  $found_vulnerabilities )
                        {
                            if ( $found_vulnerabilities > 0 )
                                $overview['vulnerable'] =   $overview['vulnerable'] + 1;
                                else
                                $overview['passed'] =   $overview['passed'] + 1;
                        }
                    
                    return $overview;
                }
            
            
            
            /**
            * Return a list of the active plugins
            * 
            */
            function get_site_plugins()
                {
                    if (!function_exists('get_plugins'))
                        require_once ABSPATH . 'wp-admin/includes/plugin.php';

                    $all_plugins = get_plugins();

                    $plugins_data = [];

                    foreach ($all_plugins as $plugin_file => $plugin_info) 
                        {
                            // Slug is usually the folder name (or file if no folder)
                            $slug = dirname($plugin_file);
                            
                            // Handle plugins in root (no folder)
                            if ($slug === '.') 
                                {
                                    $slug = basename($plugin_file, '.php');
                                }

                            $plugins_data[] = [
                                                    'slug'      =>  $slug,
                                                    'title'     =>  $plugin_info['Name'],
                                                    'version'   =>  $plugin_info['Version']  
                                                ];
                        }

                    return $plugins_data;
                }
                
            function get_plugin_by_slug( $slug )
                {
                    $site_plugins   =   $this->get_site_plugins();
                    
                    foreach ( $site_plugins as  $site_plugin )
                        {
                            if ( $site_plugin['slug']   ===  $slug )
                                {
                                    return $site_plugin;
                                }
                        }
                        
                    return FALSE;
                    
                }
            
            
            function wp_ajax_wph_site_scan_vulnerabilities_start()
                {
                    if ( ! wp_verify_nonce( $_POST['nonce'], 'wph/site_scan_vulnerabilities' ) ) 
                        die();
                        
                    $ajax_response  =   array();
                        
                    $site_plugins   =   $this->get_site_plugins();
                    
                    if ( count ( $site_plugins ) < 1 )    
                        {
                            $response['status']         =   'error';
                            $ajax_response['message']   =   __( 'No plugins to scan for vulnerabilities.', 'wp-hide-security-enhancer' );
                            $ajax_response['code']      =   '02';
                            
                            echo json_encode( $ajax_response );
                            
                            die();
                        }
                    
                    $json_site_plugins =   json_encode ( $site_plugins );
                    $api_response_data  =   $this->_call_API( $json_site_plugins );
                    
                    sleep( 1 );
                    
                    if ( $api_response_data['status']   !== 'success' )
                        {
                            $response['status']         =   'error';
                            $ajax_response['code']      =   '03';
                            $response['message']        =   $api_response_data['message'];
                                                
                            echo json_encode( $response );
                            
                            die();   
                        }
                        
                    $site_vulnerabilities_scan  =   $this->get_site_vulnerabilities_scan();
                    
                    $site_vulnerabilities_scan['last_scan'] =   time();
                    $site_vulnerabilities_scan['results']   =   $api_response_data['vulnerable_plugins'];
                    $site_vulnerabilities_scan['hash']      =   md5 ( $json_site_plugins );
                                        
                    update_site_option ( 'wph/site_vulnerabilities_scan', $site_vulnerabilities_scan );
                                        
                    $response['status']         =   'success';
                    $response['message']        =   __( "Vulnerability Scan process completed.", 'wp-hide-security-enhancer' );
                    $response['plugin_list']    =   array();
                    foreach ( $site_plugins as  $site_plugin )
                        {
                            $response['plugin_list'][]  =      $site_plugin;
                        }
                                        
                    echo json_encode( $response );
                    
                    die();  
                
                }
                
                
            function wp_ajax_wph_site_scan_vulnerabilities_single_item()
                {
                   
                    if ( ! wp_verify_nonce( $_POST['nonce'], 'wph/site_scan_vulnerabilities' ) ) 
                        die();    
                    
                    $ajax_response  =   array();
                    
                    $request_item   =   isset ( $_POST['slug'] ) ? sanitize_title ( $_POST['slug'] )    :   FALSE;
                    if ( empty ( $request_item ) )
                        {
                            $response['status']         =   'error';
                            $ajax_response['message']   =   __( 'Plugin slug not provided.', 'wp-hide-security-enhancer' );
                            $ajax_response['code']    =   '05';
                            
                            echo json_encode( $ajax_response );
                            
                            die();
                        }
                        
                    $response   =   $this->get_plugin_report( $request_item );   
                    
                    $response['status']         =   'success';
                    $response['code']           =   '07';
                    
                    echo json_encode( $response );
                    
                    die(); 
                                    
                }
                
                
            private function get_plugin_report( $request_item )
                {
                    $site_vulnerabilities_scan  =   $this->get_site_vulnerabilities_scan();
                    foreach ( $site_vulnerabilities_scan['results'] as  $item )
                        {
                            if ( $item['plugin']    === $request_item )
                                {
                                    $plugin_data                =   $this->get_plugin_by_slug( $request_item );
                                    $plugin_data['help']        =   '';
                                    
                                    $plugin_meta                =   new stdClass();
                                    $plugin_meta->description   =   [];
                                    
                                    if ( isset ( $item['cve'] ) )
                                        {
                                            $plugin_meta->status        =   FALSE;
                                            
                                            foreach ( $site_vulnerabilities_scan['results'] as  $_item )
                                                {
                                                    if ( $_item['plugin']    === $request_item )
                                                        {
                                                            $severity_raw = trim((string) ($_item['cve']['severity'] ?? ''));
                                                            $score_raw    = trim((string) ($_item['cve']['score'] ?? ''));
                                                            $vector_raw   = trim((string) ($_item['cve']['vector'] ?? ''));

                                                            $severity_display = $severity_raw !== '' ? strtoupper($severity_raw) : 'UNKNOWN';

                                                            $severity_class = 'bg-triage-unknown';
                                                            switch (strtolower($severity_display)) {
                                                                case 'critical':
                                                                    $severity_class = 'bg-triage-critical';
                                                                    break;
                                                                case 'high':
                                                                    $severity_class = 'bg-triage-high';
                                                                    break;
                                                                case 'medium':
                                                                    $severity_class = 'bg-triage-medium';
                                                                    break;
                                                                case 'low':
                                                                    $severity_class = 'bg-triage-low';
                                                                    break;
                                                                default:
                                                                    $severity_class = 'bg-triage-unknown';
                                                                    break;
                                                            }

                                                            $plugin_meta->description[] = '
                                                            <div class="vulnerability-report">
                                                                <div class="header">
                                                                    <div class="flex items-center">
                                                                        <span class="' . esc_attr($severity_class) . '">' . esc_html($severity_display) . '</span>
                                                                        <h2>' . esc_html($_item['cve']['cve_id'] ?? '') . '</h2>
                                                                    </div>
                                                                    <div class="text-right">
                                                                        <div class="published">' . esc_html__( 'Published', 'wp-hide-security-enhancer' ) . '</div>
                                                                        <div class="date">' . esc_html($_item['cve']['published_at'] ?? 'Unknown') . '</div>
                                                                    </div>
                                                                </div>

                                                                <div class="row">
                                                                    <div class="item">
                                                                        <div class="label">' . esc_html__( 'Vulnerability Type', 'wp-hide-security-enhancer' ) . '</div>
                                                                        <div class="value">' . esc_html(ucfirst((string) ($_item['cve']['type'] ?? 'Unknown'))) . '</div>
                                                                    </div>

                                                                    <div class="item">
                                                                        <div class="label">' . esc_html__( 'Affected Component', 'wp-hide-security-enhancer' ) . '</div>
                                                                        <div class="value">' . esc_html($_item['cve']['slug'] ?? 'Unknown') . '</div>
                                                                    </div>

                                                                    <div class="item">
                                                                        <div class="label">' . esc_html__( 'Version Up To', 'wp-hide-security-enhancer' ) . '</div>
                                                                        <div class="value">&lt;= ' . esc_html($_item['cve']['affected_version_up_to'] ?? 'Unknown') . '</div>
                                                                    </div>
                                                                </div>

                                                                <div class="row">
                                                                    <div class="item">
                                                                        <div class="label">' . esc_html__( 'Severity', 'wp-hide-security-enhancer' ) . '</div>
                                                                        <div class="value">' . esc_html($severity_display) . '</div>
                                                                    </div>

                                                                    <div class="item">
                                                                        <div class="label">' . esc_html__( 'Score', 'wp-hide-security-enhancer' ) . '</div>
                                                                        <div class="value">' . esc_html($score_raw !== '' ? $score_raw : 'Unknown') . '</div>
                                                                    </div>

                                                                    <div class="item">
                                                                        <div class="label">' . esc_html__( 'Last Update', 'wp-hide-security-enhancer' ) . '</div>
                                                                        <div class="value">' . esc_html($_item['cve']['updated_at'] ?? 'Unknown') . '</div>
                                                                    </div>
                                                                </div>

                                                                <div class="description">
                                                                    <div class="label">' . esc_html__( 'Description', 'wp-hide-security-enhancer' ) . '</div>
                                                                    <p>' . esc_html($_item['cve']['summary'] ?? 'Unknown') . '</p>
                                                                </div>

                                                                <div class="footer">
                                                                    <div class="item">
                                                                        <span>VECTOR:</span> <span class="vector">' . esc_html($vector_raw !== '' ? $vector_raw : 'Unknown') . '</span>
                                                                    </div>
                                                                    <a href="' . esc_url('https://www.cve.org/CVERecord?id=' . ($_item['cve']['cve_id'] ?? '')) . '" target="_blank" rel="noopener noreferrer">
                                                                        ' . esc_html__( 'View CVE Record →', 'wp-hide-security-enhancer' ) . '
                                                                    </a>
                                                                </div>
                                                            </div>';
                                                        }
                                                }
                                            
                                            $response['message']        =   __( "Vulnerability found for plugin.", 'wp-hide-security-enhancer' );
                                            $response['is_vulnerable']  =   TRUE;
                                        }
                                        else
                                        {
                                            $plugin_meta->status        =   TRUE;
                                            $plugin_meta->description[] =   __( "No vulnerability found for plugin.", 'wp-hide-security-enhancer' );
                                            $response['message']        =   __( "No vulnerability found for plugin.", 'wp-hide-security-enhancer' );
                                            $response['is_vulnerable']  =   FALSE;
                                        }
                                        
                                    $plugin_meta->info          =   $plugin_data['version'];

                                    $plugin_meta->known_vulnerabilities =   $item['total_vulnerabilities'];
                                    $plugin_meta->actions       =   [];
                                    
                                    ob_start();
                                    $this->render_item( $request_item, $plugin_data, $plugin_meta );
                                    $item_html  =   ob_get_contents();
                                    ob_end_clean();
                                    
                                    
                                    $response['item_html']      =   $item_html;
                                   }
                        }
                        
                    return $response;    
                }

                    
            function _call_API( $json_site_plugins )
                {
                    global $wph;
                    
                    $license_data   =   $wph->licence->get_licence_data();
                    
                    $args    =   array( 
                                        'sslverify' => false, 
                                        'timeout' => 30,
                                        
                                        'headers'    => array(
                                                                    'Content-Type' => 'application/x-www-form-urlencoded',
                                                                ),
                                        
                                        'body'      => array(
                                                                'license_key'  => $license_data['key'],
                                                                'site_plugins' => $json_site_plugins,
                                                            )
                                        );
                    $api_url            =   'https://vulnerabilities.wp-hide.com/';
                    $response           =   wp_remote_post( $api_url, $args  );
                    
                    if ( is_a( $response, 'WP_Error' ))
                        {
                            $this->remote_errors   =   $response->get_error_message();
                            return FALSE;
                        }
                    
                    if ( is_array( $response ) ) 
                        {
                            
                            if  ( ! isset( $response['response']['code'] ) )
                                return FALSE;
                            
                            $response_data  =   [];
                            
                            if  ( $response['response']['code'] !=  200 )
                                {
                                    if ( $response['response']['code'] ==  400 )
                                        {
                                            $remote_response_data      =   json_decode ( $response['body'] );
                                            
                                            $response_data['status']    =   'error';
                                            $response_data['message']   =   $remote_response_data->message;
                                            
                                            return $response_data;
                                        }
                                        
                                    if ( $response['response']['code'] ==  404 )
                                        {                                            
                                            $response_data['status']    =   'error';
                                            $response_data['message']   =   __( "The wp_remote_get() returns a Not Found page.", 'wp-hide-security-enhancer' );
                                            
                                            return $response_data;
                                        }
                                    
                                    if ( $response['response']['code'] ==  401 )
                                        {
                                            $response_data['status']    =   'error';
                                            $response_data['message']   =   __( "The wp_remote_get() returns a 401 error code, the request could not be authenticated. Does the site use an httpd password?", 'wp-hide-security-enhancer' );
                                            
                                            return $response_data;
                                        }
                                        
                                    if ( ! empty ( $response['response']['code'] ) )
                                        {
                                            $response_data['status']    =   'error';
                                            $response_data['message']   =   __( "The wp_remote_get() returns a", 'wp-hide-security-enhancer' ) . " " . $response['response']['code'] . " " . __( "error code", 'wp-hide-security-enhancer' );
                                            
                                            return $response_data;
                                        }
                                    
                                    $response_data['status']    =   'error';
                                    $response_data['message']   =   __( "Unespected error code for wp_remote_get() call.", 'wp-hide-security-enhancer' );
                                    
                                    return $response_data;
                                }
                                
                            $response_data      =   json_decode ( $response['body'], TRUE );
                                
                            return $response_data;
                                
                        }
                        
                    return FALSE;
                    
                }
                
                
        }
?>