-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Framework: Refactor siteUtils.getDefaultPostFormat to use a selector #13329
Conversation
I see this changes the behavior to prefer site settings value for default post format, which had previously been put in place as a workaround to the Jetpack value not always being entirely accurate. I'm not sure if this is still a problem with improved accuracy these days, but figured I'd raise it for context nonetheless:
11836-gh-calypso-pre-oss |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this one a Jetpack and wpcom site, and it appears to work well. @tyxla maybe keep an eye out if we still need to check for site settings. It did take a bit of time for the Jetpack option to sync, but it was pretty quick, like around 30 secs or so.
@aduth @gwwar that's a good catch! I thought using the In a9499a8 I've updated the selector to prioritize the default post format from settings. Also, site settings are queried in both occurrences where default post format is used (in Edit Post and in Writing Settings), so the settings value will actually be used. |
I'd appreciate another review of this one when you folks have a minute. |
* @return {?String} The default post format of that site | ||
*/ | ||
export default function getSiteDefaultPostFormat( state, siteId ) { | ||
const site = getRawSite( state, siteId ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to call getRawSite
at all? Wouldn't getSiteOption
and getSiteSettings
safely fall back to an empty value if the site data isn't available?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would make sense only if we decide to not return the standard
as the default post format if there's no other post format specified yet, but site is already known. I've expanded that in my comment below.
defaultPostFormat = getSiteOption( state, siteId, 'default_post_format' ); | ||
} | ||
|
||
if ( ! defaultPostFormat || defaultPostFormat === '0' ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To the above point, two things:
- Settings and site data are separately retrieved from the API, so it's not enough to check just that the raw site data is available
- I'm not sure if we want to assume a "standard" format in the case that we don't know what the site or site settings are; typically we'd return
null
from a selector in those cases to indicate a "can't know" state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Settings and site data are separately retrieved from the API, so it's not enough to check just that the raw site data is available
Yes, but since we're now migrating away from sites-list
, the settings
will not be available in the site
object. And that is why we're falling back to the setting in the site object. Also, since we're using the Redux site in these cases .settings
is not there anyway, so we're only improving the current solution. I'm open to any better alternatives.
I'm not sure if we want to assume a "standard" format in the case that we don't know what the site or site settings are; typically we'd return null from a selector in those cases to indicate a "can't know" state.
For me, the getSiteDefaultPostFormat
selector is a more specific one, because I'd expect it to return a default value that we can actually use. In the WordPress core, empty string is saved for the post format of a post when "standard" is selected, and if you've never touched post formats in your installation, the "Standard" is the default post format when inserting a new post. So, it would make sense for me that we'd reach the "can't know" state only if we don't have information about the site yet. But if we have information about the site, it would make sense to return the standard
post format as the default post format if there's no other default post format specified. In other words, standard is the "default" default post format in WordPress, not null
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but since we're now migrating away from
sites-list
, thesettings
will not be available in thesite
object.
Right, but there's a hypothetical situation where we have the site's settings received from GET /sites/%s/settings
but not the GET /sites/%s
data, in which case I'd expect the data from the prior to be returned. But with the implementation here, it'll never reach that point because the site hasn't yet been received. Or at least based on the intent of the selector, I'd expect this to be the flow of logic (even if in practice this would never occur).
But if we have information about the site, it would make sense to return the
standard
post
I agree with this. I think I was confused by my internal desire to remove the if ( ! site ) {
condition at the top of the function, but with it, your reasoning makes sense. Generally, I just want to be sure that we don't return standard
if the site data hasn't been received (since, for all we know, the default format is actually aside
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great catch @aduth, and a perfectly valid scenario. I've updated the logic of the selector to reflect that case too. Here is how it will work now:
- If site is not fetched AND settings are not fetched, will return
null
. - If site is fetched, but settings are not, use the value from the site.
- If site is not fetched but settings are, use the value from settings.
- If both site and settings are fetched, use the value from settings.
- If the format is
""
or"0"
, default it to'standard'
. - Otherwise, it'll return the actual fetched format.
I've also expanded the unit tests to cover all of these situations. Feel free to have another look @aduth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is how it will work now:
This makes perfect sense, thanks for illustrating 👍
@aduth did we need to add another check to see if all data is available? Otherwise I think this is good to 🚢 |
@@ -44,7 +44,7 @@ export function getSiteSettingsSaveRequestStatus( state, siteId ) { | |||
* @return {Object} Site settings | |||
*/ | |||
export function getSiteSettings( state, siteId ) { | |||
return get( state.siteSettings.items, [ siteId ], null ); | |||
return get( state, [ 'siteSettings', 'items', siteId ], null ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this changed due to tests? Based on reducer shape of state we should always be able to assume that items
exists. I think we'll want to update tests instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, will update them instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaultPostFormat = getSiteOption( state, siteId, 'default_post_format' ); | ||
} | ||
|
||
if ( ! defaultPostFormat || defaultPostFormat === '0' ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is how it will work now:
This makes perfect sense, thanks for illustrating 👍
@gwwar Can you clarify what you mean by this? |
This does work well in my testing 👍 |
I might have misread earlier, but it sounded like we might have needed to wait for 2 data sources to populate before knowing what the correct default is. |
Ah. There is two sources, yes, but we should be able to use one or the other, with a preference toward the site settings. Or at least, that's how the previous behavior had been implemented. |
19b6185
to
81734d1
Compare
0e5c017
to
7c99cf2
Compare
This PR removes
siteUtils.getDefaultPostFormat
and introduces a selector instead. Since this one is used only in one place, this PR also migrates that usage and removes the obsoletesiteUtils.getDefaultPostFormat
definition.To test:
http://calypso.localhost:3000/post/:site
for a site that has'standard'
default post format and verify it's set properly when creating a post for that site.http://calypso.localhost:3000/post/:site
for a site that has another default post format (for example'aside'
and verify it's set properly when creating a post for that site.http://calypso.localhost:3000/post/:site
for a site with default post format set to'0'
and verify it's set properly when creating a post for that site.http://calypso.localhost:3000/post/:site
for a site with no default post format specified and verify it's set properly when creating a post for that site.npm run test-client client/state/selectors/test/get-site-default-post-format.js
npm run test-client client/state/sites/test/selectors.js
Hint: Want to quickly update your post format? Go to
/wp-admin/options.php
and change thedefault_post_format
option manually.