<?php

/**
 * Admin Screens
 */

use Simple_Giveaways\GA_Users_Table;
use Simple_Giveaways\SG_Subscriber;
use Simple_Giveaways\SG_Subscriber_Entries_Table;

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

add_filter( 'post_updated_messages', 'giveasap_updated_messages' );
add_action( 'current_screen', 'giveasap_cleanup_edit_screen_tabs' );

/**
 * Remove non-functional screen/help tabs from giveaway edit screen.
 *
 * @param WP_Screen $screen Current admin screen.
 *
 * @return void
 */
function giveasap_cleanup_edit_screen_tabs( $screen ) {
	if ( ! ( $screen instanceof WP_Screen ) ) {
		return;
	}

	if ( 'post' !== $screen->base || 'giveasap' !== $screen->post_type ) {
		return;
	}

	$screen->remove_help_tabs();
	add_filter( 'screen_options_show_screen', '__return_false' );
}

/**
 * Adding customized messages
 *
 * @since 2.12.0
 *
 * @param array $messages
 *
 * @return array
 */
function giveasap_updated_messages( $messages ) {

	$messages['giveasap'] = array(
		0  => '', // Unused. Messages start at index 1.
		1  => $messages['post'][1],
		2  => $messages['post'][2],
		3  => $messages['post'][3],
		4  => $messages['post'][4],
		5  => $messages['post'][5],
		6  => $messages['post'][6],
		7  => $messages['post'][7],
		8  => $messages['post'][8],
		9  => $messages['post'][9],
		10 => $messages['post'][10],
		20 => __( 'Giveaway Ended.', 'giveasap' ),
		21 => __( 'Winners Selected. Check the right hand side "Users" box.', 'giveasap' ),
		22 => __( 'Winners notified. Check if all the winners were notified in the "Users" box.', 'giveasap' ),
		23 => __( 'Giveaway Cloned', 'giveasap' ),
		24 => __( 'Subscribers Reminded', 'giveasap' ),
	);

	return $messages;
}

function giveasap_is_current_admin_page() {
    if ( ! is_admin() ) {
        return false;
    }

    if ( isset( $_GET['post_type'] ) && 'giveasap' === sanitize_text_field( $_GET['post_type'] ) ) {
        return true;
    }

    return false;
}

add_action( 'admin_notices', 'giveasap_about_notice' );

function giveasap_about_notice() {
    if ( ! giveasap_is_current_admin_page() ) {
        return;
    }

	if ( ! get_option( 'sg_about_page_was_read', false ) ) {
		?>
		<div class="update-nag sg-update-nag notice notice-success updated">
			<p><?php printf( __( 'Simple Giveaways was updated to version %1$s. Please read about the changes in the <a href="%2$s">About page</a>.', 'giveasap' ), gasap()->version(), esc_url( admin_url( 'edit.php?post_type=giveasap&page=giveasap_about_page' ) ) ); ?></p>
		</div>
		<?php
	}
}

add_action( 'admin_init', 'giveasap_user_notify_by_email_from_user_list' );

function giveasap_user_notify_by_email_from_user_list() {
	if (
		isset( $_GET['resend_nonce'] )
		&& isset( $_GET['post_type'] )
		&& 'giveasap' === $_GET['post_type']
		&& isset( $_GET['page'] )
		&& 'giveasap-users' === $_GET['page']
		&& isset( $_GET['action'] )
		&& 'resend' === $_GET['action']
	) {
		$user = isset( $_GET['user'] ) ? absint( $_GET['user'] ) : 0;

		if ( ! $user ) {
			return;
		}

		if ( ! wp_verify_nonce( \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['resend_nonce'] ), 'giveasap_resend_email' ) ) {
			wp_die();
		}

		$email = giveasap_send_registered_email( $user );

		if ( $email->is_sent() ) {
			$subscriber    = $email->get_subscriber();
			$redirect_link = admin_url( 'edit.php?post_type=giveasap&user_notified=' . $subscriber->email );

			if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] ) {
				$redirect_link = add_query_arg( 'page', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['page'] ), $redirect_link );
			}

			if ( isset( $_REQUEST['paged'] ) && $_REQUEST['paged'] ) {
				$redirect_link = add_query_arg( 'paged', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['paged'] ), $redirect_link );
			}

			if ( isset( $_REQUEST['id'] ) && absint( $_REQUEST['id'] ) ) {
				$redirect_link = add_query_arg( 'id', absint( $_REQUEST['id'] ), $redirect_link );
			}

			wp_safe_redirect( $redirect_link );
			exit();
		}
	}
}

