PLL_Pointer

A class to manage WP pointers offers the possibility to have customized buttons.

Defined (1)

The class is defined in the following location(s).

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