<?php
class WPCom_Markdown {
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';
private static $parser;
private static $instance;
public $posts_to_uncache = array();
private $monitoring = array( 'post' => array(), 'parent' => array() );
public static function get_instance() {
if ( ! self::$instance )
self::$instance = new self();
return self::$instance;
}
public function load() {
$this->add_default_post_type_support();
$this->maybe_load_actions_and_filters();
if ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) {
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' ) ) {
$this->add_o2_helpers();
}
}
public function maybe_unload_for_bulk_edit() {
if ( isset( $_REQUEST['bulk_edit'] ) && $this->is_posting_enabled() ) {
$this->unload_markdown_for_posts();
}
}
public function maybe_load_actions_and_filters( $new_blog_id = null, $old_blog_id = null ) {
if ( $new_blog_id && $new_blog_id === $old_blog_id ) {
return;
}
if ( $this->is_posting_enabled() ) {
$this->load_markdown_for_posts();
} else {
$this->unload_markdown_for_posts();
}
if ( $this->is_commenting_enabled() ) {
$this->load_markdown_for_comments();
} else {
$this->unload_markdown_for_comments();
}
}
public function load_markdown_for_posts() {
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();
}
}
public function unload_markdown_for_posts() {
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 );
remove_filter( 'edit_post_content_filtered', array( $this, 'edit_post_content_filtered' ), 10, 2 );
remove_action( 'wp_restore_post_revision', array( $this, 'wp_restore_post_revision' ), 10, 2 );
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 );
}
protected function load_markdown_for_comments() {
add_filter( 'pre_comment_content', array( $this, 'pre_comment_content' ), 9 );
}
protected function unload_markdown_for_comments() {
remove_filter( 'pre_comment_content', array( $this, 'pre_comment_content' ), 9 );
}
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' ) );
}
public function o2_preview_post( $text ) {
if ( $this->is_posting_enabled() ) {
$text = $this->transform( $text, array( 'unslash' => false ) );
}
return $text;
}
public function o2_preview_comment( $text ) {
if ( $this->is_commenting_enabled() ) {
$text = $this->transform( $text, array( 'unslash' => false ) );
}
return $text;
}
public function o2_escape_lists( $text ) {
return preg_replace( '/^\\* /um', '* ', $text );
}
public function o2_unescape_lists( $text ) {
return preg_replace( '/^[&]\#042; /um', '* ', $text );
}
public function preserve_code_blocks( $text ) {
return $this->get_parser()->codeblock_preserve( $text );
}
public function maybe_remove_kses() {
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' );
}
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') );
}
public function sanitize_setting( $input ) {
return (bool) $input;
}
public function post_field() {
printf(
'<label><input name="%s" id="%s" type="checkbox"%s /> %s</label><p class="description">%s</p>',
self::POST_OPTION,
self::POST_OPTION,
checked( $this->is_posting_enabled(), true, false ),
esc_html__( 'Use Markdown for posts and pages.', 'jetpack' ),
sprintf( '<a href="%s">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) )
);
}
public function comment_field() {
printf(
'<label><input name="%s" id="%s" type="checkbox"%s /> %s</label><p class="description">%s</p>',
self::COMMENT_OPTION,
self::COMMENT_OPTION,
checked( $this->is_commenting_enabled(), true, false ),
esc_html__( 'Use Markdown for comments.', 'jetpack' ),
sprintf( '<a href="%s">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) )
);
}
protected function get_support_url() {
return apply_filters( 'easy_markdown_support_url', 'http:
}
public function is_posting_enabled() {
return (bool) get_option( self::POST_OPTION, '' );
}
public function is_commenting_enabled() {
return (bool) get_option( self::COMMENT_OPTION, '' );
}
public function is_markdown( $post_id ) {
return get_metadata( 'post', $post_id, self::IS_MD_META, true );
}
protected function set_as_markdown( $post_id ) {
return update_metadata( 'post', $post_id, self::IS_MD_META, true );
}
public function get_parser() {
if ( ! self::$parser ) {
jetpack_require_lib( 'markdown' );
self::$parser = new WPCom_GHF_Markdown_Parser;
}
return self::$parser;
}
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 );
}
protected function get_post_screen_post_type() {
global $pagenow;
if ( 'post-new.php' === $pagenow )
return ( isset( $_GET['post_type'] ) ) ? $_GET['post_type'] : 'post';
if ( isset( $_GET['post'] ) ) {
$post = get_post( (int) $_GET['post'] );
if ( is_object( $post ) && isset( $post->post_type ) )
return $post->post_type;
}
return 'post';
}
public function edit_post_content( $content, $id ) {
if ( $this->is_markdown( $id ) ) {
$post = get_post( $id );
if ( $post && ! empty( $post->post_content_filtered ) ) {
$post = $this->swap_for_editing( $post );
return $post->post_content;
}
}
return $content;
}
public function edit_post_content_filtered( $content, $id ) {
if ( ! $this->is_posting_enabled() && $this->is_markdown( $id ) ) {
$post = get_post( $id );
if ( $post && ! empty( $post->post_content_filtered ) )
$content = '';
}
return $content;
}
public function wp_insert_post_data( $post_data, $postarr ) {
$post_id = isset( $postarr['ID'] ) ? $postarr['ID'] : false;
if ( ! $this->is_posting_enabled() || ! post_type_supports( $post_data['post_type'], self::POST_TYPE_SUPPORT ) ) {
if ( $this->is_markdown( $post_id ) && ! empty( $post_data['post_content_filtered'] ) ) {
$post_data['post_content_filtered'] = '';
}
$post_data['post_content'] = $this->get_parser()->codeblock_restore( $post_data['post_content'] );
return $post_data;
}
if ( 'revision' !== $post_data['post_type'] ) {
$post_data['post_content_filtered'] = apply_filters( 'wpcom_untransformed_content', $post_data['post_content'] );
$post_data['post_content'] = $this->transform( $post_data['post_content'], array( 'id' => $post_id ) );
$post_data['post_content'] = apply_filters( 'content_save_pre', $post_data['post_content'] );
} elseif ( 0 === strpos( $post_data['post_name'], $post_data['post_parent'] . '-autosave' ) ) {
$post_data['post_content'] = $this->transform( $post_data['post_content'], array( 'id' => $post_data['post_parent'] ) );
$post_data['post_content'] = apply_filters( 'content_save_pre', $post_data['post_content'] );
}
if ( $post_id )
$this->monitoring['post'][ $post_id ] = true;
else
$this->monitoring['content'] = wp_unslash( $post_data['post_content'] );
if ( 'revision' === $postarr['post_type'] && $this->is_markdown( $postarr['post_parent'] ) )
$this->monitoring['parent'][ $postarr['post_parent'] ] = true;
return $post_data;
}
public function wp_insert_post( $post_id ) {
$post_parent = get_post_field( 'post_parent', $post_id );
if ( isset( $this->monitoring['content'] ) && $this->monitoring['content'] === get_post_field( 'post_content', $post_id ) ) {
unset( $this->monitoring['content'] );
$this->set_as_markdown( $post_id );
}
if ( isset( $this->monitoring['post'][$post_id] ) ) {
unset( $this->monitoring['post'][$post_id] );
$this->set_as_markdown( $post_id );
} elseif ( isset( $this->monitoring['parent'][$post_parent] ) ) {
unset( $this->monitoring['parent'][$post_parent] );
$this->set_as_markdown( $post_id );
}
}
public function pre_comment_content( $content ) {
return $this->transform( $content, array(
'id' => $this->comment_hash( $content ),
) );
}
protected function comment_hash( $content ) {
return 'c-' . substr( md5( $content ), 0, 8 );
}
public function transform( $text, $args = array() ) {
$args = wp_parse_args( $args, array(
'id' => false,
'unslash' => true,
'decode_code_blocks' => ! $this->get_parser()->use_code_shortcode
) );
if ( $args['unslash'] )
$text = wp_unslash( $text );
$text = apply_filters( 'wpcom_markdown_transform_pre', $text, $args );
$text = str_replace( array( '</p><p>', "</p>\n<p>" ), "</p>\n\n<p>", $text );
$text = $this->get_parser()->unp( $text );
$text = preg_replace( '/^>/m', '>', $text );
$this->get_parser()->fn_id_prefix = $args['id'] ? $args['id'] . '-' : '';
if ( $args['decode_code_blocks'] ) {
$text = $this->get_parser()->codeblock_restore( $text );
}
$text = $this->get_parser()->transform( $text );
$text = preg_replace( '/((id|href)="#?fn(ref)?):/', "$1-", $text );
$text = rtrim( $text );
$text = apply_filters( 'wpcom_markdown_transform_post', $text, $args );
if ( $args['unslash'] )
$text = wp_slash( $text );
return $text;
}
public function _wp_post_revision_fields( $fields ) {
$fields['post_content_filtered'] = __( 'Markdown content', 'jetpack' );
return $fields;
}
public function wp_restore_post_revision( $post_id, $revision_id ) {
if ( $this->is_markdown( $revision_id ) ) {
$revision = get_post( $revision_id, ARRAY_A );
$post = get_post( $post_id, ARRAY_A );
$post['post_content'] = $revision['post_content_filtered'];
$this->monitoring['restore'] = true;
add_filter( 'wp_revisions_to_keep', '__return_false', 99 );
wp_update_post( $post );
$this->fix_latest_revision_on_restore( $post_id );
remove_filter( 'wp_revisions_to_keep', '__return_false', 99 );
}
}
protected function fix_latest_revision_on_restore( $post_id ) {
global $wpdb;
$post = get_post( $post_id );
$last_revision = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = 'revision' AND post_parent = %d ORDER BY ID DESC", $post->ID ) );
$last_revision->post_content_filtered = $post->post_content_filtered;
wp_insert_post( (array) $last_revision );
}
public function xmlrpc_actions( $xmlrpc_method ) {
switch ( $xmlrpc_method ) {
case 'metaWeblog.getRecentPosts':
case 'wp.getPosts':
case 'wp.getPages':
add_action( 'parse_query', array( $this, 'make_filterable' ), 10, 1 );
break;
case 'wp.getPost':
$this->prime_post_cache();
break;
}
}
protected function check_for_early_methods() {
global $HTTP_RAW_POST_DATA;
if ( false === strpos( $HTTP_RAW_POST_DATA, 'metaWeblog.getPost' )
&& false === strpos( $HTTP_RAW_POST_DATA, 'wp.getPage' ) ) {
return;
}
include_once( ABSPATH . WPINC . '/class-IXR.php' );
$message = new IXR_Message( $HTTP_RAW_POST_DATA );
$message->parse();
$post_id_position = 'metaWeblog.getPost' === $message->methodName ? 0 : 1;
$this->prime_post_cache( $message->params[ $post_id_position ] );
}
private function prime_post_cache( $post_id = false ) {
global $wp_xmlrpc_server;
if ( ! $post_id ) {
$post_id = $wp_xmlrpc_server->message->params[3];
}
if ( $this->is_markdown( $post_id ) ) {
$post = get_post( $post_id );
if ( ! empty( $post->post_content_filtered ) ) {
wp_cache_delete( $post->ID, 'posts' );
$post = $this->swap_for_editing( $post );
wp_cache_add( $post->ID, $post, 'posts' );
$this->posts_to_uncache[] = $post_id;
}
}
if ( wp_using_ext_object_cache() ) {
add_action( 'shutdown', array( $this, 'uncache_munged_posts' ) );
}
}
protected function swap_for_editing( $post ) {
$markdown = $post->post_content_filtered;
$markdown = $this->get_parser()->codeblock_restore( $markdown );
$markdown = preg_replace( '/^> /m', '> ', $markdown );
$post->post_content_filtered = $post->post_content;
$post->post_content = $markdown;
return $post;
}
public function uncache_munged_posts() {
foreach( WPCom_Markdown::get_instance()->posts_to_uncache as $post_id ) {
wp_cache_delete( $post_id, 'posts' );
}
}
public function make_filterable( $wp_query ) {
$wp_query->set( 'suppress_filters', false );
add_action( 'the_posts', array( $this, 'the_posts' ), 10, 2 );
}
public function the_posts( $posts, $wp_query ) {
foreach ( $posts as $key => $post ) {
if ( $this->is_markdown( $post->ID ) && ! empty( $posts[ $key ]->post_content_filtered ) ) {
$markdown = $posts[ $key ]->post_content_filtered;
$posts[ $key ]->post_content_filtered = $posts[ $key ]->post_content;
$posts[ $key ]->post_content = $markdown;
}
}
return $posts;
}
private function __construct() {}
}
add_action( 'init', array( WPCom_Markdown::get_instance(), 'load' ) );