| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* a class to manage WP pointers |
| 5 |
* offers the possibility to have customized buttons |
| 6 |
* |
| 7 |
* @since 1.7.7 |
| 8 |
*/ |
| 9 |
class PLL_Pointer { |
| 10 |
protected $args; |
| 11 |
|
| 12 |
/** |
| 13 |
* constructor |
| 14 |
* enqueues the pointer script |
| 15 |
* |
| 16 |
* list of parameters accepted in $args: |
| 17 |
* |
| 18 |
* pointer => required, unique identifier of the pointer |
| 19 |
* id => required, the pointer will be attached to this html id |
| 20 |
* position => optional array, if used both sub parameters are required |
| 21 |
* edge => 'top' or 'bottom' |
| 22 |
* align => 'right' or 'left' |
| 23 |
* width => optional, the width in px |
| 24 |
* title => required, title |
| 25 |
* content => required, content |
| 26 |
* buttons => optional array of arrays, by default the pointer uses the standard dismiss button offered by WP |
| 27 |
* label => the label of the button |
| 28 |
* link => optional link for the button. By default, the button just dismisses the pointer |
| 29 |
* |
| 30 |
* @since 1.7.7 |
| 31 |
* |
| 32 |
* @param array $args |
| 33 |
*/ |
| 34 |
public function __construct( $args ) { |
| 35 |
$this->args = $args; |
| 36 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* enqueue javascripts and styles if the pointer has not been dismissed |
| 41 |
* |
| 42 |
* @since 1.7.7 |
| 43 |
*/ |
| 44 |
public function enqueue_scripts() { |
| 45 |
$dismissed = explode( ',', get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); |
| 46 |
if ( in_array( $this->args['pointer'], $dismissed ) || ! current_user_can( 'manage_options' ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
// Add pointer javascript |
| 51 |
add_action( 'admin_print_footer_scripts', array( $this, 'print_js' ) ); |
| 52 |
|
| 53 |
wp_enqueue_style( 'wp-pointer' ); |
| 54 |
wp_enqueue_script( 'wp-pointer' ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* adds the javascript of our pointer to the page |
| 59 |
* |
| 60 |
* @since 1.7.7 |
| 61 |
*/ |
| 62 |
public function print_js() { |
| 63 |
|
| 64 |
// add optional buttons |
| 65 |
if ( ! empty( $this->args['buttons'] ) ) { |
| 66 |
$b = " |
| 67 |
var widget = pointer.pointer( 'widget' ); |
| 68 |
var buttons = $( '.wp-pointer-buttons', widget ); |
| 69 |
$( 'a.close', widget ).remove();"; // removes the WP button |
| 70 |
|
| 71 |
// all the buttons use the standard WP ajax action to remember the pointer has been dismissed |
| 72 |
foreach ( $this->args['buttons'] as $button ) { |
| 73 |
$b .= sprintf( " |
| 74 |
$( '<a>' ).addClass( '%s' ).html( '%s' ).css( 'margin-left', '10px' ).click( function() { |
| 75 |
$.post( ajaxurl, { |
| 76 |
pointer: '%s', |
| 77 |
action: 'dismiss-wp-pointer' |
| 78 |
}, function( response ) { |
| 79 |
%s |
| 80 |
} ); |
| 81 |
} ).appendTo( buttons );", |
| 82 |
empty( $button['link'] ) ? 'button' : 'button button-primary', |
| 83 |
$button['label'], |
| 84 |
$this->args['pointer'], |
| 85 |
empty( $button['link'] ) ? "pointer.pointer( 'close' )" : sprintf( "location.href = '%s'", $button['link'] ) |
| 86 |
); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$js = sprintf( " |
| 91 |
//<![CDATA[ |
| 92 |
jQuery( document ).ready( function( $ ) { |
| 93 |
var pointer = $( '#%s' ).pointer( { |
| 94 |
content: '%s', |
| 95 |
%s |
| 96 |
%s |
| 97 |
} ); |
| 98 |
pointer.pointer( 'open' ); |
| 99 |
%s |
| 100 |
} ); |
| 101 |
// ]]>", |
| 102 |
$this->args['id'], |
| 103 |
sprintf( '<h3>%s</h3><p>%s</p>', $this->args['title'], $this->args['content'] ), |
| 104 |
empty( $this->args['position'] ) ? '' : sprintf( 'position: {edge: "%s", align: "%s",},', $this->args['position']['edge'], $this->args['position']['align'] ), |
| 105 |
empty( $this->args['width'] ) ? '' : sprintf( 'pointerWidth: %d,', $this->args['width'] ), |
| 106 |
empty( $b ) ? '' : $b |
| 107 |
); |
| 108 |
echo "<script type='text/javascript'>" .$js. "</script>"; |
| 109 |
} |
| 110 |
} |
| 111 |
|