Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Any links to restricted pages added to emails should contain a redirection parameter.
  5. * When user lands on a restricted screen with a redirection parameter,
  6. * they are redirected to the login screen (with the usual notice).
  7. * On loggin in, the user is redirected back to the restricted page.
  8. */
  9.  
  10. add_action( 'lifterlms_content_restricted', 'llms_maybe_email_unit_redirect', 10, 1);
  11.  
  12. function llms_maybe_email_unit_redirect( $restriction ){
  13.  
  14. // do nothing in case the ?redir_login parameter isn't set
  15. if( empty( llms_filter_input( INPUT_GET, 'login' ) ) ){
  16. return;
  17. }
  18.  
  19. // otherwise, filter the restriction
  20. add_filter( 'llms_restricted_by_' . $restriction['reason'] . '_redirect', 'llms_email_unit_login_redirect', 10, 2 );
  21. add_filter( 'llms_restricted_by_' . $restriction['reason'] . '_message', 'llms_email_unit_login_message', 10, 2 );
  22. }
  23.  
  24. function llms_email_unit_login_message( $message, $restriction ){
  25. return 'Please login to view <em>'. get_the_title( $restriction['content_id'] ) . '</em>.';
  26. }
  27.  
  28. function llms_email_unit_login_redirect( $url, $restriction ){
  29.  
  30. $redirect_back_to = get_permalink( $restriction['content_id'] );
  31.  
  32. // intercept redirection by sending the dashboard url instead with a redirect paramater
  33. $url = add_query_arg( array( 'redirect' => esc_url($redirect_back_to) ), llms_person_my_courses_url() );
  34.  
  35. return $url;
  36. }
  37.  
  38. // hook into login form's redirection
  39. add_filter( 'llms_student_dashboard_login_redirect', 'llms_maybe_redirect_to_unit' );
  40.  
  41. function llms_maybe_redirect_to_unit( $url ){
  42.  
  43. // since the login form's action is going to be current url, we can expect it in the querystring if redirection was set.
  44. $unit_redirect = llms_filter_input( INPUT_GET, 'redirect' );
  45.  
  46. // do nothing in case the ?redir_login
  47. return empty( $unit_redirect ) ? $url : $unit_redirect;
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement