<?php

if( ! defined( 'ABSPATH' ) ) {
    return;
}

/**
 * Add Metabox to display user information
 * @return void
 */
function giveasap_add_metabox_for_users() {
    add_meta_box( 'giveasap_users', __( 'Users', 'giveasap' ), 'giveasap_metabox_users', array( 'giveasap' ), 'side', 'high' );
    add_meta_box( 'giveasap_ideas', __( 'Have an Idea?', 'giveasap' ), 'giveasap_metabox_ideas', array( 'giveasap' ), 'side', 'low' );
}
add_action( 'add_meta_boxes', 'giveasap_add_metabox_for_users' );

 

/**
 * Rendering users and user actions in metabox
 * @param  object $post Post object
 * @return void       
 */
function giveasap_metabox_users( $post ){
    $post_id = $post->ID;
    $registered_users = giveasap_get_entries_for( $post_id );
    $count_users = 0;
    if( $registered_users && is_array( $registered_users ) ){
        $count_users = count( $registered_users );
    }
    ?>
    <p>
        <?php _e( 'Registered users:', 'giveasap' ); ?> <strong><?php echo $count_users; ?></strong>
        <?php if( $count_users > 0 ) { ?>
             <a href="<?php echo admin_url( 'admin.php?page=giveasap-users&id=' . $post->ID ); ?>"  class="button  button-small right"><?php _e( 'List Users', 'giveasap' ); ?></a>
       
        <?php } ?>
    </p>
    <?php
    do_action( 'sg_admin_metabox_users', $post );
    
    $winners = giveasap_get_winners( $post_id );
    $all_winners_notified = true;
    if( $winners ) {
        $prizes_settings = get_post_meta( $post_id, 'giveasap_prize', true );
        $prizes          = $prizes_settings && isset( $prizes_settings['prizes'] ) ? $prizes_settings['prizes'] : array();

        ?>
        <div class="giveasap_winners">
            <strong><?php _e( 'Winners', 'giveasap' ); ?></strong>
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th><?php _e( 'Email', 'giveasap' ); ?></th>
                        <th style="width: 60px;"><?php _e( 'Notified', 'giveasap' ); ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php
                    foreach ( $winners as $index => $winner ) {
                        if( 'no' === $winner['emailed'] ) {
                            $all_winners_notified = false;
                        }

                        $subscriber_id = isset( $winner['id'] ) ? absint( $winner['id'] ) : 0;
                        if ( ! $subscriber_id ) {
                            $user = giveasap_get_user_by_email( $winner['email'], $post_id );
                            $subscriber_id = $user->id;
                        }
                        ?>
                        <tr>
                            <td>
                                <a href="<?php echo admin_url( 'edit.php?post_type=giveasap&page=giveasap-users&user=' . $subscriber_id . '&action=view'); ?>"><?php echo esc_html( $winner['email'] ); ?></a>
                                <?php
                                    if ( ! empty( $winner['prizes'] ) ) {
                                        ?>
                                        <strong><?php esc_html_e( 'Prizes won:', 'giveasap' ); ?></strong>
                                        <ul style="margin:0;padding:0;list-style-type: none;">
                                            <?php
                                            foreach ( $winner['prizes'] as $winner_prize ) {
                                                ?>
                                                <li><em><?php echo esc_html( $winner_prize['title'] ); ?></em></li>
                                                <?php
                                            }
                                            ?>
                                        </ul>
                                        <?php
                                    }
                                ?>
                            </td>
                            <td><?php echo strtoupper( $winner['emailed'] ); ?></td>
                        </tr>
                        <tr class="hidden sg-select-prizes-manual">
                            <td colspan="2">
                                <strong><?php echo esc_html( $winner['email'] ); ?></strong><br/>
                                <select multiple class="widefat" name="sg_manual_prizes[<?php echo esc_attr( $index ); ?>][]">
                                    <?php
                                    foreach ( $prizes as $prize_index => $prize ) {
                                        $selected = false;
                                        if ( isset( $winner['prizes'] ) && $winner['prizes'] ) {
                                            foreach ( $winner['prizes'] as $winner_prize ) {
                                                if ( sanitize_title( $winner_prize['title'] ) === sanitize_title( $prize['title'] ) ) {
                                                    $selected = true;
                                                    break;
                                                }
                                            }
                                        }
                                        ?>
                                        <option <?php selected( $selected, true, true ); ?> value="<?php echo esc_attr( $prize_index ); ?>"><?php echo esc_html( $prize['title'] ); ?></option>
                                        <?php
                                    }
                                    ?>
                                </select>
                            </td>
                        </tr>
                        <?php
                    }
                ?>
                </tbody>
            </table>
            <input type="checkbox" class="sg-hidden" id="sg_awarding_prizes_manually" name="sg_awarding_prizes_manually" value="1" style="display:none !important;"/>
            <button type="button" class="button button-link" data-target=".sg-select-prizes-manual" data-class="hidden" id="sgToggleAwardPrizes"><?php esc_html_e( 'Award Prizes Manually', 'giveasap' ); ?></button>
            <button type="submit" name="giveasap_export_winners" id="export_winners" class="button"><?php _e( 'Export Winner(s) CSV', 'giveasap' ); ?></button>
        </div>
        <script>

        </script>
        <br/>
        <?php
    } 

    if( 'giveasap_ended' == $post->post_status && $count_users > 0 ) {
        ?>
        <button type="submit" name="giveasap_select" value="<?php echo esc_attr( $post_id ); ?>" id="notify" class="button button-primary button-large"><?php _e( 'Select Winner(s)', 'giveasap' ); ?></button>
        <?php
    }

    if( 'giveasap_winners' == $post->post_status && $count_users > 0 ) {
        ?>
        <p><button type="submit" name="giveasap_select" value="<?php echo esc_attr( $post_id ); ?>" id="sg-reselect-winners" class="button button-primary button-large"><?php _e( 'Reselect Winner(s)', 'giveasap' ); ?></button></p>
        <button type="submit" name="giveasap_notify" id="notify" class="button button-primary button-large"><?php _e( 'Notify Winner(s)', 'giveasap' ); ?></button>
        <?php
    }

    if( 'giveasap_notified' == $post->post_status && ! $all_winners_notified ) {
        ?>
        <button type="submit" name="giveasap_notify" id="notify" class="button button-primary button-large"><?php _e( 'Notify Winner(s)', 'giveasap' ); ?></button>
        <?php
    }
    ?>

    <?php if( $count_users > 0 ) { ?>
        <button type="submit" name="giveasap_export" id="export" class="button  button-large"><?php _e( 'Export CSV', 'giveasap' ); ?></button>
    <?php 
    }   

    ?>
    <hr/>
    <p><strong><?php _e( 'Have you checked out our integrations section? You can enhance your experience on new giveaways!', 'giveasap' ); ?></strong></p>
    <a target="_blank" href="<?php echo admin_url('edit.php?post_type=giveasap&page=giveasap_integrations'); ?>" class="button button-default"><?php _e( 'Go to Integrations', 'giveasap' ); ?></a>
    <?php
}

