Effective LearnDash
Create Wordpress users and read LearnDash course progress. This is an unofficial integration that is not supported or affiliated with WordPress or LearnDash.
Getting Started
This requires Rails 6+ and Twitter Bootstrap 4 and just works with Devise.
Please first install the effective_datatables gem.
Please download and install the Twitter Bootstrap4
Add to your Gemfile:
gem 'haml-rails' # or try using gem 'hamlit-rails'
gem 'effective_learndash'Run the bundle command to install it:
bundle installThen run the generator:
rails generate effective_learndash:installThe generator will install an initializer which describes all configuration options and creates a database migration.
If you want to tweak the table names, manually adjust both the configuration file and the migration now.
Then migrate the database:
rake db:migrateAdd to your user class:
class User < ApplicationRecord
effective_learndash_owner
end
Add a link to the admin menu:
- if can? :admin, :effective_learndash
= nav_dropdown 'Learndash' do
- if can? :index, Effective::LearndashUser
= nav_link_to 'Learndash Users', effective_learndash.admin_learndash_users_path
- if can? :index, Effective::LearndashCourse
= nav_link_to 'Learndash Courses', effective_learndash.admin_learndash_courses_path
- if can? :index, Effective::LearndashEnrollment
= nav_link_to 'Learndash Enrollments', effective_learndash.admin_learndash_enrollments_pathAuthorization
All authorization checks are handled via the effective_resources gem found in the config/initializers/effective_resources.rb file.
Permissions
The permissions you actually want to define are as follows (using CanCan):
can(:show, Effective::LearndashUser) { |lduser| lduser.owner_id == user.id }
can(:index, Effective::LearndashEnrollment)
can([:index, :show], Effective::LearndashCourse) { |course| !course.draft? }
can([:show, :index], Effective::CourseRegistrant) { |registrant| registrant.owner == user || registrant.owner.blank? }
can([:new, :create], EffectiveLearndash.CourseRegistration)
can([:show, :index], EffectiveLearndash.CourseRegistration) { |registration| registration.owner == user }
can([:update, :destroy], EffectiveLearndash.CourseRegistration) { |registration| registration.owner == user && !registration.was_submitted? }
if user.admin?
can :admin, :effective_learndash
can(crud + [:refresh], Effective::LearndashUser)
can(crud + [:refresh], Effective::LearndashCourse)
can(crud, Effective::LearndashEnrollment)
can(:refresh, Effective::LearndashEnrollment) { |enrollment| !enrollment.completed? }
endConfiguring LearnDash
Your WordPress should be configured ahead of time with the LearnDash plugin.
Please generate an application password via:
https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/
and fill in the username/password details in config/initializers/effective_learndash.rb
LearnDash API compatibility
The integration uses WordPress REST API v2 and LearnDash ldlms/v2 endpoints.
LearnDash 4 and 5 course-progress responses are supported: incoming underscore
statuses such as in_progress are stored using the existing in-progress format.
The date_started_gmt and date_completed_gmt fields are used when supplied;
older responses without those fields retain their previous UTC interpretation.
Both object and legacy array responses are supported for individual progress records.
No database migration is required.
Users, courses, user searches, and course-progress collections follow the
X-WP-TotalPages response header with up to 100 records per request. An individual
resource returning HTTP 404 is treated as missing; other unsuccessful GET requests
raise Effective::LearndashApi::Error, rather than triggering user or enrollment
creation after an authentication or server failure.
See the LearnDash 5 API changes and WordPress pagination reference.
Working with LearnDash
Visit /admin/learndash_courses and Refresh the list of Courses.
Create a New LearnDash User will create a new WordPress/LearnDash account with a username/password according to the settings in the config file.
When you create a user, you only get access to the password once. So any existing users will have an unknown password.
Create a new LearnDash Enrollment to enroll a LearnDash user into a course. This will begin tracking their progress.
There are no webhooks or callbacks from LearnDash, everything is a GET request that updates the local database.
You can refresh an entire LearnDash user in one operation and it will sync the entire user at once, user.learndash_user.refresh!
Delete User
If you need to delete a user, from wordpress:
- Top Left Corner -> My Sites -> Network Admin -> Users
License
MIT License. Copyright Code and Effect Inc.
Testing
rails testContributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Bonus points for test coverage
- Create new Pull Request