diff --git a/src/Admin/AdminModule.php b/src/Admin/AdminModule.php index 13cf04b2..c1aa9042 100644 --- a/src/Admin/AdminModule.php +++ b/src/Admin/AdminModule.php @@ -144,8 +144,14 @@ public function admin_init() { // Post types. new AdminGatewayPostType( $this->plugin ); - new AdminPaymentPostType( $this->plugin ); - new AdminSubscriptionPostType( $this->plugin ); + + $admin_payment_post_type = new AdminPaymentPostType( $this->plugin ); + + $admin_payment_post_type->admin_init(); + + $admin_subscription_post_type = new AdminSubscriptionPostType( $this->plugin ); + + $admin_subscription_post_type->admin_init(); } /** diff --git a/src/Admin/AdminPaymentPostType.php b/src/Admin/AdminPaymentPostType.php index c620842e..92beb34b 100644 --- a/src/Admin/AdminPaymentPostType.php +++ b/src/Admin/AdminPaymentPostType.php @@ -688,20 +688,6 @@ public function meta_box_subscription( $post ) { include __DIR__ . '/../../views/meta-box-payment-subscription.php'; } - /** - * Pronamic Pay gateway update meta box. - * - * @param WP_Post $post The object for the current post/page. - * @return void - */ - public function meta_box_update( // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Parameter is used in include. - $post - ) { - wp_nonce_field( 'pronamic_payment_update', 'pronamic_payment_update_nonce' ); - - include __DIR__ . '/../../views/meta-box-payment-update.php'; - } - /** * Post row actions. * @@ -761,4 +747,73 @@ public function post_updated_messages( $messages ) { return $messages; } + + /** + * Pronamic Pay payment update meta box. + * + * @param WP_Post $post The object for the current post/page. + * @return void + */ + public function meta_box_update( // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Parameter is used in include. + $post + ) { + wp_nonce_field( 'pronamic_payment_update', 'pronamic_payment_update_nonce' ); + + include __DIR__ . '/../../views/meta-box-payment-update.php'; + } + + /** + * Admin init. + * + * @return void + */ + public function admin_init() { + $this->maybe_update_payment(); + } + + /** + * Maybe update payment status. + * + * @return void + */ + private function maybe_update_payment() { + if ( ! \array_key_exists( 'pronamic_payment_update', $_POST ) ) { + return; + } + + if ( ! \array_key_exists( 'pronamic_payment_id', $_POST ) ) { + return; + } + + if ( ! \array_key_exists( 'pronamic_payment_status', $_POST ) ) { + return; + } + + if ( ! \array_key_exists( 'pronamic_payment_update_nonce', $_POST ) ) { + return; + } + + $nonce = \sanitize_text_field( \wp_unslash( $_POST['pronamic_payment_update_nonce'] ) ); + + if ( ! \wp_verify_nonce( $nonce, 'pronamic_payment_update' ) ) { + \wp_die( \esc_html__( 'Action failed. Please refresh the page and retry.', 'pronamic_ideal' ) ); + } + + $payment_id = \sanitize_text_field( \wp_unslash( $_POST['pronamic_payment_id'] ) ); + + $payment = \get_pronamic_payment( $payment_id ); + + if ( null === $payment ) { + return; + } + + $status = \sanitize_text_field( \wp_unslash( $_POST['pronamic_payment_status'] ) ); + + if ( '' === $status ) { + return; + } + + $payment->set_status( $status ); + $payment->save(); + } } diff --git a/src/Admin/AdminSubscriptionPostType.php b/src/Admin/AdminSubscriptionPostType.php index 75c92d9a..fbc12536 100644 --- a/src/Admin/AdminSubscriptionPostType.php +++ b/src/Admin/AdminSubscriptionPostType.php @@ -693,20 +693,6 @@ public function meta_box_payments( $post ) { include __DIR__ . '/../../views/meta-box-subscription-payments.php'; } - /** - * Pronamic Pay subscription update meta box. - * - * @param WP_Post $post The object for the current post/page. - * @return void - */ - public function meta_box_update( // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Parameter is used in include. - $post - ) { - wp_nonce_field( 'pronamic_subscription_update', 'pronamic_subscription_update_nonce' ); - - include __DIR__ . '/../../views/meta-box-subscription-update.php'; - } - /** * Post row actions. * @@ -766,4 +752,101 @@ public function post_updated_messages( $messages ) { return $messages; } + + /** + * Pronamic Pay subscription update meta box. + * + * @param WP_Post $post The object for the current post/page. + * @return void + */ + public function meta_box_update( // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Parameter is used in include. + $post + ) { + wp_nonce_field( 'pronamic_subscription_update', 'pronamic_subscription_update_nonce' ); + + include __DIR__ . '/../../views/meta-box-subscription-update.php'; + } + + /** + * Admin init. + * + * @return void + */ + public function admin_init() { + $this->maybe_update_subscription(); + } + + /** + * Maybe update subscription. + * + * @return void + */ + private function maybe_update_subscription() { + if ( ! \array_key_exists( 'pronamic_subscription_update', $_POST ) ) { + return; + } + + if ( ! \array_key_exists( 'pronamic_subscription_id', $_POST ) ) { + return; + } + + if ( ! \array_key_exists( 'pronamic_subscription_status', $_POST ) ) { + return; + } + + if ( ! \array_key_exists( 'pronamic_subscription_update_nonce', $_POST ) ) { + return; + } + + $nonce = \sanitize_text_field( \wp_unslash( $_POST['pronamic_subscription_update_nonce'] ) ); + + if ( ! \wp_verify_nonce( $nonce, 'pronamic_subscription_update' ) ) { + \wp_die( \esc_html__( 'Action failed. Please refresh the page and retry.', 'pronamic_ideal' ) ); + } + + $subscription_id = (int) \sanitize_text_field( \wp_unslash( $_POST['pronamic_subscription_id'] ) ); + + $subscription = \get_pronamic_subscription( $subscription_id ); + + if ( null === $subscription ) { + return; + } + + $status = \sanitize_text_field( \wp_unslash( $_POST['pronamic_subscription_status'] ) ); + + if ( '' !== $status ) { + $subscription->set_status( $status ); + } + + if ( \array_key_exists( 'hidden_pronamic_pay_next_payment_date', $_POST ) && \array_key_exists( 'pronamic_subscription_next_payment_date', $_POST ) ) { + $old_value = \sanitize_text_field( \wp_unslash( $_POST['hidden_pronamic_pay_next_payment_date'] ) ); + + $new_value = \sanitize_text_field( \wp_unslash( $_POST['pronamic_subscription_next_payment_date'] ) ); + + if ( ! empty( $new_value ) && $old_value !== $new_value ) { + $new_date = new DateTimeImmutable( $new_value ); + + $next_payment_date = $subscription->get_next_payment_date(); + + $updated_date = null === $next_payment_date ? clone $new_date : clone $next_payment_date; + + $updated_date = $updated_date->setDate( (int) $new_date->format( 'Y' ), (int) $new_date->format( 'm' ), (int) $new_date->format( 'd' ) ); + + if ( false !== $updated_date ) { + $subscription->set_next_payment_date( $updated_date ); + + $note = \sprintf( + /* translators: %1: old formatted date, %2: new formatted date */ + \__( 'Next payment date updated from %1$s to %2$s.', 'pronamic_ideal' ), + null === $next_payment_date ? '' : $next_payment_date->format_i18n( \__( 'D j M Y', 'pronamic_ideal' ) ), + $updated_date->format_i18n( \__( 'D j M Y', 'pronamic_ideal' ) ) + ); + + $subscription->add_note( $note ); + } + } + } + + $subscription->save(); + } } diff --git a/src/Payments/PaymentsDataStoreCPT.php b/src/Payments/PaymentsDataStoreCPT.php index 525d4c17..5b49005d 100644 --- a/src/Payments/PaymentsDataStoreCPT.php +++ b/src/Payments/PaymentsDataStoreCPT.php @@ -30,13 +30,6 @@ * @since 3.7.0 */ class PaymentsDataStoreCPT extends LegacyPaymentsDataStoreCPT { - /** - * Payment. - * - * @var Payment|null - */ - private $payment; - /** * Payments. * @@ -73,17 +66,6 @@ public function __construct() { ]; } - /** - * Setup. - * - * @return void - */ - public function setup() { - add_filter( 'wp_insert_post_data', [ $this, 'insert_payment_post_data' ], 10, 2 ); - - add_action( 'save_post_pronamic_payment', [ $this, 'save_post_meta' ], 100, 3 ); - } - /** * Get payment by ID. * @@ -91,27 +73,29 @@ public function setup() { * @return Payment|null */ public function get_payment( $id ) { - if ( ! isset( $this->payments[ $id ] ) ) { - if ( empty( $id ) ) { - return null; - } + if ( \array_key_exists( $id, $this->payments ) ) { + return $this->payments[ $id ]; + } - $id = (int) $id; + if ( empty( $id ) ) { + return null; + } - $post_type = get_post_type( $id ); + $id = (int) $id; - if ( 'pronamic_payment' !== $post_type ) { - return null; - } + $post_type = \get_post_type( $id ); - $payment = new Payment(); + if ( 'pronamic_payment' !== $post_type ) { + return null; + } - $payment->set_id( $id ); + $payment = new Payment(); - $this->payments[ $id ] = $payment; + $payment->set_id( $id ); - $this->read( $payment ); - } + $this->payments[ $id ] = $payment; + + $this->read( $payment ); return $this->payments[ $id ]; } @@ -135,123 +119,33 @@ private function get_post_status_from_meta_status( $meta_status ) { } /** - * Get meta status from post status. - * - * @param string $post_status Post status. - * @return string|null - */ - private function get_meta_status_from_post_status( $post_status ) { - $key = array_search( $post_status, $this->status_map, true ); - - if ( false !== $key ) { - return \strval( $key ); - } - - return null; - } - - /** - * Complement payment post data. - * - * @link https://github.com/WordPress/WordPress/blob/5.0.3/wp-includes/post.php#L3515-L3523 - * @link https://developer.wordpress.org/reference/functions/wp_json_encode/ - * - * @param array $data An array of slashed post data. - * @param array $postarr An array of sanitized, but otherwise unmodified post data. + * Get post data. + * + * @param Payment $payment Payment. + * @param array $data Post data. * @return array - * @throws \Exception When inserting payment post data JSON string fails. + * @throws \Exception Throws an exception if an error occurs while encoding the payment to JSON. */ - public function insert_payment_post_data( $data, $postarr ) { - $this->payment = null; - - if ( isset( $postarr['pronamic_payment'] ) ) { - $this->payment = $postarr['pronamic_payment']; - } elseif ( isset( $postarr['ID'] ) ) { - $post_id = $postarr['ID']; + private function get_post_data( Payment $payment, $data ) { + $json_string = \wp_json_encode( $payment->get_json() ); - $this->payment = $this->get_payment( $post_id ); + if ( false === $json_string ) { + throw new \Exception( 'Error occurred while encoding the payment to JSON.' ); } - if ( $this->payment instanceof Payment ) { - $payment = $this->payment; + $data['post_content'] = \wp_slash( $json_string ); + $data['post_mime_type'] = 'application/json'; + $data['post_name'] = $payment->get_slug(); - // Update subscription from post array. - $this->update_payment_form_post_array( $payment, $postarr ); + $status = $this->get_post_status_from_meta_status( $payment->get_status() ); - if ( ! isset( $data['post_status'] ) || 'trash' !== $data['post_status'] ) { - $data['post_status'] = $this->get_post_status_from_meta_status( $payment->get_status() ); - } - - // Data. - $json_string = wp_json_encode( $payment->get_json() ); - - if ( false === $json_string ) { - throw new \Exception( 'Error inserting payment post data as JSON.' ); - } - - $data['post_content'] = wp_slash( $json_string ); - $data['post_mime_type'] = 'application/json'; + if ( null !== $status ) { + $data['post_status'] = $status; } return $data; } - /** - * Update payment from post array. - * - * @param Payment $payment Payment. - * @param array $postarr Post data array. - * @return void - */ - private function update_payment_form_post_array( $payment, $postarr ) { - if ( ! isset( $postarr['pronamic_payment_post_status'] ) ) { - return; - } - - $post_status = sanitize_text_field( stripslashes( $postarr['pronamic_payment_post_status'] ) ); - $meta_status = $this->get_meta_status_from_post_status( $post_status ); - - if ( null === $meta_status ) { - return; - } - - $payment->set_status( $meta_status ); - } - - /** - * Save post meta. - * - * @link https://github.com/WordPress/WordPress/blob/5.0.3/wp-includes/post.php#L3724-L3736 - * - * @param int $post_id Post ID. - * @param \WP_Post $post Post object. - * @param bool $update Whether this is an existing post being updated or not. - * @return void - */ - public function save_post_meta( $post_id, $post, $update ) { - if ( $this->payment instanceof Payment ) { - $payment = $this->payment; - - if ( ! $update && null === $payment->get_id() ) { - $this->payments[ $post_id ] = $payment; - - $payment->set_id( $post_id ); - $payment->post = $post; - } - - $this->update_post_meta( $payment ); - - /** - * Payment updated. - * - * @param Payment $payment Payment. - */ - do_action( 'pronamic_pay_update_payment', $payment ); - } - - $this->payment = null; - } - /** * Create payment. * @@ -273,31 +167,31 @@ public function create( Payment $payment ) { $customer_user_id = null === $customer ? 0 : $customer->get_user_id(); - $result = wp_insert_post( - /** - * The 'pronamic_payment' key is not an official argument for the - * WordPress `wp_insert_post` function. - * - * @todo Simplify storing payments. - */ - [ - 'post_type' => 'pronamic_payment', - 'post_date_gmt' => $this->get_mysql_utc_date( $payment->date ), - 'post_title' => \sprintf( - 'Payment %s', - $payment->get_key() - ), - 'post_name' => $payment->get_slug(), - 'post_author' => null === $customer_user_id ? 0 : $customer_user_id, - 'pronamic_payment' => $payment, - ], + $result = \wp_insert_post( + $this->get_post_data( + $payment, + [ + 'post_type' => 'pronamic_payment', + 'post_date_gmt' => $this->get_mysql_utc_date( $payment->date ), + 'post_title' => \sprintf( + 'Payment %s', + $payment->get_key() + ), + 'post_author' => null === $customer_user_id ? 0 : $customer_user_id, + ] + ), true ); - if ( is_wp_error( $result ) ) { + if ( \is_wp_error( $result ) ) { throw new \Exception( 'Could not create payment' ); } + $payment->set_id( $result ); + $payment->post = \get_post( $result ); + + $this->payments[ $result ] = $payment; + /** * New payment created. * @@ -325,18 +219,24 @@ public function update( Payment $payment ) { return false; } - $data = [ - 'ID' => $id, - 'post_name' => $payment->get_slug(), - 'pronamic_payment' => $payment, - ]; - - $result = wp_update_post( $data, true ); + $result = \wp_update_post( + $this->get_post_data( + $payment, + [ + 'ID' => $id, + ] + ), + true + ); if ( is_wp_error( $result ) ) { throw new \Exception( 'Could not update payment' ); } + $payment->post = \get_post( $result ); + + $this->payments[ $result ] = $payment; + return true; } @@ -353,6 +253,15 @@ public function save( $payment ) { $result = empty( $id ) ? $this->create( $payment ) : $this->update( $payment ); + $this->update_post_meta( $payment ); + + /** + * Payment updated. + * + * @param Payment $payment Payment. + */ + do_action( 'pronamic_pay_update_payment', $payment ); + return $result; } diff --git a/src/Plugin.php b/src/Plugin.php index 509ec189..6aa89a58 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -623,9 +623,6 @@ public function plugins_loaded() { $this->payments_data_store = new PaymentsDataStoreCPT(); $this->subscriptions_data_store = new SubscriptionsDataStoreCPT(); - $this->payments_data_store->setup(); - $this->subscriptions_data_store->setup(); - // Post Types. $this->gateway_post_type = new GatewayPostType(); $this->payment_post_type = new PaymentPostType(); diff --git a/src/Subscriptions/SubscriptionsDataStoreCPT.php b/src/Subscriptions/SubscriptionsDataStoreCPT.php index 81e240ed..b8540420 100644 --- a/src/Subscriptions/SubscriptionsDataStoreCPT.php +++ b/src/Subscriptions/SubscriptionsDataStoreCPT.php @@ -29,13 +29,6 @@ * @since 2.0.1 */ class SubscriptionsDataStoreCPT extends LegacyPaymentsDataStoreCPT { - /** - * Subscription. - * - * @var Subscription|null - */ - private $subscription; - /** * Subscriptions. * @@ -73,17 +66,6 @@ public function __construct() { ]; } - /** - * Setup. - * - * @return void - */ - public function setup() { - add_filter( 'wp_insert_post_data', [ $this, 'insert_subscription_post_data' ], 10, 2 ); - - add_action( 'save_post_pronamic_pay_subscr', [ $this, 'save_post_meta' ], 100, 3 ); - } - /** * Get subscription by ID. * @@ -91,27 +73,29 @@ public function setup() { * @return Subscription|null */ public function get_subscription( $id ) { - if ( ! isset( $this->subscriptions[ $id ] ) ) { - if ( empty( $id ) ) { - return null; - } + if ( \array_key_exists( $id, $this->subscriptions ) ) { + return $this->subscriptions[ $id ]; + } + + if ( empty( $id ) ) { + return null; + } - $id = (int) $id; + $id = (int) $id; - $post_type = \get_post_type( $id ); + $post_type = \get_post_type( $id ); - if ( 'pronamic_pay_subscr' !== $post_type ) { - return null; - } + if ( 'pronamic_pay_subscr' !== $post_type ) { + return null; + } - $subscription = new Subscription(); + $subscription = new Subscription(); - $subscription->set_id( $id ); + $subscription->set_id( $id ); - $this->subscriptions[ $id ] = $subscription; + $this->subscriptions[ $id ] = $subscription; - $this->read( $subscription ); - } + $this->read( $subscription ); return $this->subscriptions[ $id ]; } @@ -135,148 +119,32 @@ private function get_post_status_from_meta_status( $meta_status ) { } /** - * Get meta status from post status. - * - * @param string $post_status Post status. - * @return string|null - */ - private function get_meta_status_from_post_status( $post_status ) { - $key = array_search( $post_status, $this->status_map, true ); - - if ( false !== $key ) { - return \strval( $key ); - } - - return null; - } - - /** - * Complement subscription post data. - * - * @link https://github.com/WordPress/WordPress/blob/5.0.3/wp-includes/post.php#L3515-L3523 - * - * @param array $data An array of slashed post data. - * @param array $postarr An array of sanitized, but otherwise unmodified post data. + * Get post data. + * + * @param Subscription $subscription Payment. + * @param array $data Post data. * @return array - * @throws \Exception When inserting subscription post data JSON string fails. + * @throws \Exception Throws an exception if an error occurs while encoding the payment to JSON. */ - public function insert_subscription_post_data( $data, $postarr ) { - $this->subscription = null; + private function get_post_data( Subscription $subscription, $data ) { + $json_string = \wp_json_encode( $subscription->get_json() ); - if ( isset( $postarr['pronamic_subscription'] ) ) { - $this->subscription = $postarr['pronamic_subscription']; - } elseif ( isset( $postarr['ID'] ) ) { - $post_id = $postarr['ID']; - - if ( 'pronamic_pay_subscr' === get_post_type( $post_id ) ) { - $this->subscription = $this->get_subscription( $post_id ); - } + if ( false === $json_string ) { + throw new \Exception( 'Error occurred while encoding the subscription to JSON.' ); } - if ( $this->subscription instanceof Subscription ) { - $subscription = $this->subscription; + $data['post_content'] = \wp_slash( $json_string ); + $data['post_mime_type'] = 'application/json'; - // Update subscription from post array. - $this->update_subscription_form_post_array( $subscription, $postarr ); + $status = $this->get_post_status_from_meta_status( $subscription->get_status() ); - if ( ! isset( $data['post_status'] ) || 'trash' !== $data['post_status'] ) { - $data['post_status'] = $this->get_post_status_from_meta_status( $subscription->get_status() ); - } - - // Data. - $json_string = wp_json_encode( $subscription->get_json() ); - - if ( false === $json_string ) { - throw new \Exception( 'Error inserting subscription post data as JSON.' ); - } - - $data['post_content'] = wp_slash( $json_string ); - $data['post_mime_type'] = 'application/json'; + if ( null !== $status ) { + $data['post_status'] = $status; } return $data; } - /** - * Update subscription from post array. - * - * @param Subscription $subscription Subscription. - * @param array $postarr Post data array. - * @return void - * @throws \Exception Throws exception if amount could not be parsed to Money object. - */ - private function update_subscription_form_post_array( $subscription, $postarr ) { - if ( isset( $postarr['pronamic_subscription_post_status'] ) ) { - $post_status = sanitize_text_field( stripslashes( $postarr['pronamic_subscription_post_status'] ) ); - $meta_status = $this->get_meta_status_from_post_status( $post_status ); - - if ( null !== $meta_status ) { - $subscription->set_status( $meta_status ); - } - } - - if ( ! isset( $postarr['pronamic_subscription_update_nonce'] ) ) { - return; - } - - if ( ! check_admin_referer( 'pronamic_subscription_update', 'pronamic_subscription_update_nonce' ) ) { - return; - } - - // Next payment date. - if ( \array_key_exists( 'hidden_pronamic_pay_next_payment_date', $postarr ) && \array_key_exists( 'pronamic_subscription_next_payment_date', $postarr ) ) { - $old_value = $postarr['hidden_pronamic_pay_next_payment_date']; - - $new_value = $postarr['pronamic_subscription_next_payment_date']; - - if ( ! empty( $new_value ) && $old_value !== $new_value ) { - $new_date = new DateTimeImmutable( $new_value ); - - $next_payment_date = $subscription->get_next_payment_date(); - - $updated_date = null === $next_payment_date ? clone $new_date : clone $next_payment_date; - - $updated_date = $updated_date->setDate( (int) $new_date->format( 'Y' ), (int) $new_date->format( 'm' ), (int) $new_date->format( 'd' ) ); - - if ( false !== $updated_date ) { - $subscription->set_next_payment_date( $updated_date ); - - $note = \sprintf( - /* translators: %1: old formatted date, %2: new formatted date */ - \__( 'Next payment date updated from %1$s to %2$s.', 'pronamic_ideal' ), - null === $next_payment_date ? '' : $next_payment_date->format_i18n( \__( 'D j M Y', 'pronamic_ideal' ) ), - $updated_date->format_i18n( \__( 'D j M Y', 'pronamic_ideal' ) ) - ); - - $subscription->add_note( $note ); - } - } - } - } - - /** - * Save post meta. - * - * @link https://github.com/WordPress/WordPress/blob/5.0.3/wp-includes/post.php#L3724-L3736 - * - * @param int $post_id Post ID. - * @param \WP_Post $post Post object. - * @param bool $update Whether this is an existing post being updated or not. - * @return void - */ - public function save_post_meta( $post_id, $post, $update ) { - if ( $this->subscription instanceof Subscription ) { - if ( ! $update && null === $this->subscription->get_id() ) { - $this->subscription->set_id( $post_id ); - $this->subscription->post = $post; - } - - $this->update_post_meta( $this->subscription ); - } - - $this->subscription = null; - } - /** * Create subscription. * @@ -298,23 +166,19 @@ public function create( $subscription ) { $customer_user_id = null === $customer ? 0 : $customer->get_user_id(); - $result = wp_insert_post( - /** - * The 'pronamic_subscription' key is not an official argument for the - * WordPress `wp_insert_post` function. - * - * @todo Simplify storing subscriptions. - */ - [ - 'post_type' => 'pronamic_pay_subscr', - 'post_date_gmt' => $this->get_mysql_utc_date( $subscription->date ), - 'post_title' => \sprintf( - 'Subscription %s', - $subscription->get_key() - ), - 'post_author' => null === $customer_user_id ? 0 : $customer_user_id, - 'pronamic_subscription' => $subscription, - ], + $result = \wp_insert_post( + $this->get_post_data( + $subscription, + [ + 'post_type' => 'pronamic_pay_subscr', + 'post_date_gmt' => $this->get_mysql_utc_date( $subscription->date ), + 'post_title' => \sprintf( + 'Subscription %s', + $subscription->get_key() + ), + 'post_author' => null === $customer_user_id ? 0 : $customer_user_id, + ] + ), true ); @@ -327,7 +191,10 @@ public function create( $subscription ) { ); } - $this->update_post_meta( $subscription ); + $subscription->set_id( $result ); + $subscription->post = \get_post( $result ); + + $this->subscriptions[ $result ] = $subscription; /** * New subscription created. @@ -356,12 +223,15 @@ public function update( $subscription ) { return false; } - $data = [ - 'ID' => $id, - 'pronamic_subscription' => $subscription, - ]; - - $result = wp_update_post( $data, true ); + $result = \wp_update_post( + $this->get_post_data( + $subscription, + [ + 'ID' => $id, + ] + ), + true + ); if ( \is_wp_error( $result ) ) { throw new \Exception( @@ -372,6 +242,10 @@ public function update( $subscription ) { ); } + $subscription->post = \get_post( $result ); + + $this->subscriptions[ $result ] = $subscription; + return true; } @@ -388,6 +262,8 @@ public function save( $subscription ) { $result = empty( $id ) ? $this->create( $subscription ) : $this->update( $subscription ); + $this->update_post_meta( $subscription ); + return $result; } diff --git a/tests/src/Core/ServerTest.php b/tests/src/Core/ServerTest.php deleted file mode 100644 index 7c119a50..00000000 --- a/tests/src/Core/ServerTest.php +++ /dev/null @@ -1,34 +0,0 @@ - - * @copyright 2005-2023 Pronamic - * @license GPL-3.0-or-later - * @package Pronamic\WordPress\Pay\Payments - */ - -namespace Pronamic\WordPress\Pay\Core; - -use Yoast\PHPUnitPolyfills\TestCases\TestCase; - -/** - * Title: WordPress pay server test - * Description: - * Copyright: 2005-2023 Pronamic - * Company: Pronamic - * - * @author Remco Tolsma - * @version 2.0.0 - * @since 1.1.0 - */ -class ServerTest extends TestCase { - /** - * Test server get. - */ - public function test_server_get() { - $value = Server::get( 'REQUEST_METHOD', \FILTER_DEFAULT ); - - $this->assertTrue( in_array( $value, [ null, 'GET' ], true ) ); - } -} diff --git a/views/meta-box-payment-update.php b/views/meta-box-payment-update.php index ed4a8bc5..81ca096a 100644 --- a/views/meta-box-payment-update.php +++ b/views/meta-box-payment-update.php @@ -8,8 +8,7 @@ * @package Pronamic\WordPress\Pay */ -use Pronamic\WordPress\Pay\Core\PaymentMethods; -use Pronamic\WordPress\Pay\Payments\PaymentPostType; +use Pronamic\WordPress\Pay\Payments\PaymentStatus; if ( ! defined( 'ABSPATH' ) ) { exit; @@ -19,9 +18,18 @@ return; } -$states = PaymentPostType::get_payment_states(); +$states = [ + PaymentStatus::OPEN => _x( 'Pending', 'Payment status', 'pronamic_ideal' ), + PaymentStatus::ON_HOLD => _x( 'On Hold', 'Payment status', 'pronamic_ideal' ), + PaymentStatus::SUCCESS => _x( 'Completed', 'Payment status', 'pronamic_ideal' ), + PaymentStatus::CANCELLED => _x( 'Cancelled', 'Payment status', 'pronamic_ideal' ), + PaymentStatus::REFUNDED => _x( 'Refunded', 'Payment status', 'pronamic_ideal' ), + PaymentStatus::FAILURE => _x( 'Failed', 'Payment status', 'pronamic_ideal' ), + PaymentStatus::EXPIRED => _x( 'Expired', 'Payment status', 'pronamic_ideal' ), + PaymentStatus::AUTHORIZED => _x( 'Authorized', 'Payment status', 'pronamic_ideal' ), +]; -$payment = get_pronamic_payment( get_the_ID() ); +$payment = \get_pronamic_payment( \get_the_ID() ); if ( null === $payment ) { return; @@ -48,12 +56,11 @@ post_status ); + $status_label = $payment->get_status_label(); - $status_label = isset( $status_object, $status_object->label ) ? $status_object->label : '—'; + $status_label = ( null === $status_label ) ? '—' : $status_label; ?> -
@@ -212,6 +222,11 @@ wp_nonce_field( 'pronamic_subscription_update', 'pronamic_subscription_nonce' ); + printf( + '', + esc_attr( (string) $subscription->get_id() ) + ); + submit_button( __( 'Update', 'pronamic_ideal' ), 'primary',