add_action( 'admin_init', 'giveasap_admin_end_giveaway', 99 );

/**
 * End Giveaway on Request
 *
 * @since  2.6.2 
 *
 * @return void 
 */
function giveasap_admin_end_giveaway() {
    if( isset( $_REQUEST['giveasap_end'] ) ) {
 
        $post_id = isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : absint( $_REQUEST['giveasap_end'] );

        if( ! $post_id ) { $post_id = 0; }

        if( $post_id == 0 ) {
            return false;
        }
        if( isset( $_GET['action'] ) ) {
            unset( $_GET['action'] );
        }
        if( isset( $_GET['message'] ) ) {
            unset( $_GET['message'] );
        }
        
        $set_status = giveasap_set_status( $post_id, 'giveasap', 'giveasap_ended' );
             
        wp_redirect( admin_url( 'post.php?post=' . $post_id .'&action=edit&message=20' ), '301' );
        exit();
        
    }
}

add_action( 'admin_init', 'giveasap_metabox_users_select_winner', 99 );

/**
 * Select winners
 * @return void 
 */
function giveasap_metabox_users_select_winner(){
    
    if( isset( $_REQUEST['giveasap_select'] ) ) {
 
        $post_id = isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : absint( $_REQUEST['giveasap_select'] );

        if( ! $post_id ) { $post_id = 0; }

        if( $post_id == 0 ) {
            return false;
        }
        if( isset( $_GET['action'] ) ) {
            unset( $_GET['action'] );
        }
        if( isset( $_GET['message'] ) ) {
            unset( $_GET['message'] );
        }
        
        $selected = giveasap_select_winner( $post_id );

        if( $selected ) {

            $set_status = giveasap_set_status( $post_id, 'giveasap', 'giveasap_winners', 'giveasap_ended' );
             
            wp_redirect( admin_url( 'post.php?post=' . $post_id .'&action=edit&message=21' ), '301' );
            exit();
        }
        
    }
}

