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

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