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
85 changes: 85 additions & 0 deletions admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,88 @@ 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.
*
* @since 1.0.0
* @return void
mitogh marked this conversation as resolved.
Show resolved Hide resolved
*/
function perflab_admin_pointer() {
felixarntz marked this conversation as resolved.
Show resolved Hide resolved

wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wp-pointer', false, array( 'jquery' ) );
mitogh marked this conversation as resolved.
Show resolved Hide resolved
mitogh marked this conversation as resolved.
Show resolved Hide resolved

$current_user = get_current_user_id();
$meta_key = 'my-pointer-slug-dismissed';
mitogh marked this conversation as resolved.
Show resolved Hide resolved
mitogh marked this conversation as resolved.
Show resolved Hide resolved

$heading = __( 'Performance Lab', 'performance-lab' );
/* translators: %s: settings page link */
$content = sprintf( __( '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

if ( ! get_user_meta( $current_user, $meta_key, true ) ) :
mitogh marked this conversation as resolved.
Show resolved Hide resolved
?>
<script type="text/javascript">

jQuery(function() {

var ajaxURL = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
mitogh marked this conversation as resolved.
Show resolved Hide resolved
// Pointer Options
var options = {
content: '<h3><?php echo $heading; ?></h3>' + '<p><?php echo $content; ?></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(
ajaxURL,
{
pointer: 'my-pointer-slug-dismissed',
mitogh marked this conversation as resolved.
Show resolved Hide resolved
action: 'dismiss-wp-pointer',
}
);
}
};

jQuery('#menu-settings').pointer(options).pointer('open');
mitogh marked this conversation as resolved.
Show resolved Hide resolved

});
</script>
mitogh marked this conversation as resolved.
Show resolved Hide resolved
<?php
endif;
}

add_action( 'in_admin_header', 'perflab_admin_pointer' );
mitogh marked this conversation as resolved.
Show resolved Hide resolved

/**
* Update Pointer User Meta
*
* Checks if the action $_POST index is set and if it is, it will update the user meta.
* This prevents the pointer from showing on refresh.
*
* @since 1.0.0
* @return void
mitogh marked this conversation as resolved.
Show resolved Hide resolved
*/
function perflab_update_pointer_meta() {
mitogh marked this conversation as resolved.
Show resolved Hide resolved

$current_user = get_current_user_id();
$meta_key = 'my-pointer-slug-dismissed';
mitogh marked this conversation as resolved.
Show resolved Hide resolved
mitogh marked this conversation as resolved.
Show resolved Hide resolved

if ( isset( $_POST['action'] ) && 'dismiss-wp-pointer' === $_POST['action'] ) {
mitogh marked this conversation as resolved.
Show resolved Hide resolved
update_user_meta(
$current_user,
$meta_key,
$_POST['pointer'],
mitogh marked this conversation as resolved.
Show resolved Hide resolved
true
);
}

}

add_action( 'admin_init', 'perflab_update_pointer_meta' );
mitogh marked this conversation as resolved.
Show resolved Hide resolved