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

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