public static function send_client_notification($comment_id = 0, $doc, $comment_data = array())
 {
     $user_id = $comment_data['user_id'];
     $client = $doc->get_client();
     if (is_a($client, 'SI_Client')) {
         // Send notification to all client users
         $client_users = SI_Notifications_Control::get_document_recipients($doc);
         foreach (array_unique($client_users) as $user_id) {
             if (!is_wp_error($user_id)) {
                 $to = self::get_user_email($user_id);
                 $data = array('user_id' => $user_id, 'client' => $client, 'comment_data' => $comment_data, 'to' => $to);
                 if (is_a($doc, 'SI_Estimate')) {
                     $data['estimate'] = $doc;
                     self::send_notification('comment_notification_estimate', $data, $to);
                 } elseif (is_a($doc, 'SI_Invoice')) {
                     $data['invoice'] = $doc;
                     self::send_notification('comment_notification_invoice', $data, $to);
                 }
             }
         }
     }
 }
 public static function maybe_send_notification()
 {
     // form maybe be serialized
     if (isset($_REQUEST['serialized_fields'])) {
         foreach ($_REQUEST['serialized_fields'] as $key => $data) {
             if (strpos($data['name'], '[]') !== false) {
                 $_REQUEST[str_replace('[]', '', $data['name'])][] = $data['value'];
             } else {
                 $_REQUEST[$data['name']] = $data['value'];
             }
         }
     }
     if (!isset($_REQUEST['sa_send_metabox_notification_nonce'])) {
         self::ajax_fail('Forget something (nonce)?');
     }
     $nonce = $_REQUEST['sa_send_metabox_notification_nonce'];
     if (!wp_verify_nonce($nonce, SI_Controller::NONCE)) {
         self::ajax_fail('Not going to fall for it!');
     }
     if (!isset($_REQUEST['sa_send_metabox_doc_id'])) {
         self::ajax_fail('Forget something (id)?');
     }
     if (get_post_type($_REQUEST['sa_send_metabox_doc_id']) !== SI_Invoice::POST_TYPE) {
         return;
     }
     $recipients = isset($_REQUEST['sa_metabox_recipients']) ? $_REQUEST['sa_metabox_recipients'] : array();
     if (isset($_REQUEST['sa_metabox_custom_recipient']) && '' !== trim($_REQUEST['sa_metabox_custom_recipient'])) {
         if (is_email($_REQUEST['sa_metabox_custom_recipient'])) {
             $recipients[] = $_REQUEST['sa_metabox_custom_recipient'];
         }
     }
     if (empty($recipients)) {
         self::ajax_fail('A recipient is required.');
     }
     $invoice = SI_Invoice::get_instance($_REQUEST['sa_send_metabox_doc_id']);
     $invoice->set_sender_note($_REQUEST['sa_send_metabox_sender_note']);
     $from_email = null;
     $from_name = null;
     if (isset($_REQUEST['sa_send_metabox_send_as'])) {
         $name_and_email = SI_Notifications_Control::email_split($_REQUEST['sa_send_metabox_send_as']);
         if (is_email($name_and_email['email'])) {
             $from_name = $name_and_email['name'];
             $from_email = $name_and_email['email'];
         }
     }
     do_action('send_invoice', $invoice, $recipients, $from_email, $from_name);
     // If status is temp than change to pending.
     if ($invoice->get_status() == SI_Invoice::STATUS_TEMP) {
         $invoice->set_pending();
     }
     header('Content-type: application/json');
     if (self::DEBUG) {
         header('Access-Control-Allow-Origin: *');
     }
     echo wp_json_encode(array('response' => si__('Notification Queued')));
     exit;
 }
 /**
  * Get the content for the notification.
  * @param  string $notification_name
  * @param  array  $data
  * @return string
  */
 public static function get_notification_instance_content($notification_name = '', $data = array())
 {
     self::$data = $data;
     $content = '';
     $notification = self::get_notification_instance($notification_name);
     if (!is_null($notification)) {
         $notification_post = $notification->get_post();
         $content = $notification_post->post_content;
         $content = self::do_shortcodes($notification_name, $content);
     } elseif (isset(self::$notifications[$notification_name]) && isset(self::$notifications[$notification_name]['default_content'])) {
         $content = self::$notifications[$notification_name]['default_content'];
         $content = self::do_shortcodes($notification_name, $content);
     }
     return apply_filters('si_get_notification_instance_content', $content, $notification_name, $data);
 }
