| 1 |
<?php |
| 2 |
/** |
| 3 |
* Stream pointers. Based on WP core internal pointers. |
| 4 |
* |
| 5 |
* @since 1.4.4 |
| 6 |
*/ |
| 7 |
class WP_Stream_Pointers { |
| 8 |
|
| 9 |
public static $pointers = array(); |
| 10 |
public static $caps = array(); |
| 11 |
|
| 12 |
public static function load() { |
| 13 |
add_action( 'admin_enqueue_scripts', array( 'WP_Stream_Pointers', 'enqueue_scripts' ) ); |
| 14 |
add_action( 'user_register', array( 'WP_Stream_Pointers', 'dismiss_pointers_for_new_users' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
public static function init_core_pointers() { |
| 18 |
self::$pointers = array( |
| 19 |
'WP_Stream_Pointers' => array( |
| 20 |
'index.php' => 'wpstream143_extensions', |
| 21 |
'toplevel_page_' . WP_Stream_Admin::RECORDS_PAGE_SLUG => 'wpstream143_extensions', |
| 22 |
'stream_page_' . WP_Stream_Admin::SETTINGS_PAGE_SLUG => 'wpstream143_extensions', |
| 23 |
) |
| 24 |
); |
| 25 |
|
| 26 |
self::$caps = array( |
| 27 |
'WP_Stream_Pointers' => array( |
| 28 |
'wpstream143_extensions' => array( 'install_plugins' ), |
| 29 |
) |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Initializes the pointers. |
| 35 |
* |
| 36 |
* @since 1.4.4 |
| 37 |
* |
| 38 |
* All pointers can be disabled using the following: |
| 39 |
* remove_action( 'admin_enqueue_scripts', array( 'WP_Stream_Pointers', 'enqueue_scripts' ) ); |
| 40 |
* |
| 41 |
* Individual pointers (e.g. wpstream143_extensions) can be disabled using the following: |
| 42 |
* remove_action( 'admin_print_footer_scripts', array( 'WP_Stream_Pointers', 'pointer_wpstream143_extensions' ) ); |
| 43 |
*/ |
| 44 |
public static function enqueue_scripts( $hook_suffix ) { |
| 45 |
/* |
| 46 |
* Register feature pointers |
| 47 |
* Format: array( hook_suffix => pointer_id ) |
| 48 |
*/ |
| 49 |
self::init_core_pointers(); |
| 50 |
|
| 51 |
$get_pointers = array_merge( self::$pointers, apply_filters( 'wp_stream_pointers', array() ) ); |
| 52 |
$caps = array_merge( self::$caps, apply_filters( 'wp_stream_pointer_caps', array() ) ); |
| 53 |
|
| 54 |
foreach ( $get_pointers as $context => $registered_pointers ) { |
| 55 |
|
| 56 |
// Check if screen related pointer is registered |
| 57 |
if ( empty( $registered_pointers[ $hook_suffix ] ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
$pointers = (array) $registered_pointers[ $hook_suffix ]; |
| 62 |
|
| 63 |
$caps_required = $caps[ $context ]; |
| 64 |
|
| 65 |
// Get dismissed pointers |
| 66 |
$dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); |
| 67 |
|
| 68 |
$got_pointers = false; |
| 69 |
foreach ( array_diff( $pointers, $dismissed ) as $pointer ) { |
| 70 |
if ( isset( $caps_required[ $pointer ] ) ) { |
| 71 |
foreach ( $caps_required[ $pointer ] as $cap ) { |
| 72 |
if ( ! current_user_can( $cap ) ) { |
| 73 |
continue 2; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Bind pointer print function |
| 79 |
add_action( 'admin_print_footer_scripts', array( $context, 'pointer_' . $pointer ) ); |
| 80 |
|
| 81 |
$got_pointers = true; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! $got_pointers ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
// Add pointers script and style to queue |
| 90 |
wp_enqueue_style( 'wp-pointer' ); |
| 91 |
wp_enqueue_script( 'wp-pointer' ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Print the pointer javascript data. |
| 96 |
* |
| 97 |
* @since 1.4.4 |
| 98 |
* |
| 99 |
* @param string $pointer_id The pointer ID. |
| 100 |
* @param string $selector The HTML elements, on which the pointer should be attached. |
| 101 |
* @param array $args Arguments to be passed to the pointer JS (see wp-pointer.js). |
| 102 |
*/ |
| 103 |
public static function print_js( $pointer_id, $selector, $args ) { |
| 104 |
if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['content'] ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
?> |
| 109 |
<script type="text/javascript"> |
| 110 |
//<![CDATA[ |
| 111 |
(function($){ |
| 112 |
var options = <?php echo json_encode( $args ) ?>, setup; |
| 113 |
|
| 114 |
if ( ! options ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
options = $.extend( options, { |
| 119 |
close: function() { |
| 120 |
$.post( ajaxurl, { |
| 121 |
pointer: <?php echo json_encode( $pointer_id ) ?>, |
| 122 |
action: 'dismiss-wp-pointer' |
| 123 |
}); |
| 124 |
} |
| 125 |
}); |
| 126 |
|
| 127 |
setup = function() { |
| 128 |
$(<?php echo json_encode( $selector ) ?>).first().pointer( options ).pointer('open'); |
| 129 |
}; |
| 130 |
|
| 131 |
if ( options.position && options.position.defer_loading ) { |
| 132 |
$(window).bind( 'load.wp-pointers', setup ); |
| 133 |
} else { |
| 134 |
$(document).ready( setup ); |
| 135 |
} |
| 136 |
|
| 137 |
})( jQuery ); |
| 138 |
//]]> |
| 139 |
</script> |
| 140 |
<?php |
| 141 |
} |
| 142 |
|
| 143 |
public static function pointer_wpstream143_extensions() { |
| 144 |
$content = '<h3>' . esc_html__( 'Stream Extensions', 'stream' ) . '</h3>'; |
| 145 |
$content .= '<p>' . esc_html__( 'Extension plugins are now available for Stream!', 'stream' ) . '</p>'; |
| 146 |
|
| 147 |
if ( 'dashboard' === get_current_screen()->id ) { |
| 148 |
$selector = sprintf( '#toplevel_page_%s', WP_Stream_Admin::RECORDS_PAGE_SLUG ); |
| 149 |
$position = array( 'edge' => is_rtl() ? 'right' : 'left', 'align' => 'center' ); |
| 150 |
} else { |
| 151 |
$selector = sprintf( 'a[href="%s?page=%s"]', WP_Stream_Admin::ADMIN_PARENT_PAGE, WP_Stream_Admin::ADMIN_PARENT_PAGE ); |
| 152 |
$position = array( 'edge' => is_rtl() ? 'right' : 'left', 'align' => 'center' ); |
| 153 |
} |
| 154 |
|
| 155 |
self::print_js( |
| 156 |
'wpstream143_extensions', |
| 157 |
$selector, |
| 158 |
array( |
| 159 |
'content' => $content, |
| 160 |
'position' => $position, |
| 161 |
) |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Prevents new users from seeing existing 'new feature' pointers. |
| 167 |
* |
| 168 |
* @since 1.4.4 |
| 169 |
*/ |
| 170 |
public static function dismiss_pointers_for_new_users( $user_id ) { |
| 171 |
add_user_meta( $user_id, 'dismissed_wp_pointers', '' ); |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|