Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable URL Metric storage locking by default for administrators #1835

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Jan 29, 2025

Fixes #1829

This changes the default value passed into the od_url_metric_storage_lock_ttl from 60 to 0 when the current user is an administrator. Additionally, a new od_store_url_metric_now meta capability is introduced which maps to manage_options by default. This gives sites the ability to customize which users have access to that capability.

In practice this will eliminate the need for the following code in the Optimization Detective Dev Mode plugin as long as the developer is testing while being logged-in as an administrator:

// During development, this can be useful to set to zero so that you don't have to wait for new URL Metrics to be requested when engineering a new optimization.
add_filter(
	'od_url_metric_freshness_ttl',
	static function () {
		return 0;
	}
);

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Jan 29, 2025
Copy link

codecov bot commented Jan 29, 2025

Codecov Report

Attention: Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.

Project coverage is 65.92%. Comparing base (d94977f) to head (9f89cd4).

Files with missing lines Patch % Lines
...zation-detective/storage/class-od-storage-lock.php 81.81% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1835      +/-   ##
==========================================
+ Coverage   65.89%   65.92%   +0.03%     
==========================================
  Files          88       88              
  Lines        6878     6890      +12     
==========================================
+ Hits         4532     4542      +10     
- Misses       2346     2348       +2     
Flag Coverage Δ
multisite 65.92% <84.61%> (+0.03%) ⬆️
single 38.28% <53.84%> (-0.30%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@westonruter westonruter marked this pull request as ready for review January 29, 2025 22:45
Copy link

github-actions bot commented Jan 29, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: felixarntz <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@westonruter westonruter changed the title Disable URL Metric storage locking for administrators Disable URL Metric storage locking by default for administrators Jan 29, 2025
@felixarntz
Copy link
Member

@westonruter I'm not sure about this change. This seems to be something that helps development, so I think having it in the OD Dev Mode plugin makes more sense to me than making it the default for any administrator. Many administrators of production sites are probably not developers.

On another note, I think the code snippet referenced in the PR description is for something else than described (it mentions viewport, rather than storage lock)?

@westonruter
Copy link
Member Author

On another note, I think the code snippet referenced in the PR description is for something else than described (it mentions viewport, rather than storage lock)?

Oops. I copied the wrong code. This has been corrected.

I'm not sure about this change. This seems to be something that helps development, so I think having it in the OD Dev Mode plugin makes more sense to me than making it the default for any administrator. Many administrators of production sites are probably not developers.

Actually, it's not specifically to help development. If someone creates a new post and then views it on the frontend, this should result in a URL Metric being collected right away without having to wait for any TTL storage lock to expire for the author or for another visitor to come by to view the post. A post author will almost always want to view the published post on the frontend after publishing, so this will result in a URL Metric being stored right away to give the collection a head start. Additionally, an administrator browsing around their site should be able to submit URL Metrics as they go around without having to wait for the storage lock TTL to expire. The purpose of the storage lock is to guard against unlimited DB writes from a frontend visitor. This, however, is not relevant to administrators (or even other roles above subscriber) since they can write to the DB at will in the admin.

Actually, come to think of it, manage_options probably isn't the right capability here, now that I think about it. In reality it should be edit_posts. It's about whether or not a user has "write access" to the database.

@felixarntz
Copy link
Member

Your rationale makes sense to me. In that case, maybe a better implementation to achieve that goal would be to invalidate the current user's storage lock transient whenever a post is updated? That way, any fresh content would be able to receive updates immediately, without e.g. "blasting" the server with tons of URL metrics (in case of a very unpopular site, where the administrator visits e.g. 15 URLs quickly to test stuff).

@westonruter
Copy link
Member Author

I'm not sure that would work. The storage lock TTL is sent to the client to prevent the client from even attempting to make a POST request to store a new URL Metric.

I don't see a problem with allowing an administrator to make multiple DB writes quickly. Consider bulk actions in the admin or quickly moderating through comments, for example.

@westonruter
Copy link
Member Author

Oh, I just realized this won't work as-is since the _wpnonce isn't being included for logged-in users, so the endpoint at present would always see the user as anonymous. Easy to fix.

@westonruter
Copy link
Member Author

Oh, I just realized this won't work as-is since the _wpnonce isn't being included for logged-in users, so the endpoint at present would always see the user as anonymous. Easy to fix.

There, this is fixed by 9f89cd4. Note that this could eventually mitigate the need for the Site Health test to check whether anonymous REST API requests are allowed (#1731). We could potentially add a new hook which disables URL Metric collection for anonymous users. This could be attractive to high-traffic sites (cf. #1655) which don't want to expose the REST API to anonymous visitors. Such sites would require logged-in users to collect the URL Metrics by browsing around, but also some automated collection could be implemented in the background, such as by opening the frontend in an IFRAME in the background when saving the post (#1311).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Storage lock TTL can be reduced or eliminated for admin users
2 participants