I want to modify the publishorpend function below to consider the user group of the currently logged in user when deciding whether to set the post status as "pending" or "publish" (in addition to considering whether submission_requires_approval is true or false). For example:
- If submission_requires_approval is false, post_status should be set to publish no matter what the user role is (this is how the function works currently).
- if current user is a member of administrators or editors, the post status should be set to publish. note, the current user may have more than one role assigned.
- If submission_requires_approval is true and if the current user is a group other than admin/editor (or is not logged in at all*) post_status should be set to pending. *this would be ideal but not absolutely necessary
public function publishorpend( $listing_id ) {
$listing = \MyListing\Src\Listing::force_get( $listing_id );
$post_status = mylisting_get_setting( 'submission_requires_approval' ) ? 'pending' : 'publish';
wp_update_post( [
'ID' => $listing->get_id(),
'post_status' => $post_status,
] );
}
I found this function which may be helpful:
function wcmo_get_current_user_roles() {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
return $roles; // This returns an array
// Use this to return a single value
// return $roles[0];
} else {
return array();
}
}
Thanks!