Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify data store cpt #159

Merged
merged 14 commits into from
Oct 27, 2023
Merged

Simplify data store cpt #159

merged 14 commits into from
Oct 27, 2023

Conversation

remcotolsma
Copy link
Member

@remcotolsma remcotolsma commented Oct 26, 2023

This PR may solve this issue:

It also improves/removes these comments:

$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,
],
true
);

$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,
],
true
);

And it removes some admin controller code from the data store classes:

/**
* 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 );
}

/**
* 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 );
}
}
}
}

I still have to check payments and subscriptions posts with WordPress post status draft or trash.

@remcotolsma remcotolsma self-assigned this Oct 26, 2023
@codecov-commenter
Copy link

Codecov Report

Attention: 137 lines in your changes are missing coverage. Please review.

Comparison is base (2378651) 22.38% compared to head (683bfb7) 22.13%.
Report is 13 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #159      +/-   ##
============================================
- Coverage     22.38%   22.13%   -0.26%     
+ Complexity     2553     2537      -16     
============================================
  Files           108      107       -1     
  Lines         10197    10187      -10     
============================================
- Hits           2283     2255      -28     
- Misses         7914     7932      +18     
Files Coverage Δ
src/ContactNameHelper.php 75.51% <ø> (ø)
src/CustomerHelper.php 48.51% <ø> (ø)
src/Plugin.php 19.27% <ø> (+0.09%) ⬆️
src/Subscriptions/SubscriptionsModule.php 9.97% <ø> (ø)
src/Webhooks/WebhookLogger.php 0.00% <0.00%> (ø)
src/Webhooks/WebhookRequestInfo.php 0.00% <0.00%> (ø)
src/Admin/AdminModule.php 0.00% <0.00%> (ø)
src/Admin/AdminAboutPage.php 0.00% <0.00%> (ø)
src/Payments/PaymentsDataStoreCPT.php 21.94% <75.00%> (-2.36%) ⬇️
src/Subscriptions/SubscriptionsDataStoreCPT.php 32.28% <73.46%> (+1.19%) ⬆️
... and 3 more

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@remcotolsma
Copy link
Member Author

I still have to check payments and subscriptions posts with WordPress post status draft or trash.

Just tested this, seems ok.

@remcotolsma remcotolsma merged commit f9134d1 into main Oct 27, 2023
20 checks passed
@remcotolsma remcotolsma deleted the simplify-data-store-cpt branch October 27, 2023 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants