$settings * @param array $facts file, size, duration, pruned (list), next_run (int) * @return bool Whether a message was handed to the mail system. */ public static function success( array $settings, array $facts ): bool { if ( empty( $settings['notify_success'] ) ) { return false; } $lines = array( __( 'Your scheduled backup finished.', 'migratex' ), '', 'Site: ' . home_url(), 'File: ' . $facts['file'], 'Size: ' . $facts['size'], 'Time taken: ' . self::duration( (int) $facts['seconds'] ), ); if ( ! empty( $facts['pruned'] ) ) { $lines[] = 'Old backups removed to stay within your limit: ' . implode( ', ', $facts['pruned'] ); } if ( ! empty( $facts['next_run'] ) ) { $lines[] = 'Next backup: ' . wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) $facts['next_run'] ); } return self::send( $settings, sprintf( '[MigrateX] Scheduled backup completed for %s', self::site() ), $lines ); } /** * @param array $settings * @param string $reason What went wrong, in words the site owner can act on. */ public static function failure( array $settings, string $reason ): bool { if ( empty( $settings['notify_failure'] ) ) { return false; } $lines = array( __( 'Your scheduled backup did not complete.', 'migratex' ), '', 'Site: ' . home_url(), 'Reason: ' . $reason, '', __( 'Nothing on the site was changed. You can run a backup by hand from MigrateX > Export, and check the schedule under MigrateX > Schedules.', 'migratex' ), ); return self::send( $settings, sprintf( '[MigrateX] Scheduled backup FAILED for %s', self::site() ), $lines ); } /** * @param array $settings * @param string[] $lines */ private static function send( array $settings, string $subject, array $lines ): bool { $to = ScheduleSettings::recipient( $settings ); if ( ! is_email( $to ) ) { return false; } // One line, whatever the site is called. $subject = (string) preg_replace( '/[\r\n]+/', ' ', $subject ); return ( new MailGuard() )->send( $to, $subject, implode( "\n", $lines ) ); } private static function site(): string { return (string) preg_replace( '/[\r\n]+/', ' ', wp_strip_all_tags( (string) get_bloginfo( 'name' ) ) ); } private static function duration( int $seconds ): string { return $seconds < 60 ? $seconds . ' s' : floor( $seconds / 60 ) . ' min ' . ( $seconds % 60 ) . ' s'; } }