| @@ -1,93 +1,106 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Class DashboardWidget | |
| 4 | - * | |
| 5 | - * @package Packetery\Module | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | declare( strict_types=1 ); |
| 9 | 4 | |
| 10 | - | |
| 11 | 5 | namespace Packetery\Module; |
| 12 | 6 | |
| 13 | -use PacketeryLatte\Engine; | |
| 14 | -use WC_Data_Store; | |
| 15 | -use WC_Shipping_Zone; | |
| 7 | +use Packetery\Latte\Engine; | |
| 8 | +use Packetery\Module\Carrier\CarrierActivityBridge; | |
| 9 | +use Packetery\Module\Carrier\CarrierOptionsFactory; | |
| 10 | +use Packetery\Module\Carrier\CountryListingPage; | |
| 11 | +use Packetery\Module\Dashboard\DashboardHelper; | |
| 12 | +use Packetery\Module\Options\OptionsProvider; | |
| 13 | +use Packetery\Module\Views\UrlBuilder; | |
| 16 | 14 | |
| 17 | -/** | |
| 18 | - * Class DashboardWidget | |
| 19 | - * | |
| 20 | - * @package Packetery\Module | |
| 21 | - */ | |
| 22 | 15 | class DashboardWidget { |
| 23 | 16 | |
| 24 | 17 | /** |
| 25 | - * Latte engine. | |
| 26 | - * | |
| 27 | 18 | * @var Engine |
| 28 | 19 | */ |
| 29 | 20 | private $latteEngine; |
| 30 | 21 | |
| 31 | 22 | /** |
| 32 | - * Carrier repository. | |
| 33 | - * | |
| 34 | 23 | * @var Carrier\Repository |
| 35 | 24 | */ |
| 36 | 25 | private $carrierRepository; |
| 37 | 26 | |
| 38 | 27 | /** |
| 39 | - * Options provider. | |
| 40 | - * | |
| 41 | - * @var Options\Provider | |
| 28 | + * @var OptionsProvider | |
| 42 | 29 | */ |
| 43 | 30 | private $optionsProvider; |
| 44 | 31 | |
| 45 | 32 | /** |
| 46 | - * Carrier options page. | |
| 47 | - * | |
| 48 | 33 | * @var Carrier\OptionsPage |
| 49 | 34 | */ |
| 50 | 35 | private $carrierOptionsPage; |
| 51 | 36 | |
| 52 | 37 | /** |
| 53 | - * Options page. | |
| 54 | - * | |
| 55 | 38 | * @var Options\Page |
| 56 | 39 | */ |
| 57 | 40 | private $optionsPage; |
| 58 | 41 | |
| 59 | 42 | /** |
| 60 | - * Survey config. | |
| 61 | - * | |
| 62 | - * @var array | |
| 43 | + * @var array<string, bool|string> | |
| 63 | 44 | */ |
| 64 | 45 | private $surveyConfig; |
| 65 | 46 | |
| 66 | 47 | /** |
| 67 | - * Constructor. | |
| 68 | - * | |
| 69 | - * @param Engine $latteEngine Latte engine. | |
| 70 | - * @param Carrier\Repository $carrierRepository Carrier repository. | |
| 71 | - * @param Options\Provider $optionsProvider Options provider. | |
| 72 | - * @param Carrier\OptionsPage $carrierOptionsPage Carrier options page. | |
| 73 | - * @param Options\Page $optionsPage Options page. | |
| 74 | - * @param array $surveyConfig Survey config. | |
| 48 | + * @var Carrier\EntityRepository | |
| 75 | 49 | */ |
| 50 | + private $carrierEntityRepository; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * @var ModuleHelper | |
| 54 | + */ | |
| 55 | + private $moduleHelper; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * @var CarrierOptionsFactory | |
| 59 | + */ | |
| 60 | + private $carrierOptionsFactory; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * @var UrlBuilder | |
| 64 | + */ | |
| 65 | + private $urlBuilder; | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * @var CarrierActivityBridge | |
| 69 | + */ | |
| 70 | + private $carrierActivityBridge; | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * @var DashboardHelper | |
| 74 | + */ | |
| 75 | + private $dashboardHelper; | |
| 76 | + | |
| 76 | 77 | public function __construct( |
| 77 | 78 | Engine $latteEngine, |
| 78 | 79 | Carrier\Repository $carrierRepository, |
| 79 | - Options\Provider $optionsProvider, | |
| 80 | + OptionsProvider $optionsProvider, | |
| 80 | 81 | Carrier\OptionsPage $carrierOptionsPage, |
| 81 | 82 | Options\Page $optionsPage, |
| 82 | - array $surveyConfig | |
| 83 | + array $surveyConfig, | |
| 84 | + Carrier\EntityRepository $carrierEntityRepository, | |
| 85 | + ModuleHelper $moduleHelper, | |
| 86 | + CarrierOptionsFactory $carrierOptionsFactory, | |
| 87 | + UrlBuilder $urlBuilder, | |
| 88 | + CarrierActivityBridge $carrierActivityBridge, | |
| 89 | + DashboardHelper $dashboardHelper | |
| 83 | 90 | ) { |
| 84 | - $this->latteEngine = $latteEngine; | |
| 85 | - $this->carrierRepository = $carrierRepository; | |
| 86 | - $this->optionsProvider = $optionsProvider; | |
| 87 | - $this->carrierOptionsPage = $carrierOptionsPage; | |
| 88 | - $this->optionsPage = $optionsPage; | |
| 89 | - $this->surveyConfig = $surveyConfig; | |
| 91 | + $this->latteEngine = $latteEngine; | |
| 92 | + $this->carrierRepository = $carrierRepository; | |
| 93 | + $this->optionsProvider = $optionsProvider; | |
| 94 | + $this->carrierOptionsPage = $carrierOptionsPage; | |
| 95 | + $this->optionsPage = $optionsPage; | |
| 96 | + $this->surveyConfig = $surveyConfig; | |
| 97 | + $this->carrierEntityRepository = $carrierEntityRepository; | |
| 98 | + $this->moduleHelper = $moduleHelper; | |
| 99 | + $this->carrierOptionsFactory = $carrierOptionsFactory; | |
| 100 | + $this->urlBuilder = $urlBuilder; | |
| 101 | + $this->carrierActivityBridge = $carrierActivityBridge; | |
| 102 | + $this->dashboardHelper = $dashboardHelper; | |
| 90 | 103 | } |
| 91 | 104 | |
| 92 | 105 | /** |
| 93 | 106 | * Registers widget. |
| @@ -107,30 +120,8 @@ | ||
| 107 | 120 | wp_add_dashboard_widget( 'packetery_dashboard_widget', __( 'Packeta', 'packeta' ), [ $this, 'render' ], null, null, 'normal', 'high' ); |
| 108 | 121 | } |
| 109 | 122 | |
| 110 | 123 | /** |
| 111 | - * Tells if there is Packeta shipping method configured and active. | |
| 112 | - * | |
| 113 | - * @return bool | |
| 114 | - */ | |
| 115 | - private function isPacketaShippingMethodActive(): bool { | |
| 116 | - $shippingDataStore = WC_Data_Store::load( 'shipping-zone' ); | |
| 117 | - $shippingZones = $shippingDataStore->get_zones(); | |
| 118 | - | |
| 119 | - foreach ( $shippingZones as $shippingZoneId ) { | |
| 120 | - $shippingZone = new WC_Shipping_Zone( $shippingZoneId ); | |
| 121 | - $shippingZoneMethods = $shippingZone->get_shipping_methods( true ); | |
| 122 | - foreach ( $shippingZoneMethods as $method ) { | |
| 123 | - if ( ShippingMethod::PACKETERY_METHOD_ID === $method->id ) { | |
| 124 | - return true; | |
| 125 | - } | |
| 126 | - } | |
| 127 | - } | |
| 128 | - | |
| 129 | - return false; | |
| 130 | - } | |
| 131 | - | |
| 132 | - /** | |
| 133 | 124 | * Renders widget. |
| 134 | 125 | * |
| 135 | 126 | * @return void |
| 136 | 127 | */ |
| @@ -138,23 +129,23 @@ | ||
| 138 | 129 | $wcCountries = WC()->countries->get_countries(); |
| 139 | 130 | $activeCountries = []; |
| 140 | 131 | $isCodSettingNeeded = false; |
| 141 | 132 | |
| 142 | - foreach ( $this->carrierRepository->getAllCarriersIncludingZpoints() as $carrier ) { | |
| 133 | + foreach ( $this->carrierEntityRepository->getAllCarriersIncludingNonFeed() as $carrier ) { | |
| 143 | 134 | $country = $carrier->getCountry(); |
| 144 | - $carrierOptions = Carrier\Options::createByCarrierId( $carrier->getId() ); | |
| 135 | + $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() ); | |
| 145 | 136 | |
| 146 | - if ( false === $carrierOptions->isActive() ) { | |
| 137 | + if ( $this->carrierActivityBridge->isActive( $carrier, $carrierOptions ) === false ) { | |
| 147 | 138 | continue; |
| 148 | 139 | } |
| 149 | 140 | |
| 150 | - if ( false === $isCodSettingNeeded && $this->optionsProvider->getCodPaymentMethod() === null && $carrierOptions->hasAnyCodSurchargeSetting() ) { | |
| 141 | + if ( $isCodSettingNeeded === false && $this->optionsProvider->getCodPaymentMethods() === [] && $carrierOptions->hasAnyCodSurchargeSetting() ) { | |
| 151 | 142 | $isCodSettingNeeded = true; |
| 152 | 143 | } |
| 153 | 144 | |
| 154 | 145 | if ( ! isset( $activeCountries[ $country ] ) ) { |
| 155 | 146 | $activeCountries[ $country ] = [ |
| 156 | - 'code' => $country, | |
| 147 | + CountryListingPage::DATA_KEY_COUNTRY_CODE => $country, | |
| 157 | 148 | 'name' => $wcCountries[ strtoupper( $country ) ], |
| 158 | 149 | 'url' => $this->carrierOptionsPage->createUrl( $country ), |
| 159 | 150 | ]; |
| 160 | 151 | } |
| @@ -166,13 +157,13 @@ | ||
| 166 | 157 | 'activeCountries' => $activeCountries, |
| 167 | 158 | 'isCodSettingNeeded' => $isCodSettingNeeded, |
| 168 | 159 | 'isOptionsFormValid' => $this->optionsPage->create_form()->isValid(), |
| 169 | 160 | 'hasExternalCarrier' => $this->carrierRepository->hasAnyActiveFeedCarrier(), |
| 170 | - 'hasPacketaShipping' => $this->isPacketaShippingMethodActive(), | |
| 161 | + 'hasPacketaShipping' => $this->dashboardHelper->isPacketaShippingMethodActive(), | |
| 171 | 162 | 'survey' => new SurveyConfig( |
| 172 | 163 | ( $this->surveyConfig['active'] && new \DateTimeImmutable( 'now' ) <= $this->surveyConfig['validTo'] ), |
| 173 | 164 | $this->surveyConfig['url'], |
| 174 | - Plugin::buildAssetUrl( 'public/survey-illustration.png' ) | |
| 165 | + $this->urlBuilder->buildAssetUrl( 'public/images/survey-illustration.png' ) | |
| 175 | 166 | ), |
| 176 | 167 | 'translations' => [ |
| 177 | 168 | 'packeta' => __( 'Packeta', 'packeta' ), |
| 178 | 169 | 'activeCountriesNotice' => __( 'You have set up Packeta carriers for the following countries', 'packeta' ), |
| @@ -178,29 +169,29 @@ | ||
| 178 | 169 | 'activeCountriesNotice' => __( 'You have set up Packeta carriers for the following countries', 'packeta' ), |
| 179 | 170 | 'noGlobalSettings' => sprintf( |
| 180 | 171 | // translators: 1: link start 2: link end. |
| 181 | 172 | esc_html__( 'Global plugin settings have not been made, you can make the settings %1$shere%2$s.', 'packeta' ), |
| 182 | - ...Plugin::createLinkParts( $this->optionsPage->createLink() ) | |
| 173 | + ...$this->moduleHelper->createLinkParts( $this->optionsPage->createLink() ) | |
| 183 | 174 | ), |
| 184 | 175 | 'noActiveCountry' => sprintf( |
| 185 | 176 | // translators: 1: link start 2: link end. |
| 186 | 177 | esc_html__( 'Now you do not send parcels to any country via Packeta. The settings can be made %1$shere%2$s.', 'packeta' ), |
| 187 | - ...Plugin::createLinkParts( $this->carrierOptionsPage->createUrl() ) | |
| 178 | + ...$this->moduleHelper->createLinkParts( $this->carrierOptionsPage->createUrl() ) | |
| 188 | 179 | ), |
| 189 | 180 | 'noCodPaymentConfigured' => sprintf( |
| 190 | 181 | // translators: 1: link start 2: link end. |
| 191 | 182 | esc_html__( 'No COD payment configured. The settings can be made %1$shere%2$s.', 'packeta' ), |
| 192 | - ...Plugin::createLinkParts( $this->optionsPage->createLink() ) | |
| 183 | + ...$this->moduleHelper->createLinkParts( $this->optionsPage->createLink() ) | |
| 193 | 184 | ), |
| 194 | 185 | 'noExternalCarrier' => sprintf( |
| 195 | 186 | // translators: 1: link start 2: link end. |
| 196 | 187 | esc_html__( 'No external carrier was found. Carriers can be downloaded %1$shere%2$s.', 'packeta' ), |
| 197 | - ...Plugin::createLinkParts( $this->carrierOptionsPage->createUrl() ) | |
| 188 | + ...$this->moduleHelper->createLinkParts( $this->carrierOptionsPage->createUrl() ) | |
| 198 | 189 | ), |
| 199 | 190 | 'noPacketaShipping' => sprintf( |
| 200 | 191 | // translators: 1: link start 2: link end. |
| 201 | 192 | esc_html__( 'No Packeta shipping method was configured. Configure shipping zone with Packeta shipping method %1$shere%2$s.', 'packeta' ), |
| 202 | - ...Plugin::createLinkParts( | |
| 193 | + ...$this->moduleHelper->createLinkParts( | |
| 203 | 194 | add_query_arg( |
| 204 | 195 | [ |
| 205 | 196 | 'page' => 'wc-settings', |
| 206 | 197 | 'tab' => 'shipping', |