PluginProbe
Packeta / 2.1
Packeta v2.1
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
← All changes | src/Packetery/Module/Product/DataTab.php +50 -19 1.6.12.1 View file →
@@ -6,15 +6,17 @@
6 6 */
7 7
8 8 declare( strict_types=1 );
9 9
10 -
11 10 namespace Packetery\Module\Product;
12 11
12 +use Packetery\Latte\Engine;
13 +use Packetery\Module\Carrier\CarDeliveryConfig;
13 14 use Packetery\Module\Carrier\EntityRepository;
15 +use Packetery\Module\Carrier\OptionPrefixer;
16 +use Packetery\Module\Exception\ProductNotFoundException;
14 17 use Packetery\Module\FormFactory;
15 18 use Packetery\Module\Product;
16 -use Packetery\Latte\Engine;
17 19 use Packetery\Nette\Forms\Form;
18 20
19 21 /**
20 22 * Class Tab
@@ -46,18 +48,42 @@
46 48 */
47 49 private $carrierRepository;
48 50
49 51 /**
52 + * Car delivery config.
53 + *
54 + * @var CarDeliveryConfig
55 + */
56 + private $carDeliveryConfig;
57 +
58 + /**
59 + * Product entity factory.
60 + *
61 + * @var ProductEntityFactory
62 + */
63 + private $productEntityFactory;
64 +
65 + /**
50 66 * Tab constructor.
51 67 *
52 - * @param FormFactory $formFactory Factory engine.
53 - * @param Engine $latteEngine Latte engine.
54 - * @param EntityRepository $carrierRepository Carrier repository.
68 + * @param FormFactory $formFactory Factory engine.
69 + * @param Engine $latteEngine Latte engine.
70 + * @param EntityRepository $carrierRepository Carrier repository.
71 + * @param CarDeliveryConfig $carDeliveryConfig Car Delivery config.
72 + * @param ProductEntityFactory $productEntityFactory Product entity factory.
55 73 */
56 - public function __construct( FormFactory $formFactory, Engine $latteEngine, EntityRepository $carrierRepository ) {
57 - $this->formFactory = $formFactory;
58 - $this->latteEngine = $latteEngine;
59 - $this->carrierRepository = $carrierRepository;
74 + public function __construct(
75 + FormFactory $formFactory,
76 + Engine $latteEngine,
77 + EntityRepository $carrierRepository,
78 + CarDeliveryConfig $carDeliveryConfig,
79 + ProductEntityFactory $productEntityFactory
80 + ) {
81 + $this->formFactory = $formFactory;
82 + $this->latteEngine = $latteEngine;
83 + $this->carrierRepository = $carrierRepository;
84 + $this->carDeliveryConfig = $carDeliveryConfig;
85 + $this->productEntityFactory = $productEntityFactory;
60 86 }
61 87
62 88 /**
63 89 * Register component.
@@ -72,11 +98,11 @@
72 98
73 99 /**
74 100 * Registers tab.
75 101 *
76 - * @param array $tabs Tabs definition array.
102 + * @param string[] $tabs Tabs definition array.
77 103 *
78 - * @return array
104 + * @return array<array<string,string[]|string>|string>
79 105 */
80 106 public function registerTab( array $tabs ): array {
81 107 $tabs[ self::NAME ] = [
82 108 'label' => __( 'Packeta', 'packeta' ),
@@ -100,14 +126,17 @@
100 126
101 127 $carriersContainer = $form->addContainer( Product\Entity::META_DISALLOWED_SHIPPING_RATES );
102 128 $carriersList = $this->carrierRepository->getAllActiveCarriersList();
103 129 foreach ( $carriersList as $carrier ) {
130 + if ( $this->carDeliveryConfig->isCarDeliveryCarrierDisabled( OptionPrefixer::removePrefix( $carrier['option_id'] ) ) ) {
131 + continue;
132 + }
104 133 $carriersContainer->addCheckbox( $carrier['option_id'], $carrier['label'] );
105 134 }
106 135
107 136 $form->setDefaults(
108 137 [
109 - Product\Entity::META_AGE_VERIFICATION_18_PLUS => $product->isAgeVerification18PlusRequired(),
138 + Product\Entity::META_AGE_VERIFICATION_18_PLUS => $product->isAgeVerificationRequired(),
110 139 Product\Entity::META_DISALLOWED_SHIPPING_RATES => $product->getDisallowedShippingRateChoices(),
111 140 ]
112 141 );
113 142
@@ -122,9 +151,9 @@
122 151 public function render(): void {
123 152 $this->latteEngine->render(
124 153 PACKETERY_PLUGIN_DIR . '/template/product/data-tab-panel.latte',
125 154 [
126 - 'form' => $this->createForm( Product\Entity::fromGlobals() ),
155 + 'form' => $this->createForm( $this->productEntityFactory->fromGlobals() ),
127 156 'translations' => [
128 157 'disallowedShippingRatesHeading' => __( 'Packeta shipping methods disabled for this product.', 'packeta' ),
129 158 ],
130 159 ]
@@ -134,17 +163,19 @@
134 163 /**
135 164 * Saves product data.
136 165 *
137 166 * @param int|string $postId Post ID.
167 + *
168 + * @throws ProductNotFoundException Product not found.
138 169 */
139 170 public function saveData( $postId ): void {
140 - $product = Product\Entity::fromPostId( $postId );
141 - if ( false === $product->isPhysical() ) {
171 + $product = $this->productEntityFactory->fromPostId( $postId );
172 + if ( $product->isPhysical() === false ) {
142 173 return;
143 174 }
144 175
145 176 $form = $this->createForm( $product );
146 - $form->onSuccess[] = function( Form $form, array $values ) use ( $product ) {
177 + $form->onSuccess[] = function ( Form $form, array $values ) use ( $product ) {
147 178 $this->processFormData( $product->getId(), $values );
148 179 };
149 180
150 181 if ( $form->isSubmitted() ) {
@@ -154,10 +185,10 @@
154 185
155 186 /**
156 187 * Process form data.
157 188 *
158 - * @param int $productId Product ID.
159 - * @param array $values Form values.
189 + * @param int $productId Product ID.
190 + * @param array<string, bool|array<string,bool>> $values Form values.
160 191 */
161 192 public function processFormData( int $productId, array $values ): void {
162 193 foreach ( $values as $attr => $value ) {
163 194 if ( is_bool( $value ) ) {
@@ -163,9 +194,9 @@
163 194 if ( is_bool( $value ) ) {
164 195 $value = $value ? '1' : '0';
165 196 }
166 197
167 - if ( Product\Entity::META_DISALLOWED_SHIPPING_RATES === $attr ) {
198 + if ( $attr === Product\Entity::META_DISALLOWED_SHIPPING_RATES ) {
168 199 $value = array_filter( $value );
169 200 }
170 201
171 202 update_post_meta( $productId, $attr, $value );