PluginProbe
Packeta / 1.5.3
Packeta v1.5.3
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
packeta / src / Packetery / Module / DashboardWidget.php

DashboardWidget.php in Packeta 1.5.3, at src/Packetery/Module/DashboardWidget.php

230 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class DashboardWidget
4 *
5 * @package Packetery\Module
6 */
7
8 declare( strict_types=1 );
9
10
11 namespace Packetery\Module;
12
13 use PacketeryLatte\Engine;
14 use WC_Data_Store;
15 use WC_Shipping_Zone;
16
17 /**
18 * Class DashboardWidget
19 *
20 * @package Packetery\Module
21 */
22 class DashboardWidget {
23
24 /**
25 * Latte engine.
26 *
27 * @var Engine
28 */
29 private $latteEngine;
30
31 /**
32 * Carrier repository.
33 *
34 * @var Carrier\Repository
35 */
36 private $carrierRepository;
37
38 /**
39 * Options provider.
40 *
41 * @var Options\Provider
42 */
43 private $optionsProvider;
44
45 /**
46 * Carrier options page.
47 *
48 * @var Carrier\OptionsPage
49 */
50 private $carrierOptionsPage;
51
52 /**
53 * Options page.
54 *
55 * @var Options\Page
56 */
57 private $optionsPage;
58
59 /**
60 * Survey config.
61 *
62 * @var array
63 */
64 private $surveyConfig;
65
66 /**
67 * Carrier entity repository.
68 *
69 * @var Carrier\EntityRepository
70 */
71 private $carrierEntityRepository;
72
73 /**
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.
83 */
84 public function __construct(
85 Engine $latteEngine,
86 Carrier\Repository $carrierRepository,
87 Options\Provider $optionsProvider,
88 Carrier\OptionsPage $carrierOptionsPage,
89 Options\Page $optionsPage,
90 array $surveyConfig,
91 Carrier\EntityRepository $carrierEntityRepository
92 ) {
93 $this->latteEngine = $latteEngine;
94 $this->carrierRepository = $carrierRepository;
95 $this->optionsProvider = $optionsProvider;
96 $this->carrierOptionsPage = $carrierOptionsPage;
97 $this->optionsPage = $optionsPage;
98 $this->surveyConfig = $surveyConfig;
99 $this->carrierEntityRepository = $carrierEntityRepository;
100 }
101
102 /**
103 * Registers widget.
104 *
105 * @return void
106 */
107 public function register(): void {
108 add_action( 'wp_dashboard_setup', [ $this, 'setup' ] );
109 }
110
111 /**
112 * Dashborad setup.
113 *
114 * @return void
115 */
116 public function setup(): void {
117 wp_add_dashboard_widget( 'packetery_dashboard_widget', __( 'Packeta', 'packeta' ), [ $this, 'render' ], null, null, 'normal', 'high' );
118 }
119
120 /**
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 * Renders widget.
144 *
145 * @return void
146 */
147 public function render(): void {
148 $wcCountries = WC()->countries->get_countries();
149 $activeCountries = [];
150 $isCodSettingNeeded = false;
151
152 foreach ( $this->carrierEntityRepository->getAllCarriersIncludingNonFeed() as $carrier ) {
153 $country = $carrier->getCountry();
154 $carrierOptions = Carrier\Options::createByCarrierId( $carrier->getId() );
155
156 if ( false === $carrierOptions->isActive() ) {
157 continue;
158 }
159
160 if ( false === $isCodSettingNeeded && $this->optionsProvider->getCodPaymentMethod() === null && $carrierOptions->hasAnyCodSurchargeSetting() ) {
161 $isCodSettingNeeded = true;
162 }
163
164 if ( ! isset( $activeCountries[ $country ] ) ) {
165 $activeCountries[ $country ] = [
166 'code' => $country,
167 'name' => $wcCountries[ strtoupper( $country ) ],
168 'url' => $this->carrierOptionsPage->createUrl( $country ),
169 ];
170 }
171 }
172
173 $this->latteEngine->render(
174 PACKETERY_PLUGIN_DIR . '/template/dashboard-widget.latte',
175 [
176 'activeCountries' => $activeCountries,
177 'isCodSettingNeeded' => $isCodSettingNeeded,
178 'isOptionsFormValid' => $this->optionsPage->create_form()->isValid(),
179 'hasExternalCarrier' => $this->carrierRepository->hasAnyActiveFeedCarrier(),
180 'hasPacketaShipping' => $this->isPacketaShippingMethodActive(),
181 'survey' => new SurveyConfig(
182 ( $this->surveyConfig['active'] && new \DateTimeImmutable( 'now' ) <= $this->surveyConfig['validTo'] ),
183 $this->surveyConfig['url'],
184 Plugin::buildAssetUrl( 'public/survey-illustration.png' )
185 ),
186 'translations' => [
187 'packeta' => __( 'Packeta', 'packeta' ),
188 'activeCountriesNotice' => __( 'You have set up Packeta carriers for the following countries', 'packeta' ),
189 'noGlobalSettings' => sprintf(
190 // translators: 1: link start 2: link end.
191 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() )
193 ),
194 'noActiveCountry' => sprintf(
195 // translators: 1: link start 2: link end.
196 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() )
198 ),
199 'noCodPaymentConfigured' => sprintf(
200 // translators: 1: link start 2: link end.
201 esc_html__( 'No COD payment configured. The settings can be made %1$shere%2$s.', 'packeta' ),
202 ...Plugin::createLinkParts( $this->optionsPage->createLink() )
203 ),
204 'noExternalCarrier' => sprintf(
205 // translators: 1: link start 2: link end.
206 esc_html__( 'No external carrier was found. Carriers can be downloaded %1$shere%2$s.', 'packeta' ),
207 ...Plugin::createLinkParts( $this->carrierOptionsPage->createUrl() )
208 ),
209 'noPacketaShipping' => sprintf(
210 // translators: 1: link start 2: link end.
211 esc_html__( 'No Packeta shipping method was configured. Configure shipping zone with Packeta shipping method %1$shere%2$s.', 'packeta' ),
212 ...Plugin::createLinkParts(
213 add_query_arg(
214 [
215 'page' => 'wc-settings',
216 'tab' => 'shipping',
217 ],
218 get_admin_url( null, 'admin.php' )
219 )
220 )
221 ),
222 'surveyTitle' => __( 'Help us with plugin development', 'packeta' ),
223 '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' ),
224 'surveyButtonText' => __( 'Fill out a questionnaire', 'packeta' ),
225 ],
226 ]
227 );
228 }
229 }
230