@@ -2886,22 +2886,11 @@ function save_changeset_post( $args = array() ) {
28862886 add_filter ( 'wp_save_post_revision_post_has_changed ' , array ( $ this , '_filter_revision_post_has_changed ' ), 5 , 3 );
28872887
28882888 /*
2889- * Update the changeset post. The publish_customize_changeset action
2890- * will cause the settings in the changeset to be saved via
2891- * WP_Customize_Setting::save ().
2889+ * Update the changeset post. The publish_customize_changeset action will cause the settings in the
2890+ * changeset to be saved via WP_Customize_Setting::save(). Updating a post with publish status will
2891+ * trigger WP_Customize_Manager::publish_changeset_values ().
28922892 */
2893-
2894- // Prevent content filters from corrupting JSON in post_content.
2895- $ has_kses = ( false !== has_filter ( 'content_save_pre ' , 'wp_filter_post_kses ' ) );
2896- if ( $ has_kses ) {
2897- kses_remove_filters ();
2898- }
2899- $ has_targeted_link_rel_filters = ( false !== has_filter ( 'content_save_pre ' , 'wp_targeted_link_rel ' ) );
2900- if ( $ has_targeted_link_rel_filters ) {
2901- wp_remove_targeted_link_rel_filters ();
2902- }
2903-
2904- // Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values().
2893+ add_filter ( 'wp_insert_post_data ' , array ( $ this , 'preserve_insert_changeset_post_content ' ), 5 , 3 );
29052894 if ( $ changeset_post_id ) {
29062895 if ( $ args ['autosave ' ] && 'auto-draft ' !== get_post_status ( $ changeset_post_id ) ) {
29072896 // See _wp_translate_postdata() for why this is required as it will use the edit_post meta capability.
@@ -2928,14 +2917,7 @@ function save_changeset_post( $args = array() ) {
29282917 $ this ->_changeset_post_id = $ r ; // Update cached post ID for the loaded changeset.
29292918 }
29302919 }
2931-
2932- // Restore removed content filters.
2933- if ( $ has_kses ) {
2934- kses_init_filters ();
2935- }
2936- if ( $ has_targeted_link_rel_filters ) {
2937- wp_init_targeted_link_rel_filters ();
2938- }
2920+ remove_filter ( 'wp_insert_post_data ' , array ( $ this , 'preserve_insert_changeset_post_content ' ), 5 );
29392921
29402922 $ this ->_changeset_data = null ; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
29412923
@@ -2953,6 +2935,51 @@ function save_changeset_post( $args = array() ) {
29532935 return $ response ;
29542936 }
29552937
2938+ /**
2939+ * Preserve the initial JSON post_content passed to save into the post.
2940+ *
2941+ * This is needed to prevent KSES and other {@see 'content_save_pre'} filters
2942+ * from corrupting JSON data.
2943+ *
2944+ * Note that WP_Customize_Manager::validate_setting_values() have already
2945+ * run on the setting values being serialized as JSON into the post content
2946+ * so it is pre-sanitized.
2947+ *
2948+ * Also, the sanitization logic is re-run through the respective
2949+ * WP_Customize_Setting::sanitize() method when being read out of the
2950+ * changeset, via WP_Customize_Manager::post_value(), and this sanitized
2951+ * value will also be sent into WP_Customize_Setting::update() for
2952+ * persisting to the DB.
2953+ *
2954+ * Multiple users can collaborate on a single changeset, where one user may
2955+ * have the unfiltered_html capability but another may not. A user with
2956+ * unfiltered_html may add a script tag to some field which needs to be kept
2957+ * intact even when another user updates the changeset to modify another field
2958+ * when they do not have unfiltered_html.
2959+ *
2960+ * @since 5.4.1
2961+ *
2962+ * @param array $data An array of slashed and processed post data.
2963+ * @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
2964+ * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
2965+ * @return array Filtered post data.
2966+ */
2967+ public function preserve_insert_changeset_post_content ( $ data , $ postarr , $ unsanitized_postarr ) {
2968+ if (
2969+ isset ( $ data ['post_type ' ] ) &&
2970+ isset ( $ unsanitized_postarr ['post_content ' ] ) &&
2971+ 'customize_changeset ' === $ data ['post_type ' ] ||
2972+ (
2973+ 'revision ' === $ data ['post_type ' ] &&
2974+ ! empty ( $ data ['post_parent ' ] ) &&
2975+ 'customize_changeset ' === get_post_type ( $ data ['post_parent ' ] )
2976+ )
2977+ ) {
2978+ $ data ['post_content ' ] = $ unsanitized_postarr ['post_content ' ];
2979+ }
2980+ return $ data ;
2981+ }
2982+
29562983 /**
29572984 * Trash or delete a changeset post.
29582985 *
0 commit comments