/**
 * Load the SI application
 * (function called at the bottom of this page)
 *
 * @package Sprout_Invoices
 * @return void
 */
function sprout_invoices_load()
{
    if (class_exists('Sprout_Invoices')) {
        error_log('** Sprout_Invoices Already Loaded **');
        return;
        // already loaded, or a name collision
    }
    do_action('sprout_invoices_preload');
    //////////
    // Load //
    //////////
    // Master class
    require_once SI_PATH . '/Sprout_Invoices.class.php';
    // base classes
    require_once SI_PATH . '/models/_Model.php';
    require_once SI_PATH . '/controllers/_Controller.php';
    do_action('si_require_base_classes');
    // models
    require_once SI_PATH . '/models/Client.php';
    require_once SI_PATH . '/models/Estimate.php';
    require_once SI_PATH . '/models/Invoice.php';
    require_once SI_PATH . '/models/Notification.php';
    require_once SI_PATH . '/models/Payment.php';
    require_once SI_PATH . '/models/Record.php';
    // Premium models
    require_once SI_PATH . '/models/Project.php';
    // i18n
    require_once SI_PATH . '/controllers/i18n/Countries_States.php';
    require_once SI_PATH . '/controllers/i18n/Locales.php';
    do_action('si_require_model_classes');
    /////////////////
    // Controllers //
    /////////////////
    // settings
    require_once SI_PATH . '/controllers/admin/Settings.php';
    if (!class_exists('SA_Settings_API')) {
        require_once SI_PATH . '/controllers/admin/Settings_API.php';
    }
    require_once SI_PATH . '/controllers/admin/Capabilities.php';
    require_once SI_PATH . '/controllers/admin/Help.php';
    // json api
    require_once SI_PATH . '/controllers/api/JSON_API.php';
    // checkouts
    require_once SI_PATH . '/controllers/checkout/Checkouts.php';
    // clients
    require_once SI_PATH . '/controllers/clients/Clients.php';
    // developer logs
    require_once SI_PATH . '/controllers/developer/Logs.php';
    // Estimates
    require_once SI_PATH . '/controllers/estimates/Estimate_Submission.php';
    if (!SI_FREE_TEST && file_exists(SI_PATH . '/controllers/estimates/Estimate_Submission_Premium.php')) {
        require_once SI_PATH . '/controllers/estimates/Estimate_Submission_Premium.php';
    }
    require_once SI_PATH . '/controllers/estimates/Estimates.php';
    require_once SI_PATH . '/controllers/estimates/Estimates_Admin.php';
    require_once SI_PATH . '/controllers/estimates/Estimates_Edit.php';
    require_once SI_PATH . '/controllers/estimates/Estimates_Records.php';
    require_once SI_PATH . '/controllers/estimates/Estimates_Template.php';
    if (!SI_FREE_TEST && file_exists(SI_PATH . '/controllers/estimates/Estimates_Premium.php')) {
        require_once SI_PATH . '/controllers/estimates/Estimates_Premium.php';
    }
    // invoices
    require_once SI_PATH . '/controllers/invoices/Invoices.php';
    require_once SI_PATH . '/controllers/invoices/Invoices_Admin.php';
    require_once SI_PATH . '/controllers/invoices/Invoices_Edit.php';
    require_once SI_PATH . '/controllers/invoices/Invoices_Records.php';
    require_once SI_PATH . '/controllers/invoices/Invoices_Template.php';
    require_once SI_PATH . '/controllers/invoices/Invoices_Deposit.php';
    if (!SI_FREE_TEST && file_exists(SI_PATH . '/controllers/invoices/Invoices_Premium.php')) {
        require_once SI_PATH . '/controllers/invoices/Invoices_Premium.php';
    }
    // Line Items
    require_once SI_PATH . '/controllers/line-items/Line_Items.php';
    // notifications
    require_once SI_PATH . '/controllers/notifications/Notifications_Control.php';
    require_once SI_PATH . '/controllers/notifications/Notifications.php';
    if (!SI_FREE_TEST && file_exists(SI_PATH . '/controllers/notifications/Notifications_Premium.php')) {
        require_once SI_PATH . '/controllers/notifications/Notifications_Premium.php';
    }
    require_once SI_PATH . '/controllers/notifications/Notifications_Admin_Table.php';
    // payment processing
    require_once SI_PATH . '/controllers/payment-processing/Payment_Processors.php';
    require_once SI_PATH . '/controllers/payment-processing/Credit_Card_Processors.php';
    require_once SI_PATH . '/controllers/payment-processing/Offsite_Processors.php';
    // payment processors
    if (!SI_FREE_TEST && file_exists(SI_PATH . '/controllers/payment-processing/processors/SI_Paypal_EC.php')) {
        require_once SI_PATH . '/controllers/payment-processing/processors/SI_Paypal_EC.php';
    }
    if (!SI_FREE_TEST && file_exists(SI_PATH . '/controllers/payment-processing/processors/SI_Paypal_Pro.php')) {
        require_once SI_PATH . '/controllers/payment-processing/processors/SI_Paypal_Pro.php';
    }
    require_once SI_PATH . '/controllers/payment-processing/processors/SI_Checks.php';
    require_once SI_PATH . '/controllers/payment-processing/processors/SI_Admin_Payment.php';
    do_action('si_payment_processors_loaded');
    // payments
    require_once SI_PATH . '/controllers/payments/Payments.php';
    require_once SI_PATH . '/controllers/payments/Payments_Admin_Table.php';
    // Projects
    require_once SI_PATH . '/controllers/projects/Projects.php';
    if (!SI_FREE_TEST && file_exists(SI_PATH . '/controllers/projects/Projects_Premium.php')) {
        require_once SI_PATH . '/controllers/projects/Projects_Premium.php';
    }
    // internal records
    require_once SI_PATH . '/controllers/records/Internal_Records.php';
    require_once SI_PATH . '/controllers/records/Records_Admin_Table.php';
    // reporting
    require_once SI_PATH . '/controllers/reporting/Dashboard.php';
    require_once SI_PATH . '/controllers/reporting/Reporting.php';
    if (!SI_FREE_TEST && file_exists(SI_PATH . '/controllers/reporting/Reporting_Premium.php')) {
        require_once SI_PATH . '/controllers/reporting/Reporting_Premium.php';
    }
    require_once SI_PATH . '/controllers/templating/Templating.php';
    require_once SI_PATH . '/controllers/templating/Customizer.php';
    // updates
    if (!SI_FREE_TEST && file_exists(SI_PATH . '/controllers/updates/Updates.php')) {
        require_once SI_PATH . '/controllers/updates/Updates.php';
    }
    if (file_exists(SI_PATH . '/controllers/updates/Free_License.php')) {
        require_once SI_PATH . '/controllers/updates/Free_License.php';
    }
    // importers
    require_once SI_PATH . '/importers/Importer.php';
    require_once SI_PATH . '/importers/Freshbooks.php';
    require_once SI_PATH . '/importers/Harvest.php';
    require_once SI_PATH . '/importers/WP-Invoice.php';
    require_once SI_PATH . '/importers/CSV.php';
    do_action('si_importers_loaded');
    // Fix others problems
    require_once SI_PATH . '/controllers/compat/Compatibility.php';
    // all done
    do_action('si_require_controller_classes');
    // Template tags
    require_once SI_PATH . '/template-tags/estimates.php';
    require_once SI_PATH . '/template-tags/clients.php';
    require_once SI_PATH . '/template-tags/forms.php';
    require_once SI_PATH . '/template-tags/invoices.php';
    require_once SI_PATH . '/template-tags/line-items.php';
    require_once SI_PATH . '/template-tags/projects.php';
    require_once SI_PATH . '/template-tags/ui.php';
    require_once SI_PATH . '/template-tags/utility.php';
    require_once SI_PATH . '/template-tags/docs.php';
    // l18n
    require_once SI_PATH . '/languages/SI_l10n.php';
    require_once SI_PATH . '/languages/SI_Strings.php';
    // i18n & l10n
    SI_l10n::init();
    SI_Strings::load_additional_strings();
    SI_Locales::init();
    SI_Countries_States::init();
    ///////////////////
    // init() models //
    ///////////////////
    do_action('si_models_init');
    SI_Post_Type::init();
    // _Model
    SI_Record::init();
    SI_Notification::init();
    SI_Invoice::init();
    SI_Estimate::init();
    SI_Client::init();
    SI_Payment::init();
    SI_Project::init();
    /////////////////////////
    // init() controllers //
    /////////////////////////
    do_action('si_controllers_init');
    SI_Controller::init();
    SA_Settings_API::init();
    SI_Templating_API::init();
    SI_Customizer::init();
    SI_Admin_Capabilities::init();
    // updates
    if (!SI_FREE_TEST && class_exists('SI_Updates')) {
        SI_Updates::init();
    }
    if (class_exists('SI_Free_License')) {
        SI_Free_License::init();
    }
    // api
    SI_JSON_API::init();
    // reports
    SI_Dashboard::init();
    SI_Reporting::init();
    if (!SI_FREE_TEST && class_exists('SI_Reporting_Premium')) {
        SI_Reporting_Premium::init();
    }
    // records and logs
    SI_Internal_Records::init();
    SI_Dev_Logs::init();
    // settings
    SI_Admin_Settings::init();
    // payments and processing
    SI_Payment_Processors::init();
    SI_Payments::init();
    // notifications
    SI_Notifications::init();
    // Hooks come before parent class.
    if (!SI_FREE_TEST && class_exists('SI_Notifications_Premium')) {
        SI_Notifications_Premium::init();
    }
    SI_Notifications_Control::init();
    // clients
    SI_Clients::init();
    // estimates
    SI_Estimates::init();
    if (!SI_FREE_TEST && class_exists('SI_Estimates_Premium')) {
        SI_Estimates_Premium::init();
    }
    if (!SI_FREE_TEST && class_exists('SI_Estimates_Submission_Premium')) {
        SI_Estimates_Submission_Premium::init();
    }
    SI_Estimate_Submissions::init();
    SI_Estimates_Admin::init();
    SI_Estimates_Edit::init();
    SI_Estimates_Template::init();
    SI_Estimates_Records::init();
    // checkouts
    SI_Checkouts::init();
    // invoices
    SI_Invoices::init();
    SI_Invoices_Admin::init();
    SI_Invoices_Edit::init();
    SI_Invoices_Template::init();
    SI_Invoices_Records::init();
    SI_Invoices_Deposit::init();
    if (!SI_FREE_TEST && class_exists('SI_Invoices_Premium')) {
        SI_Invoices_Premium::init();
    }
    // Line items
    SI_Line_Items::init();
    // projects
    SI_Projects::init();
    if (!SI_FREE_TEST && class_exists('SI_Projects_Premium')) {
        SI_Projects_Premium::init();
    }
    // importer
    SI_Importer::init();
    // help
    SI_Help::init();
    // Compat
    SI_Compatibility::init();
    // addons
    require_once SI_PATH . '/add-ons/Addons.php';
    require_once SI_PATH . '/add-ons/updates/edd_plugin_updater.class.php';
    SA_Addons::init();
    do_action('sprout_invoices_loaded');
}
 public static function sender_submission_fields(SI_Invoice $invoice)
 {
     $fields = array();
     $from_name = SI_Notifications_Control::from_name(array('invoice_id' => $invoice->get_id()));
     $from_email = SI_Notifications_Control::from_email(array('invoice_id' => $invoice->get_id()));
     $fields['send_as'] = array('weight' => 1, 'label' => __('Sender', 'sprout-invoices'), 'type' => 'text', 'placeholder' => '', 'attributes' => array('readonly' => 'readonly'), 'default' => $from_name . ' <' . $from_email . '>');
     // options for recipients
     $client = $invoice->get_client();
     $recipient_options = '<div class="form-group"><div class="input_wrap">';
     // client users
     if (is_a($client, 'SI_Client')) {
         $client_users = $client->get_associated_users();
         foreach ($client_users as $user_id) {
             if (!is_wp_error($user_id)) {
                 $recipient_options .= sprintf('<label class="clearfix"><input type="checkbox" name="sa_metabox_recipients[]" value="%1$s"> %2$s</label>', $user_id, esc_attr(SI_Notifications::get_user_email($user_id)));
             }
         }
     }
     $recipient_options .= sprintf('<label class="clearfix"><input type="checkbox" name="sa_metabox_custom_recipient_check" disabled="disabled"><input type="text" name="sa_metabox_custom_recipient" placeholder="%1$s"><span class="helptip" title="%2$s"></span></label>', __('*****@*****.**', 'sprout-invoices'), __('Entering an email will prevent some notification shortcodes from working since there is no client.', 'sprout-invoices'));
     // Send to me.
     $recipient_options .= sprintf('<label class="clearfix"><input type="checkbox" name="sa_metabox_recipients[]" value="%1$s"> %2$s</label>', get_current_user_id(), __('Send me a copy', 'sprout-invoices'));
     $recipient_options .= '</div></div>';
     $fields['recipients'] = array('weight' => 5, 'label' => sprintf('%s <span class="helptip" title="%s"></span>', __('Recipients', 'sprout-invoices'), __('A notification will be sent if recipients are selected and this invoice is saved.', 'sprout-invoices')), 'type' => 'bypass', 'output' => $recipient_options);
     $fields['sender_note'] = array('weight' => 10, 'label' => __('Note', 'sprout-invoices'), 'type' => 'textarea', 'default' => $invoice->get_sender_note(), 'description' => __('This note will be added to the Invoice Notification via the [admin_note] shortcode.', 'sprout-invoices'));
     $fields['doc_id'] = array('type' => 'hidden', 'value' => $invoice->get_id(), 'weight' => 10000);
     $fields['notification_nonce'] = array('type' => 'hidden', 'value' => wp_create_nonce(SI_Controller::NONCE), 'weight' => 10001);
     $fields = apply_filters('si_sender_submission_fields', $fields);
     uasort($fields, array(__CLASS__, 'sort_by_weight'));
     return $fields;
 }
 public static function load_templates()
 {
     $notifications = apply_filters('sprout_notifications', array());
     foreach ($notifications as $notification_id => $data) {
         // Find existing notification
         $notification = SI_Notifications_Control::get_notification_instance($notification_id);
         // Delete all the existing notifications
         if (is_a($notification, 'SI_Notification')) {
             wp_delete_post($notification->get_id(), true);
         }
         // Create new notification with new content
         $post_id = wp_insert_post(array('post_status' => 'publish', 'post_type' => SI_Notification::POST_TYPE, 'post_title' => $data['default_title'], 'post_content' => self::notification_content($notification_id)));
         $notification = SI_Notification::get_instance($post_id);
         SI_Notifications_Control::save_meta_box_notification_submit($post_id, $notification->get_post(), array(), $notification_id);
         // Don't allow for a notification to enabled if specifically shouldn't
         if (isset($data['always_disabled']) && $data['always_disabled']) {
             $notification->set_disabled('TRUE');
         }
         update_option(SI_Notifications_Control::EMAIL_FORMAT, 'HTML');
     }
 }