PluginProbe
Packeta / 1.2.6
Packeta v1.2.6
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
packeta / src / Packetery / Module / Product / DataTab.php

DataTab.php in Packeta 1.2.6, at src/Packetery/Module/Product/DataTab.php

147 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Packetery product tab.
4 *
5 * @package Packetery\Module\Product
6 */
7
8 declare( strict_types=1 );
9
10
11 namespace Packetery\Module\Product;
12
13 use Packetery\Module\FormFactory;
14 use Packetery\Module\Product;
15 use PacketeryLatte\Engine;
16 use PacketeryNette\Forms\Form;
17
18 /**
19 * Class Tab
20 *
21 * @package Packetery\Module\Product
22 */
23 class DataTab {
24
25 const NAME = 'packetery-tab';
26
27 /**
28 * Form factory.
29 *
30 * @var FormFactory
31 */
32 private $formFactory;
33
34 /**
35 * Latte engine.
36 *
37 * @var Engine
38 */
39 private $latteEngine;
40
41 /**
42 * Tab constructor.
43 *
44 * @param FormFactory $formFactory Factory engine.
45 * @param Engine $latteEngine Latte engine.
46 */
47 public function __construct( FormFactory $formFactory, Engine $latteEngine ) {
48 $this->formFactory = $formFactory;
49 $this->latteEngine = $latteEngine;
50 }
51
52 /**
53 * Register component.
54 *
55 * @return void
56 */
57 public function register(): void {
58 add_filter( 'woocommerce_product_data_tabs', [ $this, 'registerTab' ], 1, 1 );
59 add_action( 'woocommerce_product_data_panels', [ $this, 'render' ] );
60 add_action( 'woocommerce_process_product_meta', [ $this, 'saveData' ] );
61 }
62
63 /**
64 * Registers tab.
65 *
66 * @param array $tabs Tabs definition array.
67 *
68 * @return array
69 */
70 public function registerTab( array $tabs ): array {
71 $tabs[ self::NAME ] = [
72 'label' => __( 'Packeta', 'packetery' ),
73 'target' => self::NAME,
74 'class' => [ 'hide_if_virtual', 'hide_if_downloadable' ],
75 ];
76
77 return $tabs;
78 }
79
80 /**
81 * Creates form instance.
82 *
83 * @param Product\Entity $product Related product.
84 *
85 * @return Form
86 */
87 private function createForm( Product\Entity $product ): Form {
88 $form = $this->formFactory->create();
89 $form->addCheckbox( Product\Entity::META_AGE_VERIFICATION_18_PLUS, __( 'ageVerification18PlusLabel', 'packetery' ) );
90
91 $form->setDefaults(
92 [ Product\Entity::META_AGE_VERIFICATION_18_PLUS => $product->isAgeVerification18PlusRequired() ]
93 );
94
95 return $form;
96 }
97
98 /**
99 * Renders tab.
100 *
101 * @return void
102 */
103 public function render(): void {
104 $this->latteEngine->render(
105 PACKETERY_PLUGIN_DIR . '/template/product/data-tab-panel.latte',
106 [ 'form' => $this->createForm( Product\Entity::fromGlobals() ) ]
107 );
108 }
109
110 /**
111 * Saves product data.
112 *
113 * @param int|string $postId Post ID.
114 */
115 public function saveData( $postId ): void {
116 $product = Product\Entity::fromPostId( $postId );
117 if ( false === $product->isPhysical() ) {
118 return;
119 }
120
121 $form = $this->createForm( $product );
122 $form->onSuccess[] = function( Form $form, array $values ) use ( $product ) {
123 $this->processFormData( $product->getId(), $values );
124 };
125
126 if ( $form->isSubmitted() ) {
127 $form->fireEvents();
128 }
129 }
130
131 /**
132 * Process form data.
133 *
134 * @param int $productId Product ID.
135 * @param array $values Form values.
136 */
137 public function processFormData( int $productId, array $values ): void {
138 foreach ( $values as $attr => $value ) {
139 if ( is_bool( $value ) ) {
140 $value = $value ? '1' : '0';
141 }
142
143 update_post_meta( $productId, $attr, $value );
144 }
145 }
146 }
147