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 -157 4.1.7.3.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,79 +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 - $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;
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;
48 36 }
49 - $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 + }
50 40 }
51 - }
52 41
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;
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;
74 52 }
75 -
76 - if ( ! array_key_exists( 'priority', $section_data ) ) {
77 - $tabs[ $slug ]['sections'][ $section_slug ]['priority'] = 10;
78 - }
79 53 }
80 - uasort( $tabs[ $slug ]['sections'], array( $this, '_sort_tabs' ) );
81 - }
54 + );
82 55
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 );
56 + foreach ( $tabs_tmp as $v ) {
57 + $k = $v['key_index'];
58 + $this->_data[ $k ] = new LP_Profile_Tab( $k, $v, $profile );
89 59 }
90 60
91 - $this->_data = $tabs;
61 + $this->profile = $profile;
92 62 }
93 63
94 64 /**
95 65 * @return LP_Profile|null
@@ -106,11 +76,11 @@
106 76 $current = sanitize_text_field( $_REQUEST['view'] );
107 77 } elseif ( ! empty( $wp->query_vars['view'] ) ) {
108 78 $current = $wp->query_vars['view'];
109 79 } else {
110 - if ( $this->get_tab_at() ) {
111 - $tab = $this->get_tab_at();
112 - $current = $tab['slug'];
80 + $tab = $this->get_tab_at();
81 + if ( $tab instanceof LP_Profile_Tab ) {
82 + $current = $tab->get( 'slug' );
113 83 }
114 84 }
115 85
116 86 if ( $key ) {
@@ -149,18 +119,17 @@
149 119 } else {
150 120 $current_tab = $tab;
151 121 }
152 122
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'];
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' );
158 127 $section = reset( $sections );
159 128 if ( array_key_exists( 'slug', $section ) ) {
160 - $current = $section['slug'];
129 + $current = $tab->get( 'slug' );
161 130 } else {
162 - $sections = array_keys( $tab['sections'] );
131 + $sections = array_keys( $tab->get( 'sections' ) );
163 132 $current = reset( $sections );
164 133 }
165 134 }
166 135 }
@@ -170,13 +139,13 @@
170 139 $current_display = $current;
171 140 $current = false;
172 141
173 142 foreach ( $this->get() as $_slug => $data ) {
174 - if ( empty( $data['sections'] ) ) {
143 + if ( empty( $data->get( 'sections' ) ) ) {
175 144 continue;
176 145 }
177 146
178 - foreach ( $data['sections'] as $_slug => $data ) {
147 + foreach ( $data->get( 'sections' ) as $_slug => $data ) {
179 148 if ( array_key_exists( 'slug', $data ) && ( $data['slug'] === $current_display ) ) {
180 149 $current = $_slug;
181 150 break 2;
182 151 }
@@ -188,54 +157,44 @@
188 157 }
189 158
190 159
191 160 /**
192 - * @param bool $tab
193 - * @param bool $with_section
194 - * @param LP_User $user
161 + * @param bool $tab
162 + * @param bool $with_section
163 + * @param string $user_slug
195 164 *
196 165 * @return string
166 + * @since 3.0.0
167 + * @version 4.0.1
197 168 */
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();
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 );
202 175 }
203 176
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;
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;
216 182 } else {
217 183 unset( $args['user'] );
218 184 }
219 185
220 - if ( $with_section && ! empty( $tab_data['sections'] ) ) {
186 + if ( $with_section && ! empty( $tab_data->get( 'sections' ) ) ) {
221 187 if ( $with_section === true ) {
222 - $section_keys = array_keys( $tab_data['sections'] );
188 + $section_keys = array_keys( $tab_data->get( 'sections' ) );
223 189 $first_section = reset( $section_keys );
224 - $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 );
225 191 }
226 192 $args['section'] = $with_section;
227 193 }
228 194 }
229 195
230 - /*$args = array_map(
231 - function ( $string ) {
232 - return preg_replace( '/\s/', '+', $string );
233 - },
234 - $args
235 - );*/
236 196 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
237 -
238 197 if ( $profile_link ) {
239 198 if ( get_option( 'permalink_structure' ) ) {
240 199 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
241 200 } else {
@@ -240,19 +199,17 @@
240 199 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
241 200 } else {
242 201 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
243 202 }
244 - } else {
245 - $url = get_author_posts_url( $user->get_id() );
246 203 }
247 204
248 - 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 );
249 206 }
250 207
251 208 /**
252 209 * Get the slug of tab or section if defined.
253 210 *
254 - * @param array $tab_or_section
211 + * @param $tab_or_section
255 212 * @param string $default
256 213 *
257 214 * @return string
258 215 */
@@ -271,31 +228,37 @@
271 228 /**
272 229 * Get current link of profile
273 230 *
274 231 * @param string $args - Optional. Add more query args to url.
275 - * @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.
276 233 *
277 234 * @return mixed|string
235 + * @Todo tungnx - need check this function
278 236 */
279 237 public function get_current_url( $args = '', $with_permalink = false ) {
280 238 $current_tab = $this->get_current_tab();
281 239 $tab = $this->get_tab_at( $current_tab );
282 - $sections = isset( $tab['sections'] ) ? $tab['sections'] : array();
240 + if ( ! $tab instanceof LP_Profile_Tab ) {
241 + return '';
242 + }
283 243
244 + $lp_profile = $this->get_profile();
245 + /**
246 + * @var $lp_user LP_User
247 + */
248 + $lp_user = $lp_profile->get_user();
284 249 $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 );
250 + if ( ! $lp_user instanceof WP_User ) {
251 + return '';
291 252 }
292 253
293 - if ( array_key_exists( 'slug', $section ) ) {
294 - $current_section_slug = $section['slug'];
254 + $userModel = UserModel::find( $lp_user->get_id(), true );
255 + if ( ! $userModel ) {
256 + return '';
295 257 }
296 - $url = $this->get_tab_link( $this->get_current_tab(), $current_section_slug, $this->get_profile()->get_user()->get_username() );
297 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 );
298 261 if ( is_array( $args ) && $args ) {
299 262 if ( ! $with_permalink ) {
300 263 $url = esc_url_raw( add_query_arg( $args, $url ) );
301 264 } else {
@@ -316,9 +279,9 @@
316 279 * Get tab data at a position.
317 280 *
318 281 * @param int $position Optional. Indexed number or slug.
319 282 *
320 - * @return mixed
283 + * @return false|LP_Profile_Tab
321 284 */
322 285 public function get_tab_at( $position = 0 ) {
323 286 if ( ! $position ) {
324 287 $position = 0;
@@ -356,41 +319,8 @@
356 319 }
357 320
358 321 return $a['priority'] < $b['priority'] ? - 1 : 1;
359 322 }
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 323 }
394 324
395 325 /**
396 326 * Class LP_Profile_Tab
@@ -396,10 +326,10 @@
396 326 * Class LP_Profile_Tab
397 327 *
398 328 * @since 3.0.0
399 329 */
400 -class LP_Profile_Tab extends LP_Array_Access {
401 -
330 +class LP_Profile_Tab {
331 + protected $_data = array();
402 332 /**
403 333 * @var LP_Profile
404 334 */
405 335 protected $profile = null;
@@ -411,14 +341,15 @@
411 341
412 342 /**
413 343 * LP_Profile_Tab constructor.
414 344 *
415 - * @param string $id
416 - * @param array $data
345 + * @param string $id
346 + * @param array $data
417 347 * @param LP_Profile $profile
418 348 */
419 349 public function __construct( $id, $data, $profile ) {
420 - parent::__construct( $data );
350 + //parent::__construct( $data );
351 + $this->_data = is_array( $data ) ? $data : (array) $data;
421 352
422 353 $this->profile = $profile;
423 354 $this->id = $id;
424 355 }
@@ -448,9 +379,15 @@
448 379 public function get_profile() {
449 380 return $this->profile;
450 381 }
451 382
383 + /**
384 + * @deprecated 4.2.6.2
385 + */
452 386 public function user_can_view() {
387 + _deprecated_function( __METHOD__, '4.2.6.2' );
388 +
389 + return false;
453 390 if ( $this->is_public() || current_user_can( ADMIN_ROLE ) ) {
454 391 return true;
455 392 }
456 393
@@ -458,9 +395,16 @@
458 395
459 396 return $can;
460 397 }
461 398
399 + /**
400 + * @deprecated 4.2.6.2
401 + */
462 402 public function user_can_view_section( $section ) {
403 + _deprecated_function( __METHOD__, '4.2.6.2' );
404 +
405 + return false;
406 +
463 407 return $this->get_profile()->current_user_can( "view-section-{$section}" );
464 408 }
465 409
466 410 public function is_hidden() {
@@ -479,10 +423,14 @@
479 423 * Tab is public for all users can view.
480 424 *
481 425 * @return bool
482 426 * @since 4.0.0
427 + * @deprecated 4.2.6.2
483 428 */
484 429 public function is_public() {
430 + _deprecated_function( __METHOD__, '4.2.6.2' );
431 +
432 + return false;
485 433 $public_tabs = $this->profile->get_public_tabs();
486 434
487 435 return $public_tabs && in_array( $this->id, $public_tabs );
488 436 }