HEX
Server: nginx/1.18.0
System: Linux m1-ws1-ams3 5.4.0-148-generic #165-Ubuntu SMP Tue Apr 18 08:53:12 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /opt/aphex/sites/professorprotest.nl/wp-content/plugins/slim-seo/src/AutoRedirection.php
<?php
namespace SlimSEO;

class AutoRedirection {
	public function setup() {
		add_action( 'template_redirect', [ $this, 'redirect' ] );
	}

	public function redirect() {
		$destination = $this->get_destination();
		if ( $destination ) {
			wp_safe_redirect( esc_url( $destination ), 301, 'Slim SEO' );
			die;
		}
	}

	private function get_destination() {
		if ( is_attachment() ) {
			return $this->get_attachment_destination();
		}

		if ( is_author() ) {
			return $this->get_author_destination();
		}

		return null;
	}

	/**
	 * Get redirect destination for attachment page, which is file URL.
	 *
	 * @return string
	 */
	private function get_attachment_destination() {
		return wp_get_attachment_url( get_queried_object_id() );
	}

	/**
	 * Get redirect destination for author page.
	 * If author has no posts, or the website has only one user, then redirect author page to homepage.
	 *
	 * @return string
	 */
	private function get_author_destination() {
		// If no posts.
		if ( ! have_posts() ) {
			return home_url( '/' );
		}

		// If the website has only one user.
		$users = get_users( [
			'number' => 2,
			'fields' => 'ID',
		] );
		if ( 1 === count( $users ) ) {
			return home_url( '/' );
		}

		return null;
	}
}