-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdminTransactions.php
230 lines (192 loc) · 5.26 KB
/
AdminTransactions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Admin transactions
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2021 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay\Extensions\MemberPress
*/
namespace Pronamic\WordPress\Pay\Extensions\MemberPress\Admin;
use MeprTransactionsTable;
use WP_Post;
use WP_Query;
/**
* Admin transactions
*
* @author Remco Tolsma
* @version 3.1.0
* @since 1.0.0
*/
class AdminTransactions {
/**
* Payments map.
*
* @var array<string, WP_Post>
*/
private $payments_map;
/**
* Construct admin transactions.
*/
public function __construct() {
$this->payments_map = array();
}
/**
* Setup.
*
* @return void
*/
public function setup() {
/**
* Filter for transactions columns.
*
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprTransactionsCtrl.php#L18-L22
*/
$hook = 'memberpress_page_memberpress-trans';
\add_filter( 'manage_' . $hook . '_columns', array( $this, 'manage_transactions_columns' ), 15 );
/**
* MemberPress admin transactions cell.
*
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/views/admin/transactions/row.php#L196-L198
*/
\add_action( 'mepr-admin-transactions-cell', array( $this, 'admin_transactions_cell' ), 10, 3 );
/**
* Load payments maps.
*
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprView.php#L23-L66
*/
\add_filter( 'mepr_view_paths_get_string', array( $this, 'maybe_load_payments_map' ), 10, 3 );
/**
* Extend transaction form.
*/
\add_filter( 'mepr_view_get_string', array( $this, 'extend_transaction_form' ), 10, 3 );
}
/**
* Manage transactions columns.
*
* @param array<string, string> $columns Columns.
* @return array<string, string>
*/
public function manage_transactions_columns( $columns ) {
$columns['pronamic_payment'] = __( 'Pronamic Payment', 'pronamic_ideal' );
return $columns;
}
/**
* Admin transaction cell.
*
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/views/admin/transactions/row.php#L196-L198
*
* @param string $column_name Column name.
* @param object $rec Record.
* @param string $attributes Attributes.
* @return void
*/
public function admin_transactions_cell( $column_name, $rec, $attributes ) {
if ( 'pronamic_payment' !== $column_name ) {
return;
}
if ( ! \property_exists( $rec, 'id' ) ) {
return;
}
printf(
'<td %s>',
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$attributes
);
$memberpress_transaction_id = $rec->id;
if ( \array_key_exists( $memberpress_transaction_id, $this->payments_map ) ) {
$pronamic_payment_post = $this->payments_map[ $memberpress_transaction_id ];
\printf(
'<a href="%s">%s</a>',
\esc_attr( (string) \get_edit_post_link( $pronamic_payment_post ) ),
\esc_html( (string) $pronamic_payment_post->ID )
);
} else {
echo '—';
}
echo '</td>';
}
/**
* Maybe load payments map.
*
* @param string[] $paths Paths.
* @param string $slug Slug.
* @param mixed[] $vars Variables.
* @return string[]
*/
public function maybe_load_payments_map( $paths, $slug, $vars ) {
if ( '/admin/transactions/list' !== $slug ) {
return $paths;
}
if ( ! \array_key_exists( 'list_table', $vars ) ) {
return $paths;
}
$list_table = $vars['list_table'];
if ( ! $list_table instanceof MeprTransactionsTable ) {
return $paths;
}
$memberpress_transaction_ids = \wp_list_pluck( $list_table->items, 'id' );
$query = new WP_Query(
array(
'post_type' => 'pronamic_payment',
'post_status' => 'any',
'nopaging' => true,
'meta_query' => array(
array(
'key' => '_pronamic_payment_source',
'compare' => '=',
'value' => 'memberpress_transaction',
),
array(
'key' => '_pronamic_payment_source_id',
'compare' => 'IN',
'value' => $memberpress_transaction_ids,
),
),
)
);
$payment_posts = array_filter(
$query->posts,
function( $post ) {
return $post instanceof WP_Post;
}
);
foreach ( $payment_posts as $payment_post ) {
$memberpress_transaction_id = (string) \get_post_meta( $payment_post->ID, '_pronamic_payment_source_id', true );
$this->payments_map[ $memberpress_transaction_id ] = $payment_post;
}
return $paths;
}
/**
* Extend transaction form.
*
* @param string $view View.
* @param string $slug Slug.
* @param mixed[] $vars Variables.
* @return string
*/
public function extend_transaction_form( $view, $slug, $vars ) {
if ( '/admin/transactions/trans_form' !== $slug ) {
return $view;
}
if ( ! array_key_exists( 'txn', $vars ) ) {
return $view;
}
$memberpress_transaction = $vars['txn'];
/*
* Check if variable is a object, should be instance of `MeprTransaction`.
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprTransaction.php
*/
if ( ! is_object( $memberpress_transaction ) ) {
return $view;
}
if ( ! isset( $memberpress_transaction->id ) ) {
return $view;
}
$memberpress_transaction_id = $memberpress_transaction->id;
ob_start();
include dirname( __FILE__ ) . '/../../views/transaction-form.php';
$view .= ob_get_clean();
return $view;
}
}