PluginProbe
Packeta / 1.2.2
Packeta v1.2.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 / Options / Exporter.php

Exporter.php in Packeta 1.2.2, at src/Packetery/Module/Options/Exporter.php

214 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Exporter
4 *
5 * @package Packetery\Module\Options
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Options;
11
12 use Packetery\Core\Log\ILogger;
13 use Packetery\Module\Carrier\CountryListingPage;
14 use PacketeryLatte\Engine;
15 use PacketeryNette\Http;
16
17 /**
18 * Class Exporter
19 *
20 * @package Packetery\Module\Options
21 */
22 class Exporter {
23
24 public const OPTION_LAST_SETTINGS_EXPORT = 'packetery_last_settings_export';
25 public const ACTION_EXPORT_SETTINGS = 'export-settings';
26
27 /**
28 * Http request.
29 *
30 * @var Http\Request
31 */
32 private $httpRequest;
33
34 /**
35 * Latte engine.
36 *
37 * @var Engine
38 */
39 private $latteEngine;
40
41 /**
42 * Country listing page.
43 *
44 * @var CountryListingPage
45 */
46 private $countryListingPage;
47
48 /**
49 * Options provider.
50 *
51 * @var Provider
52 */
53 private $optionsProvider;
54
55 /**
56 * Logger.
57 *
58 * @var ILogger
59 */
60 private $logger;
61
62 /**
63 * Exporter constructor.
64 *
65 * @param Http\Request $httpRequest Http request.
66 * @param Engine $latteEngine Latte engine.
67 * @param CountryListingPage $countryListingPage Country listing page.
68 * @param Provider $optionsProvider Options provider.
69 * @param ILogger $logger Logger.
70 */
71 public function __construct(
72 Http\Request $httpRequest,
73 Engine $latteEngine,
74 CountryListingPage $countryListingPage,
75 Provider $optionsProvider,
76 ILogger $logger
77 ) {
78 $this->httpRequest = $httpRequest;
79 $this->latteEngine = $latteEngine;
80 $this->countryListingPage = $countryListingPage;
81 $this->optionsProvider = $optionsProvider;
82 $this->logger = $logger;
83 }
84
85 /**
86 * Prepares and outputs export text.
87 */
88 public function outputExportTxt(): void {
89 if (
90 $this->httpRequest->getQuery( 'page' ) !== Page::SLUG ||
91 $this->httpRequest->getQuery( 'action' ) !== self::ACTION_EXPORT_SETTINGS
92 ) {
93 return;
94 }
95
96 $globalSettings = $this->optionsProvider->data_to_array();
97 if ( ! empty( $globalSettings['api_password'] ) ) {
98 $globalSettings['api_password'] = sprintf(
99 '%s...%s (%s)',
100 substr( $globalSettings['api_password'], 0, 16 ),
101 substr( $globalSettings['api_password'], - 2, 2 ),
102 strlen( $globalSettings['api_password'] )
103 );
104 unset( $globalSettings['api_key'] );
105 }
106 $globalSettings['woocommerce_allowed_countries'] = get_option( 'woocommerce_allowed_countries' );
107 $globalSettings['woocommerce_specific_allowed_countries'] = get_option( 'woocommerce_specific_allowed_countries' );
108 $globalSettings['woocommerce_ship_to_countries'] = get_option( 'woocommerce_ship_to_countries' );
109 $globalSettings['woocommerce_specific_ship_to_countries'] = get_option( 'woocommerce_specific_ship_to_countries' );
110
111 $activeTheme = wp_get_theme();
112 $themeLatestVersion = \WC_Admin_Status::get_latest_theme_version( $activeTheme );
113 $themeLatestVersionInfo = ( $themeLatestVersion !== $activeTheme->version ? ' (' . $themeLatestVersion . ' available)' : '' );
114 $latteParams = [
115 'wpVersion' => get_bloginfo( 'version' ),
116 'wcVersion' => WC_VERSION,
117 'template' => $activeTheme->name . ' ' . $activeTheme->version . $themeLatestVersionInfo,
118 'phpVersion' => PHP_VERSION,
119 // @codingStandardsIgnoreStart
120 'soap' => var_export( extension_loaded( 'soap' ), true ),
121 'wpDebug' => var_export( WP_DEBUG, true ),
122 'packetaDebug' => var_export( PACKETERY_DEBUG, true ),
123 // @codingStandardsIgnoreStart
124 'globalSettings' => $this->formatVariable( $globalSettings ),
125 'lastCarrierUpdate' => $this->countryListingPage->getLastUpdate(),
126 'carriers' => $this->formatVariable( $this->countryListingPage->getCarriersForOptionsExport(), 0, true ),
127 'zones' => $this->formatVariable( \WC_Shipping_Zones::get_zones() ),
128 'lastFiveDaysLogs' => $this->formatVariable( $this->remapLogRecords( $this->logger->getForPeriodAsArray( [ [ 'after' => '5 days ago' ] ] ) ) ),
129 'generated' => gmdate( 'Y-m-d H:i:s' ),
130 ];
131 update_option( self::OPTION_LAST_SETTINGS_EXPORT, gmdate( DATE_ATOM ) );
132
133 $txtContents = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/options/export.latte', $latteParams );
134 header( 'Content-Type: text/plain' );
135 header( 'Content-Transfer-Encoding: Binary' );
136 header( 'Content-Length: ' . strlen( $txtContents ) );
137 header( 'Content-Disposition: attachment; filename="packeta_options_export_' . gmdate( 'Y-m-d-H-i-s' ) . '.txt"' );
138 // @codingStandardsIgnoreStart
139 echo $txtContents;
140 // @codingStandardsIgnoreEnd
141 exit;
142 }
143
144 /**
145 * Remaps log records.
146 *
147 * @param iterable $logs Logs.
148 *
149 * @return array
150 */
151 private function remapLogRecords( iterable $logs ): array {
152 $result = [];
153
154 foreach ( $logs as $log ) {
155 $result[] = [
156 'date' => $log->date->format( wc_date_format() . ' ' . wc_time_format() ),
157 'action' => $log->action,
158 'status' => $log->status,
159 'title' => $log->title,
160 'params' => $log->params,
161 ];
162 }
163
164 return $result;
165 }
166
167 /**
168 * Formats variable for text export.
169 *
170 * @param mixed $variable Variable to export.
171 * @param int $level Nesting level.
172 * @param bool $addSeparator Whether to use separator for top level items.
173 *
174 * @return string
175 */
176 private function formatVariable( $variable, int $level = 0, $addSeparator = false ): string {
177 $output = '';
178 if ( is_array( $variable ) ) {
179 foreach ( $variable as $key => $value ) {
180 if ( 0 === $level && $addSeparator ) {
181 $output .= '----------------------' . PHP_EOL;
182 }
183 $output .= str_repeat( ' ', $level );
184 $output .= $key . ':';
185 if ( is_array( $value ) ) {
186 $output .= PHP_EOL;
187 } else {
188 $output .= ' ';
189 }
190 $output .= $this->formatVariable( $value, $level + 1 );
191 }
192 } elseif ( is_bool( $variable ) ) {
193 // @codingStandardsIgnoreStart
194 $output .= var_export( $variable, true ) . PHP_EOL;
195 // @codingStandardsIgnoreEnd
196 } elseif ( $variable instanceof \stdClass ) {
197 $output .= PHP_EOL . $this->formatVariable( (array) $variable, $level );
198 } elseif ( $variable instanceof \WC_Shipping_Method ) {
199 $methodInfo = [
200 'id' => $variable->id,
201 'method_title' => $variable->method_title,
202 'enabled' => $variable->enabled,
203 ];
204 $output .= PHP_EOL . $this->formatVariable( $methodInfo, $level );
205 } elseif ( is_object( $variable ) ) {
206 $output .= gettype( $variable ) . ' ' . get_class( $variable ) . PHP_EOL;
207 } else {
208 $output .= $variable . PHP_EOL;
209 }
210
211 return $output;
212 }
213 }
214