badgeos-learndash.php
9.5 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
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
<?php
/**
* Plugin Name: BadgeOS LearnDash Add-On
* Plugin URI: https://badgeos.org/downloads/learndash-add-on/
* Description: This BadgeOS add-on integrates BadgeOS features with LearnDash
* Tags: learndash, badgeos, badgeos-learndash-integration, learndash-gamification
* Author: BadgeOS
* Version: 1.2.5
* Author URI: https://badgeos.org/
* License: GNU AGPLv3
* License URI: http://www.gnu.org/licenses/agpl-3.0.html
*/
class BadgeOS_LearnDash {
/**
* Plugin Basename
*
* @var string
*/
public $basename = '';
/**
* Plugin Directory Path
*
* @var string
*/
public $directory_path = '';
/**
* Plugin Directory URL
*
* @var string
*/
public $directory_url = '';
/**
* BadgeOS LearnDash Triggers
*
* @var array
*/
public $triggers = array();
/**
* Actions to forward for splitting an action up
*
* @var array
*/
public $actions = array();
/**
* BadgeOS_LearnDash constructor.
*/
function __construct() {
// Define plugin constants
$this->basename = plugin_basename( __FILE__ );
$this->directory_path = plugin_dir_path( __FILE__ );
$this->directory_url = plugin_dir_url( __FILE__ );
// If BadgeOS is unavailable, deactivate our plugin
add_action( 'admin_notices', array( $this, 'maybe_disable_plugin' ) );
// LearnDash Action Hooks
$this->triggers = array(
'learndash_quiz_completed' => __( 'Passed Quiz', 'badgeos-learndash' ),
'badgeos_learndash_quiz_completed_tag' => __( 'Passed Quiz from a Tag', 'badgeos-learndash' ),
'badgeos_learndash_quiz_completed_specific' => __( 'Minimum % Grade on a Quiz', 'badgeos-learndash' ), // New Quiz Trigger
'badgeos_learndash_quiz_completed_percentage' => __( 'Passed Quiz with given % range', 'badgeos-learndash'),
'badgeos_learndash_quiz_completed_fail' => __( 'Fails Quiz', 'badgeos-learndash' ),
'badgeos_learndash_quiz_completed_fail_tag' => __( 'Fails Quiz from a Tag', 'badgeos-learndash' ),
'learndash_topic_completed' => __( 'Completed Topic', 'badgeos-learndash' ),
'badgeos_learndash_topic_completed_tag' => __( 'Completed Topic from a Tag', 'badgeos-learndash' ),
'learndash_lesson_completed' => __( 'Completed Lesson', 'badgeos-learndash' ),
'badgeos_learndash_lesson_completed_tag' => __( 'Completed Lesson from a Tag', 'badgeos-learndash' ),
'learndash_course_completed' => __( 'Completed Course', 'badgeos-learndash' ),
'badgeos_learndash_course_completed_tag' => __( 'Completed Course from a Tag', 'badgeos-learndash' ),
'badgeos_learndash_purchase_course' => __( 'On Purchase of Course(s)', 'badgeos-learndash' ),
'ld_added_group_access' => __( 'On Group Registration', 'badgeos-learndash' ),
// New Assignment Triggers
'badgeos_learndash_upload_an_assignment' => __( 'Upload an assignment', 'badgeos-learndash'),
'badgeos_learndash_upload_an_assignment_specific_lesson' => __( 'Upload an Assignment to a Specific Lesson', 'badgeos-learndash'),
'badgeos_learndash_upload_an_assignment_specific_course' => __( 'Upload an Assignment to a Specific Course', 'badgeos-learndash'),
'badgeos_learndash_approve_an_assignment' => __( 'Approve an Assignment', 'badgeos-learndash'),
'badgeos_learndash_approve_an_assignment_specific_lesson' => __( 'Approve an Assignment of a Specific Lesson', 'badgeos-learndash'),
'badgeos_learndash_approve_an_assignment_specific_course' => __( 'Approve an Assignment of a Specific Course', 'badgeos-learndash'),
);
// Actions that we need split up
$this->actions = array(
'learndash_course_completed' => 'badgeos_learndash_course_completed_tag',
'learndash_topic_completed' => 'badgeos_learndash_topic_completed_tag',
'learndash_lesson_completed' => 'badgeos_learndash_lesson_completed_tag',
'learndash_quiz_completed' => array(
'actions' => array(
'badgeos_learndash_quiz_completed_specific',
'badgeos_learndash_quiz_completed_fail',
'badgeos_learndash_quiz_completed_tag',
'badgeos_learndash_quiz_completed_fail_tag',
'badgeos_learndash_quiz_completed_percentage'
)
),
'learndash_assignment_uploaded' => array(
'actions' => array(
'badgeos_learndash_upload_an_assignment',
'badgeos_learndash_upload_an_assignment_specific_lesson',
'badgeos_learndash_upload_an_assignment_specific_course'
)
),
'learndash_assignment_approved' => array(
'actions' => array(
'badgeos_learndash_approve_an_assignment',
'badgeos_learndash_approve_an_assignment_specific_lesson',
'badgeos_learndash_approve_an_assignment_specific_course'
)
)
/*
* Default action split will be badgeos_learndash_{$action}, can set multiple actions with 'actions'
*
* 'original_action' => array(
* 'priority' => 12,
* 'accepted_args' => 5,
* 'actions' => array(
* 'another_action1'
* 'another_action2'
* 'another_action3'
* )
* )
*
*
* shorthand forwarding to a single action
*
* 'original_action' => 'another_action'
*/
);
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 11 );
}
/**
* Check if BadgeOS is available
*
* @since 1.0.0
* @return bool True if BadgeOS is available, false otherwise
*/
public static function meets_requirements() {
if ( !class_exists( 'BadgeOS' ) || !function_exists( 'badgeos_get_user_earned_achievement_types' ) ) {
return false;
}
elseif ( !class_exists( 'SFWD_LMS' ) ) {
return false;
}
return true;
}
/**
* Generate a custom error message and deactivates the plugin if we don't meet requirements
*
* @since 1.0.0
*/
public function maybe_disable_plugin() {
if ( !$this->meets_requirements() ) {
$badgeos_activated = class_exists( 'BadgeOS' ) && function_exists( 'badgeos_get_user_earned_achievement_types' );
$learndash_activated = class_exists( 'SFWD_LMS' );
if ( !$badgeos_activated || !$learndash_activated ) {
unset($_GET['activate']);
$message = __('<div id="message" class="error"><p><strong>BadgeOS LearnDash Add-On</strong> requires both <a href="%s" target="_blank">%s</a> and <a href="%s" target="_blank">%s</a> add-ons to be activated.</p></div>', 'badgeos-learndash');
echo sprintf($message,
'https://badgeos.org/',
'BadgeOS',
'https://www.learndash.com/',
'LearnDash'
);
}
// Deactivate our plugin
deactivate_plugins( $this->basename );
}
}
/**
* Load the plugin textdomain and include files if plugin meets requirements
*
* @since 1.0.0
*/
public function plugins_loaded() {
// Load translations
load_plugin_textdomain( 'badgeos-learndash', false, dirname( $this->basename ) . '/languages/' );
if ( $this->meets_requirements() ) {
require_once( $this->directory_path . '/includes/rules-engine.php' );
require_once( $this->directory_path . '/includes/steps-ui.php' );
$this->action_forwarding();
}
if( file_exists( $this->directory_path . '/includes/admin-settings.php' ) ) {
require_once( $this->directory_path . '/includes/admin-settings.php' );
}
}
/**
* Forward WP actions into a new set of actions
*
* @since 1.0.0
*/
public function action_forwarding() {
foreach ( $this->actions as $action => $args ) {
$priority = 10;
$accepted_args = 20;
if ( is_array( $args ) ) {
if ( isset( $args[ 'priority' ] ) ) {
$priority = $args[ 'priority' ];
}
if ( isset( $args[ 'accepted_args' ] ) ) {
$accepted_args = $args[ 'accepted_args' ];
}
}
add_action( $action, array( $this, 'action_forward' ), $priority, $accepted_args );
}
}
/**
* Forward a specific WP action into a new set of actions
*
* @return mixed Action return
*
* @since 1.0.0
*/
public function action_forward() {
$action = current_filter();
$args = func_get_args();
if ( isset( $this->actions[ $action ] ) ) {
if ( is_array( $this->actions[ $action ] )
&& isset( $this->actions[ $action ][ 'actions' ] ) && is_array( $this->actions[ $action ][ 'actions' ] )
&& !empty( $this->actions[ $action ][ 'actions' ] ) ) {
foreach ( $this->actions[ $action ][ 'actions' ] as $new_action ) {
if ( 0 !== strpos( $new_action, strtolower( __CLASS__ ) . '_' ) ) {
$new_action = strtolower( __CLASS__ ) . '_' . $new_action;
}
$action_args = $args;
array_unshift( $action_args, $new_action );
call_user_func_array( 'do_action', $action_args );
}
return null;
}
elseif ( is_string( $this->actions[ $action ] ) ) {
$action = $this->actions[ $action ];
}
}
if ( 0 !== strpos( $action, strtolower( __CLASS__ ) . '_' ) ) {
$action = strtolower( __CLASS__ ) . '_' . $action;
}
array_unshift( $args, $action );
return call_user_func_array( 'do_action', $args );
}
}
$GLOBALS[ 'badgeos_learndash' ] = new BadgeOS_LearnDash();