PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3
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.7.3, at inc/user/class-lp-profile-tabs.php

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