Failed Conditions
Pull Request — master (#1)
by
unknown
08:13
created

AdminSubscriptions::get_subscriptions_map()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 30
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 52
ccs 0
cts 41
cp 0
crap 56
rs 8.5066

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Admin subscriptions
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Extensions\MemberPress
9
 */
10
11
namespace Pronamic\WordPress\Pay\Extensions\MemberPress\Admin;
12
13
use WP_Query;
14
15
/**
16
 * Admin subscriptions
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.0.11
20
 * @since   1.0.0
21
 */
22
class AdminSubscriptions {
23
	/**
24
	 * Subscriptions map.
25
	 *
26
	 * @var array|null
27
	 */
28
	private $subscriptions_map;
29
30
	/**
31
	 * Setup.
32
	 */
33
	public function setup() {
34
		// @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/controllers/MeprSubscriptionsCtrl.php#L19-L26
35
		$hook = 'memberpress_page_memberpress-subscriptions';
36
37
		add_filter( 'manage_' . $hook . '_columns', array( $this, 'manage_subscriptions_columns' ), 15 );
38
39
		add_action( 'mepr-admin-subscriptions-cell', array( $this, 'admin_subscriptions_cell' ), 10, 4 );
40
41
		add_filter( 'mepr_view_get_string', array( $this, 'extend_subscription_form' ), 10, 3 );
42
	}
43
44
	/**
45
	 * Manage subscriptions columns.
46
	 *
47
	 * @param array $columns Columns.
48
	 */
49
	public function manage_subscriptions_columns( $columns ) {
50
		$columns['pronamic_subscription'] = __( 'Pronamic Subscription', 'pronamic_ideal' );
51
52
		return $columns;
53
	}
54
55
	/**
56
	 * Get subscriptions map.
57
	 *
58
	 * @param object $table Table.
59
	 * @return array
60
	 */
61
	private function get_subscriptions_map( $table ) {
62
		if ( is_array( $this->subscriptions_map ) ) {
63
			return $this->subscriptions_map;
64
		}
65
66
		$this->subscriptions_map = array();
67
68
		if ( ! isset( $table->items ) ) {
69
			return;
70
		}
71
72
		$memberpress_subscriptions = $table->items;
73
74
		if ( ! is_array( $memberpress_subscriptions ) || empty( $memberpress_subscriptions ) ) {
75
			return;
76
		}
77
78
		$memberpress_subscription_ids = wp_list_pluck( $memberpress_subscriptions, 'id' );
79
80
		$query = new WP_Query(
81
			array(
82
				'post_type'   => 'pronamic_pay_subscr',
83
				'post_status' => 'any',
84
				'nopaging'    => true,
85
				'meta_query'  => array(
86
					array(
87
						'key'     => '_pronamic_subscription_source',
88
						'compare' => '=',
89
						'value'   => 'memberpress',
90
					),
91
					array(
92
						'key'     => '_pronamic_subscription_source_id',
93
						'compare' => 'IN',
94
						'value'   => $memberpress_subscription_ids,
95
					),
96
				),
97
			)
98
		);
99
100
		if ( $query->have_posts() ) {
101
			while ( $query->have_posts() ) {
102
				$query->the_post();
103
104
				$memberpress_subscription_id = get_post_meta( get_the_ID(), '_pronamic_subscription_source_id', true );
0 ignored issues
show
Bug introduced by
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, 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

104
				$memberpress_subscription_id = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_subscription_source_id', true );
Loading history...
105
106
				$this->subscriptions_map[ $memberpress_subscription_id ] = get_post();
107
			}
108
109
			wp_reset_postdata();
110
		}
111
112
		return $this->subscriptions_map;
113
	}
114
115
	/**
116
	 * Admin subscription cell.
117
	 *
118
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/controllers/MeprSubscriptionsCtrl.php#L73
119
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/lib/MeprSubscriptionsTable.php#L230
120
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/lib/MeprView.php#L49
121
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/views/admin/subscriptions/row.php
122
	 *
123
	 * @param string $column_name Column name.
124
	 * @param object $rec         Record.
125
	 * @param object $table       Table.
126
	 * @param string $attributes  Attributes.
127
	 */
128
	public function admin_subscriptions_cell( $column_name, $rec, $table, $attributes ) {
129
		if ( 'pronamic_subscription' !== $column_name ) {
130
			return;
131
		}
132
133
		$map = $this->get_subscriptions_map( $table );
134
135
		printf(
136
			'<td %s>',
137
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
138
			$attributes
139
		);
140
141
		$memberpress_subscription_id = $rec->id;
142
143
		if ( isset( $map[ $memberpress_subscription_id ] ) ) {
144
			$pronamic_subscription_post = $map[ $memberpress_subscription_id ];
145
146
			printf(
147
				'<a href="%s">%s</a>',
148
				esc_attr( get_edit_post_link( $pronamic_subscription_post ) ),
149
				esc_html( $pronamic_subscription_post->ID )
150
			);
151
		} else {
152
			echo '—';
153
		}
154
155
		echo '</td>';
156
	}
157
158
	/**
159
	 * Extend subscription form.
160
	 *
161
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/controllers/MeprSubscriptionsCtrl.php#L105-L133
162
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/views/admin/subscriptions/edit.php
163
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/views/admin/subscriptions/form.php
164
	 *
165
	 * @param string $view View.
166
	 * @param string $slug Slug.
167
	 * @param array  $vars Variables.
168
	 * @return string
169
	 */
170
	public function extend_subscription_form( $view, $slug, $vars ) {
171
		if ( '/admin/subscriptions/form' !== $slug ) {
172
			return $view;
173
		}
174
175
		if ( ! array_key_exists( 'sub', $vars ) ) {
176
			return $view;
177
		}
178
179
		$memberpress_subscription = $vars['sub'];
180
181
		/*
182
		 * Check if variable is a object, should be instance of `MeprSubscription`.
183
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprSubscription.php
184
		 */
185
		if ( ! is_object( $memberpress_subscription ) ) {
186
			return $view;
187
		}
188
189
		if ( ! isset( $memberpress_subscription->id ) ) {
190
			return $view;
191
		}
192
193
		$memberpress_subscription_id = $memberpress_subscription->id;
194
195
		ob_start();
196
197
		include dirname( __FILE__ ) . '/../../views/subscription-form.php';
198
199
		$view .= ob_get_clean();
200
201
		return $view;
202
	}
203
}
204