Skip to content

Commit

Permalink
Encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
renintw committed Dec 9, 2024
1 parent d445d0b commit 0228445
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 58 deletions.
48 changes: 24 additions & 24 deletions public_html/wp-content/plugins/camptix/camptix.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,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( append_currency( get_post_meta( $post_id, 'tix_price', true ), $this->options ) );
echo esc_html( CampTix_Utility::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 @@ -713,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( append_currency( $order_total, $this->options ) );
echo esc_html( CampTix_Utility::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( append_currency( $ticket_price, $this->options ) );
echo esc_html( CampTix_Utility::append_currency( $ticket_price, $this->options ) );
break;
}
}
Expand Down Expand Up @@ -759,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( append_currency( $discount_price, $this->options ) );
echo esc_html( CampTix_Utility::append_currency( $discount_price, $this->options ) );
} elseif ( $discount_percent > 0 ) {
echo esc_html( $discount_percent . '%' );
}
Expand Down Expand Up @@ -1915,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( append_currency( 10000, true, $key ), $this->options ) . ")";
echo " (" . esc_html( CampTix_Utility::append_currency( 10000, true, $key ), $this->options ) . ")";
?></option>
<?php endforeach; ?>
</select>
Expand Down Expand Up @@ -2534,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( append_currency( $results['actual_total'], $this->options ) )
esc_html( CampTix_Utility::append_currency( $results['actual_total'], $this->options ) )
)
);
}
Expand Down Expand Up @@ -2656,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' ) => 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 ),
__( 'Sub-Total', 'wordcamporg' ) => CampTix_Utility::append_currency( $ticket->tix_sold_count * $ticket->tix_price, $this->options ),
__( 'Discounted', 'wordcamporg' ) => CampTix_Utility::append_currency( $ticket->tix_discounted, $this->options ),
__( 'Revenue', 'wordcamporg' ) => CampTix_Utility::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' ) => append_currency( $totals->sub_total, $this->options ),
__( 'Discounted', 'wordcamporg' ) => append_currency( $totals->discounted, $this->options ),
__( 'Revenue', 'wordcamporg' ) => append_currency( $totals->revenue, $this->options ),
__( 'Sub-Total', 'wordcamporg' ) => CampTix_Utility::append_currency( $totals->sub_total, $this->options ),
__( 'Discounted', 'wordcamporg' ) => CampTix_Utility::append_currency( $totals->discounted, $this->options ),
__( 'Revenue', 'wordcamporg' ) => CampTix_Utility::append_currency( $totals->revenue, $this->options ),
);

// Update stats
Expand Down Expand Up @@ -4074,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( append_currency( get_post_meta( get_the_ID(), 'tix_price', true ), $this->options ) ); ?></span><br />
<span><?php echo esc_html( CampTix_Utility::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 @@ -4578,7 +4578,7 @@ function metabox_coupon_options() {
<?php else: ?>
<span>
<?php if ( $discount_price ) : ?>
<?php echo esc_html( append_currency( $discount_price, $this->options ) ); ?>
<?php echo esc_html( CampTix_Utility::append_currency( $discount_price, $this->options ) ); ?>
<?php else : ?>
<?php echo esc_html( $discount_percent ); ?>%
<?php endif; ?>
Expand Down Expand Up @@ -4701,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' ), append_currency( get_post_meta( $post->ID, 'tix_order_total', true ), $this->options ) );
$rows[] = array( __( 'Order Total', 'wordcamporg' ), CampTix_Utility::append_currency( get_post_meta( $post->ID, 'tix_order_total', true ), $this->options ) );

// Reservation
if ( $this->options['reservations_enabled'] ) {
Expand Down Expand Up @@ -5240,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' ), append_currency( $this->coupon->tix_discount_price, $this->options ) );
$ticket->tix_discounted_text = sprintf( __( 'Discounted %s', 'wordcamporg' ), CampTix_Utility::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 @@ -5605,7 +5605,7 @@ function form_start() {
</th>
<td class="tix-column-price">
<?php if ( $price > 0 ) : ?>
<?php echo esc_html( append_currency( $price, $this->options ) ); ?>
<?php echo esc_html( CampTix_Utility::append_currency( $price, $this->options ) ); ?>
<?php else : ?>
<?php _e( 'Free', 'wordcamporg' ); ?>
<?php endif; ?>
Expand Down Expand Up @@ -5638,7 +5638,7 @@ function form_start() {
$discount_percent = (float) $this->coupon->tix_discount_percent;
$discount_text = '0%';
if ( $discount_price > 0 ) {
$discount_text = append_currency( $discount_price, $this->options );
$discount_text = CampTix_Utility::append_currency( $discount_price, $this->options );
} elseif ( $discount_percent > 0 ) {
$discount_text = $discount_percent . '%';
}
Expand Down Expand Up @@ -5809,13 +5809,13 @@ function form_attendee_info() {
</td>
<td class="tix-column-per-ticket">
<?php if ( $price > 0 ) : ?>
<?php echo esc_html( append_currency( $price, $this->options ) ); ?>
<?php echo esc_html( CampTix_Utility::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( append_currency( $price * intval( $count ), $this->options ) ); ?></td>
<td class="tix-column-price"><?php echo esc_html( CampTix_Utility::append_currency( $price * intval( $count ), $this->options ) ); ?></td>
</tr>
<?php endforeach; ?>
<tr class="tix-row-total">
Expand All @@ -5827,7 +5827,7 @@ function form_attendee_info() {
$discount_text = '';

if ( $discount_price > 0 ) {
$discount_text = append_currency( $discount_price, $this->options );
$discount_text = CampTix_Utility::append_currency( $discount_price, $this->options );
} elseif ( $discount_percent > 0 ) {
$discount_text = $discount_percent . '%';
}
Expand All @@ -5848,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( append_currency( $total, $this->options ) ); ?></strong>
<strong><?php echo esc_html( CampTix_Utility::append_currency( $total, $this->options ) ); ?></strong>
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -7794,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, append_currency( $item['price'], $this->options, false ), $item['quantity'], append_currency( $item['price'] * $item['quantity'], $this->options, false ) );
$receipt_content .= sprintf( "* %s (%s) x%d = %s\n", $ticket->post_title, CampTix_Utility::append_currency( $item['price'], $this->options, false ), $item['quantity'], CampTix_Utility::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' ), append_currency( $order['total'], $this->options, false ) );
$receipt_content .= sprintf( "* " . __( 'Total: %s', 'wordcamporg' ), CampTix_Utility::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
73 changes: 39 additions & 34 deletions public_html/wp-content/plugins/camptix/utility.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
<?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 );
class CampTix_Utility {
/**
* 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.
*/
public static 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';
}
}

$currencies = CampTix_Currency::get_currency_list();
$currency = $currencies[ $currency_key ];

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

$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'] );
}

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 );

$formatted_amount = apply_filters( 'tix_append_currency', $formatted_amount, $currency, $amount );
if ( $nbsp ) {
$formatted_amount = str_replace( ' ', '&nbsp;', $formatted_amount );
}

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

return $formatted_amount;
}
}

0 comments on commit 0228445

Please sign in to comment.