Passed
Push — develop ( deb241...3d4e5e )
by Remco
04:25
created

AdminPaymentBulkActions   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 160
rs 10
c 0
b 0
f 0
wmc 22

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 20 3
C admin_notices() 0 45 8
A __construct() 0 2 1
C handle_bulk_action() 0 49 9
A bulk_actions() 0 8 1
1
<?php
2
/**
3
 * Payment Bulk Actions
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Admin
9
 */
10
11
namespace Pronamic\WordPress\Pay\Admin;
12
13
/**
14
 * WordPress admin payment bulk actions
15
 *
16
 * @see https://www.skyverge.com/blog/add-custom-bulk-action/
17
 * @author Remco Tolsma
18
 * @version 4.1.0
19
 * @since 4.1.0
20
 */
21
class AdminPaymentBulkActions {
22
	/**
23
	 * Constructs and initializes an admin payment bulk actions object.
24
	 */
25
	public function __construct() {
26
		add_action( 'load-edit.php', array( $this, 'load' ) );
27
	}
28
29
	/**
30
	 * Load.
31
	 */
32
	public function load() {
33
		// Current user.
34
		if ( ! current_user_can( 'edit_payments' ) ) {
35
			return;
36
		}
37
38
		// Screen.
39
		$screen = get_current_screen();
40
41
		if ( 'edit-pronamic_payment' !== $screen->id ) {
42
			return;
43
		}
44
45
		// Bulk actions.
46
		add_filter( 'bulk_actions-' . $screen->id, array( $this, 'bulk_actions' ) );
47
48
		add_filter( 'handle_bulk_actions-' . $screen->id, array( $this, 'handle_bulk_action' ), 10, 3 );
49
50
		// Admin notices.
51
		add_action( 'admin_notices', array( $this, 'admin_notices' ) );
52
	}
53
54
	/**
55
	 * Custom bulk actions.
56
	 *
57
	 * @see https://make.wordpress.org/core/2016/10/04/custom-bulk-actions/
58
	 * @see https://github.com/WordPress/WordPress/blob/4.7/wp-admin/includes/class-wp-list-table.php#L440-L452
59
	 * @param array $bulk_actions Bulk actions.
60
	 * @return array
61
	 */
62
	public function bulk_actions( $bulk_actions ) {
63
		// Don't allow edit in bulk.
64
		unset( $bulk_actions['edit'] );
65
66
		// Bulk check payment status.
67
		$bulk_actions['pronamic_payment_check_status'] = __( 'Check Payment Status', 'pronamic_ideal' );
68
69
		return $bulk_actions;
70
	}
71
72
	/**
73
	 * Handle bulk action.
74
	 *
75
	 * @see hhttps://make.wordpress.org/core/2016/10/04/custom-bulk-actions/
76
	 * @see https://github.com/WordPress/WordPress/blob/4.7/wp-admin/edit.php#L166-L167
77
	 * @param string $sendback Sendback URL.
78
	 * @param string $doaction Action indicator.
79
	 * @param array  $post_ids  Post ID's to bulk edit.
80
	 * @return string
81
	 */
82
	public function handle_bulk_action( $sendback, $doaction, $post_ids ) {
83
		if ( 'pronamic_payment_check_status' !== $doaction ) {
84
			return $sendback;
85
		}
86
87
		$status_updated       = 0;
88
		$skipped_check        = 0;
89
		$unsupported_gateways = array();
90
		$gateways             = array();
91
92
		foreach ( $post_ids as $post_id ) {
93
			$payment = get_pronamic_payment( $post_id );
94
95
			// Only check status for pending payments.
96
			if ( \Pronamic\WordPress\Pay\Core\Statuses::OPEN !== $payment->status && '' !== $payment->status ) {
97
				$skipped_check++;
98
99
				continue;
100
			}
101
102
			// Make sure gateway supports `payment_status_request` feature.
103
			$config_id = $payment->config_id;
104
105
			if ( ! isset( $gateways[ $config_id ] ) ) {
106
				$gateways[ $config_id ] = \Pronamic\WordPress\Pay\Plugin::get_gateway( $config_id );
107
108
				if ( $gateways[ $config_id ] && ! $gateways[ $config_id ]->supports( 'payment_status_request' ) ) {
109
					$unsupported_gateways[] = $config_id;
110
				}
111
			}
112
113
			if ( in_array( $config_id, $unsupported_gateways, true ) ) {
114
				continue;
115
			}
116
117
			\Pronamic\WordPress\Pay\Plugin::update_payment( $payment, false );
118
119
			$status_updated++;
120
		}
121
122
		$sendback = add_query_arg(
123
			array(
124
				'status_updated'       => $status_updated,
125
				'skipped_check'        => $skipped_check,
126
				'unsupported_gateways' => implode( ',', $unsupported_gateways ),
127
			), $sendback
128
		);
129
130
		return $sendback;
131
	}
132
133
	/**
134
	 * Admin notices.
135
	 */
136
	public function admin_notices() {
137
		if ( filter_has_var( INPUT_GET, 'status_updated' ) ) {
138
			$updated = filter_input( INPUT_GET, 'status_updated', FILTER_VALIDATE_INT );
139
140
			if ( $updated > 0 ) {
141
				$message = sprintf( _n( '%s payment updated.', '%s payments updated.', $updated, 'pronamic_ideal' ), number_format_i18n( $updated ) );
142
143
				printf(
144
					'<div class="notice notice-success"><p>%s</p></div>',
145
					esc_html( $message )
146
				);
147
			}
148
		}
149
150
		if ( filter_has_var( INPUT_GET, 'skipped_check' ) ) {
151
			$updated = filter_input( INPUT_GET, 'skipped_check', FILTER_VALIDATE_INT );
152
153
			if ( $updated > 0 ) {
154
				$message = sprintf(
155
					_n( '%s payment is not updated because it already has a final payment status.', '%s payments are not updated because they already have a final payment status.', $updated, 'pronamic_ideal' ),
156
					number_format_i18n( $updated )
157
				);
158
159
				printf(
160
					'<div class="notice notice-warning"><p>%s</p></div>',
161
					esc_html( $message )
162
				);
163
			}
164
		}
165
166
		if ( filter_has_var( INPUT_GET, 'unsupported_gateways' ) ) {
167
			$unsupported = filter_input( INPUT_GET, 'unsupported_gateways', FILTER_SANITIZE_STRING );
168
169
			if ( '' !== $unsupported ) {
170
				$gateways = explode( ',', $unsupported );
171
172
				foreach ( $gateways as $index => $config_id ) {
173
					$gateways[ $index ] = get_the_title( $config_id );
0 ignored issues
show
Bug introduced by
$config_id of type string is incompatible with the type integer|WP_Post expected by parameter $post of get_the_title(). ( Ignorable by Annotation )

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

173
					$gateways[ $index ] = get_the_title( /** @scrutinizer ignore-type */ $config_id );
Loading history...
174
				}
175
176
				$message = sprintf( __( 'Requesting the current payment status is unsupported by %s.', 'pronamic_ideal' ), implode( ', ', $gateways ) );
177
178
				printf(
179
					'<div class="notice notice-error"><p>%s</p></div>',
180
					esc_html( $message )
181
				);
182
			}
183
		}
184
	}
185
}
186