<?php
class Stylish_Cost_Calculator_Diagnostic {
  public static function init()
  {}

  public function __construct()
  {
  //  add_action( 'admin_init', array($this,'admin_init') );
    add_action( 'admin_menu', array($this,'admin_menu'), 90 );
    $this->license_return =get_option('scc_license_return');
    //add_action("wp_enqueue_scripts", "diagnostic_load");
  }

  function diagnostic_load(){
  //wp_enqueue_style('scc-admin-style', SCC_URL. 'styles/scc-front-end.css', array(), '3.1.1');
    wp_enqueue_style('gf-admin-style', SCC_URL. 'styles/scc-back-end.css', array(), '3.1.1');
  }

  function diagnostic_page()
  {
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    include_once SCC_DIR . '/admin/logo-header.php';
    ?>
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
    <style>
      #border1 {
        border: 2px black;
        -moz-box-shadow: inset 1px 1px 10px #F0F0F0;
        border-radius: 10px;
        background-color: #f9f9fe;
        padding: 15px;
        margin-left: 25px;
        margin-bottom: 15px;
        box-shadow: 0px 0px;
        max-width: 320px;
        /*box-shadow: 1px 1px 5px gray;*/
      }

      .green_dot {
        height: 16px;
        width: 16px;
        background-color: green;
        border-radius: 50%;
        display: inline-block;
        margin-left:10px;
        margin-right:10px;
      }

      .red_dot {
        height: 16px;
        width: 16px;
        background-color: red;
        border-radius: 50%;
        display: inline-block;
        margin-left:10px;
        margin-right:10px;
      }

      .yellow_dot{
        height: 16px;
        width: 16px;
        background-color: #dcdcdc;
        border-radius: 50%;
        display: inline-block;
        margin-left:10px;
        margin-right:10px;
      }
      .scc_title_bar {
        background: #314af3;
        padding: 15px;
        padding-left: 45px;
        margin-left: -23px;
        font-size: 22px;
        color: #fff;
        letter-spacing: 1px;
        text-transform: uppercase;
        width: 105%;
        margin-bottom: 40px;
        margin-top: 10px;
      }
    </style>
    <?php

    global $wp_version;
    $test_char = "".DB_CHARSET;
    $php_version = phpversion();
    ob_start();
    phpinfo(INFO_MODULES);
    $contents = ob_get_clean();
    $moduleAvailable = strpos($contents, 'mod_security') !== false;
    $plugin_conflicted = 0;
    $theme_conflicted = 0;
    $theme = wp_get_theme();
    $spl_license_return = get_option('act_ser_conn_refused');
    $license_key_activated = get_option('spl_license_return');
    $plugins_detected = "";
    $all_in_one_security_detected = "";
    $site_url = site_url();

    function url(){
      if(isset($_SERVER['HTTPS'])){
        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
      }
      else{
        $protocol = 'http';
      }
      return $protocol . "://" . $_SERVER['HTTP_HOST'];
    }

    echo '<div class="scc_title_bar" style="background: #314af3; ">Diagnostics</div>';
    echo '<p style="padding-left:25px;padding-bottom:20px;font-weight:400px;"> Please action any items in red by emailing your admin or hosting company support. Dont worry about orange or yellow items. You may also try sending us a screenshot at https://stylishcostcalculator.com/support/</h1>';



//CURRENT SITE URL AND ACTIVATED THEAM NAME

    echo '<div id="border1"><span style="font-size:1em;font-weight:bold;">Domain URL:</span> ';
    echo url().'<br><span style="font-size:1em;font-weight:bold;">Active Theam: </span>'.wp_get_theme().'<br>';
    echo "</div>";
    $plugin_data = get_plugin_data(SCC_DIR. '/stylish-cost-calculator.php');
    $scc_version = $plugin_data['Version'];

// SCC Version
    if($plugin_data['Version']){
      echo '<div id="border1"><span class="green_dot"></span>  <span style="font-size:1em;font-weight:bold;">SCC Version:</span>';
      echo"  $scc_version <br></div>";
    }

// Mysql version
    global $wpdb;
    $mysqlVersion = $wpdb->db_version();
    if($mysqlVersion){
      echo '<div id="border1"><span class="green_dot"></span>  <span style="font-size:1em;font-weight:bold;">MySQL Version:</span>';
      echo"  $mysqlVersion <br></div>";
    }

// curl present check
    if  (in_array  ('curl', get_loaded_extensions())) {
     $curl_present = "Yes";
   }  else {
     $curl_present = "No";
   }
   if($curl_present){
    echo '<div id="border1"><span class="green_dot"></span>  <span style="font-size:1em;font-weight:bold;">CURL Library Present:</span>';
    echo"  $curl_present <br></div>";
  }

