Created
May 9, 2025 06:06
-
-
Save MrVibe/6f96591638801894576b585c01ef00de to your computer and use it in GitHub Desktop.
Ticky omitted response.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WPLMS_Course_GRoup_Customisation | |
{ | |
public static $instance; | |
private $bypass = false; | |
public static function init(){ | |
if ( is_null( self::$instance ) ) | |
self::$instance = new WPLMS_Course_GRoup_Customisation(); | |
return self::$instance; | |
} | |
function __construct(){ | |
add_Action('save_post',[$this,'save_course'],10,2); | |
add_action('groups_join_group', [$this,'save_group'],10,2); | |
add_action('groups_leave_group', [$this,'leave_group'],10,2); | |
} | |
function save_course($post_id,$post){ | |
print_r("SAVE=>"); | |
print_r($post); | |
if($post->post_type == 'course'){ | |
$this->bypass = true; | |
$group_id=get_post_meta($post_id,'vibe_group',true); | |
if(!empty($post_id) && function_exists('groups_join_group')){ | |
//Find all users of course not in the group | |
global $wpdb; | |
$results = $wpdb->get_results($wpdb->prepare("Select user_id FROM {$wpdb->usermeta} WHERE meta_key = %d user_id NOT IN (Select user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d)",$post_id,$group_id),ARRAY_A); | |
if(!empty($results)){ | |
foreach($results as $result){ | |
groups_join_group($group_id, $result->user_id); | |
} | |
} | |
} | |
} | |
} | |
function save_group($group_id, $user_id){ | |
if(empty($this->bypass)){ // Sync already performed | |
global $wpdb; | |
//find all courses connected to this group | |
$results = $wpdb->get_results($wpdb->prepare("SELECT meta.post_id as post_id FROM {$wpdb->postmeta} as meta INNER JOIN {$wpdb->posts} as posts ON meta.post_id = posts.ID WHERE AND posts.post_type = %s AND meta.meta_key = %s AND meta.meta_value LIKE %s",'course','vibe_group','"id";i:'.$group_id.';'),ARRAY_A); | |
if(!empty($results)){ | |
foreach($results as $result){ | |
if(function_exists('bp_course_is_member') && !bp_course_is_member($result->post_id,$user_id)){ | |
bp_course_add_user_to_course($user_id,$result->post_id); | |
} | |
} | |
} | |
} | |
} | |
function leave_group($group_id, $user_id){ | |
global $wpdb; | |
//find all courses connected to this group | |
$results = $wpdb->get_results($wpdb->prepare("SELECT meta.post_id as post_id FROM {$wpdb->postmeta} as meta INNER JOIN {$wpdb->posts} as posts ON meta.post_id = posts.ID WHERE AND posts.post_type = %s AND meta.meta_key = %s AND meta.meta_value =LIKE %s",'course','vibe_group','"id";i:'.$group_id.';'),ARRAY_A); | |
if(!empty($results)){ | |
foreach($results as $result){ | |
if(function_exists('bp_course_is_member') && !bp_course_is_member($result->post_id,$user_id)){ | |
bp_course_remove_user_from_course($user_id,$result->post_id); | |
} | |
} | |
} | |
} | |
} | |
WPLMS_Course_GRoup_Customisation::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment