Skip to content

Instantly share code, notes, and snippets.

@cyberwani
Forked from matzeeable/is_rest.php
Created March 30, 2023 17:38
Show Gist options
  • Save cyberwani/b1ad118f6bbf763af710523af4bbdf7b to your computer and use it in GitHub Desktop.
Save cyberwani/b1ad118f6bbf763af710523af4bbdf7b to your computer and use it in GitHub Desktop.

Revisions

  1. @matzeeable matzeeable revised this gist May 31, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion is_rest.php
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,6 @@ function is_rest() {
    // (#4)
    $rest_url = wp_parse_url( trailingslashit( rest_url( ) ) );
    $current_url = wp_parse_url( add_query_arg( array( ) ) );
    return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
    return strpos( $current_url['path'] ?? '/', $rest_url['path'], 0 ) === 0;
    }
    }
  2. @matzeeable matzeeable revised this gist Aug 24, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion is_rest.php
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@
    function is_rest() {
    if (defined('REST_REQUEST') && REST_REQUEST // (#1)
    || isset($_GET['rest_route']) // (#2)
    && strpos( $_GET['rest_route'] ), '/', 0 ) === 0)
    && strpos( $_GET['rest_route'] , '/', 0 ) === 0)
    return true;

    // (#3)
  3. @matzeeable matzeeable revised this gist Aug 24, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions is_rest.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    * 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 #2: Support "plain" permalink settings and check if `rest_route` starts with `/`
    * 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)
    @@ -17,7 +17,7 @@
    function is_rest() {
    if (defined('REST_REQUEST') && REST_REQUEST // (#1)
    || isset($_GET['rest_route']) // (#2)
    && strpos( trim( $_GET['rest_route'], '\\/' ), rest_get_url_prefix( ) , 0 ) === 0)
    && strpos( $_GET['rest_route'] ), '/', 0 ) === 0)
    return true;
    // (#3)
  4. @matzeeable matzeeable revised this gist Sep 9, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions is_rest.php
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,9 @@
    * @author matzeeable
    */
    function is_rest() {
    $prefix = rest_get_url_prefix( );
    if (defined('REST_REQUEST') && REST_REQUEST // (#1)
    || isset($_GET['rest_route']) // (#2)
    && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix , 0 ) === 0)
    && strpos( trim( $_GET['rest_route'], '\\/' ), rest_get_url_prefix( ) , 0 ) === 0)
    return true;

    // (#3)
  5. @matzeeable matzeeable revised this gist Aug 27, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion is_rest.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    * 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)
    * so do this (wp-settings.php)
    * Case #4: URL Path begins with wp-json/ (your REST prefix)
    * Also supports WP installations in subfolders
    *
  6. @matzeeable matzeeable revised this gist Aug 27, 2019. 1 changed file with 31 additions and 24 deletions.
    55 changes: 31 additions & 24 deletions is_rest.php
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,33 @@
    <?php
    if (!function_exists('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
    *
    * @returns boolean
    * @author matzeeable
    */
    function is_rest() {
    $prefix = rest_get_url_prefix( );
    if (defined('REST_REQUEST') && REST_REQUEST // (#1)
    || isset($_GET['rest_route']) // (#2)
    && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix , 0 ) === 0)
    return true;

    // (#3)
    $rest_url = wp_parse_url( site_url( $prefix ) );
    $current_url = wp_parse_url( add_query_arg( array( ) ) );
    return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
    }

    if ( !function_exists( '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: 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
    *
    * @returns boolean
    * @author matzeeable
    */
    function is_rest() {
    $prefix = rest_get_url_prefix( );
    if (defined('REST_REQUEST') && REST_REQUEST // (#1)
    || isset($_GET['rest_route']) // (#2)
    && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix , 0 ) === 0)
    return true;

    // (#3)
    global $wp_rewrite;
    if ($wp_rewrite === null) $wp_rewrite = new WP_Rewrite();

    // (#4)
    $rest_url = wp_parse_url( trailingslashit( rest_url( ) ) );
    $current_url = wp_parse_url( add_query_arg( array( ) ) );
    return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
    }
    }
  7. Matthias Günter revised this gist Oct 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion is_rest.php
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ function is_rest() {

    // (#3)
    $rest_url = wp_parse_url( site_url( $prefix ) );
    $current_url = wp_parse_url( home_url( add_query_arg( array( ) ) ) );
    $current_url = wp_parse_url( add_query_arg( array( ) ) );
    return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
    }
    }
  8. Matthias Günter revised this gist Oct 18, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions is_rest.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    if (!function_exists('is_rest')) {
    /**
    * Checks if the current request is a WP REST API request.
  9. Matthias Günter revised this gist Oct 18, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions is_rest.php
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    * 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
    *
    * @returns boolean
    * @author matzeeable
  10. Matthias Günter created this gist Oct 18, 2018.
    24 changes: 24 additions & 0 deletions is_rest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    if (!function_exists('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)
    *
    * @returns boolean
    * @author matzeeable
    */
    function is_rest() {
    $prefix = rest_get_url_prefix( );
    if (defined('REST_REQUEST') && REST_REQUEST // (#1)
    || isset($_GET['rest_route']) // (#2)
    && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix , 0 ) === 0)
    return true;

    // (#3)
    $rest_url = wp_parse_url( site_url( $prefix ) );
    $current_url = wp_parse_url( home_url( add_query_arg( array( ) ) ) );
    return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
    }
    }