Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/paid-memberships-pro/includes/level-groups.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
/**
4
+
* Return an array of all level groups, with the key being the level group id.
5
+
*
6
+
* @since 3.0
7
+
*
8
+
* @return array
9
+
*/
10
+
function pmpro_get_level_groups() {
11
+
global $wpdb;
12
+
13
+
$groups = $wpdb->get_results( "SELECT * FROM $wpdb->pmpro_groups ORDER BY id" );
14
+
$to_return = array();
15
+
foreach ( $groups as $group ) {
16
+
$to_return[ $group->id ] = $group;
17
+
}
18
+
19
+
// If we don't have any groups yet, create the default one and add all levels to it.
20
+
if ( empty( $to_return ) ) {
21
+
$group_id = pmpro_create_level_group( 'Main Group', false );
22
+
if ( ! empty( $group_id ) ) {
23
+
$levels = pmpro_getAllLevels( true, true );
24
+
foreach ( $levels as $level ) {
25
+
pmpro_add_level_to_group( $level->id, $group_id );
26
+
}
27
+
return array( $group_id => (object) array( 'id' => $group_id, 'name' => 'Main Group', 'allow_multiple_selections' => 0, 'displayorder' => 0 ) );
28
+
}
29
+
}
30
+
31
+
return $to_return;
32
+
}
33
+
34
+
/**
35
+
* Return an array of all level groups in order.
36
+
*
37
+
* @since 3.0
38
+
*
39
+
* @return array
40
+
*/
41
+
function pmpro_get_level_groups_in_order() {
42
+
$level_groups = pmpro_get_level_groups();
43
+
usort( $level_groups, function ( $a, $b ) {
44
+
return (int) $a->displayorder - (int) $b->displayorder;
45
+
} );
46
+
47
+
return $level_groups;
48
+
}
49
+
50
+
/**
51
+
* Get data for a level group.
52
+
*
53
+
* @since 3.0
54
+
*
55
+
* @param int $group_id The ID of the group to get data for.
56
+
* @return object|bool The group data, or false if the group doesn't exist.
57
+
*/
58
+
function pmpro_get_level_group( $group_id ) {
59
+
$all_groups = pmpro_get_level_groups();
60
+
if ( ! empty( $all_groups[ $group_id ] ) ) {
61
+
return $all_groups[ $group_id ];
62
+
} else {
63
+
return false;
64
+
}
65
+
}
66
+
67
+
/**
68
+
* Create a level group.
69
+
*
70
+
* @since 3.0
71
+
*
72
+
* @param string $name The name of the group.
73
+
* @param bool $allow_multiple_levels Whether or not to allow multiple levels to be selected from this group.
74
+
* @param int $displayorder The display order for the group.
75
+
*
76
+
* @return int|false The id of the new group or false if the group could not be created.
77
+
*/
78
+
function pmpro_create_level_group( $name, $allow_multiple_levels = true, $displayorder = null ) {
79
+
global $wpdb;
80
+
81
+
if ( empty( $displayorder ) ) {
82
+
$displayorder = $wpdb->get_var( "SELECT MAX(displayorder) FROM $wpdb->pmpro_groups LIMIT 1" );
83
+
$displayorder = intval( $displayorder ) + 1;
84
+
}
85
+
86
+
$result = $wpdb->insert(
87
+
$wpdb->pmpro_groups,
88
+
array(
89
+
'name' => $name,
90
+
'allow_multiple_selections' => (int) $allow_multiple_levels,
91
+
'displayorder' => (int) $displayorder,
92
+
),
93
+
array( '%s', '%d', '%d' )
94
+
);
95
+
96
+
return empty( $result ) ? false : $wpdb->insert_id;
97
+
}
98
+
99
+
/**
100
+
* Edit a level group.
101
+
*
102
+
* @since 3.0
103
+
*
104
+
* @param int $id The id of the group to edit.
105
+
* @param string $name The name of the group.
106
+
* @param bool $allow_multiple_levels Whether or not to allow multiple levels to be selected from this group.
107
+
* @param int $displayorder The display order of the group.
108
+
*
109
+
* @return bool True if the group was edited, false otherwise.
110
+
*/
111
+
function pmpro_edit_level_group( $id, $name, $allow_multiple_levels = true, $displayorder = null ) {
112
+
global $wpdb;
113
+
114
+
if ( empty( $displayorder ) ) {
115
+
$displayorder = $wpdb->get_var( "SELECT MAX(displayorder) FROM $wpdb->pmpro_groups LIMIT 1" );
116
+
$displayorder = intval( $displayorder ) + 1;
117
+
}
118
+
119
+
$result = $wpdb->update(
120
+
$wpdb->pmpro_groups,
121
+
array(
122
+
'name' => $name,
123
+
'allow_multiple_selections' => (int) $allow_multiple_levels,
124
+
'displayorder' => (int) $displayorder,
125
+
),
126
+
array( 'id' => $id ),
127
+
array( '%s', '%d', '%d', '%d' )
128
+
);
129
+
130
+
return ! empty( $result );
131
+
}
132
+
133
+
/**
134
+
* Delete a level group.
135
+
*
136
+
* @since 3.0
137
+
*
138
+
* @param int $id The id of the group to delete.
139
+
*
140
+
* @return bool True if the group was deleted, false otherwise.
141
+
*/
142
+
function pmpro_delete_level_group( $id ) {
143
+
global $wpdb;
144
+
145
+
// Make sure that there are no levels in this group.
146
+
$levels_in_group = pmpro_get_level_ids_for_group( $id );
147
+
if ( ! empty( $levels_in_group ) ) {
148
+
return false;
149
+
}
150
+
151
+
$result = $wpdb->delete(
152
+
$wpdb->pmpro_groups,
153
+
array( 'id' => $id ),
154
+
array( '%d' )
155
+
);
156
+
157
+
return ! empty( $result );
158
+
}
159
+
160
+
/**
161
+
* Add a membership level to a level group.
162
+
*
163
+
* @since 3.0
164
+
*
165
+
* @param int $level_id The id of the level to add.
166
+
* @param int $group_id The id of the group to add the level to.
167
+
*/
168
+
function pmpro_add_level_to_group( $level_id, $group_id ) {
169
+
global $wpdb;
170
+
171
+
// Remove the level from its current group.
172
+
$wpdb->delete( $wpdb->pmpro_membership_levels_groups, array( 'level' => $level_id ) );
173
+
174
+
// Add the level to the new group.
175
+
$wpdb->insert( $wpdb->pmpro_membership_levels_groups, array('level' => $level_id, 'group' => $group_id ), array( '%d', '%d' ) );
176
+
}
177
+
178
+
/**
179
+
* Get the group for a level.
180
+
*
181
+
* @since 3.0
182
+
*
183
+
* @param int $level_id The id of the level to get the group for.
184
+
* @return int|false The id of the group the level is in or false if the level is not in a group.
185
+
*/
186
+
function pmpro_get_group_id_for_level( $level_id ) {
187
+
global $wpdb;
188
+
189
+
$sqlQuery = $wpdb->prepare( "SELECT `group` FROM $wpdb->pmpro_membership_levels_groups WHERE `level` = %d LIMIT 1", $level_id );
190
+
$group_id = $wpdb->get_var( $sqlQuery );
191
+
192
+
return empty( $group_id ) ? false : $group_id;
193
+
}
194
+
195
+
/**
196
+
* Get the membership levels for a given group.
197
+
*
198
+
* @since 3.0
199
+
*
200
+
* @param int $group_id The id of the group to get the levels for.
201
+
* @return array An array of membership levels.
202
+
*/
203
+
function pmpro_get_levels_for_group( $group_id ) {
204
+
global $wpdb;
205
+
206
+
$sqlQuery = $wpdb->prepare( "SELECT * FROM $wpdb->pmpro_membership_levels WHERE id IN ( SELECT level FROM $wpdb->pmpro_membership_levels_groups WHERE `group` = %d )", $group_id );
207
+
$levels = $wpdb->get_results( $sqlQuery );
208
+
209
+
return empty( $levels ) ? array() : $levels;
210
+
}
211
+
212
+
/**
213
+
* Get the level IDs for a given group.
214
+
*
215
+
* @since 3.0
216
+
*
217
+
* @param int $group_id The id of the group to get the levels for.
218
+
* @return array An array of membership level IDs.
219
+
*/
220
+
function pmpro_get_level_ids_for_group( $group_id ) {
221
+
global $wpdb;
222
+
223
+
$sqlQuery = $wpdb->prepare( "SELECT level FROM $wpdb->pmpro_membership_levels_groups WHERE `group` = %d", $group_id );
224
+
$levels = $wpdb->get_col( $sqlQuery );
225
+
226
+
return empty( $levels ) ? array() : $levels;
227
+
}
228
+
229
+
/**
230
+
* Get the active membership levels in a specific level group for a user.
231
+
* @since 3.4
232
+
*
233
+
* @param int $user_id The ID of the user to get the levels for.
234
+
* @param int $group_id The ID of the group to get the levels for.
235
+
* @return array An array of membership levels.
236
+
*/
237
+
function pmpro_get_membership_levels_for_user_in_group( $user_id, $group_id ) {
238
+
// Get all level IDs in the group.
239
+
$level_ids = pmpro_get_level_ids_for_group( $group_id );
240
+
241
+
// Get all active levels for the user.
242
+
$levels = pmpro_getMembershipLevelsForUser( $user_id );
243
+
if ( empty( $levels ) ) {
244
+
return array();
245
+
}
246
+
247
+
// Filter out levels that are not in the group.
248
+
$levels_in_group = array();
249
+
foreach ( $levels as $level ) {
250
+
if ( in_array( $level->id, $level_ids ) ) {
251
+
$levels_in_group[] = $level;
252
+
}
253
+
}
254
+
255
+
return $levels_in_group;
256
+
}
257
+