db-models
4 years ago
meta-boxes
4 years ago
pages
4 years ago
paypal
4 years ago
query-views
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
gateways-editor-data.php
120 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( 'Use redirect URL from Redirect notification', 'Gateways editor data', 'jet-form-builder' ), |
| 20 | 'action_order' => _x( 'Create payment order notification:', 'Gateways editor data', 'jet-form-builder' ), |
| 21 | ), |
| 22 | $this->options_labels(), |
| 23 | $this->custom_labels() |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | public function default_messages() { |
| 28 | return array( |
| 29 | 'success' => 'Payment success message', |
| 30 | 'failed' => 'Payment failed message', |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | private function options_labels() { |
| 35 | $result = array(); |
| 36 | |
| 37 | foreach ( $this->rep_get_items() as $gateway ) { |
| 38 | /** @var Base_Gateway $gateway */ |
| 39 | $result[ $gateway->get_id() ] = $gateway->options( 'label' ); |
| 40 | } |
| 41 | |
| 42 | return $result; |
| 43 | } |
| 44 | |
| 45 | private function custom_labels() { |
| 46 | $result = array(); |
| 47 | |
| 48 | foreach ( $this->rep_get_items() as $gateway ) { |
| 49 | /** @var Base_Gateway $gateway */ |
| 50 | $result = array_merge( |
| 51 | $result, |
| 52 | $this->join_keys( |
| 53 | $gateway->custom_labels(), |
| 54 | $gateway->get_id() |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | return $result; |
| 60 | } |
| 61 | |
| 62 | private function join_keys( $source, $prefix ): array { |
| 63 | $custom_labels = array(); |
| 64 | |
| 65 | foreach ( $source as $key => $label ) { |
| 66 | $computed_key = "{$prefix}.{$key}"; |
| 67 | |
| 68 | if ( is_string( $label ) ) { |
| 69 | $custom_labels[ $computed_key ] = $label; |
| 70 | continue; |
| 71 | } |
| 72 | if ( is_array( $label ) ) { |
| 73 | $custom_labels = array_merge( $custom_labels, $this->join_keys( $label, $computed_key ) ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return $custom_labels; |
| 78 | } |
| 79 | |
| 80 | private function gateways_additional() { |
| 81 | $result = array(); |
| 82 | |
| 83 | foreach ( $this->rep_get_items() as $gateway ) { |
| 84 | /** @var Base_Gateway $gateway */ |
| 85 | $result[ $gateway->get_id() ] = $gateway->additional_editor_data(); |
| 86 | } |
| 87 | |
| 88 | return $result; |
| 89 | } |
| 90 | |
| 91 | private function gateways_for_js() { |
| 92 | $result = array(); |
| 93 | |
| 94 | foreach ( $this->rep_get_items() as $gateway ) { |
| 95 | $result[] = array( |
| 96 | 'value' => $gateway->get_id(), |
| 97 | 'label' => $gateway->get_name(), |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | return $result; |
| 102 | } |
| 103 | |
| 104 | public function editor_data() { |
| 105 | $result = array( |
| 106 | 'allowed' => Plugin::instance()->allow_gateways, |
| 107 | ); |
| 108 | |
| 109 | if ( $result['allowed'] ) { |
| 110 | $result['labels'] = $this->labels(); |
| 111 | $result['list'] = $this->gateways_for_js(); |
| 112 | $result['messages'] = $this->default_messages(); |
| 113 | $result['additional'] = $this->gateways_additional(); |
| 114 | } |
| 115 | |
| 116 | return apply_filters( 'jet-form-builder/gateways/editor-data', $result ); |
| 117 | } |
| 118 | |
| 119 | } |
| 120 |