PluginProbe
Packeta / 1.4.2
Packeta v1.4.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.4.2, at src/Packetery/Module/Options/Exporter.php

248 lines 7.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 Packetery\Module;
15 use PacketeryLatte\Engine;
16 use PacketeryNette\Http;
17
18 /**
19 * Class Exporter
20 *
21 * @package Packetery\Module\Options
22 */
23 class Exporter {
24
25 public const OPTION_LAST_SETTINGS_EXPORT = 'packetery_last_settings_export';
26 public const ACTION_EXPORT_SETTINGS = 'export-settings';
27
28 /**
29 * Http request.
30 *
31 * @var Http\Request
32 */
33 private $httpRequest;
34
35 /**
36 * Latte engine.
37 *
38 * @var Engine
39 */
40 private $latteEngine;
41
42 /**
43 * Country listing page.
44 *
45 * @var CountryListingPage
46 */
47 private $countryListingPage;
48
49 /**
50 * Options provider.
51 *
52 * @var Provider
53 */
54 private $optionsProvider;
55
56 /**
57 * Logger.
58 *
59 * @var ILogger
60 */
61 private $logger;
62
63 /**
64 * Exporter constructor.
65 *
66 * @param Http\Request $httpRequest Http request.
67 * @param Engine $latteEngine Latte engine.
68 * @param CountryListingPage $countryListingPage Country listing page.
69 * @param Provider $optionsProvider Options provider.
70 * @param ILogger $logger Logger.
71 */
72 public function __construct(
73 Http\Request $httpRequest,
74 Engine $latteEngine,
75 CountryListingPage $countryListingPage,
76 Provider $optionsProvider,
77 ILogger $logger
78 ) {
79 $this->httpRequest = $httpRequest;
80 $this->latteEngine = $latteEngine;
81 $this->countryListingPage = $countryListingPage;
82 $this->optionsProvider = $optionsProvider;
83 $this->logger = $logger;
84 }
85
86 /**
87 * Prepares and outputs export text.
88 */
89 public function outputExportTxt(): void {
90 if (
91 $this->httpRequest->getQuery( 'page' ) !== Page::SLUG ||
92 $this->httpRequest->getQuery( 'action' ) !== self::ACTION_EXPORT_SETTINGS
93 ) {
94 return;
95 }
96
97 $globalSettings = $this->optionsProvider->data_to_array();
98 if ( ! empty( $globalSettings['api_password'] ) ) {
99 $globalSettings['api_password'] = sprintf(
100 '%s...%s (%s)',
101 substr( $globalSettings['api_password'], 0, 16 ),
102 substr( $globalSettings['api_password'], - 2, 2 ),
103 strlen( $globalSettings['api_password'] )
104 );
105 unset( $globalSettings['api_key'] );
106 }
107 $globalSettings['woocommerce_allowed_countries'] = get_option( 'woocommerce_allowed_countries' );
108 $globalSettings['woocommerce_specific_allowed_countries'] = get_option( 'woocommerce_specific_allowed_countries' );
109 $globalSettings['woocommerce_ship_to_countries'] = get_option( 'woocommerce_ship_to_countries' );
110 $globalSettings['woocommerce_specific_ship_to_countries'] = get_option( 'woocommerce_specific_ship_to_countries' );
111
112 $activeTheme = wp_get_theme();
113 $themeLatestVersion = \WC_Admin_Status::get_latest_theme_version( $activeTheme );
114 $themeLatestVersionInfo = ( $themeLatestVersion !== $activeTheme->version ? ' (' . $themeLatestVersion . ' available)' : '' );
115 $latteParams = [
116 'siteUrl' => get_option( 'siteurl' ),
117 'wpVersion' => get_bloginfo( 'version' ),
118 'wcVersion' => WC_VERSION,
119 'template' => $activeTheme->name . ' ' . $activeTheme->version . $themeLatestVersionInfo,
120 'phpVersion' => PHP_VERSION,
121 'soap' => wc_bool_to_string( extension_loaded( 'soap' ) ),
122 'wpDebug' => wc_bool_to_string( WP_DEBUG ),
123 'packetaDebug' => wc_bool_to_string( PACKETERY_DEBUG ),
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 * Filter all_plugins filters the full array of plugins.
132 *
133 * @since 3.0.0
134 */
135 'plugins' => $this->getFormattedPlugins( apply_filters( 'all_plugins', get_plugins() ) ),
136 'muPlugins' => $this->getFormattedPlugins( get_mu_plugins() ),
137 'currencySwitchers' => $this->formatVariable( Module\CurrencySwitcherFacade::$supportedCurrencySwitchers ),
138 ];
139 update_option( self::OPTION_LAST_SETTINGS_EXPORT, gmdate( DATE_ATOM ) );
140
141 $txtContents = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/options/export.latte', $latteParams );
142 header( 'Content-Type: text/plain' );
143 header( 'Content-Transfer-Encoding: Binary' );
144 header( 'Content-Length: ' . strlen( $txtContents ) );
145 header( 'Content-Disposition: attachment; filename="packeta_options_export_' . gmdate( 'Y-m-d-H-i-s' ) . '.txt"' );
146 // @codingStandardsIgnoreStart
147 echo $txtContents;
148 // @codingStandardsIgnoreEnd
149 exit;
150 }
151
152 /**
153 * Format plugins.
154 *
155 * @param array $plugins Plugins.
156 *
157 * @return string
158 */
159 private function getFormattedPlugins( array $plugins ): string {
160 $result = [];
161
162 foreach ( $plugins as $relativePath => $plugin ) {
163 $item = [
164 'Active' => wc_bool_to_string( Module\Helper::isPluginActive( $relativePath ) ),
165 ];
166
167 $options = [ 'Name', 'PluginURI', 'Version', 'WC tested up to', 'WC requires at least', 'AuthorName', 'RequiresPHP' ];
168 foreach ( $options as $option ) {
169 $item[ $option ] = $plugin[ $option ] ?? '';
170 }
171
172 $result[] = array_filter( $item );
173 }
174
175 return $this->formatVariable( $result );
176 }
177
178 /**
179 * Remaps log records.
180 *
181 * @param iterable $logs Logs.
182 *
183 * @return array
184 */
185 private function remapLogRecords( iterable $logs ): array {
186 $result = [];
187
188 foreach ( $logs as $log ) {
189 $result[] = [
190 'date' => $log->date->format( wc_date_format() . ' ' . wc_time_format() ),
191 'action' => $log->action,
192 'status' => $log->status,
193 'title' => $log->title,
194 'params' => $log->params,
195 ];
196 }
197
198 return $result;
199 }
200
201 /**
202 * Formats variable for text export.
203 *
204 * @param mixed $variable Variable to export.
205 * @param int $level Nesting level.
206 * @param bool $addSeparator Whether to use separator for top level items.
207 *
208 * @return string
209 */
210 private function formatVariable( $variable, int $level = 0, bool $addSeparator = false ): string {
211 $output = '';
212 if ( is_array( $variable ) ) {
213 foreach ( $variable as $key => $value ) {
214 if ( 0 === $level && $addSeparator ) {
215 $output .= '----------------------' . PHP_EOL;
216 }
217 $output .= str_repeat( ' ', $level );
218 $output .= $key . ':';
219 if ( is_array( $value ) ) {
220 $output .= PHP_EOL;
221 } else {
222 $output .= ' ';
223 }
224 $output .= $this->formatVariable( $value, $level + 1 );
225 }
226 } elseif ( is_bool( $variable ) ) {
227 // @codingStandardsIgnoreStart
228 $output .= var_export( $variable, true ) . PHP_EOL;
229 // @codingStandardsIgnoreEnd
230 } elseif ( $variable instanceof \stdClass ) {
231 $output .= PHP_EOL . $this->formatVariable( (array) $variable, $level );
232 } elseif ( $variable instanceof \WC_Shipping_Method ) {
233 $methodInfo = [
234 'id' => $variable->id,
235 'method_title' => $variable->method_title,
236 'enabled' => $variable->enabled,
237 ];
238 $output .= PHP_EOL . $this->formatVariable( $methodInfo, $level );
239 } elseif ( is_object( $variable ) ) {
240 $output .= gettype( $variable ) . ' ' . get_class( $variable ) . PHP_EOL;
241 } else {
242 $output .= $variable . PHP_EOL;
243 }
244
245 return $output;
246 }
247 }
248