<?php
/**
 * JNews Paywall - Backend Menu Template
 */

use JNews\Paywall\Users_Filter;

if ( ! function_exists( 'jpw_menu' ) ) {
	add_action( 'admin_menu', 'jpw_menu' );

	/**
	 * JPW Menu
	 **/
	function jpw_menu() {

		/* add_submenu_page('post-paywall', 'General', 'General', 'manage_options', 'post-paywall', 'jpw_general', 'post-paywall'); */

		if ( current_user_can( 'administrator', get_current_user_id() ) ) {
			$query['autofocus[section]'] = 'jnews_paywall_section';
			$section_link                = add_query_arg( $query, admin_url( 'customize.php' ) );

			add_menu_page( 'JNews Paywall', 'JNews Paywall', 'manage_options', 'post-paywall', 'jpw_status' );
			add_submenu_page( 'post-paywall', 'Subscribers Status', 'Users Status', 'manage_options', 'post-paywall', 'jpw_status' );
			add_submenu_page( 'post-paywall', 'Customizer Setting', 'Customizer Setting', 'manage_options', $section_link );
		} else {
			add_menu_page( 'Premium Status', 'Premium Status', 'manage_options', 'post-paywall', 'jpw_status' );
			add_submenu_page( 'post-paywall', 'Subscription', 'Subscription', 'read', 'my-subscription', 'jpw_subscribe' );
			add_submenu_page( 'post-paywall', 'Unlocked Posts', 'Unlocked Posts', 'read', 'unlocked-posts', 'jpw_unlock' );
		}
	}
}

/**
 * Admin Menu
 */
