Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/paid-memberships-pro/shortcodes/pmpro_member.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Shortcode to show a specific user field for current user or specified user ID.
4
+
*
5
+
* Example: [pmpro_member field='last_name' user_id='1', levels='1,2,3']
6
+
*
7
+
* @param array $atts The shortcode attributes passed in.
8
+
* @param string|null $content The content passed in or null if not set.
9
+
* @param string $shortcode_tag The name of the shortcode tag used.
10
+
*
11
+
* @return string The shortcode output.
12
+
*/
13
+
function pmpro_member_shortcode( $atts, $content = null, $shortcode_tag = '' ) {
14
+
global $current_user;
15
+
16
+
// Get the attributes and their defaults.
17
+
extract(
18
+
shortcode_atts(
19
+
array(
20
+
'user_id' => $current_user->ID,
21
+
'field' => null,
22
+
'levels' => null,
23
+
'group' => null,
24
+
),
25
+
$atts
26
+
)
27
+
);
28
+
29
+
// No user_id found, let's bail.
30
+
if ( ! $user_id ) {
31
+
return;
32
+
}
33
+
34
+
// Make sure the user_id is of an existing user when not viewing their own information.
35
+
if ( $user_id !== $current_user->ID ) {
36
+
$user = get_userdata( $user_id );
37
+
if ( ! $user ) {
38
+
return;
39
+
}
40
+
} else {
41
+
$user = $current_user;
42
+
}
43
+
44
+
45
+
// Bail if there's no field attribute.
46
+
if ( empty( $field ) ) {
47
+
return esc_html__( 'The "field" attribute is required in the pmpro_member shortcode.', 'paid-memberships-pro' );
48
+
}
49
+
50
+
// Get a list of fields related to the user's level.
51
+
$pmpro_level_fields = array(
52
+
'membership_id',
53
+
'membership_name',
54
+
'membership_description',
55
+
'membership_confirmation',
56
+
'membership_initial_payment',
57
+
'membership_startdate',
58
+
'membership_enddate',
59
+
'level_cost',
60
+
);
61
+
62
+
// Get a list of fields related to the user's subscription.
63
+
$pmpro_subscription_fields = array(
64
+
'membership_billing_amount',
65
+
'membership_cycle_number',
66
+
'membership_cycle_period',
67
+
'membership_billing_limit',
68
+
'membership_trial_amount',
69
+
'membership_trial_limit',
70
+
'next_payment_date',
71
+
);
72
+
73
+
// Get a list of pmpro-related fields stored in user meta.
74
+
// Note: Most of this information is no longer stored in user meta.
75
+
// These will likely be deprecated in the future. Use with caution.
76
+
$pmpro_user_meta_fields = array(
77
+
'bfirstname',
78
+
'blastname',
79
+
'baddress1',
80
+
'baddress2',
81
+
'bcity',
82
+
'bstate',
83
+
'bzipcode',
84
+
'bcountry',
85
+
'bphone',
86
+
'bemail',
87
+
'CardType',
88
+
'AccountNumber',
89
+
'ExpirationMonth',
90
+
'ExpirationYear',
91
+
);
92
+
93
+
// Get a list of fields saved in the wp_users table.
94
+
$user_column_fields = array(
95
+
'user_login',
96
+
'user_email',
97
+
'user_url',
98
+
'user_registered',
99
+
'display_name',
100
+
);
101
+
102
+
// Get a list of date fields.
103
+
$date_fields = array(
104
+
'startdate',
105
+
'enddate',
106
+
'modified',
107
+
'user_registered',
108
+
'next_payment_date',
109
+
);
110
+
111
+
// Get a list of price fields.
112
+
$price_fields = array(
113
+
'initial_payment',
114
+
'billing_amount',
115
+
'trial_amount',
116
+
);
117
+
118
+
if ( in_array( $field, $pmpro_level_fields ) || in_array( $field, $pmpro_subscription_fields ) || 'level_meta_' === substr( $field, 0, 11 ) ) {
119
+
// Fields about the user's membership or subscription.
120
+
// Get the membership level to show.
121
+
$membership_level = null;
122
+
if ( empty( $levels ) && empty( $group ) ) {
123
+
// Grab any one of the user's levels.
124
+
$membership_level = pmpro_getMembershipLevelForUser( $user_id );
125
+
} else {
126
+
// Find a level from the list of levels or group.
127
+
if ( ! empty( $levels ) ) {
128
+
// Level IDs were passed in.
129
+
$level_ids = explode( ',', $levels );
130
+
} else {
131
+
// A level group was passed in.
132
+
$level_ids = wp_list_pluck( pmpro_get_levels_for_group( intval( $group ) ), 'id' );
133
+
}
134
+
135
+
// Grab the first level the user has from the list.
136
+
foreach ( $level_ids as $level_id ) {
137
+
$membership_level = pmpro_getSpecificMembershipLevelForUser( $user_id, $level_id );
138
+
if ( ! empty( $membership_level ) ) {
139
+
break;
140
+
}
141
+
}
142
+
}
143
+
144
+
if ( empty( $membership_level ) ) {
145
+
// No level found.
146
+
$r = '';
147
+
} elseif ( in_array( $field, $pmpro_level_fields ) ) {
148
+
// Membership level fields.
149
+
if ( $field === 'level_cost' ) {
150
+
// Special case for level_cost.
151
+
$r = pmpro_getLevelCost( $membership_level, false, true );
152
+
} else {
153
+
// All other fields.
154
+
$field = str_replace( 'membership_', '', $field );
155
+
$r = $membership_level->{$field};
156
+
}
157
+
} elseif( 'level_meta_' === substr( $field, 0, 11 ) ) {
158
+
// Membership level meta fields.
159
+
$r = get_pmpro_membership_level_meta( $membership_level->id, substr( $field, 11 ), true );
160
+
} else {
161
+
// Subscription fields.
162
+
$subscriptions = PMPro_Subscription::get_subscriptions_for_user( $user_id, $membership_level->id );
163
+
if ( empty( $subscriptions ) ) {
164
+
// No subscription found.
165
+
$r = '';
166
+
} else {
167
+
$field = str_replace( 'membership_', '', $field );
168
+
$r = call_user_func( array( $subscriptions[0], 'get_' . $field ) );
169
+
}
170
+
}
171
+
} elseif ( in_array( $field, $pmpro_user_meta_fields ) ) {
172
+
// PMPro-related fields stored in user meta.
173
+
$field = 'pmpro_' . $field;
174
+
$r = get_user_meta($user_id, $field, true );
175
+
} elseif ( in_array( $field, $user_column_fields ) ) {
176
+
// wp_users column.
177
+
$r = $user->{$field};
178
+
} elseif ( $field === 'avatar' ) {
179
+
// Get the user's avatar.
180
+
$r = get_avatar( $user_id );
181
+
} else {
182
+
// Assume user meta.
183
+
$r = get_user_meta( $user_id, $field, true );
184
+
}
185
+
186
+
// Check for dates to reformat them.
187
+
if ( in_array( $field, $date_fields ) ) {
188
+
if ( empty( $r ) || $r === '0000-00-00 00:00:00' ) {
189
+
$r = ''; // Empty date.
190
+
} elseif ( is_numeric( $r ) ) {
191
+
$r = date_i18n( get_option( 'date_format' ), $r ); // Timestamp.
192
+
} else {
193
+
$r = date_i18n( get_option( 'date_format' ), strtotime( $r, current_time( 'timestamp' ) ) ); // YYYY-MM-DD/etc format.
194
+
}
195
+
}
196
+
197
+
// Check for prices to reformat them.
198
+
if ( in_array( $field, $price_fields ) ) {
199
+
if ( empty( $r ) || $r == '0.00' ) {
200
+
$r = '';
201
+
} else {
202
+
$r = pmpro_escape_price( pmpro_formatPrice( $r ) );
203
+
}
204
+
}
205
+
206
+
// If this is a user field, get the display value.
207
+
$user_field = PMPro_Field_Group::get_field( $field );
208
+
if ( ! empty( $user_field ) ) {
209
+
$r = $user_field->displayValue( $r, false );
210
+
}
211
+
212
+
// Check for arrays to reformat them.
213
+
if ( is_array( $r ) ) {
214
+
$r = implode( ', ', $r );
215
+
}
216
+
217
+
/**
218
+
* Filter
219
+
*/
220
+
$r = apply_filters( 'pmpro_member_shortcode_field', $r, $user_id, $field );
221
+
222
+
return $r;
223
+
}
224
+
add_shortcode( 'pmpro_member', 'pmpro_member_shortcode' );
225
+
226
+
/**
227
+
* Strip the [pmpro_member] shortcode from content if the current user can't edit users.
228
+
*
229
+
* @since 2.12.9
230
+
231
+
* @param string|array $content The content to strip the shortcode from.
232
+
* If an array is passed in, all elements
233
+
* will be filtered recursively.
234
+
* Non-strings are ignored.
235
+
*
236
+
* @return mixed The content with the shortcode removed. Will be the same type as the input.
237
+
*/
238
+
function pmpro_maybe_strip_member_shortcode( $content ) {
239
+
// If the user can edit users, we don't need to strip the shortcode.
240
+
if ( current_user_can( 'edit_users' ) ) {
241
+
return $content;
242
+
}
243
+
244
+
// If an array is passed in, filter all elements recursively.
245
+
if ( is_array( $content ) ) {
246
+
foreach ( $content as $key => $value ) {
247
+
$content[ $key ] = pmpro_maybe_strip_member_shortcode( $value );
248
+
}
249
+
return $content;
250
+
}
251
+
252
+
// If we're not looking at a string, just return it.
253
+
if ( ! is_string( $content ) ) {
254
+
return $content;
255
+
}
256
+
257
+
// Okay, we have a string, figure out the regex.
258
+
$shortcodeRegex = get_shortcode_regex( array( 'pmpro_member' ) );
259
+
260
+
// Replace shortcode wrapped in block comments.
261
+
$blockWrapperPattern = "/<!-- wp:shortcode -->\s*$shortcodeRegex\s*<!-- \/wp:shortcode -->/s";
262
+
$content = preg_replace( $blockWrapperPattern, '', $content );
263
+
264
+
// Replace the shortcode by itself.
265
+
$shortcodePattern = "/$shortcodeRegex/";
266
+
$content = preg_replace( $shortcodePattern, '', $content );
267
+
268
+
return $content;
269
+
}
270
+
add_filter( 'content_save_pre', 'pmpro_maybe_strip_member_shortcode' );
271
+
add_filter( 'excerpt_save_pre', 'pmpro_maybe_strip_member_shortcode' );
272
+
add_filter( 'widget_update_callback', 'pmpro_maybe_strip_member_shortcode' );
273
+
274
+
/**
275
+
* Only allow those with the edit_users capability
276
+
* to use the pmpro_member shortcode in post_meta.
277
+
*
278
+
* @since 2.12.9
279
+
* @param int $meta_id ID of the meta data entry.
280
+
* @param int $object_id ID of the object the meta is attached to.
281
+
* @param string $meta_key Meta key.
282
+
* @param mixed $_meta_value Meta value.
283
+
* @return void
284
+
*/
285
+
function pmpro_maybe_strip_member_shortcode_from_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
286
+
// Bail if the value is not a string or array.
287
+
if ( ! is_string( $_meta_value ) && ! is_array( $_meta_value ) ) {
288
+
return;
289
+
}
290
+
291
+
// Strip the shortcode from the meta value.
292
+
$stripped_value = pmpro_maybe_strip_member_shortcode( $_meta_value );
293
+
294
+
// If there was a change, save our stripped version.
295
+
if ( $stripped_value !== $_meta_value ) {
296
+
update_post_meta( $object_id, $meta_key, $stripped_value );
297
+
}
298
+
}
299
+
add_action( 'updated_post_meta', 'pmpro_maybe_strip_member_shortcode_from_post_meta', 10, 4 );
300
+