| 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 |
* 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. |
| 67 |
*/ |
| 68 |
public function __construct( |
| 69 |
Engine $latteEngine, |
| 70 |
Carrier\Repository $carrierRepository, |
| 71 |
Options\Provider $optionsProvider, |
| 72 |
Carrier\OptionsPage $carrierOptionsPage, |
| 73 |
Options\Page $optionsPage |
| 74 |
) { |
| 75 |
$this->latteEngine = $latteEngine; |
| 76 |
$this->carrierRepository = $carrierRepository; |
| 77 |
$this->optionsProvider = $optionsProvider; |
| 78 |
$this->carrierOptionsPage = $carrierOptionsPage; |
| 79 |
$this->optionsPage = $optionsPage; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Registers widget. |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function register(): void { |
| 88 |
add_action( 'wp_dashboard_setup', [ $this, 'setup' ] ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Dashborad setup. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function setup(): void { |
| 97 |
wp_add_dashboard_widget( 'packetery_dashboard_widget', __( 'Packeta', 'packeta' ), [ $this, 'render' ], null, null, 'normal', 'high' ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 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 |
* Renders widget. |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function render(): void { |
| 128 |
$wcCountries = WC()->countries->get_countries(); |
| 129 |
$activeCountries = []; |
| 130 |
$isCodSettingNeeded = false; |
| 131 |
|
| 132 |
foreach ( $this->carrierRepository->getAllCarriersIncludingZpoints() as $carrier ) { |
| 133 |
$country = $carrier->getCountry(); |
| 134 |
$carrierOptions = Carrier\Options::createByCarrierId( $carrier->getId() ); |
| 135 |
|
| 136 |
if ( false === $carrierOptions->isActive() ) { |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
if ( false === $isCodSettingNeeded && $this->optionsProvider->getCodPaymentMethod() === null && $carrierOptions->hasAnyCodSurchargeSetting() ) { |
| 141 |
$isCodSettingNeeded = true; |
| 142 |
} |
| 143 |
|
| 144 |
if ( ! isset( $activeCountries[ $country ] ) ) { |
| 145 |
$activeCountries[ $country ] = [ |
| 146 |
'code' => $country, |
| 147 |
'name' => $wcCountries[ strtoupper( $country ) ], |
| 148 |
'url' => $this->carrierOptionsPage->createUrl( $country ), |
| 149 |
]; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
$this->latteEngine->render( |
| 154 |
PACKETERY_PLUGIN_DIR . '/template/dashboard-widget.latte', |
| 155 |
[ |
| 156 |
'activeCountries' => $activeCountries, |
| 157 |
'isCodSettingNeeded' => $isCodSettingNeeded, |
| 158 |
'isOptionsFormValid' => $this->optionsPage->create_form()->isValid(), |
| 159 |
'hasExternalCarrier' => $this->carrierRepository->hasAnyActiveFeedCarrier(), |
| 160 |
'hasPacketaShipping' => $this->isPacketaShippingMethodActive(), |
| 161 |
'translations' => [ |
| 162 |
'activeCountriesNotice' => __( 'You have set up Packeta carriers for the following countries', 'packeta' ), |
| 163 |
'noGlobalSettings' => sprintf( |
| 164 |
// translators: 1: link start 2: link end. |
| 165 |
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() ) |
| 167 |
), |
| 168 |
'noActiveCountry' => sprintf( |
| 169 |
// translators: 1: link start 2: link end. |
| 170 |
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() ) |
| 172 |
), |
| 173 |
'noCodPaymentConfigured' => sprintf( |
| 174 |
// translators: 1: link start 2: link end. |
| 175 |
esc_html__( 'No COD payment configured. The settings can be made %1$shere%2$s.', 'packeta' ), |
| 176 |
...Plugin::createLinkParts( $this->optionsPage->createLink() ) |
| 177 |
), |
| 178 |
'noExternalCarrier' => sprintf( |
| 179 |
// translators: 1: link start 2: link end. |
| 180 |
esc_html__( 'No external carrier was found. Carriers can be downloaded %1$shere%2$s.', 'packeta' ), |
| 181 |
...Plugin::createLinkParts( $this->carrierOptionsPage->createUrl() ) |
| 182 |
), |
| 183 |
'noPacketaShipping' => sprintf( |
| 184 |
// translators: 1: link start 2: link end. |
| 185 |
esc_html__( 'No Packeta shipping method was configured. Configure shipping zone with Packeta shipping method %1$shere%2$s.', 'packeta' ), |
| 186 |
...Plugin::createLinkParts( |
| 187 |
add_query_arg( |
| 188 |
[ |
| 189 |
'page' => 'wc-settings', |
| 190 |
'tab' => 'shipping', |
| 191 |
], |
| 192 |
get_admin_url( null, 'admin.php' ) |
| 193 |
) |
| 194 |
) |
| 195 |
), |
| 196 |
], |
| 197 |
] |
| 198 |
); |
| 199 |
} |
| 200 |
} |
| 201 |
|