diff options
Diffstat (limited to 'plugins/jetpack/modules/markdown/easy-markdown.php')
-rw-r--r-- | plugins/jetpack/modules/markdown/easy-markdown.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/plugins/jetpack/modules/markdown/easy-markdown.php b/plugins/jetpack/modules/markdown/easy-markdown.php index 824be5e9..65e4685e 100644 --- a/plugins/jetpack/modules/markdown/easy-markdown.php +++ b/plugins/jetpack/modules/markdown/easy-markdown.php @@ -115,6 +115,8 @@ class WPCom_Markdown { * @return null */ public function load_markdown_for_posts() { + add_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ), 10, 2 ); + add_action( 'after_wp_tiny_mce', array( $this, 'after_wp_tiny_mce' ) ); add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ) ); add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); add_filter( 'edit_post_content', array( $this, 'edit_post_content' ), 10, 2 ); @@ -133,6 +135,8 @@ class WPCom_Markdown { * @return null */ public function unload_markdown_for_posts() { + remove_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ) ); + remove_action( 'after_wp_tiny_mce', array( $this, 'after_wp_tiny_mce' ) ); remove_action( 'wp_insert_post', array( $this, 'wp_insert_post' ) ); remove_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); remove_filter( 'edit_post_content', array( $this, 'edit_post_content' ), 10, 2 ); @@ -416,6 +420,52 @@ class WPCom_Markdown { } /** + * Some tags are allowed to have a 'markdown' attribute, allowing them to contain Markdown. + * We need to tell KSES about those tags. + * @param array $tags List of tags that KSES allows. + * @param string $context The context that KSES is allowing these tags. + * @return array The tags that KSES allows, with our extra 'markdown' parameter where necessary. + */ + public function wp_kses_allowed_html( $tags, $context ) { + if ( 'post' !== $context ) { + return $tags; + } + + $re = '/' . $this->get_parser()->contain_span_tags_re . '/'; + foreach ( $tags as $tag => $attributes ) { + if ( preg_match( $re, $tag ) ) { + $attributes['markdown'] = true; + $tags[ $tag ] = $attributes; + } + } + + return $tags; + } + + /** + * TinyMCE needs to know not to strip the 'markdown' attribute. Unfortunately, it doesn't + * really offer a nice API for whitelisting attributes, so we have to manually add it + * to the schema instead. + */ + public function after_wp_tiny_mce() { +?> +<script type="text/javascript"> +jQuery( function() { + tinymce.on( 'AddEditor', function( event ) { + event.editor.on( 'BeforeSetContent', function( event ) { + var editor = event.target; + Object.keys( editor.schema.elements ).forEach( function( key, index ) { + editor.schema.elements[ key ].attributes['markdown'] = {}; + editor.schema.elements[ key ].attributesOrder.push( 'markdown' ); + } ); + } ); + }, true ); +} ); +</script> +<?php + } + + /** * Magic happens here. Markdown is converted and stored on post_content. Original Markdown is stored * in post_content_filtered so that we can continue editing as Markdown. * @param array $post_data The post data that will be inserted into the DB. Slashed. |