add_action( 'admin_init', 'giveasap_metabox_users_notify_winners', 99 );
function giveasap_metabox_users_notify_winners(){
    if( isset( $_REQUEST['giveasap_notify'] ) ) {
       
        $post_id = isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : absint( $_REQUEST['giveasap_notify'] );

        if( ! $post_id ) { $post_id = 0; }

        if( $post_id == 0 ) {
            return false;
        }
        if( isset( $_GET['action'] ) ) {
            unset( $_GET['action'] );
        }
        if( isset( $_GET['message'] ) ) {
            unset( $_GET['message'] );
        }

        $winners = giveasap_get_winners( $post_id );

        giveasap_notify_winners( $winners, $post_id );

        giveasap_set_status( $post_id, 'giveasap', 'giveasap_notified', 'giveasap_winners' );
        wp_redirect( admin_url( 'post.php?post=' . $post_id .'&action=edit&message=22' ), '301' );
        exit();
    }
}

add_action( 'admin_init', 'giveasap_metabox_users_remind', 99 );
function giveasap_metabox_users_remind(){
	if( isset( $_REQUEST['giveasap_remind'] ) ) {

		$post_id = isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : absint( $_REQUEST['giveasap_remind'] );

		if( ! $post_id ) { $post_id = 0; }

		if( $post_id == 0 ) {
			return false;
		}
		if( isset( $_GET['action'] ) ) {
			unset( $_GET['action'] );
		}
		if( isset( $_GET['message'] ) ) {
			unset( $_GET['message'] );
		}

		sg_remind_subscribers($post_id);

		wp_redirect( admin_url( 'post.php?post=' . $post_id .'&action=edit&message=24' ), '301' );
		exit();
	}
}



add_action( 'admin_init', 'giveasap_metabox_users_export', 99 );
function giveasap_metabox_users_export(){
    if( isset( $_REQUEST['giveasap_export'] ) ) {
        $post_id = isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : absint( $_REQUEST['giveasap_export'] );

        if( ! $post_id ) { $post_id = 0; }
        
        if( $post_id == 0 ) {
            return false;
        }

        if( isset( $_GET['action'] ) ) {
            unset( $_GET['action'] );
        }
        if( isset( $_GET['message'] ) ) {
            unset( $_GET['message'] );
        }

        $users = giveasap_get_entries_for( $post_id );

        if( is_array( $users ) && count( $users ) > 0 ) {
            $post = get_post( $post_id );
            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Disposition: attachment; filename=giveasap_' . $post->post_name . '_users.csv');

            // create a file pointer connected to the output stream
            $output   = fopen('php://output', 'w');
            $fields   = sg_get_form_fields( $post_id );
            $headers  = array( __( 'Email Address', 'giveasap' ) );
            foreach ( $fields as $field_slug => $field ) {
	            if ( 'user_email' === $field_slug ) {
		            continue;
	            }
                if ( ! $field['label'] ) { continue; }
                $headers[] = $field['label'];
            }
	        $headers[] = __( 'Date', 'giveasap' );

            if ( sg_is_ip_enabled() ) {
	            $headers[] = __( 'IP Address', 'giveasap' );
            }
            // output the column headings
            fputcsv( $output, $headers );
            foreach ( $users as $user ) {
                if( $user->email == '' ) {
                    continue;
                }

	            $meta = giveasap_get_meta( $user->id );
                $row  = array( $user->email );

	            foreach ( $fields as $field_slug => $field ) {
		            if ( ! $field['label'] ) {
			            continue;
		            }

		            if ( 'user_email' === $field_slug ) {
		                continue;
                    }
		            $headers[] = $field['label'];

		            if ( 'user_name' === $field_slug ) {
			            $value = isset( $meta[ '_' . $field_slug ] ) ? $meta[ '_' . $field_slug ] : '';

			            if ( ! $value ) {
				            $value = isset( $meta[ '_name' ] ) ? $meta[ '_name' ] : '';
			            }
		            } else {
			            $value = isset( $meta[ '_' . $field_slug ] ) ? $meta[ '_' . $field_slug ] : '';
                    }

		            if ( $value && is_array( $value ) ) {
			            $field_value = implode( ', ', $value );
		            } else {
			            $field_value = $value;
		            }

		            $row[] = $field_value;
	            }

	            $row[] = $user->date;

	            if ( sg_is_ip_enabled() ) {
		            $ip = isset( $meta['_ip'] ) ? $meta['_ip'] : '';
		            $row[] = is_array( $ip ) ? implode(', ', $ip ) : $ip;
	            }
                fputcsv( $output,  $row );
            }
            exit();
        }
    }
}


