-
Notifications
You must be signed in to change notification settings - Fork 116
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
Proposed solution to #466, adding support for admin pointers #479
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e640d66
Essentially a port of WP Internal Pointers for Stream. Needs work to …
5e46a82
Modified to allow other extensions to more easily add their own admin…
c79c762
Merge remote-tracking branch 'origin/develop' into issue-466
7012edc
Fixed phpcs errors.
321008a
Implemented 'load' setup method, also added missing braces and switch…
773a38f
Removed superfluous quotes from JSON encoded output.
3147593
Now using proper constants for hooks
13c39df
Merge branch 'develop' into issue-466
frankiejarrett 0e93b62
Some code formatting
frankiejarrett 13aa059
Use constants for page slugs
frankiejarrett 0564ab7
Changing wording
frankiejarrett 11182d5
Use constant for admin parent page
frankiejarrett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
/** | ||
* Stream pointers. Based on WP core internal pointers. | ||
* | ||
* @since 1.4.4 | ||
*/ | ||
class WP_Stream_Pointers { | ||
|
||
public static $pointers = array(); | ||
public static $caps = array(); | ||
|
||
public static function load() { | ||
add_action( 'admin_enqueue_scripts', array( 'WP_Stream_Pointers', 'enqueue_scripts' ) ); | ||
add_action( 'user_register', array( 'WP_Stream_Pointers', 'dismiss_pointers_for_new_users' ) ); | ||
} | ||
|
||
public static function init_core_pointers() { | ||
self::$pointers = array( | ||
'WP_Stream_Pointers' => array( | ||
'index.php' => 'wpstream143_extensions', | ||
'toplevel_page_' . WP_Stream_Admin::RECORDS_PAGE_SLUG => 'wpstream143_extensions', | ||
'stream_page_' . WP_Stream_Admin::SETTINGS_PAGE_SLUG => 'wpstream143_extensions', | ||
) | ||
); | ||
|
||
self::$caps = array( | ||
'WP_Stream_Pointers' => array( | ||
'wpstream143_extensions' => array( 'install_plugins' ), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Initializes the pointers. | ||
* | ||
* @since 1.4.4 | ||
* | ||
* All pointers can be disabled using the following: | ||
* remove_action( 'admin_enqueue_scripts', array( 'WP_Stream_Pointers', 'enqueue_scripts' ) ); | ||
* | ||
* Individual pointers (e.g. wpstream143_extensions) can be disabled using the following: | ||
* remove_action( 'admin_print_footer_scripts', array( 'WP_Stream_Pointers', 'pointer_wpstream143_extensions' ) ); | ||
*/ | ||
public static function enqueue_scripts( $hook_suffix ) { | ||
/* | ||
* Register feature pointers | ||
* Format: array( hook_suffix => pointer_id ) | ||
*/ | ||
self::init_core_pointers(); | ||
|
||
$get_pointers = array_merge( self::$pointers, apply_filters( 'wp_stream_pointers', array() ) ); | ||
$caps = array_merge( self::$caps, apply_filters( 'wp_stream_pointer_caps', array() ) ); | ||
|
||
foreach ( $get_pointers as $context => $registered_pointers ) { | ||
|
||
// Check if screen related pointer is registered | ||
if ( empty( $registered_pointers[ $hook_suffix ] ) ) { | ||
return; | ||
} | ||
|
||
$pointers = (array) $registered_pointers[ $hook_suffix ]; | ||
|
||
$caps_required = $caps[ $context ]; | ||
|
||
// Get dismissed pointers | ||
$dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); | ||
|
||
$got_pointers = false; | ||
foreach ( array_diff( $pointers, $dismissed ) as $pointer ) { | ||
if ( isset( $caps_required[ $pointer ] ) ) { | ||
foreach ( $caps_required[ $pointer ] as $cap ) { | ||
if ( ! current_user_can( $cap ) ) { | ||
continue 2; | ||
} | ||
} | ||
} | ||
|
||
// Bind pointer print function | ||
add_action( 'admin_print_footer_scripts', array( $context, 'pointer_' . $pointer ) ); | ||
|
||
$got_pointers = true; | ||
} | ||
} | ||
|
||
if ( ! $got_pointers ) { | ||
return; | ||
} | ||
|
||
// Add pointers script and style to queue | ||
wp_enqueue_style( 'wp-pointer' ); | ||
wp_enqueue_script( 'wp-pointer' ); | ||
} | ||
|
||
/** | ||
* Print the pointer javascript data. | ||
* | ||
* @since 1.4.4 | ||
* | ||
* @param string $pointer_id The pointer ID. | ||
* @param string $selector The HTML elements, on which the pointer should be attached. | ||
* @param array $args Arguments to be passed to the pointer JS (see wp-pointer.js). | ||
*/ | ||
public static function print_js( $pointer_id, $selector, $args ) { | ||
if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['content'] ) ) { | ||
return; | ||
} | ||
|
||
?> | ||
<script type="text/javascript"> | ||
//<![CDATA[ | ||
(function($){ | ||
var options = <?php echo json_encode( $args ) ?>, setup; | ||
|
||
if ( ! options ) { | ||
return; | ||
} | ||
|
||
options = $.extend( options, { | ||
close: function() { | ||
$.post( ajaxurl, { | ||
pointer: <?php echo json_encode( $pointer_id ) ?>, | ||
action: 'dismiss-wp-pointer' | ||
}); | ||
} | ||
}); | ||
|
||
setup = function() { | ||
$(<?php echo json_encode( $selector ) ?>).first().pointer( options ).pointer('open'); | ||
}; | ||
|
||
if ( options.position && options.position.defer_loading ) { | ||
$(window).bind( 'load.wp-pointers', setup ); | ||
} else { | ||
$(document).ready( setup ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use brackets of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
})( jQuery ); | ||
//]]> | ||
</script> | ||
<?php | ||
} | ||
|
||
public static function pointer_wpstream143_extensions() { | ||
$content = '<h3>' . esc_html__( 'Stream Extensions', 'stream' ) . '</h3>'; | ||
$content .= '<p>' . esc_html__( 'Extension plugins are now available for Stream!', 'stream' ) . '</p>'; | ||
|
||
if ( 'dashboard' === get_current_screen()->id ) { | ||
$selector = sprintf( '#toplevel_page_%s', WP_Stream_Admin::RECORDS_PAGE_SLUG ); | ||
$position = array( 'edge' => is_rtl() ? 'right' : 'left', 'align' => 'center' ); | ||
} else { | ||
$selector = sprintf( 'a[href="%s?page=%s"]', WP_Stream_Admin::ADMIN_PARENT_PAGE, WP_Stream_Admin::EXTENSIONS_PAGE_SLUG ); | ||
$position = array( 'edge' => is_rtl() ? 'right' : 'left', 'align' => 'center' ); | ||
} | ||
|
||
self::print_js( | ||
'wpstream143_extensions', | ||
$selector, | ||
array( | ||
'content' => $content, | ||
'position' => $position, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Prevents new users from seeing existing 'new feature' pointers. | ||
* | ||
* @since 1.4.4 | ||
*/ | ||
public static function dismiss_pointers_for_new_users( $user_id ) { | ||
add_user_meta( $user_id, 'dismissed_wp_pointers', '' ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Moar braces please 😄