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