1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Mollie profile data store. |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2020 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Mollie; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Title: Mollie profile data store |
15
|
|
|
* Description: |
16
|
|
|
* Copyright: 2005-2020 Pronamic |
17
|
|
|
* Company: Pronamic |
18
|
|
|
* |
19
|
|
|
* @author Remco Tolsma |
20
|
|
|
* @version 2.1.0 |
21
|
|
|
* @since 2.1.0 |
22
|
|
|
*/ |
23
|
|
|
class ProfileDataStore { |
24
|
|
|
/** |
25
|
|
|
* Get or insert profile. |
26
|
|
|
* |
27
|
|
|
* @param Profile $profile Profile. |
28
|
|
|
* @param array<string> $data Data. |
29
|
|
|
* @param array<string> $format Format. |
30
|
|
|
* @return int |
31
|
|
|
*/ |
32
|
|
|
public function get_or_insert_profile( Profile $profile, $data = array(), $format = array() ) { |
33
|
|
|
$profile_data = $this->get_profile( $profile ); |
34
|
|
|
|
35
|
|
|
if ( null !== $profile_data ) { |
36
|
|
|
return $profile_data->id; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->insert_profile( $profile, $data, $format ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get profile data for specified Mollie profile. |
44
|
|
|
* |
45
|
|
|
* @param Profile $profile Profile. |
46
|
|
|
* @return object|null |
47
|
|
|
*/ |
48
|
|
|
public function get_profile( Profile $profile ) { |
49
|
|
|
global $wpdb; |
50
|
|
|
|
51
|
|
|
$id = $profile->get_id(); |
52
|
|
|
|
53
|
|
|
if ( null === $id ) { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$data = $wpdb->get_row( |
58
|
|
|
$wpdb->prepare( |
59
|
|
|
" |
60
|
|
|
SELECT |
61
|
|
|
* |
62
|
|
|
FROM |
63
|
|
|
$wpdb->pronamic_pay_mollie_profiles |
64
|
|
|
WHERE |
65
|
|
|
mollie_id = %s |
66
|
|
|
LIMIT |
67
|
|
|
1 |
68
|
|
|
; |
69
|
|
|
", |
70
|
|
|
$id |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
return $data; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Insert Mollie profile. |
79
|
|
|
* |
80
|
|
|
* @param Profile $profile Profile. |
81
|
|
|
* @param array<string> $data Data. |
82
|
|
|
* @param array<string> $format Format. |
83
|
|
|
* @return int |
84
|
|
|
* @throws \Exception Throws exception on error. |
85
|
|
|
*/ |
86
|
|
|
public function insert_profile( Profile $profile, $data = array(), $format = array() ) { |
87
|
|
|
global $wpdb; |
88
|
|
|
|
89
|
|
|
$mollie_id = $profile->get_id(); |
90
|
|
|
|
91
|
|
|
if ( empty( $mollie_id ) ) { |
92
|
|
|
throw new \Exception( 'Can not insert Mollie profile with empty ID.' ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$data['mollie_id'] = $mollie_id; |
96
|
|
|
$format['mollie_id'] = '%s'; |
97
|
|
|
|
98
|
|
|
$data['email'] = $profile->get_email(); |
99
|
|
|
$format['email'] = '%s'; |
100
|
|
|
|
101
|
|
|
$data['name'] = $profile->get_name(); |
102
|
|
|
$format['name'] = '%s'; |
103
|
|
|
|
104
|
|
|
$result = $wpdb->insert( |
105
|
|
|
$wpdb->pronamic_pay_mollie_profiles, |
106
|
|
|
$data, |
107
|
|
|
$format |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
if ( false === $result ) { |
111
|
|
|
throw new \Exception( |
112
|
|
|
sprintf( |
113
|
|
|
'Could not insert Mollie profile ID: %s, error: %s.', |
114
|
|
|
$mollie_id, |
115
|
|
|
$wpdb->last_error |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$id = $wpdb->insert_id; |
121
|
|
|
|
122
|
|
|
return $id; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Update Mollie profile. |
127
|
|
|
* |
128
|
|
|
* @param Profile $profile Profile. |
129
|
|
|
* @param array<string> $data Data. |
130
|
|
|
* @param array<string> $format Format. |
131
|
|
|
* @return int The number of rows updated. |
132
|
|
|
* @throws \Exception Throws exception on error. |
133
|
|
|
*/ |
134
|
|
|
public function update_profile( Profile $profile, $data = array(), $format = array() ) { |
135
|
|
|
global $wpdb; |
136
|
|
|
|
137
|
|
|
$mollie_id = $profile->get_id(); |
138
|
|
|
|
139
|
|
|
if ( empty( $mollie_id ) ) { |
140
|
|
|
throw new \Exception( 'Can not update Mollie profile with empty ID.' ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$data['email'] = $profile->get_email(); |
144
|
|
|
$format['email'] = '%s'; |
145
|
|
|
|
146
|
|
|
$data['name'] = $profile->get_name(); |
147
|
|
|
$format['name'] = '%s'; |
148
|
|
|
|
149
|
|
|
$result = $wpdb->update( |
150
|
|
|
$wpdb->pronamic_pay_mollie_profiles, |
151
|
|
|
$data, |
152
|
|
|
array( |
153
|
|
|
'mollie_id' => $mollie_id, |
154
|
|
|
), |
155
|
|
|
$format, |
156
|
|
|
array( |
157
|
|
|
'mollie_id' => '%s', |
158
|
|
|
) |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
if ( false === $result ) { |
162
|
|
|
throw new \Exception( |
163
|
|
|
sprintf( |
164
|
|
|
'Could not update Mollie profile ID: %s, error: %s.', |
165
|
|
|
$mollie_id, |
166
|
|
|
$wpdb->last_error |
167
|
|
|
) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $result; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Save Mollie profile. |
176
|
|
|
* |
177
|
|
|
* @param Profile $profile Profile. |
178
|
|
|
* @param array<string> $data Data. |
179
|
|
|
* @param array<string> $format Format. |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
|
|
public function save_profile( Profile $profile, $data = array(), $format = array() ) { |
183
|
|
|
$profile_data = $this->get_profile( $profile ); |
184
|
|
|
|
185
|
|
|
if ( null !== $profile_data ) { |
186
|
|
|
$this->update_profile( $profile, $data, $format ); |
187
|
|
|
|
188
|
|
|
return $profile_data->id; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->insert_profile( $profile, $data, $format ); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|