Skip to content

Commit

Permalink
Extract get_sorted_questions
Browse files Browse the repository at this point in the history
  • Loading branch information
renintw committed Nov 22, 2024
1 parent 8d3758c commit 88f27f1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 53 deletions.
7 changes: 2 additions & 5 deletions public_html/wp-content/plugins/camptix/addons/privacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public function register_personal_data_exporters( $exporters ) {
* @return array
*/
public function attendee_personal_data_exporter( $email_address, $page ) {
/* @var CampTix_Plugin $camptix */
global $camptix;

$page = (int) $page;

$data_to_export = array();
Expand Down Expand Up @@ -127,7 +124,7 @@ public function attendee_personal_data_exporter( $email_address, $page ) {
}
break;
case 'questions':
$questions = $camptix->get_sorted_questions( $post->tix_ticket_id );
$questions = CampTix_Utility::get_sorted_questions( $post->tix_ticket_id );
$answers = $post->tix_questions;

foreach ( $questions as $question ) {
Expand Down Expand Up @@ -327,7 +324,7 @@ public function attendee_personal_data_eraser( $email_address, $page ) {
update_post_meta( $post->ID, $key, $anonymized_value );
break;
case 'questions':
$questions = $camptix->get_sorted_questions( $post->tix_ticket_id );
$questions = CampTix_Utility::get_sorted_questions( $post->tix_ticket_id );
$answers = $post->tix_questions;

$anonymized_answers = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,13 @@ protected function registering_multiple_attendees( $tickets_selected ) {
* @return bool
*/
protected function tickets_have_questions( $tickets_selected ) {
/** @var $camptix CampTix_Plugin */
global $camptix;
$has_questions = false;

foreach ( $tickets_selected as $ticket_id => $number_attendees_current_ticket ) {
$number_attendees_current_ticket = absint( $number_attendees_current_ticket );

if ( $number_attendees_current_ticket > 0 ) {
$questions = $camptix->get_sorted_questions( $ticket_id );
$questions = CampTix_Utility::get_sorted_questions( $ticket_id );

if ( count( $questions ) >= 1 ) {
$has_questions = true;
Expand Down
50 changes: 5 additions & 45 deletions public_html/wp-content/plugins/camptix/camptix.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,46 +936,6 @@ function get_all_questions() {
return $questions;
}

/**
* Takes a ticket id and returns a sorted array of questions.
*
* @param int $ticket_id
*
* @return array
*/
function get_sorted_questions( $ticket_id ) {
$question_ids = (array) get_post_meta( $ticket_id, 'tix_question_id' );
$order = (array) get_post_meta( $ticket_id, 'tix_questions_order', true );

// Make sure we have at least some questions
if ( empty( $question_ids ) )
return array();

$questions = get_posts( array(
'post_type' => 'tix_question',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in' => $question_ids,
) );

$questions_with_keys = array();

foreach ( $questions as $question )
$questions_with_keys[ $question->ID ] = $question;

$questions = $questions_with_keys;
unset( $questions_with_keys );

$questions_sorted = array();
foreach ( $order as $question_id )
if ( isset( $questions[ $question_id ] ) )
$questions_sorted[] = $questions[ $question_id ];

unset( $questions );

return $questions_sorted;
}

/**
* Fired during init, registers our new post types. $supports depends
* on $this->debug, which if true, renders things like custom fields.
Expand Down Expand Up @@ -4400,7 +4360,7 @@ function metabox_ticket_questions() {
</div>
</div>
<?php
$questions = $this->get_sorted_questions( get_the_ID() );
$questions = CampTix_Utility::get_sorted_questions( get_the_ID() );
$i = 0;
?>
</div>
Expand Down Expand Up @@ -4715,7 +4675,7 @@ function metabox_attendee_info() {

// Questions
$rows[] = array( __( 'Questions', 'wordcamporg' ), '' );
$questions = $this->get_sorted_questions( $ticket_id );
$questions = CampTix_Utility::get_sorted_questions( $ticket_id );
$answers = get_post_meta( $post->ID, 'tix_questions', true );

foreach ( $questions as $question ) {
Expand Down Expand Up @@ -5862,7 +5822,7 @@ function form_attendee_info() {

<?php
$ticket = $this->tickets[$ticket_id];
$questions = $this->get_sorted_questions( $ticket->ID );
$questions = CampTix_Utility::get_sorted_questions( $ticket->ID );
$this->form_data['tix_attendee_info'][ $i ]['ticket_id'] = intval( $ticket->ID );
?>
<input type="hidden" name="tix_attendee_info[<?php echo esc_attr( $i ); ?>][ticket_id]" value="<?php echo intval( $ticket->ID ); ?>" />
Expand Down Expand Up @@ -6231,7 +6191,7 @@ function form_edit_attendee() {
$this->notice( __( 'Please note that the payment for this ticket is still pending.', 'wordcamporg' ) );

$ticket = get_post( $ticket_id );
$questions = $this->get_sorted_questions( $ticket->ID );
$questions = CampTix_Utility::get_sorted_questions( $ticket->ID );
$answers = (array) get_post_meta( $attendee->ID, 'tix_questions', true );

$ticket_info = array(
Expand Down Expand Up @@ -7162,7 +7122,7 @@ function form_checkout() {

$answers = array();
if ( isset( $_POST['tix_attendee_questions'][ $i ] ) ) {
$questions = $this->get_sorted_questions( $ticket->ID );
$questions = CampTix_Utility::get_sorted_questions( $ticket->ID );

foreach ( $questions as $question ) {
if ( isset( $_POST['tix_attendee_questions'][ $i ][ $question->ID ] ) ) {
Expand Down
40 changes: 40 additions & 0 deletions public_html/wp-content/plugins/camptix/utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,44 @@ public static function append_currency( $amount, $options, $nbsp = true, $curren

return $formatted_amount;
}

/**
* Takes a ticket id and returns a sorted array of questions.
*
* @param int $ticket_id
*
* @return array
*/
public static function get_sorted_questions( $ticket_id ) {
$question_ids = (array) get_post_meta( $ticket_id, 'tix_question_id' );
$order = (array) get_post_meta( $ticket_id, 'tix_questions_order', true );

// Make sure we have at least some questions
if ( empty( $question_ids ) )
return array();

$questions = get_posts( array(
'post_type' => 'tix_question',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in' => $question_ids,
) );

$questions_with_keys = array();

foreach ( $questions as $question )
$questions_with_keys[ $question->ID ] = $question;

$questions = $questions_with_keys;
unset( $questions_with_keys );

$questions_sorted = array();
foreach ( $order as $question_id )
if ( isset( $questions[ $question_id ] ) )
$questions_sorted[] = $questions[ $question_id ];

unset( $questions );

return $questions_sorted;
}
}

0 comments on commit 88f27f1

Please sign in to comment.