Skip to content

Commit

Permalink
Update settings saving to handle multiple ticketing models
Browse files Browse the repository at this point in the history
  • Loading branch information
joedolson committed Dec 27, 2023
1 parent e0906f7 commit e167614
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
37 changes: 18 additions & 19 deletions src/mt-processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,11 @@ function mt_registration_fields( $form, $has_data, $data, $public = 'admin' ) {
*
* @return string
*/
function mt_prices_table( $registration = array() ) {
function mt_prices_table( $registration = array(), $counting = '' ) {
if ( empty( $registration ) ) {
$registration = mt_get_settings( 'defaults' )['continuous'];
}
$counting = $registration['counting_method'];
$counting = ( $counting ) ? $counting : $registration['counting_method'];
$available = '';
$tickets = ( isset( $registration['tickets'] ) ) ? $registration['tickets'] : false;
$label = ( 'event' === $counting ) ? __( 'Event Date & Time', 'my-tickets' ) : __( 'Label', 'my-tickets' );
Expand All @@ -401,31 +401,30 @@ function mt_prices_table( $registration = array() ) {
</tr>
</thead>
<tbody>';
$counting = ( isset( $registration['counting_method'] ) ) ? $registration['counting_method'] : $counting;
if ( 'discrete' === $counting || 'event' === $counting ) {
$available_empty = "<input type='text' name='mt_tickets[]' id='mt_tickets' value='' size='8' />";
$total = '<input type="hidden" name="mt_tickets_total" value="inherit" />';
$available_empty = "<input type='text' name='mt_tickets[$counting][]' id='mt_tickets' value='' size='8' />";
$total = '<input type="hidden" name="mt_tickets_total[' . $counting . ']" value="inherit" />';
} else {
$disabled = ( 'general' === $counting ) ? ' disabled="disabled"' : '';
$notice = ( 'general' === $counting ) ? ' <em id="ticket-counting-status_' . $counting . '">' . __( 'Ticket counting is disabled for general admission events.', 'my-tickets' ) . '</em>' : '';
$value = ( isset( $registration['total'] ) && 'inherit' !== $registration['total'] ) ? $registration['total'] : $tickets;
$available_empty = "<input type='hidden' name='mt_tickets[]' id='mt_tickets_' . $counting . '' value='inherit' />";
$total = "<p class='mt-available-tickets'><label for='mt_tickets_total_' . $counting . ''>" . __( 'Total Tickets Available', 'my-tickets' ) . ':</label> <input ' . $disabled . ' type="text" name="mt_tickets_total" id="mt_tickets_total_' . $counting . '" aria-describedby="ticket-counting-status" value="' . esc_attr( $value ) . '" />' . $notice . '</p>';
$available_empty = "<input type='hidden' name='mt_tickets[$counting][]' id='mt_tickets_" . $counting . "' value='inherit' />";
$total = "<p class='mt-available-tickets'><label for='mt_tickets_total_" . $counting . "'>" . __( 'Total Tickets Available', 'my-tickets' ) . ':</label> <input ' . $disabled . ' type="text" name="mt_tickets_total[' . $counting . ']" id="mt_tickets_total_' . $counting . '" aria-describedby="ticket-counting-status" value="' . esc_attr( $value ) . '" />' . $notice . '</p>';
}
$labels_index = array();
$pricing = ( isset( $registration['prices'] ) ) ? $registration['prices'] : $registration['pricing']; // array of prices; label => cost/available/sold.
if ( is_array( $pricing ) ) {
foreach ( $pricing as $label => $options ) {
if ( 'discrete' === $counting || 'event' === $counting ) {
$available = "<input type='text' name='mt_tickets[]' id='mt_tickets_$counting . '_' . $label' value='" . esc_attr( $options['tickets'] ) . "' size='8' />";
$available = "<input type='text' name='mt_tickets[$counting][]' id='mt_tickets_$counting . '_' . $label' value='" . esc_attr( $options['tickets'] ) . "' size='8' />";
} else {
$available = "<input type='hidden' name='mt_tickets[]' id='mt_tickets_$counting . '_' . $label' value='inherit' />";
$available = "<input type='hidden' name='mt_tickets[$counting][]' id='mt_tickets_$counting . '_' . $label' value='inherit' />";
}
if ( $label ) {
$date = ( '' !== $options['label'] ) ? $options['label'] : gmdate( 'Y-m-d' );
$args = array(
'id' => "mt_label_$label",
'name' => 'mt_label[]',
'name' => 'mt_label[' . $counting . '][]',
'value' => gmdate( 'Y-m-d', strtotime( $date ) ),
);
$label_field = ( 'event' === $counting ) ? '<div class="mt-date-time-picker">' . mt_datepicker_html( $args ) . '<label for="mt_label_picker_time_' . $label . '" class="screen-reader-text">' . __( 'Time', 'my-tickets' ) . '</label><input type="time" name="mt_label_time[]" id="mt_label_picker_time_' . $label . '"></div>' : '';
Expand All @@ -441,11 +440,11 @@ function mt_prices_table( $registration = array() ) {
<button type='button' class='button up'><span class='dashicons dashicons-arrow-up-alt'></span><span class='screen-reader-text'>" . __( 'Move Up', 'my-tickets' ) . "</span></button>
<button type='button' class='button down'><span class='dashicons dashicons-arrow-down-alt'></span><span class='screen-reader-text'>" . __( 'Move Down', 'my-tickets' ) . "</span></button>
</td>
<td>$label_field<input type='$type' class='$label_class' name='mt_label[]' id='mt_label_$counting . '_' . $label' value='" . esc_attr( stripslashes( strip_tags( $options['label'] ) ) ) . "' />$comps</td>
<td><input type='number' name='mt_price[]' step='0.01' id='mt_price_$counting . '_' . $label' value='" . esc_attr( $options['price'] ) . "' size='8' /></td>
<td>$label_field<input type='$type' class='$label_class' name='mt_label[$counting][]' id='mt_label_$counting" . '_' . "$label' value='" . esc_attr( stripslashes( strip_tags( $options['label'] ) ) ) . "' />$comps</td>
<td><input type='number' name='mt_price[$counting][]' step='0.01' id='mt_price_$counting" . '_' . "$label' value='" . esc_attr( $options['price'] ) . "' size='8' /></td>
<td>$available</td>
<td><input type='hidden' name='mt_sold[]' value='" . $sold . "' />" . $sold . '</td>
<td><input type="date" name="mt_close[]" value="' . ( ( $close ) ? gmdate( 'Y-m-d', $close ) : '' ) . '" /></td>
<td><input type='hidden' name='mt_sold[$counting][]' value='" . $sold . "' />" . $sold . '</td>
<td><input type="date" name="mt_close[' . $counting . '][]" value="' . ( ( $close ) ? gmdate( 'Y-m-d', $close ) : '' ) . '" /></td>
</tr>';

$labels_index[ $label ] = $options['label'];
Expand All @@ -465,8 +464,8 @@ function mt_prices_table( $registration = array() ) {
<button type='button' class='button up'><span class='dashicons dashicons-arrow-up-alt'></span><span class='screen-reader-text'>" . __( 'Move Up', 'my-tickets' ) . "</span></button>
<button type='button' class='button down'><span class='dashicons dashicons-arrow-down-alt'></span><span class='screen-reader-text'>" . __( 'Move Down', 'my-tickets' ) . "</span></button>
</td>
<td><input type='text' readonly name='mt_label[]' id='mt_label_$counting . '_' . complimentary' value='Complimentary' /><br />" . __( 'Note: complimentary tickets can only be added by logged-in administrators.', 'my-tickets' ) . "</td>
<td><input type='text' readonly name='mt_price[]' id='mt_price_$counting . '_' . complimentary' value='0' size='8' /></td>
<td><input type='text' readonly name='mt_label[$counting][]' id='mt_label_$counting" . '_' . "complimentary' value='Complimentary' /><br />" . __( 'Note: complimentary tickets can only be added by logged-in administrators.', 'my-tickets' ) . "</td>
<td><input type='text' readonly name='mt_price[$counting][]' id='mt_price_$counting" . '_' . "complimentary' value='0' size='8' /></td>
<td>$available</td>
<td></td>
<td></td>
Expand All @@ -476,11 +475,11 @@ function mt_prices_table( $registration = array() ) {
$return .= "
<tr class='clonedPrice $counting' id='price" . $counting . "1'>
<td></td>
<td>$label_field<input type='text' class='" . $label_class . "' name='mt_label[]' id='mt_$counting . '_' . label' /></td>
<td><input type='text' name='mt_price[]' id='mt_$counting . '_' . price' step='0.01' size='8' /></td>
<td>$label_field<input type='text' class='$label_class' name='mt_label[$counting][]' id='mt_$counting" . '_' . "label' /></td>
<td><input type='text' name='mt_price[$counting][]' id='mt_$counting" . '_' . "price' step='0.01' size='8' /></td>
<td>$available_empty</td>
<td></td>
<td><input type='date' name='mt_close[]' value='' /></td>
<td><input type='date' name='mt_close[$counting][]' value='' /></td>
</tr>";
$return .= '</tbody></table>';
$add_field = __( 'Add a price group', 'my-tickets' );
Expand Down
42 changes: 24 additions & 18 deletions src/mt-ticketing-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,25 @@ function mt_update_ticketing_settings( $post ) {
$mt_hide_remaining_limit = ( isset( $post['mt_hide_remaining_limit'] ) ) ? intval( $post['mt_hide_remaining_limit'] ) : 0;
$mt_collect_shipping = ( isset( $post['mt_collect_shipping'] ) ) ? 'true' : 'false';
$mt_hide_empty_short_cart = ( isset( $post['mt_hide_empty_short_cart'] ) ) ? 'true' : 'false';
$defaults = ( isset( $post['defaults'] ) ) ? $post['defaults'] : array();
$labels = ( isset( $post['mt_label'] ) ) ? $post['mt_label'] : array();
$prices = ( isset( $post['mt_price'] ) ) ? $post['mt_price'] : array();
$close = ( isset( $post['mt_close'] ) ) ? $post['mt_close'] : array();
$availability = ( isset( $post['mt_tickets'] ) ) ? $post['mt_tickets'] : array();
$close_value = ( isset( $post['mt_tickets_close_value'] ) ) ? $post['mt_tickets_close_value'] : '';
$close_type = ( isset( $post['mt_tickets_close_type'] ) ) ? $post['mt_tickets_close_type'] : 'integer';
$mt_display_remaining = ( isset( $post['mt_display_remaining'] ) ) ? $post['mt_display_remaining'] : 'proportion';
$mt_ticket_image = ( isset( $post['mt_ticket_image'] ) ) ? $post['mt_ticket_image'] : 'ticket';
$pricing_array = mt_setup_pricing( $labels, $prices, $availability, $close );
$defaults['pricing'] = $pricing_array;
$defaults['tickets'] = $mt_total_tickets;
$defaults['multiple'] = ( isset( $post['defaults']['multiple'] ) ) ? $post['defaults']['multiple'] : '';

$ticket_models = array( 'continuous', 'discrete', 'event' );
foreach ( $ticket_models as $model ) {
$model_defaults = ( isset( $post['defaults'][ $model ] ) ) ? $post['defaults'][ $model ] : array();
$labels = ( isset( $post['mt_label'][ $model ] ) ) ? $post['mt_label'][ $model ] : array();
$prices = ( isset( $post['mt_price'][ $model ] ) ) ? $post['mt_price'][ $model ] : array();
$close = ( isset( $post['mt_close'][ $model ] ) ) ? $post['mt_close'][ $model ] : array();
$availability = ( isset( $post['mt_tickets'][ $model ] ) ) ? $post['mt_tickets'][ $model ] : array();
$pricing_array = mt_setup_pricing( $labels, $prices, $availability, $close );

$defaults[ $model ] = $model_defaults;
$defaults[ $model ]['pricing'] = $pricing_array;
$defaults[ $model ]['tickets'] = ( is_array( $mt_total_tickets ) ) ? $mt_total_tickets[ $model ] : $mt_total_tickets;
$defaults[ $model ]['multiple'] = ( isset( $post['defaults']['multiple'] ) ) ? $post['defaults']['multiple'] : '';
}

/**
* Filter settings array before saving option.
Expand Down Expand Up @@ -85,7 +91,7 @@ function mt_update_ticketing_settings( $post ) {
$_POST
);
$settings = array_merge( get_option( 'mt_settings', array() ), $settings );
update_option( 'mt_settings', $settings );
// update_option( 'mt_settings', $settings );
/**
* Filter updated settings messages appended to the 'My Tickets Ticketing Defaults saved' message.
*
Expand Down Expand Up @@ -264,27 +270,27 @@ function mt_ticketing_settings() {
<div class="ticket-sale-expiration">
<p>
<label for='reg_expires_<?php $model; ?>'><?php _e( 'Stop online sales <em>x</em> hours before event', 'my-tickets' ); ?></label>
<input type='number' name='defaults[<?php $model; ?>][reg_expires]' id='reg_expires_<?php $model; ?>' value='<?php stripslashes( esc_attr( $displayed['reg_expires'] ) ); ?>'/>
<input type='number' name='defaults[<?php echo $model; ?>][reg_expires]' id='reg_expires_<?php $model; ?>' value='<?php stripslashes( esc_attr( $displayed['reg_expires'] ) ); ?>'/>
</p>
<p>
<label for='multiple_<?php $model; ?>'><?php _e( 'Allow multiple tickets/ticket type per purchaser', 'my-tickets' ); ?></label>
<input type='checkbox' name='defaults[<?php $model; ?>][multiple]' id='multiple_<?php $model; ?>' value='true' <?php echo ( $multiple ) ? ' checked="checked"' : ''; ?> />
<input type='checkbox' name='defaults[<?php echo $model; ?>][multiple]' id='multiple_<?php $model; ?>' value='true' <?php echo ( $multiple ) ? ' checked="checked"' : ''; ?> />
</p>
</div>
<div class="ticket-sale-types">
<fieldset>
<legend><?php _e( 'Type of Sale', 'my-tickets' ); ?></legend>
<p>
<input type='radio' name='defaults[<?php $model; ?>][sales_type]' id='mt_sales_type_tickets_<?php $model; ?>' value='tickets'<?php checked( $is_tickets, true ); ?> />
<label for='mt_sales_type_tickets_<?php $model; ?>'><?php _e( 'Ticket Sales', 'my-tickets' ); ?></label><br/>
<input type='radio' name='defaults[<?php $model; ?>][sales_type]' id='mt_sales_type_registration_<?php $model; ?>' value='registration'<?php checked( $is_registration, true ); ?> />
<label for='mt_sales_type_registration_<?php $model; ?>'><?php _e( 'Event Registration', 'my-tickets' ); ?></label>
<input type='radio' name='defaults[<?php echo $model; ?>][sales_type]' id='mt_sales_type_tickets_<?php $model; ?>' value='tickets'<?php checked( $is_tickets, true ); ?> />
<label for='mt_sales_type_tickets_<?php echo $model; ?>'><?php _e( 'Ticket Sales', 'my-tickets' ); ?></label><br/>
<input type='radio' name='defaults[<?php echo $model; ?>][sales_type]' id='mt_sales_type_registration_<?php $model; ?>' value='registration'<?php checked( $is_registration, true ); ?> />
<label for='mt_sales_type_registration_<?php echo $model; ?>'><?php _e( 'Event Registration', 'my-tickets' ); ?></label>
</p>
</fieldset>
<input type="hidden" name='defaults[<?php $model; ?>][counting_method]' value='<?php echo esc_attr( $method ); ?>' />
<input type="hidden" name='defaults[<?php echo $model; ?>][counting_method]' value='<?php echo esc_attr( $method ); ?>' />
</div>
</div>
<?php echo mt_prices_table( $displayed ); ?>
<?php echo mt_prices_table( $displayed, $model ); ?>
</div>
<?php
}
Expand Down

0 comments on commit e167614

Please sign in to comment.