add_action( 'admin_init', 'giveasap_user_block_on_admin_request' );
function giveasap_user_block_on_admin_request() {
	if (
		isset( $_GET['post_type'] )
		&& 'giveasap' === $_GET['post_type']
		&& isset( $_GET['page'] )
		&& 'giveasap-users' === $_GET['page']
		&& isset( $_GET['action'] )
		&& 'block' === $_GET['action']
	) {

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$user = isset( $_GET['user'] ) ? absint( $_GET['user'] ) : 0;

		if ( ! $user ) {
			return;
		}

		$blocked = giveasap_block_subscriber( $user );

		if ( $blocked ) {
			$subscriber    = new SG_Subscriber( $user );
			$redirect_link = admin_url( 'edit.php?post_type=giveasap&user_blocked=' . $subscriber->get_email() );

			if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] ) {
				$redirect_link = add_query_arg( 'page', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['page'] ), $redirect_link );
			}

			if ( isset( $_REQUEST['paged'] ) && $_REQUEST['paged'] ) {
				$redirect_link = add_query_arg( 'paged', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['paged'] ), $redirect_link );
			}

			if ( isset( $_REQUEST['id'] ) && absint( $_REQUEST['id'] ) ) {
				$redirect_link = add_query_arg( 'id', absint( $_REQUEST['id'] ), $redirect_link );
			}

			wp_redirect( $redirect_link );
			exit();
		}
	}
}

add_action( 'admin_init', 'giveasap_user_unblock_on_admin_request' );
function giveasap_user_unblock_on_admin_request() {
	if (
		isset( $_GET['post_type'] )
		&& 'giveasap' === $_GET['post_type']
		&& isset( $_GET['page'] )
		&& 'giveasap-users' === $_GET['page']
		&& isset( $_GET['action'] )
		&& 'unblock' === $_GET['action']
	) {

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$user = isset( $_GET['user'] ) ? absint( $_GET['user'] ) : 0;

		if ( ! $user ) {
			return;
		}

		$blocked = giveasap_unblock_subscriber( $user );

		if ( $blocked ) {
			$subscriber    = new SG_Subscriber( $user );
			$redirect_link = admin_url( 'edit.php?post_type=giveasap&user_unblocked=' . $subscriber->get_email() );

			if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] ) {
				$redirect_link = add_query_arg( 'page', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['page'] ), $redirect_link );
			}

			if ( isset( $_REQUEST['paged'] ) && $_REQUEST['paged'] ) {
				$redirect_link = add_query_arg( 'paged', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['paged'] ), $redirect_link );
			}

			if ( isset( $_REQUEST['id'] ) && absint( $_REQUEST['id'] ) ) {
				$redirect_link = add_query_arg( 'id', absint( $_REQUEST['id'] ), $redirect_link );
			}

			wp_redirect( $redirect_link );
			exit();
		}
	}
}


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

	if (
		isset( $_GET['post_type'] )
		&& 'giveasap' === $_GET['post_type']
		&& isset( $_GET['page'] )
		&& 'giveasap-subscriber-entries' === $_GET['page']
		&& isset( $_GET['subscriber'] )
		&& isset( $_GET['action_id'] )
		&& isset( $_GET['action'] )
	) {

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$subscriber_id = isset( $_GET['subscriber'] ) ? absint( $_GET['subscriber'] ) : 0;

		if ( ! $subscriber_id ) {
			return;
		}

		$action_id = isset( $_GET['action_id'] ) ? absint( $_GET['action_id'] ) : 0;

		if ( ! $action_id ) {
			return;
		}

		$action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : '';

		if ( ! $action ) {
			return;
		}

		switch ( $action ) {
			case 'delete_action_entry':
				$action_data = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->giveasap_actions . ' WHERE id=%d', $action_id ), ARRAY_A );
				$delete      = $wpdb->delete(
					$wpdb->giveasap_actions,
					array(
						'id' => $action_id,
					)
				);

				if ( false !== $delete ) {
					$entries    = intval( $action_data['entries'] );
					$subscriber = giveasap_get_subscriber( $subscriber_id );
					$subscriber->remove_entries( $entries );
					$redirect_link = admin_url( 'edit.php?post_type=giveasap' );

					if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] ) {
						$redirect_link = add_query_arg( 'page', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['page'] ), $redirect_link );
					}

					if ( isset( $_REQUEST['paged'] ) && $_REQUEST['paged'] ) {
						$redirect_link = add_query_arg( 'paged', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['paged'] ), $redirect_link );
					}

					if ( isset( $_REQUEST['subscriber'] ) && absint( $_REQUEST['subscriber'] ) ) {
						$redirect_link = add_query_arg( 'subscriber', absint( $_REQUEST['subscriber'] ), $redirect_link );
					}

					wp_redirect( $redirect_link );
					exit();
				}
				break;
			case 'complete_action_entry':
				$action_data = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->giveasap_actions . ' WHERE id=%d', $action_id ), ARRAY_A );
				$update      = $wpdb->update( $wpdb->giveasap_actions, array( 'status' => 'completed' ), array( 'id' => $action_id ) );
				if ( false !== $update ) {
					$entries    = intval( $action_data['entries'] );
					$subscriber = giveasap_get_subscriber( $subscriber_id );
					$subscriber->add_entries( $entries );

					$redirect_link = admin_url( 'edit.php?post_type=giveasap' );

					if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] ) {
						$redirect_link = add_query_arg( 'page', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['page'] ), $redirect_link );
					}

					if ( isset( $_REQUEST['paged'] ) && $_REQUEST['paged'] ) {
						$redirect_link = add_query_arg( 'paged', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['paged'] ), $redirect_link );
					}

					if ( isset( $_REQUEST['subscriber'] ) && absint( $_REQUEST['subscriber'] ) ) {
						$redirect_link = add_query_arg( 'subscriber', absint( $_REQUEST['subscriber'] ), $redirect_link );
					}

					wp_safe_redirect( $redirect_link );
					exit();
				}
				break;
			case 'invalid_action_entry':
				$action_data = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->giveasap_actions . ' WHERE id=%d', $action_id ), ARRAY_A );
				$update      = $wpdb->update( $wpdb->giveasap_actions, array( 'status' => 'invalid' ), array( 'id' => $action_id ) );
				if ( false !== $update ) {
					$entries    = intval( $action_data['entries'] );
					$subscriber = giveasap_get_subscriber( $subscriber_id );
					$subscriber->remove_entries( $entries );

					$redirect_link = admin_url( 'edit.php?post_type=giveasap' );

					if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] ) {
						$redirect_link = add_query_arg( 'page', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['page'] ), $redirect_link );
					}

					if ( isset( $_REQUEST['paged'] ) && $_REQUEST['paged'] ) {
						$redirect_link = add_query_arg( 'paged', \Simple_Giveaways\Helpers::unslash_and_clean( $_REQUEST['paged'] ), $redirect_link );
					}

					if ( isset( $_REQUEST['subscriber'] ) && absint( $_REQUEST['subscriber'] ) ) {
						$redirect_link = add_query_arg( 'subscriber', absint( $_REQUEST['subscriber'] ), $redirect_link );
					}

					wp_redirect( $redirect_link );
					exit();
				}
				break;
		}
	}
}