function jpw_status() {
	$post_per_page = get_option( 'posts_per_page', 10 );
	$paged         = ( isset( $_GET['paged'] ) && ! empty( $_GET['paged'] ) ) ? (int) sanitize_text_field( wp_unslash( $_GET['paged'] ) ) : 1;
	$edit_user     = ( isset( $_GET['edit-user'] ) && ! empty( $_GET['edit-user'] ) ) ? (int) sanitize_text_field( wp_unslash( $_GET['edit-user'] ) ) : false;

	if ( $edit_user ) {
		$update_user = ( isset( $_POST['update_user_data'] ) && ! empty( $_POST['update_user_data'] ) ) ? sanitize_text_field( wp_unslash( $_POST['update_user_data'] ) ) : false;
		if ( $update_user ) {
			update_subscriber( $edit_user );
		} else {
			edit_subscriber( $edit_user );
		}
	} else {
		$page_url     = menu_page_url( 'post-paywall', false );
		$search       = ( isset( $_GET['search'] ) && ! empty( $_GET['search'] ) ) ? sanitize_text_field( wp_unslash( $_GET['search'] ) ) : '';
		$users_filter = ( isset( $_GET['users_filter'] ) && ! empty( $_GET['users_filter'] ) ) ? sanitize_text_field( wp_unslash( $_GET['users_filter'] ) ) : '';
		if ( $paged === 1 ) {
			$offset = 0;
		} else {
			$offset = ( $paged - 1 ) * $post_per_page;
		}

		$filter_options = array(
			'all'           => __( 'All', 'jnews-paywall' ),
			'subscribed'    => __( 'Subscribed', 'jnews-paywall' ),
			'not_subscribe' => __( 'Not Subscribe', 'jnews-paywall' ),
			'has_unlock'    => __( 'Has Unlock Remaining', 'jnews-paywall' ),
		);
		$filter_form    = '';
		foreach ( $filter_options as $value => $option ) {
			if ( $users_filter === $value ) {
				$filter_form .= "<option value=\"{$value}\" selected>$option</option>";
			} else {
				$filter_form .= "<option value=\"{$value}\">$option</option>";
			}
		}

		if ( ! empty( $users_filter ) && 'all' !== $users_filter ) {
			$user_query = new Users_Filter( $users_filter, $post_per_page, $offset );
			$users      = $user_query->get_users();
			$total_user = $user_query->get_total();

		} else {
			$user_query = new WP_User_Query(
				array(
					'number' => $post_per_page,
					'offset' => $offset,
					'search' => '*' . $search . '*',
				)
			);

			$total_user = $user_query->get_total();
			$users      = $user_query->get_results();
		}
		$total_pages = ceil( $total_user / $post_per_page );
		$userlist    = '<table class=\'jpw-subscriber widefat striped\' style=\'width:99%\'>
                    <tr>
                        <th>' . esc_html__( 'Username', 'jnews-paywall' ) . '</th>
                        <th>' . esc_html__( 'Full Name', 'jnews-paywall' ) . '</th>
                        <th>' . esc_html__( 'Email', 'jnews-paywall' ) . '</th>
                        <th>' . esc_html__( 'Subscription Status', 'jnews-paywall' ) . '</th>
                        <th>' . esc_html__( 'Times Left', 'jnews-paywall' ) . '</th>
						<th>' . esc_html__( 'Expiration Date', 'jnews-paywall' ) . '</th>
						<th>' . esc_html__( 'Unlock Remaining', 'jnews-paywall' ) . '</th>
                    </tr>';

		foreach ( $users as $user ) {
			$status           = get_user_option( 'jpw_subscribe_status', $user->ID );
			$date_format      = get_option( 'date_format' );
			$expired          = get_user_option( 'jpw_expired_date', $user->ID ); // zWMGjei1
			$unlock_remaining = get_user_option( 'jpw_unlock_remaining', $user->ID ) ? get_user_option( 'jpw_unlock_remaining', $user->ID ) : 0;
			$status_user      = esc_html__( 'Not Subscribe', 'jnews-paywall' );
			$formatted_date   = '&ndash;';
			$days_left        = '&ndash;';

			$subscription_type = get_user_option( 'jpw_subs_type', $user->ID );
			$subscription_id   = get_user_option( 'jpw_' . $subscription_type . '_subs_id', $user->ID );

			if ( function_exists( 'wcs_get_subscription' ) ) {
				$wcs_order_id = get_user_option( 'jpw_subscribe_id', $user->ID );
				if ( $wcs_order_id ) {
					$subscription_id   = $wcs_order_id;
					$wcs_order         = wcs_get_subscription( $wcs_order_id );
					$subscription_type = $wcs_order->get_payment_method();
				}
			}

			if ( 'paypal' === $subscription_type ) {
				/* get current date by paypall timezone setting */
				$timezone = new JPW_Paypal();
				$timezone = $timezone->get_the_timezone();
			} else {
				$timezone = '+0';
			}
			$timezone     = new DateTimeZone( $timezone );
			$current_date = new DateTime( 'now', $timezone );

			$expired_timestamp = strtotime( $expired ); // see vEe2hh0Z
			if ( $status && 'ACTIVE' === $status ) {
				if ( $expired_timestamp !== false ) {
					$current_date = new DateTime( $current_date->format( 'Y-m-d H:i:s' ) );
					$expired_date = new DateTime( $expired ); // Create DateTime from Unix timestamp
					if ( $current_date <= $expired_date ) {
						$diff           = date_diff( $current_date, $expired_date );
						$status_user    = esc_html__( 'Subscribed', 'jnews-paywall' );
						$formatted_date = $expired ? date_i18n( $date_format, strtotime( $expired ) ) : Date( $date_format );
						$days_left      = ' ' . $diff->days . esc_html__( 'days', 'jnews-paywall' ) . ' ' . $diff->h . esc_html__( 'hours', 'jnews-paywall' );
					} else {
						update_user_option( $user->ID, 'jpw_subscribe_status', false );
						$status_user    = esc_html__( 'Not Subscribe', 'jnews-paywall' );
						$days_left      = '&ndash;';
						$formatted_date = '&ndash;';
					}
				} else {
					// update jpw status if expried date is empty
					update_user_option( $user->ID, 'jpw_subscribe_status', false );
				}
			}

			if ( get_user_option( 'first_name', $user->ID ) || get_user_option( 'last_name', $user->ID ) ) {
				$name = get_user_option( 'first_name', $user->ID ) . ' ' . get_user_option( 'last_name', $user->ID );
			} else {
				$name = '—';
			}

			$edit_user = $subscription_id ? '<br>
						<a class=\'edit-user\' href=\'' . add_query_arg( 'edit-user', $user->ID, $page_url ) . '\' > Edit</a>' : '';

			$userlist .= '<tr>
                        <td>' . get_avatar( $user->ID, 32 ) . '<a class=\'username\' href=\'' . get_author_posts_url( $user->ID ) . '\' >' . $user->user_login . '</a>' . $edit_user . '
						</td>
                        <td>' . $name . '</td>
                        <td>' . $user->user_email . '</td>
                        <td>' . $status_user . '</td>
                        <td>' . $days_left . '</td>
						<td>' . $formatted_date . '</td> 
						<td>' . $unlock_remaining . '</td>
                    </tr>';

		}

		if ( $total_user === 0 ) {
			$userlist .= '<tr>
			<td colspan="7">' . __( 'No Users Found', 'jnews-paywall' ) . '</td>
			</tr>';
		}
		$page_link = jnews_paging_navigation(
			array(
				'base'                => $page_url . '%_%',
				'format'              => '&paged=%#%',
				'current'             => $paged,
				'total'               => $total_pages,
				'pagination_mode'     => 'nav_1',
				'pagination_align'    => 'center',
				'pagination_navtext'  => true,
				'pagination_pageinfo' => true,
				'prev_text'           => __( '&lsaquo;' ),
				'next_text'           => __( '&rsaquo;' ),
			)
		);

		$search_text = esc_html( __( 'Search Subscriber ...', 'jnews-paywall' ) );
		$filter_form = '<div class="jpw-filter-form">
							<form action= "" method="GET">
								<input type="hidden" name="page" value="post-paywall">
									<select name="users_filter" id="users_filter" class="postform">
										' . $filter_form . '
									</select>
									<input type="submit" value="Filter" class="button">
							</form>
							<form action= "" method="GET">
							<input type="hidden" name="page" value="post-paywall">
								<input type="text" name="search" ' . " value=\"{$search}\" placeholder=\"{$search_text}\"" . '>
								<input type="submit" value="Search" class="button">
							</form>
					</div>
			';
		$userlist   .= '</table>';
		$menu_status = '<div class=\'jpw_manage_status\'>
                        <h3>' . esc_html__( 'Post Paywall Users Status', 'jnews-paywall' ) . '</h3>
                        <p>' . esc_html__( 'Here you can monitor your Post Paywall Subscriber latest status.', 'jnews-paywall' ) . '</p>
                        ' . $filter_form . $userlist . '
                        ' . ( $page_link ? '<div class="tablenav"><div class=\'tablenav-pages\'>' . $page_link . '</div></div>' : '' ) . '
                    </div>';

		echo $menu_status;
	}
}

