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