Forum Replies Created

Viewing 15 replies - 1 through 15 (of 18 total)
  • Thread Starter botoxparty

    (@adamhammad)

    @mullibahr Nice one! Thanks!

    Thread Starter botoxparty

    (@adamhammad)

    I would even recommend adding this to the plugin.

    e.g.

    function get_current_user_id() {
      if (class_exists('Jwt_Auth_Public')) {
          $jwt = new \Jwt_Auth_Public('jwt-auth', '1.1.0');
          $token = $jwt->validate_token(false);
          if (\is_wp_error($token)) {
              return false;
          }
    
          return $token->data->user->id;
      } else {
          return false;
      }
    }
    Thread Starter botoxparty

    (@adamhammad)

    Sorry thought I replied to this.

    @acato

    Number 2 sounds like a more flexible option, I can see this being used for more than just authentication purposes. e.g. Do not cache based on an environment variable.

    Thread Starter botoxparty

    (@adamhammad)

    Thanks for the update! It works great, definitely fixed the issue I was having before with detecting object type and throwing an error.

    I think I want to cache the endpoint, this is my use case:

    I have a product catalogue that is cached. Admin users can see ‘draft’ and ‘private’ status products, whereas Customers can only see ‘publish’ products. So the cached responses won’t actually return what an admin should see, and if an admin sets the cached response then it will show ‘draft’ and ‘private’ products to customers.

    I guess I could create an endpoint specifically for admin users to access the catalogue.

    Let me know your thoughts.

    Thread Starter botoxparty

    (@adamhammad)

    No problem, the information you’ve provided is plenty for my solution.

    I have some endpoints that check authentication inside them, I would like a way to make sure any authenticated requests by admins are not cached. Any ideas on how to approach this?

    Thread Starter botoxparty

    (@adamhammad)

    Also what is the ‘Item API Caches’?

    Thread Starter botoxparty

    (@adamhammad)

    Okay cool I can use those functions that’s great. Converting the endpoint path to md5 isn’t a problem seems to be matching here from a quick test.

    Those use cases you describe is basically what i’m looking to achieve.

    – Flushing all caches for an endpoint regardless of query params
    – Flushing cache by specific path/endpoint

    Would be a good place to start.

    Thread Starter botoxparty

    (@adamhammad)

    Ah okay! I see!

    So yep I got this working with one of my custom endpoints which returns a single page.

    Looking at this function:

        private function determine_object_type( $data ) {
            if ( array_key_exists( 'id', $data['data'] ) ) {
                $this->is_single = true;
                if ( array_key_exists( 'type', $data['data'] ) ) {
                    return $data['data']['type'];
                } else if ( array_key_exists( 'taxonomy', $data['data'] ) ) {
                    return $data['data']['taxonomy'];
                }
            } else {
                $this->is_single = false;
                if ( count( $data['data'] ) && isset( $data['data'][0] ) ) {
                    if ( array_key_exists( 'type', $data['data'][0] ) ) {
                        return $data['data'][0]['type'];
                    } else if ( array_key_exists( 'taxonomy', $data['data'][0] ) ) {
                        return $data['data'][0]['taxonomy'];
                    }
                }
            }
    
            return 'unknown';
        }
    

    I made sure I put the page ID in ‘id’, and the ‘type’ in type. This worked successfully when updating the page and clearing the cache.

    If anyone is looking to do you can contact me at [email protected]

    Thread Starter botoxparty

    (@adamhammad)

    Okay so i went to the line of the error and realised it was related to checking the Object Type.

    Considering this doesn’t really affect the way it works i’ve just added this piece of code in for now above that line.

    if(gettype($data['data']) === 'object') {
       return "Object";
    }
    • This reply was modified 5 years, 8 months ago by botoxparty.
    Thread Starter botoxparty

    (@adamhammad)

    Hmm that is how I have it setup. However on some endpoints i get the following on the first request:

    <br />
    <b>Fatal error</b>:  Uncaught Error: Cannot use object of type stdClass as array in /Users/xxxxxxxx/Projects/xxxxxxxx/wp-content/plugins/wp-rest-cache/includes/caching/class-caching.php:714
    Stack trace:
    #0 /Users/xxxxxxxx/Projects/xxxxxxxx/wp-content/plugins/wp-rest-cache/includes/caching/class-caching.php(591): WP_Rest_Cache_Plugin\Includes\Caching\Caching->determine_object_type(Array)
    #1 /Users/xxxxxxxx/Projects/xxxxxxxx/wp-content/plugins/wp-rest-cache/includes/caching/class-caching.php(130): WP_Rest_Cache_Plugin\Includes\Caching\Caching->register_endpoint_cache('c91289d1c66027e...', Array, '/wp-json/abode/...')
    #2 /Users/xxxxxxxx/Projects/xxxxxxxx/wp-content/plugins/wp-rest-cache/includes/api/class-endpoint-api.php(153): WP_Rest_Cache_Plugin\Includes\Caching\Caching->set_cache('c91289d1c66027e...', Array, 'endpoint', '/wp-json/abode/...')
    #3 /Users/xxxxxxxx/Projects/xxxxxxxx/wp-includes/class-wp-hook.php(286): WP_Rest_Cache_Plugin\Includes\API\Endpoint_Api->save_cache(Object(stdClass), Objec in <b>/Users/xxxxxxx/Projects/xxxxxxx/wp-content/plugins/wp-rest-cache/includes/caching/class-caching.php</b> on line <b>714</b><br />

    Then i get a cached response after that. However the cache doesn’t appear in Settings > WP REST Cache.

    Other endpoints that don’t produce this error do get cached and appear in the settings.

    Any ideas on how to debug this?

    Thread Starter botoxparty

    (@adamhammad)

    Hey @acato

    Is there any way to do this programatically?

    Also looking to do this…

    Thread Starter botoxparty

    (@adamhammad)

    Okay I worked it out, thanks Martin.

    Incase anyone else is looking for this solution, I was previously using wp_get_current_user to check if I was logged in, I replaced this with the function above.

    Cheers

    Thread Starter botoxparty

    (@adamhammad)

    What do you mean by your controller's permissions check ?

    Is this part of WP Core, or part of this plugin?

    Sorry don’t have a deep knowledge of how WP authenticates requests, which file did you edit?

Viewing 15 replies - 1 through 15 (of 18 total)