Skip to content

Commit

Permalink
Fix for #160.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Oct 31, 2023
1 parent f260bc3 commit d68d9d6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Payments/PaymentsDataStoreCPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,37 @@ public function __construct() {
];
}

/**
* Preserves the initial JSON post_content passed to save into the post.
*
* This is needed to prevent KSES and other {@see 'content_save_pre'} filters
* from corrupting JSON data.
*
* @link https://github.com/pronamic/wp-pay-core/issues/160
* @link https://developer.wordpress.org/reference/hooks/wp_insert_post_data/
* @param array $data An array of slashed and processed post data.
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
* @return array Filtered post data.
*/
public function preserve_post_content( $data, $postarr, $unsanitized_postarr ) {
if ( ! \array_key_exists( 'post_type', $data ) ) {
return $data;
}

if ( 'pronamic_payment' !== $data['post_type'] ) {
return $data;
}

if ( ! \array_key_exists( 'post_content', $unsanitized_postarr ) ) {
return $data;
}

$data['post_content'] = $unsanitized_postarr['post_content'];

return $data;
}

/**
* Get payment by ID.
*
Expand Down Expand Up @@ -251,8 +282,12 @@ public function update( Payment $payment ) {
public function save( $payment ) {
$id = $payment->get_id();

\add_filter( 'wp_insert_post_data', array( $this, 'preserve_post_content' ), 5, 3 );

$result = empty( $id ) ? $this->create( $payment ) : $this->update( $payment );

\remove_filter( 'wp_insert_post_data', array( $this, 'preserve_post_content' ), 5, 3 );

Check failure on line 289 in src/Payments/PaymentsDataStoreCPT.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Function remove_filter invoked with 4 parameters, 2-3 required.

$this->update_post_meta( $payment );

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Subscriptions/SubscriptionsDataStoreCPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,37 @@ public function __construct() {
];
}

/**
* Preserves the initial JSON post_content passed to save into the post.
*
* This is needed to prevent KSES and other {@see 'content_save_pre'} filters
* from corrupting JSON data.
*
* @link https://github.com/pronamic/wp-pay-core/issues/160
* @link https://developer.wordpress.org/reference/hooks/wp_insert_post_data/
* @param array $data An array of slashed and processed post data.
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
* @return array Filtered post data.
*/
public function preserve_post_content( $data, $postarr, $unsanitized_postarr ) {
if ( ! \array_key_exists( 'post_type', $data ) ) {
return $data;
}

if ( 'pronamic_pay_subscr' !== $data['post_type'] ) {
return $data;
}

if ( ! \array_key_exists( 'post_content', $unsanitized_postarr ) ) {
return $data;
}

$data['post_content'] = $unsanitized_postarr['post_content'];

return $data;
}

/**
* Get subscription by ID.
*
Expand Down Expand Up @@ -260,8 +291,12 @@ public function update( $subscription ) {
public function save( $subscription ) {
$id = $subscription->get_id();

\add_filter( 'wp_insert_post_data', array( $this, 'preserve_post_content' ), 5, 3 );

$result = empty( $id ) ? $this->create( $subscription ) : $this->update( $subscription );

\remove_filter( 'wp_insert_post_data', array( $this, 'preserve_post_content' ), 5, 3 );

Check failure on line 298 in src/Subscriptions/SubscriptionsDataStoreCPT.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Function remove_filter invoked with 4 parameters, 2-3 required.

$this->update_post_meta( $subscription );

return $result;
Expand Down

0 comments on commit d68d9d6

Please sign in to comment.