actions-abstract
2 years ago
assets
1 year ago
db-models
2 years ago
export
2 years ago
legacy
2 years ago
meta-boxes
2 years ago
pages
2 years ago
paypal
2 years ago
query-views
2 years ago
rest-api
2 years ago
scenarios-abstract
2 years ago
tab-handlers
2 years ago
table-views
2 years ago
base-gateway-action.php
1 year ago
base-gateway.php
1 year ago
base-scenario-gateway.php
2 years ago
gateways-editor-data.php
2 years ago
legacy-base-gateway.php
2 years ago
migrate-legacy-data.php
2 years ago
module.php
1 year ago
scenario-item.php
2 years ago
gateways-editor-data.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Gateways; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | trait Gateways_Editor_Data { |
| 12 | |
| 13 | private function labels() { |
| 14 | return array_merge( |
| 15 | array( |
| 16 | 'notifications_before' => _x( 'Before payment processed:', 'Gateways editor data', 'jet-form-builder' ), |
| 17 | 'notifications_success' => _x( 'On successful payment:', 'Gateways editor data', 'jet-form-builder' ), |
| 18 | 'notifications_failed' => _x( 'On failed payment:', 'Gateways editor data', 'jet-form-builder' ), |
| 19 | 'price_field' => _x( 'Price/amount field', 'Gateways editor data', 'jet-form-builder' ), |
| 20 | 'message_success' => _x( 'Payment success message', 'Gateways editor data', 'jet-form-builder' ), |
| 21 | 'message_failed' => _x( 'Payment failed message', 'Gateways editor data', 'jet-form-builder' ), |
| 22 | 'use_success_redirect' => _x( 'Redirect to a page', 'Gateways editor data', 'jet-form-builder' ), |
| 23 | 'action_order' => _x( 'Create payment order notification:', 'Gateways editor data', 'jet-form-builder' ), |
| 24 | 'use_success_redirect_help' => _x( |
| 25 | 'Enable this toggle to redirect a user after successful payment.', |
| 26 | 'Gateways editor data', |
| 27 | 'jet-form-builder' |
| 28 | ), |
| 29 | ), |
| 30 | $this->options_labels(), |
| 31 | $this->custom_labels() |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | public function default_messages() { |
| 36 | return array( |
| 37 | 'success' => 'Payment success message', |
| 38 | 'failed' => 'Payment failed message', |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | private function options_labels() { |
| 43 | $result = array(); |
| 44 | |
| 45 | foreach ( $this->rep_get_items() as $gateway ) { |
| 46 | /** @var Base_Gateway $gateway */ |
| 47 | $result[ $gateway->get_id() ] = $gateway->options( 'label' ); |
| 48 | } |
| 49 | |
| 50 | return $result; |
| 51 | } |
| 52 | |
| 53 | private function custom_labels() { |
| 54 | $result = array(); |
| 55 | |
| 56 | foreach ( $this->rep_get_items() as $gateway ) { |
| 57 | /** @var Base_Gateway $gateway */ |
| 58 | $result = array_merge( |
| 59 | $result, |
| 60 | $this->join_keys( |
| 61 | $gateway->custom_labels(), |
| 62 | $gateway->get_id() |
| 63 | ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return $result; |
| 68 | } |
| 69 | |
| 70 | private function join_keys( $source, $prefix ): array { |
| 71 | $custom_labels = array(); |
| 72 | |
| 73 | foreach ( $source as $key => $label ) { |
| 74 | $computed_key = "{$prefix}.{$key}"; |
| 75 | |
| 76 | if ( is_string( $label ) ) { |
| 77 | $custom_labels[ $computed_key ] = $label; |
| 78 | continue; |
| 79 | } |
| 80 | if ( is_array( $label ) ) { |
| 81 | $custom_labels = array_merge( $custom_labels, $this->join_keys( $label, $computed_key ) ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return $custom_labels; |
| 86 | } |
| 87 | |
| 88 | private function gateways_additional() { |
| 89 | $result = array(); |
| 90 | |
| 91 | foreach ( $this->rep_get_items() as $gateway ) { |
| 92 | /** @var Base_Gateway $gateway */ |
| 93 | $result[ $gateway->get_id() ] = $gateway->additional_editor_data(); |
| 94 | } |
| 95 | |
| 96 | return $result; |
| 97 | } |
| 98 | |
| 99 | private function gateways_for_js() { |
| 100 | $result = array(); |
| 101 | |
| 102 | foreach ( $this->rep_get_items() as $gateway ) { |
| 103 | $result[] = array( |
| 104 | 'value' => $gateway->get_id(), |
| 105 | 'label' => $gateway->get_name(), |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | return $result; |
| 110 | } |
| 111 | |
| 112 | public function editor_data() { |
| 113 | $result = array( |
| 114 | 'allowed' => true, |
| 115 | 'labels' => $this->labels(), |
| 116 | 'list' => $this->gateways_for_js(), |
| 117 | 'messages' => $this->default_messages(), |
| 118 | 'additional' => $this->gateways_additional(), |
| 119 | ); |
| 120 | |
| 121 | return apply_filters( 'jet-form-builder/gateways/editor-data', $result ); |
| 122 | } |
| 123 | |
| 124 | } |
| 125 |