Skip to content

Commit

Permalink
Extract append_currency
Browse files Browse the repository at this point in the history
  • Loading branch information
renintw committed Dec 9, 2024
1 parent 86f6983 commit d445d0b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 66 deletions.
91 changes: 25 additions & 66 deletions public_html/wp-content/plugins/camptix/camptix.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function __construct() {
do_action( 'camptix_pre_init' );

require_once( plugin_dir_path( __FILE__ ) . 'inc/class-camptix-currencies.php' );
require_once( plugin_dir_path( __FILE__ ) . 'utility.php' );

require( dirname( __FILE__ ) . '/inc/class-camptix-addon.php' );
require( dirname( __FILE__ ) . '/inc/class-camptix-payment-method.php' );
Expand Down Expand Up @@ -610,7 +611,7 @@ function manage_columns_ticket_filter( $columns ) {
function manage_columns_ticket_action( $column, $post_id ) {
switch ( $column ) {
case 'tix_price':
echo esc_html( $this->append_currency( get_post_meta( $post_id, 'tix_price', true ) ) );
echo esc_html( append_currency( get_post_meta( $post_id, 'tix_price', true ), $this->options ) );
break;
case 'tix_quantity':
echo intval( get_post_meta( $post_id, 'tix_quantity', true ) );
Expand Down Expand Up @@ -712,11 +713,11 @@ function manage_columns_attendee_action( $column, $post_id ) {
break;
case 'tix_order_total':
$order_total = (float) get_post_meta( $post_id, 'tix_order_total', true );
echo esc_html( $this->append_currency( $order_total ) );
echo esc_html( append_currency( $order_total, $this->options ) );
break;
case 'tix_ticket_price':
$ticket_price = (float) get_post_meta( $post_id, 'tix_ticket_price', true );
echo esc_html( $this->append_currency( $ticket_price ) );
echo esc_html( append_currency( $ticket_price, $this->options ) );
break;
}
}
Expand Down Expand Up @@ -758,7 +759,7 @@ function manage_columns_coupon_action( $column, $post_id ) {
$discount_price = (float) get_post_meta( $post_id, 'tix_discount_price', true );
$discount_percent = (int) get_post_meta( $post_id, 'tix_discount_percent', true );
if ( $discount_price > 0 ) {
echo esc_html( $this->append_currency( $discount_price ) );
echo esc_html( append_currency( $discount_price, $this->options ) );
} elseif ( $discount_percent > 0 ) {
echo esc_html( $discount_percent . '%' );
}
Expand Down Expand Up @@ -1914,7 +1915,7 @@ function field_currency( $args ) {
<?php foreach ( $currencies as $key => $currency ) : ?>
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $args['value'] ); ?>><?php
echo esc_html( $currency['label'] );
echo " (" . esc_html( $this->append_currency( 10000, true, $key ) ) . ")";
echo " (" . esc_html( append_currency( 10000, true, $key ), $this->options ) . ")";
?></option>
<?php endforeach; ?>
</select>
Expand Down Expand Up @@ -1955,48 +1956,6 @@ function sort_currencies( $a, $b ) {
return $a['label'] > $b['label'] ? 1 : -1;
}

/**
* Give me a price and I'll format it according to the set currency for
* display. Don't send my output anywhere but the screen, because I will
* print &nbsp; and other things.
*/
function append_currency( $amount, $nbsp = true, $currency_key = false ) {
$amount = floatval( $amount );

$currencies = CampTix_Currency::get_currency_list();

if ( ! $currency_key ) {
if ( isset( $this->options['currency'] ) ) {
$currency_key = $this->options['currency'];
} else {
$currency_key = 'USD';
}
}

$currency = $currencies[ $currency_key ];

if ( ! isset( $currency['decimal_point'] ) ) {
$currency['decimal_point'] = 2;
}

if ( isset( $currency['locale'] ) ) {
$formatter = new NumberFormatter( $currency['locale'], NumberFormatter::CURRENCY );
$formatted_amount = $formatter->format( $amount );
} elseif ( isset( $currency['format'] ) && $currency['format'] ) {
$formatted_amount = sprintf( $currency['format'], number_format( $amount, $currency['decimal_point'] ) );
} else {
$formatted_amount = $currency_key . ' ' . number_format( $amount, $currency['decimal_point'] );
}

$formatted_amount = apply_filters( 'tix_append_currency', $formatted_amount, $currency, $amount );

if ( $nbsp ) {
$formatted_amount = str_replace( ' ', '&nbsp;', $formatted_amount );
}

return $formatted_amount;
}

/*
* Formats a string containing a first and/or last name, based on the specified name ordering scheme
* @param string $name_string A string containing placeholders for the given and surnames. e.g., "Hello %first% %last%"
Expand Down Expand Up @@ -2575,7 +2534,7 @@ function menu_tools_revenue() {
'<div class="updated settings-error below-h2"><p>%s</p></div>',
sprintf(
__( '<strong>Woah!</strong> The revenue total does not match with the transactions total. The actual total is: <strong>%s</strong>. Something somewhere has gone wrong, please report this.', 'wordcamporg' ),
esc_html( $this->append_currency( $results['actual_total'] ) )
esc_html( append_currency( $results['actual_total'], $this->options ) )
)
);
}
Expand Down Expand Up @@ -2697,18 +2656,18 @@ function generate_revenue_report_data() {
__( 'Ticket type', 'wordcamporg' ) => esc_html( $ticket->post_title ),
__( 'Sold', 'wordcamporg' ) => $ticket->tix_sold_count,
__( 'Remaining', 'wordcamporg' ) => $ticket->tix_remaining,
__( 'Sub-Total', 'wordcamporg' ) => $this->append_currency( $ticket->tix_sold_count * $ticket->tix_price ),
__( 'Discounted', 'wordcamporg' ) => $this->append_currency( $ticket->tix_discounted ),
__( 'Revenue', 'wordcamporg' ) => $this->append_currency( $ticket->tix_sold_count * $ticket->tix_price - $ticket->tix_discounted ),
__( 'Sub-Total', 'wordcamporg' ) => append_currency( $ticket->tix_sold_count * $ticket->tix_price, $this->options ),
__( 'Discounted', 'wordcamporg' ) => append_currency( $ticket->tix_discounted, $this->options ),
__( 'Revenue', 'wordcamporg' ) => append_currency( $ticket->tix_sold_count * $ticket->tix_price - $ticket->tix_discounted, $this->options ),
);
}
$rows[] = array(
__( 'Ticket type', 'wordcamporg' ) => 'Total',
__( 'Sold', 'wordcamporg' ) => $totals->sold,
__( 'Remaining', 'wordcamporg' ) => $totals->remaining,
__( 'Sub-Total', 'wordcamporg' ) => $this->append_currency( $totals->sub_total ),
__( 'Discounted', 'wordcamporg' ) => $this->append_currency( $totals->discounted ),
__( 'Revenue', 'wordcamporg' ) => $this->append_currency( $totals->revenue ),
__( 'Sub-Total', 'wordcamporg' ) => append_currency( $totals->sub_total, $this->options ),
__( 'Discounted', 'wordcamporg' ) => append_currency( $totals->discounted, $this->options ),
__( 'Revenue', 'wordcamporg' ) => append_currency( $totals->revenue, $this->options ),
);

// Update stats
Expand Down Expand Up @@ -4115,7 +4074,7 @@ function metabox_ticket_options() {
<?php if ( $purchased <= 0 ) : ?>
<input type="text" name="tix_price" class="small-text" value="<?php echo esc_attr( number_format( (float) get_post_meta( get_the_ID(), 'tix_price', true ), $decimal_point, '.', '' ) ); ?>" autocomplete="off" /> <?php echo esc_html( $this->options['currency'] ); ?>
<?php else: ?>
<span><?php echo esc_html( $this->append_currency( get_post_meta( get_the_ID(), 'tix_price', true ) ) ); ?></span><br />
<span><?php echo esc_html( append_currency( get_post_meta( get_the_ID(), 'tix_price', true ), $this->options ) ); ?></span><br />
<p class="description" style="margin-top: 10px;"><?php _e( 'You can not change the price because one or more tickets have already been purchased.', 'wordcamporg' ); ?></p>
<?php endif; ?>
</div>
Expand Down Expand Up @@ -4619,7 +4578,7 @@ function metabox_coupon_options() {
<?php else: ?>
<span>
<?php if ( $discount_price ) : ?>
<?php echo esc_html( $this->append_currency( $discount_price ) ); ?>
<?php echo esc_html( append_currency( $discount_price, $this->options ) ); ?>
<?php else : ?>
<?php echo esc_html( $discount_percent ); ?>%
<?php endif; ?>
Expand Down Expand Up @@ -4742,7 +4701,7 @@ function metabox_attendee_info() {
$rows[] = array( __( 'Coupon', 'wordcamporg' ), sprintf( '<a href="%s">%s</a>', get_edit_post_link( $coupon->ID ), $coupon->post_title ) );
}

$rows[] = array( __( 'Order Total', 'wordcamporg' ), $this->append_currency( get_post_meta( $post->ID, 'tix_order_total', true ) ) );
$rows[] = array( __( 'Order Total', 'wordcamporg' ), append_currency( get_post_meta( $post->ID, 'tix_order_total', true ), $this->options ) );

// Reservation
if ( $this->options['reservations_enabled'] ) {
Expand Down Expand Up @@ -5281,7 +5240,7 @@ function template_redirect() {

if ( $this->coupon->tix_discount_price > 0 ) {
$ticket->tix_discounted_price = number_format( $ticket->tix_price - $this->coupon->tix_discount_price, 2, '.', '' );
$ticket->tix_discounted_text = sprintf( __( 'Discounted %s', 'wordcamporg' ), $this->append_currency( $this->coupon->tix_discount_price ) );
$ticket->tix_discounted_text = sprintf( __( 'Discounted %s', 'wordcamporg' ), append_currency( $this->coupon->tix_discount_price, $this->options ) );
} elseif ( $this->coupon->tix_discount_percent > 0 ) {
$ticket->tix_discounted_price = number_format( $ticket->tix_price - ( $ticket->tix_price * $this->coupon->tix_discount_percent / 100 ), 2, '.', '' );
$ticket->tix_discounted_text = sprintf( __( 'Discounted %s%%', 'wordcamporg' ), $this->coupon->tix_discount_percent );
Expand Down Expand Up @@ -5646,7 +5605,7 @@ function form_start() {
</th>
<td class="tix-column-price">
<?php if ( $price > 0 ) : ?>
<?php echo esc_html( $this->append_currency( $price ) ); ?>
<?php echo esc_html( append_currency( $price, $this->options ) ); ?>
<?php else : ?>
<?php _e( 'Free', 'wordcamporg' ); ?>
<?php endif; ?>
Expand Down Expand Up @@ -5679,7 +5638,7 @@ function form_start() {
$discount_percent = (float) $this->coupon->tix_discount_percent;
$discount_text = '0%';
if ( $discount_price > 0 ) {
$discount_text = $this->append_currency( $discount_price );
$discount_text = append_currency( $discount_price, $this->options );
} elseif ( $discount_percent > 0 ) {
$discount_text = $discount_percent . '%';
}
Expand Down Expand Up @@ -5850,13 +5809,13 @@ function form_attendee_info() {
</td>
<td class="tix-column-per-ticket">
<?php if ( $price > 0 ) : ?>
<?php echo esc_html( $this->append_currency( $price ) ); ?>
<?php echo esc_html( append_currency( $price, $this->options ) ); ?>
<?php else : ?>
<?php _e( 'Free', 'wordcamporg' ); ?>
<?php endif; ?>
</td>
<td class="tix-column-quantity"><?php echo intval( $count ); ?></td>
<td class="tix-column-price"><?php echo esc_html( $this->append_currency( $price * intval( $count ) ) ); ?></td>
<td class="tix-column-price"><?php echo esc_html( append_currency( $price * intval( $count ), $this->options ) ); ?></td>
</tr>
<?php endforeach; ?>
<tr class="tix-row-total">
Expand All @@ -5868,7 +5827,7 @@ function form_attendee_info() {
$discount_text = '';

if ( $discount_price > 0 ) {
$discount_text = $this->append_currency( $discount_price );
$discount_text = append_currency( $discount_price, $this->options );
} elseif ( $discount_percent > 0 ) {
$discount_text = $discount_percent . '%';
}
Expand All @@ -5889,7 +5848,7 @@ function form_attendee_info() {
</td>
<td>
<span class="screen-reader-text"><?php esc_html_e( 'Total:', 'wordcamporg' ); ?></span>
<strong><?php echo esc_html( $this->append_currency( $total ) ); ?></strong>
<strong><?php echo esc_html( append_currency( $total, $this->options ) ); ?></strong>
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -7835,13 +7794,13 @@ function email_tickets( $payment_token = false, $from_status = 'draft', $to_stat
$receipt_content = '';
foreach ( $order['items'] as $item ) {
$ticket = get_post( $item['id'] );
$receipt_content .= sprintf( "* %s (%s) x%d = %s\n", $ticket->post_title, $this->append_currency( $item['price'], false ), $item['quantity'], $this->append_currency( $item['price'] * $item['quantity'], false ) );
$receipt_content .= sprintf( "* %s (%s) x%d = %s\n", $ticket->post_title, append_currency( $item['price'], $this->options, false ), $item['quantity'], append_currency( $item['price'] * $item['quantity'], $this->options, false ) );
}

if ( isset( $order['coupon'] ) && $order['coupon'] )
$receipt_content .= sprintf( '* ' . __( 'Coupon used: %s', 'wordcamporg' ) . "\n", $order['coupon'] );

$receipt_content .= sprintf( "* " . __( 'Total: %s', 'wordcamporg' ), $this->append_currency( $order['total'], false ) );
$receipt_content .= sprintf( "* " . __( 'Total: %s', 'wordcamporg' ), append_currency( $order['total'], $this->options, false ) );
$signature = apply_filters( 'camptix_ticket_email_signature', __( 'Let us know if you have any questions!', 'wordcamporg' ) );

// Set the tmp receipt for shortcodes use.
Expand Down
47 changes: 47 additions & 0 deletions public_html/wp-content/plugins/camptix/utility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Append currency to an amount.
*
* @param float $amount The amount to append currency to.
* @param array $options The options array.
* @param bool $nbsp Whether to replace spaces with non-breaking spaces.
* @param string $currency_key The currency key to use.
* @return string The amount with currency appended.
*/
function append_currency( $amount, $options, $nbsp = true, $currency_key = false ) {
$amount = floatval( $amount );

$currencies = CampTix_Currency::get_currency_list();

if ( ! $currency_key ) {
if ( isset( $options['currency'] ) ) {
$currency_key = $options['currency'];
} else {
$currency_key = 'USD';
}
}

$currency = $currencies[ $currency_key ];

if ( ! isset( $currency['decimal_point'] ) ) {
$currency['decimal_point'] = 2;
}

if ( isset( $currency['locale'] ) ) {
$formatter = new NumberFormatter( $currency['locale'], NumberFormatter::CURRENCY );
$formatted_amount = $formatter->format( $amount );
} elseif ( isset( $currency['format'] ) && $currency['format'] ) {
$formatted_amount = sprintf( $currency['format'], number_format( $amount, $currency['decimal_point'] ) );
} else {
$formatted_amount = $currency_key . ' ' . number_format( $amount, $currency['decimal_point'] );
}

$formatted_amount = apply_filters( 'tix_append_currency', $formatted_amount, $currency, $amount );

if ( $nbsp ) {
$formatted_amount = str_replace( ' ', '&nbsp;', $formatted_amount );
}

return $formatted_amount;
}

0 comments on commit d445d0b

Please sign in to comment.