Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php # -*- coding: utf-8 -*-
- /**
- * Plugin Name: Shortcode Replace
- * Plugin URI:
- * Description:
- * Version: 0.0.1
- * Text Domain:
- * Domain Path: /languages
- * License: MIT
- * License URI:
- */
- namespace FbShortcodeReplace;
- if ( ! function_exists( 'add_action' ) ) {
- exit();
- }
- if ( ! is_admin() ) {
- return;
- }
- add_action( 'admin_enqueue_scripts', __NAMESPACE__ . 'initialize' );
- function initialize( $page ) {
- if ( 'post.php' === $page ) {
- add_filter( 'mce_external_plugins', __NAMESPACE__ . 'add_tinymce_plugin' );
- }
- }
- function add_tinymce_plugin( $plugins ) {
- if ( ! is_array( $plugins ) ) {
- $plugins = array();
- }
- $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : '';
- $url = plugins_url( '/assets/js/fb_shortcode_replace.js', __FILE__ );
- $plugins = array_merge( $plugins, array( 'fb_shortcode_replace' => $url ) );
- return $plugins;
- }
- tinymce.PluginManager.add( 'fb_shortcode_replace', function( editor ) {
- var shortcode = /[.+]/g;
- var additional = 'FB-TEST';
- function ifShortcode( content ) {
- return content.search( /[.+]/ ) !== -1;
- }
- function replaceShortcodes( content ) {
- return content.replace( shortcode, function( match ) {
- return html( match );
- } );
- }
- function restoreShortcodes( content ) {
- return content.replace( additional, '' );
- }
- function html( data ) {
- console.log( data );
- return additional + data + additional;
- }
- editor.on( 'BeforeSetContent', function( event ) {
- // No shortcodes in content, return.
- if ( ! ifShortcode( event.content ) ) {
- return;
- }
- event.content = replaceShortcodes( event.content );
- } );
- editor.on( 'PostProcess', function( event ) {
- if ( event.get ) {
- event.content = restoreShortcodes( event.content );
- }
- } );
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement