('wp_ajax_get_user_locations', 'get_user_locations');
function get_active_subscription_id($user_id) {
	if (!function_exists('wcs_get_users_subscriptions')) {
	  return false;
	}
  
	$subscriptions = wcs_get_users_subscriptions($user_id, array(
	  'subscriptions_per_page' => -1,
	  'status' => array('active')
	));
  
	if (empty($subscriptions)) {
	  return false;
	}

	$subscription = reset($subscriptions);
  
	return $subscription->get_id();
}

add_action( 'wp_ajax_userdata_ajax_action', 'userdata_ajax_function' );
function userdata_ajax_function() {
?>
<table class="table gs-table">
	<thead class="gs-table-head">
		<tr>
			<th><span>Username</span></th>
			<th><span>First Name</span></th>
			<th><span>Last Name</span></th>
			<th><span>Email</span></th>
			<th><span>Active Subscriber</span></th>
			<th><span>Actions</span></th>
		</tr>
	</thead>
	<tbody>
<?php
	$user_data = get_userdata( $_POST['user_id'] );
    if(isset($_POST['user_id']) && $user_data):
		$subscription_id = get_active_subscription_id($_POST['user_id']);
		$user_edit_link = get_edit_user_link($_POST['user_id']);
		$subscription_link = get_edit_post_link($subscription_id);
?>
		<tr>
			<td><?php echo $user_data->user_login; ?></td>
			<td><?php echo $user_data->first_name; ?></td>
			<td><?php echo $user_data->last_name; ?></td>
			<td><?php echo $user_data->user_email; ?></td>
			<?php if($subscription_id):?><td><span class="active">active</span></td><?php else: ?><td><span class="inactive">inactive</span></td><?php endif; ?>
			<td><a class="btn btn-info mr-1" href="<?php echo $user_edit_link; ?>" target="_blank">View profile</a><?php if($subscription_link):?><a class="btn btn-success" href="<?php echo $subscription_link; ?>" target="_blank">View subscription</a><?php endif;?></td>
		</tr>
<?php
	elseif(empty($_POST['user_id'])):
		echo '<tr class="alert alert-warning"><td colspan="6" class="text-center">You must add USER ID</td></tr>';
	else:
		echo '<tr class="alert alert-warning"><td colspan="6" class="text-center">This user doesnt exist</td></tr>';
	endif;
?>
	</tbody>
</table>
<?php 
    wp_die();
}

add_action( 'wp_ajax_subscriptions_ajax_action', 'subscriptions_ajax_function' );
function subscriptions_ajax_function() {
?>
<table class="table gs-table">
	<thead class="gs-table-head">
		<tr>
			<th><span>Username</span></th>
			<th><span>Full name</span></th>
			<th><span>Email</span></th>
			<th><span>Price</span></th>
			<th><span>Status</span></th>
			<th><span>Next payment</span></th>
			<th><span>Actions</span></th>
		</tr>
	</thead>
	<tbody>
<?php 
// Get all subscriptions for the user
$args = array(
    'customer_id' => $_POST['user_id'],
    'subscription_status' => 'any',
    'subscriptions_per_page' => -1,
);
$subscriptions = wcs_get_subscriptions( $args );
    if(isset($_POST['user_id']) && $subscriptions):
		$user_data = get_userdata( $_POST['user_id'] );
		foreach ( $subscriptions as $subscription ) {
			$order = $subscription->get_parent();
			echo '<tr>';
			$next_payment_timestamp = $subscription->get_time( 'next_payment' );
			$next_payment_date = date( 'Y-m-d H:i:s', $next_payment_timestamp );
			echo '<td>'.$user_data->user_login.'</td>';
			echo '<td>'.$user_data->first_name.' '.$user_data->last_name.'</td>';
			echo '<td>'.$user_data->user_email.'</td>';
			echo '<td>'.$subscription->get_total().''.$subscription->get_currency().'</td>';
			echo '<td>'.$order->get_status().'</td>';
			if($order->get_status() == 'completed'): echo '<td>'.$next_payment_date.'</td>'; else: echo '<td></td>'; endif;
			echo '<td><a class="btn btn-info mr-1" href="'.get_edit_post_link($subscription->get_parent_id()).'">View order</a><a class="btn btn-success" href="'.get_edit_post_link($subscription->get_id()).'">View subscription</a></td>';
			echo '</tr>';
		}			
	elseif(empty($_POST['user_id'])):
		echo '<tr class="alert alert-warning"><td colspan="7" class="text-center">You must add USER ID</td></tr>';
	else:
		echo '<tr class="alert alert-warning"><td colspan="7" class="text-center">This user doesnt exist</td></tr>';
	endif;
	?>
	</tbody>
</table>
<?php 
    wp_die();
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      [31-Mar-2023 10:38:40 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 10:38:40 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 10:41:29 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 10:41:29 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 10:41:29 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 10:56:38 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 10:56:38 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 10:56:38 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 11:06:12 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 11:06:12 UTC] PHP Warning:  Attempt to read property "post_title" on null in /home/goldiu7z/public_html/wp-content/plugins/js_composer/include/helpers/helpers.php on line 60
[31-Mar-2023 11:08:14 UTC] PHP Warning:  Undefined array key "startDate" in /home/goldiu7z/public_html/wp-content/plugins/gp-userdata/gp-userdata.php on line 62
[31-Mar-2023 11:08:14 UTC] PHP Warning:  Undefined array key "endDate" in /home/goldiu7z/public_html/wp-content/plugins/gp-userdata/gp-userdata.php on line 63
[31-Mar-2023 11:08:20 UTC] PHP Warning:  foreach() argument must be of type array|object, string given in /home/goldiu7z/public_html/wp-content/plugins/gp-userdata/gp-userdata.php on line 70
[31-Mar-2023 11:09:27 UTC] Array
(
    [SERVER_SOFTWARE] => Apache
    [REQUEST_URI] => /my-account/
    [USER] => goldiu7z
    [HOME] => /home/goldiu7z
    [SCRIPT_NAME] => /index.php
    [QUERY_STRING] => 
    [REQUEST_METHOD] => POST
    [SERVER_PROTOCOL] => HTTP/2.0
    [GATEWAY_INTERFACE] => CGI/1.1
    [REDIRECT_URL] => /my-account/
    [REMOTE_PORT] => 62695
    [SCRIPT_FILENAME] => /home/goldiu7z/public_html/index.php
    [SERVER_ADMIN] => webmaster@gold-predictions.com
    [CONTEXT_DOCUMENT_ROOT] => /home/goldiu7z/public_html
    [CONTEXT_PREFIX] => 
    [REQUEST_SCHEME] => https
    [DOCUMENT_ROOT] => /home/goldiu7z/public_html
    [REMOTE_ADDR] => 49.206.113.139
    [SERVER_PORT] => 443
    [SERVER_ADDR] => 45.158.28.238
    [SERVER_NAME] => gold-predictions.com
    [SERVER_SIGNATURE] => 
    [PATH] => /usr/local/jdk/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin:/opt/bin
    [HTTP_X_HTTPS] => 1
    [HTTP_X_SH_APACHE_PROXIED] => 1
    [HTTP_X_SH_APACHE_IP] => 49