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