-
Notifications
You must be signed in to change notification settings - Fork 385
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
Add an admin pointer for version 1.0 #1271
Conversation
Mainly follow Weston's mockup in Issue 1254. Enqueue the script only if this hasn't been dismissed. Based on: https://code.tutsplus.com/articles/integrating-with-wordpress-ui-admin-pointers--wp-26853 Also uses the scaffold of amp-block-validation.js.
* | ||
* @var string | ||
*/ | ||
const TEMPLATE_POINTER_ID = 'amp_template_mode_pointer_10'; |
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.
When you're testing this, you might have to change this value locally. Once you dismiss the pointer, it won't display again. Unless you manually update the user meta value.
function amp_admin_pointer() { | ||
$admin_pointer = new AMP_Admin_Pointer(); | ||
$admin_pointer->init(); | ||
} |
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 bootstrapping is repetitive. It'd be nice if amp_post_meta_box()
, amp_editor_core_blocks()
, and amp_admin_pointer()
could be combined into one function, like amp_admin_bootstrap()
.
But in the unlikely case that a user unhooked one of these from wp_loaded
, this would break that.
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.
Then the questions are:
- Do we need to provide the ability to unhook any of those components?
- Do we need to provide the ability to unhook any of these components?
add_action( 'wp_loaded', 'amp_editor_core_blocks' );
add_action( 'wp_loaded', 'amp_post_meta_box' );
add_action( 'wp_loaded', 'amp_editor_core_blocks' );
add_action( 'wp_loaded', 'amp_add_options_menu' );
add_action( 'wp_loaded', 'amp_admin_pointer' );
Changing that to add_action( 'wp_loaded', 'amp_admin_bootstrap' );
and then letting amp_admin_bootstrap()
manage the order and initializations results in less callbacks having to be processed, i.e. only 1 instead of 5. It'll be slightly faster.
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 questions. I think it'd be best to avoid renaming the functions above, as a user could have unhooked them.
But it's not a strongly-held belief, and there probably aren't many users who would have unhooked them.
Another option is renaming amp_admin_pointer()
to amp_admin_bootstrap()
, without making any other change.
Then, any future admin classes could add their bootstrapping there.
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 option is renaming amp_admin_pointer() to amp_admin_bootstrap(), without making any other change.
This option is future-thinking as you are planning for future bootstrapping functionality. That's good.
Now, one could argue that confusion may be introduced when a dev is deciding where to bootstrap. Adding an inline comment above the action hook may elevate that confusion.
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.
Though amp_editor_core_blocks() has never been in a released version. It was added in version 1.0
.
amp_post_meta_box() was added in 0.6
. I'm not sure why someone would want to hide the meta box section entirely, and remove the ability to change the AMP enabled status:
Maybe I could only combine amp_editor_core_blocks()
and amp_admin_pointer()
into amp_admin_bootstrap()
.
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'd vote to review how we want to bootstrap and consider what callbacks we can consolidate. That said, I think it's outside the scope of this particular PR. We may want to open a ticket for it and begin the discussion process beyond ours here.
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.
Sounds good, Tonya.
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, I think we should try to get away from bootstrapping like this because the class instantiation variable that gets created is not accessible anywhere. We should instead of an AMP plugin manager that is responsible for instantiating classes and then keeping around the references to the class instances for the plugin (and others) to access later.
), | ||
'position' => array( | ||
'edge' => 'left', | ||
'align' => 'middle', |
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 'edge' => 'left'
is probably correct, but the 'align'
value might need improvement.
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.
@kienstra I think both left
and middle
works well.
assets/js/amp-admin-pointer.js
Outdated
pointer: data.pointer.pointer_id, | ||
action: 'dismiss-wp-pointer' | ||
} ); | ||
} |
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.
@kienstra Right now, the pointer scrolls instead of sticking in a fixed position with the AMP menu.
One way to fix this is to apply CSS position: fixed;
. A possible solution is to leverage the show
function and then set the CSS:
show: function( event, t ) {
t.pointer.css( 'position', 'fixed' );
}
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.
Thanks, Tonya! That's a great suggestion to use show
. Commit e84e22 does that.
* @since 1.0 | ||
*/ | ||
public function enqueue_pointer() { | ||
$dismissed = explode( ',', strval( get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ) ); |
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.
When no pointers have been dismissed, an empty is returned from get_user_meta
. In that case, there's no need to run the rest of the code , i.e. strval()
, explode()
, and in_array()
. We can then optimize this code by separating it out.
To keep the method focused on the enqueueing tasks, consider abstracting it into a new method:
protected function is_pointer_dismissed() {
$dismissed = get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true );
if ( empty( $dismissed ) ) {
return false;
}
$dismissed = explode( ',', strval( $dismissed ) );
return in_array( self::TEMPLATE_POINTER_ID, $dismissed, true );
}
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.
Hi @hellofromtonya,
Thanks, that's a good suggestion. Self-documenting functions like has_pointer_been_dismissed()
help 😄
But I think it only optimizes this if $dismissed
is empty...if the user has never dismissed a pointer before.
Once the user dismisses this pointer, $dismissed
should at least have the string of this pointer. Unless there was an issue in the POST request in the JS file.
What do you think?
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.
Correct. If the user has never dismissed a pointer, then the user meta does not exist or is empty. In that case, it optimizes the code by bailing out and not running strval()
, explode()
, or in_array()
.
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 also find it expresses the intent more clearly to help us more quickly read and understand what's going on.
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.
@kienstra You could name that guard clause method something less passive such as
is_pointer_dismissed()
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.
Thanks, commit e60f0 adds is_pointer_dismissed()
.
$dismissed = explode( ',', strval( get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ) ); | ||
|
||
// Exit if the pointer has been dismissed. | ||
if ( in_array( self::TEMPLATE_POINTER_ID, $dismissed, true ) ) { |
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, this can now be simplified to this:
if ( $this->is_pointer_dismissed() ) {
return;
}
These changes make it a little bit easier to read and quickly know what's going on in the guard clause.
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.
Thanks, this is applied in commit e60f0.
Fix issue where pointer didn't appear alongside the AMP menu item. Uses the show function, as Tonya suggested.
Hi @hellofromtonya, There are still a few open discussions here. I'm happy to add |
@kienstra your code works well. Great job! I'd vote for abstracting the dismissed guard clause for the following reasons:
I suggest naming the method name I'll leave it up to you whether you wish to change it or not. I'm approving the PR as your code does work. |
Abstract logic from enqueue_pointer() into this. First check that the user meta is not empty. Then, return whether it is in the array. Props @hellofromtonya.
Thanks, Applied Suggestion Hi @hellofromtonya, Commit e60f0 adds |
Request For Review
Hi @hellofromtonya,
Thanks for your PR #1274. I'm going to review it next.
It would be great if you could review this. Then, maybe @westonruter could give it another look and merge when it's ready.
/wp-admin
.Closes #1254