add_action( 'admin_init', 'giveasap_metabox_users_export_winners', 99 );
function giveasap_metabox_users_export_winners(){
	if( isset( $_REQUEST['giveasap_export_winners'] ) ) {
		$post_id = isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : absint( $_REQUEST['giveasap_export_winners'] );

		if( ! $post_id ) { $post_id = 0; }

		if( $post_id == 0 ) {
			return false;
		}

		if( isset( $_GET['action'] ) ) {
			unset( $_GET['action'] );
		}
		if( isset( $_GET['message'] ) ) {
			unset( $_GET['message'] );
		}

		$users = giveasap_get_winners( $post_id );

		if( is_array( $users ) && count( $users ) > 0 ) {
			$post = get_post( $post_id );
			header('Content-Type: text/csv; charset=utf-8');
			header('Content-Disposition: attachment; filename=giveasap_' . $post->post_name . '_winners.csv');

			// create a file pointer connected to the output stream
			$output   = fopen('php://output', 'w');
			$fields   = sg_get_form_fields( $post_id );
			$headers  = array( __( 'Email Address', 'giveasap' ) );
			foreach ( $fields as $field_slug => $field ) {
				if ( 'user_email' === $field_slug ) {
					continue;
				}
				if ( ! $field['label'] ) { continue; }
				$headers[] = $field['label'];
			}

			// output the column headings
			fputcsv( $output, $headers );
			foreach ( $users as $winner ) {
				$subscriber_id = isset( $winner['id'] ) ? absint( $winner['id'] ) : 0;
				$user          = giveasap_get_user_by_email( $winner['email'], $post_id );

				if( $user->email == '' ) {
					continue;
				}

				$meta = giveasap_get_meta( $user->id );
				$row  = array( $user->email );

				foreach ( $fields as $field_slug => $field ) {
					if ( ! $field['label'] ) {
						continue;
					}

					if ( 'user_email' === $field_slug ) {
						continue;
					}
					$headers[] = $field['label'];

					if ( 'user_name' === $field_slug ) {
						$value = isset( $meta[ '_' . $field_slug ] ) ? $meta[ '_' . $field_slug ] : '';

						if ( ! $value ) {
							$value = isset( $meta[ '_name' ] ) ? $meta[ '_name' ] : '';
						}
					} else {
						$value = isset( $meta[ '_' . $field_slug ] ) ? $meta[ '_' . $field_slug ] : '';
					}

					if ( $value && is_array( $value ) ) {
						$field_value = implode( ', ', $value );
					} else {
						$field_value = $value;
					}

					$row[] = $field_value;
				}
				fputcsv( $output,  $row );
			}
			exit();
		}
	}
}

