Skip to content
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

Added admin pointer #199

Merged
merged 15 commits into from
Mar 4, 2022
83 changes: 83 additions & 0 deletions admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package performance-lab
*/

define( 'PERLAB_ADMIN_POINTER', 'perflab-admin-pointer' );
mitogh marked this conversation as resolved.
Show resolved Hide resolved

/**
* Adds the modules page to the Settings menu.
*
Expand Down Expand Up @@ -339,6 +341,87 @@ function perflab_get_module_data( $module_file ) {
return $module_data;
}

/**
* Initialise Admin Pointer
*
* Handles the bootstrapping of the admin pointer.
* Mainly jQuery code that is self-initialising.
*
* @param string $hook_suffix The current admin page.
* @since 1.0.0
*/
function perflab_admin_pointer( $hook_suffix ) {

if ( ! in_array( $hook_suffix, array( 'index.php', 'plugins.php' ), true ) ) {
return;
}

$current_user = get_current_user_id();
$dismissed = explode( ',', (string) get_user_meta( $current_user, 'dismissed_wp_pointers', true ) );

if ( in_array( PERLAB_ADMIN_POINTER, $dismissed, true ) ) {
mitogh marked this conversation as resolved.
Show resolved Hide resolved
return;
}

// Enqueue pointer CSS and JS.
wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wp-pointer' );
mitogh marked this conversation as resolved.
Show resolved Hide resolved
}
add_action( 'admin_enqueue_scripts', 'perflab_admin_pointer' );

/**
* Renders the Admin Pointer
*
* Handles the rendering of the admin pointer.
*
* @since 1.0.0
*/
function perflab_render_pointer() {

$heading = __( 'Performance Lab', 'performance-lab' );
$wp_kses_options = array(
'a' => array(
'href' => array(),
),
);

$content = sprintf(
/* translators: %s: settings page link */
__( 'You can now test upcoming WordPress performance features. Open %s to individually toggle the performance features included in the plugin.', 'performance-lab' ),
'<a href="' . esc_url( admin_url( '/options-general.php?page=perflab-modules' ) ) . '">' . __( 'Settings > Performance', 'performance-lab' ) . '</a>'
mitogh marked this conversation as resolved.
Show resolved Hide resolved
);

?>
<script id="perflab-admin-pointer" type="text/javascript">
jQuery( function() {
// Pointer Options
var options = {
content: '<h3><?php echo esc_js( $heading ); ?></h3>' + '<p><?php echo wp_kses( $content, $wp_kses_options ); ?></p>',
mitogh marked this conversation as resolved.
Show resolved Hide resolved
position: {
edge: 'left',
align: 'right',
},
pointerClass: 'wp-pointer arrow-top',
pointerWidth: 420,
close: function() {
jQuery.post(
window.ajaxurl,
{
pointer: '<?php echo esc_js( PERLAB_ADMIN_POINTER ); ?>',
mitogh marked this conversation as resolved.
Show resolved Hide resolved
action: 'dismiss-wp-pointer',
mitogh marked this conversation as resolved.
Show resolved Hide resolved
mitogh marked this conversation as resolved.
Show resolved Hide resolved
}
);
}
};

jQuery( '#menu-settings' ).pointer( options ).pointer( 'open' );

} );
mitogh marked this conversation as resolved.
Show resolved Hide resolved
</script>
<?php
}
add_action( 'admin_print_footer_scripts', 'perflab_render_pointer' );
mitogh marked this conversation as resolved.
Show resolved Hide resolved

/**
* Adds a link to the modules page to the plugin's entry in the plugins list table.
*
Expand Down