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