55/**
66 * Outputs the image select field in WooCommerce settings API forms
77 */
8- final class Repeater_Text_Settings_Field extends Custom_Field {
8+ class Repeater_Text_Settings_Field extends Custom_Field {
99 protected function get_name (): string {
1010 return 'repeater_text ' ;
1111 }
@@ -15,18 +15,23 @@ protected function get_type(): string {
1515 }
1616
1717 public function output_field ( array $ field ) {
18- $ value = \wc_string_to_array ( $ field[ ' value ' ] ?? '' );
18+ $ value = $ this -> get_value ( $ field );
1919 $ custom_atts = array ();
2020
2121 foreach ( ( $ field ['custom_attributes ' ] ?? array () ) as $ att_key => $ att_val ) {
22- $ custom_atts [] = \sprintf ( '%s="%s" ' , \esc_attr ( $ att_key ), \esc_attr ( $ att_val ) );
22+ if ( ! $ att_val ) {
23+ continue ;
24+ }
25+
26+ $ custom_atts [ $ att_key ] = $ att_val ;
2327 }
2428
2529 \xwp_get_template (
26- __DIR__ . ' /Views/admin-html-repeater-text-template.php ' ,
30+ $ this -> get_field_template () ,
2731 array (
28- 'custom_atts ' => $ custom_atts ,
32+ 'custom_atts ' => \wc_implode_html_attributes ( $ custom_atts ) ,
2933 'field ' => $ field ,
34+ 'row_tmpl ' => $ this ->get_row_template (),
3035 'value ' => $ value ,
3136 ),
3237 );
@@ -45,30 +50,33 @@ protected function get_css(): string {
4550 }
4651
4752 protected function get_js (): string {
48- return <<<'JS'
49- jQuery(function($){
50- var rptField = {
51- template: window.wp.template('xwc-repeater-text'),
52-
53- init: function() {
54- $('.repeater-add-row').on('click', (e) => this.addRow(e));
55- $('.repeater-rows').on('click', '.repeater-remove-row', (e) => this.removeRow(e));
56- },
57-
58- addRow: function(e) {
59- var {tmpl, ...data} = $(e.target).data();
60-
61- $('#'+tmpl).append(this.template(data));
62- },
63-
64- removeRow: function(e) {
65- $(e.target).closest('.row').remove();
66- }
67- };
68-
69- rptField.init();
70- });
71- JS;
53+ return \sprintf (
54+ <<<'JS'
55+ jQuery(function($){
56+ var rptField = {
57+ template: window.wp.template('%s'),
58+
59+ init: function() {
60+ $('.repeater-add-row').on('click', (e) => this.addRow(e));
61+ $('.repeater-rows').on('click', '.repeater-remove-row', (e) => this.removeRow(e));
62+ },
63+
64+ addRow: function(e) {
65+ var {tmpl, ...data} = $(e.target).data();
66+
67+ $('#'+tmpl).append(this.template(data));
68+ },
69+
70+ removeRow: function(e) {
71+ $(e.target).closest('.row').remove();
72+ }
73+ };
74+
75+ rptField.init();
76+ });
77+ JS,
78+ $ this ->get_row_template_id (),
79+ );
7280 }
7381
7482 public function output_js () {
@@ -78,10 +86,76 @@ public function output_js() {
7886
7987 parent ::output_js ();
8088
81- include __DIR__ . '/Views/admin-js-repeater-text-row-template.php ' ;
89+ \printf (
90+ '%s%s id="tmpl-%s" type="text/html" class="repeater-tmpl"> ' ,
91+ '< ' ,
92+ 'script ' ,
93+ \esc_attr ( $ this ->get_row_template_id () ),
94+ );
95+
96+ \xwp_get_template (
97+ $ this ->get_row_template (),
98+ array (
99+ 'class ' => '{{data.class}} ' ,
100+ 'custom_atts ' => '{{data.custom_atts}} ' ,
101+ 'name ' => '{{data.name}} ' ,
102+ 'placeholder ' => '{{data.placeholder}} ' ,
103+ 'suffix ' => '{{data.suffix}} ' ,
104+ 'type ' => '{{data.type}} ' ,
105+ 'value ' => '{{data.value}} ' ,
106+ ),
107+ );
108+
109+ echo '</%> ' ;
110+ }
111+
112+ /**
113+ * Get the field value
114+ *
115+ * @param array{value?: mixed, default?: mixed} $field The field data.
116+ * @return mixed
117+ */
118+ protected function get_value ( array $ field ): mixed {
119+ return \wc_string_to_array ( $ field ['value ' ] ?? '' );
82120 }
83121
84122 protected function sanitize ( mixed $ value , array $ option , mixed $ raw_value ): mixed {
85123 return \wc_string_to_array ( $ raw_value );
86124 }
125+
126+ /**
127+ * Get the field template path
128+ *
129+ * @return string
130+ */
131+ protected function get_field_template (): string {
132+ return __DIR__ . '/Views/admin-html-repeater-text-template.php ' ;
133+ }
134+
135+ /**
136+ * Get the HTML row template path
137+ *
138+ * @return string
139+ */
140+ protected function get_row_template (): string {
141+ return __DIR__ . '/Views/admin-html-repeater-text-row-template.php ' ;
142+ }
143+
144+ /**
145+ * Get the JS row template path
146+ *
147+ * @return string
148+ */
149+ protected function get_js_row_template (): string {
150+ return __DIR__ . '/Views/admin-js-repeater-text-row-template.php ' ;
151+ }
152+
153+ /**
154+ * Get the JS row template ID
155+ *
156+ * @return string
157+ */
158+ protected function get_row_template_id (): string {
159+ return 'xwc-repeater-text ' ;
160+ }
87161}
0 commit comments