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