Skip to content

Commit ba1322a

Browse files
authored
Merge pull request #19 from pronamic/18-only-first-checked-option-added-to-total-amount
18 only first checked option added to total amount
2 parents b2ec7fd + f0e755b commit ba1322a

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

phpcs.xml.dist

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
<ruleset name="WordPress Pay Contact Form 7 rules">
44
<file>.</file>
55

6+
<exclude-pattern type="relative">^build/*</exclude-pattern>
7+
<exclude-pattern type="relative">^node_modules/*</exclude-pattern>
8+
<exclude-pattern type="relative">^packages/*</exclude-pattern>
9+
<exclude-pattern type="relative">^vendor/*</exclude-pattern>
10+
611
<exclude-pattern>tests/bootstrap.php</exclude-pattern>
712
<exclude-pattern>tests/wp-config.php</exclude-pattern>
813
<exclude-pattern>js/dist/*.asset.php</exclude-pattern>

src/Pronamic.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ public static function get_submission_payment( WPCF7_Submission $submission ) {
7474
$tags = $submission_helper->get_tags_with_basetype_or_name_or_option( 'pronamic_pay_amount' );
7575

7676
foreach ( $tags as $tag ) {
77-
$value = $submission_helper->get_value_by_tag( $tag );
77+
$values = $submission_helper->get_values_by_tag( $tag );
7878

79-
try {
80-
$amount = $parser->parse( $value );
79+
foreach ( $values as $value ) {
80+
try {
81+
$amount = $parser->parse( $value );
8182

82-
$total = $total->add( $amount );
83-
} catch ( \Exception $e ) {
84-
continue;
83+
$total = $total->add( $amount );
84+
} catch ( \Exception $e ) {
85+
continue;
86+
}
8587
}
8688
}
8789

src/SubmissionHelper.php

+38-16
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,28 @@ private function get_hidden_fields() {
136136
}
137137

138138
/**
139-
* Get value by tag.
139+
* Get values by tag.
140140
*
141141
* @param WPCF7_FormTag $tag Tag.
142-
* @return string
142+
* @return array<string>
143143
*/
144-
public function get_value_by_tag( $tag ) {
145-
$value = $this->submission->get_posted_string( $tag->name );
144+
public function get_values_by_tag( $tag ) {
145+
/**
146+
* Hidden fields.
147+
*/
148+
$hidden_fields = $this->get_hidden_fields();
149+
150+
if ( \in_array( $tag->name, $hidden_fields, true ) ) {
151+
return [];
152+
}
153+
154+
$data = $this->submission->get_posted_data( $tag->name );
155+
156+
if ( null === $data ) {
157+
return [];
158+
}
159+
160+
$data = \wpcf7_array_flatten( $data );
146161

147162
/**
148163
* Contact Form 7 concatenates the field option value with user input for free text fields. We
@@ -151,25 +166,32 @@ public function get_value_by_tag( $tag ) {
151166
* @link https://github.com/rocklobster-in/contact-form-7/blob/2cfaa472fa485c6d3366fcdd80701fdaf7f9e425/includes/submission.php#L434-L437
152167
*/
153168
if ( \wpcf7_form_tag_supports( $tag->type, 'selectable-values' ) && $tag->has_option( 'free_text' ) ) {
154-
$values = \WPCF7_USE_PIPE ? $tag->pipes->collect_afters() : $tag->values;
169+
$tag_values = \WPCF7_USE_PIPE ? $tag->pipes->collect_afters() : $tag->values;
155170

156-
$last_value = \end( $values );
171+
$tag_value_last = \end( $tag_values );
157172

158-
if ( \str_starts_with( $value, $last_value . ' ' ) ) {
159-
$value = \substr( $value, \strlen( $last_value . ' ' ) );
173+
$value = \array_pop( $data );
174+
175+
if ( \str_starts_with( $value, $tag_value_last . ' ' ) ) {
176+
$value = \substr( $value, \strlen( $tag_value_last . ' ' ) );
160177
}
178+
179+
$data[] = $value;
161180
}
162181

163-
/**
164-
* Hidden fields.
165-
*/
166-
$hidden_fields = $this->get_hidden_fields();
182+
return $data;
183+
}
167184

168-
if ( \in_array( $tag->name, $hidden_fields, true ) ) {
169-
$value = '';
170-
}
185+
/**
186+
* Get value by tag.
187+
*
188+
* @param WPCF7_FormTag $tag Tag.
189+
* @return string
190+
*/
191+
public function get_value_by_tag( $tag ) {
192+
$values = $this->get_values_by_tag( $tag );
171193

172-
return $value;
194+
return \implode( ', ', $values );
173195
}
174196

175197
/**

0 commit comments

Comments
 (0)