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

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