Skip to content

Commit

Permalink
🐛 Fixed shortcodes being converted to HTML when sent to the API
Browse files Browse the repository at this point in the history
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/
  • Loading branch information
CWDN committed May 17, 2024
1 parent 8aa54ee commit 7075eab
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion includes/classes/sync/push.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand All @@ -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,
'<shortcode>' . base64_encode($match) . '</shortcode>',
$value
);
}

$value = $callback($value);

preg_match_all(
'/<shortcode>([\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.
Expand Down

0 comments on commit 7075eab

Please sign in to comment.