PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.1
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / TemplateHooks / UserTemplate.php

UserTemplate.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.1, at inc/TemplateHooks/UserTemplate.php

265 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Template hooks User.
4 *
5 * @since 4.2.7.2
6 * @version 1.0.1
7 */
8
9 namespace LearnPress\TemplateHooks;
10
11 use LearnPress\Helpers\Template;
12 use LearnPress\Models\UserModel;
13 use LP_Profile;
14 use LP_User;
15 use Throwable;
16
17 class UserTemplate {
18 public $class_name;
19
20 public function __construct( $class_name = 'user' ) {
21 $this->class_name = $class_name;
22 }
23
24 /**
25 * Get display name html of user.
26 *
27 * @param UserModel|LP_User $userModel
28 *
29 * @return string
30 */
31 public function html_display_name( $userModel ): string {
32 if ( $userModel instanceof LP_User ) {
33 $userModel = UserModel::find( $userModel->get_id(), true );
34 }
35
36 if ( ! $userModel ) {
37 return '';
38 }
39
40 $sections = [
41 'wrapper' => sprintf( '<span class="%s-display-name">', $this->class_name ),
42 'content' => $userModel->get_display_name(),
43 'wrapper_end' => '</span>',
44 ];
45
46 return Template::combine_components( $sections );
47 }
48
49 /**
50 * Get html description of instructor.
51 *
52 * @param UserModel|LP_User $userModel
53 *
54 * @return string
55 * @since 4.2.3.4
56 * @version 1.0.1
57 */
58 public function html_description( $userModel ): string {
59 $content = '';
60
61 try {
62 if ( $userModel instanceof LP_User ) {
63 $userModel = UserModel::find( $userModel->get_id(), true );
64 }
65
66 if ( ! $userModel ) {
67 return $content;
68 }
69
70 $description = $userModel->get_description();
71 if ( empty( $description ) ) {
72 return $content;
73 }
74
75 $sections = apply_filters(
76 'learn-press/user/html-description',
77 [
78 'wrapper' => sprintf( '<div class="%s-description">', $this->class_name ),
79 'content' => wpautop( wp_kses_post( $description ) ),
80 'wrapper_end' => '</div>',
81 ],
82 $userModel
83 );
84
85 $content = Template::combine_components( $sections );
86 } catch ( Throwable $e ) {
87 error_log( __METHOD__ . ': ' . $e->getMessage() );
88 }
89
90 return $content;
91 }
92
93 /**
94 * Get html avatar of user.
95 *
96 * @param UserModel $user
97 * @param array $size_display [ 'width' => 100, 'height' => 100 ]
98 *
99 * @return string
100 * @since 4.2.7.2
101 * @version 1.0.5
102 */
103 public function html_avatar( UserModel $user, array $size_display = [] ): string {
104 $html = '';
105
106 try {
107 if ( $user instanceof LP_User ) {
108 $user = UserModel::find( $user->get_id(), true );
109 }
110
111 if ( ! $user ) {
112 return '';
113 }
114
115 if ( empty( $size_display ) ) {
116 $size_display = learn_press_get_avatar_thumb_size();
117 }
118
119 $width = $size_display;
120 $height = $size_display;
121 if ( is_array( $size_display ) ) {
122 $width = $size_display['width'];
123 $height = $size_display['height'];
124 }
125
126 $avatar_url = $user->get_avatar_url();
127 $img_avatar = sprintf(
128 '<img alt="%s" class="avatar" src="%s" width="%d" height="%d" decoding="async" loading="lazy" />',
129 esc_attr__( 'User Avatar', 'learnpress' ),
130 $avatar_url,
131 $width,
132 $height
133 );
134
135 $section = apply_filters(
136 'learn-press/user/html-avatar',
137 [
138 'wrapper' => sprintf( '<div class="%s-avatar">', $this->class_name ),
139 'avatar' => $img_avatar,
140 'wrapper_end' => '</div>',
141 ],
142 $user
143 );
144
145 $html = Template::combine_components( $section );
146 } catch ( Throwable $e ) {
147 error_log( __METHOD__ . ': ' . $e->getMessage() );
148 }
149
150 return $html;
151 }
152
153 /**
154 * Get html avatar of instructor with edit link.
155 * Don't wrapper this html with tag <a> because has tag <a> inside.
156 *
157 * @param UserModel $user
158 * @param array $size_display [ 'width' => 100, 'height' => 100 ]
159 *
160 * @return string
161 * @since 4.2.7.6
162 * @version 1.0.0
163 */
164 public function html_avatar_edit( UserModel $user, array $size_display = [] ): string {
165 $html = '';
166
167 try {
168 if ( $user instanceof LP_User ) {
169 $user = UserModel::find( $user->get_id(), true );
170 }
171
172 if ( ! $user ) {
173 return '';
174 }
175
176 if ( empty( $size_display ) ) {
177 $size_display = learn_press_get_avatar_thumb_size();
178 }
179
180 $profile = LP_Profile::instance( $user->get_id() );
181 $width = $size_display;
182 $height = $size_display;
183 if ( is_array( $size_display ) ) {
184 $width = $size_display['width'];
185 $height = $size_display['height'];
186 }
187
188 $avatar_url = $user->get_avatar_url();
189 $img_avatar = sprintf(
190 '<img alt="%s" class="avatar" src="%s" width="%d" height="%d" decoding="async" />',
191 esc_attr__( 'User Avatar', 'learnpress' ),
192 $avatar_url,
193 $width,
194 $height
195 );
196
197 $html_btn_to_edit_avatar = '';
198 if ( $user->get_id() === get_current_user_id() ) {
199 $html_btn_to_edit_avatar = sprintf(
200 '<a class="lp-btn-to-edit-avatar" href="%s" data-section-correct="%d" title="%s">+ %s</a>',
201 $profile->get_tab_link( 'settings', 'avatar' ),
202 'avatar',
203 esc_attr__( 'Edit avatar', 'learnpress' ),
204 __( 'edit avatar', 'learnpress' )
205 );
206 }
207
208 $section = apply_filters(
209 'learn-press/user/html-avatar-edit',
210 [
211 'wrapper' => sprintf( '<div class="%s-avatar">', $this->class_name ),
212 'avatar' => $img_avatar,
213 'btn_edit' => $html_btn_to_edit_avatar,
214 'wrapper_end' => '</div>',
215 ],
216 $user
217 );
218
219 $html = Template::combine_components( $section );
220 } catch ( Throwable $e ) {
221 error_log( __METHOD__ . ': ' . $e->getMessage() );
222 }
223
224 return $html;
225 }
226
227 /**
228 * Get html social of instructor.
229 *
230 * @param UserModel|LP_User $userModel
231 *
232 * @return string
233 */
234 public function html_social( $userModel ): string {
235 $content = '';
236
237 try {
238 if ( $userModel instanceof LP_User ) {
239 $userModel = UserModel::find( $userModel->get_id(), true );
240 }
241
242 if ( ! $userModel ) {
243 return '';
244 }
245
246 $socials = $userModel->get_profile_social();
247 if ( empty( $socials ) ) {
248 return $content;
249 }
250
251 $sections = [
252 'wrapper' => sprintf( '<div class="%s-social">', $this->class_name ),
253 'content' => Template::combine_components( $socials ),
254 'wrapper_end' => '</div>',
255 ];
256
257 $content = Template::combine_components( $sections );
258 } catch ( Throwable $e ) {
259 error_log( __METHOD__ . ': ' . $e->getMessage() );
260 }
261
262 return $content;
263 }
264 }
265