PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 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 All 140 releases
← All changes | inc/user/class-lp-profile-tabs.php +105 -153 4.1.6.9 → 4.4.9 View file →
@@ -1,12 +1,17 @@
1 1 <?php
2 2
3 +use LearnPress\Models\UserModel;
4 +
3 5 /**
4 6 * Class LP_Profile_Tabs
5 7 */
6 -class LP_Profile_Tabs extends LP_Array_Access {
7 -
8 +class LP_Profile_Tabs {
8 9 /**
10 + * @var array
11 + */
12 + protected $_data = array();
13 + /**
9 14 * @var LP_Profile_Tabs
10 15 */
11 16 protected static $_instance = null;
12 17
@@ -17,80 +22,44 @@
17 22
18 23 /**
19 24 * LP_Profile_Tabs constructor.
20 25 *
21 - * @param array $tabs
26 + * @param array $tabs
22 27 * @param LP_Profile $profile
23 28 */
24 29 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;
30 + $tabs_tmp = [];
31 + foreach ( $tabs as $k => $v ) {
32 + $tabs_tmp[ $k ] = $v;
33 + $tabs_tmp[ $k ]['key_index'] = $k;
34 + if ( ! array_key_exists( 'priority', $v ) ) {
35 + $tabs_tmp[ $k ]['priority'] = 10;
49 36 }
50 - $this->_data[ $k ] = new LP_Profile_Tab( $k, $v, $this->get_profile() );
37 + if ( ! array_key_exists( 'slug', $v ) ) {
38 + $tabs_tmp[ $k ]['slug'] = $k;
39 + }
51 40 }
52 - }
53 41
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;
42 + // Sort tab by priority.
43 + usort(
44 + $tabs_tmp,
45 + function ( $tab1, $tab2 ) {
46 + if ( $tab1['priority'] < $tab2['priority'] ) {
47 + return - 1;
48 + } elseif ( $tab1['priority'] > $tab2['priority'] ) {
49 + return 1;
50 + } else {
51 + return 0;
75 52 }
76 -
77 - if ( ! array_key_exists( 'priority', $section_data ) ) {
78 - $tabs[ $slug ]['sections'][ $section_slug ]['priority'] = 10;
79 - }
80 53 }
81 - uasort( $tabs[ $slug ]['sections'], array( $this, '_sort_tabs' ) );
82 - }
54 + );
83 55
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 );
56 + foreach ( $tabs_tmp as $v ) {
57 + $k = $v['key_index'];
58 + $this->_data[ $k ] = new LP_Profile_Tab( $k, $v, $profile );
90 59 }
91 60
92 - $this->_data = $tabs;
61 + $this->profile = $profile;
93 62 }
94 63
95 64 /**
96 65 * @return LP_Profile|null
@@ -107,11 +76,11 @@
107 76 $current = sanitize_text_field( $_REQUEST['view'] );
108 77 } elseif ( ! empty( $wp->query_vars['view'] ) ) {
109 78 $current = $wp->query_vars['view'];
110 79 } else {
111 - if ( $this->get_tab_at() ) {
112 - $tab = $this->get_tab_at();
113 - $current = $tab['slug'];
80 + $tab = $this->get_tab_at();
81 + if ( $tab instanceof LP_Profile_Tab ) {
82 + $current = $tab->get( 'slug' );
114 83 }
115 84 }
116 85
117 86 if ( $key ) {
@@ -150,18 +119,17 @@
150 119 } else {
151 120 $current_tab = $tab;
152 121 }
153 122
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'];
123 + $tab = $this->get_tab_at( $current_tab );
124 + if ( $tab instanceof LP_Profile_Tab ) {
125 + if ( ! empty( $tab->get( 'sections' ) ) ) {
126 + $sections = $tab->get( 'sections' );
159 127 $section = reset( $sections );
160 128 if ( array_key_exists( 'slug', $section ) ) {
161 - $current = $section['slug'];
129 + $current = $tab->get( 'slug' );
162 130 } else {
163 - $sections = array_keys( $tab['sections'] );
131 + $sections = array_keys( $tab->get( 'sections' ) );
164 132 $current = reset( $sections );
165 133 }
166 134 }
167 135 }
@@ -171,13 +139,13 @@
171 139 $current_display = $current;
172 140 $current = false;
173 141
174 142 foreach ( $this->get() as $_slug => $data ) {
175 - if ( empty( $data['sections'] ) ) {
143 + if ( empty( $data->get( 'sections' ) ) ) {
176 144 continue;
177 145 }
178 146
179 - foreach ( $data['sections'] as $_slug => $data ) {
147 + foreach ( $data->get( 'sections' ) as $_slug => $data ) {
180 148 if ( array_key_exists( 'slug', $data ) && ( $data['slug'] === $current_display ) ) {
181 149 $current = $_slug;
182 150 break 2;
183 151 }
@@ -189,49 +157,44 @@
189 157 }
190 158
191 159
192 160 /**
193 - * @param bool $tab
194 - * @param bool $with_section
195 - * @param LP_User $user
161 + * @param bool $tab
162 + * @param bool $with_section
163 + * @param string $user_slug
196 164 *
197 165 * @return string
166 + * @since 3.0.0
167 + * @version 4.0.1
198 168 */
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();
169 + public function get_tab_link( $tab = false, $with_section = false, $user_slug = '' ) {
170 + $url = '';
171 + $args = array( 'user' => $user_slug );
172 +
173 + if ( false === $tab ) {
174 + $tab = $this->get_current_tab( null, false );
203 175 }
204 176
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;
177 + $tab_data = $this->get_tab_at( $tab );
178 + if ( $tab_data instanceof LP_Profile_Tab ) {
179 + $slug = $this->get_slug( $tab_data, $tab );
180 + if ( $slug ) {
181 + $args['tab'] = $slug;
217 182 } else {
218 183 unset( $args['user'] );
219 184 }
220 185
221 - if ( $with_section && ! empty( $tab_data['sections'] ) ) {
186 + if ( $with_section && ! empty( $tab_data->get( 'sections' ) ) ) {
222 187 if ( $with_section === true ) {
223 - $section_keys = array_keys( $tab_data['sections'] );
188 + $section_keys = array_keys( $tab_data->get( 'sections' ) );
224 189 $first_section = reset( $section_keys );
225 - $with_section = $this->get_slug( $tab_data['sections'][ $first_section ], $first_section );
190 + $with_section = $this->get_slug( $tab_data->get( 'sections' )[ $first_section ], $first_section );
226 191 }
227 192 $args['section'] = $with_section;
228 193 }
229 194 }
230 195
231 - $args = array_map( '_learn_press_urlencode', $args );
232 196 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
233 -
234 197 if ( $profile_link ) {
235 198 if ( get_option( 'permalink_structure' ) ) {
236 199 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
237 200 } else {
@@ -236,19 +199,17 @@
236 199 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
237 200 } else {
238 201 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
239 202 }
240 - } else {
241 - $url = get_author_posts_url( $user->get_id() );
242 203 }
243 204
244 - return apply_filters( 'learnpress/profile/tab/link', $url, $tab, $with_section, $user );
205 + return apply_filters( 'learnpress/profile/tab/link', $url, $tab, $with_section, $user_slug );
245 206 }
246 207
247 208 /**
248 209 * Get the slug of tab or section if defined.
249 210 *
250 - * @param array $tab_or_section
211 + * @param $tab_or_section
251 212 * @param string $default
252 213 *
253 214 * @return string
254 215 */
@@ -267,31 +228,37 @@
267 228 /**
268 229 * Get current link of profile
269 230 *
270 231 * @param string $args - Optional. Add more query args to url.
271 - * @param bool $with_permalink - Optional. TRUE to build url as friendly url.
232 + * @param bool $with_permalink - Optional. TRUE to build url as friendly url.
272 233 *
273 234 * @return mixed|string
235 + * @Todo tungnx - need check this function
274 236 */
275 237 public function get_current_url( $args = '', $with_permalink = false ) {
276 238 $current_tab = $this->get_current_tab();
277 239 $tab = $this->get_tab_at( $current_tab );
278 - $sections = isset( $tab['sections'] ) ? $tab['sections'] : array();
240 + if ( ! $tab instanceof LP_Profile_Tab ) {
241 + return '';
242 + }
279 243
244 + $lp_profile = $this->get_profile();
245 + /**
246 + * @var $lp_user LP_User
247 + */
248 + $lp_user = $lp_profile->get_user();
280 249 $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 );
250 + if ( ! $lp_user instanceof WP_User ) {
251 + return '';
287 252 }
288 253
289 - if ( array_key_exists( 'slug', $section ) ) {
290 - $current_section_slug = $section['slug'];
254 + $userModel = UserModel::find( $lp_user->get_id(), true );
255 + if ( ! $userModel ) {
256 + return '';
291 257 }
292 - $url = $this->get_tab_link( $this->get_current_tab(), $current_section_slug, $this->get_profile()->get_user()->get_username() );
293 258
259 + $user_pretty_slug = $userModel->get_slug_link();
260 + $url = $this->get_tab_link( $this->get_current_tab(), $current_section_slug, $user_pretty_slug );
294 261 if ( is_array( $args ) && $args ) {
295 262 if ( ! $with_permalink ) {
296 263 $url = esc_url_raw( add_query_arg( $args, $url ) );
297 264 } else {
@@ -312,9 +279,9 @@
312 279 * Get tab data at a position.
313 280 *
314 281 * @param int $position Optional. Indexed number or slug.
315 282 *
316 - * @return mixed
283 + * @return false|LP_Profile_Tab
317 284 */
318 285 public function get_tab_at( $position = 0 ) {
319 286 if ( ! $position ) {
320 287 $position = 0;
@@ -352,41 +319,8 @@
352 319 }
353 320
354 321 return $a['priority'] < $b['priority'] ? - 1 : 1;
355 322 }
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 323 }
390 324
391 325 /**
392 326 * Class LP_Profile_Tab
@@ -392,10 +326,10 @@
392 326 * Class LP_Profile_Tab
393 327 *
394 328 * @since 3.0.0
395 329 */
396 -class LP_Profile_Tab extends LP_Array_Access {
397 -
330 +class LP_Profile_Tab {
331 + protected $_data = array();
398 332 /**
399 333 * @var LP_Profile
400 334 */
401 335 protected $profile = null;
@@ -407,14 +341,15 @@
407 341
408 342 /**
409 343 * LP_Profile_Tab constructor.
410 344 *
411 - * @param string $id
412 - * @param array $data
345 + * @param string $id
346 + * @param array $data
413 347 * @param LP_Profile $profile
414 348 */
415 349 public function __construct( $id, $data, $profile ) {
416 - parent::__construct( $data );
350 + //parent::__construct( $data );
351 + $this->_data = is_array( $data ) ? $data : (array) $data;
417 352
418 353 $this->profile = $profile;
419 354 $this->id = $id;
420 355 }
@@ -444,9 +379,15 @@
444 379 public function get_profile() {
445 380 return $this->profile;
446 381 }
447 382
383 + /**
384 + * @deprecated 4.2.6.2
385 + */
448 386 public function user_can_view() {
387 + _deprecated_function( __METHOD__, '4.2.6.2' );
388 +
389 + return false;
449 390 if ( $this->is_public() || current_user_can( ADMIN_ROLE ) ) {
450 391 return true;
451 392 }
452 393
@@ -454,9 +395,16 @@
454 395
455 396 return $can;
456 397 }
457 398
399 + /**
400 + * @deprecated 4.2.6.2
401 + */
458 402 public function user_can_view_section( $section ) {
403 + _deprecated_function( __METHOD__, '4.2.6.2' );
404 +
405 + return false;
406 +
459 407 return $this->get_profile()->current_user_can( "view-section-{$section}" );
460 408 }
461 409
462 410 public function is_hidden() {
@@ -475,10 +423,14 @@
475 423 * Tab is public for all users can view.
476 424 *
477 425 * @return bool
478 426 * @since 4.0.0
427 + * @deprecated 4.2.6.2
479 428 */
480 429 public function is_public() {
430 + _deprecated_function( __METHOD__, '4.2.6.2' );
431 +
432 + return false;
481 433 $public_tabs = $this->profile->get_public_tabs();
482 434
483 435 return $public_tabs && in_array( $this->id, $public_tabs );
484 436 }