Passed
Push — develop ( ebd8d2...570278 )
by Remco
07:21
created

PaymentsModule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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
	 * Construct and initialize a payments module object.
38
	 *
39
	 * @param Plugin $plugin The plugin.
40
	 */
41
	public function __construct( Plugin $plugin ) {
42
		$this->plugin = $plugin;
43
44
		// Exclude payment notes.
45
		add_filter( 'comments_clauses', array( $this, 'exclude_payment_comment_notes' ), 10, 2 );
46
47
		// Payment redirect URL.
48
		add_filter( 'pronamic_payment_redirect_url', array( $this, 'payment_redirect_url' ), 5, 2 );
49
50
		// Listen to payment status changes so we can log these in a note.
51
		add_action( 'pronamic_payment_status_update', array( $this, 'log_payment_status_update' ), 10, 4 );
52
53
		// Payment Status Checker.
54
		$this->status_checker = new StatusChecker();
0 ignored issues
show
Bug Best Practice introduced by
The property status_checker does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55
	}
56
57
	/**
58
	 * Comments clauses.
59
	 *
60
	 * @param array            $clauses Array with query clauses for the comments query.
61
	 * @param WP_Comment_Query $query   A WordPress comment query object.
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\Payments\WP_Comment_Query was not found. Did you mean WP_Comment_Query? If so, make sure to prefix the type with \.
Loading history...
62
	 *
63
	 * @return array
64
	 */
65
	public function exclude_payment_comment_notes( $clauses, $query ) {
66
		$type = $query->query_vars['type'];
67
68
		// Ignore payment notes comments if it's not specifically requested.
69
		if ( 'payment_note' !== $type ) {
70
			$clauses['where'] .= " AND comment_type != 'payment_note'";
71
		}
72
73
		return $clauses;
74
	}
75
76
	/**
77
	 * Payment redirect URL filter.
78
	 *
79
	 * @param string  $url     A payment redirect URL.
80
	 * @param Payment $payment The payment to get a redirect URL for.
81
	 *
82
	 * @return string
83
	 */
84
	public function payment_redirect_url( $url, $payment ) {
85
		$page_id = null;
86
87
		switch ( $payment->status ) {
88
			case Statuses::CANCELLED:
89
				$page_id = pronamic_pay_get_page_id( 'cancel' );
90
91
				break;
92
			case Statuses::EXPIRED:
93
				$page_id = pronamic_pay_get_page_id( 'expired' );
94
95
				break;
96
			case Statuses::FAILURE:
97
				$page_id = pronamic_pay_get_page_id( 'error' );
98
99
				break;
100
			case Statuses::OPEN:
101
				$page_id = pronamic_pay_get_page_id( 'unknown' );
102
103
				break;
104
			case Statuses::SUCCESS:
105
				$page_id = pronamic_pay_get_page_id( 'completed' );
106
107
				break;
108
			default:
109
				$page_id = pronamic_pay_get_page_id( 'unknown' );
110
111
				break;
112
		}
113
114
		if ( ! empty( $page_id ) ) {
115
			$page_url = get_permalink( $page_id );
116
117
			if ( false !== $page_url ) {
118
				$url = $page_url;
119
			}
120
		}
121
122
		return $url;
123
	}
124
125
	/**
126
	 * Payment status update.
127
	 *
128
	 * @param Payment $payment The status updated payment.
129
	 */
130
	public function log_payment_status_update( $payment, $can_redirect, $old_status, $new_status ) {
131
		$payment->add_note( sprintf(
132
			__( 'Payment status changed from "%1$s" to "%2$s".', 'pronamic_ideal' ),
133
			esc_html( $this->plugin->payments_data_store->get_meta_status_label( $old_status ) ),
134
			esc_html( $this->plugin->payments_data_store->get_meta_status_label( $new_status ) )
135
		) );
136
	}
137
}
138