roles; foreach ( $editable_roles as $role => $details ) { if ( $this->isPrivilegedRole( $role ) ) { continue; // never offer admin-equivalent roles. } $sub['id'] = esc_attr( $role ); $sub['label'] = translate_user_role( $details['name'] ); $roles[ $role ] = $sub; } return $roles; } /** * Get the individual item. * * @param string $role The item role. * * @return array The item for the integration, or an empty array if the role is missing or admin-equivalent. */ public function getItem( $role ) { $names = wp_roles()->get_names(); if ( ! isset( $names[ $role ] ) || $this->isPrivilegedRole( $role ) ) { return []; } return [ 'id' => $role, 'label' => $names[ $role ], ]; } /** * Only roles this integration offers can be saved. * * @param string $id The role slug. * * @return bool */ public function isValidItem( $id ): bool { return isset( $this->getItems()[ $id ] ); } /** * Whether the role grants any admin-equivalent capability. * * @param string $role The role slug. * * @return bool */ private function isPrivilegedRole( $role ): bool { $role_object = get_role( $role ); if ( ! $role_object ) { return false; } $enabled = array_keys( array_filter( (array) $role_object->capabilities ) ); return ! empty( array_intersect( $enabled, self::PRIVILEGED_CAPS ) ); } /** * Add the role when the purchase is created. * * @param \SureCart\Models\Integration $integration The integrations. * @param \WP_User $wp_user The user. * * @return boolean|void Returns true if the user course access updation was successful otherwise false. */ public function onPurchaseCreated( $integration, $wp_user ) { $this->toggleRole( $integration->integration_id, $wp_user, true ); } /** * Add the role when the purchase is invoked * * @param \SureCart\Models\Integration $integration The integrations. * @param \WP_User $wp_user The user. * * @return boolean|void Returns true if the user course access updation was successful otherwise false. */ public function onPurchaseInvoked( $integration, $wp_user ) { $this->onPurchaseCreated( $integration, $wp_user ); } /** * Remove a user role when the purchase is revoked. * * @param \SureCart\Models\Integration $integration The integrations. * @param \WP_User $wp_user The user. * * @return boolean|void Returns true if the user course access updation was successful otherwise false. */ public function onPurchaseRevoked( $integration, $wp_user ) { $this->toggleRole( $integration->integration_id, $wp_user, false ); } /** * Toggle the role * * @param string $role The role. * @param \WP_User $wp_user The user object. * @param boolean $add True to add the role, false to remove. * * @return \WP_Role|false */ public function toggleRole( $role, $wp_user, $add = true ) { // make sure the role exists. $role_object = get_role( $role ); if ( ! $role_object ) { return; } // only ever add roles this integration offers — a stored admin-equivalent // role must stay inert. Removal is still allowed (revokes are safe). if ( $add && ! isset( $this->getItems()[ $role ] ) ) { return; } // add or remove the role. return $add ? $wp_user->add_role( $role ) : $wp_user->remove_role( $role ); } }