add_action( 'admin_init', 'giveasap_export_entries', 99 );
function giveasap_export_entries(){
    global $wpdb;

	if( isset( $_REQUEST['sg_export_entries'] ) ) {
		$payload = json_decode( wp_unslash( $_REQUEST['sg_export_entries'] ), true );

		if ( ! $payload ) {
		    return false;
        }

		if( isset( $_GET['action'] ) ) {
			unset( $_GET['action'] );
		}
		if( isset( $_GET['message'] ) ) {
			unset( $_GET['message'] );
		}

		$sql  = 'SELECT sg_entries.*, subscribers.email, giveaways.post_title as giveaway_title FROM ' . $wpdb->giveasap_actions . ' as sg_entries';
		$sql .= ' LEFT JOIN ' . $wpdb->giveasap_entries . ' as subscribers on subscribers.id=sg_entries.subscriber_id';
		$sql .= ' LEFT JOIN ' . $wpdb->posts . ' as giveaways on giveaways.ID=sg_entries.giveaway_id';

		$sql .= ' WHERE 1=1';

		if ( ! empty( $payload['subscriber'] ) && $payload['subscriber'] ) {
			$sql .= $wpdb->prepare( ' AND sg_entries.subscriber_id = %d', $payload['subscriber'] );
		}

		if ( ! empty( $payload['giveaway'] ) && $payload['giveaway'] ) {
			$sql .= $wpdb->prepare( ' AND sg_entries.giveaway_id = %d', $payload['giveaway'] );
		}

		if ( ! empty( $payload['action'] ) && $payload['action'] ) {
			$sql .= $wpdb->prepare( ' AND sg_entries.action = %s', $payload['action'] );
		}

		$result = $wpdb->get_results( $sql, 'ARRAY_A' );

		if ( ! $result ) {
		    return;
        }

		if( is_array( $result ) && count( $result ) > 0 ) {
			header('Content-Type: text/csv; charset=utf-8');
			header('Content-Disposition: attachment; filename=giveasap_entries.csv');

			// create a file pointer connected to the output stream
			$output   = fopen('php://output', 'w');
			$headers  = array(
                __( 'Email Address', 'giveasap' ),
				__( 'Type', 'giveasap' ),
				__( 'Title', 'giveasap' ),
				__( 'Value', 'giveasap' ),
				__( 'Entries', 'giveasap' ),
				__( 'Status', 'giveasap' ),
				__( 'Giveaway', 'giveasap' ),
            );

			// output the column headings
			fputcsv( $output, $headers );
			foreach ( $result as $entry ) {
			    $row = array();
			    $row[] = $entry['email'];
				$row[] = $entry['action'];

				$data = maybe_unserialize( $entry['data'] );

				if ( isset( $data['title'] ) ) {
					$row[] = $data['title'];
				} else {
				    $row[] = '-';
                }

				$action_value = '-';
				$registered_actions = sg_get_registered_actions();
				if ( isset( $registered_actions[ $entry['action'] ] ) && class_exists( $registered_actions[ $entry['action'] ] ) ) {
					$action        = $registered_actions[ $entry['action'] ];
					$action_object = new $action();
					$action_object->prepare_value_for_entries_table( $entry );
					// Value
					$action_value = $action_object->get_value_for_entries_table( maybe_unserialize( $entry['data'] ) );
				}

				// Value
				$row[] = $action_value;

				$row[] = $entry['entries'];
				$row[] = $entry['status'];
				$row[] = $entry['giveaway_title'] ? $entry['giveaway_title'] : get_the_title( $entry['giveaway_id'] );

				fputcsv( $output,  $row );
			}
			exit();
		}
	}
}

/**
 * Display The Giveaway Idea metabox
 * @param  WP_Post $post 
 * @return void       
 */
function giveasap_metabox_ideas( $post ) {
    ?>
    <p>If you have an idea on a feature you would like to see, feel free to write that idea down on our site.</p>
    <a href="http://www.wpsimplegiveaways.com/ideas" target="_blank" class="button button-secondary"><strong>Share your Idea</strong></a>
    <?php
}

add_action( 'admin_init', 'sg_process_action_clone' );

/**
 * Processing the Clone Giveaway action.
 *
 * @since 2.17.0
 */
