| @@ -8,13 +8,20 @@ | ||
| 8 | 8 | declare( strict_types=1 ); |
| 9 | 9 | |
| 10 | 10 | namespace Packetery\Module\Options; |
| 11 | 11 | |
| 12 | -use Packetery\Core\Log\ILogger; | |
| 12 | +use Packetery\Core\Log\Record; | |
| 13 | +use Packetery\Latte\Engine; | |
| 14 | +use Packetery\Module\Carrier\CarrierUpdater; | |
| 13 | 15 | use Packetery\Module\Carrier\CountryListingPage; |
| 14 | -use Packetery\Module; | |
| 15 | -use PacketeryLatte\Engine; | |
| 16 | -use PacketeryNette\Http; | |
| 16 | +use Packetery\Module\Checkout\CurrencySwitcherService; | |
| 17 | +use Packetery\Module\Framework\WcAdapter; | |
| 18 | +use Packetery\Module\Framework\WpAdapter; | |
| 19 | +use Packetery\Module\Log\DbLogger; | |
| 20 | +use Packetery\Module\ModuleHelper; | |
| 21 | +use Packetery\Module\WpdbAdapter; | |
| 22 | +use Packetery\Nette\Http; | |
| 23 | +use Packetery\Tracy\Debugger; | |
| 17 | 24 | |
| 18 | 25 | /** |
| 19 | 26 | * Class Exporter |
| 20 | 27 | * |
| @@ -21,67 +28,82 @@ | ||
| 21 | 28 | * @package Packetery\Module\Options |
| 22 | 29 | */ |
| 23 | 30 | class Exporter { |
| 24 | 31 | |
| 25 | - public const OPTION_LAST_SETTINGS_EXPORT = 'packetery_last_settings_export'; | |
| 26 | - public const ACTION_EXPORT_SETTINGS = 'export-settings'; | |
| 32 | + public const ACTION_EXPORT_SETTINGS = 'export-settings'; | |
| 27 | 33 | |
| 28 | 34 | /** |
| 29 | - * Http request. | |
| 30 | - * | |
| 31 | 35 | * @var Http\Request |
| 32 | 36 | */ |
| 33 | 37 | private $httpRequest; |
| 34 | 38 | |
| 35 | 39 | /** |
| 36 | - * Latte engine. | |
| 37 | - * | |
| 38 | 40 | * @var Engine |
| 39 | 41 | */ |
| 40 | 42 | private $latteEngine; |
| 41 | 43 | |
| 42 | 44 | /** |
| 43 | - * Country listing page. | |
| 44 | - * | |
| 45 | 45 | * @var CountryListingPage |
| 46 | 46 | */ |
| 47 | 47 | private $countryListingPage; |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | - * Options provider. | |
| 51 | - * | |
| 52 | - * @var Provider | |
| 50 | + * @var OptionsProvider | |
| 53 | 51 | */ |
| 54 | 52 | private $optionsProvider; |
| 55 | 53 | |
| 56 | 54 | /** |
| 57 | - * Logger. | |
| 58 | - * | |
| 59 | - * @var ILogger | |
| 55 | + * @var DbLogger | |
| 60 | 56 | */ |
| 61 | - private $logger; | |
| 57 | + private $dbLogger; | |
| 62 | 58 | |
| 63 | 59 | /** |
| 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. | |
| 60 | + * @var ModuleHelper | |
| 71 | 61 | */ |
| 62 | + private $moduleHelper; | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * @var WpAdapter | |
| 66 | + */ | |
| 67 | + private $wpAdapter; | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * @var WcAdapter | |
| 71 | + */ | |
| 72 | + private $wcAdapter; | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * @var CarrierUpdater | |
| 76 | + */ | |
| 77 | + private $carrierUpdater; | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * @var WpdbAdapter | |
| 81 | + */ | |
| 82 | + private $wpdbAdapter; | |
| 83 | + | |
| 72 | 84 | public function __construct( |
| 73 | 85 | Http\Request $httpRequest, |
| 74 | 86 | Engine $latteEngine, |
| 75 | 87 | CountryListingPage $countryListingPage, |
| 76 | - Provider $optionsProvider, | |
| 77 | - ILogger $logger | |
| 88 | + OptionsProvider $optionsProvider, | |
| 89 | + DbLogger $dbLogger, | |
| 90 | + ModuleHelper $moduleHelper, | |
| 91 | + WpAdapter $wpAdapter, | |
| 92 | + WcAdapter $wcAdapter, | |
| 93 | + CarrierUpdater $carrierUpdater, | |
| 94 | + WpdbAdapter $wpdbAdapter | |
| 78 | 95 | ) { |
| 79 | 96 | $this->httpRequest = $httpRequest; |
| 80 | 97 | $this->latteEngine = $latteEngine; |
| 81 | 98 | $this->countryListingPage = $countryListingPage; |
| 82 | 99 | $this->optionsProvider = $optionsProvider; |
| 83 | - $this->logger = $logger; | |
| 100 | + $this->dbLogger = $dbLogger; | |
| 101 | + $this->moduleHelper = $moduleHelper; | |
| 102 | + $this->wpAdapter = $wpAdapter; | |
| 103 | + $this->wcAdapter = $wcAdapter; | |
| 104 | + $this->carrierUpdater = $carrierUpdater; | |
| 105 | + $this->wpdbAdapter = $wpdbAdapter; | |
| 84 | 106 | } |
| 85 | 107 | |
| 86 | 108 | /** |
| 87 | 109 | * Prepares and outputs export text. |
| @@ -86,10 +108,8 @@ | ||
| 86 | 108 | /** |
| 87 | 109 | * Prepares and outputs export text. |
| 88 | 110 | */ |
| 89 | 111 | public function outputExportTxt(): void { |
| 90 | - global $wpdb; | |
| 91 | - | |
| 92 | 112 | if ( |
| 93 | 113 | $this->httpRequest->getQuery( 'page' ) !== Page::SLUG || |
| 94 | 114 | $this->httpRequest->getQuery( 'action' ) !== self::ACTION_EXPORT_SETTINGS |
| 95 | 115 | ) { |
| @@ -95,17 +115,27 @@ | ||
| 95 | 115 | ) { |
| 96 | 116 | return; |
| 97 | 117 | } |
| 98 | 118 | |
| 99 | - $globalSettings = $this->optionsProvider->data_to_array(); | |
| 100 | - if ( ! empty( $globalSettings['api_password'] ) ) { | |
| 101 | - $globalSettings['api_password'] = sprintf( | |
| 119 | + $txtContents = $this->getExportContent(); | |
| 120 | + header( 'Content-Type: text/plain' ); | |
| 121 | + header( 'Content-Transfer-Encoding: Binary' ); | |
| 122 | + header( 'Content-Length: ' . strlen( $txtContents ) ); | |
| 123 | + header( 'Content-Disposition: attachment; filename="packeta_options_export_' . gmdate( 'Y-m-d-H-i-s' ) . '.txt"' ); | |
| 124 | + ModuleHelper::renderString( $txtContents ); | |
| 125 | + exit; | |
| 126 | + } | |
| 127 | + | |
| 128 | + public function getExportContent(): string { | |
| 129 | + $globalSettings = $this->optionsProvider->getAllOptions(); | |
| 130 | + if ( isset( $globalSettings[ OptionNames::PACKETERY ]['api_password'] ) ) { | |
| 131 | + $globalSettings[ OptionNames::PACKETERY ]['api_password'] = sprintf( | |
| 102 | 132 | '%s...%s (%s)', |
| 103 | - substr( $globalSettings['api_password'], 0, 16 ), | |
| 104 | - substr( $globalSettings['api_password'], - 2, 2 ), | |
| 105 | - strlen( $globalSettings['api_password'] ) | |
| 133 | + substr( $globalSettings[ OptionNames::PACKETERY ]['api_password'], 0, 16 ), | |
| 134 | + substr( $globalSettings[ OptionNames::PACKETERY ]['api_password'], - 2, 2 ), | |
| 135 | + strlen( $globalSettings[ OptionNames::PACKETERY ]['api_password'] ) | |
| 106 | 136 | ); |
| 107 | - unset( $globalSettings['api_key'] ); | |
| 137 | + unset( $globalSettings[ OptionNames::PACKETERY ]['api_key'] ); | |
| 108 | 138 | } |
| 109 | 139 | $globalSettings['woocommerce_allowed_countries'] = get_option( 'woocommerce_allowed_countries' ); |
| 110 | 140 | $globalSettings['woocommerce_specific_allowed_countries'] = get_option( 'woocommerce_specific_allowed_countries' ); |
| 111 | 141 | $globalSettings['woocommerce_ship_to_countries'] = get_option( 'woocommerce_ship_to_countries' ); |
| @@ -110,8 +140,23 @@ | ||
| 110 | 140 | $globalSettings['woocommerce_specific_allowed_countries'] = get_option( 'woocommerce_specific_allowed_countries' ); |
| 111 | 141 | $globalSettings['woocommerce_ship_to_countries'] = get_option( 'woocommerce_ship_to_countries' ); |
| 112 | 142 | $globalSettings['woocommerce_specific_ship_to_countries'] = get_option( 'woocommerce_specific_ship_to_countries' ); |
| 113 | 143 | |
| 144 | + $shippingTaxClass = $this->wpAdapter->getOption( 'woocommerce_shipping_tax_class' ); | |
| 145 | + | |
| 146 | + $taxes = [ | |
| 147 | + 'calc_taxes' => $this->wpAdapter->getOption( 'woocommerce_calc_taxes' ), | |
| 148 | + 'prices_include_tax' => $this->wpAdapter->getOption( 'woocommerce_prices_include_tax' ), | |
| 149 | + 'tax_based_on' => $this->wpAdapter->getOption( 'woocommerce_tax_based_on' ), | |
| 150 | + 'shipping_tax_class' => $shippingTaxClass, | |
| 151 | + 'tax_round_at_subtotal' => $this->wpAdapter->getOption( 'woocommerce_tax_round_at_subtotal' ), | |
| 152 | + 'tax_classes' => $this->wpAdapter->getOption( 'woocommerce_tax_classes' ), | |
| 153 | + 'tax_display_shop' => $this->wpAdapter->getOption( 'woocommerce_tax_display_shop' ), | |
| 154 | + 'tax_display_cart' => $this->wpAdapter->getOption( 'woocommerce_tax_display_cart' ), | |
| 155 | + 'tax_total_display' => $this->wpAdapter->getOption( 'woocommerce_tax_total_display' ), | |
| 156 | + 'shipping_tax_class_settings' => $this->wcAdapter->taxGetRates( $shippingTaxClass ), | |
| 157 | + ]; | |
| 158 | + | |
| 114 | 159 | $activeTheme = wp_get_theme(); |
| 115 | 160 | $themeLatestVersion = \WC_Admin_Status::get_latest_theme_version( $activeTheme ); |
| 116 | 161 | $themeLatestVersionInfo = ( $themeLatestVersion !== $activeTheme->version ? ' (' . $themeLatestVersion . ' available)' : '' ); |
| 117 | 162 | $latteParams = [ |
| @@ -119,17 +164,18 @@ | ||
| 119 | 164 | 'wpVersion' => get_bloginfo( 'version' ), |
| 120 | 165 | 'wcVersion' => WC_VERSION, |
| 121 | 166 | 'template' => $activeTheme->name . ' ' . $activeTheme->version . $themeLatestVersionInfo, |
| 122 | 167 | 'phpVersion' => PHP_VERSION, |
| 123 | - 'dbServer' => $wpdb->db_server_info(), | |
| 168 | + 'dbServer' => $this->wpdbAdapter->dbServerInfo(), | |
| 124 | 169 | 'soap' => wc_bool_to_string( extension_loaded( 'soap' ) ), |
| 125 | 170 | 'wpDebug' => wc_bool_to_string( WP_DEBUG ), |
| 126 | - 'packetaDebug' => wc_bool_to_string( PACKETERY_DEBUG ), | |
| 171 | + 'packetaDebug' => wc_bool_to_string( Debugger::isEnabled() ), | |
| 127 | 172 | 'globalSettings' => $this->formatVariable( $globalSettings ), |
| 128 | - 'lastCarrierUpdate' => $this->countryListingPage->getLastUpdate(), | |
| 173 | + 'lastCarrierUpdate' => $this->carrierUpdater->getLastUpdate(), | |
| 129 | 174 | 'carriers' => $this->formatVariable( $this->countryListingPage->getCarriersForOptionsExport(), 0, true ), |
| 130 | 175 | 'zones' => $this->formatVariable( \WC_Shipping_Zones::get_zones() ), |
| 131 | - 'lastFiveDaysLogs' => $this->formatVariable( $this->remapLogRecords( $this->logger->getForPeriodAsArray( [ [ 'after' => '5 days ago' ] ] ) ) ), | |
| 176 | + 'taxes' => $this->formatVariable( $taxes ), | |
| 177 | + 'lastFiveDaysLogs' => $this->formatVariable( $this->remapLogRecords( $this->dbLogger->getForPeriodAsArray( [ [ 'after' => '5 days ago' ] ] ) ) ), | |
| 132 | 178 | 'generated' => gmdate( 'Y-m-d H:i:s' ), |
| 133 | 179 | /** |
| 134 | 180 | * Filter all_plugins filters the full array of plugins. |
| 135 | 181 | * |
| @@ -136,27 +182,19 @@ | ||
| 136 | 182 | * @since 3.0.0 |
| 137 | 183 | */ |
| 138 | 184 | 'plugins' => $this->getFormattedPlugins( (array) apply_filters( 'all_plugins', get_plugins() ) ), |
| 139 | 185 | 'muPlugins' => $this->getFormattedPlugins( get_mu_plugins() ), |
| 140 | - 'currencySwitchers' => $this->formatVariable( Module\CurrencySwitcherFacade::$supportedCurrencySwitchers ), | |
| 186 | + 'currencySwitchers' => $this->formatVariable( CurrencySwitcherService::$supportedCurrencySwitchers ), | |
| 141 | 187 | ]; |
| 142 | - update_option( self::OPTION_LAST_SETTINGS_EXPORT, gmdate( DATE_ATOM ) ); | |
| 188 | + update_option( OptionNames::LAST_SETTINGS_EXPORT, gmdate( DATE_ATOM ) ); | |
| 143 | 189 | |
| 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; | |
| 190 | + return $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/options/export.latte', $latteParams ); | |
| 153 | 191 | } |
| 154 | 192 | |
| 155 | 193 | /** |
| 156 | 194 | * Format plugins. |
| 157 | 195 | * |
| 158 | - * @param array $plugins Plugins. | |
| 196 | + * @param array<string, array<string, string>> $plugins Plugins. | |
| 159 | 197 | * |
| 160 | 198 | * @return string |
| 161 | 199 | */ |
| 162 | 200 | private function getFormattedPlugins( array $plugins ): string { |
| @@ -163,9 +201,9 @@ | ||
| 163 | 201 | $result = []; |
| 164 | 202 | |
| 165 | 203 | foreach ( $plugins as $relativePath => $plugin ) { |
| 166 | 204 | $item = [ |
| 167 | - 'Active' => wc_bool_to_string( Module\Helper::isPluginActive( $relativePath ) ), | |
| 205 | + 'Active' => wc_bool_to_string( $this->moduleHelper->isPluginActive( $relativePath ) ), | |
| 168 | 206 | ]; |
| 169 | 207 | |
| 170 | 208 | $options = [ 'Name', 'PluginURI', 'Version', 'WC tested up to', 'WC requires at least', 'AuthorName', 'RequiresPHP' ]; |
| 171 | 209 | foreach ( $options as $option ) { |
| @@ -178,13 +216,10 @@ | ||
| 178 | 216 | return $this->formatVariable( $result ); |
| 179 | 217 | } |
| 180 | 218 | |
| 181 | 219 | /** |
| 182 | - * Remaps log records. | |
| 183 | - * | |
| 184 | - * @param iterable $logs Logs. | |
| 185 | - * | |
| 186 | - * @return array | |
| 220 | + * @param \Generator<Record>|array{} $logs | |
| 221 | + * @return array<array<string, array<string, mixed>|string>> | |
| 187 | 222 | */ |
| 188 | 223 | private function remapLogRecords( iterable $logs ): array { |
| 189 | 224 | $result = []; |
| 190 | 225 | |
| @@ -213,9 +248,9 @@ | ||
| 213 | 248 | private function formatVariable( $variable, int $level = 0, bool $addSeparator = false ): string { |
| 214 | 249 | $output = ''; |
| 215 | 250 | if ( is_array( $variable ) ) { |
| 216 | 251 | foreach ( $variable as $key => $value ) { |
| 217 | - if ( 0 === $level && $addSeparator ) { | |
| 252 | + if ( $level === 0 && $addSeparator ) { | |
| 218 | 253 | $output .= '----------------------' . PHP_EOL; |
| 219 | 254 | } |
| 220 | 255 | $output .= str_repeat( ' ', $level ); |
| 221 | 256 | $output .= $key . ':'; |
| @@ -234,8 +269,9 @@ | ||
| 234 | 269 | $output .= PHP_EOL . $this->formatVariable( (array) $variable, $level ); |
| 235 | 270 | } elseif ( $variable instanceof \WC_Shipping_Method ) { |
| 236 | 271 | $methodInfo = [ |
| 237 | 272 | 'id' => $variable->id, |
| 273 | + // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps | |
| 238 | 274 | 'method_title' => $variable->method_title, |
| 239 | 275 | 'enabled' => $variable->enabled, |
| 240 | 276 | ]; |
| 241 | 277 | $output .= PHP_EOL . $this->formatVariable( $methodInfo, $level ); |