/**
 * Showing the list of all users
 *
 * @return void
 */
function giveasap_user_list_screen() {

	if ( isset( $_GET['action'] ) && 'view' === $_GET['action'] ) {
		giveasap_admin_show_subscriber( isset( $_GET['user'] ) ? absint( $_GET['user'] ) : 0 );
	} elseif ( isset( $_GET['action'] ) && 'sg_add_subscriber' === $_GET['action'] ) {
		giveasap_admin_add_subscriber();
	} else {

		$ga_id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : false;

		$users_list = new GA_Users_Table();

		?>
		<div class="wrap">
			<h1 class="wp-heading-inline">
				<?php echo sprintf( __( 'Giveaway Subscribers: <strong>%s</strong>', 'giveasap' ), get_the_title( $ga_id ) ); ?>
				<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=giveasap&page=giveasap-users&action=sg_add_subscriber' ) ); ?>" class="page-title-action"><?php esc_html_e( 'Add Subscriber', 'giveasap' ); ?></a>
				<?php
				if ( $ga_id ) {
					?>
					<a href="<?php echo esc_url( admin_url( 'post.php?post=' . $ga_id . '&action=edit' ) ); ?>"
					   class="button button-default"><?php esc_html_e( 'Go back to the Giveaway', 'giveasap' ); ?></a>
				<?php } ?>
			</h1>
			<form method="post">
				<?php
				$users_list->prepare_items();
				$users_list->display();
				?>
			</form>
		</div>
		<?php
	}
}

/**
 * Showing the list of all entries for a subscriber
 *
 * @return void
 */