/**
 * User Menu : Subscription Status
 */
function jpw_subscribe() {
	$status            = get_user_option( 'jpw_subscribe_status', get_current_user_id() );
	$sub_type          = get_user_option( 'jpw_subs_type', get_current_user_id() );
	$sub_id            = get_user_option( 'jpw_' . $sub_type . '_subs_id', get_current_user_id() );
	$date_format       = get_option( 'date_format' );
	$expired           = get_user_option( 'jpw_expired_date', get_current_user_id() ) ? get_user_option( 'jpw_expired_date', get_current_user_id() ) : Date( $date_format );
	$expired_timestamp = strtotime( $expired );
	$current_date      = new DateTime();
	$remaining         = '';
	if ( $expired_timestamp !== false ) {
		$expired_date = new DateTime( '@' . $expired_timestamp );
		if ( $current_date <= $expired_date ) {
			$remaining = $current_date->diff( $expired_date )->format( '%a ' . esc_html__( 'days', 'jnews-paywall' ) . ' %h ' . esc_html__( 'hours', 'jnews-paywall' ) );
		}
	}

	if ( function_exists( 'wcs_get_subscription' ) ) {
		$wcs_order_id = get_user_option( 'jpw_subscribe_id', get_current_user_id() );
		if ( $wcs_order_id ) {
			$sub_id    = $wcs_order_id;
			$wcs_order = wcs_get_subscription( $wcs_order_id );
			$sub_type  = $wcs_order->get_payment_method();
		}
	}

	if ( $status && $status === 'ACTIVE' ) {
		$cencel_subs_nonce = wp_create_nonce( 'jpw_cencel_subs' );
		$mystatus          = '<div class=\'jpw_leftbox\'>
							<span><strong>' . esc_html__( 'Subscription ID', 'jnews-paywall' ) . ' : </strong>' . $sub_id . '</span>
							<span><strong>' . esc_html__( 'Subscription Status', 'jnews-paywall' ) . ' : </strong>' . esc_html__( 'ACTIVE', 'jnews-paywall' ) . '</span>
							<span><strong>' . esc_html__( 'Remaining Time', 'jnews-paywall' ) . ' : </strong>' . $remaining . '</span>
							<span><strong>' . esc_html__( 'Next Payment Due', 'jnews-paywall' ) . ' : </strong>' . date_i18n( $date_format, strtotime( $expired ) ) . '</span>
							<span><strong>' . esc_html__( 'Payment Type', 'jnews-paywall' ) . ' : </strong>' . $sub_type . '</span>
						</div>
						<div class=\'jpw_rightbox\'>
							<input type="hidden" id="jpw_cencel_subs" value="' . $cencel_subs_nonce . ' "></input>
							<button class=\'subscription\'>' . esc_html__( 'Cancel Subscription', 'jnews-paywall' ) . '</button>
						</div>';
	} else {
		$mystatus = '<span>' . esc_html__( 'You are not subscribed', 'jnews-paywall' ) . '</span>';
	}

	$output = '<div class=\'jpw_manage_status subscription\'>
					<h2>' . esc_html__( 'Subscription Status', 'jnews-paywall' ) . '</h2>
					<div class=\'jpw_boxed\'>
						' . $mystatus . '
					</div>
				</div>';

	echo $output;
}

