Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. <?php # -*- coding: utf-8 -*-
  2.  
  3. /**
  4. * Plugin Name: Shortcode Replace
  5. * Plugin URI:
  6. * Description:
  7. * Version: 0.0.1
  8. * Text Domain:
  9. * Domain Path: /languages
  10. * License: MIT
  11. * License URI:
  12. */
  13.  
  14. namespace FbShortcodeReplace;
  15.  
  16. if ( ! function_exists( 'add_action' ) ) {
  17. exit();
  18. }
  19.  
  20. if ( ! is_admin() ) {
  21. return;
  22. }
  23.  
  24. add_action( 'admin_enqueue_scripts', __NAMESPACE__ . 'initialize' );
  25. function initialize( $page ) {
  26.  
  27. if ( 'post.php' === $page ) {
  28. add_filter( 'mce_external_plugins', __NAMESPACE__ . 'add_tinymce_plugin' );
  29. }
  30. }
  31.  
  32. function add_tinymce_plugin( $plugins ) {
  33.  
  34. if ( ! is_array( $plugins ) ) {
  35. $plugins = array();
  36. }
  37.  
  38. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : '';
  39. $url = plugins_url( '/assets/js/fb_shortcode_replace.js', __FILE__ );
  40.  
  41. $plugins = array_merge( $plugins, array( 'fb_shortcode_replace' => $url ) );
  42. return $plugins;
  43. }
  44.  
  45. tinymce.PluginManager.add( 'fb_shortcode_replace', function( editor ) {
  46.  
  47. var shortcode = /[.+]/g;
  48. var additional = 'FB-TEST';
  49.  
  50. function ifShortcode( content ) {
  51.  
  52. return content.search( /[.+]/ ) !== -1;
  53. }
  54.  
  55. function replaceShortcodes( content ) {
  56.  
  57. return content.replace( shortcode, function( match ) {
  58. return html( match );
  59. } );
  60. }
  61.  
  62. function restoreShortcodes( content ) {
  63.  
  64. return content.replace( additional, '' );
  65. }
  66.  
  67. function html( data ) {
  68.  
  69. console.log( data );
  70. return additional + data + additional;
  71. }
  72.  
  73. editor.on( 'BeforeSetContent', function( event ) {
  74.  
  75. // No shortcodes in content, return.
  76. if ( ! ifShortcode( event.content ) ) {
  77. return;
  78. }
  79.  
  80. event.content = replaceShortcodes( event.content );
  81. } );
  82.  
  83.  
  84. editor.on( 'PostProcess', function( event ) {
  85.  
  86. if ( event.get ) {
  87. event.content = restoreShortcodes( event.content );
  88. }
  89. } );
  90. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement