Test Failed
Pull Request — develop (#755)
by
unknown
03:56
created

wordpoints_get_user_rank()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 52
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 26
nc 11
nop 2
dl 0
loc 52
rs 6.8493
c 0
b 0
f 0

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
/**
4
 * Rank API functions.
5
 *
6
 * Includes the ranks meta API.
7
 *
8
 * @package WordPoints\Ranks
9
 * @since 1.7.0
10
 */
11
12
//
13
// Ranks.
14
//
15
16
/**
17
 * Create a new rank.
18
 *
19
 * @since 1.7.0
20
 *
21
 * @param string $name     The name of the rank.
22
 * @param string $type     The rank type slug.
23
 * @param string $group    The slug of the group to which this rank should be added.
24
 * @param int    $position The position this rank should have in the group.
25
 * @param array  $meta     Optional metadata for this rank.
26
 *
27
 * @return int|false|WP_Error The ID of the inserted rank, or false or WP_Error on failure.
0 ignored issues
show
Bug introduced by
The type WP_Error was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
 */
29
function wordpoints_add_rank( $name, $type, $group, $position, array $meta = array() ) {
30
31
	global $wpdb;
32
33
	if ( ! WordPoints_Rank_Groups::is_type_registered_for_group( $type, $group ) ) {
34
		return false;
35
	}
36
37
	$rank_type = WordPoints_Rank_Types::get_type( $type );
38
39
	if ( ! $rank_type ) {
40
		return false;
41
	}
42
43
	$meta = $rank_type->validate_rank_meta( $meta );
44
	if ( false === $meta || is_wp_error( $meta ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
	if ( false === $meta || /** @scrutinizer ignore-call */ is_wp_error( $meta ) ) {
Loading history...
45
		return $meta;
46
	}
47
48
	if ( 'utf8' === $wpdb->get_col_charset( $wpdb->wordpoints_ranks, 'name' ) ) {
49
		$name = wp_encode_emoji( $name );
0 ignored issues
show
Bug introduced by
The function wp_encode_emoji was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
		$name = /** @scrutinizer ignore-call */ wp_encode_emoji( $name );
Loading history...
50
	}
51
52
	$inserted = $wpdb->insert(
53
		$wpdb->wordpoints_ranks
54
		, array(
55
			'name'       => $name,
56
			'type'       => $type,
57
			'rank_group' => $group,
58
			'blog_id'    => is_multisite() ? get_current_blog_id() : '0',
0 ignored issues
show
Bug introduced by
The function get_current_blog_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
			'blog_id'    => is_multisite() ? /** @scrutinizer ignore-call */ get_current_blog_id() : '0',
Loading history...
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
			'blog_id'    => /** @scrutinizer ignore-call */ is_multisite() ? get_current_blog_id() : '0',
Loading history...
59
			'site_id'    => $wpdb->siteid,
60
		)
61
	);
62
63
	if ( ! $inserted ) {
64
		return false;
65
	}
66
67
	$rank_id = (int) $wpdb->insert_id;
68
69
	foreach ( $meta as $meta_key => $meta_value ) {
70
		wordpoints_add_rank_meta( $rank_id, $meta_key, $meta_value );
71
	}
72
73
	WordPoints_Rank_Groups::get_group( $group )->add_rank( $rank_id, $position );
74
75
	/**
76
	 * Perform actions when a rank is added.
77
	 *
78
	 * @since 1.7.0
79
	 *
80
	 * @param int $rank_id The ID of the rank that was added.
81
	 */
82
	do_action( 'wordpoints_add_rank', $rank_id );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
	/** @scrutinizer ignore-call */ 
83
 do_action( 'wordpoints_add_rank', $rank_id );
Loading history...
83
84
	return $rank_id;
85
}
86
87
/**
88
 * Delete a rank.
89
 *
90
 * @since 1.7.0
91
 *
92
 * @param int $id The ID of the rank to delete.
93
 *
94
 * @return bool True if the rank was deleted, false otherwise.
95
 */
96
function wordpoints_delete_rank( $id ) {
97
98
	global $wpdb;
99
100
	$rank = wordpoints_get_rank( $id );
101
102
	if ( ! $rank ) {
103
		return false;
104
	}
105
106
	/**
107
	 * Perform actions before a rank is deleted.
108
	 *
109
	 * @since 1.7.0
110
	 *
111
	 * @param WordPoints_Rank $rank The rank that is being deleted.
112
	 */
113
	do_action( 'wordpoints_pre_delete_rank', $rank );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
	/** @scrutinizer ignore-call */ 
114
 do_action( 'wordpoints_pre_delete_rank', $rank );
Loading history...
114
115
	$deleted = $wpdb->delete(
116
		$wpdb->wordpoints_ranks
117
		, array( 'id' => $id )
118
		, array( '%d' )
119
	);
120
121
	if ( ! $deleted ) {
122
		return false;
123
	}
124
125
	$rank_meta_ids = $wpdb->get_col(
126
		$wpdb->prepare(
127
			"
128
				SELECT meta_id
129
				FROM {$wpdb->wordpoints_rankmeta}
130
				WHERE wordpoints_rank_id = %d
131
			"
132
			, $id
133
		)
134
	); // WPCS: cache pass.
135
136
	WordPoints_Rank_Groups::get_group( $rank->rank_group )->remove_rank( $id );
137
138
	wp_cache_delete( $id, 'wordpoints_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_delete was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
	/** @scrutinizer ignore-call */ 
139
 wp_cache_delete( $id, 'wordpoints_ranks' );
Loading history...
139
140
	foreach ( $rank_meta_ids as $mid ) {
141
		delete_metadata_by_mid( 'wordpoints_rank', $mid );
0 ignored issues
show
Bug introduced by
The function delete_metadata_by_mid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
		/** @scrutinizer ignore-call */ 
142
  delete_metadata_by_mid( 'wordpoints_rank', $mid );
Loading history...
142
	}
143
144
	/**
145
	 * Perform actions when a rank is deleted.
146
	 *
147
	 * @since 1.7.0
148
	 *
149
	 * @param int $rank_id The ID of the rank that was deleted.
150
	 */
151
	do_action( 'wordpoints_delete_rank', $id );
152
153
	return true;
154
}
155
156
/**
157
 * Update a rank.
158
 *
159
 * Existing metadata will not be deleted, but the meta fields passed in $meta
160
 * will be updated.
161
 *
162
 * @since 1.7.0
163
 *
164
 * @param int    $id       The ID of a rank.
165
 * @param string $name     The new name for the rank.
166
 * @param string $type     The type of the rank.
167
 * @param string $group    The slug of the group.
168
 * @param int    $position The position this rank should have in the group.
169
 * @param array  $meta     The new metadata for the rank.
170
 *
171
 * @return bool|WP_Error True the rank was updated successfully, or false/WP_Error on failure.
172
 */
173
function wordpoints_update_rank( $id, $name, $type, $group, $position, array $meta = array() ) {
174
175
	global $wpdb;
176
177
	$rank = wordpoints_get_rank( $id );
178
179
	if ( ! $rank ) {
180
		return false;
181
	}
182
183
	if ( ! WordPoints_Rank_Groups::is_type_registered_for_group( $type, $group ) ) {
184
		return false;
185
	}
186
187
	$rank_type = WordPoints_Rank_Types::get_type( $type );
188
189
	if ( ! $rank_type ) {
190
		return false;
191
	}
192
193
	$meta = $rank_type->validate_rank_meta( $meta );
194
	if ( false === $meta || is_wp_error( $meta ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

194
	if ( false === $meta || /** @scrutinizer ignore-call */ is_wp_error( $meta ) ) {
Loading history...
195
		return $meta;
196
	}
197
198
	/**
199
	 * Perform actions before a rank is updated.
200
	 *
201
	 * @since 1.7.0
202
	 *
203
	 * @param WordPoints_Rank $rank     The rank that is being updated.
204
	 * @param string          $name     The new name for the rank.
205
	 * @param string          $type     The new type of the rank.
206
	 * @param string          $group    The slug of the new group.
207
	 * @param int             $position The new position this rank should have in the group.
208
	 * @param array           $meta     The new metadata for the rank.
209
	 */
210
	do_action( 'wordpoints_pre_update_rank', $rank, $name, $type, $group, $position, $meta );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

210
	/** @scrutinizer ignore-call */ 
211
 do_action( 'wordpoints_pre_update_rank', $rank, $name, $type, $group, $position, $meta );
Loading history...
211
212
	if ( 'utf8' === $wpdb->get_col_charset( $wpdb->wordpoints_ranks, 'name' ) ) {
213
		$name = wp_encode_emoji( $name );
0 ignored issues
show
Bug introduced by
The function wp_encode_emoji was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

213
		$name = /** @scrutinizer ignore-call */ wp_encode_emoji( $name );
Loading history...
214
	}
215
216
	$updated = $wpdb->update(
217
		$wpdb->wordpoints_ranks
218
		, array( 'name' => $name, 'type' => $type, 'rank_group' => $group )
219
		, array( 'id' => $id )
220
		, '%s'
221
		, '%d'
222
	);
223
224
	if ( false === $updated ) {
225
		return false;
226
	}
227
228
	wp_cache_delete( $id, 'wordpoints_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_delete was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

228
	/** @scrutinizer ignore-call */ 
229
 wp_cache_delete( $id, 'wordpoints_ranks' );
Loading history...
229
230
	foreach ( $meta as $meta_key => $meta_value ) {
231
		wordpoints_update_rank_meta( $id, $meta_key, $meta_value );
232
	}
233
234
	$rank_group = WordPoints_Rank_Groups::get_group( $group );
235
236
	if ( $rank->rank_group !== $group ) {
237
238
		$previous_group = WordPoints_Rank_Groups::get_group( $rank->rank_group );
239
		if ( $previous_group ) {
240
			$previous_group->remove_rank( $rank->ID );
241
		}
242
243
		$rank_group->add_rank( $rank->ID, $position );
244
245
	} else {
246
247
		if ( $position !== $rank_group->get_rank_position( $rank->ID ) ) {
248
			$rank_group->move_rank( $rank->ID, $position );
249
		} else {
250
			// If the position doesn't change, we still need refresh the ranks of
251
			// users who have this rank, if the metadata or type has changed.
252
			if ( $meta || $type !== $rank->type ) {
253
				wordpoints_refresh_rank_users( $rank->ID );
254
			}
255
		}
256
	}
257
258
	/**
259
	 * Perform actions when a rank is updated.
260
	 *
261
	 * @since 1.7.0
262
	 *
263
	 * @param int $rank_id The ID of the rank that was updated.
264
	 */
265
	do_action( 'wordpoints_update_rank', $id );
266
267
	return true;
268
}
269
270
/**
271
 * Get the data for a rank.
272
 *
273
 * @since 1.7.0
274
 *
275
 * @param int $id The ID of the rank whose data to get.
276
 *
277
 * @return WordPoints_Rank|false The rank object, or false if it doesn't exist.
278
 */
279
function wordpoints_get_rank( $id ) {
280
281
	$rank = new WordPoints_Rank( $id );
282
283
	if ( ! $rank->exists() ) {
284
		return false;
285
	}
286
287
	return $rank;
288
}
289
290
/**
291
 * Format a rank name for display.
292
 *
293
 * @since 1.7.0
294
 *
295
 * @param int    $rank_id The ID of the rank to format.
296
 * @param string $context The context in which the rank will be displayed.
297
 * @param array  $args    {
298
 *        Other optional arguments.
299
 *
300
 *        @type int $user_id ID of the user the rank is being displayed with.
301
 * }
302
 *
303
 * @return string The integer value of $points formatted for display.
304
 */
305
function wordpoints_format_rank( $rank_id, $context, array $args = array() ) {
306
307
	$rank = wordpoints_get_rank( $rank_id );
308
309
	if ( ! $rank ) {
310
		return false;
311
	}
312
313
	$formatted = '<span class="wordpoints-rank">' . $rank->name . '</span>';
314
315
	/**
316
	 * Format a rank for display.
317
	 *
318
	 * @since 1.7.0
319
	 *
320
	 * @param string          $formatted The formatted rank name.
321
	 * @param WordPoints_Rank $rank      The rank object.
322
	 * @param string          $context   The context in which the rank will be displayed.
323
	 * @param array           $args      {
324
	 *        Other arguments (all optional, may be empty).
325
	 *
326
	 *        @type int $user_id The ID of the user the rank is being displayed with.
327
	 * }
328
	 */
329
	return apply_filters( 'wordpoints_format_rank', $formatted, $rank, $context, $args );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

329
	return /** @scrutinizer ignore-call */ apply_filters( 'wordpoints_format_rank', $formatted, $rank, $context, $args );
Loading history...
330
}
331
332
//
333
// Rank Meta.
334
//
335
336
/**
337
 * Add a meta field for a rank of this type.
338
 *
339
 * @since 1.7.0
340
 * @since 2.1.0 $meta_key and $meta_value are no longer expected slashed.
341
 *
342
 * @see add_metadata() For fuller explanation of args and return value.
343
 *
344
 * @param int    $rank_id    The ID of the rank to add the meta field for.
345
 * @param string $meta_key   The key for this meta field. Not expected slashed.
346
 * @param mixed  $meta_value The value for this meta field. Not expected slashed.
347
 * @param bool   $unique     Whether this meta field must be unique for this rank.
348
 *
349
 * @return int|bool The meta ID on success, false on failure.
350
 */
351
function wordpoints_add_rank_meta( $rank_id, $meta_key, $meta_value, $unique = false ) {
352
353
	return add_metadata(
0 ignored issues
show
Bug introduced by
The function add_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

353
	return /** @scrutinizer ignore-call */ add_metadata(
Loading history...
354
		'wordpoints_rank'
355
		, $rank_id
356
		, wp_slash( $meta_key )
0 ignored issues
show
Bug introduced by
The function wp_slash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

356
		, /** @scrutinizer ignore-call */ wp_slash( $meta_key )
Loading history...
357
		, wp_slash( $meta_value )
358
		, $unique
359
	);
360
}
361
362
/**
363
 * Update a meta field for a rank of this type.
364
 *
365
 * @since 1.7.0
366
 * @since 2.1.0 $meta_key and $meta_value are no longer expected slashed.
367
 *
368
 * @see update_metadata() For fuller explanation of args and return value.
369
 *
370
 * @param int    $rank_id    The ID of the rank to add the meta field for.
371
 * @param string $meta_key   The key for this meta field. Not expected slashed.
372
 * @param mixed  $meta_value The new value for this meta field. Not expected slashed.
373
 * @param mixed  $prev_value The previous value for this meta field.
374
 *
375
 * @return int|bool Meta ID if the key didn't exist, true on successful update,
376
 *                  false on failure.
377
 */
378
function wordpoints_update_rank_meta( $rank_id, $meta_key, $meta_value, $prev_value = '' ) {
379
380
	return update_metadata(
0 ignored issues
show
Bug introduced by
The function update_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

380
	return /** @scrutinizer ignore-call */ update_metadata(
Loading history...
381
		'wordpoints_rank'
382
		, $rank_id
383
		, wp_slash( $meta_key )
0 ignored issues
show
Bug introduced by
The function wp_slash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

383
		, /** @scrutinizer ignore-call */ wp_slash( $meta_key )
Loading history...
384
		, wp_slash( $meta_value )
385
		, $prev_value
386
	);
387
}
388
389
/**
390
 * Delete meta fields for a rank of this type.
391
 *
392
 * @since 1.7.0
393
 * @since 2.1.0 $meta_key and $meta_value are no longer expected slashed.
394
 *
395
 * @see delete_metadata() For fuller explanation of args and return value.
396
 *
397
 * @param int    $rank_id    The ID of the rank to delete metadata of.
398
 * @param string $meta_key   The key for this meta field. Not expected slashed.
399
 * @param mixed  $meta_value The value for this meta field. Default is ''. Not expected slashed.
400
 * @param bool   $delete_all Ignore the rank ID and delete for all ranks. Default: false.
401
 *
402
 * @return bool True on successful delete, false on failure.
403
 */
404
function wordpoints_delete_rank_meta( $rank_id, $meta_key, $meta_value = '', $delete_all = false ) {
405
406
	return delete_metadata(
0 ignored issues
show
Bug introduced by
The function delete_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

406
	return /** @scrutinizer ignore-call */ delete_metadata(
Loading history...
407
		'wordpoints_rank'
408
		, $rank_id
409
		, wp_slash( $meta_key )
0 ignored issues
show
Bug introduced by
The function wp_slash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

409
		, /** @scrutinizer ignore-call */ wp_slash( $meta_key )
Loading history...
410
		, wp_slash( $meta_value )
411
		, $delete_all
412
	);
413
}
414
415
/**
416
 * Get metadata for a rank.
417
 *
418
 * @since 1.7.0
419
 *
420
 * @see get_metadata() For fuller explanation of args and return value.
421
 *
422
 * @param int    $rank_id  The ID of the rank to get the meta field for.
423
 * @param string $meta_key The meta key whose value to get.
424
 * @param bool   $single   Whether to retrieve a single value for this key.
425
 *
426
 * @return string|array Single metadata value or array of metadata values.
427
 */
428
function wordpoints_get_rank_meta( $rank_id, $meta_key = '', $single = false ) {
429
430
	return get_metadata( 'wordpoints_rank', $rank_id, $meta_key, $single );
0 ignored issues
show
Bug introduced by
The function get_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

430
	return /** @scrutinizer ignore-call */ get_metadata( 'wordpoints_rank', $rank_id, $meta_key, $single );
Loading history...
431
}
432
433
//
434
// User Ranks.
435
//
436
437
/**
438
 * Get a user's rank.
439
 *
440
 * @since 1.7.0
441
 *
442
 * @param int    $user_id The ID of the user whose rank to get.
443
 * @param string $group   The rank group to get the rank from.
444
 *
445
 * @return int|false The ID of the rank this user has, or false for invalid args.
446
 */
447
function wordpoints_get_user_rank( $user_id, $group ) {
448
449
	global $wpdb;
450
451
	if ( ! wordpoints_posint( $user_id ) ) {
452
		return false;
453
	}
454
455
	$rank_group = WordPoints_Rank_Groups::get_group( $group );
456
457
	if ( ! $rank_group ) {
458
		return false;
459
	}
460
461
	$group_ranks = wp_cache_get( $group, 'wordpoints_user_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

461
	$group_ranks = /** @scrutinizer ignore-call */ wp_cache_get( $group, 'wordpoints_user_ranks' );
Loading history...
462
463
	foreach ( (array) $group_ranks as $_rank_id => $user_ids ) {
464
		if ( isset( $user_ids[ $user_id ] ) ) {
465
			$rank_id = $_rank_id;
466
			break;
467
		}
468
	}
469
470
	if ( ! isset( $rank_id ) ) {
471
472
		$rank_id = $wpdb->get_var(
473
			$wpdb->prepare(
474
				"
475
					SELECT `rank_id`
476
					FROM `{$wpdb->wordpoints_user_ranks}`
477
					WHERE `user_id` = %d
478
						AND `rank_group` = %s
479
						AND `blog_id` = %d
480
						AND `site_id` = %d
481
				"
482
				, $user_id
483
				, $group
484
				, is_multisite() ? get_current_blog_id() : '0'
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

484
				, /** @scrutinizer ignore-call */ is_multisite() ? get_current_blog_id() : '0'
Loading history...
Bug introduced by
The function get_current_blog_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

484
				, is_multisite() ? /** @scrutinizer ignore-call */ get_current_blog_id() : '0'
Loading history...
485
				, $wpdb->siteid
486
			)
487
		);
488
489
		if ( ! $rank_id ) {
490
			$rank_id = $rank_group->get_base_rank();
491
		}
492
493
		$group_ranks[ $rank_id ][ $user_id ] = $user_id;
494
495
		wp_cache_set( $group, $group_ranks, 'wordpoints_user_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_set was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

495
		/** @scrutinizer ignore-call */ 
496
  wp_cache_set( $group, $group_ranks, 'wordpoints_user_ranks' );
Loading history...
496
	}
497
498
	return (int) $rank_id;
499
}
500
501
/**
502
 * Get the rank of a user formatted for display.
503
 *
504
 * @since 1.7.0
505
 *
506
 * @param int    $user_id The ID of the user.
507
 * @param string $group   The rank group.
508
 * @param string $context The context in which this rank is being displayed.
509
 * @param array  $args    Other arguments.
510
 *
511
 * @return string|false The rank of this user formatted for dispay, or false.
512
 */
513
function wordpoints_get_formatted_user_rank( $user_id, $group, $context, array $args = array() ) {
514
515
	$rank_id = wordpoints_get_user_rank( $user_id, $group );
516
517
	if ( ! $rank_id ) {
518
		return false;
519
	}
520
521
	$args = array_merge( $args, array( 'user_id' => $user_id ) );
522
523
	return wordpoints_format_rank( $rank_id, $context, $args );
524
}
525
526
/**
527
 * Update a user's rank.
528
 *
529
 * @since 1.7.0
530
 *
531
 * @param int $user_id The ID of the user.
532
 * @param int $rank_id The ID of the rank to give the user.
533
 *
534
 * @return bool True if the update was successful. False otherwise.
535
 */
536
function wordpoints_update_user_rank( $user_id, $rank_id ) {
537
538
	global $wpdb;
539
540
	if ( ! wordpoints_posint( $rank_id ) || ! wordpoints_posint( $user_id ) ) {
541
		return false;
542
	}
543
544
	$rank = wordpoints_get_rank( $rank_id );
545
546
	if ( ! $rank ) {
547
		return false;
548
	}
549
550
	$old_rank_id = wordpoints_get_user_rank( $user_id, $rank->rank_group );
551
552
	if ( $rank_id === $old_rank_id ) {
553
		return true;
554
	}
555
556
	$result = $wpdb->query(
557
		$wpdb->prepare(
558
			"
559
				INSERT INTO `{$wpdb->wordpoints_user_ranks}`
560
					(`user_id`, `rank_id`, `rank_group`, `blog_id`, `site_id`)
561
				VALUES
562
					(%d, %d, %s, %d, %d)
563
				ON DUPLICATE KEY
564
					UPDATE `rank_id` = %d
565
			"
566
			, $user_id
567
			, $rank_id
568
			, $rank->rank_group
569
			, is_multisite() ? get_current_blog_id() : '0'
0 ignored issues
show
Bug introduced by
The function get_current_blog_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

569
			, is_multisite() ? /** @scrutinizer ignore-call */ get_current_blog_id() : '0'
Loading history...
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

569
			, /** @scrutinizer ignore-call */ is_multisite() ? get_current_blog_id() : '0'
Loading history...
570
			, $wpdb->siteid
571
			, $rank_id
572
		)
573
	);
574
575
	if ( false === $result ) {
576
		return false;
577
	}
578
579
	$group_ranks = wp_cache_get( $rank->rank_group, 'wordpoints_user_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

579
	$group_ranks = /** @scrutinizer ignore-call */ wp_cache_get( $rank->rank_group, 'wordpoints_user_ranks' );
Loading history...
580
581
	foreach ( $group_ranks as $_rank_id => $user_ids ) {
582
		unset( $group_ranks[ $_rank_id ][ $user_id ] );
583
	}
584
585
	wp_cache_set( $rank->rank_group, $group_ranks, 'wordpoints_user_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_set was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

585
	/** @scrutinizer ignore-call */ 
586
 wp_cache_set( $rank->rank_group, $group_ranks, 'wordpoints_user_ranks' );
Loading history...
586
587
	unset( $group_ranks );
588
589
	/**
590
	 * Perform actions when a user rank is updated.
591
	 *
592
	 * @since 1.7.0
593
	 *
594
	 * @param int $user_id     The ID of the user.
595
	 * @param int $new_rank_id The ID of the new rank the user has.
596
	 * @param int $old_rank_id The ID of the old rank the user used to have.
597
	 */
598
	do_action( 'wordpoints_update_user_rank', $user_id, $rank_id, $old_rank_id );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

598
	/** @scrutinizer ignore-call */ 
599
 do_action( 'wordpoints_update_user_rank', $user_id, $rank_id, $old_rank_id );
Loading history...
599
600
	return true;
601
}
602
603
/**
604
 * Updates a set of users to a new rank.
605
 *
606
 * All of the users must have previously had the same rank. This is mainly necessary
607
 * so that we can pass the old rank ID to the 'wordpoints_update_user_rank' action,
608
 * and for purposes of clearing the cache.
609
 *
610
 * @since 2.4.0
611
 *
612
 * @param int[] $user_ids     The IDs of the users to update.
613
 * @param int   $to_rank_id   The ID of the rank to give the users.
614
 * @param int   $from_rank_id The ID of the rank the users previously had.
615
 *
616
 * @return bool True if the update was successful. False otherwise.
617
 */
618
function wordpoints_update_users_to_rank( array $user_ids, $to_rank_id, $from_rank_id ) {
619
620
	global $wpdb;
621
622
	if ( ! wordpoints_posint( $to_rank_id ) || ! wordpoints_posint( $to_rank_id ) ) {
623
		return false;
624
	}
625
626
	$rank = wordpoints_get_rank( $to_rank_id );
627
628
	if ( ! $rank ) {
629
		return false;
630
	}
631
632
	if ( $to_rank_id === $from_rank_id ) {
633
		return true;
634
	}
635
636
	$prepared = $wpdb->prepare(
637
		', %d, %s, %d, %d'
638
		, $to_rank_id
639
		, $rank->rank_group
640
		, is_multisite() ? get_current_blog_id() : '0'
0 ignored issues
show
Bug introduced by
The function get_current_blog_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

640
		, is_multisite() ? /** @scrutinizer ignore-call */ get_current_blog_id() : '0'
Loading history...
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

640
		, /** @scrutinizer ignore-call */ is_multisite() ? get_current_blog_id() : '0'
Loading history...
641
		, $wpdb->siteid
642
	);
643
644
	$result = $wpdb->query( // WPCS: unprepared SQL OK.
645
		$wpdb->prepare( // WPCS: unprepared SQL OK.
646
			"
647
				INSERT INTO `{$wpdb->wordpoints_user_ranks}`
648
					(`user_id`, `rank_id`, `rank_group`, `blog_id`, `site_id`)
649
				VALUES
650
					(" . implode( array_map( 'absint', $user_ids ), "{$prepared}),\n\t\t\t\t\t(" ) . "{$prepared})
651
				ON DUPLICATE KEY
652
					UPDATE `rank_id` = %d
653
			"
654
			, $to_rank_id
655
		)
656
	);
657
658
	if ( false === $result ) {
659
		return false;
660
	}
661
662
	$group_ranks = wp_cache_get( $rank->rank_group, 'wordpoints_user_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

662
	$group_ranks = /** @scrutinizer ignore-call */ wp_cache_get( $rank->rank_group, 'wordpoints_user_ranks' );
Loading history...
663
664
	unset( $group_ranks[ $from_rank_id ], $group_ranks[ $to_rank_id ] );
665
666
	wp_cache_set( $rank->rank_group, $group_ranks, 'wordpoints_user_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_set was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

666
	/** @scrutinizer ignore-call */ 
667
 wp_cache_set( $rank->rank_group, $group_ranks, 'wordpoints_user_ranks' );
Loading history...
667
668
	unset( $group_ranks );
669
670
	if ( has_action( 'wordpoints_update_user_rank' ) ) {
0 ignored issues
show
Bug introduced by
The function has_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

670
	if ( /** @scrutinizer ignore-call */ has_action( 'wordpoints_update_user_rank' ) ) {
Loading history...
671
		foreach ( $user_ids as $user_id ) {
672
			/**
673
			 * Perform actions when a user rank is updated.
674
			 *
675
			 * @since 1.7.0
676
			 *
677
			 * @param int $user_id The ID of the user.
678
			 * @param int $new_rank_id The ID of the new rank the user has.
679
			 * @param int $old_rank_id The ID of the old rank the user used to have.
680
			 */
681
			do_action(
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

681
			/** @scrutinizer ignore-call */ 
682
   do_action(
Loading history...
682
				'wordpoints_update_user_rank'
683
				, $user_id
684
				, $to_rank_id
685
				, $from_rank_id
686
			);
687
		}
688
	}
689
690
	return true;
691
}
692
693
/**
694
 * Get an array of all the users who have a given rank.
695
 *
696
 * @since 1.7.0
697
 * @deprecated 2.4.0 Use the WordPoints_User_Ranks_Query class instead.
698
 *
699
 * @param int $rank_id The ID of the rank.
700
 *
701
 * @return int[]|false Array of user IDs or false if the $rank_id is invalid.
702
 */
703
function wordpoints_get_users_with_rank( $rank_id ) {
704
705
	_deprecated_function( __FUNCTION__, '2.4.0', 'WordPoints_User_Ranks_Query class' );
0 ignored issues
show
Bug introduced by
The function _deprecated_function was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

705
	/** @scrutinizer ignore-call */ 
706
 _deprecated_function( __FUNCTION__, '2.4.0', 'WordPoints_User_Ranks_Query class' );
Loading history...
706
707
	$rank = wordpoints_get_rank( $rank_id );
708
709
	if ( ! $rank ) {
710
		return false;
711
	}
712
713
	$query = new WordPoints_User_Ranks_Query(
714
		array( 'fields' => 'user_id', 'rank_id' => $rank_id )
715
	);
716
717
	$user_ids = $query->get( 'col' );
718
719
	if ( false === $user_ids ) {
720
		return false;
721
	}
722
723
	$user_ids = array_map( 'intval', $user_ids );
724
725
	return $user_ids;
726
}
727
728
/**
729
 * Refresh the standings of users who have a certain rank.
730
 *
731
 * This function is called when a rank is updated to reset the user standings.
732
 *
733
 * @since 1.7.0
734
 *
735
 * @param int $rank_id The ID of the rank to refresh.
736
 */
737
function wordpoints_refresh_rank_users( $rank_id ) {
738
739
	$rank = wordpoints_get_rank( $rank_id );
740
741
	if ( ! $rank || 'base' === $rank->type ) {
742
		return;
743
	}
744
745
	$prev_rank = $rank->get_adjacent( -1 );
746
747
	if ( ! $prev_rank ) {
748
		return;
749
	}
750
751
	// Maybe decrease users who have this rank.
752
	$maybe_decrease = new WordPoints_User_Ranks_Maybe_Decrease( $rank );
753
	$maybe_decrease->run();
754
755
	// Also maybe increase users who have the previous rank.
756
	$maybe_increase = new WordPoints_User_Ranks_Maybe_Increase( $prev_rank );
757
	$maybe_increase->run();
758
}
759
760
/**
761
 * Sets the rank of a new user when they become a member of the site.
762
 *
763
 * @since 2.4.0
764
 *
765
 * @WordPress\action user_register
766
 * @WordPress\action add_user_to_blog
767
 *
768
 * @param int $user_id The ID of the user.
769
 */
770
function wordpoints_set_new_user_ranks( $user_id ) {
771
772
	foreach ( WordPoints_Rank_Groups::get() as $rank_group ) {
773
774
		$base_rank = wordpoints_get_rank( $rank_group->get_base_rank() );
0 ignored issues
show
Bug introduced by
It seems like $rank_group->get_base_rank() can also be of type false; however, parameter $id of wordpoints_get_rank() 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

774
		$base_rank = wordpoints_get_rank( /** @scrutinizer ignore-type */ $rank_group->get_base_rank() );
Loading history...
775
776
		if ( ! $base_rank ) {
777
			continue;
778
		}
779
780
		$rank_type = WordPoints_Rank_Types::get_type( $base_rank->type );
781
782
		if ( ! $rank_type ) {
783
			continue;
784
		}
785
786
		$new_rank = $rank_type->maybe_increase_user_rank(
787
			$user_id
788
			, $base_rank
789
		);
790
791
		// If the user should have the base rank we can't use the update function
792
		// because it will check the user's current rank, which will be inferred as
793
		// the base rank even if it isn't set in the database.
794
		if ( $base_rank->ID !== $new_rank->ID ) {
795
796
			wordpoints_update_user_rank( $user_id, $new_rank->ID );
797
798
		} else {
799
800
			global $wpdb;
801
802
			$wpdb->query(
803
				$wpdb->prepare(
804
					"
805
						INSERT INTO `{$wpdb->wordpoints_user_ranks}`
806
							(`user_id`, `rank_id`, `rank_group`, `blog_id`, `site_id`)
807
						VALUES
808
							(%d, %d, %s, %d, %d)
809
						ON DUPLICATE KEY
810
							UPDATE `rank_id` = %d
811
					"
812
					, $user_id
813
					, $base_rank->ID
814
					, $base_rank->rank_group
815
					, is_multisite() ? get_current_blog_id() : '0'
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

815
					, /** @scrutinizer ignore-call */ is_multisite() ? get_current_blog_id() : '0'
Loading history...
Bug introduced by
The function get_current_blog_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

815
					, is_multisite() ? /** @scrutinizer ignore-call */ get_current_blog_id() : '0'
Loading history...
816
					, $wpdb->siteid
817
					, $base_rank->ID
818
				)
819
			); // WPCS: cache OK.
820
		}
821
	}
822
}
823
824
/**
825
 * Deletes all ranks for a user on this site.
826
 *
827
 * @since 2.4.0
828
 *
829
 * @WordPress\action user_deleted          On single-site installs.
830
 * @WordPress\action remove_user_from_blog On multisite.
831
 *
832
 * @param int $user_id The ID of the user whose ranks to delete.
833
 */
834
function wordpoints_delete_user_ranks( $user_id ) {
835
836
	global $wpdb;
837
838
	$wpdb->delete(
839
		$wpdb->wordpoints_user_ranks
840
		, array(
841
			'user_id' => $user_id,
842
			'blog_id' => is_multisite() ? get_current_blog_id() : '0',
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

842
			'blog_id' => /** @scrutinizer ignore-call */ is_multisite() ? get_current_blog_id() : '0',
Loading history...
Bug introduced by
The function get_current_blog_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

842
			'blog_id' => is_multisite() ? /** @scrutinizer ignore-call */ get_current_blog_id() : '0',
Loading history...
843
			'site_id' => $wpdb->siteid,
844
		)
845
		, '%d'
846
	);
847
848
	foreach ( WordPoints_Rank_Groups::get() as $rank_group ) {
849
850
		$group_ranks = wp_cache_get( $rank_group->slug, 'wordpoints_user_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

850
		$group_ranks = /** @scrutinizer ignore-call */ wp_cache_get( $rank_group->slug, 'wordpoints_user_ranks' );
Loading history...
851
852
		if ( ! is_array( $group_ranks ) ) {
853
			continue;
854
		}
855
856
		foreach ( $group_ranks as $rank_id => $user_ids ) {
857
			unset( $group_ranks[ $rank_id ][ $user_id ] );
858
		}
859
860
		wp_cache_set( $rank_group->slug, $group_ranks, 'wordpoints_user_ranks' );
0 ignored issues
show
Bug introduced by
The function wp_cache_set was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

860
		/** @scrutinizer ignore-call */ 
861
  wp_cache_set( $rank_group->slug, $group_ranks, 'wordpoints_user_ranks' );
Loading history...
861
	}
862
}
863
864
/**
865
 * Register the included rank types.
866
 *
867
 * @since 1.7.0
868
 *
869
 * @WordPress\action wordpoints_ranks_register
870
 */
871
function wordpoints_register_core_ranks() {}
872
873
// EOF
874