-
Notifications
You must be signed in to change notification settings - Fork 4.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
Fix getWPAdminUrl function to actually return the /wp-admin/ URL #7096
Conversation
editor/utils/url.js
Outdated
@@ -23,7 +23,8 @@ export function getPostEditUrl( postId ) { | |||
* @return {string} WPAdmin URL. | |||
*/ | |||
export function getWPAdminURL( page, query ) { | |||
return addQueryArgs( page, query ); | |||
const url = ( window._wpAdminURL ) ? window._wpAdminURL + page : page; |
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 think we should avoid introducing new globals, instead, I'd personally prefer something like we're doing in #7018 :
A config function setWPAdminUrl()
and call this function using wp_add_inline_script
when registering the wp-utils
script on the server.
@youknowriad Agreed, I didn't like the idea of another global either. I noticed in a few other utils using I thought since those settings are already in place, might be a bit simpler than new setter functions, let me now what you think. I can try the other approach if you prefer. |
Failing test might show my initial assumption was right about state not being available. |
lib/client-assets.php
Outdated
@@ -1093,6 +1093,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) { | |||
'bodyPlaceholder' => apply_filters( 'write_your_story', __( 'Write your story', 'gutenberg' ), $post ), | |||
'isRTL' => is_rtl(), | |||
'autosaveInterval' => 10, | |||
'wpAdminURL' => admin_url(), |
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.
Another challenge is that admin_url()
is filterable, meaning it can be modified based on the provided path. I don't think this is a significant issue, but something to be aware of in the decision-making process.
It appears getWPAdminURL()
is only used in a few places:
editor/utils/url.js
14: return getWPAdminURL( 'post.php', { post: postId, action: 'edit' } );
25:export function getWPAdminURL( page, query ) {
editor/components/post-permalink/index.js
19:import { getWPAdminURL } from '../../utils/url';
112: href={ getWPAdminURL( 'options-permalink.php' ) }
editor/components/post-last-revision/index.js
13:import { getWPAdminURL } from '../../utils/url';
19: href={ getWPAdminURL( 'revision.php', { revision: lastRevisionId, gutenberg: true } ) }
editor/store/effects.js
27:import { getPostEditUrl, getWPAdminURL } from '../utils/url';
273: window.location.href = getWPAdminURL( 'edit.php', {
Can we expose these individual URLs instead, and remove getWPAdminURL()
entirely?
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.
Exposing the URLs could be worthwhile, especially if it abstract a level higher.
Because even with this fix it doesn't really solve the issue I'm trying to address, it fixes a bug and prevents some weird behavior after "Move to Trash" is called but what we really want is to define where the user is redirected. After trash event it is hard coded to edit.php which doesn't make that much sense in our P2/front-end use case.
I'm not sure how granular, or annoying, it might be to set all of these, for this example, something like "RedirectURLAfterTrash()"
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.
Fair enough. Want to open a new issue for future consideration?
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.
@danielbachhuber Created => #7136
Related (conflicting, moves At a high level, |
@aduth @youknowriad - I think this is the change you were suggesting. |
Will this be needed if #7228 is merged? |
@aduth 🤷♂️ #7228 will solve the problem by being able to set the redirect URL for trashing a post, so for my specific case it bypasses the problem with a better solution. However the |
Closing per the discussion starting at #7095 (comment), but anyone up for it can reopen. |
Description
This adds a getter and setter function to
wp.utils
for the wpAdminURL.When loading Gutenberg, it will call
setWPAdminURL( %s )
where %s is the results ofadmin_url()
This can then be used when needed with a similar call using
wp.utils.getWPAdminURL( path )
Fixes #7095
How has this been tested?
The getWPAdminURL is used during "Move to Trash", and confirmed the proper admin url is returned during the trash sequence.
You can also check it is set properly by calling
wp.utils.getWPAdminURL( 'foo.php' )
in the console after a page is loading and it should return a full/wp-admin/foo.php
however it is configured for the WP install.