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