/**
  * @singleton method used to instantiate class object
  * @access    public
  * @param \EE_Message_Resource_Manager $Message_Resource_Manager
  * @return \EE_Message_Factory instance
  */
 public static function instance(EE_Message_Resource_Manager $Message_Resource_Manager)
 {
     // check if class object is instantiated, and instantiated properly
     if (!self::$_instance instanceof EE_Message_Factory) {
         self::$_instance = new EE_Message_Factory($Message_Resource_Manager);
     }
     return self::$_instance;
 }
 /**
  * generates an EE_Message using the supplied arguments and some defaults
  *
  * @param array $properties
  * @return string
  */
 protected function _generate_message($properties = array())
 {
     $message = EE_Message_Factory::create(array_merge(array('MSG_messenger' => $this->_messenger_name, 'MSG_message_type' => $this->_message_type_name, 'MSG_context' => $this->_context, 'STS_ID' => $this->_status), $properties));
     // validate the message, and if it's good, set some properties
     try {
         $message->is_valid_for_sending_or_generation(true);
         $this->_valid = true;
         $this->_messenger = $message->messenger_object();
         $this->_message_type = $message->message_type_object();
         $this->_send_now = $message->send_now();
     } catch (Exception $e) {
         $this->_valid = false;
         $this->_error_msg[] = $e->getMessage();
     }
     return $message;
 }
 /**
  * @param string $context   The context for the generated message.
  * @param EE_Messages_Addressee $recipient
  * @param array  $templates  formatted array of templates used for parsing data.
  * @param EE_Message_Template_Group $message_template_group
  * @return EE_Message | bool  (false is used when no EE_Message is generated)
  */
 protected function _setup_message_object($context, EE_Messages_Addressee $recipient, $templates, EE_Message_Template_Group $message_template_group)
 {
     //stuff we already know
     $transaction_id = $recipient->txn instanceof EE_Transaction ? $recipient->txn->ID() : 0;
     $transaction_id = empty($transaction_id) && $this->_current_data_handler->txn instanceof EE_Transaction ? $this->_current_data_handler->txn->ID() : $transaction_id;
     $message_fields = array('GRP_ID' => $message_template_group->ID(), 'TXN_ID' => $transaction_id, 'MSG_messenger' => $this->_current_messenger->name, 'MSG_message_type' => $this->_current_message_type->name, 'MSG_context' => $context);
     //recipient id and type should be on the EE_Messages_Addressee object but if this is empty, let's try to grab the
     //info from the att_obj found in the EE_Messages_Addressee object.
     if (empty($recipient->recipient_id) || empty($recipient->recipient_type)) {
         $message_fields['MSG_recipient_ID'] = $recipient->att_obj instanceof EE_Attendee ? $recipient->att_obj->ID() : 0;
         $message_fields['MSG_recipient_type'] = 'Attendee';
     } else {
         $message_fields['MSG_recipient_ID'] = $recipient->recipient_id;
         $message_fields['MSG_recipient_type'] = $recipient->recipient_type;
     }
     $message = EE_Message_Factory::create($message_fields);
     //grab valid shortcodes for shortcode parser
     $mt_shortcodes = $this->_current_message_type->get_valid_shortcodes();
     $m_shortcodes = $this->_current_messenger->get_valid_shortcodes();
     //if the 'to' field is empty (messages will ALWAYS have a "to" field, then we get out because that means this
     //context is turned off) EXCEPT if we're previewing
     if (empty($templates['to'][$context]) && !$this->_generation_queue->get_message_repository()->is_preview() && !$this->_current_messenger->allow_empty_to_field()) {
         //we silently exit here and do NOT record a fail because the message is "turned off" by having no "to" field.
         return false;
     }
     $error_msg = array();
     foreach ($templates as $field => $field_context) {
         $error_msg = array();
         //let's setup the valid shortcodes for the incoming context.
         $valid_shortcodes = $mt_shortcodes[$context];
         //merge in valid shortcodes for the field.
         $shortcodes = isset($m_shortcodes[$field]) ? $m_shortcodes[$field] : $valid_shortcodes;
         if (isset($templates[$field][$context])) {
             //prefix field.
             $column_name = 'MSG_' . $field;
             try {
                 $content = $this->_shortcode_parser->parse_message_template($templates[$field][$context], $recipient, $shortcodes, $this->_current_message_type, $this->_current_messenger, $message);
                 $message->set_field_or_extra_meta($column_name, $content);
             } catch (EE_Error $e) {
                 $error_msg[] = sprintf(__('There was a problem generating the content for the field %s: %s', 'event_espresso'), $field, $e->getMessage());
                 $message->set_STS_ID(EEM_Message::status_failed);
             }
         }
     }
     if ($message->STS_ID() === EEM_Message::status_failed) {
         $error_msg = __('There were problems generating this message:', 'event_espresso') . "\n" . implode("\n", $error_msg);
         $message->set_error_message($error_msg);
     } else {
         $message->set_STS_ID(EEM_Message::status_idle);
     }
     return $message;
 }
 /**
  * This returns all the current urls for EE_Message actions.
  *
  * @since 4.9.0
  *
  * @param  EE_Message   $message    The EE_Message object required to generate correct urls for some types.
  * @param  array    $query_params   Any additional query_params to be included with the generated url.
  *
  * @return array
  */
 public static function get_message_action_urls(EE_Message $message = null, $query_params = array())
 {
     EE_Registry::instance()->load_helper('URL');
     //if $message is not an instance of EE_Message then let's just do a dummy.
     $message = empty($message) ? EE_Message_Factory::create() : $message;
     $action_urls = apply_filters('FHEE__EEH_MSG_Template__get_message_action_url', array('view' => EEH_MSG_Template::generate_browser_trigger($message), 'error' => EEH_MSG_Template::generate_error_display_trigger($message), 'see_notifications_for' => EEH_URL::add_query_args_and_nonce(array_merge(array('page' => 'espresso_messages', 'action' => 'default', 'filterby' => 1), $query_params), admin_url('admin.php')), 'generate_now' => EEH_URL::add_query_args_and_nonce(array('page' => 'espresso_messages', 'action' => 'generate_now', 'MSG_ID' => $message->ID()), admin_url('admin.php')), 'send_now' => EEH_URL::add_query_args_and_nonce(array('page' => 'espresso_messages', 'action' => 'send_now', 'MSG_ID' => $message->ID()), admin_url('admin.php')), 'queue_for_resending' => EEH_URL::add_query_args_and_nonce(array('page' => 'espresso_messages', 'action' => 'queue_for_resending', 'MSG_ID' => $message->ID()), admin_url('admin.php'))));
     if ($message->TXN_ID() > 0 && EE_Registry::instance()->CAP->current_user_can('ee_read_transaction', 'espresso_transactions_default', $message->TXN_ID())) {
         $action_urls['view_transaction'] = EEH_URL::add_query_args_and_nonce(array('page' => 'espresso_transactions', 'action' => 'view_transaction', 'TXN_ID' => $message->TXN_ID()), admin_url('admin.php'));
     } else {
         $action_urls['view_transaction'] = '';
     }
     return $action_urls;
 }
 /**
  * @since      4.5.0
  * @deprecated 4.9.0   Moved to EED_Messages Module
  * @param string   $messenger    a string matching a valid active messenger in the system
  * @param string   $message_type Although it seems contrary to the name of the method, a message type name is still required to send along the message type to the messenger because this is used for determining what specific variations might be loaded for the generated message.
  * @param stdClass $message      a stdClass object in the format expected by the messenger.
  *
  * @return bool          success or fail.
  */
 public function send_message_with_messenger_only($messenger, $message_type, $message)
 {
     // EE_messages has been deprecated
     $this->_class_is_deprecated(__FUNCTION__);
     //setup for sending to new method.
     /** @type EE_Messages_Queue $queue */
     $queue = EE_Registry::instance()->load_lib('Messages_Queue');
     //make sure we have a proper message object
     if (!$message instanceof EE_Message && is_object($message) && isset($message->content)) {
         $msg = EE_Message_Factory::create(array('MSG_messenger' => $messenger, 'MSG_message_type' => $message_type, 'MSG_content' => $message->content, 'MSG_subject' => $message->subject));
     } else {
         $msg = $message;
     }
     if (!$msg instanceof EE_Message) {
         return false;
     }
     //make sure any content in a content property (if not empty) is set on the MSG_content.
     if (!empty($msg->content)) {
         $msg->set('MSG_content', $msg->content);
     }
     $queue->add($msg);
     return EED_Messages::send_message_with_messenger_only($messenger, $message_type, $queue);
 }
 /**
  * @param mixed $cols_n_values
  * @return \EE_Message
  */
 public function instantiate_class_from_array_or_object($cols_n_values)
 {
     /** @type EE_Message $message */
     $message = parent::instantiate_class_from_array_or_object($cols_n_values);
     if ($message instanceof EE_Message) {
         return EE_Message_Factory::set_messenger_and_message_type($message);
     }
     return null;
 }