/**
  * unsets the active if we can't find the file (failsafe)
  *
  * @access private
  * @param  string $active_name name of messenger or message type
  * @param  string $kind        messenger or message_type?
  * @return void
  */
 private function _unset_active($active_name, $kind)
 {
     //pluralize
     $active_messengers = EEH_MSG_Template::get_active_messengers_in_db();
     EE_Registry::instance()->load_helper('MSG_Template');
     if ($kind == 'messenger') {
         unset($active_messengers[$active_name]);
         EEH_MSG_Template::update_to_inactive($active_name);
         if (isset($this->_active_messengers[$active_name])) {
             unset($this->_active_messengers[$active_name]);
         }
     } else {
         foreach ($active_messengers as $messenger => $settings) {
             if (!empty($settings['settings'][$messenger . '-message_types'][$active_name])) {
                 unset($active_messengers[$messenger]['settings'][$messenger . '-message_types'][$active_name]);
             }
         }
         EEH_MSG_Template::update_to_inactive('', $active_name);
         if (isset($this->_active_message_types[$active_name])) {
             unset($this->_active_message_types[$active_name]);
         }
     }
     EEH_MSG_Template::update_active_messengers_in_db($active_messengers);
 }
 /**
  * This simply validates active messengers and message types to ensure they actually match installed messengers and message types.  If there's a mismatch then we deactivate the messenger/message type and ensure all related db rows are set inactive.
  *
  * @since 4.3.1
  *
  * @return void
  */
 public static function validate_messages_system()
 {
     //include our helper
     EE_Registry::instance()->load_helper('MSG_Template');
     //get active and installed  messengers/message types.
     $active_messengers = EEH_MSG_Template::get_active_messengers_in_db();
     $installed = EEH_MSG_Template::get_installed_message_objects();
     $installed_messengers = $installed_mts = array();
     //set up the arrays so they can be handled easier.
     foreach ($installed['messengers'] as $im) {
         if ($im instanceof EE_messenger) {
             $installed_messengers[$im->name] = $im;
         }
     }
     foreach ($installed['message_types'] as $imt) {
         if ($imt instanceof EE_message_type) {
             $installed_mts[$imt->name] = $imt;
         }
     }
     //now let's loop through the active array and validate
     foreach ($active_messengers as $messenger => $active_details) {
         //first let's see if this messenger is installed.
         if (!isset($installed_messengers[$messenger])) {
             //not set so let's just remove from actives and make sure templates are inactive.
             unset($active_messengers[$messenger]);
             EEH_MSG_Template::update_to_inactive($messenger);
             continue;
         }
         //messenger is active, so let's just make sure that any active message types not installed are deactivated.
         $mts = !empty($active_details['settings'][$messenger . '-message_types']) ? $active_details['settings'][$messenger . '-message_types'] : array();
         foreach ($mts as $mt_name => $mt) {
             if (!isset($installed_mts[$mt_name])) {
                 unset($active_messengers[$messenger]['settings'][$messenger . '-message_types'][$mt_name]);
                 EEH_MSG_Template::update_to_inactive($messenger, $mt_name);
             }
         }
     }
     //all done! let's update the active_messengers.
     EEH_MSG_Template::update_active_messengers_in_db($active_messengers);
     do_action('AHEE__EEH_Activation__validate_messages_system');
     return;
 }
 /**
  * This deregisters a message type that was previously registered with a specific mtname.
  *
  * @since    4.3.0
  *
  * @param string  $mt_name the name for the message type that was previously registered
  * @return void
  */
 public static function deregister($mt_name = NULL)
 {
     if (!empty(self::$_ee_message_type_registry[$mt_name])) {
         //let's make sure that we remove any place this message type was made active
         EE_Registry::instance()->load_helper('MSG_Template');
         $active_messengers = EEH_MSG_Template::get_active_messengers_in_db();
         foreach ($active_messengers as $messenger => $settings) {
             if (!empty($settings['settings'][$messenger . '-message_types'][$mt_name])) {
                 unset($active_messengers[$messenger]['settings'][$messenger . '-message_types'][$mt_name]);
             }
         }
         EEH_MSG_Template::update_to_inactive('', $mt_name);
         EEH_MSG_Template::update_active_messengers_in_db($active_messengers);
         unset(self::$_ee_message_type_registry[$mt_name]);
     }
 }