function giveasap_subscriber_entries_screen() {

	$subscriber_id = isset( $_GET['subscriber'] ) ? absint( $_GET['subscriber'] ) : false;
	$giveaway_id   = isset( $_REQUEST['giveaway'] ) ? absint( $_REQUEST['giveaway'] ) : 0;
	$subscriber    = null;

	if ( $subscriber_id ) {
		$subscriber = giveasap_get_subscriber( $subscriber_id );
		$subscriber->get_data();
	}

	$table = new SG_Subscriber_Entries_Table();

	?>
	<div class="wrap sg-subscriber-entries-table">
		<h1 class="wp-heading-inline">
			<?php echo esc_html__( 'Entries', 'giveasap' ); ?>
			<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=giveasap&page=giveasap-users' ) ); ?>" class="page-title-action"><?php esc_html_e( 'Back to Subscribers', 'giveasap' ); ?></a>
		</h1>
		<?php

		if ( $giveaway_id ) {
			?>
				<h2><?php esc_html_e( 'Giveaway:', 'giveasap' ); ?> <strong><?php echo esc_html( get_the_title( $giveaway_id ) ); ?></strong></strong></h2>
			<?php
		}

		if ( $subscriber ) {
			?>
			<p><?php esc_html_e( 'Subscriber:', 'giveasap' ); ?> <strong><?php echo esc_html( $subscriber->get_email() ); ?></strong> | <?php esc_html_e( 'Entries:', 'giveasap' ); ?> <strong><?php echo esc_html( $subscriber->get_entries() ); ?></strong></p>
			<?php } ?>
			<form method="post">
				<?php
				$table->prepare_items();
				$table->display();
				?>
			</form>
				</div>
	<?php

}

/**
 * Displaying the form to add the subscriber.
 *
 * @since 2.20.0
 */
function giveasap_admin_add_subscriber() {
	$giveaways = giveasap_get_giveaways();
	$errors    = array();
	$success   = array();
	if ( isset( $_POST['sg_add_subscriber_submit'] ) ) {
		$form = isset( $_POST['sg_form'] ) ? \Simple_Giveaways\Helpers::unslash_and_clean( $_POST['sg_form'] ) : false;

		if ( false !== $form ) {
			$giveaway_id = isset( $form['giveaway'] ) ? absint( $form['giveaway'] ) : 0;
			$email       = isset( $form['user_email'] ) ? $form['user_email'] : '';
			if ( $giveaway_id && $email ) {
				$giveaway = giveasap_get_giveaway( $giveaway_id );

				if ( $giveaway->is_open_to_subscribe() ) {
					$new_subscriber = $giveaway->add_subscriber( $email );
					if ( is_wp_error( $new_subscriber ) ) {
						$errors[] = $new_subscriber->get_error_message();
					} else {
						$success[] = __( 'Subscriber added.', 'giveasap' );
					}
				}
			} else {
				if ( ! $giveaway_id ) {
					$errors[] = __( 'Please, select a giveaway.', 'giveasap' );
				}

				if ( ! $email ) {
					$errors[] = __( 'Please, enter an email.', 'giveasap' );
				}
			}
		}
	}
	?>
	<div class="wrap">
		<h1 class="wp-heading-inline">
			<?php echo esc_html__( 'New Giveaway Subscriber', 'giveasap' ); ?>
			<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=giveasap&page=giveasap-users' ) ); ?>" class="page-title-action"><?php esc_html_e( 'Back to Subscribers', 'giveasap' ); ?></a>
		</h1>
		<?php
		if ( $errors ) {
			foreach ( $errors as $error ) {
				echo '<div class="notice error"><p>' . esc_html( $error ) . '</p></div>';
			}
		}

		if ( $success ) {
			foreach ( $success as $info ) {
				echo '<div class="notice updated"><p>' . esc_html( $info ) . '</p></div>';
			}
		}
		?>
		<?php
		if ( $giveaways ) {
			?>
				<br/>
				<div class="postbox">
					<form class="sg-admin-form" method="post" action="<?php echo esc_url( admin_url( 'edit.php?post_type=giveasap&page=giveasap-users&action=sg_add_subscriber' ) ); ?>">
						<div class="sg-form-field">

							<label for="sg_giveaway_add_subscriber"><?php esc_html_e( 'Giveaway', 'giveasap' ); ?></label>

							<select id="sg_giveaway_add_subscriber" name="sg_form[giveaway]">
								<option value="0"><?php esc_html_e( 'Select a Giveaway', 'giveasap' ); ?></option>
							<?php
							foreach ( $giveaways as $giveaway ) {
								echo '<option value="' . esc_attr( $giveaway->ID ) . '">' . esc_html( $giveaway->post_title ) . '</option>';
							}
							?>
							</select>

						</div>
						<div id="sgGiveawayFields" class="form-table">
							<?php
							$fields = giveasap_get_form_fields( 0 );

							if ( $fields ) {
								foreach ( $fields as $field ) {
									giveasap_render_form_field( $field );
								}
							}
							?>
						</div>
						<?php do_action( 'sg_add_subscriber_form_bottom' ); ?>
						<input type="submit" class="button button-primary" name="sg_add_subscriber_submit" value="<?php esc_attr_e( 'Add Subscriber', 'giveasap' ); ?>">
					</form>
				</div>
				<?php
		} else {
			echo '<p class="sg-notice">' . esc_html__( 'There are no active giveaways to add a subscriber to. Please create a new Giveaway.', 'giveasap' );
		}
		?>
	</div>
	<?php
}

