Skip to content

Commit

Permalink
Simplify entry existence checks in source filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
rvdsteege committed Mar 8, 2024
1 parent 6777304 commit 08cfbe8
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,26 @@ public function pre_submission( $form ) {
* @return string
*/
public function source_text( $text, Payment $payment ) {
$text = __( 'Gravity Forms', 'pronamic_ideal' ) . '<br />';
$source_url = $this->source_url( '', $payment );

$entry = $this->is_active() ? RGFormsModel::get_lead( $payment->get_source_id() ) : false;

$text .= sprintf(
false === $entry ? '%2$s' : '<a href="%1$s">%2$s</a>',
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(
'<a href="%1$s">%2$s</a>',
$source_url,
$entry_text
);
}

$text = \sprintf(
'%1$s<br />%2$s',
\__( 'Gravity Forms', 'pronamic_ideal' ),
$entry_text
);

return $text;
Expand All @@ -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(),
Expand All @@ -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' ) . '<br />';

$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' : '<a href="%1$s">%2$s</a>',
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(
'<a href="%1$s">%2$s</a>',
$source_url,
$entry_text
);
}

$text = \sprintf(
'%1$s<br />%2$s',
\__( 'Gravity Forms', 'pronamic_ideal' ),
$entry_text
);

return $text;
Expand All @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 08cfbe8

Please sign in to comment.