Failed Conditions
Push — develop ( 02b9a0...0a270b )
by Reüel
04:26
created

src/Admin/AdminSubscriptions.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Admin subscriptions
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 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.4
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' ), 10 );
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
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( // WPCS: XSS ok.
136
			'<td %s>',
137
			$attributes
138
		);
139
140
		$memberpress_subscription_id = $rec->id;
141
142
		if ( isset( $map[ $memberpress_subscription_id ] ) ) {
143
			$pronamic_subscription_post = $map[ $memberpress_subscription_id ];
144
145
			printf(
146
				'<a href="%s">%s</a>',
147
				esc_attr( get_edit_post_link( $pronamic_subscription_post ) ),
148
				esc_html( $pronamic_subscription_post->ID )
149
			);
150
		} else {
151
			echo '—';
152
		}
153
154
		echo '</td>';
155
	}
156
157
	/**
158
	 * Extend subscription form.
159
	 *
160
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/controllers/MeprSubscriptionsCtrl.php#L105-L133
161
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/views/admin/subscriptions/edit.php
162
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/views/admin/subscriptions/form.php
163
	 *
164
	 * @param string $view View.
165
	 * @param string $slug Slug.
166
	 * @param array  $vars Variables.
167
	 * @return string
168
	 */
169
	public function extend_subscription_form( $view, $slug, $vars ) {
170
		if ( '/admin/subscriptions/form' !== $slug ) {
171
			return $view;
172
		}
173
174
		if ( ! array_key_exists( 'sub', $vars ) ) {
175
			return $view;
176
		}
177
178
		$memberpress_subscription = $vars['sub'];
179
180
		/*
181
		 * Check if variable is a object, should be instance of `MeprSubscription`.
182
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprSubscription.php
183
		 */
184
		if ( ! is_object( $memberpress_subscription ) ) {
185
			return $view;
186
		}
187
188
		if ( ! isset( $memberpress_subscription->id ) ) {
189
			return $view;
190
		}
191
192
		$memberpress_subscription_id = $memberpress_subscription->id;
193
194
		ob_start();
195
196
		include dirname( __FILE__ ) . '/../../views/subscription-form.php';
197
198
		$view .= ob_get_clean();
199
200
		return $view;
201
	}
202
}
203