| | |
| | | use Exception; |
| | | use JVBase\managers\queue\executors\InvitationExecutor; |
| | | use JVBase\managers\queue\TypeConfig; |
| | | use JVBase\registrar\Registrar; |
| | | use JVBase\utility\Features; |
| | | use WP_Error; |
| | | |
| | |
| | | protected array $inviteConfig; |
| | | protected CustomTable $table; |
| | | protected int $expiryDays = 14; |
| | | protected Cache $cache; |
| | | public function __construct() |
| | | { |
| | | $this->setInviteConfig(); |
| | | $this->cache = Cache::for('invitations'); |
| | | $this->table = CustomTable::for('invitations'); |
| | | add_action('init', [$this, 'registerInvitationExecutors'], 5); |
| | | |
| | |
| | | */ |
| | | protected function buildInviteTypes(): array |
| | | { |
| | | $types = []; |
| | | $invitable = $this->cache->remember( |
| | | 'invitableTypes', |
| | | function () { |
| | | $types = []; |
| | | |
| | | // Global invitations from JVB_MEMBERSHIP |
| | | if (!empty(JVB_MEMBERSHIP['can_invite'])) { |
| | | foreach (JVB_MEMBERSHIP['can_invite'] as $role => $canInvite) { |
| | | $types[$role] = [ |
| | | 'can_invite' => $canInvite, |
| | | 'to_terms' => [] |
| | | ]; |
| | | } |
| | | } |
| | | // Global invitations from JVB_MEMBERSHIP |
| | | if (!empty(JVB_MEMBERSHIP['can_invite'])) { |
| | | foreach (JVB_MEMBERSHIP['can_invite'] as $role => $canInvite) { |
| | | $types[$role] = [ |
| | | 'can_invite' => $canInvite, |
| | | 'to_terms' => [] |
| | | ]; |
| | | } |
| | | } |
| | | |
| | | // Term invitations from invitable content taxonomies |
| | | foreach (JVB_TAXONOMY as $taxonomy => $config) { |
| | | if (Features::forTaxonomy($taxonomy)->has('invitable') && |
| | | Features::forTaxonomy($taxonomy)->has('is_content') && |
| | | Features::forTaxonomy($taxonomy)->has('is_ownable')) { |
| | | // Term invitations from invitable content taxonomies |
| | | $invitable = Registrar::getFeatured('invitable', 'term'); |
| | | $content = Registrar::getFeatured('is_content', 'term'); |
| | | $ownable = Registrar::getFeatured('is_ownable', 'term'); |
| | | $taxonomies = array_intersect($invitable, $content, $ownable); |
| | | if (!empty($taxonomies)) { |
| | | $users = Registrar::getRegistered('user'); |
| | | } |
| | | foreach ($taxonomies as $taxonomy) { |
| | | $registrar = Registrar::getInstance($taxonomy); |
| | | |
| | | $forContent = $config['for_content'] ?? []; |
| | | foreach ($forContent as $content) { |
| | | // Find which user roles can create this content |
| | | foreach (JVB_USER as $role => $userConfig) { |
| | | $creatable = Features::forUser($role)->getCreatableContent(); |
| | | if (in_array($content, $creatable)) { |
| | | if (!isset($types[$role])) { |
| | | $types[$role] = [ |
| | | 'can_invite' => [], |
| | | 'to_terms' => [] |
| | | ]; |
| | | } |
| | | if (!in_array($taxonomy, $types[$role]['to_terms'])) { |
| | | $types[$role]['to_terms'][] = $taxonomy; |
| | | foreach ($registrar->registrar->for as $content) { |
| | | // Find which user roles can create this content |
| | | foreach ($users as $user) { |
| | | $userRegistrar = Registrar::getInstance($user); |
| | | |
| | | $creatable = $userRegistrar->getCreatable(); |
| | | if (in_array($content, $creatable)) { |
| | | if (!isset($types[$role])) { |
| | | $types[$role] = [ |
| | | 'can_invite' => [], |
| | | 'to_terms' => [] |
| | | ]; |
| | | } |
| | | if (!in_array($taxonomy, $types[$role]['to_terms'])) { |
| | | $types[$role]['to_terms'][] = $taxonomy; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | return $types; |
| | | return $types; |
| | | } |
| | | ); |
| | | return $invitable; |
| | | |
| | | } |
| | | |
| | | /****************************************************************** |
| | |
| | | |
| | | $term = get_term($termID, BASE . $taxonomy); |
| | | if ($term && !is_wp_error($term)) { |
| | | $config = JVB_TAXONOMY[$taxonomy] ?? []; |
| | | $singular = $config['singular'] ?? $taxonomy; |
| | | $registrar = Registrar::getInstance($taxonomy); |
| | | |
| | | $singular = $registrar ? $registrar->getSingular() : $taxonomy; |
| | | |
| | | $termContent[] = sprintf( |
| | | "<p>%s has also invited you to join %s. You'll be automatically added to this %s when you register.</p>", |