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 -158 4.1.7.2 → 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,54 +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(
232 - function ( $string ) {
233 - return preg_replace( '/\s/', '+', $string );
234 - },
235 - $args
236 - );*/
237 196 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
238 -
239 197 if ( $profile_link ) {
240 198 if ( get_option( 'permalink_structure' ) ) {
241 199 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
242 200 } else {
@@ -241,19 +199,17 @@
241 199 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
242 200 } else {
243 201 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
244 202 }
245 - } else {
246 - $url = get_author_posts_url( $user->get_id() );
247 203 }
248 204
249 - 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 );
250 206 }
251 207
252 208 /**
253 209 * Get the slug of tab or section if defined.
254 210 *
255 - * @param array $tab_or_section
211 + * @param $tab_or_section
256 212 * @param string $default
257 213 *
258 214 * @return string
259 215 */
@@ -272,31 +228,37 @@
272 228 /**
273 229 * Get current link of profile
274 230 *
275 231 * @param string $args - Optional. Add more query args to url.
276 - * @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.
277 233 *
278 234 * @return mixed|string
235 + * @Todo tungnx - need check this function
279 236 */
280 237 public function get_current_url( $args = '', $with_permalink = false ) {
281 238 $current_tab = $this->get_current_tab();
282 239 $tab = $this->get_tab_at( $current_tab );
283 - $sections = isset( $tab['sections'] ) ? $tab['sections'] : array();
240 + if ( ! $tab instanceof LP_Profile_Tab ) {
241 + return '';
242 + }
284 243
244 + $lp_profile = $this->get_profile();
245 + /**
246 + * @var $lp_user LP_User
247 + */
248 + $lp_user = $lp_profile->get_user();
285 249 $current_section_slug = $this->get_current_section();
286 - $section = array();
287 -
288 - if ( isset( $sections[ $current_section_slug ] ) ) {
289 - $sections[ $current_section_slug ];
290 - } elseif ( $sections && ! empty( $sections ) ) {
291 - reset( $sections );
250 + if ( ! $lp_user instanceof WP_User ) {
251 + return '';
292 252 }
293 253
294 - if ( array_key_exists( 'slug', $section ) ) {
295 - $current_section_slug = $section['slug'];
254 + $userModel = UserModel::find( $lp_user->get_id(), true );
255 + if ( ! $userModel ) {
256 + return '';
296 257 }
297 - $url = $this->get_tab_link( $this->get_current_tab(), $current_section_slug, $this->get_profile()->get_user()->get_username() );
298 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 );
299 261 if ( is_array( $args ) && $args ) {
300 262 if ( ! $with_permalink ) {
301 263 $url = esc_url_raw( add_query_arg( $args, $url ) );
302 264 } else {
@@ -317,9 +279,9 @@
317 279 * Get tab data at a position.
318 280 *
319 281 * @param int $position Optional. Indexed number or slug.
320 282 *
321 - * @return mixed
283 + * @return false|LP_Profile_Tab
322 284 */
323 285 public function get_tab_at( $position = 0 ) {
324 286 if ( ! $position ) {
325 287 $position = 0;
@@ -357,41 +319,8 @@
357 319 }
358 320
359 321 return $a['priority'] < $b['priority'] ? - 1 : 1;
360 322 }
361 -
362 - /**
363 - * Remove tab.
364 - *
365 - * @param $key
366 - */
367 - public function remove_tab( $key ) {
368 - $tabs = $this->_data;
369 -
370 - foreach ( $tabs as $slug => $data ) {
371 - if ( $key == $slug ) {
372 - unset( $tabs[ $key ] );
373 - }
374 - }
375 -
376 - $this->_data = $tabs;
377 - }
378 -
379 - public function current_user_can_view( $key = '' ) {
380 - global $wp;
381 -
382 - if ( ! $key ) {
383 - $key = isset( $wp->query_vars['view'] ) ? $wp->query_vars['view'] : 'overview';
384 - }
385 -
386 - $tab = isset( $this->_data[ $key ] ) ? $this->_data[ $key ] : false;
387 -
388 - if ( $tab ) {
389 - return $tab->user_can_view();
390 - }
391 -
392 - return false;
393 - }
394 323 }
395 324
396 325 /**
397 326 * Class LP_Profile_Tab
@@ -397,10 +326,10 @@
397 326 * Class LP_Profile_Tab
398 327 *
399 328 * @since 3.0.0
400 329 */
401 -class LP_Profile_Tab extends LP_Array_Access {
402 -
330 +class LP_Profile_Tab {
331 + protected $_data = array();
403 332 /**
404 333 * @var LP_Profile
405 334 */
406 335 protected $profile = null;
@@ -412,14 +341,15 @@
412 341
413 342 /**
414 343 * LP_Profile_Tab constructor.
415 344 *
416 - * @param string $id
417 - * @param array $data
345 + * @param string $id
346 + * @param array $data
418 347 * @param LP_Profile $profile
419 348 */
420 349 public function __construct( $id, $data, $profile ) {
421 - parent::__construct( $data );
350 + //parent::__construct( $data );
351 + $this->_data = is_array( $data ) ? $data : (array) $data;
422 352
423 353 $this->profile = $profile;
424 354 $this->id = $id;
425 355 }
@@ -449,9 +379,15 @@
449 379 public function get_profile() {
450 380 return $this->profile;
451 381 }
452 382
383 + /**
384 + * @deprecated 4.2.6.2
385 + */
453 386 public function user_can_view() {
387 + _deprecated_function( __METHOD__, '4.2.6.2' );
388 +
389 + return false;
454 390 if ( $this->is_public() || current_user_can( ADMIN_ROLE ) ) {
455 391 return true;
456 392 }
457 393
@@ -459,9 +395,16 @@
459 395
460 396 return $can;
461 397 }
462 398
399 + /**
400 + * @deprecated 4.2.6.2
401 + */
463 402 public function user_can_view_section( $section ) {
403 + _deprecated_function( __METHOD__, '4.2.6.2' );
404 +
405 + return false;
406 +
464 407 return $this->get_profile()->current_user_can( "view-section-{$section}" );
465 408 }
466 409
467 410 public function is_hidden() {
@@ -480,10 +423,14 @@
480 423 * Tab is public for all users can view.
481 424 *
482 425 * @return bool
483 426 * @since 4.0.0
427 + * @deprecated 4.2.6.2
484 428 */
485 429 public function is_public() {
430 + _deprecated_function( __METHOD__, '4.2.6.2' );
431 +
432 + return false;
486 433 $public_tabs = $this->profile->get_public_tabs();
487 434
488 435 return $public_tabs && in_array( $this->id, $public_tabs );
489 436 }