From b6cc128c5f7c2f0455b61d4cde9b2f9b73913631 Mon Sep 17 00:00:00 2001 From: Ross Trottier Date: Fri, 10 May 2024 09:52:59 -0600 Subject: [PATCH] get sub id by vm id --- rest-api/provisioner_rest_api_extensions.php | 59 +++++++++++++++++--- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/rest-api/provisioner_rest_api_extensions.php b/rest-api/provisioner_rest_api_extensions.php index 3680ddd..0715fe0 100644 --- a/rest-api/provisioner_rest_api_extensions.php +++ b/rest-api/provisioner_rest_api_extensions.php @@ -425,18 +425,63 @@ function federated_route_needs_provision_register(){ function federated_needs_provision_subscriptions(){ $args = array( - 'numberposts' => -1, - 'post_type' => 'shop_subscription', - 'post_status' => 'wc-active', - 'meta_key' => 'provision_status', - 'meta_value' => 'needs_provision' - ); + 'numberposts' => -1, + 'post_type' => 'shop_subscription', + 'post_status' => 'wc-active', + 'meta_key' => 'provision_status', + 'meta_value' => 'needs_provision' + ); + + $posts = get_posts($args); + + return array_map('get_subscription_id', $posts); +} + +//Get by VM ID +add_action('rest_api_init', 'federated_route_get_subscription_by_vm_id'); + +function federated_route_get_subscription_by_vm_id(){ + //register routes here + register_rest_route( + 'federated-rest/v1', + 'get-subscription-id-by-vm-id', + array( + 'methods' => 'POST', + 'callback' => 'federated_get_subscription_by_vm_id', + 'permissions_callback' => 'check_federated_permissions' + ) + ); +} + +function federated_get_subscription_by_vm_id($request){ + $vm_id = $request['vm_id']; + + $args = array( + 'numberposts' => -1, + 'post_type' => 'shop_subscription', + 'post_status' => 'wc-active', + 'meta_key' => 'vm_id', + 'meta_value' => $vm_id + ); $posts = get_posts($args); + $output = array(); - return array_map('get_subscription_id', $posts); + foreach($posts as $post) { + $subscription_id = get_subscription_id($post); + $subscription_vm_id = get_field('vm_id', $subscription_id); + + if($subscription_vm_id == $vm_id) { + $output['subscription_id'] = $subscription_id; + return $output; + } + } + + $output['subscription_id'] = 0; + return $output; } + //Shared Functions function get_subscription_id($subscription){ return $subscription->ID;