Skip to content

Commit

Permalink
Merge pull request #19 from pronamic/18-only-first-checked-option-add…
Browse files Browse the repository at this point in the history
…ed-to-total-amount

18 only first checked option added to total amount
  • Loading branch information
rvdsteege committed Dec 19, 2023
2 parents b2ec7fd + f0e755b commit ba1322a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
5 changes: 5 additions & 0 deletions phpcs.xml.dist
Expand Up @@ -3,6 +3,11 @@
<ruleset name="WordPress Pay Contact Form 7 rules">
<file>.</file>

<exclude-pattern type="relative">^build/*</exclude-pattern>
<exclude-pattern type="relative">^node_modules/*</exclude-pattern>
<exclude-pattern type="relative">^packages/*</exclude-pattern>
<exclude-pattern type="relative">^vendor/*</exclude-pattern>

<exclude-pattern>tests/bootstrap.php</exclude-pattern>
<exclude-pattern>tests/wp-config.php</exclude-pattern>
<exclude-pattern>js/dist/*.asset.php</exclude-pattern>
Expand Down
14 changes: 8 additions & 6 deletions src/Pronamic.php
Expand Up @@ -74,14 +74,16 @@ public static function get_submission_payment( WPCF7_Submission $submission ) {
$tags = $submission_helper->get_tags_with_basetype_or_name_or_option( 'pronamic_pay_amount' );

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

try {
$amount = $parser->parse( $value );
foreach ( $values as $value ) {
try {
$amount = $parser->parse( $value );

$total = $total->add( $amount );
} catch ( \Exception $e ) {
continue;
$total = $total->add( $amount );
} catch ( \Exception $e ) {
continue;
}
}
}

Expand Down
54 changes: 38 additions & 16 deletions src/SubmissionHelper.php
Expand Up @@ -136,13 +136,28 @@ private function get_hidden_fields() {
}

/**
* Get value by tag.
* Get values by tag.
*
* @param WPCF7_FormTag $tag Tag.
* @return string
* @return array<string>
*/
public function get_value_by_tag( $tag ) {
$value = $this->submission->get_posted_string( $tag->name );
public function get_values_by_tag( $tag ) {
/**
* Hidden fields.
*/
$hidden_fields = $this->get_hidden_fields();

if ( \in_array( $tag->name, $hidden_fields, true ) ) {
return [];
}

$data = $this->submission->get_posted_data( $tag->name );

if ( null === $data ) {
return [];
}

$data = \wpcf7_array_flatten( $data );

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

$last_value = \end( $values );
$tag_value_last = \end( $tag_values );

if ( \str_starts_with( $value, $last_value . ' ' ) ) {
$value = \substr( $value, \strlen( $last_value . ' ' ) );
$value = \array_pop( $data );

if ( \str_starts_with( $value, $tag_value_last . ' ' ) ) {
$value = \substr( $value, \strlen( $tag_value_last . ' ' ) );
}

$data[] = $value;
}

/**
* Hidden fields.
*/
$hidden_fields = $this->get_hidden_fields();
return $data;
}

if ( \in_array( $tag->name, $hidden_fields, true ) ) {
$value = '';
}
/**
* Get value by tag.
*
* @param WPCF7_FormTag $tag Tag.
* @return string
*/
public function get_value_by_tag( $tag ) {
$values = $this->get_values_by_tag( $tag );

return $value;
return \implode( ', ', $values );
}

/**
Expand Down

0 comments on commit ba1322a

Please sign in to comment.