Automator_Helpers_Recipe::is_rest()

Checks if the current request is a WP REST API request.


Description Description

Case #1: After WP_REST_Request initialisation Case #2: Support "plain" permalink settings Case #3: It can happen that WP_Rewrite is not yet initialized, so do this (wp-settings.php) Case #4: URL Path begins with wp-json/ (your REST prefix) Also supports WP installations in subfolders


Source Source

File: src/core/lib/helpers/class-automator-recipe-helpers.php

	public function is_rest() {
		$prefix = rest_get_url_prefix();
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST || automator_filter_has_var( 'rest_route' ) && 0 === strpos( trim( automator_filter_input( 'rest_route' ), '\\/' ), $prefix, 0 ) ) {
			return true;
		}
		// (#3)
		global $wp_rewrite;
		if ( null === $wp_rewrite ) {
			$wp_rewrite = new \WP_Rewrite(); //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
		}

		// (#4)
		$current_url = wp_parse_url( add_query_arg( array() ) );
		$regex       = '/\/' . $prefix . '\/(' . str_replace( '/', '\/', AUTOMATOR_REST_API_END_POINT ) . '.+)/';
		$match       = isset( $current_url['path'] ) ? $current_url['path'] : '';

		return preg_match( $regex, $match );
	}