HM\BackUpWordPress\Email_Service::action PHP Method

action() public method

Fire the email notification on the hmbkp_backup_complete
See also: Backup::do_action
public action ( string $action, Backup $backup )
$action string The action received from the backup
$backup Backup
    public function action($action, Backup $backup)
    {
        if ('hmbkp_backup_complete' === $action && $this->get_email_address_array()) {
            $file = $backup->get_backup_filepath();
            $sent = false;
            $download = add_query_arg('hmbkp_download', base64_encode($file), HMBKP_ADMIN_URL);
            $domain = parse_url(home_url(), PHP_URL_HOST) . parse_url(home_url(), PHP_URL_PATH);
            $headers = 'From: BackUpWordPress <' . apply_filters('hmbkp_from_email', get_bloginfo('admin_email')) . '>' . "\r\n";
            // The backup failed, send a message saying as much
            if (!file_exists($file) && ($errors = array_merge($backup->get_errors(), $backup->get_warnings()))) {
                $error_message = '';
                foreach ($errors as $error_set) {
                    $error_message .= implode("\n - ", $error_set);
                }
                if ($error_message) {
                    $error_message = ' - ' . $error_message;
                }
                $subject = sprintf(__('Backup of %s Failed', 'backupwordpress'), $domain);
                $message = sprintf(__('BackUpWordPress was unable to backup your site %1$s.', 'backupwordpress') . "\n\n" . __('Here are the errors that we\'ve encountered:', 'backupwordpress') . "\n\n" . '%2$s' . "\n\n" . __('If the errors above look like Martian, forward this email to %3$s and we\'ll take a look', 'backupwordpress') . "\n\n" . __("Kind Regards,\nThe Apologetic BackUpWordPress Backup Emailing Robot", 'backupwordpress'), home_url(), $error_message, '[email protected]');
                wp_mail($this->get_email_address_array(), $subject, $message, $headers);
                return;
            }
            $subject = sprintf(__('Backup of %s', 'backupwordpress'), $domain);
            // If it's larger than the max attachment size limit assume it's not going to be able to send the backup
            if (@filesize($file) < get_max_attachment_size()) {
                $message = sprintf(__('BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress') . "\n\n" . __('The backup file should be attached to this email.', 'backupwordpress') . "\n\n" . __('You can download the backup file by clicking the link below:', 'backupwordpress') . "\n\n" . '%2$s' . "\n\n" . __("Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress'), home_url(), $download);
                $sent = wp_mail($this->get_email_address_array(), $subject, $message, $headers, $file);
            }
            // If we didn't send the email above then send just the notification
            if (!$sent) {
                $message = sprintf(__('BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress') . "\n\n" . __('Unfortunately, the backup file was too large to attach to this email.', 'backupwordpress') . "\n\n" . __('You can download the backup file by clicking the link below:', 'backupwordpress') . "\n\n" . '%2$s' . "\n\n" . __("Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress'), home_url(), $download);
                wp_mail($this->get_email_address_array(), $subject, $message, $headers);
            }
        }
    }