is_enabled() ) {
return;
}
$is_author_archive = is_author();
// A request for an author id/slug that doesn't exist never
// reaches is_author() -- WordPress's own query resolves it to a
// plain 404 instead. Recognized here by the query vars it leaves
// behind, the same way the reference plugin does.
$is_invalid_author_request = is_404() && ( get_query_var( 'author' ) || get_query_var( 'author_name' ) );
if ( ! $is_author_archive && ! $is_invalid_author_request ) {
return;
}
if ( $is_invalid_author_request && ! $this->redirect_invalid() ) {
return;
}
if ( $is_author_archive && $this->admin_only() ) {
$queried_user = get_queried_object();
// Checking the 'manage_options' capability rather than the
// 'administrator' role name by itself (what the reference
// plugin does): robust to a site renaming/removing that role
// while still granting the same access, and consistent with
// how every other admin-only check in this plugin works.
if ( ! ( $queried_user instanceof WP_User ) || ! user_can( $queried_user, 'manage_options' ) ) {
return;
}
}
if ( $this->not_found() ) {
global $wp_query;
$wp_query->set_404();
status_header( 404 );
nocache_headers();
remove_action( 'template_redirect', 'redirect_canonical' );
return;
}
wp_safe_redirect( $this->destination_url(), $this->status_code() );
exit;
}
/**
* Blank out the author link URL, if enabled.
*
* Best-effort: WordPress core's own `get_the_author_posts_link()`
* always wraps this in `` regardless of whether the URL
* is empty, so this stops the link from pointing anywhere useful but
* doesn't remove the `` element itself -- that depends on the
* theme.
*
* @param string $url Author page URL.
* @return string
*/
public function maybe_hide_author_link( string $url ): string {
return $this->hide_author_link() ? '' : $url;
}
/**
* Whether the feature is on at all.
*
* @return bool
*/
private function is_enabled(): bool {
return (bool) aiou_get_setting( 'security', 'author_pages', 'enabled', 0 );
}
/**
* Whether a request for a nonexistent author redirects too.
*
* @return bool
*/
private function redirect_invalid(): bool {
return (bool) aiou_get_setting( 'security', 'author_pages', 'redirect_invalid', 0 );
}
/**
* Whether disabled pages give a 404 rather than a redirect.
*
* @return bool
*/
private function not_found(): bool {
return '404' === (string) aiou_get_setting( 'security', 'author_pages', 'response', 'redirect' );
}
/**
* Whether only administrators' author pages are disabled.
*
* @return bool
*/
private function admin_only(): bool {
return (bool) aiou_get_setting( 'security', 'author_pages', 'admin_only', 0 );
}
/**
* Whether author link URLs should be blanked out.
*
* @return bool
*/
private function hide_author_link(): bool {
return (bool) aiou_get_setting( 'security', 'author_pages', 'hide_author_link', 0 );
}
/**
* The configured HTTP redirect status.
*
* @return int
*/
private function status_code(): int {
return '307' === (string) aiou_get_setting( 'security', 'author_pages', 'status_code', '301' ) ? 307 : 301;
}
/**
* The configured redirect destination.
*
* @return string
*/
private function destination_url(): string {
$page_id = (int) aiou_get_setting( 'security', 'author_pages', 'destination_page', '0' );
if ( $page_id > 0 ) {
$url = get_permalink( $page_id );
if ( $url ) {
return $url;
}
}
return home_url( '/' );
}
}