group-logo-list.php
8.13 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
namespace uncanny_pro_toolkit;
use uncanny_learndash_toolkit as toolkit;
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Group Logo List
*
* @package uncanny_pro_toolkit
*/
class GroupLogoList extends toolkit\Config implements toolkit\RequiredFunctions {
/**
* Class constructor
*/
public function __construct() {
add_action( 'plugins_loaded', array( __CLASS__, 'run_frontend_hooks' ) );
}
/**
* Initializes frontend actions and filters
*/
public static function run_frontend_hooks() {
if ( true === self::dependants_exist() ) {
add_filter( 'init', array( __CLASS__, 'add_thumbnail_support' ), 20, 2 );
add_filter( 'post_type_labels_groups', array( __CLASS__, 'add_thumbnail_labels' ), 10, 1 );
add_shortcode( 'uo_group_logo', array( __CLASS__, 'uo_group_logo' ) );
// Add group list shortcode which takes attribute separator(default = ' ,').
add_shortcode( 'uo_group_list', array( __CLASS__, 'uo_group_list' ) );
add_shortcode( 'uo_group_leaders', array( __CLASS__, 'uo_group_leaders' ) );
}
}
/**
* 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() {
/* Checks for LearnDash */
global $learndash_post_types;
if ( ! isset( $learndash_post_types ) ) {
return 'Plugin: LearnDash';
}
// Return true if no dependency or dependency is available.
return true;
}
/**
* Description of class in Admin View
*
* @return array
*/
public static function get_details() {
$module_id = 'group-logo-list';
$class_title = esc_html__( 'Group Logo/List', 'uncanny-pro-toolkit' );
$kb_link = 'https://www.uncannyowl.com/knowledge-base/ld-group-logo-list/';
/* Sample Simple Description with shortcode */
$class_description = esc_html__( "Add group-specific logos to any page, including registration pages. A shortcode to list a user's LearnDash Groups is also available.", 'uncanny-pro-toolkit' );
/* Icon as fontawesome icon */
$class_icon = '<i class="uo_icon_pro_fa uo_icon_fa fa fa-file-image-o "></i><span class="uo_pro_text">PRO</span>';
$category = 'learndash';
$type = 'pro';
return array(
'id' => $module_id,
'title' => $class_title,
'type' => $type,
'category' => $category,
'kb_link' => $kb_link, // OR set as null not to display
'description' => $class_description,
'dependants_exist' => self::dependants_exist(),
'settings' => false,
'icon' => $class_icon,
);
}
/**
* Adds thumbnail support to groups.
*/
public static function add_thumbnail_support() {
// bail if post type already supports thumbnails.
if ( post_type_supports( 'groups', 'thumbnail' ) ) {
return;
}
// otherwise, add support for thumbnails.
add_post_type_support( 'groups', 'thumbnail' );
}
/**
* Adds custom labels for logos.
*
* @param obj $labels Default labels object.
*
* @return obj
*/
public static function add_thumbnail_labels( $labels ) {
// set up groupl logo related labels.
$labels->featured_image = esc_attr__( 'Branding Logo', 'uncanny-pro-toolkit' );
$labels->set_featured_image = esc_attr__( 'Set Logo', 'uncanny-pro-toolkit' );
$labels->remove_featured_image = esc_attr__( 'Remove Logo', 'uncanny-pro-toolkit' );
$labels->use_featured_image = esc_attr__( 'Use Logo', 'uncanny-pro-toolkit' );
return $labels;
}
/**
* Outputs a images asscoiated with a user's groups
*
* @param array $attributes Shortcode attributes.
*
* @return string
*/
public static function uo_group_logo( $attributes ) {
$attributes = shortcode_atts(
array(
'size' => 'full',
'limit' => null,
'group_id' => null,
'only_user_groups' => null,
),
$attributes
);
$user_id = get_current_user_id();
if( isset( $attributes['only_user_groups'] ) && 'yes' === (string) $attributes['only_user_groups'] ) {
$group_ids = learndash_get_users_group_ids( $user_id );
} else if ( learndash_is_group_leader_user( $user_id ) || current_user_can( 'administrator' ) ) {
$group_ids = learndash_get_administrators_group_ids( $user_id );
} else {
$group_ids = learndash_get_users_group_ids( $user_id );
}
$logo = '';
$limit = ( isset( $attributes['limit'] ) && is_numeric( $attributes['limit'] ) ) ? absint( $attributes['limit'] ) : count( $group_ids );
$limit = ( $limit >= count( $group_ids ) ) ? count( $group_ids ) : $limit;
$group_ids = array_map( 'absint', $group_ids );
$attr_group_id = ( isset( $attributes['group_id'] ) && is_numeric( $attributes['group_id'] ) ) ? absint( $attributes['group_id'] ) : null;
if ( null !== $attr_group_id && ! in_array( $attr_group_id, $group_ids, true ) ) {
return $logo;
}
if ( is_numeric( $attr_group_id ) ) {
$group_ids = array( $attr_group_id );
}
if ( ! empty( $group_ids ) ) {
$i = 1;
foreach ( $group_ids as $group_id ) {
if ( $i > $limit ) {
break;
}
$img = get_the_post_thumbnail( $group_id, $attributes['size'] );
if ( ! empty( $img ) ) {
$logo .= $img;
$i ++;
}
}
}
// Add custom class.
return str_replace( 'class="', 'class="uo_white_label_logo ', $logo );
}
/**
* Return a list of LearnDash group names the use is a part of
*
* @param array $attributes Shortcode attributes.
*
* @return string
*/
public static function uo_group_list( $attributes ) {
$attributes = shortcode_atts( array( 'separator' => ', ' ), $attributes );
$user_id = get_current_user_id();
$group_ids = learndash_get_users_group_ids( $user_id );
$logo = array();
if ( ! empty( $group_ids ) ) {
foreach ( $group_ids as $group_id ) {
$post_title = get_the_title( $group_id );
if ( $post_title ) {
$logo[] = $post_title;
}
}
}
return implode( $attributes['separator'], $logo );
}
/**
* Returns a list of Group leaders.
*
* @param array $attributes Shortcode attributes.
*
* @return string
* @since 3.5.3
*/
public static function uo_group_leaders( $attributes ) {
$group_leader_info = '';
$user_id = get_current_user_id();
$group_ids = learndash_get_users_group_ids( $user_id );
if ( empty( $group_ids ) ) {
return $group_leader_info;
}
$groups_count = count( $group_ids );
$group_leader_info .= '<div class="uo_group_info_wrapper">';
foreach ( $group_ids as $group_id ) {
if ( 0 !== $group_id ) {
$group_leaders = learndash_get_groups_administrators( $group_id );
if ( $groups_count > 1 ) {
$group_leader_info .= '<div class="uo_group_name">' . get_the_title( $group_id ) . '</div>';
}
if ( ! empty( $group_leaders ) ) {
foreach ( $group_leaders as $group_leader ) {
if ( isset( $group_leader->data ) && isset( $group_leader->data->ID ) && absint( $group_leader->data->ID ) ) {
$first_name = get_user_meta( $group_leader->data->ID, 'first_name', true );
if ( ! empty( $first_name ) ) {
$first_name = $first_name . ' ';
} else {
$first_name = '';
}
$last_name = get_user_meta( $group_leader->data->ID, 'last_name', true );
if ( ! empty( $last_name ) ) {
$last_name = $last_name . ' ';
} else {
$last_name = '';
}
$add_dash = '';
if ( ! empty( $first_name ) || ! empty( $last_name ) ) {
$add_dash = '<span class="uo_group_leader_dash"> - </span>';
}
$email = $group_leader->data->user_email;
$group_leader_info .= '<div class="uo_group_leader_list"> <span class="uo_group_leader_name">' . $first_name . $last_name . '</span>' . $add_dash . '<a href="mailto:' . $email . '"><span class="uo_group_leader_email">' . $email . '</span></a></div>';
}
}
}
}
}
$group_leader_info .= '</div>';
/**
* Filters group leaders list.
*
* @param string $group_leader_info Group leaders html output.
* @param array $group_ids Array of group IDs.
*/
$group_leader_info = apply_filters( 'uo_groups_leaders', $group_leader_info, $group_ids );
return $group_leader_info;
}
}