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

251 lines 7.6 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 global $wpdb;
91
92 if (
93 $this->httpRequest->getQuery( 'page' ) !== Page::SLUG ||
94 $this->httpRequest->getQuery( 'action' ) !== self::ACTION_EXPORT_SETTINGS
95 ) {
96 return;
97 }
98
99 $globalSettings = $this->optionsProvider->data_to_array();
100 if ( ! empty( $globalSettings['api_password'] ) ) {
101 $globalSettings['api_password'] = sprintf(
102 '%s...%s (%s)',
103 substr( $globalSettings['api_password'], 0, 16 ),
104 substr( $globalSettings['api_password'], - 2, 2 ),
105 strlen( $globalSettings['api_password'] )
106 );
107 unset( $globalSettings['api_key'] );
108 }
109 $globalSettings['woocommerce_allowed_countries'] = get_option( 'woocommerce_allowed_countries' );
110 $globalSettings['woocommerce_specific_allowed_countries'] = get_option( 'woocommerce_specific_allowed_countries' );
111 $globalSettings['woocommerce_ship_to_countries'] = get_option( 'woocommerce_ship_to_countries' );
112 $globalSettings['woocommerce_specific_ship_to_countries'] = get_option( 'woocommerce_specific_ship_to_countries' );
113
114 $activeTheme = wp_get_theme();
115 $themeLatestVersion = \WC_Admin_Status::get_latest_theme_version( $activeTheme );
116 $themeLatestVersionInfo = ( $themeLatestVersion !== $activeTheme->version ? ' (' . $themeLatestVersion . ' available)' : '' );
117 $latteParams = [
118 'siteUrl' => get_option( 'siteurl' ),
119 'wpVersion' => get_bloginfo( 'version' ),
120 'wcVersion' => WC_VERSION,
121 'template' => $activeTheme->name . ' ' . $activeTheme->version . $themeLatestVersionInfo,
122 'phpVersion' => PHP_VERSION,
123 'dbServer' => $wpdb->db_server_info(),
124 'soap' => wc_bool_to_string( extension_loaded( 'soap' ) ),
125 'wpDebug' => wc_bool_to_string( WP_DEBUG ),
126 'packetaDebug' => wc_bool_to_string( PACKETERY_DEBUG ),
127 'globalSettings' => $this->formatVariable( $globalSettings ),
128 'lastCarrierUpdate' => $this->countryListingPage->getLastUpdate(),
129 'carriers' => $this->formatVariable( $this->countryListingPage->getCarriersForOptionsExport(), 0, true ),
130 'zones' => $this->formatVariable( \WC_Shipping_Zones::get_zones() ),
131 'lastFiveDaysLogs' => $this->formatVariable( $this->remapLogRecords( $this->logger->getForPeriodAsArray( [ [ 'after' => '5 days ago' ] ] ) ) ),
132 'generated' => gmdate( 'Y-m-d H:i:s' ),
133 /**
134 * Filter all_plugins filters the full array of plugins.
135 *
136 * @since 3.0.0
137 */
138 'plugins' => $this->getFormattedPlugins( (array) apply_filters( 'all_plugins', get_plugins() ) ),
139 'muPlugins' => $this->getFormattedPlugins( get_mu_plugins() ),
140 'currencySwitchers' => $this->formatVariable( Module\CurrencySwitcherFacade::$supportedCurrencySwitchers ),
141 ];
142 update_option( self::OPTION_LAST_SETTINGS_EXPORT, gmdate( DATE_ATOM ) );
143
144 $txtContents = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/options/export.latte', $latteParams );
145 header( 'Content-Type: text/plain' );
146 header( 'Content-Transfer-Encoding: Binary' );
147 header( 'Content-Length: ' . strlen( $txtContents ) );
148 header( 'Content-Disposition: attachment; filename="packeta_options_export_' . gmdate( 'Y-m-d-H-i-s' ) . '.txt"' );
149 // @codingStandardsIgnoreStart
150 echo $txtContents;
151 // @codingStandardsIgnoreEnd
152 exit;
153 }
154
155 /**
156 * Format plugins.
157 *
158 * @param array $plugins Plugins.
159 *
160 * @return string
161 */
162 private function getFormattedPlugins( array $plugins ): string {
163 $result = [];
164
165 foreach ( $plugins as $relativePath => $plugin ) {
166 $item = [
167 'Active' => wc_bool_to_string( Module\Helper::isPluginActive( $relativePath ) ),
168 ];
169
170 $options = [ 'Name', 'PluginURI', 'Version', 'WC tested up to', 'WC requires at least', 'AuthorName', 'RequiresPHP' ];
171 foreach ( $options as $option ) {
172 $item[ $option ] = $plugin[ $option ] ?? '';
173 }
174
175 $result[] = array_filter( $item );
176 }
177
178 return $this->formatVariable( $result );
179 }
180
181 /**
182 * Remaps log records.
183 *
184 * @param iterable $logs Logs.
185 *
186 * @return array
187 */
188 private function remapLogRecords( iterable $logs ): array {
189 $result = [];
190
191 foreach ( $logs as $log ) {
192 $result[] = [
193 'date' => $log->date->format( wc_date_format() . ' ' . wc_time_format() ),
194 'action' => $log->action,
195 'status' => $log->status,
196 'title' => $log->title,
197 'params' => $log->params,
198 ];
199 }
200
201 return $result;
202 }
203
204 /**
205 * Formats variable for text export.
206 *
207 * @param mixed $variable Variable to export.
208 * @param int $level Nesting level.
209 * @param bool $addSeparator Whether to use separator for top level items.
210 *
211 * @return string
212 */
213 private function formatVariable( $variable, int $level = 0, bool $addSeparator = false ): string {
214 $output = '';
215 if ( is_array( $variable ) ) {
216 foreach ( $variable as $key => $value ) {
217 if ( 0 === $level && $addSeparator ) {
218 $output .= '----------------------' . PHP_EOL;
219 }
220 $output .= str_repeat( ' ', $level );
221 $output .= $key . ':';
222 if ( is_array( $value ) ) {
223 $output .= PHP_EOL;
224 } else {
225 $output .= ' ';
226 }
227 $output .= $this->formatVariable( $value, $level + 1 );
228 }
229 } elseif ( is_bool( $variable ) ) {
230 // @codingStandardsIgnoreStart
231 $output .= var_export( $variable, true ) . PHP_EOL;
232 // @codingStandardsIgnoreEnd
233 } elseif ( $variable instanceof \stdClass ) {
234 $output .= PHP_EOL . $this->formatVariable( (array) $variable, $level );
235 } elseif ( $variable instanceof \WC_Shipping_Method ) {
236 $methodInfo = [
237 'id' => $variable->id,
238 'method_title' => $variable->method_title,
239 'enabled' => $variable->enabled,
240 ];
241 $output .= PHP_EOL . $this->formatVariable( $methodInfo, $level );
242 } elseif ( is_object( $variable ) ) {
243 $output .= gettype( $variable ) . ' ' . get_class( $variable ) . PHP_EOL;
244 } else {
245 $output .= $variable . PHP_EOL;
246 }
247
248 return $output;
249 }
250 }
251