/**
  * 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();
 }
 /**
  * constructor
  *
  * @access public
  * @return void
  */
 public function __construct()
 {
     //set name and description properties
     $this->name = 'email';
     $this->description = __('This messenger delivers messages via email using the built-in <code>wp_mail</code> function included with WordPress', 'event_espresso');
     $this->label = array('singular' => __('email', 'event_espresso'), 'plural' => __('emails', 'event_espresso'));
     //we're using defaults so let's call parent constructor that will take care of setting up all the other properties
     parent::__construct();
 }
 /**
  * Takes care of enqueuing any necessary scripts or styles for the page.  A do_action() so message types using this messenger can add their own js.
  *
  * @return void.
  */
 public function enqueue_scripts_styles()
 {
     parent::enqueue_scripts_styles();
     do_action('AHEE__EE_Pdf_messenger__enqueue_scripts_styles');
 }
 /**
  * This just returns any existing test settings that might be saved in the database
  *
  * @access public
  * @return array
  */
 public function get_existing_test_settings()
 {
     $settings = parent::get_existing_test_settings();
     //override subject if present because we always want it to be fresh.
     if (is_array($settings) && !empty($settings['subject'])) {
         $settings['subject'] = sprintf(__('Test email sent from %s', 'event_espresso'), get_bloginfo('name'));
     }
     return $settings;
 }
 /**
  * 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;
 }
 /**
  *  Executes sending a message via the given sending messenger (but generated via the given generating messenger).
  *
  * @since 4.5.0
  *
  * @param EE_messenger $generating_messenger The messenger used for generating messages
  * @param EE_message_type $message_type         The message type used for generating messages
  * @param mixed  $data                 Data provided for parsing shortcodes in message templates.
  * @param EE_messenger $sending_messenger    The messenger that will be used for SENDING the messages.
  * @param bool   $context              If provided, then a specific context for a given template will be sent.
  * @param bool  $send 			       Default TRUE.  If false, then this will just return the generated EE_Messages std_Class objects which might be used by the trigger to setup a batch message (typically html messenger uses it).
  *
  * @return mixed(bool|std_Class[])
  */
 private function _send_message(EE_messenger $generating_messenger, EE_message_type $message_type, $data, EE_messenger $sending_messenger, $context = FALSE, $send = TRUE)
 {
     //can't even get started yo!
     if ($message_type === FALSE || is_wp_error($message_type) || $message_type->set_messages($data, $generating_messenger, $context) === FALSE) {
         return FALSE;
     }
     // if the generating messenger and the sending messengers are different...
     // then are there any hooks that the generating messenger sets for the sending messenger (i.e. css file swap outs etc.)
     if ($sending_messenger != $generating_messenger) {
         $generating_messenger->do_secondary_messenger_hooks($sending_messenger->name);
     }
     //it is possible that the user has the messenger turned off for this type.
     if ($message_type->count === 0) {
         return FALSE;
     }
     //are we just sending the EE_Messages stdClass objects back?
     if (!$send) {
         return $message_type->messages;
     }
     //TODO: check count (at some point we'll use this to decide whether we send to queue or not i.e.
     //if ( $message_type->count > 1000 ) ... do something
     //else...
     $success = TRUE;
     // $success is a flag for the loop.  If there is NO error then everything is a success (true) otherwise it wasn't a success (false)
     foreach ($message_type->messages as $message) {
         //todo: should we do some reporting on messages gone out at some point?  I think we could have the $active_messenger object return bool for whether message was sent or not and we can compile a report based on that.
         // if messages send successfully then $success retains it's value, but a single fail will toggle it to FALSE
         $success = $sending_messenger->send_message($message, $message_type) === TRUE ? $success : FALSE;
     }
     unset($message_type);
     return $success;
 }
 /**
  * Setup appropriate response for activating a messenger and/or message types
  *
  * @param EE_messenger         $messenger
  * @param EE_message_type|null $message_type
  *
  * @return bool
  * @throws EE_Error
  */
 protected function _setup_response_message_for_activating_messenger_with_message_types($messenger, EE_Message_Type $message_type = null)
 {
     //if $messenger isn't a valid messenger object then get out.
     if (!$messenger instanceof EE_Messenger) {
         EE_Error::add_error(__('The messenger being activated is not a valid messenger', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
         return false;
     }
     //activated
     if ($this->_template_args['data']['active_mts']) {
         EE_Error::overwrite_success();
         //activated a message type with the messenger
         if ($message_type instanceof EE_message_type) {
             EE_Error::add_success(sprintf(__('%s message type has been successfully activated with the %s messenger', 'event_espresso'), ucwords($message_type->label['singular']), ucwords($messenger->label['singular'])));
             //if message type was invoice then let's make sure we activate the invoice payment method.
             if ($message_type->name == 'invoice') {
                 EE_Registry::instance()->load_lib('Payment_Method_Manager');
                 $pm = EE_Payment_Method_Manager::instance()->activate_a_payment_method_of_type('Invoice');
                 if ($pm instanceof EE_Payment_Method) {
                     EE_Error::add_attention(__('Activating the invoice message type also automatically activates the invoice payment method.  If you do not wish the invoice payment method to be active, or to change its settings, visit the payment method admin page.', 'event_espresso'));
                 }
             }
             //just toggles the entire messenger
         } else {
             EE_Error::add_success(sprintf(__('%s messenger has been successfully activated', 'event_espresso'), ucwords($messenger->label['singular'])));
         }
         return true;
         //possible error condition. This will happen when our active_mts data is empty because it is validated for actual active
         //message types after the activation process.  However its possible some messengers don't HAVE any default_message_types
         //in which case we just give a success message for the messenger being successfully activated.
     } else {
         if (!$messenger->get_default_message_types()) {
             //messenger doesn't have any default message types so still a success.
             EE_Error::add_success(sprintf(__('%s messenger was successfully activated.', 'event_espresso'), ucwords($messenger->label['singular'])));
             return true;
         } else {
             EE_Error::add_error($message_type instanceof EE_message_type ? sprintf(__('%s message type was not successfully activated with the %s messenger', 'event_espresso'), ucwords($message_type->label['singular']), ucwords($messenger->label['singular'])) : sprintf(__('%s messenger was not successfully activated', 'event_espresso'), ucwords($messenger->label['singular'])), __FILE__, __FUNCTION__, __LINE__);
             return false;
         }
     }
 }
 /**
  * Activates given message types for the given EE_messenger object.
  *
  * Note: (very important) This method does not persist the activation to the database.
  * See code implementing this method in this class for examples of how to persist.
  *
  * @param \EE_messenger $messenger
  * @param  array        $message_type_names
  *
  * @return array
  */
 protected function _activate_message_types(EE_messenger $messenger, $message_type_names = array())
 {
     //If $message_type_names is empty, AND $this->_active_message_types is empty, then that means
     //things have never been initialized (which should happen on EEH_Activation::generate_message_templates).
     //So ONLY then do we need to actually grab defaults and cycle through them.  Otherwise we
     //only override _active_message_types when an explicit array of $message_type_names has been provided.
     $message_type_names = empty($message_type_names) && !isset($this->_active_message_types[$messenger->name]) ? $messenger->get_default_message_types() : (array) $message_type_names;
     //now we ALWAYS need to make sure that the messenger is active for the message types we're activating!
     if (!isset($this->_active_message_types[$messenger->name])) {
         $this->_active_message_types[$messenger->name]['settings'] = array();
     }
     if ($message_type_names) {
         // cycle thru message types
         foreach ($message_type_names as $message_type_name) {
             //only register the message type as active IF it isn't already active
             //and if its actually installed.
             if (!$this->is_message_type_active_for_messenger($messenger->name, $message_type_name)) {
                 $this->add_settings_for_message_type($messenger->name, $message_type_name);
                 $this->_set_messenger_has_activated_message_type($messenger, $message_type_name);
             }
         }
     }
     return $message_type_names;
 }
 /**
 * Executes the send method on the provided messenger
 *
 *@param EE_Message            $message
 * @param EE_messenger    $messenger
 * @param EE_message_type $message_type
 * @return bool true means all went well, false means, not so much.
 */
 protected function _do_send(EE_Message $message, EE_messenger $messenger, EE_message_type $message_type)
 {
     if ($messenger->send_message($message, $message_type)) {
         $message->set_STS_ID(EEM_Message::status_sent);
         return true;
     } else {
         $message->set_STS_ID(EEM_Message::status_retry);
         return false;
     }
 }