workflow = $workflow; } /** * Resolve recipients. * * @param string $notify Target: approver_roles|author|role:slug|user:email. * @param \WP_Post $post Post. * @param array $workflow Workflow definition. * @param string $new_status Status just entered. * @param int $actor_id User who triggered the transition. * @return \WP_User[] */ public function resolve( $notify, $post, array $workflow, $new_status, $actor_id ) { $users = array(); if ( 'approver_roles' === $notify ) { $step = $this->workflow->step( $workflow, $new_status ); $roles = ( $step && isset( $step['approver_roles'] ) ) ? $step['approver_roles'] : array(); $users = $this->users_with_roles( $roles ); // Anyone who approves on every step is an approver here too, otherwise a // site whose only approver is an administrator is never told anything. if ( $step ) { $users = array_merge( $users, $this->workflow->global_approver_users() ); } } elseif ( 'author' === $notify ) { $u = get_user_by( 'id', (int) $post->post_author ); if ( $u ) { $users = array( $u ); } } elseif ( 0 === strpos( $notify, 'role:' ) ) { $users = $this->users_with_roles( array( substr( $notify, 5 ) ) ); } elseif ( 0 === strpos( $notify, 'user:' ) ) { $u = get_user_by( 'email', substr( $notify, 5 ) ); if ( $u ) { $users = array( $u ); } } // Dedupe by ID and drop the actor (don't notify yourself). $out = array(); foreach ( $users as $u ) { if ( (int) $u->ID === (int) $actor_id ) { continue; } $out[ (int) $u->ID ] = $u; } return array_values( $out ); } /** * All users holding any of the given roles. * * @param array $roles Role slugs. * @return \WP_User[] */ private function users_with_roles( array $roles ) { $roles = array_filter( $roles ); if ( empty( $roles ) ) { return array(); } return get_users( array( 'role__in' => $roles ) ); } }