PurpleTurtleCreative / completionist

Asana integration plugin for WordPress.
https://purpleturtlecreative.com/completionist/
GNU General Public License v3.0
1 stars 0 forks source link

Cache Attachments server-side + replace cURL usage with wp_remote_get() or similar #139

Closed MichelleBlanchette closed 1 year ago

MichelleBlanchette commented 1 year ago

I get phpcs warnings about using cURL instead of WordPress's HTTP functions. I think it's fair and probably easier to work with, anyways. There might be things I'm doing that I can't do with WordPress's HTTP functions, but I think it could help me figure out how to easily cache the attachments.

Also, you should probably make the proxy field option its own endpoint..? The proxy field could be a path param, like /v1/attachments/{proxy_field} with the request token still as a GET param. Either way, the URL structure should be compatible with Cloudflare/CDN caching and browser caching.

MichelleBlanchette commented 1 year ago

Cloudflare caches by file extension and not MIME type by default: https://developers.cloudflare.com/cache/about/default-cache-behavior/#:~:text=%E2%80%8B%E2%80%8B%20Default%20cached%20file,Cloudflare%20caches%20a%20website's%20robots.

So that basically means that I'd need to change the localized Attachment URL depending on its MIME type to give it the proper extension, which would require API calls to Asana for each attachment. That is not acceptable because there can be MANY attachments. Maybe you could do some sort of permanent redirect but that sounds like a can of worms. Just cache the Attachment data locally, as planned. Custom cache rules would need to be configured for Cloudflare to cache those paths. Sigh... I'm basically implementing what Cloudflare would, anyways, though, so I guess it isn't all that bad.

MichelleBlanchette commented 1 year ago

Actually, we should probably return a WP_Response object where possible, too. It might automatically replace the content type with JSON and JSON-encode the body, but I'm now aware you can at least set HTTP headers for the response:

                // Finished processing. Report is ready for download.
                $response = new \WP_REST_Response(
                    'The CSV report is ready for download.',
                    302,
                    array( 'Location' => admin_url( 'admin.php?page=ptf-paypal-thrivecart-audit' ) )
                );

See https://github.com/PurpleTurtleCreative/ptf-paypal/blob/main/src/public/rest-api/class-thrivecart.php#L323C2-L328

MichelleBlanchette commented 1 year ago

As previously suspected, the WP_REST_Server always JSON encodes the HTTP response body data and sets the Content-Type header to application/json. So the raw print of the image data and setting of HTTP headers is still required. However, we can still use wp_remote_get() instead of PHP cURL functions.