PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.1
4.4.8 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 All 139 releases
learnpress / inc / user / class-lp-profile-tabs.php

class-lp-profile-tabs.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9.1, at inc/user/class-lp-profile-tabs.php

486 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Profile_Tabs
5 */
6 class LP_Profile_Tabs extends LP_Array_Access {
7
8 /**
9 * @var LP_Profile_Tabs
10 */
11 protected static $_instance = null;
12
13 /**
14 * @var LP_Profile
15 */
16 protected $profile = null;
17
18 /**
19 * LP_Profile_Tabs constructor.
20 *
21 * @param array $tabs
22 * @param LP_Profile $profile
23 */
24 public function __construct( $tabs, $profile ) {
25 parent::__construct( $tabs );
26
27 $this->profile = $profile;
28
29 $this->_sanitize();
30 $this->init();
31 }
32
33 /**
34 *
35 */
36 protected function init() {
37 global $wp;
38
39 $current = $wp->query_vars['view'] ?? 'overview';
40 $current_page = LP_Page_Controller::page_current();
41
42 if ( LP_PAGE_PROFILE !== $current_page ) {
43 return;
44 }
45
46 foreach ( $this->_data as $k => $v ) {
47 if ( $current === $v['slug'] ) {
48 $v['is_current'] = true;
49 }
50 $this->_data[ $k ] = new LP_Profile_Tab( $k, $v, $this->get_profile() );
51 }
52 }
53
54 /**
55 * Sort tabs
56 */
57 protected function _sanitize() {
58 $tabs = $this->_data;
59 foreach ( $tabs as $slug => $data ) {
60 if ( ! array_key_exists( 'slug', $data ) ) {
61 $tabs[ $slug ]['slug'] = $slug;
62 }
63
64 if ( ! array_key_exists( 'priority', $data ) ) {
65 $tabs[ $slug ]['priority'] = 10;
66 }
67
68 if ( empty( $data['sections'] ) ) {
69 continue;
70 }
71
72 foreach ( $data['sections'] as $section_slug => $section_data ) {
73 if ( ! array_key_exists( 'slug', $section_data ) ) {
74 $tabs[ $slug ]['sections'][ $section_slug ]['slug'] = $section_slug;
75 }
76
77 if ( ! array_key_exists( 'priority', $section_data ) ) {
78 $tabs[ $slug ]['sections'][ $section_slug ]['priority'] = 10;
79 }
80 }
81 uasort( $tabs[ $slug ]['sections'], array( $this, '_sort_tabs' ) );
82 }
83
84 uasort( $tabs, array( $this, '_sort_tabs' ) );
85
86 $key = md5( serialize( array_keys( $tabs ) ) );
87 if ( $key !== get_option( '_lp_tabs_data' ) ) {
88 flush_rewrite_rules();
89 update_option( '_lp_tabs_data', $key, false );
90 }
91
92 $this->_data = $tabs;
93 }
94
95 /**
96 * @return LP_Profile|null
97 */
98 public function get_profile() {
99 return $this->profile;
100 }
101
102 public function get_current_tab( $default = '', $key = true ) {
103 global $wp;
104 $current = $default;
105
106 if ( ! empty( $_REQUEST['view'] ) ) {
107 $current = sanitize_text_field( $_REQUEST['view'] );
108 } elseif ( ! empty( $wp->query_vars['view'] ) ) {
109 $current = $wp->query_vars['view'];
110 } else {
111 if ( $this->get_tab_at() ) {
112 $tab = $this->get_tab_at();
113 $current = $tab['slug'];
114 }
115 }
116
117 if ( $key ) {
118 $current_display = $current;
119 $current = false;
120 foreach ( $this->get() as $_slug => $data ) {
121 if ( is_object( $data ) ) {
122 if ( $data->get( 'slug' ) === $current_display ) {
123 $current = $_slug;
124 break;
125 }
126 } elseif ( is_array( $data ) ) {
127 if ( $data['slug'] === $current_display ) {
128 $current = $_slug;
129 break;
130 }
131 }
132 }
133 }
134
135 return $current;
136 }
137
138 public function get_current_section( $default = '', $key = true, $tab = '' ) {
139 global $wp;
140
141 $current = $default;
142
143 if ( ! empty( $_REQUEST['section'] ) ) {
144 $current = sanitize_text_field( $_REQUEST['section'] );
145 } elseif ( ! empty( $wp->query_vars['section'] ) ) {
146 $current = $wp->query_vars['section'];
147 } else {
148 if ( false === $tab ) {
149 $current_tab = $this->get_current_tab();
150 } else {
151 $current_tab = $tab;
152 }
153
154 if ( $this->get_tab_at( $current_tab ) ) {
155 $tab = $this->get_tab_at( $current_tab );
156
157 if ( ! empty( $tab['sections'] ) ) {
158 $sections = $tab['sections'];
159 $section = reset( $sections );
160 if ( array_key_exists( 'slug', $section ) ) {
161 $current = $section['slug'];
162 } else {
163 $sections = array_keys( $tab['sections'] );
164 $current = reset( $sections );
165 }
166 }
167 }
168 }
169
170 if ( $key ) {
171 $current_display = $current;
172 $current = false;
173
174 foreach ( $this->get() as $_slug => $data ) {
175 if ( empty( $data['sections'] ) ) {
176 continue;
177 }
178
179 foreach ( $data['sections'] as $_slug => $data ) {
180 if ( array_key_exists( 'slug', $data ) && ( $data['slug'] === $current_display ) ) {
181 $current = $_slug;
182 break 2;
183 }
184 }
185 }
186 }
187
188 return $current;
189 }
190
191
192 /**
193 * @param bool $tab
194 * @param bool $with_section
195 * @param LP_User $user
196 *
197 * @return string
198 */
199 public function get_tab_link( $tab = false, $with_section = false, $user = null ) {
200 if ( ( $tab || $with_section ) && empty( $user ) ) {
201 $current_user = learn_press_get_current_user();
202 $user = $current_user->get_username();
203 }
204
205 $args = array( 'user' => $user );
206
207 if ( isset( $args['user'] ) ) {
208 if ( false === $tab ) {
209 $tab = $this->get_current_tab( null, false );
210 }
211
212 $tab_data = $this->get_tab_at( $tab );
213 $tab = $this->get_slug( $tab_data, $tab );
214
215 if ( $tab ) {
216 $args['tab'] = $tab;
217 } else {
218 unset( $args['user'] );
219 }
220
221 if ( $with_section && ! empty( $tab_data['sections'] ) ) {
222 if ( $with_section === true ) {
223 $section_keys = array_keys( $tab_data['sections'] );
224 $first_section = reset( $section_keys );
225 $with_section = $this->get_slug( $tab_data['sections'][ $first_section ], $first_section );
226 }
227 $args['section'] = $with_section;
228 }
229 }
230
231 $args = array_map( '_learn_press_urlencode', $args );
232 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
233
234 if ( $profile_link ) {
235 if ( get_option( 'permalink_structure' ) ) {
236 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
237 } else {
238 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
239 }
240 } else {
241 $url = get_author_posts_url( $user->get_id() );
242 }
243
244 return apply_filters( 'learnpress/profile/tab/link', $url, $tab, $with_section, $user );
245 }
246
247 /**
248 * Get the slug of tab or section if defined.
249 *
250 * @param array $tab_or_section
251 * @param string $default
252 *
253 * @return string
254 */
255 public function get_slug( $tab_or_section, $default = '' ) {
256 if ( is_array( $tab_or_section ) ) {
257 return array_key_exists( 'slug', $tab_or_section ) ? $tab_or_section['slug'] : false;
258 }
259
260 if ( is_string( $tab_or_section ) ) {
261 return $tab_or_section;
262 }
263
264 return $tab_or_section ? $tab_or_section->get( 'slug' ) : false;
265 }
266
267 /**
268 * Get current link of profile
269 *
270 * @param string $args - Optional. Add more query args to url.
271 * @param bool $with_permalink - Optional. TRUE to build url as friendly url.
272 *
273 * @return mixed|string
274 */
275 public function get_current_url( $args = '', $with_permalink = false ) {
276 $current_tab = $this->get_current_tab();
277 $tab = $this->get_tab_at( $current_tab );
278 $sections = isset( $tab['sections'] ) ? $tab['sections'] : array();
279
280 $current_section_slug = $this->get_current_section();
281 $section = array();
282
283 if ( isset( $sections[ $current_section_slug ] ) ) {
284 $sections[ $current_section_slug ];
285 } elseif ( $sections && ! empty( $sections ) ) {
286 reset( $sections );
287 }
288
289 if ( array_key_exists( 'slug', $section ) ) {
290 $current_section_slug = $section['slug'];
291 }
292 $url = $this->get_tab_link( $this->get_current_tab(), $current_section_slug, $this->get_profile()->get_user()->get_username() );
293
294 if ( is_array( $args ) && $args ) {
295 if ( ! $with_permalink ) {
296 $url = esc_url_raw( add_query_arg( $args, $url ) );
297 } else {
298 $parts = array();
299
300 foreach ( $args as $k => $v ) {
301 $parts[] = "{$k}/{$v}";
302 }
303
304 $url = trailingslashit( $url ) . join( '/', $parts ) . '/';
305 }
306 }
307
308 return $url;
309 }
310
311 /**
312 * Get tab data at a position.
313 *
314 * @param int $position Optional. Indexed number or slug.
315 *
316 * @return mixed
317 */
318 public function get_tab_at( $position = 0 ) {
319 if ( ! $position ) {
320 $position = 0;
321 }
322
323 if ( $this->get() ) {
324 $tabs = $this->get();
325
326 if ( is_numeric( $position ) ) {
327 $tabs = array_values( $tabs );
328 if ( ! empty( $tabs[ $position ] ) ) {
329 return $tabs[ $position ];
330 }
331 } else {
332 if ( ! empty( $tabs[ $position ] ) ) {
333 return $tabs[ $position ];
334 }
335 }
336 }
337
338 return false;
339 }
340
341 public function tabs() {
342 return $this->get();
343 }
344
345 public function get( $key = false ) {
346 return false !== $key ? ( array_key_exists( $key, $this->_data ) ? $this->_data[ $key ] : false ) : $this->_data;
347 }
348
349 protected function _sort_tabs( $a, $b ) {
350 if ( $a['priority'] === $b['priority'] ) {
351 return 0;
352 }
353
354 return $a['priority'] < $b['priority'] ? - 1 : 1;
355 }
356
357 /**
358 * Remove tab.
359 *
360 * @param $key
361 */
362 public function remove_tab( $key ) {
363 $tabs = $this->_data;
364
365 foreach ( $tabs as $slug => $data ) {
366 if ( $key == $slug ) {
367 unset( $tabs[ $key ] );
368 }
369 }
370
371 $this->_data = $tabs;
372 }
373
374 public function current_user_can_view( $key = '' ) {
375 global $wp;
376
377 if ( ! $key ) {
378 $key = isset( $wp->query_vars['view'] ) ? $wp->query_vars['view'] : 'overview';
379 }
380
381 $tab = isset( $this->_data[ $key ] ) ? $this->_data[ $key ] : false;
382
383 if ( $tab ) {
384 return $tab->user_can_view();
385 }
386
387 return false;
388 }
389 }
390
391 /**
392 * Class LP_Profile_Tab
393 *
394 * @since 3.0.0
395 */
396 class LP_Profile_Tab extends LP_Array_Access {
397
398 /**
399 * @var LP_Profile
400 */
401 protected $profile = null;
402
403 /**
404 * @var string
405 */
406 public $id = '';
407
408 /**
409 * LP_Profile_Tab constructor.
410 *
411 * @param string $id
412 * @param array $data
413 * @param LP_Profile $profile
414 */
415 public function __construct( $id, $data, $profile ) {
416 parent::__construct( $data );
417
418 $this->profile = $profile;
419 $this->id = $id;
420 }
421
422 public function sections() {
423 $profile = $this->get_profile();
424 $sections = array();
425 $all_sections = $this->get( 'sections' );
426
427 if ( $all_sections ) {
428 foreach ( $all_sections as $section_key => $section ) {
429
430 if ( $profile->is_hidden( $section ) ) {
431 continue;
432 }
433 $sections[ $section_key ] = $section;
434 }
435 }
436
437 return $sections;
438 }
439
440 public function get( $key = false ) {
441 return false !== $key ? ( array_key_exists( $key, $this->_data ) ? $this->_data[ $key ] : false ) : $this->_data;
442 }
443
444 public function get_profile() {
445 return $this->profile;
446 }
447
448 public function user_can_view() {
449 if ( $this->is_public() || current_user_can( ADMIN_ROLE ) ) {
450 return true;
451 }
452
453 $can = $this->get_profile()->current_user_can( "view-tab-{$this->id}" );
454
455 return $can;
456 }
457
458 public function user_can_view_section( $section ) {
459 return $this->get_profile()->current_user_can( "view-section-{$section}" );
460 }
461
462 public function is_hidden() {
463 return $this->get( 'hidden' );
464 }
465
466 public function tab_is_visible_for_user() {
467 return $this->is_current();
468 }
469
470 public function is_current() {
471 return isset( $this['is_current'] ) ? $this['is_current'] : false;
472 }
473
474 /**
475 * Tab is public for all users can view.
476 *
477 * @return bool
478 * @since 4.0.0
479 */
480 public function is_public() {
481 $public_tabs = $this->profile->get_public_tabs();
482
483 return $public_tabs && in_array( $this->id, $public_tabs );
484 }
485 }
486