| @@ -32,8 +32,12 @@ | ||
| 32 | 32 | * GNU General Public License for more details. |
| 33 | 33 | * ********************************************************************** |
| 34 | 34 | */ |
| 35 | 35 | |
| 36 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 37 | + exit( 0 ); | |
| 38 | +} | |
| 39 | + | |
| 36 | 40 | /** |
| 37 | 41 | * WPCom_Markdown class. |
| 38 | 42 | */ |
| 39 | 43 | class WPCom_Markdown { |
| @@ -74,8 +78,15 @@ | ||
| 74 | 78 | 'parent' => array(), |
| 75 | 79 | ); |
| 76 | 80 | |
| 77 | 81 | /** |
| 82 | + * Whether or not kses filters were removed. Only set if removal was attempted. | |
| 83 | + * | |
| 84 | + * @var ?bool | |
| 85 | + */ | |
| 86 | + public $kses; | |
| 87 | + | |
| 88 | + /** | |
| 78 | 89 | * Yay singletons! |
| 79 | 90 | * |
| 80 | 91 | * @return object WPCom_Markdown instance |
| 81 | 92 | */ |
| @@ -92,8 +103,9 @@ | ||
| 92 | 103 | public function load() { |
| 93 | 104 | $this->add_default_post_type_support(); |
| 94 | 105 | $this->maybe_load_actions_and_filters(); |
| 95 | 106 | if ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) { |
| 107 | + // phpcs:ignore WPCUT.SwitchBlog.SwitchBlog -- wpcom flags **every** use of switch_blog, apparently expecting valid instances to ignore or suppress the sniff. | |
| 96 | 108 | add_action( 'switch_blog', array( $this, 'maybe_load_actions_and_filters' ), 10, 2 ); |
| 97 | 109 | } |
| 98 | 110 | add_action( 'admin_init', array( $this, 'register_setting' ) ); |
| 99 | 111 | add_action( 'admin_init', array( $this, 'maybe_unload_for_bulk_edit' ) ); |
| @@ -169,12 +181,12 @@ | ||
| 169 | 181 | public function unload_markdown_for_posts() { |
| 170 | 182 | remove_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ) ); |
| 171 | 183 | remove_action( 'after_wp_tiny_mce', array( $this, 'after_wp_tiny_mce' ) ); |
| 172 | 184 | remove_action( 'wp_insert_post', array( $this, 'wp_insert_post' ) ); |
| 173 | - remove_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); | |
| 174 | - remove_filter( 'edit_post_content', array( $this, 'edit_post_content' ), 10, 2 ); | |
| 175 | - remove_filter( 'edit_post_content_filtered', array( $this, 'edit_post_content_filtered' ), 10, 2 ); | |
| 176 | - remove_action( 'wp_restore_post_revision', array( $this, 'wp_restore_post_revision' ), 10, 2 ); | |
| 185 | + remove_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10 ); | |
| 186 | + remove_filter( 'edit_post_content', array( $this, 'edit_post_content' ), 10 ); | |
| 187 | + remove_filter( 'edit_post_content_filtered', array( $this, 'edit_post_content_filtered' ), 10 ); | |
| 188 | + remove_action( 'wp_restore_post_revision', array( $this, 'wp_restore_post_revision' ), 10 ); | |
| 177 | 189 | remove_filter( '_wp_post_revision_fields', array( $this, 'wp_post_revision_fields' ) ); |
| 178 | 190 | remove_action( 'xmlrpc_call', array( $this, 'xmlrpc_actions' ) ); |
| 179 | 191 | remove_filter( 'content_save_pre', array( $this, 'preserve_code_blocks' ), 1 ); |
| 180 | 192 | } |
| @@ -304,9 +316,9 @@ | ||
| 304 | 316 | '<label><input name="%1$s" id="%1$s" type="checkbox"%2$s /> %3$s</label><p class="description">%4$s</p>', |
| 305 | 317 | esc_attr( self::POST_OPTION ), |
| 306 | 318 | checked( $this->is_posting_enabled(), true, false ), |
| 307 | 319 | esc_html__( 'Use Markdown for posts and pages.', 'jetpack' ), |
| 308 | - sprintf( '<a href="%s">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) ) | |
| 320 | + sprintf( '<a href="%s" data-target="wpcom-help-center">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) ) | |
| 309 | 321 | ); |
| 310 | 322 | } |
| 311 | 323 | |
| 312 | 324 | /** |
| @@ -317,9 +329,9 @@ | ||
| 317 | 329 | '<label><input name="%1$s" id="%1$s" type="checkbox"%2$s /> %3$s</label><p class="description">%4$s</p>', |
| 318 | 330 | esc_attr( self::COMMENT_OPTION ), |
| 319 | 331 | checked( $this->is_commenting_enabled(), true, false ), |
| 320 | 332 | esc_html__( 'Use Markdown for comments.', 'jetpack' ), |
| 321 | - sprintf( '<a href="%s">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) ) | |
| 333 | + sprintf( '<a href="%s" data-target="wpcom-help-center">%s</a>', esc_url( $this->get_support_url() ), esc_html__( 'Learn more about Markdown.', 'jetpack' ) ) | |
| 322 | 334 | ); |
| 323 | 335 | } |
| 324 | 336 | |
| 325 | 337 | /** |
| @@ -479,8 +491,14 @@ | ||
| 479 | 491 | } |
| 480 | 492 | |
| 481 | 493 | $re = '/' . $this->get_parser()->contain_span_tags_re . '/'; |
| 482 | 494 | foreach ( $tags as $tag => $attributes ) { |
| 495 | + | |
| 496 | + // In case other filters have changed the value to a non-array, we skip it. | |
| 497 | + if ( ! is_array( $attributes ) ) { | |
| 498 | + continue; | |
| 499 | + } | |
| 500 | + | |
| 483 | 501 | if ( preg_match( $re, $tag ) ) { |
| 484 | 502 | $attributes['markdown'] = true; |
| 485 | 503 | $tags[ $tag ] = $attributes; |
| 486 | 504 | } |
| @@ -521,9 +539,9 @@ | ||
| 521 | 539 | * @return array $post_data with post_content and post_content_filtered modified |
| 522 | 540 | */ |
| 523 | 541 | public function wp_insert_post_data( $post_data, $postarr ) { |
| 524 | 542 | // $post_data array is slashed! |
| 525 | - $post_id = isset( $postarr['ID'] ) ? $postarr['ID'] : false; | |
| 543 | + $post_id = $postarr['ID'] ?? false; | |
| 526 | 544 | // bail early if markdown is disabled or this post type is unsupported. |
| 527 | 545 | if ( ! $this->is_posting_enabled() || ! post_type_supports( $post_data['post_type'], self::POST_TYPE_SUPPORT ) ) { |
| 528 | 546 | // it's disabled, but maybe this *was* a markdown post before. |
| 529 | 547 | if ( $this->is_markdown( $post_id ) && ! empty( $post_data['post_content_filtered'] ) ) { |
| @@ -536,9 +554,9 @@ | ||
| 536 | 554 | } |
| 537 | 555 | // rejigger post_content and post_content_filtered |
| 538 | 556 | // revisions are already in the right place, except when we're restoring, but that's taken care of elsewhere |
| 539 | 557 | // also prevent quick edit feature from overriding already-saved markdown (issue https://github.com/Automattic/jetpack/issues/636). |
| 540 | - if ( 'revision' !== $post_data['post_type'] && ! isset( $_POST['_inline_edit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 558 | + if ( 'revision' !== $post_data['post_type'] && ! isset( $_POST['_inline_edit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 541 | 559 | /** |
| 542 | 560 | * Filter the original post content passed to Markdown. |
| 543 | 561 | * |
| 544 | 562 | * @module markdown |
| @@ -550,9 +568,9 @@ | ||
| 550 | 568 | $post_data['post_content_filtered'] = apply_filters( 'wpcom_untransformed_content', $post_data['post_content'] ); |
| 551 | 569 | $post_data['post_content'] = $this->transform( $post_data['post_content'], array( 'id' => $post_id ) ); |
| 552 | 570 | /** This filter is already documented in core/wp-includes/default-filters.php */ |
| 553 | 571 | $post_data['post_content'] = apply_filters( 'content_save_pre', $post_data['post_content'] ); |
| 554 | - } elseif ( 0 === strpos( $post_data['post_name'], $post_data['post_parent'] . '-autosave' ) ) { | |
| 572 | + } elseif ( str_starts_with( $post_data['post_name'], $post_data['post_parent'] . '-autosave' ) ) { | |
| 555 | 573 | // autosaves for previews are weird. |
| 556 | 574 | /** This filter is already documented in modules/markdown/easy-markdown.php */ |
| 557 | 575 | $post_data['post_content_filtered'] = apply_filters( 'wpcom_untransformed_content', $post_data['post_content'] ); |
| 558 | 576 | $post_data['post_content'] = $this->transform( $post_data['post_content'], array( 'id' => $post_data['post_parent'] ) ); |
| @@ -659,9 +677,9 @@ | ||
| 659 | 677 | * |
| 660 | 678 | * @param string $text Content to be run through Markdown |
| 661 | 679 | * @param array $args Array of Markdown options. |
| 662 | 680 | */ |
| 663 | - $text = apply_filters( 'wpcom_markdown_transform_pre', $text, $args ); | |
| 681 | + $text = apply_filters( 'wpcom_markdown_transform_pre', $text, $args ) ?? ''; | |
| 664 | 682 | // ensure our paragraphs are separated. |
| 665 | 683 | $text = str_replace( array( '</p><p>', "</p>\n<p>" ), "</p>\n\n<p>", $text ); |
| 666 | 684 | // visual editor likes to add <p>s. Buh-bye. |
| 667 | 685 | $text = $this->get_parser()->unp( $text ); |
| @@ -772,10 +790,10 @@ | ||
| 772 | 790 | * @return null |
| 773 | 791 | */ |
| 774 | 792 | protected function check_for_early_methods() { |
| 775 | 793 | $raw_post_data = file_get_contents( 'php://input' ); |
| 776 | - if ( false === strpos( $raw_post_data, 'metaWeblog.getPost' ) | |
| 777 | - && false === strpos( $raw_post_data, 'wp.getPage' ) ) { | |
| 794 | + if ( ! str_contains( $raw_post_data, 'metaWeblog.getPost' ) | |
| 795 | + && ! str_contains( $raw_post_data, 'wp.getPage' ) ) { | |
| 778 | 796 | return; |
| 779 | 797 | } |
| 780 | 798 | include_once ABSPATH . WPINC . '/class-IXR.php'; |
| 781 | 799 | $message = new IXR_Message( $raw_post_data ); |
| @@ -780,9 +798,9 @@ | ||
| 780 | 798 | include_once ABSPATH . WPINC . '/class-IXR.php'; |
| 781 | 799 | $message = new IXR_Message( $raw_post_data ); |
| 782 | 800 | $message->parse(); |
| 783 | 801 | $post_id_position = 'metaWeblog.getPost' === $message->methodName ? 0 : 1; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 784 | - $this->prime_post_cache( $message->params[ $post_id_position ] ); | |
| 802 | + $this->prime_post_cache( $message->params[ $post_id_position ] ?? false ); | |
| 785 | 803 | } |
| 786 | 804 | |
| 787 | 805 | /** |
| 788 | 806 | * Prime the post cache with swapped post_content. This is a sneaky way of getting around |
| @@ -792,9 +810,13 @@ | ||
| 792 | 810 | */ |
| 793 | 811 | private function prime_post_cache( $post_id = false ) { |
| 794 | 812 | global $wp_xmlrpc_server; |
| 795 | 813 | if ( ! $post_id ) { |
| 796 | - $post_id = $wp_xmlrpc_server->message->params[3]; | |
| 814 | + if ( isset( $wp_xmlrpc_server->message->params[3] ) ) { | |
| 815 | + $post_id = $wp_xmlrpc_server->message->params[3]; | |
| 816 | + } else { | |
| 817 | + return; // Exit early if we can't get a valid post_id | |
| 818 | + } | |
| 797 | 819 | } |
| 798 | 820 | |
| 799 | 821 | // prime the post cache. |
| 800 | 822 | if ( $this->is_markdown( $post_id ) ) { |