Skip to content

Update class-sensei-messages.php #7811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions includes/class-sensei-messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function __construct() {

// Redirect and show a success notice.
add_action( 'sensei_new_private_message', [ $this, 'show_success_notice' ], 999 );
add_action( 'template_redirect', array( $this, 'prevent_message_redirect' ), 1 );
}

public function only_show_messages_to_owner( $query ) {
Expand Down Expand Up @@ -1057,6 +1058,26 @@ public function show_success_notice(): void {
exit;
}
}
/**
* Prevent WordPress from redirecting to pretty permalinks for messages
*/
public function prevent_message_redirect() {
global $wp;

// Check if this is a message post
if ( isset( $wp->query_vars['p'] ) ) {
$post_id = $wp->query_vars['p'];
if ( get_post_type( $post_id ) === $this->post_type ) {
// If user doesn't have access, redirect to home
if ( ! $this->view_message( $post_id ) ) {
wp_safe_redirect( home_url(), 303 );
exit;
}
// If they do have access, prevent the pretty permalink redirect
remove_action( 'template_redirect', 'redirect_canonical' );
}
}
}

}

Expand Down
Loading