get sub id by vm id

This commit is contained in:
Ross Trottier 2024-05-10 09:52:59 -06:00
parent 3a6eb3b9cb
commit b6cc128c5f

View File

@ -425,18 +425,63 @@ function federated_route_needs_provision_register(){
function federated_needs_provision_subscriptions(){ function federated_needs_provision_subscriptions(){
$args = array( $args = array(
'numberposts' => -1, 'numberposts' => -1,
'post_type' => 'shop_subscription', 'post_type' => 'shop_subscription',
'post_status' => 'wc-active', 'post_status' => 'wc-active',
'meta_key' => 'provision_status', 'meta_key' => 'provision_status',
'meta_value' => 'needs_provision' 'meta_value' => 'needs_provision'
); );
$posts = get_posts($args); $posts = get_posts($args);
return array_map('get_subscription_id', $posts); 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();
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 //Shared Functions
function get_subscription_id($subscription){ function get_subscription_id($subscription){
return $subscription->ID; return $subscription->ID;