| @@ -1,437 +1,437 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -use LearnPress\Models\UserModel; | |
| 4 | - | |
| 5 | -/** | |
| 6 | - * Class LP_Profile_Tabs | |
| 7 | - */ | |
| 8 | -class LP_Profile_Tabs { | |
| 9 | - /** | |
| 10 | - * @var array | |
| 11 | - */ | |
| 12 | - protected $_data = array(); | |
| 13 | - /** | |
| 14 | - * @var LP_Profile_Tabs | |
| 15 | - */ | |
| 16 | - protected static $_instance = null; | |
| 17 | - | |
| 18 | - /** | |
| 19 | - * @var LP_Profile | |
| 20 | - */ | |
| 21 | - protected $profile = null; | |
| 22 | - | |
| 23 | - /** | |
| 24 | - * LP_Profile_Tabs constructor. | |
| 25 | - * | |
| 26 | - * @param array $tabs | |
| 27 | - * @param LP_Profile $profile | |
| 28 | - */ | |
| 29 | - public function __construct( $tabs, $profile ) { | |
| 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; | |
| 36 | - } | |
| 37 | - if ( ! array_key_exists( 'slug', $v ) ) { | |
| 38 | - $tabs_tmp[ $k ]['slug'] = $k; | |
| 39 | - } | |
| 40 | - } | |
| 41 | - | |
| 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; | |
| 52 | - } | |
| 53 | - } | |
| 54 | - ); | |
| 55 | - | |
| 56 | - foreach ( $tabs_tmp as $v ) { | |
| 57 | - $k = $v['key_index']; | |
| 58 | - $this->_data[ $k ] = new LP_Profile_Tab( $k, $v, $profile ); | |
| 59 | - } | |
| 60 | - | |
| 61 | - $this->profile = $profile; | |
| 62 | - } | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * @return LP_Profile|null | |
| 66 | - */ | |
| 67 | - public function get_profile() { | |
| 68 | - return $this->profile; | |
| 69 | - } | |
| 70 | - | |
| 71 | - public function get_current_tab( $default = '', $key = true ) { | |
| 72 | - global $wp; | |
| 73 | - $current = $default; | |
| 74 | - | |
| 75 | - if ( ! empty( $_REQUEST['view'] ) ) { | |
| 76 | - $current = sanitize_text_field( $_REQUEST['view'] ); | |
| 77 | - } elseif ( ! empty( $wp->query_vars['view'] ) ) { | |
| 78 | - $current = $wp->query_vars['view']; | |
| 79 | - } else { | |
| 80 | - $tab = $this->get_tab_at(); | |
| 81 | - if ( $tab instanceof LP_Profile_Tab ) { | |
| 82 | - $current = $tab->get( 'slug' ); | |
| 83 | - } | |
| 84 | - } | |
| 85 | - | |
| 86 | - if ( $key ) { | |
| 87 | - $current_display = $current; | |
| 88 | - $current = false; | |
| 89 | - foreach ( $this->get() as $_slug => $data ) { | |
| 90 | - if ( is_object( $data ) ) { | |
| 91 | - if ( $data->get( 'slug' ) === $current_display ) { | |
| 92 | - $current = $_slug; | |
| 93 | - break; | |
| 94 | - } | |
| 95 | - } elseif ( is_array( $data ) ) { | |
| 96 | - if ( $data['slug'] === $current_display ) { | |
| 97 | - $current = $_slug; | |
| 98 | - break; | |
| 99 | - } | |
| 100 | - } | |
| 101 | - } | |
| 102 | - } | |
| 103 | - | |
| 104 | - return $current; | |
| 105 | - } | |
| 106 | - | |
| 107 | - public function get_current_section( $default = '', $key = true, $tab = '' ) { | |
| 108 | - global $wp; | |
| 109 | - | |
| 110 | - $current = $default; | |
| 111 | - | |
| 112 | - if ( ! empty( $_REQUEST['section'] ) ) { | |
| 113 | - $current = sanitize_text_field( $_REQUEST['section'] ); | |
| 114 | - } elseif ( ! empty( $wp->query_vars['section'] ) ) { | |
| 115 | - $current = $wp->query_vars['section']; | |
| 116 | - } else { | |
| 117 | - if ( false === $tab ) { | |
| 118 | - $current_tab = $this->get_current_tab(); | |
| 119 | - } else { | |
| 120 | - $current_tab = $tab; | |
| 121 | - } | |
| 122 | - | |
| 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' ); | |
| 127 | - $section = reset( $sections ); | |
| 128 | - if ( array_key_exists( 'slug', $section ) ) { | |
| 129 | - $current = $tab->get( 'slug' ); | |
| 130 | - } else { | |
| 131 | - $sections = array_keys( $tab->get( 'sections' ) ); | |
| 132 | - $current = reset( $sections ); | |
| 133 | - } | |
| 134 | - } | |
| 135 | - } | |
| 136 | - } | |
| 137 | - | |
| 138 | - if ( $key ) { | |
| 139 | - $current_display = $current; | |
| 140 | - $current = false; | |
| 141 | - | |
| 142 | - foreach ( $this->get() as $_slug => $data ) { | |
| 143 | - if ( empty( $data->get( 'sections' ) ) ) { | |
| 144 | - continue; | |
| 145 | - } | |
| 146 | - | |
| 147 | - foreach ( $data->get( 'sections' ) as $_slug => $data ) { | |
| 148 | - if ( array_key_exists( 'slug', $data ) && ( $data['slug'] === $current_display ) ) { | |
| 149 | - $current = $_slug; | |
| 150 | - break 2; | |
| 151 | - } | |
| 152 | - } | |
| 153 | - } | |
| 154 | - } | |
| 155 | - | |
| 156 | - return $current; | |
| 157 | - } | |
| 158 | - | |
| 159 | - | |
| 160 | - /** | |
| 161 | - * @param bool $tab | |
| 162 | - * @param bool $with_section | |
| 163 | - * @param string $user_slug | |
| 164 | - * | |
| 165 | - * @return string | |
| 166 | - * @since 3.0.0 | |
| 167 | - * @version 4.0.1 | |
| 168 | - */ | |
| 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 ); | |
| 175 | - } | |
| 176 | - | |
| 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; | |
| 182 | - } else { | |
| 183 | - unset( $args['user'] ); | |
| 184 | - } | |
| 185 | - | |
| 186 | - if ( $with_section && ! empty( $tab_data->get( 'sections' ) ) ) { | |
| 187 | - if ( $with_section === true ) { | |
| 188 | - $section_keys = array_keys( $tab_data->get( 'sections' ) ); | |
| 189 | - $first_section = reset( $section_keys ); | |
| 190 | - $with_section = $this->get_slug( $tab_data->get( 'sections' )[ $first_section ], $first_section ); | |
| 191 | - } | |
| 192 | - $args['section'] = $with_section; | |
| 193 | - } | |
| 194 | - } | |
| 195 | - | |
| 196 | - $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) ); | |
| 197 | - if ( $profile_link ) { | |
| 198 | - if ( get_option( 'permalink_structure' ) ) { | |
| 199 | - $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) ); | |
| 200 | - } else { | |
| 201 | - $url = esc_url_raw( add_query_arg( $args, $profile_link ) ); | |
| 202 | - } | |
| 203 | - } | |
| 204 | - | |
| 205 | - return apply_filters( 'learnpress/profile/tab/link', $url, $tab, $with_section, $user_slug ); | |
| 206 | - } | |
| 207 | - | |
| 208 | - /** | |
| 209 | - * Get the slug of tab or section if defined. | |
| 210 | - * | |
| 211 | - * @param $tab_or_section | |
| 212 | - * @param string $default | |
| 213 | - * | |
| 214 | - * @return string | |
| 215 | - */ | |
| 216 | - public function get_slug( $tab_or_section, $default = '' ) { | |
| 217 | - if ( is_array( $tab_or_section ) ) { | |
| 218 | - return array_key_exists( 'slug', $tab_or_section ) ? $tab_or_section['slug'] : false; | |
| 219 | - } | |
| 220 | - | |
| 221 | - if ( is_string( $tab_or_section ) ) { | |
| 222 | - return $tab_or_section; | |
| 223 | - } | |
| 224 | - | |
| 225 | - return $tab_or_section ? $tab_or_section->get( 'slug' ) : false; | |
| 226 | - } | |
| 227 | - | |
| 228 | - /** | |
| 229 | - * Get current link of profile | |
| 230 | - * | |
| 231 | - * @param string $args - Optional. Add more query args to url. | |
| 232 | - * @param bool $with_permalink - Optional. TRUE to build url as friendly url. | |
| 233 | - * | |
| 234 | - * @return mixed|string | |
| 235 | - * @Todo tungnx - need check this function | |
| 236 | - */ | |
| 237 | - public function get_current_url( $args = '', $with_permalink = false ) { | |
| 238 | - $current_tab = $this->get_current_tab(); | |
| 239 | - $tab = $this->get_tab_at( $current_tab ); | |
| 240 | - if ( ! $tab instanceof LP_Profile_Tab ) { | |
| 241 | - return ''; | |
| 242 | - } | |
| 243 | - | |
| 244 | - $lp_profile = $this->get_profile(); | |
| 245 | - /** | |
| 246 | - * @var $lp_user LP_User | |
| 247 | - */ | |
| 248 | - $lp_user = $lp_profile->get_user(); | |
| 249 | - $current_section_slug = $this->get_current_section(); | |
| 250 | - if ( ! $lp_user instanceof WP_User ) { | |
| 251 | - return ''; | |
| 252 | - } | |
| 253 | - | |
| 254 | - $userModel = UserModel::find( $lp_user->get_id(), true ); | |
| 255 | - if ( ! $userModel ) { | |
| 256 | - return ''; | |
| 257 | - } | |
| 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 ); | |
| 261 | - if ( is_array( $args ) && $args ) { | |
| 262 | - if ( ! $with_permalink ) { | |
| 263 | - $url = esc_url_raw( add_query_arg( $args, $url ) ); | |
| 264 | - } else { | |
| 265 | - $parts = array(); | |
| 266 | - | |
| 267 | - foreach ( $args as $k => $v ) { | |
| 268 | - $parts[] = "{$k}/{$v}"; | |
| 269 | - } | |
| 270 | - | |
| 271 | - $url = trailingslashit( $url ) . join( '/', $parts ) . '/'; | |
| 272 | - } | |
| 273 | - } | |
| 274 | - | |
| 275 | - return $url; | |
| 276 | - } | |
| 277 | - | |
| 278 | - /** | |
| 279 | - * Get tab data at a position. | |
| 280 | - * | |
| 281 | - * @param int $position Optional. Indexed number or slug. | |
| 282 | - * | |
| 283 | - * @return false|LP_Profile_Tab | |
| 284 | - */ | |
| 285 | - public function get_tab_at( $position = 0 ) { | |
| 286 | - if ( ! $position ) { | |
| 287 | - $position = 0; | |
| 288 | - } | |
| 289 | - | |
| 290 | - if ( $this->get() ) { | |
| 291 | - $tabs = $this->get(); | |
| 292 | - | |
| 293 | - if ( is_numeric( $position ) ) { | |
| 294 | - $tabs = array_values( $tabs ); | |
| 295 | - if ( ! empty( $tabs[ $position ] ) ) { | |
| 296 | - return $tabs[ $position ]; | |
| 297 | - } | |
| 298 | - } else { | |
| 299 | - if ( ! empty( $tabs[ $position ] ) ) { | |
| 300 | - return $tabs[ $position ]; | |
| 301 | - } | |
| 302 | - } | |
| 303 | - } | |
| 304 | - | |
| 305 | - return false; | |
| 306 | - } | |
| 307 | - | |
| 308 | - public function tabs() { | |
| 309 | - return $this->get(); | |
| 310 | - } | |
| 311 | - | |
| 312 | - public function get( $key = false ) { | |
| 313 | - return false !== $key ? ( array_key_exists( $key, $this->_data ) ? $this->_data[ $key ] : false ) : $this->_data; | |
| 314 | - } | |
| 315 | - | |
| 316 | - protected function _sort_tabs( $a, $b ) { | |
| 317 | - if ( $a['priority'] === $b['priority'] ) { | |
| 318 | - return 0; | |
| 319 | - } | |
| 320 | - | |
| 321 | - return $a['priority'] < $b['priority'] ? - 1 : 1; | |
| 322 | - } | |
| 323 | -} | |
| 324 | - | |
| 325 | -/** | |
| 326 | - * Class LP_Profile_Tab | |
| 327 | - * | |
| 328 | - * @since 3.0.0 | |
| 329 | - */ | |
| 330 | -class LP_Profile_Tab { | |
| 331 | - protected $_data = array(); | |
| 332 | - /** | |
| 333 | - * @var LP_Profile | |
| 334 | - */ | |
| 335 | - protected $profile = null; | |
| 336 | - | |
| 337 | - /** | |
| 338 | - * @var string | |
| 339 | - */ | |
| 340 | - public $id = ''; | |
| 341 | - | |
| 342 | - /** | |
| 343 | - * LP_Profile_Tab constructor. | |
| 344 | - * | |
| 345 | - * @param string $id | |
| 346 | - * @param array $data | |
| 347 | - * @param LP_Profile $profile | |
| 348 | - */ | |
| 349 | - public function __construct( $id, $data, $profile ) { | |
| 350 | - //parent::__construct( $data ); | |
| 351 | - $this->_data = is_array( $data ) ? $data : (array) $data; | |
| 352 | - | |
| 353 | - $this->profile = $profile; | |
| 354 | - $this->id = $id; | |
| 355 | - } | |
| 356 | - | |
| 357 | - public function sections() { | |
| 358 | - $profile = $this->get_profile(); | |
| 359 | - $sections = array(); | |
| 360 | - $all_sections = $this->get( 'sections' ); | |
| 361 | - | |
| 362 | - if ( $all_sections ) { | |
| 363 | - foreach ( $all_sections as $section_key => $section ) { | |
| 364 | - | |
| 365 | - if ( $profile->is_hidden( $section ) ) { | |
| 366 | - continue; | |
| 367 | - } | |
| 368 | - $sections[ $section_key ] = $section; | |
| 369 | - } | |
| 370 | - } | |
| 371 | - | |
| 372 | - return $sections; | |
| 373 | - } | |
| 374 | - | |
| 375 | - public function get( $key = false ) { | |
| 376 | - return false !== $key ? ( array_key_exists( $key, $this->_data ) ? $this->_data[ $key ] : false ) : $this->_data; | |
| 377 | - } | |
| 378 | - | |
| 379 | - public function get_profile() { | |
| 380 | - return $this->profile; | |
| 381 | - } | |
| 382 | - | |
| 383 | - /** | |
| 384 | - * @deprecated 4.2.6.2 | |
| 385 | - */ | |
| 386 | - public function user_can_view() { | |
| 387 | - _deprecated_function( __METHOD__, '4.2.6.2' ); | |
| 388 | - | |
| 389 | - return false; | |
| 390 | - if ( $this->is_public() || current_user_can( ADMIN_ROLE ) ) { | |
| 391 | - return true; | |
| 392 | - } | |
| 393 | - | |
| 394 | - $can = $this->get_profile()->current_user_can( "view-tab-{$this->id}" ); | |
| 395 | - | |
| 396 | - return $can; | |
| 397 | - } | |
| 398 | - | |
| 399 | - /** | |
| 400 | - * @deprecated 4.2.6.2 | |
| 401 | - */ | |
| 402 | - public function user_can_view_section( $section ) { | |
| 403 | - _deprecated_function( __METHOD__, '4.2.6.2' ); | |
| 404 | - | |
| 405 | - return false; | |
| 406 | - | |
| 407 | - return $this->get_profile()->current_user_can( "view-section-{$section}" ); | |
| 408 | - } | |
| 409 | - | |
| 410 | - public function is_hidden() { | |
| 411 | - return $this->get( 'hidden' ); | |
| 412 | - } | |
| 413 | - | |
| 414 | - public function tab_is_visible_for_user() { | |
| 415 | - return $this->is_current(); | |
| 416 | - } | |
| 417 | - | |
| 418 | - public function is_current() { | |
| 419 | - return isset( $this['is_current'] ) ? $this['is_current'] : false; | |
| 420 | - } | |
| 421 | - | |
| 422 | - /** | |
| 423 | - * Tab is public for all users can view. | |
| 424 | - * | |
| 425 | - * @return bool | |
| 426 | - * @since 4.0.0 | |
| 427 | - * @deprecated 4.2.6.2 | |
| 428 | - */ | |
| 429 | - public function is_public() { | |
| 430 | - _deprecated_function( __METHOD__, '4.2.6.2' ); | |
| 431 | - | |
| 432 | - return false; | |
| 433 | - $public_tabs = $this->profile->get_public_tabs(); | |
| 434 | - | |
| 435 | - return $public_tabs && in_array( $this->id, $public_tabs ); | |
| 436 | - } | |
| 437 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +use LearnPress\Models\UserModel; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * Class LP_Profile_Tabs | |
| 7 | + */ | |
| 8 | +class LP_Profile_Tabs { | |
| 9 | + /** | |
| 10 | + * @var array | |
| 11 | + */ | |
| 12 | + protected $_data = array(); | |
| 13 | + /** | |
| 14 | + * @var LP_Profile_Tabs | |
| 15 | + */ | |
| 16 | + protected static $_instance = null; | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * @var LP_Profile | |
| 20 | + */ | |
| 21 | + protected $profile = null; | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * LP_Profile_Tabs constructor. | |
| 25 | + * | |
| 26 | + * @param array $tabs | |
| 27 | + * @param LP_Profile $profile | |
| 28 | + */ | |
| 29 | + public function __construct( $tabs, $profile ) { | |
| 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; | |
| 36 | + } | |
| 37 | + if ( ! array_key_exists( 'slug', $v ) ) { | |
| 38 | + $tabs_tmp[ $k ]['slug'] = $k; | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 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; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + ); | |
| 55 | + | |
| 56 | + foreach ( $tabs_tmp as $v ) { | |
| 57 | + $k = $v['key_index']; | |
| 58 | + $this->_data[ $k ] = new LP_Profile_Tab( $k, $v, $profile ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + $this->profile = $profile; | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * @return LP_Profile|null | |
| 66 | + */ | |
| 67 | + public function get_profile() { | |
| 68 | + return $this->profile; | |
| 69 | + } | |
| 70 | + | |
| 71 | + public function get_current_tab( $default = '', $key = true ) { | |
| 72 | + global $wp; | |
| 73 | + $current = $default; | |
| 74 | + | |
| 75 | + if ( ! empty( $_REQUEST['view'] ) ) { | |
| 76 | + $current = sanitize_text_field( $_REQUEST['view'] ); | |
| 77 | + } elseif ( ! empty( $wp->query_vars['view'] ) ) { | |
| 78 | + $current = $wp->query_vars['view']; | |
| 79 | + } else { | |
| 80 | + $tab = $this->get_tab_at(); | |
| 81 | + if ( $tab instanceof LP_Profile_Tab ) { | |
| 82 | + $current = $tab->get( 'slug' ); | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + if ( $key ) { | |
| 87 | + $current_display = $current; | |
| 88 | + $current = false; | |
| 89 | + foreach ( $this->get() as $_slug => $data ) { | |
| 90 | + if ( is_object( $data ) ) { | |
| 91 | + if ( $data->get( 'slug' ) === $current_display ) { | |
| 92 | + $current = $_slug; | |
| 93 | + break; | |
| 94 | + } | |
| 95 | + } elseif ( is_array( $data ) ) { | |
| 96 | + if ( $data['slug'] === $current_display ) { | |
| 97 | + $current = $_slug; | |
| 98 | + break; | |
| 99 | + } | |
| 100 | + } | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + return $current; | |
| 105 | + } | |
| 106 | + | |
| 107 | + public function get_current_section( $default = '', $key = true, $tab = '' ) { | |
| 108 | + global $wp; | |
| 109 | + | |
| 110 | + $current = $default; | |
| 111 | + | |
| 112 | + if ( ! empty( $_REQUEST['section'] ) ) { | |
| 113 | + $current = sanitize_text_field( $_REQUEST['section'] ); | |
| 114 | + } elseif ( ! empty( $wp->query_vars['section'] ) ) { | |
| 115 | + $current = $wp->query_vars['section']; | |
| 116 | + } else { | |
| 117 | + if ( false === $tab ) { | |
| 118 | + $current_tab = $this->get_current_tab(); | |
| 119 | + } else { | |
| 120 | + $current_tab = $tab; | |
| 121 | + } | |
| 122 | + | |
| 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' ); | |
| 127 | + $section = reset( $sections ); | |
| 128 | + if ( array_key_exists( 'slug', $section ) ) { | |
| 129 | + $current = $tab->get( 'slug' ); | |
| 130 | + } else { | |
| 131 | + $sections = array_keys( $tab->get( 'sections' ) ); | |
| 132 | + $current = reset( $sections ); | |
| 133 | + } | |
| 134 | + } | |
| 135 | + } | |
| 136 | + } | |
| 137 | + | |
| 138 | + if ( $key ) { | |
| 139 | + $current_display = $current; | |
| 140 | + $current = false; | |
| 141 | + | |
| 142 | + foreach ( $this->get() as $_slug => $data ) { | |
| 143 | + if ( empty( $data->get( 'sections' ) ) ) { | |
| 144 | + continue; | |
| 145 | + } | |
| 146 | + | |
| 147 | + foreach ( $data->get( 'sections' ) as $_slug => $data ) { | |
| 148 | + if ( array_key_exists( 'slug', $data ) && ( $data['slug'] === $current_display ) ) { | |
| 149 | + $current = $_slug; | |
| 150 | + break 2; | |
| 151 | + } | |
| 152 | + } | |
| 153 | + } | |
| 154 | + } | |
| 155 | + | |
| 156 | + return $current; | |
| 157 | + } | |
| 158 | + | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * @param bool $tab | |
| 162 | + * @param bool $with_section | |
| 163 | + * @param string $user_slug | |
| 164 | + * | |
| 165 | + * @return string | |
| 166 | + * @since 3.0.0 | |
| 167 | + * @version 4.0.1 | |
| 168 | + */ | |
| 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 ); | |
| 175 | + } | |
| 176 | + | |
| 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; | |
| 182 | + } else { | |
| 183 | + unset( $args['user'] ); | |
| 184 | + } | |
| 185 | + | |
| 186 | + if ( $with_section && ! empty( $tab_data->get( 'sections' ) ) ) { | |
| 187 | + if ( $with_section === true ) { | |
| 188 | + $section_keys = array_keys( $tab_data->get( 'sections' ) ); | |
| 189 | + $first_section = reset( $section_keys ); | |
| 190 | + $with_section = $this->get_slug( $tab_data->get( 'sections' )[ $first_section ], $first_section ); | |
| 191 | + } | |
| 192 | + $args['section'] = $with_section; | |
| 193 | + } | |
| 194 | + } | |
| 195 | + | |
| 196 | + $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) ); | |
| 197 | + if ( $profile_link ) { | |
| 198 | + if ( get_option( 'permalink_structure' ) ) { | |
| 199 | + $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) ); | |
| 200 | + } else { | |
| 201 | + $url = esc_url_raw( add_query_arg( $args, $profile_link ) ); | |
| 202 | + } | |
| 203 | + } | |
| 204 | + | |
| 205 | + return apply_filters( 'learnpress/profile/tab/link', $url, $tab, $with_section, $user_slug ); | |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * Get the slug of tab or section if defined. | |
| 210 | + * | |
| 211 | + * @param $tab_or_section | |
| 212 | + * @param string $default | |
| 213 | + * | |
| 214 | + * @return string | |
| 215 | + */ | |
| 216 | + public function get_slug( $tab_or_section, $default = '' ) { | |
| 217 | + if ( is_array( $tab_or_section ) ) { | |
| 218 | + return array_key_exists( 'slug', $tab_or_section ) ? $tab_or_section['slug'] : false; | |
| 219 | + } | |
| 220 | + | |
| 221 | + if ( is_string( $tab_or_section ) ) { | |
| 222 | + return $tab_or_section; | |
| 223 | + } | |
| 224 | + | |
| 225 | + return $tab_or_section ? $tab_or_section->get( 'slug' ) : false; | |
| 226 | + } | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * Get current link of profile | |
| 230 | + * | |
| 231 | + * @param string $args - Optional. Add more query args to url. | |
| 232 | + * @param bool $with_permalink - Optional. TRUE to build url as friendly url. | |
| 233 | + * | |
| 234 | + * @return mixed|string | |
| 235 | + * @Todo tungnx - need check this function | |
| 236 | + */ | |
| 237 | + public function get_current_url( $args = '', $with_permalink = false ) { | |
| 238 | + $current_tab = $this->get_current_tab(); | |
| 239 | + $tab = $this->get_tab_at( $current_tab ); | |
| 240 | + if ( ! $tab instanceof LP_Profile_Tab ) { | |
| 241 | + return ''; | |
| 242 | + } | |
| 243 | + | |
| 244 | + $lp_profile = $this->get_profile(); | |
| 245 | + /** | |
| 246 | + * @var $lp_user LP_User | |
| 247 | + */ | |
| 248 | + $lp_user = $lp_profile->get_user(); | |
| 249 | + $current_section_slug = $this->get_current_section(); | |
| 250 | + if ( ! $lp_user instanceof WP_User ) { | |
| 251 | + return ''; | |
| 252 | + } | |
| 253 | + | |
| 254 | + $userModel = UserModel::find( $lp_user->get_id(), true ); | |
| 255 | + if ( ! $userModel ) { | |
| 256 | + return ''; | |
| 257 | + } | |
| 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 ); | |
| 261 | + if ( is_array( $args ) && $args ) { | |
| 262 | + if ( ! $with_permalink ) { | |
| 263 | + $url = esc_url_raw( add_query_arg( $args, $url ) ); | |
| 264 | + } else { | |
| 265 | + $parts = array(); | |
| 266 | + | |
| 267 | + foreach ( $args as $k => $v ) { | |
| 268 | + $parts[] = "{$k}/{$v}"; | |
| 269 | + } | |
| 270 | + | |
| 271 | + $url = trailingslashit( $url ) . join( '/', $parts ) . '/'; | |
| 272 | + } | |
| 273 | + } | |
| 274 | + | |
| 275 | + return $url; | |
| 276 | + } | |
| 277 | + | |
| 278 | + /** | |
| 279 | + * Get tab data at a position. | |
| 280 | + * | |
| 281 | + * @param int $position Optional. Indexed number or slug. | |
| 282 | + * | |
| 283 | + * @return false|LP_Profile_Tab | |
| 284 | + */ | |
| 285 | + public function get_tab_at( $position = 0 ) { | |
| 286 | + if ( ! $position ) { | |
| 287 | + $position = 0; | |
| 288 | + } | |
| 289 | + | |
| 290 | + if ( $this->get() ) { | |
| 291 | + $tabs = $this->get(); | |
| 292 | + | |
| 293 | + if ( is_numeric( $position ) ) { | |
| 294 | + $tabs = array_values( $tabs ); | |
| 295 | + if ( ! empty( $tabs[ $position ] ) ) { | |
| 296 | + return $tabs[ $position ]; | |
| 297 | + } | |
| 298 | + } else { | |
| 299 | + if ( ! empty( $tabs[ $position ] ) ) { | |
| 300 | + return $tabs[ $position ]; | |
| 301 | + } | |
| 302 | + } | |
| 303 | + } | |
| 304 | + | |
| 305 | + return false; | |
| 306 | + } | |
| 307 | + | |
| 308 | + public function tabs() { | |
| 309 | + return $this->get(); | |
| 310 | + } | |
| 311 | + | |
| 312 | + public function get( $key = false ) { | |
| 313 | + return false !== $key ? ( array_key_exists( $key, $this->_data ) ? $this->_data[ $key ] : false ) : $this->_data; | |
| 314 | + } | |
| 315 | + | |
| 316 | + protected function _sort_tabs( $a, $b ) { | |
| 317 | + if ( $a['priority'] === $b['priority'] ) { | |
| 318 | + return 0; | |
| 319 | + } | |
| 320 | + | |
| 321 | + return $a['priority'] < $b['priority'] ? - 1 : 1; | |
| 322 | + } | |
| 323 | +} | |
| 324 | + | |
| 325 | +/** | |
| 326 | + * Class LP_Profile_Tab | |
| 327 | + * | |
| 328 | + * @since 3.0.0 | |
| 329 | + */ | |
| 330 | +class LP_Profile_Tab { | |
| 331 | + protected $_data = array(); | |
| 332 | + /** | |
| 333 | + * @var LP_Profile | |
| 334 | + */ | |
| 335 | + protected $profile = null; | |
| 336 | + | |
| 337 | + /** | |
| 338 | + * @var string | |
| 339 | + */ | |
| 340 | + public $id = ''; | |
| 341 | + | |
| 342 | + /** | |
| 343 | + * LP_Profile_Tab constructor. | |
| 344 | + * | |
| 345 | + * @param string $id | |
| 346 | + * @param array $data | |
| 347 | + * @param LP_Profile $profile | |
| 348 | + */ | |
| 349 | + public function __construct( $id, $data, $profile ) { | |
| 350 | + //parent::__construct( $data ); | |
| 351 | + $this->_data = is_array( $data ) ? $data : (array) $data; | |
| 352 | + | |
| 353 | + $this->profile = $profile; | |
| 354 | + $this->id = $id; | |
| 355 | + } | |
| 356 | + | |
| 357 | + public function sections() { | |
| 358 | + $profile = $this->get_profile(); | |
| 359 | + $sections = array(); | |
| 360 | + $all_sections = $this->get( 'sections' ); | |
| 361 | + | |
| 362 | + if ( $all_sections ) { | |
| 363 | + foreach ( $all_sections as $section_key => $section ) { | |
| 364 | + | |
| 365 | + if ( $profile->is_hidden( $section ) ) { | |
| 366 | + continue; | |
| 367 | + } | |
| 368 | + $sections[ $section_key ] = $section; | |
| 369 | + } | |
| 370 | + } | |
| 371 | + | |
| 372 | + return $sections; | |
| 373 | + } | |
| 374 | + | |
| 375 | + public function get( $key = false ) { | |
| 376 | + return false !== $key ? ( array_key_exists( $key, $this->_data ) ? $this->_data[ $key ] : false ) : $this->_data; | |
| 377 | + } | |
| 378 | + | |
| 379 | + public function get_profile() { | |
| 380 | + return $this->profile; | |
| 381 | + } | |
| 382 | + | |
| 383 | + /** | |
| 384 | + * @deprecated 4.2.6.2 | |
| 385 | + */ | |
| 386 | + public function user_can_view() { | |
| 387 | + _deprecated_function( __METHOD__, '4.2.6.2' ); | |
| 388 | + | |
| 389 | + return false; | |
| 390 | + if ( $this->is_public() || current_user_can( ADMIN_ROLE ) ) { | |
| 391 | + return true; | |
| 392 | + } | |
| 393 | + | |
| 394 | + $can = $this->get_profile()->current_user_can( "view-tab-{$this->id}" ); | |
| 395 | + | |
| 396 | + return $can; | |
| 397 | + } | |
| 398 | + | |
| 399 | + /** | |
| 400 | + * @deprecated 4.2.6.2 | |
| 401 | + */ | |
| 402 | + public function user_can_view_section( $section ) { | |
| 403 | + _deprecated_function( __METHOD__, '4.2.6.2' ); | |
| 404 | + | |
| 405 | + return false; | |
| 406 | + | |
| 407 | + return $this->get_profile()->current_user_can( "view-section-{$section}" ); | |
| 408 | + } | |
| 409 | + | |
| 410 | + public function is_hidden() { | |
| 411 | + return $this->get( 'hidden' ); | |
| 412 | + } | |
| 413 | + | |
| 414 | + public function tab_is_visible_for_user() { | |
| 415 | + return $this->is_current(); | |
| 416 | + } | |
| 417 | + | |
| 418 | + public function is_current() { | |
| 419 | + return isset( $this['is_current'] ) ? $this['is_current'] : false; | |
| 420 | + } | |
| 421 | + | |
| 422 | + /** | |
| 423 | + * Tab is public for all users can view. | |
| 424 | + * | |
| 425 | + * @return bool | |
| 426 | + * @since 4.0.0 | |
| 427 | + * @deprecated 4.2.6.2 | |
| 428 | + */ | |
| 429 | + public function is_public() { | |
| 430 | + _deprecated_function( __METHOD__, '4.2.6.2' ); | |
| 431 | + | |
| 432 | + return false; | |
| 433 | + $public_tabs = $this->profile->get_public_tabs(); | |
| 434 | + | |
| 435 | + return $public_tabs && in_array( $this->id, $public_tabs ); | |
| 436 | + } | |
| 437 | +} | |