<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
* Plugin URI: https://automattic.com/
* Plugin Name: Easy Markdown
* Description: Write in Markdown, publish in WordPress
* Author URI: https://automattic.com/
* @package automattic/jetpack
* Copyright (c) Automattic. All rights reserved.
* Released under the GPL license
* https://www.opensource.org/licenses/gpl-license.php
* This is an add-on for WordPress
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
if ( ! defined( 'ABSPATH' ) ) {
const POST_OPTION = 'wpcom_publish_posts_with_markdown';
const COMMENT_OPTION = 'wpcom_publish_comments_with_markdown';
const POST_TYPE_SUPPORT = 'wpcom-markdown';
const IS_MD_META = '_wpcom_is_markdown';
* @var WPCom_GHF_Markdown_Parser
* An instance of the markdown class.
private static $instance;
* To ensure that our munged posts over xml-rpc are removed from the cache.
public $posts_to_uncache = array();
* Posts and parents to monitor.
private $monitoring = array(
* Whether or not kses filters were removed. Only set if removal was attempted.
* @return object WPCom_Markdown instance
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
* Kicks things off on `init` action
$this->add_default_post_type_support();
$this->maybe_load_actions_and_filters();
if ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) {
// phpcs:ignore WPCUT.SwitchBlog.SwitchBlog -- wpcom flags **every** use of switch_blog, apparently expecting valid instances to ignore or suppress the sniff.
add_action( 'switch_blog', array( $this, 'maybe_load_actions_and_filters' ), 10, 2 );
add_action( 'admin_init', array( $this, 'register_setting' ) );
add_action( 'admin_init', array( $this, 'maybe_unload_for_bulk_edit' ) );
if ( current_theme_supports( 'o2' ) || class_exists( 'P2' ) ) {
* If we're in a bulk edit session, unload so that we don't lose our markdown metadata
public function maybe_unload_for_bulk_edit() {
if ( isset( $_REQUEST['bulk_edit'] ) && $this->is_posting_enabled() ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$this->unload_markdown_for_posts();
* Called on init and fires on switch_blog to decide if our actions and filters
* @param int|null $new_blog_id New blog ID.
* @param int|null $old_blog_id Old blog ID.
public function maybe_load_actions_and_filters( $new_blog_id = null, $old_blog_id = null ) {
// When WP sites are being installed, the options table is not available yet.
if ( function_exists( 'wp_installing' ) && wp_installing() ) {
// If this is a switch_to_blog call, and the blog isn't changing, we'll already be loaded.
if ( $new_blog_id && $new_blog_id === $old_blog_id ) {
if ( $this->is_posting_enabled() ) {
$this->load_markdown_for_posts();
$this->unload_markdown_for_posts();
if ( $this->is_commenting_enabled() ) {
$this->load_markdown_for_comments();
$this->unload_markdown_for_comments();
* Set up hooks for enabling Markdown conversion on posts
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 );
add_filter( 'edit_post_content_filtered', array( $this, 'edit_post_content_filtered' ), 10, 2 );
add_action( 'wp_restore_post_revision', array( $this, 'wp_restore_post_revision' ), 10, 2 );
add_filter( '_wp_post_revision_fields', array( $this, 'wp_post_revision_fields' ) );
add_action( 'xmlrpc_call', array( $this, 'xmlrpc_actions' ) );
add_filter( 'content_save_pre', array( $this, 'preserve_code_blocks' ), 1 );
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
$this->check_for_early_methods();
* Removes hooks to disable Markdown conversion on posts
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 );
remove_filter( 'edit_post_content', array( $this, 'edit_post_content' ), 10 );
remove_filter( 'edit_post_content_filtered', array( $this, 'edit_post_content_filtered' ), 10 );
remove_action( 'wp_restore_post_revision', array( $this, 'wp_restore_post_revision' ), 10 );
remove_filter( '_wp_post_revision_fields', array( $this, 'wp_post_revision_fields' ) );
remove_action( 'xmlrpc_call', array( $this, 'xmlrpc_actions' ) );
remove_filter( 'content_save_pre', array( $this, 'preserve_code_blocks' ), 1 );
* Set up hooks for enabling Markdown conversion on comments
protected function load_markdown_for_comments() {
// Use priority 9 so that Markdown runs before KSES, which can clean up
add_filter( 'pre_comment_content', array( $this, 'pre_comment_content' ), 9 );
* Removes hooks to disable Markdown conversion
protected function unload_markdown_for_comments() {
remove_filter( 'pre_comment_content', array( $this, 'pre_comment_content' ), 9 );
* The o2 plugin does some of what we do. Let's take precedence.
public function add_o2_helpers() {
if ( $this->is_posting_enabled() ) {
add_filter( 'content_save_pre', array( $this, 'o2_escape_lists' ), 1 );
add_filter( 'o2_preview_post', array( $this, 'o2_preview_post' ) );
add_filter( 'o2_preview_comment', array( $this, 'o2_preview_comment' ) );
add_filter( 'wpcom_markdown_transform_pre', array( $this, 'o2_unescape_lists' ) );
add_filter( 'wpcom_untransformed_content', array( $this, 'o2_unescape_lists' ) );
* If Markdown is enabled for posts on this blog, filter the text for o2 previews
* @param string $text Post text.
* @return string Post text transformed through the magic of Markdown
public function o2_preview_post( $text ) {
if ( $this->is_posting_enabled() ) {
$text = $this->transform( $text, array( 'unslash' => false ) );
* If Markdown is enabled for comments on this blog, filter the text for o2 previews
* @param string $text Comment text.
* @return string Comment text transformed through the magic of Markdown
public function o2_preview_comment( $text ) {
if ( $this->is_commenting_enabled() ) {
$text = $this->transform( $text, array( 'unslash' => false ) );
* Escapes lists so that o2 doesn't trounce them
* @param string $text Post/comment text.
* @return string Text escaped with HTML entity for asterisk.
public function o2_escape_lists( $text ) {
return preg_replace( '/^\\* /um', '* ', $text );
* Unescapes the token we inserted on o2_escape_lists
* @param string $text Post/comment text with HTML entities for asterisks.
* @return string Text with the HTML entity removed
public function o2_unescape_lists( $text ) {
return preg_replace( '/^[&]\#042; /um', '* ', $text );
* Preserve code blocks from being munged by KSES before they have a chance
* @param string $text post content.
* @return string post content with code blocks escaped.
public function preserve_code_blocks( $text ) {
return $this->get_parser()->codeblock_preserve( $text );
* Remove KSES if it's there. Store the result to manually invoke later if needed.
public function maybe_remove_kses() {
// Filters return true if they existed before you removed them.
if ( $this->is_posting_enabled() ) {
$this->kses = remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ) && remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
* Add our Writing and Discussion settings.
public function register_setting() {
add_settings_field( self::POST_OPTION, __( 'Markdown', 'jetpack' ), array( $this, 'post_field' ), 'writing' );
register_setting( 'writing', self::POST_OPTION, array( $this, 'sanitize_setting' ) );
add_settings_field( self::COMMENT_OPTION, __( 'Markdown', 'jetpack' ), array( $this, 'comment_field' ), 'discussion' );
register_setting( 'discussion', self::COMMENT_OPTION, array( $this, 'sanitize_setting' ) );
* Sanitize setting. Don't really want to store "on" value, so we'll store "1" instead!
* @param string $input Value received by settings API via $_POST.
* @return bool Cast to boolean.
public function sanitize_setting( $input ) {
* Prints HTML for the Writing setting
public function post_field() {
'<label><input name="%1$s" id="%1$s" type="checkbox"%2$s /> %3$s</label><p class="description">%4$s</p>',
esc_attr( self::POST_OPTION ),
checked( $this->is_posting_enabled(), true, false ),
esc_html__( 'Use Markdown for posts and pages.', 'jetpack' ),
sprintf( '<a href="%s" data-target="wpcom-help-center">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) )
* Prints HTML for the Discussion setting
public function comment_field() {
'<label><input name="%1$s" id="%1$s" type="checkbox"%2$s /> %3$s</label><p class="description">%4$s</p>',
esc_attr( self::COMMENT_OPTION ),
checked( $this->is_commenting_enabled(), true, false ),
esc_html__( 'Use Markdown for comments.', 'jetpack' ),
sprintf( '<a href="%s" data-target="wpcom-help-center">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) )
* Get the support url for Markdown
* @return string support url
protected function get_support_url() {
* Filter the Markdown support URL.
* @param string $url Markdown support URL.
return apply_filters( 'easy_markdown_support_url', 'https://en.support.wordpress.com/markdown-quick-reference/' );
* Is Mardown conversion for posts enabled?
public function is_posting_enabled() {
return (bool) Jetpack_Options::get_option_and_ensure_autoload( self::POST_OPTION, '' );
* Is Markdown conversion for comments enabled?
public function is_commenting_enabled() {
return (bool) Jetpack_Options::get_option_and_ensure_autoload( self::COMMENT_OPTION, '' );
* Check if a $post_id has Markdown enabled
* @param int $post_id A post ID.
public function is_markdown( $post_id ) {
return get_metadata( 'post', $post_id, self::IS_MD_META, true );
* Set Markdown as enabled on a post_id. We skip over update_postmeta so we
* can sneakily set metadata on post revisions, which we need.
* @param int $post_id A post ID.
* @return bool The metadata was successfully set.
protected function set_as_markdown( $post_id ) {
return update_metadata( 'post', $post_id, self::IS_MD_META, true );
* Get our Markdown parser object, optionally requiring all of our needed classes and
* instantiating our parser.
* @return object WPCom_GHF_Markdown_Parser instance.
public function get_parser() {
require_once JETPACK__PLUGIN_DIR . '/_inc/lib/markdown.php';
self::$parser = new WPCom_GHF_Markdown_Parser();
* We don't want Markdown conversion all over the place.
public function add_default_post_type_support() {
add_post_type_support( 'post', self::POST_TYPE_SUPPORT );
add_post_type_support( 'page', self::POST_TYPE_SUPPORT );
add_post_type_support( 'revision', self::POST_TYPE_SUPPORT );
* Figure out the post type of the post screen we're on
* @return string Current post_type
protected function get_post_screen_post_type() {
_deprecated_function( __METHOD__, 'jetpack-10.8', '' );
$post_type = filter_input( INPUT_GET, 'post_type', FILTER_UNSAFE_RAW );
$post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
if ( 'post-new.php' === $pagenow ) {
return ! empty( $post_type ) ? $post_type : 'post';
$post_type = get_post_type( $post_id );
return ! empty( $post_type ) ? $post_type : 'post';
* Swap post_content and post_content_filtered for editing
* @param string $content Post content.
* @param int $id post ID.
* @return string Swapped content
public function edit_post_content( $content, $id ) {
if ( $this->is_markdown( $id ) ) {
if ( $post && ! empty( $post->post_content_filtered ) ) {
$post = $this->swap_for_editing( $post );
return $post->post_content;
* Swap post_content_filtered and post_content for editing
* @param string $content Post content_filtered.
* @param int $id post ID.
* @return string Swapped content
public function edit_post_content_filtered( $content, $id ) {
// if markdown was disabled, let's turn this off.
if ( ! $this->is_posting_enabled() && $this->is_markdown( $id ) ) {
if ( $post && ! empty( $post->post_content_filtered ) ) {
* 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 ) {
$re = '/' . $this->get_parser()->contain_span_tags_re . '/';
foreach ( $tags as $tag => $attributes ) {
// In case other filters have changed the value to a non-array, we skip it.
if ( ! is_array( $attributes ) ) {