/**
  * 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;
 }