Skip to content

Commit

Permalink
Restore / simplify subscription update via admin UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Oct 26, 2023
1 parent d708276 commit 31678f5
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 97 deletions.
7 changes: 5 additions & 2 deletions src/Admin/AdminModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,15 @@ public function admin_init() {
$this->maybe_redirect();

// Post types.
new AdminGatewayPostType( $this->plugin );

$admin_payment_post_type = new AdminPaymentPostType( $this->plugin );

$admin_payment_post_type->admin_init();

new AdminGatewayPostType( $this->plugin );
new AdminSubscriptionPostType( $this->plugin );
$admin_subscription_post_type = new AdminSubscriptionPostType( $this->plugin );

$admin_subscription_post_type->admin_init();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Admin/AdminPaymentPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,15 +768,15 @@ public function meta_box_update( // phpcs:ignore Generic.CodeAnalysis.UnusedFunc
* @return void
*/
public function admin_init() {
$this->maybe_update_payment_status();
$this->maybe_update_payment();
}

/**
* Maybe update payment status.
*
* @return void
*/
private function maybe_update_payment_status() {
private function maybe_update_payment() {
if ( ! \array_key_exists( 'pronamic_payment_update', $_POST ) ) {
return;
}
Expand Down
111 changes: 97 additions & 14 deletions src/Admin/AdminSubscriptionPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 = \sanitize_text_field( \wp_unslash( $_POST['pronamic_subscription_id'] ) );

$subscription = \get_pronamic_subscription( $subscription_id );

Check failure on line 809 in src/Admin/AdminSubscriptionPostType.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Parameter #1 $post_id of function get_pronamic_subscription expects int, string given.

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();
}
}
Loading

0 comments on commit 31678f5

Please sign in to comment.