| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: LearnPress bbPress Course Forum |
| 4 |
Plugin URI: http://thimpress.com/learnpress |
| 5 |
Description: Using the forum for courses provided by bbPress |
| 6 |
Author: ThimPress |
| 7 |
Version: 1.2 |
| 8 |
Author URI: http://thimpress.com |
| 9 |
Tags: learnpress, lms |
| 10 |
Text Domain: learnpress-bbpress |
| 11 |
Domain Path: /languages/ |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( !defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly |
| 16 |
} |
| 17 |
|
| 18 |
define( 'LP_ADDON_BBPRESS_FILE', __FILE__ ); |
| 19 |
define( 'LP_ADDON_BBPRESS_PATH', dirname( __FILE__ ) ); |
| 20 |
|
| 21 |
/** |
| 22 |
* Class LP_Addon_BBPress_Forum |
| 23 |
*/ |
| 24 |
class LP_Addon_BBPress_Course_Forum { |
| 25 |
|
| 26 |
/** |
| 27 |
* @var null |
| 28 |
*/ |
| 29 |
protected static $_instance = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var bool |
| 33 |
*/ |
| 34 |
protected $_start_forum = false; |
| 35 |
|
| 36 |
/** |
| 37 |
* LP_Addon_BBPress_Course_Forum constructor. |
| 38 |
*/ |
| 39 |
function __construct() { |
| 40 |
if ( self::bbpress_is_active() ) { |
| 41 |
$this->_init_hooks(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Init hooks |
| 47 |
*/ |
| 48 |
private function _init_hooks() { |
| 49 |
|
| 50 |
add_action( 'add_meta_boxes', array( $this, 'meta_box' ) ); |
| 51 |
//add_action( 'post_comment_status_meta_box-options', array( $this, 'meta_box_options' ) ); |
| 52 |
add_action( 'save_post', array( $this, 'save_post' ) ); |
| 53 |
add_action( 'before_delete_post', array( $this, 'remove_forum' ) ); |
| 54 |
|
| 55 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 56 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 57 |
add_action( 'init', array( __CLASS__, 'load_text_domain' ) ); |
| 58 |
|
| 59 |
// |
| 60 |
add_action( 'bbp_template_before_single_topic', array( $this, 'before_single_forum' ) ); |
| 61 |
add_action( 'bbp_template_before_single_forum', array( $this, 'before_single_forum' ) ); |
| 62 |
add_action( 'bbp_template_after_single_topic', array( $this, 'after_single_forum' ) ); |
| 63 |
add_action( 'bbp_template_after_single_forum', array( $this, 'after_single_forum' ) ); |
| 64 |
|
| 65 |
add_action( 'learn_press_after_single_course_summary', array( $this, 'forum_link' ) ); |
| 66 |
} |
| 67 |
|
| 68 |
function remove_forum( $course_id ) { |
| 69 |
$forum_id = get_post_meta( $course_id, '_lp_course_forum', true ); |
| 70 |
if ( !$forum_id ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
remove_action( 'before_delete_post', array( $this, 'remove_forum' ) ); |
| 74 |
wp_delete_post( $forum_id ); |
| 75 |
} |
| 76 |
|
| 77 |
function meta_box() { |
| 78 |
add_meta_box( 'course-forum-id', __( 'Course Forum', 'learnpress-bbpress' ), array( $this, 'meta_box_options' ), 'lp_course', 'advanced', 'high' ); |
| 79 |
} |
| 80 |
|
| 81 |
function forum_link() { |
| 82 |
$course = LP()->course; |
| 83 |
$user = LP()->user; |
| 84 |
if ( !$course ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
$course_forum = $course->course_forum; |
| 88 |
if ( !$course_forum ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
$post_forum = get_post( $course_forum ); |
| 92 |
if ( !$post_forum || !in_array( $post_forum->post_type, array( 'topic', 'forum' ) ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
if ( !$this->can_access_forum( $post_forum->ID, $post_forum->post_type ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
global $post; |
| 99 |
$post = $post_forum; |
| 100 |
setup_postdata( $post ); |
| 101 |
learn_press_get_template( 'forum-link.php', null, learn_press_template_path() . '/addons/bbpress/', LP_ADDON_BBPRESS_PATH . '/templates/' ); |
| 102 |
wp_reset_postdata(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Process limit access forum |
| 107 |
*/ |
| 108 |
function before_single_forum() { |
| 109 |
global $post; |
| 110 |
if ( !$this->can_access_forum( $post->ID, $post->post_type ) ) { |
| 111 |
$this->_start_forum = true; |
| 112 |
ob_start(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Restrict forum content |
| 118 |
*/ |
| 119 |
function after_single_forum() { |
| 120 |
global $post; |
| 121 |
if ( $this->_start_forum ) { |
| 122 |
ob_end_clean(); |
| 123 |
//wp_enqueue_style( 'lp-dashicons', get_site_url() . '/wp-includes/css/dashicons.css' ); |
| 124 |
echo '<div id="restrict-access-form-message">'; |
| 125 |
echo '<p>' . __( 'You have to enroll the respective course!', 'learnpress-bbpress' ) . '</p>'; |
| 126 |
if ( $course_id = $this->get_forum_course( $post->ID ) ) { |
| 127 |
echo '<p>' . sprintf( __( 'Go back to %s', 'learnpress-bbpress' ), '<a href="' . get_permalink( $course_id ) . '"> ' . get_the_title( $course_id ) . '!</a>' ) . '</p>'; |
| 128 |
} |
| 129 |
echo '</div>'; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
function enqueue_assets() { |
| 134 |
wp_enqueue_style( 'bbpress-admin', plugins_url( '/', LP_ADDON_BBPRESS_FILE ) . 'assets/style.css' ); |
| 135 |
} |
| 136 |
|
| 137 |
function bbpress_require( $file ) { |
| 138 |
if ( !function_exists( ABSPATH . "wp-content/plugins/buddypress/{$file}" ) ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
require_once ABSPATH . "wp-content/plugins/buddypress/{$file}"; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Return true if a forum is public |
| 146 |
* |
| 147 |
* @param $forum_id |
| 148 |
* |
| 149 |
* @return bool |
| 150 |
*/ |
| 151 |
function is_public_forum( $forum_id ) { |
| 152 |
$restrict = get_post_meta( $forum_id, '_lp_bbpress_forum_enrolled_user', true ); |
| 153 |
if ( is_null( $restrict ) || ( $restrict === false ) || ( $restrict == '' ) ) { |
| 154 |
$restrict = false; |
| 155 |
} else { |
| 156 |
$restrict = true; |
| 157 |
} |
| 158 |
return !$restrict; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Add new settings to meta box of course |
| 163 |
* |
| 164 |
* @param $post |
| 165 |
*/ |
| 166 |
function meta_box_options( $post ) { |
| 167 |
if ( get_post_type() != 'lp_course' ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
$enable_forum = get_post_meta( $post->ID, '_lp_bbpress_forum_enable', true ); |
| 171 |
|
| 172 |
if ( is_null( $enable_forum ) || ( $enable_forum === false ) || $enable_forum == '' ) { |
| 173 |
$enable_forum = 'no'; |
| 174 |
} else { |
| 175 |
$enable_forum = 'yes'; |
| 176 |
} |
| 177 |
|
| 178 |
$restrict = $this->is_public_forum( $post->ID ) ? 'no' : 'yes'; |
| 179 |
|
| 180 |
$this->bbpress_require( "bp-forums/bbpress/bb-includes/functions.bb-forums.php" ); |
| 181 |
$this->bbpress_require( "bp-forums/bbpress/bb-includes/functions.bb-template.php" ); |
| 182 |
|
| 183 |
$forum_id = get_post_meta( $post->ID, '_lp_course_forum', true ); |
| 184 |
if ( !$forum_id ) { |
| 185 |
$forum_title = get_the_title( $post->ID ) . __( ' Forum', 'learnpress-bbpress' ); |
| 186 |
} else { |
| 187 |
$forum_title = ''; |
| 188 |
} |
| 189 |
?> |
| 190 |
<br /> |
| 191 |
<label for="bbpress_forum_enable" class="selectit"><input name="bbpress_forum_enable" type="checkbox" id="bbpress_forum_enable" value="yes" <?php checked( $enable_forum, 'yes' ); ?> /> <?php _e( 'Enable bbPress forum for this course.', 'learnpress-bbpress' ); ?> |
| 192 |
</label> |
| 193 |
<div id="bbpress-forum-settings" class="<?php echo $enable_forum != 'yes' ? 'hide-if-js' : ''; ?>"> |
| 194 |
<?php /* |
| 195 |
<p id="bbpress-forum-select"> |
| 196 |
<?php bbp_dropdown( array( |
| 197 |
'post_type' => bbp_get_forum_post_type(), |
| 198 |
'selected' => get_post_meta( $post->ID, '_lp_course_forum', true ), |
| 199 |
'numberposts' => - 1, |
| 200 |
'orderby' => 'title', |
| 201 |
'order' => 'ASC', |
| 202 |
'walker' => '', |
| 203 |
'exclude' => '', |
| 204 |
|
| 205 |
// Output-related |
| 206 |
'select_id' => '_lp_course_forum', |
| 207 |
'tab' => bbp_get_tab_index(), |
| 208 |
'options_only' => false, |
| 209 |
'show_none' => __( '--- Select existing forum or create a new one ---', 'learnpress-bbpress' ), |
| 210 |
'disable_categories' => false, |
| 211 |
'disabled' => '' |
| 212 |
) ); ?> |
| 213 |
<input type="text" value="<?php echo $forum_title; ?>" id="bbpress_forum_create_new" name="bbpress_forum_create_new" placeholder="<?php _e( 'Enter new forum name', 'learnpress-bbpress' ); ?>" /> |
| 214 |
</p>*/ ?> |
| 215 |
<?php if ( $forum_id ): ?> |
| 216 |
<p> |
| 217 |
<strong><?php _e( 'Course forum: ', 'learnpress-bbpress' ); ?></strong><a href="<?php echo get_the_permalink( $forum_id ); ?>"><?php echo get_the_title( $forum_id ); ?></a> |
| 218 |
</p> |
| 219 |
<?php else: ?> |
| 220 |
<p> |
| 221 |
<?php _e( 'Save course to create forum', 'learnpress-bbpress' ); ?> |
| 222 |
</p> |
| 223 |
<?php endif; ?> |
| 224 |
<p> |
| 225 |
<label for="bbpress_forum_enrolled_user" class="selectit"> |
| 226 |
<input name="bbpress_forum_enrolled_user" type="checkbox" id="bbpress_forum_enrolled_user" value="yes" <?php checked( $restrict, 'yes' ); ?> /> |
| 227 |
<?php _e( 'Only user enrolled course can access this forum.', 'learnpress-bbpress' ); ?> |
| 228 |
</label> |
| 229 |
</p> |
| 230 |
</div> |
| 231 |
<script type="text/javascript"> |
| 232 |
jQuery(function ($) { |
| 233 |
$('#bbpress_forum_enable').change(function () { |
| 234 |
$('#bbpress-forum-settings').toggleClass('hide-if-js', !this.checked); |
| 235 |
}); |
| 236 |
|
| 237 |
$('#_lp_course_forum').change(function () { |
| 238 |
$('#bbpress_forum_create_new').prop('disabled', this.value != ''); |
| 239 |
}).trigger('change'); |
| 240 |
}); |
| 241 |
</script> |
| 242 |
<?php |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Process ability to access forum |
| 247 |
* |
| 248 |
* @param $id |
| 249 |
* @param $type |
| 250 |
* |
| 251 |
* @return bool |
| 252 |
*/ |
| 253 |
function can_access_forum( $id, $type ) { |
| 254 |
|
| 255 |
// Case: user is Admin, Moderator or Key Master |
| 256 |
|
| 257 |
if ( current_user_can( 'manage_options' ) || current_user_can( 'bbp_moderator' ) || current_user_can( 'bbp_keymaster' ) ) { |
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
// Case: invalid forum |
| 262 |
if ( !$id ) { |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
$course_id = 0; |
| 267 |
$forum_id = $id; |
| 268 |
if ( $type == 'forum' ) { |
| 269 |
$course_id = $this->get_forum_course( $id ); |
| 270 |
} elseif ( $type == 'topic' ) { |
| 271 |
$forum_id = get_post_meta( $id, '_bbp_forum_id', true ); |
| 272 |
if ( !empty( $forum_id ) ) { |
| 273 |
$course_id = $this->get_forum_course( $forum_id ); |
| 274 |
} |
| 275 |
} |
| 276 |
// Case: a normal forum which has no connecting with any courses |
| 277 |
if ( !$course_id || $this->is_public_forum( $course_id ) ) { |
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
$course = LP_Course::get_course( $course_id ); |
| 282 |
|
| 283 |
$is_required = $course->is_require_enrollment(); |
| 284 |
// Case: no required course |
| 285 |
if ( !$is_required ) { |
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
$user = learn_press_get_current_user(); |
| 290 |
// Case: invalid user or post |
| 291 |
if ( !$user->id ) { |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
// Case: user is the course author |
| 296 |
$object = get_post( $course_id ); |
| 297 |
if ( $user->id == $object->post_author ) { |
| 298 |
return true; |
| 299 |
} |
| 300 |
|
| 301 |
// Case: users haven't enrolled any one |
| 302 |
if ( $user->has_enrolled_course( $course_id ) ) { |
| 303 |
return true; |
| 304 |
} |
| 305 |
|
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
function get_forum_course( $forum_id ) { |
| 310 |
global $wpdb; |
| 311 |
static $forum_courses = array(); |
| 312 |
if ( empty( $forum_courses[$forum_id] ) ) { |
| 313 |
if ( get_post_type( $forum_id ) == 'topic' ) { |
| 314 |
$forum_id = get_post_meta( $forum_id, '_bbp_forum_id', true ); |
| 315 |
} |
| 316 |
if ( $forum_id ) { |
| 317 |
$query = $wpdb->prepare( " |
| 318 |
SELECT c.ID |
| 319 |
FROM {$wpdb->posts} c |
| 320 |
INNER JOIN {$wpdb->postmeta} cm ON cm.post_id = c.ID AND cm.meta_key = %s AND cm.meta_value = %d |
| 321 |
INNER JOIN {$wpdb->posts} f ON f.ID = cm.meta_value |
| 322 |
WHERE f.ID = %d |
| 323 |
", '_lp_course_forum', $forum_id, $forum_id ); |
| 324 |
$forum_courses[$forum_id] = $wpdb->get_var( $query ); |
| 325 |
} |
| 326 |
} |
| 327 |
return !empty( $forum_courses[$forum_id] ) ? $forum_courses[$forum_id] : 0; |
| 328 |
} |
| 329 |
|
| 330 |
function save_post( $post_id ) { |
| 331 |
if ( get_post_type() != 'lp_course' || wp_is_post_revision( $post_id ) ) { |
| 332 |
return; |
| 333 |
} |
| 334 |
remove_action( 'save_post', array( $this, 'save_post' ) ); |
| 335 |
$course = get_post( $post_id ); |
| 336 |
if ( !empty( $_REQUEST['bbpress_forum_enable'] ) ) { |
| 337 |
update_post_meta( $post_id, '_lp_bbpress_forum_enable', 'yes' ); |
| 338 |
$forum_id = get_post_meta( $post_id, '_lp_course_forum', true ); |
| 339 |
if ( !$forum_id ) { |
| 340 |
$new_forum = array( |
| 341 |
'post_title' => $course->post_title, |
| 342 |
'post_content' => __( 'Forum of course "' . $course->post_title . '"', 'learnpress-bbpress' ), |
| 343 |
'post_author' => $course->post_author, |
| 344 |
); |
| 345 |
remove_action( 'save_post', array( $this, 'save_post' ) ); |
| 346 |
$forum_id = bbp_insert_forum( $new_forum, array() ); |
| 347 |
update_post_meta( $post_id, '_lp_course_forum', $forum_id ); |
| 348 |
} |
| 349 |
|
| 350 |
} else { |
| 351 |
//delete_option( 'bbpress_forum_enable' ); |
| 352 |
delete_post_meta( $post_id, '_lp_bbpress_forum_enable' ); |
| 353 |
//delete_post_meta( $post_id, '_lp_course_forum' ); |
| 354 |
} |
| 355 |
/*if ( !empty( $_REQUEST['bbpress_forum_create_new'] ) ) { |
| 356 |
$new_forum = array( |
| 357 |
'post_title' => $_REQUEST['bbpress_forum_create_new'], |
| 358 |
'post_content' => __( '', 'learnpress-bbpress' ), |
| 359 |
'post_author' => $course->post_author, |
| 360 |
); |
| 361 |
$forum_id = bbp_insert_forum( $new_forum, array() ); |
| 362 |
$_REQUEST['_lp_course_forum'] = $forum_id; |
| 363 |
} |
| 364 |
if ( !empty( $_REQUEST['_lp_course_forum'] ) ) { |
| 365 |
update_post_meta( $post_id, '_lp_course_forum', $_REQUEST['_lp_course_forum'] ); |
| 366 |
} else { |
| 367 |
delete_post_meta( $post_id, '_lp_course_forum' ); |
| 368 |
}*/ |
| 369 |
|
| 370 |
if ( !empty( $_REQUEST['bbpress_forum_enrolled_user'] ) ) { |
| 371 |
update_post_meta( $post_id, '_lp_bbpress_forum_enrolled_user', 'yes' ); |
| 372 |
} else { |
| 373 |
delete_post_meta( $post_id, '_lp_bbpress_forum_enrolled_user' ); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Return TRUE if bbPress plugin is installed and active |
| 379 |
* |
| 380 |
* @return bool |
| 381 |
*/ |
| 382 |
static function bbpress_is_active() { |
| 383 |
if ( !function_exists( 'is_plugin_active' ) ) { |
| 384 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 385 |
} |
| 386 |
return class_exists( 'bbPress' ) && is_plugin_active( 'bbpress/bbpress.php' ); |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Load plugin text domain |
| 391 |
*/ |
| 392 |
public static function load_text_domain() { |
| 393 |
if ( function_exists( 'learn_press_load_plugin_text_domain' ) ) { |
| 394 |
learn_press_load_plugin_text_domain( LP_ADDON_BBPRESS_PATH, true ); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Return unique instance of LP_Addon_BBPress_Forum |
| 400 |
*/ |
| 401 |
static function instance() { |
| 402 |
if ( !self::$_instance ) { |
| 403 |
self::$_instance = new self(); |
| 404 |
} |
| 405 |
return self::$_instance; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
add_action( 'learn_press_ready', array( 'LP_Addon_BBPress_Course_Forum', 'instance' ) ); |