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