/**
 * Show subscriber
 *
 * @param $entry_id
 */
function giveasap_admin_show_subscriber( $entry_id ) {

	?>
	<div class="wrap">
		<h2><?php esc_html_e( 'Giveaway Subscriber', 'giveasap' ); ?></h2>
		<?php
		if ( ! $entry_id ) {
			echo '<p class="description">' . esc_html( 'Please select a subscriber', 'giveasap' ) . '</p>';
		} else {
			include 'views/subscriber/view.php';
		}
		?>
	</div>
	<?php
}

/**
 * Showing Integrations
 *
 * @return void
 */
function giveasap_integrations() {

	$integrations        = giveasap_get_integrations();
	$active_integrations = get_option( 'giveasap_active_integrations', array() );
	$integration         = isset( $_GET['integration'] ) ? \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['integration'] ) : false;
	$integration_object  = false;

	if ( $integration && isset( $integrations[ $integration ] ) ) {
		$class              = $integrations[ $integration ];
		$integration_object = new $class();
		$integration_object->save_if_submit();
		$integration_object->init_settings();
	}
	?>
    <div class="giveasap-settings-header">
        <img src="<?php echo esc_url( trailingslashit( GASAP_URI ) . 'assets/images/logo-color.svg'); ?>" />
		<?php esc_html_e( 'Simple Giveaways', 'giveasap' ); ?>
    </div>
    <div class="giveasap-settings-subheader">
        <h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
        <p><?php esc_html_e( 'All Simple Giveaways Integrations. Choose which to use and activate them.', 'giveasap' ); ?></p>

    </div>
	<div class="wrap">
		<style>
			.plugin-icon {
				height: auto;
				top: 0;
				bottom: 0;
				margin: auto;
			}
		</style>

		<?php
		$category   = '';
		$categories = [];
		if ( ! $integration_object ) {
			$categories = giveasap_get_integration_categories();
			$category   = isset( $_GET['category'] ) ? $_GET['category'] : '';
			?>
				<div class="plugin-install-php">
					<div class="wp-filter giveasap-integration-filters">
						<ul class="filter-links">
							<li class="plugin-install-featured">
								<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=giveasap&page=giveasap_integrations' ) ); ?>" <?php echo '' === $category ? 'class="current" aria-current="page"' : ''; ?>>All</a>
							</li>
							<?php
							foreach ( $categories as $category_slug => $category_title ) {
								?>
									<li class="giveasap-integration-<?php echo esc_attr( $category_slug ); ?>">
										<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=giveasap&page=giveasap_integrations&category=' . $category_slug ) ); ?>" <?php echo $category_slug === $category ? 'class="current" aria-current="page"' : ''; ?>>
										<?php echo esc_html( $category_title ); ?>
										</a>
									</li>
									<?php
							}
							?>
						</ul>
					</div>
				</div>
				<?php
		}
		?>
		<div class="wp-list-table widefat plugin-install">
			<div id="the-list">
				<?php
				if ( $integration_object ) {

					echo '<h2>' . esc_html( $integration_object->title ) . '</h2>';
					echo '<a href="' . esc_url_raw( admin_url( 'edit.php?post_type=giveasap&page=giveasap_integrations' ) ) . '">' . __( 'Back to Integrations', 'giveasap' ) . '</a>';
					if ( isset( $_GET['section'] ) && 'documentation' === $_GET['section'] && $integration_object->has_documentation ) {
						$integration_object->documentation();
					} else {
						?>
						<form id="<?php echo esc_attr( $integration_object->settings_id ); ?>" method="POST" action="">
                            <?php wp_nonce_field( 'save_integration', 'integration_nonce' ); ?>
							<table class="form-table">
								<?php
								$integration_object->render_fields();
								?>
							</table>
							<button type="submit" name="<?php echo esc_attr( $integration_object->settings_id ); ?>_save"
									class="button button-primary">
								<?php esc_html_e( 'Save', 'textdomain' ); ?>
							</button>
						</form>
						<?php
					}
				} elseif ( $integrations ) {
					foreach ( $integrations as $slug => $class ) {
						$integration_object = new $class();

						if ( $category && ! $integration_object->is_in_category( $category ) ) {
							continue;
						}

						$integration_categories = $integration_object->get_categories();

						?>
						<div class="plugin-card plugin-card-<?php echo esc_attr( $slug ); ?> sg-plugin-card">
							<div class="plugin-card-top">
								<div class="name column-name">
									<h3>
										<?php echo esc_html( $integration_object->title ); ?>
										<img src="<?php echo esc_url( $integration_object->image ); ?>" class="plugin-icon" alt="">

									</h3>
								</div>
								<div class="desc column-description">
									<p><?php echo wp_kses_post( $integration_object->desc ); ?></p>
								</div>
								<div class="integration-categories">
									<?php
									foreach ( $integration_categories as $integration_category ) {
										$integration_category_title = isset( $categories[ $integration_category ] ) ? $categories[ $integration_category ] : $integration_category;
										?>
										<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=giveasap&page=giveasap_integrations&category=' . $integration_category ) ); ?>">
											<?php echo esc_html( $integration_category_title ); ?>
										</a>
										<?php
									}
									?>
								</div>
							</div>
							<div class="plugin-card-bottom">

								<?php $integration_object->buttons( $active_integrations ); ?>

							</div>
						</div>
						<?php
					}
				}
				?>
			</div>
		</div>
	</div>
	<?php
}

