<?php
/*
 Plugin Name:DialBunny
 Plugin URI: https://dialbunny.com/
 Description: Easily manage inbound and outbound phone calls and text messages
 Version: 1.2.3
 Author:DialBunny
 Author URI: https://dialbunny.com
 Requires at least: 6.2
 Requires PHP: 7.4
 License: GPLv2 or later
 Text Domain: dialbunny
 */

 if ( ! defined( 'ABSPATH' ) ) exit;

 // Autoload files
 require_once( plugin_dir_path( __FILE__ ) .'/vendor/twilio-php-main/src/Twilio/autoload.php');

 // Create Pages on activation
 require(plugin_dir_path(__FILE__) . 'lib/config/initiate_pages.php');
 // Create User Role on activation
 require(plugin_dir_path(__FILE__) . 'lib/config/initiate_user_role.php');
 // Create Tables on activation
 require(plugin_dir_path(__FILE__) . 'lib/config/initiate_tables.php');
 // Create encryption Keys on activation
 require(plugin_dir_path(__FILE__) . 'lib/config/initiate_keys.php');
 // Admin Controller
 require(plugin_dir_path(__FILE__) . 'lib/controllers/admin.php');
 // Calls Controller
 require(plugin_dir_path(__FILE__) . 'lib/controllers/messages.php');
 // Dashboard Controller
 require(plugin_dir_path(__FILE__) . 'lib/controllers/dashboard.php');
 // Token Controller
 require(plugin_dir_path(__FILE__) . 'lib/controllers/webcalling.php');

 class DialBunnyClass
 {
   public function __construct()
   {
     // Change styling for DialBunny plugin dashboard
     add_action('admin_enqueue_scripts', array($this,'dialbunny_load_custom_blank_page_styles'), 999);

     // Only apply the Bootstrap "light" theme on the DialBunny dashboard page
     add_action('admin_enqueue_scripts', function ($hook) {
     if ($hook === 'toplevel_page_dialbunny_dashboard') {
        add_action('admin_head', function () {
            // Set the Bootstrap theme to light
            echo '<script>document.documentElement.setAttribute("data-bs-theme", "light");</script>';
        });
      }
     });

     // Add guest video chat page
     add_action('template_redirect', function () {
      $page_id = get_queried_object_id();
      if ($page_id == get_option('dialbunny_user_guest_video_page') && !empty(get_option('dialbunny_user_guest_video_page'))) {
          include plugin_dir_path(__FILE__) . 'lib/views/dashboard/guestteams.php';
          exit;
      }
     });

     // Handling incoming calls and texts
     add_action( 'admin_post_nopriv_dialbunny_calls_logic', array(new DialBunnyWebCallingController, 'calls_logic') ); // Main incoming calls logic
     add_action( 'admin_post_nopriv_dialbunny_incoming_texts', array(new DialBunnyManageMessagesController, 'incoming_texts') );
     // Get token for browser enabled calling
     add_action( 'admin_post_dialbunny_browser_token', array(new DialBunnyWebCallingController, 'browser_token') );
     add_action( 'admin_post_dialbunny_browser_video_token', array(new DialBunnyWebCallingController, 'browser_video_token') );

     // Add menu inside wordpress admin panel
     add_action( 'admin_menu', array($this, 'dialbunny_admin_page') );
     // Add custom dashboard post requests
     add_action( 'admin_post_dialbunny_update_router_table', array(new DialBunnyDashboardController, 'update_router_table') ); // update basic information in dialbunny_phone_details table
     add_action( 'admin_post_dialbunny_update_ai_assistant', array(new DialBunnyDashboardController, 'update_ai_assistant') );
     add_action( 'admin_post_dialbunny_search_numbers', array(new DialBunnyDashboardController, 'search_numbers') );
     add_action( 'admin_post_dialbunny_request_number', array(new DialBunnyDashboardController, 'request_number') );
     add_action( 'admin_post_dialbunny_re_request', array(new DialBunnyDashboardController, 're_request') );
     add_action( 'admin_post_dialbunny_new_contact', array(new DialBunnyDashboardController, 'new_contact') );
     add_action( 'admin_post_dialbunny_update_contact', array(new DialBunnyDashboardController, 'update_contact') );
     add_action( 'admin_post_dialbunny_remove_contact', array(new DialBunnyDashboardController, 'remove_contact') );
     add_action( 'admin_post_dialbunny_remove_messages', array(new DialBunnyDashboardController, 'remove_messages') );
     add_action( 'admin_post_dialbunny_release_managed_number_user', array(new DialBunnyDashboardController, 'release_managed_number') ); // Gives users the ability to remove their managed number
     add_action( 'admin_post_dialbunny_assistant_setup', array(new DialBunnyDashboardController, 'assistant_setup') );
     add_action( 'admin_post_dialbunny_assistant_answer_calls', array(new DialBunnyDashboardController, 'assistant_answer_calls') );
     add_action( 'admin_post_dialbunny_assistant_answer_texts', array(new DialBunnyDashboardController, 'assistant_answer_texts') );
     add_action( 'admin_post_dialbunny_voice_setup', array(new DialBunnyDashboardController, 'voice_setup') );
     add_action( 'admin_post_dialbunny_send_text', array(new DialBunnyDashboardController, 'send_text') );
     add_action( 'wp_ajax_dialbunny_get_video_call_token', array(new DialBunnyDashboardController, 'get_video_call_token') );
     if (!empty(get_option('dialbunny_user_guest_video_page')) || get_option('dialbunny_user_guest_video_page') != 'dialbunny_disabled_dialbunny') { add_action( 'wp_ajax_nopriv_dialbunny_get_video_call_token', array(new DialBunnyDashboardController, 'get_video_call_token') ); }

     // Add administrator post requests
     add_action( 'admin_post_dialbunny_add_managed_number', array(new DialBunnyAdminController, 'add_managed_number') );
     add_action( 'admin_post_dialbunny_remove_managed_number', array(new DialBunnyAdminController, 'remove_managed_number') );
     add_action( 'admin_post_dialbunny_get_user_id', array(new DialBunnyAdminController, 'get_user_id') );
     add_action( 'admin_post_dialbunny_setup_api_keys', array(new DialBunnyAdminController, 'setup_api_keys') );
     add_action( 'admin_post_dialbunny_additional_setup', array(new DialBunnyAdminController, 'additional_setup') );
     add_action( 'admin_post_dialbunny_update_license_key', array(new DialBunnyAdminController, 'update_license_key') );
     add_action( 'admin_post_dialbunny_auto_setup_api_keys', array(new DialBunnyAdminController, 'auto_setup_api_keys') );

     // Add the database tables
     register_activation_hook( __FILE__, array(new DialBunnyInitiateTablesClass, 'initiate_tables') );
     // Add new role for users that only have access to DialBunny dashboard pages
     register_activation_hook( __FILE__, array(new DialBunnyInitiateUserRoleClass, 'register_user_role') );
   }

   function dialbunny_load_custom_blank_page_styles($hook)
   {
     // This only applies to DialBunny dashboard for a clean interface
     if ($hook == 'toplevel_page_dialbunny_dashboard')
     {
       // Dequeue Latepoint styles in the DialBunny dashboard when the plugin is active
       wp_dequeue_style( 'latepoint-admin' );
       wp_deregister_style( 'latepoint-admin' );
       wp_dequeue_style( 'latepoint-blocks' );
       wp_deregister_style( 'latepoint-blocks' );
       wp_dequeue_style( 'wp-components' );
       wp_deregister_style( 'wp-components' );
       // Dequeue
       wp_dequeue_style( 'dashicons' );
       // Deregister
       wp_deregister_style( 'dashicons' );
       // Remove WordPress admin bar
       remove_all_actions('in_admin_header');
       remove_all_actions('in_admin_footer');
       // Deregister notifications
       remove_all_actions('admin_notices');
       remove_all_actions('all_admin_notices');
       remove_all_actions('user_admin_notices');
       remove_all_actions('network_admin_notices');
       // Enque style
       wp_enqueue_style( 'bootstrap', plugins_url( 'public/css/dialbunny.bootstrap.css', __FILE__ ), array(), '1.0', 'all' );
       wp_enqueue_style( 'fontawesome', plugins_url( 'public/fontawesome/css/fontawesome.css', __FILE__ ), array(), '1.0', 'all' );
       wp_enqueue_style( 'solid', plugins_url( 'public/fontawesome/css/solid.css', __FILE__ ), array(), '1.0', 'all' );
       wp_enqueue_style( 'dialbunny-css', plugins_url( 'public/css/dialbunny.css', __FILE__ ), array(), '1.1', 'all' );
       // Enque scripts
       wp_enqueue_script( 'dialbunny-main', plugins_url( 'public/js/dialbunny.bootstrap.min.js', __FILE__ ), array('jquery'), '1.0', true );
       wp_enqueue_script( 'dialbunny-twilio-min', plugins_url( 'public/js/twilio.min.js', __FILE__ ), array('jquery'), '1.0', true );
       wp_enqueue_script( 'dialbunny-js', plugins_url( 'public/js/dialbunny.js', __FILE__ ), array('jquery'), '1.1', true );
       wp_localize_script('dialbunny-js', 'DialBunnyBrowserCallingVars', array(
          'tokenUrl' => esc_url(admin_url('admin-post.php')),
          'userID'   => get_current_user_id(),
          'user_nonce' => wp_create_nonce('DialBunny'),
       ));
     }
   }

   // Add admin page to Wordpress admin navigation bar
   function dialbunny_admin_page()
   {
     global $wp_roles;
     $wp_roles->add_cap( 'administrator', 'dialbunny_view_dashboard' );
     $wp_roles->add_cap( 'dialbunny', 'dialbunny_view_dashboard' );

     add_menu_page(
       __( 'DialBunny Call Center', 'dialbunny' ),
       'DialBunny',
       'dialbunny_view_dashboard',
       'dialbunny_dashboard',
       function ()
       {
         $dialbunnyDashboardUrl = admin_url('admin.php?page=dialbunny_dashboard');
         include(plugin_dir_path(__FILE__) . 'lib/components/alerts.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/countrycode.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/countries.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/timezone.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/formatnumber.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/timeelapsed.php' );
         include(plugin_dir_path(__FILE__) . 'app.php' );
       },
       'dashicons-phone',
       99
     );
     if ( current_user_can('administrator') )
     {
       add_submenu_page(
       	'dialbunny_dashboard',
       	'DialBunny - Setup',
       	'Settings',
       	'manage_options',
       	'dialbunny_setup',
       	function () { $current_page = 'setup'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/timeelapsed.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/setup.php' ); },
         null
       );
       add_submenu_page(
       	'dialbunny_dashboard',
       	'DialBunny - Info',
       	'Information',
       	'manage_options',
       	'dialbunny_info',
       	function () { $current_page = 'info'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/info.php' ); },
         null
       );
       add_submenu_page(
       	'dialbunny_dashboard',
       	'DialBunny - Active',
       	'Active Numbers',
       	'manage_options',
       	'dialbunny_activenumbers',
       	function () { $current_page = 'activenumbers'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/formatnumber.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/active_numbers.php' ); },
         null
       );
       add_submenu_page(
       	'dialbunny_dashboard',
       	'DialBunny - Released',
       	'Released Numbers',
       	'manage_options',
       	'dialbunny_released',
       	function () { $current_page = 'released'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/formatnumber.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/released.php' ); },
         null
       );
       add_submenu_page(
       	'dialbunny_dashboard',
       	'DialBunny - Add',
       	'Add Number',
       	'manage_options',
       	'dialbunny_add',
       	function () { $current_page = 'add'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/countrycode.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/add.php' ); },
         null
       );
       add_submenu_page(
       	'dialbunny_dashboard',
       	'DialBunny - Remove',
       	'Remove Number',
       	'manage_options',
       	'dialbunny_remove',
       	function () { $current_page = 'remove'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/countrycode.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/remove.php' ); },
         null
       );
       add_submenu_page(
       	'dialbunny_dashboard',
       	'DialBunny - License',
       	'License',
       	'manage_options',
       	'dialbunny_license',
       	function () { $current_page = 'license'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/license.php' ); },
         null
       );
     }
   }

}
new DialBunnyClass;
?>
