learndash-group-user-profile.php
3.8 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
<?php
namespace uncanny_learndash_toolkit;
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Class LearndashGroupUserProfile
* @package uncanny_custom_toolkit
*/
class LearndashGroupUserProfile extends Config implements RequiredFunctions {
/**
* Class constructor
*/
public function __construct() {
add_action( 'plugins_loaded', array( __CLASS__, 'run_frontend_hooks' ) );
}
/*
* Initialize frontend actions and filters
*/
public static function run_frontend_hooks() {
if ( true === self::dependants_exist() ) {
// Add learndash groups field when the user is view there own profile
add_action( 'show_user_profile', array( __CLASS__, 'show_users_groups_profile_fields' ) );
// Add learndash groups field when the user is view another users profile
add_action( 'edit_user_profile', array( __CLASS__, 'show_users_groups_profile_fields' ) );
}
}
/**
* Description of class in Admin View
*
* @return array
*/
public static function get_details() {
$module_id = 'groups-in-user-profiles';
$class_title = esc_html__( 'Groups in User Profiles', 'uncanny-learndash-toolkit' );
$kb_link = 'https://www.uncannyowl.com/knowledge-base/learndash-groups-user-profiles/';
$class_description = esc_html__( 'Displays a user\'s LearnDash Group memberships in the user profile.', 'uncanny-learndash-toolkit' );
$class_icon = '<i class="uo_icon_fa fa fa-users"></i>';
$category = 'learndash';
$type = 'free';
return array(
'id' => $module_id,
'title' => $class_title,
'type' => $type,
'category' => $category,
'kb_link' => $kb_link,
'description' => $class_description,
'dependants_exist' => self::dependants_exist(),
'settings' => false,
'icon' => $class_icon,
);
}
/**
* Does the plugin rely on another function or plugin
*
* return boolean || string Return either true or name of function or plugin
*/
public static function dependants_exist() {
global $learndash_post_types;
if ( ! isset( $learndash_post_types ) ) {
return 'Plugin: LearnDash';
}
return true;
}
/**
* @param object $user The WP_User object of the user being edited/viewed
*
* @return string
*/
public static function show_users_groups_profile_fields( $user ) {
global $wpdb;
// title of section
$section_title = esc_html__( 'LearnDash Groups', 'uncanny-learndash-toolkit' );
// User ID of user being viewed/edited
$user_ID = $user->ID;
$groups = learndash_get_users_group_ids( $user_ID );
// If the user is part of at least one group... lets create the LearnDash Group Section
if ( ! empty( $groups ) ) {
// Happy Filtering!
$section_title = apply_filters( 'learndash_users_groups_profile_title', $section_title );
// Loop through all the user's group ids and collect the title and link
$list_groups = '';
foreach ( $groups as $group_ID ) {
if ( ! empty( $group_ID ) && is_numeric( $group_ID ) ) {
$group_permalink = add_query_arg(
array(
'post' => (int) $group_ID,
'action' => 'edit',
), admin_url( 'post.php' )
);
$group_title = get_the_title( (int) $group_ID ); // Get the group title
$list_groups .= sprintf( '<li><a href="%s">%s</a></li>', esc_url( $group_permalink ), esc_html( $group_title ) );// list of all the groups
}
}
?>
<table class="form-table">
<tbody>
<tr>
<th>
<h3><?php echo esc_html( $section_title ); ?></h3>
</th>
<td>
<ol>
<?php echo $list_groups; ?>
</ol>
</td>
</tr>
</tbody>
</table>
<?php
}
}
}