/**
 * User Menu : Unlocked Posts
 */
function jpw_unlock() {
	$unlock_remaining = get_user_option( 'jpw_unlock_remaining', get_current_user_id() ) ? get_user_option( 'jpw_unlock_remaining', get_current_user_id() ) : 0;
	$unlocked_posts   = get_user_option( 'jpw_unlocked_post_list', get_current_user_id() ) ? get_user_option( 'jpw_unlocked_post_list', get_current_user_id() ) : array();
	$post_list        = '';
	$post_total       = 0;

	if ( ! empty( $unlocked_posts ) ) {
		foreach ( $unlocked_posts as $post_id ) {
			$post_list .= '<tr>
								<td><a href=\'' . get_permalink( $post_id ) . '\'>' . get_the_title( $post_id ) . '</a></td>
							</tr>';
			++$post_total;
		}
	} else {
		$post_list .= '<tr>
						<td>' . esc_html__( 'You don\'t have any post unlocked', 'jnews-paywall' ) . '</td>
					</tr>';
	}

	$output = '<div class=\'jpw_manage_status unlock\'>
					<h2>' . esc_html__( 'Unlocked Posts', 'jnews-paywal' ) . '</h2>
					<div class=\'jpw_boxed\'>
						<span><strong>' . esc_html__( 'Quotas Left', 'jnews-paywall' ) . ' : </strong>' . $unlock_remaining . ' ' . esc_html__( 'unlocks', 'jnews-paywall' ) . '</span>
						<span><strong>' . esc_html__( 'Posts Owned', 'jnews-paywall' ) . ' : </strong>' . $post_total . ' ' . esc_html__( 'posts', 'jnews-paywall' ) . '</span>
					</div>
					<br/>
					<table class=\'jpw-subscriber widefat striped\' style=\'width:66%\'>
						<tr>
							<th>' . esc_html__( 'Unlocked Posts Collection', 'jnews-paywall' ) . '</th>
						</tr>
						' . $post_list . '
					</table>
				</div>';

	echo $output;
}

/**
 * Edit subscriber data form
 *
 * @param int $id use id.
 */
