Skip to content

Commit

Permalink
Merge pull request #159 from pronamic/simplify-data-store-cpt
Browse files Browse the repository at this point in the history
Simplify data store cpt
  • Loading branch information
remcotolsma authored Oct 27, 2023
2 parents 68e9473 + 683bfb7 commit f9134d1
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 505 deletions.
10 changes: 8 additions & 2 deletions src/Admin/AdminModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
83 changes: 69 additions & 14 deletions src/Admin/AdminPaymentPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}
}
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 = (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();
}
}
Loading

0 comments on commit f9134d1

Please sign in to comment.