/**
 * About Page for showing all features
 */
function giveasap_about_page() {
	update_option( 'sg_about_page_was_read', true );
	$display_version = gasap()->version();

	?>
	<script>
		(function ($) {
			$('.sg-update-nag').hide();
		})(jQuery);
	</script>
	<div class="wrap about-wrap full-width-layout sg-about-page">
		<h1><?php printf( __( 'Simple Giveaways %s', 'giveasap' ), $display_version ); ?></h1>

		<p class="about-text"><?php esc_html_e( 'Thank you for updating to the latest version! You can check new features in this page.', 'giveasap' ); ?></p>
		<p class="about-text"><a target="_blank" href="http://www.wpsimplegiveaways.com/category/changelog/"><?php esc_html_e( 'Read about the latest versions here.', 'giveasap' ); ?></a></p>

	   <?php
		/*
		<ul>
			<li><em>Minor Changes:</em></li>
			<li>
				<strong>2.49.0</strong>
				<ul>
					<li>New: Brevo Integration.</li>
					<li>New: Webhooks Integration.</li>
					<li>Update: Moved textdomain loading to init for better compatibility.</li>
					<li>Update: Improved Code Editor initialization to refresh correctly.</li>
				</ul>
			</li>
			<li>
				<strong>2.28.2</strong>
				<ul>
					<li>Fix: Redirect link was also used in emails. Now it only uses the correct one (shortcode or landing page).</li>
					<li>Fix: Share Link could include reference parameter which might login another user.</li>
					<li>Fix: Share link attribute for sharing could be encoded incorrectly.</li>
					<li>Update: MailChimp Logo.</li>
				</ul>
			</li>
			<li>
				<strong>2.28.1</strong>
				<ul>
					<li>Fix (Premium): Multiple Prizes test was included.</li>
				</ul>
			</li>

		</ul>*/
		?>

		<div class="floating-header-section">
			<div class="section-header">
				<h2><?php esc_html_e( 'New Features', 'giveasap' ); ?><em
							style="font-size:0.5em;line-height: 1;display:block;color:#555d66;font-weight: 400;"><?php esc_html_e( 'This features are available in both free and paid versions.', 'giveasap' ); ?></em>
				</h2>
			</div>

			<div class="section-content has-2-columns feature-section two-col --fourth-col">
				<div class="section-item column col">
					<div class="inline-svg section-image purple "   >
                        <i class="fal fa-layer-group fa-7x"></i>
                    </div>
					<h3>Visual Builder <small>(Beta)</small></h3>
					<p>We've added a visual builder for giveaways. On each giveaway edit page, you can click the visual builder button and get an overlay screen where you'll see how the shortcode will look like</p>
                    <p><em>This is still in beta until we're sure that everything is working fine. So if you find an issue, let us know.</em></p>
				</div>



                <div class="section-item column col">
                    <div class="inline-svg green section-image ">
                        <i class="fal fa-cog fa-7x"></i>
                    </div>
                    <h3>Various fixes</h3>
                    <p>Fix: X icon for the sharing method was not loading properly..</p>
                    <p>Update: Licensing software</p>
                </div>

				<?php
				/*
				<div class="section-item col">
					<div class="inline-svg section-image">
						<i class="fal fa-envelope fa-7x"></i>
					</div>
					<h3>Re-send Emails</h3>
					<p>You can now send the subscription email from the Subscribers screen for each email. This feature is useful if your subscribers notify you about missing out the email.</p>
				</div>

				<div class="section-item col">
					<div class="inline-svg section-image purple">
						<i class="fal fa-pencil-paintbrush fa-7x"></i>
					</div>
					<h3>Email Template</h3>
					<p>We've changed the email template for a more structured email layout. You can add your logo to emails and also change the colors.</p>
				</div>


				<div class="section-item col">
					<div class="inline-svg section-image">
						<i class="fal fa-user fa-7x"></i>
					</div>
					<h3><?php _e( 'Subscriber Page', 'giveasap' ); ?></h3>
					<p>You can now view all the information about a subscriber on their own pages.</p>
				</div>
				<div class="section-item">
					<div class="inline-svg purple">
						<i class="fal fa-eye-slash fa-7x"></i>
					</div>
					<h3><?php _e( 'Hidden Content', 'giveasap' ); ?></h3>
					<p>Subscribers will now have the content of the giveaway hidden at first. They will see the sharing options much sooner :)</p>
				</div>
				<div class="section-item">
					<div class="inline-svg purple">
						<i class="fal fa-sitemap fa-7x"></i>
					</div>
					<h3><?php _e( 'Site Header & Footer', 'giveasap' ); ?></h3>
					<p>You can now decide for each giveaway to include the header and the footer of your site.</p>
				</div>
				<div class="section-item">
					<div class="inline-svg ">
						<i class="fal fa-trophy-alt fa-7x"></i>
					</div>
					<h3><?php _e( 'Winner in Giveaway Columns', 'giveasap' ); ?></h3>
					<p>When a winner is chosen, the winner will be shown in the list of all giveaways (in the admin).</p>
				</div>*/
				?>
			</div>
		</div>

		<?php /* Premium Section */ ?>
<?php /*
		<div class="floating-header-section">
			<div class="section-header">
				<h2><?php esc_html_e( 'Premium & Business Features', 'giveasap' ); ?>
					<?php
					if ( ! defined( 'GIVEASAP_PRO' ) ) {
						?>
						<em style="display:block;">
							<a href="<?php echo esc_url( giv_fs()->get_upgrade_url() ); ?>"
							   class="button button-primary"><?php esc_html_e( 'Upgrade to PRO', 'giveasap' ); ?></a>
						</em>
						<?php
					}
					?>
				</h2>
			</div>

			<div class="section-content has-2-columns feature-section two-col --three-col">

                <div class="section-item column col">
                    <div class="inline-svg white section-image " style="background:white;">
	                    <img src="<?php echo untrailingslashit( GASAP_URI ) . '/includes/integrations/assets/brevo.svg'; ?>" alt="">
                    </div>
                    <h3>Brevo Integration</h3>
                    <p>You can now connect Brevo with Simple Giveaways to send your subscribers to your lists.</p>
                </div>

				<?php
				/*
				<div class="section-item col column">
					<div class="inline-svg purple section-image">
						<i class="fal fa-plus-square fa-7x"></i>
					</div>
					<h3>Extra Entries Actions (Premium)</h3>
					<p>Old extra entries are now converted to new ones. The new actions are Facebook, Twitter, Instragram and YouTube visits.</p>
				</div>
				<div class="section-item col">
					<div class="inline-svg section-image purple">
						<img src="<?php echo untrailingslashit( GASAP_URI ) . '/includes/integrations/assets/mailster-logo.png'; ?>" alt="">
					</div>
					<h3>Mailster</h3>
					<p>You can connect your giveaways with Mailster.</p>
				</div>
				<div class="section-item col">
					<div class="inline-svg section-image ">
						<img src="<?php echo untrailingslashit( GASAP_URI ) . '/includes/integrations/assets/convertkit-logo.png'; ?>" alt="">
					</div>
					<h3><?php _e( 'ConvertKit - Form Builder', 'giveasap' ); ?></h3>
					<p>You can now connect custom fields from ConvertKit with custom fields.</p>
				</div>

				<div class="section-item col">
					<div class="inline-svg purple section-image">
						<i class="fal fa-palette fa-7x"></i>
					</div>
					<h3><?php _e( 'Fixes', 'giveasap' ); ?></h3>
					<p>Fixed the error when trying to delete a single subscriber.</p>
				</div>
				<div class="section-item">
					<div class="inline-svg section-image purple">
						<img src="url" alt="">
					</div>
					OR <div class="section-image">
						<img src="<?php echo trailingslashit(GASAP_URI ); ?>admin/documentation/images/custom-share.png" />
					</div>
					<h3><?php _e( 'MailChimp - Double Opt-in', 'giveasap' ); ?></h3>
					<p><?php _e( 'You can choose if your subscriber on MailChimp have to confirm before getting into the list. This will not change their entry in the giveaway.', 'giveasap' ); ?></p>
				</div>*/
				?>
			<?php /* </div>
		</div>

		<?php /* Business Section */ ?>
		<?php /* START */ ?>
		<div class="floating-header-section hidden">
			<div class="section-header">
				<h2><?php esc_html_e( 'Business Features' ); ?>
					<?php
					if ( ! defined( 'GIVEASAP_PRO' ) || ! giv_fs()->is_plan( 'business' ) ) {
						?>
						<em style="display:block;">
							<a href="<?php echo esc_url( giv_fs()->get_upgrade_url() ); ?>"
							   class="button button-primary"><?php esc_html_e( 'Upgrade to Business', 'giveasap' ); ?></a>
						</em>
						<?php
					}
					?>
				</h2>
			</div>

			<div class="section-content has-2-columns feature-section two-col --three-col ">
				<div class="section-item column col">
					<div class="inline-svg section-image white" style="background:white;">
						<img src="<?php echo untrailingslashit( GASAP_URI ) . '/includes/integrations/assets/woocommerce_logo.svg'; ?>" />
					</div>
					<h3>WooCommerce Settings</h3>
					<p>You can now decide if the user will be redirected to the giveaway page after the purchase is done.</p>
					<p>You can now decide if the "Purchased Action" under a giveawa will be completed when an order is purchased. You can check that under integration settings for WooCommerce.</p>

				</div>

				<?php
				/*
				<div class="section-item col column">
					<div class="inline-svg section-image" style="background: white;">
						<img src="<?php echo untrailingslashit( GASAP_URI ) . '/includes/integrations/assets/edd.svg'; ?>" alt="">
					</div>
					<h3>Easy Digital Downloads Integration</h3>
					<p>Easy Digital Downloads Product can add extra entries when bought.</p>
				</div>



				<div class="section-item col column">
					<div class="inline-svg purple section-image">
						<i class="fal fa-plus-square fa-7x"></i>
					</div>
					<h3>Extra Entries Actions (Premium)</h3>
					<p>Old extra entries are now converted to new ones. The new actions are Facebook, Twitter, Instragram and YouTube visits.</p>
				</div>
				<div class="section-item col">
					<div class="inline-svg section-image purple">
						<img src="<?php echo untrailingslashit( GASAP_URI ) . '/includes/integrations/assets/mailster-logo.png'; ?>" alt="">
					</div>
					<h3>Mailster</h3>
					<p>You can connect your giveaways with Mailster.</p>
				</div>
				<div class="section-item col">
					<div class="inline-svg section-image ">
						<img src="<?php echo untrailingslashit( GASAP_URI ) . '/includes/integrations/assets/convertkit-logo.png'; ?>" alt="">
					</div>
					<h3><?php _e( 'ConvertKit - Form Builder', 'giveasap' ); ?></h3>
					<p>You can now connect custom fields from ConvertKit with custom fields.</p>
				</div>

				<div class="section-item col">
					<div class="inline-svg purple section-image">
						<i class="fal fa-palette fa-7x"></i>
					</div>
					<h3><?php _e( 'Fixes', 'giveasap' ); ?></h3>
					<p>Fixed the error when trying to delete a single subscriber.</p>
				</div>
				<div class="section-item">
					<div class="inline-svg section-image purple">
						<img src="{url}}" alt="">
					</div>
					OR <div class="section-image">
						<img src="<?php echo trailingslashit(GASAP_URI ); ?>admin/documentation/images/custom-share.png" />
					</div>
					<h3><?php _e( 'MailChimp - Double Opt-in', 'giveasap' ); ?></h3>
					<p><?php _e( 'You can choose if your subscriber on MailChimp have to confirm before getting into the list. This will not change their entry in the giveaway.', 'giveasap' ); ?></p>
				</div>*/
				?>

		<?php /* BusinessEND: */ ?>
			</div>
		</div> <?php /*END */ ?>

		<hr/>

		<?php
		/*
		<div class="changelog">
			<h2>
				<?php
				_e( 'Changelog', 'giveasap' );

				?>
			</h2>
			<p>If winner(s) field is empty, the giveaway template will not display winner meta information.</p>
			<p>Added customized admin notices when changing giveaway statuses.</p>


		</div>

		<hr />*/
		?>

		<div class="return-to-dashboard">
			<a href="<?php echo esc_url( self_admin_url() ); ?>"><?php is_blog_admin() ? esc_html_e( 'Go to Dashboard &rarr; Home' ) : esc_html_e( 'Go to Dashboard' ); ?></a>
		</div>
	</div>
	<?php
}
