codelion

badgeos-learndash.php

Sep 9th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: BadgeOS LearnDash Add-On
  4. * Plugin URI: http://www.learndash.com/
  5. * Description: This BadgeOS add-on integrates BadgeOS features with LearnDash
  6. * Tags: learndash
  7. * Author: Credly
  8. * Version: 1.0.0
  9. * Author URI: https://credly.com/
  10. * License: GNU AGPLv3
  11. * License URI: http://www.gnu.org/licenses/agpl-3.0.html
  12. */
  13.  
  14. /*
  15. * Copyright © 2013 Credly, LLC
  16. *
  17. * This program is free software: you can redistribute it and/or modify it
  18. * under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  23. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
  24. * Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>;.
  28. */
  29.  
  30. class BadgeOS_LearnDash {
  31.  
  32. /**
  33. * Plugin Basename
  34. *
  35. * @var string
  36. */
  37. public $basename = '';
  38.  
  39. /**
  40. * Plugin Directory Path
  41. *
  42. * @var string
  43. */
  44. public $directory_path = '';
  45.  
  46. /**
  47. * Plugin Directory URL
  48. *
  49. * @var string
  50. */
  51. public $directory_url = '';
  52.  
  53. /**
  54. * BadgeOS LearnDash Triggers
  55. *
  56. * @var array
  57. */
  58. public $triggers = array();
  59.  
  60. /**
  61. * Actions to forward for splitting an action up
  62. *
  63. * @var array
  64. */
  65. public $actions = array();
  66.  
  67. /**
  68. *
  69. */
  70. function __construct() {
  71.  
  72. // Define plugin constants
  73. $this->basename = plugin_basename( __FILE__ );
  74. $this->directory_path = plugin_dir_path( __FILE__ );
  75. $this->directory_url = plugin_dir_url( __FILE__ );
  76.  
  77. // If BadgeOS is unavailable, deactivate our plugin
  78. add_action( 'admin_notices', array( $this, 'maybe_disable_plugin' ) );
  79.  
  80. // LearnDash Action Hooks
  81. $this->triggers = array(
  82. 'learndash_quiz_completed' => __( 'Passed Quiz', 'badgeos-learndash' ),
  83. 'badgeos_learndash_quiz_completed_specific' => __( 'Minimum % Grade on a Quiz', 'badgeos-learndash' ),
  84. 'badgeos_learndash_quiz_completed_fail' => __( 'Fails Quiz', 'badgeos-learndash' ),
  85. 'learndash_topic_completed' => __( 'Completed Topic', 'badgeos-learndash' ),
  86. 'learndash_lesson_completed' => __( 'Completed Lesson', 'badgeos-learndash' ),
  87. 'learndash_course_completed' => __( 'Completed Course', 'badgeos-learndash' ),
  88. 'badgeos_learndash_course_completed_tag' => __( 'Completed Course from a Tag', 'badgeos-learndash' )
  89. );
  90.  
  91. // Actions that we need split up
  92. $this->actions = array(
  93. 'learndash_course_completed' => 'badgeos_learndash_course_completed_tag',
  94. 'learndash_quiz_completed' => array(
  95. 'actions' => array(
  96. 'badgeos_learndash_quiz_completed_specific',
  97. 'badgeos_learndash_quiz_completed_fail'
  98. )
  99. )
  100.  
  101. /*
  102. * Default action split will be badgeos_learndash_{$action}, can set multiple actions with 'actions'
  103. *
  104. * 'original_action' => array(
  105. * 'priority' => 12,
  106. * 'accepted_args' => 5,
  107. * 'actions' => array(
  108. * 'another_action1'
  109. * 'another_action2'
  110. * 'another_action3'
  111. * )
  112. * )
  113. *
  114. *
  115. * shorthand forwarding to a single action
  116. *
  117. * 'original_action' => 'another_action'
  118. */
  119. );
  120.  
  121. add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 11 );
  122.  
  123. }
  124.  
  125. /**
  126. * Check if BadgeOS is available
  127. *
  128. * @since 1.0.0
  129. * @return bool True if BadgeOS is available, false otherwise
  130. */
  131. public static function meets_requirements() {
  132.  
  133. if ( !class_exists( 'BadgeOS' ) || !function_exists( 'badgeos_get_user_earned_achievement_types' ) ) {
  134. return false;
  135. }
  136. elseif ( !class_exists( 'SFWD_LMS' ) ) {
  137. return false;
  138. }
  139.  
  140. return true;
  141.  
  142. }
  143.  
  144. /**
  145. * Generate a custom error message and deactivates the plugin if we don't meet requirements
  146. *
  147. * @since 1.0.0
  148. */
  149. public function maybe_disable_plugin() {
  150.  
  151. if ( !$this->meets_requirements() ) {
  152. // Display our error
  153. echo '<div id="message" class="error">';
  154.  
  155. if ( !class_exists( 'BadgeOS' ) || !function_exists( 'badgeos_get_user_earned_achievement_types' ) ) {
  156. echo '<p>' . sprintf( __( 'BadgeOS LearnDash Add-On requires BadgeOS and has been <a href="%s">deactivated</a>. Please install and activate BadgeOS and then reactivate this plugin.', 'badgeos-learndash' ), admin_url( 'plugins.php' ) ) . '</p>';
  157. }
  158.  
  159. if ( !class_exists( 'SFWD_LMS' ) ) {
  160. echo '<p>' . sprintf( __( 'BadgeOS LearnDash Add-On requires LearnDash and has been <a href="%s">deactivated</a>. Please install and activate LearnDash and then reactivate this plugin.', 'badgeos-learndash' ), admin_url( 'plugins.php' ) ) . '</p>';
  161. }
  162.  
  163. echo '</div>';
  164.  
  165. // Deactivate our plugin
  166. deactivate_plugins( $this->basename );
  167. }
  168. }
  169.  
  170. /**
  171. * Load the plugin textdomain and include files if plugin meets requirements
  172. *
  173. * @since 1.0.0
  174. */
  175. public function plugins_loaded() {
  176. // Load translations
  177. load_plugin_textdomain( 'badgeos-learndash', false, dirname( $this->basename ) . '/languages/' );
  178.  
  179. if ( $this->meets_requirements() ) {
  180. require_once( $this->directory_path . '/includes/rules-engine.php' );
  181. require_once( $this->directory_path . '/includes/steps-ui.php' );
  182.  
  183. $this->action_forwarding();
  184. }
  185. }
  186.  
  187. /**
  188. * Forward WP actions into a new set of actions
  189. *
  190. * @since 1.0.0
  191. */
  192. public function action_forwarding() {
  193. foreach ( $this->actions as $action => $args ) {
  194. $priority = 10;
  195. $accepted_args = 20;
  196.  
  197. if ( is_array( $args ) ) {
  198. if ( isset( $args[ 'priority' ] ) ) {
  199. $priority = $args[ 'priority' ];
  200. }
  201.  
  202. if ( isset( $args[ 'accepted_args' ] ) ) {
  203. $accepted_args = $args[ 'accepted_args' ];
  204. }
  205. }
  206.  
  207. add_action( $action, array( $this, 'action_forward' ), $priority, $accepted_args );
  208. }
  209. }
  210.  
  211. /**
  212. * Forward a specific WP action into a new set of actions
  213. *
  214. * @return mixed Action return
  215. *
  216. * @since 1.0.0
  217. */
  218. public function action_forward() {
  219. $action = current_filter();
  220. $args = func_get_args();
  221.  
  222. if ( isset( $this->actions[ $action ] ) ) {
  223. if ( is_array( $this->actions[ $action ] )
  224. && isset( $this->actions[ $action ][ 'actions' ] ) && is_array( $this->actions[ $action ][ 'actions' ] )
  225. && !empty( $this->actions[ $action ][ 'actions' ] ) ) {
  226. foreach ( $this->actions[ $action ][ 'actions' ] as $new_action ) {
  227. if ( 0 !== strpos( $new_action, strtolower( __CLASS__ ) . '_' ) ) {
  228. $new_action = strtolower( __CLASS__ ) . '_' . $new_action;
  229. }
  230.  
  231. $action_args = $args;
  232.  
  233. array_unshift( $action_args, $new_action );
  234.  
  235. call_user_func_array( 'do_action', $action_args );
  236. }
  237.  
  238. return null;
  239. }
  240. elseif ( is_string( $this->actions[ $action ] ) ) {
  241. $action = $this->actions[ $action ];
  242. }
  243. }
  244.  
  245. if ( 0 !== strpos( $action, strtolower( __CLASS__ ) . '_' ) ) {
  246. $action = strtolower( __CLASS__ ) . '_' . $action;
  247. }
  248.  
  249. array_unshift( $args, $action );
  250.  
  251. return call_user_func_array( 'do_action', $args );
  252. }
  253.  
  254. }
  255.  
  256. $GLOBALS[ 'badgeos_learndash' ] = new BadgeOS_LearnDash();
Add Comment
Please, Sign In to add comment