function edit_subscriber( $id ) {
	$status_list       = array(
		'ACTIVE'    => __( 'ACTIVE', 'jnews-paywall' ),
		'INACTIVE'  => __( 'INACTIVE', 'jnews-paywall' ),
		'CANCELLED' => __( 'CANCELLED', 'jnews-paywall' ),
	);
	$status            = get_user_option( 'jpw_subscribe_status', $id ) ? get_user_option( 'jpw_subscribe_status', $id ) : 'INACTIVE';
	$expired           = get_user_option( 'jpw_expired_date', $id );
	$subscription_type = get_user_option( 'jpw_subs_type', $id );
	$subscription_id   = get_user_option( 'jpw_' . $subscription_type . '_subs_id', $id );
	$user              = get_user_by( 'id', $id );
	if ( function_exists( 'wcs_get_subscription' ) ) {
		$wcs_order_id = get_user_option( 'jpw_subscribe_id', $id );
		if ( $wcs_order_id ) {
			$subscription_id   = $wcs_order_id;
			$wcs_order         = wcs_get_subscription( $wcs_order_id );
			$subscription_type = $wcs_order->get_payment_method();
		}
	}
	if ( $subscription_id ) {
		if ( $expired ) {
			$date_object = new DateTime( $expired );
			$expired     = $date_object->format( 'Y-m-d\TH:i' );
		}

		if ( get_transient( 'jpw_update_user_notice' ) ) {
			echo '<div class="notice notice-success is-dismissible">';
			echo '<p>' . esc_html( get_transient( 'jpw_update_user_notice' ) ) . '</p>';
			echo '</div>';
			delete_transient( 'jpw_update_user_notice' );
		}

		if ( get_transient( 'jpw_failed_update_user_notice' ) ) {
			echo '<div class="notice notice-error is-dismissible">';
			echo '<p>' . esc_html( get_transient( 'jpw_failed_update_user_notice' ) ) . '</p>';
			echo '</div>';
			delete_transient( 'jpw_failed_update_user_notice' );
		}

		?> 
		<div class="jpw-edit-subscriber">
			<div class="form-wrap">
				<h1>
					<?php
						echo esc_html( __( 'JNews Paywall User Status', 'jnews-paywall' ) );
					?>
				</h1>
				<div class="notice-wrapper">
				<div class="notice-icon">
					<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
					<path d="M7 0.21875C3.25508 0.21875 0.21875 3.25618 0.21875 7C0.21875 10.746 3.25508 13.7812 7 13.7812C10.7449 13.7812 13.7812 10.746 13.7812 7C13.7812 3.25618 10.7449 0.21875 7 0.21875ZM7 3.22656C7.63427 3.22656 8.14844 3.74073 8.14844 4.375C8.14844 5.00927 7.63427 5.52344 7 5.52344C6.36573 5.52344 5.85156 5.00927 5.85156 4.375C5.85156 3.74073 6.36573 3.22656 7 3.22656ZM8.53125 10.1719C8.53125 10.3531 8.38433 10.5 8.20312 10.5H5.79688C5.61567 10.5 5.46875 10.3531 5.46875 10.1719V9.51562C5.46875 9.33442 5.61567 9.1875 5.79688 9.1875H6.125V7.4375H5.79688C5.61567 7.4375 5.46875 7.29058 5.46875 7.10938V6.45312C5.46875 6.27192 5.61567 6.125 5.79688 6.125H7.54688C7.72808 6.125 7.875 6.27192 7.875 6.45312V9.1875H8.20312C8.38433 9.1875 8.53125 9.33442 8.53125 9.51562V10.1719Z" fill="#2271B1"/>
					</svg>
				</div>
				<div class="notice-content">
					<p>
					Use this form to update the Subscription Status and Expiration Date for your subscriber if the user has successfully made a Post Subscribe payment but their status has not been updated. For a permanent solution, you can adjust your payment configuration according to the following <a target="_blank" href="https://support.jegtheme.com/documentation/new-metabox-paywall-gutenberg/">documentation</a> or contact  <a target="_blank" href="https://support.jegtheme.com/forums/forum/jnews/">JNews support </a>.
					</p>
				</div>
				</div>
				<h3>
					<?php
						echo esc_html( __( 'Subscriber Information', 'jnews-paywall' ) );
					?>
				</h3>
				<form action="" method="POST">
					<?php wp_nonce_field( 'jpw_update_subs', 'jpw_update_nonce' ); ?>
					<input type="hidden" name="update_user_data" value="yes">
					<input type="hidden" name="subscription_id" value="<?php echo esc_html( $subscription_id ); ?>">
					<div class="jpw-user-info-wrapper">
						<div class="user-info">
							<label class="label">
								<?php
									echo esc_html( __( 'full name', 'jnews-paywall' ) );
								?>
							</label>
							<p>
								<?php
									echo esc_html( $user->data->display_name );
								?>
							</p>
						</div>
						<div class="user-info">
							<label class="label">
								<?php
									echo esc_html( __( 'username', 'jnews-paywall' ) );
								?>
							</label>
							<p>
								<?php
									echo esc_html( $user->data->user_login );
								?>
							</p>
						</div>
						<div class="user-info">
							<label class="label">
								<?php
									echo esc_html( __( 'email address', 'jnews-paywall' ) );
								?>
							</label>
							<p>
								<?php
									echo esc_html( $user->data->user_email );
								?>
							</p>
						</div>
						<div class="user-info">
							<label class="label">
								<?php
									echo esc_html( __( 'subsriber id', 'jnews-paywall' ) );
								?>
							</label>
							<p>
								<?php
									echo esc_html( $subscription_id );
								?>
							</p>
						</div>
						<div class="user-info">
							<label class="label">
								<?php
									echo esc_html( __( 'payment type', 'jnews-paywall' ) );
								?>
							</label>
							<p style="text-transform: capitalize;">
								<?php
									echo esc_html( $subscription_type );
								?>
							</p>
						</div>
					</div>
					<div class="sparator"></div>
					<div class="jpw-user-info-wrapper">
						<div class="user-info">
							<label class="label">
								<?php
									echo esc_html( __( 'Subscription Status', 'jnews-paywall' ) );
								?>
							</label>
							<select name="subscription_status" id="subscription_status" class="postform">
								<?php
								foreach ( $status_list as $key => $val ) {
									$selected = ( $key == $status ) ? 'selected' : '';
									echo "<option value={$key} {$selected}> {$val}</option>"; /* phpcs:ignore */
								}
								?>
							</select>
						</div>
						<div class="user-info">
							<label class="label">
								<?php
									echo esc_html( __( 'Expiration Date', 'jnews-paywall' ) );
								?>
							</label>
							<input type="datetime-local" name="expired_date" class="jpw-time-remaining" id="expired_date" 
									<?php
									if ( $expired ) {
										echo "value =\"{$expired}\"";/* phpcs:ignore */
									}

									?>
						>
						</div>
					</div>
					<div class="sparator"></div>
					<p class="expired-date-note">
						<b><?php echo esc_html( __( 'Note: ', 'jnews-paywall' ) ); ?></b>
						<?php echo esc_html( __( "Please ensure the Expiration Date aligns with the next scheduled payment date based on the subscriber's payment method (e.g., PayPal, Stripe, or WooCommerce Subscription).", 'jnews-paywall' ) ); ?>
					</p>
					<input type="submit" name="submit" id="submit" class="button button-primary" value=" <?php echo esc_html( __( 'Update Subscriber', 'jnews-paywall' ) ); ?>">
				</form>
			</div>
		</div>

		<?php
	} else {
		wp_safe_redirect( menu_page_url( 'post-paywall', false ) );
	}
}

