| @@ -1,83 +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 | - * Constructor. | |
| 61 | - * | |
| 62 | - * @param Engine $latteEngine Latte engine. | |
| 63 | - * @param Carrier\Repository $carrierRepository Carrier repository. | |
| 64 | - * @param Options\Provider $optionsProvider Options provider. | |
| 65 | - * @param Carrier\OptionsPage $carrierOptionsPage Carrier options page. | |
| 66 | - * @param Options\Page $optionsPage Options page. | |
| 43 | + * @var array<string, bool|string> | |
| 67 | 44 | */ |
| 45 | + private $surveyConfig; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * @var Carrier\EntityRepository | |
| 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 | + | |
| 68 | 77 | public function __construct( |
| 69 | 78 | Engine $latteEngine, |
| 70 | 79 | Carrier\Repository $carrierRepository, |
| 71 | - Options\Provider $optionsProvider, | |
| 80 | + OptionsProvider $optionsProvider, | |
| 72 | 81 | Carrier\OptionsPage $carrierOptionsPage, |
| 73 | - Options\Page $optionsPage | |
| 82 | + Options\Page $optionsPage, | |
| 83 | + array $surveyConfig, | |
| 84 | + Carrier\EntityRepository $carrierEntityRepository, | |
| 85 | + ModuleHelper $moduleHelper, | |
| 86 | + CarrierOptionsFactory $carrierOptionsFactory, | |
| 87 | + UrlBuilder $urlBuilder, | |
| 88 | + CarrierActivityBridge $carrierActivityBridge, | |
| 89 | + DashboardHelper $dashboardHelper | |
| 74 | 90 | ) { |
| 75 | - $this->latteEngine = $latteEngine; | |
| 76 | - $this->carrierRepository = $carrierRepository; | |
| 77 | - $this->optionsProvider = $optionsProvider; | |
| 78 | - $this->carrierOptionsPage = $carrierOptionsPage; | |
| 79 | - $this->optionsPage = $optionsPage; | |
| 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; | |
| 80 | 103 | } |
| 81 | 104 | |
| 82 | 105 | /** |
| 83 | 106 | * Registers widget. |
| @@ -97,30 +120,8 @@ | ||
| 97 | 120 | wp_add_dashboard_widget( 'packetery_dashboard_widget', __( 'Packeta', 'packeta' ), [ $this, 'render' ], null, null, 'normal', 'high' ); |
| 98 | 121 | } |
| 99 | 122 | |
| 100 | 123 | /** |
| 101 | - * Tells if there is Packeta shipping method configured and active. | |
| 102 | - * | |
| 103 | - * @return bool | |
| 104 | - */ | |
| 105 | - private function isPacketaShippingMethodActive(): bool { | |
| 106 | - $shippingDataStore = WC_Data_Store::load( 'shipping-zone' ); | |
| 107 | - $shippingZones = $shippingDataStore->get_zones(); | |
| 108 | - | |
| 109 | - foreach ( $shippingZones as $shippingZoneId ) { | |
| 110 | - $shippingZone = new WC_Shipping_Zone( $shippingZoneId ); | |
| 111 | - $shippingZoneMethods = $shippingZone->get_shipping_methods( true ); | |
| 112 | - foreach ( $shippingZoneMethods as $method ) { | |
| 113 | - if ( ShippingMethod::PACKETERY_METHOD_ID === $method->id ) { | |
| 114 | - return true; | |
| 115 | - } | |
| 116 | - } | |
| 117 | - } | |
| 118 | - | |
| 119 | - return false; | |
| 120 | - } | |
| 121 | - | |
| 122 | - /** | |
| 123 | 124 | * Renders widget. |
| 124 | 125 | * |
| 125 | 126 | * @return void |
| 126 | 127 | */ |
| @@ -128,23 +129,23 @@ | ||
| 128 | 129 | $wcCountries = WC()->countries->get_countries(); |
| 129 | 130 | $activeCountries = []; |
| 130 | 131 | $isCodSettingNeeded = false; |
| 131 | 132 | |
| 132 | - foreach ( $this->carrierRepository->getAllCarriersIncludingZpoints() as $carrier ) { | |
| 133 | + foreach ( $this->carrierEntityRepository->getAllCarriersIncludingNonFeed() as $carrier ) { | |
| 133 | 134 | $country = $carrier->getCountry(); |
| 134 | - $carrierOptions = Carrier\Options::createByCarrierId( $carrier->getId() ); | |
| 135 | + $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() ); | |
| 135 | 136 | |
| 136 | - if ( false === $carrierOptions->isActive() ) { | |
| 137 | + if ( $this->carrierActivityBridge->isActive( $carrier, $carrierOptions ) === false ) { | |
| 137 | 138 | continue; |
| 138 | 139 | } |
| 139 | 140 | |
| 140 | - if ( false === $isCodSettingNeeded && $this->optionsProvider->getCodPaymentMethod() === null && $carrierOptions->hasAnyCodSurchargeSetting() ) { | |
| 141 | + if ( $isCodSettingNeeded === false && $this->optionsProvider->getCodPaymentMethods() === [] && $carrierOptions->hasAnyCodSurchargeSetting() ) { | |
| 141 | 142 | $isCodSettingNeeded = true; |
| 142 | 143 | } |
| 143 | 144 | |
| 144 | 145 | if ( ! isset( $activeCountries[ $country ] ) ) { |
| 145 | 146 | $activeCountries[ $country ] = [ |
| 146 | - 'code' => $country, | |
| 147 | + CountryListingPage::DATA_KEY_COUNTRY_CODE => $country, | |
| 147 | 148 | 'name' => $wcCountries[ strtoupper( $country ) ], |
| 148 | 149 | 'url' => $this->carrierOptionsPage->createUrl( $country ), |
| 149 | 150 | ]; |
| 150 | 151 | } |
| @@ -156,35 +157,41 @@ | ||
| 156 | 157 | 'activeCountries' => $activeCountries, |
| 157 | 158 | 'isCodSettingNeeded' => $isCodSettingNeeded, |
| 158 | 159 | 'isOptionsFormValid' => $this->optionsPage->create_form()->isValid(), |
| 159 | 160 | 'hasExternalCarrier' => $this->carrierRepository->hasAnyActiveFeedCarrier(), |
| 160 | - 'hasPacketaShipping' => $this->isPacketaShippingMethodActive(), | |
| 161 | + 'hasPacketaShipping' => $this->dashboardHelper->isPacketaShippingMethodActive(), | |
| 162 | + 'survey' => new SurveyConfig( | |
| 163 | + ( $this->surveyConfig['active'] && new \DateTimeImmutable( 'now' ) <= $this->surveyConfig['validTo'] ), | |
| 164 | + $this->surveyConfig['url'], | |
| 165 | + $this->urlBuilder->buildAssetUrl( 'public/images/survey-illustration.png' ) | |
| 166 | + ), | |
| 161 | 167 | 'translations' => [ |
| 168 | + 'packeta' => __( 'Packeta', 'packeta' ), | |
| 162 | 169 | 'activeCountriesNotice' => __( 'You have set up Packeta carriers for the following countries', 'packeta' ), |
| 163 | 170 | 'noGlobalSettings' => sprintf( |
| 164 | 171 | // translators: 1: link start 2: link end. |
| 165 | 172 | esc_html__( 'Global plugin settings have not been made, you can make the settings %1$shere%2$s.', 'packeta' ), |
| 166 | - ...Plugin::createLinkParts( $this->optionsPage->createLink() ) | |
| 173 | + ...$this->moduleHelper->createLinkParts( $this->optionsPage->createLink() ) | |
| 167 | 174 | ), |
| 168 | 175 | 'noActiveCountry' => sprintf( |
| 169 | 176 | // translators: 1: link start 2: link end. |
| 170 | 177 | esc_html__( 'Now you do not send parcels to any country via Packeta. The settings can be made %1$shere%2$s.', 'packeta' ), |
| 171 | - ...Plugin::createLinkParts( $this->carrierOptionsPage->createUrl() ) | |
| 178 | + ...$this->moduleHelper->createLinkParts( $this->carrierOptionsPage->createUrl() ) | |
| 172 | 179 | ), |
| 173 | 180 | 'noCodPaymentConfigured' => sprintf( |
| 174 | 181 | // translators: 1: link start 2: link end. |
| 175 | 182 | esc_html__( 'No COD payment configured. The settings can be made %1$shere%2$s.', 'packeta' ), |
| 176 | - ...Plugin::createLinkParts( $this->optionsPage->createLink() ) | |
| 183 | + ...$this->moduleHelper->createLinkParts( $this->optionsPage->createLink() ) | |
| 177 | 184 | ), |
| 178 | 185 | 'noExternalCarrier' => sprintf( |
| 179 | 186 | // translators: 1: link start 2: link end. |
| 180 | 187 | esc_html__( 'No external carrier was found. Carriers can be downloaded %1$shere%2$s.', 'packeta' ), |
| 181 | - ...Plugin::createLinkParts( $this->carrierOptionsPage->createUrl() ) | |
| 188 | + ...$this->moduleHelper->createLinkParts( $this->carrierOptionsPage->createUrl() ) | |
| 182 | 189 | ), |
| 183 | 190 | 'noPacketaShipping' => sprintf( |
| 184 | 191 | // translators: 1: link start 2: link end. |
| 185 | 192 | esc_html__( 'No Packeta shipping method was configured. Configure shipping zone with Packeta shipping method %1$shere%2$s.', 'packeta' ), |
| 186 | - ...Plugin::createLinkParts( | |
| 193 | + ...$this->moduleHelper->createLinkParts( | |
| 187 | 194 | add_query_arg( |
| 188 | 195 | [ |
| 189 | 196 | 'page' => 'wc-settings', |
| 190 | 197 | 'tab' => 'shipping', |
| @@ -192,8 +199,11 @@ | ||
| 192 | 199 | get_admin_url( null, 'admin.php' ) |
| 193 | 200 | ) |
| 194 | 201 | ) |
| 195 | 202 | ), |
| 203 | + 'surveyTitle' => __( 'Help us with plugin development', 'packeta' ), | |
| 204 | + 'surveyDescription' => __( 'An effective way to improve our plugin is to ask you, its users, for your opinion. It won\'t take you even two minutes and it will help us a lot to develop the plugin in the right way. Thank you very much.', 'packeta' ), | |
| 205 | + 'surveyButtonText' => __( 'Fill out a questionnaire', 'packeta' ), | |
| 196 | 206 | ], |
| 197 | 207 | ] |
| 198 | 208 | ); |
| 199 | 209 | } |