PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Email / BugReportAttachment.php

BugReportAttachment.php in Packeta 2.3.2, at src/Packetery/Module/Email/BugReportAttachment.php

147 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Packetery\Module\Email;
6
7 use Packetery\Module\Framework\WcAdapter;
8 use Packetery\Module\Framework\WpAdapter;
9 use Packetery\Module\Log\LogSizeLimiter;
10 use Packetery\Module\Options\Exporter;
11 use Packetery\Tracy\Debugger;
12 use ZipArchive;
13
14 class BugReportAttachment {
15
16 /** @var WpAdapter */
17 private $wpAdapter;
18
19 /** @var WcAdapter */
20 private $wcAdapter;
21
22 /** @var Exporter */
23 private $exporter;
24
25 /** @var LogSizeLimiter */
26 private $logSizeLimiter;
27
28 public function __construct(
29 WpAdapter $wpAdapter,
30 WcAdapter $wcAdapter,
31 Exporter $exporter,
32 LogSizeLimiter $logSizeLimiter
33 ) {
34 $this->wpAdapter = $wpAdapter;
35 $this->wcAdapter = $wcAdapter;
36 $this->exporter = $exporter;
37 $this->logSizeLimiter = $logSizeLimiter;
38 }
39
40 /**
41 * @return string[]
42 */
43 public function createAttachments(): array {
44 $attachments = [];
45 if ( ! class_exists( 'ZipArchive' ) ) {
46 return $attachments;
47 }
48
49 $timestamp = $this->wpAdapter->currentTime( 'Y-m-d_H-i-s', false );
50 $zipFilename = "logs-{$timestamp}.zip";
51
52 $zip = new ZipArchive();
53 $zipPath = sys_get_temp_dir() . '/' . $zipFilename;
54
55 if ( $zip->open( $zipPath, ZipArchive::CREATE ) === true ) {
56 $zip->addFromString( 'packeta-settings.txt', $this->exporter->getExportContent() );
57 $this->addWooCommerceSystemStatusToZip( $zip );
58 $this->addPacketaWcLogsToZip( $zip );
59 $this->addWpDebugLogToZip( $zip );
60 $this->addTracyLogsToZip( $zip );
61
62 $zip->close();
63 if ( is_file( $zipPath ) ) {
64 $attachments[] = $zipPath;
65 }
66 }
67
68 return $attachments;
69 }
70
71 private function addWooCommerceSystemStatusToZip( ZipArchive $zip ): void {
72 if ( ! class_exists( 'WC_Admin_Status' ) ) {
73 return;
74 }
75
76 ob_start();
77 $this->wcAdapter->adminStatusStatusReport();
78 $systemStatus = ob_get_clean();
79
80 if ( $systemStatus !== false && $systemStatus !== '' ) {
81 $zip->addFromString( 'woocommerce-system-status.html', $systemStatus );
82 }
83 }
84
85 private function addPacketaWcLogsToZip( ZipArchive $zip ): void {
86 $logDirectory = $this->wcAdapter->loggingUtilGetLogDirectory( false );
87 if ( $logDirectory === null || ! is_dir( $logDirectory ) ) {
88 return;
89 }
90 $this->addFreshLogsToZipByPrefix( $logDirectory, $zip, 'packeta-' );
91 }
92
93 private function addWpDebugLogToZip( ZipArchive $zip ): void {
94 $debugLogPath = null;
95 // Snippet from function `wp_debug_mode`.
96 if ( in_array( strtolower( (string) WP_DEBUG_LOG ), array( 'true', '1' ), true ) ) {
97 $debugLogPath = WP_CONTENT_DIR . '/debug.log';
98 } elseif ( is_string( WP_DEBUG_LOG ) ) {
99 $debugLogPath = WP_DEBUG_LOG;
100 }
101 if ( is_string( $debugLogPath ) ) {
102 $this->addLimitedLogContentsToZip( $debugLogPath, $zip, 'debug.log' );
103 }
104 }
105
106 private function addTracyLogsToZip( ZipArchive $zip ): void {
107 $logDirectory = Debugger::$logDirectory;
108 if ( $logDirectory === null ) {
109 return;
110 }
111
112 foreach ( [ 'packeta.log', 'exception.log', 'error.log' ] as $filename ) {
113 $this->addLimitedLogContentsToZip( $logDirectory . '/' . $filename, $zip, $filename );
114 }
115
116 $this->addFreshLogsToZipByPrefix( $logDirectory, $zip, 'exception-' );
117 }
118
119 private function addLimitedLogContentsToZip( string $logPath, ZipArchive $zip, string $fileName ): void {
120 if ( is_file( $logPath ) ) {
121 $limited = $this->logSizeLimiter->getLimitedFilePartAsString( $logPath );
122 if ( is_string( $limited ) && $limited !== '' ) {
123 $zip->addFromString( $fileName, $limited );
124 } else {
125 $zip->addFile( $logPath, $fileName );
126 }
127 }
128 }
129
130 public function addFreshLogsToZipByPrefix( string $logDirectory, ZipArchive $zip, string $prefix ): void {
131 $fileNames = scandir( $logDirectory );
132 if ( ! is_array( $fileNames ) ) {
133 return;
134 }
135 foreach ( $fileNames as $fileName ) {
136 $filePath = $logDirectory . '/' . $fileName;
137 if (
138 is_file( $filePath ) &&
139 strpos( $fileName, $prefix ) === 0 &&
140 $this->logSizeLimiter->isFileFreshEnough( $filePath )
141 ) {
142 $zip->addFile( $filePath, $fileName );
143 }
144 }
145 }
146 }
147