-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbypass-drip-content-for-learndash.php
More file actions
215 lines (179 loc) · 6.1 KB
/
bypass-drip-content-for-learndash.php
File metadata and controls
215 lines (179 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Plugin Name: Bypass Drip Content for LearnDash
* Description: Adds a bypass drip content option to LearnDash lessons.
* Version: 1.0.0
* Author: Obi Juan
* Author URI: https://obijuan.dev
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: bypass-drip-content-ld
*/
// If this file is called directly, abort.
if (!defined('WPINC')) {
die;
}
class Bypass_Drip_Content_LearnDash {
/**
* @var null|Bypass_Drip_Content_LearnDash
*/
private static $instance = null;
/**
* @var Bypass_Drip_Content_Admin
*/
private $admin;
/**
* @var int
*/
private $lesson_id;
/**
* @var int
*/
private $user_id;
/**
* @var bool
*/
private $is_bypass_enabled = false;
/**
* @var array
*/
private $selected_users = array();
/**
* @var array
*/
private $selected_groups = array();
/**
* Get singleton instance
* @return Bypass_Drip_Content_LearnDash
*/
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->define_constants();
$this->load_dependencies();
$this->init_hooks();
}
private function is_bypass_enabled($lesson_id) {
// Get bypass settings
$bypass_enabled = get_post_meta($lesson_id, 'bypass_drip_content_enabled', true);
if ('on' !== $bypass_enabled) {
return false;
}
$this->is_bypass_enabled = true;
return true;
}
private function get_bypass_users($lesson_id) {
// Get selected users and groups with proper default values
$selected_users = get_post_meta($lesson_id, 'bypass_drip_content', true);
$selected_groups = get_post_meta($lesson_id, 'bypass_drip_content_groups', true);
// Ensure we have valid JSON data or set empty arrays as defaults
$selected_users = !empty($selected_users) ? json_decode($selected_users, true) : array();
$selected_groups = !empty($selected_groups) ? json_decode($selected_groups, true) : array();
// Ensure we always have arrays, even if json_decode fails
$selected_users = is_array($selected_users) ? $selected_users : array();
$selected_groups = is_array($selected_groups) ? $selected_groups : array();
$this->selected_users = $selected_users;
$this->selected_groups = $selected_groups;
return array($selected_users, $selected_groups);
}
/**
* Check if the user should bypass drip content restrictions
*
* @param int $user_id The user ID to check
* @return bool True if user should bypass restrictions, false otherwise
*/
private function should_bypass_restrictions($user_id) {
// If user is admin, don't modify access
if (current_user_can('manage_options')) {
return false;
}
$selected_users = $this->selected_users;
$selected_groups = $this->selected_groups;
$user_id = (string) $user_id;
// Check if user is in selected users
if (in_array($user_id, $selected_users)) {
return true;
}
// Check if user is in any selected groups
if (!empty($selected_groups)) {
foreach ($selected_groups as $group_id) {
if (learndash_is_user_in_group($user_id, $group_id)) {
return true;
}
}
}
return false;
}
/**
* Maybe bypass the lesson access timer
*
* @param int $lesson_access_from The timestamp when the lesson becomes available
* @param int $lesson_id The lesson post ID
* @param int $user_id The user ID
* @return int Modified access timestamp
*/
public function maybe_bypass_lesson_timer($lesson_access_from, $lesson_id, $user_id) {
$this->is_bypass_enabled($lesson_id);
$this->get_bypass_users($lesson_id);
if ($this->is_bypass_enabled && $this->should_bypass_restrictions($user_id)) {
return 0; // Set to 0 to make the lesson immediately available
}
return $lesson_access_from;
}
/**
* Define plugin constants
*/
private function define_constants() {
define('BDCLD_VERSION', '1.0.0');
define('BDCLD_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('BDCLD_PLUGIN_URL', plugin_dir_url(__FILE__));
}
/**
* Load required dependencies
*/
private function load_dependencies() {
require_once BDCLD_PLUGIN_DIR . 'includes/class-bypass-drip-content-admin.php';
$this->admin = new Bypass_Drip_Content_Admin();
}
/**
* Initialize hooks
*/
private function init_hooks() {
// Hook into LearnDash's drip content checks
add_filter('ld_lesson_access_from__visible_after', array($this, 'maybe_bypass_lesson_timer'), 10, 3);
add_filter('ld_lesson_access_from__visible_after_specific_date', array($this, 'maybe_bypass_lesson_timer'), 10, 3);
add_action('plugins_loaded', array($this, 'on_plugins_loaded'));
}
/**
* Runs on plugins loaded
*/
public function on_plugins_loaded() {
if (!class_exists('SFWD_LMS')) {
add_action('admin_notices', array($this, 'learndash_not_active_notice'));
return;
}
}
/**
* Display notice if LearnDash is not active
*/
public function learndash_not_active_notice() {
$message = sprintf(
__('Bypass Drip Content for LearnDash requires LearnDash LMS plugin to be installed and active. You can download %s here.', 'bypass-drip-content-ld'),
'<a href="https://www.learndash.com" target="_blank">LearnDash</a>'
);
echo '<div class="error"><p>' . $message . '</p></div>';
}
}
// Initialize the plugin
function bypass_drip_content_learndash() {
return Bypass_Drip_Content_LearnDash::get_instance();
}
// Start the plugin
bypass_drip_content_learndash();