Issues (54)

src/Admin/AdminTransactions.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Admin transactions
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Extensions\MemberPress
9
 */
10
11
namespace Pronamic\WordPress\Pay\Extensions\MemberPress\Admin;
12
13
use MeprTransactionsTable;
0 ignored issues
show
The type MeprTransactionsTable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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