WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-tinymce-plugin.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 WPDKEditorButton {
24 
32  public $id = '';
33 
41  public $title = '';
42 
50  public $javascript = '';
51 
59  public $image = '';
60 
73  public function __construct( $id, $title, $javascript, $image = '' )
74  {
75  $this->id = sanitize_title( $id );
76  $this->title = $title;
77  $this->javascript = $javascript;
78  $this->image = $image;
79  }
80 
88  public function html()
89  {
91  ?>
92  ed.addButton( '<?php echo $this->id ?>',
93  <?php echo $this->toObject() ?>
94  );
95  <?php
97  }
98 
106  public function toObject()
107  {
109  ?>
110  {
111  id : "<?php echo $this->id ?>",
112  title : "<?php echo $this->title ?>",
113  <?php echo $this->image() ?>
114  onclick : function(){ <?php echo $this->javascript ?> }
115  }
116  <?php
118  }
119 
127  private function image()
128  {
129  if ( empty( $this->image ) ) {
130  return;
131  }
132 
133  if ( is_string( $this->image ) ) {
134  $url = $this->image;
135  }
136  elseif ( is_a( $this->image, 'WPDKHTMLTagImg' ) ) {
137  $url = $this->image->src;
138  }
139 
140  return sprintf( 'image : "%s",', $url );
141  }
142 
143 }
144 
156 class WPDKTinyMCEPlugin {
157 
165  public $id = '';
166 
174  public $name = '';
175 
183  public $description = '';
184 
192  public $author = '';
193 
201  public $author_url = '';
202 
210  public $url = '';
211 
219  public $version = '';
220 
228  public $buttons = array();
229 
237  public $javascript = '';
238 
252  public function __construct( $name, $description, $javascript, $buttons, $version = '1.0.0' )
253  {
254  // If $name is 'WPDK Shortcode' $id = 'wpdk-shortcode'
255  $this->id = sanitize_title( $name );
256 
257  // Name must be without spaces, '-' or underscore and capitalized
258  $this->name = $name;
259 
260  $this->description = $description;
261  $this->version = $version;
262  $this->buttons = $buttons;
263  $this->javascript = $javascript;
264 
265  if ( current_user_can( 'edit_posts' ) && current_user_can( 'edit_pages' ) ) {
266  add_filter( 'mce_buttons', array( $this, 'mce_buttons' ) );
267  add_filter( 'mce_external_plugins', array( $this, 'mce_external_plugins' ) );
268 
269  /* Passing PHP params to script */
270  add_action( 'admin_head-post.php', array( $this, 'admin_head' ) );
271  add_action( 'admin_head-post-new.php', array( $this, 'admin_head' ) );
272 
273  /* Add bbutton in HTML view */
274  add_action( 'admin_footer-post.php', array( $this, 'admin_footer' ) );
275  add_action( 'admin_footer-post-new.php', array( $this, 'admin_footer' ) );
276  }
277 
278  }
279 
285  public function admin_footer()
286  {
288  <script type="text/javascript">
289  (function ( $ )
290  {
291  "use strict";
292  QTags.addButton( 'wpdk', 'WPDK', _WPDKShortcodes.open_dialog );
293  }( window.jQuery ));
294  </script>
295  <?php
297  }
298 
304  public function admin_head()
305  {
307  <script type='text/javascript'>
308  var _WPDKShortcodes = {
309  wpdk_uri_css : '<?php echo WPDK_URI_CSS ?>',
310  wpdk_uri_javascript : '<?php echo WPDK_URI_JAVASCRIPT ?>',
311  open_dialog : function() { var dialog = new WPDKTwitterBootstrapModal( 'wpdk-shortcodes-dialog', '<?php _e( 'WPDK Editor Manager', WPDK_TEXTDOMAIN ) ?>', '<?php _e( '<h4>Information</h4><p>This feature coming in next soon release!</p>', WPDK_TEXTDOMAIN ) ?>' ); dialog.display(); },
312  buttons : [
313  <?php
314  $s = array();
318  foreach( $this->buttons as $button ) {
319  $s[] = $button->toObject();
320  }
321  echo implode( ',', $s );
322  ?>
323  ]
324  };
325  </script>
326  <?php
328  }
329 
337  public function addButton( $button )
338  {
339  if ( is_a( $button, 'WPDKEditorButton' ) ) {
340  $this->buttons[] = $button;
341  }
342  }
343 
353  public function mce_buttons( $buttons )
354  {
355  if ( empty( $this->buttons ) ) {
356  return $buttons;
357  }
358 
362  foreach ( $this->buttons as $button ) {
363  array_push( $buttons, $button->id );
364  }
365 
366  return $buttons;
367  }
368 
378  public function mce_external_plugins( $plugin_array )
379  {
380  if( !empty( $this->javascript ) ) {
381  $plugin_array[$this->name] = $this->javascript;
382  }
383  return $plugin_array;
384  }
385 
393  public function html()
394  {
396  <script type="text/javascript">
397 (function ()
398 {
399  tinymce.create( 'tinymce.plugins.<?php echo $this->name ?>', {
408  init : function ( ed, url )
409  {
410  <?php ?>
411  ed.addButton( 'dropcap', {
412  title : 'DropCap',
413  cmd : 'dropcap',
414  image : url + '/dropcap.jpg'
415  } );
416  <?php ?>
417 
418  },
419 
430  createControl : function ( n, cm )
431  {
432  return null;
433  },
434 
441  getInfo : function ()
442  {
443  return {
444  longname : '<?php echo $this->description ?>',
445  author : '<?php echo $this->author ?>',
446  authorurl : '<?php echo $this->author_url ?>',
447  infourl : '<?php echo $this->url ?>',
448  version : '<?php echo $this->version ?>'
449  };
450  }
451  } );
452 
453  // Register plugin
454  tinymce.PluginManager.add( '<?php echo $this->id ?>', tinymce.plugins.<?php echo $this->name ?> );
455 })();
456 </script>
457 <?php
458 
460 
461  }
462 
463 }
464