Passed
Push — master ( 1c1fa7...25af54 )
by Remco
15:15 queued 10:30
created

Settings   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 49
dl 0
loc 111
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sections() 0 16 1
A fields() 0 37 1
A __construct() 0 3 1
A save_post() 0 17 4
1
<?php
2
/**
3
 * Mollie settings.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use Pronamic\WordPress\Pay\Core\GatewaySettings;
14
15
/**
16
 * Title: Mollie gateway settings
17
 * Description:
18
 * Copyright: Copyright (c) 2005 - 2018
19
 * Company: Pronamic
20
 *
21
 * @author  Remco Tolsma
22
 * @version 2.0.5
23
 * @since   1.0.0
24
 */
25
class Settings extends GatewaySettings {
26
	/**
27
	 * API key meta key.
28
	 *
29
	 * @var string
30
	 */
31
	const API_KEY_META_KEY = '_pronamic_gateway_mollie_api_key';
32
33
	/**
34
	 * Settings constructor.
35
	 */
36
	public function __construct() {
37
		add_filter( 'pronamic_pay_gateway_sections', array( $this, 'sections' ) );
38
		add_filter( 'pronamic_pay_gateway_fields', array( $this, 'fields' ) );
39
	}
40
41
	/**
42
	 * Settings sections.
43
	 *
44
	 * @param array $sections Sections.
45
	 *
46
	 * @return array
47
	 */
48
	public function sections( array $sections ) {
49
		// General.
50
		$sections['mollie'] = array(
51
			'title'       => __( 'Mollie', 'pronamic_ideal' ),
52
			'methods'     => array( 'mollie' ),
53
			'description' => __( 'Account details are provided by the payment provider after registration. These settings need to match with the payment provider dashboard.', 'pronamic_ideal' ),
54
		);
55
56
		// Transaction feedback.
57
		$sections['mollie_feedback'] = array(
58
			'title'       => __( 'Transaction feedback', 'pronamic_ideal' ),
59
			'methods'     => array( 'mollie' ),
60
			'description' => __( 'Payment status updates will be processed without any additional configuration. The <em>Webhook URL</em> is being used to receive the status updates.', 'pronamic_ideal' ),
61
		);
62
63
		return $sections;
64
	}
65
66
	/**
67
	 * Settings fields.
68
	 *
69
	 * @param array $fields Fields.
70
	 *
71
	 * @return array
72
	 */
73
	public function fields( array $fields ) {
74
		// API Key.
75
		$fields[] = array(
76
			'filter'   => FILTER_SANITIZE_STRING,
77
			'section'  => 'mollie',
78
			'meta_key' => self::API_KEY_META_KEY,
79
			'title'    => _x( 'API Key', 'mollie', 'pronamic_ideal' ),
80
			'type'     => 'text',
81
			'classes'  => array( 'regular-text', 'code' ),
82
			'methods'  => array( 'mollie' ),
83
			'tooltip'  => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ),
84
		);
85
86
		// Transaction feedback.
87
		$fields[] = array(
88
			'section' => 'mollie',
89
			'title'   => __( 'Transaction feedback', 'pronamic_ideal' ),
90
			'type'    => 'description',
91
			'html'    => sprintf(
92
				'<span class="dashicons dashicons-yes"></span> %s',
93
				__( 'Payment status updates will be processed without any additional configuration.', 'pronamic_ideal' )
94
			),
95
		);
96
97
		// Webhook.
98
		$fields[] = array(
99
			'section'  => 'mollie_feedback',
100
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
101
			'type'     => 'text',
102
			'classes'  => array( 'large-text', 'code' ),
103
			'value'    => add_query_arg( 'mollie_webhook', '', home_url( '/' ) ),
104
			'readonly' => true,
105
			'methods'  => array( 'mollie' ),
106
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
107
		);
108
109
		return $fields;
110
	}
111
112
	/**
113
	 * Save post.
114
	 *
115
	 * @param array $data Data to save.
116
	 *
117
	 * @return array
118
	 */
119
	public function save_post( $data ) {
120
		// Set mode based on API key.
121
		if ( isset( $data[ self::API_KEY_META_KEY ], $data['_pronamic_gateway_mode'] ) ) {
122
			$api_key = trim( $data[ self::API_KEY_META_KEY ] );
123
124
			if ( empty( $api_key ) ) {
125
				$mode = $data['_pronamic_gateway_mode'];
126
			} elseif ( 'live_' === substr( $api_key, 0, 5 ) ) {
127
				$mode = Gateway::MODE_LIVE;
128
			} else {
129
				$mode = Gateway::MODE_TEST;
130
			}
131
132
			$data['_pronamic_gateway_mode'] = $mode;
133
		}
134
135
		return $data;
136
	}
137
}
138