/**
 * Update subsriber data method
 *
 * @param int $id user id.
 */
function update_subscriber( $id ) {

	if ( isset( $_POST['jpw_update_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['jpw_update_nonce'] ), 'jpw_update_subs' ) && isset( $_POST['_wp_http_referer'] ) ) {
		$redirect_url        = esc_url_raw( wp_unslash( $_POST['_wp_http_referer'] ) );
		$subscription_status = ( isset( $_POST['subscription_status'] ) && ! empty( $_POST['subscription_status'] ) ) ? sanitize_text_field( wp_unslash( $_POST['subscription_status'] ) ) : false;
		$expired_date        = ( isset( $_POST['expired_date'] ) && ! empty( $_POST['expired_date'] ) ) ? sanitize_text_field( wp_unslash( $_POST['expired_date'] ) ) : false;
		$subscription_id     = ( isset( $_POST['subscription_id'] ) && ! empty( $_POST['subscription_id'] ) ) ? sanitize_text_field( wp_unslash( $_POST['subscription_id'] ) ) : false;
		if ( $subscription_status && $expired_date && $subscription_id ) {
			$now     = new DateTime( 'now' );
			$expired = new DateTime( $expired_date );
			if ( 'INACTIVE' !== $subscription_status ) {
				if ( $expired < $now && 'ACTIVE' === $subscription_status ) {
					set_transient( 'jpw_failed_update_user_notice', esc_html( __( 'Failed to update the subscriber status to Active because the expiration date entered is earlier than the current time.', 'jnews-paywall' ) ), 30 );
					wp_safe_redirect( $redirect_url );
					exit;
				}
				update_user_option( $id, 'jpw_subscribe_status', strtoupper( $subscription_status ) );
			} else {
				update_user_option( $id, 'jpw_subscribe_status', false );
			}

			if ( is_object( $expired ) ) {
				$expired->setTimezone( new DateTimeZone( 'UTC' ) );
				$expired = $expired->format( 'Y-m-d H:i:s' );
			}

			update_user_option( $id, 'jpw_expired_date', $expired );
		}
		set_transient( 'jpw_update_user_notice', esc_html( __( 'Subscriber data successfully updated.', 'jnews-paywall' ) ), 30 );

		wp_safe_redirect( $redirect_url );
		exit;
	}
}
