true, self::CAP => true, ) ); } } public static function is_crm_only( $user = null ) { $user = $user ? $user : wp_get_current_user(); return $user instanceof WP_User && in_array( self::ROLE, (array) $user->roles, true ) && ! $user->has_cap( 'edit_posts' ); } /** Nodebook users land in Nodebook after logging in. */ public static function login_redirect( $to, $requested, $user ) { if ( self::is_crm_only( $user ) && ( ! $requested || admin_url() === $requested || false !== strpos( (string) $requested, 'wp-admin/profile.php' ) || admin_url( 'index.php' ) === $requested ) ) { return admin_url( 'admin.php?page=nodebook-today' ); } return $to; } /** WooCommerce keeps users without edit_posts out of wp-admin; let Nodebook users in. */ public static function allow_admin( $prevent ) { return self::is_crm_only() ? false : $prevent; } /** Their menu: Nodebook and their profile. The empty WordPress dashboard goes. */ public static function trim_menu() { if ( self::is_crm_only() ) { remove_menu_page( 'index.php' ); } } /** * Roles that may be given access: staff roles that can at least edit content. * Customer, subscriber and similar roles are never eligible (they would see every contact). */ public static function eligible_roles() { $out = array(); foreach ( wp_roles()->roles as $role => $def ) { if ( 'administrator' === $role || self::ROLE === $role ) { continue; } $caps = isset( $def['capabilities'] ) ? (array) $def['capabilities'] : array(); if ( ! empty( $caps['edit_posts'] ) || ! empty( $caps['manage_woocommerce'] ) || ! empty( $caps['edit_shop_orders'] ) ) { $out[ $role ] = $def['name']; } } return apply_filters( 'nodebook_eligible_roles', $out ); } /** Roles allowed to use Nodebook. Administrators always are. */ public static function roles() { $roles = array_intersect( (array) Nodebook_Settings::get( 'team_roles' ), array_keys( self::eligible_roles() ) ); $roles[] = 'administrator'; $roles[] = self::ROLE; return array_values( array_unique( array_map( 'sanitize_key', $roles ) ) ); } /** * @param array $allcaps All caps of the user. * @param array $caps Required primitive caps. * @param array $args Request args. * @param WP_User $user User. */ public static function grant( $allcaps, $caps, $args, $user ) { $caps = (array) $caps; if ( ! is_object( $user ) || ! array_intersect( $caps, array( self::CAP, self::SEE_ALL, self::EXPORT, self::DELETE ) ) ) { return $allcaps; } $admin = ! empty( $allcaps['manage_options'] ); $access = $admin || ! empty( $allcaps[ self::CAP ] ) || array_intersect( (array) $user->roles, self::roles() ); if ( $access ) { $allcaps[ self::CAP ] = true; } // Administrators can do everything. Others per Settings → Team, unless a role editor gave the // capability to their role or to them directly (then it is already true in $allcaps). $rules = array( self::SEE_ALL => 'all' === Nodebook_Settings::get( 'visibility' ), self::EXPORT => 'all' === Nodebook_Settings::get( 'can_export' ), self::DELETE => 'all' === Nodebook_Settings::get( 'can_delete' ), ); foreach ( $rules as $cap => $everyone ) { if ( in_array( $cap, $caps, true ) && empty( $allcaps[ $cap ] ) && ( $admin || ( $access && $everyone ) ) ) { $allcaps[ $cap ] = true; } } return $allcaps; } /* ------------------------------------------------------------------ * * Who sees which contacts * ------------------------------------------------------------------ */ /** * The user (default: current) only sees their own contacts. False when nobody is logged in * (cron, form capture, webhooks): background work is never filtered. */ public static function restricted( $user_id = null ) { $user_id = null === $user_id ? get_current_user_id() : (int) $user_id; if ( ! $user_id ) { return false; } /** * Whether a user sees only their own contacts. * * @param bool $restricted Default from Settings → Team and the nodebook_see_all capability. * @param int $user_id User. */ return (bool) apply_filters( 'nodebook_restricted', ! user_can( $user_id, self::SEE_ALL ), $user_id ); } /** Restricted users also see unassigned contacts (new leads nobody picked up yet). */ public static function sees_unassigned() { return 'own' !== Nodebook_Settings::get( 'visibility' ); } /** * Values for the fixed visibility condition "( %d = 0 OR owner = %d OR ( %d = 1 AND owner = 0 ) )": * restricted (1/0), the user, and whether unassigned contacts count as theirs (1/0). * * @param int|null $user_id User; default the current user. * @return int[] */ public static function scope_args( $user_id = null ) { $user_id = null === $user_id ? get_current_user_id() : (int) $user_id; return array( self::restricted( $user_id ) ? 1 : 0, $user_id, self::sees_unassigned() ? 1 : 0 ); } /** * Limit a query to the contacts a restricted user may see, as a condition (see Nodebook_Sql): * "c.owner_id IN (0, 5)" or "c.owner_id = 5". Always true for people who see everyone. * * @param string $col Owner column, e.g. "c.owner_id". * @param int|null $user_id User; default the current user. * @return array{0:string,1:array} */ public static function scope_where( $col = 'c.owner_id', $user_id = null ) { $user_id = null === $user_id ? get_current_user_id() : (int) $user_id; if ( ! self::restricted( $user_id ) ) { return Nodebook_Sql::all(); } $c = Nodebook_Sql::col( $col ); return self::sees_unassigned() ? array( $c[0] . ' IN (0, %d)', array_merge( $c[1], array( $user_id ) ) ) : array( $c[0] . ' = %d', array_merge( $c[1], array( $user_id ) ) ); } /** Whether the current user may see (and work on) a contact. */ public static function can_see( $contact ) { if ( ! self::restricted() ) { return true; } $c = is_array( $contact ) ? $contact : Nodebook_Contacts::get( (int) $contact ); if ( ! $c ) { return true; // Nothing to protect; callers handle "not found". } $owner = (int) $c['owner_id']; return $owner === (int) get_current_user_id() || ( 0 === $owner && self::sees_unassigned() ); } public static function can_export() { return current_user_can( self::EXPORT ); } public static function can_delete() { return current_user_can( self::DELETE ); } /** * Whether Nodebook is used by a team (Settings → Team → Who uses Nodebook). Solo hides owners, * assignees and the Mine / Everyone switches; not set yet means: a team once a second person can use it. */ public static function is_team() { $size = (string) Nodebook_Settings::get( 'team_size' ); if ( 'solo' === $size ) { return self::restricted(); } return 'team' === $size || count( self::users() ) > 1; } /** Users who can own contacts: id => display name. */ public static function users() { return self::load_users()[0]; } /** Users who can own contacts: id => handle (user_nicename), as used for @mentions. */ public static function handles() { return self::load_users()[1]; } /** Names and handles of the team, from one query per request. */ private static function load_users() { static $cache = null; if ( null === $cache ) { $cache = array( array(), array() ); foreach ( get_users( array( 'role__in' => self::roles(), 'orderby' => 'display_name', 'fields' => array( 'ID', 'display_name', 'user_nicename' ), 'number' => 200, ) ) as $u ) { $cache[0][ (int) $u->ID ] = $u->display_name; $cache[1][ (int) $u->ID ] = isset( $u->user_nicename ) ? (string) $u->user_nicename : ''; } } return $cache; } public static function name( $user_id ) { $users = self::users(); return isset( $users[ (int) $user_id ] ) ? $users[ (int) $user_id ] : ''; } /** New contacts go to the default owner, if one is set. */ public static function assign_default( $contact_id ) { // Someone who only sees their own contacts keeps what they add (by hand, on import, with a deal). if ( is_admin() && self::restricted() ) { Nodebook_Contacts::update( $contact_id, array( 'owner_id' => get_current_user_id() ) ); return; } $owner = (int) Nodebook_Settings::get( 'default_owner' ); if ( $owner && get_userdata( $owner ) ) { Nodebook_Contacts::update( $contact_id, array( 'owner_id' => $owner ) ); } } /** Alerts about an owned contact go to its owner; everything else to the alert recipients. */ public static function recipients_for( array $c ) { $owner = ! empty( $c['owner_id'] ) ? get_userdata( (int) $c['owner_id'] ) : null; if ( $owner && is_email( $owner->user_email ) ) { return $owner->user_email; } return Nodebook_Alerts::recipients(); } }