Skip to content

Commit 511fb74

Browse files
committed
fix: Fixes and tweaks
1 parent f2f9e65 commit 511fb74

File tree

6 files changed

+145
-71
lines changed

6 files changed

+145
-71
lines changed

composer.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
],
2323
"require": {
2424
"php": ">=8.0",
25-
"x-wp/helper-functions": "^1.15",
26-
"x-wp/helper-traits": "^1.15"
25+
"x-wp/helper-functions": "^1.18.1",
26+
"x-wp/helper-traits": "^1.18.1"
2727
},
2828
"require-dev": {
29-
"oblak/wordpress-coding-standard": "^1",
30-
"php-stubs/woocommerce-stubs": "^9.0",
31-
"php-stubs/wordpress-stubs": "^6.5",
32-
"phpstan/extension-installer": "^1.3",
33-
"phpstan/phpstan": "^1.10",
34-
"phpstan/phpstan-deprecation-rules": "^1.1",
35-
"swissspidy/phpstan-no-private": "^0.2.0",
36-
"szepeviktor/phpstan-wordpress": "^1.3",
37-
"automattic/jetpack-constants": "^2.0"
29+
"oblak/wordpress-coding-standard": "^1.2.5",
30+
"php-stubs/woocommerce-stubs": "^9.5",
31+
"php-stubs/wordpress-stubs": "^6.7.1",
32+
"phpstan/extension-installer": "^1.4.3",
33+
"phpstan/phpstan": "^1.12.13",
34+
"phpstan/phpstan-deprecation-rules": "^1.2.1",
35+
"swissspidy/phpstan-no-private": "^0.2.1",
36+
"szepeviktor/phpstan-wordpress": "^1.3.5",
37+
"automattic/jetpack-constants": "^2.0.5"
3838
},
3939
"suggest": {
4040
"automattic/jetpack-autoloader": "Allow for better interoperability with other plugins that use this package."

src/Admin/Custom_Field.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ abstract class Custom_Field {
2828
*/
2929
private function __construct() {
3030
\add_filter( $this->get_hook(), array( $this, 'output_field' ), 99, $this->get_args() );
31-
\add_filter( $this->get_hook(), array( $this, 'set_output' ), 100, 0 );
3231
\add_filter( 'woocommerce_admin_settings_sanitize_option', array( $this, 'sanitize_option' ), 10, 3 );
3332
\add_filter( 'admin_footer', array( $this, 'output_css' ), 99, 0 );
3433
\add_filter( 'admin_footer', array( $this, 'output_js' ), 99, 0 );

src/Admin/Image_Select_Field.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function output_field( $html, string $raw_key, array $data, \WC_Settings_
4545
);
4646
$data = \wp_parse_args( $data, $defaults );
4747

48+
$this->set_output();
49+
4850
return \xwp_get_template_html(
4951
__DIR__ . '/Views/admin-html-image-select-template.php',
5052
array(

src/Admin/Repeater_Text_Settings_Field.php

Lines changed: 104 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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
}

src/Admin/Views/admin-html-repeater-text-template.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
defined( 'ABSPATH' ) || exit;
1414

15-
16-
$field_name = "{$field['field_name']}[]";
15+
$def_val = $field['default'] ?? array();
16+
$field_name = is_scalar( current( $def_val ) ) ? "{$field['field_name']}[]" : $field['field_name'];
1717
$field_desc = WC_Admin_Settings::get_field_description( $field );
1818

1919
?>
@@ -25,23 +25,22 @@
2525
</th>
2626
<td class="forminp forminp-<?php echo \esc_attr( \sanitize_title( $field['type'] ) ); ?>">
2727
<div id="<?php echo \esc_attr( $field['id'] ); ?>" class="repeater-rows">
28-
<?php foreach ( $value ?? array() as $row_value ) : ?>
29-
<div class="repeater-row row">
30-
<input
31-
name="<?php echo \esc_attr( $field_name ); ?>"
32-
id="<?php echo \esc_attr( $field['id'] ); ?>"
33-
type="text"
34-
value="<?php echo \esc_attr( $row_value ); ?>"
35-
class="<?php echo \esc_attr( $field['class'] ); ?>"
36-
placeholder="<?php echo \esc_attr( $field['placeholder'] ); ?>"
37-
<?php echo implode( ' ', $custom_atts ); // phpcs:ignore ?>
38-
/>
39-
<?php echo esc_html( $field['suffix'] ); ?> <?php echo $field_desc['description']; //phpcs:ignore ?>
40-
<button type="button" class="button minus repeater-remove-row">
41-
<?php \esc_html_e( 'Remove', 'woocommerce' ); ?>
42-
</button>
43-
</div>
44-
<?php endforeach; ?>
28+
<?php
29+
foreach ( $value ?? array() as $row_value ) {
30+
xwp_get_template(
31+
$row_tmpl,
32+
array(
33+
'class' => $field['class'],
34+
'custom_atts' => $custom_atts,
35+
'name' => $field_name,
36+
'placeholder' => $field['placeholder'],
37+
'suffix' => $field['suffix'],
38+
'type' => 'text',
39+
'value' => $row_value,
40+
),
41+
);
42+
}
43+
?>
4544
</div>
4645
<button
4746
type="button"
@@ -52,7 +51,7 @@ class="button plus repeater-add-row"
5251
data-value=""
5352
data-class="<?php echo \esc_attr( $field['class'] ); ?>"
5453
data-placeholder="<?php echo \esc_attr( $field['placeholder'] ); ?>"
55-
data-custom_atts="<?php echo \esc_attr( \implode( ' ', $custom_atts ) ); ?>"
54+
data-custom_atts="<?php echo \esc_attr( $custom_atts ); ?>"
5655
data-suffix="<?php echo \esc_attr( $field['suffix'] ); ?>"
5756
>
5857
<?php \esc_html_e( 'Add', 'woocommerce' ); ?>

src/Functions/xwc-settings-init.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010
/**
1111
* Initializes the settings.
1212
*/
13-
function xwc_settings_init(): void {
14-
static $xwc_settings_initialized;
13+
function xwc_settings_init(): void {
14+
static $xwc_settings_initialized;
1515

16-
if ( ! is_admin() || isset( $xwc_settings_initialized ) ) {
17-
return;
18-
}
16+
if ( ! is_admin() || isset( $xwc_settings_initialized ) ) {
17+
return;
18+
}
1919

2020
\XWC\Admin\Image_Select_Field::instance();
2121
\XWC\Admin\Repeater_Text_Settings_Field::instance();
2222

23-
$xwc_settings_initialized = true;
24-
}
23+
$xwc_settings_initialized = true;
24+
}
2525

26-
did_action( 'init' ) && ! doing_action( 'init' )
26+
did_action( 'plugins_loaded' ) && ! doing_action( 'plugins_loaded' )
2727
? xwc_settings_init()
28-
: add_action( 'init', 'xwc_settings_init', 99, 0 );
28+
: add_action( 'plugins_loaded', 'xwc_settings_init', 9999, 0 );
2929

3030
endif;

0 commit comments

Comments
 (0)