#53940 closed enhancement (fixed)
Reconsider return types for `_get_cron_array()`.
Reported by: | peterwilsoncc | Owned by: | peterwilsoncc |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Cron API | Keywords: | good-first-bug has-patch has-unit-tests add-to-field-guide |
Focuses: | Cc: |
Description
`_get_cron_array()` currently has multiple return types:
false
: Thecron
option is not set/is set incorrectlyarray
: Thecron
option is set correctly and either does or does not contain any cron jobs.
As either type can be returned when there are no cron jobs, I think it's worth discussing whether the return types are correct, specifically:
- should the function always return an
array
- should the function always return
false
when there are zero cron jobs
Either change would improve the consistency of the return type but be a technical break of backward compatibility.
My inclination is to always return an array
as it's fair to expect delete_option( '_cron' )
zeros out the cron jobs in the same way that _set_cron_array( [] )
does.
I also think always returning an array when there is no option/no cron jobs is more consistent than returning a different type when there are zero registered jobs depending on how they were deleted.
Attachments (1)
Change History (16)
#2
follow-up:
↓ 4
@
3 years ago
Note: the BC break may be acceptable in this case as the function is marked as internal only.
#4
in reply to:
↑ 2
;
follow-up:
↓ 5
@
3 years ago
Replying to jrf:
Note: the BC break may be acceptable in this case as the function is marked as internal only.
That's what I was thinking but hoping for thoughts from other committers such as yourself.
There are a few plugins in the repository using it directly, but a check of those with over 100,000 installs shows that many assume it's always an array and those that don't would be unaffected by such a change.
FYI: we discussed this function and the return type in last weeks livestream about PHP 8.1.
Yeah, I saw that while cleaning the house on the weekend. It's what prompted me to start thinking whether it should always return an array.
#5
in reply to:
↑ 4
@
3 years ago
Replying to peterwilsoncc:
There are a few plugins in the repository using it directly, but a check of those with over 100,000 installs shows that many assume it's always an array and those that don't would be unaffected by such a change.
Verified and I concur, though it is painful to see that some of those huge plugins are doing it wrong.
Yeah, I saw that while cleaning the house on the weekend. It's what prompted me to start thinking whether it should always return an array.
That's wonderful to hear! Hoping that we'll be able to inspire many more improvements via those live streams in the future 💗
#8
@
2 years ago
- Milestone changed from Awaiting Review to 6.1
- Type changed from defect (bug) to enhancement
I think this can be done and _get_cron_array()
always return an array.
It's a BC change but as zero scheduled tasks can currently either return false
or an empty array I think it's an improvement.
#10
@
2 years ago
- Keywords has-patch added; needs-patch removed
Hi @peterwilsoncc, I've added a patch which will always return array
instead of false
on failure from _get_cron_array()
.
This ticket was mentioned in PR #3038 on WordPress/wordpress-develop by peterwilsoncc.
2 years ago
#11
- Keywords has-unit-tests added; needs-unit-tests removed
#12
@
2 years ago
- Owner set to peterwilsoncc
- Resolution set to fixed
- Status changed from new to closed
In 53791:
@peterwilsoncc I concur, though changing the return type is a BC-break and may break code relying on the
false
return type.FYI: we discussed this function and the return type in last weeks livestream about PHP 8.1.