-
Notifications
You must be signed in to change notification settings - Fork 0
/
learndash-full-access-groups.php
348 lines (302 loc) · 9.73 KB
/
learndash-full-access-groups.php
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* Plugin Name: LearnDash Full Access Groups
* Plugin URI: https://bizbudding.com
* Description: Allow specific Groups to access a course without start date restrictions.
* Version: 1.3.0
*
* Author: BizBudding, Mike Hemberger
* Author URI: https://bizbudding.com
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Main LD_Full_Access_Groups Class.
*
* @since 1.0.0
*/
final class LD_Full_Access_Groups {
/**
* @var LD_Full_Access_Groups The one true LD_Full_Access_Groups
* @since 1.0.0
*/
private static $instance;
/**
* Main LD_Full_Access_Groups Instance.
*
* Insures that only one instance of LD_Full_Access_Groups exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 1.0.0
* @static var array $instance
* @uses LD_Full_Access_Groups::setup_constants() Setup the constants needed.
* @uses LD_Full_Access_Groups::run() Activate, deactivate, etc.
* @see LD_FULL_ACCESS_GROUPS()
* @return object | LD_Full_Access_Groups The one true LD_Full_Access_Groups
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
// Setup the setup
self::$instance = new LD_Full_Access_Groups;
// Methods
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->run();
}
return self::$instance;
}
/**
* Throw error on object clone.
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @since 1.0.0
* @access protected
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'learndash-full-access-groups' ), '1.0' );
}
/**
* Disable unserializing of the class.
*
* @since 1.0.0
* @access protected
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'learndash-full-access-groups' ), '1.0' );
}
/**
* Setup plugin constants.
*
* @access private
* @since 1.0.0
* @return void
*/
private function setup_constants() {
// Plugin version.
if ( ! defined( 'LD_FULL_ACCESS_GROUPS_VERSION' ) ) {
define( 'LD_FULL_ACCESS_GROUPS_VERSION', '1.3.0' );
}
// Plugin Folder Path.
if ( ! defined( 'LD_FULL_ACCESS_GROUPS_PLUGIN_DIR' ) ) {
define( 'LD_FULL_ACCESS_GROUPS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Includes Path
// if ( ! defined( 'LD_FULL_ACCESS_GROUPS_INCLUDES_DIR' ) ) {
// define( 'LD_FULL_ACCESS_GROUPS_INCLUDES_DIR', LD_FULL_ACCESS_GROUPS_PLUGIN_DIR . 'includes/' );
// }
// Plugin Folder URL.
if ( ! defined( 'LD_FULL_ACCESS_GROUPS_PLUGIN_URL' ) ) {
define( 'LD_FULL_ACCESS_GROUPS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'LD_FULL_ACCESS_GROUPS_PLUGIN_FILE' ) ) {
define( 'LD_FULL_ACCESS_GROUPS_PLUGIN_FILE', __FILE__ );
}
// Plugin Base Name
if ( ! defined( 'LD_FULL_ACCESS_GROUPS_BASENAME' ) ) {
define( 'LD_FULL_ACCESS_GROUPS_BASENAME', dirname( plugin_basename( __FILE__ ) ) );
}
}
/**
* Include required files.
*
* @access private
* @since 0.1.0
* @return void
*/
private function includes() {
// Include vendor libraries.
require_once __DIR__ . '/vendor/autoload.php';
}
/**
* Hook all the things.
*
* @return void
*/
public function run() {
add_action( 'admin_init', array( $this, 'updater' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'add_meta_boxes', array( $this, 'register_metabox' ) );
add_action( 'save_post_sfwd-courses', array( $this, 'save_values' ) );
add_action( 'get_header', array( $this, 'course_bypass' ) );
}
/**
* Setup the updater.
*
* composer require yahnis-elsts/plugin-update-checker
*
* @uses https://github.com/YahnisElsts/plugin-update-checker/
*
* @return void
*/
public function updater() {
// Bail if current user cannot manage plugins.
if ( ! current_user_can( 'install_plugins' ) ) {
return;
}
// Bail if plugin updater is not loaded.
if ( ! class_exists( 'Puc_v4_Factory' ) ) {
return;
}
// Setup the updater.
$updater = Puc_v4_Factory::buildUpdateChecker( 'https://github.com/bizbudding/learndash-full-access-groups/', __FILE__, 'learndash-full-access-groups' );
}
/**
* Register scripts/styles for enqueueing later.
*
* @return void
*/
public function register_scripts() {
wp_register_style( 'ldfag-select2', LD_FULL_ACCESS_GROUPS_PLUGIN_URL . 'assets/css/select2.min.css', array(), '4.0.13' );
wp_register_script( 'ldfag-select2', LD_FULL_ACCESS_GROUPS_PLUGIN_URL . 'assets/js/select2.min.js', array( 'jquery' ), '4.0.13', true );
wp_register_script( 'ld_full_access_groups', LD_FULL_ACCESS_GROUPS_PLUGIN_URL . 'assets/js/ld-full-access-groups.js', array( 'select2' ), LD_FULL_ACCESS_GROUPS_VERSION, true );
}
/**
* Register the metabox.
*
* @return void
*/
function register_metabox() {
add_meta_box(
'ld_full_access_groups_edit',
esc_html__( 'LearnDash Full Access Groups', 'learndash-full-access-groups' ),
array( $this, 'render_metabox' ),
'sfwd-courses',
'side',
'default'
);
}
/**
* Render the metabox HTML.
*
* @param $post The current post object.
*
* @return void
*/
function render_metabox( $post ) {
wp_enqueue_style( 'ldfag-select2' );
wp_enqueue_script( 'ldfag-select2' );
wp_enqueue_script( 'ld_full_access_groups' );
$groups = get_posts( array(
'post_type' => 'groups',
'posts_per_page' => 500,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'suppress_filters' => true
) );
if ( $groups ) {
// Set nonce.
wp_nonce_field( 'nonce_ld_full_access_groups_action', 'nonce_ld_full_access_groups_field' );
// Existing values.
$selected = (array) get_post_meta( $post->ID, 'ld_full_access_groups', true );
// Select field.
echo '<p><select id="ld_full_access_groups" class="ld-full-access-groups widefat" name="ld_full_access_groups[]" multiple="multiple">';
foreach ( $groups as $group ) {
printf( '<option value="%s" %s>%s</option>', $group->ID, selected( in_array( $group->ID, $selected ) ), get_the_title( $group->ID ) );
}
echo '</select></p>';
printf( '<em>%s</em>', esc_html__( 'Selected groups will have access to this course without start date restrictions. Course access/enrollment is managed by the group itself. Course prerequisites will not be affected.', 'learndash-full-access-groups' ) );
}
}
/**
* Save the selected groups to post meta.
*
* @param $post_id The current post ID.
*
* @return void
*/
function save_values( $post_id ) {
if ( ! isset( $_POST[ 'nonce_ld_full_access_groups_field' ] ) || ! wp_verify_nonce( $_POST[ 'nonce_ld_full_access_groups_field' ], 'nonce_ld_full_access_groups_action' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( is_multisite() && ms_is_switched() ) {
return;
}
// Get the groups array.
$groups = isset( $_POST['ld_full_access_groups'] ) ? (array) $_POST['ld_full_access_groups'] : '';
// Sanitize.
$groups = ! empty( $groups ) ? array_map( 'absint', $groups ) : '';
// Update the post meta.
update_post_meta( $post_id, 'ld_full_access_groups', $groups );
}
/**
* Remove the start date restrictions
* from courses when in a group selected via the metabox on the course edit page.
*
* @return void
*/
function course_bypass() {
// Bail if not a single learndash post.
if ( ! is_singular( array( 'sfwd-courses', 'sfwd-lessons', 'sfwd-quiz', 'sfwd-topic', 'sfwd-certificates' ) ) ) {
return;
}
// Bail if LD removes this function. Better to remove course access then to blow things up.
if ( ! function_exists( 'learndash_get_course_id' ) ) {
return;
}
// Determine type of ID is being passed in. Should be the ID of anything that belongs to a course (Lesson, Topic, Quiz, etc).
$course_id = learndash_get_course_id( get_the_ID() );
// Bail if no course ID.
if ( ! $course_id ) {
return;
}
// Saved groups.
$groups = get_post_meta( $course_id, 'ld_full_access_groups', true );
// Bail if not groups to override access.
if ( ! $groups ) {
return;
}
// Start with no access.
$access = false;
// If user is in any group that grants bypass access.
foreach ( (array) $groups as $group_id ) {
if ( learndash_is_user_in_group( get_current_user_id(), $group_id ) ) {
$access = true;
break;
}
}
// Bail if no access. Let LD do its' thing.
if ( ! $access ) {
return;
}
/**
* Allows user to bypass the page.
*
* @param boolean $can_bypass Whether the user can bypass $context.
* @param int $user_id User ID.
* @param string $context The specific action to check for.
* @param array $args Optional array of args related to the context. Typically starting with an step ID, Course ID, etc.
*
* @return bool
*/
add_filter( 'learndash_user_can_bypass', function( $can_bypass, $user_id, $context, $args ) {
return true;
}, 10, 4 );
}
}
/**
* The main function for that returns LD_Full_Access_Groups.
*
* The main function responsible for returning the one true LD_Full_Access_Groups
* Instance to functions everywhere.
*
* @return object|LD_Full_Access_Groups The one true LD_Full_Access_Groups Instance.
*/
function ld_full_access_groups() {
return LD_Full_Access_Groups::instance();
}
// Get LD_Full_Access_Groups Running.
ld_full_access_groups();