Passed
Push — develop ( 8ebc76...5a0ded )
by Reüel
05:00 queued 17s
created

PaymentsModule::privacy_export()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 50
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 26
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 50
rs 8.6315
1
<?php
2
/**
3
 * Payments Module
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Subscriptions
9
 */
10
11
namespace Pronamic\WordPress\Pay\Payments;
12
13
use Pronamic\WordPress\Pay\Plugin;
14
use Pronamic\WordPress\Pay\Core\Statuses;
15
16
/**
17
 * Title: Payments module
18
 * Description:
19
 * Copyright: Copyright (c) 2005 - 2018
20
 * Company: Pronamic
21
 *
22
 * @see https://woocommerce.com/2017/04/woocommerce-3-0-release/
23
 * @see https://woocommerce.wordpress.com/2016/10/27/the-new-crud-classes-in-woocommerce-2-7/
24
 * @author Remco Tolsma
25
 * @version 3.7.0
26
 * @since 3.7.0
27
 */
28
class PaymentsModule {
29
	/**
30
	 * Plugin.
31
	 *
32
	 * @var Plugin $plugin
33
	 */
34
	public $plugin;
35
36
	/**
37
	 * Free payments to complete at shutdown.
38
	 *
39
	 * @var array
40
	 */
41
	public $free = array();
42
43
	/**
44
	 * Construct and initialize a payments module object.
45
	 *
46
	 * @param Plugin $plugin The plugin.
47
	 */
48
	public function __construct( Plugin $plugin ) {
49
		$this->plugin = $plugin;
50
51
		// Exclude payment notes.
52
		add_filter( 'comments_clauses', array( $this, 'exclude_payment_comment_notes' ), 10, 2 );
53
54
		// Payment redirect URL.
55
		add_filter( 'pronamic_payment_redirect_url', array( $this, 'payment_redirect_url' ), 5, 2 );
56
57
		// Listen to payment status changes so we can log these in a note.
58
		add_action( 'pronamic_payment_status_update', array( $this, 'log_payment_status_update' ), 10, 4 );
59
60
		// Shutdown.
61
		add_action( 'shutdown', array( $this, 'update_free_payments' ) );
62
63
		// Payment Status Checker.
64
		$status_checker = new StatusChecker();
65
66
		// The 'pronamic_ideal_check_transaction_status' hook is scheduled to request the payment status.
67
		add_action( 'pronamic_ideal_check_transaction_status', array( $status_checker, 'check_status' ), 10, 3 );
68
69
		// Privacy personal data exporter.
70
		add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_privacy_exporter' ), 10 );
71
	}
72
73
	/**
74
	 * Comments clauses.
75
	 *
76
	 * @param array             $clauses Array with query clauses for the comments query.
77
	 * @param \WP_Comment_Query $query   A WordPress comment query object.
78
	 *
79
	 * @return array
80
	 */
81
	public function exclude_payment_comment_notes( $clauses, $query ) {
82
		$type = $query->query_vars['type'];
83
84
		// Ignore payment notes comments if it's not specifically requested.
85
		if ( 'payment_note' !== $type ) {
86
			$clauses['where'] .= " AND comment_type != 'payment_note'";
87
		}
88
89
		return $clauses;
90
	}
91
92
	/**
93
	 * Payment redirect URL filter.
94
	 *
95
	 * @param string  $url     A payment redirect URL.
96
	 * @param Payment $payment The payment to get a redirect URL for.
97
	 *
98
	 * @return string
99
	 */
100
	public function payment_redirect_url( $url, $payment ) {
101
		$page_id = null;
102
103
		switch ( $payment->status ) {
104
			case Statuses::CANCELLED:
105
				$page_id = pronamic_pay_get_page_id( 'cancel' );
106
107
				break;
108
			case Statuses::EXPIRED:
109
				$page_id = pronamic_pay_get_page_id( 'expired' );
110
111
				break;
112
			case Statuses::FAILURE:
113
				$page_id = pronamic_pay_get_page_id( 'error' );
114
115
				break;
116
			case Statuses::OPEN:
117
				$page_id = pronamic_pay_get_page_id( 'unknown' );
118
119
				break;
120
			case Statuses::SUCCESS:
121
				$page_id = pronamic_pay_get_page_id( 'completed' );
122
123
				break;
124
			default:
125
				$page_id = pronamic_pay_get_page_id( 'unknown' );
126
127
				break;
128
		}
129
130
		if ( ! empty( $page_id ) ) {
131
			$page_url = get_permalink( $page_id );
132
133
			if ( false !== $page_url ) {
134
				$url = $page_url;
135
			}
136
		}
137
138
		return $url;
139
	}
140
141
	/**
142
	 * Payment status update.
143
	 *
144
	 * @param Payment $payment      The status updated payment.
145
	 * @param bool    $can_redirect Whether or not redirects should be performed.
146
	 * @param string  $old_status   Old meta status.
147
	 * @param string  $new_status   New meta status.
148
	 *
149
	 * @return void
150
	 */
151
	public function log_payment_status_update( $payment, $can_redirect, $old_status, $new_status ) {
152
		$note = sprintf(
153
			__( 'Payment status changed from "%1$s" to "%2$s".', 'pronamic_ideal' ),
154
			esc_html( $this->plugin->payments_data_store->get_meta_status_label( $old_status ) ),
155
			esc_html( $this->plugin->payments_data_store->get_meta_status_label( $new_status ) )
156
		);
157
158
		if ( null === $old_status ) {
0 ignored issues
show
introduced by
The condition null === $old_status is always false.
Loading history...
159
			$note = sprintf(
160
				__( 'Payment created with status "%1$s".', 'pronamic_ideal' ),
161
				esc_html( $this->plugin->payments_data_store->get_meta_status_label( $new_status ) )
162
			);
163
		}
164
165
		$payment->add_note( $note );
166
	}
167
168
	/**
169
	 * Update free payments.
170
	 */
171
	public function update_free_payments() {
172
		$can_redirect = false;
173
174
		foreach ( $this->free as $payment_id ) {
175
			$payment = get_pronamic_payment( $payment_id );
176
177
			Plugin::update_payment( $payment, $can_redirect );
178
		}
179
	}
180
181
	/**
182
	 * Register privacy personal data exporter.
183
	 *
184
	 * @param $exporters
185
	 *
186
	 * @return array
187
	 */
188
	public function register_privacy_exporter( $exporters ) {
189
		if ( ! is_array( $exporters ) ) {
190
			return $exporters;
191
		}
192
193
		$exporters['pronamic-pay-payments'] = array(
194
			'exporter_friendly_name' => __( 'Pronamic Pay', 'pronamic_ideal' ),
195
			'callback'               => array( $this, 'privacy_export' ),
196
		);
197
198
		return $exporters;
199
	}
200
201
	/**
202
	 * Privacy personal data exporter.
203
	 *
204
	 * @param string $email_address Email address.
205
	 * @param int    $page          Page.
206
	 *
207
	 * @return array
208
	 */
209
	public function privacy_export( $email_address, $page = 1 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

209
	public function privacy_export( $email_address, /** @scrutinizer ignore-unused */ $page = 1 ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
210
		$items = array();
211
212
		$meta_key_email = pronamic_pay_plugin()->payments_data_store->meta_key_prefix . 'email';
213
214
		// Get payments.
215
		$payments = get_pronamic_payments_by_meta( $meta_key_email, $email_address );
216
217
		foreach ( $payments as $payment ) {
218
			$data = array();
219
220
			// Get payment meta.
221
			$payment_meta = get_post_meta( $payment->get_id() );
222
223
			foreach ( $payment_meta as $meta_key => $meta_value ) {
224
				if ( '_pronamic_' !== substr( $meta_key, 0, 10 ) ) {
225
					continue;
226
				}
227
228
				// Format value.
229
				if ( 1 === count( $meta_value ) ) {
230
					$meta_value = array_shift( $meta_value );
231
				} else {
232
					$meta_value = wp_json_encode( $meta_value );
233
				}
234
235
				// Add meta to export data.
236
				$data[] = array(
237
					'name'  => $meta_key,
238
					'value' => $meta_value,
239
				);
240
			}
241
242
			// Add item to export data.
243
			if ( ! empty( $data ) ) {
244
				$items[] = array(
245
					'group_id'    => 'pronamic-payments',
246
					'group_label' => __( 'Payments', 'pronamic_ideal' ),
247
					'item_id'     => 'pronamic-payment-' . $payment->get_id(),
248
					'data'        => $data,
249
				);
250
			}
251
		}
252
253
		$done = true;
254
255
		// Return export data.
256
		return array(
257
			'data' => $items,
258
			'done' => $done,
259
		);
260
	}
261
}
262