    // PHP VERSION HIGHER THAN 5.0
  if (version_compare($php_version, '7.2', '>=')) {
    echo '<div id="border1"><span class="green_dot"></span>  <span style="font-size:1em;font-weight:bold;">PHP Version:</span>';
    echo"  $php_version <br></div>";
  } else {
    echo '<div id="border1"><span class="red_dot"></span>  <span style="font-size:1em;font-weight:bold;">PHP Version:</span>';
    echo"  $php_version <br><BR>";
    echo "Change your PHP level in your cPanel, or ask your hosting comapny to do so. <br></div>";
  }
    // WP VERSION HIGHER THAN 5.4
  if (version_compare($wp_version, '5.4', '>=')) {
      // WordPress version is greater than 4.3
    echo '<div id="border1"><span class="green_dot"></span> <span style="font-size:1em;font-weight:bold;">WordPress Version:</span>';
    echo"  $wp_version</div>";
  } else {
    echo '<div id="border1"><span class="red_dot"></span> <span style="font-size:1em;font-weight:bold;">WordPress Version:</span>';
    echo"  $wp_version <br><br>  ";
    echo "Your WordPress core version is really out-dated. Please backup, then upgrade.<br></div>";
  }
    // MOD SECURITY IS OFF
  if ($moduleAvailable == NULL) {
    echo '<div id="border1"><span class="green_dot"></span> <span style="font-size:1em;font-weight:bold;">MOD Security:</span> Off </div> ';
  } else {
    echo '<div id="border1"><span class="red_dot"></span> <span style="font-size:1em;font-weight:bold;">MOD Security:</span>';
    echo"  $moduleAvailable <br> </div>";
  }
    // Check if aWP Forms SMTP plugin is not activated

  if ( is_plugin_active( 'wp-mail-smtp/wp_mail_smtp.php') == false) {
    echo '<div id="border1"><span class="yellow_dot"></span> <span style="font-size:1em;font-weight:bold;">Email Delivery: </span>';
    echo "<br><br>Right now your site is setup to send emails via PHP, this is not a ideal and can cause your email quote PDF forms to be sent to your users spam folder. We recommend to install the <a href='https://en-ca.wordpress.org/plugins/wp-mail-smtp/' target='_blank'f>WP Forms SMTP plugin</a>.<br><br>";
    echo "</div>";
  }
    // Check if any plugins are conflicted
  if ( is_plugin_active( 'siteorigin-panels/siteorigin-panels.php' ) ) {
    $plugin_conflicted = $plugin_conflicted + 1;
    $plugins_detected = "Page Builder by SiteOrigin";
  }
    // Check If beacer page builder Activated
  if ( is_plugin_active( 'beaver-builder-lite-version/fl-builder.php' ) ) {
    $plugin_conflicted = $plugin_conflicted + 1;
    $plugins_detected = "WordPress Page Builder � Beaver Builder";
  }
    // Check If wordfence Activated
  if ( is_plugin_active( 'wordfence/wordfence.php' ) ) {
    $plugin_conflicted = $plugin_conflicted + 1;
    $wordfence_detected = "Wordfence Security";
  }
  // Check If wpFastestCache ( Mike )
  if ( is_plugin_active( 'wp-fastest-cache/wpFastestCache.php' ) ) {
    echo '<div id="border1"><span class="yellow_dot"></span> <span style="font-size:1em;font-weight:bold;">Potential Conflict: </span>';
    echo "WP Fastest Cache<br><br>";
    echo 'Warning: You are using a cache plugin. You should read <a href="https://designful.freshdesk.com/support/solutions/articles/48000950262-important-information-regarding-cache-plugins" target="_blank"> this page.</a></br>';
    echo "</div>";
  }
  // Check if Autoptimize ( Mike )
  if ( is_plugin_active( 'autoptimize/autoptimize.php' ) ) {
    echo '<div id="border1"><span class="yellow_dot"></span> <span style="font-size:1em;font-weight:bold;">Potential Conflict: </span>';
    echo "Autoptimize<br><br>";
    echo 'Warning: You are using a cache plugin. You should read <a href="https://designful.freshdesk.com/support/solutions/articles/48000950262-important-information-regarding-cache-plugins" target="_blank"> this page.</a></br>';
    echo "</div>";
  }
  // Check if W3 Total Cache ( Mike )
  if ( is_plugin_active( 'w3-total-cache/w3-total-cache.php' ) ) {
    echo '<div id="border1"><span class="yellow_dot"></span> <span style="font-size:1em;font-weight:bold;">Potential Conflict: </span>';
    echo "W3 Total Cache<br><br>";
    echo 'Warning: You are using a cache plugin. You should read <a href="https://designful.freshdesk.com/support/solutions/articles/48000950262-important-information-regarding-cache-plugins" target="_blank"> this page.</a></br>';
    echo "</div>";
  }
    // Check If All In One Security Activated
  if ( is_plugin_active( 'all-in-one-wp-security-and-firewall/wp-security.php' ) ) {
    $plugin_conflicted = $plugin_conflicted + 1;
    $all_in_one_security_detected = "All In One WP Security";
  }

