PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
← All changes | src/Packetery/Module/DashboardWidget.php +55 -74 1.5.32.3.2 View file →
@@ -1,95 +1,93 @@
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 - * Carrier entity repository.
68 - *
69 48 * @var Carrier\EntityRepository
70 49 */
71 50 private $carrierEntityRepository;
72 51
73 52 /**
74 - * Constructor.
75 - *
76 - * @param Engine $latteEngine Latte engine.
77 - * @param Carrier\Repository $carrierRepository Carrier repository.
78 - * @param Options\Provider $optionsProvider Options provider.
79 - * @param Carrier\OptionsPage $carrierOptionsPage Carrier options page.
80 - * @param Options\Page $optionsPage Options page.
81 - * @param array $surveyConfig Survey config.
82 - * @param Carrier\EntityRepository $carrierEntityRepository Carrier repository.
53 + * @var ModuleHelper
83 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 +
84 77 public function __construct(
85 78 Engine $latteEngine,
86 79 Carrier\Repository $carrierRepository,
87 - Options\Provider $optionsProvider,
80 + OptionsProvider $optionsProvider,
88 81 Carrier\OptionsPage $carrierOptionsPage,
89 82 Options\Page $optionsPage,
90 83 array $surveyConfig,
91 - Carrier\EntityRepository $carrierEntityRepository
84 + Carrier\EntityRepository $carrierEntityRepository,
85 + ModuleHelper $moduleHelper,
86 + CarrierOptionsFactory $carrierOptionsFactory,
87 + UrlBuilder $urlBuilder,
88 + CarrierActivityBridge $carrierActivityBridge,
89 + DashboardHelper $dashboardHelper
92 90 ) {
93 91 $this->latteEngine = $latteEngine;
94 92 $this->carrierRepository = $carrierRepository;
95 93 $this->optionsProvider = $optionsProvider;
@@ -96,8 +94,13 @@
96 94 $this->carrierOptionsPage = $carrierOptionsPage;
97 95 $this->optionsPage = $optionsPage;
98 96 $this->surveyConfig = $surveyConfig;
99 97 $this->carrierEntityRepository = $carrierEntityRepository;
98 + $this->moduleHelper = $moduleHelper;
99 + $this->carrierOptionsFactory = $carrierOptionsFactory;
100 + $this->urlBuilder = $urlBuilder;
101 + $this->carrierActivityBridge = $carrierActivityBridge;
102 + $this->dashboardHelper = $dashboardHelper;
100 103 }
101 104
102 105 /**
103 106 * Registers widget.
@@ -117,30 +120,8 @@
117 120 wp_add_dashboard_widget( 'packetery_dashboard_widget', __( 'Packeta', 'packeta' ), [ $this, 'render' ], null, null, 'normal', 'high' );
118 121 }
119 122
120 123 /**
121 - * Tells if there is Packeta shipping method configured and active.
122 - *
123 - * @return bool
124 - */
125 - private function isPacketaShippingMethodActive(): bool {
126 - $shippingDataStore = WC_Data_Store::load( 'shipping-zone' );
127 - $shippingZones = $shippingDataStore->get_zones();
128 -
129 - foreach ( $shippingZones as $shippingZoneId ) {
130 - $shippingZone = new WC_Shipping_Zone( $shippingZoneId );
131 - $shippingZoneMethods = $shippingZone->get_shipping_methods( true );
132 - foreach ( $shippingZoneMethods as $method ) {
133 - if ( ShippingMethod::PACKETERY_METHOD_ID === $method->id ) {
134 - return true;
135 - }
136 - }
137 - }
138 -
139 - return false;
140 - }
141 -
142 - /**
143 124 * Renders widget.
144 125 *
145 126 * @return void
146 127 */
@@ -150,21 +131,21 @@
150 131 $isCodSettingNeeded = false;
151 132
152 133 foreach ( $this->carrierEntityRepository->getAllCarriersIncludingNonFeed() as $carrier ) {
153 134 $country = $carrier->getCountry();
154 - $carrierOptions = Carrier\Options::createByCarrierId( $carrier->getId() );
135 + $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() );
155 136
156 - if ( false === $carrierOptions->isActive() ) {
137 + if ( $this->carrierActivityBridge->isActive( $carrier, $carrierOptions ) === false ) {
157 138 continue;
158 139 }
159 140
160 - if ( false === $isCodSettingNeeded && $this->optionsProvider->getCodPaymentMethod() === null && $carrierOptions->hasAnyCodSurchargeSetting() ) {
141 + if ( $isCodSettingNeeded === false && $this->optionsProvider->getCodPaymentMethods() === [] && $carrierOptions->hasAnyCodSurchargeSetting() ) {
161 142 $isCodSettingNeeded = true;
162 143 }
163 144
164 145 if ( ! isset( $activeCountries[ $country ] ) ) {
165 146 $activeCountries[ $country ] = [
166 - 'code' => $country,
147 + CountryListingPage::DATA_KEY_COUNTRY_CODE => $country,
167 148 'name' => $wcCountries[ strtoupper( $country ) ],
168 149 'url' => $this->carrierOptionsPage->createUrl( $country ),
169 150 ];
170 151 }
@@ -176,13 +157,13 @@
176 157 'activeCountries' => $activeCountries,
177 158 'isCodSettingNeeded' => $isCodSettingNeeded,
178 159 'isOptionsFormValid' => $this->optionsPage->create_form()->isValid(),
179 160 'hasExternalCarrier' => $this->carrierRepository->hasAnyActiveFeedCarrier(),
180 - 'hasPacketaShipping' => $this->isPacketaShippingMethodActive(),
161 + 'hasPacketaShipping' => $this->dashboardHelper->isPacketaShippingMethodActive(),
181 162 'survey' => new SurveyConfig(
182 163 ( $this->surveyConfig['active'] && new \DateTimeImmutable( 'now' ) <= $this->surveyConfig['validTo'] ),
183 164 $this->surveyConfig['url'],
184 - Plugin::buildAssetUrl( 'public/survey-illustration.png' )
165 + $this->urlBuilder->buildAssetUrl( 'public/images/survey-illustration.png' )
185 166 ),
186 167 'translations' => [
187 168 'packeta' => __( 'Packeta', 'packeta' ),
188 169 'activeCountriesNotice' => __( 'You have set up Packeta carriers for the following countries', 'packeta' ),
@@ -188,29 +169,29 @@
188 169 'activeCountriesNotice' => __( 'You have set up Packeta carriers for the following countries', 'packeta' ),
189 170 'noGlobalSettings' => sprintf(
190 171 // translators: 1: link start 2: link end.
191 172 esc_html__( 'Global plugin settings have not been made, you can make the settings %1$shere%2$s.', 'packeta' ),
192 - ...Plugin::createLinkParts( $this->optionsPage->createLink() )
173 + ...$this->moduleHelper->createLinkParts( $this->optionsPage->createLink() )
193 174 ),
194 175 'noActiveCountry' => sprintf(
195 176 // translators: 1: link start 2: link end.
196 177 esc_html__( 'Now you do not send parcels to any country via Packeta. The settings can be made %1$shere%2$s.', 'packeta' ),
197 - ...Plugin::createLinkParts( $this->carrierOptionsPage->createUrl() )
178 + ...$this->moduleHelper->createLinkParts( $this->carrierOptionsPage->createUrl() )
198 179 ),
199 180 'noCodPaymentConfigured' => sprintf(
200 181 // translators: 1: link start 2: link end.
201 182 esc_html__( 'No COD payment configured. The settings can be made %1$shere%2$s.', 'packeta' ),
202 - ...Plugin::createLinkParts( $this->optionsPage->createLink() )
183 + ...$this->moduleHelper->createLinkParts( $this->optionsPage->createLink() )
203 184 ),
204 185 'noExternalCarrier' => sprintf(
205 186 // translators: 1: link start 2: link end.
206 187 esc_html__( 'No external carrier was found. Carriers can be downloaded %1$shere%2$s.', 'packeta' ),
207 - ...Plugin::createLinkParts( $this->carrierOptionsPage->createUrl() )
188 + ...$this->moduleHelper->createLinkParts( $this->carrierOptionsPage->createUrl() )
208 189 ),
209 190 'noPacketaShipping' => sprintf(
210 191 // translators: 1: link start 2: link end.
211 192 esc_html__( 'No Packeta shipping method was configured. Configure shipping zone with Packeta shipping method %1$shere%2$s.', 'packeta' ),
212 - ...Plugin::createLinkParts(
193 + ...$this->moduleHelper->createLinkParts(
213 194 add_query_arg(
214 195 [
215 196 'page' => 'wc-settings',
216 197 'tab' => 'shipping',