Core
COre file of the plugin
Description
Source
File: bp-integrations/learndash/core/Core.php
class Core { /** * Constructor * * @since BuddyBoss 1.0.0 */ public function __construct() { $this->registerAutoloader(); $this->admin = new Admin; $this->dependencies = new Dependencies; $this->requirements = new Requirements; $this->buddypress = new BuddypressCore; $this->learndash = new LearndashCore; $this->settings = new Settings; $this->pluginName = __('BuddyBoss LearnDash', 'buddyboss'); add_action('bp_ld_sync/requirements_checked', [$this, 'init']); } /** * Sub action once dependencies and requirements are checked * * @since BuddyBoss 1.0.0 */ public function init() { do_action('bp_ld_sync/init'); } /** * Get absolute path from the integration folder * * @since BuddyBoss 1.0.0 */ public function path($path = '') { return bp_learndash_path(trim($path, '/\\')); } /** * Get url path from the integration folder * * @since BuddyBoss 1.0.0 */ public function url($uri = '') { return bp_learndash_url(trim($uri, '/\\')); } /** * Load template from the integration folder * * @since BuddyBoss 1.0.0 */ public function template($path = '') { return bp_learndash_path('templates/' . trim($path, '/\\')); } /** * Get the request from $_POST, $_GET, or $_REQUEST with default fallback * * @since BuddyBoss 1.0.0 */ public function getRequest($key = '*', $default = null, $type = null) { if ($type) { return $key == '*'? $$type : (isset($$type[$key])? $$type[$key] : $default); } $merged = array_merge($_GET, $_POST, $_REQUEST); return $key == '*'? $merged : (isset($merged[$key])? $merged[$key] : $default); } /** * Check if the given request isset * * @since BuddyBoss 1.0.0 */ public function isRequestExists( $key, $default = null, $type = null ) { if ($type) { return isset($$type[$key]); } $merged = array_merge($_GET, $_POST, $_REQUEST); return isset($merged[$key]); } /** * Register psr4 autoloader manually * * @since BuddyBoss 1.0.0 */ public function registerAutoloader() { spl_autoload_register(function($class) { $psr4 = [ 'Buddyboss\LearndashIntegration\Core' => 'core', 'Buddyboss\LearndashIntegration\Library' => 'library', 'Buddyboss\LearndashIntegration\Buddypress' => 'buddypress', 'Buddyboss\LearndashIntegration\Buddypress\Generators' => 'buddypress/generators', 'Buddyboss\LearndashIntegration\Buddypress\Components' => 'buddypress/components', 'Buddyboss\LearndashIntegration\Learndash' => 'learndash', ]; $segments = explode('\\', $class); $className = array_pop($segments); $namespace = implode('\\', $segments); if (array_key_exists($namespace, $psr4)) { require_once $this->path("/{$psr4[$namespace]}/{$className}.php"); } }); } public function bp_get_course_members( $course_id ) { $post = get_post( $course_id ); if ( empty( $post ) ) { return array(); } $access_list = learndash_get_course_meta_setting( $post->ID, 'course_access_list' ); if ( ! is_array( $access_list ) ) { $access_list = array(); } $result = array(); if ( ! empty( $access_list ) ) { $result = array(); foreach ( $access_list as $user_id ) { $user = get_userdata( (int) $user_id ); if ( empty( $user ) || ! $user->exists() ) { continue; } if ( is_multisite() && ! is_user_member_of_blog( $user->ID ) ) { continue; } $result[] = $user; } } return $result; } public function bp_get_courses_progress ( $user_id, $sort_order = 'desc' ) { $course_completion_percentage = array(); if ( ! $course_completion_percentage = wp_cache_get ( $user_id, 'ld_courses_progress' ) ) { $course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true ); if ( ! empty( $course_progress ) ) { foreach ( $course_progress as $course_id => $coursep ) { // We take default progress value as 1 % rather than 0% $course_completion_percentage[ $course_id ] = 1;// if ( $coursep['total'] == 0 ) { continue; } $course_steps_count = learndash_get_course_steps_count( $course_id ); $course_steps_completed = learndash_course_get_completed_steps( $user_id, $course_id, $coursep ); $completed_on = get_user_meta( $user_id, 'course_completed_' . $course_id, true ); if ( !empty( $completed_on ) ) { $coursep['completed'] = $course_steps_count; $coursep['total'] = $course_steps_count; } else { $coursep['total'] = $course_steps_count; $coursep['completed'] = $course_steps_completed; if ( $coursep['completed'] > $coursep['total'] ) $coursep['completed'] = $coursep['total']; } // cannot divide by 0 if ( $coursep['total'] == 0 ) { $course_completion_percentage[ $course_id ] = 0; } else { $course_completion_percentage[ $course_id ] = ceil( ( $coursep['completed'] * 100 ) / $coursep['total'] ); } } } //Avoid running the queries multiple times if user's course progress is empty $course_completion_percentage = !empty( $course_completion_percentage ) ? $course_completion_percentage : 'empty'; wp_cache_set( $user_id, $course_completion_percentage, 'ld_courses_progress' ); } $course_completion_percentage = 'empty' !== $course_completion_percentage ? $course_completion_percentage : array(); if ( !empty( $course_completion_percentage ) ) { // Sort. if ( 'asc' == $sort_order ) { asort( $course_completion_percentage ); } else { arsort( $course_completion_percentage ); } } return $course_completion_percentage; } public function bp_ld_get_progress_course_percentage( $user_id, $course_id ) { if ( empty( $user_id ) ) { // $current_user = wp_get_current_user(); if ( is_user_logged_in() ) { $user_id = get_current_user_id(); } else { $user_id = 0; } } if ( empty( $course_id ) ) { $course_id = learndash_get_course_id(); } if ( empty( $course_id ) ) { return ''; } $completed = 0; $total = false; if ( ! empty( $user_id ) ) { $course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true ); $percentage = 0; $message = ''; if ( ( ! empty( $course_progress ) ) && ( isset( $course_progress[ $course_id ] ) ) && ( ! empty( $course_progress[ $course_id ] ) ) ) { if ( isset( $course_progress[ $course_id ]['completed'] ) ) { $completed = absint( $course_progress[ $course_id ]['completed'] ); } if ( isset( $course_progress[ $course_id ]['total'] ) ) { $total = absint( $course_progress[ $course_id ]['total'] ); } } else { $total = 0; } } // If $total is still false we calculate the total from course steps. if ( false === $total ) { $total = learndash_get_course_steps_count( $course_id ); } if ( $total > 0 ) { $percentage = intval( $completed * 100 / $total ); $percentage = ( $percentage > 100 ) ? 100 : $percentage; } else { $percentage = 0; } return $percentage; } public function bp_course_resume( $course_id ) { if ( is_user_logged_in() ) { if ( ! empty( $course_id ) ) { $user = wp_get_current_user(); $step_course_id = $course_id; $course = get_post( $step_course_id ); $lession_list = learndash_get_lesson_list( $course_id ); $url = bp_ld_sync()->bp_ld_custom_continue_url_arr( $course_id, $lession_list ); if ( isset( $course ) && 'sfwd-courses' === $course->post_type ) { //$last_know_step = get_user_meta( $user->ID, 'learndash_last_known_course_' . $step_course_id, true ); $last_know_step = ''; // User has not hit a LD module yet if ( empty( $last_know_step ) ) { if ( isset( $url ) && '' !== $url ) { return $url; } else { return ''; } } //$step_course_id = 0; // Sanity Check if ( absint( $last_know_step ) ) { $step_id = $last_know_step; } else { if ( isset( $url ) && '' !== $url ) { return $url; } else { return ''; } } $last_know_post_object = get_post( $step_id ); // Make sure the post exists and that the user hit a page that was a post // if $last_know_page_id returns '' then get post will return current pages post object // so we need to make sure first that the $last_know_page_id is returning something and // that the something is a valid post if ( null !== $last_know_post_object ) { $post_type = $last_know_post_object->post_type; // getting post_type of last page. $label = get_post_type_object( $post_type ); // getting Labels of the post type. $title = $last_know_post_object->post_title; $resume_link_text = __( 'RESUME', 'buddyboss' ); if ( function_exists( 'learndash_get_step_permalink' ) ) { $permalink = learndash_get_step_permalink( $step_id, $step_course_id ); } else { $permalink = get_permalink( $step_id ); } return $permalink; } } } } else { $course_price_type = learndash_get_course_meta_setting( $course_id, 'course_price_type' ); if ( $course_price_type == 'open' ) { $lession_list = learndash_get_lesson_list( $course_id ); $url = bp_ld_sync()->bp_ld_custom_continue_url_arr( $course_id, $lession_list ); return $url; } } return ''; } /** * Get all the URLs of current course ( lesson, topic, quiz ) * @param $course_id * @param $lession_list * @param string $course_quizzes_list * * @return array | string */ public function bp_ld_custom_continue_url_arr( $course_id, $lession_list, $course_quizzes_list = '' ) { global $post; $course_price_type = learndash_get_course_meta_setting( $course_id, 'course_price_type' ); if ( $course_price_type == 'closed' ) { $courses_progress = bp_ld_sync()->bp_get_courses_progress( get_current_user_id() ); $user_courses = learndash_user_get_enrolled_courses( get_current_user_id() ); $course_progress = isset( $courses_progress[ $course_id ] ) ? $courses_progress[ $course_id ] : null; if ( $course_progress <= 0 && ! in_array( $course_id, $user_courses) ) { return get_the_permalink( $course_id ); } } $navigation_urls = array(); if ( ! empty( $lession_list ) ) : foreach( $lession_list as $lesson ) { $lesson_topics = learndash_get_topic_list( $lesson->ID ); $course_progress = get_user_meta( get_current_user_id(), '_sfwd-course_progress', true ); $completed = ! empty( $course_progress[ $course_id ]['lessons'][ $lesson->ID ] ) && 1 === $course_progress[ $course_id ]['lessons'][ $lesson->ID ]; $navigation_urls[] = array( 'url' => get_permalink( $lesson->ID ), 'complete' => $completed ? 'yes' : 'no', ); if( ! empty( $lesson_topics ) ) : foreach( $lesson_topics as $lesson_topic ) { $completed = ! empty( $course_progress[ $course_id ]['topics'][ $lesson->ID ][ $lesson_topic->ID ] ) && 1 === $course_progress[ $course_id ]['topics'][ $lesson->ID ][ $lesson_topic->ID ]; $navigation_urls[] = array( 'url' => get_permalink( $lesson_topic->ID ), 'complete' => $completed ? 'yes' : 'no', ); $topic_quizzes = learndash_get_lesson_quiz_list( $lesson_topic->ID ); if( ! empty( $topic_quizzes ) ) : foreach( $topic_quizzes as $topic_quiz ) { $navigation_urls[] = array( 'url' => get_permalink( $topic_quiz['post']->ID ), 'complete' => learndash_is_quiz_complete( get_current_user_id(), $topic_quiz['post']->ID ) ? 'yes' : 'no' ); } endif; } endif; $lesson_quizzes = learndash_get_lesson_quiz_list( $lesson->ID ); if( ! empty( $lesson_quizzes ) ) : foreach( $lesson_quizzes as $lesson_quiz ) { $navigation_urls[] = array( 'url' => get_permalink( $lesson_quiz['post']->ID ), 'complete' => learndash_is_quiz_complete( get_current_user_id(), $lesson_quiz['post']->ID) ? 'yes' : 'no' ); } endif; } endif; $course_quizzes = learndash_get_course_quiz_list( $course_id ); if ( ! empty( $course_quizzes ) ) : foreach( $course_quizzes as $course_quiz ) { $navigation_urls[] = array( 'url' => get_permalink( $course_quiz['post']->ID ), 'complete' => learndash_is_quiz_complete( get_current_user_id(), $course_quiz['post']->ID) ? 'yes' : 'no' ); } endif; $key = array_search('no', array_column( $navigation_urls, 'complete') ); if ( '' !== $key && isset( $navigation_urls[$key] )) { return $navigation_urls[$key]['url']; } return ''; } public function bp_ld_prepare_price_str( $price ) { if ( ! empty( $price ) ) { $currency_symbols = array( 'AED' => 'د.إ', // ? 'AFN' => 'Af', 'ALL' => 'Lek', 'AMD' => '', 'ANG' => 'ƒ', 'AOA' => 'Kz', // ? 'ARS' => '$', 'AUD' => '$', 'AWG' => 'ƒ', 'AZN' => 'ман', 'BAM' => 'KM', 'BBD' => '$', 'BDT' => '৳', // ? 'BGN' => 'лв', 'BHD' => '.د.ب', // ? 'BIF' => 'FBu', // ? 'BMD' => '$', 'BND' => '$', 'BOB' => '$b', 'BRL' => 'R$', 'BSD' => '$', 'BTN' => 'Nu.', // ? 'BWP' => 'P', 'BYR' => 'p.', 'BZD' => 'BZ$', 'CAD' => '$', 'CDF' => 'FC', 'CHF' => 'CHF', 'CLF' => '', // ? 'CLP' => '$', 'CNY' => '¥', 'COP' => '$', 'CRC' => '₡', 'CUP' => '⃌', 'CVE' => '$', // ? 'CZK' => 'Kč', 'DJF' => 'Fdj', // ? 'DKK' => 'kr', 'DOP' => 'RD$', 'DZD' => 'دج', // ? 'EGP' => '£', 'ETB' => 'Br', 'EUR' => '€', 'FJD' => '$', 'FKP' => '£', 'GBP' => '£', 'GEL' => 'ლ', // ? 'GHS' => '¢', 'GIP' => '£', 'GMD' => 'D', // ? 'GNF' => 'FG', // ? 'GTQ' => 'Q', 'GYD' => '$', 'HKD' => '$', 'HNL' => 'L', 'HRK' => 'kn', 'HTG' => 'G', // ? 'HUF' => 'Ft', 'IDR' => 'Rp', 'ILS' => '₪', 'INR' => '₹', 'IQD' => 'ع.د', // ? 'IRR' => '﷼', 'ISK' => 'kr', 'JEP' => '£', 'JMD' => 'J$', 'JOD' => 'JD', // ? 'JPY' => '¥', 'KES' => 'KSh', // ? 'KGS' => 'лв', 'KHR' => '៛', 'KMF' => 'CF', // ? 'KPW' => '₩', 'KRW' => '₩', 'KWD' => 'د.ك', // ? 'KYD' => '$', 'KZT' => 'лв', 'LAK' => '₭', 'LBP' => '£', 'LKR' => '₨', 'LRD' => '$', 'LSL' => 'L', // ? 'LTL' => 'Lt', 'LVL' => 'Ls', 'LYD' => 'ل.د', // ? 'MAD' => 'د.م.', //? 'MDL' => 'L', 'MGA' => 'Ar', // ? 'MKD' => 'ден', 'MMK' => 'K', 'MNT' => '₮', 'MOP' => 'MOP$', // ? 'MRO' => 'UM', // ? 'MUR' => '₨', // ? 'MVR' => '.ރ', // ? 'MWK' => 'MK', 'MXN' => '$', 'MYR' => 'RM', 'MZN' => 'MT', 'NAD' => '$', 'NGN' => '₦', 'NIO' => 'C$', 'NOK' => 'kr', 'NPR' => '₨', 'NZD' => '$', 'OMR' => '﷼', 'PAB' => 'B/.', 'PEN' => 'S/.', 'PGK' => 'K', // ? 'PHP' => '₱', 'PKR' => '₨', 'PLN' => 'zł', 'PYG' => 'Gs', 'QAR' => '﷼', 'RON' => 'lei', 'RSD' => 'Дин.', 'RUB' => 'руб', 'RWF' => 'ر.س', 'SAR' => '﷼', 'SBD' => '$', 'SCR' => '₨', 'SDG' => '£', // ? 'SEK' => 'kr', 'SGD' => '$', 'SHP' => '£', 'SLL' => 'Le', // ? 'SOS' => 'S', 'SRD' => '$', 'STD' => 'Db', // ? 'SVC' => '$', 'SYP' => '£', 'SZL' => 'L', // ? 'THB' => '฿', 'TJS' => 'TJS', // ? TJS (guess) 'TMT' => 'm', 'TND' => 'د.ت', 'TOP' => 'T$', 'TRY' => '₤', // New Turkey Lira (old symbol used) 'TTD' => '$', 'TWD' => 'NT$', 'TZS' => '', 'UAH' => '₴', 'UGX' => 'USh', 'USD' => '$', 'UYU' => '$U', 'UZS' => 'лв', 'VEF' => 'Bs', 'VND' => '₫', 'VUV' => 'VT', 'WST' => 'WS$', 'XAF' => 'FCFA', 'XCD' => '$', 'XDR' => '', 'XOF' => '', 'XPF' => 'F', 'YER' => '﷼', 'ZAR' => 'R', 'ZMK' => 'ZK', // ? 'ZWL' => 'Z$', ); return html_entity_decode( $currency_symbols[ $price['code'] ] ) . $price['value']; } return ''; } }
Changelog
Version | Description |
---|---|
BuddyBoss 1.0.0 | Introduced. |
Methods
- __construct — Constructor
- adminbar_nav_link — Add Menu and Sub menu navigation link for admin menu
- bp_course_resume
- bp_get_course_members
- bp_get_courses_progress
- bp_ld_custom_continue_url_arr — Get all the URLs of current course ( lesson, topic, quiz )
- bp_ld_get_progress_course_percentage
- bp_ld_prepare_price_str
- certificates_page — Display Certificates Page Content in Profile course menu
- certificates_page_content — Display Certificates Page Content
- course_page — Display Course Page Content in Profile course menu
- courses_page_content — Display Courses in My Course Profile Page
- get_nav_link — Add Menu and Sub menu navigation link for profile menu
- getRequest — Get the request from $_POST, $_GET, or $_REQUEST with default fallback
- init — Sub action once dependencies and requirements are checked
- isRequestExists — Check if the given request isset
- path — Get absolute path from the integration folder
- registerAutoloader — Register psr4 autoloader manually
- registerCourseComponent — Add Course tab in profile menu
- setup_admin_bar — Add Course tab in admin menu
- setup_nav — Add Course tab in profile menu
- setup_user_profile_bar — Add Menu in Profile section.
- template — Load template from the integration folder
- url — Get url path from the integration folder
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.