WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-pointer.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * [DRAFT]
6  *
7  * THE FOLLOWING CODE IS A DRAFT. FEEL FREE TO USE IT TO MAKE SOME EXPERIMENTS, BUT DO NOT USE IT IN ANY CASE IN
8  * PRODUCTION ENVIRONMENT. ALL CLASSES AND RELATIVE METHODS BELOW CAN CHNAGE IN THE FUTURE RELEASES.
9  *
10  */
11 
23 class WPDKPointer {
24 
32  public $align;
33 
42  public $buttonClose;
43 
51  public $buttonPrimary;
59  public $content;
67  public $edge;
75  public $id;
83  public $nextPage;
91  public $selector;
99  public $title;
100 
115  public function __construct( $id, $selector, $title, $content, $edge = 'left', $align = 'top' ) {
116  $this->id = sanitize_key( $id );
117  $this->selector = $selector;
118  $this->title = $title;
119  $this->content = $content;
120  $this->edge = $edge;
121  $this->align = $align;
122 
123  $this->nextPage = '';
124 
125  $this->buttonClose = array(
126  'id' => 'wpdk-pointer-button-close',
127  'label' => __( 'Dismiss', WPDK_TEXTDOMAIN ),
128  'dismiss' => true,
129  'function' => '',
130  'page' => ''
131  );
132 
133  $this->buttonPrimary = array(
134  'id' => 'wpdk-button-pointer-next',
135  'label' => __( 'Next', WPDK_TEXTDOMAIN ),
136  'page' => ''
137  );
138 
139  add_action( 'admin_print_footer_scripts', array( $this, 'display' ) );
140 
141  }
142 
148  public function display() {
149 
150  /* Get info pointer. */
151  $pointer_id = $this->id;
152  $selector = $this->selector;
153 
154  /* Build the pointer content. */
155  $args = array(
156  'content' => sprintf( '%s%s', $this->_title(), $this->_content() ),
157  'position' => array(
158  'edge' => $this->edge,
159  'align' => $this->align
160  )
161  );
162 
163  /* If empty exit. */
164  if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['content'] ) ) {
165  return;
166  }
167 
168  /* Controllo se l'utente a deciso di dismettere questo pointer, in tal caso non lo visualizzo */
169  $id_user = get_current_user_id();
170 
171  /* Check if dismiss, see 'action_wpdk_dismiss_wp_pointer' in Ajax for detail. */
172  $dismissed = unserialize( get_user_meta( $id_user, 'wpdk_dismissed_wp_pointer', true ) );
173  if ( isset( $dismissed[$pointer_id] ) ) {
174  return;
175  }
176 
177  /* Build a standard next button when the property nextPage is not empty. */
178  if ( !empty( $this->nextPage ) ) {
179  $this->buttonPrimary['page'] = $this->nextPage;
180  }
181 
182  ?>
183  <script type="text/javascript">
184  //<![CDATA[
185  jQuery( document ).ready( function ( $ ) {
186  var options = <?php echo json_encode( $args ); ?>, setup;
187 
188  if ( !options ) {
189  return;
190  }
191 
192  options = $.extend( options, {
193 
194  buttons : function ( event, t ) {
195  var button = jQuery( '<?php echo $this->_buttonClose() ?>' );
196  button.bind( 'click.pointer', function () {
197  t.element.pointer( 'close' );
198  } );
199  return button;
200  },
201 
202  close : function () {
203  var button = jQuery( '#<?php echo $this->buttonClose['id'] ?>' );
204  <?php if ( empty( $this->buttonClose['function'] ) ) : ?>
205  if( button.data('dismiss') ) {
206  $.post( ajaxurl, {
207  pointer : '<?php echo $pointer_id; ?>',
208  action : 'wpdk_action_dismiss_wp_pointer'
209  } );
210  }
211  <?php else: ?>
212  <?php echo $this->buttonClose['function'] ?>
213  <?php endif; ?>
214  }
215  } );
216 
217  $( '<?php echo $selector; ?>' ).pointer( options ).pointer( 'open' );
218 
219  <?php if ( !empty( $this->buttonPrimary ) && !empty( $this->buttonPrimary['page']) ) : ?>
220  $( '#<?php echo $this->buttonClose['id'] ?>' ).after( '<?php echo $this->_buttonPrimary() ?>' );
221  $( '#<?php echo $this->buttonPrimary['id'] ?>' ).click( function () {
222  <?php
223  if ( empty( $this->buttonPrimary['function'] ) && !empty( $this->buttonPrimary['page'] ) ) {
224  echo 'window.location="' . admin_url( 'admin.php?page=' . $this->buttonPrimary['page'] ) . '";';
225  }
226  elseif ( !empty( $this->buttonPrimary['function'] ) ) {
227  echo $this->buttonPrimary['function'];
228  }
229  ?>
230  } );
231  <?php endif; ?>
232 
233  } );
234  //]]>
235  </script>
236  <?php
237  }
238 
246  private function _title() {
247  if ( '<' != substr( $this->title, 0, 1 ) ) {
248  return sprintf( '<h3>%s</h3>', $this->title );
249  }
250  return $this->title;
251  }
252 
260  private function _content() {
261  if ( '<' != substr( $this->content, 0, 1 ) ) {
262  return sprintf( '<p>%s</p>', $this->content );
263  }
264  return $this->content;
265  }
266 
274  private function _buttonClose() {
275  $dismiss = '';
276  if( isset( $this->buttonClose['dismiss']) && true === $this->buttonClose['dismiss'] ) {
277  $dismiss = 'data-dismiss="true"';
278  }
279  $result = sprintf( '<a id="%s" %s class="button-secondary">%s</a>', $this->buttonClose['id'], $dismiss, $this->buttonClose['label'] );
280  return $result;
281  }
282 
290  private function _buttonPrimary() {
291  $result = sprintf( '<a id="%s" style="margin-right:5px" class="button-primary">%s</a>', $this->buttonPrimary['id'], $this->buttonPrimary['label'] );
292  return $result;
293  }
294 
295 }
296 
307 class WPDKPointerButton {
308 
309  public $id;
310  public $page;
311  public $dismiss;
312 
313 }
314 
315