/**
  * EE_Messages_Template_Defaults constructor.
  *
  * @param EE_messenger               $messenger
  * @param EE_message_type            $message_type
  * @param int                        $GRP_ID                      Optional.  If included then we're just regenerating
  *                                                                the template fields for the given group not the
  *                                                                message template group itself
  * @param EEM_Message_Template_Group $message_template_group_model
  * @param EEM_Message_Template       $message_template_model
  * @throws EE_Error
  */
 public function __construct(EE_messenger $messenger, EE_message_type $message_type, $GRP_ID = 0, EEM_Message_Template_Group $message_template_group_model, EEM_Message_Template $message_template_model)
 {
     $this->_messenger = $messenger;
     $this->_message_type = $message_type;
     $this->_GRP_ID = $GRP_ID;
     //set the model object
     $this->_message_template_group_model = $message_template_group_model;
     $this->_message_template_model = $message_template_model;
     $this->_fields = $this->_messenger->get_template_fields();
     $this->_contexts = $this->_message_type->get_contexts();
 }
 /**
  * This takes the incoming messenger and message type objects, uses them to get the set fields and contexts, then attempts to retrieve the templates matching those for this given template pack.
  *
  * @since 4.5.0
  *
  * @param EE_messenger      $messenger
  * @param EE_message_type $message_type
  *
  * @return array          Returns an multi-level associative array indexed by template context and field in the format:
  *                                array( 'context' => array( 'field' => 'value', 'another-field', 'value' ) );
  */
 protected function _get_templates(EE_messenger $messenger, EE_message_type $message_type)
 {
     $templates = array();
     /**
      * Retrieving the default pack for later usage of default templates for template packs that
      * are NOT the default pack ( or an extension of the default pack ).
      * We ONLY set this variable to be the default pack IF the loaded class is NOT the default
      * pack.  This prevents recursion in _get_specific_template().  The intention is that for
      * template packs that are NOT default packs, we use the default template pack to provide
      * the final fallback templates if there aren't any defined for the called template pack.
      *
      * @type EE_Messages_Template_Pack_Default | null $default_pack
      */
     $default_pack = !$this instanceof EE_Messages_Template_Pack_Default ? new EE_Messages_Template_Pack_Default() : null;
     $fields = $messenger->get_template_fields();
     $contexts = $message_type->get_contexts();
     foreach ($contexts as $context => $details) {
         foreach ($fields as $field => $field_details) {
             if (empty($field_details)) {
                 continue;
             }
             /**
              * is this a field array (linked to a main field)?
              */
             if ($field == 'extra') {
                 foreach ($field_details as $main_field => $sub_fields) {
                     foreach ($sub_fields as $sub_field => $sub_field_details) {
                         //make sure that the template_field_ref matches what the main template field is for this template group.
                         $template_field_ref = $sub_field == 'main' ? $main_field : $sub_field;
                         $templates[$context][$main_field][$sub_field] = $this->_get_specific_template($default_pack, $messenger, $message_type, $template_field_ref, $context);
                     }
                 }
             } else {
                 $templates[$context][$field] = $this->_get_specific_template($default_pack, $messenger, $message_type, $field, $context);
             }
         }
     }
     $templates = apply_filters('FHEE__EE_Template_Pack___get_templates__templates', $templates, $messenger, $message_type, $this);
     $this->_templates[$messenger->name][$message_type->name] = $templates;
     return $templates;
 }
 /**
  * This does some validation of incoming params, determines what type of url is being prepped and returns the
  * appropriate url trigger
  *
  * @param EE_message_type $message_type
  * @param EE_Message $message
  * @param EE_Registration | null $registration  The registration object must be included if this
  *                                              is going to be a registration trigger url.
  * @param string $sending_messenger             The (optional) sending messenger for the url.
  *
  * @return string
  * @throws EE_Error
  */
 public static function get_url_trigger(EE_message_type $message_type, EE_Message $message, $registration = null, $sending_messenger = '')
 {
     //first determine if the url can be to the EE_Message object.
     if (!$message_type->always_generate()) {
         return EEH_MSG_Template::generate_browser_trigger($message);
     }
     //if $registration object is not valid then exit early because there's nothing that can be generated.
     if (!$registration instanceof EE_Registration) {
         throw new EE_Error(__('Incoming value for registration is not a valid EE_Registration object.', 'event_espresso'));
     }
     //validate given context
     $contexts = $message_type->get_contexts();
     if ($message->context() !== '' && !isset($contexts[$message->context()])) {
         throw new EE_Error(sprintf(__('The context %s is not a valid context for %s.', 'event_espresso'), $message->context(), get_class($message_type)));
     }
     //valid sending messenger but only if sending messenger set.  Otherwise generating messenger is used.
     if (!empty($sending_messenger)) {
         $with_messengers = $message_type->with_messengers();
         if (!isset($with_messengers[$message->messenger()]) || !in_array($sending_messenger, $with_messengers[$message->messenger()])) {
             throw new EE_Error(sprintf(__('The given sending messenger string (%1$s) does not match a valid sending messenger with the %2$s.  If this is incorrect, make sure that the message type has defined this messenger as a sending messenger in its $_with_messengers array.', 'event_espresso'), $sending_messenger, get_class($message_type)));
         }
     } else {
         $sending_messenger = $message->messenger();
     }
     return EEH_MSG_Template::generate_url_trigger($sending_messenger, $message->messenger(), $message->context(), $message->message_type(), $registration, $message->GRP_ID());
 }