From 7075eab4a9475c043a74f54b606778d56e15f5d8 Mon Sep 17 00:00:00 2001 From: Chris Normansell Date: Fri, 17 May 2024 10:48:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20shortcodes=20being=20con?= =?UTF-8?q?verted=20to=20HTML=20when=20sent=20to=20the=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was caused due to the `the_content` hook going through the content and preparing it for outputting on the website, so ensuring everything is in HTML and not shortcodes appear. So what we do now is stop the shortcodes looking like shortcodes and then revert it back after. https://developer.wordpress.org/reference/hooks/the_content/ --- includes/classes/sync/push.php | 46 +++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/includes/classes/sync/push.php b/includes/classes/sync/push.php index 97d1be6c..3d82ffdd 100644 --- a/includes/classes/sync/push.php +++ b/includes/classes/sync/push.php @@ -253,6 +253,10 @@ public function maybe_do_item_update( $update ) { ); } + if ($result === false) { + wp_send_json_error( 'Failed to push content to Content Workflow' ); + } + return $result; } @@ -556,7 +560,12 @@ protected function set_post_field_value( $post_column ) { case 'post_excerpt': $el_value = wp_kses_post( $this->get_element_value() ); if ( 'post_content' === $post_column ) { - $value = apply_filters( 'the_content', $value ); + $value = $this->ensureShortcodesAreNotConvertedToHtml( + function ($value) { + return apply_filters( 'the_content', $value ); + }, + $value + ); } // There are super minor encoding issues we want to ignore. @@ -577,6 +586,41 @@ protected function set_post_field_value( $post_column ) { return $updated; } + private function ensureShortcodesAreNotConvertedToHtml(callable $callback, string $value): string + { + preg_match_all( + '/\[[\w\W]+?\]/', + $value, + $matches + ); + + foreach ($matches[0] as $match) { + $value = str_replace( + $match, + '' . base64_encode($match) . '', + $value + ); + } + + $value = $callback($value); + + preg_match_all( + '/([\w\W])+?<\/shortcode>/', + $value, + $matches + ); + + foreach ($matches[0] as $match) { + $value = str_replace( + $match, + base64_decode(strip_tags($match)), + $value + ); + } + + return $value; + } + /** * Sets the item config element value for WP taxonomy terms, * if it is determeined that the value changed.