From 08cfbe8e7311eab0ae9aebbcb4373a33d1796961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=C3=BCel=20van=20der=20Steege?= Date: Fri, 8 Mar 2024 11:00:06 +0100 Subject: [PATCH] Simplify entry existence checks in source filters. --- src/Extension.php | 60 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/src/Extension.php b/src/Extension.php index 6e2e2f8..f3a2823 100644 --- a/src/Extension.php +++ b/src/Extension.php @@ -271,15 +271,26 @@ public function pre_submission( $form ) { * @return string */ public function source_text( $text, Payment $payment ) { - $text = __( 'Gravity Forms', 'pronamic_ideal' ) . '
'; + $source_url = $this->source_url( '', $payment ); - $entry = $this->is_active() ? RGFormsModel::get_lead( $payment->get_source_id() ) : false; - - $text .= sprintf( - false === $entry ? '%2$s' : '%2$s', - add_query_arg( [ 'pronamic_gf_lid' => $payment->get_source_id() ], admin_url( 'admin.php' ) ), + $entry_text = \sprintf( /* translators: %s: source id */ - sprintf( __( 'Entry #%s', 'pronamic_ideal' ), $payment->get_source_id() ) + __( 'Entry #%s', 'pronamic_ideal' ), + $payment->get_source_id() + ); + + if ( '' !== $source_url ) { + $entry_text = \sprintf( + '%2$s', + $source_url, + $entry_text + ); + } + + $text = \sprintf( + '%1$s
%2$s', + \__( 'Gravity Forms', 'pronamic_ideal' ), + $entry_text ); return $text; @@ -306,9 +317,9 @@ public function source_description( $description, Payment $payment ) { * @return string */ public function source_url( $url, Payment $payment ) { - $entry = RGFormsModel::get_lead( $payment->get_source_id() ); + $count_entries = \GFAPI::count_entries( null, [ 'id' => $payment->get_source_id() ] ); - if ( false !== $entry ) { + if ( $count_entries > 0 ) { $url = add_query_arg( [ 'pronamic_gf_lid' => $payment->get_source_id(), @@ -329,15 +340,26 @@ public function source_url( $url, Payment $payment ) { * @return string */ public function subscription_source_text( $text, Subscription $subscription ) { - $text = __( 'Gravity Forms', 'pronamic_ideal' ) . '
'; - - $entry = $this->is_active() ? RGFormsModel::get_lead( $subscription->get_source_id() ) : false; + $source_url = $this->subscription_source_url( '', $subscription ); - $text .= sprintf( - false === $entry ? '%2$s' : '%2$s', - add_query_arg( [ 'pronamic_gf_lid' => $subscription->get_source_id() ], admin_url( 'admin.php' ) ), + $entry_text = \sprintf( /* translators: %s: source id */ - sprintf( __( 'Entry #%s', 'pronamic_ideal' ), $subscription->get_source_id() ) + __( 'Entry #%s', 'pronamic_ideal' ), + $subscription->get_source_id() + ); + + if ( '' !== $source_url ) { + $entry_text = \sprintf( + '%2$s', + $source_url, + $entry_text + ); + } + + $text = \sprintf( + '%1$s
%2$s', + \__( 'Gravity Forms', 'pronamic_ideal' ), + $entry_text ); return $text; @@ -364,9 +386,9 @@ public function subscription_source_description( $description, Subscription $sub * @return string */ public function subscription_source_url( $url, Subscription $subscription ) { - $entry = RGFormsModel::get_lead( $subscription->get_source_id() ); + $count_entries = \GFAPI::count_entries( null, [ 'id' => $subscription->get_source_id() ] ); - if ( false !== $entry ) { + if ( $count_entries > 0 ) { $url = add_query_arg( [ 'pronamic_gf_lid' => $subscription->get_source_id(), @@ -1231,7 +1253,7 @@ public function replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html /** * Bank transfer recipient details. - * + * * Use bank transfer details from last subscription payment if available. */ $payment = null;