I have the following piece of code which publishes a user's post as long as they have purchased a subscription package and it changes the status of the post to draft as soon as their package has expired. I am just wondering how do I edit it so that the user can create a post and the post is automatically set to draft and then the status of the post is updated to published as soon as the user has completed the payment for a package? Any help would be greatly appreciated.
if ( ! in_array( $new_status, array( 'active', 'trial' ) ) ) {
$user_limits = stm_get_post_limits( $user_id );
$posts_args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'listings',
'post_status' => 'publish',
'posts_per_page' => - 1,
'meta_query' => array(
array(
'key' => 'stm_car_user',
'value' => $user_id,
'compare' => ''
)
),
'fields' => 'ids'
);
$user_posts = get_posts( $posts_args );
if ( count( $user_posts ) > $user_limits['posts_allowed'] ) {
array_splice( $user_posts, 0, $user_limits['posts_allowed'] );
foreach ( $user_posts as $user_post ) {
$draft_post = array(
'ID' => $user_post,
'post_status' => 'draft'
);
wp_update_post( $draft_post );
}
}
/*Change user back to private if not admin*/
if(!user_can($user_id, 'manage_options')) {
wp_update_user( array(
'ID' => $user_id,
'role' => 'privateseller'
) );
}
} else {
/*If plan includes dealeship, change user role to dealer*/
if ( $role == 'dealer' ) {
wp_update_user( array(
'ID' => $user_id,
'role' => 'stm_dealer'
) );
}
}