Issues (54)

src/Upgrade310.php (11 issues)

1
<?php
2
/**
3
 * Upgrade 3.1.0
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Upgrades
9
 */
10
11
namespace Pronamic\WordPress\Pay\Extensions\MemberPress;
12
13
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionStatus;
14
use Pronamic\WordPress\Pay\Upgrades\Upgrade;
15
use WP_Post;
16
use WP_Query;
17
18
/**
19
 * Upgrade 3.1.0
20
 *
21
 * @author  Remco Tolsma
22
 * @version 3.1.0
23
 * @since   3.1.0
24
 */
25
class Upgrade310 extends Upgrade {
26
	/**
27
	 * Construct 2.1.6 upgrade.
28
	 */
29
	public function __construct() {
30
		parent::__construct( '3.1.0' );
31
32
		if ( \defined( 'WP_CLI' ) && WP_CLI ) {
33
			$this->cli_init();
34
		}
35
	}
36
37
	/**
38
	 * Execute.
39
	 *
40
	 * @return void
41
	 */
42
	public function execute() {
43
		$this->upgrade_subscriptions();
44
		$this->upgrade_payments();
45
	}
46
47
	/**
48
	 * WP-CLI initialize.
49
	 *
50
	 * @link https://github.com/wp-cli/wp-cli/issues/4818
51
	 * @return void
52
	 */
53
	public function cli_init() {
54
		\WP_CLI::add_command(
55
			'pronamic-pay memberpress upgrade-310 execute',
56
			function( $args, $assoc_args ) {
0 ignored issues
show
The parameter $assoc_args 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

56
			function( $args, /** @scrutinizer ignore-unused */ $assoc_args ) {

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...
The parameter $args 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

56
			function( /** @scrutinizer ignore-unused */ $args, $assoc_args ) {

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...
57
				\WP_CLI::log( 'Upgrade 3.1.0' );
58
59
				$this->execute();
60
			},
61
			array(
62
				'shortdesc' => 'Execute MemberPress upgrade 3.1.0.',
63
			)
64
		);
65
66
		\WP_CLI::add_command(
67
			'pronamic-pay memberpress upgrade-310 list-subscriptions',
68
			function( $args, $assoc_args ) {
0 ignored issues
show
The parameter $assoc_args 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

68
			function( $args, /** @scrutinizer ignore-unused */ $assoc_args ) {

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...
The parameter $args 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

68
			function( /** @scrutinizer ignore-unused */ $args, $assoc_args ) {

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...
69
				\WP_CLI::log( 'Upgrade 3.1.0 - Subscriptions List' );
70
71
				$posts = $this->get_subscription_posts();
72
73
				\WP_CLI\Utils\format_items( 'table', $posts, array( 'ID', 'post_title', 'post_status' ) );
74
			},
75
			array(
76
				'shortdesc' => 'Execute MemberPress upgrade 3.1.0.',
77
			)
78
		);
79
80
		\WP_CLI::add_command(
81
			'pronamic-pay memberpress upgrade-310 upgrade-subscriptions',
82
			function( $args, $assoc_args ) {
83
				\WP_CLI::log( 'Upgrade 3.1.0 - Subscriptions' );
84
85
				$this->upgrade_subscriptions(
86
					array(
87
						'skip-no-match' => \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-no-match', true ),
88
						'reactivate'    => \WP_CLI\Utils\get_flag_value( $assoc_args, 'reactivate', true ),
89
						'dry-run'       => \WP_CLI\Utils\get_flag_value( $assoc_args, 'dry-run', true ),
90
						'post__in'      => \WP_CLI\Utils\get_flag_value( $assoc_args, 'post__in', null ),
91
					)
92
				);
93
			},
94
			array(
95
				'shortdesc' => 'Execute MemberPress upgrade 2.1.6.',
96
			)
97
		);
98
99
		\WP_CLI::add_command(
100
			'pronamic-pay memberpress upgrade-310 list-payments',
101
			function( $args, $assoc_args ) {
0 ignored issues
show
The parameter $args 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

101
			function( /** @scrutinizer ignore-unused */ $args, $assoc_args ) {

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...
The parameter $assoc_args 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

101
			function( $args, /** @scrutinizer ignore-unused */ $assoc_args ) {

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...
102
				\WP_CLI::log( 'Upgrade 3.1.0 - Payments List' );
103
104
				$posts = $this->get_payment_posts();
105
106
				\WP_CLI\Utils\format_items( 'table', $posts, array( 'ID', 'post_title', 'post_status' ) );
107
			},
108
			array(
109
				'shortdesc' => 'Execute MemberPress upgrade 2.1.6.',
110
			)
111
		);
112
	}
113
114
	/**
115
	 * Get subscription posts to upgrade.
116
	 *
117
	 * @param array<string, mixed> $args Query arguments.
118
	 * @return WP_Post[]
119
	 */
120
	private function get_subscription_posts( $args = array() ) {
121
		$args['post_type']     = 'pronamic_pay_subscr';
122
		$args['post_status']   = 'any';
123
		$args['nopaging']      = true;
124
		$args['no_found_rows'] = true;
125
		$args['order']         = 'DESC';
126
		$args['orderby']       = 'ID';
127
		$args['meta_query']    = array(
128
			array(
129
				'key'   => '_pronamic_subscription_source',
130
				'value' => 'memberpress',
131
			),
132
		);
133
134
		$query = new WP_Query( $args );
135
136
		return array_filter(
137
			$query->posts,
138
			function( $post ) {
139
				return $post instanceof WP_Post;
140
			} 
141
		);
142
	}
143
144
	/**
145
	 * Get payment posts to upgrade.
146
	 *
147
	 * @return WP_Post[]
148
	 */
149
	private function get_payment_posts() {
150
		$query = new \WP_Query(
151
			array(
152
				'post_type'     => 'pronamic_payment',
153
				'post_status'   => 'any',
154
				'meta_query'    => array(
155
					array(
156
						'key'   => '_pronamic_payment_source',
157
						'value' => 'memberpress',
158
					),
159
				),
160
				'nopaging'      => true,
161
				'no_found_rows' => true,
162
				'order'         => 'DESC',
163
				'orderby'       => 'ID',
164
			)
165
		);
166
167
		return array_filter(
168
			$query->posts,
169
			function( $post ) {
170
				return $post instanceof WP_Post;
171
			} 
172
		);
173
	}
174
175
	/**
176
	 * Log.
177
	 *
178
	 * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-log/
179
	 * @param string $message Message.
180
	 * @return void
181
	 */
182
	private function log( $message ) {
183
		if ( method_exists( '\WP_CLI', 'log' ) ) {
184
			\WP_CLI::log( $message );
185
		}
186
	}
187
188
	/**
189
	 * Upgrade subscriptions.
190
	 *
191
	 * @param array<string, mixed> $args Arguments.
192
	 * @return void
193
	 */
194
	private function upgrade_subscriptions( $args = array() ) {
195
		$args = \wp_parse_args(
196
			$args,
197
			array(
198
				'skip-no-match' => false,
199
				'reactivate'    => false,
200
				'dry-run'       => false,
201
				'post__in'      => null,
202
			)
203
		);
204
205
		$skip_no_match = \filter_var( $args['skip-no-match'], FILTER_VALIDATE_BOOLEAN );
0 ignored issues
show
The assignment to $skip_no_match is dead and can be removed.
Loading history...
206
		$reactivate    = \filter_var( $args['reactivate'], FILTER_VALIDATE_BOOLEAN );
0 ignored issues
show
The assignment to $reactivate is dead and can be removed.
Loading history...
207
		$dry_run       = \filter_var( $args['dry-run'], FILTER_VALIDATE_BOOLEAN );
0 ignored issues
show
The assignment to $dry_run is dead and can be removed.
Loading history...
208
209
		$query_args = array();
210
211
		if ( null !== $args['post__in'] ) {
212
			$query_args['post__in'] = \explode( ',', $args['post__in'] );
213
		}
214
215
		$subscription_posts = $this->get_subscription_posts( $query_args );
216
217
		$this->log(
218
			\sprintf(
219
				'Processing %d subscription posts…',
220
				\number_format_i18n( \count( $subscription_posts ) )
221
			)
222
		);
223
224
		foreach ( $subscription_posts as $subscription_post ) {
225
			$subscription_post_id = $subscription_post->ID;
226
227
			$this->log(
228
				\sprintf(
229
					'Subscription post %s',
230
					$subscription_post_id
231
				)
232
			);
233
234
			/**
235
			 * Get subscription.
236
			 *
237
			 * @link https://github.com/wp-pay/core/blob/2.2.4/includes/functions.php#L158-L180
238
			 */
239
			$subscription = \get_pronamic_subscription( $subscription_post_id );
240
241
			if ( null === $subscription ) {
242
				continue;
243
			}
244
245
			/**
246
			 * Get source.
247
			 */
248
			$subscription_source    = \get_post_meta( $subscription_post_id, '_pronamic_subscription_source', true );
249
			$subscription_source_id = \get_post_meta( $subscription_post_id, '_pronamic_subscription_source_id', true );
250
251
			\update_post_meta( $subscription_post_id, '_pronamic_subscription_memberpress_update_source', $subscription_source );
252
			\update_post_meta( $subscription_post_id, '_pronamic_subscription_memberpress_update_source_id', $subscription_source_id );
253
254
			/**
255
			 * MemberPress transaction.
256
			 */
257
			$memberpress_subscription_id = $subscription_source_id;
258
259
			/**
260
			 * MemberPress subscription.
261
			 */
262
			$memberpress_subscription = MemberPress::get_subscription_by_id( $memberpress_subscription_id );
0 ignored issues
show
It seems like $memberpress_subscription_id can also be of type false; however, parameter $id of Pronamic\WordPress\Pay\E...et_subscription_by_id() does only seem to accept integer|null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

262
			$memberpress_subscription = MemberPress::get_subscription_by_id( /** @scrutinizer ignore-type */ $memberpress_subscription_id );
Loading history...
263
264
			if ( ! $memberpress_subscription ) {
265
				continue;
266
			}
267
268
			/**
269
			 * Ok.
270
			 */
271
			$subscription->set_source( 'memberpress_subscription' );
272
			$subscription->set_source_id( $memberpress_subscription->id );
273
274
			$subscription->save();
275
		}
276
	}
277
278
	/**
279
	 * Upgrade payments.
280
	 *
281
	 * @return void
282
	 */
283
	private function upgrade_payments() {
284
		$payment_posts = $this->get_payment_posts();
285
286
		foreach ( $payment_posts as $payment_post ) {
287
			$payment_post_id = $payment_post->ID;
288
289
			/**
290
			 * Get payment.
291
			 *
292
			 * @link https://github.com/wp-pay/core/blob/2.2.4/includes/functions.php#L24-L46
293
			 */
294
			$payment = \get_pronamic_payment( $payment_post_id );
295
296
			if ( null === $payment ) {
297
				continue;
298
			}
299
300
			/**
301
			 * Get source.
302
			 */
303
			$payment_source    = \get_post_meta( $payment_post_id, '_pronamic_payment_source', true );
304
			$payment_source_id = \get_post_meta( $payment_post_id, '_pronamic_payment_source_id', true );
305
306
			\update_post_meta( $payment_post_id, '_pronamic_payment_memberpress_update_source', $payment_source );
307
			\update_post_meta( $payment_post_id, '_pronamic_payment_memberpress_update_source_id', $payment_source_id );
308
309
			/**
310
			 * MemberPress transaction.
311
			 */
312
			$memberpress_transaction_id = $payment_source_id;
313
314
			$memberpress_transaction = MemberPress::get_transaction_by_id( $memberpress_transaction_id );
0 ignored issues
show
It seems like $memberpress_transaction_id can also be of type false; however, parameter $id of Pronamic\WordPress\Pay\E...get_transaction_by_id() does only seem to accept integer|null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

314
			$memberpress_transaction = MemberPress::get_transaction_by_id( /** @scrutinizer ignore-type */ $memberpress_transaction_id );
Loading history...
315
316
			if ( null === $memberpress_transaction ) {
317
				continue;
318
			}
319
320
			/**
321
			 * Ok.
322
			 */
323
			$payment->set_source( 'memberpress_transaction' );
324
			$payment->set_source_id( $memberpress_transaction->id );
325
326
			$payment->save();
327
		}
328
	}
329
}
330