-
Notifications
You must be signed in to change notification settings - Fork 16
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
Fix issue on page preview #16
Fix issue on page preview #16
Conversation
…ewing a newly created page (before first save having post_name = '')
First of all, thanks for this @slackday There're two things that I would like to change before merging. If you don't have time I'll change them, otherwise if you push a new commit, I'll merge your PR. First, a very minor thing. For the creation of "empty" post, there's no need to pass 'title' as empty string, because that is already set on WP_Post (https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-post.php#L71). After that, you are setting the variable So I think that we could fix the bug, by only adding an $pagename = $query->get('pagename');
if (!$pagename && $query->is_preview() && $query->get('page_id')) {
$pagename = sanitize_title($post->post_title);
} Opinions? And thanks again for your work. |
Also: have you tested how WordPress behave in this case? Reading WordPress code, it seems that WordPress does not have special cases for preview. |
…nchPage Make sure new case with preview logic only runs on page post type
Doh, of course the WP_Post object takes care of this. Thanks!
Also good feedback. I assumed
When investigating this issue I started with a clean WP install. Then I added a Controller and I could narrow it down. On a clean WP install templates behave as expected. But now when I'm looking at the WordPress code for The Controller mentioned above relies on body classes to load data for each route in WP. Looking at the Core So what we have is an edge case where the Controller is assuming (through Hierarchy) that the classes are the same as returned by I think this solution will work with the changes you suggested above. What I don't know is why this separation exists in WP Core. What are your thoughts? I'm not so "well traveled" in WP Core code so I might sound like I know more than I do 😄 |
Codecov Report
@@ Coverage Diff @@
## master #16 +/- ##
=========================================
- Coverage 90.31% 90.12% -0.2%
=========================================
Files 29 29
Lines 413 415 +2
=========================================
+ Hits 373 374 +1
- Misses 40 41 +1
Continue to review full report at Codecov.
|
Thanks @slackday your code looks fine to me now. I want to look how core do it, because I want hierarchy to be in sync with core template hierarchy. Regarding body class, for some reason the page name is never part of the classes, so WP don't bother checking pagename. Also hierarchy needs to resolve to one thing, body class can accumulate. Btw, I'll do some check agains core and let you know. Thanks again. |
Cool! I'd like to know if you find anything. Also I'd tell if I figure something out. Thanks for great code and glad I could contribute! |
So it seems that WordPress does not use pagename when in preview of no saved posts, nevertheless the page template is took into consideration. Basically, there's a bug in Hierarchy (compared to how WP works) that make Hierarchy skip page template in case no pagename is there. This needs to be fixed. Moreover, I saw that in latest version of WP So I @slackday Would you be so nice to test if code in that branch solves your use case? |
Tested it. It works as expected for my use case. Also tested this in the context of the Controller and it also works. Nice & quick work! I like this code more than the solution I posted before. |
Thanks @slackday for yor time and you input. Sorry I could not merge your code, but there was the need a complete rewrite and it was faster to me do it. I've merged the branch in |
Fixed an issue where the wrong page hierarchy would be returned when in preview mode of a newly created page.
This was a problem when you would create a new page, selected a custom template and previewed it. The hierarchy would still be Page/Singular due to
$post->post_name
not being set.