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

Add setting for order ID template #5

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function redirect_for_payment( $entry_id, $form_id ) {

$payment->source = 'formidable-forms';
$payment->source_id = $entry_id;
$payment->order_id = $entry_id;
$payment->order_id = FormidableFormsHelper::get_order_id( $this->action, $form_id, $entry, $entry_id );

$description = FormidableFormsHelper::get_description( $this->action, $form_id, $entry, $entry_id );

Expand Down
41 changes: 41 additions & 0 deletions src/FormidableFormsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,47 @@ public static function get_currency_from_settings() {
return $currency;
}

/**
* Get order ID.
*
* @param unknown $action Action.
* @param int $form_id Form ID.
* @param unknown $entry Entry.
* @param int $entry_id Entry ID.
* @return string
*/
public static function get_order_id( $action, $form_id, $entry, $entry_id ) {
if ( ! \array_key_exists( 'pronamic_pay_order_id', $action->post_content ) ) {
return $entry_id;
}

// Order ID template.
$order_id_template = $action->post_content['pronamic_pay_order_id'];

/**
* Find shortcode.
*
* @link https://github.com/wp-premium/formidable/blob/2.0.22/classes/helpers/FrmFieldsHelper.php#L684-L696
*/
$shortcodes = FrmFieldsHelper::get_shortcodes( $order_id_template, $form_id );

/**
* Replace shortcodes.
*
* @link https://github.com/wp-premium/formidable/blob/2.0.22/classes/helpers/FrmFieldsHelper.php#L715-L821
*/
$order_id = FrmFieldsHelper::replace_content_shortcodes( $order_id_template, $entry, $shortcodes );

/**
* Fallback to entry ID.
*/
if ( '' === $order_id ) {
$order_id = (string) $entry_id;
}

return $order_id;
}

/**
* Get description.
*
Expand Down
2 changes: 2 additions & 0 deletions src/PaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function __construct() {
public function form( $instance, $args = [] ) {
$form_fields = $this->get_field_options( $args['form']->id );

$payment_action = $this;

include __DIR__ . '/../views/payment-settings.php';
}

Expand Down
61 changes: 35 additions & 26 deletions views/payment-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,28 @@
* @since 1.0.0
*/

$callback_text_field = function ( $field ) use ( $instance, $payment_action ) {
$id = $field['id'];

$current = '';

if ( \array_key_exists( $id, $instance->post_content ) ) {
$current = $instance->post_content[ $id ];
}

printf(
'<input type="text" name="%s" value="%s" class="large-text frm_help" title="" data-original-title="%s" />',
esc_attr( $payment_action->get_field_name( $id ) ),
esc_attr( $current ),
esc_attr( $field['description'] )
);
};

$fields = [
[
'id' => 'pronamic_pay_amount_field',
'label' => __( 'Amount', 'pronamic_ideal' ),
'callback' => function ( $field ) use ( $form_fields, $instance ) {
'callback' => function ( $field ) use ( $form_fields, $instance, $payment_action ) {
$id = $field['id'];

$current = '';
Expand All @@ -31,7 +48,7 @@

printf(
'<select name="%s">',
esc_attr( $this->get_field_name( $id ) )
esc_attr( $payment_action->get_field_name( $id ) )
);

$options = [
Expand All @@ -57,7 +74,7 @@
[
'id' => 'pronamic_pay_payment_method_field',
'label' => __( 'Payment method', 'pronamic_ideal' ),
'callback' => function ( $field ) use ( $form_fields, $instance ) {
'callback' => function ( $field ) use ( $form_fields, $instance, $payment_action ) {
$id = $field['id'];

$current = '';
Expand All @@ -68,7 +85,7 @@

printf(
'<select name="%s">',
esc_attr( $this->get_field_name( $id ) )
esc_attr( $payment_action->get_field_name( $id ) )
);

$options = [
Expand Down Expand Up @@ -98,7 +115,7 @@
[
'id' => 'pronamic_pay_config_id',
'label' => __( 'Payment Gateway Configuration', 'pronamic_ideal' ),
'callback' => function ( $field ) use ( $instance ) {
'callback' => function ( $field ) use ( $instance, $payment_action ) {
$id = $field['id'];

$current = '';
Expand All @@ -109,7 +126,7 @@

\printf(
'<select name="%s">',
esc_attr( $this->get_field_name( $id ) )
esc_attr( $payment_action->get_field_name( $id ) )
);

$options = Plugin::get_config_select_options();
Expand All @@ -129,29 +146,21 @@
},
],
[
'id' => 'pronamic_pay_transaction_description',
'label' => __( 'Transaction Description', 'pronamic_ideal' ),
'callback' => function ( $field ) use ( $instance ) {
$id = $field['id'];

$current = '';

if ( \array_key_exists( $id, $instance->post_content ) ) {
$current = $instance->post_content[ $id ];
}

printf(
'<input type="text" name="%s" value="%s" class="large-text frm_help" title="" data-original-title="%s" />',
esc_attr( $this->get_field_name( $id ) ),
esc_attr( $current ),
esc_attr__( 'Enter a transaction description, you can use Formidable Forms shortcodes.', 'pronamic_ideal' )
);
},
'id' => 'pronamic_pay_order_id',
'label' => __( 'Order ID', 'pronamic_ideal' ),
'description' => __( 'Enter an order ID, you can use Formidable Forms shortcodes.', 'pronamic_ideal' ),
'callback' => $callback_text_field,
],
[
'id' => 'pronamic_pay_transaction_description',
'label' => __( 'Transaction Description', 'pronamic_ideal' ),
'description' => __( 'Enter a transaction description, you can use Formidable Forms shortcodes.', 'pronamic_ideal' ),
'callback' => $callback_text_field,
],
[
'id' => 'pronamic_pay_delay_notifications',
'label' => __( 'Notifications', 'pronamic_ideal' ),
'callback' => function ( $field ) use ( $instance ) {
'callback' => function ( $field ) use ( $instance, $payment_action ) {
$id = $field['id'];

$current = '';
Expand All @@ -162,7 +171,7 @@

printf(
'<input type="checkbox" name="%s" title="" %s /> %s',
esc_attr( $this->get_field_name( $id ) ),
esc_attr( $payment_action->get_field_name( $id ) ),
checked( $current, 'on', false ),
esc_attr__( 'Delay email notifications until payment has been received.', 'pronamic_ideal' )
);
Expand Down