function sg_process_action_clone() {
    global $wpdb;
    $giveaway_id = isset( $_GET['sg_clone_giveaway'] ) ? absint( $_GET['sg_clone_giveaway'] ) : 0;

    if ( ! $giveaway_id ) {
        return;
    }

    $giveaway = sg_get_giveaway( $giveaway_id );
    $post     = (array) $giveaway->get_post();

    if ( ! $post ) {
        return;
    }

    $new_giveaway = array(
        'post_title'   => $post['post_title'],
        'post_content' => $post['post_content'],
        'post_type'    => 'giveasap',
        'post_status'  => 'draft'
    );
    $new_id = wp_insert_post( wp_slash( $new_giveaway ) );

    if ( ! $new_id || is_wp_error( $new_id ) ) {
        return;
    }

    $post_meta = get_post_meta( $giveaway_id );

    $ignored_meta = apply_filters(
            'sg_clone_giveaway_ignored_meta',
            array(
                'giveasap_winners',
                'end_time',
                '_edit_last',
                '_edit_lock',
            )
    );

    foreach ( $post_meta as $meta_key => $meta_value ) {
        if ( in_array( $meta_key, $ignored_meta, true ) ) {
            continue;
        }
        foreach ( $meta_value as $value ) {
	        add_post_meta( $new_id, $meta_key, maybe_unserialize( $value ) );
        }
    }

    if ( sg_is_cloning_subscribers_enabled() ) {
	    $ignored_sub_meta = apply_filters(
		    'sg_clone_subscriber_ignored_meta',
		    array(
			    'winner'
		    )
	    );

	    $entries = giveasap_get_active_entries_for( $giveaway_id );
	    foreach ( $entries as $entry ) {
		    $subscriber_id = $wpdb->insert(
			    $wpdb->giveasap_entries,
			    array(
				    'post_id' => $new_id,
				    'email'   => $entry->email,
				    'entries' => 1,
				    'ref_id'  => $ref_id = md5( time() . $entry->email ),
				    'date'    => current_time( 'mysql' ),
				    'status'  => 'active',
			    ),
			    array(
				    '%d',
				    '%s',
				    '%d',
				    '%s',
				    '%s',
				    '%s',
			    )
		    );

		    $entry_meta = giveasap_get_meta( $entry->id );

		    foreach ( $entry_meta as $meta_key => $meta_value ) {
			    if ( in_array( $meta_key, $ignored_sub_meta, true ) ) {
				    continue;
			    }
			    foreach ( $meta_value as $value ) {
				    giveasap_add_meta( $subscriber_id, $meta_key, maybe_unserialize( $value ) );
			    }
		    }
	    }
    }

	wp_redirect( admin_url( 'edit.php?post_type=giveasap&message=23' ), '301' );
	exit();
}

add_action( 'save_post_giveasap', 'sg_save_manually_awarded_prizes', 100, 2 );

/**
 * Save Manually Awarded Prizes
 *
 * @param $post_id
 * @param $post
 */
function sg_save_manually_awarded_prizes( $post_id, $post ) {
    if ( ! isset( $_POST['sg_awarding_prizes_manually'] ) ) {
        return;
    }

    $awarded_prizes = isset( $_POST['sg_manual_prizes'] ) ? $_POST['sg_manual_prizes'] : false;

    if ( ! $awarded_prizes ) {
        return;
    }

	$prizes_settings = get_post_meta( $post_id, 'giveasap_prize', true );
	$prizes          = $prizes_settings && isset( $prizes_settings['prizes'] ) ? $prizes_settings['prizes'] : array();

	if ( ! $prizes ) {
	    return;
    }

	$winners = giveasap_get_winners( $post_id );

    if ( ! $winners ) {
        return;
    }

    foreach ( $winners as $winner_index => $winner ) {
        $winner_index = absint( $winner_index );
        $winner_awarded_prizes = isset( $awarded_prizes[ $winner_index ] ) ? $awarded_prizes[ $winner_index ] : false;
        if ( ! $winner_awarded_prizes ) {
            continue;
        }

	    $winner['prizes'] = array();

        foreach ( $winner_awarded_prizes as$awarded_prize_index ) {
	        $awarded_prize_index = absint( $awarded_prize_index );
	        $prize = isset( $prizes[ $awarded_prize_index ] ) ? $prizes[ $awarded_prize_index ] : false;
	        if ( ! $prize ) {
	            continue;
            }

	        $winner['prizes'][] = $prize;
        }

        $winners[ $winner_index ] = $winner;
    }

    giveasap_update_winners( $winners, $post_id );
}