PluginProbe
Packeta / trunk
Packeta vtrunk
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 trunk, at src/Packetery/Module/Options/Exporter.php

287 lines 9.3 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\Record;
13 use Packetery\Latte\Engine;
14 use Packetery\Module\Carrier\CarrierUpdater;
15 use Packetery\Module\Carrier\CountryListingPage;
16 use Packetery\Module\Checkout\CurrencySwitcherService;
17 use Packetery\Module\Framework\WcAdapter;
18 use Packetery\Module\Framework\WpAdapter;
19 use Packetery\Module\Log\DbLogger;
20 use Packetery\Module\ModuleHelper;
21 use Packetery\Module\WpdbAdapter;
22 use Packetery\Nette\Http;
23 use Packetery\Tracy\Debugger;
24
25 /**
26 * Class Exporter
27 *
28 * @package Packetery\Module\Options
29 */
30 class Exporter {
31
32 public const ACTION_EXPORT_SETTINGS = 'export-settings';
33
34 /**
35 * @var Http\Request
36 */
37 private $httpRequest;
38
39 /**
40 * @var Engine
41 */
42 private $latteEngine;
43
44 /**
45 * @var CountryListingPage
46 */
47 private $countryListingPage;
48
49 /**
50 * @var OptionsProvider
51 */
52 private $optionsProvider;
53
54 /**
55 * @var DbLogger
56 */
57 private $dbLogger;
58
59 /**
60 * @var ModuleHelper
61 */
62 private $moduleHelper;
63
64 /**
65 * @var WpAdapter
66 */
67 private $wpAdapter;
68
69 /**
70 * @var WcAdapter
71 */
72 private $wcAdapter;
73
74 /**
75 * @var CarrierUpdater
76 */
77 private $carrierUpdater;
78
79 /**
80 * @var WpdbAdapter
81 */
82 private $wpdbAdapter;
83
84 public function __construct(
85 Http\Request $httpRequest,
86 Engine $latteEngine,
87 CountryListingPage $countryListingPage,
88 OptionsProvider $optionsProvider,
89 DbLogger $dbLogger,
90 ModuleHelper $moduleHelper,
91 WpAdapter $wpAdapter,
92 WcAdapter $wcAdapter,
93 CarrierUpdater $carrierUpdater,
94 WpdbAdapter $wpdbAdapter
95 ) {
96 $this->httpRequest = $httpRequest;
97 $this->latteEngine = $latteEngine;
98 $this->countryListingPage = $countryListingPage;
99 $this->optionsProvider = $optionsProvider;
100 $this->dbLogger = $dbLogger;
101 $this->moduleHelper = $moduleHelper;
102 $this->wpAdapter = $wpAdapter;
103 $this->wcAdapter = $wcAdapter;
104 $this->carrierUpdater = $carrierUpdater;
105 $this->wpdbAdapter = $wpdbAdapter;
106 }
107
108 /**
109 * Prepares and outputs export text.
110 */
111 public function outputExportTxt(): void {
112 if (
113 $this->httpRequest->getQuery( 'page' ) !== Page::SLUG ||
114 $this->httpRequest->getQuery( 'action' ) !== self::ACTION_EXPORT_SETTINGS
115 ) {
116 return;
117 }
118
119 $txtContents = $this->getExportContent();
120 header( 'Content-Type: text/plain' );
121 header( 'Content-Transfer-Encoding: Binary' );
122 header( 'Content-Length: ' . strlen( $txtContents ) );
123 header( 'Content-Disposition: attachment; filename="packeta_options_export_' . gmdate( 'Y-m-d-H-i-s' ) . '.txt"' );
124 ModuleHelper::renderString( $txtContents );
125 exit;
126 }
127
128 public function getExportContent(): string {
129 $globalSettings = $this->optionsProvider->getAllOptions();
130 if ( isset( $globalSettings[ OptionNames::PACKETERY ]['api_password'] ) ) {
131 $globalSettings[ OptionNames::PACKETERY ]['api_password'] = sprintf(
132 '%s...%s (%s)',
133 substr( $globalSettings[ OptionNames::PACKETERY ]['api_password'], 0, 16 ),
134 substr( $globalSettings[ OptionNames::PACKETERY ]['api_password'], - 2, 2 ),
135 strlen( $globalSettings[ OptionNames::PACKETERY ]['api_password'] )
136 );
137 unset( $globalSettings[ OptionNames::PACKETERY ]['api_key'] );
138 }
139 $globalSettings['woocommerce_allowed_countries'] = get_option( 'woocommerce_allowed_countries' );
140 $globalSettings['woocommerce_specific_allowed_countries'] = get_option( 'woocommerce_specific_allowed_countries' );
141 $globalSettings['woocommerce_ship_to_countries'] = get_option( 'woocommerce_ship_to_countries' );
142 $globalSettings['woocommerce_specific_ship_to_countries'] = get_option( 'woocommerce_specific_ship_to_countries' );
143
144 $shippingTaxClass = $this->wpAdapter->getOption( 'woocommerce_shipping_tax_class' );
145
146 $taxes = [
147 'calc_taxes' => $this->wpAdapter->getOption( 'woocommerce_calc_taxes' ),
148 'prices_include_tax' => $this->wpAdapter->getOption( 'woocommerce_prices_include_tax' ),
149 'tax_based_on' => $this->wpAdapter->getOption( 'woocommerce_tax_based_on' ),
150 'shipping_tax_class' => $shippingTaxClass,
151 'tax_round_at_subtotal' => $this->wpAdapter->getOption( 'woocommerce_tax_round_at_subtotal' ),
152 'tax_classes' => $this->wpAdapter->getOption( 'woocommerce_tax_classes' ),
153 'tax_display_shop' => $this->wpAdapter->getOption( 'woocommerce_tax_display_shop' ),
154 'tax_display_cart' => $this->wpAdapter->getOption( 'woocommerce_tax_display_cart' ),
155 'tax_total_display' => $this->wpAdapter->getOption( 'woocommerce_tax_total_display' ),
156 'shipping_tax_class_settings' => $this->wcAdapter->taxGetRates( $shippingTaxClass ),
157 ];
158
159 $activeTheme = wp_get_theme();
160 $themeLatestVersion = \WC_Admin_Status::get_latest_theme_version( $activeTheme );
161 $themeLatestVersionInfo = ( $themeLatestVersion !== $activeTheme->version ? ' (' . $themeLatestVersion . ' available)' : '' );
162 $latteParams = [
163 'siteUrl' => get_option( 'siteurl' ),
164 'wpVersion' => get_bloginfo( 'version' ),
165 'wcVersion' => WC_VERSION,
166 'template' => $activeTheme->name . ' ' . $activeTheme->version . $themeLatestVersionInfo,
167 'phpVersion' => PHP_VERSION,
168 'dbServer' => $this->wpdbAdapter->dbServerInfo(),
169 'soap' => wc_bool_to_string( extension_loaded( 'soap' ) ),
170 'wpDebug' => wc_bool_to_string( WP_DEBUG ),
171 'packetaDebug' => wc_bool_to_string( Debugger::isEnabled() ),
172 'globalSettings' => $this->formatVariable( $globalSettings ),
173 'lastCarrierUpdate' => $this->carrierUpdater->getLastUpdate(),
174 'carriers' => $this->formatVariable( $this->countryListingPage->getCarriersForOptionsExport(), 0, true ),
175 'zones' => $this->formatVariable( \WC_Shipping_Zones::get_zones() ),
176 'taxes' => $this->formatVariable( $taxes ),
177 'lastFiveDaysLogs' => $this->formatVariable( $this->remapLogRecords( $this->dbLogger->getForPeriodAsArray( [ [ 'after' => '5 days ago' ] ] ) ) ),
178 'generated' => gmdate( 'Y-m-d H:i:s' ),
179 /**
180 * Filter all_plugins filters the full array of plugins.
181 *
182 * @since 3.0.0
183 */
184 'plugins' => $this->getFormattedPlugins( (array) apply_filters( 'all_plugins', get_plugins() ) ),
185 'muPlugins' => $this->getFormattedPlugins( get_mu_plugins() ),
186 'currencySwitchers' => $this->formatVariable( CurrencySwitcherService::$supportedCurrencySwitchers ),
187 ];
188 update_option( OptionNames::LAST_SETTINGS_EXPORT, gmdate( DATE_ATOM ) );
189
190 return $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/options/export.latte', $latteParams );
191 }
192
193 /**
194 * Format plugins.
195 *
196 * @param array<string, array<string, string>> $plugins Plugins.
197 *
198 * @return string
199 */
200 private function getFormattedPlugins( array $plugins ): string {
201 $result = [];
202
203 foreach ( $plugins as $relativePath => $plugin ) {
204 $item = [
205 'Active' => wc_bool_to_string( $this->moduleHelper->isPluginActive( $relativePath ) ),
206 ];
207
208 $options = [ 'Name', 'PluginURI', 'Version', 'WC tested up to', 'WC requires at least', 'AuthorName', 'RequiresPHP' ];
209 foreach ( $options as $option ) {
210 $item[ $option ] = $plugin[ $option ] ?? '';
211 }
212
213 $result[] = array_filter( $item );
214 }
215
216 return $this->formatVariable( $result );
217 }
218
219 /**
220 * @param \Generator<Record>|array{} $logs
221 * @return array<array<string, array<string, mixed>|string>>
222 */
223 private function remapLogRecords( iterable $logs ): array {
224 $result = [];
225
226 foreach ( $logs as $log ) {
227 $result[] = [
228 'date' => $log->date->format( wc_date_format() . ' ' . wc_time_format() ),
229 'action' => $log->action,
230 'status' => $log->status,
231 'title' => $log->title,
232 'params' => $log->params,
233 ];
234 }
235
236 return $result;
237 }
238
239 /**
240 * Formats variable for text export.
241 *
242 * @param mixed $variable Variable to export.
243 * @param int $level Nesting level.
244 * @param bool $addSeparator Whether to use separator for top level items.
245 *
246 * @return string
247 */
248 private function formatVariable( $variable, int $level = 0, bool $addSeparator = false ): string {
249 $output = '';
250 if ( is_array( $variable ) ) {
251 foreach ( $variable as $key => $value ) {
252 if ( $level === 0 && $addSeparator ) {
253 $output .= '----------------------' . PHP_EOL;
254 }
255 $output .= str_repeat( ' ', $level );
256 $output .= $key . ':';
257 if ( is_array( $value ) ) {
258 $output .= PHP_EOL;
259 } else {
260 $output .= ' ';
261 }
262 $output .= $this->formatVariable( $value, $level + 1 );
263 }
264 } elseif ( is_bool( $variable ) ) {
265 // @codingStandardsIgnoreStart
266 $output .= var_export( $variable, true ) . PHP_EOL;
267 // @codingStandardsIgnoreEnd
268 } elseif ( $variable instanceof \stdClass ) {
269 $output .= PHP_EOL . $this->formatVariable( (array) $variable, $level );
270 } elseif ( $variable instanceof \WC_Shipping_Method ) {
271 $methodInfo = [
272 'id' => $variable->id,
273 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
274 'method_title' => $variable->method_title,
275 'enabled' => $variable->enabled,
276 ];
277 $output .= PHP_EOL . $this->formatVariable( $methodInfo, $level );
278 } elseif ( is_object( $variable ) ) {
279 $output .= gettype( $variable ) . ' ' . get_class( $variable ) . PHP_EOL;
280 } else {
281 $output .= $variable . PHP_EOL;
282 }
283
284 return $output;
285 }
286 }
287