Failed Conditions
Push — develop ( bdd916...26c591 )
by Remco
06:44 queued 13s
created

Upgrade310   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 109
c 1
b 0
f 0
dl 0
loc 248
ccs 0
cts 155
cp 0
rs 10
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get_subscription_posts() 0 17 1
A get_payment_posts() 0 19 1
A execute() 0 3 1
A upgrade_payments() 0 25 3
A cli_init() 0 57 1
A upgrade_subscriptions() 0 59 4
A log() 0 3 2
A __construct() 0 5 3
1
<?php
2
/**
3
 * Upgrade 3.1.0
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 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
16
/**
17
 * Upgrade 3.1.0
18
 *
19
 * @author  Remco Tolsma
20
 * @version 3.1.0
21
 * @since   3.1.0
22
 */
23
class Upgrade310 extends Upgrade {
24
	/**
25
	 * Construct 2.1.6 upgrade.
26
	 */
27
	public function __construct() {
28
		parent::__construct( '3.1.0' );
29
30
		if ( \defined( '\WP_CLI' ) && \WP_CLI ) {
0 ignored issues
show
Bug introduced by
The constant WP_CLI was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
			$this->cli_init();
32
		}
33
	}
34
35
	/**
36
	 * Execute.
37
	 *
38
	 * @return void
39
	 */
40
	public function execute() {
41
		$this->upgrade_subscriptions();
42
		$this->upgrade_payments();
43
	}
44
45
	/**
46
	 * WP-CLI initialize.
47
	 *
48
	 * @link https://github.com/wp-cli/wp-cli/issues/4818
49
	 * @return void
50
	 */
51
	public function cli_init() {
52
		\WP_CLI::add_command(
0 ignored issues
show
Bug introduced by
The type WP_CLI 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...
53
			'pronamic-pay memberpress upgrade-310 execute',
54
			function( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
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

54
			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...
Unused Code introduced by
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

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

66
			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...
Unused Code introduced by
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

66
			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...
67
				\WP_CLI::log( 'Upgrade 3.1.0 - Subscriptions List' );
68
69
				$posts = $this->get_subscription_posts();
70
71
				\WP_CLI\Utils\format_items( 'table', $posts, array( 'ID', 'post_title', 'post_status' ) );
0 ignored issues
show
Bug introduced by
The function format_items was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

71
				/** @scrutinizer ignore-call */ 
72
    \WP_CLI\Utils\format_items( 'table', $posts, array( 'ID', 'post_title', 'post_status' ) );
Loading history...
72
			},
73
			array(
74
				'shortdesc' => 'Execute MemberPress upgrade 3.1.0.',
75
			)
76
		);
77
78
		\WP_CLI::add_command(
79
			'pronamic-pay memberpress upgrade-310 upgrade-subscriptions',
80
			function( $args, $assoc_args ) {
81
				\WP_CLI::log( 'Upgrade 3.1.0 - Subscriptions' );
82
83
				$this->upgrade_subscriptions(
84
					array(
85
						'skip-no-match' => \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-no-match', true ),
0 ignored issues
show
Bug introduced by
The function get_flag_value was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

85
						'skip-no-match' => /** @scrutinizer ignore-call */ \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-no-match', true ),
Loading history...
86
						'reactivate'    => \WP_CLI\Utils\get_flag_value( $assoc_args, 'reactivate', true ),
87
						'dry-run'       => \WP_CLI\Utils\get_flag_value( $assoc_args, 'dry-run', true ),
88
						'post__in'      => \WP_CLI\Utils\get_flag_value( $assoc_args, 'post__in', null ),
89
					)
90
				);
91
			},
92
			array(
93
				'shortdesc' => 'Execute MemberPress upgrade 2.1.6.',
94
			)
95
		);
96
97
		\WP_CLI::add_command(
98
			'pronamic-pay memberpress upgrade-310 list-payments',
99
			function( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
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

99
			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...
Unused Code introduced by
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

99
			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...
100
				\WP_CLI::log( 'Upgrade 3.1.0 - Payments List' );
101
102
				$posts = $this->get_payment_posts();
103
104
				\WP_CLI\Utils\format_items( 'table', $posts, array( 'ID', 'post_title', 'post_status' ) );
0 ignored issues
show
Bug introduced by
The function format_items was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

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