  if ($plugin_conflicted == NULL) {
    echo '<div id="border1"><span class="green_dot"></span> <span style="font-size:1em;font-weight:bold;">Conflicted Plugins:</span> None </div>';
  } else {
    if($wordfence_detected == "Wordfence Security" && ($plugins_detected == "Page Builder by SiteOrigin" || $plugins_detected == "WordPress Page Builder � Beaver Builder")){
      echo '<div id="border1"><span class="yellow_dot"></span> <span style="font-size:1em;font-weight:bold;">Conflicted Plugins:</span>';
      echo" $plugin_conflicted<br><br>";
      echo "Warning: You are using a page builder. Please make sure you are using the correct container size and widget to add our shortcode to.</br>";
      echo "Warning: Detected WordFence plugin. You must whitelist our plugin.<br>";
      echo "</div>";
    }elseif(($wordfence_detected == "Wordfence Security" && $plugins_detected == NULL) || ($all_in_one_security_detected == "All In One WP Security" && $plugins_detected == NULL)){
      echo '<div id="border1"><span class="yellow_dot"></span> <span style="font-size:1em;font-weight:bold;">Conflicted Plugins:</span>';
      echo" $plugin_conflicted<br><Br>";
      if($all_in_one_security_detected != '' && $wordfence_detected != ''){
        $plugis_detect = $wordfence_detected. ', ' .$all_in_one_security_detected;
      }elseif($all_in_one_security_detected != '' && $wordfence_detected == ''){
        $plugis_detect = $all_in_one_security_detected;
      }else{
        $plugis_detect = $wordfence_detected;
      }
      echo "Warning: Detected $plugis_detect plugin. You must whitelist our plugin.<br>";
      echo "</div>";
    }else{
      echo '<div id="border1"><span class="yellow_dot"></span> <span style="font-size:1em;font-weight:bold;">Conflicted Plugins:</span>';
      echo" $plugin_conflicted<br>";
      echo "Warning: You are using a page builder. Please make sure you are using the correct container size and widget to add our shortcode to.";
      echo "</div>";
    }
  }

  if ($spl_license_return == "connection refused" && $license_key_activated != 1) {
    echo '<div id="border1"><span class="red_dot"></span> <span style="font-size:1em;font-weight:bold;">Pingback to server:</span>';
    echo "Cannot ping our activation server. Ask hosting company to add our I.P to the whitelist. IP = 104.152.168.28";
    echo '</div>';
  }else{
    echo '<div id="border1"><span class="green_dot"></span> <span style="font-size:1em;font-weight:bold;">Pingback to server:</span> Successful';
    echo '</div>';
  }

  if ($test_char == "utf8mb4") {
    echo '<div id="border1"><span class="green_dot"></span> <span style="font-size:1em;font-weight:bold;">Charset:</span>';
    echo " $test_char";
    echo '</div>';
  }elseif($test_char == "utf8") {
    echo '<div id="border1"><span class="yellow_dot"></span> <span style="font-size:1em;font-weight:bold;">Charset:</span>';
    echo " $test_char";
    echo "<br><Br>Suggestion: You should edit the DB_CHARSET variable in your wp_config.php file to utf8mb4";
    echo '</div>';
  }else{
    echo '<div id="border1"><span class="red_dot"></span> <span style="font-size:1em;font-weight:bold;">Charset:</span>';
    echo " $test_char";
    echo "<br><Br>Warning: You should edit the DB_CHARSET variable in your wp_config.php file to utf8mb4";
    echo '</div>';
  }

  include_once SCC_DIR . '/admin/logo-footer.php';
}

function admin_menu()
{
  add_submenu_page( 'scc-tabs', __( 'Diagnostic', 'stylishpl' ), __( 'Diagnostic', 'stylishpl' ), 'manage_options', 'stylish_cost_calculator_Diagnostic', array( $this, 'diagnostic_page' ) );
}
}
$stylish_cost_calculator_Diagnostic = new Stylish_Cost_Calculator_Diagnostic();
?>