1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Admin subscriptions |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2022 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_Post; |
14
|
|
|
use WP_Query; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Admin subscriptions |
18
|
|
|
* |
19
|
|
|
* @author Remco Tolsma |
20
|
|
|
* @version 3.1.0 |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class AdminSubscriptions { |
24
|
|
|
/** |
25
|
|
|
* Subscriptions map. |
26
|
|
|
* |
27
|
|
|
* @var array<string, WP_Post>|null |
28
|
|
|
*/ |
29
|
|
|
private $subscriptions_map; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Setup. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function setup() { |
37
|
|
|
/** |
38
|
|
|
* Filter for subscriptions columns. |
39
|
|
|
* |
40
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprSubscriptionsCtrl.php#L20-L27 |
41
|
|
|
*/ |
42
|
|
|
$hook = 'memberpress_page_memberpress-subscriptions'; |
43
|
|
|
|
44
|
|
|
add_filter( 'manage_' . $hook . '_columns', array( $this, 'manage_subscriptions_columns' ), 15 ); |
45
|
|
|
|
46
|
|
|
add_action( 'mepr-admin-subscriptions-cell', array( $this, 'admin_subscriptions_cell' ), 10, 4 ); |
47
|
|
|
|
48
|
|
|
add_filter( 'mepr_view_get_string', array( $this, 'extend_subscription_form' ), 10, 3 ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Manage subscriptions columns. |
53
|
|
|
* |
54
|
|
|
* @param array<string, string> $columns Columns. |
55
|
|
|
* @return array<string, string> |
56
|
|
|
*/ |
57
|
|
|
public function manage_subscriptions_columns( $columns ) { |
58
|
|
|
$columns['pronamic_subscription'] = __( 'Pronamic Subscription', 'pronamic_ideal' ); |
59
|
|
|
|
60
|
|
|
return $columns; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get subscriptions map. |
65
|
|
|
* |
66
|
|
|
* @param object $table Table. |
67
|
|
|
* @return array<string, WP_Post>|null |
68
|
|
|
*/ |
69
|
|
|
private function get_subscriptions_map( $table ) { |
70
|
|
|
if ( is_array( $this->subscriptions_map ) ) { |
71
|
|
|
return $this->subscriptions_map; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->subscriptions_map = array(); |
75
|
|
|
|
76
|
|
|
if ( ! isset( $table->items ) ) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$memberpress_subscriptions = $table->items; |
81
|
|
|
|
82
|
|
|
if ( ! is_array( $memberpress_subscriptions ) || empty( $memberpress_subscriptions ) ) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$memberpress_subscription_ids = wp_list_pluck( $memberpress_subscriptions, 'id' ); |
87
|
|
|
|
88
|
|
|
$query = new WP_Query( |
89
|
|
|
array( |
90
|
|
|
'post_type' => 'pronamic_pay_subscr', |
91
|
|
|
'post_status' => 'any', |
92
|
|
|
'nopaging' => true, |
93
|
|
|
'meta_query' => array( |
94
|
|
|
array( |
95
|
|
|
'key' => '_pronamic_subscription_source', |
96
|
|
|
'compare' => '=', |
97
|
|
|
'value' => 'memberpress_subscription', |
98
|
|
|
), |
99
|
|
|
array( |
100
|
|
|
'key' => '_pronamic_subscription_source_id', |
101
|
|
|
'compare' => 'IN', |
102
|
|
|
'value' => $memberpress_subscription_ids, |
103
|
|
|
), |
104
|
|
|
), |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$subscription_posts = $query->get_posts(); |
109
|
|
|
|
110
|
|
|
foreach ( $subscription_posts as $subscription_post ) { |
111
|
|
|
if ( ! $subscription_post instanceof WP_Post ) { |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$memberpress_subscription_id = (string) \get_post_meta( $subscription_post->ID, '_pronamic_subscription_source_id', true ); |
116
|
|
|
|
117
|
|
|
$this->subscriptions_map[ $memberpress_subscription_id ] = $subscription_post; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->subscriptions_map; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Admin subscription cell. |
125
|
|
|
* |
126
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/controllers/MeprSubscriptionsCtrl.php#L73 |
127
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/lib/MeprSubscriptionsTable.php#L230 |
128
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/lib/MeprView.php#L49 |
129
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/views/admin/subscriptions/row.php |
130
|
|
|
* |
131
|
|
|
* @param string $column_name Column name. |
132
|
|
|
* @param object $rec Record. |
133
|
|
|
* @param object $table Table. |
134
|
|
|
* @param string $attributes Attributes. |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public function admin_subscriptions_cell( $column_name, $rec, $table, $attributes ) { |
138
|
|
|
if ( 'pronamic_subscription' !== $column_name ) { |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ( ! \property_exists( $rec, 'id' ) ) { |
143
|
|
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$map = $this->get_subscriptions_map( $table ); |
147
|
|
|
|
148
|
|
|
if ( null === $map ) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
printf( |
153
|
|
|
'<td %s>', |
154
|
|
|
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
155
|
|
|
$attributes |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$memberpress_subscription_id = $rec->id; |
159
|
|
|
|
160
|
|
|
if ( isset( $map[ $memberpress_subscription_id ] ) ) { |
161
|
|
|
$pronamic_subscription_post = $map[ $memberpress_subscription_id ]; |
162
|
|
|
|
163
|
|
|
\printf( |
164
|
|
|
'<a href="%s">%s</a>', |
165
|
|
|
\esc_attr( (string) \get_edit_post_link( $pronamic_subscription_post ) ), |
166
|
|
|
\esc_html( (string) $pronamic_subscription_post->ID ) |
167
|
|
|
); |
168
|
|
|
} else { |
169
|
|
|
echo '—'; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
echo '</td>'; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Extend subscription form. |
177
|
|
|
* |
178
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/controllers/MeprSubscriptionsCtrl.php#L105-L133 |
179
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/views/admin/subscriptions/edit.php |
180
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/views/admin/subscriptions/form.php |
181
|
|
|
* |
182
|
|
|
* @param string $view View. |
183
|
|
|
* @param string $slug Slug. |
184
|
|
|
* @param mixed[] $vars Variables. |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function extend_subscription_form( $view, $slug, $vars ) { |
188
|
|
|
if ( '/admin/subscriptions/form' !== $slug ) { |
189
|
|
|
return $view; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if ( ! array_key_exists( 'sub', $vars ) ) { |
193
|
|
|
return $view; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$memberpress_subscription = $vars['sub']; |
197
|
|
|
|
198
|
|
|
/* |
199
|
|
|
* Check if variable is a object, should be instance of `MeprSubscription`. |
200
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprSubscription.php |
201
|
|
|
*/ |
202
|
|
|
if ( ! is_object( $memberpress_subscription ) ) { |
203
|
|
|
return $view; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ( ! isset( $memberpress_subscription->id ) ) { |
207
|
|
|
return $view; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$memberpress_subscription_id = $memberpress_subscription->id; |
211
|
|
|
|
212
|
|
|
ob_start(); |
213
|
|
|
|
214
|
|
|
include dirname( __FILE__ ) . '/../../views/subscription-form.php'; |
215
|
|
|
|
216
|
|
|
$view .= ob_get_clean(); |
217
|
|
|
|
218
|
|
|
return $view; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|