<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}



if ( ! function_exists( 'wp_doing_ajax' ) ) {
	/**
	 * Compatibility WP < 4.7.0
	 * @return bool
	 */
	function wp_doing_ajax() {
		/**
		 * Filters whether the current request is a WordPress Ajax request.
		 *
		 * @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
		 *
		 * @since 4.7.0
		 *
		 */
		return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
	}
}


if ( ! function_exists( 'wg_is_rest' ) ) {
	/**
	 * Checks if the current request is a WP REST API request.
	 *
	 * Case #1: After WP_REST_Request initialisation
	 * Case #2: Support "plain" permalink settings
	 * Case #3: URL Path begins with wp-json/ (your REST prefix)
	 * Also supports WP installations in subfolders
	 *
	 * @return boolean
	 * @author matzeeable
	 */
	function wg_is_rest() {
		$prefix = rest_get_url_prefix();
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST // (#1)
		     || isset( $_GET['rest_route'] ) //phpcs:ignore
		        && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix, 0 ) === 0 ) { //phpcs:ignore
			return true;
		}
		// (#3)
		$rest_url    = wp_parse_url( site_url( $prefix ) );
		$current_url = wp_parse_url( add_query_arg( array() ) );

		if( !isset( $current_url['path'] ) || !isset( $rest_url['path'] ) ) {
			return false;
		} else {
			return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
		}

	}
}
