<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://orphic.ca
 * @since      1.0.0
 *
 * @package    Wb_Onboard
 * @subpackage Wb_Onboard/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Wb_Onboard
 * @subpackage Wb_Onboard/public
 * @author     Orphic Inc. <contact@orphic.ca>
 */
class Wb_Onboard_Public
{

  /**
   * The ID of this plugin.
   *
   * @since    1.0.0
   * @access   private
   * @var      string    $plugin_name    The ID of this plugin.
   */
  private $plugin_name;

  /**
   * The version of this plugin.
   *
   * @since    1.0.0
   * @access   private
   * @var      string    $version    The current version of this plugin.
   */
  private $version;

  /**
   * Initialize the class and set its properties.
   *
   * @since    1.0.0
   * @param      string    $plugin_name       The name of the plugin.
   * @param      string    $version    The version of this plugin.
   */
  public function __construct($plugin_name, $version)
  {

    $this->plugin_name = $plugin_name;
    $this->version = $version;
  }



  public function wb_add_script_to_footer()
  {
    echo '<div style="display:none;">miocommerce-monitoring-keyword</div>';
  }


  /**
   * Register the stylesheets for the public-facing side of the site.
   *
   * @since    1.0.0
   */
  public function enqueue_styles()
  {

    /**
     * This function is provided for demonstration purposes only.
     *
     * An instance of this class should be passed to the run() function
     * defined in Wb_Onboard_Loader as all of the hooks are defined
     * in that particular class.
     *
     * The Wb_Onboard_Loader will then create the relationship
     * between the defined hooks and the functions defined in this
     * class.
     */

    wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/wb-onboard-public.css', array(), $this->version, 'all');
  }

  /**
   * Register the JavaScript for the public-facing side of the site.
   *
   * @since    1.0.0
   */
  public function enqueue_scripts()
  {

    /**
     * This function is provided for demonstration purposes only.
     *
     * An instance of this class should be passed to the run() function
     * defined in Wb_Onboard_Loader as all of the hooks are defined
     * in that particular class.
     *
     * The Wb_Onboard_Loader will then create the relationship
     * between the defined hooks and the functions defined in this
     * class.
     */

    wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/wb-onboard-public.js', array('jquery'), $this->version, false);
  }

  public function wb_enqueue_login_styles()
  {
    wp_enqueue_style('wb-custom-login-styles', plugin_dir_url(__FILE__) . 'css/wb-onboard-login-styles.css', array(), $this->version, 'all');
  }

  public function wb_customize_admin_bar()
  {
    if (is_user_logged_in()) {
      $user = wp_get_current_user();
      $role = (array) $user->roles;

      if (isset($role[0]) && $role[0] == 'wb_onboarding_user') {
        global $wp_admin_bar;
        $wp_admin_bar->remove_node('wp-logo');
        // $wp_admin_bar->remove_node( 'my-account' );
        $wp_admin_bar->remove_node('search');
        $wp_admin_bar->remove_node('dashboard');

        // edit site name node
        $site_name_node = $wp_admin_bar->get_node('site-name');
        $site_name_node->title = 'Edit site';
        $wp_admin_bar->remove_node('site-name');
        $wp_admin_bar->add_node($site_name_node);
      }
    }
  }

  public function wb_block_frontend()
  {
    if (get_option('wb_onboard_website_is_blocked')) {
      include_once WB_ONBOARD_PLUGIN_DIR_PATH . 'public/partials/wb-onboard-site-suspended-frontend.php';
      die();
    }
    
    if (!isset($_GET['is_preview']) && !get_option('wb_is_done_creating_site') && !current_user_can('manage_options') && !(isset($_SERVER['HTTP_SEC_FETCH_DEST']) && $_SERVER['HTTP_SEC_FETCH_DEST'] == 'iframe')) {
      include_once  WB_ONBOARD_PLUGIN_DIR_PATH . '/public/partials/wb-onboard-temp-frontend.php';
      die();
    }
  }
}
