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

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

220 lines 6.4 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 * 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.
75 */
76 public function __construct(
77 Engine $latteEngine,
78 Carrier\Repository $carrierRepository,
79 Options\Provider $optionsProvider,
80 Carrier\OptionsPage $carrierOptionsPage,
81 Options\Page $optionsPage,
82 array $surveyConfig
83 ) {
84 $this->latteEngine = $latteEngine;
85 $this->carrierRepository = $carrierRepository;
86 $this->optionsProvider = $optionsProvider;
87 $this->carrierOptionsPage = $carrierOptionsPage;
88 $this->optionsPage = $optionsPage;
89 $this->surveyConfig = $surveyConfig;
90 }
91
92 /**
93 * Registers widget.
94 *
95 * @return void
96 */
97 public function register(): void {
98 add_action( 'wp_dashboard_setup', [ $this, 'setup' ] );
99 }
100
101 /**
102 * Dashborad setup.
103 *
104 * @return void
105 */
106 public function setup(): void {
107 wp_add_dashboard_widget( 'packetery_dashboard_widget', __( 'Packeta', 'packeta' ), [ $this, 'render' ], null, null, 'normal', 'high' );
108 }
109
110 /**
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 * Renders widget.
134 *
135 * @return void
136 */
137 public function render(): void {
138 $wcCountries = WC()->countries->get_countries();
139 $activeCountries = [];
140 $isCodSettingNeeded = false;
141
142 foreach ( $this->carrierRepository->getAllCarriersIncludingZpoints() as $carrier ) {
143 $country = $carrier->getCountry();
144 $carrierOptions = Carrier\Options::createByCarrierId( $carrier->getId() );
145
146 if ( false === $carrierOptions->isActive() ) {
147 continue;
148 }
149
150 if ( false === $isCodSettingNeeded && $this->optionsProvider->getCodPaymentMethod() === null && $carrierOptions->hasAnyCodSurchargeSetting() ) {
151 $isCodSettingNeeded = true;
152 }
153
154 if ( ! isset( $activeCountries[ $country ] ) ) {
155 $activeCountries[ $country ] = [
156 'code' => $country,
157 'name' => $wcCountries[ strtoupper( $country ) ],
158 'url' => $this->carrierOptionsPage->createUrl( $country ),
159 ];
160 }
161 }
162
163 $this->latteEngine->render(
164 PACKETERY_PLUGIN_DIR . '/template/dashboard-widget.latte',
165 [
166 'activeCountries' => $activeCountries,
167 'isCodSettingNeeded' => $isCodSettingNeeded,
168 'isOptionsFormValid' => $this->optionsPage->create_form()->isValid(),
169 'hasExternalCarrier' => $this->carrierRepository->hasAnyActiveFeedCarrier(),
170 'hasPacketaShipping' => $this->isPacketaShippingMethodActive(),
171 'survey' => new SurveyConfig(
172 ( $this->surveyConfig['active'] && new \DateTimeImmutable( 'now' ) <= $this->surveyConfig['validTo'] ),
173 $this->surveyConfig['url'],
174 Plugin::buildAssetUrl( 'public/survey-illustration.png' )
175 ),
176 'translations' => [
177 'packeta' => __( 'Packeta', 'packeta' ),
178 'activeCountriesNotice' => __( 'You have set up Packeta carriers for the following countries', 'packeta' ),
179 'noGlobalSettings' => sprintf(
180 // translators: 1: link start 2: link end.
181 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() )
183 ),
184 'noActiveCountry' => sprintf(
185 // translators: 1: link start 2: link end.
186 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() )
188 ),
189 'noCodPaymentConfigured' => sprintf(
190 // translators: 1: link start 2: link end.
191 esc_html__( 'No COD payment configured. The settings can be made %1$shere%2$s.', 'packeta' ),
192 ...Plugin::createLinkParts( $this->optionsPage->createLink() )
193 ),
194 'noExternalCarrier' => sprintf(
195 // translators: 1: link start 2: link end.
196 esc_html__( 'No external carrier was found. Carriers can be downloaded %1$shere%2$s.', 'packeta' ),
197 ...Plugin::createLinkParts( $this->carrierOptionsPage->createUrl() )
198 ),
199 'noPacketaShipping' => sprintf(
200 // translators: 1: link start 2: link end.
201 esc_html__( 'No Packeta shipping method was configured. Configure shipping zone with Packeta shipping method %1$shere%2$s.', 'packeta' ),
202 ...Plugin::createLinkParts(
203 add_query_arg(
204 [
205 'page' => 'wc-settings',
206 'tab' => 'shipping',
207 ],
208 get_admin_url( null, 'admin.php' )
209 )
210 )
211 ),
212 'surveyTitle' => __( 'Help us with plugin development', 'packeta' ),
213 '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' ),
214 'surveyButtonText' => __( 'Fill out a questionnaire', 'packeta' ),
215 ],
216 ]
217 );
218 }
219 }
220