Redirect_404_Vehicles::maybe_redirect( WP $wp_obj )

Checks to see if the request is for a vehicle that no longer exists.

Description Description

If it was, it decides where to redirect the user and performs that redirect.


Top ↑

Parameters Parameters

$wp_obj

(WP) (Required) Current WordPress environment instance (passed by reference).


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-redirect-404-vehicles.php

			if ( ! isset( $wp_obj->request ) ) {
				return false;
			}

			$pieces = explode( '/', $wp_obj->request );
			return isset( $pieces[0] ) && trim( str_replace( site_url(), '', get_post_type_archive_link( INVP::POST_TYPE ) ), '/' ) === $pieces[0];
		}

		/**
		 * Checks to see if the request is for a vehicle that no longer exists.
		 * If it was, it decides where to redirect the user and performs that
		 * redirect.
		 *
		 * @param  WP $wp_obj Current WordPress environment instance (passed by reference).
		 * @return void
		 */
		public function maybe_redirect( $wp_obj ) {

			if ( is_admin() ) {
				return;
			}

			// is this a 404?
			if ( ! is_404() ) {
				return;
			}

			// is this a request for a vehicle or vehicle archive?
			if ( ! $this->is_request_for_vehicle_archive( $wp_obj ) && ! $this->is_request_for_vehicle( $wp_obj ) ) {
				return;
			}

			// base link to the inventory page.
			$url = get_post_type_archive_link( INVP::POST_TYPE );

			// get the make out of the slug.
			$make = $this->extract_make( $wp_obj );
			if ( ! is_string( $make ) || '' == $make ) {
				// no make, redirect to vehicle archive.
				wp_safe_redirect( $url, 302 );
				exit;
			}