diff options
Diffstat (limited to 'plugins/jetpack/modules/markdown/easy-markdown.php')
-rw-r--r-- | plugins/jetpack/modules/markdown/easy-markdown.php | 357 |
1 files changed, 208 insertions, 149 deletions
diff --git a/plugins/jetpack/modules/markdown/easy-markdown.php b/plugins/jetpack/modules/markdown/easy-markdown.php index 43e30de4..0abcddf3 100644 --- a/plugins/jetpack/modules/markdown/easy-markdown.php +++ b/plugins/jetpack/modules/markdown/easy-markdown.php @@ -1,13 +1,15 @@ -<?php - -/* -Plugin Name: Easy Markdown -Plugin URI: https://automattic.com/ -Description: Write in Markdown, publish in WordPress -Version: 0.1 -Author: Matt Wiebe -Author URI: https://automattic.com/ -*/ +<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName +/** + * Plugin URI: https://automattic.com/ + * Plugin Name: Easy Markdown + * Description: Write in Markdown, publish in WordPress + * Version: 0.1 + * Author: Matt Wiebe + * Author URI: https://automattic.com/ + * Text Domain: jetpack + * + * @package automattic/jetpack + */ /** * Copyright (c) Automattic. All rights reserved. @@ -31,35 +33,61 @@ Author URI: https://automattic.com/ * ********************************************************************** */ +/** + * WPCom_Markdown class. + */ class WPCom_Markdown { - - const POST_OPTION = 'wpcom_publish_posts_with_markdown'; - const COMMENT_OPTION = 'wpcom_publish_comments_with_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'; + const IS_MD_META = '_wpcom_is_markdown'; + /** + * Our markdown parser. + * + * @var WPCom_GHF_Markdown_Parser + */ private static $parser; + + /** + * An instance of the markdown class. + * + * @var WPCom_Markdown + */ private static $instance; - // to ensure that our munged posts over xml-rpc are removed from the cache + /** + * To ensure that our munged posts over xml-rpc are removed from the cache. + * + * @var array + */ public $posts_to_uncache = array(); - private $monitoring = array( 'post' => array(), 'parent' => array() ); + /** + * Posts and parents to monitor. + * + * @var array + */ + private $monitoring = array( + 'post' => array(), + 'parent' => array(), + ); /** * Yay singletons! + * * @return object WPCom_Markdown instance */ public static function get_instance() { - if ( ! self::$instance ) + if ( ! self::$instance ) { self::$instance = new self(); + } return self::$instance; } /** * Kicks things off on `init` action - * @return null */ public function load() { $this->add_default_post_type_support(); @@ -76,10 +104,9 @@ class WPCom_Markdown { /** * If we're in a bulk edit session, unload so that we don't lose our markdown metadata - * @return null */ public function maybe_unload_for_bulk_edit() { - if ( isset( $_REQUEST['bulk_edit'] ) && $this->is_posting_enabled() ) { + if ( isset( $_REQUEST['bulk_edit'] ) && $this->is_posting_enabled() ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $this->unload_markdown_for_posts(); } } @@ -87,8 +114,9 @@ class WPCom_Markdown { /** * Called on init and fires on switch_blog to decide if our actions and filters * should be running. - * @param int|null $new_blog_id New blog ID - * @param int|null $old_blog_id Old blog ID + * + * @param int|null $new_blog_id New blog ID. + * @param int|null $old_blog_id Old blog ID. * @return null */ public function maybe_load_actions_and_filters( $new_blog_id = null, $old_blog_id = null ) { @@ -98,7 +126,7 @@ class WPCom_Markdown { return; } - // If this is a switch_to_blog call, and the blog isn't changing, we'll already be loaded + // 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 ) { return; } @@ -118,7 +146,6 @@ class WPCom_Markdown { /** * Set up hooks for enabling Markdown conversion on posts - * @return null */ public function load_markdown_for_posts() { add_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ), 10, 2 ); @@ -128,7 +155,7 @@ class WPCom_Markdown { 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_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 ) { @@ -138,7 +165,6 @@ class WPCom_Markdown { /** * Removes hooks to disable Markdown conversion on posts - * @return null */ public function unload_markdown_for_posts() { remove_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ) ); @@ -148,14 +174,13 @@ class WPCom_Markdown { 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_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 - * @return null */ protected function load_markdown_for_comments() { // Use priority 9 so that Markdown runs before KSES, which can clean up @@ -165,15 +190,13 @@ class WPCom_Markdown { /** * Removes hooks to disable Markdown conversion - * @return null */ protected function unload_markdown_for_comments() { remove_filter( 'pre_comment_content', array( $this, 'pre_comment_content' ), 9 ); } /** - * o2 does some of what we do. Let's take precedence. - * @return null + * The o2 plugin does some of what we do. Let's take precedence. */ public function add_o2_helpers() { if ( $this->is_posting_enabled() ) { @@ -189,7 +212,8 @@ class WPCom_Markdown { /** * If Markdown is enabled for posts on this blog, filter the text for o2 previews - * @param string $text Post text + * + * @param string $text Post text. * @return string Post text transformed through the magic of Markdown */ public function o2_preview_post( $text ) { @@ -201,7 +225,8 @@ class WPCom_Markdown { /** * If Markdown is enabled for comments on this blog, filter the text for o2 previews - * @param string $text Comment text + * + * @param string $text Comment text. * @return string Comment text transformed through the magic of Markdown */ public function o2_preview_comment( $text ) { @@ -213,8 +238,9 @@ class WPCom_Markdown { /** * Escapes lists so that o2 doesn't trounce them - * @param string $text Post/comment text - * @return string Text escaped with HTML entity for asterisk + * + * @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 ); @@ -222,7 +248,8 @@ class WPCom_Markdown { /** * Unescapes the token we inserted on o2_escape_lists - * @param string $text Post/comment text with HTML entities for asterisks + * + * @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 ) { @@ -231,8 +258,9 @@ class WPCom_Markdown { /** * 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 + * + * @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 ); @@ -240,28 +268,28 @@ class WPCom_Markdown { /** * Remove KSES if it's there. Store the result to manually invoke later if needed. - * @return null */ public function maybe_remove_kses() { - // Filters return true if they existed before you removed them - if ( $this->is_posting_enabled() ) + // 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. - * @return null */ 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') ); + 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') ); + 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 + * + * @param string $input Value received by settings API via $_POST. * @return bool Cast to boolean. */ public function sanitize_setting( $input ) { @@ -270,13 +298,11 @@ class WPCom_Markdown { /** * Prints HTML for the Writing setting - * @return null */ 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, + '<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">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) ) @@ -285,13 +311,11 @@ class WPCom_Markdown { /** * Prints HTML for the Discussion setting - * @return null */ 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, + '<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">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) ) @@ -300,6 +324,7 @@ class WPCom_Markdown { /** * Get the support url for Markdown + * * @uses apply_filters * @return string support url */ @@ -318,6 +343,7 @@ class WPCom_Markdown { /** * Is Mardown conversion for posts enabled? + * * @return boolean */ public function is_posting_enabled() { @@ -326,6 +352,7 @@ class WPCom_Markdown { /** * Is Markdown conversion for comments enabled? + * * @return boolean */ public function is_commenting_enabled() { @@ -334,7 +361,8 @@ class WPCom_Markdown { /** * Check if a $post_id has Markdown enabled - * @param int $post_id A post ID. + * + * @param int $post_id A post ID. * @return boolean */ public function is_markdown( $post_id ) { @@ -344,7 +372,8 @@ class WPCom_Markdown { /** * 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. + * + * @param int $post_id A post ID. * @return bool The metadata was successfully set. */ protected function set_as_markdown( $post_id ) { @@ -354,13 +383,14 @@ class WPCom_Markdown { /** * 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() { if ( ! self::$parser ) { jetpack_require_lib( 'markdown' ); - self::$parser = new WPCom_GHF_Markdown_Parser; + self::$parser = new WPCom_GHF_Markdown_Parser(); } return self::$parser; @@ -368,7 +398,6 @@ class WPCom_Markdown { /** * We don't want Markdown conversion all over the place. - * @return null */ public function add_default_post_type_support() { add_post_type_support( 'post', self::POST_TYPE_SUPPORT ); @@ -378,24 +407,33 @@ class WPCom_Markdown { /** * Figure out the post type of the post screen we're on + * + * @deprecated since 10.8 * @return string Current post_type */ protected function get_post_screen_post_type() { + _deprecated_function( __METHOD__, 'jetpack-10.8', '' ); + 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; + $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'; + } + + if ( $post_id ) { + $post_type = get_post_type( $post_id ); } - return 'post'; + + 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 + * + * @param string $content Post content. + * @param int $id post ID. * @return string Swapped content */ public function edit_post_content( $content, $id ) { @@ -411,16 +449,18 @@ class WPCom_Markdown { /** * Swap post_content_filtered and post_content for editing - * @param string $content Post content_filtered - * @param int $id post ID + * + * @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 markdown was disabled, let's turn this off. if ( ! $this->is_posting_enabled() && $this->is_markdown( $id ) ) { $post = get_post( $id ); - if ( $post && ! empty( $post->post_content_filtered ) ) + if ( $post && ! empty( $post->post_content_filtered ) ) { $content = ''; + } } return $content; } @@ -428,7 +468,8 @@ 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 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. */ @@ -441,7 +482,7 @@ class WPCom_Markdown { foreach ( $tags as $tag => $attributes ) { if ( preg_match( $re, $tag ) ) { $attributes['markdown'] = true; - $tags[ $tag ] = $attributes; + $tags[ $tag ] = $attributes; } } @@ -454,7 +495,7 @@ class WPCom_Markdown { * to the schema instead. */ public function after_wp_tiny_mce() { -?> + ?> <script type="text/javascript"> jQuery( function() { ( 'undefined' !== typeof tinymce ) && tinymce.on( 'AddEditor', function( event ) { @@ -468,12 +509,13 @@ jQuery( function() { }, true ); } ); </script> -<?php + <?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. * @param array $postarr All the stuff that was in $_POST. * @return array $post_data with post_content and post_content_filtered modified @@ -494,8 +536,8 @@ jQuery( function() { } // rejigger post_content and post_content_filtered // revisions are already in the right place, except when we're restoring, but that's taken care of elsewhere - // also prevent quick edit feature from overriding already-saved markdown (issue https://github.com/Automattic/jetpack/issues/636) - if ( 'revision' !== $post_data['post_type'] && ! isset( $_POST['_inline_edit'] ) ) { + // also prevent quick edit feature from overriding already-saved markdown (issue https://github.com/Automattic/jetpack/issues/636). + if ( 'revision' !== $post_data['post_type'] && ! isset( $_POST['_inline_edit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing /** * Filter the original post content passed to Markdown. * @@ -506,25 +548,27 @@ jQuery( function() { * @param string $post_data['post_content'] Untransformed post content. */ $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'] = $this->transform( $post_data['post_content'], array( 'id' => $post_id ) ); /** This filter is already documented in core/wp-includes/default-filters.php */ $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' ) ) { - // autosaves for previews are weird + // autosaves for previews are weird. /** This filter is already documented in modules/markdown/easy-markdown.php */ $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_data['post_parent'] ) ); + $post_data['post_content'] = $this->transform( $post_data['post_content'], array( 'id' => $post_data['post_parent'] ) ); /** This filter is already documented in core/wp-includes/default-filters.php */ $post_data['post_content'] = apply_filters( 'content_save_pre', $post_data['post_content'] ); } - // set as markdown on the wp_insert_post hook later - if ( $post_id ) + // set as markdown on the wp_insert_post hook later. + if ( $post_id ) { $this->monitoring['post'][ $post_id ] = true; - else + } else { $this->monitoring['content'] = wp_unslash( $post_data['post_content'] ); - if ( 'revision' === $postarr['post_type'] && $this->is_markdown( $postarr['post_parent'] ) ) + } + if ( 'revision' === $postarr['post_type'] && $this->is_markdown( $postarr['post_parent'] ) ) { $this->monitoring['parent'][ $postarr['post_parent'] ] = true; + } return $post_data; } @@ -532,43 +576,53 @@ jQuery( function() { /** * Calls on wp_insert_post action, after wp_insert_post_data. This way we can * still set postmeta on our revisions after it's all been deleted. - * @param int $post_id The post ID that has just been added/updated - * @return null + * + * @param int $post_id The post ID that has just been added/updated. */ public function wp_insert_post( $post_id ) { $post_parent = get_post_field( 'post_parent', $post_id ); // this didn't have an ID yet. Compare the content that was just saved. - if ( isset( $this->monitoring['content'] ) && $this->monitoring['content'] === get_post_field( 'post_content', $post_id ) ) { + if ( isset( $this->monitoring['content'] ) && get_post_field( 'post_content', $post_id ) === $this->monitoring['content'] ) { unset( $this->monitoring['content'] ); $this->set_as_markdown( $post_id ); } - if ( isset( $this->monitoring['post'][$post_id] ) ) { - unset( $this->monitoring['post'][$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] ); + } elseif ( isset( $this->monitoring['parent'][ $post_parent ] ) ) { + unset( $this->monitoring['parent'][ $post_parent ] ); $this->set_as_markdown( $post_id ); } } /** * Run a comment through Markdown. Easy peasy. - * @param string $content + * + * @param string $content - the content. * @return string */ public function pre_comment_content( $content ) { - return $this->transform( $content, array( - 'id' => $this->comment_hash( $content ), - ) ); + return $this->transform( + $content, + array( + 'id' => $this->comment_hash( $content ), + ) + ); } + /** + * Return a comment hash. + * + * @param string $content - the content of the comment. + */ protected function comment_hash( $content ) { return 'c-' . substr( md5( $content ), 0, 8 ); } /** * Markdown conversion. Some DRYness for repetitive tasks. - * @param string $text Content to be run through Markdown + * + * @param string $text Content to be run through Markdown. * @param array $args Arguments, with keys: * id: provide a string to prefix footnotes with a unique identifier * unslash: when true, expects and returns slashed data @@ -583,14 +637,18 @@ jQuery( function() { return $text; } - $args = wp_parse_args( $args, array( - 'id' => false, - 'unslash' => true, - 'decode_code_blocks' => ! $this->get_parser()->use_code_shortcode - ) ); - // probably need to unslash - if ( $args['unslash'] ) + $args = wp_parse_args( + $args, + array( + 'id' => false, + 'unslash' => true, + 'decode_code_blocks' => ! $this->get_parser()->use_code_shortcode, + ) + ); + // probably need to unslash. + if ( $args['unslash'] ) { $text = wp_unslash( $text ); + } /** * Filter the content to be run through Markdown, before it's transformed by Markdown. @@ -603,13 +661,13 @@ jQuery( function() { * @param array $args Array of Markdown options. */ $text = apply_filters( 'wpcom_markdown_transform_pre', $text, $args ); - // ensure our paragraphs are separated + // ensure our paragraphs are separated. $text = str_replace( array( '</p><p>', "</p>\n<p>" ), "</p>\n\n<p>", $text ); // visual editor likes to add <p>s. Buh-bye. $text = $this->get_parser()->unp( $text ); - // sometimes we get an encoded > at start of line, breaking blockquotes + // sometimes we get an encoded > at start of line, breaking blockquotes. $text = preg_replace( '/^>/m', '>', $text ); - // prefixes are because we need to namespace footnotes by post_id + // prefixes are because we need to namespace footnotes by post_id. $this->get_parser()->fn_id_prefix = $args['id'] ? $args['id'] . '-' : ''; // If we're not using the code shortcode, prevent over-encoding. if ( $args['decode_code_blocks'] ) { @@ -617,8 +675,8 @@ jQuery( function() { } // Transform it! $text = $this->get_parser()->transform( $text ); - // Fix footnotes - kses doesn't like the : IDs it supplies - $text = preg_replace( '/((id|href)="#?fn(ref)?):/', "$1-", $text ); + // Fix footnotes - kses doesn't like the : IDs it supplies. + $text = preg_replace( '/((id|href)="#?fn(ref)?):/', '$1-', $text ); // Markdown inserts extra spaces to make itself work. Buh-bye. $text = rtrim( $text ); /** @@ -633,9 +691,10 @@ jQuery( function() { */ $text = apply_filters( 'wpcom_markdown_transform_post', $text, $args ); - // probably need to re-slash - if ( $args['unslash'] ) + // probably need to re-slash. + if ( $args['unslash'] ) { $text = wp_slash( $text ); + } return $text; } @@ -643,10 +702,10 @@ jQuery( function() { /** * Shows Markdown in the Revisions screen, and ensures that post_content_filtered * is maintained on revisions - * @param array $fields Post fields pertinent to revisions - * @return array Modified array to include post_content_filtered + * + * @param array $fields Post fields pertinent to revisions. */ - public function _wp_post_revision_fields( $fields ) { + public function wp_post_revision_fields( $fields ) { $fields['post_content_filtered'] = __( 'Markdown content', 'jetpack' ); return $fields; } @@ -654,18 +713,18 @@ jQuery( function() { /** * Do some song and dance to keep all post_content and post_content_filtered content * in the expected place when a post revision is restored. - * @param int $post_id The post ID have a restore done to it - * @param int $revision_id The revision ID being restored - * @return null + * + * @param int $post_id The post ID have a restore done to it. + * @param int $revision_id The revision ID being restored. */ 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']; // Yes, we put it in post_content, because our wp_insert_post_data() expects that - // set this flag so we can restore the post_content_filtered on the last revision later + $revision = get_post( $revision_id, ARRAY_A ); + $post = get_post( $post_id, ARRAY_A ); + $post['post_content'] = $revision['post_content_filtered']; // Yes, we put it in post_content, because our wp_insert_post_data() expects that. + // set this flag so we can restore the post_content_filtered on the last revision later. $this->monitoring['restore'] = true; - // let's not make a revision of our fixing update + // let's not make a revision of our fixing update. add_filter( 'wp_revisions_to_keep', '__return_false', 99 ); wp_update_post( $post ); $this->fix_latest_revision_on_restore( $post_id ); @@ -676,13 +735,13 @@ jQuery( function() { /** * We need to ensure the last revision has Markdown, not HTML in its post_content_filtered * column after a restore. + * * @param int $post_id The post ID that was just restored. - * @return null */ 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 ) ); + $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 ); } @@ -690,8 +749,8 @@ jQuery( function() { /** * Kicks off magic for an XML-RPC session. We want to keep editing Markdown * and publishing HTML. - * @param string $xmlrpc_method The current XML-RPC method - * @return null + * + * @param string $xmlrpc_method The current XML-RPC method. */ public function xmlrpc_actions( $xmlrpc_method ) { switch ( $xmlrpc_method ) { @@ -707,20 +766,21 @@ jQuery( function() { } /** - * metaWeblog.getPost and wp.getPage fire xmlrpc_call action *after* get_post() is called. + * Function metaWeblog.getPost and wp.getPage fire xmlrpc_call action *after* get_post() is called. * So, we have to detect those methods and prime the post cache early. + * * @return null */ protected function check_for_early_methods() { - $raw_post_data = file_get_contents( "php://input" ); + $raw_post_data = file_get_contents( 'php://input' ); if ( false === strpos( $raw_post_data, 'metaWeblog.getPost' ) && false === strpos( $raw_post_data, 'wp.getPage' ) ) { return; } - include_once( ABSPATH . WPINC . '/class-IXR.php' ); + include_once ABSPATH . WPINC . '/class-IXR.php'; $message = new IXR_Message( $raw_post_data ); $message->parse(); - $post_id_position = 'metaWeblog.getPost' === $message->methodName ? 0 : 1; + $post_id_position = 'metaWeblog.getPost' === $message->methodName ? 0 : 1; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase $this->prime_post_cache( $message->params[ $post_id_position ] ); } @@ -728,7 +788,7 @@ jQuery( function() { * Prime the post cache with swapped post_content. This is a sneaky way of getting around * the fact that there are no good hooks to call on the *.getPost xmlrpc methods. * - * @return null + * @param bool $post_id - the post ID that we're priming. */ private function prime_post_cache( $post_id = false ) { global $wp_xmlrpc_server; @@ -736,7 +796,7 @@ jQuery( function() { $post_id = $wp_xmlrpc_server->message->params[3]; } - // prime the post cache + // prime the post cache. if ( $this->is_markdown( $post_id ) ) { $post = get_post( $post_id ); if ( ! empty( $post->post_content_filtered ) ) { @@ -746,7 +806,7 @@ jQuery( function() { $this->posts_to_uncache[] = $post_id; } } - // uncache munged posts if using a persistent object cache + // uncache munged posts if using a persistent object cache. if ( wp_using_ext_object_cache() ) { add_action( 'shutdown', array( $this, 'uncache_munged_posts' ) ); } @@ -754,29 +814,28 @@ jQuery( function() { /** * Swaps `post_content_filtered` back to `post_content` for editing purposes. - * @param object $post WP_Post object - * @return object WP_Post object with swapped `post_content_filtered` and `post_content` + * + * @param object $post WP_Post object. + * @return object WP_Post object with swapped `post_content_filtered` and `post_content`. */ protected function swap_for_editing( $post ) { $markdown = $post->post_content_filtered; - // unencode encoded code blocks + // unencode encoded code blocks. $markdown = $this->get_parser()->codeblock_restore( $markdown ); - // restore beginning of line blockquotes - $markdown = preg_replace( '/^> /m', '> ', $markdown ); + // restore beginning of line blockquotes. + $markdown = preg_replace( '/^> /m', '> ', $markdown ); $post->post_content_filtered = $post->post_content; - $post->post_content = $markdown; + $post->post_content = $markdown; return $post; } - /** * We munge the post cache to serve proper markdown content to XML-RPC clients. * Uncache these after the XML-RPC session ends. - * @return null */ public function uncache_munged_posts() { // $this context gets lost in testing sometimes. Weird. - foreach( WPCom_Markdown::get_instance()->posts_to_uncache as $post_id ) { + foreach ( self::get_instance()->posts_to_uncache as $post_id ) { wp_cache_delete( $post_id, 'posts' ); } } @@ -784,8 +843,8 @@ jQuery( function() { /** * Since *.(get)?[Rr]ecentPosts calls get_posts with suppress filters on, we need to * turn them back on so that we can swap things for editing. - * @param object $wp_query WP_Query object - * @return null + * + * @param object $wp_query WP_Query object. */ public function make_filterable( $wp_query ) { $wp_query->set( 'suppress_filters', false ); @@ -794,16 +853,16 @@ jQuery( function() { /** * Swaps post_content and post_content_filtered for editing. - * @param array $posts Posts returned by the just-completed query - * @param object $wp_query Current WP_Query object - * @return array Modified $posts + * + * @param array $posts Posts returned by the just-completed query. + * @return array Modified $posts */ - public function the_posts( $posts, $wp_query ) { + public function the_posts( $posts ) { foreach ( $posts as $key => $post ) { if ( $this->is_markdown( $post->ID ) && ! empty( $posts[ $key ]->post_content_filtered ) ) { - $markdown = $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; + $posts[ $key ]->post_content = $markdown; } } return $posts; |