· 6 years ago · Apr 29, 2019, 05:30 PM
1<?php
2if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
4/**
5 * myCRED_Settings class
6 * @see http://codex.mycred.me/classes/mycred_settings/
7 * @since 0.1
8 * @version 1.6
9 */
10if ( ! class_exists( 'myCRED_Settings' ) ) :
11 class myCRED_Settings {
12
13 /**
14 * The default point type key
15 */
16 private $default_cred_id = '';
17
18 /**
19 * The current point type key
20 */
21 public $cred_id = '';
22
23 /**
24 * Indicates if this is the main type or not
25 */
26 private $is_main_type = false;
27
28 /**
29 * The Log database table
30 */
31 public $log_table = '';
32
33 /**
34 * Indicates if we are using multisite
35 */
36 public $is_multisite = false;
37
38 /**
39 * Indicates if the master template feature is in use
40 */
41 public $use_master_template = false;
42
43 /**
44 * Indicates if the central logging feature is in use
45 */
46 public $use_central_logging = false;
47
48 /**
49 * The point type settings option key
50 */
51 private $option_id = '';
52
53 /**
54 * The point type settings array
55 */
56 public $core = array();
57
58 /**
59 * Construct
60 * @since 1.0
61 * @version 1.8
62 */
63 public function __construct( $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
64
65 // The point type key
66 $this->default_cred_id = MYCRED_DEFAULT_TYPE_KEY;
67 $this->cred_id = ( ( ! is_string( $point_type ) || sanitize_key( $point_type ) == '' || $point_type === NULL ) ? $this->default_cred_id : $point_type );
68 $this->is_main_type = ( ( $this->cred_id == $this->default_cred_id ) ? true : false );
69
70 // Log table
71 $this->log_table = $this->get_log_table();
72
73 // Multisite related
74 $this->is_multisite = is_multisite();
75 $this->use_master_template = mycred_override_settings();
76 $this->use_central_logging = mycred_centralize_log();
77
78 // Option ID
79 $this->option_id = 'mycred_pref_core';
80 if ( ! $this->is_main_type )
81 $this->option_id .= '_' . $this->cred_id;
82
83 // The point type settings
84 $this->core = $this->get_point_type_settings();
85 if ( $this->core !== false ) {
86 foreach ( $this->core as $key => $value ) {
87 $this->$key = $value;
88 }
89 }
90
91 do_action_ref_array( 'mycred_settings', array( &$this ) );
92
93 }
94
95 /**
96 * Default Settings
97 * @since 1.3
98 * @version 1.1
99 */
100 public function defaults() {
101
102 return array(
103 'format' => array(
104 'type' => 'bigint',
105 'decimals' => 0,
106 'separators' => array(
107 'decimal' => '.',
108 'thousand' => ','
109 )
110 ),
111 'name' => array(
112 'singular' => __( 'Point', 'mycred' ),
113 'plural' => __( 'Points', 'mycred' )
114 ),
115 'before' => '',
116 'after' => '',
117 'caps' => array(
118 'plugin' => 'manage_options',
119 'creds' => 'export'
120 ),
121 'max' => 0,
122 'exclude' => array(
123 'plugin_editors' => 0,
124 'cred_editors' => 0,
125 'list' => ''
126 ),
127 'frequency' => array(
128 'rate' => 'always',
129 'date' => ''
130 ),
131 'delete_user' => 0
132 );
133
134 }
135
136 /**
137 * Get Point Type Settings
138 * @since 1.8
139 * @version 1.0
140 */
141 public function get_point_type_settings() {
142
143 $defaults = $this->defaults();
144 $settings = mycred_get_option( $this->option_id, $defaults );
145
146 return apply_filters( 'mycred_get_point_type_settings', $settings, $defaults, $this );
147
148 }
149
150 /**
151 * Default Settings
152 * @since 1.8
153 * @version 1.0
154 */
155 public function get_log_table() {
156
157 global $wpdb;
158
159 if ( $this->is_multisite && $this->use_central_logging )
160 $wp_prefix = $wpdb->base_prefix;
161 else
162 $wp_prefix = $wpdb->prefix;
163
164 $table_name = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $wp_prefix . 'myCRED_log' ) );
165
166 if( $table_name == NULL ) {
167 $table_name = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $wp_prefix . 'mycred_log' ) );
168
169 if( $table_name == NULL )
170 $table_name = $wp_prefix . 'myCRED_log';
171 }
172
173 if ( defined( 'MYCRED_LOG_TABLE' ) )
174 $table_name = MYCRED_LOG_TABLE;
175
176 return $table_name;
177
178 }
179
180 /**
181 * The Point Types Name - Singular form
182 * @since 0.1
183 * @version 1.2
184 */
185 public function singular() {
186
187 return $this->name['singular'];
188
189 }
190
191 /**
192 * The Point Types Name - Plural form
193 * @since 0.1
194 * @version 1.2
195 */
196 public function plural() {
197
198 return $this->name['plural'];
199
200 }
201
202 /**
203 * Zero
204 * Returns zero formated with or without decimals.
205 * @since 1.3
206 * @version 1.0
207 */
208 public function zero() {
209
210 return number_format( 0, $this->format['decimals'] );
211
212 }
213
214 /**
215 * Number
216 * Returns a given creds formated either as a float with the set number of decimals or as a integer.
217 * This function should be used when you need to make sure the variable is returned in correct format
218 * but without any custom layout you might have given your creds.
219 *
220 * @param $number (int|float) the initial number
221 * @returns the given number formated either as an integer or float
222 * @since 0.1
223 * @version 1.2
224 */
225 public function number( $value = NULL ) {
226
227 if ( $value === NULL ) return $value;
228
229 $decimals = $this->format['decimals'];
230 $value = str_replace( '+', '', $value );
231
232 if ( $decimals > 0 )
233 return (float) number_format( (float) $value, $decimals, '.', '' );
234
235 return (int) $value;
236
237 }
238
239 /**
240 * Format Number
241 * Returns a given creds formated with set decimal and thousands separator and either as a float with
242 * the set number of decimals or as a integer. This function should be used when you want to display creds
243 * formated according to your settings. Do not use this function when adding/removing points!
244 *
245 * @param $number (int|float) the initial number
246 * @returns the given number formated either as an integer or float
247 * @filter 'mycred_format_number'
248 * @since 0.1
249 * @version 1.1
250 */
251 public function format_number( $value = NULL ) {
252
253 if ( $value === NULL ) return $value;
254
255 $value = $this->number( $value );
256 $decimals = $this->format['decimals'];
257 $sep_dec = $this->format['separators']['decimal'];
258 $sep_tho = $this->format['separators']['thousand'];
259
260 // Format
261 $creds = number_format( $value, (int) $decimals, $sep_dec, $sep_tho );
262
263 return apply_filters( 'mycred_format_number', $creds, $value, $this->core );
264
265 }
266
267 /**
268 * Format Creds
269 * Returns a given number formated with prefix and/or suffix along with any custom presentation set.
270 *
271 * @param $creds (int|float) number of creds
272 * @param $before (string) optional string to insert before the number
273 * @param $after (string) optional string to isnert after the number
274 * @param $force_in (boolean) option to force $before after prefix and $after before suffix
275 * @filter 'mycred_format_creds'
276 * @returns formated string
277 * @since 0.1
278 * @version 1.1
279 */
280 public function format_creds( $value = 0, $before = '', $after = '', $force_in = false ) {
281
282 // Prefix
283 $prefix = ( ! empty( $this->before ) ) ? $this->before . ' ' : '';
284
285 // Suffix
286 $suffix = ( ! empty( $this->after ) ) ? ' ' . $this->after : '';
287
288 // Layout
289 $layout = $before . $prefix . $this->format_number( $value ) . $suffix . $after;
290 if ( $force_in )
291 $layout = $prefix . $before . $this->format_number( $value ) . $after . $suffix;
292
293 return apply_filters( 'mycred_format_creds', $layout, $value, $this );
294
295 }
296
297 /**
298 * Round Value
299 * Will round a given value either up or down with the option to use precision.
300 *
301 * @param $amount (int|float) required amount to round
302 * @param $up_down (string|boolean) choice of rounding up or down. using false bypasses this function
303 * @param $precision (int) the optional number of decimal digits to round to. defaults to 0
304 * @returns rounded int or float
305 * @since 0.1
306 * @version 1.1
307 */
308 public function round_value( $value = NULL, $up_down = false, $precision = 0 ) {
309
310 if ( $value === NULL || ! $up_down ) return $amount;
311
312 // Use round() for precision
313 $original_value = $value;
314 if ( $precision !== false ) {
315
316 if ( $up_down == 'up' )
317 $value = round( $value, (int) $precision, PHP_ROUND_HALF_UP );
318
319 elseif ( $up_down == 'down' )
320 $value = round( $value, (int) $precision, PHP_ROUND_HALF_DOWN );
321
322 }
323
324 // Use ceil() or floor() for everything else
325 else {
326
327 if ( $up_down == 'up' )
328 $value = ceil( $value );
329
330 elseif ( $up_down == 'down' )
331 $value = floor( $value );
332
333 }
334
335 return apply_filters( 'mycred_round_value', $value, $original_value, $up_down, $precision );
336
337 }
338
339 /**
340 * Get Lowest Value
341 * Returns the lowest point value available based on the number of decimal places
342 * we use. So with 1 decimal = 0.1, 2 decimals 0.01 etc. Defaults to 1.
343 * @since 1.7
344 * @version 1.1
345 */
346 public function get_lowest_value() {
347
348 $lowest = 1;
349 $decimals = $this->format['decimals'] - 1;
350
351 if ( $decimals > 0 ) {
352
353 $lowest = '0.' . str_repeat( '0', $decimals ) . '1';
354 $lowest = (float) $lowest;
355
356 }
357
358 return $lowest;
359
360 }
361
362 /**
363 * Apply Exchange Rate
364 * Applies a given exchange rate to the given amount.
365 *
366 * @param $amount (int|float) the initial amount
367 * @param $rate (int|float) the exchange rate to devide by
368 * @param $round (bool) option to round values, defaults to yes.
369 * @since 0.1
370 * @version 1.3
371 */
372 public function apply_exchange_rate( $amount = 0, $rate = 1 ) {
373
374 $value = $amount;
375 if ( $rate != 1 ) {
376
377 // Make sure we are not passing decimals without a leading zero
378 if ( substr( $rate, 0, 1 ) === '.' )
379 $rate = (float) '0' . $rate;
380
381 $value = $amount / $rate;
382
383 $value = $this->number( $value );
384
385 }
386
387 return apply_filters( 'mycred_apply_exchange_rate', $value, $amount, $rate );
388
389 }
390
391 /**
392 * Parse Template Tags
393 * Parses template tags in a given string by checking for the 'ref_type' array key under $log_entry->data.
394 * @since 0.1
395 * @version 1.0
396 */
397 public function parse_template_tags( $content = '', $log_entry ) {
398
399 // Prep
400 $reference = $log_entry->ref;
401 $ref_id = $log_entry->ref_id;
402 $data = $log_entry->data;
403
404 // Unserialize if serialized
405 $data = maybe_unserialize( $data );
406
407 // Run basic template tags first
408 $content = $this->template_tags_general( $content );
409
410 // Start by allowing others to play
411 $content = apply_filters( 'mycred_parse_log_entry', $content, $log_entry );
412 $content = apply_filters( "mycred_parse_log_entry_{$reference}", $content, $log_entry );
413
414 // Get the reference type
415 if ( isset( $data['ref_type'] ) || isset( $data['post_type'] ) ) {
416
417 if ( isset( $data['ref_type'] ) )
418 $type = $data['ref_type'];
419
420 elseif ( isset( $data['post_type'] ) )
421 $type = $data['post_type'];
422
423 if ( $type == 'post' )
424 $content = $this->template_tags_post( $content, $ref_id, $data );
425
426 elseif ( $type == 'user' )
427 $content = $this->template_tags_user( $content, $ref_id, $data );
428
429 elseif ( $type == 'comment' )
430 $content = $this->template_tags_comment( $content, $ref_id, $data );
431
432 $content = apply_filters( "mycred_parse_tags_{$type}", $content, $log_entry );
433
434 }
435
436 return $content;
437
438 }
439
440 /**
441 * General Template Tags
442 * Replaces the general template tags in a given string.
443 * @since 0.1
444 * @version 1.2
445 */
446 public function template_tags_general( $content = '' ) {
447
448 $content = apply_filters( 'mycred_parse_tags_general', $content );
449
450 // Singular
451 $content = str_replace( array( '%singular%', '%Singular%' ), $this->singular(), $content );
452 $content = str_replace( '%_singular%', strtolower( $this->singular() ), $content );
453
454 // Plural
455 $content = str_replace( array( '%plural%', '%Plural%' ), $this->plural(), $content );
456 $content = str_replace( '%_plural%', strtolower( $this->plural() ), $content );
457
458 // Login URL
459 $content = str_replace( '%login_url%', wp_login_url(), $content );
460 $content = str_replace( '%login_url_here%', wp_login_url( mycred_get_permalink() ), $content );
461
462 // Logout URL
463 $content = str_replace( '%logout_url%', wp_logout_url(), $content );
464 $content = str_replace( '%logout_url_here%', wp_logout_url( mycred_get_permalink() ), $content );
465
466 // Blog Related
467 if ( preg_match( '%(num_members|blog_name|blog_url|blog_info|admin_email)%', $content, $matches ) ) {
468 $content = str_replace( '%num_members%', $this->count_members(), $content );
469 $content = str_replace( '%blog_name%', get_bloginfo( 'name' ), $content );
470 $content = str_replace( '%blog_url%', get_bloginfo( 'url' ), $content );
471 $content = str_replace( '%blog_info%', get_bloginfo( 'description' ), $content );
472 $content = str_replace( '%admin_email%', get_bloginfo( 'admin_email' ), $content );
473 }
474
475 return $content;
476
477 }
478
479 /**
480 * Amount Template Tags
481 * Replaces the amount template tags in a given string.
482 * @since 0.1
483 * @version 1.0.3
484 */
485 public function template_tags_amount( $content = '', $amount = 0 ) {
486
487 $content = $this->template_tags_general( $content );
488 if ( ! $this->has_tags( 'amount', 'cred|cred_f', $content ) ) return $content;
489
490 $content = apply_filters( 'mycred_parse_tags_amount', $content, $amount, $this );
491 $content = str_replace( '%cred_f%', $this->format_creds( $amount ), $content );
492 $content = str_replace( '%cred%', $amount, $content );
493
494 return $content;
495
496 }
497
498 /**
499 * Post Related Template Tags
500 * Replaces the post related template tags in a given string.
501 * @param $content (string) string containing the template tags
502 * @param $ref_id (int) required post id as reference id
503 * @param $data (object) Log entry data object
504 * @param $link_target (string) Optional link target to add to any links
505 * @return (string) parsed string
506 * @since 0.1
507 * @version 1.1
508 */
509 public function template_tags_post( $content = '', $ref_id = NULL, $data = '', $link_target = '' ) {
510
511 if ( $ref_id === NULL ) return $content;
512
513 $content = $this->template_tags_general( $content );
514 if ( ! $this->has_tags( 'post', 'post_title|post_url|link_with_title|post_type', $content ) ) return $content;
515
516 if ( $link_target != '' )
517 $link_target = ' target="' . esc_attr( $link_target ) . '"';
518
519 // Get Post Object
520 $post = mycred_get_post( $ref_id );
521 $post_url = '#item-has-been-deleted';
522
523 // Post does not exist - see if we can re-construct
524 if ( !isset( $post->ID ) ) {
525
526 // Nope, no backup, bye
527 if ( ! is_array( $data ) || ! array_key_exists( 'ID', $data ) ) return $content;
528
529 // Re-construct
530 $post = new StdClass();
531 foreach ( $data as $key => $value ) {
532
533 if ( $key == 'post_title' )
534 $value .= ' (' . __( 'Deleted', 'mycred' ) . ')';
535
536 $post->$key = $value;
537
538 }
539
540 }
541 else {
542
543 $post_url = mycred_get_permalink( $post );
544
545 }
546
547 // Let others play first
548 $content = apply_filters( 'mycred_parse_tags_post', $content, $post, $data );
549
550 // Replace template tags
551 $content = str_replace( '%post_title%', esc_attr( mycred_get_the_title( $post ) ), $content );
552 $content = str_replace( '%post_url%', esc_url_raw( $post_url ), $content );
553 $content = str_replace( '%link_with_title%', '<a href="' . esc_url_raw( $post_url ) . '"' . $link_target . '>' . esc_attr( $post->post_title ) . '</a>', $content );
554
555 // Post type template tag
556 $post_type = get_post_type_object( $post->post_type );
557 if ( $post_type !== NULL )
558 $content = str_replace( '%post_type%', $post_type->labels->singular_name, $content );
559
560 return $content;
561
562 }
563
564 /**
565 * User Related Template Tags
566 * Replaces the user related template tags in the given string.
567 * @param $content (string) string containing the template tags
568 * @param $ref_id (int) required user id as reference id
569 * @param $data (object) Log entry data object
570 * @param $link_target (string) Optional link target to add to any links
571 * @return (string) parsed string
572 * @since 0.1
573 * @version 1.4
574 */
575 public function template_tags_user( $content = '', $ref_id = NULL, $data = '', $link_target = '' ) {
576
577 if ( $ref_id === NULL ) return $content;
578
579 $content = $this->template_tags_general( $content );
580 if ( ! $this->has_tags( 'user', 'user_id|user_name|user_name_en|display_name|user_profile_url|user_profile_link|user_nicename|user_email|user_url|balance|balance_f', $content ) ) return $content;
581
582 // Get User Object
583 if ( $ref_id !== false )
584 $user = get_userdata( $ref_id );
585
586 // User object is passed on though $data
587 elseif ( $ref_id === false && is_object( $data ) && isset( $data->ID ) )
588 $user = $data;
589
590 // User array is passed on though $data
591 elseif ( $ref_id === false && is_array( $data ) || array_key_exists( 'ID', (array) $data ) ) {
592
593 $user = new StdClass();
594 foreach ( $data as $key => $value ) {
595
596 if ( $key == 'login' )
597 $user->user_login = $value;
598
599 else
600 $user->$key = $value;
601
602 }
603
604 }
605
606 else return $content;
607
608 // Let others play first
609 $content = apply_filters( 'mycred_parse_tags_user', $content, $user, $data );
610
611 if ( ! isset( $user->ID ) ) return $content;
612
613 // Replace template tags
614 $content = str_replace( '%user_id%', $user->ID, $content );
615 $content = str_replace( '%user_name%', esc_attr( $user->user_login ), $content );
616 $content = str_replace( '%user_name_en%', urlencode( $user->user_login ), $content );
617
618 $profile_url = mycred_get_users_profile_url( $user->ID );
619
620 if ( $link_target != '' )
621 $link_target = ' target="' . esc_attr( $link_target ) . '"';
622
623 $content = str_replace( '%display_name%', esc_attr( $user->display_name ), $content );
624 $content = str_replace( '%user_profile_url%', esc_url_raw( $profile_url ), $content );
625 $content = str_replace( '%user_profile_link%', '<a href="' . esc_url_raw( $profile_url ) . '"' . $link_target . '>' . esc_attr( $user->display_name ) . '</a>', $content );
626
627 $content = str_replace( '%user_nicename%', ( isset( $user->user_nicename ) ) ? esc_attr( $user->user_nicename ) : '', $content );
628 $content = str_replace( '%user_email%', ( isset( $user->user_email ) ) ? esc_attr( $user->user_email ) : '', $content );
629 $content = str_replace( '%user_url%', ( isset( $user->user_url ) ) ? esc_url_raw( $user->user_url ) : '', $content );
630
631 // Balance Related
632 $balance = $this->get_users_balance( $user->ID );
633
634 $content = str_replace( '%balance%', $balance, $content );
635 $content = str_replace( '%balance_f%', $this->format_creds( $balance ), $content );
636
637 return $content;
638
639 }
640
641 /**
642 * Comment Related Template Tags
643 * Replaces the comment related template tags in a given string.
644 * @param $content (string) string containing the template tags
645 * @param $ref_id (int) required comment id as reference id
646 * @param $data (object) Log entry data object
647 * @param $link_target (string) Optional link target to add to any links
648 * @return (string) parsed string
649 * @since 0.1
650 * @version 1.1
651 */
652 public function template_tags_comment( $content = '', $ref_id = NULL, $data = '', $link_target = '' ) {
653
654 if ( $ref_id === NULL ) return $content;
655
656 $content = $this->template_tags_general( $content );
657 if ( ! $this->has_tags( 'comment', 'comment_id|c_post_id|c_post_title|c_post_url|c_link_with_title', $content ) ) return $content;
658
659 // Get Comment Object
660 $comment = get_comment( $ref_id );
661 $comment_url = '#item-has-been-deleted';
662 $comment_post_title = __( 'Deleted Item', 'mycred' );
663
664 // Comment does not exist - see if we can re-construct
665 if ( $comment === NULL ) {
666
667 // Nope, no backup, bye
668 if ( ! is_array( $data ) || ! array_key_exists( 'comment_ID', $data ) ) return $content;
669
670 // Re-construct
671 $comment = new StdClass();
672 foreach ( $data as $key => $value ) {
673 $comment->$key = $value;
674 }
675
676 }
677 else {
678
679 $comment_post = mycred_get_post( $comment->comment_post_ID );
680 $comment_url = mycred_get_permalink( $comment_post );
681 $comment_post_title = mycred_get_permalink( $comment_post );
682
683 }
684
685 // Let others play first
686 $content = apply_filters( 'mycred_parse_tags_comment', $content, $comment, $data );
687
688 if ( $link_target != '' )
689 $link_target = ' target="' . esc_attr( $link_target ) . '"';
690
691 $content = str_replace( '%comment_id%', $comment->comment_ID, $content );
692
693 $content = str_replace( '%c_post_id%', $comment->comment_post_ID, $content );
694 $content = str_replace( '%c_post_title%', esc_attr( $comment_post_title ), $content );
695
696 $content = str_replace( '%c_post_url%', esc_url_raw( $comment_url ), $content );
697 $content = str_replace( '%c_link_with_title%', '<a href="' . esc_url_raw( $comment_url ) . '">' . esc_attr( $comment_post_title ) . '</a>', $content );
698
699 return $content;
700
701 }
702
703 /**
704 * Has Tags
705 * Checks if a string has any of the defined template tags.
706 * @param $type (string) template tag type
707 * @param $tags (string) tags to search for, list with |
708 * @param $content (string) content to search
709 * @filter 'mycred_has_tags'
710 * @filter 'mycred_has_tags_{$type}'
711 * @returns (boolean) true or false
712 * @since 1.2.2
713 * @version 1.1
714 */
715 public function has_tags( $type = '', $tags = '', $content = '' ) {
716
717 $tags = apply_filters( 'mycred_has_tags', $tags, $content );
718 $tags = apply_filters( 'mycred_has_tags_' . $type, $tags, $content );
719
720 if ( $tags == '' || ! preg_match( '%(' . trim( $tags ) . ')%', $content, $matches ) ) return false;
721
722 return true;
723
724 }
725
726 /**
727 * Available Template Tags
728 * Based on an array of template tag types, a list of codex links
729 * are generted for each tag type.
730 * @since 1.4
731 * @version 1.0.1
732 */
733 public function available_template_tags( $available = array(), $custom = '' ) {
734
735 // Prep
736 $links = $template_tags = array();
737
738 // General
739 if ( in_array( 'general', $available ) )
740 $template_tags[] = array(
741 'title' => __( 'General', 'mycred' ),
742 'url' => 'http://codex.mycred.me/category/template-tags/temp-general/'
743 );
744
745 // User
746 if ( in_array( 'user', $available ) )
747 $template_tags[] = array(
748 'title' => __( 'User Related', 'mycred' ),
749 'url' => 'http://codex.mycred.me/category/template-tags/temp-user/'
750 );
751
752 // Post
753 if ( in_array( 'post', $available ) )
754 $template_tags[] = array(
755 'title' => __( 'Post Related', 'mycred' ),
756 'url' => 'http://codex.mycred.me/category/template-tags/temp-post/'
757 );
758
759 // Comment
760 if ( in_array( 'comment', $available ) )
761 $template_tags[] = array(
762 'title' => __( 'Comment Related', 'mycred' ),
763 'url' => 'http://codex.mycred.me/category/template-tags/temp-comment/'
764 );
765
766 // Widget
767 if ( in_array( 'widget', $available ) )
768 $template_tags[] = array(
769 'title' => __( 'Widget Related', 'mycred' ),
770 'url' => 'http://codex.mycred.me/category/template-tags/temp-widget/'
771 );
772
773 // Amount
774 if ( in_array( 'amount', $available ) )
775 $template_tags[] = array(
776 'title' => __( 'Amount Related', 'mycred' ),
777 'url' => 'http://codex.mycred.me/category/template-tags/temp-amount/'
778 );
779
780 // Video
781 if ( in_array( 'video', $available ) )
782 $template_tags[] = array(
783 'title' => __( 'Video Related', 'mycred' ),
784 'url' => 'http://codex.mycred.me/category/template-tags/temp-video/'
785 );
786
787 if ( ! empty( $template_tags ) ) {
788 foreach ( $template_tags as $tag ) {
789 $links[] = '<a href="' . $tag['url'] . '" target="_blank">' . $tag['title'] . '</a>';
790 }
791 }
792
793 if ( ! empty( $custom ) )
794 $custom = ' ' . __( 'and', 'mycred' ) . ': ' . $custom;
795
796 return __( 'Available Template Tags:', 'mycred' ) . ' ' . implode( ', ', $links ) . $custom . '.';
797
798 }
799
800 /**
801 * Allowed Tags
802 * Strips HTML tags from a given string.
803 *
804 * @param $data (string) to strip tags off
805 * @param $allow (string) allows you to overwrite the default filter with a custom set of tags to strip
806 * @filter 'mycred_allowed_tags'
807 * @returns (string) string stripped of tags
808 * @since 0.1
809 * @version 1.1
810 */
811 public function allowed_tags( $data = '', $allow = '' ) {
812
813 if ( $allow === false )
814 return strip_tags( $data );
815
816 elseif ( ! empty( $allow ) )
817 return strip_tags( $data, $allow );
818
819 return strip_tags( $data, apply_filters( 'mycred_allowed_tags', '<a><br><em><strong><span>' ) );
820
821 }
822
823 /**
824 * Allowed HTML Tags
825 * Used for settings that support HTML. These settings are
826 * sanitized using wp_kses() where these tags are used.
827 * @since 1.6
828 * @version 1.0.1
829 */
830 public function allowed_html_tags() {
831
832 return apply_filters( 'mycred_allowed_html_tags', array(
833 'a' => array( 'href' => array(), 'title' => array(), 'target' => array() ),
834 'abbr' => array( 'title' => array() ), 'acronym' => array( 'title' => array() ),
835 'code' => array(), 'pre' => array(), 'em' => array(), 'strong' => array(),
836 'div' => array( 'class' => array(), 'id' => array() ), 'span' => array( 'class' => array() ),
837 'p' => array(), 'ul' => array(), 'ol' => array(), 'li' => array(),
838 'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(),
839 'img' => array( 'src' => array(), 'class' => array(), 'alt' => array() ),
840 'br' => array( 'class' => array() )
841 ), $this );
842
843 }
844
845 /**
846 * Get Point Admin Capability
847 * Returns the set WordPress capability that defines who is considered a "Point Administrator".
848 * @returns capability (string)
849 * @since 1.8
850 * @version 1.0
851 */
852 public function get_point_admin_capability() {
853
854 // Need to have something or we are in deep trouble
855 if ( ! isset( $this->caps['plugin'] ) || empty( $this->caps['plugin'] ) )
856 $this->caps['plugin'] = 'edit_theme_options';
857
858 // Try to prevent "lockouts" on Multisites where the delete_user cap is not available admins.
859 // Try instead using "export" which should also be available for admins.
860 if ( $this->is_multisite && $this->caps['plugin'] == 'delete_user' )
861 $this->caps['plugin'] = 'edit_theme_options';
862
863 // backwards cap.
864 $capability = apply_filters( 'mycred_edit_plugin_cap', $this->caps['plugin'] );
865
866 return apply_filters( 'get_point_admin_capability', $capability, $this );
867
868 }
869 // Backwards comp
870 public function edit_creds_cap() {
871
872 _deprecated_function( __FUNCTION__, 'get_point_admin_capability', '1.8' );
873
874 return $this->get_point_admin_capability();
875
876 }
877
878 /**
879 * Is Point Administrator
880 * Check if a given user or the current user is a Point Administrator.
881 * @param $user_id (int) user id
882 * @returns true or false
883 * @since 1.8
884 * @version 1.0
885 */
886 public function user_is_point_admin( $user_id = NULL ) {
887
888 $result = false;
889
890 if ( ! did_action( 'init' ) ) {
891 _doing_it_wrong( __FUNCTION__, 'Capability should not be checked before wp init', '1.8' );
892 return $result;
893 }
894
895 // Grab current user id
896 if ( $user_id === NULL )
897 $user_id = get_current_user_id();
898
899 // Check if user can
900 if ( user_can( $user_id, $this->get_point_admin_capability() ) )
901 $result = true;
902
903 return $result;
904
905 }
906 // Backwards comp
907 public function can_edit_creds( $user_id = NULL ) {
908
909 _deprecated_function( __FUNCTION__, 'user_is_point_admin', '1.8' );
910
911 return $this->user_is_point_admin( $user_id );
912
913 }
914
915 /**
916 * Get Point Editor Capability
917 * Returns the set WordPress capability that defines who is considered a "Point Editor".
918 * @returns capability (string)
919 * @since 1.8
920 * @version 1.0
921 */
922 public function get_point_editor_capability() {
923
924 if ( ! isset( $this->caps['creds'] ) || empty( $this->caps['creds'] ) )
925 $this->caps['creds'] = 'manage_options';
926
927 $capability = apply_filters( 'mycred_edit_creds_cap', $this->caps['creds'] );
928
929 return apply_filters( 'get_point_editor_capability', $capability, $this );
930
931 }
932 // Backwards comp
933 public function edit_plugin_cap() {
934
935 _deprecated_function( __FUNCTION__, 'get_point_editor_capability', '1.8' );
936
937 return $this->get_point_admin_capability();
938
939 }
940
941 /**
942 * Is Point Editor
943 * Check if a given user or the current user is a Point Editor.
944 * @param $user_id (int) user id
945 * @returns true or false
946 * @since 1.8
947 * @version 1.0
948 */
949 public function user_is_point_editor( $user_id = NULL ) {
950
951 $result = false;
952
953 if ( ! did_action( 'init' ) ) {
954 _doing_it_wrong( __FUNCTION__, 'Capability should not be checked before wp init', '1.8' );
955 return $result;
956 }
957
958 // Grab current user id
959 if ( $user_id === NULL )
960 $user_id = get_current_user_id();
961
962 // Check if user can
963 if ( user_can( $user_id, $this->get_point_editor_capability() ) )
964 $result = true;
965
966 return $result;
967
968 }
969 // Backwards comp
970 public function can_edit_plugin( $user_id = '' ) {
971
972 _deprecated_function( __FUNCTION__, 'user_is_point_editor', '1.8' );
973
974 return $this->user_is_point_editor( $user_id );
975
976 }
977
978 /**
979 * Check if user id is in exclude list
980 * @return true or false
981 * @since 0.1
982 * @version 1.1
983 */
984 public function in_exclude_list( $user_id = '' ) {
985
986 $result = false;
987
988 // Grab current user id
989 if ( empty( $user_id ) )
990 $user_id = get_current_user_id();
991
992 if ( ! isset( $this->exclude['list'] ) )
993 $this->exclude['list'] = '';
994
995 $list = wp_parse_id_list( $this->exclude['list'] );
996 if ( in_array( $user_id, $list ) )
997 $result = true;
998
999 return apply_filters( 'mycred_is_excluded_list', $result, $user_id );
1000
1001 }
1002
1003 /**
1004 * Exclude Point Administrators?
1005 * @return true or false
1006 * @since 1.8
1007 * @version 1.0
1008 */
1009 public function exclude_point_admins() {
1010
1011 return (bool) $this->exclude['plugin_editors'];
1012
1013 }
1014 // Backwards comp
1015 public function exclude_plugin_editors() {
1016
1017 _deprecated_function( __FUNCTION__, 'exclude_point_admins', '1.8' );
1018
1019 return $this->exclude_point_admins();
1020
1021 }
1022
1023 /**
1024 * Exclude Point Editors?
1025 * @return true or false
1026 * @since 1.8
1027 * @version 1.0
1028 */
1029 public function exclude_point_editors() {
1030
1031 return (bool) $this->exclude['cred_editors'];
1032
1033 }
1034 // Backwards comp
1035 public function exclude_creds_editors() {
1036
1037 _deprecated_function( __FUNCTION__, 'exclude_point_editors', '1.8' );
1038
1039 return $this->exclude_point_editors();
1040
1041 }
1042
1043 /**
1044 * Exclude User
1045 * Checks is a given user or the current user is excluded from using this point type.
1046 * @param $user_id (int), the users numeric ID
1047 * @returns boolean true on user should be excluded else false
1048 * @since 0.1
1049 * @version 1.1
1050 */
1051 public function exclude_user( $user_id = NULL ) {
1052
1053 if ( $user_id === NULL )
1054 $user_id = get_current_user_id();
1055
1056 // Quick override
1057 if ( apply_filters( 'mycred_exclude_user', false, $user_id, $this ) === true ) return true;
1058
1059 // In case we auto exclude point administrators
1060 if ( $this->exclude_point_admins() && $this->user_is_point_admin( $user_id ) ) return true;
1061
1062 // In case we auto exclude point editors
1063 if ( $this->exclude_point_editors() && $this->user_is_point_editor( $user_id ) ) return true;
1064
1065 // In case our ID is in our exclude list of ids
1066 if ( $this->in_exclude_list( $user_id ) ) return true;
1067
1068 return false;
1069
1070 }
1071
1072 /**
1073 * Count Members
1074 * @since 1.1
1075 * @version 1.1
1076 */
1077 public function count_members() {
1078
1079 global $wpdb;
1080
1081 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( DISTINCT user_id ) FROM {$wpdb->usermeta} WHERE meta_key = %s;", mycred_get_meta_key( $this->cred_id ) ) );
1082 if ( $count === NULL ) $count = 0;
1083
1084 return $count;
1085
1086 }
1087
1088 /**
1089 * Get Point Type Key
1090 * Returns the default cred id.
1091 * @since 1.8
1092 * @version 1.0
1093 */
1094 public function get_point_type_key() {
1095
1096 return $this->cred_id;
1097
1098 }
1099 // Backwards comp
1100 public function get_cred_id() {
1101
1102 _deprecated_function( __FUNCTION__, 'get_point_type_key', '1.8' );
1103
1104 return $this->get_point_type_key();
1105
1106 }
1107
1108 /**
1109 * Get Max
1110 * @since 1.3
1111 * @version 1.0
1112 */
1113 public function max() {
1114
1115 if ( ! isset( $this->max ) )
1116 $this->max = 0;
1117
1118 return $this->max;
1119
1120 }
1121
1122 /**
1123 * Enforce Max
1124 * @since 1.8
1125 * @version 1.0
1126 */
1127 public function enforce_max( $user_id = NULL, $amount = NULL ) {
1128
1129 $maxium = $this->max();
1130 if ( $amount == 0 || $maxium == 0 ) return $amount;
1131
1132 $original_amount = $amount;
1133
1134 // Enforce max adjustments
1135 if ( ( $maxium > $this->zero() && $amount > $maxium ) || ( $maxium < $this->zero() && $amount < $maxium ) ) {
1136
1137 $amount = $this->number( $maxium );
1138
1139 do_action( 'mycred_max_enforced', $user_id, $original_amount, $maxium );
1140
1141 }
1142
1143 return $amount;
1144
1145 }
1146
1147 /**
1148 * Get users balance
1149 * Returns a given users current balance raw.
1150 * @param $user_id (int), required user id
1151 * @param $type (string), optional cred type to check for
1152 * @returns zero if user id is not set or if no creds were found, else returns amount
1153 * @since 0.1
1154 * @version 1.5
1155 */
1156 public function get_users_balance( $user_id = NULL, $point_type = NULL ) {
1157
1158 if ( $user_id === NULL ) return $this->zero();
1159
1160 global $mycred_types, $mycred_current_account;
1161
1162 // Point type
1163 if ( $point_type === NULL || ! array_key_exists( $point_type, $mycred_types ) )
1164 $point_type = $this->get_point_type_key();
1165
1166 if ( mycred_is_current_account( $user_id ) && $mycred_current_account->balance[ $point_type ] !== false )
1167 $balance = $mycred_current_account->balance[ $point_type ]->get( 'current' );
1168
1169 else
1170 $balance = mycred_get_user_meta( $user_id, $point_type, '', true );
1171
1172 if ( $balance == '' ) $balance = $this->zero();
1173
1174 return $this->number( apply_filters( 'mycred_get_users_cred', $balance, $this, $user_id, $point_type ) );
1175
1176 }
1177 // Backwards comp
1178 public function get_users_cred( $user_id = NULL, $type = NULL ) {
1179
1180 _deprecated_function( __FUNCTION__, 'get_users_balance', '1.8' );
1181
1182 return $this->get_users_balance( $user_id, $type );
1183
1184 }
1185
1186 /**
1187 * Get users total balance
1188 * Returns a given users total balance raw.
1189 * @param $user_id (int), required user id
1190 * @param $type (string), optional cred type to check for
1191 * @returns zero if user id is not set or if no creds were found, else returns amount
1192 * @since 1.8
1193 * @version 1.0
1194 */
1195 public function get_users_total_balance( $user_id = NULL, $point_type = NULL ) {
1196
1197 $total_balance = $this->zero();
1198
1199 $user_id = absint( $user_id );
1200 if ( $user_id === 0 ) return $total_balance;
1201
1202 // Feature needs to be enabled
1203 if ( MYCRED_ENABLE_TOTAL_BALANCE ) {
1204
1205 global $mycred_types, $mycred_current_account;
1206
1207 // Point type
1208 if ( $point_type === NULL || ! array_key_exists( $point_type, $mycred_types ) )
1209 $point_type = $this->get_point_type_key();
1210
1211 if ( mycred_is_current_account( $user_id ) && $mycred_current_account->balance[ $point_type ] !== false )
1212 $total_balance = $mycred_current_account->balance[ $point_type ]->get( 'accumulated' );
1213
1214 else
1215 $total_balance = mycred_get_user_meta( $user_id, $point_type, '_total', true );
1216
1217 if ( $total_balance == '' ) {
1218
1219 $total_balance = mycred_query_users_total( $user_id, $point_type );
1220
1221 mycred_update_user_meta( $user_id, $point_type, '_total', $total_balance );
1222
1223 }
1224
1225 }
1226
1227 return $this->number( apply_filters( 'mycred_get_users_total_cred', $total_balance, $this, $user_id, $point_type ) );
1228
1229 }
1230
1231 /**
1232 * Update users balance
1233 * Used to adjust a users balance for a given point type. Returns the new balance.
1234 * @param $user_id (int), required user id
1235 * @param $amount (int|float), amount to add/deduct from users balance. This value must be pre-formated.
1236 * @param $type (string), optional point type key to adjust instead of the current one.
1237 * @returns the new balance.
1238 * @since 0.1
1239 * @version 1.5
1240 */
1241 public function update_users_balance( $user_id = NULL, $amount = NULL, $point_type = NULL ) {
1242
1243 // Minimum Requirements: User id and amount can not be null
1244 if ( $user_id === NULL || $amount === NULL || $amount == $this->zero() ) return $amount;
1245
1246 global $mycred_types, $mycred_current_account;
1247
1248 // Point type
1249 if ( $point_type === NULL || ! array_key_exists( $point_type, $mycred_types ) )
1250 $point_type = $this->get_point_type_key();
1251
1252 // Prep amount
1253 $amount = $this->number( $amount );
1254 $amount = $this->enforce_max( $user_id, $amount );
1255
1256 // Adjust balance
1257 $current_balance = $this->get_users_balance( $user_id, $point_type );
1258 $new_balance = $this->number( $current_balance + $amount );
1259
1260 // Save new balance
1261 mycred_update_user_meta( $user_id, $point_type, '', $new_balance );
1262
1263 // Update the current account object
1264 if ( mycred_is_current_account( $user_id ) && $mycred_current_account->balance[ $point_type ] !== false )
1265 $mycred_current_account->balance[ $point_type ]->set( 'current', $new_balance );
1266
1267 // Let others play
1268 do_action( 'mycred_update_user_balance', $user_id, $current_balance, $amount, $point_type );
1269
1270 // Return the new balance
1271 return $new_balance;
1272
1273 }
1274
1275 /**
1276 * Update users total balance
1277 * Updates a given users total balance with the option to add an adjustment directly.
1278 * @param $user_id (int), required user id
1279 * @param $amount (int|float), required amount to add to the total
1280 * @param $type (string), optional cred type to check for
1281 * @returns zero if user id is not set or if no creds were found, else returns amount
1282 * @since 1.8
1283 * @version 1.0
1284 */
1285 public function update_users_total_balance( $user_id = NULL, $amount = 0, $point_type = NULL ) {
1286
1287 if ( ! MYCRED_ENABLE_TOTAL_BALANCE || ! MYCRED_ENABLE_LOGGING || $amount == 0 ) return $amount;
1288
1289 global $mycred_current_account;
1290
1291 if ( mycred_is_current_account( $user_id ) && $mycred_current_account->balance[ $point_type ] !== false )
1292 $total_balance = $mycred_current_account->balance[ $point_type ]->get( 'accumulated' );
1293
1294 else {
1295
1296 $total_balance = mycred_get_user_meta( $user_id, $point_type, '_total', true );
1297 $total_balance = $this->number( $total_balance );
1298
1299 }
1300
1301 $total_balance += $amount;
1302
1303 mycred_update_user_meta( $user_id, $point_type, '_total', $total_balance );
1304
1305 if ( mycred_is_current_account( $user_id ) && $mycred_current_account->balance[ $point_type ] !== false )
1306 $mycred_current_account->balance[ $point_type ]->set( 'accumulated', $total_balance );
1307
1308 do_action( 'mycred_update_user_total_balance', $total_balance, $user_id, $point_type, $this );
1309
1310 return $total_balance;
1311
1312 }
1313
1314 /**
1315 * Set users balance
1316 * Changes a users balance to the amount given.
1317 * @param $user_id (int), required user id
1318 * @param $new_balance (int|float), amount to add/deduct from users balance. This value must be pre-formated.
1319 * @returns (bool) true on success or false on fail.
1320 * @since 1.7.3
1321 * @version 1.1
1322 */
1323 public function set_users_balance( $user_id = NULL, $new_balance = NULL ) {
1324
1325 // Minimum Requirements: User id and amount can not be null
1326 if ( $user_id === NULL || $new_balance === NULL ) return false;
1327
1328 global $mycred_current_account;
1329
1330 $point_type = $this->get_point_type_key();
1331 $new_balance = $this->number( $new_balance );
1332
1333 // Update balance
1334 mycred_update_user_meta( $user_id, $point_type, '', $new_balance );
1335
1336 if ( mycred_is_current_account( $user_id ) && $mycred_current_account->balance[ $point_type ] !== false )
1337 $mycred_current_account->balance[ $point_type ]->set( 'current', $new_balance );
1338
1339 // Clear caches
1340 mycred_delete_option( 'mycred-cache-total-' . $point_type );
1341
1342 // Let others play
1343 do_action( 'mycred_set_user_balance', $user_id, $new_balance, $this );
1344
1345 return true;
1346
1347 }
1348
1349 /**
1350 * Set users total balance
1351 * Changes a users total balance to the amount given.
1352 * @param $user_id (int), required user id
1353 * @param $new_balance (int|float), amount to add/deduct from users balance. This value must be pre-formated.
1354 * @returns (bool) true on success or false on fail.
1355 * @since 1.8
1356 * @version 1.0
1357 */
1358 public function set_users_total_balance( $user_id = NULL, $new_balance = NULL ) {
1359
1360 // Minimum Requirements: User id and amount can not be null
1361 if ( $user_id === NULL || $new_balance === NULL || ! MYCRED_ENABLE_TOTAL_BALANCE || ! MYCRED_ENABLE_LOGGING ) return false;
1362
1363 global $mycred_current_account;
1364
1365 $total_balance = $this->number( $new_balance );
1366
1367 // Update balance
1368 mycred_update_user_meta( $user_id, $this->get_point_type_key(), '_total', $total_balance );
1369
1370 if ( mycred_is_current_account( $user_id ) && $mycred_current_account->balance[ $point_type ] !== false )
1371 $mycred_current_account->balance[ $point_type ]->set( 'accumulated', $total_balance );
1372
1373 // Let others play
1374 do_action( 'mycred_set_user_total_balance', $user_id, $total_balance, $this );
1375
1376 return true;
1377
1378 }
1379
1380 /**
1381 * Add Creds
1382 * Adds creds to a given user. A refernece ID, user id and number of creds must be given.
1383 * Important! This function will not check if the user should be excluded from gaining points, this must
1384 * be done before calling this function!
1385 * @param $ref (string), required reference id
1386 * @param $user_id (int), required id of the user who will get these points
1387 * @param $cred (int|float), required number of creds to give or deduct from the given user.
1388 * @param $ref_id (int), optional array of reference IDs allowing the use of content specific keywords in the log entry
1389 * @param $data (object|array|string|int), optional extra data to save in the log. Note that arrays gets serialized!
1390 * @param $type (string), optional point name, defaults to MYCRED_DEFAULT_TYPE_KEY
1391 * @returns boolean true on success or false on fail
1392 * @since 0.1
1393 * @version 1.7
1394 */
1395 public function add_creds( $ref = '', $user_id = '', $amount = '', $entry = '', $ref_id = '', $data = '', $type = NULL ) {
1396
1397 // Minimum Requirements: Reference not empty, User ID not empty and Amount is not empty
1398 if ( empty( $ref ) || empty( $user_id ) || empty( $amount ) ) return false;
1399
1400 // Check exclusion
1401 if ( $this->exclude_user( $user_id ) ) return false;
1402
1403 // Prep amount
1404 $amount = $this->number( $amount );
1405 $amount = $this->enforce_max( $user_id, $amount );
1406 if ( $amount == $this->zero() || $amount == 0 ) return false;
1407
1408 global $mycred_types;
1409
1410 // Point type
1411 if ( $type === NULL || ! array_key_exists( $type, $mycred_types ) )
1412 $type = $this->get_point_type_key();
1413
1414 // Execution Override
1415 // Allows us to stop an execution.
1416 // excepts a boolean reply
1417 $execute = apply_filters( 'mycred_add', true, compact( 'ref', 'user_id', 'amount', 'entry', 'ref_id', 'data', 'type' ), $this );
1418
1419 // Acceptable answers:
1420 // true (boolean)
1421 if ( $execute === true ) {
1422
1423 // Allow the adjustment of the values before we run them
1424 $run_this = apply_filters( 'mycred_run_this', compact( 'ref', 'user_id', 'amount', 'entry', 'ref_id', 'data', 'type' ), $this );
1425
1426 // Add to log
1427 $this->add_to_log(
1428 $run_this['ref'],
1429 $run_this['user_id'],
1430 $run_this['amount'],
1431 $run_this['entry'],
1432 $run_this['ref_id'],
1433 $run_this['data'],
1434 $run_this['type']
1435 );
1436
1437 // Update balance
1438 $this->update_users_balance( (int) $run_this['user_id'], $run_this['amount'], $run_this['type'] );
1439
1440 // Update total balance (if enabled)
1441 if ( MYCRED_ENABLE_TOTAL_BALANCE && MYCRED_ENABLE_LOGGING && ( $run_this['amount'] > 0 || ( $run_this['amount'] < 0 && $run_this['ref'] == 'manual' ) ) ) {
1442
1443 $this->update_users_total_balance( (int) $run_this['user_id'], $run_this['amount'], $run_this['type'] );
1444
1445 }
1446
1447 }
1448
1449 // false (boolean)
1450 else { $run_this = false; }
1451
1452 // For all features that need to run once we have done or not done something
1453 return apply_filters( 'mycred_add_finished', $execute, $run_this, $this );
1454
1455 }
1456
1457 /**
1458 * Add Log Entry
1459 * Adds a new entry into the log. A reference id, user id and number of credits must be set.
1460 * @param $ref (string), required reference id
1461 * @param $user_id (int), required id of the user who will get these points
1462 * @param $cred (int|float), required number of creds to give or deduct from the given user.
1463 * @param $ref_id (array), optional array of reference IDs allowing the use of content specific keywords in the log entry
1464 * @param $data (object|array|string|int), optional extra data to save in the log. Note that arrays gets serialized!
1465 * @returns false if requirements are not set or db insert id if successful.
1466 * @since 0.1
1467 * @version 1.5
1468 */
1469 public function add_to_log( $ref = '', $user_id = '', $amount = '', $entry = '', $ref_id = '', $data = '', $type = NULL ) {
1470
1471 // Minimum Requirements: Reference not empty, User ID not empty and Amount is not empty
1472 if ( empty( $ref ) || empty( $user_id ) || empty( $amount ) || empty( $entry ) ) return false;
1473
1474 // Prep amount
1475 $amount = $this->number( $amount );
1476 $amount = $this->enforce_max( $user_id, $amount );
1477 if ( $amount === $this->zero() || $amount == 0 ) return false;
1478
1479 $insert_id = 0;
1480
1481 mycred_update_users_history( $user_id, $type, $ref, $ref_id, $amount );
1482
1483 // Option to disable logging
1484 if ( MYCRED_ENABLE_LOGGING ) {
1485
1486 global $wpdb, $mycred_types;
1487
1488 // Strip HTML from log entry
1489 $entry = $this->allowed_tags( $entry );
1490
1491 // Point type
1492 if ( $type === NULL || ! array_key_exists( $type, $mycred_types ) )
1493 $type = $this->get_point_type_key();
1494
1495 $time = apply_filters( 'mycred_log_time', current_time( 'timestamp' ), $ref, $user_id, $amount, $entry, $ref_id, $data, $type );
1496 $insert = array(
1497 'ref' => $ref,
1498 'ref_id' => $ref_id,
1499 'user_id' => (int) $user_id,
1500 'creds' => $amount,
1501 'ctype' => $type,
1502 'time' => $time,
1503 'entry' => $entry,
1504 'data' => ( is_array( $data ) || is_object( $data ) ) ? serialize( $data ) : $data
1505 );
1506
1507 // Insert into DB
1508 $wpdb->insert(
1509 $this->log_table,
1510 $insert,
1511 array( '%s', '%d', '%d', '%s', '%s', '%d', '%s', ( is_numeric( $data ) ) ? '%d' : '%s' )
1512 );
1513
1514 $insert_id = $wpdb->insert_id;
1515
1516 wp_cache_delete( 'mycred_references' . $type, MYCRED_SLUG );
1517
1518 delete_transient( 'mycred_log_entries' );
1519
1520 }
1521
1522 return apply_filters( 'mycred_new_log_entry_id', $insert_id, $insert, $this );
1523
1524 }
1525
1526 /**
1527 * Update Log Entry
1528 * Updates an existing log entry.
1529 * @param $entry_id (id), required log entry id
1530 * @param $data (array), required column data to update
1531 * @param $prep (array), required column prep
1532 * @returns false if requirements are not met or true
1533 * @since 1.6.7
1534 * @version 1.1
1535 */
1536 public function update_log_entry( $entry_id = NULL, $data = array(), $prep = array() ) {
1537
1538 if ( $entry_id === NULL || empty( $data ) || empty( $prep ) ) return false;
1539
1540 // If logging is disabled, pretend we did the job
1541 if ( ! MYCRED_ENABLE_LOGGING ) return true;
1542
1543 global $wpdb;
1544
1545 $wpdb->update(
1546 $this->log_table,
1547 $data,
1548 array( 'id' => $entry_id ),
1549 $prep,
1550 array( '%d' )
1551 );
1552
1553 do_action( 'mycred_log_entry_updated', $entry_id, $data );
1554
1555 return true;
1556
1557 }
1558
1559 /**
1560 * Has Entry
1561 * Checks to see if a given action with reference ID and user ID exists in the log database.
1562 * @param $reference (string) required reference ID
1563 * @param $ref_id (int) optional reference id
1564 * @param $user_id (int) optional user id
1565 * @param $data (array|string) option data to search
1566 * @since 0.1
1567 * @version 1.4
1568 */
1569 function has_entry( $reference = NULL, $ref_id = NULL, $user_id = NULL, $data = NULL, $type = NULL ) {
1570
1571 $has_entry = false;
1572 if ( ! MYCRED_ENABLE_LOGGING ) return $has_entry;
1573
1574 global $mycred_current_account;
1575
1576 if ( $user_id !== NULL && mycred_is_current_account( $user_id ) && ! empty( $mycred_current_account->point_type ) && in_array( $type, $mycred_current_account->point_type ) ) {
1577
1578 if ( isset( $mycred_current_account->balance[ $type ]->history ) && ! empty( $mycred_current_account->balance[ $type ]->history->data ) ) {
1579
1580 $data = $mycred_current_account->balance[ $type ]->history->data;
1581 if ( array_key_exists( $reference, $data ) && ! empty( $data[ $reference ]->reference_ids ) && in_array( $ref_id, $data[ $reference ]->reference_ids ) ) {
1582
1583 $has_entry = true;
1584
1585 }
1586
1587 }
1588
1589 }
1590
1591 if ( ! $has_entry ) {
1592
1593 global $wpdb;
1594
1595 $wheres = array();
1596
1597 if ( $reference !== NULL )
1598 $wheres[] = $wpdb->prepare( "ref = %s", $reference );
1599
1600 if ( $ref_id !== NULL )
1601 $wheres[] = $wpdb->prepare( "ref_id = %d", $ref_id );
1602
1603 if ( $user_id !== NULL )
1604 $wheres[] = $wpdb->prepare( "user_id = %d", $user_id );
1605
1606 if ( $data !== NULL )
1607 $wheres[] = $wpdb->prepare( "data = %s", maybe_serialize( $data ) );
1608
1609 if ( $type === NULL ) $type = $this->get_point_type_key();
1610 $wheres[] = $wpdb->prepare( "ctype = %s", $type );
1611
1612 $where = implode( ' AND ', $wheres );
1613
1614 if ( ! empty( $wheres ) ) {
1615
1616 $check = $wpdb->get_var( "SELECT id FROM {$this->log_table} WHERE {$where};" );
1617 if ( $check !== NULL )
1618 $has_entry = true;
1619
1620 }
1621
1622 }
1623
1624 return apply_filters( 'mycred_has_entry', $has_entry, $reference, $ref_id, $user_id, $data, $type );
1625
1626 }
1627
1628 }
1629endif;
1630
1631/**
1632 * myCRED Label
1633 * Returns the myCRED Label
1634 * @since 1.3.3
1635 * @version 1.1
1636 */
1637if ( ! function_exists( 'mycred_label' ) ) :
1638 function mycred_label( $trim = false ) {
1639
1640 global $mycred_label;
1641
1642 if ( $mycred_label === NULL )
1643 $mycred_label = apply_filters( 'mycred_label', MYCRED_DEFAULT_LABEL );
1644
1645 $name = $mycred_label;
1646 if ( $trim )
1647 $name = strip_tags( $mycred_label );
1648
1649 return $name;
1650
1651 }
1652endif;
1653
1654/**
1655 * Get myCRED
1656 * Returns myCRED's general settings and core functions.
1657 * Replaces mycred_get_settings()
1658 * @since 1.4
1659 * @version 1.1
1660 */
1661if ( ! function_exists( 'mycred' ) ) :
1662 function mycred( $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
1663
1664 global $mycred, $current_mycred;
1665
1666 // Custom point type
1667 if ( $point_type != MYCRED_DEFAULT_TYPE_KEY ) {
1668
1669 if ( isset( $current_mycred->cred_id ) && $current_mycred->cred_id === $point_type )
1670 return $current_mycred;
1671
1672 $current_mycred = new myCRED_Settings( $point_type );
1673
1674 return $current_mycred;
1675
1676 }
1677
1678 // Main point type
1679 if ( ! isset( $mycred->cred_id ) )
1680 $mycred = new myCRED_Settings();
1681
1682 return $mycred;
1683
1684 }
1685endif;
1686
1687/**
1688 * Get Network Settings
1689 * Returns myCRED's network settings or false if multisite is not enabled.
1690 * @since 0.1
1691 * @version 1.2
1692 */
1693if ( ! function_exists( 'mycred_get_settings_network' ) ) :
1694 function mycred_get_settings_network() {
1695
1696 global $mycred_network;
1697
1698 $defaults = array(
1699 'master' => 0,
1700 'central' => 0,
1701 'block' => ''
1702 );
1703
1704 if ( is_array( $mycred_network ) && ! empty( $mycred_network ) && array_key_exists( 'master', $mycred_network ) )
1705 return $mycred_network;
1706
1707 $settings = ( ( is_multisite() ) ? get_blog_option( get_network()->site_id, 'mycred_network', $defaults ) : $defaults );
1708 $settings = shortcode_atts( $defaults, $settings );
1709
1710 $settings['master'] = (bool) $settings['master'];
1711 $settings['central'] = (bool) $settings['central'];
1712
1713 return $settings;
1714
1715 }
1716endif;
1717
1718/**
1719 * Is Main Site
1720 * In Multisite installs, this function will check if the current site or
1721 * a given site is the main site in the network.
1722 * @since 1.8
1723 * @version 1.0
1724 */
1725if ( ! function_exists( 'mycred_is_main_site' ) ) :
1726 function mycred_is_main_site( $site_id = NULL ) {
1727
1728 if ( ! is_multisite() ) return true;
1729
1730 if ( $site_id === NULL )
1731 $site_id = get_current_blog_id();
1732
1733 if ( get_network()->site_id != $site_id )
1734 return false;
1735
1736 return true;
1737
1738 }
1739endif;
1740
1741/**
1742 * Check if site is blocked
1743 * @since 1.5.4
1744 * @version 1.1
1745 */
1746if ( ! function_exists( 'mycred_is_site_blocked' ) ) :
1747 function mycred_is_site_blocked( $blog_id = NULL ) {
1748
1749 // Only applicable for multisites
1750 if ( ! is_multisite() ) return false;
1751
1752 // Blog ID
1753 if ( $blog_id === NULL )
1754 $blog_id = get_current_blog_id();
1755
1756 // Main sites can not be blocked
1757 if ( $blog_id == get_network()->site_id ) return false;
1758
1759 // Get Network settings
1760 $network = mycred_get_settings_network();
1761 $block_list = wp_parse_id_list( $network['block'] );
1762 $blocked = false;
1763
1764 // Check if we are in the block list
1765 if ( ! empty( $block_list ) && in_array( $blog_id, $block_list ) )
1766 $blocked = true;
1767
1768 return apply_filters( 'mycred_is_site_blocked', $blocked, $blog_id );
1769
1770 }
1771endif;
1772
1773/**
1774 * Override Settings
1775 * Checks if the Master Template feature is enabled on a Multisite install.
1776 * @since 0.1
1777 * @version 1.1
1778 */
1779if ( ! function_exists( 'mycred_override_settings' ) ) :
1780 function mycred_override_settings() {
1781
1782 // Not a multisite
1783 if ( ! is_multisite() ) return false;
1784
1785 $network_setup = mycred_get_settings_network();
1786
1787 return apply_filters( 'mycred_mu_override_settings', (bool) $network_setup['master'], $network_setup );
1788
1789 }
1790endif;
1791
1792/**
1793 * Centralize Log
1794 * Checks if the Central Logging feature is enabled on a Multisite install.
1795 * @since 1.3
1796 * @version 1.1
1797 */
1798if ( ! function_exists( 'mycred_centralize_log' ) ) :
1799 function mycred_centralize_log() {
1800
1801 // Not a multisite
1802 if ( ! is_multisite() ) return true;
1803
1804 $network_setup = mycred_get_settings_network();
1805
1806 return apply_filters( 'mycred_mu_centralize_log', (bool) $network_setup['central'], $network_setup );
1807
1808 }
1809endif;
1810
1811/**
1812 * Get Module
1813 * @since 1.7.3
1814 * @version 1.0.1
1815 */
1816if ( ! function_exists( 'mycred_get_module' ) ) :
1817 function mycred_get_module( $module = '', $type = 'solo' ) {
1818
1819 global $mycred_modules;
1820
1821 if ( $type == 'solo' ) {
1822
1823 if ( ! array_key_exists( $module, $mycred_modules['solo'] ) )
1824 return false;
1825
1826 return $mycred_modules['solo'][ $module ];
1827
1828 }
1829
1830 if ( ! array_key_exists( $type, $mycred_modules['type'] ) )
1831 return false;
1832
1833 return $mycred_modules['type'][ $type ][ $module ];
1834
1835 }
1836endif;
1837
1838/**
1839 * Get Addon Settings
1840 * @since 1.7.7
1841 * @version 1.1
1842 */
1843if ( ! function_exists( 'mycred_get_addon_settings' ) ) :
1844 function mycred_get_addon_settings( $addon = '', $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
1845
1846 $settings = false;
1847 $main_type = mycred();
1848
1849 $mycred = $main_type;
1850 if ( $point_type != MYCRED_DEFAULT_TYPE_KEY )
1851 $mycred = mycred( $point_type );
1852
1853 if ( $addon != '' ) {
1854
1855 if ( isset( $mycred->$addon ) )
1856 $settings = $mycred->$addon;
1857
1858 if ( $settings === false && isset( $main_type->$addon ) )
1859 $settings = $main_type->$addon;
1860
1861 }
1862
1863 return apply_filters( 'mycred_get_addon_settings', $settings, $addon, $point_type );
1864
1865 }
1866endif;
1867
1868/**
1869 * Get Remote API Settings
1870 * @since 1.3
1871 * @version 1.0
1872 */
1873if ( ! function_exists( 'mycred_get_remote' ) ) :
1874 function mycred_get_remote() {
1875
1876 $defaults = apply_filters( 'mycred_remote_defaults', array(
1877 'enabled' => 0,
1878 'key' => '',
1879 'uri' => 'api-dev',
1880 'debug' => 0
1881 ) );
1882
1883 return mycred_apply_defaults( $defaults, mycred_get_option( 'mycred_pref_remote', array() ) );
1884
1885 }
1886endif;
1887
1888/**
1889 * Is myCRED Ready
1890 * @since 1.3
1891 * @version 1.1
1892 */
1893if ( ! function_exists( 'is_mycred_ready' ) ) :
1894 function is_mycred_ready() {
1895
1896 if ( mycred_is_installed() !== false ) return true;
1897
1898 return false;
1899
1900 }
1901endif;
1902
1903/**
1904 * Is myCRED Installed
1905 * Returns either false (setup has not been run) or the timestamp when it was completed.
1906 * @since 1.7
1907 * @version 1.0.1
1908 */
1909if ( ! function_exists( 'mycred_is_installed' ) ) :
1910 function mycred_is_installed() {
1911
1912 return mycred_get_option( 'mycred_setup_completed', false );
1913
1914 }
1915endif;
1916
1917/**
1918 * Maybe Install myCRED Table
1919 * Check to see if maybe the myCRED table needs to be installed.
1920 * @since 1.7.6
1921 * @version 1.0.1
1922 */
1923if ( ! function_exists( 'maybe_install_mycred_table' ) ) :
1924 function maybe_install_mycred_table() {
1925
1926 // No need to check this if we have disabled logging. Prevent this from being used using AJAX
1927 if ( ! MYCRED_ENABLE_LOGGING || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || apply_filters( 'mycred_maybe_install_db', true ) === false ) return;
1928
1929 global $wpdb, $mycred_log_table;
1930
1931 // Check if the table exists
1932 if ( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $mycred_log_table ) ) != $mycred_log_table ) {
1933
1934 mycred_install_log( NULL, true );
1935
1936 do_action( 'mycred_reinstalled_table' );
1937
1938 }
1939
1940 }
1941endif;
1942
1943/**
1944 * Install Log
1945 * Installs the log for a site.
1946 * @since 1.3
1947 * @version 1.4.1
1948 */
1949if ( ! function_exists( 'mycred_install_log' ) ) :
1950 function mycred_install_log( $decimals = NULL, $force = false ) {
1951
1952 if ( ! MYCRED_ENABLE_LOGGING ) return true;
1953 $mycred = mycred();
1954
1955 if ( ! $force ) {
1956
1957 $db_version = mycred_get_option( 'mycred_version_db', false );
1958
1959 // DB Already installed
1960 if ( $db_version == myCRED_DB_VERSION ) return true;
1961
1962 }
1963
1964 global $wpdb;
1965
1966 $cred_format = 'bigint(22)';
1967 $point_type = $mycred->cred_id;
1968
1969 // If decimals is not provided
1970 if ( $decimals === NULL )
1971 $decimals = $mycred->format['decimals'];
1972
1973 // Point format in the log
1974 if ( $decimals > 0 ) {
1975
1976 if ( $decimals > 4 )
1977 $cred_format = "decimal(32,$decimals)";
1978
1979 else
1980 $cred_format = "decimal(22,$decimals)";
1981
1982 }
1983
1984 $wpdb->hide_errors();
1985
1986 $collate = '';
1987 if ( $wpdb->has_cap( 'collation' ) ) {
1988
1989 if ( ! empty( $wpdb->charset ) )
1990 $collate .= "DEFAULT CHARACTER SET {$wpdb->charset}";
1991
1992 if ( ! empty( $wpdb->collate ) )
1993 $collate .= " COLLATE {$wpdb->collate}";
1994
1995 }
1996
1997 // Log structure
1998 $sql = "
1999 id INT(11) NOT NULL AUTO_INCREMENT,
2000 ref VARCHAR(256) NOT NULL,
2001 ref_id INT(11) DEFAULT NULL,
2002 user_id INT(11) DEFAULT NULL,
2003 creds {$cred_format} DEFAULT NULL,
2004 ctype VARCHAR(64) DEFAULT '{$point_type}',
2005 time BIGINT(20) DEFAULT NULL,
2006 entry LONGTEXT DEFAULT NULL,
2007 data LONGTEXT DEFAULT NULL,
2008 PRIMARY KEY (id),
2009 UNIQUE KEY id (id)";
2010
2011 // Insert table
2012 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
2013 dbDelta( "CREATE TABLE IF NOT EXISTS {$mycred->log_table} ( " . $sql . " ) $collate;" );
2014
2015 mycred_update_option( 'mycred_version_db', myCRED_DB_VERSION );
2016
2017 return true;
2018
2019 }
2020endif;
2021
2022/**
2023 * Get Post Types
2024 * Returns an array of post types that myCRED uses.
2025 * @since 1.7
2026 * @version 1.1
2027 */
2028if ( ! function_exists( 'get_mycred_post_types' ) ) :
2029 function get_mycred_post_types() {
2030
2031 $post_type_keys = array( 'mycred_badge', 'buycred_payment' );
2032
2033 // Badges
2034 $post_type_keys[] = ( defined( 'MYCRED_BADGE_KEY' ) ) ? MYCRED_BADGE_KEY : 'mycred_badge';
2035
2036 // Coupons
2037 $post_type_keys[] = ( defined( 'MYCRED_COUPON_KEY' ) ) ? MYCRED_COUPON_KEY : 'mycred_coupon';
2038
2039 // Ranks
2040 $post_type_keys[] = ( defined( 'MYCRED_RANK_KEY' ) ) ? MYCRED_RANK_KEY : 'mycred_rank';
2041
2042 return apply_filters( 'mycred_post_types', $post_type_keys );
2043
2044 }
2045endif;
2046
2047/**
2048 * Get User ID
2049 * Attempts to return a user ID based on the request passed to this function-
2050 * Supports:
2051 * - NULL / empty string - returns the current users ID.
2052 * - "current" string - returns the current users ID.
2053 * - "bbprofile" string - returns the BuddyPress profile ID. Requires use on BP profiles.
2054 * - "author" string - returns the post authors user ID. Requires use inside the loop.
2055 * - "replyauthor" string - returns the bbPress reply authors ID. Requires use in bbPress forums topics.
2056 *
2057 * @since 1.7
2058 * @version 1.1
2059 */
2060if ( ! function_exists( 'mycred_get_user_id' ) ) :
2061 function mycred_get_user_id( $requested = '' ) {
2062
2063 if ( is_string( $requested ) && strlen( $requested ) == 0 ) return $requested;
2064
2065 $user_id = 0;
2066 if ( ! is_numeric( $requested ) ) {
2067
2068 // Current user
2069 if ( $requested === 'current' || strlen( $requested ) == 0 )
2070 $user_id = get_current_user_id();
2071
2072 // Comma separated list of IDs
2073 elseif ( count( explode( ',', $requested ) ) > 1 ) {
2074
2075 $user_id = wp_parse_id_list( $requested );
2076
2077 }
2078
2079 // BuddyPress Profile ID
2080 elseif ( $requested === 'bbprofile' ) {
2081
2082 if ( function_exists( 'bp_displayed_user_id' ) )
2083 $requested = bp_displayed_user_id();
2084
2085 }
2086
2087 // Post Author
2088 elseif ( $requested === 'author' ) {
2089
2090 global $post;
2091
2092 $author = get_the_author_meta( 'ID' );
2093
2094 if ( empty( $author ) && isset( $post->post_author ) )
2095 $author = $post->post_author;
2096
2097 if ( absint( $author ) )
2098 $user_id = $author;
2099
2100 }
2101
2102 // bbPress reply author
2103 elseif ( $requested === 'replyauthor' ) {
2104
2105 if ( function_exists( 'bbp_get_reply_author_id' ) )
2106 $user_id = bbp_get_reply_author_id( bbp_get_reply_id() );
2107
2108 }
2109
2110 // Email address
2111 elseif ( is_email( $requested ) ) {
2112
2113 $user = get_user_by( 'email', $requested );
2114 if ( isset( $user->ID ) )
2115 $user_id = $user->ID;
2116
2117 }
2118
2119 else {
2120
2121 $user = get_user_by( 'login', $requested );
2122 if ( isset( $user->ID ) )
2123 $user_id = $user->ID;
2124
2125 else {
2126
2127 $user = get_user_by( 'slug', $requested );
2128 if ( isset( $user->ID ) )
2129 $user_id = $user->ID;
2130
2131 }
2132
2133 }
2134
2135 }
2136 else {
2137
2138 $user_id = absint( $requested );
2139
2140 }
2141
2142 return apply_filters( 'mycred_get_user_id', $user_id, $requested );
2143
2144 }
2145endif;
2146
2147/**
2148 * Get Users Profile URL
2149 * Returns a given users profile URL.
2150 * @since 1.7.4
2151 * @version 1.0.2
2152 */
2153if ( ! function_exists( 'mycred_get_users_profile_url' ) ) :
2154 function mycred_get_users_profile_url( $user_id = NULL ) {
2155
2156 $profile_url = '';
2157 if ( $user_id === NULL || absint( $user_id ) === 0 ) return $profile_url;
2158
2159 $user = get_userdata( $user_id );
2160 $profile_url = get_author_posts_url( $user_id );
2161
2162 // BuddyPress option
2163 if ( function_exists( 'bp_core_get_user_domain' ) )
2164 $profile_url = bp_core_get_user_domain( $user_id );
2165
2166 return apply_filters( 'mycred_users_profile_url', $profile_url, $user );
2167
2168 }
2169endif;
2170
2171/**
2172 * Get Users Account
2173 * Returns either the current users or the given users account object.
2174 * @since 1.7
2175 * @version 1.1
2176 */
2177if ( ! function_exists( 'mycred_get_account' ) ) :
2178 function mycred_get_account( $user_id = NULL ) {
2179
2180 global $mycred_current_account, $mycred_account;
2181
2182 if ( $user_id === NULL ) {
2183
2184 if ( ! did_action( 'init' ) ) {
2185
2186 _doing_it_wrong( __FUNCTION__, 'This function should not be used before init.', '1.8' );
2187
2188 return false;
2189
2190 }
2191
2192 if ( ! is_user_logged_in() ) return false;
2193
2194 $user_id = get_current_user_id();
2195
2196 }
2197
2198 $user_id = absint( $user_id );
2199 if ( $user_id === 0 ) return false;
2200
2201 if ( mycred_is_current_account( $user_id ) )
2202 return $mycred_current_account;
2203
2204 if ( mycred_is_account( $user_id ) ) {
2205
2206 return $mycred_account;
2207
2208 }
2209
2210 $mycred_account = new myCRED_Account( $user_id );
2211
2212 do_action( 'mycred_get_account' );
2213
2214 return $mycred_account;
2215
2216 }
2217endif;
2218
2219/**
2220 * Get Account
2221 * Check if the account global is available based on a given user id.
2222 * @since 1.8
2223 * @version 1.0
2224 */
2225if ( ! function_exists( 'mycred_is_account' ) ) :
2226 function mycred_is_account( $user_id = NULL ) {
2227
2228 global $mycred_account;
2229
2230 if ( isset( $mycred_account )
2231 && ( $mycred_account instanceof myCRED_Account )
2232 && ( $user_id === $mycred_account->user_id )
2233 && ( $user_id !== NULL )
2234 && ( $user_id !== 0 )
2235 ) {
2236
2237 return true;
2238
2239 }
2240
2241 return false;
2242
2243 }
2244endif;
2245
2246/**
2247 * Get Current Account
2248 * Returns the current account object (if one exists) else false.
2249 * @since 1.8
2250 * @version 1.0
2251 */
2252if ( ! function_exists( 'mycred_get_current_account' ) ) :
2253 function mycred_get_current_account() {
2254
2255 global $mycred_current_account;
2256
2257 if ( isset( $mycred_current_account ) && ( $mycred_current_account instanceof myCRED_Account ) )
2258 return $mycred_current_account;
2259
2260 return false;
2261
2262 }
2263endif;
2264
2265/**
2266 * Set Current Account
2267 * Sets the current account object.
2268 * @since 1.8
2269 * @version 1.0
2270 */
2271if ( ! function_exists( 'mycred_set_current_account' ) ) :
2272 function mycred_set_current_account( $user_id = NULL ) {
2273
2274 global $mycred_current_account;
2275
2276 if ( isset( $mycred_current_account )
2277 && ( $mycred_current_account instanceof myCRED_Account )
2278 && ( $user_id === $mycred_current_account->user_id )
2279 && ( $user_id !== NULL )
2280 ) {
2281
2282 return $mycred_current_account;
2283
2284 }
2285
2286 $mycred_current_account = new myCRED_Account( ( ( $user_id === NULL ) ? get_current_user_id() : $user_id ) );
2287
2288 do_action( 'mycred_set_current_account' );
2289
2290 return $mycred_current_account;
2291
2292 }
2293endif;
2294
2295/**
2296 * Get Current Account
2297 * Check if the current account global is available based on a given user id.
2298 * @since 1.8
2299 * @version 1.0
2300 */
2301if ( ! function_exists( 'mycred_is_current_account' ) ) :
2302 function mycred_is_current_account( $user_id = NULL ) {
2303
2304 global $mycred_current_account;
2305
2306 if ( isset( $mycred_current_account )
2307 && ( $mycred_current_account instanceof myCRED_Account )
2308 && ( $user_id === $mycred_current_account->user_id )
2309 && ( $user_id !== NULL )
2310 && ( $user_id !== 0 )
2311 ) {
2312
2313 return true;
2314
2315 }
2316
2317 return false;
2318
2319 }
2320endif;
2321
2322/**
2323 * Get Cred Types
2324 * Returns an associative array of registered point types.
2325 * @param $name_first (bool) option to replace "myCRED" with the point type name set.
2326 * @since 1.4
2327 * @version 1.1
2328 */
2329if ( ! function_exists( 'mycred_get_types' ) ) :
2330 function mycred_get_types( $name_first = false ) {
2331
2332 global $mycred_types;
2333
2334 if ( is_array( $mycred_types ) && ! empty( $mycred_types ) )
2335 $types = $mycred_types;
2336
2337 else {
2338
2339 $types = array();
2340
2341 $available_types = mycred_get_option( 'mycred_types', array( MYCRED_DEFAULT_TYPE_KEY => mycred_label() ) );
2342 if ( count( $available_types ) > 1 ) {
2343
2344 foreach ( $available_types as $type => $label ) {
2345
2346 if ( $type == MYCRED_DEFAULT_TYPE_KEY )
2347 $label = mycred_get_point_type_name( MYCRED_DEFAULT_TYPE_KEY, false );
2348
2349 $types[ $type ] = $label;
2350
2351 }
2352
2353 }
2354 else {
2355
2356 if ( $name_first )
2357 $available_types[ MYCRED_DEFAULT_TYPE_KEY ] = mycred_get_point_type_name( MYCRED_DEFAULT_TYPE_KEY, false );
2358
2359 $types = $available_types;
2360
2361 }
2362
2363 }
2364
2365 return apply_filters( 'mycred_types', $types );
2366
2367 }
2368endif;
2369
2370/**
2371 * Get Point Type
2372 * @since 1.8
2373 * @version 1.0
2374 */
2375if ( ! function_exists( 'mycred_get_point_type' ) ) :
2376 function mycred_get_point_type( $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
2377
2378 $point_type = sanitize_key( $point_type );
2379
2380 global $current_mycred_type;
2381
2382 if ( isset( $current_mycred_type )
2383 && ( $current_mycred_type instanceof myCRED_Point_Type )
2384 && ( $current_mycred_type->cred_id === $point_type )
2385 ) {
2386
2387 return $current_mycred_type;
2388
2389 }
2390
2391 $current_mycred_type = new myCRED_Point_Type( $point_type );
2392
2393 do_action( 'mycred_get_point_type', $point_type );
2394
2395 return $current_mycred_type;
2396
2397 }
2398endif;
2399
2400/**
2401 * Get Usable Point Types
2402 * Returns an array of point type keys that a given user is allowed to use.
2403 * @since 1.7
2404 * @version 1.0.1
2405 */
2406if ( ! function_exists( 'mycred_get_usable_types' ) ) :
2407 function mycred_get_usable_types( $user_id = NULL ) {
2408
2409 $original_id = $user_id;
2410 if ( $user_id === NULL )
2411 $user_id = get_current_user_id();
2412
2413 $usable = array();
2414 if ( is_user_logged_in() || $original_id !== NULL ) {
2415
2416 global $mycred, $mycred_current_account;
2417
2418 if ( ! isset( $mycred_current_account->balance ) || empty( $mycred_current_account->balance ) ) {
2419
2420 $types = mycred_get_types();
2421
2422 if ( count( $types ) == 1 && ! $mycred->exclude_user( $user_id ) )
2423 $usable[] = MYCRED_DEFAULT_TYPE_KEY;
2424
2425 else {
2426
2427 foreach ( $types as $type_id => $type ) {
2428
2429 if ( $type_id == MYCRED_DEFAULT_TYPE_KEY && ! $mycred->exclude_user( $user_id ) )
2430 $usable[] = MYCRED_DEFAULT_TYPE_KEY;
2431
2432 else {
2433
2434 $custom_type = mycred( $type_id );
2435 if ( ! $custom_type->exclude_user( $user_id ) )
2436 $usable[] = $type_id;
2437
2438 }
2439
2440 }
2441
2442 }
2443
2444 }
2445 elseif ( ! empty( $mycred_current_account->balance ) ) {
2446
2447 foreach ( $mycred_current_account->balance as $balance ) {
2448
2449 if ( $balance !== false )
2450 $usable[] = $balance->point_type->cred_id;
2451
2452 }
2453
2454 }
2455
2456 }
2457
2458 return $usable;
2459
2460 }
2461endif;
2462
2463/**
2464 * Point Type Exists
2465 * @since 1.6.8
2466 * @version 1.0.1
2467 */
2468if ( ! function_exists( 'mycred_point_type_exists' ) ) :
2469 function mycred_point_type_exists( $type = NULL ) {
2470
2471 $result = false;
2472 $types = mycred_get_types();
2473 $type = sanitize_key( $type );
2474
2475 // Remove _total from total balances to get the underlaying id
2476 $type = str_replace( '_total', '', $type );
2477
2478 // Need to remove blog id suffix on multisites
2479 // This function should not be used to check for point type ids with
2480 // blog ID suffixes but in case it is used incorrectly, we need to fix this.
2481 if ( is_multisite() )
2482 $type = str_replace( '_' . get_current_blog_id(), '', $type );
2483
2484 if ( strlen( $type ) > 0 && array_key_exists( $type, $types ) )
2485 $result = true;
2486
2487 return $result;
2488
2489 }
2490endif;
2491
2492/**
2493 * Get Point Type Name
2494 * Returns the name given to a particular point type.
2495 * @param $signular (boolean) option to return the plural version, returns singular by default
2496 * @since 0.1
2497 * @version 1.1
2498 */
2499if ( ! function_exists( 'mycred_get_point_type_name' ) ) :
2500 function mycred_get_point_type_name( $point_type = MYCRED_DEFAULT_TYPE_KEY, $singular = true ) {
2501
2502 $mycred = mycred( $point_type );
2503
2504 if ( $singular )
2505 return $mycred->singular();
2506
2507 return $mycred->plural();
2508
2509 }
2510endif;
2511
2512/**
2513 * Select Point Type from Select Dropdown
2514 * @since 1.4
2515 * @version 1.0
2516 */
2517if ( ! function_exists( 'mycred_types_select_from_dropdown' ) ) :
2518 function mycred_types_select_from_dropdown( $name = '', $id = '', $selected = '', $return = false, $extra = '' ) {
2519
2520 $types = mycred_get_types();
2521 $output = '';
2522
2523 if ( count( $types ) == 1 )
2524 $output .= '<input type="hidden"' . $extra . ' name="' . $name . '" id="' . $id . '" value="mycred_default" />';
2525
2526 else {
2527
2528 $output .= '<select' . $extra . ' name="' . $name . '" id="' . $id . '">';
2529
2530 foreach ( $types as $type => $label ) {
2531
2532 if ( $type == MYCRED_DEFAULT_TYPE_KEY ) {
2533 $_mycred = mycred( $type );
2534 $label = $_mycred->plural();
2535 }
2536
2537 $output .= '<option value="' . $type . '"';
2538 if ( $selected == $type ) $output .= ' selected="selected"';
2539 $output .= '>' . $label . '</option>';
2540
2541 }
2542
2543 $output .= '</select>';
2544
2545 }
2546
2547 if ( $return )
2548 return $output;
2549
2550 echo $output;
2551
2552 }
2553endif;
2554
2555/**
2556 * Select Point Type from Checkboxes
2557 * @since 1.4
2558 * @version 1.0.1
2559 */
2560if ( ! function_exists( 'mycred_types_select_from_checkboxes' ) ) :
2561 function mycred_types_select_from_checkboxes( $name = '', $id = '', $selected_values = array(), $return = false ) {
2562
2563 $types = mycred_get_types();
2564
2565 $output = '';
2566 if ( count( $types ) > 0 ) {
2567 foreach ( $types as $type => $label ) {
2568 $selected = '';
2569 if ( in_array( $type, (array) $selected_values ) )
2570 $selected = ' checked="checked"';
2571
2572 $id .= '-' . $type;
2573
2574 $output .= '<label for="' . $id . '"><input type="checkbox" name="' . $name . '" id="' . $id . '" value="' . $type . '"' . $selected . ' /> ' . $label . '</label>';
2575 }
2576 }
2577
2578 if ( $return )
2579 return $output;
2580
2581 echo $output;
2582
2583 }
2584endif;
2585
2586/**
2587 * Get DB Column
2588 * Helper function to return the correct database tabel based on
2589 * our multisite setup.
2590 * @since 1.8
2591 * @version 1.0
2592 */
2593if ( ! function_exists( 'mycred_get_db_column' ) ) :
2594 function mycred_get_db_column( $column = '' ) {
2595
2596 global $wpdb;
2597
2598 $table = '';
2599 if ( ! in_array( $column, array( 'posts', 'postmeta', 'comments', 'commentmeta', 'terms', 'term_meta', 'term_relationships', 'links', 'options' ) ) ) return $table;
2600
2601 if ( isset( $wpdb->$column ) )
2602 $table = $wpdb->$column;
2603
2604 // This is what are are here for. On multisites, if we enable the Master template
2605 // feature, we need to get the column for the networks main site instead of our own
2606 if ( mycred_override_settings() && ! mycred_is_main_site() )
2607 $table = $wpdb->get_blog_prefix( get_network()->site_id ) . $column;
2608
2609 return apply_filters( 'mycred_get_db_column', $table );
2610
2611 }
2612endif;
2613
2614/**
2615 * Add Option
2616 * @since 1.7.6
2617 * @version 1.0
2618 */
2619if ( ! function_exists( 'mycred_add_option' ) ) :
2620 function mycred_add_option( $option_id, $value = '' ) {
2621
2622 if ( is_multisite() ) {
2623
2624 // Master template enabled
2625 if ( mycred_override_settings() )
2626 return add_blog_option( 1, $option_id, $value );
2627
2628 // Master template disabled
2629 return add_blog_option( $GLOBALS['blog_id'], $option_id, $value );
2630
2631 }
2632 return add_option( $option_id, $value );
2633
2634 }
2635endif;
2636
2637/**
2638 * Get Option
2639 * @since 1.4
2640 * @version 1.0.2
2641 */
2642if ( ! function_exists( 'mycred_get_option' ) ) :
2643 function mycred_get_option( $option_id, $default = array() ) {
2644
2645 if ( is_multisite() ) {
2646
2647 // Master template enabled
2648 if ( mycred_override_settings() )
2649 return get_blog_option( get_network()->site_id, $option_id, $default );
2650
2651 // Master template disabled
2652 return get_blog_option( get_current_blog_id(), $option_id, $default );
2653
2654 }
2655
2656 return get_option( $option_id, $default );
2657
2658 }
2659endif;
2660
2661/**
2662 * Update Option
2663 * @since 1.4
2664 * @version 1.0.2
2665 */
2666if ( ! function_exists( 'mycred_update_option' ) ) :
2667 function mycred_update_option( $option_id, $value = '' ) {
2668
2669 if ( is_multisite() ) {
2670
2671 // Master template enabled
2672 if ( mycred_override_settings() )
2673 return update_blog_option( get_network()->site_id, $option_id, $value );
2674
2675 // Master template disabled
2676 return update_blog_option( get_current_blog_id(), $option_id, $value );
2677
2678 }
2679
2680 return update_option( $option_id, $value );
2681
2682 }
2683endif;
2684
2685/**
2686 * Delete Option
2687 * @since 1.5.2
2688 * @version 1.0.1
2689 */
2690if ( ! function_exists( 'mycred_delete_option' ) ) :
2691 function mycred_delete_option( $option_id ) {
2692
2693 if ( is_multisite() ) {
2694
2695 // Master template enabled
2696 if ( mycred_override_settings() )
2697 return delete_blog_option( get_network()->site_id, $option_id );
2698
2699 // Master template disabled
2700 return delete_blog_option( get_current_blog_id(), $option_id );
2701
2702 }
2703
2704 return delete_option( $option_id );
2705
2706 }
2707endif;
2708
2709/**
2710 * Get Meta Key
2711 * @since 1.6.8
2712 * @version 1.0
2713 */
2714if ( ! function_exists( 'mycred_get_meta_key' ) ) :
2715 function mycred_get_meta_key( $key = '', $end = '' ) {
2716
2717 if ( is_multisite() ) {
2718
2719 $blog_id = get_current_blog_id();
2720
2721 if ( $blog_id > 1 && ! mycred_centralize_log() && $key != 'mycred_rank' )
2722 $key .= '_' . $blog_id;
2723
2724 elseif ( $blog_id > 1 && ! mycred_override_settings() && $key == 'mycred_rank' )
2725 $key .= '_' . $blog_id;
2726
2727 }
2728
2729 if ( strlen( $end ) > 0 )
2730 $key .= $end;
2731
2732 return $key;
2733
2734 }
2735endif;
2736
2737/**
2738 * Add User Meta
2739 * @since 1.5
2740 * @version 1.1
2741 */
2742if ( ! function_exists( 'mycred_add_user_meta' ) ) :
2743 function mycred_add_user_meta( $user_id, $key = '', $end = '', $value = '', $unique = false ) {
2744
2745 $key = mycred_get_meta_key( $key, $end );
2746
2747 return add_user_meta( $user_id, $key, $value, $unique );
2748
2749 }
2750endif;
2751
2752/**
2753 * Get User Meta
2754 * @since 1.5
2755 * @version 1.1
2756 */
2757if ( ! function_exists( 'mycred_get_user_meta' ) ) :
2758 function mycred_get_user_meta( $user_id, $key = '', $end = '', $unique = false ) {
2759
2760 $key = mycred_get_meta_key( $key, $end );
2761
2762 return get_user_meta( $user_id, $key, $unique );
2763
2764 }
2765endif;
2766
2767/**
2768 * Update User Meta
2769 * @since 1.5
2770 * @version 1.1
2771 */
2772if ( ! function_exists( 'mycred_update_user_meta' ) ) :
2773 function mycred_update_user_meta( $user_id, $key = '', $end = '', $value = '', $previous = '' ) {
2774
2775 $key = mycred_get_meta_key( $key, $end );
2776
2777 return update_user_meta( $user_id, $key, $value, $previous );
2778
2779 }
2780endif;
2781
2782/**
2783 * Delete User Meta
2784 * @since 1.5
2785 * @version 1.1.1
2786 */
2787if ( ! function_exists( 'mycred_delete_user_meta' ) ) :
2788 function mycred_delete_user_meta( $user_id, $key = '', $end = '', $value = '' ) {
2789
2790 $key = mycred_get_meta_key( $key, $end );
2791
2792 if ( $value === NULL )
2793 return delete_user_meta( $user_id, $key );
2794
2795 return delete_user_meta( $user_id, $key, $value );
2796
2797 }
2798endif;
2799
2800/**
2801 * Add Post Meta
2802 * @since 1.8
2803 * @version 1.0
2804 */
2805if ( ! function_exists( 'mycred_add_post_meta' ) ) :
2806 function mycred_add_post_meta( $post_id, $key = '', $value = '', $unique = false ) {
2807
2808 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
2809
2810 if ( $override )
2811 switch_to_blog( get_network()->site_id );
2812
2813 $result = add_post_meta( $post_id, $key, $value, $unique );
2814
2815 if ( $override )
2816 restore_current_blog();
2817
2818 return $result;
2819
2820 }
2821endif;
2822
2823/**
2824 * Get Post Meta
2825 * @since 1.8
2826 * @version 1.0
2827 */
2828if ( ! function_exists( 'mycred_get_post_meta' ) ) :
2829 function mycred_get_post_meta( $post_id, $key = '', $unique = false ) {
2830
2831 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
2832
2833 if ( $override )
2834 switch_to_blog( get_network()->site_id );
2835
2836 $value = get_post_meta( $post_id, $key, $unique );
2837
2838 if ( $override )
2839 restore_current_blog();
2840
2841 return $value;
2842
2843 }
2844endif;
2845
2846/**
2847 * Update Post Meta
2848 * @since 1.8
2849 * @version 1.0
2850 */
2851if ( ! function_exists( 'mycred_update_post_meta' ) ) :
2852 function mycred_update_post_meta( $post_id, $key = '', $value = '', $previous = '' ) {
2853
2854 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
2855
2856 if ( $override )
2857 switch_to_blog( get_network()->site_id );
2858
2859 $result = update_post_meta( $post_id, $key, $value, $previous );
2860
2861 if ( $override )
2862 restore_current_blog();
2863
2864 return $result;
2865
2866 }
2867endif;
2868
2869/**
2870 * Delete Post Meta
2871 * @since 1.8
2872 * @version 1.0
2873 */
2874if ( ! function_exists( 'mycred_delete_post_meta' ) ) :
2875 function mycred_delete_post_meta( $post_id, $key = '', $value = '' ) {
2876
2877 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
2878
2879 if ( $override )
2880 switch_to_blog( get_network()->site_id );
2881
2882 $result = delete_post_meta( $post_id, $key, $value );
2883
2884 if ( $override )
2885 restore_current_blog();
2886
2887 return $result;
2888
2889 }
2890endif;
2891
2892/**
2893 * Get Post
2894 * @since 1.8
2895 * @version 1.0
2896 */
2897if ( ! function_exists( 'mycred_get_post' ) ) :
2898 function mycred_get_post( $post_id = NULL ) {
2899
2900 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
2901
2902 if ( $override )
2903 switch_to_blog( get_network()->site_id );
2904
2905 $post = get_post( $post_id );
2906
2907 if ( $override )
2908 restore_current_blog();
2909
2910 return $post;
2911
2912 }
2913endif;
2914
2915/**
2916 * Get Post
2917 * @since 1.8
2918 * @version 1.0
2919 */
2920if ( ! function_exists( 'mycred_get_permalink' ) ) :
2921 function mycred_get_permalink( $post_id = NULL ) {
2922
2923 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
2924
2925 if ( $override )
2926 switch_to_blog( get_network()->site_id );
2927
2928 $permalink = get_permalink( $post_id );
2929
2930 if ( $override )
2931 restore_current_blog();
2932
2933 return $permalink;
2934
2935 }
2936endif;
2937
2938/**
2939 * Get Post Type
2940 * @since 1.8
2941 * @version 1.0
2942 */
2943if ( ! function_exists( 'mycred_get_post_type' ) ) :
2944 function mycred_get_post_type( $post_id = NULL ) {
2945
2946 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
2947
2948 if ( $override )
2949 switch_to_blog( get_network()->site_id );
2950
2951 $post_type = get_post_type( $post_id );
2952
2953 if ( $override )
2954 restore_current_blog();
2955
2956 return $post_type;
2957
2958 }
2959endif;
2960
2961/**
2962 * Get Post Title
2963 * @since 1.8
2964 * @version 1.0
2965 */
2966if ( ! function_exists( 'mycred_get_the_title' ) ) :
2967 function mycred_get_the_title( $post_id = NULL ) {
2968
2969 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
2970
2971 if ( $override )
2972 switch_to_blog( get_network()->site_id );
2973
2974 $post_type = get_the_title( $post_id );
2975
2976 if ( $override )
2977 restore_current_blog();
2978
2979 return $post_type;
2980
2981 }
2982endif;
2983
2984/**
2985 * Get Page by Title
2986 * @since 1.8
2987 * @version 1.0
2988 */
2989if ( ! function_exists( 'mycred_get_page_by_title' ) ) :
2990 function mycred_get_page_by_title( $post_id, $type, $post_type ) {
2991
2992 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
2993
2994 if ( $override )
2995 switch_to_blog( get_network()->site_id );
2996
2997 $results = get_page_by_title( $post_id, $type, $post_type );
2998
2999 if ( $override )
3000 restore_current_blog();
3001
3002 return $results;
3003
3004 }
3005endif;
3006
3007/**
3008 * Trash Post
3009 * @since 1.8
3010 * @version 1.0
3011 */
3012if ( ! function_exists( 'mycred_trash_post' ) ) :
3013 function mycred_trash_post( $post_id = NULL ) {
3014
3015 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
3016
3017 if ( $override )
3018 switch_to_blog( get_network()->site_id );
3019
3020 $results = wp_trash_post( $post_id );
3021
3022 if ( $override )
3023 restore_current_blog();
3024
3025 return $results;
3026
3027 }
3028endif;
3029
3030/**
3031 * Delete Post
3032 * @since 1.8
3033 * @version 1.0
3034 */
3035if ( ! function_exists( 'mycred_delete_post' ) ) :
3036 function mycred_delete_post( $post_id = NULL, $force = false ) {
3037
3038 $override = ( mycred_override_settings() && ! mycred_is_main_site() );
3039
3040 if ( $override )
3041 switch_to_blog( get_network()->site_id );
3042
3043 $results = wp_delete_post( $post_id, $force );
3044
3045 if ( $override )
3046 restore_current_blog();
3047
3048 return $results;
3049
3050 }
3051endif;
3052
3053/**
3054 * Is Admin
3055 * Conditional tag that checks if a given user or the current user
3056 * can either edit the plugin or creds.
3057 * @param $user_id (int), optional user id to check, defaults to current user
3058 * @returns true or false
3059 * @since 0.1
3060 * @version 1.2
3061 */
3062if ( ! function_exists( 'mycred_is_admin' ) ) :
3063 function mycred_is_admin( $user_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3064
3065 $mycred = mycred( $point_type );
3066
3067 if ( $mycred->user_is_point_admin( $user_id ) || $mycred->user_is_point_editor( $user_id ) )
3068 return true;
3069
3070 return false;
3071
3072 }
3073endif;
3074
3075/**
3076 * Exclude User
3077 * Checks if a given user is excluded from using myCRED.
3078 * @see http://codex.mycred.me/functions/mycred_exclude_user/
3079 * @param $user_id (int), optional user to check, defaults to current user
3080 * @since 0.1
3081 * @version 1.2
3082 */
3083if ( ! function_exists( 'mycred_exclude_user' ) ) :
3084 function mycred_exclude_user( $user_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3085
3086 $mycred = mycred( $point_type );
3087
3088 return $mycred->exclude_user( $user_id );
3089
3090 }
3091endif;
3092
3093/**
3094 * Get Users Point Balance
3095 * Retreaves a given users point balance.
3096 * @returns false if user is excluded or if invalid values are provided, else returns the raw balance.
3097 * @since 1.7.4
3098 * @version 1.1
3099 */
3100if ( ! function_exists( 'mycred_get_users_balance' ) ) :
3101 function mycred_get_users_balance( $user_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3102
3103 $mycred = mycred( $point_type );
3104
3105 if ( $mycred->exclude_user( $user_id ) ) return false;
3106
3107 return $mycred->get_users_balance( $user_id, $point_type );
3108
3109 }
3110endif;
3111// Depreciated
3112if ( ! function_exists( 'mycred_get_users_cred' ) ) :
3113 function mycred_get_users_cred( $user_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3114
3115 return mycred_get_users_balance( $user_id, $point_type );
3116
3117 }
3118endif;
3119
3120/**
3121 * Get Users Total Balance
3122 * @since 1.7.6
3123 * @version 1.0
3124 */
3125if ( ! function_exists( 'mycred_get_users_total_balance' ) ) :
3126 function mycred_get_users_total_balance( $user_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3127
3128 $mycred = mycred( $point_type );
3129
3130 if ( $mycred->exclude_user( $user_id ) ) return false;
3131
3132 return $mycred->get_users_total_balance( $user_id, $point_type );
3133
3134 }
3135endif;
3136
3137/**
3138 * Get Users Creds Formated
3139 * Returns the given users current cred balance formated. If no user id is given
3140 * this function will return false!
3141 * @param $user_id (int), required user id
3142 * @return users balance (string) or false if no user id is given
3143 * @since 0.1
3144 * @version 1.3
3145 */
3146if ( ! function_exists( 'mycred_display_users_balance' ) ) :
3147 function mycred_display_users_balance( $user_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3148
3149 $mycred = mycred( $point_type );
3150
3151 if ( $mycred->exclude_user( $user_id ) ) return '';
3152
3153 $balance = $mycred->get_users_balance( $user_id, $point_type );
3154
3155 return $mycred->format_creds( $balance );
3156
3157 }
3158endif;
3159// Depreciated
3160if ( ! function_exists( 'mycred_get_users_fcred' ) ) :
3161 function mycred_get_users_fcred( $user_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3162
3163 return mycred_display_users_balance( $user_id, $point_type );
3164
3165 }
3166endif;
3167
3168/**
3169 * Display Users Total Balance
3170 * @since 1.7.6
3171 * @version 1.1
3172 */
3173if ( ! function_exists( 'mycred_display_users_total_balance' ) ) :
3174 function mycred_display_users_total_balance( $user_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3175
3176 $mycred = mycred( $point_type );
3177
3178 if ( $mycred->exclude_user( $user_id ) ) return '';
3179
3180 $balance = $mycred->get_users_total_balance( $user_id, $point_type );
3181
3182 return $mycred->format_creds( $balance );
3183
3184 }
3185endif;
3186
3187/**
3188 * Format Number
3189 * @since 1.3.3
3190 * @version 1.2
3191 */
3192if ( ! function_exists( 'mycred_format_number' ) ) :
3193 function mycred_format_number( $value = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3194
3195 if ( $value === NULL || ! is_numeric( $value ) ) return $value;
3196
3197 $mycred = mycred( $point_type );
3198
3199 return $mycred->format_number( $value );
3200
3201 }
3202endif;
3203
3204/**
3205 * Format Points
3206 * @since 1.8
3207 * @version 1.0
3208 */
3209if ( ! function_exists( 'mycred_format_points' ) ) :
3210 function mycred_format_points( $value = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3211
3212 if ( $value === NULL || ! is_numeric( $value ) ) return $value;
3213
3214 $mycred = mycred( $point_type );
3215
3216 return $mycred->format_creds( $value );
3217
3218 }
3219endif;
3220// Depreciated
3221if ( ! function_exists( 'mycred_format_creds' ) ) :
3222 function mycred_format_creds( $value = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3223
3224 return mycred_format_points( $value, $point_type );
3225
3226 }
3227endif;
3228
3229/**
3230 * Add Points
3231 * Adds creds to a given user. A refernece ID, user id and amount must be given.
3232 * Important! This function will not check if the user should be excluded from gaining points, this must
3233 * be done before calling this function!
3234 * @see http://codex.mycred.me/functions/mycred_add/
3235 * @param $ref (string), required reference id
3236 * @param $user_id (int), required id of the user who will get these points
3237 * @param $amount (int|float), required number of creds to give or deduct from the given user.
3238 * @param $ref_id (array), optional array of reference IDs allowing the use of content specific keywords in the log entry
3239 * @param $data (object|array|string|int), optional extra data to save in the log. Note that arrays gets serialized!
3240 * @returns boolean true on success or false on fail
3241 * @since 0.1
3242 * @version 1.3
3243 */
3244if ( ! function_exists( 'mycred_add' ) ) :
3245 function mycred_add( $ref = '', $user_id = '', $amount = '', $entry = '', $ref_id = '', $data = '', $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3246
3247 // $ref, $user_id and $cred is required
3248 if ( $ref == '' || $user_id == '' || $amount == '' ) return false;
3249
3250 $mycred = mycred( $point_type );
3251
3252 return $mycred->add_creds( $ref, $user_id, $amount, $entry, $ref_id, $data, $point_type );
3253
3254 }
3255endif;
3256
3257/**
3258 * Subtract Creds
3259 * Subtracts creds from a given user. Works just as mycred_add() but the creds are converted into a negative value.
3260 * @see http://codex.mycred.me/functions/mycred_subtract/
3261 * @uses mycred_add()
3262 * @since 0.1
3263 * @version 1.1.1
3264 */
3265if ( ! function_exists( 'mycred_subtract' ) ) :
3266 function mycred_subtract( $ref = '', $user_id = '', $amount = '', $entry = '', $ref_id = '', $data = '', $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
3267
3268 if ( $ref == '' || $user_id == '' || $amount == '' ) return false;
3269 if ( $amount > 0 ) $amount = 0 - $amount;
3270
3271 return mycred_add( $ref, $user_id, $amount, $entry, $ref_id, $data, $point_type );
3272
3273 }
3274endif;
3275
3276/**
3277 * Plugin Activation
3278 * @since 1.3
3279 * @version 1.1.1
3280 */
3281if ( ! function_exists( 'mycred_plugin_activation' ) ) :
3282 function mycred_plugin_activation() {
3283
3284 // Load Installer
3285 require_once myCRED_INCLUDES_DIR . 'mycred-install.php';
3286 $installer = mycred_installer();
3287
3288 // Compatibility check
3289 $installer::compat();
3290
3291 // First time activation
3292 if ( get_option( 'mycred_version', false ) === false )
3293 $installer::activate();
3294
3295 // Re-activation
3296 else
3297 $installer::reactivate();
3298
3299 }
3300endif;
3301
3302/**
3303 * Runs when the plugin is deactivated
3304 * @since 1.3
3305 * @version 1.0
3306 */
3307if ( ! function_exists( 'mycred_plugin_deactivation' ) ) :
3308 function mycred_plugin_deactivation() {
3309
3310 // Clear Cron
3311 wp_clear_scheduled_hook( 'mycred_reset_key' );
3312 wp_clear_scheduled_hook( 'mycred_banking_recurring_payout' );
3313 wp_clear_scheduled_hook( 'mycred_banking_interest_compound' );
3314 wp_clear_scheduled_hook( 'mycred_banking_interest_payout' );
3315
3316 do_action( 'mycred_deactivation' );
3317
3318 }
3319endif;
3320
3321/**
3322 * Runs when the plugin is deleted
3323 * @since 1.3
3324 * @version 1.0.2
3325 */
3326if ( ! function_exists( 'mycred_plugin_uninstall' ) ) :
3327 function mycred_plugin_uninstall() {
3328
3329 // Load Installer
3330 require_once myCRED_INCLUDES_DIR . 'mycred-install.php';
3331 $installer = mycred_installer();
3332
3333 do_action( 'mycred_before_deletion', $installer );
3334
3335 // Run uninstaller
3336 $installer::uninstall();
3337
3338 do_action( 'mycred_after_deletion', $installer );
3339
3340 }
3341endif;
3342
3343/**
3344 * Apply Defaults
3345 * Based on the shortcode_atts() function with support for
3346 * multidimentional arrays.
3347 * @since 1.1.2
3348 * @version 1.0
3349 */
3350if ( ! function_exists( 'mycred_apply_defaults' ) ) :
3351 function mycred_apply_defaults( &$pref, $set ) {
3352
3353 $set = (array) $set;
3354 $return = array();
3355
3356 foreach ( $pref as $key => $value ) {
3357
3358 if ( array_key_exists( $key, $set ) ) {
3359
3360 if ( is_array( $value ) && ! empty( $value ) )
3361 $return[ $key ] = mycred_apply_defaults( $value, $set[ $key ] );
3362
3363 else
3364 $return[ $key ] = $set[ $key ];
3365
3366 }
3367
3368 else $return[ $key ] = $value;
3369
3370 }
3371
3372 return $return;
3373
3374 }
3375endif;
3376
3377/**
3378 * Strip Tags
3379 * Strippes HTML tags from a given string.
3380 * @param $string (string) string to stip
3381 * @param $overwrite (string), optional HTML tags to allow
3382 * @since 0.1
3383 * @version 1.0
3384 */
3385if ( ! function_exists( 'mycred_strip_tags' ) ) :
3386 function mycred_strip_tags( $string = '', $overwride = '' ) {
3387
3388 $mycred = mycred();
3389
3390 return $mycred->allowed_tags( $string, $overwrite );
3391
3392 }
3393endif;
3394
3395/**
3396 * Flush Widget Cache
3397 * @since 0.1
3398 * @version 1.0
3399 */
3400if ( ! function_exists( 'mycred_flush_widget_cache' ) ) :
3401 function mycred_flush_widget_cache( $id = NULL ) {
3402
3403 if ( $id === NULL ) return;
3404 wp_cache_delete( $id, 'widget' );
3405
3406 }
3407endif;
3408
3409/**
3410 * Get Exchange Rates
3411 * Returns the exchange rates for point types
3412 * @since 1.5
3413 * @version 1.0
3414 */
3415if ( ! function_exists( 'mycred_get_exchange_rates' ) ) :
3416 function mycred_get_exchange_rates( $point_type = '' ) {
3417
3418 $types = mycred_get_types();
3419 $default = array();
3420
3421 foreach ( $types as $type => $label ) {
3422 if ( $type == $point_type ) continue;
3423 $default[ $type ] = 0;
3424 }
3425
3426 $settings = mycred_get_option( 'mycred_pref_exchange_' . $point_type, $default );
3427 $settings = mycred_apply_defaults( $default, $settings );
3428
3429 return $settings;
3430
3431 }
3432endif;
3433
3434/**
3435 * Is Float?
3436 * @since 1.5
3437 * @version 1.0
3438 */
3439if ( ! function_exists( 'isfloat' ) ) :
3440 function isfloat( $f ) {
3441
3442 return ( $f == (string)(float) $f );
3443
3444 }
3445endif;
3446
3447/**
3448 * Translate Limit Code
3449 * @since 1.6
3450 * @version 1.0.1
3451 */
3452if ( ! function_exists( 'mycred_translate_limit_code' ) ) :
3453 function mycred_translate_limit_code( $code = '' ) {
3454
3455 if ( $code == '' ) return '-';
3456
3457 if ( $code == '0/x' || $code == 0 )
3458 return __( 'No limit', 'mycred' );
3459
3460 $result = '-';
3461 $check = explode( '/', $code );
3462 if ( count( $check ) == 2 ) {
3463
3464 $per = __( 'in total', 'mycred' );
3465 if ( $check[1] == 'd' )
3466 $per = __( 'per day', 'mycred' );
3467
3468 elseif ( $check[1] == 'w' )
3469 $per = __( 'per week', 'mycred' );
3470
3471 elseif ( $check[1] == 'm' )
3472 $per = __( 'per month', 'mycred' );
3473
3474 $result = sprintf( _n( 'Maximum once', 'Maximum %d times', $check[0], 'mycred' ), $check[0] ) . ' ' . $per;
3475
3476 }
3477
3478 elseif ( is_numeric( $code ) ) {
3479
3480 $result = sprintf( _n( 'Maximum once', 'Maximum %d times', $code, 'mycred' ), $code );
3481
3482 }
3483
3484 return apply_filters( 'mycred_translate_limit_code', $result, $code );
3485
3486 }
3487endif;
3488
3489/**
3490 * Ordinal Suffix
3491 * @since 1.7
3492 * @version 1.1
3493 */
3494if ( ! function_exists( 'mycred_ordinal_suffix' ) ) :
3495 function mycred_ordinal_suffix( $num = 0, $depreciated = true ) {
3496
3497 if ( ! is_numeric( $num ) ) return $num;
3498
3499 $value = $num;
3500 $num = $num % 100; // protect against large numbers
3501
3502 $result = sprintf( _x( '%d th', 'e.g. 5 th', 'mycred' ), $value );
3503 if ( $num < 11 || $num > 13 ) {
3504 switch ( $num % 10 ) {
3505
3506 case 1 : $result = sprintf( _x( '%d st', 'e.g. 1 st', 'mycred' ), $value );
3507 case 2 : $result = sprintf( _x( '%d nd', 'e.g. 2 nd', 'mycred' ), $value );
3508 case 3 : $result = sprintf( _x( '%d rd', 'e.g. 3 rd', 'mycred' ), $value );
3509
3510 }
3511 }
3512
3513 return apply_filters( 'mycred_ordinal_suffix', $result, $value );
3514
3515 }
3516endif;
3517
3518/**
3519 * Date to Timestamp
3520 * Converts a well formatted date string into GMT unixtimestamp.
3521 * @since 1.7
3522 * @version 1.0
3523 */
3524if ( ! function_exists( 'mycred_date_to_gmt_timestamp' ) ) :
3525 function mycred_date_to_gmt_timestamp( $string = '' ) {
3526
3527 return strtotime( get_gmt_from_date( $string ) );
3528
3529 }
3530endif;
3531
3532/**
3533 * Timestamp to Date
3534 * Converts a GMT unixtimestamp to local timestamp
3535 * @since 1.7
3536 * @version 1.0
3537 */
3538if ( ! function_exists( 'mycred_gmt_timestamp_to_local' ) ) :
3539 function mycred_gmt_timestamp_to_local( $string = '' ) {
3540
3541 return strtotime( get_date_from_gmt( date( 'Y-m-d H:i:s', $string ), 'Y-m-d H:i:s' ) );
3542
3543 }
3544endif;
3545
3546/**
3547 * Force Singular Session
3548 * Used to prevent multiple simultaneous AJAX calls from any one user.
3549 * The $timelimit sets the minimum amount of seconds that must have passed between
3550 * two AJAX requests.
3551 * @since 1.7
3552 * @version 1.1
3553 */
3554if ( ! function_exists( 'mycred_force_singular_session' ) ) :
3555 function mycred_force_singular_session( $user_id = NULL, $key = NULL, $timelimit = MYCRED_MIN_TIME_LIMIT ) {
3556
3557 $force = false;
3558 $time = time();
3559 $user_id = absint( $user_id );
3560 $key = sanitize_text_field( $key );
3561 $timelimit = absint( $timelimit );
3562
3563 if ( $key == '' ) return true;
3564
3565 // 1 - Cookies
3566 $last_call = $time - $timelimit;
3567 $cookie_key = md5( $user_id . $key );
3568 if ( isset( $_COOKIE[ $cookie_key ] ) )
3569 $last_call = absint( $_COOKIE[ $cookie_key ] );
3570
3571 if ( ( $time - $last_call ) < $timelimit )
3572 $force = true;
3573
3574 setcookie( $cookie_key, $time, ( time() + DAY_IN_SECONDS ), COOKIEPATH, COOKIE_DOMAIN );
3575
3576 return apply_filters( 'mycred_force_singular_session', $force, $user_id, $key, $timelimit );
3577
3578 }
3579endif;
3580
3581/**
3582 * Locate Template
3583 * @since 1.0
3584 * @version 1.0
3585 */
3586if ( ! function_exists( 'mycred_locate_template' ) ) :
3587 function mycred_locate_template( $template_name, $template_path = 'mycred', $default_path = '' ) {
3588
3589 if ( empty( $template_path ) || empty( $default_path ) ) return false;
3590
3591 if ( substr( $template_path, -1 ) != '/' )
3592 $template_path = trailingslashit( $template_path );
3593
3594 // Look within passed path within the theme - this is priority.
3595 $template = locate_template( array( $template_path . $template_name, $template_name ) );
3596
3597 // Get default template/
3598 if ( ! $template || empty( $template ) ) $template = $default_path . $template_name;
3599
3600 // Return what we found.
3601 return apply_filters( 'mycred_locate_template', $template, $template_name, $template_path );
3602
3603 }
3604endif;