myCRED - Version 1.7

Version Description

Improved Badges add-on, Banking add-on and Sell Content add-on. Redesigned admin area and improved performance.

=

Download this release

Release Info

Developer designbymerovingi
Plugin Icon 128x128 myCRED
Version 1.7
Comparing to
See all releases

Code changes from version 1.6.9 to 1.7

Files changed (73) hide show
  1. abstracts/mycred-abstract-hook.php +5 -5
  2. abstracts/mycred-abstract-module.php +42 -21
  3. abstracts/mycred-abstract-object.php +41 -0
  4. addons/badges/assets/js/edit-badge.js +455 -0
  5. addons/{transfer/assets/css → badges/assets/js}/index.php +0 -0
  6. addons/badges/includes/mycred-badge-functions.php +533 -159
  7. addons/badges/includes/mycred-badge-object.php +92 -0
  8. addons/badges/includes/mycred-badge-shortcodes.php +48 -86
  9. addons/badges/myCRED-addon-badges.php +894 -630
  10. addons/banking/abstracts/mycred-abstract-service.php +152 -384
  11. addons/banking/assets/index.php +3 -0
  12. addons/banking/assets/js/index.php +3 -0
  13. addons/banking/assets/js/manage-schedules.js +180 -0
  14. addons/banking/includes/index.php +3 -0
  15. addons/banking/includes/mycred-banking-functions.php +391 -0
  16. addons/banking/myCRED-addon-banking.php +165 -102
  17. addons/banking/services/mycred-bank-service-interest.php +0 -685
  18. addons/banking/services/mycred-bank-service-payouts.php +0 -352
  19. addons/banking/services/{mycred-bank-service-central.php → mycred-service-central.php} +47 -39
  20. addons/banking/services/mycred-service-interest.php +693 -0
  21. addons/banking/services/mycred-service-payouts.php +1004 -0
  22. addons/buy-creds/abstracts/mycred-abstract-payment-gateway.php +318 -216
  23. addons/buy-creds/assets/css/gateway.css +47 -85
  24. addons/buy-creds/gateways/bank-transfer.php +233 -0
  25. addons/buy-creds/gateways/bitpay.php +30 -38
  26. addons/buy-creds/gateways/netbilling.php +28 -36
  27. addons/buy-creds/gateways/paypal-standard.php +29 -36
  28. addons/buy-creds/gateways/skrill.php +28 -34
  29. addons/buy-creds/gateways/zombaio.php +45 -43
  30. addons/buy-creds/includes/buycred-functions.php +298 -0
  31. addons/buy-creds/includes/index.php +3 -0
  32. addons/buy-creds/modules/buycred-module-core.php +1486 -0
  33. addons/buy-creds/modules/buycred-module-pending.php +830 -0
  34. addons/buy-creds/modules/index.php +3 -0
  35. addons/buy-creds/myCRED-addon-buy-creds.php +28 -1823
  36. addons/coupons/includes/mycred-coupon-functions.php +274 -131
  37. addons/coupons/includes/mycred-coupon-shortcodes.php +20 -14
  38. addons/coupons/myCRED-addon-coupons.php +283 -155
  39. addons/email-notices/myCRED-addon-email-notices.php +1041 -853
  40. addons/gateway/carts/mycred-marketpress.php +158 -99
  41. addons/gateway/carts/mycred-woocommerce.php +99 -65
  42. addons/gateway/carts/mycred-wpecommerce.php +152 -117
  43. addons/gateway/event-booking/mycred-eventespresso3.php +123 -69
  44. addons/gateway/event-booking/mycred-eventsmanager-pro.php +1 -1
  45. addons/gateway/event-booking/mycred-eventsmanager.php +4 -4
  46. addons/gateway/myCRED-addon-gateway.php +7 -5
  47. addons/notifications/myCRED-addon-notifications.php +18 -8
  48. addons/ranks/assets/js/management.js +2 -2
  49. addons/ranks/includes/mycred-rank-functions.php +347 -555
  50. addons/ranks/includes/mycred-rank-object.php +80 -0
  51. addons/ranks/includes/mycred-rank-shortcodes.php +79 -70
  52. addons/ranks/myCRED-addon-ranks.php +658 -534
  53. addons/sell-content/assets/js/buy-content.js +36 -22
  54. addons/sell-content/includes/index.php +3 -0
  55. addons/sell-content/includes/mycred-sell-functions.php +805 -0
  56. addons/sell-content/includes/mycred-sell-shortcodes.php +319 -0
  57. addons/sell-content/myCRED-addon-sell-content.php +718 -1055
  58. addons/stats/abstracts/mycred-abstract-stat-widget.php +8 -10
  59. addons/stats/assets/css/{stats-page.css → mycred-statistics-admin.css} +1 -1
  60. addons/stats/includes/classes/class.query-stats.php +29 -0
  61. addons/stats/includes/classes/index.php +3 -0
  62. addons/stats/includes/mycred-stats-functions.php +9 -1
  63. addons/stats/includes/mycred-stats-shortcodes.php +6 -13
  64. addons/stats/myCRED-addon-stats.php +211 -151
  65. addons/stats/widgets/mycred-stats-widget-circulation.php +12 -9
  66. addons/stats/widgets/mycred-stats-widget-daily-gains.php +2 -2
  67. addons/stats/widgets/mycred-stats-widget-daily-loses.php +2 -2
  68. addons/transfer/assets/css/transfer.css +0 -33
  69. addons/transfer/assets/js/mycred-transfer.js +133 -0
  70. addons/transfer/assets/js/transfer.js +0 -105
  71. addons/transfer/includes/mycred-transfer-functions.php +33 -25
  72. addons/transfer/includes/mycred-transfer-shortcodes.php +160 -108
  73. addons/transfer/includes/mycred-transfer-widgets.php +42 -68
abstracts/mycred-abstract-hook.php CHANGED
@@ -21,7 +21,7 @@ if ( ! class_exists( 'myCRED_Hook' ) ) :
21
 
22
  // Multipoint types
23
  public $is_main_type = true;
24
- public $mycred_type = 'mycred_default';
25
 
26
  // Hook Prefs
27
  public $prefs = false;
@@ -29,7 +29,7 @@ if ( ! class_exists( 'myCRED_Hook' ) ) :
29
  /**
30
  * Construct
31
  */
32
- function __construct( $args = array(), $hook_prefs = NULL, $type = 'mycred_default' ) {
33
 
34
  if ( ! empty( $args ) ) {
35
  foreach ( $args as $key => $value ) {
@@ -38,15 +38,15 @@ if ( ! class_exists( 'myCRED_Hook' ) ) :
38
  }
39
 
40
  // Grab myCRED Settings
41
- $this->core = mycred( $type );
42
  $this->point_types = mycred_get_types();
43
 
44
  if ( $type != '' ) {
45
  $this->core->cred_id = sanitize_text_field( $type );
46
- $this->mycred_type = $this->core->cred_id;
47
  }
48
 
49
- if ( $this->mycred_type != 'mycred_default' )
50
  $this->is_main_type = false;
51
 
52
  // Grab settings
21
 
22
  // Multipoint types
23
  public $is_main_type = true;
24
+ public $mycred_type = MYCRED_DEFAULT_TYPE_KEY;
25
 
26
  // Hook Prefs
27
  public $prefs = false;
29
  /**
30
  * Construct
31
  */
32
+ function __construct( $args = array(), $hook_prefs = NULL, $type = MYCRED_DEFAULT_TYPE_KEY ) {
33
 
34
  if ( ! empty( $args ) ) {
35
  foreach ( $args as $key => $value ) {
38
  }
39
 
40
  // Grab myCRED Settings
41
+ $this->core = mycred( $type );
42
  $this->point_types = mycred_get_types();
43
 
44
  if ( $type != '' ) {
45
  $this->core->cred_id = sanitize_text_field( $type );
46
+ $this->mycred_type = $this->core->cred_id;
47
  }
48
 
49
+ if ( $this->mycred_type != MYCRED_DEFAULT_TYPE_KEY )
50
  $this->is_main_type = false;
51
 
52
  // Grab settings
abstracts/mycred-abstract-module.php CHANGED
@@ -58,11 +58,10 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
58
  /**
59
  * Construct
60
  */
61
- function __construct( $module_id = '', $args = array(), $type = 'mycred_default' ) {
62
 
63
  // Module ID is required
64
- if ( empty( $module_id ) )
65
- wp_die( 'myCRED_Module() Error. A Module ID is required!' );
66
 
67
  $this->module_id = $module_id;
68
  $this->core = mycred( $type );
@@ -72,7 +71,7 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
72
  $this->mycred_type = $this->core->cred_id;
73
  }
74
 
75
- if ( $this->mycred_type != 'mycred_default' )
76
  $this->is_main_type = false;
77
 
78
  $this->point_types = mycred_get_types();
@@ -110,7 +109,7 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
110
  $this->screen_id = $args['screen_id'];
111
 
112
  if ( ! $this->is_main_type && ! empty( $this->screen_id ) )
113
- $this->screen_id = 'myCRED_' . $this->mycred_type . substr( $this->screen_id, 6 );
114
 
115
  $this->add_to_core = $args['add_to_core'];
116
  $this->accordion = $args['accordion'];
@@ -127,7 +126,7 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
127
  /**
128
  * Set Settings
129
  * @since 0.1
130
- * @version 1.2
131
  */
132
  function set_settings() {
133
 
@@ -145,8 +144,8 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
145
  $this->$module = $this->core->$module;
146
 
147
  // Apply defaults in case new settings have been applied
148
- if ( ! empty( $defaults ) )
149
- $this->$module = mycred_apply_defaults( $this->default_prefs, $this->$module );
150
 
151
  }
152
 
@@ -176,8 +175,8 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
176
  $this->$module[ $option_name ] = $settings;
177
 
178
  // Apply defaults in case new settings have been applied
179
- if ( array_key_exists( $option_id, $defaults ) )
180
- $this->$module[ $option_name ] = mycred_apply_defaults( $this->default_prefs[ $option_id ], $this->$module[ $option_name ] );
181
 
182
  }
183
 
@@ -200,7 +199,7 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
200
 
201
  // Apply defaults in case new settings have been applied
202
  if ( ! empty( $this->default_prefs ) )
203
- $this->$module = mycred_apply_defaults( $this->default_prefs, $this->$module );
204
 
205
  }
206
 
@@ -240,11 +239,11 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
240
  add_filter( 'mycred_save_core_prefs', array( $this, 'sanitize_extra_settings' ), 90, 3 );
241
  }
242
 
243
- // add_action( 'mycred_ready', array( $this, 'module_ready' ) );
244
  add_action( 'mycred_pre_init', array( $this, 'module_pre_init' ) );
245
  add_action( 'mycred_init', array( $this, 'module_init' ) );
246
  add_action( 'mycred_admin_init', array( $this, 'module_admin_init' ) );
247
  add_action( 'mycred_widgets_init', array( $this, 'module_widgets_init' ) );
 
248
 
249
  }
250
 
@@ -382,19 +381,19 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
382
  /**
383
  * Add Admin Menu Item
384
  * @since 0.1
385
- * @version 1.2.1
386
  */
387
  function add_menu() {
388
 
389
  // Network Setting for Multisites
390
- if ( mycred_override_settings() && $GLOBALS['blog_id'] > 1 && substr( $this->screen_id, 0, 6 ) == 'myCRED' && strlen( $this->screen_id ) > 6 ) return;
391
 
392
  if ( ! empty( $this->labels ) && ! empty( $this->screen_id ) ) {
393
 
394
  // Menu Slug
395
- $menu_slug = 'myCRED';
396
  if ( ! $this->is_main_type )
397
- $menu_slug = 'myCRED_' . $this->mycred_type;
398
 
399
  // Menu Label
400
  if ( ! isset( $this->labels['page_title'] ) && ! isset( $this->labels['menu'] ) )
@@ -439,6 +438,13 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
439
 
440
  }
441
 
 
 
 
 
 
 
 
442
  /**
443
  * Save Log Entries per page
444
  * @since 0.1
@@ -483,6 +489,16 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
483
 
484
  }
485
 
 
 
 
 
 
 
 
 
 
 
486
  /**
487
  * Enqueue Scripts & Styles
488
  * Scripts and styles to enqueu on module admin pages.
@@ -496,14 +512,14 @@ if ( ! class_exists( 'myCRED_Module' ) ) :
496
  // Load Accordion
497
  if ( $this->accordion ) {
498
 
499
- wp_enqueue_script( 'mycred-admin' );
500
  wp_enqueue_style( 'mycred-admin' );
501
 
502
  $open = '-1';
503
  if ( isset( $_GET['open-tab'] ) && is_numeric( $_GET['open-tab'] ) )
504
  $open = absint( $_GET['open-tab'] );
505
 
506
- wp_localize_script( 'mycred-admin', 'myCRED', apply_filters( 'mycred_localize_admin', array( 'active' => $open ) ) );
 
507
 
508
  ?>
509
  <!-- myCRED Accordion Styling -->
@@ -646,9 +662,14 @@ h4.ui-accordion-header:before { content: "<?php _e( 'click to open', 'mycred' );
646
  }
647
 
648
  if ( $this->add_to_core === true )
649
- $id = $this->module_name . '-' . $id;
 
 
 
 
 
650
 
651
- return str_replace( '_', '-', $this->module_id ) . '-' . $id;
652
 
653
  }
654
 
@@ -670,7 +691,7 @@ h4.ui-accordion-header:before { content: "<?php _e( 'click to open', 'mycred' );
670
  */
671
  function get_settings_url( $module = '' ) {
672
 
673
- $variables = array( 'page' => 'myCRED_page_settings' );
674
  if ( ! empty( $module ) )
675
  $variables['open-tab'] = $module;
676
 
58
  /**
59
  * Construct
60
  */
61
+ function __construct( $module_id = '', $args = array(), $type = MYCRED_DEFAULT_TYPE_KEY ) {
62
 
63
  // Module ID is required
64
+ if ( empty( $module_id ) ) wp_die( 'myCRED_Module() Error. A Module ID is required!' );
 
65
 
66
  $this->module_id = $module_id;
67
  $this->core = mycred( $type );
71
  $this->mycred_type = $this->core->cred_id;
72
  }
73
 
74
+ if ( $this->mycred_type != MYCRED_DEFAULT_TYPE_KEY )
75
  $this->is_main_type = false;
76
 
77
  $this->point_types = mycred_get_types();
109
  $this->screen_id = $args['screen_id'];
110
 
111
  if ( ! $this->is_main_type && ! empty( $this->screen_id ) )
112
+ $this->screen_id = 'mycred_' . $this->mycred_type . substr( $this->screen_id, 6 );
113
 
114
  $this->add_to_core = $args['add_to_core'];
115
  $this->accordion = $args['accordion'];
126
  /**
127
  * Set Settings
128
  * @since 0.1
129
+ * @version 1.2.1
130
  */
131
  function set_settings() {
132
 
144
  $this->$module = $this->core->$module;
145
 
146
  // Apply defaults in case new settings have been applied
147
+ if ( ! empty( $this->default_prefs ) )
148
+ $this->$module = wp_parse_args( $this->$module, $this->default_prefs );
149
 
150
  }
151
 
175
  $this->$module[ $option_name ] = $settings;
176
 
177
  // Apply defaults in case new settings have been applied
178
+ if ( array_key_exists( $option_id, $this->default_prefs ) )
179
+ $this->$module[ $option_name ] = wp_parse_args( $this->$module[ $option_name ], $this->default_prefs[ $option_id ] );
180
 
181
  }
182
 
199
 
200
  // Apply defaults in case new settings have been applied
201
  if ( ! empty( $this->default_prefs ) )
202
+ $this->$module = wp_parse_args( $this->$module, $this->default_prefs );
203
 
204
  }
205
 
239
  add_filter( 'mycred_save_core_prefs', array( $this, 'sanitize_extra_settings' ), 90, 3 );
240
  }
241
 
 
242
  add_action( 'mycred_pre_init', array( $this, 'module_pre_init' ) );
243
  add_action( 'mycred_init', array( $this, 'module_init' ) );
244
  add_action( 'mycred_admin_init', array( $this, 'module_admin_init' ) );
245
  add_action( 'mycred_widgets_init', array( $this, 'module_widgets_init' ) );
246
+ add_action( 'mycred_admin_enqueue', array( $this, 'scripts_and_styles' ) );
247
 
248
  }
249
 
381
  /**
382
  * Add Admin Menu Item
383
  * @since 0.1
384
+ * @version 1.2.2
385
  */
386
  function add_menu() {
387
 
388
  // Network Setting for Multisites
389
+ if ( mycred_override_settings() && $GLOBALS['blog_id'] > 1 && substr( $this->screen_id, 0, 6 ) == MYCRED_SLUG && strlen( $this->screen_id ) > 6 ) return;
390
 
391
  if ( ! empty( $this->labels ) && ! empty( $this->screen_id ) ) {
392
 
393
  // Menu Slug
394
+ $menu_slug = MYCRED_SLUG;
395
  if ( ! $this->is_main_type )
396
+ $menu_slug = MYCRED_SLUG . '_' . $this->mycred_type;
397
 
398
  // Menu Label
399
  if ( ! isset( $this->labels['page_title'] ) && ! isset( $this->labels['menu'] ) )
438
 
439
  }
440
 
441
+ /**
442
+ * Register Scripts & Styles
443
+ * @since 1.7
444
+ * @version 1.0
445
+ */
446
+ function scripts_and_styles() { }
447
+
448
  /**
449
  * Save Log Entries per page
450
  * @since 0.1
489
 
490
  }
491
 
492
+ /**
493
+ * Add Metabox Class
494
+ * @since 1.7
495
+ * @version 1.0
496
+ */
497
+ public function metabox_classes( $classes ) {
498
+ $classes[] = 'mycred-metabox';
499
+ return $classes;
500
+ }
501
+
502
  /**
503
  * Enqueue Scripts & Styles
504
  * Scripts and styles to enqueu on module admin pages.
512
  // Load Accordion
513
  if ( $this->accordion ) {
514
 
 
515
  wp_enqueue_style( 'mycred-admin' );
516
 
517
  $open = '-1';
518
  if ( isset( $_GET['open-tab'] ) && is_numeric( $_GET['open-tab'] ) )
519
  $open = absint( $_GET['open-tab'] );
520
 
521
+ wp_localize_script( 'mycred-accordion', 'myCRED', apply_filters( 'mycred_localize_admin', array( 'active' => $open ) ) );
522
+ wp_enqueue_script( 'mycred-accordion' );
523
 
524
  ?>
525
  <!-- myCRED Accordion Styling -->
662
  }
663
 
664
  if ( $this->add_to_core === true )
665
+ $id = 'prefs' . $id;
666
+
667
+ $id = strtolower( $this->module_name ) . $id;
668
+ $id = strtolower( $id );
669
+ $id = str_replace( array( '[', ']' ), '', $id );
670
+ $id = str_replace( array( '_', '-' ), '', $id );
671
 
672
+ return $id;
673
 
674
  }
675
 
691
  */
692
  function get_settings_url( $module = '' ) {
693
 
694
+ $variables = array( 'page' => MYCRED_SLUG . '-settings' );
695
  if ( ! empty( $module ) )
696
  $variables['open-tab'] = $module;
697
 
abstracts/mycred-abstract-object.php ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * myCRED_Object class
6
+ * @see http://codex.mycred.me/classes/mycred_object/
7
+ * @since 1.7
8
+ * @version 1.0
9
+ */
10
+ if ( ! class_exists( 'myCRED_Object' ) ) :
11
+ abstract class myCRED_Object {
12
+
13
+ /**
14
+ * Construct
15
+ */
16
+ function __construct() {
17
+
18
+ if ( ! did_action( 'init' ) )
19
+ wp_die( 'myCRED_Account class used too early. This class should be called after the init action fires.' );
20
+
21
+ }
22
+
23
+ public function get_mycred( $object = NULL, $type_id = MYCRED_DEFAULT_TYPE_KEY ) {
24
+
25
+ if ( ! is_object( $object ) ) {
26
+
27
+ if ( ! is_string( $type_id ) || ! mycred_point_type_exists( $type_id ) )
28
+ $type_id = MYCRED_DEFAULT_TYPE_KEY;
29
+
30
+ $object = mycred( $type_id );
31
+
32
+ }
33
+
34
+ return $object;
35
+
36
+ }
37
+
38
+ }
39
+ endif;
40
+
41
+ ?>
addons/badges/assets/js/edit-badge.js ADDED
@@ -0,0 +1,455 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(function($){
2
+
3
+ var myCREDBadgeLevelsWrap = $( '#mycred-badge-setup .inside' );
4
+
5
+ var AddNewLevelButton = $( '#badges-add-new-level' );
6
+ var AddNewRequirementButton = $( '#badges-add-new-requirement' );
7
+ var ChangeDefaultImageButton = $( '#badges-change-default-image' );
8
+
9
+ var TotalBadgeLevels = 1;
10
+ var TotalRequirements = 1;
11
+ var UsedtoCompare = 'AND';
12
+
13
+ var PointTypeSelect = $( 'select#badge-select-point-type' );
14
+ var ReferenceSelect = $( 'select#badge-select-reference' );
15
+ var RequirementTypeSelect = $( 'select#badge-select-requirement-type' );
16
+ var RequirementCompare = $( '#badge-requirement-compare a' );
17
+
18
+ var LevelImageSelector;
19
+ var DefaultImageSelector;
20
+
21
+ /**
22
+ * Get Badge Requirements Template
23
+ * Creates a template of the top-level badge's set requirements.
24
+ * @since 1.7
25
+ * @version 1.0
26
+ */
27
+ var mycred_get_badge_requirement_template = function( badgelevel ) {
28
+
29
+ var template = '';
30
+
31
+ // Loop through each requirement set for the top-level badge
32
+ $( '#mycred-badge-level0 .level-requirements .row-narrow' ).each(function(index){
33
+
34
+ // Row
35
+ var required_row_id = $(this).data( 'row' );
36
+
37
+ // Point Type
38
+ var required_point_type = $(this).find( 'select.point-type option:selected' );
39
+ if ( required_point_type === undefined || required_point_type == '' )
40
+ required_point_type = '-';
41
+ else
42
+ required_point_type = required_point_type.text();
43
+
44
+ // Reference
45
+ var required_reference = $(this).find( 'select.reference option:selected' );
46
+ if ( required_reference === undefined || required_reference == '' )
47
+ required_reference = '-';
48
+ else
49
+ required_reference = required_reference.text();
50
+
51
+ // Amount
52
+ var required_amount = $(this).find( 'input.form-control' ).val();
53
+
54
+ // Requirement type
55
+ var required_type = $(this).find( 'select.req-type option:selected' );
56
+ if ( required_type === undefined || required_type == '' )
57
+ required_type = '-';
58
+ else
59
+ required_type = required_type.text();
60
+
61
+ // Comparison
62
+ var compare_to_show = myCREDBadge.compareOR;
63
+ if ( UsedtoCompare == 'AND' )
64
+ compare_to_show = myCREDBadge.compareAND;
65
+
66
+ var totalrequirements = $( '#mycred-badge-level0 .level-requirements .row-narrow' ).length;
67
+ if ( totalrequirements > 1 && index+1 == totalrequirements || ( totalrequirements == 1 && index == 0 ) )
68
+ compare_to_show = '';
69
+
70
+ // Render fresh requirement template
71
+ template += Mustache.render( BadgeRequirement, {
72
+ level : badgelevel,
73
+ reqlevel : required_row_id,
74
+ selectedtype : required_point_type,
75
+ selectedref : required_reference,
76
+ reqamount : required_amount,
77
+ selectedby : required_type,
78
+ comparelabel : compare_to_show
79
+ });
80
+
81
+ });
82
+
83
+ return template;
84
+
85
+ };
86
+
87
+ $(document).ready(function(){
88
+
89
+ TotalBadgeLevels = $( '#mycred-badge-setup #badge-levels .badge-level' ).length;
90
+ TotalRequirements = $( '#mycred-badge-setup .level-requirements .row-narrow' ).length;
91
+
92
+ console.log( 'Total levels detected: ' + TotalBadgeLevels );
93
+
94
+ // Change Requirement Compare Action
95
+ RequirementCompare.click(function(e){
96
+
97
+ e.preventDefault();
98
+
99
+ var refselectcompare = $(this);
100
+ refselectcompare.blur();
101
+
102
+ if ( refselectcompare.hasClass( 'selected' ) ) return false;
103
+
104
+ var currentlyselected = $( '#badge-requirement-compare a.selected' );
105
+ if ( currentlyselected !== undefined )
106
+ currentlyselected.removeClass( 'selected' );
107
+
108
+ UsedtoCompare = refselectcompare.data( 'do' );
109
+ $( '#badge-requirement-compare input' ).val( UsedtoCompare );
110
+
111
+ refselectcompare.addClass( 'selected' );
112
+
113
+ // Make sure there is more then one level
114
+ var numberoflevels = $( '#mycred-badge-setup #badge-levels .badge-level' ).length;
115
+ if ( numberoflevels == 1 ) return;
116
+
117
+ var newrequirementlevel = $( '#mycred-badge-level0 .level-requirements .row-narrow' ).length;
118
+
119
+ var compare_to_show = myCREDBadge.compareOR;
120
+ if ( UsedtoCompare == 'AND' )
121
+ compare_to_show = myCREDBadge.compareAND;
122
+
123
+ // Loop through all place holders for each level and change the text
124
+ $( '#mycred-badge-setup #badge-levels .level-requirements .level-compare p' ).each(function(index){
125
+
126
+ $(this).fadeOut(function(){
127
+
128
+ var reqrowid = $(this).data( 'row' );
129
+
130
+ if ( newrequirementlevel > 1 && reqrowid+1 == newrequirementlevel )
131
+ $(this).empty().fadeIn();
132
+
133
+ else
134
+ $(this).empty().text( compare_to_show ).fadeIn();
135
+
136
+ });
137
+
138
+ });
139
+
140
+ });
141
+
142
+ // Add New Requirement Action
143
+ AddNewRequirementButton.click(function(e){
144
+
145
+ e.preventDefault();
146
+
147
+ console.log( 'Add new requirement clicked' );
148
+ // Prep
149
+ var totallevels = $( '#mycred-badge-setup #badge-levels .badge-level' );
150
+ var totalrequirements = $( '#mycred-badge-level0 .level-requirements .row-narrow' );
151
+ var newrequirementlevel = totalrequirements.length;
152
+
153
+ // Render a fresh requirement row
154
+ var template = Mustache.render( BadgeNewRequrement, {
155
+ reqlevel : newrequirementlevel,
156
+ level : 0,
157
+ reqamount : ''
158
+ });
159
+
160
+ // Insert fresh row to the top level
161
+ $( '#mycred-badge-setup #mycred-badge-level0 .level-requirements' ).append( template );
162
+
163
+ if ( totallevels.length > 1 ) {
164
+
165
+ // Loop through each level and insert a blank requirement row for each one
166
+ totallevels.each(function( index ){
167
+
168
+ var currentlevel = $(this).data( 'level' );
169
+
170
+ // Skip firt row as we just added one in there
171
+ if ( currentlevel > 0 ) {
172
+
173
+ var compare_to_show = myCREDBadge.compareOR;
174
+ if ( UsedtoCompare == 'AND' )
175
+ compare_to_show = myCREDBadge.compareAND;
176
+
177
+ // Render a fresh requirement row
178
+ var reqtemplate = Mustache.render( BadgeRequirement, {
179
+ level : currentlevel,
180
+ reqlevel : newrequirementlevel,
181
+ reqamount : '',
182
+ selectedtype : '-',
183
+ selectedref : '-',
184
+ selectedby : '-',
185
+ comparelabel : ''
186
+ });
187
+
188
+ // Insert fresh row to the requirement list
189
+ $( '#mycred-badge-setup #mycred-badge-level' + currentlevel + ' .level-requirements' ).append( reqtemplate );
190
+
191
+ if ( newrequirementlevel > 0 ) {
192
+
193
+ for ( var i = newrequirementlevel-1; i > -1; i-- ) {
194
+
195
+ $( '#mycred-badge-setup #mycred-badge-level' + currentlevel + ' #level' + currentlevel + 'requirement' + i + ' .level-compare p' ).fadeOut(function(){
196
+ $(this).empty().text( compare_to_show ).fadeIn();
197
+ });
198
+
199
+ }
200
+
201
+ }
202
+
203
+ }
204
+
205
+ // Next!
206
+
207
+ });
208
+
209
+ }
210
+
211
+ });
212
+
213
+ // Remove Requirement Action
214
+ $( '#mycred-badge-setup' ).on( 'click', 'button.remove-requirement', function(e){
215
+
216
+ var rowtoremove = $(this).data( 'req' );
217
+
218
+ $( '#level0requirement' + rowtoremove ).slideUp(function(){
219
+ $(this).remove();
220
+ });
221
+
222
+ $( '#mycred-badge-setup #badge-levels .badge-level' ).each(function( index ){
223
+
224
+ var currentlevel = $(this).data( 'level' );
225
+
226
+ if ( currentlevel > 0 ) {
227
+
228
+ $( '#mycred-badge-setup #level' + currentlevel + 'requirement' + rowtoremove ).remove();
229
+
230
+ }
231
+
232
+ });
233
+
234
+ });
235
+
236
+ // Change Point Type Requirement Action
237
+ $( '#mycred-badge-setup' ).on( 'change', 'select.point-type', function(e){
238
+
239
+ var typeselectelement = $(this);
240
+
241
+ // Make sure something was selected
242
+ var selectedtype = typeselectelement.find( ':selected' );
243
+ if ( selectedtype === undefined ) return;
244
+
245
+ // Make sure there is more then one level
246
+ var numberoflevels = $( '#mycred-badge-setup #badge-levels .badge-level' ).length;
247
+ if ( numberoflevels == 1 ) return;
248
+
249
+ var requirementrow = typeselectelement.data( 'row' );
250
+
251
+ // Loop through each level
252
+ $( '#mycred-badge-setup #badge-levels .badge-level' ).each(function(index){
253
+
254
+ var badgelevel = $(this).data( 'level' );
255
+ if ( badgelevel == 0 ) { return true; }
256
+
257
+ var subrequirementrow = $(this).find( '#level' + badgelevel + 'requirement' + requirementrow + ' .level-type p' );
258
+ if ( subrequirementrow !== undefined ) {
259
+
260
+ subrequirementrow.fadeOut(function(){
261
+ subrequirementrow.empty().text( selectedtype.text() ).fadeIn();
262
+ });
263
+
264
+ }
265
+
266
+ });
267
+
268
+ });
269
+
270
+ // Change Reference Requrirement Action
271
+ $( '#mycred-badge-setup' ).on( 'change', 'select.reference', function(e){
272
+
273
+ var refselectelement = $(this);
274
+
275
+ // Make sure something was selected
276
+ var selectedref = refselectelement.find( ':selected' );
277
+ if ( selectedref === undefined ) return;
278
+
279
+ // Make sure there is more then one level
280
+ var numberoflevels = $( '#mycred-badge-setup #badge-levels .badge-level' ).length;
281
+ if ( numberoflevels == 1 ) return;
282
+
283
+ var requirementrow = refselectelement.data( 'row' );
284
+
285
+ // Loop through each level
286
+ $( '#mycred-badge-setup #badge-levels .badge-level' ).each(function(index){
287
+
288
+ var badgelevel = $(this).data( 'level' );
289
+ if ( badgelevel == 0 ) { return true; }
290
+
291
+ var subrequirementrow = $(this).find( '#level' + badgelevel + 'requirement' + requirementrow + ' .level-ref p' );
292
+ if ( subrequirementrow !== undefined ) {
293
+
294
+ subrequirementrow.fadeOut(function(){
295
+ subrequirementrow.empty().text( selectedref.text() ).fadeIn();
296
+ });
297
+
298
+ }
299
+
300
+ });
301
+
302
+ });
303
+
304
+ // Change Requirement Type Action
305
+ $( '#mycred-badge-setup' ).on( 'change', 'select.req-type', function(e){
306
+
307
+ var reftypeselectelement = $(this);
308
+
309
+ // Make sure something was selected
310
+ var selectedreftype = reftypeselectelement.find( ':selected' );
311
+ if ( selectedreftype === undefined ) return;
312
+
313
+ // Make sure there is more then one level
314
+ var numberoflevels = $( '#mycred-badge-setup #badge-levels .badge-level' ).length;
315
+ if ( numberoflevels == 1 ) return;
316
+
317
+ var requirementrow = reftypeselectelement.data( 'row' );
318
+
319
+ // Loop through each level
320
+ $( '#mycred-badge-setup #badge-levels .badge-level' ).each(function(index){
321
+
322
+ var badgelevel = $(this).data( 'level' );
323
+ if ( badgelevel == 0 ) { return true; }
324
+
325
+ var subrequirementrow = $(this).find( '#level' + badgelevel + 'requirement' + requirementrow + ' .level-type-by p' );
326
+ if ( subrequirementrow !== undefined ) {
327
+
328
+ subrequirementrow.fadeOut(function(){
329
+ subrequirementrow.empty().text( selectedreftype.text() ).fadeIn();
330
+ });
331
+
332
+ }
333
+
334
+ });
335
+
336
+ });
337
+
338
+ // Add New Level Action
339
+ AddNewLevelButton.click(function(e){
340
+
341
+ e.preventDefault();
342
+
343
+ console.log( 'Add new level button' );
344
+ // Prep
345
+ TotalBadgeLevels = $( '#mycred-badge-setup #badge-levels .badge-level' ).length;
346
+ TotalRequirements = $( '#mycred-badge-setup .level-requirements .row-narrow' ).length;
347
+
348
+ // Get the top-level requirement list as a template
349
+ var reqtemplate = mycred_get_badge_requirement_template( TotalBadgeLevels );
350
+
351
+ // Render a fresh level
352
+ var template = Mustache.render( BadgeLevel, {
353
+ level : TotalBadgeLevels,
354
+ requirements : reqtemplate
355
+ });
356
+
357
+ // Insert fresh row
358
+ $( '#mycred-badge-setup #badge-levels' ).append( template );
359
+
360
+ });
361
+
362
+ // Set / Change Level Image Action
363
+ $( '#mycred-badge-setup' ).on( 'click', 'button.change-level-image', function(e){
364
+
365
+ console.log( 'Change level image button' );
366
+
367
+ var button = $(this);
368
+ var currentlevel = button.data( 'level' );
369
+
370
+ LevelImageSelector = wp.media.frames.file_frame = wp.media({
371
+ title : myCREDBadge.uploadtitle,
372
+ button : {
373
+ text : myCREDBadge.uploadbutton
374
+ },
375
+ multiple : false
376
+ });
377
+
378
+ // When a file is selected, grab the URL and set it as the text field's value
379
+ LevelImageSelector.on( 'select', function(){
380
+
381
+ attachment = LevelImageSelector.state().get('selection').first().toJSON();
382
+ if ( attachment.url != '' ) {
383
+
384
+ $( '#mycred-badge-level' + currentlevel + ' .level-image-wrapper' ).fadeOut(function(){
385
+
386
+ $( '#mycred-badge-level' + currentlevel + ' .level-image-wrapper' ).empty().removeClass( 'empty dashicons' ).html( '<img src="' + attachment.url + '" alt="Badge level image" \/><input type="hidden" name="mycred_badge[levels][' + currentlevel + '][attachment_id]" value="' + attachment.id + '" \/><input type="hidden" name="mycred_badge[levels][' + currentlevel + '][image]" value="" \/>' ).fadeIn();
387
+ button.text( myCREDBadge.changeimage );
388
+
389
+ });
390
+
391
+ }
392
+
393
+ });
394
+
395
+ // Open the uploader dialog
396
+ LevelImageSelector.open();
397
+
398
+ });
399
+
400
+ // Remove Level Action
401
+ $( '#mycred-badge-setup' ).on( 'click', 'button.remove-badge-level', function(e){
402
+
403
+ var leveltoremove = $(this).data( 'level' );
404
+ if ( $( '#mycred-badge-level' + leveltoremove ) === undefined ) return false;
405
+
406
+ console.log( 'Remove level button' );
407
+
408
+ if ( ! confirm( myCREDBadge.remove ) ) return false;
409
+
410
+ $( '#mycred-badge-level' + leveltoremove ).slideUp().remove();
411
+ TotalBadgeLevels--;
412
+
413
+ });
414
+
415
+ // Change Default Image Action
416
+ ChangeDefaultImageButton.click(function(e){
417
+
418
+ e.preventDefault();
419
+ console.log( 'Change default image button' );
420
+
421
+ var button = $(this);
422
+
423
+ DefaultImageSelector = wp.media.frames.file_frame = wp.media({
424
+ title : myCREDBadge.uploadtitle,
425
+ button : {
426
+ text : myCREDBadge.uploadbutton
427
+ },
428
+ multiple : false
429
+ });
430
+
431
+ // When a file is selected, grab the URL and set it as the text field's value
432
+ DefaultImageSelector.on( 'select', function(){
433
+
434
+ attachment = DefaultImageSelector.state().get('selection').first().toJSON();
435
+ if ( attachment.url != '' ) {
436
+
437
+ $( '#mycred-badge-default .default-image-wrapper' ).fadeOut(function(){
438
+
439
+ $( '#mycred-badge-default .default-image-wrapper' ).empty().removeClass( 'empty dashicons' ).html( '<img src="' + attachment.url + '" alt="Badge default image" \/><input type="hidden" name="mycred_badge[main_image]" value="' + attachment.id + '" \/><input type="hidden" name="mycred_badge[main_image_url]" value="" \/>' ).fadeIn();
440
+ button.text( myCREDBadge.changeimage );
441
+
442
+ });
443
+
444
+ }
445
+
446
+ });
447
+
448
+ // Open the uploader dialog
449
+ DefaultImageSelector.open();
450
+
451
+ });
452
+
453
+ });
454
+
455
+ });
addons/{transfer/assets/css → badges/assets/js}/index.php RENAMED
File without changes
addons/badges/includes/mycred-badge-functions.php CHANGED
@@ -1,27 +1,197 @@
1
  <?php
2
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  /**
5
  * Get Badge Requirements
6
  * Returns the badge requirements as an array.
7
  * @since 1.5
8
- * @version 1.0.1
9
  */
10
  if ( ! function_exists( 'mycred_get_badge_requirements' ) ) :
11
- function mycred_get_badge_requirements( $post_id = NULL, $editor = false ) {
12
-
13
- $req = (array) get_post_meta( $post_id, 'badge_requirements', true );
14
- if ( $editor && empty( $req ) )
15
- $req = array(
16
- 0 => array(
17
- 'type' => 'mycred_default',
18
- 'reference' => '',
19
- 'amount' => '',
20
- 'by' => ''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  )
22
  );
23
 
24
- return apply_filters( 'mycred_badge_requirements', $req, $editor );
25
 
26
  }
27
  endif;
@@ -30,77 +200,136 @@ endif;
30
  * Display Badge Requirements
31
  * Returns the badge requirements as a string in a readable format.
32
  * @since 1.5
33
- * @version 1.1.1
34
  */
35
  if ( ! function_exists( 'mycred_display_badge_requirement' ) ) :
36
- function mycred_display_badge_requirements( $post_id = NULL, $sep = '<br />' ) {
37
 
38
- $requirements = mycred_get_badge_requirements( $post_id );
39
- if ( empty( $requirements ) ) {
40
 
41
  $reply = '-';
42
 
43
  }
44
  else {
45
 
46
- $types = mycred_get_types();
47
- $references = mycred_get_all_references();
48
- $req_count = count( $requirements );
49
 
50
- $output = array();
51
- foreach ( $requirements as $level => $needs ) {
 
52
 
53
- if ( $needs['type'] == '' )
54
- $needs['type'] = 'mycred_default';
55
 
56
- if ( ! isset( $types[ $needs['type'] ] ) )
57
  continue;
58
 
59
- $mycred = mycred( $needs['type'] );
60
- $point_type = $mycred->plural();
61
-
62
- if ( ! isset( $references[ $needs['reference'] ] ) )
63
- $ref = '-';
64
  else
65
- $ref = $references[ $needs['reference'] ];
66
 
67
- $level_label = '';
68
- if ( $req_count > 1 )
69
- $level_label = '<strong>' . sprintf( __( 'Level %s', 'mycred' ), ( $level + 1 ) ) . '</strong>';
 
 
 
70
 
71
- if ( $needs['by'] == 'count' )
72
- $output[] = sprintf( _x( '%s for %s %s - %s', '"Points" for "reference" "x time(s)" - Level', 'mycred' ), $point_type, $ref, sprintf( _n( '1 time', '%d times', $needs['amount'], 'mycred' ), $needs['amount'] ), $level_label );
73
- else
74
- $output[] = sprintf( _x( '%s for %s in total', '"x points" for "reference" in total', 'mycred' ), $mycred->format_creds( $needs['amount'] ), $ref );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  }
77
- $reply = implode( $sep, $output );
 
 
 
 
78
 
79
  }
80
 
81
- return apply_filters( 'mycred_badge_display_requirements', $reply, $post_id, $sep );
82
 
83
  }
84
  endif;
85
 
86
  /**
87
  * Count Users with Badge
88
- * Counts the number of users that has the given badge.
 
89
  * @since 1.5
90
- * @version 1.0
91
  */
92
  if ( ! function_exists( 'mycred_count_users_with_badge' ) ) :
93
- function mycred_count_users_with_badge( $post_id = NULL ) {
94
 
95
- global $wpdb;
96
 
97
- $key = 'mycred_badge' . $post_id;
98
- $count = $wpdb->get_var( $wpdb->prepare( "
99
- SELECT COUNT( DISTINCT user_id )
100
- FROM {$wpdb->usermeta}
101
- WHERE meta_key = %s", $key ) );
102
 
103
- return apply_filters( 'mycred_count_users_with_badge', $count, $post_id );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
  }
106
  endif;
@@ -109,20 +338,17 @@ endif;
109
  * Count Users without Badge
110
  * Counts the number of users that does not have a given badge.
111
  * @since 1.5
112
- * @version 1.0
113
  */
114
  if ( ! function_exists( 'mycred_count_users_without_badge' ) ) :
115
- function mycred_count_users_without_badge( $post_id = NULL ) {
116
 
117
- global $wpdb;
 
118
 
119
- $key = 'mycred_badge' . $post_id;
120
- $count = $wpdb->get_var( $wpdb->prepare( "
121
- SELECT COUNT( DISTINCT user_id )
122
- FROM {$wpdb->usermeta}
123
- WHERE meta_key != %s", $key ) );
124
 
125
- return apply_filters( 'mycred_count_users_without_badge', $count, $post_id );
126
 
127
  }
128
  endif;
@@ -131,25 +357,96 @@ endif;
131
  * Reference Has Badge
132
  * Checks if a given reference has a badge associated with it.
133
  * @since 1.5
134
- * @version 1.2
135
  */
136
  if ( ! function_exists( 'mycred_ref_has_badge' ) ) :
137
- function mycred_ref_has_badge( $reference = NULL, $request = NULL ) {
138
-
139
- if ( $reference === NULL ) return false;
140
 
141
- global $wpdb;
 
142
 
143
- $badge_ids = $wpdb->get_col( $wpdb->prepare( "
144
- SELECT post_id
145
- FROM {$wpdb->postmeta}
146
- WHERE meta_key = %s
147
- AND meta_value LIKE %s;", 'badge_requirements', '%"' . $reference . '"%' ) );
148
 
149
  if ( empty( $badge_ids ) )
150
  $badge_ids = false;
151
 
152
- return apply_filters( 'mycred_ref_has_badge', $badge_ids, $reference, $request );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
  }
155
  endif;
@@ -158,75 +455,189 @@ endif;
158
  * Check if User Gets Badge
159
  * Checks if a given user has earned one or multiple badges.
160
  * @since 1.5
161
- * @version 1.2.2
162
  */
163
  if ( ! function_exists( 'mycred_check_if_user_gets_badge' ) ) :
164
- function mycred_check_if_user_gets_badge( $user_id = NULL, $badge_ids = array(), $save = true ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
- if ( $user_id === NULL || empty( $badge_ids ) ) return;
 
 
 
 
 
 
 
 
 
 
 
167
 
168
  global $wpdb;
169
 
170
- $ids = array();
171
- foreach ( $badge_ids as $badge_id ) {
 
 
 
 
172
 
173
- $level = false;
174
- $requirements = mycred_get_badge_requirements( $badge_id );
175
- foreach ( $requirements as $req_level => $needs ) {
176
 
177
- if ( $needs['type'] == '' )
178
- $needs['type'] = 'mycred_default';
179
 
180
- $mycred = mycred( $needs['type'] );
 
 
181
 
182
- // Count occurences
183
- if ( $needs['by'] == 'count' ) {
184
- $select = 'COUNT( * )';
185
- $amount = absint( $needs['amount'] );
 
 
 
 
 
 
 
 
 
 
 
 
186
  }
187
 
188
- // Sum up points
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  else {
190
- $select = 'SUM( creds )';
191
- $amount = $mycred->number( $needs['amount'] );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  }
193
 
194
- $result = $wpdb->get_var( apply_filters( 'mycred_if_user_gets_badge_sql', $wpdb->prepare( "
195
- SELECT {$select}
196
- FROM {$mycred->log_table}
197
- WHERE user_id = %d
198
- AND ctype = %s
199
- AND ref = %s;", $user_id, $needs['type'], $needs['reference'] ), $user_id, $badge_id, $req_level, $needs ) );
200
 
201
- if ( $result === NULL ) $result = 0;
 
 
 
202
 
203
- if ( $needs['by'] != 'count' )
204
- $result = $mycred->number( $result );
205
- else
206
- $result = absint( $result );
207
 
208
- $level = NULL;
209
- if ( $result >= $amount )
210
- $level = absint( $req_level );
211
 
212
- $current = mycred_get_user_meta( $user_id, 'mycred_badge' . $badge_id, '', true );
213
- if ( $current == '' ) $current = -1;
214
 
215
- // If a level has been reached assign it now unless the user has this level already
216
- if ( $level !== NULL && $current < $level ) {
217
 
218
- if ( $save )
219
- mycred_update_user_meta( $user_id, 'mycred_badge' . $badge_id, '', apply_filters( 'mycred_badge_user_value', $level, $user_id, $badge_id ) );
 
 
 
220
 
221
- $ids[ $badge_id ] = $level;
 
 
 
 
 
 
 
 
 
 
222
 
223
  }
224
 
225
  }
226
-
227
  }
228
 
229
- return $ids;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
  }
232
  endif;
@@ -235,7 +646,7 @@ endif;
235
  * Get Users Badges
236
  * Returns the badge post IDs that a given user currently holds.
237
  * @since 1.5
238
- * @version 1.1
239
  */
240
  if ( ! function_exists( 'mycred_get_users_badges' ) ) :
241
  function mycred_get_users_badges( $user_id = NULL ) {
@@ -260,11 +671,7 @@ if ( ! function_exists( 'mycred_get_users_badges' ) ) :
260
  $badge_id = (int) $badge_id;
261
  if ( array_key_exists( $badge_id, $badge_ids ) ) continue;
262
 
263
- $requirements = mycred_get_badge_requirements( $badge_id );
264
- if ( count( $requirements ) > 1 )
265
- $badge_ids[ $badge_id ] = $badge->meta_value;
266
- else
267
- $badge_ids[ $badge_id ] = 0;
268
 
269
  }
270
  }
@@ -278,16 +685,17 @@ endif;
278
  * Display Users Badges
279
  * Will echo all badge images a given user has earned.
280
  * @since 1.5
281
- * @version 1.2
282
  */
283
  if ( ! function_exists( 'mycred_display_users_badges' ) ) :
284
  function mycred_display_users_badges( $user_id = NULL ) {
285
 
286
- if ( $user_id === NULL || $user_id == 0 ) return;
 
287
 
288
  $users_badges = mycred_get_users_badges( $user_id );
289
 
290
- echo '<div id="mycred-users-badges">';
291
 
292
  do_action( 'mycred_before_users_badges', $user_id, $users_badges );
293
 
@@ -295,12 +703,9 @@ if ( ! function_exists( 'mycred_display_users_badges' ) ) :
295
 
296
  foreach ( $users_badges as $badge_id => $level ) {
297
 
298
- $level_image = get_post_meta( $badge_id, 'level_image' . $level, true );
299
- if ( $level_image == '' )
300
- $level_image = get_post_meta( $badge_id, 'main_image', true );
301
-
302
- $badge = '<img src="' . $level_image . '" class="mycred-badge earned badge-id-' . $badge_id . ' level-' . $level . '" alt="' . get_the_title( $badge_id ) . '" title="' . get_the_title( $badge_id ) . '" />';
303
- echo apply_filters( 'mycred_the_badge', $badge, $badge_id, $level, $user_id );
304
 
305
  }
306
 
@@ -308,7 +713,7 @@ if ( ! function_exists( 'mycred_display_users_badges' ) ) :
308
 
309
  do_action( 'mycred_after_users_badges', $user_id, $users_badges );
310
 
311
- echo '</div>';
312
 
313
  }
314
  endif;
@@ -328,43 +733,12 @@ if ( ! function_exists( 'mycred_get_badge_ids' ) ) :
328
  SELECT ID
329
  FROM {$wpdb->posts}
330
  WHERE post_type = 'mycred_badge'
331
- AND post_status = 'publish';" );
 
332
 
333
  return apply_filters( 'mycred_get_badge_ids', $badge_ids );
334
 
335
  }
336
  endif;
337
 
338
- /**
339
- * Get Badges
340
- * Returns all badges with it's requirements and badge images.
341
- * @since 1.5
342
- * @version 1.0
343
- */
344
- if ( ! function_exists( 'mycred_get_badges' ) ) :
345
- function mycred_get_badges() {
346
-
347
- global $wpdb;
348
-
349
- $badges = $wpdb->get_results( "
350
- SELECT posts.ID, posts.post_title, req.meta_value AS requires, def.meta_value AS default_img, main.meta_value AS main_img
351
- FROM {$wpdb->posts} posts
352
- INNER JOIN {$wpdb->postmeta} req
353
- ON ( posts.ID = req.post_id )
354
- INNER JOIN {$wpdb->postmeta} def
355
- ON ( posts.ID = def.post_id )
356
- INNER JOIN {$wpdb->postmeta} main
357
- ON ( posts.ID = main.post_id )
358
- WHERE posts.post_type = 'mycred_badge'
359
- AND posts.post_status = 'publish'
360
- AND req.meta_key = 'badge_requirements'
361
- AND def.meta_key = 'default_image'
362
- AND main.meta_key = 'main_image'
363
- ORDER BY posts.post_date DESC;" );
364
-
365
- return apply_filters( 'mycred_get_badges', $badges );
366
-
367
- }
368
- endif;
369
-
370
  ?>
1
  <?php
2
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
 
4
+ /**
5
+ * Get Badge
6
+ * @since 1.7
7
+ * @version 1.0
8
+ */
9
+ if ( ! function_exists( 'mycred_get_badge' ) ) :
10
+ function mycred_get_badge( $badge_id = NULL, $level = NULL ) {
11
+
12
+ if ( absint( $badge_id ) === 0 || get_post_type( $badge_id ) != 'mycred_badge' ) return false;
13
+
14
+ $badge = new myCRED_Badge( $badge_id, $level );
15
+
16
+ return apply_filters( 'mycred_get_badge', $badge, $badge_id, $level );
17
+
18
+ }
19
+ endif;
20
+
21
+ /**
22
+ * Get Badge References
23
+ * Returns an array of references used by badges for quicker checks.
24
+ * @since 1.7
25
+ * @version 1.0
26
+ */
27
+ if ( ! function_exists( 'mycred_get_badge_references' ) ) :
28
+ function mycred_get_badge_references( $point_type = MYCRED_DEFAULT_TYPE_KEY, $force = false ) {
29
+
30
+ $references = mycred_get_option( 'mycred-badge-refs-' . $point_type );
31
+ if ( ! is_array( $references ) || empty( $references ) || $force ) {
32
+
33
+ global $wpdb;
34
+
35
+ $new_list = array();
36
+
37
+ // Old versions
38
+ $references = $wpdb->get_results( "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'badge_requirements';" );
39
+ if ( ! empty( $references ) ) {
40
+ foreach ( $references as $entry ) {
41
+
42
+ $requirement = maybe_unserialize( $entry->meta_value );
43
+ if ( ! is_array( $requirement ) || empty( $requirement ) ) continue;
44
+
45
+ if ( ! array_key_exists( 'type', $requirement ) || $requirement['type'] != $point_type || $requirement['reference'] == '' ) continue;
46
+
47
+ if ( ! array_key_exists( $requirement['reference'], $new_list ) )
48
+ $new_list[ $requirement['reference'] ] = array();
49
+
50
+ if ( ! in_array( $entry->post_id, $new_list[ $requirement['reference'] ] ) )
51
+ $new_list[ $requirement['reference'] ][] = $entry->post_id;
52
+
53
+ }
54
+ }
55
+
56
+ // New version (post 1.7)
57
+ $references = $wpdb->get_results( "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'badge_prefs';" );
58
+ if ( ! empty( $references ) ) {
59
+ foreach ( $references as $entry ) {
60
+
61
+ // Manual badges should be ignored
62
+ if ( absint( get_post_meta( $entry->post_id, 'manual_badge', true ) ) === 1 ) continue;
63
+
64
+ $levels = maybe_unserialize( $entry->meta_value );
65
+ if ( ! is_array( $levels ) || empty( $levels ) ) continue;
66
+
67
+ foreach ( $levels as $level => $setup ) {
68
+
69
+ if ( $level > 0 ) continue;
70
+
71
+ foreach ( $setup['requires'] as $requirement_row => $requirement ) {
72
+
73
+ if ( $requirement['type'] != $point_type || $requirement['reference'] == '' ) continue;
74
+
75
+ if ( ! array_key_exists( $requirement['reference'], $new_list ) )
76
+ $new_list[ $requirement['reference'] ] = array();
77
+
78
+ if ( ! in_array( $entry->post_id, $new_list[ $requirement['reference'] ] ) )
79
+ $new_list[ $requirement['reference'] ][] = $entry->post_id;
80
+
81
+ }
82
+
83
+ }
84
+
85
+ }
86
+ }
87
+
88
+ if ( ! empty( $new_list ) )
89
+ mycred_update_option( 'mycred-badge-references-' . $point_type, $new_list );
90
+
91
+ $references = $new_list;
92
+
93
+ }
94
+
95
+ return apply_filters( 'mycred_get_badge_references', $references, $point_type );
96
+
97
+ }
98
+ endif;
99
+
100
  /**
101
  * Get Badge Requirements
102
  * Returns the badge requirements as an array.
103
  * @since 1.5
104
+ * @version 1.1
105
  */
106
  if ( ! function_exists( 'mycred_get_badge_requirements' ) ) :
107
+ function mycred_get_badge_requirements( $badge_id = NULL ) {
108
+
109
+ return mycred_get_badge_levels( $badge_id );
110
+
111
+ }
112
+ endif;
113
+
114
+ /**
115
+ * Get Badge Levels
116
+ * Returns an array of levels associated with a given badge.
117
+ * @since 1.7
118
+ * @version 1.0
119
+ */
120
+ if ( ! function_exists( 'mycred_get_badge_levels' ) ) :
121
+ function mycred_get_badge_levels( $badge_id ) {
122
+
123
+ $setup = get_post_meta( $badge_id, 'badge_prefs', true );
124
+ if ( ! is_array( $setup ) || empty( $setup ) ) {
125
+
126
+ // Backwards comp.
127
+ $old_setup = get_post_meta( $badge_id, 'badge_requirements', true );
128
+
129
+ // Convert old setup to new
130
+ if ( is_array( $old_setup ) && ! empty( $old_setup ) ) {
131
+
132
+ $new_setup = array();
133
+ foreach ( $old_setup as $level => $requirements ) {
134
+
135
+ $level_image = get_post_meta( $badge_id, 'level_image' . $level, true );
136
+ if ( $level_image == '' || $level == 0 )
137
+ $level_image = get_post_meta( $badge_id, 'main_image', true );
138
+
139
+ $row = array(
140
+ 'image_url' => $level_image,
141
+ 'attachment_id' => 0,
142
+ 'label' => '',
143
+ 'compare' => 'AND',
144
+ 'requires' => array(),
145
+ 'reward' => array(
146
+ 'type' => MYCRED_DEFAULT_TYPE_KEY,
147
+ 'amount' => 0,
148
+ 'log' => ''
149
+ )
150
+ );
151
+
152
+ $row['requires'][] = $requirements;
153
+
154
+ $new_setup[] = $row;
155
+
156
+ }
157
+
158
+ if ( ! empty( $new_setup ) ) {
159
+
160
+ update_post_meta( $badge_id, 'badge_prefs', $new_setup );
161
+ delete_post_meta( $badge_id, 'badge_requirements' );
162
+
163
+ $setup = $new_setup;
164
+
165
+ }
166
+
167
+ }
168
+
169
+ }
170
+
171
+ //$setup = array();
172
+
173
+ if ( empty( $setup ) )
174
+ $setup[] = array(
175
+ 'image_url' => '',
176
+ 'attachment_id' => 0,
177
+ 'label' => '',
178
+ 'compare' => 'AND',
179
+ 'requires' => array(
180
+ 0 => array(
181
+ 'type' => MYCRED_DEFAULT_TYPE_KEY,
182
+ 'reference' => '',
183
+ 'amount' => '',
184
+ 'by' => ''
185
+ )
186
+ ),
187
+ 'reward' => array(
188
+ 'type' => MYCRED_DEFAULT_TYPE_KEY,
189
+ 'amount' => 0,
190
+ 'log' => ''
191
  )
192
  );
193
 
194
+ return apply_filters( 'mycred_badge_levels', $setup, $badge_id );
195
 
196
  }
197
  endif;
200
  * Display Badge Requirements
201
  * Returns the badge requirements as a string in a readable format.
202
  * @since 1.5
203
+ * @version 1.2
204
  */
205
  if ( ! function_exists( 'mycred_display_badge_requirement' ) ) :
206
+ function mycred_display_badge_requirements( $badge_id = NULL ) {
207
 
208
+ $levels = mycred_get_badge_levels( $badge_id );
209
+ if ( empty( $levels ) ) {
210
 
211
  $reply = '-';
212
 
213
  }
214
  else {
215
 
216
+ $point_types = mycred_get_types( true );
217
+ $references = mycred_get_all_references();
218
+ $req_count = count( $levels[0]['requires'] );
219
 
220
+ // Get the requirements for the first level
221
+ $base_requirements = array();
222
+ foreach ( $levels[0]['requires'] as $requirement_row => $requirement ) {
223
 
224
+ if ( $requirement['type'] == '' )
225
+ $requirement['type'] = MYCRED_DEFAULT_TYPE_KEY;
226
 
227
+ if ( ! array_key_exists( $requirement['type'], $point_types ) )
228
  continue;
229
 
230
+ if ( ! array_key_exists( $requirement['reference'], $references ) )
231
+ $reference = '-';
 
 
 
232
  else
233
+ $reference = $references[ $requirement['reference'] ];
234
 
235
+ $base_requirements[ $requirement_row ] = array(
236
+ 'type' => $requirement['type'],
237
+ 'ref' => $reference,
238
+ 'amount' => $requirement['amount'],
239
+ 'by' => $requirement['by']
240
+ );
241
 
242
+ }
243
+
244
+ // Loop through each level
245
+ $output = array();
246
+ foreach ( $levels as $level => $setup ) {
247
+
248
+ $level_label = '<strong>' . sprintf( __( 'Level %s', 'mycred' ), ( $level + 1 ) ) . ':</strong>';
249
+ if ( $levels[0]['label'] != '' )
250
+ $level_label = '<strong>' . $levels[0]['label'] . ':</strong>';
251
+
252
+ // Construct requirements to be used in an unorganized list.
253
+ $level_req = array();
254
+ foreach ( $setup['requires'] as $requirement_row => $requirement ) {
255
+
256
+ $level_value = $requirement['amount'];
257
+ $requirement = $base_requirements[ $requirement_row ];
258
+
259
+ $mycred = mycred( $requirement['type'] );
260
+
261
+ if ( $level > 0 )
262
+ $requirement['amount'] = $level_value;
263
+
264
+ if ( $requirement['by'] == 'count' )
265
+ $rendered_row = sprintf( _x( '%s for "%s" x %d', '"Points" for "reference" x times', 'mycred' ), $mycred->plural(), $requirement['ref'], $requirement['amount'] );
266
+ else
267
+ $rendered_row = sprintf( _x( '%s %s for "%s"', '"Gained/Lost" "x points" for "reference"', 'mycred' ), ( ( $requirement['amount'] < 0 ) ? __( 'Lost', 'mycred' ) : __( 'Gained', 'mycred' ) ), $mycred->format_creds( $requirement['amount'] ), $requirement['ref'] );
268
+
269
+ $compare = _x( 'OR', 'Comparison of badge requirements. A OR B', 'mycred' );
270
+ if ( $setup['compare'] === 'AND' )
271
+ $compare = _x( 'AND', 'Comparison of badge requirements. A AND B', 'mycred' );
272
+
273
+ if ( $req_count > 1 && $requirement_row+1 < $req_count )
274
+ $rendered_row .= '<span>' . $compare . '</span>';
275
+
276
+ $level_req[] = $rendered_row;
277
+
278
+ }
279
+
280
+ if ( empty( $level_req ) ) continue;
281
+
282
+ $output[] = $level_label . '<ul class="mycred-badge-requirement-list"><li>' . implode( '</li><li>', $level_req ) . '</li></ul>';
283
 
284
  }
285
+
286
+ if ( (int) get_post_meta( $badge_id, 'manual_badge', true ) === 1 )
287
+ $output[] = '<strong><small><em>' . __( 'This badge is manually awarded.', 'mycred' ) . '</em></small></strong>';
288
+
289
+ $reply = implode( '', $output );
290
 
291
  }
292
 
293
+ return apply_filters( 'mycred_badge_display_requirements', $reply, $badge_id );
294
 
295
  }
296
  endif;
297
 
298
  /**
299
  * Count Users with Badge
300
+ * Counts the number of users that has the given badge. Option to get count
301
+ * of a specific level.
302
  * @since 1.5
303
+ * @version 1.1
304
  */
305
  if ( ! function_exists( 'mycred_count_users_with_badge' ) ) :
306
+ function mycred_count_users_with_badge( $badge_id = NULL, $level = NULL ) {
307
 
308
+ if ( $badge_id === NULL ) return 0;
309
 
310
+ $count = get_post_meta( $badge_id, 'total-users-with-badge', true );
311
+
312
+ if ( $count == '' || $level !== NULL ) {
 
 
313
 
314
+ global $wpdb;
315
+
316
+ $level_filter = '';
317
+ if ( $level !== NULL && is_numeric( $level ) )
318
+ $level_filter = $wpdb->prepare( "AND meta_value = %s", $level );
319
+
320
+ $count = $wpdb->get_var( $wpdb->prepare( "
321
+ SELECT COUNT( DISTINCT user_id )
322
+ FROM {$wpdb->usermeta}
323
+ WHERE meta_key = %s {$level_filter};", mycred_get_meta_key( 'mycred_badge' . $badge_id ) ) );
324
+
325
+ if ( $count === NULL ) $count = 0;
326
+
327
+ if ( $count > 0 && $level === NULL )
328
+ add_post_meta( $badge_id, 'total-users-with-badge', $count, true );
329
+
330
+ }
331
+
332
+ return apply_filters( 'mycred_count_users_with_badge', absint( $count ), $badge_id );
333
 
334
  }
335
  endif;
338
  * Count Users without Badge
339
  * Counts the number of users that does not have a given badge.
340
  * @since 1.5
341
+ * @version 1.1
342
  */
343
  if ( ! function_exists( 'mycred_count_users_without_badge' ) ) :
344
+ function mycred_count_users_without_badge( $badge_id = NULL ) {
345
 
346
+ $total = count_users();
347
+ $with_badge = mycred_count_users_with_badge( $badge_id );
348
 
349
+ $without_badge = $total['total_users'] - $with_badge;
 
 
 
 
350
 
351
+ return apply_filters( 'mycred_count_users_without_badge', absint( $without_badge ), $badge_id );
352
 
353
  }
354
  endif;
357
  * Reference Has Badge
358
  * Checks if a given reference has a badge associated with it.
359
  * @since 1.5
360
+ * @version 1.4
361
  */
362
  if ( ! function_exists( 'mycred_ref_has_badge' ) ) :
363
+ function mycred_ref_has_badge( $reference = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
 
 
364
 
365
+ $badge_references = mycred_get_badge_references( $point_type );
366
+ $badge_references = maybe_unserialize( $badge_references );
367
 
368
+ $badge_ids = array();
369
+ if ( ! empty( $badge_references ) && array_key_exists( $reference, $badge_references ) )
370
+ $badge_ids = $badge_references[ $reference ];
 
 
371
 
372
  if ( empty( $badge_ids ) )
373
  $badge_ids = false;
374
 
375
+ return apply_filters( 'mycred_ref_has_badge', $badge_ids, $reference, $badge_references, $point_type );
376
+
377
+ }
378
+ endif;
379
+
380
+ /**
381
+ * Badge Level Reached
382
+ * Checks what level a user has earned for a badge. Returns false if badge was not earned.
383
+ * @since 1.7
384
+ * @version 1.0
385
+ */
386
+ if ( ! function_exists( 'mycred_badge_level_reached' ) ) :
387
+ function mycred_badge_level_reached( $user_id = NULL, $badge_id = NULL ) {
388
+
389
+ $user_id = absint( $user_id );
390
+ if ( $user_id === 0 ) return false;
391
+
392
+ $badge_id = absint( $badge_id );
393
+ if ( $badge_id === 0 ) return false;
394
+
395
+ global $wpdb;
396
+
397
+ $levels = mycred_get_badge_levels( $badge_id );
398
+ if ( empty( $levels ) ) return false;
399
+
400
+ $base_requirements = $levels[0]['requires'];
401
+ $compare = $levels[0]['compare'];
402
+ $requirements = count( $base_requirements );
403
+ $level_reached = false;
404
+ $results = array();
405
+
406
+ // Based on the base requirements, we first get the users log entry results
407
+ if ( ! empty( $base_requirements ) ) {
408
+ foreach ( $base_requirements as $requirement_id => $requirement ) {
409
+
410
+ if ( $requirement['type'] == '' )
411
+ $requirement['type'] = MYCRED_DEFAULT_TYPE_KEY;
412
+
413
+ $mycred = mycred( $requirement['type'] );
414
+ if ( $mycred->exclude_user( $user_id ) ) continue;
415
+
416
+ $having = 'COUNT(*)';
417
+ if ( $requirement['by'] != 'count' )
418
+ $having = 'SUM(creds)';
419
+
420
+ $query = $wpdb->get_var( $wpdb->prepare( "SELECT {$having} FROM {$mycred->log_table} WHERE ctype = %s AND ref = %s AND user_id = %d;", $requirement['type'], $requirement['reference'], $user_id ) );
421
+ if ( $query === NULL ) $query = 0;
422
+
423
+ $results[ $requirement['reference'] ] = $query;
424
+
425
+ }
426
+ }
427
+
428
+ // Next we loop through the levels and see compare the previous results to the requirements to determan our level
429
+ foreach ( $levels as $level_id => $level_setup ) {
430
+
431
+ $reqs_met = 0;
432
+ foreach ( $level_setup['requires'] as $requirement_id => $requirement ) {
433
+
434
+ if ( $results[ $requirement['reference'] ] >= $requirement['amount'] )
435
+ $reqs_met++;
436
+
437
+ }
438
+
439
+ if ( $compare === 'AND' && $reqs_met >= $requirements )
440
+ $level_reached = $level_id;
441
+
442
+ elseif ( $compare === 'OR' && $reqs_met > 0 )
443
+ $level_reached = $level_id;
444
+
445
+ }
446
+
447
+ do_action( 'mycred_badge_level_reached', $user_id, $badge_id, $level_reached );
448
+
449
+ return $level_reached;
450
 
451
  }
452
  endif;
455
  * Check if User Gets Badge
456
  * Checks if a given user has earned one or multiple badges.
457
  * @since 1.5
458
+ * @version 1.3
459
  */
460
  if ( ! function_exists( 'mycred_check_if_user_gets_badge' ) ) :
461
+ function mycred_check_if_user_gets_badge( $user_id = NULL, $badge_ids = array(), $depreciated = array(), $save = true ) {
462
+
463
+ $user_id = absint( $user_id );
464
+ if ( $user_id === 0 ) return false;
465
+
466
+ $earned_badge_ids = array();
467
+ if ( ! empty( $badge_ids ) ) {
468
+ foreach ( $badge_ids as $badge_id ) {
469
+
470
+ $level_reached = mycred_badge_level_reached( $user_id, $badge_id );
471
+ if ( $level_reached !== false ) {
472
+
473
+ $earned_badge_ids[] = $badge_id;
474
+ delete_post_meta( $badge_id, 'total-users-with-badge' );
475
+
476
+ if ( $save )
477
+ mycred_assign_badge_to_user( $user_id, $badge_id, $level_reached );
478
+ }
479
+
480
+ }
481
+ }
482
+
483
+ return $earned_badge_ids;
484
+
485
+ }
486
+ endif;
487
 
488
+ /**
489
+ * Assign Badge
490
+ * Assigns a given badge to all users that fulfill the
491
+ * badges requirements.
492
+ * @since 1.7
493
+ * @version 1.0
494
+ */
495
+ if ( ! function_exists( 'mycred_assign_badge' ) ) :
496
+ function mycred_assign_badge( $badge_id = NULL ) {
497
+
498
+ $badge_id = absint( $badge_id );
499
+ if ( $badge_id === 0 ) return false;
500
 
501
  global $wpdb;
502
 
503
+ $user_ids = array();
504
+ $levels = mycred_get_badge_levels( $badge_id );
505
+ if ( empty( $levels ) ) return false;
506
+
507
+ $requirements = count( $levels[0]['requires'] );
508
+ $compare = $levels[0]['compare'];
509
 
510
+ if ( ! empty( $levels ) ) {
511
+ foreach ( $levels as $level_id => $level_setup ) {
 
512
 
513
+ $level_user_ids = array();
 
514
 
515
+ // Get all user IDs that fulfill each requirements set
516
+ if ( ! empty( $level_setup['requires'] ) ) {
517
+ foreach ( $level_setup['requires'] as $requirement_id => $requirement ) {
518
 
519
+ if ( $requirement['type'] == '' )
520
+ $requirement['type'] = MYCRED_DEFAULT_TYPE_KEY;
521
+
522
+ $mycred = mycred( $requirement['type'] );
523
+ $having = "COUNT(id)";
524
+ $format = '%d';
525
+
526
+ if ( $requirement['by'] != 'count' )
527
+ $having = "SUM(creds)";
528
+
529
+ if ( $requirement['by'] != 'count' && $mycred->format['decimals'] > 0 )
530
+ $format = '%f';
531
+
532
+ $level_user_ids[ $requirement_id ] = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT user_id FROM {$mycred->log_table} WHERE ctype = %s AND ref = %s GROUP BY user_id HAVING {$having} >= {$format};", $requirement['type'], $requirement['reference'], $requirement['amount'] ) );
533
+
534
+ }
535
  }
536
 
537
+ // OR = get all unique IDs
538
+ if ( $compare == 'OR' ) {
539
+
540
+ $list = array();
541
+ foreach ( $level_user_ids as $requirement_id => $list_of_ids ) {
542
+ if ( ! empty( $list_of_ids ) ) {
543
+ foreach ( $list_of_ids as $uid ) {
544
+ if ( ! in_array( $uid, $list ) )
545
+ $list[] = $uid;
546
+ }
547
+ }
548
+ }
549
+
550
+ }
551
+
552
+ // AND = get IDs that are in all requirements
553
  else {
554
+
555
+ $list = $_list = array();
556
+
557
+ foreach ( $level_user_ids as $requirement_id => $list_of_ids ) {
558
+ if ( ! empty( $list_of_ids ) ) {
559
+ foreach ( $list_of_ids as $uid ) {
560
+ if ( ! array_key_exists( $uid, $_list ) )
561
+ $_list[ $uid ] = 1;
562
+ else
563
+ $_list[ $uid ]++;
564
+ }
565
+ }
566
+ }
567
+
568
+ foreach ( $_list as $uid => $count ) {
569
+ if ( $count >= $requirements )
570
+ $list[] = $uid;
571
+ }
572
+
573
  }
574
 
575
+ // If no user has reached the first level, no one will have reached higher levels and there is no need to continue
576
+ if ( $level_id == 0 && empty( $list ) ) break;
 
 
 
 
577
 
578
+ // Create a list where the array key represents the user ID and the array value represents the badge level reached by the user
579
+ foreach ( $list as $user_id ) {
580
+ $user_ids[ $user_id ] = $level_id;
581
+ }
582
 
583
+ }
584
+ }
 
 
585
 
586
+ // If we have results, save
587
+ if ( ! empty( $user_ids ) ) {
588
+ foreach ( $user_ids as $user_id => $level_reached ) {
589
 
590
+ // Assign the badge
591
+ mycred_assign_badge_to_user( $user_id, $badge_id, $level_reached );
592
 
593
+ // Payout reward
594
+ if ( $levels[ $level_reached ]['reward']['log'] != '' && $levels[ $level_reached ]['reward']['amount'] != 0 ) {
595
 
596
+ $reward_type = $levels[ $level_reached ]['reward']['type'];
597
+ if ( $reward_type == '' )
598
+ $reward_type = MYCRED_DEFAULT_TYPE_KEY;
599
+
600
+ $mycred = mycred( $reward_type );
601
 
602
+ // Make sure we only get points once for each level we reach for each badge
603
+ if ( ! $mycred->has_entry( 'badge_reward', $badge_id, $user_id, $level_reached, $reward_type ) )
604
+ $mycred->add_creds(
605
+ 'badge_reward',
606
+ $user_id,
607
+ $levels[ $level_reached ]['reward']['amount'],
608
+ $levels[ $level_reached ]['reward']['log'],
609
+ $badge_id,
610
+ $level_reached,
611
+ $reward_type
612
+ );
613
 
614
  }
615
 
616
  }
 
617
  }
618
 
619
+ return $user_ids;
620
+
621
+ }
622
+ endif;
623
+
624
+ /**
625
+ * Assign Badge to User
626
+ * @since 1.7
627
+ * @version 1.0
628
+ */
629
+ if ( ! function_exists( 'mycred_assign_badge_to_user' ) ) :
630
+ function mycred_assign_badge_to_user( $user_id = NULL, $badge_id = NULL, $level = 0 ) {
631
+
632
+ $user_id = absint( $user_id );
633
+ if ( $user_id === 0 ) return false;
634
+
635
+ $badge_id = absint( $badge_id );
636
+ if ( $badge_id === 0 ) return false;
637
+
638
+ $level = absint( $level );
639
+
640
+ mycred_update_user_meta( $user_id, 'mycred_badge' . $badge_id, '', apply_filters( 'mycred_badge_user_value', $level, $user_id, $badge_id ) );
641
 
642
  }
643
  endif;
646
  * Get Users Badges
647
  * Returns the badge post IDs that a given user currently holds.
648
  * @since 1.5
649
+ * @version 1.2
650
  */
651
  if ( ! function_exists( 'mycred_get_users_badges' ) ) :
652
  function mycred_get_users_badges( $user_id = NULL ) {
671
  $badge_id = (int) $badge_id;
672
  if ( array_key_exists( $badge_id, $badge_ids ) ) continue;
673
 
674
+ $badge_ids[ $badge_id ] = $badge->meta_value;
 
 
 
 
675
 
676
  }
677
  }
685
  * Display Users Badges
686
  * Will echo all badge images a given user has earned.
687
  * @since 1.5
688
+ * @version 1.3
689
  */
690
  if ( ! function_exists( 'mycred_display_users_badges' ) ) :
691
  function mycred_display_users_badges( $user_id = NULL ) {
692
 
693
+ $user_id = absint( $user_id );
694
+ if ( $user_id === 0 ) return;
695
 
696
  $users_badges = mycred_get_users_badges( $user_id );
697
 
698
+ echo '<div class="row" id="mycred-users-badges"><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">';
699
 
700
  do_action( 'mycred_before_users_badges', $user_id, $users_badges );
701
 
703
 
704
  foreach ( $users_badges as $badge_id => $level ) {
705
 
706
+ $badge = mycred_get_badge( $badge_id, $level );
707
+ if ( $badge->level_image !== false )
708
+ echo apply_filters( 'mycred_the_badge', $badge->level_image, $badge_id, $badge, $user_id );
 
 
 
709
 
710
  }
711
 
713
 
714
  do_action( 'mycred_after_users_badges', $user_id, $users_badges );
715
 
716
+ echo '</div></div>';
717
 
718
  }
719
  endif;
733
  SELECT ID
734
  FROM {$wpdb->posts}
735
  WHERE post_type = 'mycred_badge'
736
+ AND post_status = 'publish'
737
+ ORDER BY post_date ASC;" );
738
 
739
  return apply_filters( 'mycred_get_badge_ids', $badge_ids );
740
 
741
  }
742
  endif;
743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744
  ?>
addons/badges/includes/mycred-badge-object.php ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * myCRED_Badge class
6
+ * @see http://codex.mycred.me/classes/mycred_badge/
7
+ * @since 1.7
8
+ * @version 1.0
9
+ */
10
+ if ( ! class_exists( 'myCRED_Badge' ) ) :
11
+ class myCRED_Badge extends myCRED_Object {
12
+
13
+ public $post_id = false;
14
+ public $title = '';
15
+ public $earnedby = 0;
16
+ public $manual = false;
17
+ public $levels = array();
18
+ public $level = false;
19
+ public $level_label = false;
20
+ public $main_image = false;
21
+ public $level_image = false;
22
+ public $image_width = false;
23
+ public $image_height = false;
24
+
25
+ /**
26
+ * Construct
27
+ */
28
+ function __construct( $badge_id = NULL, $level = NULL ) {
29
+
30
+ parent::__construct();
31
+
32
+ $badge_id = absint( $badge_id );
33
+
34
+ if ( get_post_type( $badge_id ) != 'mycred_badge' ) return;
35
+
36
+ $this->image_width = MYCRED_BADGE_WIDTH;
37
+ $this->image_height = MYCRED_BADGE_HEIGHT;
38
+
39
+ $this->populate( $badge_id, $level );
40
+
41
+ }
42
+
43
+ protected function populate( $badge_id = NULL, $level = NULL ) {
44
+
45
+ $this->post_id = absint( $badge_id );
46
+ $this->title = get_the_title( $this->post_id );
47
+ $this->earnedby = mycred_count_users_with_badge( $badge_id, $level );
48
+ $this->levels = mycred_get_badge_levels( $this->post_id );
49
+
50
+ if ( absint( get_post_meta( $this->post_id, 'manual_badge', true ) ) === 1 )
51
+ $this->manual = true;
52
+
53
+ if ( $level !== NULL && ! empty( $this->levels ) && array_key_exists( $level, $this->levels ) ) {
54
+ $this->level = $this->levels[ $level ];
55
+ if ( $this->level['label'] != '' )
56
+ $this->level_label = $this->level['label'];
57
+ }
58
+
59
+ $this->main_image = $this->get_image( 'main' );
60
+ $this->level_image = $this->get_image( $level );
61
+
62
+ }
63
+
64
+ public function get_image( $image = NULL ) {
65
+
66
+ $image_identification = false;
67
+
68
+ if ( $image === 'main' )
69
+ $image_identification = get_post_meta( $this->post_id, 'main_image', true );
70
+
71
+ elseif ( $image !== NULL && is_numeric( $image ) && isset( $this->levels[ $image ]['attachment_id'] ) ) {
72
+
73
+ $image_identification = $this->levels[ $image ]['image_url'];
74
+ if ( $this->levels[ $image ]['attachment_id'] > 0 )
75
+ $image_identification = $this->levels[ $image ]['attachment_id'];
76
+
77
+ }
78
+
79
+ if ( $image_identification === false || strlen( $image_identification ) == 0 ) return false;
80
+
81
+ $image_url = $image_identification;
82
+ if ( is_numeric( $image_identification ) && strpos( '://', $image_identification ) === false )
83
+ $image_url = wp_get_attachment_url( $image_identification );
84
+
85
+ return '<img src="' . esc_url( $image_url ) . '" alt="' . esc_attr( $this->title ) . '" width="' . $this->image_width . '" height="' . $this->image_height . '" />';
86
+
87
+ }
88
+
89
+ }
90
+ endif;
91
+
92
+ ?>
addons/badges/includes/mycred-badge-shortcodes.php CHANGED
@@ -5,79 +5,67 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
5
  * Shortcode: mycred_my_badges
6
  * Allows you to show the current users earned badges.
7
  * @since 1.5
8
- * @version 1.1.2
9
  */
10
  if ( ! function_exists( 'mycred_render_my_badges' ) ) :
11
  function mycred_render_my_badges( $atts, $content = '' ) {
12
 
13
  extract( shortcode_atts( array(
14
- 'show' => 'earned',
15
- 'width' => MYCRED_BADGE_WIDTH,
16
- 'height' => MYCRED_BADGE_HEIGHT,
17
- 'user_id' => ''
18
  ), $atts ) );
19
 
20
- if ( ! is_user_logged_in() && $user_id == '' ) return $content;
21
-
22
- if ( $user_id == '' )
23
- $user_id = get_current_user_id();
24
-
25
- $users_badges = mycred_get_users_badges( $user_id );
26
-
27
- if ( $width != '' )
28
- $width = ' width="' . $width . '"';
29
 
30
- if ( $height != '' )
31
- $height = ' height="' . $height . '"';
32
 
33
  ob_start();
34
 
35
- echo '<div id="mycred-users-badges">';
36
 
37
  // Show only badges that we have earned
38
  if ( $show == 'earned' ) {
39
 
40
- if ( ! empty( $users_badges ) ) {
41
-
42
- foreach ( $users_badges as $badge_id => $level ) {
43
-
44
- $level_image = get_post_meta( $badge_id, 'level_image' . $level, true );
45
- if ( $level_image == '' )
46
- $level_image = get_post_meta( $badge_id, 'main_image', true );
47
-
48
- $badge_title = get_the_title( $badge_id );
49
- echo apply_filters( 'mycred_my_badge', '<img src="' . $level_image . '"' . $width . $height . ' class="mycred-badge earned" alt="' . esc_attr( $badge_title ) . '" title="' . esc_attr( $badge_title ) . '" />', $badge_id, $level, $user_id, $atts );
50
-
51
- }
52
-
53
- }
54
 
55
  }
56
 
57
  // Show all badges highlighting the ones we earned
58
  elseif ( $show == 'all' ) {
59
 
60
- $all_badges = mycred_get_badges();
61
- foreach ( $all_badges as $badge ) {
 
 
62
 
63
  echo '<div class="the-badge">';
64
 
65
  // User has not earned badge
66
- if ( ! array_key_exists( $badge->ID, $users_badges ) ) {
67
 
68
- if ( $badge->default_img != '' )
69
- echo '<img src="' . $badge->default_img . '"' . $width . $height . ' class="mycred-badge not-earned" alt="' . $badge->post_title . '" title="' . $badge->post_title . '" />';
 
 
 
 
70
 
71
  }
72
 
73
- // User has earned badge
74
  else {
75
 
76
- $level_image = get_post_meta( $badge->ID, 'level_image' . $users_badges[ $badge->ID ], true );
77
- if ( $level_image == '' )
78
- $level_image = $badge->main_img;
 
 
 
 
79
 
80
- echo '<img src="' . $badge->main_img . '"' . $width . $height . ' class="mycred-badge earned" alt="' . $badge->post_title . '" title="' . $badge->post_title . '" />';
81
  }
82
 
83
  echo '</div>';
@@ -85,7 +73,7 @@ if ( ! function_exists( 'mycred_render_my_badges' ) ) :
85
  }
86
 
87
  }
88
- echo '</div>';
89
 
90
  $output = ob_get_contents();
91
  ob_end_clean();
@@ -99,70 +87,44 @@ endif;
99
  * Shortcode: mycred_badges
100
  * Allows you to show all published badges
101
  * @since 1.5
102
- * @version 1.0.2
103
  */
104
  if ( ! function_exists( 'mycred_render_badges' ) ) :
105
  function mycred_render_badges( $atts, $content = '' ) {
106
 
107
  extract( shortcode_atts( array(
108
- 'show' => 'default',
109
- 'title' => 0,
110
- 'requires' => 0,
111
- 'show_count' => 0,
112
- 'width' => MYCRED_BADGE_WIDTH,
113
- 'height' => MYCRED_BADGE_HEIGHT
114
  ), $atts ) );
115
 
116
- $all_badges = mycred_get_badges();
117
 
118
- if ( $width != '' )
119
- $width = ' width="' . $width . '"';
120
 
121
- if ( $height != '' )
122
- $height = ' height="' . $height . '"';
123
-
124
- ob_start();
125
-
126
- echo '<div id="mycred-all-badges">';
127
 
128
  if ( ! empty( $all_badges ) ) {
129
 
130
- foreach ( $all_badges as $badge ) {
131
-
132
- echo '<div class="the-badge">';
133
-
134
- if ( $title == 1 )
135
- echo '<h3 class="badge-title">' . $badge->post_title . '</h3>';
136
-
137
- if ( $requires == 1 )
138
- echo '<div class="badge-requirements">' . mycred_display_badge_requirements( $badge->ID ) . '</div>';
139
 
140
- if ( $show_count == 1 )
141
- echo '<div class="users-with-badge">' . mycred_count_users_with_badge( $badge->ID ) . '</div>';
 
142
 
143
- // Show default image
144
- if ( $show == 'default' )
145
- echo '<img src="' . $badge->default_img . '"' . $width . $height . ' class="mycred-badge dislay-default" alt="' . $badge->post_title . '" title="' . $badge->post_title . '" />';
 
 
146
 
147
- // Show main image
148
- elseif ( $show == 'main' )
149
- echo '<img src="' . $badge->main_img . '"' . $width . $height . ' class="mycred-badge display-main" alt="' . $badge->post_title . '" title="' . $badge->post_title . '" />';
150
-
151
- // Show both
152
- else {
153
- echo '<img src="' . $badge->default_img . '"' . $width . $height . ' class="mycred-badge dislay-default" alt="' . $badge->post_title . '" title="' . $badge->post_title . '" />';
154
- echo '<img src="' . $badge->main_img . '"' . $width . $height . ' class="mycred-badge display-main" alt="' . $badge->post_title . '" title="' . $badge->post_title . '" />';
155
- }
156
- echo '</div>';
157
 
158
  }
159
 
160
  }
161
 
162
- echo '</div>';
163
-
164
- $output = ob_get_contents();
165
- ob_end_clean();
166
 
167
  return apply_filters( 'mycred_badges', $output );
168
 
5
  * Shortcode: mycred_my_badges
6
  * Allows you to show the current users earned badges.
7
  * @since 1.5
8
+ * @version 1.2
9
  */
10
  if ( ! function_exists( 'mycred_render_my_badges' ) ) :
11
  function mycred_render_my_badges( $atts, $content = '' ) {
12
 
13
  extract( shortcode_atts( array(
14
+ 'show' => 'earned',
15
+ 'width' => MYCRED_BADGE_WIDTH,
16
+ 'height' => MYCRED_BADGE_HEIGHT,
17
+ 'user_id' => 'current'
18
  ), $atts ) );
19
 
20
+ if ( ! is_user_logged_in() && $user_id == 'current' )
21
+ return $content;
 
 
 
 
 
 
 
22
 
23
+ $user_id = mycred_get_user_id( $user_id );
 
24
 
25
  ob_start();
26
 
27
+ echo '<div class="row" id="mycred-users-badges"><div class="col-xs-12">';
28
 
29
  // Show only badges that we have earned
30
  if ( $show == 'earned' ) {
31
 
32
+ mycred_display_users_badges( $user_id, $width, $height );
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  }
35
 
36
  // Show all badges highlighting the ones we earned
37
  elseif ( $show == 'all' ) {
38
 
39
+ $users_badges = mycred_get_users_badges( $user_id );
40
+ $all_badges = mycred_get_badge_ids();
41
+
42
+ foreach ( $all_badges as $badge_id ) {
43
 
44
  echo '<div class="the-badge">';
45
 
46
  // User has not earned badge
47
+ if ( ! array_key_exists( $badge_id, $users_badges ) ) {
48
 
49
+ $badge = mycred_get_badge( $badge_id );
50
+ $badge->image_width = $width;
51
+ $badge->image_height = $height;
52
+
53
+ if ( $badge->main_image !== false )
54
+ echo $badge->main_image;
55
 
56
  }
57
 
58
+ // User has earned badge
59
  else {
60
 
61
+ $level = $users_badges[ $badge_id ];
62
+ $badge = mycred_get_badge( $badge_id, $level );
63
+ $badge->image_width = $width;
64
+ $badge->image_height = $height;
65
+
66
+ if ( $badge->level_image !== false )
67
+ echo $badge->level_image;
68
 
 
69
  }
70
 
71
  echo '</div>';
73
  }
74
 
75
  }
76
+ echo '</div></div>';
77
 
78
  $output = ob_get_contents();
79
  ob_end_clean();
87
  * Shortcode: mycred_badges
88
  * Allows you to show all published badges
89
  * @since 1.5
90
+ * @version 1.1
91
  */
92
  if ( ! function_exists( 'mycred_render_badges' ) ) :
93
  function mycred_render_badges( $atts, $content = '' ) {
94
 
95
  extract( shortcode_atts( array(
96
+ 'width' => MYCRED_BADGE_WIDTH,
97
+ 'height' => MYCRED_BADGE_HEIGHT
 
 
 
 
98
  ), $atts ) );
99
 
100
+ $all_badges = mycred_get_badge_ids();
101
 
102
+ if ( $content == '' )
103
+ $content = '<div class="the-badge row"><div class="col-xs-12"><h3 class="badge-title">%badge_title%</h3><div class="badge-requirements">%requirements%</div><div class="users-with-badge">%count%</div><div class="badge-images">%default_image%</div></div></div>';
104
 
105
+ $output = '<div id="mycred-all-badges">';
 
 
 
 
 
106
 
107
  if ( ! empty( $all_badges ) ) {
108
 
109
+ foreach ( $all_badges as $badge_id ) {
 
 
 
 
 
 
 
 
110
 
111
+ $badge = mycred_get_badge( $badge_id, 0 );
112
+ $badge->image_width = $width;
113
+ $badge->image_height = $height;
114
 
115
+ $row = str_replace( '%badge_title%', $badge->title, $content );
116
+ $row = str_replace( '%requirements%', mycred_display_badge_requirements( $badge_id ), $content );
117
+ $row = str_replace( '%count%', $badge->earnedby, $content );
118
+ $row = str_replace( '%default_image%', $badge->main_image, $content );
119
+ $row = str_replace( '%main_image%', $badge->level_image, $content );
120
 
121
+ $output .= apply_filters( 'mycred_badges_badge', $row, $badge );
 
 
 
 
 
 
 
 
 
122
 
123
  }
124
 
125
  }
126
 
127
+ $output .= '</div>';
 
 
 
128
 
129
  return apply_filters( 'mycred_badges', $output );
130
 
addons/badges/myCRED-addon-badges.php CHANGED
@@ -2,16 +2,14 @@
2
  /**
3
  * Addon: Badges
4
  * Addon URI: http://mycred.me/add-ons/badges/
5
- * Version: 1.1.2
6
- * Description: Give your users badges based on their interaction with your website.
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
 
12
- define( 'myCRED_BADGE', __FILE__ );
13
- define( 'myCRED_BADGE_DIR', myCRED_ADDONS_DIR . 'badges/' );
14
- define( 'myCRED_BADGE_VERSION', myCRED_VERSION . '.1' );
 
15
 
16
  // Default badge width
17
  if ( ! defined( 'MYCRED_BADGE_WIDTH' ) )
@@ -21,13 +19,14 @@ if ( ! defined( 'MYCRED_BADGE_WIDTH' ) )
21
  if ( ! defined( 'MYCRED_BADGE_HEIGHT' ) )
22
  define( 'MYCRED_BADGE_HEIGHT', 100 );
23
 
24
- require_once myCRED_BADGE_DIR . 'includes/mycred-badge-functions.php';
25
- require_once myCRED_BADGE_DIR . 'includes/mycred-badge-shortcodes.php';
 
26
 
27
  /**
28
  * myCRED_buyCRED_Module class
29
  * @since 1.5
30
- * @version 1.1
31
  */
32
  if ( ! class_exists( 'myCRED_Badge_Module' ) ) :
33
  class myCRED_Badge_Module extends myCRED_Module {
@@ -35,7 +34,7 @@ if ( ! class_exists( 'myCRED_Badge_Module' ) ) :
35
  /**
36
  * Construct
37
  */
38
- function __construct( $type = 'mycred_default' ) {
39
 
40
  parent::__construct( 'myCRED_Badge_Module', array(
41
  'module_name' => 'badges',
@@ -45,47 +44,37 @@ if ( ! class_exists( 'myCRED_Badge_Module' ) ) :
45
  'show_all_bp' => 0,
46
  'show_all_bb' => 0
47
  ),
48
- 'labels' => array(
49
- 'menu' => __( 'Badges', 'mycred' ),
50
- 'page_title' => __( 'Badges', 'mycred' ),
51
- 'page_header' => __( 'Badges', 'mycred' )
52
- ),
53
  'add_to_core' => true,
54
  'register' => false,
55
- 'menu_pos' => 90
56
  ), $type );
57
 
58
  }
59
 
60
  /**
61
  * Module Pre Init
 
62
  * @version 1.0
63
  */
64
  public function module_pre_init() {
65
 
66
  add_filter( 'mycred_add_finished', array( $this, 'add_finished' ), 30, 3 );
 
67
 
68
  }
69
 
70
  /**
71
  * Module Init
72
- * @version 1.0.2
 
73
  */
74
  public function module_init() {
75
 
76
- $this->register_post_type();
77
 
78
  add_shortcode( 'mycred_my_badges', 'mycred_render_my_badges' );
79
  add_shortcode( 'mycred_badges', 'mycred_render_badges' );
80
 
81
- add_action( 'admin_head', array( $this, 'remove_user_page' ) );
82
- add_action( 'admin_menu', array( $this, 'add_user_subpage' ) );
83
-
84
- if ( class_exists( 'BuddyPress' ) )
85
- add_filter( 'mycred_edit_profile_tabs_bp', array( $this, 'add_user_tabs' ), 10, 2 );
86
-
87
- add_filter( 'mycred_edit_profile_tabs', array( $this, 'add_user_tabs' ), 10, 2 );
88
-
89
  // Insert into bbPress
90
  if ( class_exists( 'bbPress' ) ) {
91
 
@@ -110,14 +99,19 @@ if ( ! class_exists( 'myCRED_Badge_Module' ) ) :
110
 
111
  }
112
 
 
 
113
  }
114
 
115
  /**
116
  * Module Admin Init
 
117
  * @version 1.0
118
  */
119
  public function module_admin_init() {
120
 
 
 
121
  add_action( 'mycred_admin_enqueue', array( $this, 'enqueue_scripts' ) );
122
 
123
  add_filter( 'manage_mycred_badge_posts_columns', array( $this, 'adjust_column_headers' ) );
@@ -127,338 +121,281 @@ if ( ! class_exists( 'myCRED_Badge_Module' ) ) :
127
 
128
  add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
129
  add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
130
-
131
- add_action( 'add_meta_boxes_mycred_badge', array( $this, 'add_meta_boxes' ) );
132
  add_action( 'post_submitbox_start', array( $this, 'publishing_actions' ) );
133
- add_action( 'save_post_mycred_badge', array( $this, 'save_badge_post' ) );
134
 
135
  add_action( 'wp_ajax_mycred-assign-badge', array( $this, 'action_assign_badge' ) );
136
  add_action( 'wp_ajax_mycred-remove-connections', array( $this, 'action_remove_connections' ) );
137
 
138
- }
139
 
140
- /**
141
- * Remove User Badge from Menu
142
- * @version 1.0
143
- */
144
- public function remove_user_page() {
145
 
146
- remove_submenu_page( 'users.php', 'mycred-edit-badges' );
 
147
 
148
  }
149
 
150
  /**
151
- * Add User Badge Sub Page
 
152
  * @version 1.0
153
  */
154
- public function add_user_subpage() {
155
 
156
- $page = add_users_page(
157
- __( 'Badges', 'mycred' ),
158
- __( 'Badges', 'mycred' ),
159
- 'edit_users',
160
- 'mycred-edit-badges',
161
- array( $this, 'badge_user_screen' )
 
 
 
 
 
 
 
 
162
  );
163
 
164
- add_action( 'admin_print_styles-' . $page, array( $this, 'badge_user_screen_header' ) );
165
-
166
- }
167
-
168
- /**
169
- * Add User Tabs
170
- * @version 1.0
171
- */
172
- public function add_user_tabs( $tabs, $user = NULL ) {
173
-
174
- if ( ! isset( $user->ID ) )
175
- $user_id = $_GET['user_id'];
176
- else
177
- $user_id = $user->ID;
178
-
179
- $classes = 'nav-tab';
180
- if ( isset( $_GET['page'] ) && $_GET['page'] == 'mycred-edit-badges' ) $classes .= ' nav-tab-active';
181
-
182
- $tabs[] = array(
183
- 'label' => sprintf( __( 'Badges (%d)', 'mycred' ), count( mycred_get_users_badges( $user_id ) ) ),
184
- 'url' => add_query_arg( array( 'page' => 'mycred-edit-badges', 'user_id' => $user_id ), admin_url( 'users.php' ) ),
185
- 'classes' => $classes
186
  );
187
 
188
- return $tabs;
189
 
190
  }
191
 
192
  /**
193
- * AJAX: Assign Badge
194
- * @version 1.1.1
 
195
  */
196
- public function action_assign_badge() {
197
-
198
- check_ajax_referer( 'mycred-assign-badge', 'token' );
199
-
200
- $badge_id = absint( $_POST['badge_id'] );
201
- $requirements = mycred_get_badge_requirements( $badge_id );
202
-
203
- if ( empty( $requirements ) )
204
- wp_send_json_error( 'This badge has no requirements set!' );
205
 
206
- global $wpdb;
207
-
208
- $levels = array();
209
- foreach ( $requirements as $req_level => $needs ) {
210
-
211
- if ( $needs['type'] == '' )
212
- $needs['type'] = 'mycred_default';
213
-
214
- $mycred = mycred( $needs['type'] );
215
-
216
- if ( ! array_key_exists( $req_level, $levels ) )
217
- $levels[ $req_level ] = array();
218
-
219
- $sql = "
220
- SELECT user_id
221
- FROM {$mycred->log_table}
222
- WHERE " . $wpdb->prepare( "ctype = %s AND ref = %s ", $needs['type'], $needs['reference'] );
223
-
224
- $sql .= " GROUP by user_id ";
225
-
226
- $amount = $needs['amount'];
227
- if ( $needs['by'] == 'count' )
228
- $sql .= "HAVING COUNT( id ) >= {$amount}";
229
- else
230
- $sql .= "HAVING SUM( creds ) >= {$amount}";
231
-
232
- // Let others play
233
- $users = $wpdb->get_col( apply_filters( 'mycred_assign_badge_sql', $sql, $badge_id ) );
234
 
235
- if ( ! empty( $users ) ) {
 
236
 
237
- $levels[ $req_level ] = $users;
238
-
239
- $unique = array();
240
- foreach ( $levels[ $req_level ] as $user_id ) {
241
 
242
- if ( $req_level == 0 )
243
- $unique[] = (int) $user_id;
244
-
245
- // If this user has a badge under the previous level we "move" it
246
- elseif ( isset( $levels[ $req_level - 1 ] ) && in_array( $user_id, $levels[ $req_level - 1 ] ) ) {
247
- $unique[] = (int) $user_id;
248
- $prev_key = array_search( $user_id, $levels[ $req_level - 1 ] );
249
- if ( $prev_key !== false )
250
- unset( $levels[ $req_level - 1 ][ $prev_key ] );
251
- }
252
 
253
- }
 
 
254
 
255
- $levels[ $req_level ] = $unique;
256
 
257
  }
258
-
259
  }
260
 
261
- if ( ! empty( $levels ) ) {
262
 
263
- $count = 0;
264
- foreach ( $levels as $level => $user_ids ) {
265
 
266
- if ( empty( $user_ids ) ) continue;
267
-
268
- foreach ( $user_ids as $user_id ) {
269
- mycred_update_user_meta( $user_id, 'mycred_badge' . $badge_id, '', apply_filters( 'mycred_badge_user_value', $level, $user_id, $badge_id ) );
270
- $count ++;
271
- }
272
 
 
 
 
 
 
 
 
273
 
274
- }
275
 
276
- if ( $count > 0 )
277
- wp_send_json_success( sprintf( __( '%d Users earned this badge.', 'mycred' ), $count ) );
278
 
279
- }
280
 
281
- wp_send_json_error( __( 'No users has yet earned this badge.', 'mycred' ) );
282
 
283
  }
284
 
285
  /**
286
- * AJAX: Remove Badge Connections
287
- * @version 1.0.1
 
 
288
  */
289
- public function action_remove_connections() {
290
 
291
- check_ajax_referer( 'mycred-remove-badge-connection', 'token' );
292
 
293
- $badge_id = absint( $_POST['badge_id'] );
 
 
294
 
295
  global $wpdb;
296
 
297
- // Delete connections
298
- $count = $wpdb->delete(
299
  $wpdb->usermeta,
300
- array( 'meta_key' => 'mycred_badge' . $badge_id ),
301
  array( '%s' )
302
  );
303
 
304
- if ( $count == 0 )
305
- wp_send_json_success( __( 'No connections where removed.', 'mycred' ) );
306
-
307
- wp_send_json_success( sprintf( __( '%s connections where removed.', 'mycred' ), $count ) );
308
-
309
  }
310
 
311
  /**
312
- * Insert Badges into bbPress profile
313
- * @version 1.1
 
314
  */
315
- public function insert_into_bbpress_profile() {
316
 
317
- $user_id = bbp_get_displayed_user_id();
318
- if ( isset( $this->badges['show_all_bb'] ) && $this->badges['show_all_bb'] == 1 )
319
- mycred_render_my_badges( array(
320
- 'show' => 'all',
321
- 'width' => MYCRED_BADGE_WIDTH,
322
- 'height' => MYCRED_BADGE_HEIGHT,
323
- 'user_id' => $user_id
324
- ) );
325
 
326
- else
327
- mycred_display_users_badges( $user_id );
 
 
 
 
 
 
 
 
 
 
 
 
 
328
 
329
  }
330
 
331
  /**
332
- * Insert Badges into bbPress
333
- * @version 1.1
 
334
  */
335
- public function insert_into_bbpress_reply() {
336
 
337
- $user_id = bbp_get_reply_author_id();
338
- if ( $user_id > 0 ) {
 
 
 
 
 
339
 
340
- if ( isset( $this->badges['show_all_bb'] ) && $this->badges['show_all_bb'] == 1 )
341
- mycred_render_my_badges( array(
342
- 'show' => 'all',
343
- 'width' => MYCRED_BADGE_WIDTH,
344
- 'height' => MYCRED_BADGE_HEIGHT,
345
- 'user_id' => $user_id
346
- ) );
347
 
348
- else
349
- mycred_display_users_badges( $user_id );
 
 
 
 
 
 
350
 
 
 
 
 
351
  }
352
 
353
- }
354
 
355
- /**
356
- * Insert Badges in BuddyPress
357
- * @version 1.1.1
358
- */
359
- public function insert_into_buddypress() {
360
 
361
- $user_id = bp_displayed_user_id();
362
- if ( isset( $this->badges['show_all_bp'] ) && $this->badges['show_all_bp'] == 1 )
363
- echo mycred_render_my_badges( array(
364
- 'show' => 'all',
365
- 'width' => MYCRED_BADGE_WIDTH,
366
- 'height' => MYCRED_BADGE_HEIGHT,
367
- 'user_id' => $user_id
368
- ) );
369
 
370
- else
371
- mycred_display_users_badges( $user_id );
372
 
373
  }
374
 
375
  /**
376
- * Add Finished
377
- * @version 1.1.1
 
378
  */
379
- public function add_finished( $ran, $request, $mycred ) {
380
 
381
- if ( $ran !== false ) {
382
 
383
- // Check if this reference has badges
384
- $badge_ids = mycred_ref_has_badge( $request['ref'], $request );
385
- if ( $badge_ids !== false ) {
386
 
387
- mycred_check_if_user_gets_badge( absint( $request['user_id'] ), $badge_ids, $request );
 
 
388
 
389
- }
 
 
390
 
391
  }
392
 
393
- return $ran;
394
 
395
  }
396
 
397
  /**
398
- * Register Badge Post Type
399
- * @version 1.0
 
400
  */
401
- public function register_post_type() {
402
 
403
- $labels = array(
404
- 'name' => __( 'Badges', 'mycred' ),
405
- 'singular_name' => __( 'Badge', 'mycred' ),
406
- 'add_new' => __( 'Add New', 'mycred' ),
407
- 'add_new_item' => __( 'Add New Badge', 'mycred' ),
408
- 'edit_item' => __( 'Edit Badge', 'mycred' ),
409
- 'new_item' => __( 'New Badge', 'mycred' ),
410
- 'all_items' => __( 'Badges', 'mycred' ),
411
- 'view_item' => __( 'View Badge', 'mycred' ),
412
- 'search_items' => __( 'Search Badge', 'mycred' ),
413
- 'not_found' => __( 'No badges found', 'mycred' ),
414
- 'not_found_in_trash' => __( 'No badges found in Trash', 'mycred' ),
415
- 'parent_item_colon' => '',
416
- 'menu_name' => __( 'Badges', 'mycred' )
417
- );
418
 
419
- $args = array(
420
- 'labels' => $labels,
421
- 'public' => false,
422
- 'has_archive' => false,
423
- 'show_ui' => true,
424
- 'show_in_menu' => 'myCRED',
425
- 'supports' => array( 'title' )
426
- );
427
 
428
- register_post_type( 'mycred_badge', apply_filters( 'mycred_register_badge', $args ) );
 
 
429
 
430
- }
 
431
 
432
- /**
433
- * Enqueue Scripts
434
- * @version 1.0
435
- */
436
- public function enqueue_scripts() {
437
 
438
- $screen = get_current_screen();
439
- if ( $screen->id == 'mycred_badge' )
440
- wp_enqueue_media();
441
 
442
- elseif ( $screen->id == 'edit-mycred_badge' )
443
- wp_enqueue_style( 'mycred-badge-admin', plugins_url( 'assets/css/admin.css', myCRED_BADGE ) );
444
 
445
- elseif ( $screen->id == 'users_page_mycred-edit-badges' )
446
- wp_enqueue_style( 'mycred-badge-admin', plugins_url( 'assets/css/admin.css', myCRED_BADGE ) );
447
 
448
  }
449
 
450
  /**
451
  * Adjust Badge Column Header
 
452
  * @version 1.0
453
  */
454
  public function adjust_column_headers( $defaults ) {
455
 
456
- $columns = array();
457
- $columns['cb'] = $defaults['cb'];
458
 
459
  // Add / Adjust
460
  $columns['title'] = __( 'Badge Name', 'mycred' );
461
- $columns['badge-default-image'] = __( 'Badge Images', 'mycred' );
 
462
  $columns['badge-reqs'] = __( 'Requirements', 'mycred' );
463
  $columns['badge-users'] = __( 'Users', 'mycred' );
464
 
@@ -469,47 +406,46 @@ if ( ! class_exists( 'myCRED_Badge_Module' ) ) :
469
 
470
  /**
471
  * Adjust Badge Column Content
472
- * @version 1.0
 
473
  */
474
- public function adjust_column_content( $column_name, $post_id ) {
475
 
476
- // Default Image
477
  if ( $column_name == 'badge-default-image' ) {
478
 
479
- $default_image = get_post_meta( $post_id, 'default_image', true );
480
- if ( $default_image != '' )
481
- echo '<img src="' . $default_image . '" style="max-width: ' . MYCRED_BADGE_WIDTH . 'px;height: auto;" alt="" />';
 
 
 
 
 
482
 
483
- $main_image = get_post_meta( $post_id, 'main_image', true );
484
- if ( $main_image != '' )
485
- echo '<img src="' . $main_image . '" style="max-width: ' . MYCRED_BADGE_WIDTH . 'px;height: auto;" alt="" />';
486
 
487
- if ( $default_image == '' && $main_image == '' )
 
488
  echo '-';
489
 
490
- $requirements = mycred_get_badge_requirements( $post_id );
491
- if ( count( $requirements ) > 1 ) {
492
- foreach ( $requirements as $level => $needs ) {
493
- $level_image = get_post_meta( $post_id, 'level_image' . $level, true );
494
- if ( $level_image != '' )
495
- echo '<img src="' . $level_image . '" style="max-width: ' . MYCRED_BADGE_WIDTH . 'px;height: auto;" alt="" />';
496
- }
497
- }
498
 
499
  }
500
 
501
  // Badge Requirements
502
  elseif ( $column_name == 'badge-reqs' ) {
503
 
504
- echo '<small>' . __( 'A user must have gained or lost:', 'mycred' ) . '</small><br />';
505
- echo mycred_display_badge_requirements( $post_id );
506
 
507
  }
508
 
509
  // Badge Users
510
  elseif ( $column_name == 'badge-users' ) {
511
 
512
- echo mycred_count_users_with_badge( $post_id );
513
 
514
  }
515
 
@@ -517,6 +453,7 @@ if ( ! class_exists( 'myCRED_Badge_Module' ) ) :
517
 
518
  /**
519
  * Adjust Row Actions
 
520
  * @version 1.0
521
  */
522
  public function adjust_row_actions( $actions, $post ) {
@@ -531,79 +468,209 @@ if ( ! class_exists( 'myCRED_Badge_Module' ) ) :
531
  }
532
 
533
  /**
534
- * Adjust Post Updated Messages
 
535
  * @version 1.0
536
  */
537
- public function post_updated_messages( $messages ) {
538
 
539
- global $post;
540
 
541
- $messages['mycred_badge'] = array(
542
- 0 => '',
543
- 1 => __( 'Badge Updated.', 'mycred' ),
544
- 2 => '',
545
- 3 => '',
546
- 4 => __( 'Badge Updated.', 'mycred' ),
547
- 5 => false,
548
- 6 => __( 'Badge Enabled', 'mycred' ),
549
- 7 => __( 'Badge Saved', 'mycred' ),
550
- 8 => __( 'Badge Updated.', 'mycred' ),
551
- 9 => __( 'Badge Updated.', 'mycred' ),
552
- 10 => __( 'Badge Updated.', 'mycred' )
553
- );
554
 
555
- return $messages;
556
 
557
  }
558
 
559
  /**
560
- * Adjust Enter Title Here
 
561
  * @version 1.0
562
  */
563
- public function enter_title_here( $title ) {
564
 
565
- global $post_type;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
566
 
567
- if ( $post_type == 'mycred_badge' )
568
- return __( 'Badge Name', 'mycred' );
 
 
 
 
569
 
570
- return $title;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
571
 
572
  }
573
 
574
  /**
575
  * Add Meta Boxes
 
576
  * @version 1.0
577
  */
578
- public function add_meta_boxes() {
579
 
580
  add_meta_box(
581
- 'mycred_badge_requirements',
582
  __( 'Badge Setup', 'mycred' ),
583
- array( $this, 'metabox_badge_requirements' ),
584
  'mycred_badge',
585
  'normal',
586
  'high'
587
  );
588
 
 
 
 
 
 
 
 
 
 
589
  }
590
 
591
  /**
592
- * Badge Publishing Actions
 
593
  * @version 1.0
594
  */
595
- public function publishing_actions() {
596
 
597
- global $post;
 
598
 
599
- if ( ! isset( $post->post_type ) || $post->post_type != 'mycred_badge' ) return;
600
 
601
- $lock = '';
602
- if ( $post->post_status != 'publish' )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603
  $lock = ' disabled="disabled"';
604
 
605
  ?>
606
- <div id="mycred-badge-actions">
607
 
608
  <?php do_action( 'mycred_edit_badge_before_actions', $post ); ?>
609
 
@@ -651,326 +718,394 @@ jQuery(function($) {
651
  <?php endif; ?>
652
 
653
  </div>
 
 
 
654
  <?php
655
 
656
  }
657
 
658
  /**
659
- * Badge Preferences Metabox
660
- * @version 1.1
 
661
  */
662
- public function metabox_badge_requirements( $post ) {
663
 
664
- $requirements = mycred_get_badge_requirements( $post->ID, true );
665
- $references = mycred_get_all_references();
 
666
 
667
- $default_image = get_post_meta( $post->ID, 'default_image', true );
668
- $main_image = get_post_meta( $post->ID, 'main_image', true );
 
 
 
669
 
670
- $sums = array(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
671
  'count' => __( 'Time(s)', 'mycred' ),
672
  'sum' => __( 'In total', 'mycred' )
673
- );
 
 
 
 
 
 
 
 
 
 
674
 
675
  ?>
676
- <style type="text/css">
677
- #mycred-remove-badge-connections { float: right; }
678
- #mycred-badge-actions { padding-bottom: 12px; margin-bottom: 12px; border-bottom: 1px solid #ccc; }
679
- <?php if ( $post->post_status != 'publish' ) : ?>
680
- #minor-publishing-actions { padding-bottom: 12px; }
681
- <?php else : ?>
682
- #minor-publishing-actions { display: none; }
683
- <?php endif; ?>
684
- #misc-publishing-actions { display: none; display: none !important; }
685
- table#setup-badge-reqs { width: 100%; }
686
- table#setup-badge-reqs tr.bodered-row td { padding-bottom: 12px; border-bottom: 1px solid #dedede; }
687
- table#setup-badge-reqs tr.badge-requires td { padding-top: 12px; }
688
- table#setup-badge-reqs td.level { width: 15%; color: #aaa; }
689
- table#setup-badge-reqs td.type { width: 20%; }
690
- table#setup-badge-reqs td.for { width: 5%; }
691
- table#setup-badge-reqs td.reference { width: auto; }
692
- table#setup-badge-reqs td.amount { width: 10%; }
693
- table#setup-badge-reqs td.sum { width: 10%; }
694
- #setup-badge-reqs td label { display: block; margin-bottom: 4px; font-weight: bold; }
695
- p.actions { text-align: right; }
696
- #mycred_badge_images .inside, #mycred_badge_images .inside p { margin: 0; padding: 0; }
697
- #image-wrapper { float: none; clear: both; min-height: 100px; }
698
- #image-wrapper #main-image { border-bottom: none; }
699
- #setup-badge-reqs div.inner-box { float: none; clear: both; min-height: 50px; margin: 0; padding: 0; }
700
- #setup-badge-reqs div.inner-box .thumb { display: block; width: <?php echo MYCRED_BADGE_WIDTH; ?>px; height: <?php echo MYCRED_BADGE_HEIGHT; ?>px; padding: 12px; overflow: hidden; }
701
- #setup-badge-reqs div.inner-box .thumb img { max-width: <?php echo MYCRED_BADGE_WIDTH; ?>px; height: auto; margin: 0 auto; }
702
- </style>
703
- <script type="text/javascript">
704
- jQuery(function($) {
705
 
706
- $( '#postimagediv h3.hndle span' ).empty().text( '<?php echo esc_js( esc_attr__( 'Badge Image', 'mycred' ) ); ?>' );
707
- $( '#postimagediv div.inside p a' ).attr( 'title', '<?php echo esc_js( esc_attr__( 'Set badge image', 'mycred' ) ); ?>' ).empty().text( '<?php echo esc_js( esc_attr__( 'Set badge image', 'mycred' ) ); ?>' );
 
708
 
709
- });
710
- </script>
711
- <table class="table" style="width: 100%;" id="setup-badge-reqs">
712
 
713
- <?php do_action( 'mycred_edit_badge_before_req', $post ); ?>
 
714
 
715
- <tr class="bodered-row">
716
- <td class="level"><div class="inner-box"><div class="thumb"><?php if ( $default_image != '' ) echo '<img src="' . $default_image . '" alt="" />'; ?></div></div></td>
717
- <td colspan="5">
718
- <label><?php _e( 'Default Image', 'mycred' ); ?></label>
719
- <input type="text" name="mycred_badge[default_image]" placeholder="<?php _e( 'image url', 'mycred' ); ?>" id="mycred-default-image-url" class="regular-text" size="30" value="<?php echo $default_image; ?>" />
720
- <input type="button" data-target="default-image" id="mycred-add-default-image" class="button button-primary mycred-badge-load-image" value="<?php _e( 'Add Image', 'mycred' ); ?>" />
721
- <p><span class="description"><?php _e( 'Optional image to show when a user has not yet earned this badge.', 'mycred' ); ?></span></p>
722
- </td>
723
- </tr>
724
- <?php
725
 
726
- foreach ( $requirements as $row => $needs ) {
727
 
728
- // First row
729
- if ( $row == 0 ) {
 
730
 
731
- if ( ! isset( $needs['by'] ) )
732
- $needs = array(
733
- 'type' => '',
734
- 'reference' => '',
735
- 'amount' => '',
736
- 'by' => ''
737
- );
738
 
739
- ?>
740
- <tr class="badge-requires" id="badge-requirement-<?php echo $row; ?>">
741
- <td class="level"><?php printf( __( 'Level %d', 'mycred' ), $row + 1 ); ?></td>
742
- <td class="type">
743
- <?php if ( count( $this->point_types ) == 1 ) echo $this->core->plural(); ?>
744
- <?php mycred_types_select_from_dropdown( 'mycred_badge[req][' . $row . '][type]', 'default-badge-req-type', $needs['type'] ); ?>
745
- </td>
746
- <td class="for"><?php _e( 'for', 'mycred' ); ?></td>
747
- <td class="reference">
748
- <select name="mycred_badge[req][<?php echo $row; ?>][reference]" id="default-badge-req-reference">
749
- <?php
750
 
751
- foreach ( $references as $ref => $label ) {
752
- echo '<option value="' . $ref . '"';
753
- if ( $needs['reference'] == $ref ) echo ' selected="selected"';
754
- echo '>' . $label . '</option>';
755
- }
756
 
757
- ?></select>
758
- </td>
759
- <td class="amount">
760
- <input type="text" size="8" name="mycred_badge[req][<?php echo $row; ?>][amount]" id="default-badge-req-amount" value="<?php echo $needs['amount']; ?>" />
761
- </td>
762
- <td class="sum">
763
- <select name="mycred_badge[req][<?php echo $row; ?>][by]" id="default-badge-req-by">
764
- <?php
765
 
766
- foreach ( $sums as $sum => $label ) {
767
- echo '<option value="' . $sum . '"';
768
- if ( $needs['by'] == $sum ) echo ' selected="selected"';
769
- echo '>' . $label . '</option>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
770
  }
771
 
772
- ?></select>
773
- </td>
774
- </tr>
775
- <tr class="bodered-row">
776
- <td class="level"><div class="inner-box"><div class="thumb"><?php if ( $main_image != '' ) echo '<img src="' . $main_image . '" alt="" />'; ?></div></div></td>
777
- <td colspan="5">
778
- <label><?php _e( 'Main Image', 'mycred' ); ?></label>
779
- <input type="text" name="mycred_badge[images][<?php echo $row; ?>]" placeholder="<?php _e( 'image url', 'mycred' ); ?>" id="mycred-badge-level<?php echo $row; ?>" class="regular-text" size="30" value="<?php echo $main_image; ?>" />
780
- <input type="button" data-target="mycred-badge-level<?php echo $row; ?>" data-row="<?php echo $row; ?>" class="button button-primary mycred-badge-load-image" value="<?php _e( 'Add Image', 'mycred' ); ?>" />
781
- </td>
782
- </tr>
783
- <?php
 
784
 
785
- }
786
- else {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
787
 
788
- $default = $requirements[0];
789
- $level_image = get_post_meta( $post->ID, 'level_image' . $row, true );
790
 
791
- ?>
792
- <tr class="badge-requires" id="badge-requirement-<?php echo $row; ?>">
793
- <td class="level">
794
- <div><?php printf( __( 'Level %d', 'mycred' ), $row + 1 ); ?></div>
795
- </td>
796
- <td class="type">
797
- <div><?php echo $this->point_types[ $default['type'] ]; ?></div>
798
- </td>
799
- <td class="for"><?php _e( 'for', 'mycred' ); ?></td>
800
- <td class="reference"><div><?php echo $references[ $default['reference'] ]; ?></div></td>
801
- <td class="amount">
802
- <input type="text" size="8" name="mycred_badge[req][<?php echo $row; ?>][amount]" value="<?php echo $needs['amount']; ?>" />
803
- </td>
804
- <td class="sum"><div><?php echo $sums[ $default['by'] ]; ?></div></td>
805
- </tr>
806
- <tr class="bodered-row" id="mycred-badge-image<?php echo $row; ?>">
807
- <td class="level"><div class="inner-box"><div class="thumb"><?php if ( $level_image != '' ) echo '<img src="' . $level_image . '" alt="" />'; ?></div></div></td>
808
- <td colspan="5">
809
- <label><?php _e( 'Badge Image', 'mycred' ); ?></label>
810
- <input type="text" name="mycred_badge[images][<?php echo $row; ?>]" placeholder="<?php _e( 'image url', 'mycred' ); ?>" id="mycred-badge-level<?php echo $row; ?>" class="regular-text" size="30" value="<?php echo $level_image; ?>" />
811
- <input type="button" data-target="mycred-badge-level<?php echo $row; ?>" data-row="<?php echo $row; ?>" class="button button-primary mycred-badge-load-image" value="<?php _e( 'Add Image', 'mycred' ); ?>" />
812
- <p><span class="description"><?php _e( 'Leave empty if you do not want to assign a custom image for this level.', 'mycred' ); ?></span><button class="button button-secondary button-small pull-right remove-this-row"><?php _e( 'Remove this level', 'mycred' ); ?></button></p>
813
- </td>
814
- </tr>
815
- <?php
816
 
817
- }
 
 
818
 
819
- }
820
 
821
- ?>
 
 
822
 
823
- <?php do_action( 'mycred_edit_badge_after_req', $post ); ?>
824
 
825
- </table>
826
- <p><button id="add-mycred-badge-level" class="pull-right button button-secondary"><?php _e( 'Add Level', 'mycred' ); ?></button></p>
827
- <script type="text/javascript">
828
- (function($) {
829
 
830
- $( 'select#default-badge-req-type' ).change(function(){
831
-
832
- $( 'td.type div' ).empty().text( $(this).find( ':selected' ).text() );
833
-
834
- });
835
 
836
- $( '#default-badge-req-reference' ).change(function(){
837
-
838
- $( 'td.reference div' ).empty().text( $(this).find( ':selected' ).text() );
839
-
840
- });
841
 
842
- $( 'select#default-badge-req-by' ).change(function(){
843
-
844
- $( 'td.sum div' ).empty().text( $(this).find( ':selected' ).text() );
845
-
846
- });
847
 
848
- var custom_uploader;
849
 
850
- var rows = <?php echo count( $requirements ); ?>;
 
 
851
 
852
- $( 'table#setup-badge-reqs' ).on( 'click', 'button.remove-this-row', function(){
 
853
 
854
- var trrow = $(this).parent().parent().parent();
855
- trrow.prev().remove();
856
- trrow.remove();
857
- rows = rows-1;
858
 
859
- });
860
 
861
- $( '#add-mycred-badge-level' ).on( 'click', function(e){
862
- e.preventDefault();
863
 
864
- var badgetype = $( '#default-badge-req-type' );
865
- var badgeref = $( '#default-badge-req-reference' ).find( ':selected' ).text();
866
- var badgeamount = $( '#default-badge-req-amount' );
867
- var badgeby = $( '#default-badge-req-by' ).find( ':selected' ).text();
868
 
869
- var reqtemplate = '<tr class="badge-requires" id="badge-requirement-' + rows + '"><td class="level"><?php echo esc_js( esc_attr__( 'Level', 'mycred' ) ); ?> ' + ( rows + 1 ) + '</td><td class="type"><div>' + badgetype.find( ':selected' ).text() + '</div></td><td class="for"><?php echo esc_js( esc_attr__( 'for', 'mycred' ) ); ?></td><td class="reference"><div>' + badgeref + '</div></td><td class="amount"><input type="text" size="8" name="mycred_badge[req][' + rows + '][amount]" value="" /></td><td class="sum"><div>' + badgeby + '</div></td></tr><tr class="bodered-row"><td class="level"><div class="inner-box"><div class="thumb"></div></div></td><td colspan="5"><label><?php echo esc_js( esc_attr__( 'Badge Image', 'mycred' ) ); ?></label><input type="text" name="mycred_badge[images][' + rows + ']" placeholder="<?php echo esc_js( esc_attr__( 'image url', 'mycred' ) ); ?>" id="mycred-badge-level' + rows + '" class="regular-text" size="30" value="" />&nbsp;<input type="button" data-target="mycred-badge-level' + rows + '" data-row="' + rows + '" class="button button-primary mycred-badge-load-image" value="<?php echo esc_js( esc_attr__( 'Add Image', 'mycred' ) ); ?>" /><p><span class="description"><?php echo esc_js( esc_attr__( 'Leave empty if you do not want to assign a custom image for this level.', 'mycred' ) ); ?></span><br /><button class="button button-secondary button-small pull-right remove-this-row"><?php echo esc_js( esc_attr__( 'Remove this level', 'mycred' ) ); ?></button></p></td></tr>';
870
 
871
- $( 'table#setup-badge-reqs' ).append( reqtemplate );
872
- rows = rows+1;
873
 
874
- });
 
875
 
876
- $( 'table#setup-badge-reqs' ).on( 'click', '.mycred-badge-load-image', function(e){
877
- e.preventDefault();
 
 
 
 
878
 
879
- var imagerow = $(this).attr( 'data-target' );
880
- var button = $(this);
 
 
881
 
882
- //Extend the wp.media object
883
- custom_uploader = wp.media.frames.file_frame = wp.media({
884
- title : '<?php echo esc_js( esc_attr__( 'Badge Image', 'mycred' ) ); ?>',
885
- button : {
886
- text : '<?php echo esc_js( esc_attr__( 'Use as Badge', 'mycred' ) ); ?>'
887
- },
888
- multiple : false
889
- });
890
 
891
- //When a file is selected, grab the URL and set it as the text field's value
892
- custom_uploader.on( 'select', function(){
893
- attachment = custom_uploader.state().get('selection').first().toJSON();
894
- if ( attachment.url != '' ) {
895
- console.log( attachment );
896
- button.prev().val( attachment.url );
897
- button.parent().prev().find( 'div.thumb' ).empty().append( "<img class='show-selected-image' src='" + attachment.url + "' alt='' />" );
898
- }
899
- });
900
 
901
- //Open the uploader dialog
902
- custom_uploader.open();
903
 
904
- });
905
 
906
- })( jQuery );
 
 
 
 
 
907
  </script>
908
- <div class="clear clearfix"></div>
909
  <?php
910
 
911
  }
912
 
913
  /**
914
  * Save Badge Details
915
- * @version 1.0
 
916
  */
917
- public function save_badge_post( $post_id ) {
918
 
919
- // Make sure this is for badges
920
- if ( ! isset( $_POST['mycred_badge'] ) || ! isset( $_POST['mycred-badge-edit'] ) || ! wp_verify_nonce( $_POST['mycred-badge-edit'], 'edit-mycred-badge' ) ) return;
921
 
922
- if ( isset( $_POST['mycred_badge']['req'][0] ) ) {
 
 
923
 
924
- $requirements = array();
925
- $base_requirements = $_POST['mycred_badge']['req'][0];
926
- $requirements[0] = $base_requirements;
927
 
928
- if ( count( $_POST['mycred_badge']['req'] ) > 1 ) {
929
- foreach ( $_POST['mycred_badge']['req'] as $row => $req ) {
930
 
931
- if ( $row == 0 || $req['amount'] == '' ) continue;
 
932
 
933
- $requirements[ $row ] = array(
934
- 'type' => $base_requirements['type'],
935
- 'reference' => $base_requirements['reference'],
936
- 'amount' => $req['amount'],
937
- 'by' => $base_requirements['by']
938
- );
939
 
 
 
 
 
 
 
 
940
  }
941
- }
942
 
943
- }
944
 
945
- // Requirements
946
- update_post_meta( $post_id, 'badge_requirements', $requirements );
 
 
947
 
948
- // Default Image
949
- $default_image = sanitize_text_field( $_POST['mycred_badge']['default_image'] );
950
- update_post_meta( $post_id, 'default_image', $default_image );
951
 
952
- if ( isset( $_POST['mycred_badge']['images'] ) || count( $_POST['mycred_badge']['images'] ) > 0 ) {
953
 
954
- // Main Image
955
- $main_image = sanitize_text_field( $_POST['mycred_badge']['images'][0] );
956
- update_post_meta( $post_id, 'main_image', $main_image );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
957
 
958
- global $wpdb;
959
 
960
- $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key LIKE %s;", $post_id, 'level_image%' ) );
961
 
962
- if ( count( $_POST['mycred_badge']['images'] ) > 1 ) {
963
- foreach ( $_POST['mycred_badge']['images'] as $row => $image ) {
964
 
965
- if ( $row == 0 || $image == '' ) continue;
 
 
 
966
 
967
- update_post_meta( $post_id, 'level_image' . $row, $image );
968
 
969
  }
 
 
 
 
 
 
970
  }
971
 
972
  }
973
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
974
  // Let others play
975
  do_action( 'mycred_save_badge', $post_id );
976
 
@@ -978,6 +1113,7 @@ jQuery(function($) {
978
 
979
  /**
980
  * Add to General Settings
 
981
  * @version 1.0.1
982
  */
983
  public function after_general_settings( $mycred = NULL ) {
@@ -995,7 +1131,7 @@ jQuery(function($) {
995
  if ( ! $buddypress && ! $bbpress ) return;
996
 
997
  ?>
998
- <h4><div class="icon icon-hook icon-active"></div><label>Badges</label></h4>
999
  <div class="body" style="display:none;">
1000
 
1001
  <?php if ( $buddypress ) : ?>
@@ -1077,30 +1213,142 @@ jQuery(function($) {
1077
 
1078
  /**
1079
  * Save Settings
1080
- * @version 1.0.1
 
1081
  */
1082
  public function sanitize_extra_settings( $new_data, $data, $core ) {
1083
 
1084
  $new_data['badges']['show_all_bp'] = ( isset( $data['badges']['show_all_bp'] ) ) ? $data['badges']['show_all_bp'] : 0;
1085
  $new_data['badges']['show_all_bb'] = ( isset( $data['badges']['show_all_bb'] ) ) ? $data['badges']['show_all_bb'] : 0;
1086
 
1087
- $new_data['badges']['buddypress'] = sanitize_text_field( $data['badges']['buddypress'] );
1088
- $new_data['badges']['bbpress'] = sanitize_text_field( $data['badges']['bbpress'] );
1089
 
1090
  return $new_data;
1091
 
1092
  }
1093
 
1094
  /**
1095
- * User Screen Header
 
1096
  * @version 1.0
1097
  */
1098
- public function badge_user_screen_header() {
1099
 
1100
- if ( isset( $_POST['mycred_badge_manual']['token'] ) && isset( $_GET['user_id'] ) ) {
 
1101
 
1102
- $user_id = absint( $_GET['user_id'] );
1103
- if ( wp_verify_nonce( $_POST['mycred_badge_manual']['token'], 'mycred-adjust-users-badge' . $user_id ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1104
 
1105
  $added = $removed = $updated = 0;
1106
  $users_badges = mycred_get_users_badges( $user_id );
@@ -1113,7 +1361,7 @@ jQuery(function($) {
1113
  if ( isset( $badge['level'] ) && $badge['level'] != '' )
1114
  $level = absint( $badge['level'] );
1115
 
1116
- update_user_meta( $user_id, 'mycred_badge' . $badge_id, $level );
1117
  $added ++;
1118
  }
1119
 
@@ -1125,7 +1373,7 @@ jQuery(function($) {
1125
 
1126
  // Level change
1127
  elseif ( array_key_exists( $badge_id, $users_badges ) && isset( $badge['level'] ) && $badge['level'] != $users_badges[ $badge_id ] ) {
1128
- update_user_meta( $user_id, 'mycred_badge' . $badge_id, absint( $badge['level'] ) );
1129
  $updated ++;
1130
  }
1131
 
@@ -1139,124 +1387,140 @@ jQuery(function($) {
1139
  }
1140
 
1141
  /**
1142
- * User Badges Admin Screen
1143
- * @version 1.0
 
1144
  */
1145
- public function badge_user_screen() {
1146
-
1147
- global $mycred_manual_badges;
1148
-
1149
- $user_id = absint( $_GET['user_id'] );
1150
- $user = get_userdata( $user_id );
1151
 
1152
- global $bp;
1153
 
1154
- $mycred_admin = new myCRED_Admin();
1155
 
1156
- if ( is_object( $bp ) && isset( $bp->version ) && version_compare( $bp->version, '2.0', '>=' ) && bp_is_active( 'xprofile' ) )
1157
- $mycred_admin->using_bp = true;
1158
 
1159
- $all_badges = mycred_get_badge_ids();
1160
- $users_badges = mycred_get_users_badges( $user_id );
1161
 
1162
- ?>
1163
- <div class="wrap" id="edit-badges-page">
1164
- <h2><?php _e( 'User Badges', 'mycred' ); ?></h2>
1165
 
1166
- <?php if ( isset( $_POST['mycred_badge_manual'] ) ) echo '<div class="updated"><p>Badges successfully updated.</p></div>'; ?>
1167
 
1168
- <form id="your-profile" action="" method="post">
1169
 
1170
- <?php $mycred_admin->user_nav( $user, 'badges' ); ?>
 
 
 
 
 
1171
 
1172
- <div class="clear clearfix"></div>
1173
- <p><?php _e( 'Here you can view the badges this user has earned and if needed, manually give or take away a badge from a user.', 'mycred' ); ?></p>
1174
- <div id="badge-wrapper">
1175
- <?php
1176
 
1177
- if ( ! empty( $all_badges ) ) {
1178
- foreach ( $all_badges as $badge_id ) {
1179
 
1180
- $badge_id = absint( $badge_id );
1181
- $earned = $level = 0;
1182
- $status = '<span class="not-earned">' . __( 'Not earned', 'mycred' ) . '</span>';
1183
- $image = get_post_meta( $badge_id, 'default_image', true );
1184
- $requirements = mycred_get_badge_requirements( $badge_id );
1185
 
1186
- if ( array_key_exists( $badge_id, $users_badges ) ) {
 
 
 
 
 
1187
 
1188
- $earned = 1;
1189
- $status = '<span class="earned">' . __( 'Earned', 'mycred' ) . '</span>';
1190
- $level = $users_badges[ $badge_id ];
1191
- $image = get_post_meta( $badge_id, 'level_image' . $level, true );
1192
 
1193
- if ( $image == '' )
1194
- $image = get_post_meta( $badge_id, 'main_image', true );
1195
 
1196
- }
1197
 
1198
- if ( $image != '' )
1199
- $image = '<img src="' . $image . '" alt="" />';
1200
- else
1201
- $image = '<span>' . __( 'No image', 'mycred' ) . '</span>';
 
 
1202
 
1203
- $level_select = '<input type="hidden" name="mycred_badge_manual[badges][' . $badge_id . '][level]" value="" />';
1204
- if ( count( $requirements ) > 1 ) {
 
 
 
 
 
 
1205
 
1206
- $level_select = '<li><select name="mycred_badge_manual[badges][' . $badge_id . '][level]">';
1207
- $level_select .= '<option value=""';
1208
- if ( ! $earned ) $level_select .= ' selected="selected"';
1209
- $level_select .= '>' . __( 'Select a level', 'mycred' ) . '</option>';
1210
 
1211
- foreach ( $requirements as $l => $needs ) {
1212
- $level_select .= '<option value="' . $l . '"';
1213
- if ( $earned && $level == $l ) $level_select .= ' selected="selected"';
1214
- $level_select .= '>' . __( 'Level', 'mycred' ) . ' ' . ( $l + 1 ) . '</option>';
1215
- }
1216
 
1217
- $level_select .= '</select></li>';
 
 
 
 
 
1218
 
1219
- }
 
1220
 
1221
- ?>
1222
- <div class="the-badge">
1223
- <div class="badge-image-wrap">
 
 
 
 
1224
 
1225
- <?php echo $image; ?>
 
1226
 
1227
- </div>
1228
- <h4><?php echo get_the_title( $badge_id ); ?></h4>
1229
- <div class="badge-status"><label><?php _e( 'Status', 'mycred' ); ?></label><?php echo $status; ?></div>
1230
- <div class="badge-actions">
1231
- <ul>
1232
- <li><label><input type="checkbox" name="mycred_badge_manual[badges][<?php echo $badge_id; ?>][has]" <?php checked( $earned, 1 );?> value="1" /> <?php _e( 'Earned', 'mycred' ); ?></label></li>
1233
 
1234
- <?php echo $level_select; ?>
1235
 
1236
- </ul>
1237
- </div>
1238
- </div>
1239
- <?php
 
 
1240
 
1241
- }
1242
- }
 
 
 
 
 
 
1243
 
1244
- ?>
1245
- <div class="clear clearfix"></div>
1246
- </div>
1247
- <input type="hidden" name="mycred_badge_manual[token]" value="<?php echo wp_create_nonce( 'mycred-adjust-users-badge' . $user_id ); ?>" />
1248
- <p><input type="submit" class="button button-primary" value="<?php _e( 'Save Changes', 'mycred' ); ?>" /></p>
1249
- </form>
1250
- </div>
1251
- <?php
1252
 
1253
  }
1254
 
1255
  }
 
1256
 
1257
- $badge = new myCRED_Badge_Module();
1258
- $badge->load();
 
 
 
 
 
 
 
 
1259
 
 
 
 
1260
  endif;
 
1261
 
1262
  ?>
2
  /**
3
  * Addon: Badges
4
  * Addon URI: http://mycred.me/add-ons/badges/
5
+ * Version: 1.2
 
 
 
6
  */
7
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
 
9
+ define( 'myCRED_BADGE', __FILE__ );
10
+ define( 'myCRED_BADGE_VERSION', '1.2' );
11
+ define( 'MYCRED_BADGE_DIR', myCRED_ADDONS_DIR . 'badges/' );
12
+ define( 'MYCRED_BADGE_INCLUDES_DIR', MYCRED_BADGE_DIR . 'includes/' );
13
 
14
  // Default badge width
15
  if ( ! defined( 'MYCRED_BADGE_WIDTH' ) )
19
  if ( ! defined( 'MYCRED_BADGE_HEIGHT' ) )
20
  define( 'MYCRED_BADGE_HEIGHT', 100 );
21
 
22
+ require_once MYCRED_BADGE_INCLUDES_DIR . 'mycred-badge-functions.php';
23
+ require_once MYCRED_BADGE_INCLUDES_DIR . 'mycred-badge-shortcodes.php';
24
+ require_once MYCRED_BADGE_INCLUDES_DIR . 'mycred-badge-object.php';
25
 
26
  /**
27
  * myCRED_buyCRED_Module class
28
  * @since 1.5
29
+ * @version 1.2
30
  */
31
  if ( ! class_exists( 'myCRED_Badge_Module' ) ) :
32
  class myCRED_Badge_Module extends myCRED_Module {
34
  /**
35
  * Construct
36
  */
37
+ function __construct( $type = MYCRED_DEFAULT_TYPE_KEY ) {
38
 
39
  parent::__construct( 'myCRED_Badge_Module', array(
40
  'module_name' => 'badges',
44
  'show_all_bp' => 0,
45
  'show_all_bb' => 0
46
  ),
 
 
 
 
 
47
  'add_to_core' => true,
48
  'register' => false,
49
+ 'menu_pos' => 50
50
  ), $type );
51
 
52
  }
53
 
54
  /**
55
  * Module Pre Init
56
+ * @since 1.0
57
  * @version 1.0
58
  */
59
  public function module_pre_init() {
60
 
61
  add_filter( 'mycred_add_finished', array( $this, 'add_finished' ), 30, 3 );
62
+ //add_filter( 'mycred_get_account', array( $this, 'set_globals' ), 30, 3 );
63
 
64
  }
65
 
66
  /**
67
  * Module Init
68
+ * @since 1.0
69
+ * @version 1.0.3
70
  */
71
  public function module_init() {
72
 
73
+ $this->register_badges();
74
 
75
  add_shortcode( 'mycred_my_badges', 'mycred_render_my_badges' );
76
  add_shortcode( 'mycred_badges', 'mycred_render_badges' );
77
 
 
 
 
 
 
 
 
 
78
  // Insert into bbPress
79
  if ( class_exists( 'bbPress' ) ) {
80
 
99
 
100
  }
101
 
102
+ add_action( 'mycred_add_menu', array( $this, 'add_to_menu' ), $this->menu_pos );
103
+
104
  }
105
 
106
  /**
107
  * Module Admin Init
108
+ * @since 1.0
109
  * @version 1.0
110
  */
111
  public function module_admin_init() {
112
 
113
+ add_filter( 'parent_file', array( $this, 'parent_file' ) );
114
+ add_filter( 'submenu_file', array( $this, 'subparent_file' ), 10, 2 );
115
  add_action( 'mycred_admin_enqueue', array( $this, 'enqueue_scripts' ) );
116
 
117
  add_filter( 'manage_mycred_badge_posts_columns', array( $this, 'adjust_column_headers' ) );
121
 
122
  add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
123
  add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
 
 
124
  add_action( 'post_submitbox_start', array( $this, 'publishing_actions' ) );
125
+ add_action( 'save_post_mycred_badge', array( $this, 'save_badge' ), 10, 2 );
126
 
127
  add_action( 'wp_ajax_mycred-assign-badge', array( $this, 'action_assign_badge' ) );
128
  add_action( 'wp_ajax_mycred-remove-connections', array( $this, 'action_remove_connections' ) );
129
 
130
+ add_action( 'mycred_user_edit_after_balances', array( $this, 'badge_user_screen' ), 10 );
131
 
132
+ add_action( 'personal_options_update', array( $this, 'save_manual_badges' ), 10 );
133
+ add_action( 'edit_user_profile_update', array( $this, 'save_manual_badges' ), 10 );
 
 
 
134
 
135
+ add_action( 'mycred_delete_point_type', array( $this, 'delete_point_type' ) );
136
+ add_action( 'before_delete_post', array( $this, 'delete_badge' ) );
137
 
138
  }
139
 
140
  /**
141
+ * Register Badge Post Type
142
+ * @since 1.0
143
  * @version 1.0
144
  */
145
+ public function register_badges() {
146
 
147
+ $labels = array(
148
+ 'name' => __( 'Badges', 'mycred' ),
149
+ 'singular_name' => __( 'Badge', 'mycred' ),
150
+ 'add_new' => __( 'Add New', 'mycred' ),
151
+ 'add_new_item' => __( 'Add New', 'mycred' ),
152
+ 'edit_item' => __( 'Edit Badge', 'mycred' ),
153
+ 'new_item' => __( 'New Badge', 'mycred' ),
154
+ 'all_items' => __( 'Badges', 'mycred' ),
155
+ 'view_item' => __( 'View Badge', 'mycred' ),
156
+ 'search_items' => __( 'Search Badge', 'mycred' ),
157
+ 'not_found' => __( 'No badges found', 'mycred' ),
158
+ 'not_found_in_trash' => __( 'No badges found in Trash', 'mycred' ),
159
+ 'parent_item_colon' => '',
160
+ 'menu_name' => __( 'Badges', 'mycred' )
161
  );
162
 
163
+ $args = array(
164
+ 'labels' => $labels,
165
+ 'supports' => array( 'title' ),
166
+ 'hierarchical' => false,
167
+ 'public' => false,
168
+ 'show_ui' => true,
169
+ 'show_in_menu' => false,
170
+ 'show_in_nav_menus' => false,
171
+ 'show_in_admin_bar' => false,
172
+ 'can_export' => true,
173
+ 'has_archive' => false,
174
+ 'exclude_from_search' => true,
175
+ 'publicly_queryable' => false,
176
+ 'register_meta_box_cb' => array( $this, 'add_metaboxes' )
 
 
 
 
 
 
 
 
177
  );
178
 
179
+ register_post_type( 'mycred_badge', apply_filters( 'mycred_register_badge', $args ) );
180
 
181
  }
182
 
183
  /**
184
+ * Set Globals
185
+ * @since 1.7
186
+ * @version 1.0
187
  */
188
+ public function set_globals( $account, $user_id = NULL, $type = '' ) {
 
 
 
 
 
 
 
 
189
 
190
+ if ( ! isset( $account->user_id ) || $account->user_id === false ) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
+ $earned = array();
193
+ $users_badges = mycred_get_users_badges( $account->user_id );
194
 
195
+ if ( ! empty( $users_badges ) ) {
196
+ foreach ( $users_badges as $badge_id => $level ) {
 
 
197
 
198
+ if ( ! is_numeric( $level ) )
199
+ $level = 0;
 
 
 
 
 
 
 
 
200
 
201
+ $badge_id = absint( $badge_id );
202
+ $level = absint( $level );
203
+ $badge = mycred_get_badge( $badge_id, $level );
204
 
205
+ $earned[ $badge_id ] = $badge;
206
 
207
  }
 
208
  }
209
 
210
+ $account->badge = $earned;
211
 
212
+ return $account;
 
213
 
214
+ }
 
 
 
 
 
215
 
216
+ /**
217
+ * Delete Point Type
218
+ * When a point type is deleted, we want to remove any data saved for this point type.
219
+ * @since 1.7
220
+ * @version 1.0
221
+ */
222
+ public function delete_point_type( $point_type = NULL ) {
223
 
224
+ if ( ! mycred_point_type_exists( $point_type ) || $point_type == MYCRED_DEFAULT_TYPE_KEY ) return;
225
 
226
+ $mycred = mycred( $point_type );
 
227
 
228
+ if ( ! current_user_can( $mycred->edit_plugin_cap() ) ) return;
229
 
230
+ mycred_delete_option( 'mycred-badge-refs-' . $point_type );
231
 
232
  }
233
 
234
  /**
235
+ * Delete Badge
236
+ * When a badge is deleted, we want to delete connections as well.
237
+ * @since 1.7
238
+ * @version 1.0
239
  */
240
+ public function delete_badge( $post_id ) {
241
 
242
+ if ( get_post_status( $post_id ) != 'mycred_badge' ) return $post_id;
243
 
244
+ // Delete reference list to force a new query
245
+ foreach ( $this->point_types as $type_id => $label )
246
+ mycred_delete_option( 'mycred-badge-refs-' . $type_id );
247
 
248
  global $wpdb;
249
 
250
+ // Delete connections to keep usermeta table clean
251
+ $wpdb->delete(
252
  $wpdb->usermeta,
253
+ array( 'meta_key' => 'mycred_badge' . $post_id ),
254
  array( '%s' )
255
  );
256
 
 
 
 
 
 
257
  }
258
 
259
  /**
260
+ * Adjust Post Updated Messages
261
+ * @since 1.0
262
+ * @version 1.0
263
  */
264
+ public function post_updated_messages( $messages ) {
265
 
266
+ global $post;
 
 
 
 
 
 
 
267
 
268
+ $messages['mycred_badge'] = array(
269
+ 0 => '',
270
+ 1 => __( 'Badge Updated.', 'mycred' ),
271
+ 2 => __( 'Badge Updated.', 'mycred' ),
272
+ 3 => __( 'Badge Updated.', 'mycred' ),
273
+ 4 => __( 'Badge Updated.', 'mycred' ),
274
+ 5 => false,
275
+ 6 => __( 'Badge Enabled.', 'mycred' ),
276
+ 7 => __( 'Badge Saved.', 'mycred' ),
277
+ 8 => __( 'Badge Updated.', 'mycred' ),
278
+ 9 => __( 'Badge Updated.', 'mycred' ),
279
+ 10 => __( 'Badge Updated.', 'mycred' )
280
+ );
281
+
282
+ return $messages;
283
 
284
  }
285
 
286
  /**
287
+ * Add Admin Menu Item
288
+ * @since 1.7
289
+ * @version 1.0
290
  */
291
+ public function add_to_menu() {
292
 
293
+ add_submenu_page(
294
+ 'mycred',
295
+ __( 'Badges', 'mycred' ),
296
+ __( 'Badges', 'mycred' ),
297
+ $this->core->edit_creds_cap(),
298
+ 'edit.php?post_type=mycred_badge'
299
+ );
300
 
301
+ }
 
 
 
 
 
 
302
 
303
+ /**
304
+ * Parent File
305
+ * @since 1.6
306
+ * @version 1.0.1
307
+ */
308
+ public function parent_file( $parent = '' ) {
309
+
310
+ global $pagenow;
311
 
312
+ if ( ( $pagenow == 'edit.php' || $pagenow == 'post-new.php' ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_badge' ) {
313
+
314
+ return 'mycred';
315
+
316
  }
317
 
318
+ elseif ( $pagenow == 'post.php' && isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'mycred_badge' ) {
319
 
320
+ return 'mycred';
 
 
 
 
321
 
322
+ }
 
 
 
 
 
 
 
323
 
324
+ return $parent;
 
325
 
326
  }
327
 
328
  /**
329
+ * Sub Parent File
330
+ * @since 1.7
331
+ * @version 1.0
332
  */
333
+ public function subparent_file( $subparent = '', $parent = '' ) {
334
 
335
+ global $pagenow;
336
 
337
+ if ( ( $pagenow == 'edit.php' || $pagenow == 'post-new.php' ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_badge' ) {
 
 
338
 
339
+ return 'edit.php?post_type=mycred_badge';
340
+
341
+ }
342
 
343
+ elseif ( $pagenow == 'post.php' && isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'mycred_badge' ) {
344
+
345
+ return 'edit.php?post_type=mycred_badge';
346
 
347
  }
348
 
349
+ return $subparent;
350
 
351
  }
352
 
353
  /**
354
+ * Add Finished
355
+ * @since 1.0
356
+ * @version 1.2
357
  */
358
+ public function add_finished( $result, $request, $mycred ) {
359
 
360
+ extract( $request );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
 
362
+ if ( $result !== false && $ref != 'badge_reward' ) {
 
 
 
 
 
 
 
363
 
364
+ // Check if this reference has badges
365
+ $badge_ids = mycred_ref_has_badge( $ref, $type );
366
+ if ( $badge_ids !== false ) {
367
 
368
+ // Check if user gets any of the badges
369
+ foreach ( $badge_ids as $badge_id ) {
370
 
371
+ $level_reached = mycred_badge_level_reached( $user_id, $badge_id );
372
+ if ( $level_reached !== false )
373
+ mycred_assign_badge_to_user( $user_id, $badge_id, $level_reached );
 
 
374
 
375
+ }
376
+
377
+ }
378
 
379
+ }
 
380
 
381
+ return $result;
 
382
 
383
  }
384
 
385
  /**
386
  * Adjust Badge Column Header
387
+ * @since 1.0
388
  * @version 1.0
389
  */
390
  public function adjust_column_headers( $defaults ) {
391
 
392
+ $columns = array();
393
+ $columns['cb'] = $defaults['cb'];
394
 
395
  // Add / Adjust
396
  $columns['title'] = __( 'Badge Name', 'mycred' );
397
+ $columns['badge-default-image'] = __( 'Default Image', 'mycred' );
398
+ $columns['badge-earned-image'] = __( 'First Level', 'mycred' );
399
  $columns['badge-reqs'] = __( 'Requirements', 'mycred' );
400
  $columns['badge-users'] = __( 'Users', 'mycred' );
401
 
406
 
407
  /**
408
  * Adjust Badge Column Content
409
+ * @since 1.0
410
+ * @version 1.1
411
  */
412
+ public function adjust_column_content( $column_name, $badge_id ) {
413
 
414
+ // Default Images
415
  if ( $column_name == 'badge-default-image' ) {
416
 
417
+ $badge = mycred_get_badge( $badge_id );
418
+ if ( $badge->post_id === false )
419
+ echo '-';
420
+
421
+ elseif ( $badge->main_image !== false )
422
+ echo $badge->main_image;
423
+
424
+ }
425
 
426
+ // First Level Image
427
+ if ( $column_name == 'badge-earned-image' ) {
 
428
 
429
+ $badge = mycred_get_badge( $badge_id, 0 );
430
+ if ( $badge->post_id === false )
431
  echo '-';
432
 
433
+ elseif ( $badge->level_image !== false )
434
+ echo $badge->level_image;
 
 
 
 
 
 
435
 
436
  }
437
 
438
  // Badge Requirements
439
  elseif ( $column_name == 'badge-reqs' ) {
440
 
441
+ echo mycred_display_badge_requirements( $badge_id );
 
442
 
443
  }
444
 
445
  // Badge Users
446
  elseif ( $column_name == 'badge-users' ) {
447
 
448
+ echo mycred_count_users_with_badge( $badge_id );
449
 
450
  }
451
 
453
 
454
  /**
455
  * Adjust Row Actions
456
+ * @since 1.0
457
  * @version 1.0
458
  */
459
  public function adjust_row_actions( $actions, $post ) {
468
  }
469
 
470
  /**
471
+ * Adjust Enter Title Here
472
+ * @since 1.0
473
  * @version 1.0
474
  */
475
+ public function enter_title_here( $title ) {
476
 
477
+ global $post_type;
478
 
479
+ if ( $post_type == 'mycred_badge' )
480
+ return __( 'Badge Name', 'mycred' );
 
 
 
 
 
 
 
 
 
 
 
481
 
482
+ return $title;
483
 
484
  }
485
 
486
  /**
487
+ * Enqueue Scripts
488
+ * @since 1.0
489
  * @version 1.0
490
  */
491
+ public function enqueue_scripts() {
492
 
493
+ $screen = get_current_screen();
494
+ if ( $screen->id == 'mycred_badge' ) {
495
+
496
+ wp_enqueue_media();
497
+
498
+ wp_register_script(
499
+ 'mycred-edit-badge',
500
+ plugins_url( 'assets/js/edit-badge.js', myCRED_BADGE ),
501
+ array( 'jquery', 'mycred-mustache' ),
502
+ myCRED_BADGE_VERSION . '.1'
503
+ );
504
+
505
+ wp_localize_script(
506
+ 'mycred-edit-badge',
507
+ 'myCREDBadge',
508
+ array(
509
+ 'ajaxurl' => admin_url( 'admin-ajax.php' ),
510
+ 'addlevel' => __( 'Add Level', 'mycred' ),
511
+ 'removelevel' => __( 'Remove Level', 'mycred' ),
512
+ 'setimage' => __( 'Set Image', 'mycred' ),
513
+ 'changeimage' => __( 'Change Image', 'mycred' ),
514
+ 'remove' => esc_js( esc_attr__( 'Are you sure you want to remove this level?', 'mycred' ) ),
515
+ 'levellabel' => esc_js( sprintf( '%s {{level}}', __( 'Level', 'mycred' ) ) ),
516
+ 'uploadtitle' => esc_js( esc_attr__( 'Badge Image', 'mycred' ) ),
517
+ 'uploadbutton' => esc_js( esc_attr__( 'Use as Badge', 'mycred' ) ),
518
+ 'compareAND' => _x( 'AND', 'Comparison of badge requirements. A AND B', 'mycred' ),
519
+ 'compareOR' => _x( 'OR', 'Comparison of badge requirements. A OR B', 'mycred' )
520
+ )
521
+ );
522
+
523
+ wp_enqueue_script( 'mycred-edit-badge' );
524
+
525
+ wp_enqueue_style( 'mycred-bootstrap-grid' );
526
+ wp_enqueue_style( 'mycred-forms' );
527
+
528
+ add_filter( 'postbox_classes_mycred_badge_mycred-badge-setup', array( $this, 'metabox_classes' ) );
529
+ add_filter( 'postbox_classes_mycred_badge_mycred-badge-default', array( $this, 'metabox_classes' ) );
530
+ add_filter( 'postbox_classes_mycred_badge_mycred-badge-rewards', array( $this, 'metabox_classes' ) );
531
 
532
+ ?>
533
+ <style type="text/css">
534
+ #misc-publishing-actions #visibility, #misc-publishing-actions .misc-pub-post-status { display: none; }
535
+ #save-action #save-post { margin-bottom: 12px; }
536
+ </style>
537
+ <?php
538
 
539
+ }
540
+
541
+ elseif ( $screen->id == 'edit-mycred_badge' ) {
542
+
543
+ ?>
544
+ <style type="text/css">
545
+ th#badge-default-image { width: 120px; }
546
+ th#badge-earned-image { width: 120px; }
547
+ th#badge-reqs { width: 35%; }
548
+ th#badge-users { width: 10%; }
549
+ .column-badge-default-image img { max-width: 100px; height: auto; }
550
+ .mycred-badge-requirement-list { margin: 6px 0 0 0; padding: 6px 0 0 18px; border-top: 1px dashed #aeaeae; }
551
+ .mycred-badge-requirement-list li { margin: 0 0 0 0; padding: 0 0 0 0; font-size: 12px; line-height: 16px; list-style-type: circle; }
552
+ .mycred-badge-requirement-list li span { float: right; }
553
+ .column-badge-reqs strong { display: block; }
554
+ .column-badge-reqs span { color: #aeaeae; }
555
+ </style>
556
+ <?php
557
+
558
+ }
559
 
560
  }
561
 
562
  /**
563
  * Add Meta Boxes
564
+ * @since 1.0
565
  * @version 1.0
566
  */
567
+ public function add_metaboxes() {
568
 
569
  add_meta_box(
570
+ 'mycred-badge-setup',
571
  __( 'Badge Setup', 'mycred' ),
572
+ array( $this, 'metabox_badge_setup' ),
573
  'mycred_badge',
574
  'normal',
575
  'high'
576
  );
577
 
578
+ add_meta_box(
579
+ 'mycred-badge-default',
580
+ __( 'Default Badge Image', 'mycred' ),
581
+ array( $this, 'metabox_badge_default' ),
582
+ 'mycred_badge',
583
+ 'side',
584
+ 'low'
585
+ );
586
+
587
  }
588
 
589
  /**
590
+ * Level Template
591
+ * @since 1.7
592
  * @version 1.0
593
  */
594
+ public function level_template( $level = 0 ) {
595
 
596
+ if ( $level == 0 )
597
+ return '<div class="row badge-level" id="mycred-badge-level{{level}}" data-level="{{level}}"><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">{{addlevelbutton}}<div class="level-image"><div class="level-image-wrapper image-wrapper {{emptylevelimage}}">{{levelimage}}</div><div class="level-image-actions"><button type="button" class="button button-secondary change-level-image" data-level="{{level}}">{{levelimagebutton}}</button></div></div><div class="label-field"><input type="text" placeholder="{{levelplaceholder}}" name="mycred_badge[levels][{{level}}][label]" value="{{levellabel}}" /></div></div><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><div class="req-title">{{requirementslabel}}<div class="pull-right" id="badge-requirement-compare"><a href="javascript:void(0);" data-do="AND" class="{{adnselected}}">AND</a> / <a href="javascript:void(0);" data-do="OR" class="{{orselected}}">OR</a><input type="hidden" name="mycred_badge[levels][{{level}}][compare]" value="AND" /></div></div><div class="level-requirements">{{{requirements}}}</div></div><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">{{rewards}}</div></div>';
598
 
599
+ return '<div class="row badge-level" id="mycred-badge-level{{level}}" data-level="{{level}}"><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">{{removelevelbutton}}<div class="level-image"><div class="level-image-wrapper image-wrapper {{emptylevelimage}}">{{levelimage}}</div><div class="level-image-actions"><button type="button" class="button button-secondary change-level-image" data-level="{{level}}">{{levelimagebutton}}</button></div></div><div class="label-field"><input type="text" placeholder="{{levelplaceholder}}" name="mycred_badge[levels][{{level}}][label]" value="{{levellabel}}" /></div></div><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><div class="req-title">{{requirementslabel}}</div><div class="level-requirements">{{{requirements}}}</div></div><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">{{rewards}}</div></div>';
600
 
601
+ }
602
+
603
+ /**
604
+ * Get Level Image
605
+ * @since 1.7
606
+ * @version 1.0
607
+ */
608
+ public function get_level_image( $setup, $level = 0 ) {
609
+
610
+ $image = false;
611
+
612
+ if ( $setup['attachment_id'] > 0 ) {
613
+
614
+ $_image = wp_get_attachment_url( $setup['attachment_id'] );
615
+ if ( strlen( $_image ) > 5 )
616
+ $image = '<img src="' . $_image . '" alt="Badge level image" /><input type="hidden" name="mycred_badge[levels][' . $level . '][attachment_id]" value="' . $setup['attachment_id'] . '" /><input type="hidden" name="mycred_badge[levels][' . $level . '][image_url]" value="" />';
617
+
618
+ }
619
+ else {
620
+
621
+ if ( strlen( $setup['image_url'] ) > 5 )
622
+ $image = '<img src="' . $setup['image_url'] . '" alt="Badge level image" /><input type="hidden" name="mycred_badge[levels][' . $level . '][attachment_id]" value="0" /><input type="hidden" name="mycred_badge[levels][' . $level . '][image_url]" value="' . $setup['image_url'] . '" />';
623
+
624
+ }
625
+
626
+ return $image;
627
+
628
+ }
629
+
630
+ /**
631
+ * Requirements Template
632
+ * @since 1.7
633
+ * @version 1.0
634
+ */
635
+ public function requirements_template( $level = 0 ) {
636
+
637
+ // only first level dictates requirements
638
+ if ( $level == 0 )
639
+ return '<div class="row row-narrow" id="level{{level}}requirement{{reqlevel}}" data-row="{{reqlevel}}"><div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 form"><div class="form-group"><select name="mycred_badge[levels][{{level}}][requires][{{reqlevel}}][type]" data-row="{{reqlevel}}" class="form-control point-type">{{pointtypes}}</select></div></div><div class="col-lg-5 col-md-5 col-sm-6 col-xs-12 form"><div class="form-group"><select name="mycred_badge[levels][{{level}}][requires][{{reqlevel}}][reference]" data-row="{{reqlevel}}" class="form-control reference">{{references}}</select></div></div><div class="col-lg-3 col-md-3 col-sm-6 col-xs-10 form-inline"><div class="form-group"><input type="text" size="5" name="mycred_badge[levels][{{level}}][requires][{{reqlevel}}][amount]" class="form-control" value="{{reqamount}}" /></div><div class="form-group"><select name="mycred_badge[levels][{{level}}][requires][{{reqlevel}}][by]" data-row="{{reqlevel}}" class="form-control req-type">{{requirementtype}}</select></div></div><div class="col-lg-1 col-md-1 col-sm-6 col-xs-2 form">{{reqbutton}}</div></div>';
640
+
641
+ // All other requirements reflect the level 0's setup
642
+ return '<div class="row row-narrow" id="level{{level}}requirement{{reqlevel}}"><div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 form"><div class="form-group level-type"><p class="form-control-static level-requirement{{reqlevel}}-type">{{selectedtype}}</p></div></div><div class="col-lg-5 col-md-5 col-sm-6 col-xs-12 form"><div class="form-group level-ref"><p class="form-control-static level-requirement{{reqlevel}}-ref">{{selectedref}}</p></div></div><div class="col-lg-3 col-md-3 col-sm-6 col-xs-10 form-inline"><div class="form-group level-val"><input type="text" size="5" name="mycred_badge[levels][{{level}}][requires][{{reqlevel}}][amount]" class="form-control" value="{{reqamount}}" /></div><div class="form-group level-type-by"><p class="form-control-static level-requirement{{reqlevel}}-by">{{selectedby}}</p></div></div><div class="col-lg-1 col-md-1 col-sm-6 col-xs-2 level-compare form"><p class="form-control-static" data-row="{{reqlevel}}">{{comparelabel}}</p></div></div>';
643
+
644
+ }
645
+
646
+ /**
647
+ * Rewards Template
648
+ * @since 1.7
649
+ * @version 1.0
650
+ */
651
+ public function rewards_template() {
652
+
653
+ return '<div class="req-title">{{rewardlabel}}</div><div class="row form"><div class="col-lg-4 col-md-4 col-sm-12 col-xs-12"><select name="mycred_badge[levels][{{level}}][reward][type]" class="form-control">{{pointtypes}}</select></div><div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"><input type="text" class="form-control" name="mycred_badge[levels][{{level}}][reward][log]" placeholder="{{logplaceholder}}" value="{{logtemplate}}" /></div><div class="col-lg-2 col-md-2 col-sm-12 col-xs-12"><input type="text" class="form-control" name="mycred_badge[levels][{{level}}][reward][amount]" placeholder="0" value="{{rewardamount}}" /></div></div>';
654
+
655
+ }
656
+
657
+ /**
658
+ * Badge Publishing Actions
659
+ * @since 1.7
660
+ * @version 1.0
661
+ */
662
+ public function publishing_actions() {
663
+
664
+ global $post;
665
+
666
+ if ( ! isset( $post->post_type ) || $post->post_type != 'mycred_badge' ) return;
667
+
668
+ $lock = '';
669
+ if ( $post->post_status != 'publish' )
670
  $lock = ' disabled="disabled"';
671
 
672
  ?>
673
+ <div id="mycred-badge-actions" class="seperate-bottom">
674
 
675
  <?php do_action( 'mycred_edit_badge_before_actions', $post ); ?>
676
 
718
  <?php endif; ?>
719
 
720
  </div>
721
+ <div id="mycred-manual-badge" class="seperate-bottom">
722
+ <label for="mycred-badge-is-manual"><input type="checkbox" name="mycred_badge[manual]" id="mycred-badge-is-manual"<?php checked( (int) get_post_meta( $post->ID, 'manual_badge', true ), 1 ); ?> value="1" /> <?php _e( 'This badge is manually awarded.', 'mycred' ); ?></label>
723
+ </div>
724
  <?php
725
 
726
  }
727
 
728
  /**
729
+ * Default Image Metabox
730
+ * @since 1.7
731
+ * @version 1.0
732
  */
733
+ public function metabox_badge_default( $post ) {
734
 
735
+ $default_image = $di = get_post_meta( $post->ID, 'main_image', true );
736
+ if ( $default_image != '' )
737
+ $default_image = '<img src="' . $default_image . '" alt="" />';
738
 
739
+ $attachment = false;
740
+ if ( is_numeric( $di ) && strpos( '://', $di ) === false ) {
741
+ $attachment = $di;
742
+ $default_image = '<img src="' . wp_get_attachment_url( $di ) . '" alt="" />';
743
+ }
744
 
745
+ ?>
746
+ <div class="row">
747
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
748
+ <div class="default-image text-center seperate-bottom">
749
+ <div class="default-image-wrapper image-wrapper<?php if ( $default_image == '' ) echo ' empty dashicons'; ?>">
750
+ <?php echo $default_image; ?>
751
+ <input type="hidden" name="mycred_badge[main_image]" id="badge-main-image-id" value="<?php if ( $attachment ) echo esc_attr( $di ); ?>" />
752
+ <input type="hidden" name="mycred_badge[main_image_url]" id="badge-main-image-url" value="<?php if ( $di != '' && strpos( '://', $di ) !== false ) echo esc_attr( $default_image ); ?>" />
753
+ </div>
754
+ <div class="level-image-actions">
755
+ <button type="button" class="button button-secondary" id="badges-change-default-image" data-do="<?php if ( $default_image == '' ) echo 'set'; else echo 'change'; ?>"><?php if ( $default_image == '' ) _e( 'Set Image', 'mycred' ); else _e( 'Change Image', 'mycred' ); ?></button>
756
+ </div>
757
+ </div>
758
+ <span class="description"><?php _e( 'Optional image to show when a user has not earned this badge.', 'mycred' ); ?></span>
759
+ </div>
760
+ </div>
761
+ <?php
762
+
763
+ }
764
+
765
+ /**
766
+ * Badge Setup Metabox
767
+ * @since 1.7
768
+ * @version 1.1
769
+ */
770
+ public function metabox_badge_setup( $post ) {
771
+
772
+ $badge = mycred_get_badge( $post->ID );
773
+ $references = mycred_get_all_references();
774
+ $point_types = mycred_get_types( true );
775
+
776
+ $sums = apply_filters( 'mycred_badge_requirement_sums', array(
777
  'count' => __( 'Time(s)', 'mycred' ),
778
  'sum' => __( 'In total', 'mycred' )
779
+ ), $badge );
780
+
781
+ // Badge rewards can no be used as a requirement
782
+ if ( array_key_exists( 'badge_reward', $references ) )
783
+ unset( $references['badge_reward'] );
784
+
785
+ global $mycred_account;
786
+
787
+ $js_level = $this->level_template( 1 );
788
+ $js_requirement = $this->requirements_template( 0 );
789
+ $js_requirement_clone = $this->requirements_template( 1 );
790
 
791
  ?>
792
+ <div id="badge-levels">
793
+ <?php
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
794
 
795
+ // Loop through each badge level
796
+ $level_counter = 0;
797
+ foreach ( $badge->levels as $level => $setup ) {
798
 
799
+ $level = $level_counter;
 
 
800
 
801
+ $add_level = '<button type="button" class="button button-seconary button-small top-right-corner" id="badges-add-new-level">' . __( 'Add Level', 'mycred' ) . '</button>';
802
+ $remove_level = '<button type="button" class="button button-seconary button-small top-right-corner remove-badge-level" data-level="' . $level . '">' . __( 'Remove Level', 'mycred' ) . '</button>';
803
 
804
+ $level_image = $this->get_level_image( $setup, $level );
805
+ $empty_level = 'empty dashicons';
806
+ if ( $level_image !== false )
807
+ $empty_level = '';
 
 
 
 
 
 
808
 
809
+ $template = $this->level_template( $level );
810
 
811
+ $template = str_replace( '{{level}}', $level, $template );
812
+ $template = str_replace( '{{addlevelbutton}}', $add_level, $template );
813
+ $template = str_replace( '{{removelevelbutton}}', $remove_level, $template );
814
 
815
+ $js_level = str_replace( '{{removelevelbutton}}', $remove_level, $js_level );
816
+ $js_level = str_replace( '{{emptylevelimage}}', $empty_level, $js_level );
817
+ $js_level = str_replace( '{{levelimage}}', '', $js_level );
818
+ $js_level = str_replace( '{{levelimagebutton}}', __( 'Set Image', 'mycred' ), $js_level );
819
+ $js_level = str_replace( '{{levelplaceholder}}', __( 'Level', 'mycred' ) . ' {{level}}', $js_level );
 
 
820
 
821
+ $template = str_replace( '{{levelimage}}', $level_image, $template );
822
+ $template = str_replace( '{{emptylevelimage}}', $empty_level, $template );
823
+ $template = str_replace( '{{levelimagebutton}}', ( ( $level_image === false ) ? __( 'Set Image', 'mycred' ) : __( 'Change Image', 'mycred' ) ), $template );
 
 
 
 
 
 
 
 
824
 
825
+ $template = str_replace( '{{levelplaceholder}}', sprintf( __( 'Level %d', 'mycred' ), $level+1 ), $template );
826
+ $template = str_replace( '{{levellabel}}', $setup['label'], $template );
 
 
 
827
 
828
+ $template = str_replace( '{{requirementslabel}}', __( 'Requirement', 'mycred' ), $template );
829
+ $js_level = str_replace( '{{requirementslabel}}', __( 'Requirement', 'mycred' ), $js_level );
830
+
831
+ $template = str_replace( '{{adnselected}}', ( ( $setup['compare'] === 'AND' ) ? 'selected' : '' ), $template );
832
+ $template = str_replace( '{{orselected}}', ( ( $setup['compare'] === 'OR' ) ? 'selected' : '' ), $template );
833
+
834
+ //$requirement = $this->requirements_template( 1 );
 
835
 
836
+ $total_requirements = count( $setup['requires'] );
837
+ $level_requirements = '';
838
+
839
+ foreach ( $setup['requires'] as $req_level => $reqsetup ) {
840
+
841
+ $requirement = $this->requirements_template( $level );
842
+
843
+ $requirement = str_replace( '{{level}}', $level, $requirement );
844
+ $requirement = str_replace( '{{reqlevel}}', $req_level, $requirement );
845
+
846
+ $point_type_options = '';
847
+ $point_type_options .= '<option value=""';
848
+ if ( $reqsetup['type'] == '' ) $point_type_options .= ' selected="selected"';
849
+ $point_type_options .= '>' . __( 'Select Point Type', 'mycred' ) . '</option>';
850
+ foreach ( $point_types as $type_id => $type_label ) {
851
+ $point_type_options .= '<option value="' . $type_id . '"';
852
+ if ( $reqsetup['type'] == $type_id ) $point_type_options .= ' selected="selected"';
853
+ $point_type_options .= '>' . $type_label . '</option>';
854
  }
855
 
856
+ $requirement = str_replace( '{{pointtypes}}', $point_type_options, $requirement );
857
+ $point_type_options = str_replace( 'selected="selected"', '', $point_type_options );
858
+ $js_requirement = str_replace( '{{pointtypes}}', $point_type_options, $js_requirement );
859
+
860
+ $reference_options = '';
861
+ $reference_options .= '<option value=""';
862
+ if ( $reqsetup['reference'] == '' ) $reference_options .= ' selected="selected"';
863
+ $reference_options .= '>' . __( 'Select Reference', 'mycred' ) . '</option>';
864
+ foreach ( $references as $ref_id => $ref_label ) {
865
+ $reference_options .= '<option value="' . $ref_id . '"';
866
+ if ( $reqsetup['reference'] == $ref_id ) $reference_options .= ' selected="selected"';
867
+ $reference_options .= '>' . $ref_label . '</option>';
868
+ }
869
 
870
+ $requirement = str_replace( '{{references}}', $reference_options, $requirement );
871
+ $requirement = str_replace( '{{reqamount}}', $reqsetup['amount'], $requirement );
872
+
873
+ $reference_options = str_replace( 'selected="selected"', '', $reference_options );
874
+ $js_requirement = str_replace( '{{references}}', $reference_options, $js_requirement );
875
+ $js_requirement = str_replace( '{{reqamount}}', $reqsetup['amount'], $js_requirement );
876
+
877
+ $by_options = '';
878
+ $by_options .= '<option value=""';
879
+ if ( $reqsetup['by'] == '' ) $by_options .= ' selected="selected"';
880
+ $by_options .= '>' . __( 'Select', 'mycred' ) . '</option>';
881
+ foreach ( $sums as $sum_id => $sum_label ) {
882
+ $by_options .= '<option value="' . $sum_id . '"';
883
+ if ( $reqsetup['by'] == $sum_id ) $by_options .= ' selected="selected"';
884
+ $by_options .= '>' . $sum_label . '</option>';
885
+ }
886
 
887
+ $requirement = str_replace( '{{requirementtype}}', $by_options, $requirement );
 
888
 
889
+ $by_options = str_replace( 'selected="selected"', '', $by_options );
890
+ $js_requirement = str_replace( '{{requirementtype}}', $by_options, $js_requirement );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
891
 
892
+ $selectedtype = '-';
893
+ if ( array_key_exists( $reqsetup['type'], $point_types ) )
894
+ $selectedtype = $point_types[ $reqsetup['type'] ];
895
 
896
+ $requirement = str_replace( '{{selectedtype}}', $selectedtype, $requirement );
897
 
898
+ $selectedreference = '-';
899
+ if ( array_key_exists( $reqsetup['reference'], $references ) )
900
+ $selectedreference = $references[ $reqsetup['reference'] ];
901
 
902
+ $requirement = str_replace( '{{selectedref}}', $selectedreference, $requirement );
903
 
904
+ $selectedby = '-';
905
+ if ( array_key_exists( $reqsetup['by'], $sums ) )
906
+ $selectedby = $sums[ $reqsetup['by'] ];
 
907
 
908
+ $requirement = str_replace( '{{selectedby}}', $selectedby, $requirement );
 
 
 
 
909
 
910
+ $requirement_button = '<button type="button" class="button button-primary form-control remove-requirement" data-req="{{reqlevel}}">-</button>';
911
+ $js_requirement = str_replace( '{{reqbutton}}', $requirement_button, $js_requirement );
 
 
 
912
 
913
+ $requirement_button = '<button type="button" class="button button-primary form-control remove-requirement" data-req="' . $req_level . '">-</button>';
914
+ if ( $req_level == 0 )
915
+ $requirement_button = '<button type="button" class="button button-secondary form-control" id="badges-add-new-requirement">+</button>';
 
 
916
 
917
+ $requirement = str_replace( '{{reqbutton}}', $requirement_button, $requirement );
918
 
919
+ $compare_label = '';
920
+ if ( $level > 0 && $req_level < $total_requirements )
921
+ $compare_label = ( ( $setup['compare'] === 'AND' ) ? _x( 'AND', 'Comparison of badge requirements. A AND B', 'mycred' ) : _x( 'OR', 'Comparison of badge requirements. A OR B', 'mycred' ) );
922
 
923
+ if ( $req_level+1 == $total_requirements )
924
+ $compare_label = '';
925
 
926
+ $requirement = str_replace( '{{comparelabel}}', $compare_label, $requirement );
 
 
 
927
 
928
+ $level_requirements .= $requirement;
929
 
930
+ }
 
931
 
932
+ $template = str_replace( '{{{requirements}}}', $level_requirements, $template );
 
 
 
933
 
934
+ $rewards = $this->rewards_template();
935
 
936
+ $js_level = str_replace( '{{reqamount}}', '', $js_level );
 
937
 
938
+ $rewards = str_replace( '{{level}}', $level, $rewards );
939
+ $rewards = str_replace( '{{rewardlabel}}', __( 'Reward', 'mycred' ), $rewards );
940
 
941
+ $point_type_options = '';
942
+ foreach ( $point_types as $type_id => $type_label ) {
943
+ $point_type_options .= '<option value="' . $type_id . '"';
944
+ if ( $setup['reward']['type'] == $type_id ) $point_type_options .= ' selected="selected"';
945
+ $point_type_options .= '>' . $type_label . '</option>';
946
+ }
947
 
948
+ $rewards = str_replace( '{{pointtypes}}', $point_type_options, $rewards );
949
+ $rewards = str_replace( '{{logplaceholder}}', __( 'Log template', 'mycred' ), $rewards );
950
+ $rewards = str_replace( '{{logtemplate}}', $setup['reward']['log'], $rewards );
951
+ $rewards = str_replace( '{{rewardamount}}', $setup['reward']['amount'], $rewards );
952
 
953
+ $template = str_replace( '{{rewards}}', $rewards, $template );
954
+ $js_level = str_replace( '{{rewards}}', $rewards, $js_level );
 
 
 
 
 
 
955
 
956
+ echo $template;
 
 
 
 
 
 
 
 
957
 
958
+ $level_counter++;
 
959
 
960
+ }
961
 
962
+ ?>
963
+ </div>
964
+ <script type="text/javascript">
965
+ var BadgeLevel = '<?php echo $js_level; ?>';
966
+ var BadgeNewRequrement = '<?php echo $js_requirement; ?>';
967
+ var BadgeRequirement = '<?php echo $js_requirement_clone; ?>';
968
  </script>
 
969
  <?php
970
 
971
  }
972
 
973
  /**
974
  * Save Badge Details
975
+ * @since 1.7
976
+ * @version 1.1
977
  */
978
+ public function save_badge( $post_id, $post = NULL ) {
979
 
980
+ if ( $post === NULL || ! current_user_can( $this->core->edit_creds_cap() ) || ! isset( $_POST['mycred_badge'] ) ) return $post_id;
 
981
 
982
+ $manual = 0;
983
+ if ( isset( $_POST['mycred_badge']['manual'] ) )
984
+ $manual = 1;
985
 
986
+ $badge_levels = array();
987
+ $badge_requirements = array();
 
988
 
989
+ // Run through each level
990
+ if ( ! empty( $_POST['mycred_badge']['levels'] ) ) {
991
 
992
+ $level_row = 0;
993
+ foreach ( $_POST['mycred_badge']['levels'] as $level_id => $level_setup ) {
994
 
995
+ $level = array();
 
 
 
 
 
996
 
997
+ if ( array_key_exists( 'attachment_id', $level_setup ) ) {
998
+ $level['attachment_id'] = absint( $level_setup['attachment_id'] );
999
+ $level['image_url'] = ( ( array_key_exists( 'image_url', $level_setup ) ) ? sanitize_text_field( $level_setup['image_url'] ) : '' );
1000
+ }
1001
+ else {
1002
+ $level['attachment_id'] = 0;
1003
+ $level['image_url'] = '';
1004
  }
 
1005
 
1006
+ $level['label'] = sanitize_text_field( $level_setup['label'] );
1007
 
1008
+ if ( array_key_exists( 'compare', $level_setup ) )
1009
+ $level['compare'] = ( ( $level_setup['compare'] == 'AND' ) ? 'AND' : 'OR' );
1010
+ else
1011
+ $level['compare'] = ( ( array_key_exists( 'compare', $badge_levels[0] ) ) ? $badge_levels[0]['compare'] : 'AND' );
1012
 
1013
+ $level['requires'] = array();
 
 
1014
 
1015
+ if ( array_key_exists( 'requires', $level_setup ) ) {
1016
 
1017
+ $level_requirements = array();
1018
+
1019
+ $row = 0;
1020
+ foreach ( $level_setup['requires'] as $requirement_id => $requirement_setup ) {
1021
+
1022
+ $requirement = array();
1023
+ $requirement['type'] = ( ( array_key_exists( 'type', $requirement_setup ) ) ? sanitize_key( $requirement_setup['type'] ) : '' );
1024
+ $requirement['reference'] = ( ( array_key_exists( 'reference', $requirement_setup ) ) ? sanitize_key( $requirement_setup['reference'] ) : '' );
1025
+ $requirement['amount'] = ( ( array_key_exists( 'amount', $requirement_setup ) ) ? sanitize_text_field( $requirement_setup['amount'] ) : '' );
1026
+ $requirement['by'] = ( ( array_key_exists( 'by', $requirement_setup ) ) ? sanitize_key( $requirement_setup['by'] ) : '' );
1027
+
1028
+ $level_requirements[ $row ] = $requirement;
1029
+ $row ++;
1030
+
1031
+ }
1032
+
1033
+ if ( $level_row == 0 )
1034
+ $badge_requirements = $level_requirements;
1035
+
1036
+ $completed_requirements = array();
1037
+ foreach ( $level_requirements as $requirement_id => $requirement_setup ) {
1038
+
1039
+ if ( $level_row == 0 ) {
1040
+ $completed_requirements[ $requirement_id ] = $requirement_setup;
1041
+ continue;
1042
+ }
1043
+
1044
+ $completed_requirements[ $requirement_id ] = $badge_requirements[ $requirement_id ];
1045
+ $completed_requirements[ $requirement_id ]['amount'] = $requirement_setup['amount'];
1046
+
1047
+ }
1048
+
1049
+ $level['requires'] = $completed_requirements;
1050
+
1051
+ }
1052
 
1053
+ $reward = array( 'type' => '', 'log' => '', 'amount' => '' );
1054
 
1055
+ if ( array_key_exists( 'reward', $level_setup ) ) {
1056
 
1057
+ $reward['type'] = sanitize_key( $level_setup['reward']['type'] );
1058
+ $reward['log'] = sanitize_text_field( $level_setup['reward']['log'] );
1059
 
1060
+ if ( $reward['type'] != MYCRED_DEFAULT_TYPE_KEY )
1061
+ $mycred = mycred( $reward['type'] );
1062
+ else
1063
+ $mycred = $this->core;
1064
 
1065
+ $reward['amount'] = $mycred->number( $level_setup['reward']['amount'] );
1066
 
1067
  }
1068
+
1069
+ $level['reward'] = $reward;
1070
+
1071
+ $badge_levels[] = $level;
1072
+ $level_row ++;
1073
+
1074
  }
1075
 
1076
  }
1077
 
1078
+ // Save Badge Setup
1079
+ update_post_meta( $post_id, 'badge_prefs', $badge_levels );
1080
+
1081
+ // If we just set the badge to be manual we need to re-parse all references.
1082
+ $old_manual = get_post_meta( $post_id, 'manual_badge', true );
1083
+ if ( absint( $old_manual ) === 0 && $manual === 1 ) {
1084
+ foreach ( $this->point_types as $type_id => $label ) {
1085
+ mycred_get_badge_references( $type_id, true );
1086
+ }
1087
+ }
1088
+
1089
+ // Force re-calculation of used references
1090
+ foreach ( $this->point_types as $type_id => $type )
1091
+ mycred_delete_option( 'mycred-badge-refs-' . $type_id );
1092
+
1093
+ // Save if badge is manuall
1094
+ update_post_meta( $post_id, 'manual_badge', $manual );
1095
+
1096
+ // Main image (used when a user has not earned a badge
1097
+ $main_image = $_POST['mycred_badge']['main_image'];
1098
+
1099
+ // If we are using an attachment
1100
+ if ( absint( $main_image ) > 0 )
1101
+ $image = absint( $main_image );
1102
+
1103
+ // Else we are using a URL (old setup)
1104
+ else
1105
+ $image = sanitize_text_field( $_POST['mycred_badge']['main_image_url'] );
1106
+
1107
+ update_post_meta( $post_id, 'main_image', $image );
1108
+
1109
  // Let others play
1110
  do_action( 'mycred_save_badge', $post_id );
1111
 
1113
 
1114
  /**
1115
  * Add to General Settings
1116
+ * @since 1.0
1117
  * @version 1.0.1
1118
  */
1119
  public function after_general_settings( $mycred = NULL ) {
1131
  if ( ! $buddypress && ! $bbpress ) return;
1132
 
1133
  ?>
1134
+ <h4><span class="dashicons dashicons-admin-plugins static"></span><?php _e( 'Badges', 'mycred' ); ?></h4>
1135
  <div class="body" style="display:none;">
1136
 
1137
  <?php if ( $buddypress ) : ?>
1213
 
1214
  /**
1215
  * Save Settings
1216
+ * @since 1.0
1217
+ * @version 1.0.2
1218
  */
1219
  public function sanitize_extra_settings( $new_data, $data, $core ) {
1220
 
1221
  $new_data['badges']['show_all_bp'] = ( isset( $data['badges']['show_all_bp'] ) ) ? $data['badges']['show_all_bp'] : 0;
1222
  $new_data['badges']['show_all_bb'] = ( isset( $data['badges']['show_all_bb'] ) ) ? $data['badges']['show_all_bb'] : 0;
1223
 
1224
+ $new_data['badges']['buddypress'] = ( isset( $data['badges']['buddypress'] ) ) ? sanitize_text_field( $data['badges']['buddypress'] ) : '';
1225
+ $new_data['badges']['bbpress'] = ( isset( $data['badges']['bbpress'] ) ) ? sanitize_text_field( $data['badges']['bbpress'] ) : '';
1226
 
1227
  return $new_data;
1228
 
1229
  }
1230
 
1231
  /**
1232
+ * User Badges Admin Screen
1233
+ * @since 1.0
1234
  * @version 1.0
1235
  */
1236
+ public function badge_user_screen( $user ) {
1237
 
1238
+ // Only visible to admins
1239
+ if ( ! mycred_is_admin() ) return;
1240
 
1241
+ $user_id = $user->ID;
1242
+ $all_badges = mycred_get_badge_ids();
1243
+ $users_badges = mycred_get_users_badges( $user_id );
1244
+
1245
+ ?>
1246
+ <style type="text/css">
1247
+ .badge-wrapper { min-height: 230px; }
1248
+ .badge-wrapper select { width: 100%; }
1249
+ .badge-image-wrap { text-align: center; }
1250
+ .badge-image-wrap .badge-image { display: block; width: 100%; height: 100px; line-height: 100px; }
1251
+ .badge-image-wrap .badge-image.empty { content: "<?php _e( 'No image set', 'mycred' ); ?>"; }
1252
+ .badge-image-wrap .badge-image img { width: auto; height: auto; max-height: 100px; }
1253
+ </style>
1254
+ <table class="form-table">
1255
+ <tr>
1256
+ <th scope="row"><?php _e( 'Badges', 'mycred' ); ?></th>
1257
+ <td>
1258
+ <fieldset id="mycred-badge-list" class="badge-list">
1259
+ <legend class="screen-reader-text"><span><?php _e( 'Badges', 'mycred' ); ?></span></legend>
1260
+ <?php
1261
+
1262
+ if ( ! empty( $all_badges ) ) {
1263
+ foreach ( $all_badges as $badge_id ) {
1264
+
1265
+ $badge_id = absint( $badge_id );
1266
+ $badge = mycred_get_badge( $badge_id );
1267
+ $earned = 0;
1268
+ $earned_level = 0;
1269
+ $badge_image = $badge->main_image;
1270
+
1271
+ if ( array_key_exists( $badge_id, $users_badges ) ) {
1272
+ $earned = 1;
1273
+ $earned_level = $users_badges[ $badge_id ];
1274
+ $badge_image = $badge->get_image( $earned_level );
1275
+ }
1276
+
1277
+ $level_select = '<input type="hidden" name="mycred_badge_manual[badges][' . $badge_id . '][level]" value="0" /><select disabled="disabled"><option>Level 1</option></select>';
1278
+ if ( count( $badge->levels ) > 1 ) {
1279
+
1280
+ $level_select = '<select name="mycred_badge_manual[badges][' . $badge_id . '][level]">';
1281
+ $level_select .= '<option value=""';
1282
+ if ( ! $earned ) $level_select .= ' selected="selected"';
1283
+ $level_select .= '>' . __( 'Select a level', 'mycred' ) . '</option>';
1284
+
1285
+ foreach ( $badge->levels as $level_id => $level ) {
1286
+ $level_select .= '<option value="' . $level_id . '"';
1287
+ if ( $earned && $earned_level == $level_id ) $level_select .= ' selected="selected"';
1288
+ $level_select .= '>' . __( 'Level', 'mycred' ) . ' ' . ( $level_id + 1 ) . '</option>';
1289
+ }
1290
+
1291
+ $level_select .= '</select>';
1292
+
1293
+ }
1294
+
1295
+ ?>
1296
+ <div class="badge-wrapper color-option<?php if ( $earned === 1 ) echo ' selected'; ?>" id="mycred-badge<?php echo $badge_id; ?>-wrapper">
1297
+ <label for="mycred-badge<?php echo $badge_id; ?>"><input type="checkbox" name="mycred_badge_manual[badges][<?php echo $badge_id; ?>][has]" class="toggle-badge" id="mycred-badge<?php echo $badge_id; ?>" <?php checked( $earned, 1 );?> value="1" /> <?php _e( 'Earned', 'mycred' ); ?></label>
1298
+ <div class="badge-image-wrap">
1299
+
1300
+ <div class="badge-image<?php if ( $badge_image == '' ) echo ' empty'; ?>"><?php echo $badge_image; ?></div>
1301
+
1302
+ <h4><?php echo $badge->title; ?></h4>
1303
+ </div>
1304
+ <div class="badge-actions" style="min-height: 32px;">
1305
+
1306
+ <?php echo $level_select; ?>
1307
+
1308
+ </div>
1309
+ </div>
1310
+ <?php
1311
+
1312
+ }
1313
+ }
1314
+
1315
+ ?>
1316
+ </fieldset>
1317
+ <input type="hidden" name="mycred_badge_manual[token]" value="<?php echo wp_create_nonce( 'mycred-manual-badges' . $user_id ); ?>" />
1318
+ </td>
1319
+ </tr>
1320
+ </table>
1321
+ <script type="text/javascript">
1322
+ jQuery(function($) {
1323
+
1324
+ $( '.badge-wrapper label input.toggle-badge' ).click(function(){
1325
+
1326
+ if ( $(this).is( ':checked' ) )
1327
+ $( '#' + $(this).attr( 'id' ) + '-wrapper' ).addClass( 'selected' );
1328
+
1329
+ else
1330
+ $( '#' + $(this).attr( 'id' ) + '-wrapper' ).removeClass( 'selected' );
1331
+
1332
+ });
1333
+
1334
+ });
1335
+ </script>
1336
+ <?php
1337
+
1338
+ }
1339
+
1340
+ /**
1341
+ * Save Manual Badges
1342
+ * @since 1.0
1343
+ * @version 1.0
1344
+ */
1345
+ public function save_manual_badges( $user_id ) {
1346
+
1347
+ if ( ! mycred_is_admin() ) return;
1348
+
1349
+ if ( isset( $_POST['mycred_badge_manual']['token'] ) ) {
1350
+
1351
+ if ( wp_verify_nonce( $_POST['mycred_badge_manual']['token'], 'mycred-manual-badges' . $user_id ) ) {
1352
 
1353
  $added = $removed = $updated = 0;
1354
  $users_badges = mycred_get_users_badges( $user_id );
1361
  if ( isset( $badge['level'] ) && $badge['level'] != '' )
1362
  $level = absint( $badge['level'] );
1363
 
1364
+ mycred_assign_badge_to_user( $user_id, $badge_id, $level );
1365
  $added ++;
1366
  }
1367
 
1373
 
1374
  // Level change
1375
  elseif ( array_key_exists( $badge_id, $users_badges ) && isset( $badge['level'] ) && $badge['level'] != $users_badges[ $badge_id ] ) {
1376
+ mycred_assign_badge_to_user( $user_id, $badge_id, $badge['level'] );
1377
  $updated ++;
1378
  }
1379
 
1387
  }
1388
 
1389
  /**
1390
+ * AJAX: Assign Badge
1391
+ * @since 1.0
1392
+ * @version 1.2
1393
  */
1394
+ public function action_assign_badge() {
 
 
 
 
 
1395
 
1396
+ check_ajax_referer( 'mycred-assign-badge', 'token' );
1397
 
1398
+ global $wpdb;
1399
 
1400
+ $badge_id = absint( $_POST['badge_id'] );
1401
+ if ( $badge_id === 0 ) wp_send_json_error();
1402
 
1403
+ $results = mycred_assign_badge( $badge_id );
 
1404
 
1405
+ if ( $results !== false && ! empty( $results ) )
1406
+ wp_send_json_success( sprintf( __( 'A total of %d users have received this badge.', 'mycred' ), count( $results ) ) );
 
1407
 
1408
+ wp_send_json_error( __( 'No users has yet earned this badge.', 'mycred' ) );
1409
 
1410
+ }
1411
 
1412
+ /**
1413
+ * AJAX: Remove Badge Connections
1414
+ * @since 1.0
1415
+ * @version 1.0.1
1416
+ */
1417
+ public function action_remove_connections() {
1418
 
1419
+ check_ajax_referer( 'mycred-remove-badge-connection', 'token' );
 
 
 
1420
 
1421
+ $badge_id = absint( $_POST['badge_id'] );
 
1422
 
1423
+ global $wpdb;
 
 
 
 
1424
 
1425
+ // Delete connections
1426
+ $count = $wpdb->delete(
1427
+ $wpdb->usermeta,
1428
+ array( 'meta_key' => 'mycred_badge' . $badge_id ),
1429
+ array( '%s' )
1430
+ );
1431
 
1432
+ if ( $count == 0 )
1433
+ wp_send_json_success( __( 'No connections where removed.', 'mycred' ) );
 
 
1434
 
1435
+ wp_send_json_success( sprintf( __( '%s connections where removed.', 'mycred' ), $count ) );
 
1436
 
1437
+ }
1438
 
1439
+ /**
1440
+ * Insert Badges into bbPress profile
1441
+ * @since 1.0
1442
+ * @version 1.1
1443
+ */
1444
+ public function insert_into_bbpress_profile() {
1445
 
1446
+ $user_id = bbp_get_displayed_user_id();
1447
+ if ( isset( $this->badges['show_all_bb'] ) && $this->badges['show_all_bb'] == 1 )
1448
+ mycred_render_my_badges( array(
1449
+ 'show' => 'all',
1450
+ 'width' => MYCRED_BADGE_WIDTH,
1451
+ 'height' => MYCRED_BADGE_HEIGHT,
1452
+ 'user_id' => $user_id
1453
+ ) );
1454
 
1455
+ else
1456
+ mycred_display_users_badges( $user_id );
 
 
1457
 
1458
+ }
 
 
 
 
1459
 
1460
+ /**
1461
+ * Insert Badges into bbPress
1462
+ * @since 1.0
1463
+ * @version 1.1
1464
+ */
1465
+ public function insert_into_bbpress_reply() {
1466
 
1467
+ $user_id = bbp_get_reply_author_id();
1468
+ if ( $user_id > 0 ) {
1469
 
1470
+ if ( isset( $this->badges['show_all_bb'] ) && $this->badges['show_all_bb'] == 1 )
1471
+ mycred_render_my_badges( array(
1472
+ 'show' => 'all',
1473
+ 'width' => MYCRED_BADGE_WIDTH,
1474
+ 'height' => MYCRED_BADGE_HEIGHT,
1475
+ 'user_id' => $user_id
1476
+ ) );
1477
 
1478
+ else
1479
+ mycred_display_users_badges( $user_id );
1480
 
1481
+ }
 
 
 
 
 
1482
 
1483
+ }
1484
 
1485
+ /**
1486
+ * Insert Badges in BuddyPress
1487
+ * @since 1.0
1488
+ * @version 1.1.1
1489
+ */
1490
+ public function insert_into_buddypress() {
1491
 
1492
+ $user_id = bp_displayed_user_id();
1493
+ if ( isset( $this->badges['show_all_bp'] ) && $this->badges['show_all_bp'] == 1 )
1494
+ echo mycred_render_my_badges( array(
1495
+ 'show' => 'all',
1496
+ 'width' => MYCRED_BADGE_WIDTH,
1497
+ 'height' => MYCRED_BADGE_HEIGHT,
1498
+ 'user_id' => $user_id
1499
+ ) );
1500
 
1501
+ else
1502
+ mycred_display_users_badges( $user_id );
 
 
 
 
 
 
1503
 
1504
  }
1505
 
1506
  }
1507
+ endif;
1508
 
1509
+ /**
1510
+ * Load Badges Module
1511
+ * @since 1.7
1512
+ * @version 1.0
1513
+ */
1514
+ if ( ! function_exists( 'mycred_load_badges_addon' ) ) :
1515
+ function mycred_load_badges_addon( $modules, $point_types ) {
1516
+
1517
+ $modules['solo']['badges'] = new myCRED_Badge_Module();
1518
+ $modules['solo']['badges']->load();
1519
 
1520
+ return $modules;
1521
+
1522
+ }
1523
  endif;
1524
+ add_filter( 'mycred_load_modules', 'mycred_load_badges_addon', 10, 2 );
1525
 
1526
  ?>
addons/banking/abstracts/mycred-abstract-service.php CHANGED
@@ -7,7 +7,7 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
7
  * @since 1.2
8
  * @version 1.1
9
  */
10
- if ( ! class_exists( 'myCRED_Service' ) ) {
11
  abstract class myCRED_Service {
12
 
13
  // Service ID
@@ -18,46 +18,40 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
18
 
19
  // Multipoint types
20
  public $is_main_type = true;
21
- public $mycred_type = 'mycred_default';
22
 
23
  // Service Prefs
24
- public $prefs = false;
 
25
 
26
  /**
27
  * Construct
28
  */
29
- function __construct( $args = array(), $service_prefs = NULL, $type = 'mycred_default' ) {
 
30
  if ( ! empty( $args ) ) {
31
  foreach ( $args as $key => $value ) {
32
  $this->$key = $value;
33
  }
34
  }
35
-
36
- // Grab myCRED Settings
37
- $this->core = mycred( $type );
38
 
39
- if ( $type != '' ) {
40
- $this->core->cred_id = sanitize_text_field( $type );
41
- $this->mycred_type = $this->core->cred_id;
42
- }
43
 
44
- if ( $this->mycred_type != 'mycred_default' )
45
  $this->is_main_type = false;
46
 
47
- // Grab settings
48
- if ( $service_prefs !== NULL ) {
49
- // Assign prefs if set
50
- if ( isset( $service_prefs[ $this->id ] ) )
51
- $this->prefs = $service_prefs[ $this->id ];
52
 
53
- // Defaults must be set
54
- if ( ! isset( $this->defaults ) )
55
- $this->defaults = array();
56
- }
 
57
 
58
- // Apply default settings if needed
59
- if ( ! empty( $this->defaults ) )
60
- $this->prefs = mycred_apply_defaults( $this->defaults, $this->prefs );
61
  }
62
 
63
  /**
@@ -66,7 +60,7 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
66
  * @since 1.2
67
  * @version 1.0
68
  */
69
- function run() {
70
  wp_die( 'function myCRED_Service::run() must be over-ridden in a sub-class.' );
71
  }
72
 
@@ -75,17 +69,17 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
75
  * @since 0.1
76
  * @version 1.0
77
  */
78
- function preferences() {
79
- echo '<p>' . __( 'This Service has no settings', 'mycred' ) . '</p>';
80
- }
81
 
82
  /**
83
  * Sanitise Preference
84
  * @since 1.2
85
  * @version 1.0
86
  */
87
- function sanitise_preferences( $post ) {
 
88
  return $post;
 
89
  }
90
 
91
  /**
@@ -93,35 +87,28 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
93
  * @since 1.5.2
94
  * @version 1.0
95
  */
96
- function activate() { }
97
 
98
  /**
99
  * Deactivate
100
  * @since 1.2
101
  * @version 1.0
102
  */
103
- function deactivate() { }
104
 
105
  /**
106
- * User Override
107
- * @since 1.5.2
108
- * @version 1.0
109
- */
110
- function user_override( $user = NULL, $type = 'mycred_default' ) { }
111
-
112
- /**
113
- * Save User Override
114
- * @since 1.5.2
115
  * @version 1.0
116
  */
117
- function save_user_override() { }
118
 
119
  /**
120
- * User Override Notice
121
- * @since 1.5.2
122
  * @version 1.0
123
  */
124
- function user_override_notice() { }
125
 
126
  /**
127
  * Get Field Name
@@ -129,7 +116,8 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
129
  * @since 1.2
130
  * @version 1.1
131
  */
132
- function field_name( $field = '' ) {
 
133
  if ( is_array( $field ) ) {
134
  $array = array();
135
  foreach ( $field as $parent => $child ) {
@@ -144,12 +132,13 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
144
  else {
145
  $field = '[' . $field . ']';
146
  }
147
-
148
  $option_id = 'mycred_pref_bank';
149
  if ( ! $this->is_main_type )
150
  $option_id = $option_id . '_' . $this->mycred_type;
151
 
152
  return $option_id . '[service_prefs][' . $this->id . ']' . $field;
 
153
  }
154
 
155
  /**
@@ -158,7 +147,8 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
158
  * @since 1.2
159
  * @version 1.1
160
  */
161
- function field_id( $field = '' ) {
 
162
  if ( is_array( $field ) ) {
163
  $array = array();
164
  foreach ( $field as $parent => $child ) {
@@ -173,312 +163,175 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
173
  else {
174
  $field = str_replace( '_', '-', $field );
175
  }
176
-
177
  $option_id = 'mycred_pref_bank';
178
  if ( ! $this->is_main_type )
179
  $option_id = $option_id . '_' . $this->mycred_type;
180
 
181
  $option_id = str_replace( '_', '-', $option_id );
182
  return $option_id . '-' . str_replace( '_', '-', $this->id ) . '-' . $field;
 
183
  }
184
 
185
  /**
186
- * Exclude User Check
187
- * @since 1.5.2
188
- * @version 1.0
189
  */
190
- function exclude_user( $user_id = NULL ) {
191
-
192
- $reply = false;
193
-
194
- // Check if we are excluded based on ID
195
- if ( isset( $this->prefs['exclude_ids'] ) && $this->prefs['exclude_ids'] != '' ) {
196
-
197
- $excluded_ids = explode( ',', $this->prefs['exclude_ids'] );
198
- if ( ! empty( $excluded_ids ) ) {
199
- $clean_ids = array();
200
- foreach ( $excluded_ids as $id )
201
- $clean_ids[] = (int) trim( $id );
202
-
203
- if ( in_array( $user_id, $clean_ids ) )
204
- $reply = 'list';
205
- }
206
-
207
- }
208
-
209
- // Check if we are excluded based on role
210
- if ( $reply === false && isset( $this->prefs['exclude_roles'] ) && ! empty( $this->prefs['exclude_roles'] ) ) {
211
 
212
- $user = new WP_User( $user_id );
213
- if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
214
- foreach ( $user->roles as $role ) {
215
- if ( in_array( $role, $this->prefs['exclude_roles'] ) )
216
- $reply = 'role';
217
- }
218
- }
219
 
220
- }
221
 
222
- return apply_filters( 'mycred_banking_exclude_user', $reply, $user_id, $this );
223
  }
224
 
225
  /**
226
- * Get Timeframes
227
- * @since 1.2
228
  * @version 1.0
229
  */
230
- function get_timeframes() {
231
- $timeframes = array(
232
- 'hourly' => array(
233
- 'label' => __( 'Hourly', 'mycred' ),
234
- 'date_format' => 'G'
235
- ),
236
- 'daily' => array(
237
- 'label' => __( 'Daily', 'mycred' ),
238
- 'date_format' => 'z'
239
- ),
240
- 'weekly' => array(
241
- 'label' => __( 'Weekly', 'mycred' ),
242
- 'date_format' => 'W'
243
- ),
244
- 'monthly' => array(
245
- 'label' => __( 'Monthly', 'mycred' ),
246
- 'date_format' => 'M'
247
- ),
248
- 'quarterly' => array(
249
- 'label' => __( 'Quarterly', 'mycred' ),
250
- 'date_format' => 'Y'
251
- ),
252
- 'semiannually' => array(
253
- 'label' => __( 'Semiannually', 'mycred' ),
254
- 'date_format' => 'Y'
255
- ),
256
- 'annually' => array(
257
- 'label' => __( 'Annually', 'mycred' ),
258
- 'date_format' => 'Y'
259
- )
260
- );
261
- return apply_filters( 'mycred_banking_timeframes', $timeframes );
262
- }
263
 
264
- /**
265
- * Get Now
266
- * @since 1.2
267
- * @version 1.0
268
- */
269
- public function get_now( $rate = '' ) {
270
-
271
- $timeframes = $this->get_timeframes();
272
- if ( array_key_exists( $rate, $timeframes ) ) {
273
- // Quarterly
274
- if ( $rate == 'quarterly' ) {
275
- $month = date_i18n( 'n' );
276
- return 'Q' . ceil( $month/3 );
277
- }
278
- elseif ( $rate == 'semiannually' ) {
279
- $month = date_i18n( 'n' );
280
- return ( $month <= 6 ) ? 'first' : 'second';
281
- }
282
- else {
283
- return date_i18n( $timeframes[ $rate ]['date_format'] );
284
- }
285
- }
286
-
287
- return false;
288
 
289
- }
290
 
291
- /**
292
- * Get Run Count
293
- * @since 1.5.2
294
- * @version 1.0
295
- */
296
- public function get_run_count( $instance = '' ) {
297
- $key = 'mycred_brc_' . $instance . '_' . $this->mycred_type;
298
- return mycred_get_option( $key, 0 );
299
- }
300
 
301
- /**
302
- * Update Run Count
303
- * @since 1.5.2
304
- * @version 1.0
305
- */
306
- public function update_run_count( $instance = '' ) {
307
 
308
- $count = $this->get_run_count( $instance );
309
- $count ++;
310
- mycred_update_option( 'mycred_brc_' . $instance . '_' . $this->mycred_type, $count );
311
 
312
- }
 
 
313
 
314
- /**
315
- * Last Run
316
- * @since 1.2
317
- * @version 1.1
318
- */
319
- public function get_last_run( $instance = '' ) {
320
- $key = 'mycred_banking_' . $this->id . '_' . $instance . $this->mycred_type;
321
- return mycred_get_option( $key, 'n/a' );
322
- }
323
 
324
- /**
325
- * Save Last Run
326
- * @since 1.5.2
327
- * @version 1.0
328
- */
329
- public function save_last_run( $instance = '', $time = NULL ) {
330
 
331
- if ( $time === NULL ) $time = date_i18n( 'U' );
332
- mycred_update_option( 'mycred_banking_' . $this->id . '_' . $instance . $this->mycred_type, $time );
333
 
334
  }
335
 
336
  /**
337
- * Display Last Run
338
- * @since 1.5.2
339
  * @version 1.0
340
  */
341
- public function display_last_run( $instance = '' ) {
342
- $last_run = $this->get_last_run( $instance );
343
- if ( $last_run != 'n/a' )
344
- $last_run = date( 'Y-m-d G:i', $last_run );
345
-
346
- return $last_run;
347
- }
348
-
349
- /**
350
- * Time To Run
351
- * @since 1.2.2
352
- * @version 1.1
353
- */
354
- public function time_to_run( $rate, $last_run ) {
355
- $now = $this->get_now( $rate );
356
- if ( $last_run == 'n/a' ) return false;
357
-
358
- $timeframes = $this->get_timeframes();
359
- $last_run = date_i18n( $timeframes[ $rate ]['date_format'], $last_run );
360
-
361
- switch ( $rate ) {
362
 
363
- case 'hourly' :
364
-
365
- if ( $now == 0 && $last_run == 23 ) return true;
366
- elseif ( $now-1 == $last_run ) return true;
367
-
368
- break;
369
 
370
- case 'daily' :
371
 
372
- if ( $now == 0 && $last_run >= 365 ) return true;
373
- elseif ( $now-1 == $last_run ) return true;
374
 
375
- break;
 
 
 
 
376
 
377
- case 'weekly' :
378
 
379
- if ( $now == 0 && $last_run >= 52 ) return true;
380
- elseif ( $now-1 == $last_run ) return true;
381
 
382
- break;
 
 
 
 
383
 
384
- case 'monthly' :
 
 
 
 
 
385
 
386
- if ( $now == 1 && $last_run == 12 ) return true;
387
- elseif ( $now-1 == $last_run ) return true;
388
 
389
- break;
390
 
391
- case 'quarterly' :
 
 
 
 
392
 
393
- $current_quarter = substr( $now, 0, -1 );
394
- if ( $current_quarter == 1 )
395
- $last_quarter = 4;
396
- else
397
- $last_quarter = $current_quarter-1;
398
- if ( 'Q' . $last_quarter == $last_run ) return true;
399
 
400
- break;
401
 
402
- case 'semiannually' :
 
 
403
 
404
- if ( $now != $last_run ) return true;
 
 
 
 
 
 
405
 
406
- break;
407
 
408
- case 'annually' :
409
 
410
- if ( $now-1 == $last_run ) return true;
411
 
412
- break;
 
 
 
 
 
413
 
414
- default :
415
 
416
- return apply_filters( 'mycred_banking_time_to_run', false, $rate, $last_run );
417
 
418
- break;
 
 
 
 
 
419
 
420
- }
421
 
422
- return false;
423
  }
424
 
425
  /**
426
- * Get Users
427
- * Returns all blog users IDs either from a daily transient or
428
- * by making a fresh SQL Query.
429
- * @since 1.2
430
- * @version 1.2
431
  */
432
- public function get_users() {
433
- // Get daily transient
434
- $data = get_transient( 'mycred_banking_payout_ids' );
435
-
436
- // If the user count does not equal the total number of users, get a
437
- // new result, else run the same.
438
- if ( $data !== false ) {
439
- $user_count = $this->core->count_members();
440
- $cached_count = count( $data );
441
- if ( $cached_count != $user_count ) {
442
- unset( $data );
443
- $data = false;
444
- }
445
- }
446
-
447
- // New Query
448
- if ( $data === false ) {
449
- global $wpdb;
450
- $data = $wpdb->get_col( "SELECT ID FROM {$wpdb->users};" );
451
-
452
- foreach ( $data as $num => $user_id ) {
453
- $user_id = intval( $user_id );
454
- if ( isset( $this->prefs['excludes'] ) ) {
455
- if ( ! empty( $this->prefs['excludes'] ) ) {
456
- if ( ! is_array( $this->prefs['excludes'] ) ) $excludes = explode( ',', $this->prefs['excludes'] );
457
- if ( in_array( $user_id, $excludes ) ) unset( $data[ $num ] );
458
- }
459
- }
460
-
461
- if ( $this->core->exclude_user( $user_id ) ) unset( $data[ $num ] );
462
- }
463
-
464
- set_transient( 'mycred_banking_payout_ids', $data, DAY_IN_SECONDS );
465
- $wpdb->flush();
466
  }
467
-
468
- return $data;
469
- }
470
 
471
- /**
472
- * Get Days in Year
473
- * @since 1.2
474
- * @version 1.0.1
475
- */
476
- public function get_days_in_year() {
477
- if ( date_i18n( 'L' ) )
478
- $days = 366;
479
- else
480
- $days = 365;
481
- return apply_filters( 'mycred_banking_days_in_year', $days, $this );
482
  }
483
 
484
  /**
@@ -486,14 +339,14 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
486
  * @since 1.2
487
  * @version 1.0
488
  */
489
- function timeframe_dropdown( $pref_id = '', $use_select = true, $hourly = true ) {
490
-
491
- $timeframes = $this->get_timeframes();
492
  if ( ! $hourly )
493
  unset( $timeframes['hourly'] );
494
-
495
- echo '<select name="' . $this->field_name( $pref_id ) . '" id="' . $this->field_id( $pref_id ) . '">';
496
-
497
  if ( $use_select )
498
  echo '<option value="">' . __( 'Select', 'mycred' ) . '</option>';
499
 
@@ -506,102 +359,17 @@ if ( ! class_exists( 'myCRED_Service' ) ) {
506
  elseif ( isset( $this->prefs[ $pref_id ] ) ) {
507
  $settings = $this->prefs[ $pref_id ];
508
  }
509
-
510
  foreach ( $timeframes as $value => $details ) {
511
  echo '<option value="' . $value . '"';
512
  if ( $settings == $value ) echo ' selected="selected"';
513
  echo '>' . $details['label'] . '</option>';
514
  }
515
  echo '</select>';
516
- }
517
-
518
- /**
519
- * Get Eligeble Users
520
- * Returns an array of user IDs that are not excluded
521
- * from this service.
522
- * @since 1.5.2
523
- * @version 1.1
524
- */
525
- public function get_eligeble_users() {
526
-
527
- global $wpdb;
528
- $joins = $wheres = array();
529
-
530
- $format = '%d';
531
- if ( $this->core->format['decimals'] > 0 )
532
- $format = '%f';
533
-
534
- // Minimum Balance
535
- if ( isset( $this->prefs['min_balance'] ) && $this->prefs['min_balance'] != '' && $this->prefs['min_balance'] != 0 ) {
536
-
537
- $balance_key = $this->mycred_type;
538
- if ( is_multisite() && $GLOBALS['blog_id'] > 1 && ! $this->core->use_central_logging )
539
- $balance_key .= '_' . $GLOBALS['blog_id'];
540
-
541
- $joins[] = $wpdb->prepare( "INNER JOIN {$wpdb->usermeta} balance ON ( users.ID = balance.user_id AND balance.meta_key = %s )", $balance_key );
542
- $wheres[] = $wpdb->prepare( "balance.meta_value > {$format}", $this->prefs['min_balance'] );
543
-
544
- }
545
-
546
- // Exclude IDs
547
- if ( isset( $this->prefs['exclude_ids'] ) && $this->prefs['exclude_ids'] != '' ) {
548
- $clean_ids = array();
549
- $the_list = explode( ',', $this->prefs['exclude_ids'] );
550
- foreach ( $the_list as $user_id ) {
551
- $user_id = trim( $user_id );
552
- if ( $user_id == '' || $user_id == 0 ) continue;
553
- $clean_ids[] = (int) $user_id;
554
- }
555
-
556
- if ( count( $clean_ids ) > 0 )
557
- $wheres[] = "users.ID NOT IN (" . implode( ', ', $clean_ids ) . ")";
558
- }
559
-
560
- // Core Excludes
561
- if ( $this->core->exclude['list'] != '' ) {
562
- $clean_ids = array();
563
- $the_list = explode( ',', $this->core->exclude['list'] );
564
- foreach ( $the_list as $user_id ) {
565
- $user_id = trim( $user_id );
566
- if ( $user_id == '' || $user_id == 0 ) continue;
567
- $clean_ids[] = (int) $user_id;
568
- }
569
-
570
- if ( count( $clean_ids ) > 0 )
571
- $wheres[] = "users.ID NOT IN (" . implode( ', ', $clean_ids ) . ")";
572
- }
573
-
574
- // Exclude roles
575
- if ( isset( $this->prefs['exclude_roles'] ) && ! empty( $this->prefs['exclude_roles'] ) ) {
576
- $cap_id = $wpdb->prefix . 'capabilities';
577
-
578
- $joins[] = $wpdb->prepare( "INNER JOIN {$wpdb->usermeta} role ON ( users.ID = role.user_id AND role.meta_key = %s )", $cap_id );
579
-
580
- $excluded_roles = array();
581
- foreach ( $this->prefs['exclude_roles'] as $role_id )
582
- $excluded_roles[] = "'%" . $role_id . "%'";
583
-
584
- $wheres[] = "role.meta_value NOT LIKE " . implode( " AND role.meta_value NOT LIKE ", $excluded_roles );
585
- }
586
-
587
- // Construct Query
588
- $SQL = "SELECT DISTINCT users.ID FROM {$wpdb->users} users ";
589
-
590
- if ( ! empty( $joins ) )
591
- $SQL .= implode( " ", $joins ) . " ";
592
-
593
- if ( ! empty( $wheres ) )
594
- $SQL .= "WHERE " . implode( " AND ", $wheres ) . " ";
595
-
596
- // The Query
597
- $users = $wpdb->get_col( $SQL );
598
- if ( $users === NULL )
599
- $users = array();
600
-
601
- return $users;
602
 
603
  }
604
 
605
  }
606
- }
 
607
  ?>
7
  * @since 1.2
8
  * @version 1.1
9
  */
10
+ if ( ! class_exists( 'myCRED_Service' ) ) :
11
  abstract class myCRED_Service {
12
 
13
  // Service ID
18
 
19
  // Multipoint types
20
  public $is_main_type = true;
21
+ public $mycred_type = MYCRED_DEFAULT_TYPE_KEY;
22
 
23
  // Service Prefs
24
+ public $prefs = array();
25
+ public $now = 0;
26
 
27
  /**
28
  * Construct
29
  */
30
+ function __construct( $args = array(), $service_prefs = NULL, $type = MYCRED_DEFAULT_TYPE_KEY ) {
31
+
32
  if ( ! empty( $args ) ) {
33
  foreach ( $args as $key => $value ) {
34
  $this->$key = $value;
35
  }
36
  }
 
 
 
37
 
38
+ // Grab myCRED Settings
39
+ $this->core = mycred( $type );
40
+ $this->mycred_type = $type;
 
41
 
42
+ if ( $this->mycred_type != MYCRED_DEFAULT_TYPE_KEY )
43
  $this->is_main_type = false;
44
 
45
+ // Prep settings
46
+ if ( ! empty( $this->defaults ) )
47
+ $this->prefs = $this->defaults;
 
 
48
 
49
+ if ( $service_prefs !== NULL && array_key_exists( $this->id, $service_prefs ) )
50
+ $this->prefs = $service_prefs[ $this->id ];
51
+
52
+ $this->prefs = wp_parse_args( $this->prefs, $this->defaults );
53
+ $this->now = current_time( 'timestamp', 1 );
54
 
 
 
 
55
  }
56
 
57
  /**
60
  * @since 1.2
61
  * @version 1.0
62
  */
63
+ public function run() {
64
  wp_die( 'function myCRED_Service::run() must be over-ridden in a sub-class.' );
65
  }
66
 
69
  * @since 0.1
70
  * @version 1.0
71
  */
72
+ public function preferences() { }
 
 
73
 
74
  /**
75
  * Sanitise Preference
76
  * @since 1.2
77
  * @version 1.0
78
  */
79
+ public function sanitise_preferences( $post ) {
80
+
81
  return $post;
82
+
83
  }
84
 
85
  /**
87
  * @since 1.5.2
88
  * @version 1.0
89
  */
90
+ public function activate() { }
91
 
92
  /**
93
  * Deactivate
94
  * @since 1.2
95
  * @version 1.0
96
  */
97
+ public function deactivate() { }
98
 
99
  /**
100
+ * Ajax Handler
101
+ * @since 1.7
 
 
 
 
 
 
 
102
  * @version 1.0
103
  */
104
+ public function ajax_handler() { }
105
 
106
  /**
107
+ * User Screen
108
+ * @since 1.7
109
  * @version 1.0
110
  */
111
+ public function user_screen( $user ) { }
112
 
113
  /**
114
  * Get Field Name
116
  * @since 1.2
117
  * @version 1.1
118
  */
119
+ public function field_name( $field = '' ) {
120
+
121
  if ( is_array( $field ) ) {
122
  $array = array();
123
  foreach ( $field as $parent => $child ) {
132
  else {
133
  $field = '[' . $field . ']';
134
  }
135
+
136
  $option_id = 'mycred_pref_bank';
137
  if ( ! $this->is_main_type )
138
  $option_id = $option_id . '_' . $this->mycred_type;
139
 
140
  return $option_id . '[service_prefs][' . $this->id . ']' . $field;
141
+
142
  }
143
 
144
  /**
147
  * @since 1.2
148
  * @version 1.1
149
  */
150
+ public function field_id( $field = '' ) {
151
+
152
  if ( is_array( $field ) ) {
153
  $array = array();
154
  foreach ( $field as $parent => $child ) {
163
  else {
164
  $field = str_replace( '_', '-', $field );
165
  }
166
+
167
  $option_id = 'mycred_pref_bank';
168
  if ( ! $this->is_main_type )
169
  $option_id = $option_id . '_' . $this->mycred_type;
170
 
171
  $option_id = str_replace( '_', '-', $option_id );
172
  return $option_id . '-' . str_replace( '_', '-', $this->id ) . '-' . $field;
173
+
174
  }
175
 
176
  /**
177
+ * Get Days in Year
178
+ * @since 1.2
179
+ * @version 1.0.2
180
  */
181
+ public function get_days_in_year() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
+ if ( date( 'L' ) )
184
+ $days = 366;
185
+ else
186
+ $days = 365;
 
 
 
187
 
188
+ return apply_filters( 'mycred_banking_days_in_year', $days, $this );
189
 
 
190
  }
191
 
192
  /**
193
+ * Is Large Site?
194
+ * @since 1.7
195
  * @version 1.0
196
  */
197
+ public function is_large_site() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
+ $is_large = false;
200
+ if ( is_multisite() && wp_is_large_network() )
201
+ $is_large = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
 
203
+ else {
204
 
205
+ $users = count_users();
206
+ if ( $users['total_users'] > 3000 )
207
+ $is_large = true;
 
 
 
 
 
 
208
 
209
+ else {
 
 
 
 
 
210
 
211
+ global $wpdb, $mycred;
 
 
212
 
213
+ $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$mycred->log_table};" );
214
+ if ( $total !== NULL && $total > 100000 )
215
+ $is_large = true;
216
 
217
+ }
 
 
 
 
 
 
 
 
218
 
219
+ }
 
 
 
 
 
220
 
221
+ return $is_large;
 
222
 
223
  }
224
 
225
  /**
226
+ * Get Excluded User IDs
227
+ * @since 1.7
228
  * @version 1.0
229
  */
230
+ public function get_excluded_user_ids() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
+ global $wpdb;
 
 
 
 
 
233
 
234
+ $exclude_ids = array();
235
 
236
+ if ( strlen( $this->prefs['exclude_ids'] ) > 0 ) {
 
237
 
238
+ foreach ( explode( ',', $this->prefs['exclude_ids'] ) as $user_id ) {
239
+ $user_id = absint( preg_replace( "/[^0-9]/", "", $user_id ) );
240
+ if ( $user_id === 0 ) continue;
241
+ $exclude_ids[] = $user_id;
242
+ }
243
 
244
+ }
245
 
246
+ if ( ! empty( $this->prefs['exclude_roles'] ) ) {
 
247
 
248
+ $args = array();
249
+ $args['role__in'] = $this->prefs['exclude_roles'];
250
+ $args['fields'] = 'ID';
251
+ $user_query = new WP_User_Query( $args );
252
+ $user_ids = $user_query->get_results();
253
 
254
+ if ( ! empty( $user_ids ) ) {
255
+ foreach ( $user_ids as $user_id ) {
256
+ if ( in_array( $user_id, $exclude_ids ) ) continue;
257
+ $exclude_ids[] = absint( $user_id );
258
+ }
259
+ }
260
 
261
+ }
 
262
 
263
+ if ( $this->core->exclude['list'] != '' ) {
264
 
265
+ foreach ( explode( ',', $this->core->exclude['list'] ) as $user_id ) {
266
+ $user_id = absint( preg_replace( "/[^0-9]/", "", $user_id ) );
267
+ if ( $user_id === 0 || in_array( $user_id, $exclude_ids ) ) continue;
268
+ $exclude_ids[] = $user_id;
269
+ }
270
 
271
+ }
 
 
 
 
 
272
 
273
+ if ( $this->prefs['min_balance'] != '' && $this->core->number( $this->prefs['min_balance'] ) > $this->core->zero() ) {
274
 
275
+ $format = '%d';
276
+ if ( $this->core->format['decimals'] > 0 )
277
+ $format = '%f';
278
 
279
+ $user_ids = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value < {$format};", $this->mycred_type, $this->prefs['min_balance'] ) );
280
+ if ( ! empty( $user_ids ) ) {
281
+ foreach ( $user_ids as $user_id ) {
282
+ if ( in_array( $user_id, $exclude_ids ) ) continue;
283
+ $exclude_ids[] = absint( $user_id );
284
+ }
285
+ }
286
 
287
+ }
288
 
289
+ return $exclude_ids;
290
 
291
+ }
292
 
293
+ /**
294
+ * Convert Date to GMT Timestamp
295
+ * @since 1.7
296
+ * @version 1.0
297
+ */
298
+ public function date_to_timestamp( $string = '' ) {
299
 
300
+ return mycred_date_to_gmt_timestamp( $string );
301
 
302
+ }
303
 
304
+ /**
305
+ * Convert GMT Timestamp to Date
306
+ * @since 1.7
307
+ * @version 1.0
308
+ */
309
+ public function timestamp_to_date( $string = '' ) {
310
 
311
+ return mycred_gmt_timestamp_to_local( $string );
312
 
 
313
  }
314
 
315
  /**
316
+ * Time Select
317
+ * @since 1.7
318
+ * @version 1.0
 
 
319
  */
320
+ public function time_select( $name = '', $id = '', $date = NULL ) {
321
+
322
+ $date = ( ( $date !== false && strlen( $date ) > 0 ) ? date( 'H:i', $date ) : '' );
323
+ $options = mycred_banking_get_time_options();
324
+
325
+ $element = '<select name="' . $name . '" id="' . $id . '" class="form-control">';
326
+ foreach ( $options as $time => $label ) {
327
+ $element .= '<option value="' . $time . '"';
328
+ if ( $date == $time ) $element .= ' selected="selected"';
329
+ $element .= '>' . $label . '</option>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
  }
331
+ $element .= '</select>';
332
+
333
+ return $element;
334
 
 
 
 
 
 
 
 
 
 
 
 
335
  }
336
 
337
  /**
339
  * @since 1.2
340
  * @version 1.0
341
  */
342
+ public function timeframe_dropdown( $pref_id = '', $use_select = true, $hourly = true ) {
343
+
344
+ $timeframes = mycred_banking_get_timeframes();
345
  if ( ! $hourly )
346
  unset( $timeframes['hourly'] );
347
+
348
+ echo '<select name="' . $this->field_name( $pref_id ) . '" id="' . $this->field_id( $pref_id ) . '" class="form-control">';
349
+
350
  if ( $use_select )
351
  echo '<option value="">' . __( 'Select', 'mycred' ) . '</option>';
352
 
359
  elseif ( isset( $this->prefs[ $pref_id ] ) ) {
360
  $settings = $this->prefs[ $pref_id ];
361
  }
362
+
363
  foreach ( $timeframes as $value => $details ) {
364
  echo '<option value="' . $value . '"';
365
  if ( $settings == $value ) echo ' selected="selected"';
366
  echo '>' . $details['label'] . '</option>';
367
  }
368
  echo '</select>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
 
370
  }
371
 
372
  }
373
+ endif;
374
+
375
  ?>
addons/banking/assets/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ ?>
addons/banking/assets/js/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ ?>
addons/banking/assets/js/manage-schedules.js ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(function($) {
2
+
3
+ var RecurringSchedule = $( '#manage-recurring-schedule' );
4
+ var currentBankService = '';
5
+ var wWidth = $(window).width();
6
+ var dWidth = wWidth * 0.75;
7
+
8
+ function check_form_for_empty_fields() {
9
+
10
+ var emptyfields = 0;
11
+
12
+ $( '#manage-recurring-schedule-form input.cant-be-empty' ).each(function(index){
13
+
14
+ var fieldvalue = $(this).val();
15
+ if ( fieldvalue.length == 0 ) {
16
+ $(this).parent().addClass( 'has-error' );
17
+ emptyfields++;
18
+ }
19
+ else
20
+ $(this).parent().removeClass( 'has-error' );
21
+
22
+ });
23
+
24
+ if ( emptyfields > 0 )
25
+ return false;
26
+
27
+ else return true;
28
+
29
+ };
30
+
31
+ $(document).ready(function(){
32
+
33
+ if ( dWidth < 250 )
34
+ dWidth = wWidth;
35
+
36
+ if ( dWidth > 960 )
37
+ dWidth = 960;
38
+
39
+ /**
40
+ * Setup Schedule Modal
41
+ */
42
+ RecurringSchedule.dialog({
43
+ dialogClass : 'mycred-update-balance mycred-metabox',
44
+ draggable : true,
45
+ autoOpen : false,
46
+ title : Banking.new,
47
+ closeText : Banking.close,
48
+ modal : true,
49
+ width : dWidth,
50
+ height : 'auto',
51
+ resizable : false,
52
+ position : { my: "center", at: "top+25%", of: window },
53
+ show : {
54
+ effect : 'fadeIn',
55
+ duration : 250
56
+ },
57
+ hide : {
58
+ effect : 'fadeOut',
59
+ duration : 250
60
+ }
61
+ });
62
+
63
+ RecurringSchedule.on( "dialogclose", function( event, ui ) {
64
+
65
+ $( '#manage-recurring-schedule-form' ).empty();
66
+ $( '#mycred-processing' ).show();
67
+
68
+ } );
69
+
70
+ // Schedule
71
+ $( '#add-new-schedule' ).click(function(e){
72
+
73
+ e.preventDefault();
74
+
75
+ $(this).blur();
76
+
77
+ RecurringSchedule.dialog({ title : Banking.new });
78
+ RecurringSchedule.dialog( 'open' );
79
+ $( '#manage-recurring-schedule-form' ).submit();
80
+
81
+ });
82
+
83
+ // Form submissions
84
+ $( '#manage-recurring-schedule-form' ).on( 'submit', function(e){
85
+
86
+ e.preventDefault();
87
+
88
+ if ( check_form_for_empty_fields() === false ) {
89
+ alert( Banking.emptyfields );
90
+ return false;
91
+ }
92
+
93
+ $( '#mycred-processing' ).show();
94
+
95
+ var thisform = $(this);
96
+
97
+ $.ajax({
98
+ type : 'POST',
99
+ data : {
100
+ action : 'run-mycred-bank-service',
101
+ _token : Banking.token,
102
+ service : 'payouts',
103
+ form : thisform.serialize()
104
+ },
105
+ dataType : 'JSON',
106
+ url : Banking.ajaxurl,
107
+ beforeSend : function(){
108
+ thisform.slideUp();
109
+ },
110
+ success : function( response ) {
111
+
112
+ if ( response.success === undefined ) {
113
+ location.reload();
114
+ return false;
115
+ }
116
+
117
+ $( '#mycred-processing' ).hide();
118
+
119
+ thisform.empty().append( response.data.form ).slideDown();
120
+
121
+ if ( response.data.table !== false ) {
122
+
123
+ var scheduletable = $( '#recurring-schedule-body tr' );
124
+ if ( scheduletable.length == 1 )
125
+ $( '#no-banking-schedules' ).hide();
126
+
127
+ $( '#recurring-schedule-body #no-banking-schedules' ).before( response.data.table );
128
+
129
+ }
130
+ else {
131
+ var scheduletable = $( '#recurring-schedule-body tr' );
132
+ if ( scheduletable.length == 1 )
133
+ $( '#no-banking-schedules' ).show();
134
+ }
135
+
136
+ }
137
+ });
138
+
139
+ });
140
+
141
+ // View Schedule
142
+ $( '#recurring-schedule-body' ).on( 'click', 'a.view-recurring-schedule', function(e){
143
+
144
+ e.preventDefault();
145
+
146
+ $(this).blur();
147
+
148
+ var scheduleid = $(this).data( 'id' );
149
+ $( '#manage-recurring-schedule-form' ).append( '<input type="hidden" name="schedule_id" value="' + scheduleid + '" />' );
150
+
151
+ RecurringSchedule.dialog({ title : $(this).data( 'title' ) });
152
+ RecurringSchedule.dialog( 'open' );
153
+ $( '#manage-recurring-schedule-form' ).submit();
154
+
155
+ });
156
+
157
+ // Delete Schedule
158
+ $( '#recurring-schedule-body' ).on( 'click', 'a.delete-recurring-schedule', function(e){
159
+
160
+ e.preventDefault();
161
+
162
+ $(this).blur();
163
+
164
+ var scheduleid = $(this).data( 'id' );
165
+ $( '#manage-recurring-schedule-form' ).append( '<input type="hidden" name="remove_token" value="' + scheduleid + '" />' );
166
+
167
+ if ( confirm( Banking.confirmremoval ) ) {
168
+ RecurringSchedule.dialog({ title : $(this).data( 'title' ) });
169
+ RecurringSchedule.dialog( 'open' );
170
+ $( '#manage-recurring-schedule-form' ).submit();
171
+ }
172
+ else {
173
+ $( '#manage-recurring-schedule-form' ).empty();
174
+ }
175
+
176
+ });
177
+
178
+ });
179
+
180
+ });
addons/banking/includes/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ ?>
addons/banking/includes/mycred-banking-functions.php ADDED
@@ -0,0 +1,391 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * Get Next Payout
6
+ * Adds seconds to a given time based on the payout period set.
7
+ * @since 1.7
8
+ * @version 1.0
9
+ */
10
+ if ( ! function_exists( 'mycred_get_banking_addon_settings' ) ) :
11
+ function mycred_get_banking_addon_settings( $service = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
12
+
13
+ $default = array(
14
+ 'active' => array(),
15
+ 'services' => array(),
16
+ 'service_prefs' => array()
17
+ );
18
+
19
+ $option_id = 'mycred_pref_bank';
20
+ if ( $point_type != MYCRED_DEFAULT_TYPE_KEY )
21
+ $option_id .= '_' . $point_type;
22
+
23
+ $settings = mycred_get_option( $option_id, $default );
24
+ $settings = wp_parse_args( $settings, $default );
25
+
26
+ if ( $service !== NULL && array_key_exists( $service, $settings['service_prefs'] ) )
27
+ $settings = $settings['service_prefs'][ $service ];
28
+
29
+ return $settings;
30
+
31
+ }
32
+ endif;
33
+
34
+ /**
35
+ * Get Next Payout
36
+ * Adds seconds to a given time based on the payout period set.
37
+ * @since 1.7
38
+ * @version 1.0
39
+ */
40
+ if ( ! function_exists( 'mycred_banking_get_next_payout' ) ) :
41
+ function mycred_banking_get_next_payout( $prefs = '', $time = 0 ) {
42
+
43
+ if ( $prefs == 'hourly' )
44
+ return $time + HOUR_IN_SECONDS;
45
+
46
+ elseif ( $prefs == 'daily' )
47
+ return $time + DAY_IN_SECONDS;
48
+
49
+ elseif ( $prefs == 'weekly' )
50
+ return $time + WEEK_IN_SECONDS;
51
+
52
+ elseif ( $prefs == 'monthly' )
53
+ return $time + MONTH_IN_SECONDS;
54
+
55
+ elseif ( $prefs == 'quarterly' )
56
+ return $time + ( MONTH_IN_SECONDS * 4 );
57
+
58
+ elseif ( $prefs == 'semiannually' )
59
+ return $time + ( MONTH_IN_SECONDS * 6 );
60
+
61
+ elseif ( $prefs == 'annually' )
62
+ return $time + YEAR_IN_SECONDS;
63
+
64
+ return apply_filters( 'mycred_banking_get_next_payout', $time, $prefs );
65
+
66
+ }
67
+ endif;
68
+
69
+ /**
70
+ * Get Timeframes
71
+ * @since 1.7
72
+ * @version 1.0
73
+ */
74
+ if ( ! function_exists( 'mycred_banking_get_timeframes' ) ) :
75
+ function mycred_banking_get_timeframes() {
76
+
77
+ $timeframes = array(
78
+ 'hourly' => array(
79
+ 'label' => __( 'Hourly', 'mycred' ),
80
+ 'single' => __( 'Hour', 'mycred' ),
81
+ 'date_format' => 'G'
82
+ ),
83
+ 'daily' => array(
84
+ 'label' => __( 'Daily', 'mycred' ),
85
+ 'single' => __( 'Day', 'mycred' ),
86
+ 'date_format' => 'z'
87
+ ),
88
+ 'weekly' => array(
89
+ 'label' => __( 'Weekly', 'mycred' ),
90
+ 'single' => __( 'Week', 'mycred' ),
91
+ 'date_format' => 'W'
92
+ ),
93
+ 'monthly' => array(
94
+ 'label' => __( 'Monthly', 'mycred' ),
95
+ 'single' => __( 'Month', 'mycred' ),
96
+ 'date_format' => 'M'
97
+ ),
98
+ 'quarterly' => array(
99
+ 'label' => __( 'Quarterly', 'mycred' ),
100
+ 'single' => __( 'Quarter', 'mycred' ),
101
+ 'date_format' => 'Y'
102
+ ),
103
+ 'semiannually' => array(
104
+ 'label' => __( 'Semiannually', 'mycred' ),
105
+ 'single' => __( 'Semiannual', 'mycred' ),
106
+ 'date_format' => 'Y'
107
+ ),
108
+ 'annually' => array(
109
+ 'label' => __( 'Annually', 'mycred' ),
110
+ 'single' => __( 'Annual', 'mycred' ),
111
+ 'date_format' => 'Y'
112
+ )
113
+ );
114
+
115
+ return apply_filters( 'mycred_banking_timeframes', $timeframes );
116
+
117
+ }
118
+ endif;
119
+
120
+ /**
121
+ * Get Time Options
122
+ * Returns an array of 24 hours with support for either 24 or 12 hour formats.
123
+ * @since 1.7
124
+ * @version 1.0
125
+ */
126
+ if ( ! function_exists( 'mycred_banking_get_time_options' ) ) :
127
+ function mycred_banking_get_time_options( $twelve_hour = false ) {
128
+
129
+ $options = array(
130
+ '00:00' => '00:00',
131
+ '01:00' => '01:00',
132
+ '02:00' => '02:00',
133
+ '03:00' => '03:00',
134
+ '04:00' => '04:00',
135
+ '05:00' => '05:00',
136
+ '06:00' => '06:00',
137
+ '07:00' => '07:00',
138
+ '08:00' => '08:00',
139
+ '09:00' => '09:00',
140
+ '10:00' => '10:00',
141
+ '11:00' => '11:00',
142
+ '12:00' => '12:00',
143
+ '13:00' => '13:00',
144
+ '14:00' => '14:00',
145
+ '15:00' => '15:00',
146
+ '16:00' => '16:00',
147
+ '17:00' => '17:00',
148
+ '18:00' => '18:00',
149
+ '19:00' => '19:00',
150
+ '20:00' => '20:00',
151
+ '21:00' => '21:00',
152
+ '22:00' => '22:00',
153
+ '23:00' => '23:00'
154
+ );
155
+
156
+ $time_format = get_option( 'time_format' );
157
+ if ( str_replace( array( 'a', 'A' ), '', $time_format ) != $time_format || $twelve_hour )
158
+ $options = array(
159
+ '00:00' => '12:00 AM',
160
+ '01:00' => '1:00 AM',
161
+ '02:00' => '2:00 AM',
162
+ '03:00' => '3:00 AM',
163
+ '04:00' => '4:00 AM',
164
+ '05:00' => '5:00 AM',
165
+ '06:00' => '6:00 AM',
166
+ '07:00' => '7:00 AM',
167
+ '08:00' => '8:00 AM',
168
+ '09:00' => '9:00 AM',
169
+ '10:00' => '10:00 AM',
170
+ '11:00' => '11:00 AM',
171
+ '12:00' => '12:00 PM',
172
+ '13:00' => '1:00 PM',
173
+ '14:00' => '2:00 PM',
174
+ '15:00' => '3:00 PM',
175
+ '16:00' => '4:00 PM',
176
+ '17:00' => '5:00 PM',
177
+ '18:00' => '6:00 PM',
178
+ '19:00' => '7:00 PM',
179
+ '20:00' => '8:00 PM',
180
+ '21:00' => '9:00 PM',
181
+ '22:00' => '10:00 PM',
182
+ '23:00' => '11:00 PM'
183
+ );
184
+
185
+ return apply_filters( 'mycred_banking_time_options', $options );
186
+
187
+ }
188
+ endif;
189
+
190
+ /**
191
+ * Get Recurring Payout Schedules
192
+ * @since 1.7
193
+ * @version 1.0
194
+ */
195
+ if ( ! function_exists( 'mycred_get_recurring_payout_schedules' ) ) :
196
+ function mycred_get_recurring_payout_schedules( $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
197
+
198
+ $schedules = mycred_get_option( 'mycred-recurring-schedule-' . $point_type, array() );
199
+ $defaults = mycred_get_recurring_payout_defaults();
200
+
201
+ $results = array();
202
+ if ( ! empty( $schedules ) ) {
203
+
204
+ foreach ( $schedules as $schedule_id => $setup )
205
+ $results[ $schedule_id ] = shortcode_atts( $defaults, $setup );
206
+
207
+ }
208
+
209
+ return $results;
210
+
211
+ }
212
+ endif;
213
+
214
+ /**
215
+ * Get Recurring Payout Defaults
216
+ * @since 1.7
217
+ * @version 1.0
218
+ */
219
+ if ( ! function_exists( 'mycred_get_recurring_payout_defaults' ) ) :
220
+ function mycred_get_recurring_payout_defaults() {
221
+
222
+ $defaults = array(
223
+ 'job_title' => '',
224
+ 'status' => 0,
225
+ 'payout' => '',
226
+ 'frequency' => 'daily',
227
+ 'last_run' => '',
228
+ 'total_runs' => 1,
229
+ 'runs_remaining' => 0,
230
+ 'min_balance' => 0,
231
+ 'max_balance' => 0,
232
+ 'id_exclude' => 'exclude',
233
+ 'id_list' => '',
234
+ 'role_exclude' => 'exclude',
235
+ 'role_list' => array(),
236
+ 'log_template' => '%plural% payout',
237
+ 'total_completed' => 0,
238
+ 'total_misses' => 0,
239
+ 'ignore_central' => 0
240
+ );
241
+
242
+ return $defaults;
243
+
244
+ }
245
+ endif;
246
+
247
+ /**
248
+ * Get Recurring Payout
249
+ * Returns the settings of a specific recurring payout.
250
+ * @since 1.7
251
+ * @version 1.0
252
+ */
253
+ if ( ! function_exists( 'mycred_get_recurring_payout' ) ) :
254
+ function mycred_get_recurring_payout( $schedule_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
255
+
256
+ $result = false;
257
+ $schedules = mycred_get_recurring_payout_schedules( $point_type );
258
+
259
+ if ( array_key_exists( $schedule_id, $schedules ) )
260
+ $result = $schedules[ $schedule_id ];
261
+
262
+ return $result;
263
+
264
+ }
265
+ endif;
266
+
267
+ /**
268
+ * Add New Recurring Payout
269
+ * @since 1.7
270
+ * @version 1.0
271
+ */
272
+ if ( ! function_exists( 'mycred_add_new_recurring_payout' ) ) :
273
+ function mycred_add_new_recurring_payout( $new_id = '', $schedule_setup = array(), $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
274
+
275
+ $default = mycred_get_recurring_payout_defaults();
276
+ $new_id = sanitize_key( $new_id );
277
+ $schedule_setup = shortcode_atts( $default, $schedule_setup );
278
+ $mycred = mycred( $point_type );
279
+
280
+ // Validate first
281
+ $title = sanitize_text_field( $schedule_setup['job_title'] );
282
+ if ( $title == '' )
283
+ return new WP_Error( 'invalid', __( 'A title must be set.', 'mycred' ) );
284
+
285
+ $schedule_setup['job_title'] = $title;
286
+
287
+ $payout = sanitize_text_field( $schedule_setup['payout'] );
288
+ $payout = $mycred->number( $payout );
289
+ if ( $payout == $mycred->zero() )
290
+ return new WP_Error( 'invalid', __( 'The amount to payout can not be zero.', 'mycred' ) );
291
+
292
+ $schedule_setup['payout'] = $payout;
293
+
294
+ $last_run = absint( $schedule_setup['last_run'] );
295
+ if ( $last_run < time() )
296
+ return new WP_Error( 'invalid', __( 'Start date can not be in the past.', 'mycred' ) );
297
+
298
+ $schedule_setup['last_run'] = $last_run;
299
+
300
+ $total_runs = sanitize_text_field( $schedule_setup['total_runs'] );
301
+ if ( $total_runs < -1 ) $total_runs = -1;
302
+ if ( $total_runs == 0 )
303
+ return new WP_Error( 'invalid', __( 'Repeat can not be zero.', 'mycred' ) );
304
+
305
+ $schedule_setup['total_runs'] = $total_runs;
306
+
307
+ $schedules = mycred_get_recurring_payout_schedules( $point_type );
308
+ if ( array_key_exists( $new_id, $schedules ) )
309
+ return new WP_Error( 'invalid', __( 'Duplicate schedule.', 'mycred' ) );
310
+
311
+ $schedule_setup['status'] = 0;
312
+ $schedule_setup['runs_remaining'] = $schedule_setup['total_runs'];
313
+
314
+ $schedules[ $new_id ] = $schedule_setup;
315
+
316
+ // Save
317
+ mycred_update_option( 'mycred-recurring-schedule-' . $point_type, $schedules );
318
+
319
+ // Add Schedule to CRON
320
+ wp_schedule_single_event( $last_run, 'mycred-recurring-' . $new_id, array( 'id' => $new_id ) );
321
+
322
+ return $schedule_setup;
323
+
324
+ }
325
+ endif;
326
+
327
+ /**
328
+ * Update Recurring Payout
329
+ * @since 1.7
330
+ * @version 1.0
331
+ */
332
+ if ( ! function_exists( 'mycred_update_recurring_payout' ) ) :
333
+ function mycred_update_recurring_payout( $schedule_id = '', $schedule_setup = array(), $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
334
+
335
+ $result = false;
336
+ $setup = mycred_get_recurring_payout( $schedule_id, $point_type );
337
+
338
+ if ( is_array( $setup ) ) {
339
+
340
+ $schedules = mycred_get_recurring_payout_schedules( $point_type );
341
+
342
+ $schedules[ $schedule_id ] = shortcode_atts( $setup, $schedule_setup );
343
+
344
+ mycred_update_option( 'mycred-recurring-schedule-' . $point_type, $schedules );
345
+
346
+ $result = true;
347
+
348
+ }
349
+
350
+ return $result;
351
+
352
+ }
353
+ endif;
354
+
355
+ /**
356
+ * Delete Recurring Payout
357
+ * @since 1.7
358
+ * @version 1.0
359
+ */
360
+ if ( ! function_exists( 'mycred_delete_recurring_payout' ) ) :
361
+ function mycred_delete_recurring_payout( $id_to_remove = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
362
+
363
+ $result = false;
364
+ $schedules = mycred_get_recurring_payout_schedules( $point_type );
365
+
366
+ if ( ! empty( $schedules ) ) {
367
+
368
+ $new_schedule = array();
369
+ foreach ( $schedules as $schedule_id => $schedule_setup ) {
370
+
371
+ if ( $schedule_id == $id_to_remove ) continue;
372
+ $new_schedule[ $schedule_id ] = $schedule_setup;
373
+
374
+ }
375
+
376
+ // Save
377
+ mycred_update_option( 'mycred-recurring-schedule-' . $point_type, $new_schedule );
378
+
379
+ // Clear schedule in CRON
380
+ wp_clear_scheduled_hook( 'mycred-recurring-' . $schedule_id, array( 'id' => $schedule_id ) );
381
+
382
+ $result = true;
383
+
384
+ }
385
+
386
+ return $result;
387
+
388
+ }
389
+ endif;
390
+
391
+ ?>
addons/banking/myCRED-addon-banking.php CHANGED
@@ -1,29 +1,29 @@
1
  <?php
 
 
2
  /**
3
  * Addon: Banking
4
  * Addon URI: http://mycred.me/add-ons/banking/
5
- * Version: 1.2
6
- * Description: Setup recurring payouts or offer / charge interest on user account balances.
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
- if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
-
12
  define( 'myCRED_BANK', __FILE__ );
13
  define( 'myCRED_BANK_DIR', myCRED_ADDONS_DIR . 'banking/' );
14
  define( 'myCRED_BANK_ABSTRACT_DIR', myCRED_BANK_DIR . 'abstracts/' );
 
15
  define( 'myCRED_BANK_SERVICES_DIR', myCRED_BANK_DIR . 'services/' );
16
 
17
  require_once myCRED_BANK_ABSTRACT_DIR . 'mycred-abstract-service.php';
18
 
19
- require_once myCRED_BANK_SERVICES_DIR . 'mycred-bank-service-central.php';
20
- require_once myCRED_BANK_SERVICES_DIR . 'mycred-bank-service-interest.php';
21
- require_once myCRED_BANK_SERVICES_DIR . 'mycred-bank-service-payouts.php';
 
 
22
 
23
  /**
24
  * myCRED_Banking_Module class
25
  * @since 0.1
26
- * @version 1.2
27
  */
28
  if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
29
  class myCRED_Banking_Module extends myCRED_Module {
@@ -31,7 +31,7 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
31
  /**
32
  * Constructor
33
  */
34
- public function __construct( $type = 'mycred_default' ) {
35
 
36
  parent::__construct( 'myCRED_Banking_Module', array(
37
  'module_name' => 'banking',
@@ -46,15 +46,11 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
46
  'page_title' => __( 'Banking', 'mycred' ),
47
  'page_header' => __( 'Banking', 'mycred' )
48
  ),
49
- 'screen_id' => 'myCRED_page_banking',
50
  'accordion' => true,
51
- 'menu_pos' => 30
52
  ), $type );
53
 
54
- add_action( 'mycred_edit_profile', array( $this, 'user_level_override' ), 30, 2 );
55
- add_action( 'mycred_edit_profile_action', array( $this, 'save_user_level_override' ) );
56
- add_action( 'mycred_admin_notices', array( $this, 'update_user_level_profile_notice' ) );
57
-
58
  }
59
 
60
  /**
@@ -65,65 +61,30 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
65
  public function module_init() {
66
 
67
  if ( ! empty( $this->services ) ) {
 
68
  foreach ( $this->services as $key => $gdata ) {
 
69
  if ( $this->is_active( $key ) && isset( $gdata['callback'] ) ) {
70
  $this->call( 'run', $gdata['callback'] );
71
  }
72
- }
73
- }
74
 
75
- }
76
-
77
- /**
78
- * User Level Override
79
- * @since 1.5.2
80
- * @version 1.0
81
- */
82
- public function user_level_override( $user, $type ) {
83
-
84
- if ( $this->mycred_type != $type ) return;
85
-
86
- if ( ! empty( $this->services ) ) {
87
- foreach ( $this->services as $key => $gdata ) {
88
- if ( $this->is_active( $key ) && isset( $gdata['callback'] ) ) {
89
- $this->call( 'user_override', $gdata['callback'], $user, $type );
90
- }
91
  }
92
- }
93
-
94
- }
95
-
96
- /**
97
- * Save User Level Override
98
- * @since 1.5.2
99
- * @version 1.0
100
- */
101
- public function save_user_level_override() {
102
 
103
- if ( ! empty( $this->services ) ) {
104
- foreach ( $this->services as $key => $gdata ) {
105
- if ( $this->is_active( $key ) && isset( $gdata['callback'] ) ) {
106
- $this->call( 'save_user_override', $gdata['callback'] );
107
- }
108
- }
109
  }
110
 
 
 
111
  }
112
 
113
  /**
114
- * User Level Profile Notice
115
- * @since 1.5.2
116
  * @version 1.0
117
  */
118
- public function update_user_level_profile_notice() {
119
 
120
- if ( ! empty( $this->services ) ) {
121
- foreach ( $this->services as $key => $gdata ) {
122
- if ( $this->is_active( $key ) && isset( $gdata['callback'] ) ) {
123
- $this->call( 'user_override_notice', $gdata['callback'] );
124
- }
125
- }
126
- }
127
 
128
  }
129
 
@@ -131,28 +92,36 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
131
  * Call
132
  * Either runs a given class method or function.
133
  * @since 1.2
134
- * @version 1.1.1
135
  */
136
- public function call( $call, $callback, $return = NULL, $var1 = NULL, $var2 = NULL, $var3 = NULL ) {
137
 
138
  // Class
139
  if ( is_array( $callback ) && class_exists( $callback[0] ) ) {
 
140
  $class = $callback[0];
141
  $methods = get_class_methods( $class );
142
  if ( in_array( $call, $methods ) ) {
 
143
  $new = new $class( ( isset( $this->service_prefs ) ) ? $this->service_prefs : array(), $this->mycred_type );
144
- return $new->$call( $return, $var1, $var2, $var3 );
 
145
  }
 
146
  }
147
 
148
  // Function
149
  elseif ( ! is_array( $callback ) ) {
 
150
  if ( function_exists( $callback ) ) {
 
151
  if ( $return !== NULL )
152
  return call_user_func( $callback, $return, $this );
153
  else
154
  return call_user_func( $callback, $this );
 
155
  }
 
156
  }
157
 
158
  if ( $return !== NULL )
@@ -170,14 +139,18 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
170
  // Savings
171
  $services['central'] = array(
172
  'title' => __( 'Central Banking', 'mycred' ),
173
- 'description' => __( 'Instead of creating %_plural% out of thin-air, all payouts are made from a nominated "Central Bank" account. Any %_plural% a user spends or loses are deposited back into this account.', 'mycred' ),
 
 
174
  'callback' => array( 'myCRED_Banking_Service_Central' )
175
  );
176
 
177
  // Interest
178
  $services['interest'] = array(
179
  'title' => __( 'Compound Interest', 'mycred' ),
180
- 'description' => __( 'Apply a positive or negative interest rate on your users %_plural% balances.', 'mycred' ),
 
 
181
  'callback' => array( 'myCRED_Banking_Service_Interest' )
182
  );
183
 
@@ -185,6 +158,8 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
185
  $services['payouts'] = array(
186
  'title' => __( 'Recurring Payouts', 'mycred' ),
187
  'description' => __( 'Setup mass %_singular% payouts for your users.', 'mycred' ),
 
 
188
  'callback' => array( 'myCRED_Banking_Service_Payouts' )
189
  );
190
 
@@ -213,14 +188,45 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
213
 
214
  $banking_icons = plugins_url( 'assets/images/gateway-icons.png', myCRED_THIS );
215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  ?>
217
- <!-- Banking Add-on -->
218
  <style type="text/css">
219
- #myCRED-wrap #accordion h4 .gate-icon { display: block; width: 48px; height: 48px; margin: 0 0 0 0; padding: 0; float: left; line-height: 48px; }
220
- #myCRED-wrap #accordion h4 .gate-icon { background-repeat: no-repeat; background-image: url("<?php echo $banking_icons; ?>"); background-position: 0 0; }
221
- #myCRED-wrap #accordion h4 .gate-icon.inactive { background-position-x: 0; }
222
- #myCRED-wrap #accordion h4 .gate-icon.active { background-position-x: -48px; }
223
- #myCRED-wrap #accordion h4 .gate-icon.sandbox { background-position-x: -96px; }
 
 
 
 
 
 
 
 
 
 
 
 
224
  </style>
225
  <?php
226
 
@@ -234,28 +240,18 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
234
  public function admin_page() {
235
 
236
  // Security
237
- if ( ! $this->core->can_edit_creds() )
238
- wp_die( __( 'Access Denied', 'mycred' ) );
239
 
240
  // Get installed
241
- $installed = $this->get( true );
242
 
243
  ?>
244
- <div class="wrap" id="myCRED-wrap">
245
- <h2><?php echo sprintf( __( '%s Banking', 'mycred' ), mycred_label() ); ?></h2>
246
 
247
  <?php $this->update_notice(); ?>
248
 
249
- <p><?php echo $this->core->template_tags_general( __( 'Your banking setup for %plural%.', 'mycred' ) ); ?></p>
250
-
251
- <?php if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) : ?>
252
-
253
- <p><strong><?php _e( 'WP-Cron deactivation detected!', 'mycred' ); ?></strong></p>
254
- <p><?php _e( 'Warning! This add-on requires WP - Cron to work.', 'mycred' ); ?></p>
255
-
256
- <?php endif; ?>
257
-
258
- <form method="post" action="options.php">
259
 
260
  <?php settings_fields( $this->settings_name ); ?>
261
 
@@ -268,9 +264,12 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
268
  foreach ( $installed as $key => $data ) {
269
 
270
  ?>
271
- <h4><div class="gate-icon <?php if ( $this->is_active( $key ) ) echo 'active'; else echo 'inactive'; ?>"></div><?php echo $this->core->template_tags_general( $data['title'] ); ?></h4>
272
- <div class="body" style="display:none;">
273
  <p><?php echo nl2br( $this->core->template_tags_general( $data['description'] ) ); ?></p>
 
 
 
274
  <label class="subheader"><?php _e( 'Enable', 'mycred' ); ?></label>
275
  <ol>
276
  <li>
@@ -294,6 +293,15 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
294
 
295
  </form>
296
  </div>
 
 
 
 
 
 
 
 
 
297
  <?php
298
 
299
  }
@@ -305,16 +313,18 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
305
  */
306
  public function sanitize_settings( $post ) {
307
 
308
- // Loop though all installed hooks
309
- $installed = $this->get();
310
 
311
  // Construct new settings
 
312
  $new_post['services'] = $installed;
 
313
  if ( empty( $post['active'] ) || ! isset( $post['active'] ) )
314
  $post['active'] = array();
315
 
316
- $new_post['active'] = $post['active'];
317
 
 
318
  if ( ! empty( $installed ) ) {
319
  foreach ( $installed as $key => $data ) {
320
 
@@ -348,25 +358,78 @@ if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
348
  }
349
  }
350
 
351
- $installed = NULL;
352
  return $new_post;
353
 
354
  }
355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  }
357
  endif;
358
 
359
- add_action( 'mycred_pre_init', 'mycred_load_banking_addon' );
360
- function mycred_load_banking_addon() {
 
 
 
 
 
 
 
 
 
 
361
 
362
- global $mycred_modules;
363
 
364
- $mycred_types = mycred_get_types();
365
- foreach ( $mycred_types as $type => $title ) {
366
- $mycred_modules[ $type ]['banking'] = new myCRED_Banking_Module( $type );
367
- $mycred_modules[ $type ]['banking']->load();
368
  }
369
-
370
- }
371
 
372
  ?>
1
  <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
  /**
5
  * Addon: Banking
6
  * Addon URI: http://mycred.me/add-ons/banking/
7
+ * Version: 2.0
 
 
 
8
  */
 
 
9
  define( 'myCRED_BANK', __FILE__ );
10
  define( 'myCRED_BANK_DIR', myCRED_ADDONS_DIR . 'banking/' );
11
  define( 'myCRED_BANK_ABSTRACT_DIR', myCRED_BANK_DIR . 'abstracts/' );
12
+ define( 'myCRED_BANK_INCLUDES_DIR', myCRED_BANK_DIR . 'includes/' );
13
  define( 'myCRED_BANK_SERVICES_DIR', myCRED_BANK_DIR . 'services/' );
14
 
15
  require_once myCRED_BANK_ABSTRACT_DIR . 'mycred-abstract-service.php';
16
 
17
+ require_once myCRED_BANK_INCLUDES_DIR . 'mycred-banking-functions.php';
18
+
19
+ require_once myCRED_BANK_SERVICES_DIR . 'mycred-service-central.php';
20
+ require_once myCRED_BANK_SERVICES_DIR . 'mycred-service-interest.php';
21
+ require_once myCRED_BANK_SERVICES_DIR . 'mycred-service-payouts.php';
22
 
23
  /**
24
  * myCRED_Banking_Module class
25
  * @since 0.1
26
+ * @version 2.0
27
  */
28
  if ( ! class_exists( 'myCRED_Banking_Module' ) ) :
29
  class myCRED_Banking_Module extends myCRED_Module {
31
  /**
32
  * Constructor
33
  */
34
+ public function __construct( $type = MYCRED_DEFAULT_TYPE_KEY ) {
35
 
36
  parent::__construct( 'myCRED_Banking_Module', array(
37
  'module_name' => 'banking',
46
  'page_title' => __( 'Banking', 'mycred' ),
47
  'page_header' => __( 'Banking', 'mycred' )
48
  ),
49
+ 'screen_id' => MYCRED_SLUG . '-banking',
50
  'accordion' => true,
51
+ 'menu_pos' => 60
52
  ), $type );
53
 
 
 
 
 
54
  }
55
 
56
  /**
61
  public function module_init() {
62
 
63
  if ( ! empty( $this->services ) ) {
64
+
65
  foreach ( $this->services as $key => $gdata ) {
66
+
67
  if ( $this->is_active( $key ) && isset( $gdata['callback'] ) ) {
68
  $this->call( 'run', $gdata['callback'] );
69
  }
 
 
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  }
 
 
 
 
 
 
 
 
 
 
72
 
 
 
 
 
 
 
73
  }
74
 
75
+ add_action( 'wp_ajax_run-mycred-bank-service', array( $this, 'ajax_handler' ) );
76
+
77
  }
78
 
79
  /**
80
+ * Module Admin Init
81
+ * @since 1.7
82
  * @version 1.0
83
  */
84
+ public function module_admin_init() {
85
 
86
+ // User Override
87
+ add_action( 'mycred_user_edit_after_' . $this->mycred_type, array( $this, 'banking_user_screen' ), 20 );
 
 
 
 
 
88
 
89
  }
90
 
92
  * Call
93
  * Either runs a given class method or function.
94
  * @since 1.2
95
+ * @version 1.2
96
  */
97
+ public function call( $call, $callback, $return = NULL ) {
98
 
99
  // Class
100
  if ( is_array( $callback ) && class_exists( $callback[0] ) ) {
101
+
102
  $class = $callback[0];
103
  $methods = get_class_methods( $class );
104
  if ( in_array( $call, $methods ) ) {
105
+
106
  $new = new $class( ( isset( $this->service_prefs ) ) ? $this->service_prefs : array(), $this->mycred_type );
107
+ return $new->$call( $return );
108
+
109
  }
110
+
111
  }
112
 
113
  // Function
114
  elseif ( ! is_array( $callback ) ) {
115
+
116
  if ( function_exists( $callback ) ) {
117
+
118
  if ( $return !== NULL )
119
  return call_user_func( $callback, $return, $this );
120
  else
121
  return call_user_func( $callback, $this );
122
+
123
  }
124
+
125
  }
126
 
127
  if ( $return !== NULL )
139
  // Savings
140
  $services['central'] = array(
141
  'title' => __( 'Central Banking', 'mycred' ),
142
+ 'description' => __( 'Instead of creating %_plural% out of thin-air, all payouts are made from a nominated "Central Bank" account. Any %_plural% a user spends or loses are deposited back into this account. If the central bank runs out of %_plural%, no %_plural% will be paid out.', 'mycred' ),
143
+ 'cron' => false,
144
+ 'icon' => 'dashicons-admin-site',
145
  'callback' => array( 'myCRED_Banking_Service_Central' )
146
  );
147
 
148
  // Interest
149
  $services['interest'] = array(
150
  'title' => __( 'Compound Interest', 'mycred' ),
151
+ 'description' => __( 'Offer your users interest on the %_plural% they earn on your website. The interest is compounded daily.', 'mycred' ),
152
+ 'cron' => true,
153
+ 'icon' => 'dashicons-vault',
154
  'callback' => array( 'myCRED_Banking_Service_Interest' )
155
  );
156
 
158
  $services['payouts'] = array(
159
  'title' => __( 'Recurring Payouts', 'mycred' ),
160
  'description' => __( 'Setup mass %_singular% payouts for your users.', 'mycred' ),
161
+ 'cron' => true,
162
+ 'icon' => 'dashicons-update',
163
  'callback' => array( 'myCRED_Banking_Service_Payouts' )
164
  );
165
 
188
 
189
  $banking_icons = plugins_url( 'assets/images/gateway-icons.png', myCRED_THIS );
190
 
191
+ wp_enqueue_style( 'mycred-bootstrap-grid' );
192
+ wp_enqueue_style( 'mycred-forms' );
193
+
194
+ wp_register_script( 'mycred-bank-manage-schedules', plugins_url( 'assets/js/manage-schedules.js', myCRED_BANK ), array( 'jquery', 'jquery-ui-core', 'jquery-ui-dialog', 'jquery-effects-core', 'jquery-effects-slide', 'jquery-numerator' ), myCRED_VERSION );
195
+
196
+ wp_localize_script(
197
+ 'mycred-bank-manage-schedules',
198
+ 'Banking',
199
+ array(
200
+ 'ajaxurl' => admin_url( 'admin-ajax.php' ),
201
+ 'token' => wp_create_nonce( 'run-mycred-bank-task' . $this->mycred_type ),
202
+ 'new' => esc_attr__( 'New Recurring Payout', 'mycred' ),
203
+ 'edit' => esc_attr__( 'Edit Recurring Payout', 'mycred' ),
204
+ 'close' => esc_attr__( 'Close', 'mycred' ),
205
+ 'emptyfields' => esc_attr__( 'Please fill out all required fields that are highlighted in red.', 'mycred' ),
206
+ 'confirmremoval' => esc_attr__( 'Are you sure you want to remove this schedule? This can not be undone!', 'mycred' )
207
+ )
208
+ );
209
+ wp_enqueue_script( 'mycred-bank-manage-schedules' );
210
+
211
  ?>
 
212
  <style type="text/css">
213
+ .mycred-update-balance { font-family: "Open Sans",sans-serif; background-color: white !important; z-index: 9999 !important; border: none !important; border-radius: 0 !important; background-image: none !important; padding: 0 !important; overflow: visible !important; }
214
+ .mycred-update-balance.ui-dialog .ui-dialog-content { padding: 0 0 0 0; }
215
+ .mycred-update-balance .ui-widget-header { border: none !important; background: transparent !important; font-weight: normal !important; }
216
+ .mycred-update-balance .ui-dialog-titlebar { line-height: 24px !important; border-bottom: 1px solid #ddd !important; border-left: none; border-top: none; border-right: none; padding: 12px !important; border-radius: 0 !important; }
217
+ .mycred-update-balance .ui-dialog-titlebar:hover { cursor: move; }
218
+ .mycred-update-balance .ui-dialog-titlebar-close { float: right; margin: 0 12px 0 0; background: 0 0; border: none; -webkit-box-shadow: none; box-shadow: none; color: #666; cursor: pointer; display: block; padding: 0; position: absolute; top: 0; right: 0; width: 36px; height: 36px; text-align: center; font-size: 13px; line-height: 26px; vertical-align: top; white-space: nowrap; }
219
+ .mycred-update-balance .ui-icon { display: none !important; }
220
+ .mycred-update-balance .ui-button:focus, .mycred-update-balance .ui-button:active { outline: none !important; }
221
+ .mycred-update-balance .ui-button .ui-button-text { display: block; text-indent: 0; }
222
+ .mycred-update-balance .ui-dialog-title { font-size: 22px; font-weight: 600; margin: 0 0 0 0; width: auto !important; float: none !important; }
223
+ .ui-widget-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: repeat-x scroll 70% 70% #000; opacity: 0.7; overflow: hidden; background: repeat-x scroll 70% 70% #000; z-index: 99; }
224
+ #manage-recurring-schedule { min-height: 4px !important; background-color: #f3f3f3; }
225
+ #manage-recurring-schedule-form h3 { margin: 0 0 12px 0; }
226
+ .mycred-metabox .form .has-error .form-control { border-color: #dc3232; }
227
+ .alert { padding: 24px; }
228
+ .alert-warning { background-color: #dc3232; color: white; }
229
+ .alert-success { background-color: #46b450; color: white; }
230
  </style>
231
  <?php
232
 
240
  public function admin_page() {
241
 
242
  // Security
243
+ if ( ! $this->core->can_edit_creds() ) wp_die( 'Access Denied' );
 
244
 
245
  // Get installed
246
+ $installed = $this->get();
247
 
248
  ?>
249
+ <div class="wrap mycred-metabox" id="myCRED-wrap">
 
250
 
251
  <?php $this->update_notice(); ?>
252
 
253
+ <h1><?php echo sprintf( __( '%s Banking', 'mycred' ), mycred_label() ); ?></h1>
254
+ <form method="post" class="form" action="options.php">
 
 
 
 
 
 
 
 
255
 
256
  <?php settings_fields( $this->settings_name ); ?>
257
 
264
  foreach ( $installed as $key => $data ) {
265
 
266
  ?>
267
+ <h4><span class="dashicons <?php echo $data['icon']; ?><?php if ( $this->is_active( $key ) ) echo ' active'; else echo ' static'; ?>"></span><?php echo $this->core->template_tags_general( $data['title'] ); ?></h4>
268
+ <div class="body" style="display: none;">
269
  <p><?php echo nl2br( $this->core->template_tags_general( $data['description'] ) ); ?></p>
270
+ <?php if ( $data['cron'] && defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) : ?>
271
+ <div class="alert alert-warning"><strong><?php _e( 'Warning', 'mycred' ); ?></strong> - <?php _e( 'This banking service uses the WordPress CRON to schedule events. If the WordPress CRON is disabled, this service will not work correctly.', 'mycred' ); ?></div>
272
+ <?php endif; ?>
273
  <label class="subheader"><?php _e( 'Enable', 'mycred' ); ?></label>
274
  <ol>
275
  <li>
293
 
294
  </form>
295
  </div>
296
+ <style type="text/css">
297
+ body .ui-dialog.ui.widget { height: auto !important; }
298
+ </style>
299
+ <div id="manage-recurring-schedule" style="display: none;">
300
+ <div class="mycred-container">
301
+ <form class="form" method="post" action="" id="manage-recurring-schedule-form"></form>
302
+ <div id="mycred-processing"><div class="loading-indicator"></div></div>
303
+ </div>
304
+ </div>
305
  <?php
306
 
307
  }
313
  */
314
  public function sanitize_settings( $post ) {
315
 
316
+ $installed = $this->get();
 
317
 
318
  // Construct new settings
319
+ $new_post = array();
320
  $new_post['services'] = $installed;
321
+
322
  if ( empty( $post['active'] ) || ! isset( $post['active'] ) )
323
  $post['active'] = array();
324
 
325
+ $new_post['active'] = $post['active'];
326
 
327
+ // Loop though all installed hooks
328
  if ( ! empty( $installed ) ) {
329
  foreach ( $installed as $key => $data ) {
330
 
358
  }
359
  }
360
 
 
361
  return $new_post;
362
 
363
  }
364
 
365
+ /**
366
+ * User Screen
367
+ * @since 1.7
368
+ * @version 1.0
369
+ */
370
+ public function banking_user_screen( $user ) {
371
+
372
+ if ( ! empty( $this->services ) ) {
373
+
374
+ foreach ( $this->services as $key => $gdata ) {
375
+
376
+ if ( $this->is_active( $key ) && isset( $gdata['callback'] ) ) {
377
+ $this->call( 'user_screen', $gdata['callback'], $user );
378
+ }
379
+
380
+ }
381
+
382
+ }
383
+
384
+ }
385
+
386
+ /**
387
+ * Ajax Handler
388
+ * @since 1.7
389
+ * @version 1.0
390
+ */
391
+ public function ajax_handler() {
392
+
393
+ // Make sure this is an ajax call for this point type
394
+ if ( isset( $_REQUEST['_token'] ) && wp_verify_nonce( $_REQUEST['_token'], 'run-mycred-bank-task' . $this->mycred_type ) ) {
395
+
396
+ // Make sure ajax call is made by an admin
397
+ if ( $this->core->can_edit_creds() || $this->core->can_edit_plugin() ) {
398
+
399
+ // Get the service requesting to use this
400
+ $service = sanitize_key( $_POST['service'] );
401
+ $installed = $this->get();
402
+
403
+ // If there is such a service, load it's ajax handler
404
+ if ( array_key_exists( $service, $installed ) )
405
+ $this->call( 'ajax_handler', $installed[ $service ]['callback'] );
406
+
407
+ }
408
+
409
+ }
410
+
411
+ }
412
+
413
  }
414
  endif;
415
 
416
+ /**
417
+ * Load Banking Module
418
+ * @since 1.2
419
+ * @version 1.1
420
+ */
421
+ if ( ! function_exists( 'mycred_load_banking_addon' ) ) :
422
+ function mycred_load_banking_addon( $modules, $point_types ) {
423
+
424
+ foreach ( $point_types as $type => $title ) {
425
+ $modules['type'][ $type ]['banking'] = new myCRED_Banking_Module( $type );
426
+ $modules['type'][ $type ]['banking']->load();
427
+ }
428
 
429
+ return $modules;
430
 
 
 
 
 
431
  }
432
+ endif;
433
+ add_filter( 'mycred_load_modules', 'mycred_load_banking_addon', 20, 2 );
434
 
435
  ?>
addons/banking/services/mycred-bank-service-interest.php DELETED
@@ -1,685 +0,0 @@
1
- <?php
2
- /**
3
- * myCRED Bank Service - Interest
4
- * @since 1.2
5
- * @version 1.2
6
- */
7
- if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
-
9
- if ( ! class_exists( 'myCRED_Banking_Service_Interest' ) ) :
10
- class myCRED_Banking_Service_Interest extends myCRED_Service {
11
-
12
- /**
13
- * Construct
14
- */
15
- function __construct( $service_prefs, $type = 'mycred_default' ) {
16
- parent::__construct( array(
17
- 'id' => 'interest',
18
- 'defaults' => array(
19
- 'rate' => array(
20
- 'amount' => 2,
21
- 'period' => 1,
22
- 'pay_out' => 'monthly'
23
- ),
24
- 'log' => __( '%plural% interest rate payment', 'mycred' ),
25
- 'min_balance' => 1,
26
- 'exclude_ids' => '',
27
- 'exclude_roles' => array()
28
- )
29
- ), $service_prefs, $type );
30
-
31
- add_action( 'mycred_bank_interest_comp' . $this->mycred_type, array( $this, 'do_compounding' ) );
32
- add_action( 'mycred_bank_interest_pay' . $this->mycred_type, array( $this, 'do_interest_payout' ) );
33
- }
34
-
35
- /**
36
- * Activate Service
37
- * @since 1.5.2
38
- * @version 1.0
39
- */
40
- public function activate() {
41
-
42
- $this->save_last_run( 'compound' );
43
- $this->save_last_run( 'payout' );
44
-
45
- }
46
-
47
- /**
48
- * Deactivate Service
49
- * @since 1.5.2
50
- * @version 1.0
51
- */
52
- public function deactivate() {
53
-
54
- $option_id = 'mycred_bank_interest_comp' . $this->mycred_type;
55
- $timestamp = wp_next_scheduled( $option_id );
56
- if ( $timestamp !== false )
57
- wp_clear_scheduled_hook( $timestamp, $option_id );
58
-
59
- $option_id = 'mycred_bank_interest_pay' . $this->mycred_type;
60
- $timestamp = wp_next_scheduled( $option_id );
61
- if ( $timestamp !== false )
62
- wp_clear_scheduled_hook( $timestamp, $option_id );
63
-
64
- }
65
-
66
- /**
67
- * Run
68
- * @since 1.2
69
- * @version 1.1
70
- */
71
- public function run() {
72
-
73
- // Time to compound interest?
74
- $compound_rate = apply_filters( 'mycred_compound_interest', 'daily', $this );
75
- $time_to_compound = $this->time_to_run( $compound_rate, $this->get_last_run( 'compound' ) );
76
- if ( $time_to_compound ) {
77
-
78
- // Get Work
79
- $option_id = 'mycred_bank_interest_comp' . $this->mycred_type;
80
- $work_marker = 'MYCRED_BANK_COMPOUND_' . $this->mycred_type;
81
-
82
- $eligeble_users = $this->get_eligeble_users();
83
-
84
- // Work to do?
85
- if ( ! empty( $eligeble_users ) ) {
86
-
87
- // Get current workload
88
- $current_work = mycred_get_option( $option_id, false );
89
-
90
- // Currently working?
91
- if ( ! defined( $work_marker ) ) {
92
-
93
- // Cron is scheduled?
94
- if ( wp_next_scheduled( $option_id ) === false ) {
95
- if ( $current_work === false )
96
- mycred_update_option( $option_id, $eligeble_users );
97
-
98
- wp_schedule_single_event( time(), $option_id );
99
- }
100
-
101
- // Work left to do?
102
- elseif ( $current_work === false ) {
103
-
104
- // Mark completed
105
- $this->update_run_count( 'compound' );
106
- $this->save_last_run( 'compound' );
107
-
108
- }
109
-
110
- }
111
-
112
- }
113
- else {
114
-
115
- // Mark completed
116
- $this->update_run_count( 'compound' );
117
- $this->save_last_run( 'compound' );
118
-
119
- }
120
-
121
- }
122
-
123
- // Time to Payout?
124
- $time_to_payout = $this->time_to_run( $this->prefs['rate']['pay_out'], $this->get_last_run( 'payout' ) );
125
- if ( $time_to_payout ) {
126
-
127
- // Get Work
128
- $option_id = 'mycred_bank_interest_pay' . $this->mycred_type;
129
- $work_marker = 'MYCRED_BANK_COMPPAY_' . $this->mycred_type;
130
-
131
- $eligeble_users = $this->get_eligeble_users_payout();
132
-
133
- // Work to do?
134
- if ( ! empty( $eligeble_users ) ) {
135
-
136
- // Get current workload
137
- $current_work = mycred_get_option( $option_id, false );
138
-
139
- // Currently working?
140
- if ( ! defined( $work_marker ) ) {
141
-
142
- // Cron is scheduled?
143
- if ( wp_next_scheduled( $option_id ) === false ) {
144
- if ( $current_work === false )
145
- mycred_update_option( $option_id, $eligeble_users );
146
-
147
- wp_schedule_single_event( time(), $option_id );
148
- }
149
-
150
- // Work left to do?
151
- elseif ( $current_work === false ) {
152
-
153
- // Mark completed
154
- $this->update_run_count( 'payout' );
155
- $this->save_last_run( 'payout' );
156
-
157
- }
158
-
159
- }
160
-
161
- }
162
- else {
163
-
164
- // Mark completed
165
- $this->update_run_count( 'payout' );
166
- $this->save_last_run( 'payout' );
167
-
168
- }
169
-
170
- }
171
-
172
- }
173
-
174
- /**
175
- * Do Interest Compounding
176
- * @since 1.5.2
177
- * @version 1.0
178
- */
179
- public function do_compounding() {
180
-
181
- $work_marker = 'MYCRED_BANK_COMPOUND_' . $this->mycred_type;
182
- define( $work_marker, time() );
183
-
184
- $option_id = 'mycred_bank_interest_comp' . $this->mycred_type;
185
- $current_work = mycred_get_option( $option_id, false );
186
- if ( $current_work === false ) {
187
- $this->update_run_count( 'compound' );
188
- $this->save_last_run( 'compound' );
189
- return;
190
- }
191
-
192
- $work = $current_work;
193
- foreach ( $current_work as $row => $user_id ) {
194
-
195
- // Get users balance
196
- $balance = mycred_get_user_meta( $user_id, $this->mycred_type );
197
-
198
- // Get past interest to add up to
199
- $past_interest = mycred_get_user_meta( $user_id, $this->mycred_type . '_comp' );
200
- if ( $past_interest == '' ) $past_interest = 0;
201
-
202
- // Get interest rate
203
- $user_override = mycred_get_user_meta( $user_id, 'mycred_banking_rate_' . $this->mycred_type );
204
- if ( $user_override != '' )
205
- $rate = $user_override / 100;
206
- else
207
- $rate = $this->prefs['rate']['amount'] / 100;
208
-
209
- // Period
210
- $period = $this->prefs['rate']['period'] / $this->get_days_in_year();
211
-
212
- // Compound
213
- $interest = ( $balance + $past_interest ) * $rate * $period;
214
- $interest = round( $interest, 2 );
215
-
216
- // Save interest
217
- mycred_update_user_meta( $user_id, $this->mycred_type . '_comp', '', $interest );
218
-
219
- // Remove from workload
220
- unset( $work[ $row ] );
221
-
222
- if ( ! empty( $work ) )
223
- mycred_update_option( $option_id, $work );
224
- else {
225
- mycred_delete_option( $option_id );
226
- $this->update_run_count( 'compound' );
227
- $this->save_last_run( 'compound' );
228
- }
229
-
230
- }
231
-
232
- }
233
-
234
- /**
235
- * Do Interest Payout
236
- * @since 1.5.2
237
- * @version 1.0
238
- */
239
- public function do_interest_payout() {
240
-
241
- $work_marker = 'MYCRED_BANK_COMPPAY_' . $this->mycred_type;
242
- define( $work_marker, time() );
243
-
244
- $option_id = 'mycred_bank_interest_pay' . $this->mycred_type;
245
- $current_work = mycred_get_option( $option_id, false );
246
- if ( $current_work === false ) {
247
- $this->update_run_count( 'payout' );
248
- $this->save_last_run( 'payout' );
249
- return;
250
- }
251
-
252
- $now = date_i18n( 'U' );
253
- $work = $current_work;
254
- foreach ( $current_work as $row => $user_id ) {
255
-
256
- // Get past interest to add up to
257
- $accumulated_interest = mycred_get_user_meta( $user_id, $this->mycred_type . '_comp', '', true );
258
- if ( $accumulated_interest != '' ) {
259
-
260
- // Add a unique Payout ID
261
- $data = array( 'payout_id' => $now . $user_id );
262
-
263
- // Prevent duplicates
264
- if ( ! $this->core->has_entry( 'interest', 0, $user_id, $data, $this->mycred_type ) )
265
- $this->core->add_creds(
266
- 'interest',
267
- $user_id,
268
- $accumulated_interest,
269
- $this->prefs['log'],
270
- 0,
271
- $data,
272
- $this->mycred_type
273
- );
274
-
275
- }
276
-
277
- // Remove from workload
278
- unset( $work[ $row ] );
279
-
280
- if ( ! empty( $work ) )
281
- mycred_update_option( $option_id, $work );
282
- else {
283
- mycred_delete_option( $option_id );
284
- $this->update_run_count( 'payout' );
285
- $this->save_last_run( 'payout' );
286
- }
287
-
288
- }
289
-
290
- }
291
-
292
- /**
293
- * Get Eligeble User Payouts
294
- * @since 1.5.2
295
- * @version 1.0
296
- */
297
- public function get_eligeble_users_payout() {
298
-
299
- global $wpdb;
300
-
301
- $key = $this->mycred_type . '_comp';
302
- if ( is_multisite() && $GLOBALS['blog_id'] > 1 && ! $this->core->use_central_logging )
303
- $key .= '_' . $GLOBALS['blog_id'];
304
-
305
- $users = $wpdb->get_col( $wpdb->prepare( "
306
- SELECT DISTINCT user_id
307
- FROM {$wpdb->usermeta}
308
- WHERE meta_key = %s;", $key ) );
309
-
310
- if ( $users === NULL )
311
- $users = array();
312
-
313
- return $users;
314
-
315
- }
316
-
317
- /**
318
- * Get Total Pending for Payout
319
- * Returns the total amount of compounded interest that is currently
320
- * pending to be paid out.
321
- * @since 1.5.2
322
- * @version 1.0
323
- */
324
- public function get_total_pending_payout() {
325
-
326
- global $wpdb;
327
- $key = $this->mycred_type . '_comp';
328
-
329
- $total = $wpdb->get_var( "
330
- SELECT SUM( meta_value )
331
- FROM {$wpdb->usermeta}
332
- WHERE meta_key = '{$key}';" );
333
-
334
- if ( $total === NULL )
335
- $total = $this->core->zero();
336
-
337
- return $total;
338
-
339
- }
340
-
341
- /**
342
- * Get Total Payouts
343
- * @since 1.5.2
344
- * @version 1.0
345
- */
346
- public function get_total_interest_payouts() {
347
-
348
- global $wpdb;
349
- $log = $this->core->log_table;
350
-
351
- $total = $wpdb->get_var( "
352
- SELECT SUM( creds )
353
- FROM {$log}
354
- WHERE ref = 'interest'
355
- AND ctype = '{$this->mycred_type}';" );
356
-
357
- if ( $total === NULL )
358
- $total = $this->core->zero();
359
-
360
- return $total;
361
-
362
- }
363
-
364
- /**
365
- * Preference for interest rates
366
- * @since 1.2
367
- * @version 1.3
368
- */
369
- public function preferences() {
370
-
371
- $prefs = $this->prefs;
372
- $editable_roles = array_reverse( get_editable_roles() );
373
-
374
- // Inform user when compounding is running
375
- $comp_work_marker = 'MYCRED_BANK_COMPOUND_' . $this->mycred_type;
376
- if ( defined( $comp_work_marker ) ) :
377
- $current_work = mycred_get_option( 'mycred_bank_interest_comp' . $this->mycred_type, false ); ?>
378
-
379
- <p><strong><?php _e( 'Compounding Interest', 'mycred' ); ?></strong> <?php print_r( __( '%d Users are left to process.', 'mycred' ), count( $current_work ) ); ?></p>
380
- <?php
381
-
382
- endif;
383
-
384
- $pay_count = $this->get_run_count( 'payout' );
385
- $comp_count = $this->get_run_count( 'compound' );
386
-
387
- ?>
388
- <label class="subheader"><?php _e( 'Payout History', 'mycred' ); ?></label>
389
- <ol class="inline">
390
- <?php if ( $pay_count > 0 ) : ?>
391
- <li style="min-width: 100px;">
392
- <label><?php _e( 'Run Count', 'mycred' ); ?></label>
393
- <div class="h2"><?php echo $pay_count; ?></div>
394
- </li>
395
- <?php endif; ?>
396
- <li style="min-width: 200px;">
397
- <label><?php if ( $pay_count > 0 ) _e( 'Last Payout', 'mycred' ); else _e( 'Activated', 'mycred' ); ?></label>
398
- <div class="h2"><?php echo $this->display_last_run( 'payout' ); ?></div>
399
- </li>
400
- <li>
401
- <label><?php _e( 'Total Payed Interest', 'mycred' ); ?></label>
402
- <div class="h2"><?php echo $this->core->format_creds( $this->get_total_interest_payouts() ); ?></div>
403
- </li>
404
- </ol>
405
- <label class="subheader"><?php _e( 'Compound History', 'mycred' ); ?></label>
406
- <ol class="inline">
407
- <?php if ( $comp_count > 0 ) : ?>
408
- <li style="min-width: 100px;">
409
- <label><?php _e( 'Run Count', 'mycred' ); ?></label>
410
- <div class="h2"><?php echo $comp_count; ?></div>
411
- </li>
412
- <?php endif; ?>
413
- <li style="min-width: 200px;">
414
- <label><?php if ( $comp_count > 0 ) _e( 'Last Interest Compound', 'mycred' ); else _e( 'Activated', 'mycred' ); ?></label>
415
- <div class="h2"><?php echo $this->display_last_run( 'compound' ); ?></div>
416
- </li>
417
- <li>
418
- <label><?php _e( 'Total Compounded Interest', 'mycred' ); ?></label>
419
- <div class="h2"><?php echo $this->core->format_creds( $this->get_total_pending_payout() ); ?></div>
420
- </li>
421
- </ol>
422
- <label class="subheader"><?php _e( 'Interest Rate', 'mycred' ); ?></label>
423
- <ol class="inline">
424
- <li>
425
- <label><?php _e( 'Default Rate', 'mycred' ); ?></label>
426
- <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'rate' => 'amount' ) ); ?>" id="<?php echo $this->field_id( array( 'rate' => 'amount' ) ); ?>" value="<?php echo $this->core->format_number( $prefs['rate']['amount'] ); ?>" size="8" />%</div>
427
- <span class="description"><?php _e( 'Can not be zero.', 'mycred' ); ?></span>
428
- </li>
429
- <li>
430
- <label for="<?php echo $this->field_id( 'rate' ); ?>"><?php _e( 'Payout', 'mycred' ); ?></label><br />
431
- <?php $this->timeframe_dropdown( array( 'rate' => 'pay_out' ), false, false ); ?>
432
-
433
- </li>
434
- </ol>
435
- <label class="subheader"><?php _e( 'Log Template', 'mycred' ); ?></label>
436
- <ol>
437
- <li>
438
- <div class="h2"><input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo $prefs['log']; ?>" style="width: 65%;" /></div>
439
- <span class="description"><?php echo $this->core->available_template_tags( array( 'general' ), '%timeframe%, %rate%, %base%' ); ?></span>
440
- </li>
441
- </ol>
442
- <label class="subheader"><?php _e( 'Minimum Balance', 'mycred' ); ?></label>
443
- <ol>
444
- <li>
445
- <div class="h2"><?php if ( $this->core->before != '' ) echo $this->core->before . ' '; ?><input type="text" name="<?php echo $this->field_name( 'min_balance' ); ?>" id="<?php echo $this->field_id( 'min_balance' ); ?>" value="<?php echo $this->core->format_number( $prefs['min_balance'] ); ?>" size="8" placeholder="<?php echo $this->core->zero(); ?>" /><?php if ( $this->core->after != '' ) echo ' ' . $this->core->after; ?></div>
446
- <span class="description"><?php _e( 'Optional minimum balance requirement.', 'mycred' ); ?></span>
447
- </li>
448
- </ol>
449
- <label class="subheader"><?php _e( 'Exclude', 'mycred' ); ?></label>
450
- <ol>
451
- <li>
452
- <label><?php _e( 'Comma separated list of user IDs', 'mycred' ); ?></label>
453
- <div class="h2"><input type="text" name="<?php echo $this->field_name( 'exclude_ids' ); ?>" id="<?php echo $this->field_id( 'exclude_ids' ); ?>" value="<?php echo $prefs['exclude_ids']; ?>" class="long" /></div>
454
- </li>
455
- <li>
456
- <label><?php _e( 'Roles', 'mycred' ); ?></label><br />
457
-
458
- <?php
459
-
460
- foreach ( $editable_roles as $role => $details ) {
461
- $name = translate_user_role( $details['name'] );
462
-
463
- echo '<label for="' . $this->field_id( 'exclude-roles-' . $role ) . '"><input type="checkbox" name="' . $this->field_name( 'exclude_roles][' ) . '" id="' . $this->field_id( 'exclude-roles-' . $role ) . '" value="' . esc_attr( $role ) . '"';
464
- if ( in_array( $role, (array) $prefs['exclude_roles'] ) ) echo ' checked="checked"';
465
- echo ' />' . $name . '</label><br />';
466
- }
467
-
468
- ?>
469
-
470
- </li>
471
- </ol>
472
- <?php do_action( 'mycred_banking_compound_interest', $this ); ?>
473
-
474
- <?php
475
- }
476
-
477
- /**
478
- * Sanitise Preferences
479
- * @since 1.2
480
- * @version 1.3
481
- */
482
- function sanitise_preferences( $post ) {
483
-
484
- $new_settings = $post;
485
- $new_settings['rate']['amount'] = str_replace( ',', '.', trim( $post['rate']['amount'] ) );
486
- $new_settings['rate']['period'] = $this->get_days_in_year();
487
- $new_settings['rate']['pay_out'] = sanitize_text_field( $post['rate']['pay_out'] );
488
-
489
- $new_settings['log'] = trim( $post['log'] );
490
- $new_settings['min_balance'] = str_replace( ',', '.', trim( $post['min_balance'] ) );
491
-
492
- $new_settings['exclude_ids'] = sanitize_text_field( $post['exclude_ids'] );
493
-
494
- if ( ! isset( $post['exclude_roles'] ) )
495
- $post['exclude_roles'] = array();
496
-
497
- $new_settings['exclude_roles'] = $post['exclude_roles'];
498
- return apply_filters( 'mycred_banking_save_interest', $new_settings, $this );
499
-
500
- }
501
-
502
- /**
503
- * User Override
504
- * @since 1.5.2
505
- * @version 1.0
506
- */
507
- function user_override( $user = NULL, $type = 'mycred_default' ) {
508
-
509
- $users_rate = mycred_get_user_meta( $user->ID, 'mycred_banking_rate_' . $type );
510
- $excluded = $this->exclude_user( $user->ID ); ?>
511
-
512
- <h3><?php _e( 'Compound Interest', 'mycred' ); ?></h3>
513
-
514
- <?php
515
-
516
- if ( $excluded == 'list' ) :
517
-
518
- ?>
519
-
520
- <table class="form-table">
521
- <tr>
522
- <td colspan="2"><?php _e( 'This user is excluded from receiving interest on this balance.', 'mycred' ); ?></td>
523
- </tr>
524
- <tr>
525
- <td colspan="2"><?php submit_button( __( 'Remove from Excluded List', 'mycred' ), 'primary medium', 'mycred_include_users_interest_rate', false ); ?></td>
526
- </tr>
527
- </table>
528
-
529
- <?php
530
-
531
- elseif ( $excluded == 'role' ) :
532
-
533
- ?>
534
-
535
- <table class="form-table">
536
- <tr>
537
- <td colspan="2"><?php _e( 'This user role is excluded from receiving interest on this balance.', 'mycred' ); ?></td>
538
- </tr>
539
- </table>
540
-
541
- <?php
542
-
543
- else :
544
-
545
- ?>
546
-
547
- <table class="form-table">
548
- <tr>
549
- <th scope="row"><?php _e( 'Interest Rate', 'mycred' ); ?></th>
550
- <td>
551
- <input type="text" name="mycred_adjust_users_interest_rate" id="mycred-adjust-users-interest-rate" value="<?php echo $users_rate; ?>" placeholder="<?php echo $this->prefs['rate']['amount']; ?>" size="8" /> %<br />
552
- <span class="description"><?php _e( 'Leave empty to use the default value.', 'mycred' ); ?></span>
553
- </td>
554
- </tr>
555
- <tr>
556
- <th scope="row"></th>
557
- <td>
558
- <?php submit_button( __( 'Save Interest Rate', 'mycred' ), 'primary medium', 'mycred_adjust_users_interest_rate_run', false ); ?>
559
- <?php submit_button( __( 'Exclude from receiving interest', 'mycred' ), 'primary medium', 'mycred_exclude_users_interest_rate', false ); ?>
560
- </td>
561
- </tr>
562
- </table>
563
- <?php
564
- endif;
565
-
566
- }
567
-
568
- /**
569
- * Save User Override
570
- * @since 1.5.2
571
- * @version 1.0.2
572
- */
573
- function save_user_override() {
574
-
575
- // Save interest rate
576
- if ( isset( $_POST['mycred_adjust_users_interest_rate_run'] ) && isset( $_POST['mycred_adjust_users_interest_rate'] ) ) {
577
-
578
- $ctype = sanitize_key( $_GET['ctype'] );
579
- $user_id = absint( $_GET['user_id'] );
580
-
581
- $rate = $_POST['mycred_adjust_users_interest_rate'];
582
- if ( $rate != '' ) {
583
- if ( isfloat( $rate ) )
584
- $rate = (float) $rate;
585
- else
586
- $rate = (int) $rate;
587
-
588
- mycred_update_user_meta( $user_id, 'mycred_banking_rate_' . $ctype, '', $rate );
589
- }
590
- else {
591
- mycred_delete_user_meta( $user_id, 'mycred_banking_rate_' . $ctype );
592
- }
593
-
594
- wp_safe_redirect( add_query_arg( array( 'result' => 'banking_interest_rate' ) ) );
595
- exit;
596
-
597
- }
598
-
599
- // Exclude
600
- elseif ( isset( $_POST['mycred_exclude_users_interest_rate'] ) ) {
601
-
602
- $ctype = sanitize_key( $_GET['ctype'] );
603
- $user_id = absint( $_GET['user_id'] );
604
-
605
- $excluded = explode( ',', $this->prefs['exclude_ids'] );
606
- $clean_ids = array();
607
- if ( ! empty( $excluded ) ) {
608
- foreach ( $excluded as $id ) {
609
- if ( $id == 0 ) continue;
610
- $clean_ids[] = (int) trim( $id );
611
- }
612
- }
613
-
614
- if ( ! in_array( $user_id, $clean_ids ) && $user_id != 0 )
615
- $clean_ids[] = $user_id;
616
-
617
- $option_id = 'mycred_pref_bank';
618
- if ( ! $this->is_main_type )
619
- $option_id .= '_' . $ctype;
620
-
621
- $data = mycred_get_option( $option_id );
622
- $data['service_prefs'][ $this->id ]['exclude_ids'] = implode( ',', $clean_ids );
623
-
624
- mycred_update_option( $option_id, $data );
625
-
626
- wp_safe_redirect( add_query_arg( array( 'result' => 'banking_interest_excluded' ) ) );
627
- exit;
628
-
629
- }
630
-
631
- // Include
632
- elseif ( isset( $_POST['mycred_include_users_interest_rate'] ) ) {
633
-
634
- $ctype = sanitize_key( $_GET['ctype'] );
635
- $user_id = absint( $_GET['user_id'] );
636
-
637
- $excluded = explode( ',', $this->prefs['exclude_ids'] );
638
- if ( ! empty( $excluded ) ) {
639
- $clean_ids = array();
640
- foreach ( $excluded as $id ) {
641
- $clean_id = (int) trim( $id );
642
- if ( $clean_id != $user_id && $user_id != 0 )
643
- $clean_ids[] = $clean_id;
644
- }
645
-
646
- $option_id = 'mycred_pref_bank';
647
- if ( ! $this->is_main_type )
648
- $option_id .= '_' . $ctype;
649
-
650
- $data = mycred_get_option( $option_id );
651
- $data['service_prefs'][ $this->id ]['exclude_ids'] = implode( ',', $clean_ids );
652
-
653
- mycred_update_option( $option_id, $data );
654
-
655
- wp_safe_redirect( add_query_arg( array( 'result' => 'banking_interest_included' ) ) );
656
- exit;
657
- }
658
-
659
- }
660
-
661
- }
662
-
663
- /**
664
- * User Override Notice
665
- * @since 1.5.2
666
- * @version 1.0
667
- */
668
- function user_override_notice() {
669
-
670
- if ( isset( $_GET['page'] ) && $_GET['page'] == 'mycred-edit-balance' && isset( $_GET['result'] ) ) {
671
-
672
- if ( $_GET['result'] == 'banking_interest_rate' )
673
- echo '<div class="updated"><p>' . __( 'Compound interest rate saved.', 'mycred' ) . '</p></div>';
674
- elseif ( $_GET['result'] == 'banking_interest_excluded' )
675
- echo '<div class="updated"><p>' . __( 'User excluded from receiving interest.', 'mycred' ) . '</p></div>';
676
- elseif ( $_GET['result'] == 'banking_interest_included' )
677
- echo '<div class="updated"><p>' . __( 'User included in receiving interest.', 'mycred' ) . '</p></div>';
678
-
679
- }
680
-
681
- }
682
-
683
- }
684
- endif;
685
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
addons/banking/services/mycred-bank-service-payouts.php DELETED
@@ -1,352 +0,0 @@
1
- <?php
2
- /**
3
- * myCRED Bank Service - Recurring Payouts
4
- * @since 1.2
5
- * @version 1.2
6
- */
7
- if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
-
9
- if ( ! class_exists( 'myCRED_Banking_Service_Payouts' ) ) :
10
- class myCRED_Banking_Service_Payouts extends myCRED_Service {
11
-
12
- /**
13
- * Construct
14
- */
15
- function __construct( $service_prefs, $type = 'mycred_default' ) {
16
- parent::__construct( array(
17
- 'id' => 'payouts',
18
- 'defaults' => array(
19
- 'amount' => 10,
20
- 'rate' => 'daily',
21
- 'log' => __( 'Daily %_plural%', 'mycred' ),
22
- 'cycles' => 0,
23
- 'min_balance' => 1,
24
- 'exclude_ids' => '',
25
- 'exclude_roles' => array()
26
- )
27
- ), $service_prefs, $type );
28
-
29
- add_action( 'mycred_bank_recurring_pay' . $this->mycred_type, array( $this, 'do_masspayout' ) );
30
- }
31
-
32
- /**
33
- * Activate Service
34
- * @since 1.5.2
35
- * @version 1.0
36
- */
37
- public function activate() {
38
-
39
- $this->save_last_run( 'masspayout' );
40
-
41
- }
42
-
43
- /**
44
- * Deactivation
45
- * @since 1.2
46
- * @version 1.0.1
47
- */
48
- public function deactivate() {
49
-
50
- // Unschedule payouts
51
- wp_clear_scheduled_hook( 'mycred_bank_recurring_pay' . $this->mycred_type );
52
-
53
- }
54
-
55
- /**
56
- * Run
57
- * @since 1.2
58
- * @version 1.1
59
- */
60
- public function run() {
61
-
62
- // Max Cycles
63
- if ( $this->prefs['cycles'] == 0 ) return;
64
-
65
- // Time to Payout?
66
- $time_to_payout = $this->time_to_run( $this->prefs['rate'], $this->get_last_run( 'masspayout' ) );
67
- if ( $time_to_payout ) {
68
-
69
- // Get Work
70
- $option_id = 'mycred_bank_recurring_pay' . $this->mycred_type;
71
- $work_marker = 'MYCRED_BANK_RECPAY_' . $this->mycred_type;
72
-
73
- $eligeble_users = $this->get_eligeble_users();
74
-
75
- // Work to do?
76
- if ( ! empty( $eligeble_users ) ) {
77
-
78
- // Get current workload
79
- $current_work = mycred_get_option( $option_id, false );
80
-
81
- // Currently working?
82
- if ( ! defined( $work_marker ) ) {
83
-
84
- // Cron is scheduled?
85
- if ( wp_next_scheduled( $option_id ) === false ) {
86
- if ( $current_work === false )
87
- mycred_update_option( $option_id, $eligeble_users );
88
-
89
- wp_schedule_single_event( time(), $option_id );
90
- }
91
-
92
- // Work left to do?
93
- elseif ( $current_work === false ) {
94
-
95
- // Mark completed
96
- $this->update_run_count( 'masspayout' );
97
- $this->save_last_run( 'masspayout' );
98
- $this->update_cycles();
99
-
100
- }
101
-
102
- }
103
-
104
- }
105
- else {
106
-
107
- // Mark completed
108
- $this->update_run_count( 'masspayout' );
109
- $this->save_last_run( 'masspayout' );
110
- $this->update_cycles();
111
-
112
- }
113
-
114
- }
115
-
116
- }
117
-
118
- /**
119
- * Do Payout
120
- * @since 1.5.2
121
- * @version 1.0
122
- */
123
- public function do_masspayout() {
124
-
125
- $work_marker = 'MYCRED_BANK_RECPAY_' . $this->mycred_type;
126
- if ( ! defined( $work_marker ) )
127
- define( $work_marker, time() );
128
-
129
- $option_id = 'mycred_bank_recurring_pay' . $this->mycred_type;
130
- $current_work = mycred_get_option( $option_id, false );
131
- if ( $current_work === false ) {
132
- $this->update_run_count( 'masspayout' );
133
- $this->save_last_run( 'masspayout' );
134
- $this->update_cycles();
135
- return;
136
- }
137
-
138
- $now = date_i18n( 'U' );
139
- $work = $current_work;
140
- foreach ( $current_work as $row => $user_id ) {
141
-
142
- // Add a unique Payout ID
143
- $data = array( 'payout_id' => $now . $user_id );
144
-
145
- // Prevent duplicates
146
- if ( ! $this->core->has_entry( 'interest', 0, $user_id, $data, $this->mycred_type ) )
147
- $this->core->add_creds(
148
- 'recurring_payout',
149
- $user_id,
150
- $this->prefs['amount'],
151
- $this->prefs['log'],
152
- 0,
153
- $data,
154
- $this->mycred_type
155
- );
156
-
157
- // Remove from workload
158
- unset( $work[ $row ] );
159
-
160
- if ( ! empty( $work ) )
161
- mycred_update_option( $option_id, $work );
162
- else {
163
- mycred_delete_option( $option_id );
164
- $this->update_run_count( 'masspayout' );
165
- $this->save_last_run( 'masspayout' );
166
- $this->update_cycles();
167
- }
168
-
169
- }
170
-
171
- }
172
-
173
- /**
174
- * Update Cycles
175
- * @since 1.5.2
176
- * @version 1.0
177
- */
178
- public function update_cycles() {
179
-
180
- // Manage cycles if it's not set to unlimited
181
- if ( $this->prefs['cycles'] != '-1' && $this->prefs['cycles'] != 0 ) {
182
-
183
- // Prep option id for this addon.
184
- $option_id = 'mycred_pref_bank';
185
- if ( ! $this->is_main_type )
186
- $option_id .= '_' . $this->mycred_type;
187
-
188
- // Get addon settings
189
- $addon = mycred_get_option( $option_id );
190
-
191
- // Deduct cycle
192
- $current_cycle = (int) $this->prefs['cycles'];
193
- $current_cycle --;
194
- $addon['service_prefs'][ $this->id ]['cycles'] = $current_cycle;
195
-
196
- // If we reach zero, disable this service
197
- if ( $current_cycle == 0 && in_array( $this->id, $addon['active'] ) ) {
198
- $new_active = array();
199
- foreach ( $addon['active'] as $active ) {
200
- if ( $active == $this->id || in_array( $active, $new_active ) ) continue;
201
- $new_active[] = $active;
202
- }
203
- $addon['active'] = $new_active;
204
- }
205
-
206
- // Update settings
207
- mycred_update_option( $option_id, $addon );
208
-
209
- }
210
-
211
- }
212
-
213
- /**
214
- * Get Total Payed out
215
- * @since 1.5.2
216
- * @version 1.0
217
- */
218
- public function get_total_payedout() {
219
-
220
- global $wpdb;
221
- $log = $this->core->log_table;
222
-
223
- $total = $wpdb->get_var( "
224
- SELECT SUM( creds )
225
- FROM {$log}
226
- WHERE ref = 'recurring_payout'
227
- AND ctype = '{$this->mycred_type}';" );
228
-
229
- if ( $total === NULL )
230
- $total = $this->core->zero();
231
-
232
- return $total;
233
-
234
- }
235
-
236
- /**
237
- * Preference for recurring payouts
238
- * @since 1.2
239
- * @version 1.1
240
- */
241
- public function preferences() {
242
- $prefs = $this->prefs;
243
- $editable_roles = array_reverse( get_editable_roles() );
244
- $pay_count = $this->get_run_count( 'masspayout' ); ?>
245
-
246
- <label class="subheader"><?php _e( 'History', 'mycred' ); ?></label>
247
- <ol class="inline">
248
- <?php if ( $pay_count > 0 ) : ?>
249
- <li style="min-width: 100px;">
250
- <label><?php _e( 'Run Count', 'mycred' ); ?></label>
251
- <div class="h2"><?php echo $pay_count; ?></div>
252
- </li>
253
- <?php endif; ?>
254
- <li style="min-width: 200px;">
255
- <label><?php _e( 'Last Run', 'mycred' ); ?></label>
256
- <div class="h2"><?php echo $this->display_last_run( 'masspayout' ); ?></div>
257
- </li>
258
- <li>
259
- <label><?php _e( 'Total Payouts', 'mycred' ); ?></label>
260
- <div class="h2"><?php echo $this->core->format_creds( $this->get_total_payedout() ); ?></div>
261
- </li>
262
- </ol>
263
- <label class="subheader"><?php _e( 'Pay Users', 'mycred' ); ?></label>
264
- <ol class="inline">
265
- <li>
266
- <label><?php _e( 'Amount', 'mycred' ); ?></label>
267
- <div class="h2"><?php if ( !empty( $this->core->before ) ) echo $this->core->before . ' '; ?><input type="text" name="<?php echo $this->field_name( 'amount' ); ?>" id="<?php echo $this->field_id( 'amount' ); ?>" value="<?php echo $this->core->format_number( $prefs['amount'] ); ?>" size="8" /><?php if ( !empty( $this->core->after ) ) echo ' ' . $this->core->after; ?></div>
268
- <span class="description"><?php _e( 'Can not be zero.', 'mycred' ); ?></span>
269
- </li>
270
- <li>
271
- <label for="<?php echo $this->field_id( 'rate' ); ?>"><?php _e( 'Interval', 'mycred' ); ?></label><br />
272
- <?php $this->timeframe_dropdown( 'rate', false ); ?>
273
-
274
- </li>
275
- <li>
276
- <label><?php _e( 'Cycles', 'mycred' ); ?></label>
277
- <div class="h2"><input type="text" name="<?php echo $this->field_name( 'cycles' ); ?>" id="<?php echo $this->field_id( 'cycles' ); ?>" value="<?php echo $prefs['cycles']; ?>" size="8" /></div>
278
- <span class="description"><?php _e( 'Set to -1 for unlimited', 'mycred' ); ?></span>
279
- </li>
280
- <li>
281
- <li class="block"><strong><?php _e( 'Important', 'mycred' ); ?></strong><br /><?php _e( 'You can always stop payouts by deactivating this service. Just remember that if you deactivate while there are cycles left, this service will continue on when it gets re-activated. Set cycles to zero to reset.', 'mycred' ); ?></li>
282
- </ol>
283
- <label class="subheader" for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
284
- <ol>
285
- <li>
286
- <div class="h2"><input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo $prefs['log']; ?>" style="width: 65%;" /></div>
287
- <span class="description"><?php echo $this->core->available_template_tags( array( 'general' ) ); ?></span>
288
- </li>
289
- </ol>
290
- <label class="subheader"><?php _e( 'Minimum Balance', 'mycred' ); ?></label>
291
- <ol>
292
- <li>
293
- <div class="h2"><?php if ( $this->core->before != '' ) echo $this->core->before . ' '; ?><input type="text" name="<?php echo $this->field_name( 'min_balance' ); ?>" id="<?php echo $this->field_id( 'min_balance' ); ?>" value="<?php echo $this->core->format_number( $prefs['min_balance'] ); ?>" size="8" placeholder="<?php echo $this->core->zero(); ?>" /><?php if ( $this->core->after != '' ) echo ' ' . $this->core->after; ?></div>
294
- <span class="description"><?php _e( 'Optional minimum balance requirement.', 'mycred' ); ?></span>
295
- </li>
296
- </ol>
297
- <label class="subheader"><?php _e( 'Exclude', 'mycred' ); ?></label>
298
- <ol>
299
- <li>
300
- <label><?php _e( 'Comma separated list of user IDs', 'mycred' ); ?></label>
301
- <div class="h2"><input type="text" name="<?php echo $this->field_name( 'exclude_ids' ); ?>" id="<?php echo $this->field_id( 'exclude_ids' ); ?>" value="<?php echo $prefs['exclude_ids']; ?>" class="long" /></div>
302
- </li>
303
- <li>
304
- <label><?php _e( 'Roles', 'mycred' ); ?></label><br />
305
-
306
- <?php
307
-
308
- foreach ( $editable_roles as $role => $details ) {
309
- $name = translate_user_role( $details['name'] );
310
-
311
- echo '<label for="' . $this->field_id( 'exclude-roles-' . $role ) . '"><input type="checkbox" name="' . $this->field_name( 'exclude_roles][' ) . '" id="' . $this->field_id( 'exclude-roles-' . $role ) . '" value="' . esc_attr( $role ) . '"';
312
- if ( in_array( $role, (array) $prefs['exclude_roles'] ) ) echo ' checked="checked"';
313
- echo ' />' . $name . '</label><br />';
314
- }
315
-
316
- ?>
317
-
318
- </li>
319
- </ol>
320
-
321
- <?php do_action( 'mycred_banking_recurring_payouts', $this ); ?>
322
-
323
- <?php
324
- }
325
-
326
- /**
327
- * Sanitise Preferences
328
- * @since 1.2
329
- * @version 1.1
330
- */
331
- function sanitise_preferences( $post ) {
332
-
333
- $new_settings = $post;
334
- $new_settings['amount'] = trim( $post['amount'] );
335
- $new_settings['rate'] = sanitize_text_field( $post['rate'] );
336
- $new_settings['cycles'] = sanitize_text_field( $post['cycles'] );
337
-
338
- $new_settings['log'] = trim( $post['log'] );
339
- $new_settings['min_balance'] = str_replace( ',', '.', trim( $post['min_balance'] ) );
340
-
341
- $new_settings['exclude_ids'] = sanitize_text_field( $post['exclude_ids'] );
342
-
343
- if ( ! isset( $post['exclude_roles'] ) )
344
- $post['exclude_roles'] = array();
345
-
346
- $new_settings['exclude_roles'] = $post['exclude_roles'];
347
- return apply_filters( 'mycred_banking_save_recurring', $new_settings, $this );
348
-
349
- }
350
- }
351
- endif;
352
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
addons/banking/services/{mycred-bank-service-central.php → mycred-service-central.php} RENAMED
@@ -1,18 +1,19 @@
1
  <?php
 
 
2
  /**
3
  * myCRED Bank Service - Central Bank
4
  * @since 1.5.2
5
- * @version 1.0
6
  */
7
- if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
-
9
  if ( ! class_exists( 'myCRED_Banking_Service_Central' ) ) :
10
  class myCRED_Banking_Service_Central extends myCRED_Service {
11
 
12
  /**
13
  * Construct
14
  */
15
- function __construct( $service_prefs, $type = 'mycred_default' ) {
 
16
  parent::__construct( array(
17
  'id' => 'central',
18
  'defaults' => array(
@@ -20,6 +21,7 @@ if ( ! class_exists( 'myCRED_Banking_Service_Central' ) ) :
20
  'ignore_manual' => 1
21
  )
22
  ), $service_prefs, $type );
 
23
  }
24
 
25
  /**
@@ -29,8 +31,7 @@ if ( ! class_exists( 'myCRED_Banking_Service_Central' ) ) :
29
  */
30
  public function run() {
31
 
32
- if ( ! function_exists( 'charge_bank_account' ) )
33
- add_filter( 'mycred_add', array( $this, 'mycred_add' ), 1, 3 );
34
 
35
  }
36
 
@@ -40,6 +41,7 @@ if ( ! class_exists( 'myCRED_Banking_Service_Central' ) ) :
40
  * @version 1.0.1
41
  */
42
  public function mycred_add( $reply, $request, $mycred ) {
 
43
  // Make sure we are in the correct point type
44
  if ( $this->mycred_type != $mycred->cred_id || $reply === false ) return $reply;
45
 
@@ -47,8 +49,7 @@ if ( ! class_exists( 'myCRED_Banking_Service_Central' ) ) :
47
  if ( isset( $this->prefs['ignore_manual'] ) && $this->prefs['ignore_manual'] == 0 && $request['ref'] == 'manual' ) return $reply;
48
 
49
  // Instances to ignore
50
- $ignore = apply_filters( 'mycred_central_banking_ignore', array( 'interest', 'recurring_payout', 'transfer' ), $this );
51
- if ( in_array( $request['ref'], $ignore ) ) return $reply;
52
 
53
  extract( $request );
54
 
@@ -56,19 +57,22 @@ if ( ! class_exists( 'myCRED_Banking_Service_Central' ) ) :
56
  if ( $user_id == $this->prefs['bank_id'] ) return $reply;
57
 
58
  // Get the banks balance
59
- $bank_balance = $mycred->get_users_cred( $this->prefs['bank_id'], $type );
60
 
61
  // User is to lose points
62
  if ( $amount < 0 ) {
 
63
  // Add the points getting deducted to our bank account
64
- $mycred->update_users_balance( $this->prefs['bank_id'], abs( $amount ), $type );
65
 
66
  // Log event
67
- $mycred->add_to_log( $ref, $this->prefs['bank_id'], abs( $amount ), $entry, $ref_id, $data, $type );
 
68
  }
69
 
70
  // User is to gain points
71
  else {
 
72
  // First make sure the bank is not bust
73
  if ( $bank_balance <= $mycred->zero() ) return false;
74
 
@@ -76,10 +80,11 @@ if ( ! class_exists( 'myCRED_Banking_Service_Central' ) ) :
76
  if ( $bank_balance-$amount < $mycred->zero() ) return false;
77
 
78
  // If we have come this far, the bank has sufficient funds so lets deduct
79
- $mycred->update_users_balance( $this->prefs['bank_id'], 0-$amount, $type );
80
 
81
  // Log event
82
- $mycred->add_to_log( $ref, $this->prefs['bank_id'], 0-$amount, $entry, $ref_id, $data, $type );
 
83
  }
84
 
85
  // Return the result
@@ -90,50 +95,53 @@ if ( ! class_exists( 'myCRED_Banking_Service_Central' ) ) :
90
  /**
91
  * Preference for Central Bank
92
  * @since 1.5.2
93
- * @version 1.0
94
  */
95
  public function preferences() {
 
96
  $prefs = $this->prefs;
97
- if ( function_exists( 'charge_bank_account' ) ) : ?>
98
 
99
- <p><strong>Warning!</strong>Please remove the charge_bank_account function from your themes functions.php file to use this service!</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  <?php
101
 
102
- endif;
103
-
104
- ?>
105
- <label class="subheader"><?php _e( 'Bank User', 'mycred' ); ?></label>
106
- <ol class="inline">
107
- <li>
108
- <div class="h2"><input type="text" name="<?php echo $this->field_name( 'bank_id' ); ?>" id="<?php echo $this->field_id( 'bank_id' ); ?>" value="<?php echo $prefs['bank_id']; ?>" size="8" /></div>
109
- <span class="description"><?php _e( 'The user ID of the central bank account. This user can not be excluded!', 'mycred' ); ?></span>
110
- </li>
111
- </ol>
112
- <label class="subheader"><?php _e( 'Ignore Manual Adjustments', 'mycred' ); ?></label>
113
- <ol class="inline">
114
- <li>
115
- <div class="h2"><input type="checkbox" name="<?php echo $this->field_name( 'ignore_manual' ); ?>" id="<?php echo $this->field_id( 'ignore_manual' ); ?>" value="1"<?php checked( $prefs['ignore_manual'], 1 ); ?> /></div>
116
- </li>
117
- </ol>
118
-
119
- <?php do_action( 'mycred_banking_central', $this ); ?>
120
 
121
- <?php
122
  }
123
 
124
  /**
125
  * Sanitise Preferences
126
  * @since 1.5.2
127
- * @version 1.0
128
  */
129
  function sanitise_preferences( $post ) {
130
 
131
- $new_settings = $post;
132
- $new_settings['bank_id'] = absint( $post['bank_id'] );
133
- $new_settings['ignore_manual'] = ( isset( $post['ignore_manual'] ) ) ? $post['ignore_manual'] : 0;
 
134
  return apply_filters( 'mycred_banking_save_central', $new_settings, $this );
135
 
136
  }
 
137
  }
138
  endif;
 
139
  ?>
1
  <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
  /**
5
  * myCRED Bank Service - Central Bank
6
  * @since 1.5.2
7
+ * @version 1.0.1
8
  */
 
 
9
  if ( ! class_exists( 'myCRED_Banking_Service_Central' ) ) :
10
  class myCRED_Banking_Service_Central extends myCRED_Service {
11
 
12
  /**
13
  * Construct
14
  */
15
+ function __construct( $service_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) {
16
+
17
  parent::__construct( array(
18
  'id' => 'central',
19
  'defaults' => array(
21
  'ignore_manual' => 1
22
  )
23
  ), $service_prefs, $type );
24
+
25
  }
26
 
27
  /**
31
  */
32
  public function run() {
33
 
34
+ add_filter( 'mycred_add', array( $this, 'mycred_add' ), 1, 3 );
 
35
 
36
  }
37
 
41
  * @version 1.0.1
42
  */
43
  public function mycred_add( $reply, $request, $mycred ) {
44
+
45
  // Make sure we are in the correct point type
46
  if ( $this->mycred_type != $mycred->cred_id || $reply === false ) return $reply;
47
 
49
  if ( isset( $this->prefs['ignore_manual'] ) && $this->prefs['ignore_manual'] == 0 && $request['ref'] == 'manual' ) return $reply;
50
 
51
  // Instances to ignore
52
+ if ( in_array( $request['ref'], apply_filters( 'mycred_central_banking_ignore', array( 'interest', 'recurring_payout', 'transfer' ), $this ) ) ) return $reply;
 
53
 
54
  extract( $request );
55
 
57
  if ( $user_id == $this->prefs['bank_id'] ) return $reply;
58
 
59
  // Get the banks balance
60
+ $bank_balance = $mycred->get_users_balance( $this->prefs['bank_id'], $this->mycred_type );
61
 
62
  // User is to lose points
63
  if ( $amount < 0 ) {
64
+
65
  // Add the points getting deducted to our bank account
66
+ $mycred->update_users_balance( $this->prefs['bank_id'], abs( $amount ), $this->mycred_type );
67
 
68
  // Log event
69
+ $mycred->add_to_log( $ref, $this->prefs['bank_id'], abs( $amount ), $entry, $ref_id, $data, $this->mycred_type );
70
+
71
  }
72
 
73
  // User is to gain points
74
  else {
75
+
76
  // First make sure the bank is not bust
77
  if ( $bank_balance <= $mycred->zero() ) return false;
78
 
80
  if ( $bank_balance-$amount < $mycred->zero() ) return false;
81
 
82
  // If we have come this far, the bank has sufficient funds so lets deduct
83
+ $mycred->update_users_balance( $this->prefs['bank_id'], 0-$amount, $this->mycred_type );
84
 
85
  // Log event
86
+ $mycred->add_to_log( $ref, $this->prefs['bank_id'], 0-$amount, $entry, $ref_id, $data, $this->mycred_type );
87
+
88
  }
89
 
90
  // Return the result
95
  /**
96
  * Preference for Central Bank
97
  * @since 1.5.2
98
+ * @version 1.1
99
  */
100
  public function preferences() {
101
+
102
  $prefs = $this->prefs;
 
103
 
104
+ ?>
105
+ <div class="row">
106
+ <div class="col-xs-12">
107
+ <div class="row">
108
+ <div class="col-sm-4">
109
+ <div class="form-group">
110
+ <label for="<?php echo $this->field_id( 'bank_id' ); ?>"><?php _e( 'Central Bank Account', 'mycred' ); ?></label>
111
+ <input type="text" name="<?php echo $this->field_name( 'bank_id' ); ?>" id="<?php echo $this->field_id( 'bank_id' ); ?>" class="form-control" placeholder="<?php _e( 'Required', 'mycred' ); ?>" value="<?php echo esc_attr( $this->prefs['bank_id'] ); ?>" />
112
+ </div>
113
+ <p><span class="description"><?php _e( 'The ID of the user representing the central bank.', 'mycred' ); ?></span></p>
114
+ </div>
115
+ <div class="col-sm-8">
116
+ <div class="form-group">
117
+ <div class="checkbox"<label for="<?php echo $this->field_id( 'ignore_manual' ); ?>"><input type="checkbox" name="<?php echo $this->field_name( 'ignore_manual' ); ?>" id="<?php echo $this->field_id( 'ignore_manual' ); ?>" value="1"<?php checked( $this->prefs['ignore_manual'], 1 ); ?> /> <?php _e( 'Ignore Manual Adjustments', 'mycred' ); ?></label></div>
118
+ </div>
119
+ </div>
120
+ </div>
121
+ </div>
122
+ </div>
123
  <?php
124
 
125
+ do_action( 'mycred_banking_central', $this );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
 
127
  }
128
 
129
  /**
130
  * Sanitise Preferences
131
  * @since 1.5.2
132
+ * @version 1.1
133
  */
134
  function sanitise_preferences( $post ) {
135
 
136
+ $new_settings = array();
137
+ $new_settings['bank_id'] = absint( $post['bank_id'] );
138
+ $new_settings['ignore_manual'] = ( isset( $post['ignore_manual'] ) ) ? absint( $post['ignore_manual'] ) : 0;
139
+
140
  return apply_filters( 'mycred_banking_save_central', $new_settings, $this );
141
 
142
  }
143
+
144
  }
145
  endif;
146
+
147
  ?>
addons/banking/services/mycred-service-interest.php ADDED
@@ -0,0 +1,693 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * myCRED Bank Service - Interest
6
+ * @since 1.2
7
+ * @version 1.2
8
+ */
9
+ if ( ! class_exists( 'myCRED_Banking_Service_Interest' ) ) :
10
+ class myCRED_Banking_Service_Interest extends myCRED_Service {
11
+
12
+ public $cron_compound_key = '';
13
+ public $cron_payout_key = '';
14
+ public $compound_meta_key = '';
15
+
16
+ /**
17
+ * Construct
18
+ */
19
+ function __construct( $service_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) {
20
+
21
+ parent::__construct( array(
22
+ 'id' => 'interest',
23
+ 'defaults' => array(
24
+ 'rate' => array(
25
+ 'amount' => 2,
26
+ 'pay_out' => 'monthly'
27
+ ),
28
+ 'log' => __( '%plural% interest rate payment', 'mycred' ),
29
+ 'min_balance' => 1,
30
+ 'exclude_ids' => '',
31
+ 'exclude_roles' => array()
32
+ )
33
+ ), $service_prefs, $type );
34
+
35
+ $this->cron_compound_key = 'mycred_bank_interest_comp' . $type;
36
+ $this->cron_payout_key = 'mycred_bank_interest_pay' . $type;
37
+ $this->compound_meta_key = $type . '_comp';
38
+ $this->log_reference = apply_filters( 'mycred_bank_interest_reference', 'interest', $this );
39
+
40
+ }
41
+
42
+ /**
43
+ * Deactivate Service
44
+ * Used if the service is no longer reach the minimum requirements or when the
45
+ * service is disabled in the admin area.
46
+ * @since 1.5.2
47
+ * @version 1.1
48
+ */
49
+ public function deactivate() {
50
+
51
+ $timestamp = wp_next_scheduled( $this->cron_compound_key );
52
+ if ( $timestamp !== false )
53
+ wp_clear_scheduled_hook( $this->cron_compound_key );
54
+
55
+ $timestamp = wp_next_scheduled( $this->cron_payout_key );
56
+ if ( $timestamp !== false )
57
+ wp_clear_scheduled_hook( $this->cron_payout_key );
58
+
59
+ }
60
+
61
+ /**
62
+ * Is Ready
63
+ * Checks to make sure the most common "bugs" users experience with this service
64
+ * are taken care of. An empty log or empty interest rate will not work.
65
+ * @since 1.7
66
+ * @version 1.0
67
+ */
68
+ protected function is_ready() {
69
+
70
+ $rate = $this->prefs['rate']['amount'];
71
+ if ( strlen( $rate ) == 0 ) return false;
72
+
73
+ $log = $this->prefs['log'];
74
+ if ( strlen( $log ) == 0 ) return false;
75
+
76
+ return true;
77
+
78
+ }
79
+
80
+ /**
81
+ * Run
82
+ * Actions taken during WordPress init. Hooks into the cron jobs and schedules
83
+ * the cron jobs if not scheduled. Will only run if service is enabled.
84
+ * @since 1.2
85
+ * @version 1.1
86
+ */
87
+ public function run() {
88
+
89
+ // Make sure we can run this service
90
+ if ( ! $this->is_ready() ) {
91
+
92
+ $this->deactivate();
93
+ return;
94
+
95
+ }
96
+
97
+ add_action( 'mycred_bank_interest_comp' . $this->mycred_type, array( $this, 'do_compounding' ), 10 );
98
+ add_action( 'mycred_bank_interest_pay' . $this->mycred_type, array( $this, 'do_interest_payout' ), 10 );
99
+
100
+ $now = $this->now;
101
+
102
+ // Make sure there is a compounding scheduled
103
+ $compound = wp_next_scheduled( $this->cron_compound_key );
104
+ if ( $compound === false )
105
+ wp_schedule_single_event( $now + 600, $this->cron_compound_key );
106
+
107
+ // Make sure there is a compounding scheduled
108
+ $payout = wp_next_scheduled( $this->cron_payout_key );
109
+ if ( $payout === false ) {
110
+
111
+ $now += mycred_banking_get_next_payout( $this->prefs['rate']['pay_out'] );
112
+ wp_schedule_single_event( $now, $this->cron_payout_key );
113
+
114
+ }
115
+
116
+ add_action( 'personal_options_update', array( $this, 'save_custom_rate' ), 30 );
117
+ add_action( 'edit_user_profile_update', array( $this, 'save_custom_rate' ), 30 );
118
+
119
+ }
120
+
121
+ /**
122
+ * Do Interest Compounding
123
+ * Compounds interest on balances each day.
124
+ * @since 1.5.2
125
+ * @version 1.1
126
+ */
127
+ public function do_compounding() {
128
+
129
+ global $wpdb;
130
+
131
+ $limit = '';
132
+ $select = "user_id, meta_value as value";
133
+
134
+ // Default number of balances we will be running through for now
135
+ $number = absint( apply_filters( 'mycred_compound_max_limit', 1500, $this ) );
136
+
137
+ // On large sites, we need to do this in batches
138
+ if ( $this->is_large_site() ) {
139
+
140
+ // Schedule our next event in 2 minutes
141
+ wp_schedule_single_event( ( $this->now + 180 ), $this->cron_compound_key );
142
+
143
+ $transient_key = 'mycred-compoun-' . $this->mycred_type;
144
+ $offset = get_transient( $transient_key );
145
+
146
+ // Apply limit and set a transient to keep track of our progress
147
+ if ( $offset === false ) {
148
+
149
+ set_transient( $transient_key, $number, HOUR_IN_SECONDS );
150
+ $limit = "LIMIT 0,{$number}";
151
+
152
+ }
153
+
154
+ // While in loop, we need to keep track of our progress
155
+ else {
156
+
157
+ delete_transient( $transient_key );
158
+ set_transient( $transient_key, $offset + $number, HOUR_IN_SECONDS );
159
+ $limit = "LIMIT {$offset},{$number}";
160
+
161
+ }
162
+
163
+ // Save total rows so we can check when we are finished
164
+ $select = "SQL_CALC_FOUND_ROWS " . $select;
165
+
166
+ // Used to store the time at which tomorrows run should be running
167
+ $original_start = get_option( 'mycred_todays_compound_started', false );
168
+ if ( $original_start === false )
169
+ update_option( 'mycred_todays_compound_started', $this->now );
170
+
171
+ }
172
+
173
+ // Should be able to run through 1500 users on most sites
174
+ else {
175
+ wp_schedule_single_event( ( $this->now + DAY_IN_SECONDS ), $this->cron_compound_key );
176
+ }
177
+
178
+ $format = '%d';
179
+ if ( $this->core->format['decimals'] > 0 )
180
+ $format = '%f';
181
+
182
+ $wheres = array();
183
+ $wheres[] = $wpdb->prepare( "meta_key = %s", $this->mycred_type );
184
+ $wheres[] = $wpdb->prepare( "meta_value != {$format}", $this->core->zero() );
185
+
186
+ // Check if we need to exclude certain users
187
+ $excluded = $this->get_excluded_user_ids();
188
+ if ( ! empty( $excluded ) )
189
+ $wheres[] = "user_id NOT IN (" . implode( ', ', $excluded ) . ")";
190
+
191
+ // Apply minimum balance requirement (if used)
192
+ $minimum = ( ( $this->prefs['min_balance'] != '' ) ? $this->core->number( $this->prefs['min_balance'] ) : 0 );
193
+ if ( $minimum > 0 )
194
+ $wheres[] = $wpdb->prepare( "meta_value >= {$format}", $minimum );
195
+
196
+ // A few items we need for calculations
197
+ $days_in_year = $this->get_days_in_year();
198
+ $period = apply_filters( 'mycred_compound_period', 1, $this );
199
+ if ( $period > 0 ) $period = ( $period / $days_in_year );
200
+
201
+ // Get the balance key
202
+ $balance_key = mycred_get_meta_key( $this->mycred_type );
203
+
204
+ // Construct the WHERE statement
205
+ $wheres = implode( ' AND ', $wheres );
206
+
207
+ // Run query
208
+ $user_balances = $wpdb->get_results( "SELECT {$select} FROM {$wpdb->usermeta} WHERE {$wheres} ORDER BY umeta_id ASC {$limit};" );
209
+
210
+ if ( ! empty( $user_balances ) ) {
211
+
212
+ // Loop through each balance
213
+ foreach ( $user_balances as $entry ) {
214
+
215
+ $balance = (float) $entry->value;
216
+
217
+ // Do not apply an interest on negative values
218
+ if ( $balance <= $this->core->zero() && apply_filters( 'mycred_compound_negative_interest', false, $this ) === false ) continue;
219
+
220
+ // Get the interest we have earned up until now
221
+ $saved_interest = mycred_get_user_meta( $entry->user_id, $this->compound_meta_key );
222
+ if ( $saved_interest == '' ) $saved_interest = 0;
223
+
224
+ // Allow customization of the calculated interest
225
+ $interest_rate = $this->get_users_interest_rate( $entry->user_id, $this->prefs['rate']['amount'] );
226
+ if ( $interest_rate > 0 ) {
227
+
228
+ // Compound interest
229
+ $interest_rate = $interest_rate / 100;
230
+ $interest = ( ( $balance + $saved_interest ) * $interest_rate ) * $period;
231
+
232
+ // Save the new interest
233
+ mycred_update_user_meta( $entry->user_id, $this->compound_meta_key, '', $interest );
234
+
235
+ }
236
+
237
+ // NEXT!
238
+
239
+ }
240
+
241
+ }
242
+
243
+ // On large sites, check if we finished.
244
+ if ( $limit != '' ) {
245
+
246
+ $offset += $number;
247
+ $total_rows = $wpdb->get_var( 'SELECT FOUND_ROWS()' );
248
+
249
+ if ( count( $user_balances ) < $number || $offset > $total_rows ) {
250
+
251
+ // Always clean up after ourselves
252
+ delete_transient( $transient_key );
253
+
254
+ // Get the time we should have started
255
+ $original_start = (int) get_option( 'mycred_todays_compound_started', $this->now );
256
+ delete_option( 'mycred_todays_compound_started' );
257
+
258
+ // Schedule tomorrows run
259
+ wp_schedule_single_event( ( $original_start + DAY_IN_SECONDS ), $this->cron_compound_key );
260
+
261
+ }
262
+
263
+ }
264
+
265
+ }
266
+
267
+ /**
268
+ * Do Interest Payout
269
+ * Handles payouts of pending compounded interests.
270
+ * @since 1.5.2
271
+ * @version 1.2
272
+ */
273
+ public function do_interest_payout() {
274
+
275
+ global $wpdb;
276
+
277
+ $now = $this->now;
278
+
279
+ // Default number of balances we will be running through for now
280
+ $number = absint( apply_filters( 'mycred_compound_pay_max_limit', 1500, $this ) );
281
+ $total_rows = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->usermeta} WHERE meta_key = %s;", $this->compound_meta_key ) );
282
+
283
+ // If we have more then what can handle in one go, schedule in the next run in 2 minutes time
284
+ if ( $total_rows > $number ) {
285
+
286
+ // Schedule our next event in 2 minutes
287
+ wp_schedule_single_event( ( $now + 180 ), $this->cron_payout_key );
288
+
289
+ // Used to store the time at which the next run should be running
290
+ $original_start = get_option( 'mycred_todays_compound_payout_started', false );
291
+ if ( $original_start === false )
292
+ update_option( 'mycred_todays_compound_payout_started', $now );
293
+
294
+ }
295
+
296
+ // Get pending interests
297
+ $pending_interests = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->usermeta} WHERE meta_key = %s LIMIT {$number};", $this->compound_meta_key ) );
298
+
299
+ if ( ! empty( $pending_interests ) ) {
300
+
301
+ // Loop through each
302
+ foreach ( $pending_interests as $entry ) {
303
+
304
+ // Make sure the payout is unique
305
+ if ( ! $this->core->has_entry( $this->log_reference, $entry->umeta_id, $entry->user_id, '', $this->mycred_type ) ) {
306
+
307
+ // Attempt to pay
308
+ $payout = $this->core->add_creds(
309
+ $this->log_reference,
310
+ $entry->user_id,
311
+ $entry->meta_value,
312
+ $this->prefs['log'],
313
+ $entry->umeta_id,
314
+ '',
315
+ $this->mycred_type
316
+ );
317
+
318
+ // Payout successfull
319
+ if ( $payout ) {
320
+ $total_rows--;
321
+ mycred_delete_user_meta( $entry->user_id, $this->compound_meta_key );
322
+ }
323
+
324
+ // NEXT!
325
+
326
+ }
327
+
328
+ }
329
+
330
+ }
331
+
332
+ // Nothing more to payout
333
+ if ( $total_rows <= 0 ) {
334
+
335
+ // Get the time we should have done our scheduling
336
+ $original_start = (int) get_option( 'mycred_todays_compound_payout_started', $now );
337
+ if ( $original_start != $now )
338
+ delete_option( 'mycred_todays_compound_payout_started' );
339
+
340
+ // Schedule next run
341
+ $next = $original_start + mycred_banking_get_next_payout( $this->prefs['rate']['pay_out'] );
342
+ wp_schedule_single_event( $next, $this->cron_payout_key );
343
+
344
+ }
345
+
346
+ }
347
+
348
+ /**
349
+ * Get Pending Interest
350
+ * Returns the total amount of points that have been compounded but not yet paid out.
351
+ * @since 1.7
352
+ * @version 1.0
353
+ */
354
+ protected function get_pending_interest() {
355
+
356
+ global $wpdb;
357
+
358
+ $amount = $wpdb->get_var( $wpdb->prepare( "SELECT SUM( meta_value ) FROM {$wpdb->usermeta} WHERE meta_key = %s;", $this->compound_meta_key ) );
359
+ if ( $amount === NULL ) $amount = 0;
360
+
361
+ return $this->core->number( $amount );
362
+
363
+ }
364
+
365
+ /**
366
+ * Get Users Interet Rate
367
+ * Takes into account any custom rates that might have been saved for the given user.
368
+ * @since 1.7
369
+ * @version 1.0
370
+ */
371
+ public function get_users_interest_rate( $user_id = 0, $default = 0 ) {
372
+
373
+ $rate = $default;
374
+ $saved = mycred_get_user_meta( $user_id, 'mycred_banking_rate_' . $this->mycred_type );
375
+ if ( strlen( $saved ) > 0 )
376
+ $rate = $saved;
377
+
378
+ return apply_filters( 'mycred_get_users_interest_rate', $rate, $user_id, $default, $this );
379
+
380
+ }
381
+
382
+ /**
383
+ * Settings Screen
384
+ * Renders the service settings on the Banking page in the admin area.
385
+ * @since 1.2
386
+ * @version 1.3
387
+ */
388
+ public function preferences() {
389
+
390
+ $cyear = $cmonth = $cday = $chour = $cminute = $csecond = '';
391
+ $pyear = $pmonth = $pday = $phour = $pminute = $psecond = '';
392
+ $editable_roles = array_reverse( get_editable_roles() );
393
+
394
+ // If scheduled, split the date and time of the cron timestamp
395
+ $compound = wp_next_scheduled( $this->cron_compound_key );
396
+ if ( $compound !== false ) {
397
+
398
+ $compound = $this->timestamp_to_date( $compound );
399
+
400
+ $date = date( 'Y-m-d', $compound );
401
+ list ( $cyear, $cmonth, $cday ) = explode( '-', $date );
402
+
403
+ $time = date( 'H:i:s', $compound );
404
+ list ( $chour, $cminute, $cseconds ) = explode( ':', $time );
405
+
406
+ }
407
+
408
+ // If scheduled, split the date and time of the cron timestamp
409
+ $payout = wp_next_scheduled( $this->cron_payout_key );
410
+ if ( $payout !== false ) {
411
+
412
+ $payout = $this->timestamp_to_date( $payout );
413
+
414
+ $date = date( 'Y-m-d', $payout );
415
+ list ( $pyear, $pmonth, $pday ) = explode( '-', $date );
416
+
417
+ $time = date( 'H:i:s', $payout );
418
+ list ( $phour, $pminute, $pseconds ) = explode( ':', $time );
419
+
420
+ }
421
+
422
+ ?>
423
+ <div class="row">
424
+ <div class="col-sm-6">
425
+
426
+ <table class="widefat fixed striped" cellpadding="0" cellspacing="0">
427
+ <thead>
428
+ <tr>
429
+ <th style="width: 100%;" colspan="4"><?php _e( 'Daily Compound Schedule', 'mycred' ); ?></th>
430
+ </tr>
431
+ <tr>
432
+ <th style="width: 30%;"><?php _e( 'Year', 'mycred' ); ?></th>
433
+ <th style="width: 20%;"><?php _e( 'Month', 'mycred' ); ?></th>
434
+ <th style="width: 20%;"><?php _e( 'Day', 'mycred' ); ?></th>
435
+ <th style="width: 30%;"><?php _e( 'Time', 'mycred' ); ?></th>
436
+ </tr>
437
+ </thead>
438
+ <tbody>
439
+ <tr>
440
+ <td><input type="text" name="<?php echo $this->field_name( array( 'cron_compound' => 'year' ) ); ?>" id="<?php echo $this->field_id( array( 'cron_compound' => 'year' ) ); ?>" class="form-control" placeholder="<?php echo $cyear; ?>" maxlength="4" size="6" value="" /></td>
441
+ <td><input type="text" name="<?php echo $this->field_name( array( 'cron_compound' => 'month' ) ); ?>" id="<?php echo $this->field_id( array( 'cron_compound' => 'month' ) ); ?>" class="form-control" placeholder="<?php echo $cmonth; ?>" maxlength="2" size="4" value="" /></td>
442
+ <td><input type="text" name="<?php echo $this->field_name( array( 'cron_compound' => 'day' ) ); ?>" id="<?php echo $this->field_id( array( 'cron_compound' => 'day' ) ); ?>" class="form-control" placeholder="<?php echo $cday; ?>" maxlength="2" size="4" value="" /></td>
443
+ <td><?php echo $this->time_select( $this->field_name( array( 'cron_compound' => 'time' ) ), $this->field_id( array( 'cron_compound' => 'time' ) ), $compound ); ?></td>
444
+ </tr>
445
+ </tbody>
446
+ </table>
447
+
448
+ </div>
449
+ <div class="col-sm-6">
450
+
451
+ <table class="widefat fixed striped" cellpadding="0" cellspacing="0">
452
+ <thead>
453
+ <tr>
454
+ <th style="width: 100%;" colspan="4"><?php _e( 'Payout Schedule', 'mycred' ); ?></th>
455
+ </tr>
456
+ <tr>
457
+ <th style="width: 30%;"><?php _e( 'Year', 'mycred' ); ?></th>
458
+ <th style="width: 20%;"><?php _e( 'Month', 'mycred' ); ?></th>
459
+ <th style="width: 20%;"><?php _e( 'Day', 'mycred' ); ?></th>
460
+ <th style="width: 30%;"><?php _e( 'Time', 'mycred' ); ?></th>
461
+ </tr>
462
+ </thead>
463
+ <tbody>
464
+ <tr>
465
+ <td><input type="text" name="<?php echo $this->field_name( array( 'cron_payout' => 'year' ) ); ?>" id="<?php echo $this->field_id( array( 'cron_payout' => 'year' ) ); ?>" class="form-control" placeholder="<?php echo $pyear; ?>" maxlength="4" size="6" value="" /></td>
466
+ <td><input type="text" name="<?php echo $this->field_name( array( 'cron_payout' => 'month' ) ); ?>" id="<?php echo $this->field_id( array( 'cron_payout' => 'month' ) ); ?>" class="form-control" placeholder="<?php echo $pmonth; ?>" maxlength="2" size="4" value="" /></td>
467
+ <td><input type="text" name="<?php echo $this->field_name( array( 'cron_payout' => 'day' ) ); ?>" id="<?php echo $this->field_id( array( 'cron_payout' => 'day' ) ); ?>" class="form-control" placeholder="<?php echo $pday; ?>" maxlength="2" size="4" value="" /></td>
468
+ <td><?php echo $this->time_select( $this->field_name( array( 'cron_payout' => 'time' ) ), $this->field_id( array( 'cron_payout' => 'time' ) ), $payout ); ?></td>
469
+ </tr>
470
+ </tbody>
471
+ </table>
472
+
473
+ </div>
474
+ </div>
475
+ <div class="row">
476
+ <div class="col-sm-6">
477
+ <h3><?php _e( 'Setup', 'mycred' ); ?></h3>
478
+ <div class="row">
479
+ <div class="col-sm-3 col-xs-10">
480
+ <div class="form-group">
481
+ <label for="<?php echo $this->field_id( array( 'rate' => 'amount' ) ); ?>"><?php _e( 'Interest Rate', 'mycred' ); ?></label>
482
+ <input type="text" name="<?php echo $this->field_name( array( 'rate' => 'amount' ) ); ?>" id="<?php echo $this->field_id( array( 'rate' => 'amount' ) ); ?>" class="form-control" placeholder="<?php _e( 'Required', 'mycred' ); ?>" value="<?php echo esc_attr( $this->prefs['rate']['amount'] ); ?>" />
483
+ </div>
484
+ </div>
485
+ <div class="col-xs-1">
486
+ <div class="form-group">
487
+ <label>&nbsp;</label>
488
+ <p class="form-control-static">%</p>
489
+ </div>
490
+ </div>
491
+ <div class="col-sm-4 col-xs-6">
492
+ <div class="form-group">
493
+ <label for="<?php echo $this->field_id( array( 'name' => 'pay_out' ) ); ?>"><?php _e( 'Payout Frequency', 'mycred' ); ?></label>
494
+ <?php $this->timeframe_dropdown( array( 'rate' => 'pay_out' ), false, false ); ?>
495
+ </div>
496
+ </div>
497
+ <div class="col-sm-4 col-xs-6">
498
+ <div class="form-group">
499
+ <label for="<?php echo $this->field_id( 'min_balance' ); ?>"><?php _e( 'Minimum Balance', 'mycred' ); ?></label>
500
+ <input type="text" name="<?php echo $this->field_name( 'min_balance' ); ?>" id="<?php echo $this->field_id( 'min_balance' ); ?>" class="form-control" placeholder="<?php _e( 'Required', 'mycred' ); ?>" value="<?php echo $this->core->number( $this->prefs['min_balance'] ); ?>" />
501
+ </div>
502
+ </div>
503
+ </div>
504
+ </div>
505
+ <div class="col-sm-6">
506
+ <h3><?php _e( 'Payout Log Table', 'mycred' ); ?></h3>
507
+ <div class="row">
508
+ <div class="col-xs-12">
509
+ <div class="form-group">
510
+ <label for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
511
+ <input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" class="form-control" value="<?php echo esc_attr( $this->prefs['log'] ); ?>" />
512
+ </div>
513
+ </div>
514
+ </div>
515
+ </div>
516
+ </div>
517
+ <div class="row">
518
+ <div class="col-xs-12">
519
+ <p><span class="description"><?php _e( 'Changing the payout period once the service is enabled, will only take effect once the currently scheduled payout runs. To change this, you will also need to adjust the payout schedule above.', 'mycred' ); ?></span></p>
520
+ </div>
521
+ </div>
522
+ <div class="row">
523
+ <div class="col-sm-6">
524
+ <h3><?php _e( 'Exclude by ID', 'mycred' ); ?></h3>
525
+ <div class="row">
526
+ <div class="col-xs-12">
527
+ <div class="form-group">
528
+ <label for="<?php echo $this->field_id( 'exclude_ids' ); ?>"><?php _e( 'Comma separated list of user IDs', 'mycred' ); ?></label>
529
+ <input type="text" name="<?php echo $this->field_name( 'exclude_ids' ); ?>" id="<?php echo $this->field_id( 'exclude_ids' ); ?>" class="form-control" placeholder="<?php _e( 'Comma separated list of user IDs', 'mycred' ); ?>" value="<?php echo esc_attr( $this->prefs['exclude_ids'] ); ?>" />
530
+ </div>
531
+ </div>
532
+ </div>
533
+ </div>
534
+ <div class="col-sm-6">
535
+ <h3><?php _e( 'Exclude by Role', 'mycred' ); ?></h3>
536
+ <div class="row">
537
+ <div class="col-xs-12">
538
+ <div class="form-group">
539
+ <label for="<?php echo $this->field_id( 'exclude_roles' ); ?>"><?php _e( 'Roles to exclude', 'mycred' ); ?></label>
540
+ </div>
541
+ <div class="row">
542
+ <?php
543
+
544
+ foreach ( $editable_roles as $role => $details ) {
545
+
546
+ $name = translate_user_role( $details['name'] );
547
+
548
+ echo '<div class="col-xs-6"><div class="checkbox"><label for="' . $this->field_id( 'exclude-roles-' . $role ) . '"><input type="checkbox" name="' . $this->field_name( 'exclude_roles][' ) . '" id="' . $this->field_id( 'exclude-roles-' . $role ) . '" value="' . esc_attr( $role ) . '"';
549
+ if ( in_array( $role, (array) $this->prefs['exclude_roles'] ) ) echo ' checked="checked"';
550
+ echo ' />' . $name . '</label></div></div>';
551
+ }
552
+
553
+ ?>
554
+ </div>
555
+ </div>
556
+ </div>
557
+ </div>
558
+ </div>
559
+ <?php
560
+
561
+ do_action( 'mycred_banking_compound_interest', $this );
562
+
563
+ }
564
+
565
+ /**
566
+ * Sanitise Preferences
567
+ * @since 1.2
568
+ * @version 1.4
569
+ */
570
+ public function sanitise_preferences( $post ) {
571
+
572
+ $new_settings = array( 'rate' => array() );
573
+ $new_settings['rate']['amount'] = str_replace( ',', '.', sanitize_text_field( $post['rate']['amount'] ) );
574
+ if ( $new_settings['rate']['amount'] == '' ) $new_settings['rate']['amount'] = 0;
575
+
576
+ $new_settings['rate']['pay_out'] = sanitize_text_field( $post['rate']['pay_out'] );
577
+
578
+ $new_settings['log'] = sanitize_text_field( $post['log'] );
579
+ $new_settings['min_balance'] = $this->core->number( str_replace( ',', '.', sanitize_text_field( $post['min_balance'] ) ) );
580
+ $new_settings['exclude_ids'] = sanitize_text_field( $post['exclude_ids'] );
581
+
582
+ if ( ! isset( $post['exclude_roles'] ) )
583
+ $post['exclude_roles'] = array();
584
+
585
+ $new_settings['exclude_roles'] = $post['exclude_roles'];
586
+
587
+ $rescheduled = false;
588
+
589
+ // Re-schedule compound cron job
590
+ if ( $post['cron_compound']['year'] != '' && $post['cron_compound']['month'] != '' && $post['cron_compound']['day'] != '' && $post['cron_compound']['time'] != '' ) {
591
+
592
+ $year = absint( $post['cron_compound']['year'] );
593
+ $month = zeroise( absint( $post['cron_compound']['month'] ), 2 );
594
+ $day = zeroise( absint( $post['cron_compound']['day'] ), 2 );
595
+
596
+ $compound = $this->date_to_timestamp( $year . '-' . $month . '-' . $day . ' ' . $post['cron_compound']['time'] . ':00' );
597
+ if ( $compound !== false && $compound > $this->now ) {
598
+
599
+ $timestamp = wp_next_scheduled( $this->cron_compound_key );
600
+ if ( $timestamp !== false ) {
601
+ wp_clear_scheduled_hook( $this->cron_compound_key );
602
+ $rescheduled = true;
603
+ }
604
+
605
+ wp_schedule_single_event( $compound, $this->cron_compound_key );
606
+
607
+ }
608
+
609
+ }
610
+
611
+ // Re-schedule payout cron job
612
+ if ( $post['cron_payout']['year'] != '' && $post['cron_payout']['month'] != '' && $post['cron_payout']['day'] != '' && $post['cron_payout']['time'] != '' ) {
613
+
614
+ $year = absint( $post['cron_payout']['year'] );
615
+ $month = zeroise( absint( $post['cron_payout']['month'] ), 2 );
616
+ $day = zeroise( absint( $post['cron_payout']['day'] ), 2 );
617
+
618
+ $payout = $this->date_to_timestamp( $year . '-' . $month . '-' . $day . ' ' . $post['cron_payout']['time'] . ':00' );
619
+ if ( $payout !== false && $payout > $this->now ) {
620
+
621
+ $timestamp = wp_next_scheduled( $this->cron_payout_key );
622
+ if ( $timestamp !== false ) {
623
+ wp_clear_scheduled_hook( $this->cron_payout_key );
624
+ $rescheduled = true;
625
+ }
626
+
627
+ wp_schedule_single_event( $payout, $this->cron_payout_key );
628
+
629
+ }
630
+
631
+ }
632
+
633
+ return apply_filters( 'mycred_banking_save_interest', $new_settings, $this, $rescheduled );
634
+
635
+ }
636
+
637
+ /**
638
+ * User Screen
639
+ * @since 1.7
640
+ * @version 1.0
641
+ */
642
+ public function user_screen( $user ) {
643
+
644
+ // Only visible to admins
645
+ if ( ! mycred_is_admin() ) return;
646
+
647
+ $users_rate = mycred_get_user_meta( $user->ID, 'mycred_banking_rate_' . $this->mycred_type );
648
+
649
+ ?>
650
+ <table class="form-table">
651
+ <tr>
652
+ <th><label for="compoun-interest-rate-<?php echo $this->mycred_type; ?>"><?php _e( 'Interest Rate', 'mycred' ); echo ' (' . $this->core->plural() . ')'; ?></label></th>
653
+ <td>
654
+ <input type="text" size="8" name="mycred_interest_rate[<?php echo $this->mycred_type; ?>][rate]" id="compoun-interest-rate-<?php echo $this->mycred_type; ?>" placeholder="<?php echo esc_attr( $this->prefs['rate']['amount'] . ' %' ); ?>" value="<?php echo esc_attr( $users_rate ); ?>" /> %
655
+ <p><span class="description"><?php _e( 'Leave empty to pay the default rate.', 'mycred' ); ?></span></p>
656
+ </td>
657
+ </tr>
658
+ </table>
659
+ <?php
660
+
661
+ }
662
+
663
+ /**
664
+ * Save User Override
665
+ * @since 1.5.2
666
+ * @version 1.0.2
667
+ */
668
+ function save_custom_rate( $user_id ) {
669
+
670
+ // Only visible to admins
671
+ if ( ! mycred_is_admin() ) return;
672
+
673
+ if ( isset( $_POST['mycred_interest_rate'] ) && array_key_exists( $this->mycred_type, $_POST['mycred_interest_rate'] ) ) {
674
+
675
+ $rate = sanitize_text_field( $_POST['mycred_interest_rate'][ $this->mycred_type ]['rate'] );
676
+ if ( strlen( $rate ) > 0 ) {
677
+
678
+ $rate = str_replace( ',', '.', $rate );
679
+ if ( $rate != $this->prefs['rate']['amount'] )
680
+ mycred_update_user_meta( $user_id, 'mycred_banking_rate_' . $this->mycred_type, '', $rate );
681
+ else
682
+ mycred_delete_user_meta( $user_id, 'mycred_banking_rate_' . $this->mycred_type );
683
+
684
+ }
685
+
686
+ }
687
+
688
+ }
689
+
690
+ }
691
+ endif;
692
+
693
+ ?>
addons/banking/services/mycred-service-payouts.php ADDED
@@ -0,0 +1,1004 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * myCRED Bank Service - Recurring Payouts
6
+ * @since 1.2
7
+ * @version 1.2
8
+ */
9
+ if ( ! class_exists( 'myCRED_Banking_Service_Payouts' ) ) :
10
+ class myCRED_Banking_Service_Payouts extends myCRED_Service {
11
+
12
+ public $default_schedule = array();
13
+ public $statuses = array();
14
+ public $schedules = array();
15
+ public $log_reference = '';
16
+
17
+ /**
18
+ * Construct
19
+ */
20
+ function __construct( $service_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) {
21
+
22
+ parent::__construct( array(
23
+ 'id' => 'payouts',
24
+ 'defaults' => array()
25
+ ), $service_prefs, $type );
26
+
27
+ $this->default_schedule = mycred_get_recurring_payout_defaults();
28
+
29
+ $this->statuses = array(
30
+ 0 => __( 'Waiting to Start', 'mycred' ),
31
+ 1 => __( 'Active', 'mycred' ),
32
+ 2 => __( 'Running', 'mycred' ),
33
+ 3 => __( 'Finished', 'mycred' ),
34
+ 4 => __( 'Stopped', 'mycred' )
35
+ );
36
+
37
+ $this->schedules = mycred_get_recurring_payout_schedules( $type );
38
+ $this->log_reference = apply_filters( 'mycred_bank_recurring_reference', 'recurring', $this );
39
+
40
+ }
41
+
42
+ /**
43
+ * Activate Service
44
+ * @since 1.5.2
45
+ * @version 1.1
46
+ */
47
+ public function activate() {
48
+
49
+ if ( ! empty( $this->schedules ) ) {
50
+ foreach ( $this->schedules as $schedule_id => $setup ) {
51
+
52
+ // Status 3 = finished and Status 4 = Stopped
53
+ if ( absint( $setup['status'] ) > 2 ) continue;
54
+
55
+ $next_run = mycred_banking_get_next_payout( $setup['frequency'], $setup['last_run'] );
56
+ $timestamp = wp_next_scheduled( 'mycred-recurring-' . $schedule_id );
57
+
58
+ if ( $timestamp !== false )
59
+ $next_run = $timestamp;
60
+
61
+ // We did not miss our schedule, lets re-schedule
62
+ if ( $next_run > $this->now )
63
+ wp_schedule_single_event( $next_run, 'mycred-recurring-' . $schedule_id, array( 'id' => $schedule_id ) );
64
+
65
+ // Upps, we missed it, clear and disable this setup
66
+ else {
67
+
68
+ $setup['status'] = 4;
69
+ mycred_update_recurring_payout( $schedule_id, $setup, $this->mycred_type );
70
+
71
+ if ( $timestamp !== false )
72
+ wp_clear_scheduled_hook( 'mycred-recurring-' . $schedule_id, array( 'id' => $schedule_id ) );
73
+
74
+ }
75
+
76
+ }
77
+ }
78
+
79
+ }
80
+
81
+ /**
82
+ * Deactivation
83
+ * @since 1.2
84
+ * @version 1.1
85
+ */
86
+ public function deactivate() {
87
+
88
+ if ( ! empty( $this->schedules ) ) {
89
+ foreach ( $this->schedules as $schedule_id => $setup ) {
90
+
91
+ // Status 3 = finished and Status 4 = Stopped
92
+ if ( absint( $setup['status'] ) > 2 ) continue;
93
+
94
+ wp_clear_scheduled_hook( 'mycred-recurring-' . $schedule_id, array( 'id' => $schedule_id ) );
95
+
96
+ }
97
+ }
98
+
99
+ }
100
+
101
+ /**
102
+ * Run
103
+ * @since 1.2
104
+ * @version 1.1
105
+ */
106
+ public function run() {
107
+
108
+ if ( ! empty( $this->schedules ) ) {
109
+ foreach ( $this->schedules as $schedule_id => $setup ) {
110
+
111
+ // Status 3 = finished and Status 4 = Stopped
112
+ if ( absint( $setup['status'] ) > 2 ) continue;
113
+
114
+ add_action( 'mycred-recurring-' . $schedule_id, array( $this, 'run_schedule' ) );
115
+
116
+ }
117
+ }
118
+
119
+ }
120
+
121
+ /**
122
+ * Run Schedule
123
+ * @since 1.2
124
+ * @version 1.1
125
+ */
126
+ public function run_schedule( $schedule_id = NULL ) {
127
+
128
+ if ( $schedule_id === NULL ) return;
129
+
130
+ // Get settings
131
+ $setup = mycred_get_recurring_payout( $schedule_id, $this->mycred_type );
132
+ $instance = 'mycred-recurring-' . $schedule_id;
133
+ $infinite_run = ( ( $setup['runs_remaining'] < 0 ) ? true : false );
134
+ $runs_left = absint( $setup['runs_remaining'] );
135
+ $next_run = mycred_banking_get_next_payout( $setup['frequency'], $this->now );
136
+
137
+ // Need to make sure amount is not zero
138
+ if ( $setup['payout'] == '' || $this->core->number( $setup['payout'] ) === $this->core->zero() ) return;
139
+
140
+ // No runs remaining - Move to finish
141
+ if ( ! $infinite_run && $runs_left === 0 ) {
142
+
143
+ $setup['status'] = 3;
144
+ $setup['last_run'] = $this->now;
145
+
146
+ mycred_update_recurring_payout( $schedule_id, $setup, $this->mycred_type );
147
+ wp_clear_scheduled_hook( $instance );
148
+
149
+ return;
150
+
151
+ }
152
+
153
+ // No need to run if the central bank is out of funds
154
+ $settings = mycred_get_banking_addon_settings( NULL, $this->mycred_type );
155
+ $ignore_central_bank = false;
156
+ if ( in_array( 'central', $settings['active'] ) && $setup['ignore_central'] == 1 )
157
+ $ignore_central_bank = true;
158
+
159
+ // Default number of balances we will be running through for now
160
+ $number = absint( apply_filters( 'mycred_recurring_max_limit', 1500, $this ) );
161
+ $original_start = get_option( 'mycred-recurring-next-run-' . $schedule_id, false );
162
+
163
+ $single_instance = true;
164
+ $transient_key = 'mycred-recurring-' . $schedule_id;
165
+ $offset = get_transient( $transient_key );
166
+ if ( $offset === false ) $offset = 0;
167
+
168
+ // Get eligible users
169
+ $eligeble_users = $this->get_eligible_users( $number, $offset, $setup );
170
+
171
+ // Check if we can do this in a single instance or if we need multiple
172
+ if ( ( $eligeble_users['total'] - $offset ) > $number ) {
173
+
174
+ $single_instance = false;
175
+
176
+ // Schedule our next event in 2 minutes
177
+ wp_schedule_single_event( ( $this->now + 180 ), $instance );
178
+
179
+ // Apply limit and set a transient to keep track of our progress
180
+ if ( $offset === 0 )
181
+ set_transient( $transient_key, $number, HOUR_IN_SECONDS );
182
+
183
+ // While in loop, we need to keep track of our progress
184
+ else {
185
+
186
+ delete_transient( $transient_key );
187
+ $offset += count( $eligeble_users['results'] );
188
+ set_transient( $transient_key, $offset, HOUR_IN_SECONDS );
189
+
190
+ }
191
+
192
+ if ( $original_start === false )
193
+ update_option( 'mycred-recurring-next-run-' . $schedule_id, $next_run );
194
+
195
+ }
196
+
197
+ // Single instance
198
+ else {
199
+
200
+ if ( $original_start === false )
201
+ $original_start = $next_run;
202
+
203
+ else {
204
+ delete_transient( $transient_key );
205
+ delete_option( 'mycred-recurring-next-run-' . $schedule_id );
206
+ }
207
+
208
+ wp_schedule_single_event( $original_start, $instance, array( 'id' => $schedule_id ) );
209
+
210
+ }
211
+
212
+ // Loop through users and payout
213
+ $missed = $completed = 0;
214
+ if ( ! empty( $eligeble_users['results'] ) ) {
215
+
216
+ foreach ( $eligeble_users['results'] as $user_id ) {
217
+
218
+ if ( ! $ignore_central_bank ) {
219
+
220
+ $paid = $this->core->add_creds(
221
+ $this->log_reference,
222
+ $user_id,
223
+ $setup['payout'],
224
+ $setup['log_template'],
225
+ 0,
226
+ $schedule_id,
227
+ $this->mycred_type
228
+ );
229
+
230
+ }
231
+
232
+ // Ignore the central bank and just pay
233
+ // Curcumvents any custom code using the mycred_add, mycred_run_this and mycred_add_finished filters.
234
+ else {
235
+
236
+ $this->core->update_users_balance( $user_id, $setup['payout'], $this->mycred_type );
237
+ $this->core->add_to_log(
238
+ $this->log_reference,
239
+ $user_id,
240
+ $setup['payout'],
241
+ $setup['log_template'],
242
+ 0,
243
+ $schedule_id,
244
+ $this->mycred_type
245
+ );
246
+
247
+ $paid = true;
248
+
249
+ }
250
+
251
+ if ( $paid )
252
+ $completed ++;
253
+ else
254
+ $missed ++;
255
+
256
+ }
257
+
258
+ }
259
+
260
+ if ( $setup['status'] == 0 )
261
+ $setup['status'] = 1;
262
+
263
+ $setup['total_completed'] = ( $setup['total_completed'] + $completed );
264
+ $setup['total_misses'] = ( $setup['total_misses'] + $missed );
265
+
266
+ mycred_update_recurring_payout( $schedule_id, $setup, $this->mycred_type );
267
+
268
+ // In case we finished, update the schedule and clear the CRON
269
+ if ( $single_instance ) {
270
+
271
+ if ( ! $infinite_run ) {
272
+
273
+ $runs_left--;
274
+
275
+ $setup['runs_remaining'] = $runs_left;
276
+
277
+ }
278
+
279
+ $setup['last_run'] = $this->now;
280
+
281
+ if ( ! $infinite_run && $runs_left == 0 )
282
+ $setup['status'] = 3;
283
+
284
+ mycred_update_recurring_payout( $schedule_id, $setup, $this->mycred_type );
285
+
286
+ if ( ! $infinite_run && $runs_left === 0 )
287
+ wp_clear_scheduled_hook( $instance );
288
+
289
+ }
290
+
291
+ }
292
+
293
+ /**
294
+ * Get Eligible Users
295
+ * @since 1.7
296
+ * @version 1.0
297
+ */
298
+ public function get_eligible_users( $number = 0, $offset = 0, $setup ) {
299
+
300
+ global $wpdb;
301
+
302
+ $query_args = array();
303
+ $meta_query = array();
304
+
305
+ // Only interested in the user IDs.
306
+ $query_args['fields'] = 'ID';
307
+ $query_args['orderby'] = 'ID';
308
+
309
+ $query_args['number'] = $number;
310
+
311
+ if ( $offset > 0 )
312
+ $query_args['offset'] = $offset;
313
+
314
+ // Limit by minimum balance
315
+ if ( $setup['min_balance'] != 0 && $setup['max_balance'] == 0 )
316
+ $meta_query[] = array(
317
+ 'key' => $this->mycred_type,
318
+ 'value' => $this->core->number( $setup['min_balance'] ),
319
+ 'compare' => '>=',
320
+ 'type' => 'NUMERIC'
321
+ );
322
+
323
+ // Limit by maximum balance
324
+ elseif ( $setup['min_balance'] == 0 && $setup['max_balance'] != 0 )
325
+ $meta_query[] = array(
326
+ 'key' => $this->mycred_type,
327
+ 'value' => $this->core->number( $setup['max_balance'] ),
328
+ 'compare' => '<',
329
+ 'type' => 'NUMERIC'
330
+ );
331
+
332
+ // Range
333
+ elseif ( $setup['min_balance'] != 0 && $setup['max_balance'] != 0 )
334
+ $meta_query[] = array(
335
+ 'key' => $this->mycred_type,
336
+ 'value' => array( $this->core->number( $setup['min_balance'] ), $this->core->number( $setup['max_balance'] ) ),
337
+ 'compare' => 'BETWEEN',
338
+ 'type' => 'NUMERIC'
339
+ );
340
+
341
+ // Limit by id
342
+ if ( $setup['id_list'] != '' ) {
343
+
344
+ $user_id_list = array();
345
+
346
+ $list_of_ids = explode( ',', $setup['id_list'] );
347
+ foreach ( $list_of_ids as $user_id ) {
348
+ $user_id = absint( trim( $user_id ) );
349
+ if ( $user_id !== 0 && ! in_array( $user_id, $clean_ids ) )
350
+ $user_id_list[] = $user_id;
351
+ }
352
+
353
+ // Take into account users that have been excluded via the point type
354
+ if ( ! empty( $this->core->exclude['list'] ) ) {
355
+ $list_of_ids = explode( ',', $this->core->exclude['list'] );
356
+ foreach ( $list_of_ids as $user_id ) {
357
+ $user_id = absint( trim( $user_id ) );
358
+ if ( $user_id !== 0 && ! in_array( $user_id, $clean_ids ) )
359
+ $user_id_list[] = $user_id;
360
+ }
361
+ }
362
+
363
+ if ( ! empty( $user_id_list ) ) {
364
+
365
+ if ( $setup['id_exclude'] == 'exclude' )
366
+ $query_args['exclude'] = $user_id_list;
367
+ else
368
+ $query_args['include'] = $user_id_list;
369
+
370
+ }
371
+
372
+ }
373
+
374
+ // Limit by role
375
+ if ( ! empty( $setup['role_list'] ) ) {
376
+
377
+ $blog_id = 0;
378
+ if ( ! mycred_centralize_log() ) {
379
+ $blog_id = get_current_blog_id();
380
+ $query_args['blog_id'] = $blog_id;
381
+ }
382
+
383
+ $role_query = array();
384
+
385
+ // Exclude by role = role is not x AND role is not y
386
+ // Include by role = role is x OR role is y
387
+ if ( count( $setup['role_list'] ) > 1 )
388
+ $role_query['relation'] = ( ( $setup['role_exclude'] == 'exclude' ) ? 'AND' : 'OR' );
389
+
390
+ // Since the "role__in" and "role__not_in" arguments are not available until WordPress 4.4, we just use meta query for this.
391
+ foreach ( $setup['role_list'] as $role )
392
+ $role_query[] = array(
393
+ 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
394
+ 'value' => '"' . $role . '"',
395
+ 'compare' => ( ( $setup['role_exclude'] == 'exclude' ) ? 'NOT LIKE' : 'LIKE' )
396
+ );
397
+
398
+ $meta_query[] = $role_query;
399
+
400
+ }
401
+
402
+
403
+ if ( ! empty( $meta_query ) )
404
+ $query_args['meta_query'] = $meta_query;
405
+
406
+ $query = new WP_User_Query( $query_args );
407
+
408
+ return array(
409
+ 'total' => $query->get_total(),
410
+ 'results' => $query->get_results()
411
+ );
412
+
413
+ }
414
+
415
+ /**
416
+ * Display Schedule Table
417
+ * @since 1.7
418
+ * @version 1.0
419
+ */
420
+ public function display_schedule_table( $schedule = NULL ) {
421
+
422
+ $content = '';
423
+ $date_format = get_option( 'date_format' );
424
+ $time_format = get_option( 'time_format' );
425
+ $timeframes = mycred_banking_get_timeframes();
426
+
427
+ $schedules = mycred_get_recurring_payout_schedules( $this->mycred_type );
428
+
429
+ // This function is also used to display just one particular table row
430
+ if ( $schedule !== NULL )
431
+ $schedules = array( $schedule['id'] => $schedule );
432
+
433
+ if ( ! empty( $schedules ) ) {
434
+
435
+ ob_start();
436
+
437
+ foreach ( $schedules as $schedule_id => $setup ) {
438
+
439
+ $setup = shortcode_atts( $this->default_schedule, $setup );
440
+ $last_run = $this->timestamp_to_date( $setup['last_run'] );
441
+ $next_run = mycred_banking_get_next_payout( $setup['frequency'], $last_run );
442
+
443
+ // Pending start
444
+ if ( $setup['status'] == 0 ) {
445
+ $next_run = date( $date_format . ' ' . $time_format, $last_run );
446
+ $last_run = __( 'Not yet started', 'mycred' );
447
+ }
448
+
449
+ // Running
450
+ elseif ( $setup['status'] == 1 ) {
451
+ $last_run = date( $date_format . ' ' . $time_format, $last_run );
452
+ $next_run = date( $date_format . ' ' . $time_format, $next_run );
453
+ }
454
+
455
+ // CRON job is running right now
456
+ elseif ( $setup['status'] == 2 ) {
457
+ $last_run = __( 'Currently Running', 'mycred' );
458
+ $next_run = '-';
459
+ }
460
+
461
+ // Finished or stopped
462
+ else {
463
+ $last_run = date( $date_format . ' ' . $time_format, $last_run );
464
+ $next_run = '-';
465
+ }
466
+
467
+ ?>
468
+ <tr id="schedule-<?php echo $schedule_id; ?>">
469
+ <td class="col-job-title">
470
+ <div><?php echo esc_attr( $setup['job_title'] ); ?></div>
471
+ <div class="row-actions">
472
+ <span class="edit"><a href="javascript:void(0);" class="view-recurring-schedule" data-id="<?php echo $schedule_id; ?>" data-title="<?php _e( 'View Schedule', 'mycred' ); ?>"><?php _e( 'View', 'mycred' ); ?></a> | </span> <span class="delete"><a href="javascript:void(0);" class="delete-recurring-schedule" data-id="<?php echo $schedule_id; ?>" data-title="<?php _e( 'Delete Schedule', 'mycred' ); ?>"><?php _e( 'Delete', 'mycred' ); ?></a></span>
473
+ </div>
474
+ </td>
475
+ <td class="col-job-status">
476
+ <div><?php if ( array_key_exists( $setup['status'], $this->statuses ) ) echo $this->statuses[ $setup['status'] ]; else echo '-'; ?></div>
477
+ </td>
478
+ <td class="col-job-frequency">
479
+ <div><?php if ( array_key_exists( $setup['frequency'], $timeframes ) ) echo $timeframes[ $setup['frequency'] ]['label']; else echo '-'; ?></div>
480
+ </td>
481
+ <td class="col-job-last-ran">
482
+ <div><?php echo $last_run; ?></div>
483
+ </td>
484
+ <td class="col-job-next-run">
485
+ <div><?php echo $next_run; ?></div>
486
+ </td>
487
+ </tr>
488
+ <?php
489
+
490
+ }
491
+
492
+ $content = ob_get_contents();
493
+ ob_end_clean();
494
+
495
+ }
496
+
497
+ return $content;
498
+
499
+ }
500
+
501
+ /**
502
+ * View Schedule Form
503
+ * @since 1.7
504
+ * @version 1.0
505
+ */
506
+ public function view_schedule_form( $schedule_id, $setup ) {
507
+
508
+ $limits = array();
509
+ $date_format = get_option( 'date_format' );
510
+ $time_format = get_option( 'time_format' );
511
+ $timeframes = mycred_banking_get_timeframes();
512
+ $settings = mycred_get_banking_addon_settings( NULL, $this->mycred_type );
513
+
514
+ global $wpdb;
515
+
516
+ $total_payout = $wpdb->get_var( $wpdb->prepare( "SELECT SUM(creds) FROM {$this->core->log_table} WHERE ref = %s AND data = %s;", $this->log_reference, $schedule_id ) );
517
+ if ( $total_payout === NULL ) $total_payout = 0;
518
+
519
+ $last_run = mycred_gmt_timestamp_to_local( $setup['last_run'] );
520
+ $eligeble_users = $this->get_eligible_users( 1, 5, $setup );
521
+
522
+ if ( $setup['min_balance'] != 0 )
523
+ $limits[] = '<div class="col-xs-6"><p>' . __( 'Min. Balance', 'mycred' ) . ': ' . $this->core->format_creds( $setup['min_balance'] ) . '</p></div>';
524
+
525
+ if ( $setup['max_balance'] != 0 )
526
+ $limits[] = '<div class="col-xs-6"><p>' . __( 'Max. Balance', 'mycred' ) . ': ' . $this->core->format_creds( $setup['max_balance'] ) . '</p></div>';
527
+
528
+ if ( $setup['id_list'] != '' ) {
529
+
530
+ if ( $setup['id_exclude'] == 'exclude' )
531
+ $limits[] = '<div class="col-xs-6"><p>' . __( 'Exclude Users', 'mycred' ) . ': ' . esc_attr( $setup['id_list'] ) . '</p></div>';
532
+ else
533
+ $limits[] = '<div class="col-xs-6"><p>' . __( 'Include Users', 'mycred' ) . ': ' . esc_attr( $setup['id_list'] ) . '</p></div>';
534
+
535
+ }
536
+
537
+ if ( ! empty( $setup['role_list'] ) ) {
538
+
539
+ if ( $setup['role_exclude'] == 'exclude' )
540
+ $limits[] = '<div class="col-xs-12"><p>' . __( 'Exclude Roles', 'mycred' ) . ': <code>' . implode( '</code>, <code>', $setup['role_list'] ) . '</code></p></div>';
541
+ else
542
+ $limits[] = '<div class="col-xs-12"><p>' . __( 'Include Roles', 'mycred' ) . ': <code>' . implode( '</code>, <code>', $setup['role_list'] ) . '</code></p></div>';
543
+
544
+ }
545
+
546
+ ob_start();
547
+
548
+ ?>
549
+ <div class="padded">
550
+ <div class="row">
551
+ <div class="col-xs-3">
552
+ <div class="form-group">
553
+ <label><?php _e( 'ID', 'mycred' ); ?></label>
554
+ <p class="form-control-static"><?php echo esc_attr( $schedule_id ); ?></p>
555
+ </div>
556
+ </div>
557
+ <div class="col-xs-6">
558
+ <div class="form-group">
559
+ <label><?php _e( 'Job Title', 'mycred' ); ?></label>
560
+ <p class="form-control-static"><?php echo esc_attr( $setup['job_title'] ); ?></p>
561
+ </div>
562
+ </div>
563
+ <div class="col-xs-3">
564
+ <div class="form-group">
565
+ <label><?php _e( 'Status', 'mycred' ); ?></label>
566
+ <p class="form-control-static"><?php if ( array_key_exists( $setup['status'], $this->statuses ) ) echo $this->statuses[ $setup['status'] ]; else echo '-'; ?></p>
567
+ </div>
568
+ </div>
569
+ </div>
570
+ <div class="row">
571
+ <div class="col-xs-5">
572
+ <div class="form-group">
573
+ <label><?php if ( $setup['status'] == 0 ) _e( 'Start Date', 'mycred' ); else _e( 'Last Run', 'mycred' ); ?></label>
574
+ <p class="form-control-static"><?php echo date( $date_format . ' ' . $time_format, $last_run ); ?></p>
575
+ </div>
576
+ </div>
577
+ <div class="col-xs-2">
578
+ <div class="form-group">
579
+ <label><?php _e( 'Eligible', 'mycred' ); ?></label>
580
+ <p class="form-control-static"><?php printf( _n( '1 User', '%d Users', $eligeble_users['total'], 'mycred' ), $eligeble_users['total'] ); ?></p>
581
+ </div>
582
+ </div>
583
+ <div class="col-xs-2">
584
+ <div class="form-group">
585
+ <label><?php _e( 'Runs', 'mycred' ); ?></label>
586
+ <p class="form-control-static"><?php echo esc_attr( ( ( $setup['total_runs'] < 0 ) ? __( 'Infinite', 'mycred' ) : $setup['total_runs'] ) ); ?></p>
587
+ </div>
588
+ </div>
589
+ <div class="col-xs-3">
590
+ <div class="form-group">
591
+ <label><?php _e( 'Payout', 'mycred' ); ?></label>
592
+ <p class="form-control-static"><?php printf( '%s / %s', $this->core->format_creds( $setup['payout'] ), $timeframes[ $setup['frequency'] ]['single'] ); ?></p>
593
+ </div>
594
+ </div>
595
+ </div>
596
+ <?php if ( ! empty( $limits ) ) : ?>
597
+ <div class="row">
598
+ <div class="col-xs-12">
599
+ <strong><label><?php _e( 'Limits', 'mycred' ); ?>:</label></strong>
600
+ <div class="row list">
601
+ <?php echo implode( '', $limits ); ?>
602
+ </div>
603
+ </div>
604
+ </div>
605
+ <?php endif; ?>
606
+ <div class="row">
607
+ <div class="col-xs-12">
608
+ <?php if ( $setup['ignore_central'] == 1 && in_array( 'central', $settings['active'] ) ) : ?>
609
+ <p><strong><?php _e( 'Will payout even if the central bank account has run out of funds.', 'mycred' ); ?></strong></p>
610
+ <?php endif; ?>
611
+ <table class="widefat fixed striped">
612
+ <thead>
613
+ <tr>
614
+ <th style="width: 25%;"><?php _e( 'Paid Out', 'mycred' ); ?></th>
615
+ <th style="width: 25%;"><?php _e( 'Completed', 'mycred' ); ?></th>
616
+ <th style="width: 25%;"><?php _e( 'Misses', 'mycred' ); ?> *</th>
617
+ <th style="width: 25%;"><?php _e( 'Runs Left', 'mycred' ); ?></th>
618
+ </tr>
619
+ </thead>
620
+ <tbody>
621
+ <tr>
622
+ <td>
623
+ <h1><?php echo $this->core->format_creds( $total_payout ); ?></h1>
624
+ </td>
625
+ <td>
626
+ <h1><?php echo absint( $setup['total_completed'] ); ?></h1>
627
+ </td>
628
+ <td>
629
+ <h1><?php echo absint( $setup['total_misses'] ); ?></h1>
630
+ </td>
631
+ <td>
632
+ <h1><?php echo absint( $setup['runs_remaining'] ); ?></h1>
633
+ </td>
634
+ </tr>
635
+ </tbody>
636
+ </table>
637
+ <p><span class="description">* <?php _e( 'A miss is when a payout was declined by the plugin. This can be due to custom code declining the payout or if the user is excluded.', 'mycred' ); ?></span></p>
638
+ </div>
639
+ </div>
640
+ </div>
641
+ <?php
642
+
643
+ $content = ob_get_contents();
644
+ ob_end_clean();
645
+
646
+ return $content;
647
+
648
+ }
649
+
650
+ /**
651
+ * Manage Schedule Form
652
+ * @since 1.7
653
+ * @version 1.0
654
+ */
655
+ public function manage_schedule_form( $schedule_id, $setup ) {
656
+
657
+ $year = $month = $day = '';
658
+ $time = NULL;
659
+
660
+ $timeframes = mycred_banking_get_timeframes();
661
+ $compound = wp_next_scheduled( $setup['last_run'] );
662
+ $settings = mycred_get_banking_addon_settings( NULL, $this->mycred_type );
663
+
664
+ if ( $compound !== false ) {
665
+
666
+ $last_run = $this->timestamp_to_date( $setup['last_run'] );
667
+
668
+ $date = date( 'Y-m-d', $last_run );
669
+ list ( $year, $month, $day ) = explode( '-', $date );
670
+
671
+ $time = date( 'H:i', $last_run );
672
+
673
+ }
674
+
675
+ ob_start();
676
+
677
+ ?>
678
+ <div class="padded">
679
+ <div class="row">
680
+ <div class="col-xs-6">
681
+ <div class="form-group">
682
+ <label><?php _e( 'Schedule Title', 'mycred' ); ?></label>
683
+ <input type="text" name="job_title" class="form-control cant-be-empty" value="<?php echo esc_attr( $setup['job_title'] ); ?>" />
684
+ </div>
685
+ </div>
686
+ <div class="col-xs-3">
687
+ <div class="form-group">
688
+ <label><?php echo $this->core->plural(); ?></label>
689
+ <input type="text" name="payout" class="form-control cant-be-empty" placeholder="<?php echo $this->core->zero(); ?>" value="" />
690
+ </div>
691
+ </div>
692
+ <div class="col-xs-3">
693
+ <div class="form-group">
694
+ <label><?php _e( 'Frequency', 'mycred' ); ?></label>
695
+ <select name="frequency" class="form-control">
696
+ <?php
697
+
698
+ foreach ( $timeframes as $value => $data ) {
699
+ echo '<option value="' . $value . '"';
700
+ if ( $setup['frequency'] == $value ) echo ' selected="selected"';
701
+ echo '>' . $data['label'] . '</option>';
702
+ }
703
+
704
+ ?>
705
+ </select>
706
+ </div>
707
+ </div>
708
+ <div class="col-xs-12">
709
+ <div class="form-group">
710
+ <label><?php _e( 'Log Template', 'mycred' ); ?></label>
711
+ <input type="text" name="log_template" class="form-control cant-be-empty" value="<?php echo esc_attr( $setup['log_template'] ); ?>" />
712
+ </div>
713
+ <?php if ( in_array( 'central', $settings['active'] ) ) : ?>
714
+ <div class="form-group">
715
+ <div class="checkbox"><label for="new-schedule-ignore-central-bank"><input type="checkbox" name="ignore_central" id="new-schedule-ignore-central-bank"<?php checked( $setup['ignore_central'], 1 ); ?> value="1" /> <?php _e( 'Payout even if the central bank account runs out of funds.', 'mycred' ); ?></label></div>
716
+ </div>
717
+ <?php else : ?>
718
+ <input type="hidden" name="ignore_central"<?php checked( $setup['ignore_central'], 1 ); ?> value="1" />
719
+ <?php endif; ?>
720
+ </div>
721
+ </div>
722
+ <h3><?php _e( 'First Payout', 'mycred' ); ?></h3>
723
+ <div class="row">
724
+ <div class="col-xs-8">
725
+ <div class="row">
726
+ <div class="col-xs-3">
727
+ <div class="form-group">
728
+ <label><?php _e( 'Year', 'mycred' ); ?></label>
729
+ <input type="text" name="last_run[year]" class="form-control cant-be-empty" placeholder="YYYY" value="<?php echo esc_attr( $year ); ?>" />
730
+ </div>
731
+ </div>
732
+ <div class="col-xs-3">
733
+ <div class="form-group">
734
+ <label><?php _e( 'Month', 'mycred' ); ?></label>
735
+ <input type="text" name="last_run[month]" class="form-control cant-be-empty" placeholder="MM" value="<?php echo esc_attr( $month ); ?>" />
736
+ </div>
737
+ </div>
738
+ <div class="col-xs-3">
739
+ <div class="form-group">
740
+ <label><?php _e( 'Date', 'mycred' ); ?></label>
741
+ <input type="text" name="last_run[day]" class="form-control cant-be-empty" placeholder="DD" value="<?php echo esc_attr( $day ); ?>" />
742
+ </div>
743
+ </div>
744
+ <div class="col-xs-3">
745
+ <div class="form-group">
746
+ <label><?php _e( 'Time', 'mycred' ); ?></label>
747
+ <?php echo $this->time_select( 'last_run[time]', 'last-run-time', $time ); ?>
748
+ </div>
749
+ </div>
750
+ </div>
751
+ </div>
752
+ <div class="col-xs-4">
753
+ <div class="form-group">
754
+ <label><?php _e( 'Repeat', 'mycred' ); ?></label>
755
+ <input type="number" name="total_runs" class="form-control" placeholder="0" min="-1" max="256" value="<?php echo esc_attr( $setup['total_runs'] ); ?>" />
756
+ <p><span class="description"><?php _e( 'Use -1 for infinite runs.', 'mycred' ); ?></span></p>
757
+ </div>
758
+ </div>
759
+ </div>
760
+ <h3><?php _e( 'Limits', 'mycred' ); ?></h3>
761
+ <div class="row">
762
+ <div class="col-sm-4 col-xs-12">
763
+ <div class="row">
764
+ <div class="col-sm-12 col-xs-6">
765
+ <div class="form-group">
766
+ <label><?php _e( 'Min. Balance', 'mycred' ); ?></label>
767
+ <input type="number" name="min_balance" class="form-control" placeholder="0" min="0" value="<?php echo esc_attr( $setup['min_balance'] ); ?>" />
768
+ <p><span class="description"><?php _e( 'Use zero to disable.', 'mycred' ); ?></span></p>
769
+ </div>
770
+ </div>
771
+ <div class="col-sm-12 col-xs-6">
772
+ <div class="form-group">
773
+ <label><?php _e( 'Max. Balance', 'mycred' ); ?></label>
774
+ <input type="number" name="max_balance" class="form-control" placeholder="0" min="0" value="<?php echo esc_attr( $setup['max_balance'] ); ?>" />
775
+ <p><span class="description"><?php _e( 'Use zero to disable.', 'mycred' ); ?></span></p>
776
+ </div>
777
+ </div>
778
+ </div>
779
+ </div>
780
+ <div class="col-sm-8 col-xs-12">
781
+ <div class="row">
782
+ <div class="col-sm-5 col-xs-12">
783
+ <div class="form-group">
784
+ <label><?php _e( 'Limit by ID', 'mycred' ); ?></label>
785
+ <?php echo $this->include_exclude_dropdown( 'id_exclude', 'exclude-include-id', $setup['id_exclude'] ); ?>
786
+ </div>
787
+ </div>
788
+ <div class="col-sm-7 col-xs-12">
789
+ <div class="form-group">
790
+ <label class="xs-hidden">&nbsp;</label>
791
+ <input type="text" name="id_list" class="form-control" placeholder="<?php _e( 'Comma separated list of user IDs', 'mycred' ); ?>" value="<?php echo esc_attr( $setup['id_list'] ); ?>" />
792
+ </div>
793
+ </div>
794
+ </div>
795
+ <div class="row">
796
+ <div class="col-sm-5 col-xs-12">
797
+ <div class="form-group">
798
+ <label><?php _e( 'Limit by Role(s)', 'mycred' ); ?></label>
799
+ <?php echo $this->include_exclude_dropdown( 'role_exclude', 'exclude-include-role', $setup['role_exclude'] ); ?>
800
+ </div>
801
+ </div>
802
+ <div class="col-sm-7 col-xs-12">
803
+ <div class="row">
804
+ <?php
805
+
806
+ $editable_roles = array_reverse( get_editable_roles() );
807
+ foreach ( $editable_roles as $role => $details ) {
808
+
809
+ $name = translate_user_role( $details['name'] );
810
+
811
+ echo '<div class="col-sm-6 col-xs-4"><label for="role-list-' . esc_attr( $role ) . '"><input type="checkbox" name="role_list[]" id="role-list-' . esc_attr( $role ) . '" value="' . esc_attr( $role ) . '"';
812
+ if ( in_array( $role, (array) $setup['role_list'] ) ) echo ' checked="checked"';
813
+ echo ' />' . $name . '</label></div>';
814
+ }
815
+
816
+ ?>
817
+ </div>
818
+ </div>
819
+ </div>
820
+ </div>
821
+ </div>
822
+ <div class="row">
823
+ <div class="col-xs-12 tr">
824
+ <input type="hidden" name="new_id" value="<?php echo $schedule_id; ?>" />
825
+ <input type="submit" class="button button-primary" value="<?php _e( 'Schedule', 'mycred' ); ?>" />
826
+ </div>
827
+ </div>
828
+ </div>
829
+ <?php
830
+
831
+ $content = ob_get_contents();
832
+ ob_end_clean();
833
+
834
+ return $content;
835
+
836
+ }
837
+
838
+ /**
839
+ * Include Exclude Dropdown
840
+ * @since 1.7
841
+ * @version 1.0
842
+ */
843
+ public function include_exclude_dropdown( $name = '', $id = '', $selected = '' ) {
844
+
845
+ $options = array(
846
+ 'include' => __( 'Include:', 'mycred' ),
847
+ 'exclude' => __( 'Exclude:', 'mycred' )
848
+ );
849
+
850
+ $content = '<select name="' . $name . '" id="' . $id . '" class="form-control">';
851
+ foreach ( $options as $value => $label ) {
852
+ $content .= '<option value="' . $value . '"';
853
+ if ( $selected == $value ) $content .= ' selected="selected"';
854
+ $content .= '>' . $label . '</option>';
855
+ }
856
+ $content .= '</select>';
857
+
858
+ return $content;
859
+
860
+ }
861
+
862
+ /**
863
+ * Preference for recurring payouts
864
+ * @since 1.2
865
+ * @version 1.2
866
+ */
867
+ public function preferences() {
868
+
869
+ $schedules = mycred_get_recurring_payout_schedules( $this->mycred_type );
870
+
871
+ ?>
872
+ <p><?php _e( 'As long as this service remains disabled, none of your scheduled payouts will run!', 'mycred' ); ?></p>
873
+ <div class="row">
874
+ <div class="col-xs-12">
875
+ <h3><?php _e( 'Schedules', 'mycred' ); ?></h3>
876
+ <table class="widefat fixed striped" cellpadding="0" cellspacing="0">
877
+ <thead>
878
+ <tr>
879
+ <th style="width: 25%;"><?php _e( 'Job Title', 'mycred' ); ?></th>
880
+ <th style="width: 15%;"><?php _e( 'Status', 'mycred' ); ?></th>
881
+ <th style="width: 20%;"><?php _e( 'Frequency', 'mycred' ); ?></th>
882
+ <th style="width: 20%;"><?php _e( 'Last Ran', 'mycred' ); ?></th>
883
+ <th style="width: 20%;"><?php _e( 'Next Run', 'mycred' ); ?></th>
884
+ </tr>
885
+ </thead>
886
+ <tbody id="recurring-schedule-body">
887
+
888
+ <?php echo $this->display_schedule_table(); ?>
889
+
890
+ <tr id="no-banking-schedules"<?php if ( ! empty( $schedules ) ) echo ' style="display: none;"'; ?>>
891
+ <td colspan="5"><?php _e( 'No schedules found.', 'mycred' ); ?></td>
892
+ </tr>
893
+ </tbody>
894
+ </table>
895
+ <input type="hidden" name="<?php echo $this->field_name( 'here' ); ?>" value="1" />
896
+ <p style="text-align: right;"><button type="button" id="add-new-schedule" class="button button-secondary"><?php _e( 'Add New', 'mycred' ); ?></button>
897
+ </div>
898
+ </div>
899
+ <?php
900
+
901
+ do_action( 'mycred_banking_recurring_payouts', $this );
902
+
903
+ }
904
+
905
+ /**
906
+ * Sanitise Preferences
907
+ * @since 1.2
908
+ * @version 1.1
909
+ */
910
+ function sanitise_preferences( $post ) {
911
+
912
+ return apply_filters( 'mycred_banking_save_recurring', array(), $this );
913
+
914
+ }
915
+
916
+ /**
917
+ * Ajax Handler
918
+ * @since 1.7
919
+ * @version 1.0
920
+ */
921
+ function ajax_handler() {
922
+
923
+ parse_str( $_POST['form'], $form );
924
+
925
+ // Add new Schedule
926
+ if ( empty( $form ) ) {
927
+ $new_id = strtolower( wp_generate_password( 6, false, false ) );
928
+ wp_send_json_success( array( 'form' => $this->manage_schedule_form( $new_id, $this->default_schedule ), 'table' => false ) );
929
+ }
930
+
931
+ // Save new Schedule
932
+ if ( array_key_exists( 'new_id', $form ) ) {
933
+
934
+ $setup = shortcode_atts( $this->default_schedule, $form );
935
+
936
+ // Prep start time and convert it into a gmt unix timestamp
937
+ $year = absint( $form['last_run']['year'] );
938
+ $month = zeroise( absint( $form['last_run']['month'] ), 2 );
939
+ $day = zeroise( absint( $form['last_run']['day'] ), 2 );
940
+ $time = sanitize_text_field( $form['last_run']['time'] );
941
+
942
+ $setup['last_run'] = $this->date_to_timestamp( $year . '-' . $month . '-' . $day . ' ' . $time . ':00' );
943
+
944
+ // Attempt to add new payout schedule
945
+ $results = mycred_add_new_recurring_payout( $form['new_id'], $setup, $this->mycred_type );
946
+
947
+ // Something went wrong
948
+ if ( is_wp_error( $results ) ) {
949
+ $content = '<div class="alert alert-warning">' . $results->get_error_message() . '</div>';
950
+ $content .= $this->manage_schedule_form( $form['new_id'], $setup );
951
+ wp_send_json_success( array( 'form' => $content, 'table' => false ) );
952
+ }
953
+
954
+ $results['id'] = $form['new_id'];
955
+ $message = '<div class="alert alert-success">' . __( 'Schedule Added', 'mycred' ) . '</div>';
956
+
957
+ $table_row = $this->display_schedule_table( $results );
958
+ $eligeble_users = $this->get_eligible_users( 5, 0, $setup );
959
+
960
+ // Warn user if no users are eligile for a payout based of their setup
961
+ if ( $eligeble_users['total'] == 0 )
962
+ $message .= '<div class="padded"><p>' . __( 'While the recurring payout has been successfully saved, based on the limits you set, right now there are no users that are eligible for a payout!', 'mycred' ) . '</p></div>';
963
+
964
+ wp_send_json_success( array( 'form' => $message, 'table' => $table_row ) );
965
+
966
+ }
967
+
968
+ // View existing schedule
969
+ elseif ( array_key_exists( 'schedule_id', $form ) ) {
970
+
971
+ $schedule_id = sanitize_key( $form['schedule_id'] );
972
+ $setup = mycred_get_recurring_payout( $schedule_id, $this->mycred_type );
973
+ if ( $setup === false ) {
974
+ $content = '<div class="alert alert-warning">' . __( 'Schedule not found. Please refresh this page and try again.', 'mycred' ) . '</div>';
975
+ wp_send_json_success( array( 'form' => $content, 'table' => false ) );
976
+ }
977
+
978
+ wp_send_json_success( array( 'form' => $this->view_schedule_form( $schedule_id, $setup ), 'table' => false ) );
979
+
980
+ }
981
+
982
+ // Delete existing schedule
983
+ elseif ( array_key_exists( 'remove_token', $form ) ) {
984
+
985
+ $schedule_id = sanitize_key( $form['remove_token'] );
986
+ $setup = mycred_get_recurring_payout( $schedule_id, $this->mycred_type );
987
+ if ( $setup === false ) {
988
+ $content = '<div class="alert alert-warning">' . __( 'Schedule not found. Please refresh this page and try again.', 'mycred' ) . '</div>';
989
+ wp_send_json_success( array( 'form' => $content, 'table' => false ) );
990
+ }
991
+
992
+ mycred_delete_recurring_payout( $schedule_id, $this->mycred_type );
993
+ $content = '<div class="alert alert-success">' . __( 'Schedule Deleted', 'mycred' ) . '</div>';
994
+ $content .= "<script type=\"text/javascript\">jQuery(function($) { $( 'tr#schedule-{$schedule_id}' ).remove(); });</script>";
995
+ wp_send_json_success( array( 'form' => $content, 'table' => false ) );
996
+
997
+ }
998
+
999
+ }
1000
+
1001
+ }
1002
+ endif;
1003
+
1004
+ ?>
addons/buy-creds/abstracts/mycred-abstract-payment-gateway.php CHANGED
@@ -7,27 +7,29 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
7
  * @since 0.1
8
  * @version 1.2.2
9
  */
10
- if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
11
  abstract class myCRED_Payment_Gateway {
12
 
13
  public $id;
14
  public $core;
15
- public $prefs = false;
16
 
17
- public $current_user_id = 0;
18
- public $sandbox_mode = NULL;
19
- public $gateway_logo_url = '';
 
20
 
21
  protected $response;
22
  protected $request;
23
  protected $status;
24
- protected $errors = array();
25
  protected $processing_log = NULL;
26
 
27
  /**
28
  * Construct
29
  */
30
  function __construct( $args = array(), $gateway_prefs = NULL ) {
 
31
  // Make sure gateway prefs is set
32
  if ( $gateway_prefs === NULL ) return;
33
 
@@ -63,7 +65,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
63
  else
64
  $this->mycred_type = $mycred->cred_id;
65
 
66
- if ( $this->mycred_type != 'mycred_default' )
67
  $this->core = new myCRED_Settings( $this->mycred_type );
68
  else
69
  $this->core = $mycred;
@@ -78,6 +80,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
78
  // Decode Log Entries
79
  add_filter( 'mycred_prep_template_tags', array( $this, 'decode_log_entries' ), 10, 2 );
80
  add_filter( 'mycred_parse_log_entry_buy_creds_with_' . $this->id, array( $this, 'log_entry' ), 10, 2 );
 
81
  }
82
 
83
  /**
@@ -85,18 +88,14 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
85
  * @since 0.1
86
  * @version 1.0
87
  */
88
- function process() {
89
- wp_die( 'function myCRED_Payment_Gateway::process() must be over-ridden in a sub-class.' );
90
- }
91
 
92
  /**
93
  * Buy Creds Handler
94
  * @since 0.1
95
  * @version 1.0
96
  */
97
- function buy() {
98
- wp_die( 'function myCRED_Payment_Gateway::buy() must be over-ridden in a sub-class.' );
99
- }
100
 
101
  /**
102
  * Results Handler
@@ -111,7 +110,9 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
111
  * @version 1.0
112
  */
113
  function preferences() {
114
- echo '<p>' . __( 'This Payment Gateway has no settings', 'mycred' ) . '</p>';
 
 
115
  }
116
 
117
  /**
@@ -120,40 +121,45 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
120
  * @version 1.0
121
  */
122
  function sanitise_preferences( $data ) {
 
123
  return $data;
 
124
  }
125
 
126
  /**
127
  * Exchange Rate Setup
128
  * @since 1.5
129
- * @version 1.0.1
130
  */
131
  function exchange_rate_setup( $default = 'USD' ) {
 
132
  if ( ! isset( $this->prefs['exchange'] ) ) return;
133
 
134
- $types = array( 'mycred_default' );
135
  if ( isset( $this->core->buy_creds['types'] ) )
136
  $types = (array) $this->core->buy_creds['types'];
137
 
138
  foreach ( $types as $type ) {
 
139
  $mycred = mycred( $type );
140
-
141
  if ( ! isset( $this->prefs['exchange'][ $type ] ) )
142
  $this->prefs['exchange'][ $type ] = 1;
143
- ?>
144
 
 
145
  <li>
146
  <table>
147
  <tr>
148
  <td style="min-width: 100px;"><div class="h2">1 <?php echo $mycred->singular(); ?></div></td>
149
  <td style="width: 10px;"><div class="h2">=</div></td>
150
- <td><div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'exchange' => $type ) ); ?>" id="<?php echo $this->field_id( array( 'exchange' => $type ) ); ?>" value="<?php echo $this->prefs['exchange'][ $type ]; ?>" size="8" />
151
  <?php if ( isset( $this->prefs['currency'] ) ) : ?><span class="mycred-gateway-<?php echo $this->id; ?>-currency"><?php echo ( $this->prefs['currency'] == '' ) ? __( 'Select currency', 'mycred' ) : $this->prefs['currency']; ?></span><?php else : ?><span><?php echo $default; ?></span><?php endif; ?>
152
  </div></td>
153
  </tr>
154
  </table>
155
  </li>
156
  <?php
 
157
  }
158
 
159
  }
@@ -161,11 +167,12 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
161
  /**
162
  * Add Pending Payment
163
  * @since 1.5
164
- * @version 1.0
165
  */
166
  function add_pending_payment( $data ) {
167
 
168
  $post_id = false;
 
169
 
170
  // Title
171
  if ( isset( $_REQUEST['transaction_id'] ) )
@@ -177,30 +184,41 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
177
  $check = get_page_by_title( $post_title, ARRAY_A, 'buycred_payment' );
178
  if ( $check === NULL || ( isset( $check['post_status'] ) && $check['post_status'] == 'trash' ) ) {
179
 
180
- // Generate new id
181
- if ( isset( $check['post_status'] ) && $check['post_status'] == 'trash' )
 
182
  $post_title = strtoupper( wp_generate_password( 6, false, false ) );
 
183
 
184
  // Insert post
185
  $post_id = wp_insert_post( array(
186
  'post_title' => $post_title,
187
  'post_type' => 'buycred_payment',
188
  'post_status' => 'publish',
189
- 'post_author' => $this->current_user_id,
190
  'ping_status' => 'closed',
191
- 'comment_status' => 'closed'
192
  ) );
193
 
194
  // Add meta details if insertion was a success
195
  if ( $post_id !== NULL && ! is_wp_error( $post_id ) ) {
196
- add_post_meta( $post_id, 'to', $data[0] );
197
- add_post_meta( $post_id, 'from', $data[1] );
198
- add_post_meta( $post_id, 'amount', $data[2] );
199
- add_post_meta( $post_id, 'cost', $data[3] );
200
- add_post_meta( $post_id, 'currency', $data[4] );
201
- add_post_meta( $post_id, 'point_type', $data[5] );
202
- add_post_meta( $post_id, 'gateway', $this->id );
 
 
 
 
 
 
 
 
203
  }
 
204
  }
205
  else $post_id = $check['ID'];
206
 
@@ -211,28 +229,11 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
211
  /**
212
  * Get Pending Payment
213
  * @since 1.5
214
- * @version 1.0.1
215
  */
216
- function get_pending_payment( $post_id = '' ) {
217
 
218
- $post_id = sanitize_key( $post_id );
219
- if ( is_numeric( $post_id ) )
220
- $post = get_post( $post_id );
221
- else
222
- $post = get_page_by_title( $post_id, OBJECT, 'buycred_payment' );
223
-
224
- if ( isset( $post->ID ) && $post->post_type == 'buycred_payment' ) {
225
- $pending_payment = array( 'ID' => $post->ID );
226
- $pending_payment['to'] = (int) get_post_meta( $post->ID, 'to', true );
227
- $pending_payment['from'] = (int) get_post_meta( $post->ID, 'from', true );
228
- $pending_payment['amount'] = get_post_meta( $post->ID, 'amount', true );
229
- $pending_payment['cost'] = get_post_meta( $post->ID, 'cost', true );
230
- $pending_payment['currency'] = get_post_meta( $post->ID, 'currency', true );
231
- $pending_payment['ctype'] = get_post_meta( $post->ID, 'point_type', true );
232
- }
233
- else {
234
- $pending_payment = false;
235
- }
236
 
237
  return apply_filters( 'mycred_get_pending_payment', $pending_payment, $post_id );
238
 
@@ -241,25 +242,14 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
241
  /**
242
  * Log Gateway Call
243
  * @since 1.5
244
- * @version 1.0.1
245
  */
246
- function log_call( $post_id, $log ) {
247
 
248
- if ( is_numeric( $post_id ) )
249
- $post = get_post( $post_id );
250
- else
251
- $post = get_page_by_title( $post_id, OBJECT, 'buycred_payment' );
252
 
253
- if ( isset( $post->ID ) )
254
- return wp_insert_comment( array(
255
- 'comment_post_ID' => $post->ID,
256
- 'comment_author' => $this->label,
257
- 'comment_content' => implode( '<br />', $log ),
258
- 'comment_type' => 'payment_gatway_call',
259
- 'comment_author_IP' => $_SERVER['REMOTE_ADDR'],
260
- 'comment_date' => current_time( 'mysql' ),
261
- 'comment_approved' => 1
262
- ) );
263
 
264
  }
265
 
@@ -269,21 +259,26 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
269
  * @version 1.0
270
  */
271
  function log_entry( $content, $log_entry ) {
272
- $content = $this->core->template_tags_user( $content, $log_entry->ref_id );
273
- return $content;
 
274
  }
275
 
276
  /**
277
  * Get Log Entry
278
  * @since 1.4
279
- * @version 1.0
280
  */
281
  function get_log_entry( $from = 0, $to = 0 ) {
 
282
  $entry = $this->get_entry( $from, $to );
283
- $entry = str_replace( '%gateway%', $this->label, $entry );
 
 
284
  if ( $this->sandbox_mode ) $entry = 'TEST ' . $entry;
285
 
286
  return apply_filters( 'mycred_buycred_get_log_entry', $entry, $from, $to, $this );
 
287
  }
288
 
289
  /**
@@ -293,7 +288,9 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
293
  * @version 1.0
294
  */
295
  function field_name( $field = '' ) {
 
296
  if ( is_array( $field ) ) {
 
297
  $array = array();
298
  foreach ( $field as $parent => $child ) {
299
  if ( ! is_numeric( $parent ) )
@@ -303,11 +300,16 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
303
  $array[] = str_replace( '-', '_', $child );
304
  }
305
  $field = '[' . implode( '][', $array ) . ']';
 
306
  }
307
  else {
 
308
  $field = '[' . $field . ']';
 
309
  }
 
310
  return 'mycred_pref_buycreds[gateway_prefs][' . $this->id . ']' . $field;
 
311
  }
312
 
313
  /**
@@ -317,7 +319,9 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
317
  * @version 1.0
318
  */
319
  function field_id( $field = '' ) {
 
320
  if ( is_array( $field ) ) {
 
321
  $array = array();
322
  foreach ( $field as $parent => $child ) {
323
  if ( ! is_numeric( $parent ) )
@@ -327,20 +331,27 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
327
  $array[] = str_replace( '_', '-', $child );
328
  }
329
  $field = implode( '-', $array );
 
330
  }
331
  else {
 
332
  $field = str_replace( '_', '-', $field );
 
333
  }
 
334
  return 'mycred-gateway-prefs-' . str_replace( '_', '-', $this->id ) . '-' . $field;
 
335
  }
336
 
337
  /**
338
  * Callback URL
339
  * @since 0.1
340
- * @version 1.0
341
  */
342
  function callback_url() {
343
- return get_bloginfo( 'url' ) . '/?mycred_call=' . $this->id;
 
 
344
  }
345
 
346
  /**
@@ -349,9 +360,11 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
349
  * @version 1.0
350
  */
351
  function start_log() {
352
- $this->new_log_entry( __( 'Incoming confirmation call detected', 'mycred' ) );
353
- $this->new_log_entry( sprintf( __( 'Gateway identified itself as "%s"', 'mycred' ), $this->id ) );
354
- $this->new_log_entry( __( 'Verifying caller', 'mycred' ) );
 
 
355
  }
356
 
357
  /**
@@ -360,10 +373,12 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
360
  * @version 1.0
361
  */
362
  function new_log_entry( $entry = '' ) {
 
363
  if ( ! isset( $this->processing_log[ $this->id ] ) )
364
  $this->processing_log[ $this->id ] = array();
365
 
366
  $this->processing_log[ $this->id ][] = $entry;
 
367
  }
368
 
369
  /**
@@ -372,6 +387,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
372
  * @version 1.0
373
  */
374
  function save_log_entry( $id = '', $outcome = '' ) {
 
375
  update_option( 'mycred_buycred_last_call', array(
376
  'gateway' => $this->id,
377
  'date' => time(),
@@ -379,34 +395,41 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
379
  'id' => $id,
380
  'entries' => serialize( $this->processing_log[ $this->id ] )
381
  ) );
 
382
  }
383
 
384
  /**
385
  * Payment Page Header
386
  * @since 0.1
387
- * @version 1.1
388
  */
389
  function get_page_header( $site_title = '', $reload = false ) {
 
390
  // Set Logo
391
  $logo = '';
392
  if ( isset( $this->prefs['logo'] ) && ! empty( $this->prefs['logo'] ) )
393
  $logo = '<img src="' . $this->prefs['logo'] . '" alt="" />';
 
394
  elseif ( isset( $this->prefs['logo_url'] ) && ! empty( $this->prefs['logo_url'] ) )
395
  $logo = '<img src="' . $this->prefs['logo_url'] . '" alt="" />';
 
396
  elseif ( isset( $this->gateway_logo_url ) && ! empty( $this->gateway_logo_url ) )
397
  $logo = '<img src="' . $this->gateway_logo_url . '" alt="" />';
398
-
 
399
  if ( $this->sandbox_mode )
400
  $title = __( 'Test Payment', 'mycred' );
 
401
  elseif ( isset( $this->label ) )
402
  $title = $this->label;
 
403
  else
404
  $title = __( 'Payment', 'mycred' );
405
 
406
  if ( ! isset( $this->transaction_id ) )
407
- $this->transaction_id = ''; ?>
408
 
409
- <!DOCTYPE html>
410
  <html <?php language_attributes(); ?>>
411
  <head>
412
  <meta charset="<?php bloginfo( 'charset' ); ?>" />
@@ -416,14 +439,26 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
416
  <meta name="robots" content="noindex, nofollow" />
417
  <?php if ( $reload ) echo '<meta http-equiv="refresh" content="2;url=' . $reload . '" />'; ?>
418
 
419
- <link rel="stylesheet" href="<?php echo plugins_url( 'assets/css/gateway.css', myCRED_PURCHASE ); ?>" type="text/css" media="all" />
 
 
420
  <?php do_action( 'mycred_buycred_page_header', $title, $reload, $this->id ); ?>
421
 
422
  </head>
423
- <body>
424
- <div id="payment-title"><?php echo $logo; ?><?php echo $title; ?><a href="<?php echo $this->get_cancelled( $this->transaction_id ); ?>" id="return-where-we-came-from"><?php _e( 'Cancel purchase', 'mycred_stripe' ); ?></a></div>
 
 
 
 
 
 
 
 
425
  <?php
 
426
  do_action( 'mycred_buycred_page_top', $title, $reload, $this->id );
 
427
  }
428
  public function purchase_header( $title = '', $reload = false ) {
429
  $this->get_page_header( $title, $reload );
@@ -435,11 +470,14 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
435
  * @version 1.1
436
  */
437
  function get_page_footer() {
438
- do_action( 'mycred_buycred_page_footer', $this->id ); ?>
439
 
 
 
 
440
  </body>
441
  </html>
442
  <?php
 
443
  }
444
  function purchase_footer() {
445
  $this->get_page_footer();
@@ -447,10 +485,12 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
447
 
448
  /**
449
  * Get Billing Address Form
 
450
  * @since 1.4
451
  * @version 1.0
452
  */
453
  function get_billing_address_form( $country_dropdown = false ) {
 
454
  if ( ! is_user_logged_in() ) return;
455
 
456
  $user = wp_get_current_user();
@@ -469,6 +509,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
469
 
470
  // Grab WooCommerce User Fields
471
  if ( ! isset( $_POST['billing']['address1'] ) ) {
 
472
  if ( class_exists( 'WC_Customer' ) ) {
473
  $user_details['first_name'] = get_user_meta( $user->ID, 'billing_first_name', true );
474
  $user_details['last_name'] = get_user_meta( $user->ID, 'billing_last_name', true );
@@ -491,6 +532,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
491
  $user_details['country'] = ( isset( $meta['country'] ) ) ? $meta['country'] : '';
492
  }
493
  }
 
494
  }
495
 
496
  // Let others play
@@ -500,57 +542,88 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
500
  $required_fields = apply_filters( 'mycred_buycred_req_fields', array( 'first_name', 'last_name', 'address1', 'city', 'zip', 'state', 'country' ), $this );
501
 
502
  // Show required and optional fields via placeholders
503
- $required = 'placeholder="' . __( 'required', 'mycred' ) . '"';
504
- $optional = 'placeholder="' . __( 'optional', 'mycred' ) . '"'; ?>
505
 
506
- <div class="gateway-section">
507
- <ul class="input-fields">
508
- <li class="inline">
 
509
  <label for="billing-first-name"><?php _e( 'First Name', 'mycred' ); ?></label>
510
- <input type="text" name="billing[first_name]" id="billing-first-name" value="<?php echo $user_details['first_name']; ?>" class="long<?php if ( array_key_exists( 'first_name', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'first_name', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
511
- </li>
512
- <li class="inline last">
 
 
513
  <label for="billing-last-name"><?php _e( 'Last Name', 'mycred' ); ?></label>
514
- <input type="text" name="billing[last_name]" id="billing-last-name" value="<?php echo $user_details['last_name']; ?>" class="long<?php if ( array_key_exists( 'last_name', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'last_name', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
515
- </li>
516
- <li class="inline">
 
 
 
 
517
  <label for="billing-address1"><?php _e( 'Address Line 1', 'mycred' ); ?></label>
518
- <input type="text" name="billing[address1]" id="billing-address1" value="<?php echo $user_details['address1']; ?>" class="long<?php if ( array_key_exists( 'address1', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'address1', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
519
- </li>
520
- <li class="inline">
 
 
 
 
521
  <label for="billing-address2"><?php _e( 'Address Line 2', 'mycred' ); ?></label>
522
- <input type="text" name="billing[address2]" id="billing-address2" value="<?php echo $user_details['address2']; ?>" class="long<?php if ( array_key_exists( 'address2', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'address2', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
523
- </li>
524
- <li class="inline">
 
 
 
 
525
  <label for="billing-city"><?php _e( 'City', 'mycred' ); ?></label>
526
- <input type="text" name="billing[city]" id="billing-city" value="<?php echo $user_details['city']; ?>" class="medium<?php if ( array_key_exists( 'city', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'city', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
527
- </li>
528
- <li class="inline">
 
 
529
  <label for="billing-zip"><?php _e( 'Zip', 'mycred' ); ?></label>
530
- <input type="text" name="billing[zip]" id="billing-zip" value="<?php echo $user_details['postcode']; ?>" class="short<?php if ( array_key_exists( 'zip', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'zip', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
531
- </li>
532
- <li class="inline">
 
 
 
 
533
  <label for="billing-state"><?php _e( 'State', 'mycred' ); ?></label>
534
- <input type="text" name="billing[state]" id="billing-state" value="<?php echo $user_details['state']; ?>" class="short<?php if ( array_key_exists( 'state', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'state', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
535
- </li>
536
- <li class="inline last">
 
 
 
 
537
  <label for="billing-country"><?php _e( 'Country', 'mycred' ); ?></label>
 
538
  <?php if ( $country_dropdown !== false ) : ?>
539
- <select name="billing[country]" id="billing-country">
 
540
  <option value=""><?php _e( 'Choose Country', 'mycred' ); ?></option>
 
541
  <?php $this->list_option_countries(); ?>
 
542
  </select>
 
543
  <?php else : ?>
544
- <input type="text" name="billing[country]" id="billing-country" value="<?php echo $user_details['country']; ?>" class="medium<?php if ( array_key_exists( 'country', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'country', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
545
 
546
- <?php endif; ?>
547
-
548
- </li>
549
- </ul>
550
- <?php do_action( 'mycred_buycred_after_billing_details', $user_details, $this ); ?>
551
 
 
 
 
552
  </div>
553
  <?php
 
 
 
554
  }
555
 
556
  /**
@@ -584,12 +657,14 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
584
  * @version 1.0
585
  */
586
  function get_order( $amount, $cost ) {
587
- $order_name = apply_filters( 'mycred_buycred_order_name', sprintf( __( '%s Purchase', 'mycred' ), $this->core->singular() ), $amount, $cost, $this ) ?>
588
 
 
 
 
589
  <table cellpadding="0" cellspacing="0">
590
  <thead>
591
  <tr>
592
- <th id="gateway-order-item" class="order-item"><?php _e( 'Item', 'mycred' ); ?></th>
593
  <th id="gateway-order-amount" class="order-amount"><?php _e( 'Amount', 'mycred' ); ?></th>
594
  <th id="gateway-order-cost" class="order-cost"><?php _e( 'Cost', 'mycred' ); ?></th>
595
  </tr>
@@ -603,6 +678,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
603
  </tbody>
604
  </table>
605
  <?php
 
606
  }
607
 
608
  /**
@@ -610,33 +686,35 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
610
  * @since 1.0
611
  * @version 1.0
612
  */
613
- function get_debug() { ?>
614
 
615
- <h2><?php _e( 'Debug', 'mycred' ); ?></h2>
616
- <p><span class="description"><?php _e( 'Here you can see information that are collected and sent to this gateway. Debug information is only visible for administrators and are intended for troubleshooting / testing of this gateway. Please disable "Sandbox Mode" when you want to take this gateway online.', 'mycred' ); ?></span></p>
 
617
  <table id="gateway-debug">
618
  <thead>
619
  <tr>
620
- <th id="gateway-col-section" class="col-section"><?php _e( 'Section', 'mycred' ); ?></th>
621
- <th id="gateway-col-result" class="col-result"><?php _e( 'Result', 'mycred' ); ?></th>
622
  </tr>
623
  </thead>
624
  <tbody>
625
  <tr>
626
- <td class="col-section"><?php _e( 'Payment Status', 'mycred' ); ?></td>
627
  <td class="col-result"><pre><?php print_r( $this->status ); ?></pre></td>
628
  </tr>
629
  <tr>
630
- <td class="col-section"><?php _e( 'Request', 'mycred' ); ?></td>
631
  <td class="col-result"><pre><?php print_r( $this->request ); ?></pre></td>
632
  </tr>
633
  <tr>
634
- <td class="col-section"><?php _e( 'Gateway Response', 'mycred' ); ?></td>
635
  <td class="col-result"><pre><?php print_r( $this->response ); ?></pre></td>
636
  </tr>
637
  </tbody>
638
  </table>
639
  <?php
 
640
  }
641
 
642
  /**
@@ -645,14 +723,17 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
645
  * @version 1.0
646
  */
647
  function get_errors() {
 
648
  if ( empty( $this->errors ) ) return;
649
 
650
  $errors = array();
651
  foreach ( $this->errors as $form_field => $error_message )
652
- $errors[] = $error_message; ?>
653
 
 
654
  <div class="gateway-error"><?php echo implode( '<br />', $errors ); ?></div>
655
  <?php
 
656
  }
657
 
658
  /**
@@ -662,6 +743,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
662
  * @version 1.0
663
  */
664
  function get_page_redirect( $hidden_fields = array(), $location = '' ) {
 
665
  $id = str_replace( '-', '_', $this->id );
666
 
667
  // Logo
@@ -669,31 +751,22 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
669
  $logo_url = plugins_url( 'images/cred-icon32.png', myCRED_THIS );
670
 
671
  // Hidden Fields
672
- $hidden_fields = apply_filters( "mycred_{$id}_purchase_fields", $hidden_fields, $this ); ?>
673
-
674
- <form name="mycred_<?php echo $id; ?>_request" action="<?php echo $location; ?>" method="post" id="redirect-form">
675
- <?php
676
- // Required hidden form fields
677
- foreach ( $hidden_fields as $name => $value )
678
- echo "\t" . '<input type="hidden" name="' . $name . '" value="' . $value . '" />' . "\n"; ?>
679
-
680
- <img src="<?php echo plugins_url( 'assets/images/loading.gif', myCRED_PURCHASE ); ?>" alt="Loading" />
681
- <noscript><input type="submit" name="submit-form" value="<?php printf( __( 'Continue to %s', 'mycred' ), $this->label ); ?>" /></noscript>
682
- <p id="manual-continue"><a href="javascript:void(0);" onclick="document.mycred_<?php echo str_replace( '-', '_', $this->id ); ?>_request.submit();return false;"><?php _e( 'Click here if you are not automatically redirected', 'mycred' ); ?></a></p>
683
- </form>
684
- <?php if ( $this->sandbox_mode ) : ?>
685
-
686
- <div class="gateway-section">
687
- <pre><?php _e( 'The following information will be sent to the gateway', 'mycred' ); ?>:
688
 
689
- <?php print_r( $hidden_fields ); ?></pre>
 
 
 
 
 
 
 
 
 
690
  </div>
691
- <?php endif; ?>
692
-
693
- <script type="text/javascript">
694
- <?php if ( $this->sandbox_mode ) echo '//'; ?>setTimeout( "document.mycred_<?php echo $id; ?>_request.submit()",2000 );
695
- </script>
696
  <?php
 
697
  }
698
  function form_with_redirect( $hidden_fields = array(), $location = '', $logo_url = '', $custom_html = '', $sales_data = '' ) {
699
  $this->get_page_redirect( $hidden_fields, $location, $custom_html, $sales_data );
@@ -707,6 +780,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
707
  * @version 1.0
708
  */
709
  function get_to() {
 
710
  // Gift to a user
711
  if ( $this->core->buy_creds['gifting']['members'] == 1 ) {
712
  if ( isset( $_POST['gift_to'] ) ) {
@@ -731,6 +805,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
731
  }
732
 
733
  return $this->current_user_id;
 
734
  }
735
 
736
  /**
@@ -758,26 +833,9 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
758
  * @since 0.1
759
  * @version 1.3.1
760
  */
761
- function get_cancelled( $cancel_token = '' ) {
762
-
763
- $base = get_bloginfo( 'url' );
764
- if ( $this->core->buy_creds['cancelled']['use'] == 'page' ) {
765
- if ( ! empty( $this->core->buy_creds['cancelled']['page'] ) )
766
- $base = get_permalink( $this->core->buy_creds['cancelled']['page'] );
767
- }
768
- else {
769
- $base = get_bloginfo( 'url' ) . '/' . $this->core->buy_creds['cancelled']['custom'];
770
- }
771
-
772
- if ( isset( $_REQUEST['return_to'] ) )
773
- $base = sanitize_text_field( $_REQUEST['return_to'] );
774
-
775
- if ( $cancel_token != '' )
776
- $url = add_query_arg( array( 'buycred_cancel' => $cancel_token ), $base );
777
- else
778
- $url = $base;
779
 
780
- return apply_filters( 'mycred_buycred_cancel_url', $url, $cancel_token, $this );
781
 
782
  }
783
 
@@ -788,6 +846,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
788
  * @version 1.0
789
  */
790
  function get_entry( $_to, $_from ) {
 
791
  // Log entry
792
  if ( $_to == $_from ) return $this->core->buy_creds['log'];
793
 
@@ -795,6 +854,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
795
  return $this->core->buy_creds['gifting']['log'];
796
 
797
  return $this->core->buy_creds['log'];
 
798
  }
799
 
800
  /**
@@ -803,6 +863,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
803
  * @version 1.2
804
  */
805
  function POST_to_data( $unset = false ) {
 
806
  $data = array();
807
  foreach ( $_POST as $key => $value ) {
808
  $data[ $key ] = stripslashes( $value );
@@ -811,6 +872,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
811
  unset( $_POST );
812
 
813
  return $data;
 
814
  }
815
 
816
  /**
@@ -822,6 +884,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
822
  * @version 1.0.1
823
  */
824
  function transaction_id_is_unique( $transaction_id = '' ) {
 
825
  if ( empty( $transaction_id ) ) return false;
826
 
827
  global $wpdb;
@@ -841,6 +904,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
841
  if ( $wpdb->num_rows > 0 ) return false;
842
 
843
  return true;
 
844
  }
845
 
846
  /**
@@ -850,6 +914,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
850
  * @version 1.0
851
  */
852
  function create_unique_transaction_id() {
 
853
  global $wpdb;
854
 
855
  do {
@@ -864,6 +929,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
864
  } while ( ! empty( $query ) );
865
 
866
  return $id;
 
867
  }
868
 
869
  /**
@@ -873,24 +939,28 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
873
  * @version 1.0
874
  */
875
  function create_token( $user_id = NULL ) {
 
876
  return wp_create_nonce( 'mycred-buy-' . $this->id );
 
877
  }
878
 
879
  /**
880
  * Get Point Type
881
  * @since 1.5
882
- * @version 1.0.1
883
  */
884
  function get_point_type() {
 
885
  $type = '';
886
-
887
  if ( isset( $_REQUEST['ctype'] ) )
888
- $type = sanitize_text_field( $_REQUEST['ctype'] );
889
 
890
- if ( $type == '' )
891
- $type = 'mycred_default';
892
 
893
  return $type;
 
894
  }
895
 
896
  /**
@@ -906,9 +976,9 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
906
  * @version 1.0.1
907
  */
908
  function verify_token( $user_id, $nonce ) {
909
- $uid = absint( $user_id );
910
 
911
- $i = wp_nonce_tick();
 
912
 
913
  if ( substr( wp_hash( $i . 'mycred-buy-' . $this->id . $uid, 'nonce' ), -12, 10 ) == $nonce )
914
  return true;
@@ -916,6 +986,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
916
  return true;
917
 
918
  return false;
 
919
  }
920
 
921
  /**
@@ -924,11 +995,13 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
924
  * @version 1.1
925
  */
926
  function encode_sales_data( $data ) {
 
927
  $protect = new myCRED_Protect();
928
  if ( $protect !== false )
929
  return $protect->do_encode( $data );
930
- else
931
- return $data;
 
932
  }
933
 
934
  /**
@@ -937,11 +1010,13 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
937
  * @version 1.1
938
  */
939
  function decode_sales_data( $data ) {
 
940
  $protect = new myCRED_Protect();
941
  if ( $protect !== false )
942
  return $protect->do_decode( $data );
943
- else
944
- return $data;
 
945
  }
946
 
947
  /**
@@ -949,13 +1024,15 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
949
  * @since 1.3.2
950
  * @version 1.1
951
  */
952
- function get_cost( $amount = 0, $type = 'mycred_default', $raw = false ) {
 
953
  // Apply minimum
954
  if ( $amount < $this->core->buy_creds['minimum'] )
955
  $amount = $this->core->buy_creds['minimum'];
956
 
957
  // Calculate cost here so we can use any exchange rate
958
- if ( isset( $this->prefs['exchange'][ $type ] ) ) {
 
959
  // Check for user override
960
  $override = mycred_get_user_meta( $this->current_user_id, 'mycred_buycred_rates_' . $type, '', true );
961
  if ( isset( $override[ $this->id ] ) && $override[ $this->id ] != '' )
@@ -968,8 +1045,8 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
968
  else
969
  $rate = (int) $rate;
970
 
971
- $mycred = mycred( $type );
972
- $cost = $mycred->number( $amount ) * $rate;
973
  }
974
  else
975
  $cost = $amount;
@@ -979,6 +1056,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
979
  $cost = number_format( $cost, 2, '.', '' );
980
 
981
  return apply_filters( 'mycred_buycred_get_cost', $cost, $amount, $type, $this->prefs, $this->core->buy_creds );
 
982
  }
983
 
984
  /**
@@ -987,6 +1065,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
987
  * @version 1.0.2
988
  */
989
  function currencies_dropdown( $name = '', $js = '' ) {
 
990
  $currencies = array(
991
  'USD' => 'US Dollars',
992
  'AUD' => 'Australian Dollars',
@@ -1027,6 +1106,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1027
  echo '>' . $cname . '</option>';
1028
  }
1029
  echo '</select>';
 
1030
  }
1031
 
1032
  /**
@@ -1035,6 +1115,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1035
  * @version 1.0
1036
  */
1037
  function item_types_dropdown( $name = '' ) {
 
1038
  $types = array(
1039
  'product' => 'Product',
1040
  'service' => 'Service',
@@ -1050,6 +1131,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1050
  echo '>' . $cname . '</option>';
1051
  }
1052
  echo '</select>';
 
1053
  }
1054
 
1055
  /**
@@ -1058,6 +1140,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1058
  * @version 1.0
1059
  */
1060
  function list_option_countries( $selected = '' ) {
 
1061
  $countries = array (
1062
  "US" => "UNITED STATES",
1063
  "AF" => "AFGHANISTAN",
@@ -1301,12 +1384,13 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1301
  "ZW" => "ZIMBABWE"
1302
  );
1303
  $countries = apply_filters( 'mycred_list_option_countries', $countries );
1304
-
1305
  foreach ( $countries as $code => $cname ) {
1306
  echo '<option value="' . $code . '"';
1307
  if ( $selected == $code ) echo ' selected="selected"';
1308
  echo '>' . $cname . '</option>';
1309
  }
 
1310
  }
1311
 
1312
  /**
@@ -1315,6 +1399,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1315
  * @version 1.0
1316
  */
1317
  function list_option_us_states( $selected = '', $non_us = false ) {
 
1318
  $states = array (
1319
  "AL" => "Alabama",
1320
  "AK" => "Alaska",
@@ -1370,7 +1455,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1370
  );
1371
  $states = apply_filters( 'mycred_list_option_us', $states );
1372
 
1373
- $outside = __( 'Outside US', 'mycred' );
1374
  if ( $non_us == 'top' ) echo '<option value="">' . $outside . '</option>';
1375
  foreach ( $states as $code => $cname ) {
1376
  echo '<option value="' . $code . '"';
@@ -1378,6 +1463,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1378
  echo '>' . $cname . '</option>';
1379
  }
1380
  if ( $non_us == 'bottom' ) echo '<option value="">' . $outside . '</option>';
 
1381
  }
1382
 
1383
  /**
@@ -1386,6 +1472,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1386
  * @version 1.0
1387
  */
1388
  function list_option_months( $selected = '' ) {
 
1389
  $months = array (
1390
  "01" => __( 'January', 'mycred' ),
1391
  "02" => __( 'February', 'mycred' ),
@@ -1406,6 +1493,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1406
  if ( $selected == $number ) echo ' selected="selected"';
1407
  echo '>' . $text . '</option>';
1408
  }
 
1409
  }
1410
 
1411
  /**
@@ -1414,9 +1502,11 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1414
  * @version 1.0
1415
  */
1416
  function list_option_card_years( $selected = '', $number = 16 ) {
1417
- $yy = date_i18n( 'y' );
1418
- $yyyy = date_i18n( 'Y' );
1419
- $count = 0;
 
 
1420
  $options = array();
1421
 
1422
  while ( $count <= (int) $number ) {
@@ -1433,6 +1523,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1433
  if ( $selected == $key ) echo ' selected="selected"';
1434
  echo '>' . $value . '</option>';
1435
  }
 
1436
  }
1437
 
1438
  /**
@@ -1441,6 +1532,7 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1441
  * @version 1.0
1442
  */
1443
  function IPN_has_required_fields( $required_fields = array(), $method = 'REQUEST' ) {
 
1444
  $missing = 0;
1445
  foreach ( $required_fields as $field_key ) {
1446
  if ( $method == 'POST' ) {
@@ -1460,15 +1552,16 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1460
  $missing ++;
1461
  }
1462
  }
1463
-
1464
  if ( $missing > 0 )
1465
  $result = false;
1466
  else
1467
  $result = true;
1468
-
1469
  $result = apply_filters( 'mycred_buycred_IPN_missing', $result, $required_fields, $this->id );
1470
-
1471
  return $result;
 
1472
  }
1473
 
1474
  /**
@@ -1477,7 +1570,9 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1477
  * @version 1.0
1478
  */
1479
  function IPN_is_valid_call() {
 
1480
  return false;
 
1481
  }
1482
 
1483
  /**
@@ -1498,14 +1593,14 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1498
  if ( $pending_payment === false ) return false;
1499
 
1500
  $result = true;
1501
-
1502
  if ( $method == 'POST' )
1503
  $price = $_POST[ $cost_key ];
1504
  elseif ( $method == 'GET' )
1505
  $price = $_GET[ $cost_key ];
1506
  else
1507
  $price = $_REQUEST[ $cost_key ];
1508
-
1509
  if ( $result === true && $pending_payment['cost'] != $price ) {
1510
  $result = false;
1511
  }
@@ -1524,13 +1619,14 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1524
  if ( $result === true && ! $this->transaction_id_is_unique( $transaction_id ) ) {
1525
  $result = false;
1526
  }
1527
-
1528
  $result = apply_filters( 'mycred_buycred_valid_sale', $result, $sales_data_key, $cost_key, $transactionid_key, $method, $this );
1529
-
1530
  if ( $result === true )
1531
  return $decoded_data;
1532
 
1533
  return $result;
 
1534
  }
1535
 
1536
  /**
@@ -1538,38 +1634,42 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1538
  * @since 1.4
1539
  * @version 1.4
1540
  */
1541
- function complete_payment( $pending_payment = array(), $transaction_id = '' ) {
1542
- $reference = 'buy_creds_with_' . str_replace( array( ' ', '-' ), '_', $this->id );
 
 
 
 
 
 
1543
  $sales_data = array(
1544
- $pending_payment['to'],
1545
- $pending_payment['from'],
1546
- $pending_payment['amount'],
1547
- $pending_payment['cost'],
1548
- $pending_payment['currency'],
1549
- $pending_payment['ctype']
1550
  );
 
1551
 
1552
- $point_type = $pending_payment['ctype'];
1553
- $mycred = mycred( $point_type );
1554
- $data = array( 'ref_type' => 'user', 'txn_id' => $transaction_id, 'sales_data' => implode( '|', $sales_data ) );
1555
-
1556
- $reply = false;
1557
 
1558
- if ( ! $mycred->has_entry( $reference, $pending_payment['from'], $pending_payment['to'], $data, $pending_payment['ctype'] ) ) {
1559
  add_filter( 'mycred_get_email_events', array( $this, 'email_notice' ), 10, 2 );
1560
  $reply = $mycred->add_creds(
1561
  $reference,
1562
- $pending_payment['to'],
1563
- $pending_payment['amount'],
1564
- $this->get_log_entry( $pending_payment['to'], $pending_payment['from'] ),
1565
- $pending_payment['from'],
1566
  $data,
1567
- $pending_payment['ctype']
1568
  );
1569
  remove_filter( 'mycred_get_email_events', array( $this, 'email_notice' ), 10, 2 );
 
1570
  }
1571
 
1572
  return apply_filters( 'mycred_buycred_complete_payment', $reply, $transaction_id, $this );
 
1573
  }
1574
 
1575
  /**
@@ -1602,6 +1702,8 @@ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) {
1602
  wp_trash_post( $post->ID );
1603
 
1604
  }
 
1605
  }
1606
- }
 
1607
  ?>
7
  * @since 0.1
8
  * @version 1.2.2
9
  */
10
+ if ( ! class_exists( 'myCRED_Payment_Gateway' ) ) :
11
  abstract class myCRED_Payment_Gateway {
12
 
13
  public $id;
14
  public $core;
15
+ public $prefs = false;
16
 
17
+ public $current_user_id = 0;
18
+ public $sandbox_mode = NULL;
19
+ public $gateway_logo_url = '';
20
+ public $label = '';
21
 
22
  protected $response;
23
  protected $request;
24
  protected $status;
25
+ protected $errors = array();
26
  protected $processing_log = NULL;
27
 
28
  /**
29
  * Construct
30
  */
31
  function __construct( $args = array(), $gateway_prefs = NULL ) {
32
+
33
  // Make sure gateway prefs is set
34
  if ( $gateway_prefs === NULL ) return;
35
 
65
  else
66
  $this->mycred_type = $mycred->cred_id;
67
 
68
+ if ( $this->mycred_type != MYCRED_DEFAULT_TYPE_KEY )
69
  $this->core = new myCRED_Settings( $this->mycred_type );
70
  else
71
  $this->core = $mycred;
80
  // Decode Log Entries
81
  add_filter( 'mycred_prep_template_tags', array( $this, 'decode_log_entries' ), 10, 2 );
82
  add_filter( 'mycred_parse_log_entry_buy_creds_with_' . $this->id, array( $this, 'log_entry' ), 10, 2 );
83
+
84
  }
85
 
86
  /**
88
  * @since 0.1
89
  * @version 1.0
90
  */
91
+ function process() { }
 
 
92
 
93
  /**
94
  * Buy Creds Handler
95
  * @since 0.1
96
  * @version 1.0
97
  */
98
+ function buy() { }
 
 
99
 
100
  /**
101
  * Results Handler
110
  * @version 1.0
111
  */
112
  function preferences() {
113
+
114
+ echo '<p>This Payment Gateway has no settings</p>';
115
+
116
  }
117
 
118
  /**
121
  * @version 1.0
122
  */
123
  function sanitise_preferences( $data ) {
124
+
125
  return $data;
126
+
127
  }
128
 
129
  /**
130
  * Exchange Rate Setup
131
  * @since 1.5
132
+ * @version 1.0.2
133
  */
134
  function exchange_rate_setup( $default = 'USD' ) {
135
+
136
  if ( ! isset( $this->prefs['exchange'] ) ) return;
137
 
138
+ $types = array( MYCRED_DEFAULT_TYPE_KEY );
139
  if ( isset( $this->core->buy_creds['types'] ) )
140
  $types = (array) $this->core->buy_creds['types'];
141
 
142
  foreach ( $types as $type ) {
143
+
144
  $mycred = mycred( $type );
145
+
146
  if ( ! isset( $this->prefs['exchange'][ $type ] ) )
147
  $this->prefs['exchange'][ $type ] = 1;
 
148
 
149
+ ?>
150
  <li>
151
  <table>
152
  <tr>
153
  <td style="min-width: 100px;"><div class="h2">1 <?php echo $mycred->singular(); ?></div></td>
154
  <td style="width: 10px;"><div class="h2">=</div></td>
155
+ <td><div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'exchange' => $type ) ); ?>" id="<?php echo $this->field_id( array( 'exchange' => $type ) ); ?>" value="<?php echo esc_attr( $this->prefs['exchange'][ $type ] ); ?>" size="8" />
156
  <?php if ( isset( $this->prefs['currency'] ) ) : ?><span class="mycred-gateway-<?php echo $this->id; ?>-currency"><?php echo ( $this->prefs['currency'] == '' ) ? __( 'Select currency', 'mycred' ) : $this->prefs['currency']; ?></span><?php else : ?><span><?php echo $default; ?></span><?php endif; ?>
157
  </div></td>
158
  </tr>
159
  </table>
160
  </li>
161
  <?php
162
+
163
  }
164
 
165
  }
167
  /**
168
  * Add Pending Payment
169
  * @since 1.5
170
+ * @version 1.1
171
  */
172
  function add_pending_payment( $data ) {
173
 
174
  $post_id = false;
175
+ list ( $to, $from, $amount, $cost, $currency, $point_type ) = $data;
176
 
177
  // Title
178
  if ( isset( $_REQUEST['transaction_id'] ) )
184
  $check = get_page_by_title( $post_title, ARRAY_A, 'buycred_payment' );
185
  if ( $check === NULL || ( isset( $check['post_status'] ) && $check['post_status'] == 'trash' ) ) {
186
 
187
+ // Generate new id and trash old request
188
+ if ( isset( $check['post_status'] ) && $check['post_status'] == 'trash' ) {
189
+ buycred_trash_pending_payment( $check['ID'] );
190
  $post_title = strtoupper( wp_generate_password( 6, false, false ) );
191
+ }
192
 
193
  // Insert post
194
  $post_id = wp_insert_post( array(
195
  'post_title' => $post_title,
196
  'post_type' => 'buycred_payment',
197
  'post_status' => 'publish',
198
+ 'post_author' => $from,
199
  'ping_status' => 'closed',
200
+ 'comment_status' => 'open'
201
  ) );
202
 
203
  // Add meta details if insertion was a success
204
  if ( $post_id !== NULL && ! is_wp_error( $post_id ) ) {
205
+
206
+ add_post_meta( $post_id, 'to', $to, true );
207
+ add_post_meta( $post_id, 'from', $from, true );
208
+ add_post_meta( $post_id, 'amount', $amount, true );
209
+ add_post_meta( $post_id, 'cost', $cost, true );
210
+ add_post_meta( $post_id, 'currency', $currency, true );
211
+ add_post_meta( $post_id, 'point_type', $point_type, true);
212
+ add_post_meta( $post_id, 'gateway', $this->id, true );
213
+
214
+ delete_user_meta( $from, 'buycred_pending_payments' );
215
+
216
+ $mycred = mycred( $point_type );
217
+
218
+ $this->log_call( $post_id, sprintf( _x( 'Received new request to purchase %s.', '%s is replaced with the point amount and name.', 'mycred' ), $mycred->format_number( $amount ) . ' ' . $mycred->plural() ) );
219
+
220
  }
221
+
222
  }
223
  else $post_id = $check['ID'];
224
 
229
  /**
230
  * Get Pending Payment
231
  * @since 1.5
232
+ * @version 1.1
233
  */
234
+ function get_pending_payment( $post_id = NULL ) {
235
 
236
+ $pending_payment = buycred_get_pending_payment( $post_id );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
 
238
  return apply_filters( 'mycred_get_pending_payment', $pending_payment, $post_id );
239
 
242
  /**
243
  * Log Gateway Call
244
  * @since 1.5
245
+ * @version 1.2
246
  */
247
+ function log_call( $payment_id, $log ) {
248
 
249
+ if ( is_array( $log ) )
250
+ $log = implode( '<br />', $log );
 
 
251
 
252
+ buycred_add_pending_comment( $payment_id, $log );
 
 
 
 
 
 
 
 
 
253
 
254
  }
255
 
259
  * @version 1.0
260
  */
261
  function log_entry( $content, $log_entry ) {
262
+
263
+ return $this->core->template_tags_user( $content, $log_entry->ref_id );
264
+
265
  }
266
 
267
  /**
268
  * Get Log Entry
269
  * @since 1.4
270
+ * @version 1.0.1
271
  */
272
  function get_log_entry( $from = 0, $to = 0 ) {
273
+
274
  $entry = $this->get_entry( $from, $to );
275
+ if ( isset( $this->label ) )
276
+ $entry = str_replace( '%gateway%', $this->label, $entry );
277
+
278
  if ( $this->sandbox_mode ) $entry = 'TEST ' . $entry;
279
 
280
  return apply_filters( 'mycred_buycred_get_log_entry', $entry, $from, $to, $this );
281
+
282
  }
283
 
284
  /**
288
  * @version 1.0
289
  */
290
  function field_name( $field = '' ) {
291
+
292
  if ( is_array( $field ) ) {
293
+
294
  $array = array();
295
  foreach ( $field as $parent => $child ) {
296
  if ( ! is_numeric( $parent ) )
300
  $array[] = str_replace( '-', '_', $child );
301
  }
302
  $field = '[' . implode( '][', $array ) . ']';
303
+
304
  }
305
  else {
306
+
307
  $field = '[' . $field . ']';
308
+
309
  }
310
+
311
  return 'mycred_pref_buycreds[gateway_prefs][' . $this->id . ']' . $field;
312
+
313
  }
314
 
315
  /**
319
  * @version 1.0
320
  */
321
  function field_id( $field = '' ) {
322
+
323
  if ( is_array( $field ) ) {
324
+
325
  $array = array();
326
  foreach ( $field as $parent => $child ) {
327
  if ( ! is_numeric( $parent ) )
331
  $array[] = str_replace( '_', '-', $child );
332
  }
333
  $field = implode( '-', $array );
334
+
335
  }
336
  else {
337
+
338
  $field = str_replace( '_', '-', $field );
339
+
340
  }
341
+
342
  return 'mycred-gateway-prefs-' . str_replace( '_', '-', $this->id ) . '-' . $field;
343
+
344
  }
345
 
346
  /**
347
  * Callback URL
348
  * @since 0.1
349
+ * @version 1.1
350
  */
351
  function callback_url() {
352
+
353
+ return add_query_arg( 'mycred_call', $this->id, home_url( '/' ) );
354
+
355
  }
356
 
357
  /**
360
  * @version 1.0
361
  */
362
  function start_log() {
363
+
364
+ $this->new_log_entry( 'Incoming confirmation call detected' );
365
+ $this->new_log_entry( sprintf( 'Gateway identified itself as "%s"', $this->id ) );
366
+ $this->new_log_entry( 'Verifying caller' );
367
+
368
  }
369
 
370
  /**
373
  * @version 1.0
374
  */
375
  function new_log_entry( $entry = '' ) {
376
+
377
  if ( ! isset( $this->processing_log[ $this->id ] ) )
378
  $this->processing_log[ $this->id ] = array();
379
 
380
  $this->processing_log[ $this->id ][] = $entry;
381
+
382
  }
383
 
384
  /**
387
  * @version 1.0
388
  */
389
  function save_log_entry( $id = '', $outcome = '' ) {
390
+
391
  update_option( 'mycred_buycred_last_call', array(
392
  'gateway' => $this->id,
393
  'date' => time(),
395
  'id' => $id,
396
  'entries' => serialize( $this->processing_log[ $this->id ] )
397
  ) );
398
+
399
  }
400
 
401
  /**
402
  * Payment Page Header
403
  * @since 0.1
404
+ * @version 1.2
405
  */
406
  function get_page_header( $site_title = '', $reload = false ) {
407
+
408
  // Set Logo
409
  $logo = '';
410
  if ( isset( $this->prefs['logo'] ) && ! empty( $this->prefs['logo'] ) )
411
  $logo = '<img src="' . $this->prefs['logo'] . '" alt="" />';
412
+
413
  elseif ( isset( $this->prefs['logo_url'] ) && ! empty( $this->prefs['logo_url'] ) )
414
  $logo = '<img src="' . $this->prefs['logo_url'] . '" alt="" />';
415
+
416
  elseif ( isset( $this->gateway_logo_url ) && ! empty( $this->gateway_logo_url ) )
417
  $logo = '<img src="' . $this->gateway_logo_url . '" alt="" />';
418
+
419
+ // Set Title
420
  if ( $this->sandbox_mode )
421
  $title = __( 'Test Payment', 'mycred' );
422
+
423
  elseif ( isset( $this->label ) )
424
  $title = $this->label;
425
+
426
  else
427
  $title = __( 'Payment', 'mycred' );
428
 
429
  if ( ! isset( $this->transaction_id ) )
430
+ $this->transaction_id = '';
431
 
432
+ ?><!DOCTYPE html>
433
  <html <?php language_attributes(); ?>>
434
  <head>
435
  <meta charset="<?php bloginfo( 'charset' ); ?>" />
439
  <meta name="robots" content="noindex, nofollow" />
440
  <?php if ( $reload ) echo '<meta http-equiv="refresh" content="2;url=' . $reload . '" />'; ?>
441
 
442
+ <link rel="stylesheet" href="<?php echo plugins_url( 'assets/css/gateway.css', MYCRED_PURCHASE ); ?>" type="text/css" media="all" />
443
+ <link rel="stylesheet" href="<?php echo plugins_url( 'assets/css/bootstrap-grid.css', myCRED_THIS ); ?>" type="text/css" media="all" />
444
+ <link rel="stylesheet" href="<?php echo plugins_url( 'assets/css/mycred-forms.css', myCRED_THIS ); ?>" type="text/css" media="all" />
445
  <?php do_action( 'mycred_buycred_page_header', $title, $reload, $this->id ); ?>
446
 
447
  </head>
448
+ <body class="mycred-metabox">
449
+ <div class="row">
450
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
451
+ <?php echo $logo; ?>
452
+ </div>
453
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 text-right">
454
+ <h2><?php echo $title; ?></h2>
455
+ <p><a href="<?php echo $this->get_cancelled( $this->transaction_id ); ?>" id="return-where-we-came-from"><?php _e( 'Cancel', 'mycred' ); ?></a></p>
456
+ </div>
457
+ </div>
458
  <?php
459
+
460
  do_action( 'mycred_buycred_page_top', $title, $reload, $this->id );
461
+
462
  }
463
  public function purchase_header( $title = '', $reload = false ) {
464
  $this->get_page_header( $title, $reload );
470
  * @version 1.1
471
  */
472
  function get_page_footer() {
 
473
 
474
+ do_action( 'mycred_buycred_page_footer', $this->id );
475
+
476
+ ?>
477
  </body>
478
  </html>
479
  <?php
480
+
481
  }
482
  function purchase_footer() {
483
  $this->get_page_footer();
485
 
486
  /**
487
  * Get Billing Address Form
488
+ * Depreciated as of 1.7. This should be added by the gateway.
489
  * @since 1.4
490
  * @version 1.0
491
  */
492
  function get_billing_address_form( $country_dropdown = false ) {
493
+
494
  if ( ! is_user_logged_in() ) return;
495
 
496
  $user = wp_get_current_user();
509
 
510
  // Grab WooCommerce User Fields
511
  if ( ! isset( $_POST['billing']['address1'] ) ) {
512
+
513
  if ( class_exists( 'WC_Customer' ) ) {
514
  $user_details['first_name'] = get_user_meta( $user->ID, 'billing_first_name', true );
515
  $user_details['last_name'] = get_user_meta( $user->ID, 'billing_last_name', true );
532
  $user_details['country'] = ( isset( $meta['country'] ) ) ? $meta['country'] : '';
533
  }
534
  }
535
+
536
  }
537
 
538
  // Let others play
542
  $required_fields = apply_filters( 'mycred_buycred_req_fields', array( 'first_name', 'last_name', 'address1', 'city', 'zip', 'state', 'country' ), $this );
543
 
544
  // Show required and optional fields via placeholders
545
+ $required = 'placeholder="required"';
546
+ $optional = 'placeholder="optional"';
547
 
548
+ ?>
549
+ <div class="row">
550
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
551
+ <div class="form-group">
552
  <label for="billing-first-name"><?php _e( 'First Name', 'mycred' ); ?></label>
553
+ <input type="text" name="billing[first_name]" id="billing-first-name" value="<?php echo $user_details['first_name']; ?>" class="form-control<?php if ( array_key_exists( 'first_name', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'first_name', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
554
+ </div>
555
+ </div>
556
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
557
+ <div class="form-group">
558
  <label for="billing-last-name"><?php _e( 'Last Name', 'mycred' ); ?></label>
559
+ <input type="text" name="billing[last_name]" id="billing-last-name" value="<?php echo $user_details['last_name']; ?>" class="form-control<?php if ( array_key_exists( 'last_name', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'last_name', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
560
+ </div>
561
+ </div>
562
+ </div>
563
+ <div class="row">
564
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
565
+ <div class="form-group">
566
  <label for="billing-address1"><?php _e( 'Address Line 1', 'mycred' ); ?></label>
567
+ <input type="text" name="billing[address1]" id="billing-address1" value="<?php echo $user_details['address1']; ?>" class="form-control<?php if ( array_key_exists( 'address1', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'address1', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
568
+ </div>
569
+ </div>
570
+ </div>
571
+ <div class="row">
572
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
573
+ <div class="form-group">
574
  <label for="billing-address2"><?php _e( 'Address Line 2', 'mycred' ); ?></label>
575
+ <input type="text" name="billing[address2]" id="billing-address2" value="<?php echo $user_details['address2']; ?>" class="form-control<?php if ( array_key_exists( 'address2', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'address2', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
576
+ </div>
577
+ </div>
578
+ </div>
579
+ <div class="row">
580
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
581
+ <div class="form-group">
582
  <label for="billing-city"><?php _e( 'City', 'mycred' ); ?></label>
583
+ <input type="text" name="billing[city]" id="billing-city" value="<?php echo $user_details['city']; ?>" class="form-control<?php if ( array_key_exists( 'city', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'city', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
584
+ </div>
585
+ </div>
586
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
587
+ <div class="form-group">
588
  <label for="billing-zip"><?php _e( 'Zip', 'mycred' ); ?></label>
589
+ <input type="text" name="billing[zip]" id="billing-zip" value="<?php echo $user_details['postcode']; ?>" class="form-control<?php if ( array_key_exists( 'zip', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'zip', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
590
+ </div>
591
+ </div>
592
+ </div>
593
+ <div class="row">
594
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
595
+ <div class="form-group">
596
  <label for="billing-state"><?php _e( 'State', 'mycred' ); ?></label>
597
+ <input type="text" name="billing[state]" id="billing-state" value="<?php echo $user_details['state']; ?>" class="form-control<?php if ( array_key_exists( 'state', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'state', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
598
+ </div>
599
+ </div>
600
+ </div>
601
+ <div class="row">
602
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
603
+ <div class="form-group">
604
  <label for="billing-country"><?php _e( 'Country', 'mycred' ); ?></label>
605
+
606
  <?php if ( $country_dropdown !== false ) : ?>
607
+
608
+ <select name="billing[country]" id="billing-country" class="form-control">
609
  <option value=""><?php _e( 'Choose Country', 'mycred' ); ?></option>
610
+
611
  <?php $this->list_option_countries(); ?>
612
+
613
  </select>
614
+
615
  <?php else : ?>
 
616
 
617
+ <input type="text" name="billing[country]" id="billing-country" value="<?php echo $user_details['country']; ?>" class="form-control<?php if ( array_key_exists( 'country', $this->errors ) ) { echo ' error'; } ?>" <?php if ( in_array( 'country', $required_fields ) ) echo $required; else echo $optional; ?> autocomplete="off" />
 
 
 
 
618
 
619
+ <?php endif; ?>
620
+ </div>
621
+ </div>
622
  </div>
623
  <?php
624
+
625
+ do_action( 'mycred_buycred_after_billing_details', $user_details, $this );
626
+
627
  }
628
 
629
  /**
657
  * @version 1.0
658
  */
659
  function get_order( $amount, $cost ) {
 
660
 
661
+ $order_name = apply_filters( 'mycred_buycred_order_name', sprintf( __( '%s Purchase', 'mycred' ), $this->core->singular() ), $amount, $cost, $this );
662
+
663
+ ?>
664
  <table cellpadding="0" cellspacing="0">
665
  <thead>
666
  <tr>
667
+ <th id="gateway-order-item" class="order-item"><?php _ex( 'Item', 'buyCRED order description', 'mycred' ); ?></th>
668
  <th id="gateway-order-amount" class="order-amount"><?php _e( 'Amount', 'mycred' ); ?></th>
669
  <th id="gateway-order-cost" class="order-cost"><?php _e( 'Cost', 'mycred' ); ?></th>
670
  </tr>
678
  </tbody>
679
  </table>
680
  <?php
681
+
682
  }
683
 
684
  /**
686
  * @since 1.0
687
  * @version 1.0
688
  */
689
+ function get_debug() {
690
 
691
+ ?>
692
+ <h2><?php echo 'Debug'; ?></h2>
693
+ <p><span class="description"><?php echo 'Here you can see information that are collected and sent to this gateway. Debug information is only visible for administrators and are intended for troubleshooting / testing of this gateway. Please disable "Sandbox Mode" when you want to take this gateway online.'; ?></span></p>
694
  <table id="gateway-debug">
695
  <thead>
696
  <tr>
697
+ <th id="gateway-col-section" class="col-section"><?php echo 'Section'; ?></th>
698
+ <th id="gateway-col-result" class="col-result"><?php echo 'Result'; ?></th>
699
  </tr>
700
  </thead>
701
  <tbody>
702
  <tr>
703
+ <td class="col-section"><?php echo 'Payment Status'; ?></td>
704
  <td class="col-result"><pre><?php print_r( $this->status ); ?></pre></td>
705
  </tr>
706
  <tr>
707
+ <td class="col-section"><?php echo 'Request'; ?></td>
708
  <td class="col-result"><pre><?php print_r( $this->request ); ?></pre></td>
709
  </tr>
710
  <tr>
711
+ <td class="col-section"><?php echo 'Gateway Response'; ?></td>
712
  <td class="col-result"><pre><?php print_r( $this->response ); ?></pre></td>
713
  </tr>
714
  </tbody>
715
  </table>
716
  <?php
717
+
718
  }
719
 
720
  /**
723
  * @version 1.0
724
  */
725
  function get_errors() {
726
+
727
  if ( empty( $this->errors ) ) return;
728
 
729
  $errors = array();
730
  foreach ( $this->errors as $form_field => $error_message )
731
+ $errors[] = $error_message;
732
 
733
+ ?>
734
  <div class="gateway-error"><?php echo implode( '<br />', $errors ); ?></div>
735
  <?php
736
+
737
  }
738
 
739
  /**
743
  * @version 1.0
744
  */
745
  function get_page_redirect( $hidden_fields = array(), $location = '' ) {
746
+
747
  $id = str_replace( '-', '_', $this->id );
748
 
749
  // Logo
751
  $logo_url = plugins_url( 'images/cred-icon32.png', myCRED_THIS );
752
 
753
  // Hidden Fields
754
+ $hidden_fields = apply_filters( "mycred_{$id}_purchase_fields", $hidden_fields, $this );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
755
 
756
+ ?>
757
+ <div class="row">
758
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
759
+ <form name="mycred_<?php echo $id; ?>_request" class="form text-center" action="<?php echo $location; ?>" method="post" id="redirect-form">
760
+ <?php foreach ( $hidden_fields as $name => $value ) echo '<input type="hidden" name="' . $name . '" value="' . $value . '" />' . "\n"; ?>
761
+ <img src="<?php echo plugins_url( 'assets/images/loading.gif', MYCRED_PURCHASE ); ?>" alt="Loading" />
762
+ <noscript><input type="submit" name="submit-form" value="<?php printf( __( 'Continue to %s', 'mycred' ), $this->label ); ?>" /></noscript>
763
+ <p id="manual-continue"><a href="javascript:void(0);" onclick="document.mycred_<?php echo $id; ?>_request.submit();return false;"><?php _e( 'Click here if you are not automatically redirected', 'mycred' ); ?></a></p>
764
+ </form>
765
+ </div>
766
  </div>
767
+ <script type="text/javascript"><?php if ( $this->sandbox_mode ) echo '//'; ?>setTimeout( "document.mycred_<?php echo $id; ?>_request.submit()",2000 );</script>
 
 
 
 
768
  <?php
769
+
770
  }
771
  function form_with_redirect( $hidden_fields = array(), $location = '', $logo_url = '', $custom_html = '', $sales_data = '' ) {
772
  $this->get_page_redirect( $hidden_fields, $location, $custom_html, $sales_data );
780
  * @version 1.0
781
  */
782
  function get_to() {
783
+
784
  // Gift to a user
785
  if ( $this->core->buy_creds['gifting']['members'] == 1 ) {
786
  if ( isset( $_POST['gift_to'] ) ) {
805
  }
806
 
807
  return $this->current_user_id;
808
+
809
  }
810
 
811
  /**
833
  * @since 0.1
834
  * @version 1.3.1
835
  */
836
+ function get_cancelled( $transaction_id = NULL ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
837
 
838
+ return buycred_get_cancel_transaction_url( $transaction_id );
839
 
840
  }
841
 
846
  * @version 1.0
847
  */
848
  function get_entry( $_to, $_from ) {
849
+
850
  // Log entry
851
  if ( $_to == $_from ) return $this->core->buy_creds['log'];
852
 
854
  return $this->core->buy_creds['gifting']['log'];
855
 
856
  return $this->core->buy_creds['log'];
857
+
858
  }
859
 
860
  /**
863
  * @version 1.2
864
  */
865
  function POST_to_data( $unset = false ) {
866
+
867
  $data = array();
868
  foreach ( $_POST as $key => $value ) {
869
  $data[ $key ] = stripslashes( $value );
872
  unset( $_POST );
873
 
874
  return $data;
875
+
876
  }
877
 
878
  /**
884
  * @version 1.0.1
885
  */
886
  function transaction_id_is_unique( $transaction_id = '' ) {
887
+
888
  if ( empty( $transaction_id ) ) return false;
889
 
890
  global $wpdb;
904
  if ( $wpdb->num_rows > 0 ) return false;
905
 
906
  return true;
907
+
908
  }
909
 
910
  /**
914
  * @version 1.0
915
  */
916
  function create_unique_transaction_id() {
917
+
918
  global $wpdb;
919
 
920
  do {
929
  } while ( ! empty( $query ) );
930
 
931
  return $id;
932
+
933
  }
934
 
935
  /**
939
  * @version 1.0
940
  */
941
  function create_token( $user_id = NULL ) {
942
+
943
  return wp_create_nonce( 'mycred-buy-' . $this->id );
944
+
945
  }
946
 
947
  /**
948
  * Get Point Type
949
  * @since 1.5
950
+ * @version 1.1
951
  */
952
  function get_point_type() {
953
+
954
  $type = '';
955
+
956
  if ( isset( $_REQUEST['ctype'] ) )
957
+ $type = sanitize_key( $_REQUEST['ctype'] );
958
 
959
+ if ( $type == '' || ! mycred_point_type_exists( $type ) )
960
+ $type = MYCRED_DEFAULT_TYPE_KEY;
961
 
962
  return $type;
963
+
964
  }
965
 
966
  /**
976
  * @version 1.0.1
977
  */
978
  function verify_token( $user_id, $nonce ) {
 
979
 
980
+ $uid = absint( $user_id );
981
+ $i = wp_nonce_tick();
982
 
983
  if ( substr( wp_hash( $i . 'mycred-buy-' . $this->id . $uid, 'nonce' ), -12, 10 ) == $nonce )
984
  return true;
986
  return true;
987
 
988
  return false;
989
+
990
  }
991
 
992
  /**
995
  * @version 1.1
996
  */
997
  function encode_sales_data( $data ) {
998
+
999
  $protect = new myCRED_Protect();
1000
  if ( $protect !== false )
1001
  return $protect->do_encode( $data );
1002
+
1003
+ return $data;
1004
+
1005
  }
1006
 
1007
  /**
1010
  * @version 1.1
1011
  */
1012
  function decode_sales_data( $data ) {
1013
+
1014
  $protect = new myCRED_Protect();
1015
  if ( $protect !== false )
1016
  return $protect->do_decode( $data );
1017
+
1018
+ return $data;
1019
+
1020
  }
1021
 
1022
  /**
1024
  * @since 1.3.2
1025
  * @version 1.1
1026
  */
1027
+ function get_cost( $amount = 0, $type = MYCRED_DEFAULT_TYPE_KEY, $raw = false ) {
1028
+
1029
  // Apply minimum
1030
  if ( $amount < $this->core->buy_creds['minimum'] )
1031
  $amount = $this->core->buy_creds['minimum'];
1032
 
1033
  // Calculate cost here so we can use any exchange rate
1034
+ if ( array_key_exists( $type, $this->prefs['exchange'] ) ) {
1035
+
1036
  // Check for user override
1037
  $override = mycred_get_user_meta( $this->current_user_id, 'mycred_buycred_rates_' . $type, '', true );
1038
  if ( isset( $override[ $this->id ] ) && $override[ $this->id ] != '' )
1045
  else
1046
  $rate = (int) $rate;
1047
 
1048
+ $cost = $amount * $rate;
1049
+
1050
  }
1051
  else
1052
  $cost = $amount;
1056
  $cost = number_format( $cost, 2, '.', '' );
1057
 
1058
  return apply_filters( 'mycred_buycred_get_cost', $cost, $amount, $type, $this->prefs, $this->core->buy_creds );
1059
+
1060
  }
1061
 
1062
  /**
1065
  * @version 1.0.2
1066
  */
1067
  function currencies_dropdown( $name = '', $js = '' ) {
1068
+
1069
  $currencies = array(
1070
  'USD' => 'US Dollars',
1071
  'AUD' => 'Australian Dollars',
1106
  echo '>' . $cname . '</option>';
1107
  }
1108
  echo '</select>';
1109
+
1110
  }
1111
 
1112
  /**
1115
  * @version 1.0
1116
  */
1117
  function item_types_dropdown( $name = '' ) {
1118
+
1119
  $types = array(
1120
  'product' => 'Product',
1121
  'service' => 'Service',
1131
  echo '>' . $cname . '</option>';
1132
  }
1133
  echo '</select>';
1134
+
1135
  }
1136
 
1137
  /**
1140
  * @version 1.0
1141
  */
1142
  function list_option_countries( $selected = '' ) {
1143
+
1144
  $countries = array (
1145
  "US" => "UNITED STATES",
1146
  "AF" => "AFGHANISTAN",
1384
  "ZW" => "ZIMBABWE"
1385
  );
1386
  $countries = apply_filters( 'mycred_list_option_countries', $countries );
1387
+
1388
  foreach ( $countries as $code => $cname ) {
1389
  echo '<option value="' . $code . '"';
1390
  if ( $selected == $code ) echo ' selected="selected"';
1391
  echo '>' . $cname . '</option>';
1392
  }
1393
+
1394
  }
1395
 
1396
  /**
1399
  * @version 1.0
1400
  */
1401
  function list_option_us_states( $selected = '', $non_us = false ) {
1402
+
1403
  $states = array (
1404
  "AL" => "Alabama",
1405
  "AK" => "Alaska",
1455
  );
1456
  $states = apply_filters( 'mycred_list_option_us', $states );
1457
 
1458
+ $outside = 'Outside US';
1459
  if ( $non_us == 'top' ) echo '<option value="">' . $outside . '</option>';
1460
  foreach ( $states as $code => $cname ) {
1461
  echo '<option value="' . $code . '"';
1463
  echo '>' . $cname . '</option>';
1464
  }
1465
  if ( $non_us == 'bottom' ) echo '<option value="">' . $outside . '</option>';
1466
+
1467
  }
1468
 
1469
  /**
1472
  * @version 1.0
1473
  */
1474
  function list_option_months( $selected = '' ) {
1475
+
1476
  $months = array (
1477
  "01" => __( 'January', 'mycred' ),
1478
  "02" => __( 'February', 'mycred' ),
1493
  if ( $selected == $number ) echo ' selected="selected"';
1494
  echo '>' . $text . '</option>';
1495
  }
1496
+
1497
  }
1498
 
1499
  /**
1502
  * @version 1.0
1503
  */
1504
  function list_option_card_years( $selected = '', $number = 16 ) {
1505
+
1506
+ $now = current_time( 'timestamp' );
1507
+ $yy = date( 'y', $now );
1508
+ $yyyy = date( 'Y', $now );
1509
+ $count = 0;
1510
  $options = array();
1511
 
1512
  while ( $count <= (int) $number ) {
1523
  if ( $selected == $key ) echo ' selected="selected"';
1524
  echo '>' . $value . '</option>';
1525
  }
1526
+
1527
  }
1528
 
1529
  /**
1532
  * @version 1.0
1533
  */
1534
  function IPN_has_required_fields( $required_fields = array(), $method = 'REQUEST' ) {
1535
+
1536
  $missing = 0;
1537
  foreach ( $required_fields as $field_key ) {
1538
  if ( $method == 'POST' ) {
1552
  $missing ++;
1553
  }
1554
  }
1555
+
1556
  if ( $missing > 0 )
1557
  $result = false;
1558
  else
1559
  $result = true;
1560
+
1561
  $result = apply_filters( 'mycred_buycred_IPN_missing', $result, $required_fields, $this->id );
1562
+
1563
  return $result;
1564
+
1565
  }
1566
 
1567
  /**
1570
  * @version 1.0
1571
  */
1572
  function IPN_is_valid_call() {
1573
+
1574
  return false;
1575
+
1576
  }
1577
 
1578
  /**
1593
  if ( $pending_payment === false ) return false;
1594
 
1595
  $result = true;
1596
+
1597
  if ( $method == 'POST' )
1598
  $price = $_POST[ $cost_key ];
1599
  elseif ( $method == 'GET' )
1600
  $price = $_GET[ $cost_key ];
1601
  else
1602
  $price = $_REQUEST[ $cost_key ];
1603
+
1604
  if ( $result === true && $pending_payment['cost'] != $price ) {
1605
  $result = false;
1606
  }
1619
  if ( $result === true && ! $this->transaction_id_is_unique( $transaction_id ) ) {
1620
  $result = false;
1621
  }
1622
+
1623
  $result = apply_filters( 'mycred_buycred_valid_sale', $result, $sales_data_key, $cost_key, $transactionid_key, $method, $this );
1624
+
1625
  if ( $result === true )
1626
  return $decoded_data;
1627
 
1628
  return $result;
1629
+
1630
  }
1631
 
1632
  /**
1634
  * @since 1.4
1635
  * @version 1.4
1636
  */
1637
+ function complete_payment( $pending_payment = NULL, $transaction_id = '' ) {
1638
+
1639
+ if ( $pending_payment === NULL ) return false;
1640
+
1641
+ $reply = false;
1642
+ $mycred = mycred( $pending_payment->point_type );
1643
+
1644
+ $reference = 'buy_creds_with_' . str_replace( array( ' ', '-' ), '_', $this->id );
1645
  $sales_data = array(
1646
+ 'to' => $pending_payment->recipient_id,
1647
+ 'from' => $pending_payment->buyer_id,
1648
+ 'amount' => $pending_payment->amount,
1649
+ 'cost' => $pending_payment->cost,
1650
+ 'currency' => $pending_payment->currency,
1651
+ 'ctype' => $pending_payment->point_type
1652
  );
1653
+ $data = array( 'ref_type' => 'user', 'txn_id' => $transaction_id, 'sales_data' => implode( '|', $sales_data ) );
1654
 
1655
+ if ( ! $mycred->has_entry( $reference, $pending_payment->buyer_id, $pending_payment->recipient_id, $data, $pending_payment->point_type ) ) {
 
 
 
 
1656
 
 
1657
  add_filter( 'mycred_get_email_events', array( $this, 'email_notice' ), 10, 2 );
1658
  $reply = $mycred->add_creds(
1659
  $reference,
1660
+ $pending_payment->recipient_id,
1661
+ $pending_payment->amount,
1662
+ $this->get_log_entry( $pending_payment->recipient_id, $pending_payment->buyer_id ),
1663
+ $pending_payment->buyer_id,
1664
  $data,
1665
+ $pending_payment->point_type
1666
  );
1667
  remove_filter( 'mycred_get_email_events', array( $this, 'email_notice' ), 10, 2 );
1668
+
1669
  }
1670
 
1671
  return apply_filters( 'mycred_buycred_complete_payment', $reply, $transaction_id, $this );
1672
+
1673
  }
1674
 
1675
  /**
1702
  wp_trash_post( $post->ID );
1703
 
1704
  }
1705
+
1706
  }
1707
+ endif;
1708
+
1709
  ?>
addons/buy-creds/assets/css/gateway.css CHANGED
@@ -1,88 +1,50 @@
1
  html { text-align: center; background-color: #eee; }
2
  body { -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13); box-shadow: 0 1px 3px rgba(0,0,0,0.13); border: 1px solid #dedede; background-color: white; font-family: "Open Sans", sans-serif; }
3
- body { padding: 12px 12px 24px 12px; margin: 48px auto 48px auto; text-align: left; font-weight: 100; width: 730px; font-size: 1em; }
4
-
5
- h2 { margin-bottom: 12px; text-transform: uppercase; font-weight: 100; font-size: 20px; padding: 0 12px; }
6
- p { margin-top: 0; font-size: 14px; }
7
-
8
- table { width: 100%; font-size: 14px; line-height: 28px; vertical-align: top !important; margin-bottom: 24px; }
9
- table thead tr th { border-bottom: 1px solid #dedede; }
10
- table th { text-align: left; font-weight: bold; }
11
- table td pre { line-height: 22px; min-height: 18px !important; margin: 0; font-size: 12px; }
12
-
13
- table#gateway-debug #gateway-col-section { width: 30%; }
14
- table#gateway-debug #gateway-col-result { width: 70%; }
15
-
16
- table #gateway-saved-card-select { width: 10%; }
17
- table #gateway-saved-card-number { width: 40%; }
18
- table #gateway-saved-card-type { width: 20%; }
19
- table #gateway-saved-card-exp { width: 30%; }
20
-
21
- span.description { font-size: 12px; line-height: 14px; font-style: italic; color: gray; }
22
-
23
- form p { margin-top: 0; }
24
- form label { display: block; }
25
- form .long { width: 40%; }
26
- form .medium { width: 20%; }
27
- form .short { width: 10%; }
28
- form .submit { text-align: right; }
29
- form .error label { color: red; }
30
-
31
- #payment-title { text-align: right; font-size: 20px; line-height: 59px; padding-right: 6px; height: 80px; font-weight: 100; text-transform: uppercase; }
32
- #payment-title img { float: left; }
33
- #payment-title #return-where-we-came-from { display: block; font-size: 12px; line-height: 12px; margin-top: -12px; font-weight: 100; text-transform: none; }
34
-
35
- .gateway-error { display: block; float: none; clear: both; border: 1px solid #C02942; background-color: rgba(192,41,66,0.2); border-radius: 2px; padding: 6px; font-size: 13px; line-height: 1.5em; text-align: left; margin-bottom: 24px; }
36
-
37
- div.sandbox-title { margin: 0; padding: 0 12px; outline: none; border-top: 1px solid #efefef; font-size: 18px; line-height: 42px; }
38
-
39
- form#redirect-form { display: block; text-align: center; padding: 48px; border-top: 1px dotted #dedede; }
40
- form#redirect-form img { display: block; margin: 0 auto 24px auto; }
41
- form#redirect-form p { font-size: 10px; }
42
-
43
- ul.input-fields { display: block; clear: both; float: none; margin: 0; padding: 0 24px; list-style-type: none; }
44
- ul.input-fields li { display: block; margin: 0; padding: 0; }
45
- ul.input-fields li.inline { display: inline-block; padding-right: 24px; min-height: 60px; vertical-align: top; }
46
- ul.input-fields li.inline.last { padding-right: 0; }
47
- ul.input-fields li input[type="text"] { border: 1px solid #dedede; line-height: 24px; border-radius: 2px; padding-left: 6px; padding-right: 6px; }
48
- ul.input-fields li label.inline { display: inline; font-size: 12px; }
49
- ul.input-fields li input#card_number { width: 160px; }
50
-
51
- ul.input-fields li input.error { border-color: #C02942; background-color: rgba(192,41,66,0.2); }
52
-
53
- ul.input-fields .long { width: 250px; }
54
- ul.input-fields .medium { width: 150px; }
55
- ul.input-fields .short { width: 80px; }
56
-
57
- input.submit-button { border: 1px solid #C02942; background-color: #C02942; padding: 0 24px; width: auto; line-height: 32px; border-radius: 2px; color: white; font-size: 14px; text-transform: uppercase; font-weight: bold; }
58
- input.submit-button.disabled { border: 1px solid #ededed; background-color: #dedede; color: #ededed; transition: all 1s; }
59
- input.submit-button:hover { cursor: pointer; }
60
-
61
- div.gateway-section { border-top: 1px solid #efefef; border-bottom: 1px solid #efefef; padding: 24px 0; }
62
- div.gateway-section > pre { margin: 0 24px; padding: 12px; background-color: #efefef; border: 1px solid #dedede; }
63
-
64
- #accordion { display: block; float: none; clear: both; border-bottom: 1px solid #efefef; }
65
- #accordion > div.title { margin: 0; padding: 0 12px; outline: none; border-top: 1px solid #efefef; font-size: 18px; line-height: 42px; }
66
- #accordion > div.title span.ui-accordion-header-icon { display: none; }
67
- #accordion > div.title label:hover { cursor: pointer; }
68
- #accordion > div.title.ui-accordion-header-active { border-bottom: 1px solid #efefef; }
69
- #accordion > div.title > input { float: left; display: block; margin-right: 24px; }
70
-
71
- #accordion > div.body { padding: 24px; }
72
- #accordion > div.body p { margin: 0; }
73
-
74
- @media only screen and (max-width: 767px) {
75
- body { width: 90%; margin: 0; padding: 5%; }
76
-
77
- ul.input-fields { padding: 0; }
78
- ul.input-fields li { float: none; display: block; padding-right: 0; }
79
-
80
- #accordion > div.title span.ui-accordion-header-icon { display: none; }
81
- #accordion > div.body { padding: 24px 0; }
82
- }
83
-
84
- @media only screen and (max-width: 480px) {
85
- #payment-title { height: auto; text-align: center; line-height: 32px; }
86
- #payment-title img { display: block; float: none; margin: 0 auto; }
87
- #payment-title #return-where-we-came-from { margin-top: 0; margin-bottom: 24px; }
88
  }
1
  html { text-align: center; background-color: #eee; }
2
  body { -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13); box-shadow: 0 1px 3px rgba(0,0,0,0.13); border: 1px solid #dedede; background-color: white; font-family: "Open Sans", sans-serif; }
3
+ body { padding: 12px; margin: 20% auto 48px auto; text-align: left; font-weight: 100; width: 50%; font-size: 1em; }
4
+ .text-left { text-align: left; }
5
+ .text-center { text-align: center; }
6
+ .text-right { text-align: right; }
7
+ h2 { margin: 12px 0 6px 0; font-weight: 100; }
8
+ form { display: block; margin: 48px 0 48px 0; }
9
+ p { margin: 0 0 12px 0; }
10
+ pre { margin: 0 0 0 0; padding: 12px; border: 1px solid #dedede; background-color: #f1f1f1; color: #333; }
11
+ body .row:last-of-type { margin-bottom: 0; }
12
+ table { width: 100%; }
13
+ .form-control { display: block; width: 100%; height: 34px; padding: 6px 12px; font-size: 14px; line-height: 1.42857143; color: #555555; background-color: #ffffff; background-image: none; border: 1px solid #cccccc; border-radius: 0; -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s; -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s; transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s; }
14
+ .form-control:focus { border-color: #66afe9; outline: 0; -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, 0.6); box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, 0.6); }
15
+ .form-control::-moz-placeholder { color: #999999; opacity: 1; }
16
+ .form-control:-ms-input-placeholder { color: #999999; }
17
+ .form-control::-webkit-input-placeholder { color: #999999; }
18
+ .form-control::-ms-expand { border: 0; background-color: transparent; }
19
+ .form-control[disabled], .form-control[readonly], fieldset[disabled] .form-control { background-color: #eeeeee; opacity: 1; }
20
+ .form-control[disabled], fieldset[disabled] .form-control { cursor: not-allowed; }
21
+ textarea.form-control { height: auto; }
22
+ input[type="search"] { -webkit-appearance: none; }
23
+ @media screen and (-webkit-min-device-pixel-ratio: 0) {
24
+ input[type="date"].form-control,
25
+ input[type="time"].form-control,
26
+ input[type="datetime-local"].form-control,
27
+ input[type="month"].form-control {
28
+ line-height: 34px;
29
+ }
30
+ input[type="date"].input-sm,
31
+ input[type="time"].input-sm,
32
+ input[type="datetime-local"].input-sm,
33
+ input[type="month"].input-sm,
34
+ .input-group-sm input[type="date"],
35
+ .input-group-sm input[type="time"],
36
+ .input-group-sm input[type="datetime-local"],
37
+ .input-group-sm input[type="month"] {
38
+ line-height: 30px;
39
+ }
40
+ input[type="date"].input-lg,
41
+ input[type="time"].input-lg,
42
+ input[type="datetime-local"].input-lg,
43
+ input[type="month"].input-lg,
44
+ .input-group-lg input[type="date"],
45
+ .input-group-lg input[type="time"],
46
+ .input-group-lg input[type="datetime-local"],
47
+ .input-group-lg input[type="month"] {
48
+ line-height: 46px;
49
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  }
addons/buy-creds/gateways/bank-transfer.php ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * myCRED_Bank_Transfer class
6
+ * Manual payment gateway - bank transfers
7
+ * @since 1.7
8
+ * @version 1.0
9
+ */
10
+ if ( ! class_exists( 'myCRED_Bank_Transfer' ) ) :
11
+ class myCRED_Bank_Transfer extends myCRED_Payment_Gateway {
12
+
13
+ /**
14
+ * Construct
15
+ */
16
+ function __construct( $gateway_prefs ) {
17
+
18
+ $types = mycred_get_types();
19
+ $default_exchange = array();
20
+ foreach ( $types as $type => $label )
21
+ $default_exchange[ $type ] = 1;
22
+
23
+ parent::__construct( array(
24
+ 'id' => 'bank',
25
+ 'label' => 'Bank Transfer',
26
+ 'gateway_logo_url' => '',
27
+ 'defaults' => array(
28
+ 'logo' => '',
29
+ 'title' => '',
30
+ 'account' => '',
31
+ 'currency' => 'EUR',
32
+ 'exchange' => $default_exchange
33
+ )
34
+ ), $gateway_prefs );
35
+
36
+ }
37
+
38
+ /**
39
+ * Process Handler
40
+ * @since 1.0
41
+ * @version 1.0
42
+ */
43
+ public function process() { }
44
+
45
+ /**
46
+ * Results Handler
47
+ * @since 1.0
48
+ * @version 1.0
49
+ */
50
+ public function returning() { }
51
+
52
+ /**
53
+ * Buy Handler
54
+ * @since 1.0
55
+ * @version 1.0
56
+ */
57
+ public function buy() {
58
+
59
+ if ( empty( $this->prefs['account'] ) ) wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
60
+
61
+ // Prep
62
+ $type = $this->get_point_type();
63
+ $mycred = mycred( $type );
64
+
65
+ $amount = $mycred->number( $_REQUEST['amount'] );
66
+ $amount = abs( $amount );
67
+
68
+ $cost = $this->get_cost( $amount, $type );
69
+ $to = $this->get_to();
70
+ $from = get_current_user_id();
71
+ $thankyou_url = $this->get_thankyou();
72
+
73
+ // Revisiting pending payment
74
+ if ( isset( $_REQUEST['revisit'] ) )
75
+ $this->transaction_id = strtoupper( sanitize_text_field( $_REQUEST['revisit'] ) );
76
+
77
+ // New pending payment
78
+ else {
79
+ $post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, $this->prefs['currency'], $type ) );
80
+ $this->transaction_id = get_the_title( $post_id );
81
+ }
82
+
83
+ $cancel_url = $this->get_cancelled( $this->transaction_id );
84
+
85
+ // Set Logo
86
+ $logo = '';
87
+ if ( isset( $this->prefs['logo'] ) && ! empty( $this->prefs['logo'] ) )
88
+ $logo = '<img src="' . $this->prefs['logo'] . '" alt="" />';
89
+
90
+ elseif ( isset( $this->prefs['logo_url'] ) && ! empty( $this->prefs['logo_url'] ) )
91
+ $logo = '<img src="' . $this->prefs['logo_url'] . '" alt="" />';
92
+ elseif ( isset( $this->gateway_logo_url ) && ! empty( $this->gateway_logo_url ) )
93
+ $logo = '<img src="' . $this->gateway_logo_url . '" alt="" />';
94
+
95
+ if ( $this->sandbox_mode )
96
+ $title = __( 'Test Payment', 'mycred' );
97
+ elseif ( ! empty( $this->prefs['title'] ) )
98
+ $title = $this->prefs['title'];
99
+ else
100
+ $title = __( 'Payment', 'mycred' );
101
+
102
+ $this->get_page_header( $title );
103
+
104
+ ?>
105
+ <div class="row">
106
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
107
+ <table cellpadding="0" cellspacing="0">
108
+ <thead>
109
+ <tr>
110
+ <th id="gateway-order-item" class="order-item"><?php _e( 'Item', 'mycred' ); ?></th>
111
+ <th id="gateway-order-amount" class="order-amount"><?php echo $this->core->plural() ?></th>
112
+ <th id="gateway-order-cost" class="order-cost"><?php _e( 'Cost', 'mycred' ); ?></th>
113
+ <th id="gateway-order-transaction" class="order-transaction"><?php _e( 'Transaction ID', 'mycred' ); ?></th>
114
+ </tr>
115
+ </thead>
116
+ <tbody>
117
+ <tr>
118
+ <td class="order-item"><?php echo apply_filters( 'mycred_buycred_order_name', sprintf( __( '%s Purchase', 'mycred' ), $this->core->singular() ), $amount, $cost, $this ); ?></td>
119
+ <td class="order-amount"><?php echo $amount; ?></td>
120
+ <td class="order-cost"><?php echo $cost; ?> <?php if ( isset( $this->prefs['currency'] ) ) echo $this->prefs['currency']; else echo 'USD'; ?></td>
121
+ <td class="order-transaction"><?php echo $this->transaction_id; ?></td>
122
+ </tr>
123
+ </tbody>
124
+ </table>
125
+ </div>
126
+ </div>
127
+ <div class="row">
128
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
129
+ <div id="buycred-bank-details">
130
+
131
+ <?php echo wptexturize( wpautop( $this->prefs['account'] ) ); ?>
132
+
133
+ </div>
134
+ </div>
135
+ </div>
136
+ <div class="row">
137
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
138
+ <div id="buycred-continue">
139
+ <p style="text-align:center;"><a href="<?php echo $this->get_thankyou(); ?>"><?php _e( 'Continue', 'mycred' ); ?></a></p>
140
+ </div>
141
+ </div>
142
+ </div>
143
+ <?php
144
+
145
+ $this->get_page_footer();
146
+
147
+ exit;
148
+
149
+ }
150
+
151
+ /**
152
+ * Preferences
153
+ * @since 1.0
154
+ * @version 1.0
155
+ */
156
+ function preferences() {
157
+
158
+ $prefs = $this->prefs;
159
+
160
+ ?>
161
+ <label class="subheader" for="<?php echo $this->field_id( 'title' ); ?>"><?php _e( 'Title', 'mycred' ); ?></label>
162
+ <ol>
163
+ <li>
164
+ <div class="h2"><input type="text" name="<?php echo $this->field_name( 'title' ); ?>" id="<?php echo $this->field_id( 'title' ); ?>" value="<?php echo esc_attr( $prefs['title'] ); ?>" class="long" /></div>
165
+ </li>
166
+ </ol>
167
+ <label class="subheader" for="<?php echo $this->field_id( 'logo' ); ?>"><?php _e( 'Checkout Logo', 'mycred' ); ?></label>
168
+ <ol>
169
+ <li>
170
+ <div class="h2"><input type="text" name="<?php echo $this->field_name( 'logo' ); ?>" id="<?php echo $this->field_id( 'logo' ); ?>" value="<?php echo esc_attr( $prefs['logo'] ); ?>" class="long" /></div>
171
+ </li>
172
+ </ol>
173
+ <label class="subheader" for="buycredbanktransferaccount"><?php _e( 'Bank Account Information', 'mycred' ); ?></label>
174
+ <ol>
175
+ <li>
176
+ <?php wp_editor( $prefs['account'], 'buycredbanktransferaccount', array( 'textarea_name' => $this->field_name( 'account' ), 'textarea_rows' => 10 ) ); ?>
177
+ <span class="description"><?php _e( 'Bank transfer details to show the user on the checkout page.', 'mycred' ); ?></span>
178
+ </li>
179
+ </ol>
180
+ <label class="subheader" for="<?php echo $this->field_id( 'currency' ); ?>"><?php _e( 'Currency', 'mycred' ); ?></label>
181
+ <ol>
182
+ <li>
183
+ <div class="h2"><input type="text" name="<?php echo $this->field_name( 'currency' ); ?>" id="<?php echo $this->field_id( 'currency' ); ?>" data-update="mycred-gateway-bank-currency" value="<?php echo esc_attr( $prefs['currency'] ); ?>" size="8" maxlength="3" /></div>
184
+ </li>
185
+ </ol>
186
+ <label class="subheader"><?php _e( 'Exchange Rates', 'mycred' ); ?></label>
187
+ <ol>
188
+ <?php $this->exchange_rate_setup(); ?>
189
+ </ol>
190
+ <script type="text/javascript">
191
+ jQuery(function($){
192
+
193
+ $( '#mycred-gateway-prefs-bank-currency' ).change(function(){
194
+ $( 'span.mycred-gateway-bank-currency' ).text( $(this).val() );
195
+ });
196
+
197
+ });
198
+ </script>
199
+ <?php
200
+
201
+ }
202
+
203
+ /**
204
+ * Sanatize Prefs
205
+ * @since 1.0
206
+ * @version 1.0
207
+ */
208
+ public function sanitise_preferences( $data ) {
209
+
210
+ $new_data = array();
211
+
212
+ $new_data['title'] = sanitize_text_field( $data['title'] );
213
+ $new_data['logo'] = sanitize_text_field( $data['logo'] );
214
+ $new_data['account'] = wp_kses_post( $data['account'] );
215
+ $new_data['currency'] = sanitize_text_field( $data['currency'] );
216
+
217
+ // If exchange is less then 1 we must start with a zero
218
+ if ( isset( $data['exchange'] ) ) {
219
+ foreach ( (array) $data['exchange'] as $type => $rate ) {
220
+ if ( $rate != 1 && in_array( substr( $rate, 0, 1 ), array( '.', ',' ) ) )
221
+ $data['exchange'][ $type ] = (float) '0' . $rate;
222
+ }
223
+ }
224
+ $new_data['exchange'] = $data['exchange'];
225
+
226
+ return $new_data;
227
+
228
+ }
229
+
230
+ }
231
+ endif;
232
+
233
+ ?>
addons/buy-creds/gateways/bitpay.php CHANGED
@@ -23,7 +23,7 @@ if ( ! class_exists( 'myCRED_Bitpay' ) ) :
23
  parent::__construct( array(
24
  'id' => 'bitpay',
25
  'label' => 'Bitpay',
26
- 'gateway_logo_url' => plugins_url( 'assets/images/bitpay.png', myCRED_PURCHASE ),
27
  'defaults' => array(
28
  'api_key' => '',
29
  'currency' => 'USD',
@@ -54,25 +54,25 @@ if ( ! class_exists( 'myCRED_Bitpay' ) ) :
54
  // Verify Call with PayPal
55
  if ( $this->IPN_is_valid_call() ) {
56
 
57
- $errors = false;
58
  $new_call = array();
59
 
60
  // Check amount paid
61
- if ( $_POST['price'] != $pending_payment['cost'] ) {
62
- $new_call[] = sprintf( __( 'Price mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment['cost'], $_POST['price'] );
63
- $errors = true;
64
  }
65
 
66
  // Check currency
67
- if ( $_POST['currency'] != $pending_payment['currency'] ) {
68
- $new_call[] = sprintf( __( 'Currency mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment['currency'], $_POST['currency'] );
69
- $errors = true;
70
  }
71
 
72
  // Check status
73
  if ( $_POST['status'] != 'paid' ) {
74
  $new_call[] = sprintf( __( 'Payment not completed. Received: %s', 'mycred' ), $_POST['status'] );
75
- $errors = true;
76
  }
77
 
78
  // Credit payment
@@ -156,48 +156,40 @@ if ( ! class_exists( 'myCRED_Bitpay' ) ) :
156
  /**
157
  * Buy Creds
158
  * @since 1.4
159
- * @version 1.1
160
  */
161
  public function buy() {
162
 
163
- if ( ! isset( $this->prefs['api_key'] ) || empty( $this->prefs['api_key'] ) )
164
- wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
165
 
166
- // Type
167
- $type = $this->get_point_type();
168
- $mycred = mycred( $type );
169
 
170
- // Amount
171
- $amount = $mycred->number( $_REQUEST['amount'] );
172
- $amount = abs( $amount );
173
 
174
- // Get Cost
175
- $cost = $this->get_cost( $amount, $type );
 
 
176
 
177
- $to = $this->get_to();
178
- $from = get_current_user_id();
 
179
 
180
  // Revisiting pending payment
181
- if ( isset( $_REQUEST['revisit'] ) ) {
182
- $this->transaction_id = strtoupper( $_REQUEST['revisit'] );
183
- }
 
184
  else {
185
- $post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, $this->prefs['currency'], $type ) );
186
  $this->transaction_id = get_the_title( $post_id );
187
  }
188
 
189
- // Thank you page
190
- $thankyou_url = $this->get_thankyou();
191
-
192
- // Cancel page
193
  $cancel_url = $this->get_cancelled( $this->transaction_id );
194
 
195
- // Item Name
196
- $item_name = str_replace( '%number%', $amount, $this->prefs['item_name'] );
197
- $item_name = $mycred->template_tags_general( $item_name );
198
-
199
- $from_user = get_userdata( $from );
200
-
201
  // Hidden form fields
202
  $request = $this->create_invoice( array(
203
  'apiKey' => $this->prefs['api_key'],
@@ -207,7 +199,7 @@ if ( ! class_exists( 'myCRED_Bitpay' ) ) :
207
  'notificationURL' => $this->callback_url(),
208
  'fullNotifications' => ( $this->prefs['notifications'] ) ? true : false,
209
  'posData' => $this->transaction_id,
210
- 'buyerName' => $from_user->first_name . ' ' . $from_user->last_name,
211
  'itemDesc' => $item_name
212
  ) );
213
 
@@ -229,7 +221,7 @@ if ( ! class_exists( 'myCRED_Bitpay' ) ) :
229
  ?>
230
  <div class="continue-forward" style="text-align:center;">
231
  <p>&nbsp;</p>
232
- <img src="<?php echo plugins_url( 'assets/images/loading.gif', myCRED_PURCHASE ); ?>" alt="Loading" />
233
  <p id="manual-continue"><a href="<?php echo $request['url']; ?>"><?php _e( 'Click here if you are not automatically redirected', 'mycred' ); ?></a></p>
234
  </div>
235
  <?php
23
  parent::__construct( array(
24
  'id' => 'bitpay',
25
  'label' => 'Bitpay',
26
+ 'gateway_logo_url' => plugins_url( 'assets/images/bitpay.png', MYCRED_PURCHASE ),
27
  'defaults' => array(
28
  'api_key' => '',
29
  'currency' => 'USD',
54
  // Verify Call with PayPal
55
  if ( $this->IPN_is_valid_call() ) {
56
 
57
+ $errors = false;
58
  $new_call = array();
59
 
60
  // Check amount paid
61
+ if ( $_POST['price'] != $pending_payment->cost ) {
62
+ $new_call[] = sprintf( __( 'Price mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment->cost, $_POST['price'] );
63
+ $errors = true;
64
  }
65
 
66
  // Check currency
67
+ if ( $_POST['currency'] != $pending_payment->currency ) {
68
+ $new_call[] = sprintf( __( 'Currency mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment->currency, $_POST['currency'] );
69
+ $errors = true;
70
  }
71
 
72
  // Check status
73
  if ( $_POST['status'] != 'paid' ) {
74
  $new_call[] = sprintf( __( 'Payment not completed. Received: %s', 'mycred' ), $_POST['status'] );
75
+ $errors = true;
76
  }
77
 
78
  // Credit payment
156
  /**
157
  * Buy Creds
158
  * @since 1.4
159
+ * @version 1.2
160
  */
161
  public function buy() {
162
 
163
+ if ( ! isset( $this->prefs['api_key'] ) || empty( $this->prefs['api_key'] ) ) wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
 
164
 
165
+ // Prep
166
+ $type = $this->get_point_type();
167
+ $mycred = mycred( $type );
168
 
169
+ $amount = $mycred->number( $_REQUEST['amount'] );
170
+ $amount = abs( $amount );
 
171
 
172
+ $cost = $this->get_cost( $amount, $type );
173
+ $to = $this->get_to();
174
+ $from = get_current_user_id();
175
+ $thankyou_url = $this->get_thankyou();
176
 
177
+ // Item Name
178
+ $item_name = str_replace( '%number%', $amount, $this->prefs['item_name'] );
179
+ $item_name = $mycred->template_tags_general( $item_name );
180
 
181
  // Revisiting pending payment
182
+ if ( isset( $_REQUEST['revisit'] ) )
183
+ $this->transaction_id = strtoupper( sanitize_text_field( $_REQUEST['revisit'] ) );
184
+
185
+ // New pending payment
186
  else {
187
+ $post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, $this->prefs['currency'], $type ) );
188
  $this->transaction_id = get_the_title( $post_id );
189
  }
190
 
 
 
 
 
191
  $cancel_url = $this->get_cancelled( $this->transaction_id );
192
 
 
 
 
 
 
 
193
  // Hidden form fields
194
  $request = $this->create_invoice( array(
195
  'apiKey' => $this->prefs['api_key'],
199
  'notificationURL' => $this->callback_url(),
200
  'fullNotifications' => ( $this->prefs['notifications'] ) ? true : false,
201
  'posData' => $this->transaction_id,
202
+ 'buyerName' => $this->get_buyers_name( $from ),
203
  'itemDesc' => $item_name
204
  ) );
205
 
221
  ?>
222
  <div class="continue-forward" style="text-align:center;">
223
  <p>&nbsp;</p>
224
+ <img src="<?php echo plugins_url( 'assets/images/loading.gif', MYCRED_PURCHASE ); ?>" alt="Loading" />
225
  <p id="manual-continue"><a href="<?php echo $request['url']; ?>"><?php _e( 'Click here if you are not automatically redirected', 'mycred' ); ?></a></p>
226
  </div>
227
  <?php
addons/buy-creds/gateways/netbilling.php CHANGED
@@ -28,7 +28,7 @@ if ( ! class_exists( 'myCRED_NETbilling' ) ) :
28
  parent::__construct( array(
29
  'id' => 'netbilling',
30
  'label' => 'NETbilling',
31
- 'gateway_logo_url' => plugins_url( 'assets/images/netbilling.png', myCRED_PURCHASE ),
32
  'defaults' => array(
33
  'sandbox' => 0,
34
  'account' => '',
@@ -84,19 +84,19 @@ if ( ! class_exists( 'myCRED_NETbilling' ) ) :
84
  // Verify Call with PayPal
85
  if ( $this->IPN_is_valid_call() ) {
86
 
87
- $errors = false;
88
  $new_call = array();
89
 
90
  // Check amount paid
91
- if ( $_REQUEST['Ecom_Cost_Total'] != $pending_payment['cost'] ) {
92
- $new_call[] = sprintf( __( 'Price mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment['cost'], $_REQUEST['Ecom_Cost_Total'] );
93
- $errors = true;
94
  }
95
 
96
  // Check status
97
  if ( $_REQUEST['Ecom_Ezic_Response_StatusCode'] != 1 ) {
98
  $new_call[] = sprintf( __( 'Payment not completed. Received: %s', 'mycred' ), $_REQUEST['Ecom_Ezic_Response_StatusCode'] );
99
- $errors = true;
100
  }
101
 
102
  // Credit payment
@@ -137,49 +137,41 @@ if ( ! class_exists( 'myCRED_NETbilling' ) ) :
137
  /**
138
  * Buy Handler
139
  * @since 0.1
140
- * @version 1.3
141
  */
142
  public function buy() {
143
 
144
- if ( ! isset( $this->prefs['account'] ) || empty( $this->prefs['account'] ) )
145
- wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
146
 
147
- $home = get_bloginfo( 'url' );
 
 
148
 
149
- // Type
150
- $type = $this->get_point_type();
151
- $mycred = mycred( $type );
152
 
153
- // Amount
154
- $amount = $mycred->number( $_REQUEST['amount'] );
155
- $amount = abs( $amount );
156
-
157
- // Get Cost
158
- $cost = $this->get_cost( $amount, $type );
159
- $cost = number_format( $cost, 2, '.', '' );
160
 
161
- $to = $this->get_to();
162
- $from = get_current_user_id();
 
163
 
164
  // Revisiting pending payment
165
- if ( isset( $_REQUEST['revisit'] ) ) {
166
- $this->transaction_id = strtoupper( $_REQUEST['revisit'] );
167
- }
 
168
  else {
169
- $post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, 'USD', $type ) );
170
  $this->transaction_id = get_the_title( $post_id );
171
  }
172
 
173
- // Thank you page
174
- $thankyou_url = $this->get_thankyou();
175
-
176
- // Cancel page
177
  $cancel_url = $this->get_cancelled( $this->transaction_id );
178
 
179
- // Item Name
180
- $item_name = str_replace( '%number%', $amount, $this->prefs['item_name'] );
181
- $item_name = $mycred->template_tags_general( $item_name );
182
-
183
  // Hidden form fields
184
  $hidden_fields = array(
185
  'Ecom_Ezic_AccountAndSitetag' => $this->prefs['account'] . ':' . $this->prefs['site_tag'],
@@ -298,8 +290,8 @@ if ( ! class_exists( 'myCRED_NETbilling' ) ) :
298
  $errors['number'] = __( 'Incorrect Credit Card number', 'mycred' );
299
 
300
  // Check expiration date
301
- $exp_date = mktime( 0, 0, 0, $data['card_expire_month'], 30, $data['card_expire_year'] );
302
- $today_date = date_i18n( 'U' );
303
  if ( $exp_date < $today_date )
304
  $errors['expire'] = __( 'The credit card entered is past its expiration date.', 'mycred' );
305
 
28
  parent::__construct( array(
29
  'id' => 'netbilling',
30
  'label' => 'NETbilling',
31
+ 'gateway_logo_url' => plugins_url( 'assets/images/netbilling.png', MYCRED_PURCHASE ),
32
  'defaults' => array(
33
  'sandbox' => 0,
34
  'account' => '',
84
  // Verify Call with PayPal
85
  if ( $this->IPN_is_valid_call() ) {
86
 
87
+ $errors = false;
88
  $new_call = array();
89
 
90
  // Check amount paid
91
+ if ( $_REQUEST['Ecom_Cost_Total'] != $pending_payment->cost ) {
92
+ $new_call[] = sprintf( __( 'Price mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment->cost, $_REQUEST['Ecom_Cost_Total'] );
93
+ $errors = true;
94
  }
95
 
96
  // Check status
97
  if ( $_REQUEST['Ecom_Ezic_Response_StatusCode'] != 1 ) {
98
  $new_call[] = sprintf( __( 'Payment not completed. Received: %s', 'mycred' ), $_REQUEST['Ecom_Ezic_Response_StatusCode'] );
99
+ $errors = true;
100
  }
101
 
102
  // Credit payment
137
  /**
138
  * Buy Handler
139
  * @since 0.1
140
+ * @version 1.4
141
  */
142
  public function buy() {
143
 
144
+ if ( ! isset( $this->prefs['account'] ) || empty( $this->prefs['account'] ) ) wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
 
145
 
146
+ // Prep
147
+ $type = $this->get_point_type();
148
+ $mycred = mycred( $type );
149
 
150
+ $amount = $mycred->number( $_REQUEST['amount'] );
151
+ $amount = abs( $amount );
 
152
 
153
+ $cost = $this->get_cost( $amount, $type );
154
+ $cost = number_format( $cost, 2, '.', '' );
155
+ $to = $this->get_to();
156
+ $from = get_current_user_id();
157
+ $thankyou_url = $this->get_thankyou();
 
 
158
 
159
+ // Item Name
160
+ $item_name = str_replace( '%number%', $amount, $this->prefs['item_name'] );
161
+ $item_name = $mycred->template_tags_general( $item_name );
162
 
163
  // Revisiting pending payment
164
+ if ( isset( $_REQUEST['revisit'] ) )
165
+ $this->transaction_id = strtoupper( sanitize_text_field( $_REQUEST['revisit'] ) );
166
+
167
+ // New pending payment
168
  else {
169
+ $post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, 'USD', $type ) );
170
  $this->transaction_id = get_the_title( $post_id );
171
  }
172
 
 
 
 
 
173
  $cancel_url = $this->get_cancelled( $this->transaction_id );
174
 
 
 
 
 
175
  // Hidden form fields
176
  $hidden_fields = array(
177
  'Ecom_Ezic_AccountAndSitetag' => $this->prefs['account'] . ':' . $this->prefs['site_tag'],
290
  $errors['number'] = __( 'Incorrect Credit Card number', 'mycred' );
291
 
292
  // Check expiration date
293
+ $exp_date = mktime( 0, 0, 0, $data['card_expire_month'], 30, $data['card_expire_year'] );
294
+ $today_date = current_time( 'timestamp' );
295
  if ( $exp_date < $today_date )
296
  $errors['expire'] = __( 'The credit card entered is past its expiration date.', 'mycred' );
297
 
addons/buy-creds/gateways/paypal-standard.php CHANGED
@@ -23,7 +23,7 @@ if ( ! class_exists( 'myCRED_PayPal_Standard' ) ) :
23
  parent::__construct( array(
24
  'id' => 'paypal-standard',
25
  'label' => 'PayPal',
26
- 'gateway_logo_url' => plugins_url( 'assets/images/paypal.png', myCRED_PURCHASE ),
27
  'defaults' => array(
28
  'sandbox' => 0,
29
  'currency' => '',
@@ -79,7 +79,7 @@ if ( ! class_exists( 'myCRED_PayPal_Standard' ) ) :
79
  curl_setopt( $call, CURLOPT_RETURNTRANSFER, 1 );
80
  curl_setopt( $call, CURLOPT_POSTFIELDS, $request );
81
  curl_setopt( $call, CURLOPT_SSL_VERIFYPEER, 1 );
82
- curl_setopt( $call, CURLOPT_CAINFO, myCRED_PURCHASE_DIR . '/cacert.pem' );
83
  curl_setopt( $call, CURLOPT_SSL_VERIFYHOST, 2 );
84
  curl_setopt( $call, CURLOPT_FRESH_CONNECT, 1 );
85
  curl_setopt( $call, CURLOPT_FORBID_REUSE, 1 );
@@ -137,25 +137,25 @@ if ( ! class_exists( 'myCRED_PayPal_Standard' ) ) :
137
  // Verify Call with PayPal
138
  if ( $this->IPN_is_valid_call() ) {
139
 
140
- $errors = false;
141
  $new_call = array();
142
 
143
  // Check amount paid
144
- if ( $_POST['mc_gross'] != $pending_payment['cost'] ) {
145
- $new_call[] = sprintf( __( 'Price mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment['cost'], $_POST['mc_gross'] );
146
- $errors = true;
147
  }
148
 
149
  // Check currency
150
- if ( $_POST['mc_currency'] != $pending_payment['currency'] ) {
151
- $new_call[] = sprintf( __( 'Currency mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment['currency'], $_POST['mc_currency'] );
152
- $errors = true;
153
  }
154
 
155
  // Check status
156
  if ( $_POST['payment_status'] != 'Completed' ) {
157
  $new_call[] = sprintf( __( 'Payment not completed. Received: %s', 'mycred' ), $_POST['payment_status'] );
158
- $errors = true;
159
  }
160
 
161
  // Credit payment
@@ -204,8 +204,7 @@ if ( ! class_exists( 'myCRED_PayPal_Standard' ) ) :
204
  */
205
  public function buy() {
206
 
207
- if ( ! isset( $this->prefs['account'] ) || empty( $this->prefs['account'] ) )
208
- wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
209
 
210
  // Location
211
  if ( $this->sandbox_mode )
@@ -213,38 +212,32 @@ if ( ! class_exists( 'myCRED_PayPal_Standard' ) ) :
213
  else
214
  $location = 'https://www.paypal.com/cgi-bin/webscr';
215
 
216
- // Type
217
- $type = $this->get_point_type();
218
- $mycred = mycred( $type );
219
 
220
- // Amount
221
- $amount = $mycred->number( $_REQUEST['amount'] );
222
- $amount = abs( $amount );
223
 
224
- // Get Cost
225
- $cost = $this->get_cost( $amount, $type );
 
 
226
 
227
- $to = $this->get_to();
228
- $from = get_current_user_id();
229
 
230
  // Revisiting pending payment
231
- if ( isset( $_REQUEST['revisit'] ) ) {
232
- $this->transaction_id = strtoupper( $_REQUEST['revisit'] );
233
- }
 
234
  else {
235
- $post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, $this->prefs['currency'], $type ) );
236
  $this->transaction_id = get_the_title( $post_id );
237
  }
238
 
239
- // Thank you page
240
- $thankyou_url = $this->get_thankyou();
241
-
242
- // Cancel page
243
- $cancel_url = $this->get_cancelled( $this->transaction_id );
244
-
245
- // Item Name
246
- $item_name = str_replace( '%number%', $amount, $this->prefs['item_name'] );
247
- $item_name = $mycred->template_tags_general( $item_name );
248
 
249
  // Hidden form fields
250
  $hidden_fields = array(
@@ -264,7 +257,7 @@ if ( ! class_exists( 'myCRED_PayPal_Standard' ) ) :
264
  'cancel_return' => $cancel_url
265
  );
266
 
267
- // Generate processing page
268
  $this->get_page_header( __( 'Processing payment &hellip;', 'mycred' ) );
269
  $this->get_page_redirect( $hidden_fields, $location );
270
  $this->get_page_footer();
23
  parent::__construct( array(
24
  'id' => 'paypal-standard',
25
  'label' => 'PayPal',
26
+ 'gateway_logo_url' => plugins_url( 'assets/images/paypal.png', MYCRED_PURCHASE ),
27
  'defaults' => array(
28
  'sandbox' => 0,
29
  'currency' => '',
79
  curl_setopt( $call, CURLOPT_RETURNTRANSFER, 1 );
80
  curl_setopt( $call, CURLOPT_POSTFIELDS, $request );
81
  curl_setopt( $call, CURLOPT_SSL_VERIFYPEER, 1 );
82
+ curl_setopt( $call, CURLOPT_CAINFO, MYCRED_PURCHASE_DIR . '/cacert.pem' );
83
  curl_setopt( $call, CURLOPT_SSL_VERIFYHOST, 2 );
84
  curl_setopt( $call, CURLOPT_FRESH_CONNECT, 1 );
85
  curl_setopt( $call, CURLOPT_FORBID_REUSE, 1 );
137
  // Verify Call with PayPal
138
  if ( $this->IPN_is_valid_call() ) {
139
 
140
+ $errors = false;
141
  $new_call = array();
142
 
143
  // Check amount paid
144
+ if ( $_POST['mc_gross'] != $pending_payment->cost ) {
145
+ $new_call[] = sprintf( __( 'Price mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment->cost, $_POST['mc_gross'] );
146
+ $errors = true;
147
  }
148
 
149
  // Check currency
150
+ if ( $_POST['mc_currency'] != $pending_payment->currency ) {
151
+ $new_call[] = sprintf( __( 'Currency mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment->currency, $_POST['mc_currency'] );
152
+ $errors = true;
153
  }
154
 
155
  // Check status
156
  if ( $_POST['payment_status'] != 'Completed' ) {
157
  $new_call[] = sprintf( __( 'Payment not completed. Received: %s', 'mycred' ), $_POST['payment_status'] );
158
+ $errors = true;
159
  }
160
 
161
  // Credit payment
204
  */
205
  public function buy() {
206
 
207
+ if ( ! isset( $this->prefs['account'] ) || empty( $this->prefs['account'] ) ) wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
 
208
 
209
  // Location
210
  if ( $this->sandbox_mode )
212
  else
213
  $location = 'https://www.paypal.com/cgi-bin/webscr';
214
 
215
+ // Prep
216
+ $type = $this->get_point_type();
217
+ $mycred = mycred( $type );
218
 
219
+ $amount = $mycred->number( $_REQUEST['amount'] );
220
+ $amount = abs( $amount );
 
221
 
222
+ $cost = $this->get_cost( $amount, $type );
223
+ $to = $this->get_to();
224
+ $from = get_current_user_id();
225
+ $thankyou_url = $this->get_thankyou();
226
 
227
+ $item_name = str_replace( '%number%', $amount, $this->prefs['item_name'] );
228
+ $item_name = $mycred->template_tags_general( $item_name );
229
 
230
  // Revisiting pending payment
231
+ if ( isset( $_REQUEST['revisit'] ) )
232
+ $this->transaction_id = strtoupper( sanitize_text_field( $_REQUEST['revisit'] ) );
233
+
234
+ // New pending payment
235
  else {
236
+ $post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, $this->prefs['currency'], $type ) );
237
  $this->transaction_id = get_the_title( $post_id );
238
  }
239
 
240
+ $cancel_url = $this->get_cancelled( $this->transaction_id );
 
 
 
 
 
 
 
 
241
 
242
  // Hidden form fields
243
  $hidden_fields = array(
257
  'cancel_return' => $cancel_url
258
  );
259
 
260
+ // Create Checkout Page
261
  $this->get_page_header( __( 'Processing payment &hellip;', 'mycred' ) );
262
  $this->get_page_redirect( $hidden_fields, $location );
263
  $this->get_page_footer();
addons/buy-creds/gateways/skrill.php CHANGED
@@ -23,7 +23,7 @@ if ( ! class_exists( 'myCRED_Skrill' ) ) :
23
  parent::__construct( array(
24
  'id' => 'skrill',
25
  'label' => 'Skrill Payment',
26
- 'gateway_logo_url' => plugins_url( 'assets/images/skrill.png', myCRED_PURCHASE ),
27
  'defaults' => array(
28
  'sandbox' => 0,
29
  'currency' => '',
@@ -116,25 +116,25 @@ if ( ! class_exists( 'myCRED_Skrill' ) ) :
116
  // Verify Call with PayPal
117
  if ( $this->IPN_is_valid_call() ) {
118
 
119
- $errors = false;
120
  $new_call = array();
121
 
122
  // Check amount paid
123
- if ( $_POST['amount'] != $pending_payment['cost'] ) {
124
- $new_call[] = sprintf( __( 'Price mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment['cost'], $_POST['amount'] );
125
- $errors = true;
126
  }
127
 
128
  // Check currency
129
- if ( $_POST['currency'] != $pending_payment['currency'] ) {
130
- $new_call[] = sprintf( __( 'Currency mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment['currency'], $_POST['currency'] );
131
- $errors = true;
132
  }
133
 
134
  // Check status
135
  if ( $_POST['status'] != '2' ) {
136
  $new_call[] = sprintf( __( 'Payment not completed. Received: %s', 'mycred' ), $_POST['status'] );
137
- $errors = true;
138
  }
139
 
140
  // Credit payment
@@ -183,39 +183,37 @@ if ( ! class_exists( 'myCRED_Skrill' ) ) :
183
  */
184
  public function buy() {
185
 
186
- if ( ! isset( $this->prefs['account'] ) || empty( $this->prefs['account'] ) )
187
- wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
188
 
189
  // Location
190
- $location = 'https://www.moneybookers.com/app/payment.pl';
191
 
192
- // Type
193
- $type = $this->get_point_type();
194
- $mycred = mycred( $type );
195
 
196
- // Amount
197
- $amount = $mycred->number( $_REQUEST['amount'] );
198
- $amount = abs( $amount );
199
 
200
- // Get Cost
201
- $cost = $this->get_cost( $amount, $type );
 
 
202
 
203
- $to = $this->get_to();
204
- $from = get_current_user_id();
 
205
 
206
  // Revisiting pending payment
207
- if ( isset( $_REQUEST['revisit'] ) ) {
208
- $this->transaction_id = strtoupper( $_REQUEST['revisit'] );
209
- }
 
210
  else {
211
- $post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, $this->prefs['currency'], $type ) );
212
  $this->transaction_id = get_the_title( $post_id );
213
  }
214
 
215
- // Thank you page
216
- $thankyou_url = $this->get_thankyou();
217
-
218
- // Cancel page
219
  $cancel_url = $this->get_cancelled( $this->transaction_id );
220
 
221
  // Start constructing merchant details
@@ -251,10 +249,6 @@ if ( ! class_exists( 'myCRED_Skrill' ) ) :
251
  'status_url2' => $this->prefs['account']
252
  ) );
253
 
254
- // Item Name
255
- $item_name = str_replace( '%number%', $amount, $this->prefs['item_name'] );
256
- $item_name = $mycred->template_tags_general( $item_name );
257
-
258
  // Hidden form fields
259
  $sale_details = array(
260
  'merchant_fields' => 'sales_data',
23
  parent::__construct( array(
24
  'id' => 'skrill',
25
  'label' => 'Skrill Payment',
26
+ 'gateway_logo_url' => plugins_url( 'assets/images/skrill.png', MYCRED_PURCHASE ),
27
  'defaults' => array(
28
  'sandbox' => 0,
29
  'currency' => '',
116
  // Verify Call with PayPal
117
  if ( $this->IPN_is_valid_call() ) {
118
 
119
+ $errors = false;
120
  $new_call = array();
121
 
122
  // Check amount paid
123
+ if ( $_POST['amount'] != $pending_payment->cost ) {
124
+ $new_call[] = sprintf( __( 'Price mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment->cost, $_POST['amount'] );
125
+ $errors = true;
126
  }
127
 
128
  // Check currency
129
+ if ( $_POST['currency'] != $pending_payment->currency ) {
130
+ $new_call[] = sprintf( __( 'Currency mismatch. Expected: %s Received: %s', 'mycred' ), $pending_payment->currency, $_POST['currency'] );
131
+ $errors = true;
132
  }
133
 
134
  // Check status
135
  if ( $_POST['status'] != '2' ) {
136
  $new_call[] = sprintf( __( 'Payment not completed. Received: %s', 'mycred' ), $_POST['status'] );
137
+ $errors = true;
138
  }
139
 
140
  // Credit payment
183
  */
184
  public function buy() {
185
 
186
+ if ( ! isset( $this->prefs['account'] ) || empty( $this->prefs['account'] ) ) wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
 
187
 
188
  // Location
189
+ $location = 'https://www.moneybookers.com/app/payment.pl';
190
 
191
+ // Prep
192
+ $type = $this->get_point_type();
193
+ $mycred = mycred( $type );
194
 
195
+ $amount = $mycred->number( $_REQUEST['amount'] );
196
+ $amount = abs( $amount );
 
197
 
198
+ $cost = $this->get_cost( $amount, $type );
199
+ $to = $this->get_to();
200
+ $from = get_current_user_id();
201
+ $thankyou_url = $this->get_thankyou();
202
 
203
+ // Item Name
204
+ $item_name = str_replace( '%number%', $amount, $this->prefs['item_name'] );
205
+ $item_name = $mycred->template_tags_general( $item_name );
206
 
207
  // Revisiting pending payment
208
+ if ( isset( $_REQUEST['revisit'] ) )
209
+ $this->transaction_id = strtoupper( sanitize_text_field( $_REQUEST['revisit'] ) );
210
+
211
+ // New pending payment
212
  else {
213
+ $post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, $this->prefs['currency'], $type ) );
214
  $this->transaction_id = get_the_title( $post_id );
215
  }
216
 
 
 
 
 
217
  $cancel_url = $this->get_cancelled( $this->transaction_id );
218
 
219
  // Start constructing merchant details
249
  'status_url2' => $this->prefs['account']
250
  ) );
251
 
 
 
 
 
252
  // Hidden form fields
253
  $sale_details = array(
254
  'merchant_fields' => 'sales_data',
addons/buy-creds/gateways/zombaio.php CHANGED
@@ -18,7 +18,7 @@ if ( ! class_exists( 'myCRED_Zombaio' ) ) :
18
  parent::__construct( array(
19
  'id' => 'zombaio',
20
  'label' => 'Zombaio',
21
- 'gateway_logo_url' => plugins_url( 'assets/images/zombaio.png', myCRED_PURCHASE ),
22
  'defaults' => array(
23
  'sandbox' => 0,
24
  'site_id' => '',
@@ -146,43 +146,49 @@ if ( ! class_exists( 'myCRED_Zombaio' ) ) :
146
  // Validate call
147
  if ( $this->IPN_is_valid_call() ) {
148
 
149
- $errors = false;
150
- $new_call = array();
 
151
 
152
  // Make sure transaction is unique
153
- if ( ! $this->transaction_id_is_unique( $_GET['TransactionID'] ) ) {
154
- $new_call[] = sprintf( __( 'Duplicate transaction. Received: %s', 'mycred' ), $_GET['TransactionID'] );
155
- $errors = true;
156
  }
157
 
158
  // Live transaction during testing
159
- if ( $this->sandbox_mode && $_GET['TransactionID'] != '0000' ) {
160
- $new_call[] = sprintf( __( 'Live transaction while debug mode is enabled! Received: %s', 'mycred' ), $_GET['TransactionID'] );
161
- $errors = true;
162
  }
163
 
164
  // Credit payment
165
  if ( $errors === false ) {
166
 
167
- // Type
168
- $type = $pending_payment['ctype'];
169
- $mycred = mycred( $type );
170
 
171
- // Amount
172
- $amount = $mycred->number( $_GET['Credits'] );
173
- $pending_payment['amount'] = $amount;
174
 
175
- // Get Cost
176
- $cost = $this->get_cost( $amount, $type );
177
- $pending_payment['cost'] = $cost;
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
- // If account is credited, delete the post and it's comments.
180
- if ( $this->complete_payment( $pending_payment, $_GET['TransactionID'] ) ) {
181
- $this->trash_pending_payment( $pending_post_id );
182
- $outcome = 'COMPLETED';
183
  }
184
- else
185
- $new_call[] = __( 'Failed to credit users account.', 'mycred' );
186
 
187
  }
188
 
@@ -206,35 +212,31 @@ if ( ! class_exists( 'myCRED_Zombaio' ) ) :
206
  /**
207
  * Buy Handler
208
  * @since 1.1
209
- * @version 1.2
210
  */
211
  public function buy() {
212
 
213
- if ( ! isset( $this->prefs['site_id'] ) || empty( $this->prefs['site_id'] ) )
214
- wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
215
 
216
  // Construct location
217
  $location = 'https://secure.zombaio.com/?' . $this->prefs['site_id'] . '.' . $this->prefs['pricing_id'] . '.' . $this->prefs['lang'];
218
 
219
- // Type
220
- $type = $this->get_point_type();
221
-
222
- $to = $this->get_to();
223
- $from = get_current_user_id();
224
 
225
  // Revisiting pending payment
226
- if ( isset( $_REQUEST['revisit'] ) ) {
227
- $this->transaction_id = strtoupper( $_REQUEST['revisit'] );
228
- }
 
229
  else {
230
- $post_id = $this->add_pending_payment( array( $to, $from, '-', '-', 'USD', $type ) );
231
  $this->transaction_id = get_the_title( $post_id );
232
  }
233
 
234
- // Thank you page
235
- $thankyou_url = $this->get_thankyou();
236
-
237
- // Cancel page
238
  $cancel_url = $this->get_cancelled( $this->transaction_id );
239
 
240
  $hidden_fields = array(
@@ -317,14 +319,14 @@ if ( ! class_exists( 'myCRED_Zombaio' ) ) :
317
  */
318
  public function sanitise_preferences( $data ) {
319
 
320
- $new_data = array();
321
 
322
- $new_data['sandbox'] = ( isset( $data['sandbox'] ) ) ? 1 : 0;
323
  $new_data['site_id'] = sanitize_text_field( $data['site_id'] );
324
  $new_data['gwpass'] = sanitize_text_field( $data['gwpass'] );
325
  $new_data['pricing_id'] = sanitize_text_field( $data['pricing_id'] );
326
  $new_data['logo_url'] = sanitize_text_field( $data['logo_url'] );
327
- $new_data['bypass_ipn'] = ( isset( $data['bypass_ipn'] ) ) ? 1 : 0;
328
  $new_data['lang'] = sanitize_text_field( $data['lang'] );
329
 
330
  return $new_data;
18
  parent::__construct( array(
19
  'id' => 'zombaio',
20
  'label' => 'Zombaio',
21
+ 'gateway_logo_url' => plugins_url( 'assets/images/zombaio.png', MYCRED_PURCHASE ),
22
  'defaults' => array(
23
  'sandbox' => 0,
24
  'site_id' => '',
146
  // Validate call
147
  if ( $this->IPN_is_valid_call() ) {
148
 
149
+ $errors = false;
150
+ $new_call = array();
151
+ $transaction_id = sanitize_text_field( $_GET['TransactionID'] );
152
 
153
  // Make sure transaction is unique
154
+ if ( ! $this->transaction_id_is_unique( $transaction_id ) ) {
155
+ $new_call[] = sprintf( __( 'Duplicate transaction. Received: %s', 'mycred' ), $transaction_id );
156
+ $errors = true;
157
  }
158
 
159
  // Live transaction during testing
160
+ if ( $this->sandbox_mode && $transaction_id != '0000' ) {
161
+ $new_call[] = sprintf( __( 'Live transaction while debug mode is enabled! Received: %s', 'mycred' ), $transaction_id );
162
+ $errors = true;
163
  }
164
 
165
  // Credit payment
166
  if ( $errors === false ) {
167
 
168
+ $amount = sanitize_text_field( $_GET['Credits'] );
169
+ if ( is_numeric( $amount ) && $amount > 0 ) {
 
170
 
171
+ // Type
172
+ $type = $pending_payment->point_type;
173
+ $mycred = mycred( $type );
174
 
175
+ // Amount
176
+ $amount = $mycred->number( $amount );
177
+ $pending_payment->amount = $amount;
178
+
179
+ // Get Cost
180
+ $cost = $this->get_cost( $amount, $type );
181
+ $pending_payment->cost = $cost;
182
+
183
+ // If account is credited, delete the post and it's comments.
184
+ if ( $this->complete_payment( $pending_payment, $transaction_id ) ) {
185
+ $this->trash_pending_payment( $pending_post_id );
186
+ $outcome = 'COMPLETED';
187
+ }
188
+ else
189
+ $new_call[] = __( 'Failed to credit users account.', 'mycred' );
190
 
 
 
 
 
191
  }
 
 
192
 
193
  }
194
 
212
  /**
213
  * Buy Handler
214
  * @since 1.1
215
+ * @version 1.3
216
  */
217
  public function buy() {
218
 
219
+ if ( ! isset( $this->prefs['site_id'] ) || empty( $this->prefs['site_id'] ) ) wp_die( __( 'Please setup this gateway before attempting to make a purchase!', 'mycred' ) );
 
220
 
221
  // Construct location
222
  $location = 'https://secure.zombaio.com/?' . $this->prefs['site_id'] . '.' . $this->prefs['pricing_id'] . '.' . $this->prefs['lang'];
223
 
224
+ // Prep
225
+ $type = $this->get_point_type();
226
+ $to = $this->get_to();
227
+ $from = get_current_user_id();
228
+ $thankyou_url = $this->get_thankyou();
229
 
230
  // Revisiting pending payment
231
+ if ( isset( $_REQUEST['revisit'] ) )
232
+ $this->transaction_id = strtoupper( sanitize_text_field( $_REQUEST['revisit'] ) );
233
+
234
+ // New pending payment
235
  else {
236
+ $post_id = $this->add_pending_payment( array( $to, $from, '-', '-', 'USD', $type ) );
237
  $this->transaction_id = get_the_title( $post_id );
238
  }
239
 
 
 
 
 
240
  $cancel_url = $this->get_cancelled( $this->transaction_id );
241
 
242
  $hidden_fields = array(
319
  */
320
  public function sanitise_preferences( $data ) {
321
 
322
+ $new_data = array();
323
 
324
+ $new_data['sandbox'] = ( array_key_exists( 'sandbox', $data ) ) ? 1 : 0;
325
  $new_data['site_id'] = sanitize_text_field( $data['site_id'] );
326
  $new_data['gwpass'] = sanitize_text_field( $data['gwpass'] );
327
  $new_data['pricing_id'] = sanitize_text_field( $data['pricing_id'] );
328
  $new_data['logo_url'] = sanitize_text_field( $data['logo_url'] );
329
+ $new_data['bypass_ipn'] = ( array_key_exists( 'bypass_ipn', $data ) ) ? 1 : 0;
330
  $new_data['lang'] = sanitize_text_field( $data['lang'] );
331
 
332
  return $new_data;
addons/buy-creds/includes/buycred-functions.php ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'MYCRED_PURCHASE' ) ) exit;
3
+
4
+ /**
5
+ * Get Pending Payment
6
+ * @since 1.7
7
+ * @version 1.0
8
+ */
9
+ if ( ! function_exists( 'buycred_get_pending_payment_id' ) ) :
10
+ function buycred_get_pending_payment_id( $payment_id = NULL ) {
11
+
12
+ if ( $payment_id === NULL || $payment_id == '' ) return false;
13
+
14
+ // In case we are using the transaction ID instead of the post ID.
15
+ $post_id = false;
16
+ if ( ! is_numeric( $payment_id ) ) {
17
+
18
+ $post = get_page_by_title( strtoupper( $payment_id ), OBJECT, 'buycred_payment' );
19
+ if ( $post === NULL ) return false;
20
+
21
+ $post_id = $post->ID;
22
+
23
+ }
24
+ else {
25
+ $post_id = absint( $payment_id );
26
+ }
27
+
28
+ return $post_id;
29
+
30
+ }
31
+ endif;
32
+
33
+ /**
34
+ * Get Pending Payment
35
+ * @since 1.7
36
+ * @version 1.0
37
+ */
38
+ if ( ! function_exists( 'buycred_get_pending_payment' ) ) :
39
+ function buycred_get_pending_payment( $payment_id = NULL ) {
40
+
41
+ // Construct fake pending object ( when no pending payment object exists )
42
+ if ( is_array( $payment_id ) ) {
43
+
44
+ $pending_payment = new StdClass();
45
+ $pending_payment->payment_id = false;
46
+ $pending_payment->public_id = $payment_id['public_id'];
47
+ $pending_payment->point_type = $payment_id['point_type'];
48
+ $pending_payment->amount = $payment_id['amount'];
49
+ $pending_payment->cost = $payment_id['cost'];
50
+ $pending_payment->currency = $payment_id['currency'];
51
+ $pending_payment->buyer_id = $payment_id['buyer_id'];
52
+ $pending_payment->recipient_id = $payment_id['recipient_id'];
53
+ $pending_payment->gateway_id = $payment_id['gateway_id'];
54
+ $pending_payment->transaction_id = $payment_id['transaction_id'];
55
+ $pending_payment->cancel_url = false;
56
+ $pending_payment->pay_now_url = false;
57
+
58
+ }
59
+
60
+ else {
61
+
62
+ $payment_id = buycred_get_pending_payment_id( $payment_id );
63
+
64
+ if ( $payment_id === false ) return false;
65
+
66
+ $pending_payment = new StdClass();
67
+ $pending_payment->payment_id = absint( $payment_id );
68
+ $pending_payment->public_id = get_the_title( $payment_id );
69
+ $pending_payment->point_type = get_post_meta( $payment_id, 'point_type', true );
70
+ $pending_payment->amount = get_post_meta( $payment_id, 'amount', true );
71
+ $pending_payment->cost = get_post_meta( $payment_id, 'cost', true );
72
+ $pending_payment->currency = get_post_meta( $payment_id, 'currency', true );
73
+ $pending_payment->buyer_id = get_post_meta( $payment_id, 'from', true );
74
+ $pending_payment->recipient_id = get_post_meta( $payment_id, 'to', true );
75
+ $pending_payment->gateway_id = get_post_meta( $payment_id, 'gateway', true );
76
+ $pending_payment->transaction_id = get_the_title( $payment_id );
77
+
78
+ $pending_payment->cancel_url = buycred_get_cancel_transaction_url( $pending_payment->public_id );
79
+
80
+ $pending_payment->pay_now_url = add_query_arg( array(
81
+ 'mycred_buy' => $pending_payment->gateway_id,
82
+ 'amount' => $pending_payment->amount,
83
+ 'revisit' => $payment_id,
84
+ 'token' => wp_create_nonce( 'mycred-buy-creds' )
85
+ ), set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) );
86
+
87
+ }
88
+
89
+ return apply_filters( 'buycred_get_pending_payment', $pending_payment, $payment_id );
90
+
91
+ }
92
+ endif;
93
+
94
+ /**
95
+ * Add Pending Comment
96
+ * @since 1.7
97
+ * @version 1.0
98
+ */
99
+ if ( ! function_exists( 'buycred_add_pending_comment' ) ) :
100
+ function buycred_add_pending_comment( $payment_id = NULL, $comment = NULL, $time = NULL ) {
101
+
102
+ if ( MYCRED_BUYCRED_PENDING_COMMENTS === false ) return true;
103
+
104
+ $post_id = buycred_get_pending_payment_id( $payment_id );
105
+ if ( $post_id === false ) return false;
106
+
107
+ global $mycred_modules;
108
+
109
+ if ( $time === NULL || $time == 'now' )
110
+ $time = current_time( 'mysql' );
111
+
112
+ $author = 'buyCRED';
113
+ $gateway = get_post_meta( $post_id, 'gateway', true );
114
+ $gateways = $mycred_modules['solo']['buycred']->get();
115
+ $author_url = '';
116
+
117
+ if ( array_key_exists( $gateway, $gateways ) ) {
118
+ $author = sprintf( 'buyCRED: %s %s', $gateways[ $gateway ]['title'], __( 'Gateway', 'mycred' ) );
119
+ $author_email = apply_filters( 'mycred_buycred_comment_email', 'buycred-service@mycred.me' );
120
+ }
121
+
122
+ return wp_insert_comment( array(
123
+ 'comment_post_ID' => $post_id,
124
+ 'comment_author' => $author,
125
+ 'comment_author_email' => $author_email,
126
+ 'comment_content' => $comment,
127
+ 'comment_type' => 'comment',
128
+ 'comment_author_IP' => $_SERVER['REMOTE_ADDR'],
129
+ 'comment_date' => $time,
130
+ 'comment_approved' => 1,
131
+ 'user_id' => 0
132
+ ) );
133
+
134
+ }
135
+ endif;
136
+
137
+ /**
138
+ * Get Cancel URL
139
+ * @since 1.7
140
+ * @version 1.0
141
+ */
142
+ if ( ! function_exists( 'buycred_get_cancel_transaction_url' ) ) :
143
+ function buycred_get_cancel_transaction_url( $transaction_id = NULL ) {
144
+
145
+ $mycred = mycred();
146
+ $base = get_bloginfo( 'url' );
147
+
148
+ // Cancel page
149
+ if ( $mycred->buy_creds['cancelled']['use'] == 'page' ) {
150
+ if ( ! empty( $mycred->buy_creds['cancelled']['page'] ) )
151
+ $base = get_permalink( $mycred->buy_creds['cancelled']['page'] );
152
+ }
153
+
154
+ // Custom URL
155
+ else {
156
+ $base = get_bloginfo( 'url' ) . '/' . $mycred->buy_creds['cancelled']['custom'];
157
+ }
158
+
159
+ // Override
160
+ if ( isset( $_REQUEST['return_to'] ) && sanitize_url( $_REQUEST['return_to'] ) != '' )
161
+ $base = sanitize_url( $_REQUEST['return_to'] );
162
+
163
+ if ( $transaction_id !== NULL )
164
+ $url = add_query_arg( array( 'buycred-cancel' => $transaction_id, '_token' => wp_create_nonce( 'buycred-cancel-pending-payment' ) ), $base );
165
+ else
166
+ $url = $base;
167
+
168
+ return apply_filters( 'mycred_buycred_cancel_url', $url, $transaction_id );
169
+
170
+ }
171
+ endif;
172
+
173
+
174
+
175
+
176
+ /**
177
+ * Get Users Pending Payments
178
+ * @since 1.7
179
+ * @version 1.0
180
+ */
181
+ if ( ! function_exists( 'buycred_get_users_pending_payments' ) ) :
182
+ function buycred_get_users_pending_payments( $user_id = NULL, $point_type = NULL ) {
183
+
184
+ $user_id = absint( $user_id );
185
+ if ( $user_id === 0 ) return false;
186
+
187
+ $pending = get_user_meta( $user_id, 'buycred_pending_payments', true );
188
+ if ( $pending == '' ) {
189
+
190
+ global $wpdb;
191
+
192
+ $pending = array();
193
+ $saved = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} posts WHERE posts.post_type = 'buycred_payment' AND posts.post_author = %d AND posts.post_status = 'publish';", $user_id ) );
194
+ if ( ! empty( $saved ) ) {
195
+
196
+ foreach ( $saved as $entry ) {
197
+
198
+ $point_type = get_post_meta( $entry->ID, 'point_type', true );
199
+ if ( $point_type == '' ) $point_type = MYCRED_DEFAULT_TYPE_KEY;
200
+
201
+ if ( ! array_key_exists( $point_type, $pending ) )
202
+ $pending[ $point_type ] = array();
203
+
204
+ $pending[ $point_type ][] = buycred_get_pending_payment( (int) $entry->ID );
205
+
206
+ }
207
+
208
+ //add_user_meta( $user_id, 'buycred_pending_payments', $pending, true );
209
+
210
+ }
211
+
212
+ }
213
+
214
+ if ( $point_type !== NULL && mycred_point_type_exists( $point_type ) ) {
215
+
216
+ if ( ! array_key_exists( $point_type, $pending ) )
217
+ return false;
218
+
219
+ return $pending[ $point_type ];
220
+
221
+ }
222
+
223
+ return $pending;
224
+
225
+ }
226
+ endif;
227
+
228
+ /**
229
+ * buyCRED Gateway Constructor
230
+ * @since 1.7
231
+ * @version 1.0
232
+ */
233
+ if ( ! function_exists( 'buycred_gateway' ) ) :
234
+ function buycred_gateway( $gateway_id = NULL ) {
235
+
236
+ global $mycred_modules;
237
+
238
+ $gateway = false;
239
+ $installed = $mycred_modules['solo']['buycred']->get();
240
+
241
+ if ( array_key_exists( $gateway_id, $installed ) ) {
242
+
243
+ $class = $installed[ $gateway_id ]['callback'][0];
244
+
245
+ // Construct Gateway
246
+ $gateway = new $class( $mycred_modules['solo']['buycred']->gateway_prefs );
247
+
248
+ }
249
+
250
+ return $gateway;
251
+
252
+ }
253
+ endif;
254
+
255
+ /**
256
+ * Delete Pending Payment
257
+ * @since 1.7
258
+ * @version 1.0
259
+ */
260
+ if ( ! function_exists( 'buycred_trash_pending_payment' ) ) :
261
+ function buycred_trash_pending_payment( $payment_id = NULL ) {
262
+
263
+ $pending_payment = buycred_get_pending_payment( $payment_id );
264
+ if ( $pending_payment === false ) return false;
265
+
266
+ delete_user_meta( $pending_payment->buyer_id, 'buycred_pending_payments' );
267
+
268
+ return wp_trash_post( $pending_payment->payment_id );
269
+
270
+ }
271
+ endif;
272
+
273
+ /**
274
+ * Complete Pending Payment
275
+ * @since 1.7
276
+ * @version 1.0
277
+ */
278
+ if ( ! function_exists( 'buycred_complete_pending_payment' ) ) :
279
+ function buycred_complete_pending_payment( $pending_id ) {
280
+
281
+ $pending_payment = buycred_get_pending_payment( $pending_id );
282
+ if ( $pending_payment === false ) return false;
283
+
284
+ $gateway = buycred_gateway( $pending_payment->gateway_id );
285
+ if ( $gateway === false ) return false;
286
+
287
+ // Complete Payment
288
+ $paid = $gateway->complete_payment( $pending_payment, $pending_payment->transaction_id );
289
+
290
+ if ( $paid )
291
+ return buycred_trash_pending_payment( $pending_id );
292
+
293
+ return $paid;
294
+
295
+ }
296
+ endif;
297
+
298
+ ?>
addons/buy-creds/includes/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ ?>
addons/buy-creds/modules/buycred-module-core.php ADDED
@@ -0,0 +1,1486 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'MYCRED_PURCHASE' ) ) exit;
3
+
4
+ /**
5
+ * myCRED_buyCRED_Module class
6
+ * @since 0.1
7
+ * @version 1.4
8
+ */
9
+ if ( ! class_exists( 'myCRED_buyCRED_Module' ) ) :
10
+ class myCRED_buyCRED_Module extends myCRED_Module {
11
+
12
+ public $purchase_log = '';
13
+
14
+ /**
15
+ * Construct
16
+ */
17
+ function __construct( $type = MYCRED_DEFAULT_TYPE_KEY ) {
18
+
19
+ parent::__construct( 'myCRED_BuyCRED_Module', array(
20
+ 'module_name' => 'gateways',
21
+ 'option_id' => 'mycred_pref_buycreds',
22
+ 'defaults' => array(
23
+ 'installed' => array(),
24
+ 'active' => array(),
25
+ 'gateway_prefs' => array()
26
+ ),
27
+ 'labels' => array(
28
+ 'menu' => __( 'Payment Gateways', 'mycred' ),
29
+ 'page_title' => __( 'Payment Gateways', 'mycred' ),
30
+ 'page_header' => __( 'Payment Gateways', 'mycred' )
31
+ ),
32
+ 'screen_id' => MYCRED_SLUG . '-gateways',
33
+ 'accordion' => true,
34
+ 'add_to_core' => true,
35
+ 'menu_pos' => 70
36
+ ), $type );
37
+
38
+ // Adjust Module to the selected point type
39
+ $this->mycred_type = MYCRED_DEFAULT_TYPE_KEY;
40
+ if ( isset( $this->core->buy_creds['type'] ) )
41
+ $this->mycred_type = $this->core->buy_creds['type'];
42
+
43
+ }
44
+
45
+ /**
46
+ * Load
47
+ * @version 1.0
48
+ */
49
+ public function load() {
50
+
51
+ add_filter( 'mycred_parse_log_entry', array( $this, 'render_gift_tags' ), 10, 2 );
52
+
53
+ add_action( 'mycred_init', array( $this, 'module_init' ) );
54
+ add_action( 'template_redirect', array( $this, 'module_run' ) );
55
+ add_action( 'mycred_admin_init', array( $this, 'module_admin_init' ) );
56
+
57
+ add_action( 'mycred_admin_init', array( $this, 'register_settings' ) );
58
+ add_action( 'mycred_add_menu', array( $this, 'add_menu' ), $this->menu_pos );
59
+ add_action( 'mycred_add_menu', array( $this, 'add_to_menu' ), $this->menu_pos+1 );
60
+ add_action( 'mycred_after_core_prefs', array( $this, 'after_general_settings' ) );
61
+ add_filter( 'mycred_save_core_prefs', array( $this, 'sanitize_extra_settings' ), 90, 3 );
62
+
63
+ }
64
+
65
+ /**
66
+ * Init
67
+ * Register shortcodes.
68
+ * @since 0.1
69
+ * @version 1.4
70
+ */
71
+ public function module_init() {
72
+
73
+ // Add shortcodes first
74
+ add_shortcode( 'mycred_buy', array( $this, 'render_shortcode_basic' ) );
75
+ add_shortcode( 'mycred_buy_form', array( $this, 'render_shortcode_form' ) );
76
+
77
+ $this->current_user_id = get_current_user_id();
78
+
79
+ }
80
+
81
+ /**
82
+ * Get Payment Gateways
83
+ * Retreivs all available payment gateways that can be used to buy CREDs.
84
+ * @since 0.1
85
+ * @version 1.1
86
+ */
87
+ public function get() {
88
+
89
+ $installed = array();
90
+
91
+ // PayPal Standard
92
+ $installed['paypal-standard'] = array(
93
+ 'title' => 'PayPal Payments Standard',
94
+ 'callback' => array( 'myCRED_PayPal_Standard' ),
95
+ 'icon' => 'dashicons-admin-generic',
96
+ 'external' => true
97
+ );
98
+
99
+ // BitPay
100
+ $installed['bitpay'] = array(
101
+ 'title' => 'BitPay (Bitcoins)',
102
+ 'callback' => array( 'myCRED_Bitpay' ),
103
+ 'icon' => 'dashicons-admin-generic',
104
+ 'external' => true
105
+ );
106
+
107
+ // NetBilling
108
+ $installed['netbilling'] = array(
109
+ 'title' => 'NETBilling',
110
+ 'callback' => array( 'myCRED_NETbilling' ),
111
+ 'icon' => 'dashicons-admin-generic',
112
+ 'external' => true
113
+ );
114
+
115
+ // Skrill
116
+ $installed['skrill'] = array(
117
+ 'title' => 'Skrill (Moneybookers)',
118
+ 'callback' => array( 'myCRED_Skrill' ),
119
+ 'icon' => 'dashicons-admin-generic',
120
+ 'external' => true
121
+ );
122
+
123
+ // Zombaio
124
+ $installed['zombaio'] = array(
125
+ 'title' => 'Zombaio',
126
+ 'callback' => array( 'myCRED_Zombaio' ),
127
+ 'icon' => 'dashicons-admin-generic',
128
+ 'external' => true
129
+ );
130
+
131
+ // Bank Transfers
132
+ $installed['bank'] = array(
133
+ 'title' => __( 'Bank Transfer', 'mycred' ),
134
+ 'callback' => array( 'myCRED_Bank_Transfer' ),
135
+ 'icon' => 'dashicons-admin-generic',
136
+ 'external' => false
137
+ );
138
+
139
+ $installed = apply_filters( 'mycred_setup_gateways', $installed );
140
+
141
+ // Untill all custom gateways have been updated, make sure all gateways have an external setting
142
+ if ( ! empty( $installed ) ) {
143
+ foreach ( $installed as $id => $settings ) {
144
+ if ( ! array_key_exists( 'external', $settings ) )
145
+ $installed[ $id ]['external'] = true;
146
+ }
147
+ }
148
+
149
+ return $installed;
150
+
151
+ }
152
+
153
+ /**
154
+ * Run
155
+ * Runs a gateway if requested.
156
+ * @since 1.7
157
+ * @version 1.0
158
+ */
159
+ public function module_run() {
160
+
161
+ // Prep
162
+ $installed = $this->get();
163
+
164
+ // Make sure we have installed gateways.
165
+ if ( empty( $installed ) ) return;
166
+
167
+ /**
168
+ * Step 1 - Look for returns
169
+ * Runs though all active payment gateways and lets them decide if this is the
170
+ * user returning after a remote purchase. Each gateway should know what to look
171
+ * for to determen if they are responsible for handling the return.
172
+ */
173
+ foreach ( $installed as $id => $data ) {
174
+
175
+ // Only applicable if the gateway is active and marked as external (new in 1.7)
176
+ if ( $this->is_active( $id ) && $data['external'] === true )
177
+ $this->call( 'returning', $installed[ $id ]['callback'] );
178
+
179
+ }
180
+
181
+ /**
182
+ * Step 2 - Check for gateway calls
183
+ * Checks to see if a gateway should be loaded.
184
+ */
185
+ $gateway_id = false;
186
+ $process = false;
187
+
188
+ if ( isset( $_REQUEST['mycred_call'] ) )
189
+ $gateway_id = trim( $_REQUEST['mycred_call'] );
190
+
191
+ elseif ( isset( $_REQUEST['mycred_buy'] ) && is_user_logged_in() )
192
+ $gateway_id = trim( $_REQUEST['mycred_buy'] );
193
+
194
+ elseif ( isset( $_REQUEST['wp_zombaio_ips'] ) || isset( $_REQUEST['ZombaioGWPass'] ) ) {
195
+ $gateway_id = 'zombaio';
196
+ $process = true;
197
+ }
198
+
199
+ $gateway_id = apply_filters( 'mycred_gateway_id', $gateway_id );
200
+
201
+ // If we have a valid gateway ID and the gateway is active, lets run that gateway.
202
+ if ( $gateway_id !== false && array_key_exists( $gateway_id, $installed ) && $this->is_active( $gateway_id ) ) {
203
+
204
+ // Construct Gateway
205
+ $gateway = buycred_gateway( $gateway_id );
206
+
207
+ // Check payment processing
208
+ if ( isset( $_REQUEST['mycred_call'] ) || ( $gateway_id == 'zombaio' && $process ) ) {
209
+
210
+ $gateway->process();
211
+
212
+ do_action( 'mycred_buycred_process', $gateway_id, $this->gateway_prefs, $this->core->buy_creds );
213
+ do_action( "mycred_buycred_process_{$gateway_id}", $this->gateway_prefs, $this->core->buy_creds );
214
+
215
+ }
216
+
217
+ // Check purchase request
218
+ if ( isset( $_REQUEST['mycred_buy'] ) ) {
219
+
220
+ // Validate token
221
+ $token = false;
222
+ if ( isset( $_REQUEST['token'] ) && wp_verify_nonce( $_REQUEST['token'], 'mycred-buy-creds' ) )
223
+ $token = true;
224
+
225
+ // Validate amount ( amount is not zero and higher then minimum required )
226
+ $amount = false;
227
+ if ( isset( $_REQUEST['amount'] ) && is_numeric( $_REQUEST['amount'] ) && $_REQUEST['amount'] >= $this->core->buy_creds['minimum'] )
228
+ $amount = true;
229
+
230
+ if ( $token && $amount ) {
231
+
232
+ $gateway->buy();
233
+
234
+ do_action( 'mycred_buycred_buy', $gateway_id, $this->gateway_prefs, $this->core->buy_creds );
235
+ do_action( "mycred_buycred_buy_{$gateway_id}", $this->gateway_prefs, $this->core->buy_creds );
236
+
237
+ }
238
+
239
+ }
240
+
241
+ }
242
+
243
+ }
244
+
245
+ /**
246
+ * Admin Init
247
+ * @since 1.5
248
+ * @version 1.1
249
+ */
250
+ function module_admin_init() {
251
+
252
+ add_action( 'mycred_user_edit_after_balances', array( $this, 'exchange_rates_user_screen' ), 30 );
253
+
254
+ add_action( 'personal_options_update', array( $this, 'save_manual_exchange_rates' ), 30 );
255
+ add_action( 'edit_user_profile_update', array( $this, 'save_manual_exchange_rates' ), 30 );
256
+
257
+ }
258
+
259
+ /**
260
+ * Add to General Settings
261
+ * @since 0.1
262
+ * @version 1.1
263
+ */
264
+ public function after_general_settings( $mycred = NULL ) {
265
+
266
+ // Since we are both registering our own settings and want to hook into
267
+ // the core settings, we need to define our "defaults" here.
268
+ $defaults = array(
269
+ 'minimum' => 1,
270
+ 'types' => array( MYCRED_DEFAULT_TYPE_KEY ),
271
+ 'exchange' => 1,
272
+ 'log' => '%plural% purchase',
273
+ 'login' => __( 'Please login to purchase %_plural%', 'mycred' ),
274
+ 'custom_log' => 0,
275
+ 'thankyou' => array(
276
+ 'use' => 'page',
277
+ 'custom' => '',
278
+ 'page' => ''
279
+ ),
280
+ 'cancelled' => array(
281
+ 'use' => 'custom',
282
+ 'custom' => '',
283
+ 'page' => ''
284
+ ),
285
+ 'gifting' => array(
286
+ 'members' => 1,
287
+ 'authors' => 1,
288
+ 'log' => __( 'Gift purchase from %display_name%.', 'mycred' )
289
+ )
290
+ );
291
+
292
+ if ( isset( $this->core->buy_creds ) )
293
+ $buy_creds = $this->core->buy_creds;
294
+ else
295
+ $buy_creds = $defaults;
296
+
297
+ $thankyou_use = $buy_creds['thankyou']['use'];
298
+ $cancelled_use = $buy_creds['cancelled']['use'];
299
+
300
+ ?>
301
+ <h4><span class="dashicons dashicons-admin-plugins static"></span><strong>buy</strong>CRED</h4>
302
+ <div class="body" style="display:none;">
303
+ <label class="subheader"><?php echo $this->core->template_tags_general( __( 'Minimum %plural%', 'mycred' ) ); ?></label>
304
+ <ol id="mycred-buy-creds-minimum-amount">
305
+ <li>
306
+ <div class="h2"><input type="text" name="mycred_pref_core[buy_creds][minimum]" id="<?php echo $this->field_id( 'minimum' ); ?>" value="<?php echo $buy_creds['minimum']; ?>" size="5" /></div>
307
+ <span class="description"><?php echo $this->core->template_tags_general( __( 'Minimum amount of %plural% a user must purchase. Will default to 1.', 'mycred' ) ); ?></span>
308
+ </li>
309
+ </ol>
310
+ <?php if ( count( $this->point_types ) > 1 ) : ?>
311
+
312
+ <label class="subheader"><?php _e( 'Point Types', 'mycred' ); ?></label>
313
+ <ol id="mycred-buy-creds-type">
314
+ <li>
315
+ <span class="description"><?php _e( 'Select the point types that users can buy. You must select at least one!', 'mycred' ); ?></span>
316
+ </li>
317
+ <li>
318
+ <?php mycred_types_select_from_checkboxes( 'mycred_pref_core[buy_creds][types][]', $this->field_id( 'types' ), $buy_creds['types'] ); ?>
319
+ </li>
320
+ </ol>
321
+ <?php else : ?>
322
+
323
+ <input type="hidden" name="mycred_pref_core[buy_creds][types][]" value="mycred_default" />
324
+ <?php endif; ?>
325
+
326
+ <label class="subheader" for="<?php echo $this->field_id( 'login' ); ?>"><?php _e( 'Login Template', 'mycred' ); ?></label>
327
+ <ol id="mycred-buy-creds-default-log">
328
+ <li>
329
+ <input type="text" name="mycred_pref_core[buy_creds][login]" id="<?php echo $this->field_id( 'login' ); ?>" class="large-text code" value="<?php echo esc_attr( $buy_creds['login'] ); ?>" />
330
+ <span class="description"><?php _e( 'Content to show when a user is not logged in.', 'mycred' ); ?></span>
331
+ </li>
332
+ </ol>
333
+ <label class="subheader" for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
334
+ <ol id="mycred-buy-creds-default-log">
335
+ <li>
336
+ <div class="h2"><input type="text" name="mycred_pref_core[buy_creds][log]" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo $buy_creds['log']; ?>" class="long" /></div>
337
+ <span class="description"><?php echo $this->core->available_template_tags( array( 'general' ), '%gateway%' ); ?></span>
338
+ </li>
339
+ </ol>
340
+ <label class="subheader"><?php _e( 'Thank You Page', 'mycred' ); ?></label>
341
+ <ol id="mycred-buy-creds-thankyou-page">
342
+ <li class="option">
343
+ <input type="radio" name="mycred_pref_core[buy_creds][thankyou][use]" <?php checked( $thankyou_use, 'custom' ); ?> id="<?php echo $this->field_id( array( 'thankyou' => 'use' ) ); ?>-custom" value="custom" /> <label for="<?php echo $this->field_id( array( 'thankyou' => 'custom' ) ); ?>"><?php _e( 'Custom URL', 'mycred' ); ?></label><br />
344
+ <div class="h2"><?php echo get_bloginfo( 'url' ) . '/'; ?> <input type="text" name="mycred_pref_core[buy_creds][thankyou][custom]" id="<?php echo $this->field_id( array( 'thankyou' => 'custom' ) ); ?>" value="<?php echo $buy_creds['thankyou']['custom']; ?>" /></div>
345
+ </li>
346
+ <li class="empty">&nbsp;</li>
347
+ <li class="option">
348
+ <input type="radio" name="mycred_pref_core[buy_creds][thankyou][use]" <?php checked( $thankyou_use, 'page' ); ?> id="<?php echo $this->field_id( array( 'thankyou' => 'use' ) ); ?>-page" value="page" /> <label for="mycred-buy-creds-thankyou-use-page"><?php _e( 'Page', 'mycred' ); ?></label><br />
349
+ <?php
350
+
351
+ // Thank you page dropdown
352
+ $thankyou_args = array(
353
+ 'name' => 'mycred_pref_core[buy_creds][thankyou][page]',
354
+ 'id' => $this->field_id( array( 'thankyou' => 'page' ) ) . '-id',
355
+ 'selected' => $buy_creds['thankyou']['page'],
356
+ 'show_option_none' => __( 'Select', 'mycred' )
357
+ );
358
+ wp_dropdown_pages( $thankyou_args );
359
+
360
+ ?>
361
+ </li>
362
+ </ol>
363
+ <label class="subheader"><?php _e( 'Cancellation Page', 'mycred' ); ?></label>
364
+ <ol id="mycred-buy-creds-cancel-page">
365
+ <li class="option">
366
+ <input type="radio" name="mycred_pref_core[buy_creds][cancelled][use]" <?php checked( $cancelled_use, 'custom' ); ?> id="<?php echo $this->field_id( array( 'cancelled' => 'custom' ) ); ?>" value="custom" /> <label for="<?php echo $this->field_id( array( 'cancelled' => 'custom' ) ); ?>"><?php _e( 'Custom URL', 'mycred' ); ?></label><br />
367
+ <div class="h2"><?php echo get_bloginfo( 'url' ) . '/'; ?> <input type="text" name="mycred_pref_core[buy_creds][cancelled][custom]" id="mycred-buy-creds-cancelled-custom-url" value="<?php echo $buy_creds['cancelled']['custom']; ?>" /></div>
368
+ </li>
369
+ <li class="empty">&nbsp;</li>
370
+ <li class="option">
371
+ <input type="radio" name="mycred_pref_core[buy_creds][cancelled][use]" <?php checked( $cancelled_use, 'page' ); ?> id="<?php echo $this->field_id( array( 'cancelled' => 'use' ) ); ?>-page" value="page" /> <label for="<?php echo $this->field_id( array( 'cancelled' => 'use' ) ); ?>-page"><?php _e( 'Page', 'mycred' ); ?></label><br />
372
+ <?php
373
+
374
+ // Cancelled page dropdown
375
+ $cancelled_args = array(
376
+ 'name' => 'mycred_pref_core[buy_creds][cancelled][page]',
377
+ 'id' => $this->field_id( array( 'cancelled' => 'page' ) ) . '-id',
378
+ 'selected' => $buy_creds['cancelled']['page'],
379
+ 'show_option_none' => __( 'Select', 'mycred' )
380
+ );
381
+ wp_dropdown_pages( $cancelled_args );
382
+
383
+ ?>
384
+ </li>
385
+ </ol>
386
+ <label class="subheader"><?php _e( 'Purchase Log', 'mycred' ); ?></label>
387
+ <ol id="mycred-buy-creds-seperate-log">
388
+ <li><input type="checkbox" name="mycred_pref_core[buy_creds][custom_log]" id="<?php echo $this->field_id( 'custom_log' ); ?>"<?php checked( $buy_creds['custom_log'], 1 ); ?> value="1" /><label for="<?php echo $this->field_id( 'custom_log' ); ?>"><?php echo $this->core->template_tags_general( __( 'Show seperate log for %_plural% purchases.', 'mycred' ) ); ?></label></li>
389
+ </ol>
390
+ <label class="subheader"><?php _e( 'Gifting', 'mycred' ); ?></label>
391
+ <ol id="mycred-buy-creds-gifting">
392
+ <li><input type="checkbox" name="mycred_pref_core[buy_creds][gifting][members]" id="<?php echo $this->field_id( array( 'gifting' => 'members' ) ); ?>"<?php checked( $buy_creds['gifting']['members'], 1 ); ?> value="1" /><label for="<?php echo $this->field_id( array( 'gifting' => 'members' ) ); ?>"><?php echo $this->core->template_tags_general( __( 'Allow users to buy %_plural% for other users.', 'mycred' ) ); ?></label></li>
393
+ <li><input type="checkbox" name="mycred_pref_core[buy_creds][gifting][authors]" id="<?php echo $this->field_id( array( 'gifting' => 'authors' ) ); ?>"<?php checked( $buy_creds['gifting']['authors'], 1 ); ?> value="1" /><label for="<?php echo $this->field_id( array( 'gifting' => 'authors' ) ); ?>"><?php echo $this->core->template_tags_general( __( 'Allow users to buy %_plural% for content authors.', 'mycred' ) ); ?></label></li>
394
+ <li class="empty">&nbsp;</li>
395
+ <li>
396
+ <label for="<?php echo $this->field_id( array( 'gifting' => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
397
+ <div class="h2"><input type="text" name="mycred_pref_core[buy_creds][gifting][log]" id="<?php echo $this->field_id( array( 'gifting' => 'log' ) ); ?>" value="<?php echo $buy_creds['gifting']['log']; ?>" class="long" /></div>
398
+ <span class="description"><?php echo $this->core->available_template_tags( array( 'general', 'user' ) ); ?></span>
399
+ </li>
400
+ </ol>
401
+ <label class="subheader"><?php _e( 'Available Shortcodes', 'mycred' ); ?></label>
402
+ <ol id="mycred-buy-creds-shortcodes">
403
+ <li>
404
+ <a href="http://codex.mycred.me/shortcodes/mycred_buy/" target="_blank">[mycred_buy]</a> ,
405
+ <a href="http://codex.mycred.me/shortcodes/mycred_buy_form/" target="_blank">[mycred_buy_form]</a> ,
406
+ <a href="http://codex.mycred.me/shortcodes/mycred_buy_pending/" target="_blank">[mycred_buy_pending]</a>
407
+ </li>
408
+ </ol>
409
+ </div>
410
+ <?php
411
+
412
+ }
413
+
414
+ /**
415
+ * Save Settings
416
+ * @since 0.1
417
+ * @version 1.1.1
418
+ */
419
+ public function sanitize_extra_settings( $new_data, $data, $core ) {
420
+
421
+ if ( ! isset( $data['buy_creds']['types'] ) )
422
+ $new_data['buy_creds']['types'] = array( MYCRED_DEFAULT_TYPE_KEY );
423
+ else
424
+ $new_data['buy_creds']['types'] = $data['buy_creds']['types'];
425
+
426
+ $new_data['buy_creds']['minimum'] = abs( $data['buy_creds']['minimum'] );
427
+ $new_data['buy_creds']['log'] = sanitize_text_field( $data['buy_creds']['log'] );
428
+ $new_data['buy_creds']['login'] = wp_kses_post( $data['buy_creds']['login'] );
429
+
430
+ $new_data['buy_creds']['thankyou']['use'] = sanitize_text_field( $data['buy_creds']['thankyou']['use'] );
431
+ $new_data['buy_creds']['thankyou']['custom'] = sanitize_text_field( $data['buy_creds']['thankyou']['custom'] );
432
+ $new_data['buy_creds']['thankyou']['page'] = absint( $data['buy_creds']['thankyou']['page'] );
433
+
434
+ $new_data['buy_creds']['cancelled']['use'] = sanitize_text_field( $data['buy_creds']['cancelled']['use'] );
435
+ $new_data['buy_creds']['cancelled']['custom'] = sanitize_text_field( $data['buy_creds']['cancelled']['custom'] );
436
+ $new_data['buy_creds']['cancelled']['page'] = absint( $data['buy_creds']['cancelled']['page'] );
437
+
438
+ $new_data['buy_creds']['custom_log'] = ( ! isset( $data['buy_creds']['custom_log'] ) ) ? 0 : 1;
439
+
440
+ $new_data['buy_creds']['gifting']['members'] = ( ! isset( $data['buy_creds']['gifting']['members'] ) ) ? 0 : 1;
441
+ $new_data['buy_creds']['gifting']['authors'] = ( ! isset( $data['buy_creds']['gifting']['authors'] ) ) ? 0 : 1;
442
+ $new_data['buy_creds']['gifting']['log'] = sanitize_text_field( $data['buy_creds']['gifting']['log'] );
443
+
444
+ delete_option( 'mycred_buycred_reset' );
445
+
446
+ return $new_data;
447
+
448
+ }
449
+
450
+ /**
451
+ * Render Gift Tags
452
+ * @since 1.4.1
453
+ * @version 1.0
454
+ */
455
+ public function render_gift_tags( $content, $log ) {
456
+
457
+ if ( substr( $log->ref, 0, 15 ) != 'buy_creds_with_' ) return $content;
458
+ return $this->core->template_tags_user( $content, absint( $log->ref_id ) );
459
+
460
+ }
461
+
462
+ /**
463
+ * Add Admin Menu Item
464
+ * @since 0.1
465
+ * @version 1.1
466
+ */
467
+ public function add_to_menu() {
468
+
469
+ if ( isset( $this->core->buy_creds['custom_log'] ) && $this->core->buy_creds['custom_log'] ) {
470
+
471
+ // Menu Slug
472
+ $menu_slug = 'mycred';
473
+
474
+ $types = array( MYCRED_DEFAULT_TYPE_KEY );
475
+ if ( isset( $this->core->buy_creds['types'] ) )
476
+ $types = $this->core->buy_creds['types'];
477
+
478
+ $pages = array();
479
+ foreach ( $types as $type ) {
480
+
481
+ $mycred = mycred( $type );
482
+
483
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY )
484
+ $menu_slug .= '_' . $type;
485
+
486
+ $pages[] = add_submenu_page(
487
+ $menu_slug,
488
+ __( 'buyCRED Purchase Log', 'mycred' ),
489
+ __( 'Purchase Log', 'mycred' ),
490
+ $mycred->edit_plugin_cap(),
491
+ MYCRED_SLUG . '-purchases-' . $type,
492
+ array( $this, 'purchase_log_page' )
493
+ );
494
+
495
+ }
496
+
497
+ foreach ( $pages as $page ) {
498
+
499
+ add_action( 'admin_print_styles-' . $page, array( $this, 'settings_page_enqueue' ) );
500
+ add_action( 'load-' . $page, array( $this, 'screen_options' ) );
501
+
502
+ }
503
+
504
+ $this->purchase_log = $pages;
505
+
506
+ }
507
+
508
+ }
509
+
510
+ /**
511
+ * Page Header
512
+ * @since 1.3
513
+ * @version 1.1
514
+ */
515
+ public function settings_header() {
516
+
517
+ }
518
+
519
+ /**
520
+ * Payment Gateways Page
521
+ * @since 0.1
522
+ * @version 1.2.1
523
+ */
524
+ public function admin_page() {
525
+
526
+ // Security
527
+ if ( ! $this->core->can_edit_creds() ) wp_die( 'Access Denied' );
528
+
529
+ $installed = $this->get();
530
+
531
+ ?>
532
+ <div class="wrap list" id="myCRED-wrap">
533
+ <h1><?php printf( __( '%s Payment Gateways', 'mycred' ), '<strong>buy</strong>CRED' ); ?> <a href="<?php echo esc_url( add_query_arg( array( 'page' => MYCRED_SLUG . '-settings', 'open-tab' => 'buycred_module' ), admin_url( 'admin.php' ) ) ); ?>" class="page-title-action"><?php _e( 'Settings', 'mycred' ); ?></a></h1>
534
+ <?php
535
+
536
+ // Updated settings
537
+ if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true )
538
+ echo '<div class="updated settings-error"><p>' . __( 'Settings Updated', 'mycred' ) . '</p></div>';
539
+
540
+ ?>
541
+ <form method="post" action="options.php">
542
+
543
+ <?php settings_fields( $this->settings_name ); ?>
544
+
545
+ <?php do_action( 'mycred_before_buycreds_page', $this ); ?>
546
+
547
+ <div class="list-items expandable-li" id="accordion">
548
+ <?php
549
+
550
+ if ( ! empty( $installed ) ) {
551
+ foreach ( $installed as $key => $data ) {
552
+
553
+ if ( ! array_key_exists( 'icon', $data ) )
554
+ $data['icon'] = 'dashicons-admin-plugins';
555
+
556
+ ?>
557
+ <h4><span class="dashicons <?php echo $data['icon']; ?><?php if ( $this->is_active( $key ) ) { if ( array_key_exists( 'sandbox', $this->gateway_prefs[ $key ] ) && $this->gateway_prefs[ $key ]['sandbox'] === 1 ) echo ' debug'; else echo ' active'; } else echo ' static'; ?>"></span><?php echo $this->core->template_tags_general( $data['title'] ); ?></h4>
558
+ <div class="body" style="display: none;">
559
+ <label class="subheader"><?php _e( 'Enable', 'mycred' ); ?></label>
560
+ <ol>
561
+ <li>
562
+ <input type="checkbox" name="mycred_pref_buycreds[active][]" id="mycred-gateway-<?php echo $key; ?>" value="<?php echo $key; ?>"<?php if ( $this->is_active( $key ) ) echo ' checked="checked"'; ?> />
563
+ </li>
564
+ </ol>
565
+
566
+ <?php if ( array_key_exists( 'sandbox', $this->gateway_prefs[ $key ] ) ) : ?>
567
+
568
+ <label class="subheader" for="mycred-gateway-<?php echo $key; ?>-sandbox"><?php _e( 'Sandbox Mode', 'mycred' ); ?></label>
569
+ <ol>
570
+ <li>
571
+ <input type="checkbox" name="mycred_pref_buycreds[gateway_prefs][<?php echo $key; ?>][sandbox]" id="mycred-gateway-<?php echo $key; ?>-sandbox" value="1"<?php checked( $this->gateway_prefs[ $key ]['sandbox'], 1 ); ?> /> <span class="description"><?php _e( 'Enable for test purchases.', 'mycred' ); ?></span>
572
+ </li>
573
+ </ol>
574
+
575
+ <?php endif; ?>
576
+
577
+ <?php $this->call( 'preferences', $data['callback'] ); ?>
578
+
579
+ <input type="hidden" name="mycred_pref_buycreds[installed]" value="<?php echo $key; ?>" />
580
+ </div>
581
+ <?php
582
+
583
+ }
584
+ }
585
+
586
+ ?>
587
+ </div>
588
+
589
+ <?php do_action( 'mycred_after_buycreds_page', $this ); ?>
590
+
591
+ <p><?php submit_button( __( 'Update Settings', 'mycred' ), 'primary large', 'submit', false ); ?> <a href="http://mycred.me/product-category/buycred-gateways/" class="button button-secondary button-large" target="_blank"><?php _e( 'More Gateways', 'mycred' ); ?></a></p>
592
+
593
+ </form>
594
+
595
+ <?php do_action( 'mycred_bottom_buycreds_page', $this ); ?>
596
+
597
+ <script type="text/javascript">
598
+ jQuery(function($) {
599
+ $( 'select.currency' ).change(function(){
600
+ var target = $(this).attr( 'data-update' );
601
+ $( '.' + target ).empty();
602
+ $( '.' + target ).text( $(this).val() );
603
+ });
604
+ });
605
+ </script>
606
+ </div>
607
+ <?php
608
+
609
+ }
610
+
611
+ /**
612
+ * Sanititze Settings
613
+ * @since 0.1
614
+ * @version 1.3
615
+ */
616
+ public function sanitize_settings( $data ) {
617
+
618
+ $data = apply_filters( 'mycred_buycred_save_prefs', $data );
619
+ $installed = $this->get();
620
+
621
+ if ( empty( $installed ) ) return $data;
622
+
623
+ foreach ( $installed as $id => $gdata )
624
+ $data['gateway_prefs'][ $id ] = $this->call( 'sanitise_preferences', $installed[ $id ]['callback'], $data['gateway_prefs'][ $id ] );
625
+
626
+ return $data;
627
+
628
+ }
629
+
630
+ /**
631
+ * Purchase Log Screen Options
632
+ * @since 1.4
633
+ * @version 1.1
634
+ */
635
+ public function screen_options() {
636
+
637
+ if ( empty( $this->purchase_log ) ) return;
638
+
639
+ $meta_key = 'mycred_payments_' . str_replace( MYCRED_SLUG . '-purchases-', '', $_GET['page'] );
640
+
641
+ if ( isset( $_REQUEST['wp_screen_options']['option'] ) && isset( $_REQUEST['wp_screen_options']['value'] ) ) {
642
+
643
+ if ( $_REQUEST['wp_screen_options']['option'] == $meta_key ) {
644
+ $value = absint( $_REQUEST['wp_screen_options']['value'] );
645
+ update_user_meta( $this->current_user_id, $meta_key, $value );
646
+ }
647
+
648
+ }
649
+
650
+ $args = array(
651
+ 'label' => __( 'Payments', 'mycred' ),
652
+ 'default' => 10,
653
+ 'option' => $meta_key
654
+ );
655
+ add_screen_option( 'per_page', $args );
656
+
657
+ }
658
+
659
+ /**
660
+ * Purchase Log
661
+ * Render the dedicated admin screen where all point purchases are shown from the myCRED Log.
662
+ * This screen is added in for each point type that is set to be for sale.
663
+ * @since 1.4
664
+ * @version 1.4
665
+ */
666
+ public function purchase_log_page() {
667
+
668
+ $type = str_replace( 'mycred-purchases-', '', $_GET['page'] );
669
+ $installed = $this->get();
670
+
671
+ if ( $type == MYCRED_DEFAULT_TYPE_KEY )
672
+ $mycred = $this->core;
673
+ else
674
+ $mycred = mycred( $type );
675
+
676
+ // Security
677
+ if ( ! $mycred->can_edit_creds() ) wp_die( 'Access Denied' );
678
+
679
+ $per_page = mycred_get_user_meta( $this->current_user_id, 'mycred_payments_' . $type, '', true );
680
+ if ( empty( $per_page ) || $per_page < 1 ) $per_page = 10;
681
+
682
+ // Get references
683
+ $references = apply_filters( 'mycred_buycred_log_refs', array(
684
+ 'buy_creds_with_paypal_standard',
685
+ 'buy_creds_with_skrill',
686
+ 'buy_creds_with_zombaio',
687
+ 'buy_creds_with_netbilling',
688
+ 'buy_creds_with_bitpay'
689
+ ), $this, $type );
690
+
691
+ // Prep
692
+ $args = array(
693
+ 'number' => $per_page,
694
+ 'ctype' => $type,
695
+ 'ref' => implode( ',', $references )
696
+ );
697
+
698
+ if ( isset( $_GET['user_id'] ) && ! empty( $_GET['user_id'] ) )
699
+ $args['user_id'] = $_GET['user_id'];
700
+
701
+ if ( isset( $_GET['s'] ) && ! empty( $_GET['s'] ) )
702
+ $args['s'] = $_GET['s'];
703
+
704
+ if ( isset( $_GET['ref'] ) && ! empty( $_GET['ref'] ) )
705
+ $args['ref'] = $_GET['ref'];
706
+
707
+ if ( isset( $_GET['show'] ) && ! empty( $_GET['show'] ) )
708
+ $args['time'] = $_GET['show'];
709
+
710
+ if ( isset( $_GET['order'] ) && ! empty( $_GET['order'] ) )
711
+ $args['order'] = $_GET['order'];
712
+
713
+ if ( isset( $_GET['paged'] ) && ! empty( $_GET['paged'] ) )
714
+ $args['paged'] = $_GET['paged'];
715
+
716
+ if ( isset( $_GET['start'] ) && isset( $_GET['end'] ) )
717
+ $args['amount'] = array( 'start' => $_GET['start'], 'end' => $_GET['end'] );
718
+
719
+ elseif ( isset( $_GET['num'] ) && isset( $_GET['compare'] ) )
720
+ $args['amount'] = array( 'num' => $_GET['num'], 'compare' => $_GET['compare'] );
721
+
722
+ elseif ( isset( $_GET['amount'] ) )
723
+ $args['amount'] = $_GET['amount'];
724
+
725
+ $log = new myCRED_Query_Log( $args );
726
+
727
+ $log->headers = apply_filters( 'mycred_buycred_log_columns', array(
728
+ 'column-gateway' => __( 'Gateway', 'mycred' ),
729
+ 'column-username' => __( 'Buyer', 'mycred' ),
730
+ 'column-date' => __( 'Date', 'mycred' ),
731
+ 'column-amount' => $mycred->plural(),
732
+ 'column-payed' => __( 'Payed', 'mycred' ),
733
+ 'column-tranid' => __( 'Transaction ID', 'mycred' )
734
+ ) );
735
+
736
+ $filter_url = admin_url( 'admin.php?page=' . MYCRED_SLUG . '-purchases-' . $type );
737
+
738
+ ?>
739
+ <div class="wrap list" id="myCRED-wrap">
740
+ <h1><?php _e( 'Purchase Log', 'mycred' ); ?></h1>
741
+
742
+ <?php $log->filter_dates( esc_url( $filter_url ) ); ?>
743
+
744
+ <div class="clear"></div>
745
+ <form method="get" action="">
746
+ <?php
747
+
748
+ if ( isset( $_GET['user_id'] ) && ! empty( $_GET['user_id'] ) )
749
+ echo '<input type="hidden" name="user_id" value="' . esc_attr( $_GET['user_id'] ) . '" />';
750
+
751
+ if ( isset( $_GET['s'] ) && ! empty( $_GET['s'] ) )
752
+ echo '<input type="hidden" name="s" value="' . esc_attr( $_GET['s'] ) . '" />';
753
+
754
+ if ( isset( $_GET['ref'] ) && ! empty( $_GET['ref'] ) )
755
+ echo '<input type="hidden" name="ref" value="' . esc_attr( $_GET['ref'] ) . '" />';
756
+
757
+ if ( isset( $_GET['show'] ) && ! empty( $_GET['show'] ) )
758
+ echo '<input type="hidden" name="show" value="' . esc_attr( $_GET['show'] ) . '" />';
759
+
760
+ if ( isset( $_GET['order'] ) && ! empty( $_GET['order'] ) )
761
+ echo '<input type="hidden" name="order" value="' . esc_attr( $_GET['order'] ) . '" />';
762
+
763
+ if ( isset( $_GET['paged'] ) && ! empty( $_GET['paged'] ) )
764
+ echo '<input type="hidden" name="paged" value="' . esc_attr( $_GET['paged'] ) . '" />';
765
+
766
+ $log->search();
767
+
768
+ ?>
769
+ <input type="hidden" name="page" value="<?php echo esc_attr( $_GET['page'] ); ?>" />
770
+
771
+ <?php do_action( 'mycred_above_payment_log_table', $this ); ?>
772
+
773
+ <div class="tablenav top">
774
+
775
+ <?php $log->table_nav( 'top' ); ?>
776
+
777
+ </div>
778
+ <table class="table wp-list-table widefat mycred-table log-entries" cellspacing="0">
779
+ <thead>
780
+ <tr>
781
+ <?php
782
+
783
+ foreach ( $log->headers as $col_id => $col_title )
784
+ echo '<th scope="col" id="' . str_replace( 'column-', '', $col_id ) . '" class="manage-column ' . $col_id . '">' . $col_title . '</th>';
785
+
786
+ ?>
787
+ </tr>
788
+ </thead>
789
+ <tfoot>
790
+ <tr>
791
+ <?php
792
+
793
+ foreach ( $log->headers as $col_id => $col_title )
794
+ echo '<th scope="col" class="manage-column ' . $col_id . '">' . $col_title . '</th>';
795
+
796
+ ?>
797
+ </tr>
798
+ </tfoot>
799
+ <tbody id="the-list">
800
+ <?php
801
+
802
+ // If we have results
803
+ if ( $log->have_entries() ) {
804
+
805
+ // Prep
806
+ $date_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
807
+ $entry_data = '';
808
+ $alt = 0;
809
+
810
+ // Loop results
811
+ foreach ( $log->results as $log_entry ) {
812
+
813
+ // Highlight alternate rows
814
+ $alt = $alt+1;
815
+ if ( $alt % 2 == 0 )
816
+ $class = ' alt';
817
+ else
818
+ $class = '';
819
+
820
+ // Prep Sales data for use in columns
821
+ $sales_data = $this->get_sales_data_from_log_data( $log_entry->data );
822
+ list ( $buyer_id, $payer_id, $amount, $cost, $currency, $token, $other ) = $sales_data;
823
+
824
+ // Default Currency
825
+ if ( empty( $currency ) )
826
+ $currency = 'USD';
827
+
828
+ $gateway_name = str_replace( 'buy_creds_with_', '', $log_entry->ref );
829
+
830
+ if ( ! array_key_exists( str_replace( '_', '-', $gateway_name ), $installed ) )
831
+ $style = ' style="color:silver;"';
832
+ elseif ( ! $this->is_active( str_replace( '_', '-', $gateway_name ) ) )
833
+ $style = ' style="color:gray;"';
834
+ elseif ( substr( $log_entry->entry, 0, 4 ) == 'TEST' )
835
+ $style = ' style="color:orange;"';
836
+ else
837
+ $style = '';
838
+
839
+ echo '<tr class="myCRED-log-row' . $class . '" id="mycred-log-entry-' . $log_entry->id . '">';
840
+
841
+ // Run though columns
842
+ foreach ( $log->headers as $column_id => $column_name ) {
843
+
844
+ echo '<td class="' . $column_id . '"' . $style . '>';
845
+
846
+ switch ( $column_id ) {
847
+
848
+ // Used gateway
849
+ case 'column-gateway' :
850
+
851
+ $gateway = str_replace( array( '-', '_' ), ' ', $gateway_name );
852
+ echo ucwords( $gateway );
853
+
854
+ break;
855
+
856
+ // Username Column
857
+ case 'column-username' :
858
+
859
+ $user = get_userdata( $log_entry->user_id );
860
+ if ( $user === false )
861
+ echo 'ID: ' . $log_entry->user_id;
862
+ else
863
+ echo $user->display_name . ' <em><small>(ID: ' . $log_entry->user_id . ')</small></em>';
864
+
865
+ break;
866
+
867
+ // Date & Time Column
868
+ case 'column-date' :
869
+
870
+ echo date( $date_format, $log_entry->time );
871
+
872
+ break;
873
+
874
+ // Amount Column
875
+ case 'column-amount' :
876
+
877
+ echo $mycred->format_creds( $log_entry->creds );
878
+
879
+ break;
880
+
881
+ // Amount Paid
882
+ case 'column-payed' :
883
+
884
+ $cost = 'n/a';
885
+ $currency = '';
886
+ $data = maybe_unserialize( $log_entry->data );
887
+ if ( is_array( $data ) && array_key_exists( 'sales_data', $data ) ) {
888
+
889
+ $sales_data = explode( '|', $data['sales_data'] );
890
+ if ( count( $sales_data ) >= 5 ) {
891
+ $cost = $sales_data[3];
892
+ $currency = $sales_data[4];
893
+ }
894
+
895
+ }
896
+
897
+ if ( $cost === 'n/a' )
898
+ echo 'n/a';
899
+ else
900
+ echo number_format( $cost, 2 ) . ' ' . $currency;
901
+
902
+ break;
903
+
904
+ // Transaction ID
905
+ case 'column-tranid' :
906
+
907
+ $transaction_id = $log_entry->time . $log_entry->user_id;
908
+ $saved_data = maybe_unserialize( $log_entry->data );
909
+ if ( isset( $saved_data['txn_id'] ) )
910
+ $transaction_id = $saved_data['txn_id'];
911
+ elseif ( isset( $saved_data['transaction_id'] ) )
912
+ $transaction_id = $saved_data['transaction_id'];
913
+
914
+ echo $transaction_id;
915
+
916
+ break;
917
+
918
+ default :
919
+
920
+ do_action( "mycred_payment_log_{$column_id}", $log_entry );
921
+ do_action( "mycred_payment_log_{$column_id}_{$type}", $log_entry );
922
+
923
+ break;
924
+
925
+ }
926
+
927
+ echo '</td>';
928
+
929
+ }
930
+
931
+ echo '</tr>';
932
+
933
+ }
934
+
935
+ }
936
+
937
+ // No log entry
938
+ else {
939
+
940
+ echo '<tr><td colspan="' . count( $log->headers ) . '" class="no-entries">' . __( 'No purchases found', 'mycred' ) . '</td></tr>';
941
+
942
+ }
943
+
944
+ ?>
945
+ </tbody>
946
+ </table>
947
+ <div class="tablenav bottom">
948
+
949
+ <?php $log->table_nav( 'bottom' ); ?>
950
+
951
+ </div>
952
+
953
+ <?php do_action( 'mycred_below_payment_log_table', $this ); ?>
954
+
955
+ </form>
956
+ </div>
957
+ <?php
958
+
959
+ }
960
+
961
+ /**
962
+ * Get Sales Data from Log Data
963
+ * @since 1.4
964
+ * @version 1.0.1
965
+ */
966
+ public function get_sales_data_from_log_data( $log_data = '' ) {
967
+
968
+ $defaults = array( '', '', '', '', '', '', '' );
969
+ $log_data = maybe_unserialize( $log_data );
970
+
971
+ $found_data = array();
972
+ if ( is_array( $log_data ) && array_key_exists( 'sales_data', $log_data ) ) {
973
+ if ( is_array( $log_data['sales_data'] ) )
974
+ $found_data = $log_data['sales_data'];
975
+ else
976
+ $found_data = explode( '|', $log_data['sales_data'] );
977
+ }
978
+ elseif ( ! empty( $log_data ) && ! is_array( $log_data ) ) {
979
+ $try = explode( '|', $log_data );
980
+ if ( count( $try == 7 ) )
981
+ $found_data = $log_data;
982
+ }
983
+
984
+ return wp_parse_args( $found_data, $defaults );
985
+
986
+ }
987
+
988
+ /**
989
+ * User Rates Admin Screen
990
+ * @since 1.5
991
+ * @version 1.0
992
+ */
993
+ public function exchange_rates_user_screen( $user ) {
994
+
995
+ // Only visible to admins
996
+ if ( ! mycred_is_admin() ) return;
997
+
998
+ $type = MYCRED_DEFAULT_TYPE_KEY;
999
+
1000
+ $types = array( MYCRED_DEFAULT_TYPE_KEY );
1001
+ if ( isset( $this->core->buy_creds['types'] ) )
1002
+ $types = $this->core->buy_creds['types'];
1003
+
1004
+ $users_saved_overrides = (array) mycred_get_user_meta( $user->ID, 'mycred_buycred_rates_' . $type, '', true );
1005
+ $installed = $this->get();
1006
+
1007
+ $usable = array();
1008
+ foreach ( $installed as $gateway_id => $prefs ) {
1009
+
1010
+ if ( ! $this->is_active( $gateway_id ) || ! isset( $this->gateway_prefs[ $gateway_id ] ) ) continue;
1011
+
1012
+ $gateway_prefs = $this->gateway_prefs[ $gateway_id ];
1013
+
1014
+ if ( ! isset( $gateway_prefs['exchange'][ $type ] ) || ( isset( $gateway_prefs['currency'] ) && $gateway_prefs['currency'] == '' ) ) continue;
1015
+
1016
+ $usable[ $gateway_id ] = $gateway_prefs;
1017
+ $usable[ $gateway_id ]['title'] = $prefs['title'];
1018
+
1019
+ if ( ! isset( $users_saved_overrides[ $gateway_id ] ) )
1020
+ $users_saved_overrides[ $gateway_id ] = '';
1021
+
1022
+ }
1023
+
1024
+ if ( empty( $usable ) ) return;
1025
+
1026
+ $mycred = mycred( $type );
1027
+
1028
+ ?>
1029
+ <h2><?php _e( 'buyCRED Exchange Rates', 'mycred' ); ?></h2>
1030
+ <table class="form-table buycred-table">
1031
+ <?php
1032
+
1033
+ foreach ( $usable as $gateway_id => $gateway_prefs ) :
1034
+
1035
+ if ( ! isset( $gateway_prefs['currency'] ) )
1036
+ $gateway_prefs['currency'] = 'USD';
1037
+
1038
+ ?>
1039
+ <tr>
1040
+ <th scope="row"><?php echo esc_attr( $gateway_prefs['title'] ); ?></th>
1041
+ <td>
1042
+ <fieldset id="mycred-buycred-list" class="buycred-list">
1043
+ <legend class="screen-reader-text"><span><?php _e( 'buyCRED Exchange Rates', 'mycred' ); ?></span></legend>
1044
+ <?php
1045
+
1046
+ foreach ( $types as $type_id ) {
1047
+
1048
+ $placeholder = '';
1049
+ if ( isset( $this->gateway_prefs[ $gateway_id ]['exchange'][ $type_id ] ) )
1050
+ $placeholder = $this->gateway_prefs[ $gateway_id ]['exchange'][ $type_id ];
1051
+
1052
+ $value = '';
1053
+ if ( array_key_exists( $gateway_id, $users_saved_overrides ) )
1054
+ $value = $users_saved_overrides[ $gateway_id ];
1055
+
1056
+ ?>
1057
+ <div class="buycred-wrapper color-option<?php if ( $value != '' ) echo ' selected'; ?>">
1058
+ <p><label>1 <?php echo $mycred->singular(); ?> = <input type="text" name="mycred_adjust_users_buyrates[<?php echo $type_id; ?>][<?php echo $gateway_id; ?>]" placeholder="<?php echo $placeholder; ?>" value="<?php echo $value; ?>" class="trigger-buycred" size="8" /> <span><?php echo esc_attr( $gateway_prefs['currency'] ); ?></span></label></p>
1059
+ <span class="description"><?php _e( 'Leave empty to use the default value.', 'mycred' ); ?></span>
1060
+ </div>
1061
+ <?php
1062
+
1063
+ }
1064
+
1065
+ ?>
1066
+ </fieldset>
1067
+ </td>
1068
+ </tr>
1069
+ <?php
1070
+
1071
+ endforeach;
1072
+
1073
+ ?>
1074
+ </table>
1075
+ <script type="text/javascript">
1076
+ jQuery(function($) {
1077
+
1078
+ $( '.buycred-wrapper label input.trigger-buycred' ).change(function(){
1079
+
1080
+ if ( $(this).val().length > 0 )
1081
+ $(this).parent().parent().parent().addClass( 'selected' );
1082
+
1083
+ else
1084
+ $(this).parent().parent().parent().removeClass( 'selected' );
1085
+
1086
+ });
1087
+
1088
+ });
1089
+ </script>
1090
+ <?php
1091
+
1092
+ }
1093
+
1094
+ /**
1095
+ * Save Override
1096
+ * @since 1.5
1097
+ * @version 1.2
1098
+ */
1099
+ public function save_manual_exchange_rates( $user_id ) {
1100
+
1101
+ if ( ! mycred_is_admin() ) return;
1102
+
1103
+ if ( isset( $_POST['mycred_adjust_users_buyrates'] ) && is_array( $_POST['mycred_adjust_users_buyrates'] ) && ! empty( $_POST['mycred_adjust_users_buyrates'] ) ) {
1104
+
1105
+ foreach ( $_POST['mycred_adjust_users_buyrates'] as $ctype => $gateway ) {
1106
+
1107
+ $ctype = sanitize_key( $ctype );
1108
+ $mycred = mycred( $ctype );
1109
+
1110
+ if ( ! $mycred->exclude_user( $user_id ) ) {
1111
+
1112
+ $new_rates = array();
1113
+ foreach ( (array) $gateway as $gateway_id => $rate ) {
1114
+
1115
+ if ( $rate == '' ) continue;
1116
+
1117
+ if ( $rate != 1 && in_array( substr( $rate, 0, 1 ), array( '.', ',' ) ) )
1118
+ $rate = (float) '0' . $rate;
1119
+
1120
+ $new_rates[ $gateway_id ] = $rate;
1121
+
1122
+ }
1123
+
1124
+ if ( ! empty( $new_rates ) )
1125
+ mycred_update_user_meta( $user_id, 'mycred_buycred_rates_' . $ctype, '', $new_rates );
1126
+ else
1127
+ mycred_delete_user_meta( $user_id, 'mycred_buycred_rates_' . $ctype );
1128
+
1129
+ }
1130
+
1131
+ }
1132
+
1133
+ }
1134
+
1135
+ }
1136
+
1137
+ /**
1138
+ * Render Shortcode Basic
1139
+ * This shortcode returns a link element to a specified payment gateway.
1140
+ * @since 0.1
1141
+ * @version 1.6
1142
+ */
1143
+ public function render_shortcode_basic( $atts, $title = '' ) {
1144
+
1145
+ // Make sure the add-on has been setup
1146
+ if ( ! isset( $this->core->buy_creds ) ) {
1147
+ if ( mycred_is_admin() )
1148
+ return '<p style="color:red;"><a href="' . $this->get_settings_url( 'buycred_module' ) . '">This Add-on needs to setup before you can use this shortcode.</a></p>';
1149
+ else
1150
+ return '';
1151
+ }
1152
+
1153
+ extract( shortcode_atts( array(
1154
+ 'gateway' => '',
1155
+ 'ctype' => MYCRED_DEFAULT_TYPE_KEY,
1156
+ 'amount' => '',
1157
+ 'gift_to' => '',
1158
+ 'class' => 'mycred-buy-link btn btn-primary btn-lg',
1159
+ 'login' => $this->core->template_tags_general( $this->core->buy_creds['login'] )
1160
+ ), $atts ) );
1161
+
1162
+ // Make sure we are trying to sell a point type that is allowed to be purchased
1163
+ if ( ! in_array( $ctype, $this->core->buy_creds['types'] ) )
1164
+ $ctype = $this->core->buy_creds['types'][0];
1165
+
1166
+ if ( $ctype == $this->core->cred_id )
1167
+ $mycred = $this->core;
1168
+ else
1169
+ $mycred = mycred( $ctype );
1170
+
1171
+ global $post;
1172
+
1173
+ // If we are not logged in
1174
+ if ( $this->current_user_id == 0 ) return '<div class="mycred-buy login">' . $mycred->template_tags_general( $login ) . '</div>';
1175
+
1176
+ // Arguments
1177
+ $args = array( 'ctype' => $ctype, 'token' => wp_create_nonce( 'mycred-buy-creds' ) );
1178
+
1179
+ // Gateways
1180
+ $installed = $this->get();
1181
+
1182
+ if ( empty( $installed ) ) return 'No gateways installed.';
1183
+ elseif ( ! empty( $gateway ) && ! array_key_exists( $gateway, $installed ) ) return 'Gateway does not exist.';
1184
+ elseif ( empty( $this->active ) ) return 'No active gateways found.';
1185
+
1186
+ // If no gateway is set, chooce the first one that is active
1187
+ if ( empty( $gateway ) || ! array_key_exists( $gateway, $installed ) ) {
1188
+
1189
+ $gateway_to_use = false;
1190
+ foreach ( $installed as $gateway_id => $info ) {
1191
+ if ( ! $this->is_active( $gateway_id ) ) continue;
1192
+ $gateway_to_use = $gateway_id;
1193
+ break;
1194
+ }
1195
+
1196
+ // Doubt this should happen if we come this far but you can never be sure
1197
+ if ( $gateway_to_use === false ) return 'No active gateways found.';
1198
+
1199
+ $gateway = $gateway_to_use;
1200
+
1201
+ }
1202
+
1203
+ $args['mycred_buy'] = $gateway;
1204
+ $classes[] = $gateway;
1205
+
1206
+ // Prep
1207
+ $buyer_id = $this->current_user_id;
1208
+ $recipient_id = $buyer_id;
1209
+
1210
+ if ( $this->core->buy_creds['gifting']['authors'] && $gift_to == 'author' )
1211
+ $recipient_id = $post->post_author;
1212
+
1213
+ if ( $this->core->buy_creds['gifting']['members'] && absint( $gift_to ) !== 0 )
1214
+ $recipient_id = absint( $gift_to );
1215
+
1216
+ if ( $recipient_id !== $buyer_id )
1217
+ $args['gift_to'] = $recipient_id;
1218
+
1219
+ // Allow user related template tags to be used in the button label
1220
+ $title = $mycred->template_tags_general( $title );
1221
+ $title = $mycred->template_tags_user( $title, $recipient_id );
1222
+
1223
+ // Amount
1224
+ $minimum = $mycred->number( $this->core->buy_creds['minimum'] );
1225
+ $amount = $mycred->number( $amount );
1226
+
1227
+ // Enfoce the minimum we set in our buyCRED settings
1228
+ if ( $amount < $minimum )
1229
+ $amount = $minimum;
1230
+
1231
+ $args['amount'] = $amount;
1232
+
1233
+ // Let others add items to the arguments
1234
+ $args = apply_filters( 'mycred_buy_args', $args, $atts, $mycred );
1235
+
1236
+ // Classes
1237
+ $classes = explode( ' ', $class );
1238
+
1239
+ if ( empty( $classes ) )
1240
+ $classes[] = 'mycred-buy-link';
1241
+
1242
+ $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
1243
+
1244
+ // Construct anchor element to take us to the checkout page
1245
+ return '<a href="' . esc_url( add_query_arg( $args, $current_url ) ) . '" class="' . implode( ' ', $classes ) . '" title="' . esc_attr( strip_tags( $title ) ) . '">' . do_shortcode( $title ) . '</a>';
1246
+
1247
+ }
1248
+
1249
+ /**
1250
+ * Render Shortcode Form
1251
+ * Returns an advanced version allowing for further customizations.
1252
+ * @since 0.1
1253
+ * @version 1.6
1254
+ */
1255
+ public function render_shortcode_form( $atts, $content = '' ) {
1256
+
1257
+ // Make sure the add-on has been setup
1258
+ if ( ! isset( $this->core->buy_creds ) ) {
1259
+ if ( mycred_is_admin() )
1260
+ return '<p style="color:red;"><a href="' . $this->get_settings_url( 'buycred_module' ) . '">This Add-on needs to setup before you can use this shortcode.</a></p>';
1261
+ else
1262
+ return '';
1263
+ }
1264
+
1265
+ extract( shortcode_atts( array(
1266
+ 'button' => __( 'Buy Now', 'mycred' ),
1267
+ 'gateway' => '',
1268
+ 'ctype' => MYCRED_DEFAULT_TYPE_KEY,
1269
+ 'amount' => '',
1270
+ 'gift_to' => '',
1271
+ 'gift_by' => __( 'Username', 'mycred' ),
1272
+ 'inline' => 0
1273
+ ), $atts ) );
1274
+
1275
+ // If we are not logged in
1276
+ if ( $this->current_user_id == 0 ) return $content;
1277
+
1278
+ // Get gateways
1279
+ $installed = $this->get();
1280
+
1281
+ // Catch errors
1282
+ if ( empty( $installed ) ) return 'No gateways installed.';
1283
+ elseif ( ! empty( $gateway ) && ! array_key_exists( $gateway, $installed ) ) return 'Gateway does not exist.';
1284
+ elseif ( empty( $this->active ) ) return 'No active gateways found.';
1285
+ elseif ( ! empty( $gateway ) && ! $this->is_active( $gateway ) ) return 'The selected gateway is not active.';
1286
+
1287
+ // Make sure we are trying to sell a point type that is allowed to be purchased
1288
+ if ( ! in_array( $ctype, $this->core->buy_creds['types'] ) )
1289
+ $ctype = $this->core->buy_creds['types'][0];
1290
+
1291
+ if ( $ctype == $this->core->cred_id )
1292
+ $mycred = $this->core;
1293
+ else
1294
+ $mycred = mycred( $ctype );
1295
+
1296
+ global $post;
1297
+
1298
+ // Prep
1299
+ $buyer_id = $this->current_user_id;
1300
+ $recipient_id = $buyer_id;
1301
+ $classes = array( 'myCRED-buy-form' );
1302
+
1303
+ if ( $this->core->buy_creds['gifting']['authors'] && $gift_to == 'author' )
1304
+ $recipient_id = $post->post_author;
1305
+
1306
+ if ( $this->core->buy_creds['gifting']['members'] && absint( $gift_to ) !== 0 )
1307
+ $recipient_id = absint( $gift_to );
1308
+
1309
+ $button_label = $mycred->template_tags_general( $button );
1310
+
1311
+ if ( ! empty( $gateway ) ) {
1312
+ $gateway_name = explode( ' ', $installed[ $gateway ]['title'] );
1313
+ $button_label = str_replace( '%gateway%', $gateway_name[0], $button_label );
1314
+ $classes[] = $gateway_name[0];
1315
+ }
1316
+
1317
+ $amounts = array();
1318
+ $minimum = $this->core->number( $this->core->buy_creds['minimum'] );
1319
+ if ( ! empty( $amount ) ) {
1320
+ $_amounts = explode( ',', $amount );
1321
+ foreach ( $_amounts as $_amount ) {
1322
+ $_amount = $mycred->number( $_amount );
1323
+ if ( $_amount === $mycred->zero() ) continue;
1324
+ $amounts[] = $_amount;
1325
+ }
1326
+ }
1327
+
1328
+ ob_start();
1329
+
1330
+ ?>
1331
+ <div class="row">
1332
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
1333
+ <form method="post" class="form<?php if ( $inline == 1 ) echo '-inline'; ?> <?php echo implode( ' ', $classes ); ?>" action="">
1334
+ <input type="hidden" name="token" value="<?php echo wp_create_nonce( 'mycred-buy-creds' ); ?>" />
1335
+ <input type="hidden" name="transaction_id" value="<?php echo strtoupper( wp_generate_password( 6, false, false ) ); ?>" />
1336
+ <input type="hidden" name="ctype" value="<?php echo $ctype; ?>" />
1337
+
1338
+ <div class="form-group">
1339
+ <label><?php echo $mycred->plural(); ?></label>
1340
+ <?php
1341
+
1342
+ // No amount given - user must nominate the amount
1343
+ if ( count( $amounts ) == 0 ) :
1344
+
1345
+ ?>
1346
+ <input type="text" name="amount" class="form-control" placeholder="<?php echo $mycred->format_creds( $minimum ); ?>" min="<?php echo $minimum; ?>" value="" />
1347
+ <?php
1348
+
1349
+ // Amount given
1350
+ else :
1351
+
1352
+ // One amount - this is the amount a user must buy
1353
+ if ( count( $amount ) > 1 ) {
1354
+
1355
+ ?>
1356
+ <p class="form-control-static"><?php echo $mycred->format_creds( $amounts[0] ); ?></p>
1357
+ <input type="hidden" name="amount" value="<?php echo $amounts[0]; ?>" />
1358
+ <?php
1359
+
1360
+ }
1361
+
1362
+ // Multiple items - user selects the amount from a dropdown menu
1363
+ else {
1364
+
1365
+ ?>
1366
+ <select name="amount" class="form-control">
1367
+ <?php
1368
+
1369
+ foreach ( $amounts as $amount )
1370
+ echo '<option value="' . $amount . '">' . $mycred->format_creds( $amount ) . '</option>';
1371
+
1372
+ ?>
1373
+ </select>
1374
+ <?php
1375
+
1376
+ }
1377
+
1378
+ endif;
1379
+
1380
+ ?>
1381
+ <?php if ( $gift_to != '' ) : ?>
1382
+
1383
+ <div class="form-group">
1384
+ <label for="gift_to"><?php _e( 'Recipient', 'mycred' ); ?></label>
1385
+ <?php
1386
+
1387
+ // Post author - show the authors name
1388
+ if ( $this->core->buy_creds['gifting']['authors'] && $gift_to == 'author' ) {
1389
+
1390
+ $author = get_userdata( $recipient_id );
1391
+
1392
+ ?>
1393
+ <p class="form-control-static"><?php echo $author->display_name; ?></p>
1394
+ <input type="hidden" name="gift_to" value="<?php echo $recipient_id; ?>" />
1395
+ <?php
1396
+
1397
+ }
1398
+
1399
+ // Specific User - show the members name
1400
+ elseif ( $this->core->buy_creds['gifting']['members'] && absint( $gift_to ) !== 0 ) {
1401
+
1402
+ $member = get_userdata( $recipient_id );
1403
+
1404
+ ?>
1405
+ <p class="form-control-static"><?php echo $member->display_name; ?></p>
1406
+ <input type="hidden" name="gift_to" value="<?php echo $recipient_id; ?>" />
1407
+ <?php
1408
+
1409
+ }
1410
+
1411
+ // Users need to nominate recipient
1412
+ else {
1413
+
1414
+ ?>
1415
+ <input type="text" class="form-control pick-user" name="gift_to" placeholder="<?php echo $gift_by; ?>" value="" />
1416
+ <?php
1417
+
1418
+ }
1419
+
1420
+ ?>
1421
+ </div>
1422
+
1423
+ <?php endif; ?>
1424
+
1425
+ <?php if ( empty( $gateway ) && count( $installed ) > 1 ) : ?>
1426
+
1427
+ <div class="form-group">
1428
+ <label for="gateway"><?php _e( 'Pay Using', 'mycred' ); ?></label>
1429
+ <select name="mycred_buy" class="form-control">
1430
+ <?php
1431
+
1432
+ foreach ( $installed as $gateway_id => $info ) {
1433
+
1434
+ if ( $this->is_active( $gateway_id ) )
1435
+ echo '<option value="' . $gateway_id . '">' . $info['title'] . '</option>';
1436
+
1437
+ }
1438
+
1439
+ ?>
1440
+ </select>
1441
+ </div>
1442
+
1443
+ <?php else : ?>
1444
+
1445
+ <input type="hidden" name="mycred_buy" value="<?php echo $gateway; ?>" />
1446
+
1447
+ <?php endif; ?>
1448
+
1449
+ <div class="form-group">
1450
+ <input type="submit" class="btn btn-primary btn-block btn-lg" value="<?php echo $button_label; ?>" />
1451
+ </div>
1452
+ </div>
1453
+
1454
+ </form>
1455
+ </div>
1456
+ </div>
1457
+ <?php
1458
+
1459
+ $content = ob_get_contents();
1460
+ ob_end_clean();
1461
+
1462
+ return $content;
1463
+
1464
+ }
1465
+
1466
+ }
1467
+ endif;
1468
+
1469
+ /**
1470
+ * Load buyCRED Module
1471
+ * @since 1.7
1472
+ * @version 1.0
1473
+ */
1474
+ if ( ! function_exists( 'mycred_load_buycred_core_addon' ) ) :
1475
+ function mycred_load_buycred_core_addon( $modules, $point_types ) {
1476
+
1477
+ $modules['solo']['buycred'] = new myCRED_buyCRED_Module();
1478
+ $modules['solo']['buycred']->load();
1479
+
1480
+ return $modules;
1481
+
1482
+ }
1483
+ endif;
1484
+ add_filter( 'mycred_load_modules', 'mycred_load_buycred_core_addon', 30, 2 );
1485
+
1486
+ ?>
addons/buy-creds/modules/buycred-module-pending.php ADDED
@@ -0,0 +1,830 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'MYCRED_PURCHASE' ) ) exit;
3
+
4
+ /**
5
+ * buyCRED_Pending_Payments class
6
+ * @since 1.7
7
+ * @version 1.0
8
+ */
9
+ if ( ! class_exists( 'buyCRED_Pending_Payments' ) ) :
10
+ class buyCRED_Pending_Payments extends myCRED_Module {
11
+
12
+ /**
13
+ * Construct
14
+ */
15
+ function __construct( $type = MYCRED_DEFAULT_TYPE_KEY ) {
16
+
17
+ parent::__construct( 'buyCRED_Pending_Payments', array(
18
+ 'module_name' => 'pending_payments',
19
+ 'option_id' => '',
20
+ 'defaults' => array(),
21
+ 'screen_id' => '',
22
+ 'accordion' => false,
23
+ 'add_to_core' => false,
24
+ 'menu_pos' => 75
25
+ ), $type );
26
+
27
+ }
28
+
29
+ /**
30
+ * Load
31
+ * @version 1.0
32
+ */
33
+ public function load() {
34
+
35
+ add_action( 'mycred_init', array( $this, 'module_init' ) );
36
+ add_action( 'mycred_admin_init', array( $this, 'module_admin_init' ) );
37
+
38
+ }
39
+
40
+ /**
41
+ * Module Init
42
+ * @since 1.7
43
+ * @version 1.0
44
+ */
45
+ public function module_init() {
46
+
47
+ $this->register_pending_payments();
48
+
49
+ add_shortcode( 'mycred_buy_pending', array( $this, 'render_shortcode_pending' ) );
50
+
51
+ add_action( 'mycred_add_menu', array( $this, 'add_to_menu' ), $this->menu_pos );
52
+ add_action( 'template_redirect', array( $this, 'intercept_cancellations' ) );
53
+
54
+ }
55
+
56
+ /**
57
+ * Intercept Cancellations
58
+ * @since 1.7
59
+ * @version 1.0
60
+ */
61
+ public function intercept_cancellations() {
62
+
63
+ // Intercept payment cancellations
64
+ if ( isset( $_REQUEST['buycred-cancel'] ) && isset( $_REQUEST['_token'] ) && wp_verify_nonce( $_REQUEST['_token'], 'buycred-cancel-pending-payment' ) ) {
65
+
66
+ // Get pending payment object
67
+ $pending_payment_id = sanitize_text_field( $_REQUEST['buycred-cancel'] );
68
+ $pending_payment = buycred_get_pending_payment( $pending_payment_id );
69
+
70
+ // Make sure pending payment still exists and that we are cancelling our own and not someone elses
71
+ if ( $pending_payment === false || $pending_payment->buyer_id != get_current_user_id() ) return;
72
+
73
+ // Move item to trash
74
+ wp_trash_post( $pending_payment->payment_id );
75
+
76
+ // Redirect
77
+ wp_redirect( remove_query_arg( array( 'buycred-cancel', '_token' ) ) );
78
+ exit;
79
+
80
+ }
81
+
82
+ }
83
+
84
+ /**
85
+ * Module Admin Init
86
+ * @since 1.7
87
+ * @version 1.0
88
+ */
89
+ public function module_admin_init() {
90
+
91
+ add_action( 'admin_notices', array( $this, 'admin_notices' ) );
92
+ add_filter( 'parent_file', array( $this, 'parent_file' ) );
93
+ add_filter( 'manage_buycred_payment_posts_columns', array( $this, 'adjust_column_headers' ) );
94
+ add_action( 'manage_buycred_payment_posts_custom_column', array( $this, 'adjust_column_content' ), 10, 2 );
95
+ add_filter( 'bulk_actions-edit-buycred_payment', array( $this, 'bulk_actions' ) );
96
+ add_filter( 'post_row_actions', array( $this, 'adjust_row_actions' ), 10, 2 );
97
+ add_action( 'admin_head-post.php', array( $this, 'edit_pending_payment_style' ) );
98
+ add_action( 'admin_head-edit.php', array( $this, 'pending_payments_style' ) );
99
+ add_action( 'save_post_buycred_payment', array( $this, 'save_pending_payment' ), 10, 2 );
100
+ add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
101
+
102
+ // Intercept payment completions
103
+ if ( isset( $_GET['credit'] ) && isset( $_GET['token'] ) && wp_verify_nonce( $_GET['token'], 'buycred-payout-pending' ) ) {
104
+
105
+ $pending_id = absint( $_GET['credit'] );
106
+
107
+ if ( $this->core->can_edit_creds() ) {
108
+
109
+ $url = remove_query_arg( array( 'credit', 'token' ) );
110
+
111
+ if ( buycred_complete_pending_payment( $pending_id ) ) {
112
+ $url = add_query_arg( array( 'credited' => 1 ), $url );
113
+ }
114
+ else {
115
+ $url = add_query_arg( array( 'credited' => 0 ), $url );
116
+ }
117
+
118
+ wp_safe_redirect( $url );
119
+ exit;
120
+
121
+ }
122
+
123
+ }
124
+
125
+ }
126
+
127
+ /**
128
+ * Register Pending Payments
129
+ * @since 1.5
130
+ * @version 1.0.1
131
+ */
132
+ protected function register_pending_payments() {
133
+
134
+ $labels = array(
135
+ 'name' => _x( 'Pending Payments', 'Post Type General Name', 'mycred' ),
136
+ 'singular_name' => _x( 'Pending Payment', 'Post Type Singular Name', 'mycred' ),
137
+ 'menu_name' => __( 'Pending Payments', 'mycred' ),
138
+ 'parent_item_colon' => '',
139
+ 'all_items' => __( 'Pending Payments', 'mycred' ),
140
+ 'view_item' => '',
141
+ 'add_new_item' => '',
142
+ 'add_new' => '',
143
+ 'edit_item' => __( 'Edit Pending Payment', 'mycred' ),
144
+ 'update_item' => '',
145
+ 'search_items' => '',
146
+ 'not_found' => __( 'Not found in Trash', 'mycred' ),
147
+ 'not_found_in_trash' => __( 'Not found in Trash', 'mycred' ),
148
+ );
149
+ $args = array(
150
+ 'labels' => $labels,
151
+ 'supports' => array( 'title', 'comments' ),
152
+ 'hierarchical' => false,
153
+ 'public' => false,
154
+ 'show_ui' => true,
155
+ 'show_in_menu' => false,
156
+ 'show_in_nav_menus' => false,
157
+ 'show_in_admin_bar' => false,
158
+ 'can_export' => true,
159
+ 'has_archive' => false,
160
+ 'exclude_from_search' => true,
161
+ 'publicly_queryable' => false,
162
+ 'register_meta_box_cb' => array( $this, 'add_metaboxes' )
163
+ );
164
+ register_post_type( 'buycred_payment', apply_filters( 'mycred_setup_pending_payment', $args ) );
165
+
166
+ }
167
+
168
+ /**
169
+ * Adjust Post Updated Messages
170
+ * @since 1.7
171
+ * @version 1.0
172
+ */
173
+ public function post_updated_messages( $messages ) {
174
+
175
+ $messages['buycred_payment'] = array(
176
+ 0 => '',
177
+ 1 => __( 'Payment Updated.', 'mycred' ),
178
+ 2 => __( 'Payment Updated.', 'mycred' ),
179
+ 3 => __( 'Payment Updated.', 'mycred' ),
180
+ 4 => __( 'Payment Updated.', 'mycred' ),
181
+ 5 => __( 'Payment Updated.', 'mycred' ),
182
+ 6 => __( 'Payment Updated.', 'mycred' ),
183
+ 7 => __( 'Payment Updated.', 'mycred' ),
184
+ 8 => __( 'Payment Updated.', 'mycred' ),
185
+ 9 => __( 'Payment Updated.', 'mycred' ),
186
+ 10 => ''
187
+ );
188
+
189
+ return $messages;
190
+
191
+ }
192
+
193
+ /**
194
+ * Add Comment
195
+ * @since 1.7
196
+ * @version 1.0
197
+ */
198
+ public function add_comment( $post_id, $event = '', $time = NULL ) {
199
+
200
+ return buycred_add_pending_comment( $post_id, $event, $time );
201
+
202
+ }
203
+
204
+ /**
205
+ * Admin Notices
206
+ * @since 1.7
207
+ * @version 1.0
208
+ */
209
+ public function admin_notices() {
210
+
211
+ if ( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'buycred_payment' && isset( $_GET['credited'] ) ) {
212
+ if ( $_GET['credited'] == 1 )
213
+ echo '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Pending payment successfully credited to account.', 'mycred' ) . '</p><button type="button" class="notice-dismiss"></button></div>';
214
+ elseif ( $_GET['credited'] == 0 )
215
+ echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'Failed to credit the pending payment to account.', 'mycred' ) . '</p><button type="button" class="notice-dismiss"></button></div>';
216
+ }
217
+
218
+ }
219
+
220
+ /**
221
+ * Add Admin Menu Item
222
+ * @since 1.7
223
+ * @version 1.0
224
+ */
225
+ public function add_to_menu() {
226
+
227
+ add_submenu_page(
228
+ 'mycred',
229
+ __( 'Pending Payments', 'mycred' ),
230
+ __( 'Pending Payments', 'mycred' ),
231
+ $this->core->edit_creds_cap(),
232
+ 'edit.php?post_type=buycred_payment'
233
+ );
234
+
235
+ }
236
+
237
+ /**
238
+ * Parent File
239
+ * @since 1.7
240
+ * @version 1.0
241
+ */
242
+ public function parent_file( $parent = '' ) {
243
+
244
+ global $pagenow;
245
+
246
+ if ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'buycred_payment' && isset( $_GET['action'] ) && $_GET['action'] == 'edit' )
247
+ return 'mycred';
248
+
249
+ return $parent;
250
+
251
+ }
252
+
253
+ /**
254
+ * Pending Payment Column Headers
255
+ * @since 1.5
256
+ * @version 1.0
257
+ */
258
+ public function adjust_column_headers( $columns ) {
259
+
260
+ return array(
261
+ 'cb' => $columns['cb'],
262
+ 'comments' => $columns['comments'],
263
+ 'title' => __( 'Transaction ID', 'mycred' ),
264
+ 'date' => $columns['date'],
265
+ 'author' => __( 'Buyer', 'mycred' ),
266
+ 'amount' => __( 'Amount', 'mycred' ),
267
+ 'cost' => __( 'Cost', 'mycred' ),
268
+ 'gateway' => __( 'Gateway', 'mycred' ),
269
+ 'ctype' => __( 'Type', 'mycred' )
270
+ );
271
+
272
+ }
273
+
274
+ /**
275
+ * Pending Payment Column Content
276
+ * @since 1.5
277
+ * @version 1.0
278
+ */
279
+ public function adjust_column_content( $column_name, $post_id ) {
280
+
281
+ global $mycred_modules;
282
+
283
+ switch ( $column_name ) {
284
+ case 'author' :
285
+
286
+ $from = (int) get_post_meta( $post_id, 'from', true );
287
+ $user = get_userdata( $from );
288
+
289
+ if ( isset( $user->display_name ) )
290
+ echo '<a href="' . add_query_arg( array( 'user_id' => $user->ID ), admin_url( 'user-edit.php' ) ) . '">' . $user->display_name . '</a>';
291
+ else
292
+ echo 'ID: ' . $from;
293
+
294
+ break;
295
+ case 'amount';
296
+
297
+ $type = get_post_meta( $post_id, 'point_type', true );
298
+ $amount = get_post_meta( $post_id, 'amount', true );
299
+ $mycred = mycred( $type );
300
+
301
+ echo $mycred->format_creds( $amount );
302
+
303
+ break;
304
+ case 'cost';
305
+
306
+ $cost = get_post_meta( $post_id, 'cost', true );
307
+ $currency = get_post_meta( $post_id, 'currency', true );
308
+
309
+ echo $cost . ' ' . $currency;
310
+
311
+ break;
312
+ case 'gateway';
313
+
314
+ $gateway = get_post_meta( $post_id, 'gateway', true );
315
+ $installed = $mycred_modules['solo']['buycred']->get();
316
+
317
+ if ( isset( $installed[ $gateway ] ) )
318
+ echo $installed[ $gateway ]['title'];
319
+ else
320
+ echo $gateway;
321
+
322
+ break;
323
+ case 'ctype';
324
+
325
+ $type = get_post_meta( $post_id, 'point_type', true );
326
+
327
+ if ( isset( $this->point_types[ $type ] ) )
328
+ echo $this->point_types[ $type ];
329
+ else
330
+ echo $type;
331
+
332
+ break;
333
+ }
334
+
335
+ }
336
+
337
+ /**
338
+ * Adjust Bulk Actions
339
+ * @since 1.5
340
+ * @version 1.0
341
+ */
342
+ public function bulk_actions( $actions ) {
343
+
344
+ unset( $actions['edit'] );
345
+ return $actions;
346
+
347
+ }
348
+
349
+ /**
350
+ * Pending Payment Row Actions
351
+ * @since 1.5
352
+ * @version 1.1
353
+ */
354
+ public function adjust_row_actions( $actions, $post ) {
355
+
356
+ if ( $post->post_type == 'buycred_payment' && $post->post_status != 'trash' ) {
357
+
358
+ unset( $actions['inline hide-if-no-js'] );
359
+
360
+ // Add option to "Pay Out" now
361
+ if ( $this->core->can_edit_creds() )
362
+ $actions['credit'] = '<a href="' . esc_url( add_query_arg( array(
363
+ 'post_type' => $post->post_type,
364
+ 'credit' => $post->ID,
365
+ 'token' => wp_create_nonce( 'buycred-payout-pending' )
366
+ ), admin_url( 'edit.php' ) ) ) . '">' . __( 'Pay Out', 'mycred' ) . '</a>';
367
+
368
+ }
369
+
370
+ return $actions;
371
+
372
+ }
373
+
374
+ /**
375
+ * Edit Pending Payment Style
376
+ * @since 1.7
377
+ * @version 1.0
378
+ */
379
+ public function edit_pending_payment_style() {
380
+
381
+ global $post_type;
382
+
383
+ if ( $post_type !== 'buycred_payment' ) return;
384
+
385
+ wp_enqueue_style( 'mycred-bootstrap-grid' );
386
+ wp_enqueue_style( 'mycred-forms' );
387
+
388
+ add_filter( 'postbox_classes_buycred_payment_buycred-pending-payment', array( $this, 'metabox_classes' ) );
389
+ add_filter( 'postbox_classes_buycred_payment_buycred-pending-comments', array( $this, 'metabox_classes' ) );
390
+
391
+ ?>
392
+ <script type="text/javascript">
393
+ jQuery(function($){
394
+
395
+ $( 'h1 .page-title-action' ).remove();
396
+ $( '#titlewrap #title' ).attr( 'readonly', 'readonly' ).addClass( 'readonly' );
397
+
398
+ });
399
+ </script>
400
+ <?php
401
+
402
+ }
403
+
404
+ /**
405
+ * Pending Payment Style
406
+ * @since 1.7
407
+ * @version 1.0
408
+ */
409
+ public function pending_payments_style() {
410
+
411
+ global $post_type;
412
+
413
+ if ( $post_type !== 'buycred_payment' ) return;
414
+
415
+ ?>
416
+ <script type="text/javascript">
417
+ jQuery(function($){
418
+
419
+ $( 'h1 .page-title-action' ).remove();
420
+
421
+ });
422
+ </script>
423
+ <?php
424
+
425
+ }
426
+
427
+ /**
428
+ * Add Metaboxes
429
+ * @since 1.7
430
+ * @version 1.0
431
+ */
432
+ public function add_metaboxes() {
433
+
434
+ add_meta_box( 'buycred-pending-payment', __( 'Pending Payment', 'mycred' ), array( $this, 'metabox_pending_payment' ), 'buycred_payment', 'normal', 'high' );
435
+
436
+ if ( MYCRED_BUYCRED_PENDING_COMMENTS !== false )
437
+ add_meta_box( 'buycred-pending-comments', __( 'History', 'mycred' ), array( $this, 'metabox_pending_comments' ), 'buycred_payment', 'normal', 'default' );
438
+
439
+ remove_meta_box( 'commentstatusdiv', 'buycred_payment', 'normal' );
440
+ remove_meta_box( 'commentsdiv', 'buycred_payment', 'normal' );
441
+
442
+ remove_meta_box( 'submitdiv', 'buycred_payment', 'side' );
443
+ add_meta_box( 'submitdiv', __( 'Actions', 'mycred' ), array( $this, 'metabox_pending_actions' ), 'buycred_payment', 'side', 'high' );
444
+
445
+ }
446
+
447
+ /**
448
+ * Metabox: Pending Actions
449
+ * @since 1.7
450
+ * @version 1.0
451
+ */
452
+ public function metabox_pending_actions( $post ) {
453
+
454
+ $payout_url = add_query_arg( array(
455
+ 'post_type' => $post->post_type,
456
+ 'credit' => $post->ID,
457
+ 'token' => wp_create_nonce( 'buycred-payout-pending' )
458
+ ), admin_url( 'edit.php' ) );
459
+
460
+ $delete_url = get_delete_post_link( $post->ID );
461
+
462
+ ?>
463
+ <div class="submitbox mycred-metabox" id="submitpost">
464
+ <div id="minor-publishing">
465
+ <div style="display:none;">
466
+ <?php submit_button( __( 'Save', 'mycred' ), 'button', 'save' ); ?>
467
+ </div>
468
+
469
+ <div id="minor-publishing-actions">
470
+
471
+ <div><a href="<?php echo $payout_url; ?>" class="button button-secondary button-block"><?php _e( 'Pay Out', 'mycred' ); ?></a></div>
472
+ <div><a href="<?php echo $delete_url; ?>" class="button button-secondary button-block"><?php _e( 'Trash', 'mycred' ); ?></a></div>
473
+
474
+ </div><!-- #minor-publishing-actions -->
475
+
476
+ <div class="clear"></div>
477
+ </div>
478
+ <div id="major-publishing-actions">
479
+
480
+ <div id="publishing-action">
481
+ <span class="spinner"></span>
482
+
483
+ <input type="submit" id="publish" class="button button-primary primary button-large" value="<?php _e( 'Save Changes', 'mycred' ); ?>" />
484
+
485
+ </div>
486
+ <div class="clear"></div>
487
+ </div>
488
+ </div>
489
+ <?php
490
+
491
+ }
492
+
493
+ /**
494
+ * Metabox: Pending Payment
495
+ * @since 1.7
496
+ * @version 1.0
497
+ */
498
+ public function metabox_pending_payment( $post ) {
499
+
500
+ global $mycred_modules;
501
+
502
+ $pending_payment = buycred_get_pending_payment( $post->ID );
503
+
504
+ $buyer_name = 'ID: ' . $pending_payment->buyer_id;
505
+ $buyer_object = get_userdata( $pending_payment->buyer_id );
506
+ if ( isset( $buyer_object->ID ) ) {
507
+ $buyer_name = $buyer_object->display_name;
508
+ if ( $buyer_name == '' )
509
+ $buyer_name = $buyer_object->user_email;
510
+ }
511
+
512
+ if ( $pending_payment->recipient_id == $pending_payment->buyer_id )
513
+ $recipient_name = $buyer_name;
514
+
515
+ else {
516
+ $recipient_name = 'ID: ' . $pending_payment->recipient_id;
517
+ $recipient_object = get_userdata( $pending_payment->recipient_id );
518
+ if ( isset( $recipient_object->ID ) ) {
519
+ $recipient_name = $recipient_object->display_name;
520
+ if ( $recipient_name == '' )
521
+ $recipient_name = $recipient_object->user_email;
522
+ }
523
+ }
524
+
525
+ if ( $pending_payment->point_type == $this->core->cred_id )
526
+ $mycred = $this->core;
527
+
528
+ else
529
+ $mycred = mycred( $type );
530
+
531
+ ?>
532
+ <div class="form">
533
+ <div class="row">
534
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
535
+ <div class="form-group">
536
+ <label><?php _e( 'Payer', 'mycred' ); ?></label>
537
+ <p class="form-control-static"><?php echo esc_attr( $buyer_name ); ?></p>
538
+ </div>
539
+ </div>
540
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
541
+ <div class="form-group">
542
+ <label><?php _e( 'Recipient', 'mycred' ); ?></label>
543
+ <p class="form-control-static"><?php echo esc_attr( $recipient_name ); ?></p>
544
+ </div>
545
+ </div>
546
+ </div>
547
+ <div class="row">
548
+ <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
549
+ <div class="form-group">
550
+ <label for="buycred-pending-payment-point_type"><?php _e( 'Point Type', 'mycred' ); ?></label>
551
+ <?php
552
+
553
+ // Just one point type is set for sale. No need for a dropdown menu
554
+ if ( count( $this->core->buy_creds['types'] ) == 1 ) {
555
+
556
+ ?>
557
+ <p class="form-control-static"><?php echo strip_tags( $mycred->plural() ); ?></p>
558
+ <input type="hidden" name="buycred_pending_payment[point_type]" value="<?php echo $pending_payment->point_type; ?>" />
559
+ <?php
560
+
561
+ }
562
+
563
+ // Multiple point types are set for sale. Show a dropdown menu
564
+ else {
565
+
566
+ ?>
567
+ <select name="buycred_pending_payment[point_type]" id="buycred-pending-payment-point_type" class="form-control">
568
+ <?php
569
+
570
+ foreach ( $this->core->buy_creds['types'] as $point_type ) {
571
+
572
+ echo '<option value="' . $point_type . '"';
573
+ if ( $pending_payment->point_type == $point_type ) echo ' selected="selected"';
574
+ echo '>' . mycred_get_point_type_name( $pending_payment->point_type, false ) . '</option>';
575
+
576
+ }
577
+
578
+ ?>
579
+ </select>
580
+ <?php
581
+
582
+ }
583
+
584
+ ?>
585
+ </div>
586
+ </div>
587
+ <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
588
+ <div class="form-group">
589
+ <label for="buycred-pending-payment-gateway"><?php _e( 'Gateway', 'mycred' ); ?></label>
590
+ <select name="buycred_pending_payment[gateway]" id="buycred-pending-payment-gateway" class="form-control">
591
+ <?php
592
+
593
+ foreach ( $mycred_modules['solo']['buycred']->get() as $gateway_id => $info ) {
594
+
595
+ echo '<option value="' . $gateway_id . '"';
596
+ if ( $pending_payment->gateway_id == $gateway_id ) echo ' selected="selected"';
597
+ if ( ! $mycred_modules['solo']['buycred']->is_active( $gateway_id ) ) echo ' disabled="disabled"';
598
+ echo '>' . $info['title'] . '</option>';
599
+
600
+ }
601
+
602
+ ?>
603
+ </select>
604
+ </div>
605
+ </div>
606
+ <div class="col-lg-2 col-md-2 col-sm-6 col-xs-12">
607
+ <div class="form-group">
608
+ <label for="buycred-pending-payment-amount"><?php _e( 'Amount', 'mycred' ); ?></label>
609
+ <input type="text" name="buycred_pending_payment[amount]" id="buycred-pending-payment-amount" class="form-control" value="<?php echo $mycred->number( $pending_payment->amount ); ?>" />
610
+ </div>
611
+ </div>
612
+ <div class="col-lg-2 col-md-2 col-sm-6 col-xs-12">
613
+ <div class="form-group">
614
+ <label for="buycred-pending-payment-cost"><?php _e( 'Cost', 'mycred' ); ?></label>
615
+ <input type="text" name="buycred_pending_payment[cost]" id="buycred-pending-payment-cost" class="form-control" value="<?php echo esc_attr( $pending_payment->cost ); ?>" />
616
+ </div>
617
+ </div>
618
+ <div class="col-lg-2 col-md-2 col-sm-6 col-xs-12">
619
+ <div class="form-group">
620
+ <label for="buycred-pending-payment-currency"><?php _e( 'Currency', 'mycred' ); ?></label>
621
+ <input type="text" name="buycred_pending_payment[currency]" id="buycred-pending-payment-currency" class="form-control" value="<?php echo esc_attr( $pending_payment->currency ); ?>" />
622
+ </div>
623
+ </div>
624
+ </div>
625
+ </div>
626
+ <?php
627
+
628
+ }
629
+
630
+ /**
631
+ * Metabox: Pending Payment Comments
632
+ * @since 1.7
633
+ * @version 1.0
634
+ */
635
+ public function metabox_pending_comments( $post ) {
636
+
637
+ $comments = get_comments( array( 'post_id' => $post->ID ) );
638
+
639
+ echo '<ul class="history">';
640
+
641
+ if ( empty( $comments ) ) {
642
+
643
+ $c = new StdClass();
644
+ $c->comment_date = $post->post_date;
645
+ $c->comment_content = __( 'Pending request created.', 'mycred' );
646
+
647
+ $event = $this->add_comment( $post->ID, $c->comment_content, $c->comment_date );
648
+ if ( $event === false )
649
+ $c->comment_content .= ' Unsaved';
650
+
651
+ else
652
+ $c->comment_content .= ' ' . $event;
653
+
654
+ $comments[] = $c;
655
+
656
+ }
657
+
658
+ foreach ( $comments as $comment ) {
659
+
660
+ echo '<li><time>' . $comment->comment_date . '</time><p>' . $comment->comment_content . '</p></li>';
661
+
662
+ }
663
+
664
+ echo '</ul>';
665
+
666
+ }
667
+
668
+ /**
669
+ * Save Pending Payment
670
+ * @since 1.7
671
+ * @version 1.0
672
+ */
673
+ public function save_pending_payment( $post_id, $post ) {
674
+
675
+ if ( ! current_user_can( $this->core->edit_creds_cap() ) || ! isset( $_POST['buycred_pending_payment'] ) ) return;
676
+
677
+ $pending_payment = $_POST['buycred_pending_payment'];
678
+ $changed = false;
679
+
680
+ foreach ( $pending_payment as $meta_key => $meta_value ) {
681
+
682
+ $new_value = sanitize_text_field( $meta_value );
683
+ $old_value = get_post_meta( $post_id, $meta_key, true );
684
+ if ( $new_value != $old_value ) {
685
+ update_post_meta( $post_id, $meta_key, $new_value );
686
+ $changed = true;
687
+ }
688
+
689
+ }
690
+
691
+ if ( $changed ) {
692
+ $user = wp_get_current_user();
693
+ $this->add_comment( $post_id, sprintf( __( 'Pending payment updated by %s', 'mycred' ), $user->user_login ) );
694
+ }
695
+
696
+ }
697
+
698
+ /**
699
+ * Render Shortcode Pending
700
+ * @since 1.5
701
+ * @version 1.1
702
+ */
703
+ public function render_shortcode_pending( $attr, $content = '' ) {
704
+
705
+ // Must be logged in
706
+ if ( ! is_user_logged_in() ) return $content;
707
+
708
+ extract( shortcode_atts( array(
709
+ 'ctype' => MYCRED_DEFAULT_TYPE_KEY,
710
+ 'pay_now' => __( 'Pay Now', 'mycred' ),
711
+ 'cancel' => __( 'Cancel', 'mycred' )
712
+ ), $attr ) );
713
+
714
+ $user_id = get_current_user_id();
715
+ $pending = buycred_get_users_pending_payments( $user_id, $ctype );
716
+
717
+ ob_start();
718
+
719
+ ?>
720
+ <div id="pending-buycred-payments-<?php echo $ctype; ?>">
721
+ <table class="table">
722
+ <thead>
723
+ <tr>
724
+ <th class="column-transaction-id"><?php _e( 'Transaction ID', 'mycred' ); ?></th>
725
+ <th class="column-gateway"><?php _e( 'Gateway', 'mycred' ); ?></th>
726
+ <th class="column-amount"><?php _e( 'Amount', 'mycred' ); ?></th>
727
+ <th class="column-cost"><?php _e( 'Cost', 'mycred' ); ?></th>
728
+ <?php if ( $ctype == '' ) : ?><th class="column-ctype"><?php _e( 'Point Type', 'mycred' ); ?></th><?php endif; ?>
729
+ <th class="column-actions"><?php _e( 'Actions', 'mycred' ); ?></th>
730
+ </tr>
731
+ </thead>
732
+ <tbody>
733
+ <?php
734
+
735
+ if ( ! empty( $pending ) ) {
736
+
737
+ // Showing all point types
738
+ if ( $ctype == '' ) {
739
+
740
+ foreach ( $pending as $point_type => $entries ) {
741
+
742
+ if ( empty( $entries ) ) continue;
743
+
744
+ foreach ( $entries as $entry ) {
745
+
746
+ ?>
747
+ <tr>
748
+ <td class="column-transaction-id"><?php echo esc_attr( $entry->public_id ); ?></td>
749
+ <td class="column-gateway"><?php echo $this->adjust_column_content( 'gateway', $entry->payment_id ); ?></td>
750
+ <td class="column-amount"><?php echo $this->adjust_column_content( 'amount', $entry->payment_id ); ?></td>
751
+ <td class="column-cost"><?php echo $this->adjust_column_content( 'cost', $entry->payment_id ); ?></td>
752
+ <td class="column-ctype"><?php echo mycred_get_point_type_name( $entry->point_type, false ); ?></td>
753
+ <td class="column-actions">
754
+ <a href="<?php echo esc_url( $entry->pay_now_url ); ?>"><?php echo $pay_now; ?></a> &bull; <a href="<?php echo esc_url( $entry->cancel_url ); ?>"><?php echo $cancel; ?></a>
755
+ </td>
756
+ </tr>
757
+ <?php
758
+
759
+ }
760
+
761
+ }
762
+
763
+ }
764
+
765
+ // Showing a particular point type
766
+ else {
767
+
768
+ foreach ( $pending as $entry ) {
769
+
770
+ ?>
771
+ <tr>
772
+ <td class="column-transaction-id"><?php echo esc_attr( $entry->public_id ); ?></td>
773
+ <td class="column-gateway"><?php echo $this->adjust_column_content( 'gateway', $entry->payment_id ); ?></td>
774
+ <td class="column-amount"><?php echo $this->adjust_column_content( 'amount', $entry->payment_id ); ?></td>
775
+ <td class="column-cost"><?php echo $this->adjust_column_content( 'cost', $entry->payment_id ); ?></td>
776
+ <td class="column-actions">
777
+ <a href="<?php echo esc_url( $entry->pay_now_url ); ?>"><?php echo $pay_now; ?></a> &bull; <a href="<?php echo esc_url( $entry->cancel_url ); ?>"><?php echo $cancel; ?></a>
778
+ </td>
779
+ </tr>
780
+ <?php
781
+
782
+ }
783
+
784
+ }
785
+
786
+ }
787
+ else {
788
+
789
+ ?>
790
+ <tr>
791
+ <td colspan="<?php if ( $ctype == '' ) echo '6'; else echo '5'; ?>"><?php _e( 'No pending payments found', 'mycred' ); ?></td>
792
+ </tr>
793
+ <?php
794
+
795
+ }
796
+
797
+ ?>
798
+ </tbody>
799
+ </table>
800
+ </div>
801
+ <?php
802
+
803
+ $output = ob_get_contents();
804
+ ob_end_clean();
805
+
806
+ return $output;
807
+
808
+ }
809
+
810
+ }
811
+ endif;
812
+
813
+ /**
814
+ * Load buyCRED Pending Module
815
+ * @since 1.7
816
+ * @version 1.0
817
+ */
818
+ if ( ! function_exists( 'mycred_load_buycred_pending_addon' ) ) :
819
+ function mycred_load_buycred_pending_addon( $modules, $point_types ) {
820
+
821
+ $modules['solo']['buycred-pending'] = new buyCRED_Pending_Payments();
822
+ $modules['solo']['buycred-pending']->load();
823
+
824
+ return $modules;
825
+
826
+ }
827
+ endif;
828
+ add_filter( 'mycred_load_modules', 'mycred_load_buycred_pending_addon', 40, 2 );
829
+
830
+ ?>
addons/buy-creds/modules/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ ?>
addons/buy-creds/myCRED-addon-buy-creds.php CHANGED
@@ -2,1842 +2,47 @@
2
  /**
3
  * Addon: buyCRED
4
  * Addon URI: http://mycred.me/add-ons/buycred/
5
- * Version: 1.4.1
6
- * Description: The <strong>buy</strong>CRED Add-on allows your users to buy points using PayPal, Skrill (Moneybookers), Zombaio or NETbilling. <strong>buy</strong>CRED can also let your users buy points for other members.
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
 
12
- define( 'myCRED_PURCHASE', __FILE__ );
13
- define( 'myCRED_PURCHASE_VERSION', myCRED_VERSION . '.1' );
14
- define( 'myCRED_PURCHASE_DIR', myCRED_ADDONS_DIR . 'buy-creds/' );
 
 
 
 
 
 
 
15
 
16
  /**
17
- * Payment Gateway factory
18
  */
19
- require_once myCRED_PURCHASE_DIR . 'abstracts/mycred-abstract-payment-gateway.php';
 
20
 
21
  /**
22
- * Payment Gateways, if you do not want to use one just comment it out.
 
 
23
  */
24
- require_once myCRED_PURCHASE_DIR . 'gateways/paypal-standard.php';
25
- require_once myCRED_PURCHASE_DIR . 'gateways/bitpay.php';
26
- require_once myCRED_PURCHASE_DIR . 'gateways/netbilling.php';
27
- require_once myCRED_PURCHASE_DIR . 'gateways/skrill.php';
28
- require_once myCRED_PURCHASE_DIR . 'gateways/zombaio.php';
 
29
 
30
  do_action( 'mycred_buycred_load_gateways' );
31
 
32
  /**
33
- * myCRED_buyCRED_Module class
34
- * @since 0.1
35
- * @version 1.4
36
  */
37
- if ( ! class_exists( 'myCRED_buyCRED_Module' ) ) :
38
- class myCRED_buyCRED_Module extends myCRED_Module {
39
-
40
- public $purchase_log = '';
41
-
42
- /**
43
- * Construct
44
- */
45
- function __construct( $type = 'mycred_default' ) {
46
-
47
- parent::__construct( 'myCRED_BuyCRED_Module', array(
48
- 'module_name' => 'gateways',
49
- 'option_id' => 'mycred_pref_buycreds',
50
- 'defaults' => array(
51
- 'installed' => array(),
52
- 'active' => array(),
53
- 'gateway_prefs' => array()
54
- ),
55
- 'labels' => array(
56
- 'menu' => __( 'Payment Gateways', 'mycred' ),
57
- 'page_title' => __( 'Payment Gateways', 'mycred' ),
58
- 'page_header' => __( 'Payment Gateways', 'mycred' )
59
- ),
60
- 'screen_id' => 'myCRED_page_gateways',
61
- 'accordion' => true,
62
- 'add_to_core' => true,
63
- 'menu_pos' => 85
64
- ), $type );
65
-
66
- // Adjust Module to the selected point type
67
- $this->mycred_type = 'mycred_default';
68
- if ( isset( $this->core->buy_creds['type'] ) )
69
- $this->mycred_type = $this->core->buy_creds['type'];
70
-
71
- add_filter( 'mycred_parse_log_entry', array( $this, 'render_gift_tags' ), 10, 2 );
72
-
73
- }
74
-
75
- /**
76
- * Load
77
- * @version 1.0
78
- */
79
- public function load() {
80
-
81
- add_action( 'mycred_init', array( $this, 'module_init' ) );
82
- add_action( 'mycred_admin_init', array( $this, 'module_admin_init' ) );
83
-
84
- add_filter( 'set-screen-option', array( $this, 'set_payments_per_page' ), 11, 3 );
85
- add_action( 'mycred_admin_init', array( $this, 'register_settings' ) );
86
- add_action( 'mycred_add_menu', array( $this, 'add_menu' ), $this->menu_pos );
87
- add_action( 'mycred_add_menu', array( $this, 'add_to_menu' ), $this->menu_pos+1 );
88
- add_action( 'mycred_after_core_prefs', array( $this, 'after_general_settings' ) );
89
- add_filter( 'mycred_save_core_prefs', array( $this, 'sanitize_extra_settings' ), 90, 3 );
90
-
91
- add_action( 'mycred_edit_profile', array( $this, 'user_level_exchange_rates' ), 10, 2 );
92
- add_action( 'mycred_edit_profile_action', array( $this, 'save_user_override' ) );
93
- add_action( 'mycred_admin_notices', array( $this, 'update_profile_notice' ) );
94
-
95
- add_filter( 'manage_buycred_payment_posts_columns', array( $this, 'adjust_column_headers' ) );
96
- add_action( 'manage_buycred_payment_posts_custom_column', array( $this, 'adjust_column_content' ), 10, 2 );
97
- add_filter( 'bulk_actions-edit-buycred_payment', array( $this, 'bulk_actions' ) );
98
- add_filter( 'post_row_actions', array( $this, 'adjust_row_actions' ), 10, 2 );
99
-
100
- }
101
-
102
- /**
103
- * Render Gift Tags
104
- * @since 1.4.1
105
- * @version 1.0
106
- */
107
- public function render_gift_tags( $content, $log ) {
108
-
109
- if ( substr( $log->ref, 0, 15 ) != 'buy_creds_with_' ) return $content;
110
- return $this->core->template_tags_user( $content, absint( $log->ref_id ) );
111
-
112
- }
113
-
114
- /**
115
- * Process
116
- * Processes Gateway returns and IPN calls
117
- * @since 0.1
118
- * @version 1.3
119
- */
120
- public function module_init() {
121
-
122
- // Register Pending Purchases
123
- $this->current_user_id = get_current_user_id();
124
- $this->register_pending_payments();
125
-
126
- // Add shortcodes first
127
- add_shortcode( 'mycred_buy', array( $this, 'render_shortcode_basic' ) );
128
- add_shortcode( 'mycred_buy_form', array( $this, 'render_shortcode_form' ) );
129
- add_shortcode( 'mycred_buy_pending', array( $this, 'render_shortcode_pending' ) );
130
-
131
- // Intercept payment cancellations
132
- if ( isset( $_REQUEST['buycred_cancel'] ) && strlen( $_REQUEST['buycred_cancel'] ) > 5 )
133
- $this->delete_pending_payment( $_REQUEST['buycred_cancel'] );
134
-
135
- $gateway = NULL;
136
-
137
- // Make sure we have installed gateways.
138
- $installed = $this->get();
139
- if ( empty( $installed ) ) return;
140
-
141
- /**
142
- * Step 1 - Look for returns
143
- * Runs though all active payment gateways and lets them decide if this is the
144
- * user returning after a remote purchase. Each gateway should know what to look
145
- * for to determen if they are responsible for handling the return.
146
- */
147
- foreach ( $installed as $id => $data ) {
148
-
149
- if ( ! $this->is_active( $id ) ) continue;
150
- $this->call( 'returning', $installed[ $id ]['callback'] );
151
-
152
- }
153
-
154
- /**
155
- * Step 2 - Check for gateway calls
156
- * Checks to see if a gateway should be loaded.
157
- */
158
- $gateway_id = '';
159
- $process = false;
160
- if ( isset( $_REQUEST['mycred_call'] ) )
161
- $gateway_id = trim( $_REQUEST['mycred_call'] );
162
-
163
- elseif ( isset( $_REQUEST['mycred_buy'] ) && is_user_logged_in() )
164
- $gateway_id = trim( $_REQUEST['mycred_buy'] );
165
-
166
- elseif ( isset( $_REQUEST['wp_zombaio_ips'] ) || isset( $_REQUEST['ZombaioGWPass'] ) ) {
167
- $gateway_id = 'zombaio';
168
- $process = true;
169
- }
170
-
171
- $gateway_id = apply_filters( 'mycred_gateway_id', $gateway_id );
172
-
173
- // If we have a valid gateway ID and the gateway is active, lets run that gateway.
174
- if ( ! empty( $gateway_id ) && array_key_exists( $gateway_id, $installed ) && $this->is_active( $gateway_id ) ) {
175
-
176
- // Gateway Class
177
- $class = $installed[ $gateway_id ]['callback'][0];
178
-
179
- // Construct Gateway
180
- $gateway = new $class( $this->gateway_prefs );
181
-
182
- // Check payment processing
183
- if ( isset( $_REQUEST['mycred_call'] ) || ( $gateway_id == 'zombaio' && $process ) ) {
184
-
185
- $gateway->process();
186
-
187
- do_action( 'mycred_buycred_process', $gateway_id, $this->gateway_prefs, $this->core->buy_creds );
188
- do_action( 'mycred_buycred_process_' . $gateway_id, $this->gateway_prefs, $this->core->buy_creds );
189
-
190
- }
191
-
192
- // Check purchase request
193
- if ( isset( $_REQUEST['mycred_buy'] ) ) {
194
-
195
- // Validate token
196
- $token = false;
197
- if ( isset( $_REQUEST['token'] ) && wp_verify_nonce( $_REQUEST['token'], 'mycred-buy-creds' ) )
198
- $token = true;
199
-
200
- // Validate amount ( amount is not zero and higher then minimum required
201
- $amount = false;
202
- if ( isset( $_REQUEST['amount'] ) && $_REQUEST['amount'] != 0 && $_REQUEST['amount'] >= $this->core->buy_creds['minimum'] )
203
- $amount = true;
204
-
205
- if ( $token && $amount ) {
206
-
207
- $gateway->buy();
208
-
209
- do_action( 'mycred_buycred_buy', $gateway_id, $this->gateway_prefs, $this->core->buy_creds );
210
- do_action( 'mycred_buycred_buy_' . $gateway_id, $this->gateway_prefs, $this->core->buy_creds );
211
-
212
- }
213
-
214
- }
215
-
216
- }
217
-
218
- }
219
-
220
- /**
221
- * Admin Init
222
- * @since 1.5
223
- * @version 1.0
224
- */
225
- function module_admin_init() {
226
-
227
- if ( isset( $_GET['credit'] ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'buycred_payment' && isset( $_GET['ctype'] ) ) {
228
-
229
- $pending_id = absint( $_GET['credit'] );
230
- $point_type = sanitize_text_field( $_GET['ctype'] );
231
- $mycred = mycred( $point_type );
232
-
233
- if ( ! $mycred->can_edit_creds() ) return;
234
-
235
- $gateway_id = sanitize_text_field( $_GET['gateway'] );
236
- $installed = $this->get();
237
-
238
- if ( array_key_exists( $gateway_id, $installed ) ) {
239
-
240
- $class = $installed[ $gateway_id ]['callback'][0];
241
-
242
- // Construct Gateway
243
- $gateway = new $class( $this->gateway_prefs );
244
-
245
- // Get Pending Payment
246
- $pending_payment = $gateway->get_pending_payment( $pending_id );
247
-
248
- // Complete Payment
249
- $gateway->complete_payment( $pending_payment, get_the_title( $pending_id ) );
250
-
251
- wp_delete_post( $pending_id );
252
-
253
- $url = remove_query_arg( array( 'credit', 'ctype', 'gateway' ) );
254
- $url = add_query_arg( array( 'credited' => 1 ), $url );
255
- wp_safe_redirect( $url );
256
-
257
- exit;
258
-
259
- }
260
-
261
- }
262
-
263
- }
264
-
265
- /**
266
- * Register Pending Payments
267
- * @since 1.5
268
- * @version 1.0.1
269
- */
270
- function register_pending_payments() {
271
-
272
- $labels = array(
273
- 'name' => _x( 'Pending Payments', 'Post Type General Name', 'mycred' ),
274
- 'singular_name' => _x( 'Pending Payment', 'Post Type Singular Name', 'mycred' ),
275
- 'menu_name' => __( 'Pending Payments', 'mycred' ),
276
- 'parent_item_colon' => '',
277
- 'all_items' => __( 'Pending Payments', 'mycred' ),
278
- 'view_item' => '',
279
- 'add_new_item' => __( 'Add New', 'mycred' ),
280
- 'add_new' => __( 'Add New', 'mycred' ),
281
- 'edit_item' => __( 'Edit Pending Payment', 'mycred' ),
282
- 'update_item' => '',
283
- 'search_items' => '',
284
- 'not_found' => __( 'No pending payments found', 'mycred' ),
285
- 'not_found_in_trash' => __( 'Not found in Trash', 'mycred' ),
286
- );
287
- $args = array(
288
- 'labels' => $labels,
289
- 'supports' => array( 'title', 'author', 'comments', 'custom-fields' ),
290
- 'hierarchical' => false,
291
- 'public' => false,
292
- 'show_ui' => true,
293
- 'show_in_menu' => 'myCRED',
294
- 'show_in_nav_menus' => false,
295
- 'show_in_admin_bar' => false,
296
- 'can_export' => true,
297
- 'has_archive' => false,
298
- 'exclude_from_search' => true,
299
- 'publicly_queryable' => false
300
- );
301
- register_post_type( 'buycred_payment', apply_filters( 'mycred_setup_pending_payment', $args ) );
302
-
303
- }
304
-
305
- /**
306
- * Delete Pending Payment
307
- * @since 1.6.4
308
- * @version 1.0.1
309
- */
310
- function delete_pending_payment( $payment_id = '' ) {
311
-
312
- if ( $this->current_user_id == 0 ) return;
313
-
314
- $payment_id = sanitize_key( $payment_id );
315
- $payment = get_page_by_title( strtoupper( $payment_id ), OBJECT, 'buycred_payment' );
316
-
317
- // If the pending transaction is found and it is ours, delete it now.
318
- if ( isset( $payment->ID ) && get_post_meta( $payment->ID, 'from', true ) == $this->current_user_id ) {
319
-
320
- wp_delete_post( $payment_id, true );
321
-
322
- wp_redirect( remove_query_arg( array( 'buycred_cancel' ) ) );
323
-
324
- exit;
325
-
326
- }
327
-
328
- }
329
-
330
- /**
331
- * Pending Payment Column Headers
332
- * @since 1.5
333
- * @version 1.0
334
- */
335
- function adjust_column_headers( $columns ) {
336
-
337
- return array(
338
- 'cb' => $columns['cb'],
339
- 'comments' => $columns['comments'],
340
- 'title' => __( 'Transaction ID', 'mycred' ),
341
- 'date' => $columns['date'],
342
- 'author' => __( 'Buyer', 'mycred' ),
343
- 'amount' => __( 'Amount', 'mycred' ),
344
- 'cost' => __( 'Cost', 'mycred' ),
345
- 'gateway' => __( 'Gateway', 'mycred' ),
346
- 'ctype' => __( 'Type', 'mycred' )
347
- );
348
-
349
- }
350
-
351
- /**
352
- * Pending Payment Column Content
353
- * @since 1.5
354
- * @version 1.0
355
- */
356
- function adjust_column_content( $column_name, $post_id ) {
357
-
358
- switch ( $column_name ) {
359
- case 'author' :
360
-
361
- $from = (int) get_post_meta( $post_id, 'from', true );
362
- $user = get_userdata( $from );
363
- if ( isset( $user->display_name ) )
364
- echo '<a href="' . add_query_arg( array( 'user_id' => $user->ID ), admin_url( 'user-edit.php' ) ) . '">' . $user->display_name . '</a>';
365
- else
366
- echo 'ID: ' . $from;
367
-
368
- break;
369
- case 'amount';
370
-
371
- $type = get_post_meta( $post_id, 'point_type', true );
372
- $amount = get_post_meta( $post_id, 'amount', true );
373
- $mycred = mycred( $type );
374
- echo $mycred->format_creds( $amount );
375
-
376
- break;
377
- case 'cost';
378
-
379
- $cost = get_post_meta( $post_id, 'cost', true );
380
- $currency = get_post_meta( $post_id, 'currency', true );
381
- echo $cost . ' ' . $currency;
382
-
383
- break;
384
- case 'gateway';
385
-
386
- $gateway = get_post_meta( $post_id, 'gateway', true );
387
- $installed = $this->get();
388
- if ( isset( $installed[ $gateway ] ) )
389
- echo $installed[ $gateway ]['title'];
390
- else
391
- echo $gateway;
392
-
393
- break;
394
- case 'ctype';
395
-
396
- $type = get_post_meta( $post_id, 'point_type', true );
397
- if ( isset( $this->point_types[ $type ] ) )
398
- echo $this->point_types[ $type ];
399
- else
400
- echo $type;
401
-
402
- break;
403
- }
404
-
405
- }
406
-
407
- /**
408
- * Adjust Bulk Actions
409
- * @since 1.5
410
- * @version 1.0
411
- */
412
- function bulk_actions( $actions ) {
413
-
414
- unset( $actions['edit'] );
415
- return $actions;
416
-
417
- }
418
-
419
- /**
420
- * Pending Payment Row Actions
421
- * @since 1.5
422
- * @version 1.0.1
423
- */
424
- function adjust_row_actions( $actions, $post ) {
425
-
426
- if ( $post->post_type == 'buycred_payment' ) {
427
- unset( $actions['inline hide-if-no-js'] );
428
-
429
- $ctype = get_post_meta( $post->ID, 'point_type', true );
430
- $mycred = mycred( $ctype );
431
-
432
- if ( $mycred->can_edit_creds() ) {
433
- $credit = add_query_arg( array(
434
- 'credit' => $post->ID,
435
- 'ctype' => $ctype,
436
- 'post_type' => $post->post_type,
437
- 'gateway' => get_post_meta( $post->ID, 'gateway', true )
438
- ) );
439
- $actions['credit'] = '<a href="' . esc_url( $credit ) . '">' . __( 'Pay Out', 'mycred' ) . '</a>';
440
- }
441
- }
442
-
443
- return $actions;
444
-
445
- }
446
-
447
- /**
448
- * Add Admin Menu Item
449
- * @since 0.1
450
- * @version 1.1
451
- */
452
- function add_to_menu() {
453
-
454
- if ( isset( $this->core->buy_creds['custom_log'] ) && $this->core->buy_creds['custom_log'] ) {
455
-
456
- // Menu Slug
457
- $menu_slug = 'myCRED';
458
-
459
- $types = array( 'mycred_default' );
460
- if ( isset( $this->core->buy_creds['types'] ) )
461
- $types = $this->core->buy_creds['types'];
462
-
463
- $pages = array();
464
- foreach ( $types as $type ) {
465
-
466
- $mycred = mycred( $type );
467
-
468
- if ( $type != 'mycred_default' )
469
- $menu_slug .= '_' . $type;
470
-
471
- $pages[] = add_submenu_page(
472
- $menu_slug,
473
- __( 'buyCRED Purchase Log', 'mycred' ),
474
- __( 'Purchase Log', 'mycred' ),
475
- $mycred->edit_plugin_cap(),
476
- 'myCRED_page_gateways_log_' . $type,
477
- array( $this, 'purchase_log_page' )
478
- );
479
-
480
- }
481
-
482
- foreach ( $pages as $page ) {
483
-
484
- add_action( 'admin_print_styles-' . $page, array( $this, 'settings_page_enqueue' ) );
485
- add_action( 'load-' . $page, array( $this, 'screen_options' ) );
486
-
487
- }
488
-
489
- $this->purchase_log = $pages;
490
-
491
- }
492
-
493
- }
494
-
495
- /**
496
- * Get Payment Gateways
497
- * Retreivs all available payment gateways that can be used to buy CREDs.
498
- * @since 0.1
499
- * @version 1.0.1
500
- */
501
- public function get() {
502
-
503
- $installed = array();
504
-
505
- // PayPal Standard
506
- $installed['paypal-standard'] = array(
507
- 'title' => 'PayPal Payments Standard',
508
- 'callback' => array( 'myCRED_PayPal_Standard' )
509
- );
510
-
511
- // BitPay
512
- $installed['bitpay'] = array(
513
- 'title' => 'BitPay (Bitcoins)',
514
- 'callback' => array( 'myCRED_Bitpay' )
515
- );
516
-
517
- // NetBilling
518
- $installed['netbilling'] = array(
519
- 'title' => 'NETBilling',
520
- 'callback' => array( 'myCRED_NETbilling' )
521
- );
522
-
523
- // Skrill
524
- $installed['skrill'] = array(
525
- 'title' => 'Skrill (Moneybookers)',
526
- 'callback' => array( 'myCRED_Skrill' )
527
- );
528
-
529
- // Zombaio
530
- $installed['zombaio'] = array(
531
- 'title' => 'Zombaio',
532
- 'callback' => array( 'myCRED_Zombaio' )
533
- );
534
-
535
- $installed = apply_filters( 'mycred_setup_gateways', $installed );
536
-
537
- $this->installed = $installed;
538
- return $installed;
539
-
540
- }
541
-
542
- /**
543
- * Page Header
544
- * @since 1.3
545
- * @version 1.1
546
- */
547
- public function settings_header() {
548
-
549
- $gateway_icons = plugins_url( 'assets/images/gateway-icons.png', myCRED_THIS );
550
-
551
- ?>
552
- <!-- buyCRED Module -->
553
- <style type="text/css">
554
- #myCRED-wrap #accordion h4 .gate-icon { display: block; width: 48px; height: 48px; margin: 0 0 0 0; padding: 0; float: left; line-height: 48px; }
555
- #myCRED-wrap #accordion h4 .gate-icon { background-repeat: no-repeat; background-image: url("<?php echo $gateway_icons; ?>"); background-position: 0 0; }
556
- #myCRED-wrap #accordion h4 .gate-icon.inactive { background-position-x: 0; }
557
- #myCRED-wrap #accordion h4 .gate-icon.active { background-position-x: -48px; }
558
- #myCRED-wrap #accordion h4 .gate-icon.sandbox { background-position-x: -96px; }
559
- #myCRED-wrap #accordion h4 .gate-icon.monitor { background-position-x: 0; background-position-y: -48px; }
560
- </style>
561
- <?php
562
-
563
- }
564
-
565
- /**
566
- * Screen Options
567
- * @since 1.4
568
- * @version 1.1
569
- */
570
- public function screen_options() {
571
-
572
- if ( empty( $this->purchase_log ) ) return;
573
-
574
- $current_screen = get_current_screen();
575
- if ( ! in_array( $current_screen->id, $this->purchase_log ) ) return;
576
-
577
- $args = array(
578
- 'label' => __( 'Payments', 'mycred' ),
579
- 'default' => 10,
580
- 'option' => 'mycred_payments_' . $current_screen->id
581
- );
582
- add_screen_option( 'per_page', $args );
583
-
584
- }
585
-
586
- /**
587
- * Save Payments per page
588
- * @since 1.4
589
- * @version 1.1
590
- */
591
- public function set_payments_per_page( $status, $option, $value ) {
592
-
593
- if ( substr( $option, 0, 16 ) == 'mycred_payments_' ) return $value;
594
- return $status;
595
-
596
- }
597
-
598
- /**
599
- * Add to General Settings
600
- * @since 0.1
601
- * @version 1.1
602
- */
603
- public function after_general_settings( $mycred = NULL ) {
604
-
605
- // Since we are both registering our own settings and want to hook into
606
- // the core settings, we need to define our "defaults" here.
607
- $defaults = array(
608
- 'minimum' => 1,
609
- 'types' => array( 'mycred_default' ),
610
- 'exchange' => 1,
611
- 'log' => '%plural% purchase',
612
- 'login' => __( 'Please login to purchase %_plural%', 'mycred' ),
613
- 'custom_log' => 0,
614
- 'thankyou' => array(
615
- 'use' => 'page',
616
- 'custom' => '',
617
- 'page' => ''
618
- ),
619
- 'cancelled' => array(
620
- 'use' => 'custom',
621
- 'custom' => '',
622
- 'page' => ''
623
- ),
624
- 'gifting' => array(
625
- 'members' => 1,
626
- 'authors' => 1,
627
- 'log' => __( 'Gift purchase from %display_name%.', 'mycred' )
628
- )
629
- );
630
-
631
- if ( isset( $this->core->buy_creds ) )
632
- $buy_creds = $this->core->buy_creds;
633
- else
634
- $buy_creds = $defaults;
635
-
636
- $thankyou_use = $buy_creds['thankyou']['use'];
637
- $cancelled_use = $buy_creds['cancelled']['use'];
638
-
639
- ?>
640
- <h4><div class="icon icon-active"></div><strong>buy</strong>CRED</h4>
641
- <div class="body" style="display:none;">
642
- <label class="subheader"><?php echo $this->core->template_tags_general( __( 'Minimum %plural%', 'mycred' ) ); ?></label>
643
- <ol id="mycred-buy-creds-minimum-amount">
644
- <li>
645
- <div class="h2"><input type="text" name="mycred_pref_core[buy_creds][minimum]" id="<?php echo $this->field_id( 'minimum' ); ?>" value="<?php echo $buy_creds['minimum']; ?>" size="5" /></div>
646
- <span class="description"><?php echo $this->core->template_tags_general( __( 'Minimum amount of %plural% a user must purchase. Will default to 1.', 'mycred' ) ); ?></span>
647
- </li>
648
- </ol>
649
- <?php if ( count( $this->point_types ) > 1 ) : ?>
650
-
651
- <label class="subheader"><?php _e( 'Point Types', 'mycred' ); ?></label>
652
- <ol id="mycred-buy-creds-type">
653
- <li>
654
- <span class="description"><?php _e( 'Select the point types that users can buy. You must select at least one!', 'mycred' ); ?></span>
655
- </li>
656
- <li>
657
- <?php mycred_types_select_from_checkboxes( 'mycred_pref_core[buy_creds][types][]', $this->field_id( 'types' ), $buy_creds['types'] ); ?>
658
- </li>
659
- </ol>
660
- <?php else : ?>
661
-
662
- <input type="hidden" name="mycred_pref_core[buy_creds][types][]" value="mycred_default" />
663
- <?php endif; ?>
664
-
665
- <label class="subheader" for="<?php echo $this->field_id( 'login' ); ?>"><?php _e( 'Login Template', 'mycred' ); ?></label>
666
- <ol id="mycred-buy-creds-default-log">
667
- <li>
668
- <input type="text" name="mycred_pref_core[buy_creds][login]" id="<?php echo $this->field_id( 'login' ); ?>" class="large-text code" value="<?php echo esc_attr( $buy_creds['login'] ); ?>" />
669
- <span class="description"><?php _e( 'Content to show when a user is not logged in.', 'mycred' ); ?></span>
670
- </li>
671
- </ol>
672
- <label class="subheader" for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
673
- <ol id="mycred-buy-creds-default-log">
674
- <li>
675
- <div class="h2"><input type="text" name="mycred_pref_core[buy_creds][log]" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo $buy_creds['log']; ?>" class="long" /></div>
676
- <span class="description"><?php echo $this->core->available_template_tags( array( 'general' ), '%gateway%' ); ?></span>
677
- </li>
678
- </ol>
679
- <label class="subheader"><?php _e( 'Thank You Page', 'mycred' ); ?></label>
680
- <ol id="mycred-buy-creds-thankyou-page">
681
- <li class="option">
682
- <input type="radio" name="mycred_pref_core[buy_creds][thankyou][use]" <?php checked( $thankyou_use, 'custom' ); ?> id="<?php echo $this->field_id( array( 'thankyou' => 'use' ) ); ?>-custom" value="custom" /> <label for="<?php echo $this->field_id( array( 'thankyou' => 'custom' ) ); ?>"><?php _e( 'Custom URL', 'mycred' ); ?></label><br />
683
- <div class="h2"><?php echo get_bloginfo( 'url' ) . '/'; ?> <input type="text" name="mycred_pref_core[buy_creds][thankyou][custom]" id="<?php echo $this->field_id( array( 'thankyou' => 'custom' ) ); ?>" value="<?php echo $buy_creds['thankyou']['custom']; ?>" /></div>
684
- </li>
685
- <li class="empty">&nbsp;</li>
686
- <li class="option">
687
- <input type="radio" name="mycred_pref_core[buy_creds][thankyou][use]" <?php checked( $thankyou_use, 'page' ); ?> id="<?php echo $this->field_id( array( 'thankyou' => 'use' ) ); ?>-page" value="page" /> <label for="mycred-buy-creds-thankyou-use-page"><?php _e( 'Page', 'mycred' ); ?></label><br />
688
- <?php
689
-
690
- // Thank you page dropdown
691
- $thankyou_args = array(
692
- 'name' => 'mycred_pref_core[buy_creds][thankyou][page]',
693
- 'id' => $this->field_id( array( 'thankyou' => 'page' ) ) . '-id',
694
- 'selected' => $buy_creds['thankyou']['page'],
695
- 'show_option_none' => __( 'Select', 'mycred' )
696
- );
697
- wp_dropdown_pages( $thankyou_args );
698
-
699
- ?>
700
- </li>
701
- </ol>
702
- <label class="subheader"><?php _e( 'Cancellation Page', 'mycred' ); ?></label>
703
- <ol id="mycred-buy-creds-cancel-page">
704
- <li class="option">
705
- <input type="radio" name="mycred_pref_core[buy_creds][cancelled][use]" <?php checked( $cancelled_use, 'custom' ); ?> id="<?php echo $this->field_id( array( 'cancelled' => 'custom' ) ); ?>" value="custom" /> <label for="<?php echo $this->field_id( array( 'cancelled' => 'custom' ) ); ?>"><?php _e( 'Custom URL', 'mycred' ); ?></label><br />
706
- <div class="h2"><?php echo get_bloginfo( 'url' ) . '/'; ?> <input type="text" name="mycred_pref_core[buy_creds][cancelled][custom]" id="mycred-buy-creds-cancelled-custom-url" value="<?php echo $buy_creds['cancelled']['custom']; ?>" /></div>
707
- </li>
708
- <li class="empty">&nbsp;</li>
709
- <li class="option">
710
- <input type="radio" name="mycred_pref_core[buy_creds][cancelled][use]" <?php checked( $cancelled_use, 'page' ); ?> id="<?php echo $this->field_id( array( 'cancelled' => 'use' ) ); ?>-page" value="page" /> <label for="<?php echo $this->field_id( array( 'cancelled' => 'use' ) ); ?>-page"><?php _e( 'Page', 'mycred' ); ?></label><br />
711
- <?php
712
-
713
- // Cancelled page dropdown
714
- $cancelled_args = array(
715
- 'name' => 'mycred_pref_core[buy_creds][cancelled][page]',
716
- 'id' => $this->field_id( array( 'cancelled' => 'page' ) ) . '-id',
717
- 'selected' => $buy_creds['cancelled']['page'],
718
- 'show_option_none' => __( 'Select', 'mycred' )
719
- );
720
- wp_dropdown_pages( $cancelled_args );
721
-
722
- ?>
723
- </li>
724
- </ol>
725
- <label class="subheader"><?php _e( 'Purchase Log', 'mycred' ); ?></label>
726
- <ol id="mycred-buy-creds-seperate-log">
727
- <li><input type="checkbox" name="mycred_pref_core[buy_creds][custom_log]" id="<?php echo $this->field_id( 'custom_log' ); ?>"<?php checked( $buy_creds['custom_log'], 1 ); ?> value="1" /><label for="<?php echo $this->field_id( 'custom_log' ); ?>"><?php echo $this->core->template_tags_general( __( 'Show seperate log for %_plural% purchases.', 'mycred' ) ); ?></label></li>
728
- </ol>
729
- <label class="subheader"><?php _e( 'Gifting', 'mycred' ); ?></label>
730
- <ol id="mycred-buy-creds-gifting">
731
- <li><input type="checkbox" name="mycred_pref_core[buy_creds][gifting][members]" id="<?php echo $this->field_id( array( 'gifting' => 'members' ) ); ?>"<?php checked( $buy_creds['gifting']['members'], 1 ); ?> value="1" /><label for="<?php echo $this->field_id( array( 'gifting' => 'members' ) ); ?>"><?php echo $this->core->template_tags_general( __( 'Allow users to buy %_plural% for other users.', 'mycred' ) ); ?></label></li>
732
- <li><input type="checkbox" name="mycred_pref_core[buy_creds][gifting][authors]" id="<?php echo $this->field_id( array( 'gifting' => 'authors' ) ); ?>"<?php checked( $buy_creds['gifting']['authors'], 1 ); ?> value="1" /><label for="<?php echo $this->field_id( array( 'gifting' => 'authors' ) ); ?>"><?php echo $this->core->template_tags_general( __( 'Allow users to buy %_plural% for content authors.', 'mycred' ) ); ?></label></li>
733
- <li class="empty">&nbsp;</li>
734
- <li>
735
- <label for="<?php echo $this->field_id( array( 'gifting' => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
736
- <div class="h2"><input type="text" name="mycred_pref_core[buy_creds][gifting][log]" id="<?php echo $this->field_id( array( 'gifting' => 'log' ) ); ?>" value="<?php echo $buy_creds['gifting']['log']; ?>" class="long" /></div>
737
- <span class="description"><?php echo $this->core->available_template_tags( array( 'general', 'user' ) ); ?></span>
738
- </li>
739
- </ol>
740
- <label class="subheader"><?php _e( 'Available Shortcodes', 'mycred' ); ?></label>
741
- <ol id="mycred-buy-creds-shortcodes">
742
- <li>
743
- <a href="http://codex.mycred.me/shortcodes/mycred_buy/" target="_blank">[mycred_buy]</a> ,
744
- <a href="http://codex.mycred.me/shortcodes/mycred_buy_form/" target="_blank">[mycred_buy_form]</a> ,
745
- <a href="http://codex.mycred.me/shortcodes/mycred_buy_pending/" target="_blank">[mycred_buy_pending]</a>
746
- </li>
747
- </ol>
748
- </div>
749
- <?php
750
-
751
- }
752
-
753
- /**
754
- * Save Settings
755
- * @since 0.1
756
- * @version 1.1
757
- */
758
- public function sanitize_extra_settings( $new_data, $data, $core ) {
759
-
760
- $new_data['buy_creds']['minimum'] = abs( $data['buy_creds']['minimum'] );
761
-
762
- if ( ! isset( $data['buy_creds']['types'] ) )
763
- $new_data['buy_creds']['types'] = array( 'mycred_default' );
764
- else
765
- $new_data['buy_creds']['types'] = $data['buy_creds']['types'];
766
-
767
- $new_data['buy_creds']['log'] = sanitize_text_field( $data['buy_creds']['log'] );
768
- $new_data['buy_creds']['login'] = trim( $data['buy_creds']['login'] );
769
-
770
- $new_data['buy_creds']['thankyou']['use'] = sanitize_text_field( $data['buy_creds']['thankyou']['use'] );
771
- $new_data['buy_creds']['thankyou']['custom'] = sanitize_text_field( $data['buy_creds']['thankyou']['custom'] );
772
- $new_data['buy_creds']['thankyou']['page'] = absint( $data['buy_creds']['thankyou']['page'] );
773
-
774
- $new_data['buy_creds']['cancelled']['use'] = sanitize_text_field( $data['buy_creds']['cancelled']['use'] );
775
- $new_data['buy_creds']['cancelled']['custom'] = sanitize_text_field( $data['buy_creds']['cancelled']['custom'] );
776
- $new_data['buy_creds']['cancelled']['page'] = absint( $data['buy_creds']['cancelled']['page'] );
777
-
778
- $new_data['buy_creds']['custom_log'] = ( ! isset( $data['buy_creds']['custom_log'] ) ) ? 0 : 1;
779
-
780
- $new_data['buy_creds']['gifting']['members'] = ( ! isset( $data['buy_creds']['gifting']['members'] ) ) ? 0 : 1;
781
- $new_data['buy_creds']['gifting']['authors'] = ( ! isset( $data['buy_creds']['gifting']['authors'] ) ) ? 0 : 1;
782
- $new_data['buy_creds']['gifting']['log'] = sanitize_text_field( $data['buy_creds']['gifting']['log'] );
783
-
784
- delete_option( 'mycred_buycred_reset' );
785
-
786
- return $new_data;
787
-
788
- }
789
-
790
- /**
791
- * User Level Override
792
- * @since 1.5
793
- * @version 1.0
794
- */
795
- public function user_level_exchange_rates( $user, $type ) {
796
-
797
- $types = array( 'mycred_default' );
798
- if ( isset( $this->core->buy_creds['types'] ) )
799
- $types = $this->core->buy_creds['types'];
800
-
801
- $users_saved_overrides = (array) mycred_get_user_meta( $user->ID, 'mycred_buycred_rates_' . $type, '', true );
802
- $installed = $this->get();
803
-
804
- $usable = array();
805
- foreach ( $installed as $gateway_id => $prefs ) {
806
-
807
- if ( ! $this->is_active( $gateway_id ) || ! isset( $this->gateway_prefs[ $gateway_id ] ) ) continue;
808
-
809
- $gateway_prefs = $this->gateway_prefs[ $gateway_id ];
810
-
811
- if ( ! isset( $gateway_prefs['exchange'][ $type ] ) || ( isset( $gateway_prefs['currency'] ) && $gateway_prefs['currency'] == '' ) ) continue;
812
-
813
- $usable[ $gateway_id ] = $gateway_prefs;
814
- $usable[ $gateway_id ]['title'] = $prefs['title'];
815
-
816
- if ( ! isset( $users_saved_overrides[ $gateway_id ] ) )
817
- $users_saved_overrides[ $gateway_id ] = '';
818
-
819
- }
820
-
821
- if ( empty( $usable ) ) return;
822
-
823
- $mycred = mycred( $type );
824
-
825
- ?>
826
- <h3><strong>buy</strong>CRED</h3>
827
- <table class="form-table">
828
- <?php foreach ( $usable as $gateway_id => $gateway_prefs ) : if ( ! isset( $gateway_prefs['currency'] ) ) $gateway_prefs['currency'] = 'USD'; ?>
829
- <tr>
830
- <th scope="row"><?php printf( __( '%s Exchange Rate', 'mycred' ), $gateway_prefs['title'] ); ?></th>
831
- <td>
832
- <table>
833
- <tr>
834
- <td style="min-width: 100px;"><div class="h2">1 <?php echo $mycred->singular(); ?></div></td>
835
- <td style="width: 10px;"><div class="h2">=</div></td>
836
- <td><div class="h2"><input type="text" name="mycred_adjust_users_buyrates[<?php echo $gateway_id; ?>]" placeholder="<?php if ( isset( $this->gateway_prefs[ $gateway_id ]['exchange'][ $type ] ) ) echo $this->gateway_prefs[ $gateway_id ]['exchange'][ $type ]; ?>" value="<?php echo $users_saved_overrides[ $gateway_id ]; ?>" size="8" /> <span><?php echo $gateway_prefs['currency']; ?></span></div><span class="description"><?php _e( 'Leave empty to use the default value.', 'mycred' ); ?></span></td>
837
- </tr>
838
- </table>
839
- </td>
840
- </tr>
841
- <?php endforeach; ?>
842
- <tr>
843
- <th scope="row"></th>
844
- <td><?php submit_button( __( 'Save Exchange Rates', 'mycred' ), 'primary medium', 'mycred_adjust_users_buyrates_run', false ); ?></td>
845
- </tr>
846
- </table>
847
- <?php
848
-
849
- }
850
-
851
- /**
852
- * Save Override
853
- * @since 1.5
854
- * @version 1.1.1
855
- */
856
- public function save_user_override() {
857
-
858
- if ( isset( $_POST['mycred_adjust_users_buyrates_run'] ) && isset( $_POST['mycred_adjust_users_buyrates'] ) ) {
859
-
860
- $ctype = sanitize_key( $_GET['ctype'] );
861
- $user_id = absint( $_GET['user_id'] );
862
- $mycred = mycred( $ctype );
863
-
864
- if ( $mycred->edit_plugin_cap() && ! $mycred->exclude_user( $user_id ) ) {
865
-
866
- $new_rates = array();
867
- foreach ( (array) $_POST['mycred_adjust_users_buyrates'] as $gateway_id => $rate ) {
868
- if ( $rate == '' ) continue;
869
-
870
- if ( $rate != 1 && in_array( substr( $rate, 0, 1 ), array( '.', ',' ) ) )
871
- $rate = (float) '0' . $rate;
872
-
873
- $new_rates[ $gateway_id ] = $rate;
874
- }
875
-
876
- if ( ! empty( $new_rates ) )
877
- mycred_update_user_meta( $user_id, 'mycred_buycred_rates_' . $ctype, '', $new_rates );
878
- else
879
- mycred_delete_user_meta( $user_id, 'mycred_buycred_rates_' . $ctype );
880
-
881
- wp_safe_redirect( add_query_arg( array( 'result' => 'buycred_rates' ) ) );
882
-
883
- exit;
884
-
885
- }
886
-
887
- }
888
-
889
- }
890
-
891
- /**
892
- * Override Update Notice
893
- * @since 1.5
894
- * @version 1.0
895
- */
896
- public function update_profile_notice() {
897
-
898
- if ( isset( $_GET['page'] ) && $_GET['page'] == 'mycred-edit-balance' && isset( $_GET['result'] ) && $_GET['result'] == 'buycred_rates' )
899
- echo '<div class="updated"><p>' . __( 'Exchange rate override saved', 'mycred' ) . '</p></div>';
900
-
901
- elseif ( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'buycred_payment' && isset( $_GET['credited'] ) )
902
- echo '<div class="updated"><p>' . __( 'Payment completed', 'mycred' ) . '</p></div>';
903
-
904
- }
905
-
906
- /**
907
- * Payment Gateways Page
908
- * @since 0.1
909
- * @version 1.2.1
910
- */
911
- public function admin_page() {
912
-
913
- // Security
914
- if ( ! $this->core->can_edit_creds() )
915
- wp_die( __( 'Access Denied', 'mycred' ) );
916
-
917
- $installed = $this->get();
918
-
919
- ?>
920
- <div class="wrap list" id="myCRED-wrap">
921
- <h2><?php echo sprintf( __( '%s Payment Gateways', 'mycred' ), '<strong>buy</strong>CRED' ); ?> <?php if ( isset( $this->core->buy_creds['custom_log'] ) && $this->core->buy_creds['custom_log'] == 1 ) : ?><a href="<?php echo admin_url( 'admin.php?page=myCRED_page_gateways_log' ); ?>" class="add-new-h2"><?php _e( 'Purchase Log', 'mycred' ); ?></a><?php endif; ?><a href="<?php echo $this->get_settings_url( 'buycred_module' ); ?>" class="add-new-h2"><?php _e( 'buyCRED Settings', 'mycred' ); ?></a></h2>
922
- <?php
923
-
924
- // Updated settings
925
- if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true )
926
- echo '<div class="updated settings-error"><p>' . __( 'Settings Updated', 'mycred' ) . '</p></div>';
927
-
928
- ?>
929
- <form method="post" action="options.php">
930
- <?php settings_fields( $this->settings_name ); ?>
931
-
932
- <?php do_action( 'mycred_before_buycreds_page', $this ); ?>
933
-
934
- <div class="list-items expandable-li" id="accordion">
935
- <?php
936
-
937
- if ( ! empty( $installed ) ) {
938
- foreach ( $installed as $key => $data ) {
939
-
940
- ?>
941
- <h4><div class="gate-icon <?php
942
-
943
- // Mark
944
- if ( $this->is_active( $key ) ) {
945
- if ( isset( $this->gateway_prefs[ $key ]['sandbox'] ) && $this->gateway_prefs[ $key ]['sandbox'] == 1 )
946
- echo 'sandbox" title="' . __( 'Test Mode', 'mycred' );
947
- else
948
- echo 'active" title="' . __( 'Enabled', 'mycred' );
949
- }
950
- else
951
- echo 'inactive" title="' . __( 'Disabled', 'mycred' ); ?>"></div><?php echo $this->core->template_tags_general( $data['title'] ); ?></h4>
952
- <div class="body" style="display:none;">
953
- <label class="subheader"><?php _e( 'Enable', 'mycred' ); ?></label>
954
- <ol>
955
- <li>
956
- <input type="checkbox" name="mycred_pref_buycreds[active][]" id="mycred-gateway-<?php echo $key; ?>" value="<?php echo $key; ?>"<?php if ( $this->is_active( $key ) ) echo ' checked="checked"'; ?> />
957
- </li>
958
- </ol>
959
- <?php if ( isset( $this->gateway_prefs[ $key ]['sandbox'] ) && $this->gateway_prefs[ $key ]['sandbox'] !== NULL ) : ?>
960
-
961
- <label class="subheader" for="mycred-gateway-<?php echo $key; ?>-sandbox"><?php _e( 'Sandbox Mode', 'mycred' ); ?></label>
962
- <ol>
963
- <li>
964
- <input type="checkbox" name="mycred_pref_buycreds[gateway_prefs][<?php echo $key; ?>][sandbox]" id="mycred-gateway-<?php echo $key; ?>-sandbox" value="1"<?php checked( $this->gateway_prefs[ $key ]['sandbox'], 1 ); ?> /> <span class="description"><?php _e( 'Enable for test purchases.', 'mycred' ); ?></span>
965
- </li>
966
- </ol>
967
- <?php
968
-
969
- endif;
970
-
971
- $this->call( 'preferences', $data['callback'] );
972
-
973
- ?>
974
- <input type="hidden" name="mycred_pref_buycreds[installed]" value="<?php echo $key; ?>" />
975
- </div>
976
- <?php
977
-
978
- }
979
- }
980
-
981
- ?>
982
- </div>
983
- <?php do_action( 'mycred_after_buycreds_page', $this ); ?>
984
-
985
- <?php submit_button( __( 'Update Settings', 'mycred' ), 'primary large', 'submit', false ); ?>
986
- <a href="http://mycred.me/product-category/buycred-gateways/" class="button button-secondary button-large" target="_blank"><?php _e( 'More Gateways', 'mycred' ); ?></a>
987
-
988
- </form>
989
- <?php do_action( 'mycred_bottom_buycreds_page', $this ); ?>
990
-
991
- <script type="text/javascript">
992
- jQuery(function($) {
993
- $( 'select.currency' ).change(function(){
994
- var target = $(this).attr( 'data-update' );
995
- $( '.' + target ).empty();
996
- $( '.' + target ).text( $(this).val() );
997
- });
998
- });
999
- </script>
1000
- </div>
1001
- <?php
1002
-
1003
- }
1004
-
1005
- /**
1006
- * Custom Log Page
1007
- * @since 1.4
1008
- * @version 1.3.2
1009
- */
1010
- public function purchase_log_page() {
1011
-
1012
- $type = substr( $_GET['page'], 25 );
1013
-
1014
- if ( $type == 'mycred_default' )
1015
- $mycred = $this->core;
1016
- else
1017
- $mycred = mycred( $type );
1018
-
1019
- // Security
1020
- if ( ! $mycred->can_edit_creds() )
1021
- wp_die( __( 'Access Denied', 'mycred' ) );
1022
-
1023
- $per_page = mycred_get_user_meta( get_current_user_id(), 'mycred_payments_' . $type, '', true );
1024
- if ( empty( $per_page ) || $per_page < 1 ) $per_page = 10;
1025
-
1026
- // Get references
1027
- $references = apply_filters( 'mycred_buycred_log_refs', array(
1028
- 'buy_creds_with_paypal_standard',
1029
- 'buy_creds_with_skrill',
1030
- 'buy_creds_with_zombaio',
1031
- 'buy_creds_with_netbilling',
1032
- 'buy_creds_with_bitpay'
1033
- ), $this, $type );
1034
-
1035
- // Prep
1036
- $args = array(
1037
- 'number' => $per_page,
1038
- 'ctype' => $type,
1039
- 'ref' => implode( ',', $references )
1040
- );
1041
-
1042
- if ( isset( $_GET['user_id'] ) && ! empty( $_GET['user_id'] ) )
1043
- $args['user_id'] = $_GET['user_id'];
1044
-
1045
- if ( isset( $_GET['s'] ) && ! empty( $_GET['s'] ) )
1046
- $args['s'] = $_GET['s'];
1047
-
1048
- if ( isset( $_GET['ref'] ) && ! empty( $_GET['ref'] ) )
1049
- $args['ref'] = $_GET['ref'];
1050
-
1051
- if ( isset( $_GET['show'] ) && ! empty( $_GET['show'] ) )
1052
- $args['time'] = $_GET['show'];
1053
-
1054
- if ( isset( $_GET['order'] ) && ! empty( $_GET['order'] ) )
1055
- $args['order'] = $_GET['order'];
1056
-
1057
- if ( isset( $_GET['paged'] ) && ! empty( $_GET['paged'] ) )
1058
- $args['paged'] = $_GET['paged'];
1059
-
1060
- if ( isset( $_GET['start'] ) && isset( $_GET['end'] ) )
1061
- $args['amount'] = array( 'start' => $_GET['start'], 'end' => $_GET['end'] );
1062
-
1063
- elseif ( isset( $_GET['num'] ) && isset( $_GET['compare'] ) )
1064
- $args['amount'] = array( 'num' => $_GET['num'], 'compare' => $_GET['compare'] );
1065
-
1066
- elseif ( isset( $_GET['amount'] ) )
1067
- $args['amount'] = $_GET['amount'];
1068
-
1069
- $log = new myCRED_Query_Log( $args );
1070
-
1071
- $log->headers = apply_filters( 'mycred_buycred_log_columns', array(
1072
- 'column-gateway' => __( 'Gateway', 'mycred' ),
1073
- 'column-username' => __( 'Buyer', 'mycred' ),
1074
- 'column-date' => __( 'Date', 'mycred' ),
1075
- 'column-amount' => $mycred->plural(),
1076
- 'column-payed' => __( 'Payed', 'mycred' ),
1077
- 'column-tranid' => __( 'Transaction ID', 'mycred' )
1078
- ) );
1079
-
1080
- $filter_url = admin_url( 'admin.php?page=myCRED_page_gateways_log_' . $type );
1081
-
1082
- ?>
1083
- <div class="wrap list" id="myCRED-wrap">
1084
- <h2><?php _e( '<strong>buy</strong>CRED Purchase Log', 'mycred' ); ?> <a href="<?php echo admin_url( 'admin.php?page=myCRED_page_gateways' ); ?>" class="click-to-toggle add-new-h2"><?php _e( 'Gateway Settings', 'mycred' ); ?></a> <a href="<?php echo $this->get_settings_url( 'buycred_module' ); ?>" class="click-to-toggle add-new-h2"><?php _e( 'buyCRED Settings', 'mycred' ); ?></a></h2>
1085
- <?php $log->filter_dates( esc_url( $filter_url ) ); ?>
1086
-
1087
- <div class="clear"></div>
1088
- <form method="get" action="">
1089
- <?php
1090
-
1091
- if ( isset( $_GET['user_id'] ) && ! empty( $_GET['user_id'] ) )
1092
- echo '<input type="hidden" name="user_id" value="' . esc_attr( $_GET['user_id'] ) . '" />';
1093
-
1094
- if ( isset( $_GET['s'] ) && ! empty( $_GET['s'] ) )
1095
- echo '<input type="hidden" name="s" value="' . esc_attr( $_GET['s'] ) . '" />';
1096
-
1097
- if ( isset( $_GET['ref'] ) && ! empty( $_GET['ref'] ) )
1098
- echo '<input type="hidden" name="ref" value="' . esc_attr( $_GET['ref'] ) . '" />';
1099
-
1100
- if ( isset( $_GET['show'] ) && ! empty( $_GET['show'] ) )
1101
- echo '<input type="hidden" name="show" value="' . esc_attr( $_GET['show'] ) . '" />';
1102
-
1103
- if ( isset( $_GET['order'] ) && ! empty( $_GET['order'] ) )
1104
- echo '<input type="hidden" name="order" value="' . esc_attr( $_GET['order'] ) . '" />';
1105
-
1106
- if ( isset( $_GET['paged'] ) && ! empty( $_GET['paged'] ) )
1107
- echo '<input type="hidden" name="paged" value="' . esc_attr( $_GET['paged'] ) . '" />';
1108
-
1109
- $log->search();
1110
-
1111
- ?>
1112
- <input type="hidden" name="page" value="<?php echo esc_attr( $_GET['page'] ); ?>" />
1113
-
1114
- <?php do_action( 'mycred_above_payment_log_table', $this ); ?>
1115
-
1116
- <div class="tablenav top">
1117
- <?php
1118
-
1119
- $log->filter_options( false, $references );
1120
- $log->navigation( 'top' );
1121
-
1122
- ?>
1123
- </div>
1124
- <table class="table wp-list-table widefat mycred-table log-entries" cellspacing="0">
1125
- <thead>
1126
- <tr>
1127
- <?php
1128
-
1129
- foreach ( $log->headers as $col_id => $col_title )
1130
- echo '<th scope="col" id="' . str_replace( 'column-', '', $col_id ) . '" class="manage-column ' . $col_id . '">' . $col_title . '</th>';
1131
-
1132
- ?>
1133
- </tr>
1134
- </thead>
1135
- <tfoot>
1136
- <tr>
1137
- <?php
1138
-
1139
- foreach ( $log->headers as $col_id => $col_title )
1140
- echo '<th scope="col" class="manage-column ' . $col_id . '">' . $col_title . '</th>';
1141
-
1142
- ?>
1143
- </tr>
1144
- </tfoot>
1145
- <tbody id="the-list">
1146
- <?php
1147
-
1148
- // If we have results
1149
- if ( $log->have_entries() ) {
1150
-
1151
- // Prep
1152
- $date_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
1153
- $entry_data = '';
1154
- $alt = 0;
1155
-
1156
- // Loop results
1157
- foreach ( $log->results as $log_entry ) {
1158
-
1159
- // Highlight alternate rows
1160
- $alt = $alt+1;
1161
- if ( $alt % 2 == 0 )
1162
- $class = ' alt';
1163
- else
1164
- $class = '';
1165
-
1166
- // Prep Sales data for use in columns
1167
- $sales_data = $this->get_sales_data_from_log_data( $log_entry->data );
1168
- list ( $buyer_id, $payer_id, $amount, $cost, $currency, $token, $other ) = $sales_data;
1169
-
1170
- // Default Currency
1171
- if ( empty( $currency ) )
1172
- $currency = 'USD';
1173
-
1174
- $gateway_name = str_replace( 'buy_creds_with_', '', $log_entry->ref );
1175
-
1176
- if ( ! array_key_exists( str_replace( '_', '-', $gateway_name ), $this->installed ) )
1177
- $style = ' style="color:silver;"';
1178
- elseif ( ! $this->is_active( str_replace( '_', '-', $gateway_name ) ) )
1179
- $style = ' style="color:gray;"';
1180
- elseif ( substr( $log_entry->entry, 0, 4 ) == 'TEST' )
1181
- $style = ' style="color:orange;"';
1182
- else
1183
- $style = '';
1184
-
1185
- echo '<tr class="myCRED-log-row' . $class . '" id="mycred-log-entry-' . $log_entry->id . '">';
1186
-
1187
- // Run though columns
1188
- foreach ( $log->headers as $column_id => $column_name ) {
1189
-
1190
- echo '<td class="' . $column_id . '"' . $style . '>';
1191
-
1192
- switch ( $column_id ) {
1193
-
1194
- // Used gateway
1195
- case 'column-gateway' :
1196
-
1197
- $gateway = str_replace( array( '-', '_' ), ' ', $gateway_name );
1198
- echo ucwords( $gateway );
1199
-
1200
- break;
1201
-
1202
- // Username Column
1203
- case 'column-username' :
1204
-
1205
- $user = get_userdata( $log_entry->user_id );
1206
- if ( $user === false )
1207
- echo 'ID: ' . $log_entry->user_id;
1208
- else
1209
- echo $user->display_name . ' <em><small>(ID: ' . $log_entry->user_id . ')</small></em>';
1210
-
1211
- break;
1212
-
1213
- // Date & Time Column
1214
- case 'column-date' :
1215
-
1216
- echo date_i18n( $date_format, $log_entry->time );
1217
-
1218
- break;
1219
-
1220
- // Amount Column
1221
- case 'column-amount' :
1222
-
1223
- echo $mycred->format_creds( $log_entry->creds );
1224
-
1225
- break;
1226
-
1227
- // Amount Paid
1228
- case 'column-payed' :
1229
-
1230
- $cost = 'n/a';
1231
- $currency = '';
1232
- $data = maybe_unserialize( $log_entry->data );
1233
- if ( array_key_exists( 'sales_data', $data ) ) {
1234
-
1235
- $sales_data = explode( '|', $data['sales_data'] );
1236
- if ( count( $sales_data ) >= 5 ) {
1237
- $cost = $sales_data[3];
1238
- $currency = $sales_data[4];
1239
- }
1240
-
1241
- }
1242
-
1243
- if ( $cost === 'n/a' )
1244
- echo 'n/a';
1245
- else
1246
- echo number_format( $cost, 2 ) . ' ' . $currency;
1247
-
1248
- break;
1249
-
1250
- // Transaction ID
1251
- case 'column-tranid' :
1252
-
1253
- $transaction_id = $log_entry->time . $log_entry->user_id;
1254
- $saved_data = maybe_unserialize( $log_entry->data );
1255
- if ( isset( $saved_data['txn_id'] ) )
1256
- $transaction_id = $saved_data['txn_id'];
1257
- elseif ( isset( $saved_data['transaction_id'] ) )
1258
- $transaction_id = $saved_data['transaction_id'];
1259
-
1260
- echo $transaction_id;
1261
-
1262
- break;
1263
-
1264
- default :
1265
-
1266
- do_action( 'mycred_payment_log_' . $column_id, $log_entry );
1267
- do_action( 'mycred_payment_log_' . $column_id . '_' . $type, $log_entry );
1268
-
1269
- break;
1270
-
1271
- }
1272
-
1273
- echo '</td>';
1274
-
1275
- }
1276
-
1277
- echo '</tr>';
1278
-
1279
- }
1280
-
1281
- }
1282
- // No log entry
1283
- else {
1284
- echo '<tr><td colspan="' . count( $log->headers ) . '" class="no-entries">' . __( 'No purchases found', 'mycred' ) . '</td></tr>';
1285
- }
1286
-
1287
- ?>
1288
- </tbody>
1289
- </table>
1290
- <div class="tablenav bottom">
1291
-
1292
- <?php $log->table_nav( 'bottom', false ); ?>
1293
-
1294
- </div>
1295
-
1296
- <?php do_action( 'mycred_below_payment_log_table', $this ); ?>
1297
-
1298
- </form>
1299
- </div>
1300
- <?php
1301
-
1302
- }
1303
-
1304
- /**
1305
- * Get Sales Data from Log Data
1306
- * @since 1.4
1307
- * @version 1.0.1
1308
- */
1309
- public function get_sales_data_from_log_data( $log_data = '' ) {
1310
-
1311
- $defaults = array( '', '', '', '', '', '', '' );
1312
- $log_data = maybe_unserialize( $log_data );
1313
-
1314
- $found_data = array();
1315
- if ( is_array( $log_data ) && array_key_exists( 'sales_data', $log_data ) ) {
1316
- if ( is_array( $log_data['sales_data'] ) )
1317
- $found_data = $log_data['sales_data'];
1318
- else
1319
- $found_data = explode( '|', $log_data['sales_data'] );
1320
- }
1321
- elseif ( ! empty( $log_data ) && ! is_array( $log_data ) ) {
1322
- $try = explode( '|', $log_data );
1323
- if ( count( $try == 7 ) )
1324
- $found_data = $log_data;
1325
- }
1326
-
1327
- return wp_parse_args( $found_data, $defaults );
1328
-
1329
- }
1330
-
1331
- /**
1332
- * Sanititze Settings
1333
- * @since 0.1
1334
- * @version 1.2
1335
- */
1336
- public function sanitize_settings( $data ) {
1337
-
1338
- $data = apply_filters( 'mycred_buycred_save_prefs', $data );
1339
-
1340
- $installed = $this->get();
1341
- if ( empty( $installed ) ) return $data;
1342
-
1343
- foreach ( $installed as $id => $gdata )
1344
- $data['gateway_prefs'][ $id ] = $this->call( 'sanitise_preferences', $installed[ $id ]['callback'], $data['gateway_prefs'][ $id ] );
1345
-
1346
- unset( $installed );
1347
- return $data;
1348
-
1349
- }
1350
-
1351
- /**
1352
- * Render Shortcode Basic
1353
- * This shortcode returns a link element to a specified payment gateway.
1354
- * @since 0.1
1355
- * @version 1.5.1
1356
- */
1357
- public function render_shortcode_basic( $atts, $title = '' ) {
1358
-
1359
- // Make sure the add-on has been setup
1360
- if ( ! isset( $this->core->buy_creds ) ) {
1361
- if ( mycred_is_admin() )
1362
- return '<p style="color:red;"><a href="' . $this->get_settings_url( 'buycred_module' ) . '">' . __( 'This Add-on needs to setup before you can use this shortcode.', 'mycred' ) . '</a></p>';
1363
- else
1364
- return '';
1365
- }
1366
-
1367
- extract( shortcode_atts( array(
1368
- 'gateway' => '',
1369
- 'ctype' => 'mycred_default',
1370
- 'amount' => '',
1371
- 'gift_to' => '',
1372
- 'class' => 'mycred-buy-link button large custom',
1373
- 'login' => $this->core->template_tags_general( $this->core->buy_creds['login'] )
1374
- ), $atts ) );
1375
-
1376
- // If we are not logged in
1377
- if ( ! is_user_logged_in() ) return '<div class="mycred-buy login">' . $this->core->template_tags_general( $login ) . '</div>';
1378
-
1379
- // Gateways
1380
- $installed = $this->get();
1381
- if ( empty( $installed ) ) return __( 'No gateways installed.', 'mycred' );
1382
- if ( ! empty( $gateway ) && ! array_key_exists( $gateway, $installed ) ) return __( 'Gateway does not exist.', 'mycred' );
1383
- if ( empty( $gateway ) || ! array_key_exists( $gateway, $installed ) ) {
1384
- reset( $installed );
1385
- $gateway = key( $installed );
1386
- }
1387
-
1388
- $buy_author = false;
1389
- $buy_member = false;
1390
- $buy_self = false;
1391
-
1392
- // Gifting is allowed
1393
- if ( $this->core->buy_creds['gifting']['authors'] == 1 ) {
1394
-
1395
- // Gift the author
1396
- if ( $gift_to == 'author' ) {
1397
- global $post;
1398
- $user_id = $post->post_author;
1399
- $buy_author = true;
1400
- }
1401
-
1402
- // Gift a specific user
1403
- elseif ( is_numeric( $gift_to ) ) {
1404
- $user_id = absint( $gift_to );
1405
- $buy_member = true;
1406
- }
1407
-
1408
- // Else gift the current user
1409
- else {
1410
- $user_id = get_current_user_id();
1411
- $buy_self = true;
1412
- }
1413
-
1414
- }
1415
-
1416
- // Gifting is not enabled
1417
- else {
1418
- $user_id = get_current_user_id();
1419
- $buy_self = true;
1420
- }
1421
-
1422
- // Adjust title
1423
- if ( $buy_self === false ) {
1424
- $user = get_userdata( (int) $user_id );
1425
- $username = $user->user_login;
1426
- $title = $this->core->template_tags_user( $title, $user );
1427
- unset( $user );
1428
- }
1429
- else {
1430
- $title = str_replace( '%display_name%', __( 'Yourself', 'mycred' ), $title );
1431
- }
1432
-
1433
- // Amount
1434
- $amount = $this->prep_shortcode_amount( $amount );
1435
-
1436
- // Title
1437
- $title = $this->prep_shortcode_title( $title );
1438
-
1439
- // Type
1440
-
1441
- // URL
1442
- $url = get_bloginfo( 'url' ) . '/';
1443
- $args = apply_filters( 'mycred_buy_args', array(
1444
- 'mycred_buy' => $gateway,
1445
- 'amount' => $this->core->number( $amount ),
1446
- 'token' => wp_create_nonce( 'mycred-buy-creds' ),
1447
- 'ctype' => $ctype
1448
- ), $atts, $buy_author, $buy_member, $buy_self );
1449
-
1450
- // Classes
1451
- $classes = explode( ' ', $class );
1452
-
1453
- if ( empty( $classes ) )
1454
- $classes = array( 'mycred-buy-link', 'button large', 'custom' );
1455
-
1456
- $classes[] = $gateway;
1457
-
1458
- if ( $buy_author || $buy_member )
1459
- $args = array_merge_recursive( $args, array( 'gift_to' => $user_id ) );
1460
-
1461
- // Element to return
1462
- $element = '<a href="' . esc_url( add_query_arg( $args, $url ) ) . '" class="' . implode( ' ', $classes ) . '" title="' . $title . '">' . $title . '</a>';
1463
-
1464
- return $element;
1465
-
1466
- }
1467
-
1468
- /**
1469
- * Render Shortcode Form
1470
- * Returns an advanced version allowing for further customizations.
1471
- * @since 0.1
1472
- * @version 1.5
1473
- */
1474
- public function render_shortcode_form( $atts, $content = '' ) {
1475
-
1476
- // Make sure the add-on has been setup
1477
- if ( ! isset( $this->core->buy_creds ) ) {
1478
- if ( mycred_is_admin() )
1479
- return '<p style="color:red;"><a href="' . $this->get_settings_url( 'buycred_module' ) . '">' . __( 'This Add-on needs to setup before you can use this shortcode.', 'mycred' ) . '</a></p>';
1480
- else
1481
- return '';
1482
- }
1483
-
1484
- extract( shortcode_atts( array(
1485
- 'button' => '',
1486
- 'gateway' => '',
1487
- 'ctype' => 'mycred_default',
1488
- 'amount' => '',
1489
- 'gift_to' => '',
1490
- 'login' => $this->core->template_tags_general( $this->core->buy_creds['login'] ),
1491
- ), $atts ) );
1492
-
1493
- // If we are not logged in
1494
- if ( ! is_user_logged_in() ) return '<p class="mycred-buy login">' . $login . '</p>';
1495
-
1496
- // Catch errors
1497
- $installed = $this->get();
1498
- if ( empty( $installed ) ) return __( 'No gateways installed.', 'mycred' );
1499
- if ( ! empty( $gateway ) && ! array_key_exists( $gateway, $installed ) ) return __( 'Gateway does not exist.', 'mycred' );
1500
- if ( empty( $this->active ) ) return __( 'No active gateways found.', 'mycred' );
1501
- if ( ! empty( $gateway ) && ! $this->is_active( $gateway ) ) return __( 'The selected gateway is not active.', 'mycred' );
1502
-
1503
- // Prep
1504
- $buy_author = false;
1505
- $buy_member = false;
1506
- $buy_others = false;
1507
- $buy_self = false;
1508
- $classes = array( 'myCRED-buy-form' );
1509
-
1510
- // Gifting is enabled
1511
- if ( $this->core->buy_creds['gifting']['authors'] ) {
1512
-
1513
- // This is a gift to the post author
1514
- if ( $gift_to == 'author' ) {
1515
- global $post;
1516
- $user_id = $post->post_author;
1517
- $buy_author = true;
1518
- }
1519
-
1520
- // This is a gift and we want to select who gets this
1521
- elseif ( $gift_to == 'select' ) {
1522
- $user_id = '';
1523
- $buy_others = true;
1524
- }
1525
-
1526
- // This is a gift to a specific user that we have provided an id for
1527
- elseif ( is_numeric( $gift_to ) ) {
1528
- $user_id = absint( $gift_to );
1529
- $buy_member = true;
1530
- }
1531
-
1532
- // This is a purchase for ourselves
1533
- else {
1534
- $user_id = get_current_user_id();
1535
- $buy_self = true;
1536
- }
1537
-
1538
- }
1539
-
1540
- // Gifting is disabled = We buy for ourselves
1541
- else {
1542
- $user_id = get_current_user_id();
1543
- $buy_self = true;
1544
- }
1545
-
1546
- // Button
1547
- if ( ! empty( $gateway ) && isset( $installed[ $gateway ]['title'] ) && empty( $button ) )
1548
- $button_label = __( 'Buy with %gateway%', 'mycred' );
1549
-
1550
- elseif ( ! empty( $button ) )
1551
- $button_label = $button;
1552
-
1553
- else
1554
- $button_label = __( 'Buy Now', 'mycred' );
1555
-
1556
- $button_label = $this->core->template_tags_general( $button_label );
1557
-
1558
- if ( ! empty( $gateway ) ) {
1559
- $gateway_name = explode( ' ', $installed[ $gateway ]['title'] );
1560
- $button_label = str_replace( '%gateway%', $gateway_name[0], $button_label );
1561
- $classes[] = $gateway_name[0];
1562
- }
1563
-
1564
- // Start constructing form with title and submit button
1565
- $form = '
1566
- <style type="text/css">
1567
- form.myCRED-buy-form { display: block; min-height: 80px; }
1568
- form.myCRED-buy-form > div { float: left; margin-right: 24px; }
1569
- form.myCRED-buy-form > div label { display: block; }
1570
- </style>
1571
- <form method="post" action="" class="' . implode( ' ', $classes ) . '">';
1572
-
1573
- // Gifting a specific user or post author
1574
- if ( $buy_author ) {
1575
- $form .= '
1576
- <input type="hidden" name="post_id" value="' . $post_id . '" />';
1577
- }
1578
-
1579
- // Gift to a specific member
1580
- elseif ( $buy_member ) {
1581
- $form .= '
1582
- <input type="hidden" name="gift_to" value="' . $user_id . '" />';
1583
- }
1584
-
1585
- // Gifting is allowed so we can select someone
1586
- elseif ( $buy_others ) {
1587
- // Select gift recipient from a drop-down
1588
- if ( $gift_to == 'select' ) {
1589
- $select = '<select name="gift_to">';
1590
- $blog_users = get_users();
1591
- if ( ! empty( $blog_users ) ) {
1592
- foreach ( $blog_users as $blog_user ) {
1593
- if ( $this->core->exclude_user( $blog_user->ID ) || $blog_user->ID === get_current_user_id() ) continue;
1594
- $select .= '<option value="' . $blog_user->ID . '">' . $blog_user->display_name . '</option>';
1595
- }
1596
- unset( $blog_users );
1597
- }
1598
- else {
1599
- $select .= '<option value="">' . __( 'No users found', 'mycred' ) . '</option>';
1600
- }
1601
- $select .= '</select>';
1602
- }
1603
- // Nominate user
1604
- else {
1605
- $select = '<input type="text" name="gift_to" value="" class="pick-user" size="20" />';
1606
- }
1607
- $form .= '
1608
- <div class="select-to">
1609
- <label>' . __( 'To', 'mycred' ) . ':</label>
1610
- ' . $select . '
1611
- </div>';
1612
- }
1613
-
1614
- // Amount
1615
- $no_of_amounts = 0;
1616
- $minimum = $this->core->number( $this->core->buy_creds['minimum'] );
1617
- if ( ! empty( $amount ) )
1618
- $no_of_amounts = sizeof( array_filter( explode( ',', $amount ), create_function( '$a', 'return !empty($a);' ) ) );
1619
-
1620
- // Multiple amounts set
1621
- if ( $no_of_amounts > 1 ) {
1622
- // Let user select from this list of amounts
1623
- $amount = explode( ',', $amount );
1624
- $form .= '
1625
- <div class="select-amount">
1626
- <label>' . __( 'Select Amount', 'mycred' ) . ':</label>
1627
- <select name="amount">';
1628
-
1629
- foreach ( $amount as $number ) {
1630
- $form .= '<option value="' . $number . '">' . $number . '</option>';
1631
- }
1632
-
1633
- $form .= '
1634
- </select>
1635
- </div>';
1636
- }
1637
-
1638
- // One amount set
1639
- elseif ( (int) $no_of_amounts == 1 ) {
1640
- $form .= '
1641
- <input type="hidden" name="amount" value="' . $this->core->number( $amount ) . '" />';
1642
- }
1643
-
1644
- // No amount set let user pick
1645
- else {
1646
- $form .= '
1647
- <div class="select-amount">
1648
- <label>' . __( 'Amount', 'mycred' ) . ':</label>
1649
- <input type="text" name="amount" value="' . $minimum . '" size="5" /><br />
1650
- <em>' . __( 'min.', 'mycred' ) . ' ' . $minimum . '</em>
1651
- </div>';
1652
- }
1653
-
1654
- // No gateway nominated - User needs to select
1655
- if ( $gateway == '' ) {
1656
- $form .= '
1657
- <div class="select-gateway">
1658
- <label>' . __( 'Select Gateway', 'mycred' ) . ':</label>
1659
- <select name="mycred_buy">';
1660
-
1661
- foreach ( $installed as $gateway_id => $data ) {
1662
- if ( ! $this->is_active( $gateway_id ) ) continue;
1663
- $form .= '<option value="' . $gateway_id . '">' . $data['title'] . '</option>';
1664
- }
1665
-
1666
- $form .= '
1667
- </select>
1668
- </div>';
1669
- }
1670
-
1671
- // Gateway is nominated - Use this
1672
- else {
1673
- $form .= '
1674
- <input type="hidden" name="mycred_buy" value="' . $gateway . '" />';
1675
- }
1676
-
1677
- $form .= '
1678
- <input type="hidden" name="token" value="' . wp_create_nonce( 'mycred-buy-creds' ) . '" />
1679
- <input type="hidden" name="transaction_id" value="' . strtoupper( wp_generate_password( 6, false, false ) ) . '" />
1680
- <input type="hidden" name="ctype" value="' . $ctype . '" />
1681
- <input type="submit" name="submit" style="margin-top:18px;" value="' . $button_label . '" class="mycred-buy button button-primary button-large btn btn-primary btn-lg" />
1682
- <div class="clear clearfix"></div>
1683
- </form>';
1684
-
1685
- return $form;
1686
-
1687
- }
1688
-
1689
- /**
1690
- * Render Shortcode Pending
1691
- * @since 1.5
1692
- * @version 1.0
1693
- */
1694
- public function render_shortcode_pending( $attr, $content = '' ) {
1695
-
1696
- // Must be logged in
1697
- if ( ! is_user_logged_in() ) return $content;
1698
-
1699
- extract( shortcode_atts( array(
1700
- 'ctype' => 'mycred_default',
1701
- 'pay_now' => __( 'Pay Now', 'mycred' ),
1702
- 'cancel' => __( 'Cancel', 'mycred' )
1703
- ), $attr ) );
1704
-
1705
- $user_id = get_current_user_id();
1706
-
1707
- global $wpdb;
1708
-
1709
- $pending = $wpdb->get_results( $wpdb->prepare( "
1710
- SELECT *
1711
- FROM {$wpdb->posts} posts
1712
- INNER JOIN {$wpdb->postmeta} meta
1713
- ON ( posts.ID = meta.post_id AND meta.meta_key = %s )
1714
- WHERE posts.post_type = %s
1715
- AND posts.post_author = %d
1716
- AND posts.post_status = %s
1717
- AND meta.meta_value = %s;", 'point_type', 'buycred_payment', $user_id, 'publish', $ctype ) );
1718
-
1719
- ob_start();
1720
-
1721
- ?>
1722
- <div id="pending-buycred-payments-<?php echo $ctype; ?>">
1723
- <table class="table">
1724
- <thead>
1725
- <tr>
1726
- <th class="column-transaction-id"><?php _e( 'Transaction ID', 'mycred' ); ?></th>
1727
- <th class="column-gateway"><?php _e( 'Gateway', 'mycred' ); ?></th>
1728
- <th class="column-amount"><?php _e( 'Amount', 'mycred' ); ?></th>
1729
- <th class="column-cost"><?php _e( 'Cost', 'mycred' ); ?></th>
1730
- <th class="column-actions"><?php _e( 'Actions', 'mycred' ); ?></th>
1731
- </tr>
1732
- </thead>
1733
- <tbody>
1734
- <?php
1735
-
1736
- if ( ! empty( $pending ) ) {
1737
-
1738
- foreach ( $pending as $post ) {
1739
-
1740
- $cancel_url = add_query_arg( array( 'buycred_cancel' => $post->post_title ) );
1741
- $revisit_url = add_query_arg( array(
1742
- 'mycred_buy' => get_post_meta( $post->ID, 'gateway', true ),
1743
- 'amount' => get_post_meta( $post->ID, 'amount', true ),
1744
- 'revisit' => $post->post_title,
1745
- 'token' => wp_create_nonce( 'mycred-buy-creds' )
1746
- ) );
1747
-
1748
- ?>
1749
- <tr>
1750
- <td class="column-transaction-id"><?php echo $post->post_title; ?></td>
1751
- <td class="column-gateway"><?php echo $this->adjust_column_content( 'gateway', $post->ID ); ?></td>
1752
- <td class="column-amount"><?php echo $this->adjust_column_content( 'amount', $post->ID ); ?></td>
1753
- <td class="column-cost"><?php echo $this->adjust_column_content( 'cost', $post->ID ); ?></td>
1754
- <td class="column-actions">
1755
- <a href="<?php echo $revisit_url; ?>"><?php echo $pay_now; ?></a> &bull; <a href="<?php echo $cancel_url; ?>"><?php echo $cancel; ?></a>
1756
- </td>
1757
- </tr>
1758
- <?php
1759
-
1760
- }
1761
-
1762
- }
1763
- else {
1764
-
1765
- ?>
1766
- <tr>
1767
- <td colspan="5"><?php _e( 'No pending payments found', 'mycred' ); ?></td>
1768
- </tr>
1769
- <?php
1770
-
1771
- }
1772
-
1773
- ?>
1774
- </tbody>
1775
- </table>
1776
- </div>
1777
- <?php
1778
-
1779
- $output = ob_get_contents();
1780
- ob_end_clean();
1781
-
1782
- return $output;
1783
-
1784
- }
1785
-
1786
- /**
1787
- * Prep Shortcode Title
1788
- * @since 0.1
1789
- * @version 1.0
1790
- */
1791
- public function prep_shortcode_title( $string = '' ) {
1792
-
1793
- $string = $this->core->allowed_tags( $string, false );
1794
- $string = trim( $string );
1795
- $title = $this->core->template_tags_general( $string );
1796
-
1797
- return $title;
1798
-
1799
- }
1800
-
1801
- /**
1802
- * Prep Shortcode Amount
1803
- * @since 0.1
1804
- * @version 1.0
1805
- */
1806
- public function prep_shortcode_amount( $amount = '' ) {
1807
-
1808
- $amount = $this->core->number( $amount );
1809
- $minimum = $this->core->number( $this->core->buy_creds['minimum'] );
1810
-
1811
- if ( empty( $amount ) || $amount < $minimum )
1812
- return $minimum;
1813
- else
1814
- return $amount;
1815
-
1816
- }
1817
-
1818
- /**
1819
- * Prep Shortcode Gifting
1820
- * @since 0.1
1821
- * @version 1.0
1822
- */
1823
- public function prep_shortcode_gifting( $gift_to = '' ) {
1824
-
1825
- if ( empty( $gift_to ) ) return $gift_to;
1826
-
1827
- if ( $this->core->buy_creds['gifting']['authors'] == 1 && $gift_to == 'author' && in_the_loop() )
1828
- return $GLOBALS['post']->post_author;
1829
-
1830
- if ( $this->core->buy_creds['gifting']['members'] == 1 )
1831
- return abs( $gift_to );
1832
-
1833
- return false;
1834
-
1835
- }
1836
-
1837
- }
1838
-
1839
- $buy_creds = new myCRED_buyCRED_Module();
1840
- $buy_creds->load();
1841
 
1842
- endif;
1843
  ?>
2
  /**
3
  * Addon: buyCRED
4
  * Addon URI: http://mycred.me/add-ons/buycred/
5
+ * Version: 1.5
 
 
 
6
  */
7
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
 
9
+ define( 'MYCRED_PURCHASE', __FILE__ );
10
+ define( 'MYCRED_PURCHASE_VERSION', '1.5' );
11
+ define( 'MYCRED_PURCHASE_DIR', myCRED_ADDONS_DIR . 'buy-creds/' );
12
+ define( 'MYCRED_BUYCRED_ABSTRACT_DIR', MYCRED_PURCHASE_DIR . 'abstracts/' );
13
+ define( 'MYCRED_BUYCRED_GATEWAYS_DIR', MYCRED_PURCHASE_DIR . 'gateways/' );
14
+ define( 'MYCRED_BUYCRED_MODULES_DIR', MYCRED_PURCHASE_DIR . 'modules/' );
15
+ define( 'MYCRED_BUYCRED_INCLUDES_DIR', MYCRED_PURCHASE_DIR . 'includes/' );
16
+
17
+ if ( ! defined( 'MYCRED_BUYCRED_PENDING_COMMENTS' ) )
18
+ define( 'MYCRED_BUYCRED_PENDING_COMMENTS', true );
19
 
20
  /**
21
+ * Load Dependencies
22
  */
23
+ require_once MYCRED_BUYCRED_ABSTRACT_DIR . 'mycred-abstract-payment-gateway.php';
24
+ require_once MYCRED_BUYCRED_INCLUDES_DIR . 'buycred-functions.php';
25
 
26
  /**
27
+ * Load Built-in Gateways
28
+ * @since 1.4
29
+ * @version 1.0
30
  */
31
+ require_once MYCRED_BUYCRED_GATEWAYS_DIR . 'paypal-standard.php';
32
+ require_once MYCRED_BUYCRED_GATEWAYS_DIR . 'bitpay.php';
33
+ require_once MYCRED_BUYCRED_GATEWAYS_DIR . 'netbilling.php';
34
+ require_once MYCRED_BUYCRED_GATEWAYS_DIR . 'skrill.php';
35
+ require_once MYCRED_BUYCRED_GATEWAYS_DIR . 'zombaio.php';
36
+ require_once MYCRED_BUYCRED_GATEWAYS_DIR . 'bank-transfer.php';
37
 
38
  do_action( 'mycred_buycred_load_gateways' );
39
 
40
  /**
41
+ * Load Modules
42
+ * @since 1.7
43
+ * @version 1.0
44
  */
45
+ require_once MYCRED_BUYCRED_MODULES_DIR . 'buycred-module-core.php';
46
+ require_once MYCRED_BUYCRED_MODULES_DIR . 'buycred-module-pending.php';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
 
48
  ?>
addons/coupons/includes/mycred-coupon-functions.php CHANGED
@@ -1,6 +1,107 @@
1
  <?php
2
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  /**
5
  * Create New Coupon
6
  * Creates a new myCRED coupon post.
@@ -9,46 +110,56 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
9
  * @returns false if data is missing, post ID on success or wp_error / 0 if
10
  * post creation failed.
11
  * @since 1.4
12
- * @version 1.0
13
  */
14
  if ( ! function_exists( 'mycred_create_new_coupon' ) ) :
15
  function mycred_create_new_coupon( $data = array() ) {
 
16
  // Required data is missing
17
  if ( empty( $data ) ) return false;
18
 
19
  // Apply defaults
20
  extract( shortcode_atts( array(
21
- 'code' => mycred_get_unique_coupon_code(),
22
- 'value' => 0,
23
- 'global_max' => 1,
24
- 'user_max' => 1,
25
- 'min_balance' => 0,
26
- 'max_balance' => 0,
27
- 'expires' => ''
 
 
 
28
  ), $data ) );
29
 
30
  // Create Coupon Post
31
  $post_id = wp_insert_post( apply_filters( 'mycred_create_new_coupon_post', array(
32
- 'post_type' => 'mycred_coupon',
33
- 'post_title' => $code,
34
- 'post_status' => 'publish'
 
 
35
  ), $data ) );
36
 
37
  // Error
38
  if ( $post_id !== 0 && ! is_wp_error( $post_id ) ) {
39
 
40
  // Save Coupon Details
41
- add_post_meta( $post_id, 'value', $value );
42
- add_post_meta( $post_id, 'global', $global_max );
43
- add_post_meta( $post_id, 'user', $user_max );
44
- add_post_meta( $post_id, 'min', $min_balance );
45
- add_post_meta( $post_id, 'max', $max_balance );
 
 
 
46
  if ( ! empty( $expires ) )
47
  add_post_meta( $post_id, 'expires', $expires );
48
 
49
  }
50
 
51
  return apply_filters( 'mycred_create_new_coupon', $post_id, $data );
 
52
  }
53
  endif;
54
 
@@ -61,16 +172,18 @@ endif;
61
  */
62
  if ( ! function_exists( 'mycred_get_unique_coupon_code' ) ) :
63
  function mycred_get_unique_coupon_code() {
 
64
  global $wpdb;
65
 
66
  do {
67
 
68
- $id = strtoupper( wp_generate_password( 12, false, false ) );
69
  $query = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s;", $id, 'mycred_coupon' ) );
70
 
71
  } while ( ! empty( $query ) );
72
 
73
  return apply_filters( 'mycred_get_unique_coupon_code', $id );
 
74
  }
75
  endif;
76
 
@@ -82,7 +195,9 @@ endif;
82
  */
83
  if ( ! function_exists( 'mycred_get_coupon_post' ) ) :
84
  function mycred_get_coupon_post( $code = '' ) {
 
85
  return apply_filters( 'mycred_get_coupon_by_code', get_page_by_title( strtoupper( $code ), 'OBJECT', 'mycred_coupon' ), $code );
 
86
  }
87
  endif;
88
 
@@ -92,7 +207,7 @@ endif;
92
  * to a given user. Requires you to provide a log entry template.
93
  * @action mycred_use_coupon
94
  * @since 1.4
95
- * @version 1.0.2
96
  */
97
  if ( ! function_exists( 'mycred_use_coupon' ) ) :
98
  function mycred_use_coupon( $code = '', $user_id = 0 ) {
@@ -100,6 +215,8 @@ if ( ! function_exists( 'mycred_use_coupon' ) ) :
100
  // Missing required information
101
  if ( empty( $code ) || $user_id === 0 ) return 'missing';
102
 
 
 
103
  // Get coupon by code (post title)
104
  $coupon = mycred_get_coupon_post( $code );
105
 
@@ -108,78 +225,127 @@ if ( ! function_exists( 'mycred_use_coupon' ) ) :
108
  return 'missing';
109
 
110
  // Check Expiration
111
- $now = current_time( 'timestamp' );
112
  $expires = mycred_get_coupon_expire_date( $coupon->ID, true );
113
- if ( ! empty( $expires ) && $expires !== 0 && $expires <= $now ) {
114
- wp_trash_post( $coupon->ID );
115
- return 'expired';
116
- }
117
 
118
  // Get Global Count
119
- $global_count = mycred_get_global_coupon_count( $coupon->ID );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
- // We start with enforcing the global count
122
- $global_max = mycred_get_coupon_global_max( $coupon->ID );
123
- if ( $global_count >= $global_max ) {
124
- wp_trash_post( $coupon->ID );
125
- return 'expired';
126
  }
127
 
128
  $type = get_post_meta( $coupon->ID, 'type', true );
129
  if ( $type == '' )
130
- $type = 'mycred_default';
131
 
132
  $mycred = mycred( $type );
133
 
134
- // Get User max
135
- $user_count = mycred_get_users_coupon_count( $code, $user_id );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
- // Next we enforce the user max
138
- $user_max = mycred_get_coupon_user_max( $coupon->ID );
139
- if ( $user_count >= $user_max )
140
- return 'max';
141
 
142
- // Min balance requirement
143
- $users_balance = $mycred->get_users_cred( $user_id, $type );
144
- $min_balance = mycred_get_coupon_min_balance( $coupon->ID );
145
- if ( $min_balance > $mycred->zero() && $users_balance < $min_balance )
146
- return 'min_balance';
147
 
148
- // Max balance requirement
149
- $max_balance = mycred_get_coupon_max_balance( $coupon->ID );
150
- if ( $max_balance > $mycred->zero() && $users_balance >= $max_balance )
151
- return 'max_balance';
 
 
152
 
153
  // Ready to use coupon!
154
- $value = mycred_get_coupon_value( $coupon->ID );
155
- $value = $mycred->number( $value );
156
-
157
- // Get Coupon log template
158
- if ( ! isset( $mycred->core['coupons']['log'] ) )
159
- $mycred->core['coupons']['log'] = 'Coupon redemption';
160
-
161
- // Apply Coupon
162
- $mycred->add_creds(
163
- 'coupon',
164
- $user_id,
165
- $value,
166
- $mycred->core['coupons']['log'],
167
- $coupon->ID,
168
- $code,
169
- $type
170
- );
171
-
172
- do_action( 'mycred_use_coupon', $user_id, $coupon );
173
-
174
- // Increment global counter
175
- $global_count ++;
176
- update_post_meta( $coupon->ID, 'global_count', $global_count );
177
-
178
- // If the updated counter reaches the max, trash the coupon now
179
- if ( $global_count >= $global_max )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  wp_trash_post( $coupon->ID );
181
 
182
- return $mycred->number( $users_balance+$value );
 
 
183
 
184
  }
185
  endif;
@@ -193,6 +359,7 @@ endif;
193
  */
194
  if ( ! function_exists( 'mycred_get_users_coupon_count' ) ) :
195
  function mycred_get_users_coupon_count( $code = '', $user_id = '' ) {
 
196
  global $wpdb, $mycred;
197
 
198
  // Count how many times a given user has used a given coupon
@@ -204,6 +371,7 @@ if ( ! function_exists( 'mycred_get_users_coupon_count' ) ) :
204
  AND data = %s;", 'coupon', $user_id, $code ) );
205
 
206
  return apply_filters( 'mycred_get_users_coupon_count', $result, $code, $user_id );
 
207
  }
208
  endif;
209
 
@@ -211,69 +379,22 @@ endif;
211
  * Get Coupons Global Count
212
  * @filter mycred_get_global_coupon_count
213
  * @since 1.4
214
- * @version 1.0
215
  */
216
  if ( ! function_exists( 'mycred_get_global_coupon_count' ) ) :
217
  function mycred_get_global_coupon_count( $post_id = 0 ) {
218
- $count = get_post_meta( $post_id, 'global_count', true );
219
- if ( $count == '' )
220
- $count = 0;
221
 
222
- return apply_filters( 'mycred_get_global_coupon_count', $count, $post_id );
223
- }
224
- endif;
225
 
226
- /**
227
- * Get Coupon Value
228
- * @filter mycred_coupon_value
229
- * @since 1.4
230
- * @version 1.0
231
- */
232
- if ( ! function_exists( 'mycred_get_coupon_value' ) ) :
233
- function mycred_get_coupon_value( $post_id = 0 ) {
234
- return apply_filters( 'mycred_coupon_value', get_post_meta( $post_id, 'value', true ), $post_id );
235
- }
236
- endif;
237
 
238
- /**
239
- * Get Coupon Expire Date
240
- * @filter mycred_coupon_max_balance
241
- * @since 1.4
242
- * @version 1.0
243
- */
244
- if ( ! function_exists( 'mycred_get_coupon_expire_date' ) ) :
245
- function mycred_get_coupon_expire_date( $post_id = 0, $unix = false ) {
246
- $date = get_post_meta( $post_id, 'expires', true );
247
-
248
- if ( ! empty( $date ) && $unix )
249
- $date = strtotime( $date );
250
-
251
- return apply_filters( 'mycred_coupon_expires', $date, $post_id, $unix );
252
- }
253
- endif;
254
 
255
- /**
256
- * Get Coupon User Max
257
- * The maximum number a user can use this coupon.
258
- * @filter mycred_coupon_user_max
259
- * @since 1.4
260
- * @version 1.0
261
- */
262
- if ( ! function_exists( 'mycred_get_coupon_user_max' ) ) :
263
- function mycred_get_coupon_user_max( $post_id = 0 ) {
264
- return apply_filters( 'mycred_coupon_user_max', get_post_meta( $post_id, 'user', true ), $post_id );
265
- }
266
- endif;
267
 
268
- /**
269
- * Get Coupons Global Max
270
- * @filter mycred_coupon_global_max
271
- * @since 1.4
272
- * @version 1.0
273
- */
274
- if ( ! function_exists( 'mycred_get_coupon_global_max' ) ) :
275
- function mycred_get_coupon_global_max( $post_id = 0 ) {
276
- return apply_filters( 'mycred_coupon_global_max', get_post_meta( $post_id, 'global', true ), $post_id );
277
  }
278
  endif;
279
 
@@ -281,11 +402,22 @@ endif;
281
  * Get Coupons Minimum Balance Requirement
282
  * @filter mycred_coupon_min_balance
283
  * @since 1.4
284
- * @version 1.0.1
285
  */
286
  if ( ! function_exists( 'mycred_get_coupon_min_balance' ) ) :
287
  function mycred_get_coupon_min_balance( $post_id = 0 ) {
288
- return apply_filters( 'mycred_coupon_min_balance', get_post_meta( $post_id, 'min_balance', true ), $post_id );
 
 
 
 
 
 
 
 
 
 
 
289
  }
290
  endif;
291
 
@@ -293,11 +425,22 @@ endif;
293
  * Get Coupons Maximum Balance Requirement
294
  * @filter mycred_coupon_max_balance
295
  * @since 1.4
296
- * @version 1.0.1
297
  */
298
  if ( ! function_exists( 'mycred_get_coupon_max_balance' ) ) :
299
  function mycred_get_coupon_max_balance( $post_id = 0 ) {
300
- return apply_filters( 'mycred_coupon_max_balance', get_post_meta( $post_id, 'max_balance', true ), $post_id );
 
 
 
 
 
 
 
 
 
 
 
301
  }
302
  endif;
303
  ?>
1
  <?php
2
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
 
4
+ /**
5
+ * Get Coupon
6
+ * Returns a coupon object based on the post ID.
7
+ * @since 1.7
8
+ * @version 1.0
9
+ */
10
+ if ( ! function_exists( 'mycred_get_coupon' ) ) :
11
+ function mycred_get_coupon( $coupon_post_id = NULL ) {
12
+
13
+ if ( $coupon_post_id === NULL || get_post_type( $coupon_post_id ) != 'mycred_coupon' ) return false;
14
+
15
+ $coupon = new StdClass();
16
+ $coupon->post_id = absint( $coupon_post_id );
17
+ $coupon->code = get_the_title( $coupon_post_id );
18
+ $coupon->value = mycred_get_coupon_value( $coupon_post_id );
19
+ $coupon->point_type = get_post_meta( $coupon_post_id, 'type', true );
20
+ $coupon->max_global = mycred_get_coupon_global_max( $coupon_post_id );
21
+ $coupon->max_user = mycred_get_coupon_user_max( $coupon_post_id );
22
+ $coupon->requires_min = mycred_get_coupon_min_balance( $coupon_post_id );
23
+ $coupon->requires_min_type = get_post_meta( $coupon_post_id, 'min_balance_type', true );
24
+ $coupon->requires_max = mycred_get_coupon_max_balance( $coupon_post_id );
25
+ $coupon->requires_max_type = get_post_meta( $coupon_post_id, 'max_balance_type', true );
26
+ $coupon->expires = mycred_get_coupon_expire_date( $coupon_post_id );
27
+ $coupon->used = mycred_get_global_coupon_count( $coupon_post_id );
28
+
29
+ if ( ! mycred_point_type_exists( $coupon->point_type ) )
30
+ $coupon->point_type = MYCRED_DEFAULT_TYPE_KEY;
31
+
32
+ if ( ! mycred_point_type_exists( $coupon->requires_min_type ) )
33
+ $coupon->requires_min_type = MYCRED_DEFAULT_TYPE_KEY;
34
+
35
+ if ( ! mycred_point_type_exists( $coupon->requires_max_type ) )
36
+ $coupon->requires_max_type = MYCRED_DEFAULT_TYPE_KEY;
37
+
38
+ return apply_filters( 'mycred_get_coupon', $coupon, $coupon_post_id );
39
+
40
+ }
41
+ endif;
42
+
43
+ /**
44
+ * Get Coupon Value
45
+ * @filter mycred_coupon_value
46
+ * @since 1.4
47
+ * @version 1.0
48
+ */
49
+ if ( ! function_exists( 'mycred_get_coupon_value' ) ) :
50
+ function mycred_get_coupon_value( $post_id = 0 ) {
51
+
52
+ return apply_filters( 'mycred_coupon_value', get_post_meta( $post_id, 'value', true ), $post_id );
53
+
54
+ }
55
+ endif;
56
+
57
+ /**
58
+ * Get Coupon Expire Date
59
+ * @filter mycred_coupon_max_balance
60
+ * @since 1.4
61
+ * @version 1.0
62
+ */
63
+ if ( ! function_exists( 'mycred_get_coupon_expire_date' ) ) :
64
+ function mycred_get_coupon_expire_date( $post_id = 0, $unix = false ) {
65
+
66
+ $date = get_post_meta( $post_id, 'expires', true );
67
+
68
+ if ( ! empty( $date ) && $unix )
69
+ $date = strtotime( $date );
70
+
71
+ return apply_filters( 'mycred_coupon_expires', $date, $post_id, $unix );
72
+
73
+ }
74
+ endif;
75
+
76
+ /**
77
+ * Get Coupon User Max
78
+ * The maximum number a user can use this coupon.
79
+ * @filter mycred_coupon_user_max
80
+ * @since 1.4
81
+ * @version 1.0
82
+ */
83
+ if ( ! function_exists( 'mycred_get_coupon_user_max' ) ) :
84
+ function mycred_get_coupon_user_max( $post_id = 0 ) {
85
+
86
+ return apply_filters( 'mycred_coupon_user_max', get_post_meta( $post_id, 'user', true ), $post_id );
87
+
88
+ }
89
+ endif;
90
+
91
+ /**
92
+ * Get Coupons Global Max
93
+ * @filter mycred_coupon_global_max
94
+ * @since 1.4
95
+ * @version 1.0
96
+ */
97
+ if ( ! function_exists( 'mycred_get_coupon_global_max' ) ) :
98
+ function mycred_get_coupon_global_max( $post_id = 0 ) {
99
+
100
+ return apply_filters( 'mycred_coupon_global_max', get_post_meta( $post_id, 'global', true ), $post_id );
101
+
102
+ }
103
+ endif;
104
+
105
  /**
106
  * Create New Coupon
107
  * Creates a new myCRED coupon post.
110
  * @returns false if data is missing, post ID on success or wp_error / 0 if
111
  * post creation failed.
112
  * @since 1.4
113
+ * @version 1.1
114
  */
115
  if ( ! function_exists( 'mycred_create_new_coupon' ) ) :
116
  function mycred_create_new_coupon( $data = array() ) {
117
+
118
  // Required data is missing
119
  if ( empty( $data ) ) return false;
120
 
121
  // Apply defaults
122
  extract( shortcode_atts( array(
123
+ 'code' => mycred_get_unique_coupon_code(),
124
+ 'value' => 0,
125
+ 'global_max' => 1,
126
+ 'user_max' => 1,
127
+ 'min_balance' => 0,
128
+ 'min_balance_type' => MYCRED_DEFAULT_TYPE_KEY,
129
+ 'max_balance' => 0,
130
+ 'max_balance_type' => MYCRED_DEFAULT_TYPE_KEY,
131
+ 'expires' => '',
132
+ 'type' => MYCRED_DEFAULT_TYPE_KEY
133
  ), $data ) );
134
 
135
  // Create Coupon Post
136
  $post_id = wp_insert_post( apply_filters( 'mycred_create_new_coupon_post', array(
137
+ 'post_type' => 'mycred_coupon',
138
+ 'post_title' => $code,
139
+ 'post_status' => 'publish',
140
+ 'comment_status' => 'closed',
141
+ 'ping_status' => 'closed'
142
  ), $data ) );
143
 
144
  // Error
145
  if ( $post_id !== 0 && ! is_wp_error( $post_id ) ) {
146
 
147
  // Save Coupon Details
148
+ add_post_meta( $post_id, 'value', $value, true );
149
+ add_post_meta( $post_id, 'global', $global_max, true );
150
+ add_post_meta( $post_id, 'user', $user_max, true );
151
+ add_post_meta( $post_id, 'min', $min_balance, true );
152
+ add_post_meta( $post_id, 'min_balance_type', $min_balance_type, true );
153
+ add_post_meta( $post_id, 'max', $max_balance, true );
154
+ add_post_meta( $post_id, 'max_balance_type', $max_balance_type, true );
155
+
156
  if ( ! empty( $expires ) )
157
  add_post_meta( $post_id, 'expires', $expires );
158
 
159
  }
160
 
161
  return apply_filters( 'mycred_create_new_coupon', $post_id, $data );
162
+
163
  }
164
  endif;
165
 
172
  */
173
  if ( ! function_exists( 'mycred_get_unique_coupon_code' ) ) :
174
  function mycred_get_unique_coupon_code() {
175
+
176
  global $wpdb;
177
 
178
  do {
179
 
180
+ $id = strtoupper( wp_generate_password( 12, false, false ) );
181
  $query = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s;", $id, 'mycred_coupon' ) );
182
 
183
  } while ( ! empty( $query ) );
184
 
185
  return apply_filters( 'mycred_get_unique_coupon_code', $id );
186
+
187
  }
188
  endif;
189
 
195
  */
196
  if ( ! function_exists( 'mycred_get_coupon_post' ) ) :
197
  function mycred_get_coupon_post( $code = '' ) {
198
+
199
  return apply_filters( 'mycred_get_coupon_by_code', get_page_by_title( strtoupper( $code ), 'OBJECT', 'mycred_coupon' ), $code );
200
+
201
  }
202
  endif;
203
 
207
  * to a given user. Requires you to provide a log entry template.
208
  * @action mycred_use_coupon
209
  * @since 1.4
210
+ * @version 1.1
211
  */
212
  if ( ! function_exists( 'mycred_use_coupon' ) ) :
213
  function mycred_use_coupon( $code = '', $user_id = 0 ) {
215
  // Missing required information
216
  if ( empty( $code ) || $user_id === 0 ) return 'missing';
217
 
218
+ $can_use = true;
219
+
220
  // Get coupon by code (post title)
221
  $coupon = mycred_get_coupon_post( $code );
222
 
225
  return 'missing';
226
 
227
  // Check Expiration
228
+ $now = current_time( 'timestamp' );
229
  $expires = mycred_get_coupon_expire_date( $coupon->ID, true );
230
+ if ( ! empty( $expires ) && $expires !== 0 && $expires <= $now )
231
+ $can_use = 'expired';
 
 
232
 
233
  // Get Global Count
234
+ if ( $can_use === true ) {
235
+
236
+ $global_count = mycred_get_global_coupon_count( $coupon->ID );
237
+ $global_max = mycred_get_coupon_global_max( $coupon->ID );
238
+
239
+ if ( $global_count >= $global_max )
240
+ $can_use = 'expired';
241
+
242
+ }
243
+
244
+ // Get User max
245
+ if ( $can_use === true ) {
246
+
247
+ $user_count = mycred_get_users_coupon_count( $code, $user_id );
248
+ $user_max = mycred_get_coupon_user_max( $coupon->ID );
249
+
250
+ if ( $user_count >= $user_max )
251
+ $can_use = 'max';
252
 
 
 
 
 
 
253
  }
254
 
255
  $type = get_post_meta( $coupon->ID, 'type', true );
256
  if ( $type == '' )
257
+ $type = MYCRED_DEFAULT_TYPE_KEY;
258
 
259
  $mycred = mycred( $type );
260
 
261
+ if ( $can_use === true ) {
262
+
263
+ // Min balance requirement
264
+ $users_balance = $mycred->get_users_balance( $user_id, $type );
265
+ $min_balance = mycred_get_coupon_min_balance( $coupon->ID );
266
+
267
+ if ( $min_balance['type'] != $type ) {
268
+
269
+ $mycred = mycred( $min_balance['type'] );
270
+ $users_balance = $mycred->get_users_balance( $user_id, $min_balance['type'] );
271
+
272
+ }
273
+
274
+ if ( $mycred->number( $min_balance['value'] ) > $mycred->zero() && $users_balance < $mycred->number( $min_balance['value'] ) )
275
+ $can_use = 'min_balance';
276
+
277
+ // Max balance requirement
278
+ if ( $can_use === true ) {
279
+
280
+ $max_balance = mycred_get_coupon_max_balance( $coupon->ID );
281
+
282
+ if ( $max_balance['type'] != $type ) {
283
+
284
+ $mycred = mycred( $max_balance['type'] );
285
+ $users_balance = $mycred->get_users_balance( $user_id, $max_balance['type'] );
286
 
287
+ }
 
 
 
288
 
289
+ if ( $mycred->number( $max_balance['value'] ) > $mycred->zero() && $users_balance >= $mycred->number( $max_balance['value'] ) )
290
+ $can_use = 'max_balance';
 
 
 
291
 
292
+ }
293
+
294
+ }
295
+
296
+ // Let other play and change the value of $can_use
297
+ $can_use = apply_filters( 'mycred_can_use_coupon', $can_use, $code, $user_id, $coupon );
298
 
299
  // Ready to use coupon!
300
+ if ( $can_use === true ) {
301
+
302
+ $mycred = mycred( $type );
303
+
304
+ $value = mycred_get_coupon_value( $coupon->ID );
305
+ $value = $mycred->number( $value );
306
+
307
+ // Get Coupon log template
308
+ if ( ! isset( $mycred->core['coupons']['log'] ) )
309
+ $mycred->core['coupons']['log'] = 'Coupon redemption';
310
+
311
+ // Apply Coupon
312
+ $mycred->add_creds(
313
+ 'coupon',
314
+ $user_id,
315
+ $value,
316
+ $mycred->core['coupons']['log'],
317
+ $coupon->ID,
318
+ $code,
319
+ $type
320
+ );
321
+
322
+ do_action( 'mycred_use_coupon', $user_id, $coupon );
323
+
324
+ // Increment global counter
325
+ $global_count ++;
326
+ update_post_meta( $coupon->ID, 'global_count', $global_count );
327
+
328
+ // If the updated counter reaches the max, trash the coupon now
329
+ if ( $global_count >= $global_max )
330
+ wp_trash_post( $coupon->ID );
331
+
332
+ // This should resolves issues where caching prevents the new global count from being loaded.
333
+ else {
334
+ clean_post_cache( $coupon->ID );
335
+ }
336
+
337
+ return $mycred->number( $users_balance+$value );
338
+
339
+ }
340
+
341
+ // Trash expired coupons to preent further usage
342
+ elseif ( $can_use == 'expired' ) {
343
+
344
  wp_trash_post( $coupon->ID );
345
 
346
+ }
347
+
348
+ return $can_use;
349
 
350
  }
351
  endif;
359
  */
360
  if ( ! function_exists( 'mycred_get_users_coupon_count' ) ) :
361
  function mycred_get_users_coupon_count( $code = '', $user_id = '' ) {
362
+
363
  global $wpdb, $mycred;
364
 
365
  // Count how many times a given user has used a given coupon
371
  AND data = %s;", 'coupon', $user_id, $code ) );
372
 
373
  return apply_filters( 'mycred_get_users_coupon_count', $result, $code, $user_id );
374
+
375
  }
376
  endif;
377
 
379
  * Get Coupons Global Count
380
  * @filter mycred_get_global_coupon_count
381
  * @since 1.4
382
+ * @version 1.1
383
  */
384
  if ( ! function_exists( 'mycred_get_global_coupon_count' ) ) :
385
  function mycred_get_global_coupon_count( $post_id = 0 ) {
 
 
 
386
 
387
+ global $wpdb, $mycred;
 
 
388
 
389
+ $count = $wpdb->get_var( $wpdb->prepare( "
390
+ SELECT COUNT(*)
391
+ FROM {$mycred->log_table}
392
+ WHERE ref = 'coupon' AND ref_id = %d;", $post_id ) );
 
 
 
 
 
 
 
393
 
394
+ if ( $count === NULL ) $count = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
 
396
+ return apply_filters( 'mycred_get_global_coupon_count', $count, $post_id );
 
 
 
 
 
 
 
 
 
 
 
397
 
 
 
 
 
 
 
 
 
 
398
  }
399
  endif;
400
 
402
  * Get Coupons Minimum Balance Requirement
403
  * @filter mycred_coupon_min_balance
404
  * @since 1.4
405
+ * @version 1.1
406
  */
407
  if ( ! function_exists( 'mycred_get_coupon_min_balance' ) ) :
408
  function mycred_get_coupon_min_balance( $post_id = 0 ) {
409
+
410
+ $type = get_post_meta( $post_id, 'min_balance_type', true );
411
+ if ( ! mycred_point_type_exists( $type ) ) $type = MYCRED_DEFAULT_TYPE_KEY;
412
+
413
+ $min = get_post_meta( $post_id, 'min_balance', true );
414
+ if ( $min == '' ) $min == 0;
415
+
416
+ return apply_filters( 'mycred_coupon_min_balance', array(
417
+ 'type' => $type,
418
+ 'value' => $min
419
+ ), $post_id );
420
+
421
  }
422
  endif;
423
 
425
  * Get Coupons Maximum Balance Requirement
426
  * @filter mycred_coupon_max_balance
427
  * @since 1.4
428
+ * @version 1.1
429
  */
430
  if ( ! function_exists( 'mycred_get_coupon_max_balance' ) ) :
431
  function mycred_get_coupon_max_balance( $post_id = 0 ) {
432
+
433
+ $type = get_post_meta( $post_id, 'max_balance_type', true );
434
+ if ( ! mycred_point_type_exists( $type ) ) $type = MYCRED_DEFAULT_TYPE_KEY;
435
+
436
+ $max = get_post_meta( $post_id, 'max_balance', true );
437
+ if ( $max == '' ) $max == 0;
438
+
439
+ return apply_filters( 'mycred_coupon_max_balance', array(
440
+ 'type' => $type,
441
+ 'value' => $max
442
+ ), $post_id );
443
+
444
  }
445
  endif;
446
  ?>
addons/coupons/includes/mycred-coupon-shortcodes.php CHANGED
@@ -15,8 +15,9 @@ if ( ! function_exists( 'mycred_render_shortcode_load_coupon' ) ) :
15
  return $content;
16
 
17
  extract( shortcode_atts( array(
18
- 'label' => __( 'Coupon', 'mycred' ),
19
- 'button' => __( 'Apply Coupon', 'mycred' )
 
20
  ), $atts ) );
21
 
22
  $mycred = mycred();
@@ -39,46 +40,51 @@ if ( ! function_exists( 'mycred_render_shortcode_load_coupon' ) ) :
39
 
40
  // Coupon does not exist
41
  if ( $load === 'missing' )
42
- $output .= '<p class="mycred-coupon-status">' . $mycred->coupons['invalid'] . '</p>';
43
 
44
  // Coupon has expired
45
  elseif ( $load === 'expired' )
46
- $output .= '<p class="mycred-coupon-status">' . $mycred->coupons['expired'] . '</p>';
47
 
48
  // User limit reached
49
  elseif ( $load === 'max' )
50
- $output .= '<p class="mycred-coupon-status">' . $mycred->coupons['user_limit'] . '</p>';
51
 
52
  // Failed minimum balance requirement
53
  elseif ( $load === 'min_balance' ) {
54
  $min = get_post_meta( $coupon->ID, 'min', true );
55
  $template = str_replace( '%min%', $min, $mycred->coupons['min'] );
56
- $output .= '<p class="mycred-coupon-status">' . $template . '</p>';
57
  }
58
 
59
  // Failed maximum balance requirement
60
  elseif ( $load === 'max_balance' ) {
61
  $max = get_post_meta( $coupon->ID, 'max', true );
62
  $template = str_replace( '%max%', $max, $mycred->coupons['max'] );
63
- $output .= '<p class="mycred-coupon-status">' . $template . '</p>';
64
  }
65
 
66
  // Success
67
  else
68
- $output .= '<p class="mycred-coupon-status">' . $mycred->coupons['success'] . '</p>';
69
 
70
  }
71
 
72
  }
73
 
 
 
 
74
  $output .= '
75
- <form action="" method="post">
76
- <p>
77
- <label for="mycred-coupon-code">' . $label . '</label><br />
78
- <input type="text" name="mycred_coupon_load[couponkey]" id="mycred-coupon-couponkey" value="" />
 
 
79
  <input type="hidden" name="mycred_coupon_load[token]" value="' . wp_create_nonce( 'mycred-load-coupon' . $user_id ) . '" />
80
- <input type="submit" class="btn btn-primary btn-large button button-large button-primary" value="' . $button . '" />
81
- </p>
82
  </form>
83
  </div>';
84
 
15
  return $content;
16
 
17
  extract( shortcode_atts( array(
18
+ 'label' => 'Coupon',
19
+ 'button' => 'Apply Coupon',
20
+ 'placeholder' => ''
21
  ), $atts ) );
22
 
23
  $mycred = mycred();
40
 
41
  // Coupon does not exist
42
  if ( $load === 'missing' )
43
+ $output .= '<div class="alert alert-danger">' . $mycred->coupons['invalid'] . '</div>';
44
 
45
  // Coupon has expired
46
  elseif ( $load === 'expired' )
47
+ $output .= '<div class="alert alert-danger">' . $mycred->coupons['expired'] . '</div>';
48
 
49
  // User limit reached
50
  elseif ( $load === 'max' )
51
+ $output .= '<div class="alert alert-warning">' . $mycred->coupons['user_limit'] . '</div>';
52
 
53
  // Failed minimum balance requirement
54
  elseif ( $load === 'min_balance' ) {
55
  $min = get_post_meta( $coupon->ID, 'min', true );
56
  $template = str_replace( '%min%', $min, $mycred->coupons['min'] );
57
+ $output .= '<div class="alert alert-danger">' . $template . '</div>';
58
  }
59
 
60
  // Failed maximum balance requirement
61
  elseif ( $load === 'max_balance' ) {
62
  $max = get_post_meta( $coupon->ID, 'max', true );
63
  $template = str_replace( '%max%', $max, $mycred->coupons['max'] );
64
+ $output .= '<div class="alert alert-danger">' . $template . '</div>';
65
  }
66
 
67
  // Success
68
  else
69
+ $output .= '<div class="alert alert-success">' . $mycred->coupons['success'] . '</div>';
70
 
71
  }
72
 
73
  }
74
 
75
+ if ( $label != '' )
76
+ $label = '<label for="mycred-coupon-code">' . $label . '</label>';
77
+
78
  $output .= '
79
+ <form action="" method="post" class="form-inline">
80
+ <div class="form-group">
81
+ ' . $label . '
82
+ <input type="text" name="mycred_coupon_load[couponkey]" placeholder="' . esc_attr( $placeholder ) . '" id="mycred-coupon-couponkey" class="form-control" value="" />
83
+ </div>
84
+ <div class="form-group">
85
  <input type="hidden" name="mycred_coupon_load[token]" value="' . wp_create_nonce( 'mycred-load-coupon' . $user_id ) . '" />
86
+ <input type="submit" class="btn btn-primary" value="' . $button . '" />
87
+ </div>
88
  </form>
89
  </div>';
90
 
addons/coupons/myCRED-addon-coupons.php CHANGED
@@ -2,16 +2,13 @@
2
  /**
3
  * Addon: Coupons
4
  * Addon URI: http://mycred.me/add-ons/coupons/
5
- * Version: 1.1.1
6
- * Description: Create coupons that your users can use to add points to their accounts.
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
 
12
  define( 'myCRED_COUPONS', __FILE__ );
13
  define( 'myCRED_COUPONS_DIR', myCRED_ADDONS_DIR . 'coupons/' );
14
- define( 'myCRED_COUPONS_VERSION', myCRED_VERSION . '.1' );
15
 
16
  require_once myCRED_COUPONS_DIR . 'includes/mycred-coupon-functions.php';
17
  require_once myCRED_COUPONS_DIR . 'includes/mycred-coupon-shortcodes.php';
@@ -43,7 +40,8 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
43
  'success' => 'Coupon successfully deposited into your account'
44
  ),
45
  'register' => false,
46
- 'add_to_core' => true
 
47
  ) );
48
 
49
  add_filter( 'mycred_parse_log_entry_coupon', array( $this, 'parse_log_entry' ), 10, 2 );
@@ -57,10 +55,12 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
57
  */
58
  public function module_init() {
59
 
60
- $this->register_post_type();
61
 
62
  add_shortcode( 'mycred_load_coupon', 'mycred_render_shortcode_load_coupon' );
63
 
 
 
64
  }
65
 
66
  /**
@@ -70,30 +70,37 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
70
  */
71
  public function module_admin_init() {
72
 
73
- add_filter( 'post_updated_messages', array( $this, 'update_messages' ) );
74
 
75
  add_filter( 'manage_mycred_coupon_posts_columns', array( $this, 'adjust_column_headers' ) );
76
  add_action( 'manage_mycred_coupon_posts_custom_column', array( $this, 'adjust_column_content' ), 10, 2 );
77
 
 
 
 
78
  add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
79
  add_filter( 'post_row_actions', array( $this, 'adjust_row_actions' ), 10, 2 );
80
- add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
81
- add_action( 'save_post_mycred_coupon', array( $this, 'update_coupon_details' ) );
 
 
 
 
82
 
83
  }
84
 
85
  /**
86
  * Register Coupons Post Type
87
  * @since 1.4
88
- * @version 1.0
89
  */
90
- protected function register_post_type() {
91
 
92
  $labels = array(
93
  'name' => __( 'Coupons', 'mycred' ),
94
  'singular_name' => __( 'Coupon', 'mycred' ),
95
  'add_new' => __( 'Create New', 'mycred' ),
96
- 'add_new_item' => __( 'Create New Coupon', 'mycred' ),
97
  'edit_item' => __( 'Edit Coupon', 'mycred' ),
98
  'new_item' => __( 'New Coupon', 'mycred' ),
99
  'all_items' => __( 'Coupons', 'mycred' ),
@@ -105,12 +112,19 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
105
  'menu_name' => __( 'Email Notices', 'mycred' )
106
  );
107
  $args = array(
108
- 'labels' => $labels,
109
- 'publicly_queryable' => false,
110
- 'show_ui' => true,
111
- 'show_in_menu' => 'myCRED',
112
- 'capability_type' => 'page',
113
- 'supports' => array( 'title' )
 
 
 
 
 
 
 
114
  );
115
 
116
  register_post_type( 'mycred_coupon', apply_filters( 'mycred_register_coupons', $args ) );
@@ -120,9 +134,9 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
120
  /**
121
  * Adjust Update Messages
122
  * @since 1.4
123
- * @version 1.0
124
  */
125
- public function update_messages( $messages ) {
126
 
127
  $messages['mycred_coupon'] = array(
128
  0 => '',
@@ -132,16 +146,77 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
132
  4 => __( 'Coupon updated.', 'mycred' ),
133
  5 => false,
134
  6 => __( 'Coupon published.', 'mycred' ),
135
- 7 => __( 'Coupon saved.', 'mycred' ),
136
- 8 => '',
137
- 9 => '',
138
- 10 => __( 'Draft Coupon saved.', 'mycred' ),
139
  );
140
 
141
  return $messages;
142
 
143
  }
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  /**
146
  * Adjust Enter Title Here
147
  * @since 1.4
@@ -152,7 +227,7 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
152
  global $post_type;
153
 
154
  if ( $post_type == 'mycred_coupon' )
155
- return __( 'Unique Coupon Code', 'mycred' );
156
 
157
  return $title;
158
 
@@ -207,14 +282,14 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
207
  $count = mycred_get_global_coupon_count( $post_id );
208
 
209
  if ( $count == 0 )
210
- _e( 'not yet used', 'mycred' );
211
 
212
  else {
213
 
214
  $set_type = get_post_meta( $post_id, 'type', true );
215
- $page = 'myCRED';
216
 
217
- if ( $set_type != 'mycred_default' && array_key_exists( $set_type, $this->point_types ) )
218
  $page .= '_' . $set_type;
219
 
220
  $url = add_query_arg( array( 'page' => $page, 'ref' => 'coupon', 'data' => get_the_title( $post_id ) ), admin_url( 'admin.php' ) );
@@ -242,13 +317,13 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
242
 
243
  else {
244
 
245
- if ( $expires < date_i18n( 'U' ) ) {
246
  wp_trash_post( $post_id );
247
  echo '<span style="color:red;">' . __( 'Expired', 'mycred' ) . '</span>';
248
  }
249
 
250
  else {
251
- echo sprintf( __( 'In %s time', 'mycred' ), human_time_diff( $expires ) ) . '<br /><small class="description">' . date_i18n( get_option( 'date_format' ), $expires ) . '</small>';
252
  }
253
 
254
  }
@@ -270,6 +345,18 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
270
  }
271
  }
272
 
 
 
 
 
 
 
 
 
 
 
 
 
273
  /**
274
  * Adjust Row Actions
275
  * @since 1.4
@@ -287,27 +374,54 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
287
  }
288
 
289
  /**
290
- * Parse Log Entries
291
- * @since 1.4
292
  * @version 1.0
293
  */
294
- public function parse_log_entry( $content, $log_entry ) {
295
 
296
- return str_replace( '%coupon%', $log_entry->data, $content );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
  }
299
 
300
  /**
301
- * Add Meta Boxes
302
- * @since 1.4
303
  * @version 1.0
304
  */
305
- public function add_meta_boxes() {
 
 
306
 
307
- global $post;
 
 
 
 
 
 
 
 
 
308
 
309
  add_meta_box(
310
- 'mycred_coupon_setup',
311
  __( 'Coupon Setup', 'mycred' ),
312
  array( $this, 'metabox_coupon_setup' ),
313
  'mycred_coupon',
@@ -316,7 +430,7 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
316
  );
317
 
318
  add_meta_box(
319
- 'mycred_coupon_limits',
320
  __( 'Coupon Limits', 'mycred' ),
321
  array( $this, 'metabox_coupon_limits' ),
322
  'mycred_coupon',
@@ -325,7 +439,7 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
325
  );
326
 
327
  add_meta_box(
328
- 'mycred_coupon_requirements',
329
  __( 'Coupon Requirements', 'mycred' ),
330
  array( $this, 'mycred_coupon_requirements' ),
331
  'mycred_coupon',
@@ -335,8 +449,8 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
335
 
336
  if ( $post->post_status == 'publish' )
337
  add_meta_box(
338
- 'mycred_coupon_usage',
339
- __( 'Usage', 'mycred' ),
340
  array( $this, 'mycred_coupon_usage' ),
341
  'mycred_coupon',
342
  'side',
@@ -348,69 +462,52 @@ if ( ! class_exists( 'myCRED_Coupons_Module' ) ) :
348
  /**
349
  * Metabox: Coupon Setup
350
  * @since 1.4
351
- * @version 1.1
352
  */
353
  public function metabox_coupon_setup( $post ) {
354
 
355
- global $mycred;
356
-
357
- $value = get_post_meta( $post->ID, 'value', true );
358
- if ( empty( $value ) )
359
- $value = 1;
360
 
361
- $expires = get_post_meta( $post->ID, 'expires', true );
362
- $set_type = get_post_meta( $post->ID, 'type', true );
 
 
363
 
364
  ?>
365
- <style type="text/css">
366
- table { width: 100%; }
367
- table th { width: 20%; text-align: right; }
368
- table th label { padding-right: 12px; }
369
- table td { width: 80%; padding-bottom: 6px; }
370
- table td textarea { width: 95%; }
371
- #submitdiv .misc-pub-curtime, #submitdiv #visibility, #submitdiv #misc-publishing-actions { display: none; }
372
- #submitdiv #minor-publishing-actions { padding-bottom: 10px; }
373
- <?php if ( $post->post_status == 'publish' ) : ?>
374
- #submitdiv #minor-publishing-actions { padding: 0 0 0 0; }
375
- <?php endif; ?>
376
- </style>
377
- <input type="hidden" name="mycred-coupon-nonce" value="<?php echo wp_create_nonce( 'update-mycred-coupon' ); ?>" />
378
- <table class="table wide-fat">
379
- <tbody>
380
- <tr valign="top">
381
- <th scope="row"><label for="mycred-coupon-value"><?php _e( 'Value', 'mycred' ); ?></label></th>
382
- <td>
383
- <input type="text" name="mycred_coupon[value]" id="mycred-coupon-value" value="<?php echo $mycred->number( $value ); ?>" /><br />
384
  <span class="description"><?php echo $mycred->template_tags_general( __( 'The amount of %plural% this coupon is worth.', 'mycred' ) ); ?></span>
385
- </td>
386
- </tr>
387
- <tr valign="top">
388
- <th scope="row"><label for="mycred-coupon-value"><?php _e( 'Point Type', 'mycred' ); ?></label></th>
389
- <td>
390
  <?php if ( count( $this->point_types ) > 1 ) : ?>
391
 
392
- <?php mycred_types_select_from_dropdown( 'mycred_coupon[type]', 'mycred-coupon-type', $set_type ); ?><br />
393
  <span class="description"><?php _e( 'Select the point type that this coupon is applied.', 'mycred' ); ?></span>
394
 
395
  <?php else : ?>
396
 
397
- <?php echo $this->core->plural(); ?>
 
398
 
399
  <?php endif; ?>
400
- </td>
401
- </tr>
402
- <tr valign="top">
403
- <th scope="row"><label for="mycred-coupon-value"><?php _e( 'Expire', 'mycred' ); ?></label></th>
404
- <td>
405
- <input type="text" name="mycred_coupon[expires]" id="mycred-coupon-expire" value="<?php echo $expires; ?>" placeholder="YYYY-MM-DD" /><br />
406
  <span class="description"><?php _e( 'Optional date when this coupon expires. Expired coupons will be trashed.', 'mycred' ); ?></span>
407
- </td>
408
- </tr>
409
- </tbody>
410
- </table>
411
-
412
- <?php do_action( 'mycred_coupon_after_setup', $post ); ?>
413
-
414
  <?php
415
 
416
  }
@@ -418,42 +515,36 @@ table td textarea { width: 95%; }
418
  /**
419
  * Metabox: Coupon Limits
420
  * @since 1.4
421
- * @version 1.0
422
  */
423
  public function metabox_coupon_limits( $post ) {
424
 
425
- global $mycred;
426
-
427
- $global_max = get_post_meta( $post->ID, 'global', true );
428
- if ( empty( $global_max ) )
429
- $global_max = 1;
430
 
431
- $user_max = get_post_meta( $post->ID, 'user', true );
432
- if ( empty( $user_max ) )
433
- $user_max = 1;
 
434
 
435
  ?>
436
- <table class="table wide-fat">
437
- <tbody>
438
- <tr valign="top">
439
- <th scope="row"><label for="mycred-coupon-global"><?php _e( 'Global Maximum', 'mycred' ); ?></label></th>
440
- <td>
441
- <input type="text" name="mycred_coupon[global]" id="mycred-coupon-global" value="<?php echo abs( $global_max ); ?>" /><br />
442
- <span class="description"><?php _e( 'The maximum number of times this coupon can be used. Note that the coupon will be automatically trashed once this maximum is reached!', 'mycred' ); ?></span>
443
- </td>
444
- </tr>
445
- <tr valign="top">
446
- <th scope="row"><label for="mycred-coupon-user"><?php _e( 'User Maximum', 'mycred' ); ?></label></th>
447
- <td>
448
- <input type="text" name="mycred_coupon[user]" id="mycred-coupon-user" value="<?php echo abs( $user_max ); ?>" /><br />
449
  <span class="description"><?php _e( 'The maximum number of times this coupon can be used by a user.', 'mycred' ); ?></span>
450
- </td>
451
- </tr>
452
- </tbody>
453
- </table>
454
-
455
- <?php do_action( 'mycred_coupon_after_limits', $post ); ?>
456
-
457
  <?php
458
 
459
  }
@@ -461,34 +552,47 @@ table td textarea { width: 95%; }
461
  /**
462
  * Metabox: Coupon Requirements
463
  * @since 1.4
464
- * @version 1.0
465
  */
466
  public function mycred_coupon_requirements( $post ) {
467
 
468
- global $mycred;
469
 
470
- $min_balance = get_post_meta( $post->ID, 'min_balance', true );
471
- if ( empty( $min_balance ) )
472
- $min_balance = 0;
 
473
 
474
- $max_balance = get_post_meta( $post->ID, 'max_balance', true );
475
- if ( empty( $max_balance ) )
476
- $max_balance = 0;
477
 
478
  ?>
479
- <p>
480
- <label for="mycred-coupon-min_balance"><?php _e( 'Minimum Balance', 'mycred' ); ?></label><br />
481
- <input type="text" name="mycred_coupon[min_balance]" id="mycred-coupon-min_balance" value="<?php echo $mycred->number( $min_balance ); ?>" /><br />
482
- <span class="description"><?php _e( 'Optional minimum balance a user must have in order to use this coupon. Use zero to disable.', 'mycred' ); ?></span>
483
- </p>
484
- <p>
485
- <label for="mycred-coupon-max_balance"><?php _e( 'Maximum Balance', 'mycred' ); ?></label><br />
486
- <input type="text" name="mycred_coupon[max_balance]" id="mycred-coupon-max_balance" value="<?php echo $mycred->number( $max_balance ); ?>" /><br />
487
- <span class="description"><?php _e( 'Optional maximum balance a user can have in order to use this coupon. Use zero to disable.', 'mycred' ); ?></span>
488
- </p>
489
-
490
- <?php do_action( 'mycred_coupon_after_requirements', $post ); ?>
491
-
 
 
 
 
 
 
 
 
 
 
 
 
 
492
  <?php
493
 
494
  }
@@ -502,14 +606,14 @@ table td textarea { width: 95%; }
502
 
503
  $count = mycred_get_global_coupon_count( $post->ID );
504
  if ( empty( $count ) )
505
- _e( 'not yet used', 'mycred' );
506
 
507
  else {
508
 
509
  $set_type = get_post_meta( $post->ID, 'type', true );
510
- $page = 'myCRED';
511
 
512
- if ( $set_type != 'mycred_default' && array_key_exists( $set_type, $this->point_types ) )
513
  $page .= '_' . $set_type;
514
 
515
  $url = add_query_arg( array( 'page' => $page, 'ref' => 'coupon', 'data' => $post->post_title ), admin_url( 'admin.php' ) );
@@ -520,20 +624,20 @@ table td textarea { width: 95%; }
520
  }
521
 
522
  /**
523
- * Update Coupon Details
524
  * @since 1.4
525
  * @version 1.0
526
  */
527
- public function update_coupon_details( $post_id ) {
528
 
529
- if ( ! isset( $_POST['mycred-coupon-nonce'] ) || ! wp_verify_nonce( $_POST['mycred-coupon-nonce'], 'update-mycred-coupon' ) ) return $post_id;
530
 
531
- if ( isset( $_POST['mycred_coupon'] ) ) {
532
 
533
- foreach ( $_POST['mycred_coupon'] as $key => $value ) {
534
- $value = sanitize_text_field( $value );
535
- update_post_meta( $post_id, $key, $value );
536
- }
537
 
538
  }
539
 
@@ -552,7 +656,7 @@ table td textarea { width: 95%; }
552
  $prefs = mycred_apply_defaults( $this->default_prefs, $this->coupons );
553
 
554
  ?>
555
- <h4><div class="icon icon-active"></div><?php _e( 'Coupons', 'mycred' ); ?></h4>
556
  <div class="body" style="display:none;">
557
  <label class="subheader" for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
558
  <ol id="myCRED-coupon-log">
@@ -628,11 +732,35 @@ table td textarea { width: 95%; }
628
 
629
  }
630
 
 
 
 
 
 
 
 
 
 
 
 
631
  }
 
632
 
633
- $coupons = new myCRED_Coupons_Module();
634
- $coupons->load();
 
 
 
 
 
635
 
 
 
 
 
 
 
636
  endif;
 
637
 
638
  ?>
2
  /**
3
  * Addon: Coupons
4
  * Addon URI: http://mycred.me/add-ons/coupons/
5
+ * Version: 1.3
 
 
 
6
  */
7
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
 
9
  define( 'myCRED_COUPONS', __FILE__ );
10
  define( 'myCRED_COUPONS_DIR', myCRED_ADDONS_DIR . 'coupons/' );
11
+ define( 'myCRED_COUPONS_VERSION', '1.3' );
12
 
13
  require_once myCRED_COUPONS_DIR . 'includes/mycred-coupon-functions.php';
14
  require_once myCRED_COUPONS_DIR . 'includes/mycred-coupon-shortcodes.php';
40
  'success' => 'Coupon successfully deposited into your account'
41
  ),
42
  'register' => false,
43
+ 'add_to_core' => true,
44
+ 'menu_pos' => 80
45
  ) );
46
 
47
  add_filter( 'mycred_parse_log_entry_coupon', array( $this, 'parse_log_entry' ), 10, 2 );
55
  */
56
  public function module_init() {
57
 
58
+ $this->register_coupons();
59
 
60
  add_shortcode( 'mycred_load_coupon', 'mycred_render_shortcode_load_coupon' );
61
 
62
+ add_action( 'mycred_add_menu', array( $this, 'add_to_menu' ), $this->menu_pos );
63
+
64
  }
65
 
66
  /**
70
  */
71
  public function module_admin_init() {
72
 
73
+ add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
74
 
75
  add_filter( 'manage_mycred_coupon_posts_columns', array( $this, 'adjust_column_headers' ) );
76
  add_action( 'manage_mycred_coupon_posts_custom_column', array( $this, 'adjust_column_content' ), 10, 2 );
77
 
78
+ add_filter( 'parent_file', array( $this, 'parent_file' ) );
79
+ add_filter( 'submenu_file', array( $this, 'subparent_file' ), 10, 2 );
80
+
81
  add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
82
  add_filter( 'post_row_actions', array( $this, 'adjust_row_actions' ), 10, 2 );
83
+ add_filter( 'bulk_actions-edit-mycred_coupon', array( $this, 'bulk_actions' ) );
84
+ add_action( 'save_post_mycred_coupon', array( $this, 'save_coupon' ), 10, 2 );
85
+
86
+ add_action( 'admin_head-post.php', array( $this, 'edit_coupons_style' ) );
87
+ add_action( 'admin_head-post-new.php', array( $this, 'edit_coupons_style' ) );
88
+ add_action( 'admin_head-edit.php', array( $this, 'coupon_style' ) );
89
 
90
  }
91
 
92
  /**
93
  * Register Coupons Post Type
94
  * @since 1.4
95
+ * @version 1.0.1
96
  */
97
+ protected function register_coupons() {
98
 
99
  $labels = array(
100
  'name' => __( 'Coupons', 'mycred' ),
101
  'singular_name' => __( 'Coupon', 'mycred' ),
102
  'add_new' => __( 'Create New', 'mycred' ),
103
+ 'add_new_item' => __( 'Create New', 'mycred' ),
104
  'edit_item' => __( 'Edit Coupon', 'mycred' ),
105
  'new_item' => __( 'New Coupon', 'mycred' ),
106
  'all_items' => __( 'Coupons', 'mycred' ),
112
  'menu_name' => __( 'Email Notices', 'mycred' )
113
  );
114
  $args = array(
115
+ 'labels' => $labels,
116
+ 'supports' => array( 'title' ),
117
+ 'hierarchical' => false,
118
+ 'public' => false,
119
+ 'show_ui' => true,
120
+ 'show_in_menu' => false,
121
+ 'show_in_nav_menus' => false,
122
+ 'show_in_admin_bar' => false,
123
+ 'can_export' => true,
124
+ 'has_archive' => false,
125
+ 'exclude_from_search' => true,
126
+ 'publicly_queryable' => false,
127
+ 'register_meta_box_cb' => array( $this, 'add_metaboxes' )
128
  );
129
 
130
  register_post_type( 'mycred_coupon', apply_filters( 'mycred_register_coupons', $args ) );
134
  /**
135
  * Adjust Update Messages
136
  * @since 1.4
137
+ * @version 1.0.1
138
  */
139
+ public function post_updated_messages( $messages ) {
140
 
141
  $messages['mycred_coupon'] = array(
142
  0 => '',
146
  4 => __( 'Coupon updated.', 'mycred' ),
147
  5 => false,
148
  6 => __( 'Coupon published.', 'mycred' ),
149
+ 7 => __( 'Coupon updated.', 'mycred' ),
150
+ 8 => __( 'Coupon updated.', 'mycred' ),
151
+ 9 => __( 'Coupon updated.', 'mycred' ),
152
+ 10 => __( 'Coupon updated.', 'mycred' ),
153
  );
154
 
155
  return $messages;
156
 
157
  }
158
 
159
+ /**
160
+ * Add Admin Menu Item
161
+ * @since 1.7
162
+ * @version 1.0
163
+ */
164
+ public function add_to_menu() {
165
+
166
+ add_submenu_page(
167
+ 'mycred',
168
+ __( 'Coupons', 'mycred' ),
169
+ __( 'Coupons', 'mycred' ),
170
+ $this->core->edit_creds_cap(),
171
+ 'edit.php?post_type=mycred_coupon'
172
+ );
173
+
174
+ }
175
+
176
+ /**
177
+ * Parent File
178
+ * @since 1.7
179
+ * @version 1.0
180
+ */
181
+ public function parent_file( $parent = '' ) {
182
+
183
+ global $pagenow;
184
+
185
+ if ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'mycred_coupon' && isset( $_GET['action'] ) && $_GET['action'] == 'edit' )
186
+ return 'mycred';
187
+
188
+ if ( $pagenow == 'post-new.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_coupon' )
189
+ return 'mycred';
190
+
191
+ return $parent;
192
+
193
+ }
194
+
195
+ /**
196
+ * Sub Parent File
197
+ * @since 1.7
198
+ * @version 1.0
199
+ */
200
+ public function subparent_file( $subparent = '', $parent = '' ) {
201
+
202
+ global $pagenow;
203
+
204
+ if ( ( $pagenow == 'edit.php' || $pagenow == 'post-new.php' ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_coupon' ) {
205
+
206
+ return 'edit.php?post_type=mycred_coupon';
207
+
208
+ }
209
+
210
+ elseif ( $pagenow == 'post.php' && isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'mycred_coupon' ) {
211
+
212
+ return 'edit.php?post_type=mycred_coupon';
213
+
214
+ }
215
+
216
+ return $subparent;
217
+
218
+ }
219
+
220
  /**
221
  * Adjust Enter Title Here
222
  * @since 1.4
227
  global $post_type;
228
 
229
  if ( $post_type == 'mycred_coupon' )
230
+ return __( 'Coupon Code', 'mycred' );
231
 
232
  return $title;
233
 
282
  $count = mycred_get_global_coupon_count( $post_id );
283
 
284
  if ( $count == 0 )
285
+ echo '-';
286
 
287
  else {
288
 
289
  $set_type = get_post_meta( $post_id, 'type', true );
290
+ $page = MYCRED_SLUG;
291
 
292
+ if ( $set_type != MYCRED_DEFAULT_TYPE_KEY && array_key_exists( $set_type, $this->point_types ) )
293
  $page .= '_' . $set_type;
294
 
295
  $url = add_query_arg( array( 'page' => $page, 'ref' => 'coupon', 'data' => get_the_title( $post_id ) ), admin_url( 'admin.php' ) );
317
 
318
  else {
319
 
320
+ if ( $expires < current_time( 'timestamp' ) ) {
321
  wp_trash_post( $post_id );
322
  echo '<span style="color:red;">' . __( 'Expired', 'mycred' ) . '</span>';
323
  }
324
 
325
  else {
326
+ echo sprintf( __( 'In %s time', 'mycred' ), human_time_diff( $expires ) ) . '<br /><small class="description">' . date( get_option( 'date_format' ), $expires ) . '</small>';
327
  }
328
 
329
  }
345
  }
346
  }
347
 
348
+ /**
349
+ * Adjust Bulk Actions
350
+ * @since 1.7
351
+ * @version 1.0
352
+ */
353
+ public function bulk_actions( $actions ) {
354
+
355
+ unset( $actions['edit'] );
356
+ return $actions;
357
+
358
+ }
359
+
360
  /**
361
  * Adjust Row Actions
362
  * @since 1.4
374
  }
375
 
376
  /**
377
+ * Edit Coupon Style
378
+ * @since 1.7
379
  * @version 1.0
380
  */
381
+ public function edit_coupons_style() {
382
 
383
+ global $post_type;
384
+
385
+ if ( $post_type !== 'mycred_coupon' ) return;
386
+
387
+ wp_enqueue_style( 'mycred-bootstrap-grid' );
388
+ wp_enqueue_style( 'mycred-forms' );
389
+
390
+ add_filter( 'postbox_classes_mycred_coupon_mycred-coupon-setup', array( $this, 'metabox_classes' ) );
391
+ add_filter( 'postbox_classes_mycred_coupon_mycred-coupon-limits', array( $this, 'metabox_classes' ) );
392
+ add_filter( 'postbox_classes_mycred_coupon_mycred-coupon-requirements', array( $this, 'metabox_classes' ) );
393
+ add_filter( 'postbox_classes_mycred_coupon_mycred-coupon-usage', array( $this, 'metabox_classes' ) );
394
+
395
+ ?>
396
+ <style type="text/css">
397
+ #misc-publishing-actions #visibility { display: none; }
398
+ </style>
399
+ <?php
400
 
401
  }
402
 
403
  /**
404
+ * Coupon Style
405
+ * @since 1.7
406
  * @version 1.0
407
  */
408
+ public function coupon_style() {
409
+
410
+ global $post_type;
411
 
412
+ if ( $post_type !== 'mycred_coupon' ) return;
413
+
414
+ }
415
+
416
+ /**
417
+ * Add Meta Boxes
418
+ * @since 1.4
419
+ * @version 1.1
420
+ */
421
+ public function add_metaboxes( $post ) {
422
 
423
  add_meta_box(
424
+ 'mycred-coupon-setup',
425
  __( 'Coupon Setup', 'mycred' ),
426
  array( $this, 'metabox_coupon_setup' ),
427
  'mycred_coupon',
430
  );
431
 
432
  add_meta_box(
433
+ 'mycred-coupon-limits',
434
  __( 'Coupon Limits', 'mycred' ),
435
  array( $this, 'metabox_coupon_limits' ),
436
  'mycred_coupon',
439
  );
440
 
441
  add_meta_box(
442
+ 'mycred-coupon-requirements',
443
  __( 'Coupon Requirements', 'mycred' ),
444
  array( $this, 'mycred_coupon_requirements' ),
445
  'mycred_coupon',
449
 
450
  if ( $post->post_status == 'publish' )
451
  add_meta_box(
452
+ 'mycred-coupon-usage',
453
+ __( 'Coupon Usage', 'mycred' ),
454
  array( $this, 'mycred_coupon_usage' ),
455
  'mycred_coupon',
456
  'side',
462
  /**
463
  * Metabox: Coupon Setup
464
  * @since 1.4
465
+ * @version 1.2
466
  */
467
  public function metabox_coupon_setup( $post ) {
468
 
469
+ $coupon = mycred_get_coupon( $post->ID );
 
 
 
 
470
 
471
+ if ( $coupon->point_type != $this->core->cred_id )
472
+ $mycred = mycred( $coupon->point_type );
473
+ else
474
+ $mycred = $this->core;
475
 
476
  ?>
477
+ <div class="form">
478
+ <div class="row">
479
+ <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
480
+ <div class="form-group">
481
+ <label for=""><?php _e( 'Value', 'mycred' ); ?></label>
482
+ <input type="text" name="mycred_coupon[value]" class="form-control" id="mycred-coupon-value" value="<?php echo $mycred->number( $coupon->value ); ?>" />
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  <span class="description"><?php echo $mycred->template_tags_general( __( 'The amount of %plural% this coupon is worth.', 'mycred' ) ); ?></span>
484
+ </div>
485
+ </div>
486
+ <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
487
+ <div class="form-group">
488
+ <label for=""><?php _e( 'Point Type', 'mycred' ); ?></label>
489
  <?php if ( count( $this->point_types ) > 1 ) : ?>
490
 
491
+ <?php mycred_types_select_from_dropdown( 'mycred_coupon[type]', 'mycred-coupon-type', $coupon->point_type, false, ' class="form-control"' ); ?><br />
492
  <span class="description"><?php _e( 'Select the point type that this coupon is applied.', 'mycred' ); ?></span>
493
 
494
  <?php else : ?>
495
 
496
+ <p class="form-control-static"><?php echo $mycred->plural(); ?></p>
497
+ <input type="hidden" name="mycred_coupon[type]" value="mycred_default" />
498
 
499
  <?php endif; ?>
500
+ </div>
501
+ </div>
502
+ <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
503
+ <div class="form-group">
504
+ <label for=""><?php _e( 'Expire', 'mycred' ); ?></label>
505
+ <input type="text" name="mycred_coupon[expires]" class="form-control" id="mycred-coupon-expire" value="<?php echo $coupon->expires; ?>" placeholder="YYYY-MM-DD" />
506
  <span class="description"><?php _e( 'Optional date when this coupon expires. Expired coupons will be trashed.', 'mycred' ); ?></span>
507
+ </div>
508
+ </div>
509
+ </div>
510
+ </div>
 
 
 
511
  <?php
512
 
513
  }
515
  /**
516
  * Metabox: Coupon Limits
517
  * @since 1.4
518
+ * @version 1.1
519
  */
520
  public function metabox_coupon_limits( $post ) {
521
 
522
+ $coupon = mycred_get_coupon( $post->ID );
 
 
 
 
523
 
524
+ if ( $coupon->point_type != $this->core->cred_id )
525
+ $mycred = mycred( $coupon->point_type );
526
+ else
527
+ $mycred = $this->core;
528
 
529
  ?>
530
+ <div class="form">
531
+ <div class="row">
532
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
533
+ <div class="form-group">
534
+ <label for="mycred-coupon-global"><?php _e( 'Global Maximum', 'mycred' ); ?></label>
535
+ <input type="text" name="mycred_coupon[global]" class="form-control" id="mycred-coupon-global" value="<?php echo absint( $coupon->max_global ); ?>" />
536
+ <span class="description"><?php _e( 'The maximum number of times this coupon can be used.', 'mycred' ); ?></span>
537
+ </div>
538
+ </div>
539
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
540
+ <div class="form-group">
541
+ <label for="mycred-coupon-user"><?php _e( 'User Maximum', 'mycred' ); ?></label>
542
+ <input type="text" name="mycred_coupon[user]" class="form-control" id="mycred-coupon-user" value="<?php echo absint( $coupon->max_user ); ?>" />
543
  <span class="description"><?php _e( 'The maximum number of times this coupon can be used by a user.', 'mycred' ); ?></span>
544
+ </div>
545
+ </div>
546
+ </div>
547
+ </div>
 
 
 
548
  <?php
549
 
550
  }
552
  /**
553
  * Metabox: Coupon Requirements
554
  * @since 1.4
555
+ * @version 1.1
556
  */
557
  public function mycred_coupon_requirements( $post ) {
558
 
559
+ $coupon = mycred_get_coupon( $post->ID );
560
 
561
+ if ( $coupon->point_type != $this->core->cred_id )
562
+ $mycred = mycred( $coupon->point_type );
563
+ else
564
+ $mycred = $this->core;
565
 
566
+ $min_balance_type = mycred_types_select_from_dropdown( 'mycred_coupon[min_balance_type]', 'mycred-coupon-min_balance_type', $coupon->requires_min_type, true, ' style="vertical-align: top;"' );
567
+ $max_balance_type = mycred_types_select_from_dropdown( 'mycred_coupon[max_balance_type]', 'mycred-coupon-max_balance_type', $coupon->requires_max_type, true, ' style="vertical-align: top;"' );
 
568
 
569
  ?>
570
+ <div class="form">
571
+ <div class="row">
572
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
573
+ <div class="form-group">
574
+ <label for="mycred-coupon-min_balance"><?php _e( 'Minimum Balance', 'mycred' ); ?></label>
575
+ <div>
576
+ <input type="text" name="mycred_coupon[min_balance]" <?php if ( count( $this->point_types ) > 1 ) echo 'size="8"'; else echo ' style="width: 99%;"'; ?> id="mycred-coupon-min_balance" value="<?php echo $mycred->number( $coupon->requires_min ); ?>" />
577
+ <?php echo $min_balance_type; ?>
578
+ </div>
579
+ <span class="description"><?php _e( 'Optional minimum balance a user must have in order to use this coupon. Use zero to disable.', 'mycred' ); ?></span>
580
+ </div>
581
+ </div>
582
+ </div>
583
+ <div class="row">
584
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
585
+ <div class="form-group">
586
+ <label for="mycred-coupon-max_balance"><?php _e( 'Maximum Balance', 'mycred' ); ?></label>
587
+ <div>
588
+ <input type="text" name="mycred_coupon[max_balance]" <?php if ( count( $this->point_types ) > 1 ) echo 'size="8"'; else echo ' style="width: 99%;"'; ?> id="mycred-coupon-max_balance" value="<?php echo $mycred->number( $coupon->requires_max ); ?>" />
589
+ <?php echo $max_balance_type; ?>
590
+ </div>
591
+ <span class="description"><?php _e( 'Optional maximum balance a user can have in order to use this coupon. Use zero to disable.', 'mycred' ); ?></span>
592
+ </div>
593
+ </div>
594
+ </div>
595
+ </div>
596
  <?php
597
 
598
  }
606
 
607
  $count = mycred_get_global_coupon_count( $post->ID );
608
  if ( empty( $count ) )
609
+ echo '-';
610
 
611
  else {
612
 
613
  $set_type = get_post_meta( $post->ID, 'type', true );
614
+ $page = MYCRED_SLUG;
615
 
616
+ if ( $set_type != MYCRED_DEFAULT_TYPE_KEY && array_key_exists( $set_type, $this->point_types ) )
617
  $page .= '_' . $set_type;
618
 
619
  $url = add_query_arg( array( 'page' => $page, 'ref' => 'coupon', 'data' => $post->post_title ), admin_url( 'admin.php' ) );
624
  }
625
 
626
  /**
627
+ * Save Coupon
628
  * @since 1.4
629
  * @version 1.0
630
  */
631
+ public function save_coupon( $post_id, $post = NULL ) {
632
 
633
+ if ( $post === NULL || ! current_user_can( $this->core->edit_creds_cap() ) || ! isset( $_POST['mycred_coupon'] ) ) return $post_id;
634
 
635
+ foreach ( $_POST['mycred_coupon'] as $meta_key => $meta_value ) {
636
 
637
+ $new_value = sanitize_text_field( $meta_value );
638
+ $old_value = get_post_meta( $post_id, $meta_key, true );
639
+ if ( $new_value != $old_value )
640
+ update_post_meta( $post_id, $meta_key, $new_value );
641
 
642
  }
643
 
656
  $prefs = mycred_apply_defaults( $this->default_prefs, $this->coupons );
657
 
658
  ?>
659
+ <h4><span class="dashicons dashicons-admin-plugins static"></span><?php _e( 'Coupons', 'mycred' ); ?></h4>
660
  <div class="body" style="display:none;">
661
  <label class="subheader" for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
662
  <ol id="myCRED-coupon-log">
732
 
733
  }
734
 
735
+ /**
736
+ * Parse Log Entries
737
+ * @since 1.4
738
+ * @version 1.0
739
+ */
740
+ public function parse_log_entry( $content, $log_entry ) {
741
+
742
+ return str_replace( '%coupon%', $log_entry->data, $content );
743
+
744
+ }
745
+
746
  }
747
+ endif;
748
 
749
+ /**
750
+ * Load Coupons Module
751
+ * @since 1.7
752
+ * @version 1.0
753
+ */
754
+ if ( ! function_exists( 'mycred_load_coupons_addon' ) ) :
755
+ function mycred_load_coupons_addon( $modules, $point_types ) {
756
 
757
+ $modules['solo']['coupons'] = new myCRED_Coupons_Module();
758
+ $modules['solo']['coupons']->load();
759
+
760
+ return $modules;
761
+
762
+ }
763
  endif;
764
+ add_filter( 'mycred_load_modules', 'mycred_load_coupons_addon', 50, 2 );
765
 
766
  ?>
addons/email-notices/myCRED-addon-email-notices.php CHANGED
@@ -3,14 +3,12 @@
3
  * Addon: Email Notices
4
  * Addon URI: http://mycred.me/add-ons/email-notices/
5
  * Version: 1.3.1
6
- * Description: Create email notices for any type of myCRED instance.
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
 
12
  define( 'myCRED_EMAIL', __FILE__ );
13
- define( 'myCRED_EMAIL_VERSION', myCRED_VERSION . '.1' );
 
14
 
15
  /**
16
  * myCRED_Email_Notice_Module class
@@ -46,7 +44,8 @@ if ( ! class_exists( 'myCRED_Email_Notice_Module' ) ) :
46
  'override' => 0
47
  ),
48
  'register' => false,
49
- 'add_to_core' => true
 
50
  ) );
51
 
52
  }
@@ -58,15 +57,18 @@ if ( ! class_exists( 'myCRED_Email_Notice_Module' ) ) :
58
  */
59
  public function module_init() {
60
 
61
- $this->register_post_type();
62
  $this->setup_instances();
63
 
64
- add_action( 'mycred_admin_enqueue', array( $this, 'enqueue_scripts' ) );
65
- add_filter( 'mycred_add_finished', array( $this, 'email_check' ), 50, 3 );
66
- add_action( 'mycred_send_email_notices', 'mycred_email_notice_cron_job' );
 
67
 
68
  add_shortcode( 'mycred_email_subscriptions', array( $this, 'render_subscription_shortcode' ) );
69
 
 
 
70
  // Schedule Cron
71
  if ( ! isset( $this->emailnotices['send'] ) ) return;
72
 
@@ -88,19 +90,23 @@ if ( ! class_exists( 'myCRED_Email_Notice_Module' ) ) :
88
  */
89
  public function module_admin_init() {
90
 
91
- add_action( 'admin_head', array( $this, 'admin_header' ) );
92
- add_filter( 'post_row_actions', array( $this, 'adjust_row_actions' ), 10, 2 );
93
 
94
  add_filter( 'manage_mycred_email_notice_posts_columns', array( $this, 'adjust_column_headers' ), 50 );
95
  add_action( 'manage_mycred_email_notice_posts_custom_column', array( $this, 'adjust_column_content' ), 10, 2 );
96
 
97
- add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
 
 
 
 
98
  add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
99
- add_filter( 'default_content', array( $this, 'default_content' ) );
100
 
101
- add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
102
  add_action( 'post_submitbox_start', array( $this, 'publish_warning' ) );
103
- add_action( 'save_post_mycred_email_notice', array( $this, 'save_email_notice' ) );
 
104
 
105
  if ( $this->emailnotices['use_html'] === false )
106
  add_filter( 'user_can_richedit', array( $this, 'disable_richedit' ) );
@@ -108,524 +114,440 @@ if ( ! class_exists( 'myCRED_Email_Notice_Module' ) ) :
108
  }
109
 
110
  /**
111
- * Admin Header
112
  * @since 1.1
113
- * @version 1.0
114
  */
115
- public function admin_header() {
116
 
117
- $screen = get_current_screen();
118
- if ( $screen->id == 'mycred_email_notice' && $this->emailnotices['use_html'] === false ) {
119
- remove_action( 'media_buttons', 'media_buttons' );
120
- echo '<style type="text/css">#ed_toolbar { display: none !important; }</style>';
121
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  }
124
 
125
  /**
126
- * Enqueue Scripts & Styles
127
  * @since 1.1
128
  * @version 1.1
129
  */
130
- public function enqueue_scripts() {
131
-
132
- // Register Email List Styling
133
- wp_register_style(
134
- 'mycred-email-notices',
135
- plugins_url( 'assets/css/email-notice.css', myCRED_EMAIL ),
136
- false,
137
- myCRED_EMAIL_VERSION . '.1',
138
- 'all'
139
- );
140
 
141
- // Register Edit Email Notice Styling
142
- wp_register_style(
143
- 'mycred-email-edit-notice',
144
- plugins_url( 'assets/css/edit-email-notice.css', myCRED_EMAIL ),
145
- false,
146
- myCRED_EMAIL_VERSION . '.1',
147
- 'all'
 
 
 
 
 
148
  );
149
 
150
- $screen = get_current_screen();
151
- // Commonly used
152
- if ( $screen->id == 'edit-mycred_email_notice' || $screen->id == 'mycred_email_notice' )
153
- wp_enqueue_style( 'mycred-admin' );
154
 
155
- // Edit Email Notice Styling
156
- if ( $screen->id == 'mycred_email_notice' )
157
- wp_enqueue_style( 'mycred-email-edit-notice' );
158
 
159
- // Email Notice List Styling
160
- elseif ( $screen->id == 'edit-mycred_email_notice' )
161
- wp_enqueue_style( 'mycred-email-notices' );
 
 
 
 
 
 
 
 
 
 
 
162
 
163
  }
164
 
165
  /**
166
- * Register Email Notice Post Type
167
- * @since 1.1
168
  * @version 1.0
169
  */
170
- public function register_post_type() {
171
 
172
- $labels = array(
173
- 'name' => __( 'Email Notices', 'mycred' ),
174
- 'singular_name' => __( 'Email Notice', 'mycred' ),
175
- 'add_new' => __( 'Add New', 'mycred' ),
176
- 'add_new_item' => __( 'Add New Notice', 'mycred' ),
177
- 'edit_item' => __( 'Edit Notice', 'mycred' ),
178
- 'new_item' => __( 'New Notice', 'mycred' ),
179
- 'all_items' => __( 'Email Notices', 'mycred' ),
180
- 'view_item' => __( 'View Notice', 'mycred' ),
181
- 'search_items' => __( 'Search Email Notices', 'mycred' ),
182
- 'not_found' => __( 'No email notices found', 'mycred' ),
183
- 'not_found_in_trash' => __( 'No email notices found in Trash', 'mycred' ),
184
- 'parent_item_colon' => '',
185
- 'menu_name' => __( 'Email Notices', 'mycred' )
186
- );
187
- $args = array(
188
- 'labels' => $labels,
189
- 'publicly_queryable' => false,
190
- 'show_ui' => true,
191
- 'show_in_menu' => 'myCRED',
192
- 'hierarchical' => true,
193
- 'capability_type' => 'page',
194
- 'supports' => array( 'title', 'editor' )
195
- );
196
 
197
- register_post_type( 'mycred_email_notice', apply_filters( 'mycred_register_emailnotices', $args ) );
 
 
 
 
 
 
198
 
199
  }
200
 
201
  /**
202
- * Setup Instances
203
- * @since 1.1
204
- * @version 1.1
205
  */
206
- public function setup_instances() {
207
 
208
- $instances[''] = __( 'Select', 'mycred' );
209
- $instances['general'] = array(
210
- 'label' => __( 'General', 'mycred' ),
211
- 'all' => __( 'users balance changes', 'mycred' ),
212
- 'positive' => __( 'user gains %_plural%', 'mycred' ),
213
- 'negative' => __( 'user lose %_plural%', 'mycred' ),
214
- 'zero' => __( 'users balance reaches zero', 'mycred' ),
215
- 'minus' => __( 'users balance goes minus', 'mycred' ),
216
- 'end' => ''
217
- );
218
 
219
- if ( class_exists( 'myCRED_Badge_Module' ) ) {
220
- $instances['badges'] = array(
221
- 'label' => __( 'Badge Add-on', 'mycred' ),
222
- 'positive' => __( 'user gains a badge', 'mycred' ),
223
- 'end' => ''
224
- );
225
- }
226
 
227
- if ( class_exists( 'myCRED_Sell_Content_Module' ) ) {
228
- $instances['buy_content'] = array(
229
- 'label' => __( 'Sell Content Add-on', 'mycred' ),
230
- 'negative' => __( 'user buys content', 'mycred' ),
231
- 'positive' => __( 'authors content gets sold', 'mycred' ),
232
- 'end' => ''
233
- );
234
  }
235
 
236
- if ( class_exists( 'myCRED_buyCRED_Module' ) ) {
237
- $instances['buy_creds'] = array(
238
- 'label' => __( 'buyCREDs Add-on', 'mycred' ),
239
- 'positive' => __( 'user buys %_plural%', 'mycred' ),
240
- 'end' => ''
241
- );
242
- }
243
 
244
- if ( class_exists( 'myCRED_Transfer_Module' ) ) {
245
- $instances['transfer'] = array(
246
- 'label' => __( 'Transfer Add-on', 'mycred' ),
247
- 'negative' => __( 'user sends %_plural%', 'mycred' ),
248
- 'positive' => __( 'user receives %_plural%', 'mycred' ),
249
- 'end' => ''
250
- );
251
- }
252
 
253
- if ( class_exists( 'myCRED_Ranks_Module' ) ) {
254
- $instances['ranks'] = array(
255
- 'label' => __( 'Ranks Add-on', 'mycred' ),
256
- 'negative' => __( 'user is demoted', 'mycred' ),
257
- 'positive' => __( 'user is promoted', 'mycred' ),
258
- 'end' => ''
259
- );
260
  }
261
 
262
- $this->instances = apply_filters( 'mycred_email_instances', $instances );
263
 
264
  }
265
 
266
  /**
267
- * Get Instance
268
  * @since 1.1
269
  * @version 1.0
270
  */
271
- public function get_instance( $key = '', $detail = NULL ) {
272
-
273
- $instance_keys = explode( '|', $key );
274
- if ( $instance_keys === false || empty( $instance_keys ) || count( $instance_keys ) != 2 ) return NULL;
275
 
276
- // By default we return the entire array for the given key
277
- if ( $detail === NULL && array_key_exists( $instance_keys[0], $this->instances ) )
278
- return $this->core->template_tags_general( $this->instances[ $instance_keys[0] ][ $instance_keys[1] ] );
279
 
280
- if ( $detail !== NULL && array_key_exists( $detail, $this->instances[ $instance_keys[0] ] ) )
281
- return $this->core->template_tags_general( $this->instances[ $instance_keys[0] ][ $detail ] );
282
 
283
- return NULL;
284
 
285
  }
286
 
287
  /**
288
- * Add to General Settings
289
  * @since 1.1
290
  * @version 1.1
291
  */
292
- public function after_general_settings( $mycred = NULL ) {
293
-
294
- $this->emailnotices = mycred_apply_defaults( $this->default_prefs, $this->emailnotices );
295
-
296
- ?>
297
- <h4><div class="icon icon-active"></div><?php _e( 'Email Notices', 'mycred' ); ?></h4>
298
- <div class="body" style="display:none;">
299
- <p><?php _e( 'Settings that apply to all email notices and can not be overridden for individual emails.', 'mycred' ); ?></p>
300
- <label class="subheader" for="<?php echo $this->field_id( array( 'use_html' => 'no' ) ); ?>"><?php _e( 'Email Format', 'mycred' ); ?></label>
301
- <ol id="myCRED-email-notice-use-html">
302
- <li>
303
- <input type="radio" name="<?php echo $this->field_name( 'use_html' ); ?>" id="<?php echo $this->field_id( array( 'use_html' => 'no' ) ); ?>" <?php checked( $this->emailnotices['use_html'], 0 ); ?> value="0" />
304
- <label for="<?php echo $this->field_id( array( 'use_html' => 'no' ) ); ?>"><?php _e( 'Plain text emails only.', 'mycred' ); ?></label>
305
- </li>
306
- <li>
307
- <input type="radio" name="<?php echo $this->field_name( 'use_html' ); ?>" id="<?php echo $this->field_id( array( 'use_html' => 'yes' ) ); ?>" <?php checked( $this->emailnotices['use_html'], 1 ); ?> value="1" />
308
- <label for="<?php echo $this->field_id( array( 'use_html' => 'yes' ) ); ?>"><?php _e( 'HTML or Plain text emails.', 'mycred' ); ?></label>
309
- </li>
310
- </ol>
311
- <label class="subheader" for="<?php echo $this->field_id( array( 'filter' => 'subject' ) ); ?>"><?php _e( 'Filters', 'mycred' ); ?></label>
312
- <ol id="myCRED-email-notice-allow-filters">
313
- <li>
314
- <input type="checkbox" name="<?php echo $this->field_name( array( 'filter' => 'subject' ) ); ?>" id="<?php echo $this->field_id( array( 'filter' => 'subject' ) ); ?>" <?php checked( $this->emailnotices['filter']['subject'], 1 ); ?> value="1" />
315
- <label for="<?php echo $this->field_id( array( 'filter' => 'subject' ) ); ?>"><?php _e( 'Allow WordPress and Third Party Plugins to filter the email subject before an email is sent.', 'mycred' ); ?></label>
316
- </li>
317
- <li>
318
- <input type="checkbox" name="<?php echo $this->field_name( array( 'filter' => 'content' ) ); ?>" id="<?php echo $this->field_id( array( 'filter' => 'content' ) ); ?>" <?php checked( $this->emailnotices['filter']['content'], 1 ); ?> value="1" />
319
- <label for="<?php echo $this->field_id( array( 'filter' => 'content' ) ); ?>"><?php _e( 'Allow WordPress and Third Party Plugins to filter the email content before an email is sent.', 'mycred' ); ?></label>
320
- </li>
321
- </ol>
322
-
323
- <?php if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) : ?>
324
-
325
- <label class="subheader" for="<?php echo $this->field_id( 'send' ); ?>"><?php _e( 'Email Schedule', 'mycred' ); ?></label>
326
- <ol id="myCRED-email-notice-schedule">
327
- <li><?php _e( 'WordPress Cron is disabled. Emails will be sent immediately.', 'mycred' ); ?><input type="hidden" name="<?php echo $this->field_name( 'send' ); ?>" value="" /></li>
328
 
329
- <?php else : ?>
 
330
 
331
- <label class="subheader" for="<?php echo $this->field_id( 'send' ); ?>"><?php _e( 'Email Schedule', 'mycred' ); ?></label>
332
- <ol id="myCRED-email-notice-schedule">
333
- <li>
334
- <input type="radio" name="<?php echo $this->field_name( 'send' ); ?>" id="<?php echo $this->field_id( 'send' ); ?>-hourly" <?php checked( $this->emailnotices['send'], '' ); ?> value="" />
335
- <label for="<?php echo $this->field_id( 'send' ); ?>-hourly"><?php _e( 'Send emails immediately', 'mycred' ); ?></label>
336
- </li>
337
- <li>
338
- <input type="radio" name="<?php echo $this->field_name( 'send' ); ?>" id="<?php echo $this->field_id( 'send' ); ?>" <?php checked( $this->emailnotices['send'], 'hourly' ); ?> value="hourly" />
339
- <label for="<?php echo $this->field_id( 'send' ); ?>"><?php _e( 'Send emails once an hour', 'mycred' ); ?></label>
340
- </li>
341
- <li>
342
- <input type="radio" name="<?php echo $this->field_name( 'send' ); ?>" id="<?php echo $this->field_id( 'send' ); ?>-daily" <?php checked( $this->emailnotices['send'], 'daily' ); ?> value="daily" />
343
- <label for="<?php echo $this->field_id( 'send' ); ?>-daily"><?php _e( 'Send emails once a day', 'mycred' ); ?></label>
344
- </li>
345
- </ol>
346
- <label class="subheader" for="<?php echo $this->field_id( 'send' ); ?>"><?php _e( 'Subscriptions', 'mycred' ); ?></label>
347
- <ol id="myCRED-email-notice-schedule">
348
- <li><?php printf( __( 'Use the %s shortcode to allow users to subscribe / unsubscribe to email updates.', 'mycred' ), '<a href="http://codex.mycred.me/shortcodes/mycred_email_subscriptions/">mycred_email_subscriptions</a>' ); ?></p></li>
349
- </ol>
350
 
351
- <?php endif; ?>
 
352
 
353
- <label class="subheader" for="<?php echo $this->field_id( 'override' ); ?>"><?php _e( 'SMTP Override', 'mycred' ); ?></label>
354
- <ol id="myCRED-email-notice-override">
355
- <li>
356
- <input type="checkbox" name="<?php echo $this->field_name( 'override' ); ?>" id="<?php echo $this->field_id( 'override' ); ?>" <?php checked( $this->emailnotices['override'], 1 ); ?> value="1" />
357
- <label for="<?php echo $this->field_id( 'override' ); ?>"><?php _e( 'SMTP Debug. Enable if you are experiencing issues with wp_mail() or if you use a SMTP plugin for emails.', 'mycred' ); ?></label>
358
- </li>
359
- </ol>
360
- <p><?php _e( 'Default email settings. These settings can be individually overridden when editing emails.', 'mycred' ); ?></p>
361
- <label class="subheader"><?php _e( 'Email Settings', 'mycred' ); ?></label>
362
- <ol id="myCRED-email-default-sender">
363
- <li>
364
- <label for="<?php echo $this->field_id( array( 'from' => 'name' ) ); ?>"><?php _e( 'Senders Name:', 'mycred' ); ?></label>
365
- <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'from' => 'name' ) ); ?>" id="<?php echo $this->field_id( array( 'from' => 'name' ) ); ?>" value="<?php echo $this->emailnotices['from']['name']; ?>" class="long" /></div>
366
- </li>
367
- <li>
368
- <label for="<?php echo $this->field_id( array( 'from' => 'email' ) ); ?>"><?php _e( 'Senders Email:', 'mycred' ); ?></label>
369
- <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'from' => 'email' ) ); ?>" id="<?php echo $this->field_id( array( 'from' => 'email' ) ); ?>" value="<?php echo $this->emailnotices['from']['email']; ?>" class="long" /></div>
370
- </li>
371
- <li>
372
- <label for="<?php echo $this->field_id( array( 'from' => 'reply_to' ) ); ?>"><?php _e( 'Reply-To:', 'mycred' ); ?></label>
373
- <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'from' => 'reply_to' ) ); ?>" id="<?php echo $this->field_id( array( 'from' => 'reply_to' ) ); ?>" value="<?php echo $this->emailnotices['from']['reply_to']; ?>" class="long" /></div>
374
- </li>
375
- </ol>
376
- <label class="subheader" for="<?php echo $this->field_id( 'content' ); ?>"><?php _e( 'Default Email Content', 'mycred' ); ?></label>
377
- <ol id="myCRED-email-notice-defaults">
378
- <li>
379
- <textarea rows="10" cols="50" name="<?php echo $this->field_name( 'content' ); ?>" id="<?php echo $this->field_id( 'content' ); ?>" class="large-text code"><?php echo esc_attr( $this->emailnotices['content'] ); ?></textarea>
380
- <span class="description"><?php _e( 'Default email content.', 'mycred' ); ?></span>
381
- </li>
382
- </ol>
383
- <label class="subheader" for="<?php echo $this->field_id( 'styling' ); ?>"><?php _e( 'Default Email Styling', 'mycred' ); ?></label>
384
- <ol>
385
- <li>
386
- <textarea rows="10" cols="50" name="<?php echo $this->field_name( 'styling' ); ?>" id="<?php echo $this->field_id( 'styling' ); ?>" class="large-text code"><?php echo esc_attr( $this->emailnotices['styling'] ); ?></textarea>
387
- <span class="description"><?php _e( 'Ignored if HTML is not allowed in emails.', 'mycred' ); ?></span>
388
- </li>
389
- </ol>
390
- </div>
391
- <?php
392
 
393
  }
394
 
395
  /**
396
- * Save Settings
397
  * @since 1.1
398
- * @version 1.1
399
  */
400
- public function sanitize_extra_settings( $new_data, $data, $core ) {
401
 
402
- if ( ! isset( $data['emailnotices']['use_html'] ) )
403
- $data['emailnotices']['use_html'] = 0;
 
404
 
405
- $new_data['emailnotices']['use_html'] = ( $data['emailnotices']['use_html'] == 1 ) ? true : false;
 
406
 
407
- $new_data['emailnotices']['filter']['subject'] = ( isset( $data['emailnotices']['filter']['subject'] ) ) ? true : false;
408
- $new_data['emailnotices']['filter']['content'] = ( isset( $data['emailnotices']['filter']['content'] ) ) ? true : false;
409
 
410
- $new_data['emailnotices']['from']['name'] = sanitize_text_field( $data['emailnotices']['from']['name'] );
411
- $new_data['emailnotices']['from']['email'] = sanitize_text_field( $data['emailnotices']['from']['email'] );
412
- $new_data['emailnotices']['from']['reply_to'] = sanitize_text_field( $data['emailnotices']['from']['reply_to'] );
413
 
414
- $new_data['emailnotices']['content'] = sanitize_text_field( $data['emailnotices']['content'] );
415
- $new_data['emailnotices']['styling'] = sanitize_text_field( $data['emailnotices']['styling'] );
 
 
 
 
 
416
 
417
- $new_data['emailnotices']['send'] = sanitize_text_field( $data['emailnotices']['send'] );
418
 
419
- if ( ! isset( $data['emailnotices']['override'] ) )
420
- $data['emailnotices']['override'] = 0;
421
 
422
- $new_data['emailnotices']['override'] = ( $data['emailnotices']['override'] == 1 ) ? true : false;
 
 
423
 
424
- return $new_data;
 
 
 
425
 
426
- }
 
 
 
 
427
 
428
- /**
429
- * Email Notice Check
430
- * @since 1.4.6
431
- * @version 1.1
432
- */
433
- public function get_events_from_instance( $request, $mycred ) {
434
 
435
- extract( $request );
436
 
437
- $events = array( 'general|all' );
 
438
 
439
- // Events based on amount being given or taken
440
- if ( $amount < $mycred->zero() )
441
- $events[] = 'general|negative';
442
- else
443
- $events[] = 'general|positive';
444
 
445
- // Events based on this transaction leading to the users balance
446
- // reaching or surpassing zero
447
- $users_current_balance = $mycred->get_users_balance( $user_id, $type );
448
- if ( ( $users_current_balance - $amount ) < $mycred->zero() )
449
- $events[] = 'general|minus';
450
- elseif ( ( $users_current_balance - $amount ) == $mycred->zero() )
451
- $events[] = 'general|zero';
452
 
453
- // Ranks Related
454
- if ( function_exists( 'mycred_get_users_rank' ) ) {
455
- $rank_id = mycred_find_users_rank( $user_id, false, $type );
456
- if ( $rank_id !== NULL && mycred_user_got_demoted( $user_id, $rank_id ) )
457
- $events[] = 'ranks|negative';
458
 
459
- elseif ( $rank_id !== NULL && mycred_user_got_promoted( $user_id, $rank_id ) )
460
- $events[] = 'ranks|positive';
461
- }
462
 
463
- // Let others play
464
- return apply_filters( 'mycred_get_email_events', $events, $request, $mycred );
465
 
466
  }
467
 
468
  /**
469
- * Email Notice Check
470
  * @since 1.1
471
- * @version 1.5
472
  */
473
- public function email_check( $ran, $request, $mycred ) {
474
 
475
- // Exit now if $ran is false or new settings is not yet saved.
476
- if ( $ran === false || ! isset( $this->emailnotices['send'] ) ) return $ran;
 
 
477
 
478
- $user_id = absint( $request['user_id'] );
479
 
480
- // Construct events
481
- $events = $this->get_events_from_instance( $request, $mycred );
482
 
483
- // Badge Related
484
- if ( function_exists( 'mycred_ref_has_badge' ) ) {
 
 
 
 
485
 
486
- /*
487
- In order for us to save on database queries down the line, we will
488
- check if the user got any badges for this instnace and save the badge ids
489
- under "badges".
490
-
491
- Since the process is already completed and we are simply "reacting" to the
492
- event, we can manipulte $request without it having any effect on other features.
493
- */
494
- $badge_ids = mycred_ref_has_badge( $request['ref'] );
495
- if ( ! empty( $badge_ids ) ) {
496
- $badges = mycred_check_if_user_gets_badge( $user_id, $badge_ids, false );
497
- if ( ! empty( $badges ) ) {
498
- $events[] = 'badges|positive';
499
- $request['badges'] = $badges;
500
- }
501
- }
502
 
503
- }
 
 
 
 
 
 
 
504
 
505
- // Do not send emails now
506
- if ( $this->emailnotices['send'] != '' ) {
507
 
508
- // Save for cron job
509
- mycred_add_user_meta( $user_id, 'mycred_scheduled_email_notices', '', array(
510
- 'events' => $events,
511
- 'request' => $request
512
- ) );
 
 
 
513
 
514
- }
515
 
516
- // Send emails now
517
- else {
 
 
 
 
518
 
519
- $this->do_email_notices( $events, $request );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520
 
521
  }
522
 
523
- return $ran;
 
 
524
 
525
  }
526
 
527
  /**
528
- * Do Email Notices
529
  * @since 1.1
530
- * @version 1.2
531
  */
532
- public function do_email_notices( $events = array(), $request = array() ) {
533
-
534
- if ( ! isset( $request['user_id'] ) || empty( $events ) ) return;
535
 
536
- extract( $request );
537
 
538
- // Get all notices that a user has unsubscribed to
539
- $unsubscriptions = (array) mycred_get_user_meta( $user_id, 'mycred_email_unsubscriptions', '', true );
540
 
541
- global $wpdb;
542
 
543
- // Loop though events
544
- foreach ( $events as $event ) {
545
 
546
- // Get the email notice post object
547
- $notice = $wpdb->get_row( $wpdb->prepare( "
548
- SELECT *
549
- FROM {$wpdb->posts} notices
 
 
550
 
551
- LEFT JOIN {$wpdb->postmeta} instances
552
- ON ( notices.ID = instances.post_id AND instances.meta_key = 'mycred_email_instance' )
553
 
554
- LEFT JOIN {$wpdb->postmeta} pointtype
555
- ON ( notices.ID = pointtype.post_id AND pointtype.meta_key = 'mycred_email_ctype' )
556
 
557
- WHERE instances.meta_value = %s
558
- AND pointtype.meta_value IN (%s,'all')
559
- AND notices.post_type = 'mycred_email_notice'
560
- AND notices.post_status = 'publish';", $event, $request['type'] ) );
561
 
562
- // Notice found
563
- if ( $notice !== NULL ) {
564
 
565
- // Ignore unsubscribed events
566
- if ( in_array( $notice->ID, $unsubscriptions ) ) continue;
 
 
 
 
567
 
568
- // Get notice setup
569
- $settings = $this->get_email_settings( $notice->ID );
570
 
571
- // Send to user
572
- if ( $settings['recipient'] == 'user' || $settings['recipient'] == 'both' ) {
573
- $user = get_user_by( 'id', $user_id );
574
- $to = $user->user_email;
575
- unset( $user );
576
- }
577
-
578
- elseif ( $settings['recipient'] == 'admin' ) {
579
- $to = get_option( 'admin_email' );
580
- }
581
 
582
- // Filtered Subject
583
- if ( $this->emailnotices['filter']['subject'] === true )
584
- $subject = get_the_title( $notice->ID );
585
 
586
- // Unfiltered Subject
587
- else $subject = $notice->post_title;
 
 
 
 
588
 
589
- // Filtered Content
590
- if ( $this->emailnotices['filter']['content'] === true )
591
- $message = apply_filters( 'the_content', $notice->post_content );
592
 
593
- // Unfiltered Content
594
- else $message = $notice->post_content;
 
 
 
 
595
 
596
- $headers = array();
597
- $attachments = '';
598
 
599
- if ( ! $this->emailnotices['override'] ) {
 
600
 
601
- // Construct headers
602
- if ( $this->emailnotices['use_html'] === true ) {
603
- $headers[] = 'MIME-Version: 1.0';
604
- $headers[] = 'Content-Type: text/HTML; charset="' . get_option( 'blog_charset' ) . '"';
605
- }
606
- $headers[] = 'From: ' . $settings['senders_name'] . ' <' . $settings['senders_email'] . '>';
607
 
608
- // Reply-To
609
- if ( $settings['reply_to'] != '' )
610
- $headers[] = 'Reply-To: ' . $settings['reply_to'];
 
 
 
 
 
611
 
612
- // Both means we blank carbon copy the admin so the user does not see email
613
- if ( $settings['recipient'] == 'both' )
614
- $headers[] = 'Bcc: ' . get_option( 'admin_email' );
 
615
 
616
- // If email was successfully sent we update 'last_run'
617
- if ( $this->wp_mail( $to, $subject, $message, $headers, $attachments, $request, $notice->ID ) === true )
618
- update_post_meta( $notice->ID, 'mycred_email_last_run', time() );
619
 
620
- }
621
- else {
622
 
623
- // If email was successfully sent we update 'last_run'
624
- if ( $this->wp_mail( $to, $subject, $message, $headers, $attachments, $request, $notice->ID ) === true ) {
625
- update_post_meta( $notice->ID, 'mycred_email_last_run', time() );
626
 
627
- if ( $settings['recipient'] == 'both' )
628
- $this->wp_mail( get_option( 'admin_email' ), $subject, $message, $headers, $attachments, $request, $notice->ID );
 
 
 
 
 
 
 
 
 
 
 
629
  }
630
 
631
  }
@@ -634,653 +556,905 @@ if ( ! class_exists( 'myCRED_Email_Notice_Module' ) ) :
634
 
635
  }
636
 
637
- }
638
-
639
- /**
640
- * WP Mail
641
- * @since 1.1
642
- * @version 1.3.1
643
- */
644
- public function wp_mail( $to, $subject, $message, $headers, $attachments, $request, $email_id ) {
645
-
646
- // Let others play before we do our thing
647
- $filtered = apply_filters( 'mycred_email_before_send', compact( 'to', 'subject', 'message', 'headers', 'attachments', 'request', 'email_id' ) );
648
-
649
- if ( ! isset( $filtered['request'] ) || ! is_array( $filtered['request'] ) ) return false;
650
-
651
- $subject = $this->template_tags_request( $filtered['subject'], $filtered['request'] );
652
- $message = $this->template_tags_request( $filtered['message'], $filtered['request'] );
653
-
654
- $entry = new stdClass();
655
- foreach ( $filtered['request'] as $key => $value )
656
- $entry->$key = $value;
657
-
658
- $mycred = mycred( $filtered['request']['type'] );
659
-
660
- $subject = $mycred->template_tags_user( $subject, $filtered['request']['user_id'] );
661
- $message = $mycred->template_tags_user( $message, $filtered['request']['user_id'] );
 
662
 
663
- $subject = $mycred->template_tags_amount( $subject, $filtered['request']['amount'] );
664
- $message = $mycred->template_tags_amount( $message, $filtered['request']['amount'] );
665
-
666
- $subject = $mycred->parse_template_tags( $subject, $entry );
667
- $message = $mycred->parse_template_tags( $message, $entry );
668
 
669
- // Construct HTML Content
670
- if ( $this->emailnotices['use_html'] === true ) {
671
- $styling = $this->get_email_styling( $email_id );
672
- $message = '<html><head><title>' . $subject . '</title><style type="text/css" media="all"> ' . trim( $styling ) . '</style></head><body>' . nl2br( $message ) . '</body></html>';
673
  }
674
 
675
- // Send Email
676
- add_filter( 'wp_mail_content_type', array( $this, 'get_email_format' ) );
677
- $result = wp_mail( $filtered['to'], $subject, $message, $filtered['headers'], $filtered['attachments'] );
678
- remove_filter( 'wp_mail_content_type', array( $this, 'get_email_format' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
679
 
680
- // Let others play
681
- do_action( 'mycred_email_sent', $filtered );
682
 
683
- return $result;
 
684
 
685
  }
686
 
687
  /**
688
- * Get Email Format
689
  * @since 1.1
690
  * @version 1.0
691
  */
692
- public function get_email_format() {
693
 
694
- if ( $this->emailnotices['use_html'] === false )
695
- return 'text/plain';
 
 
 
 
 
 
 
 
 
696
 
697
- return 'text/html';
 
 
698
 
699
  }
700
 
701
  /**
702
- * Request Related Template Tags
703
  * @since 1.1
704
- * @version 1.2.1
705
  */
706
- public function template_tags_request( $content, $request ) {
707
-
708
- $type = $this->core;
709
- if ( $request['type'] != 'mycred_default' )
710
- $type = mycred( $request['type'] );
711
-
712
- $user_id = $request['user_id'];
713
- $new_balance = $type->get_users_balance( $user_id, $request['type'] );
714
-
715
- $content = str_replace( '%new_balance%', $new_balance, $content );
716
- $content = str_replace( '%new_balance_f%', $type->format_creds( $new_balance ), $content );
717
-
718
- if ( $request['amount'] > 0 )
719
- $old_balance = $type->number( $new_balance - $request['amount'] );
720
- else
721
- $old_balance = $type->number( $new_balance + $request['amount'] );
722
-
723
- $content = str_replace( '%old_balance%', $old_balance, $content );
724
- $content = str_replace( '%old_balance_f%', $type->format_creds( $old_balance ), $content );
725
 
726
- $content = str_replace( '%amount%', $request['amount'], $content );
727
- $content = str_replace( '%entry%', $request['entry'], $content );
728
- $content = str_replace( '%data%', $request['data'], $content );
729
-
730
- $content = str_replace( '%blog_name%', get_option( 'blogname' ), $content );
731
- $content = str_replace( '%blog_url%', get_option( 'home' ), $content );
732
- $content = str_replace( '%blog_info%', get_option( 'blogdescription' ), $content );
733
- $content = str_replace( '%admin_email%', get_option( 'admin_email' ), $content );
734
- $content = str_replace( '%num_members%', $this->core->count_members(), $content );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
735
 
736
- // Badges related
737
- if ( function_exists( 'mycred_ref_has_badge' ) && isset( $request['badges'] ) ) {
738
 
739
- $titles = array();
740
- $images = array();
741
- foreach ( $request['badges'] as $level => $badge_id ) {
 
 
 
742
 
743
- $badge_id = absint( $badge_id );
744
- $title = sprintf( _x( '%s - Level %d', 'Badge Title - Level 1,2,3..', 'mycred' ), get_the_title( $badge_id ), $level );
745
- $titles[] = $title;
746
 
747
- // Level image first
748
- $level_image = get_post_meta( $badge_id, 'level_image' . $level, true );
749
 
750
- // Default to main image
751
- if ( $level_image == '' )
752
- $level_image = get_post_meta( $badge_id, 'main_image', true );
 
753
 
754
- if ( $level_image == '' )
755
- $images[] = '<img src="' . $level_image . '" alt="' . $title . '" />';
756
 
757
- }
758
-
759
- $title = implode( ', ', $titles );
760
- $content = str_replace( '%badge_title%', $title, $content );
761
 
762
- $image = implode( ' ', $images );
763
- $content = str_replace( '%badge_image%', $image, $content );
 
 
764
 
765
- }
 
 
 
766
 
767
- return $content;
 
 
 
768
 
769
- }
 
 
 
770
 
771
- /**
772
- * Get Email Settings
773
- * @since 1.1
774
- * @version 1.1
775
- */
776
- public function get_email_settings( $post_id ) {
777
 
778
- $settings = get_post_meta( $post_id, 'mycred_email_settings', true );
779
- if ( $settings == '' )
780
- $settings = array();
781
 
782
- // Defaults
783
- $default = array(
784
- 'recipient' => 'user',
785
- 'senders_name' => $this->emailnotices['from']['name'],
786
- 'senders_email' => $this->emailnotices['from']['email'],
787
- 'reply_to' => $this->emailnotices['from']['reply_to'],
788
- 'label' => ''
789
- );
790
 
791
- $settings = mycred_apply_defaults( $default, $settings );
 
792
 
793
- return apply_filters( 'mycred_email_notice_settings', $settings, $post_id );
 
 
794
 
795
  }
796
 
797
  /**
798
- * Get Email Styling
799
  * @since 1.1
800
  * @version 1.0
801
  */
802
- public function get_email_styling( $post_id ) {
803
-
804
- if ( $this->emailnotices['use_html'] === false ) return '';
805
- $style = get_post_meta( $post_id, 'mycred_email_styling', true );
806
-
807
- // Defaults
808
- if ( empty( $style ) )
809
- return $this->emailnotices['styling'];
810
 
811
- return $style;
 
 
 
 
812
 
813
  }
814
 
815
  /**
816
- * Adjust Row Actions
817
- * @since 1.1
818
  * @version 1.0
819
  */
820
- public function adjust_row_actions( $actions, $post ) {
821
 
822
- if ( $post->post_type == 'mycred_email_notice' ) {
823
- unset( $actions['inline hide-if-no-js'] );
824
- unset( $actions['view'] );
825
- }
 
 
 
 
826
 
827
- return $actions;
 
 
 
 
 
 
 
828
 
829
  }
830
 
831
  /**
832
- * Adjust Column Header
833
  * @since 1.1
834
  * @version 1.1
835
  */
836
- public function adjust_column_headers( $defaults ) {
837
 
838
- $columns = array();
839
- $columns['cb'] = $defaults['cb'];
 
 
 
 
 
 
 
 
840
 
841
- // Add / Adjust
842
- $columns['title'] = __( 'Email Subject', 'mycred' );
843
- $columns['mycred-email-status'] = __( 'Status', 'mycred' );
844
- $columns['mycred-email-reference'] = __( 'Setup', 'mycred' );
 
 
 
845
 
846
- if ( count( $this->point_types ) > 1 )
847
- $columns['mycred-email-ctype'] = __( 'Point Type', 'mycred' );
 
 
 
 
 
 
848
 
849
- // Return
850
- return $columns;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
851
 
852
  }
853
 
854
  /**
855
- * Adjust Column Content
856
  * @since 1.1
857
  * @version 1.0
858
  */
859
- public function adjust_column_content( $column_name, $post_id ) {
860
-
861
- // Get the post
862
- if ( in_array( $column_name, array( 'mycred-email-status', 'mycred-email-reference', 'mycred-email-ctype' ) ) )
863
- $post = get_post( $post_id );
864
 
865
- // Email Status Column
866
- if ( $column_name == 'mycred-email-status' ) {
867
 
868
- if ( $post->post_status != 'publish' && $post->post_status != 'future' )
869
- echo '<p>' . __( 'Not Active', 'mycred' ) . '</p>';
 
870
 
871
- elseif ( $post->post_status == 'future' )
872
- echo '<p>' . sprintf( __( 'Scheduled:<br /><strong>%1$s</strong>', 'mycred' ), date_i18n( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), strtotime( $post->post_date ) ) ) . '</p>';
873
 
874
- else {
875
- $date = get_post_meta( $post_id, 'mycred_email_last_run', true );
876
- if ( empty( $date ) )
877
- echo '<p>' . __( 'Active', 'mycred' ) . '</p>';
878
- else
879
- echo '<p>' . sprintf( __( 'Active - Last run:<br /><strong>%1$s</strong>', 'mycred' ), date_i18n( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), $date ) ) . '</p>';
880
- }
881
 
882
- }
883
 
884
- // Email Setup Column
885
- elseif ( $column_name == 'mycred-email-reference' ) {
 
 
 
 
886
 
887
- echo '<p>';
888
- $instance_key = get_post_meta( $post->ID, 'mycred_email_instance', true );
889
- $label = $this->get_instance( $instance_key );
890
 
891
- if ( ! empty( $instance_key ) && ! empty( $label ) )
892
- echo '<em>' . __( 'Email is sent when', 'mycred' ) .' ' . $label . '.</em></br />';
893
- else
894
- echo '<em>' . __( 'Missing instance for this notice!', 'mycred' ) . '</em><br />';
895
 
896
- $settings = get_post_meta( $post->ID, 'mycred_email_settings', true );
897
- if ( ! empty( $settings ) && isset( $settings['recipient'] ) )
898
- $recipient = $settings['recipient'];
899
- else
900
- $recipient = 'user';
901
 
902
- if ( $recipient == 'user' )
903
- echo '<strong>' . __( 'Sent To', 'mycred' ) . '</strong>: ' . __( 'User', 'mycred' ) . '</p>';
904
- elseif ( $recipient == 'admin' )
905
- echo '<strong>' . __( 'Sent To', 'mycred' ) . '</strong>: ' . __( 'Administrator', 'mycred' ) . '</p>';
906
- else
907
- echo '<strong>' . __( 'Sent To', 'mycred' ) . '</strong>: ' . __( 'Both Administrator and User', 'mycred' ) . '</p>';
 
908
 
909
- }
 
910
 
911
- // Email Setup Column
912
- elseif ( $column_name == 'mycred-email-ctype' ) {
913
 
914
- $type = get_post_meta( $post_id, 'mycred_email_ctype', true );
915
- if ( $type == '' ) $type = 'all';
916
 
917
- if ( $type == 'all' )
918
- echo __( 'All types', 'mycred' );
919
 
920
- elseif ( array_key_exists( $type, $this->point_types ) )
921
- echo $this->point_types[ $type ];
922
 
923
- else
924
- echo '-';
925
 
926
  }
927
 
 
 
 
928
  }
929
 
930
  /**
931
- * Add Meta Boxes
932
  * @since 1.1
933
- * @version 1.0
934
  */
935
- public function add_meta_boxes() {
936
 
937
- add_meta_box(
938
- 'mycred_email_settings',
939
- __( 'Email Settings', 'mycred' ),
940
- array( $this, 'email_settings' ),
941
- 'mycred_email_notice',
942
- 'side',
943
- 'high'
944
- );
945
 
946
- add_meta_box(
947
- 'mycred_email_template_tags',
948
- __( 'Available Template Tags', 'mycred' ),
949
- array( $this, 'template_tags' ),
950
- 'mycred_email_notice',
951
- 'normal',
952
- 'core'
953
- );
954
 
955
- if ( $this->emailnotices['use_html'] === false ) return;
 
956
 
957
- add_meta_box(
958
- 'mycred_email_header',
959
- __( 'Email Header', 'mycred' ),
960
- array( $this, 'email_header' ),
961
- 'mycred_email_notice',
962
- 'normal',
963
- 'high'
964
- );
965
 
966
- }
 
 
 
 
967
 
968
- /**
969
- * Disable WYSIWYG Editor
970
- * @since 1.1
971
- * @version 1.0
972
- */
973
- public function disable_richedit( $default ) {
974
 
975
- global $post;
 
976
 
977
- if ( $post->post_type == 'mycred_email_notice' )
978
- return false;
979
 
980
- return $default;
 
 
981
 
982
  }
983
 
984
  /**
985
- * Adjust Enter Title Here
986
- * @since 1.1
987
  * @version 1.0
988
  */
989
- public function enter_title_here( $title ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
990
 
991
- global $post_type;
 
992
 
993
- if ( $post_type == 'mycred_email_notice' )
994
- return __( 'Email Subject', 'mycred' );
 
 
 
995
 
996
- return $title;
 
 
 
 
 
 
 
997
 
998
  }
999
 
1000
  /**
1001
- * Apply Default Content
1002
  * @since 1.1
1003
- * @version 1.0
1004
  */
1005
- public function default_content( $content ) {
1006
 
1007
- global $post_type;
1008
 
1009
- if ( $post_type == 'mycred_email_notice' && !empty( $this->emailnotices['content'] ) )
1010
- $content = $this->emailnotices['content'];
1011
 
1012
- return $content;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1013
 
1014
  }
1015
 
1016
  /**
1017
- * Email Settings Metabox
1018
  * @since 1.1
1019
- * @version 1.1
1020
  */
1021
- public function email_settings( $post ) {
1022
-
1023
- // Get instance
1024
- $instance = get_post_meta( $post->ID, 'mycred_email_instance', true );
1025
 
1026
- // Get settings
1027
- $settings = $this->get_email_settings( $post->ID );
1028
 
1029
- $set_type = get_post_meta( $post->ID, 'mycred_email_ctype', true );
1030
- if ( $set_type == '' )
1031
- $set_type = 'mycred_default';
1032
 
1033
- ?>
1034
- <div class="misc-pub-section">
1035
- <input type="hidden" name="mycred_email[token]" value="<?php echo wp_create_nonce( 'mycred-edit-email' ); ?>" />
1036
- <label for="mycred-email-instance"<?php if ( $post->post_status == 'publish' && empty( $instance ) ) echo ' style="color:red;font-weight:bold;"'; ?>><?php _e( 'Send this email notice when...', 'mycred' ); ?></label><br />
1037
- <select name="mycred_email[instance]" id="mycred-email-instance">
1038
- <?php
1039
 
1040
- // Default
1041
- echo '<option value=""';
1042
- if ( empty( $instance ) ) echo ' selected="selected"';
1043
- echo '>' . __( 'Select', 'mycred' ) . '</option>';
1044
 
1045
- // Loop though instances
1046
- foreach ( $this->instances as $hook_ref => $values ) {
1047
 
1048
- if ( is_array( $values ) ) {
1049
- foreach ( $values as $key => $value ) {
1050
 
1051
- // Make sure that the submitted value is unique
1052
- $key_value = $hook_ref . '|' . $key;
 
 
 
1053
 
1054
- // Option group starts with 'label'
1055
- if ( $key == 'label' )
1056
- echo '<optgroup label="' . $value . '">';
 
 
1057
 
1058
- // Option group ends with 'end'
1059
- elseif ( $key == 'end' )
1060
- echo '</optgroup>';
 
1061
 
1062
- // The selectable options
1063
- else {
1064
- echo '<option value="' . $key_value . '"';
1065
- if ( $instance == $key_value ) echo ' selected="selected"';
1066
- echo '>... ' . $this->core->template_tags_general( $value ) . '</option>';
1067
- }
1068
 
1069
- }
1070
 
1071
- }
1072
 
1073
- }
 
 
 
 
 
1074
 
1075
- ?>
1076
- </select><br />
1077
- <label for="mycred-email-recipient-user"><?php _e( 'Recipient:', 'mycred' ); ?></label><br />
1078
- <div class="mycred-inline">
1079
- <label for="mycred-email-recipient-user"><input type="radio" name="mycred_email[recipient]" id="mycred-email-recipient-user" value="user" <?php checked( $settings['recipient'], 'user' ); ?> /> <?php _e( 'User', 'mycred' ); ?></label>
1080
- <label for="mycred-email-recipient-admin"><input type="radio" name="mycred_email[recipient]" id="mycred-email-recipient-admin" value="admin" <?php checked( $settings['recipient'], 'admin' ); ?> /> <?php _e( 'Administrator', 'mycred' ); ?></label>
1081
- <label for="mycred-email-recipient-both"><input type="radio" name="mycred_email[recipient]" id="mycred-email-recipient-both" value="both" <?php checked( $settings['recipient'], 'both' ); ?> /> <?php _e( 'Both', 'mycred' ); ?></label>
1082
- </div>
1083
- </div>
1084
- <div class="misc-pub-section">
1085
- <label for="mycred-email-label"><?php _e( 'Label', 'mycred' ); ?></label><br />
1086
- <input type="text" name="mycred_email[label]" id="mycred-email-label" value="<?php echo $settings['label']; ?>" />
1087
- </div>
1088
 
1089
- <?php if ( count( $this->point_types ) > 1 ) : ?>
1090
 
1091
- <div class="misc-pub-section">
1092
- <label for="mycred-email-ctype"><?php _e( 'Point Type', 'mycred' ); ?></label><br />
1093
- <select name="mycred_email[ctype]" id="mycred-email-ctype">
1094
- <?php
1095
 
1096
- echo '<option value="all"';
1097
- if ( $set_type == 'all' ) echo ' selected="selected"';
1098
- echo '>' . __( 'All types', 'mycred' ) . '</option>';
 
 
 
1099
 
1100
- foreach ( $this->point_types as $type_id => $label ) {
1101
- echo '<option value="' . $type_id . '"';
1102
- if ( $set_type == $type_id ) echo ' selected="selected"';
1103
- echo '>' . $label . '</option>';
1104
- }
1105
 
1106
- ?>
1107
- </select>
1108
- </div>
1109
 
1110
- <?php else : ?>
 
1111
 
1112
- <input type="hidden" name="mycred_email[ctype]" id="mycred-email-ctype" value="mycred_default" />
 
 
 
1113
 
1114
- <?php endif; ?>
 
1115
 
1116
- <div class="misc-pub-section">
1117
- <label for="mycred-email-senders-name"><?php _e( 'Senders Name:', 'mycred' ); ?></label><br />
1118
- <input type="text" name="mycred_email[senders_name]" id="mycred-email-senders-name" value="<?php echo $settings['senders_name']; ?>" /><br />
1119
- <label for="mycred-email-senders-email"><?php _e( 'Senders Email:', 'mycred' ); ?></label><br />
1120
- <input type="text" name="mycred_email[senders_email]" id="mycred-email-senders-email" value="<?php echo $settings['senders_email']; ?>" /><br />
1121
- <label for="mycred-email-reply-to"><?php _e( 'Reply-To Email:', 'mycred' ); ?></label><br />
1122
- <input type="text" name="mycred_email[reply_to]" id="mycred-email-reply-to" value="<?php echo $settings['reply_to']; ?>" />
1123
- </div>
1124
 
1125
- <?php do_action( 'mycred_email_settings_box', $this ); ?>
 
 
 
 
1126
 
1127
- <div class="mycred-save">
1128
- <?php submit_button( __( 'Save', 'mycred' ), 'primary', 'save', false ); ?>
1129
- </div>
1130
- <?php
1131
 
1132
  }
1133
 
1134
  /**
1135
- * Email Header Metabox
1136
  * @since 1.1
1137
- * @version 1.0
1138
  */
1139
- public function email_header( $post ) {
1140
 
1141
- ?>
1142
- <p><label for="mycred-email-styling"><?php _e( 'CSS Styling', 'mycred' ); ?></label></p>
1143
- <textarea name="mycred_email[styling]" id="mycred-email-styling"><?php echo $this->get_email_styling( $post->ID ); ?></textarea>
1144
 
1145
- <?php do_action( 'mycred_email_header_box', $this ); ?>
 
 
 
 
 
 
 
1146
 
1147
- <?php
 
 
1148
 
1149
  }
1150
 
1151
  /**
1152
- * Template Tags Metabox
1153
  * @since 1.1
1154
- * @version 1.2
1155
  */
1156
- public function template_tags( $post ) {
1157
-
1158
- echo '
1159
- <ul>
1160
- <li class="title">' . __( 'Site Related', 'mycred' ) . '</li>
1161
- <li><strong>%blog_name%</strong><div>' . __( 'Your websites title', 'mycred' ) . '</div></li>
1162
- <li><strong>%blog_url%</strong><div>' . __( 'Your websites address', 'mycred' ) . '</div></li>
1163
- <li><strong>%blog_info%</strong><div>' . __( 'Your websites tagline (description)', 'mycred' ) . '</div></li>
1164
- <li><strong>%admin_email%</strong><div>' . __( 'Your websites admin email', 'mycred' ) . '</div></li>
1165
- <li><strong>%num_members%</strong><div>' . __( 'Total number of blog members', 'mycred' ) . '</div></li>
1166
- </ul>
1167
- <ul>
1168
- <li class="title">Instance Related</li>
1169
- <li><strong>%new_balance%</strong><div>' . __( 'The users new balance', 'mycred' ) . '</div></li>
1170
- <li><strong>%old_balance%</strong><div>' . __( 'The users old balance', 'mycred' ) . '</div></li>
1171
- <li><strong>%amount%</strong><div>' . __( 'The amount of points gained or lost in this instance', 'mycred' ) . '</div></li>
1172
- <li><strong>%entry%</strong><div>' . __( 'The log entry', 'mycred' ) . '</div></li>
1173
- </ul>
1174
- <div class="clear"></div>';
1175
 
1176
  }
1177
 
1178
  /**
1179
- * Save Email Notice Details
1180
  * @since 1.1
1181
- * @version 1.2
1182
  */
1183
- public function save_email_notice( $post_id ) {
1184
-
1185
- if ( ! isset( $_POST['mycred_email'] ) || ! is_array( $_POST['mycred_email'] ) || ! wp_verify_nonce( $_POST['mycred_email']['token'], 'mycred-edit-email' ) ) return;
1186
-
1187
- // Update Instance
1188
- if ( ! empty( $_POST['mycred_email']['instance'] ) ) {
1189
- // Lets make sure the value is properly formatted otherwise things could go uggly later
1190
- $instance_key = trim( $_POST['mycred_email']['instance'] );
1191
- $keys = explode( '|', $instance_key );
1192
- if ( $keys !== false && !empty( $keys ) );
1193
- update_post_meta( $post_id, 'mycred_email_instance', $instance_key );
1194
- }
1195
-
1196
- // Construct new settings
1197
- $settings = array();
1198
-
1199
- // If recipient is set but differs from the default, use the posted one else use default
1200
- if ( ! empty( $_POST['mycred_email']['recipient'] ) )
1201
- $settings['recipient'] = $_POST['mycred_email']['recipient'];
1202
- else
1203
- $settings['recipient'] = 'user';
1204
 
1205
- // If senders name is set but differs from the default, use the posted one else use default
1206
- if ( ! empty( $_POST['mycred_email']['senders_name'] ) )
1207
- $settings['senders_name'] = $_POST['mycred_email']['senders_name'];
1208
- else
1209
- $settings['senders_name'] = $this->emailnotices['from']['name'];
1210
 
1211
- // If senders email is set but differs from the default, use the posted one else use default
1212
- if ( ! empty( $_POST['mycred_email']['senders_email'] ) )
1213
- $settings['senders_email'] = $_POST['mycred_email']['senders_email'];
1214
- else
1215
- $settings['senders_email'] = $this->emailnotices['from']['email'];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1216
 
1217
- // If senders email is set but differs from the default, use the posted one else use default
1218
- if ( ! empty( $_POST['mycred_email']['reply_to'] ) )
1219
- $settings['reply_to'] = $_POST['mycred_email']['reply_to'];
1220
- else
1221
- $settings['reply_to'] = $this->emailnotices['from']['reply_to'];
1222
 
1223
- $settings['label'] = sanitize_text_field( $_POST['mycred_email']['label'] );
 
 
 
1224
 
1225
- // Save settings
1226
- update_post_meta( $post_id, 'mycred_email_settings', $settings );
1227
 
1228
- $point_type = sanitize_text_field( $_POST['mycred_email']['ctype'] );
1229
- update_post_meta( $post_id, 'mycred_email_ctype', $point_type );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1230
 
1231
- // If rich editing is disabled bail now
1232
- if ( $this->emailnotices['use_html'] === false ) return;
1233
 
1234
- // Save styling
1235
- if ( ! empty( $_POST['mycred_email']['styling'] ) )
1236
- update_post_meta( $post_id, 'mycred_email_styling', trim( $_POST['mycred_email']['styling'] ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1237
 
1238
  }
1239
 
1240
  /**
1241
- * Adjust Post Updated Messages
1242
  * @since 1.1
1243
- * @version 1.0
1244
  */
1245
- public function post_updated_messages( $messages ) {
1246
 
1247
- global $post;
 
1248
 
1249
- $messages['mycred_email_notice'] = array(
1250
- 0 => '',
1251
- 1 => __( 'Email Notice Updated.', 'mycred' ),
1252
- 2 => '',
1253
- 3 => '',
1254
- 4 => __( 'Email Notice Updated.', 'mycred' ),
1255
- 5 => false,
1256
- 6 => __( 'Email Notice Activated', 'mycred' ),
1257
- 7 => __( 'Email Notice Saved', 'mycred' ),
1258
- 8 => '',
1259
- 9 => '',
1260
- 10 => __( 'Email Notice Updated.', 'mycred' )
1261
- );
1262
 
1263
- return $messages;
 
1264
 
1265
- }
 
 
1266
 
1267
- /**
1268
- * Add Publish Notice
1269
- * @since 1.1
1270
- * @version 1.0
1271
- */
1272
- public function publish_warning() {
1273
 
1274
- global $post;
1275
 
1276
- if ( $post->post_type != 'mycred_email_notice' ) return;
 
1277
 
1278
- if ( $post->post_status != 'publish' && $post->post_status != 'future' )
1279
- echo '<p id="mycred-email-notice">' . __( 'Once a notice is "published" it becomes active! Select "Save Draft" if you are not yet ready to use this email notice!', 'mycred' ) . '</p>';
1280
- elseif ( $post->post_status == 'future' )
1281
- echo '<p id="mycred-email-notice">' . sprintf( __( 'This notice will become active on:<br /><strong>%1$s</strong>', 'mycred' ), date_i18n( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), strtotime( $post->post_date ) ) ) . '</p>';
1282
- else
1283
- echo '<p id="mycred-email-notice">' . __( 'This email notice is active.', 'mycred' ) . '</p>';
1284
 
1285
  }
1286
 
@@ -1292,7 +1466,7 @@ if ( ! class_exists( 'myCRED_Email_Notice_Module' ) ) :
1292
  public function render_subscription_shortcode( $attr, $content = NULL ) {
1293
 
1294
  extract( shortcode_atts( array(
1295
- 'success' => __( 'Settings saved.', 'mycred' )
1296
  ), $attr ) );
1297
 
1298
  if ( ! is_user_logged_in() ) return $content;
@@ -1385,10 +1559,24 @@ if ( ! class_exists( 'myCRED_Email_Notice_Module' ) ) :
1385
 
1386
  }
1387
 
1388
- $email_notice = new myCRED_Email_Notice_Module();
1389
- $email_notice->load();
 
 
 
 
 
 
 
 
 
 
1390
 
 
 
 
1391
  endif;
 
1392
 
1393
  /**
1394
  * myCRED Email Notifications Cron Job
3
  * Addon: Email Notices
4
  * Addon URI: http://mycred.me/add-ons/email-notices/
5
  * Version: 1.3.1
 
 
 
6
  */
7
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
 
9
  define( 'myCRED_EMAIL', __FILE__ );
10
+ define( 'myCRED_EMAIL_DIR', myCRED_ADDONS_DIR . 'email-notices/' );
11
+ define( 'myCRED_EMAIL_VERSION', '1.3.1' );
12
 
13
  /**
14
  * myCRED_Email_Notice_Module class
44
  'override' => 0
45
  ),
46
  'register' => false,
47
+ 'add_to_core' => true,
48
+ 'menu_pos' => 90
49
  ) );
50
 
51
  }
57
  */
58
  public function module_init() {
59
 
60
+ $this->register_email_notices();
61
  $this->setup_instances();
62
 
63
+ add_action( 'mycred_admin_enqueue', array( $this, 'enqueue_scripts' ) );
64
+ add_filter( 'mycred_add_finished', array( $this, 'email_check' ), 80, 3 );
65
+ add_action( 'mycred_badge_level_reached', array( $this, 'badge_check' ), 10, 3 );
66
+ add_action( 'mycred_send_email_notices', 'mycred_email_notice_cron_job' );
67
 
68
  add_shortcode( 'mycred_email_subscriptions', array( $this, 'render_subscription_shortcode' ) );
69
 
70
+ add_action( 'mycred_add_menu', array( $this, 'add_to_menu' ), $this->menu_pos );
71
+
72
  // Schedule Cron
73
  if ( ! isset( $this->emailnotices['send'] ) ) return;
74
 
90
  */
91
  public function module_admin_init() {
92
 
93
+ add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
 
94
 
95
  add_filter( 'manage_mycred_email_notice_posts_columns', array( $this, 'adjust_column_headers' ), 50 );
96
  add_action( 'manage_mycred_email_notice_posts_custom_column', array( $this, 'adjust_column_content' ), 10, 2 );
97
 
98
+ add_filter( 'parent_file', array( $this, 'parent_file' ) );
99
+ add_filter( 'submenu_file', array( $this, 'subparent_file' ), 10, 2 );
100
+
101
+ add_action( 'admin_head', array( $this, 'admin_header' ) );
102
+
103
  add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
104
+ add_filter( 'post_row_actions', array( $this, 'adjust_row_actions' ), 10, 2 );
105
 
106
+ add_filter( 'default_content', array( $this, 'default_content' ) );
107
  add_action( 'post_submitbox_start', array( $this, 'publish_warning' ) );
108
+
109
+ add_action( 'save_post_mycred_email_notice', array( $this, 'save_email_notice' ), 10, 2 );
110
 
111
  if ( $this->emailnotices['use_html'] === false )
112
  add_filter( 'user_can_richedit', array( $this, 'disable_richedit' ) );
114
  }
115
 
116
  /**
117
+ * Register Email Notice Post Type
118
  * @since 1.1
119
+ * @version 1.1
120
  */
121
+ protected function register_email_notices() {
122
 
123
+ $labels = array(
124
+ 'name' => __( 'Email Notifications', 'mycred' ),
125
+ 'singular_name' => __( 'Email Notification', 'mycred' ),
126
+ 'add_new' => __( 'Add New', 'mycred' ),
127
+ 'add_new_item' => __( 'Add New', 'mycred' ),
128
+ 'edit_item' => __( 'Edit Email Notification', 'mycred' ),
129
+ 'new_item' => __( 'New Email Notification', 'mycred' ),
130
+ 'all_items' => __( 'Email Notifications', 'mycred' ),
131
+ 'view_item' => '',
132
+ 'search_items' => __( 'Search Email Notifications', 'mycred' ),
133
+ 'not_found' => __( 'No email notifications found', 'mycred' ),
134
+ 'not_found_in_trash' => __( 'No email notifications found in Trash', 'mycred' ),
135
+ 'parent_item_colon' => '',
136
+ 'menu_name' => __( 'Email Notifications', 'mycred' )
137
+ );
138
+ $args = array(
139
+ 'labels' => $labels,
140
+ 'supports' => array( 'title', 'editor' ),
141
+ 'hierarchical' => true,
142
+ 'public' => false,
143
+ 'show_ui' => true,
144
+ 'show_in_menu' => false,
145
+ 'show_in_nav_menus' => false,
146
+ 'show_in_admin_bar' => false,
147
+ 'can_export' => true,
148
+ 'has_archive' => false,
149
+ 'exclude_from_search' => true,
150
+ 'publicly_queryable' => false,
151
+ 'register_meta_box_cb' => array( $this, 'add_metaboxes' )
152
+ );
153
+
154
+ register_post_type( 'mycred_email_notice', apply_filters( 'mycred_register_emailnotices', $args ) );
155
 
156
  }
157
 
158
  /**
159
+ * Adjust Post Updated Messages
160
  * @since 1.1
161
  * @version 1.1
162
  */
163
+ public function post_updated_messages( $messages ) {
 
 
 
 
 
 
 
 
 
164
 
165
+ $messages['mycred_email_notice'] = array(
166
+ 0 => '',
167
+ 1 => __( 'Email Notice Updated.', 'mycred' ),
168
+ 2 => __( 'Email Notice Updated.', 'mycred' ),
169
+ 3 => __( 'Email Notice Updated.', 'mycred' ),
170
+ 4 => __( 'Email Notice Updated.', 'mycred' ),
171
+ 5 => false,
172
+ 6 => __( 'Email Notice Activated.', 'mycred' ),
173
+ 7 => __( 'Email Notice Updated.', 'mycred' ),
174
+ 8 => __( 'Email Notice Updated.', 'mycred' ),
175
+ 9 => __( 'Email Notice Updated.', 'mycred' ),
176
+ 10 => __( 'Email Notice Updated.', 'mycred' )
177
  );
178
 
179
+ return $messages;
 
 
 
180
 
181
+ }
 
 
182
 
183
+ /**
184
+ * Add Admin Menu Item
185
+ * @since 1.7
186
+ * @version 1.0
187
+ */
188
+ public function add_to_menu() {
189
+
190
+ add_submenu_page(
191
+ 'mycred',
192
+ __( 'Email Notifications', 'mycred' ),
193
+ __( 'Email Notifications', 'mycred' ),
194
+ $this->core->edit_creds_cap(),
195
+ 'edit.php?post_type=mycred_email_notice'
196
+ );
197
 
198
  }
199
 
200
  /**
201
+ * Parent File
202
+ * @since 1.7
203
  * @version 1.0
204
  */
205
+ public function parent_file( $parent = '' ) {
206
 
207
+ global $pagenow;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
+ if ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'mycred_email_notice' && isset( $_GET['action'] ) && $_GET['action'] == 'edit' )
210
+ return 'mycred';
211
+
212
+ if ( $pagenow == 'post-new.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_email_notice' )
213
+ return 'mycred';
214
+
215
+ return $parent;
216
 
217
  }
218
 
219
  /**
220
+ * Sub Parent File
221
+ * @since 1.7
222
+ * @version 1.0
223
  */
224
+ public function subparent_file( $subparent = '', $parent = '' ) {
225
 
226
+ global $pagenow;
 
 
 
 
 
 
 
 
 
227
 
228
+ if ( ( $pagenow == 'edit.php' || $pagenow == 'post-new.php' ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_email_notice' ) {
 
 
 
 
 
 
229
 
230
+ return 'edit.php?post_type=mycred_email_notice';
231
+
 
 
 
 
 
232
  }
233
 
234
+ elseif ( $pagenow == 'post.php' && isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'mycred_email_notice' ) {
 
 
 
 
 
 
235
 
236
+ return 'edit.php?post_type=mycred_email_notice';
 
 
 
 
 
 
 
237
 
 
 
 
 
 
 
 
238
  }
239
 
240
+ return $subparent;
241
 
242
  }
243
 
244
  /**
245
+ * Adjust Enter Title Here
246
  * @since 1.1
247
  * @version 1.0
248
  */
249
+ public function enter_title_here( $title ) {
 
 
 
250
 
251
+ global $post_type;
 
 
252
 
253
+ if ( $post_type == 'mycred_email_notice' )
254
+ return __( 'Email Subject', 'mycred' );
255
 
256
+ return $title;
257
 
258
  }
259
 
260
  /**
261
+ * Adjust Column Header
262
  * @since 1.1
263
  * @version 1.1
264
  */
265
+ public function adjust_column_headers( $defaults ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
 
267
+ $columns = array();
268
+ $columns['cb'] = $defaults['cb'];
269
 
270
+ // Add / Adjust
271
+ $columns['title'] = __( 'Email Subject', 'mycred' );
272
+ $columns['mycred-email-status'] = __( 'Status', 'mycred' );
273
+ $columns['mycred-email-reference'] = __( 'Setup', 'mycred' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
 
275
+ if ( count( $this->point_types ) > 1 )
276
+ $columns['mycred-email-ctype'] = __( 'Point Type', 'mycred' );
277
 
278
+ // Return
279
+ return $columns;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
 
281
  }
282
 
283
  /**
284
+ * Adjust Column Content
285
  * @since 1.1
286
+ * @version 1.0
287
  */
288
+ public function adjust_column_content( $column_name, $post_id ) {
289
 
290
+ // Get the post
291
+ if ( in_array( $column_name, array( 'mycred-email-status', 'mycred-email-reference', 'mycred-email-ctype' ) ) )
292
+ $post = get_post( $post_id );
293
 
294
+ // Email Status Column
295
+ if ( $column_name == 'mycred-email-status' ) {
296
 
297
+ if ( $post->post_status != 'publish' && $post->post_status != 'future' )
298
+ echo '<p>' . __( 'Not Active', 'mycred' ) . '</p>';
299
 
300
+ elseif ( $post->post_status == 'future' )
301
+ echo '<p>' . sprintf( __( 'Scheduled:<br /><strong>%1$s</strong>', 'mycred' ), date( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), strtotime( $post->post_date ) ) ) . '</p>';
 
302
 
303
+ else {
304
+ $date = get_post_meta( $post_id, 'mycred_email_last_run', true );
305
+ if ( empty( $date ) )
306
+ echo '<p>' . __( 'Active', 'mycred' ) . '</p>';
307
+ else
308
+ echo '<p>' . sprintf( __( 'Active - Last run:<br /><strong>%1$s</strong>', 'mycred' ), date( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), $date ) ) . '</p>';
309
+ }
310
 
311
+ }
312
 
313
+ // Email Setup Column
314
+ elseif ( $column_name == 'mycred-email-reference' ) {
315
 
316
+ echo '<p>';
317
+ $instance_key = get_post_meta( $post->ID, 'mycred_email_instance', true );
318
+ $label = $this->get_instance( $instance_key );
319
 
320
+ if ( ! empty( $instance_key ) && ! empty( $label ) )
321
+ echo '<em>' . __( 'Email is sent when', 'mycred' ) .' ' . $label . '.</em></br />';
322
+ else
323
+ echo '<em>' . __( 'Missing instance for this notice!', 'mycred' ) . '</em><br />';
324
 
325
+ $settings = get_post_meta( $post->ID, 'mycred_email_settings', true );
326
+ if ( ! empty( $settings ) && isset( $settings['recipient'] ) )
327
+ $recipient = $settings['recipient'];
328
+ else
329
+ $recipient = 'user';
330
 
331
+ if ( $recipient == 'user' )
332
+ echo '<strong>' . __( 'Sent To', 'mycred' ) . '</strong>: ' . __( 'User', 'mycred' ) . '</p>';
333
+ elseif ( $recipient == 'admin' )
334
+ echo '<strong>' . __( 'Sent To', 'mycred' ) . '</strong>: ' . __( 'Administrator', 'mycred' ) . '</p>';
335
+ else
336
+ echo '<strong>' . __( 'Sent To', 'mycred' ) . '</strong>: ' . __( 'Both Administrator and User', 'mycred' ) . '</p>';
337
 
338
+ }
339
 
340
+ // Email Setup Column
341
+ elseif ( $column_name == 'mycred-email-ctype' ) {
342
 
343
+ $type = get_post_meta( $post_id, 'mycred_email_ctype', true );
344
+ if ( $type == '' ) $type = 'all';
 
 
 
345
 
346
+ if ( $type == 'all' )
347
+ echo __( 'All types', 'mycred' );
 
 
 
 
 
348
 
349
+ elseif ( array_key_exists( $type, $this->point_types ) )
350
+ echo $this->point_types[ $type ];
 
 
 
351
 
352
+ else
353
+ echo '-';
 
354
 
355
+ }
 
356
 
357
  }
358
 
359
  /**
360
+ * Adjust Row Actions
361
  * @since 1.1
362
+ * @version 1.0
363
  */
364
+ public function adjust_row_actions( $actions, $post ) {
365
 
366
+ if ( $post->post_type == 'mycred_email_notice' ) {
367
+ unset( $actions['inline hide-if-no-js'] );
368
+ unset( $actions['view'] );
369
+ }
370
 
371
+ return $actions;
372
 
373
+ }
 
374
 
375
+ /**
376
+ * Add Meta Boxes
377
+ * @since 1.1
378
+ * @version 1.0
379
+ */
380
+ public function add_metaboxes() {
381
 
382
+ add_meta_box(
383
+ 'mycred-email-setup',
384
+ __( 'Email Settings', 'mycred' ),
385
+ array( $this, 'metabox_email_setup' ),
386
+ 'mycred_email_notice',
387
+ 'side',
388
+ 'high'
389
+ );
 
 
 
 
 
 
 
 
390
 
391
+ add_meta_box(
392
+ 'mycred-email-tags',
393
+ __( 'Available Template Tags', 'mycred' ),
394
+ array( $this, 'metabox_template_tags' ),
395
+ 'mycred_email_notice',
396
+ 'normal',
397
+ 'core'
398
+ );
399
 
400
+ if ( $this->emailnotices['use_html'] === false ) return;
 
401
 
402
+ add_meta_box(
403
+ 'mycred-email-header',
404
+ __( 'Email Header', 'mycred' ),
405
+ array( $this, 'metabox_email_header' ),
406
+ 'mycred_email_notice',
407
+ 'normal',
408
+ 'high'
409
+ );
410
 
411
+ }
412
 
413
+ /**
414
+ * Enqueue Scripts & Styles
415
+ * @since 1.1
416
+ * @version 1.1
417
+ */
418
+ public function enqueue_scripts() {
419
 
420
+ $screen = get_current_screen();
421
+ // Commonly used
422
+ if ( $screen->id == 'edit-mycred_email_notice' || $screen->id == 'mycred_email_notice' )
423
+ wp_enqueue_style( 'mycred-admin' );
424
+
425
+ // Edit Email Notice Styling
426
+ if ( $screen->id == 'mycred_email_notice' ) {
427
+
428
+ //wp_enqueue_style( 'mycred-email-edit-notice' );
429
+ wp_enqueue_style( 'mycred-bootstrap-grid' );
430
+ wp_enqueue_style( 'mycred-forms' );
431
+
432
+ add_filter( 'postbox_classes_mycred_email_notice_mycred-email-setup', array( $this, 'metabox_classes' ) );
433
+ add_filter( 'postbox_classes_mycred_email_notice_mycred-email-tags', array( $this, 'metabox_classes' ) );
434
+ add_filter( 'postbox_classes_mycred_email_notice_mycred-email-header', array( $this, 'metabox_classes' ) );
435
 
436
  }
437
 
438
+ // Email Notice List Styling
439
+ elseif ( $screen->id == 'edit-mycred_email_notice' )
440
+ wp_enqueue_style( 'mycred-email-notices' );
441
 
442
  }
443
 
444
  /**
445
+ * Disable WYSIWYG Editor
446
  * @since 1.1
447
+ * @version 1.0
448
  */
449
+ public function disable_richedit( $default ) {
 
 
450
 
451
+ global $post;
452
 
453
+ if ( $post->post_type == 'mycred_email_notice' )
454
+ return false;
455
 
456
+ return $default;
457
 
458
+ }
 
459
 
460
+ /**
461
+ * Apply Default Content
462
+ * @since 1.1
463
+ * @version 1.0
464
+ */
465
+ public function default_content( $content ) {
466
 
467
+ global $post_type;
 
468
 
469
+ if ( $post_type == 'mycred_email_notice' && !empty( $this->emailnotices['content'] ) )
470
+ $content = $this->emailnotices['content'];
471
 
472
+ return $content;
 
 
 
473
 
474
+ }
 
475
 
476
+ /**
477
+ * Add Publish Notice
478
+ * @since 1.1
479
+ * @version 1.0
480
+ */
481
+ public function publish_warning() {
482
 
483
+ global $post;
 
484
 
485
+ if ( $post->post_type != 'mycred_email_notice' ) return;
 
 
 
 
 
 
 
 
 
486
 
487
+ echo '<style type="text/css">#mycred-email-notice { margin-top: 0; } #visibility { display: none; }</style>';
 
 
488
 
489
+ if ( $post->post_status != 'publish' && $post->post_status != 'future' )
490
+ echo '<p id="mycred-email-notice"><span class="description">' . __( 'Once a notice is "published" it becomes active! Select "Save Draft" if you are not yet ready to use this email notice!', 'mycred' ) . '</span></p>';
491
+ elseif ( $post->post_status == 'future' )
492
+ echo '<p id="mycred-email-notice"><span class="description">' . sprintf( __( 'This notice will become active on:<br /><strong>%1$s</strong>', 'mycred' ), date( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), strtotime( $post->post_date ) ) ) . '</span></p>';
493
+ else
494
+ echo '<p id="mycred-email-notice"><span class="description">' . __( 'This email notice is active.', 'mycred' ) . '</span></p>';
495
 
496
+ }
 
 
497
 
498
+ /**
499
+ * Email Settings Metabox
500
+ * @since 1.1
501
+ * @version 1.1
502
+ */
503
+ public function metabox_email_setup( $post ) {
504
 
505
+ // Get instance
506
+ $instance = get_post_meta( $post->ID, 'mycred_email_instance', true );
507
 
508
+ // Get settings
509
+ $settings = $this->get_email_settings( $post->ID );
510
 
511
+ $set_type = get_post_meta( $post->ID, 'mycred_email_ctype', true );
512
+ if ( $set_type == '' )
513
+ $set_type = MYCRED_DEFAULT_TYPE_KEY;
 
 
 
514
 
515
+ ?>
516
+ <div class="form">
517
+ <div class="row">
518
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
519
+ <div class="form-group">
520
+ <label for="mycred-email-instance"<?php if ( $post->post_status == 'publish' && empty( $instance ) ) echo ' style="color:red;font-weight:bold;"'; ?>><?php _e( 'Send this email notice when...', 'mycred' ); ?></label>
521
+ <select name="mycred_email[instance]" id="mycred-email-instance" class="form-control">
522
+ <?php
523
 
524
+ // Default
525
+ echo '<option value=""';
526
+ if ( empty( $instance ) ) echo ' selected="selected"';
527
+ echo '>' . __( 'Select', 'mycred' ) . '</option>';
528
 
529
+ // Loop though instances
530
+ foreach ( $this->instances as $hook_ref => $values ) {
 
531
 
532
+ if ( is_array( $values ) ) {
533
+ foreach ( $values as $key => $value ) {
534
 
535
+ // Make sure that the submitted value is unique
536
+ $key_value = $hook_ref . '|' . $key;
 
537
 
538
+ // Option group starts with 'label'
539
+ if ( $key == 'label' )
540
+ echo '<optgroup label="' . $value . '">';
541
+
542
+ // Option group ends with 'end'
543
+ elseif ( $key == 'end' )
544
+ echo '</optgroup>';
545
+
546
+ // The selectable options
547
+ else {
548
+ echo '<option value="' . $key_value . '"';
549
+ if ( $instance == $key_value ) echo ' selected="selected"';
550
+ echo '>... ' . $this->core->template_tags_general( $value ) . '</option>';
551
  }
552
 
553
  }
556
 
557
  }
558
 
559
+ ?>
560
+ </select>
561
+ </div>
562
+ <div class="form-group">
563
+ <label for="mycred-email-recipient-user"><?php _e( 'Recipient:', 'mycred' ); ?></label>
564
+ <div class="inline-radio">
565
+ <label for="mycred-email-recipient-user"><input type="radio" name="mycred_email[recipient]" id="mycred-email-recipient-user" value="user" <?php checked( $settings['recipient'], 'user' ); ?> /> <?php _e( 'User', 'mycred' ); ?></label>
566
+ </div>
567
+ <div class="inline-radio">
568
+ <label for="mycred-email-recipient-admin"><input type="radio" name="mycred_email[recipient]" id="mycred-email-recipient-admin" value="admin" <?php checked( $settings['recipient'], 'admin' ); ?> /> <?php _e( 'Administrator', 'mycred' ); ?></label>
569
+ </div>
570
+ <div class="inline-radio">
571
+ <label for="mycred-email-recipient-both"><input type="radio" name="mycred_email[recipient]" id="mycred-email-recipient-both" value="both" <?php checked( $settings['recipient'], 'both' ); ?> /> <?php _e( 'Both', 'mycred' ); ?></label>
572
+ </div>
573
+ </div>
574
+ <div class="form-group">
575
+ <label for="mycred-email-label"><?php _e( 'Label', 'mycred' ); ?></label>
576
+ <input type="text" name="mycred_email[label]" id="mycred-email-label" class="form-control" value="<?php echo esc_attr( $settings['label'] ); ?>" />
577
+ </div>
578
+
579
+ <?php if ( count( $this->point_types ) > 1 ) : ?>
580
+
581
+ <div class="form-group">
582
+ <label for="mycred-email-ctype"><?php _e( 'Point Type', 'mycred' ); ?></label>
583
+ <select name="mycred_email[ctype]" id="mycred-email-ctype" class="form-control">
584
+ <?php
585
 
586
+ echo '<option value="all"';
587
+ if ( $set_type == 'all' ) echo ' selected="selected"';
588
+ echo '>' . __( 'All types', 'mycred' ) . '</option>';
 
 
589
 
590
+ foreach ( $this->point_types as $type_id => $label ) {
591
+ echo '<option value="' . $type_id . '"';
592
+ if ( $set_type == $type_id ) echo ' selected="selected"';
593
+ echo '>' . $label . '</option>';
594
  }
595
 
596
+ ?>
597
+ </select>
598
+ </div>
599
+
600
+ <?php else : ?>
601
+
602
+ <input type="hidden" name="mycred_email[ctype]" id="mycred-email-ctype" value="<?php echo MYCRED_DEFAULT_TYPE_KEY; ?>" />
603
+
604
+ <?php endif; ?>
605
+
606
+ <div class="form-group">
607
+ <label for="mycred-email-senders-name"><?php _e( 'Senders Name:', 'mycred' ); ?></label>
608
+ <input type="text" name="mycred_email[senders_name]" id="mycred-email-senders-name" class="form-control" value="<?php echo esc_attr( $settings['senders_name'] ); ?>" />
609
+ </div>
610
+ <div class="form-group">
611
+ <label for="mycred-email-senders-email"><?php _e( 'Senders Email:', 'mycred' ); ?></label>
612
+ <input type="text" name="mycred_email[senders_email]" id="mycred-email-senders-email" class="form-control" value="<?php echo esc_attr( $settings['senders_email'] ); ?>" />
613
+ </div>
614
+ <div class="form-group">
615
+ <label for="mycred-email-reply-to"><?php _e( 'Reply-To Email:', 'mycred' ); ?></label>
616
+ <input type="text" name="mycred_email[reply_to]" id="mycred-email-reply-to" class="form-control" value="<?php echo esc_attr( $settings['reply_to'] ); ?>" />
617
+ </div>
618
+ </div>
619
+ </div>
620
 
621
+ <?php do_action( 'mycred_email_settings_box', $this ); ?>
 
622
 
623
+ </div>
624
+ <?php
625
 
626
  }
627
 
628
  /**
629
+ * Email Header Metabox
630
  * @since 1.1
631
  * @version 1.0
632
  */
633
+ public function metabox_email_header( $post ) {
634
 
635
+ ?>
636
+ <div class="form">
637
+ <div class="row">
638
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
639
+ <div class="form-group">
640
+ <label for="mycred-email-styling"><?php _e( 'CSS Styling', 'mycred' ); ?></label>
641
+ <textarea name="mycred_email[styling]" class="form-control code" rows="10" cols="30" id="mycred-email-styling"><?php echo $this->get_email_styling( $post->ID ); ?></textarea>
642
+ </div>
643
+ </div>
644
+ </div>
645
+ </div>
646
 
647
+ <?php do_action( 'mycred_email_header_box', $this ); ?>
648
+
649
+ <?php
650
 
651
  }
652
 
653
  /**
654
+ * Template Tags Metabox
655
  * @since 1.1
656
+ * @version 1.2
657
  */
658
+ public function metabox_template_tags( $post ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
659
 
660
+ ?>
661
+ <div class="row">
662
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
663
+ <h3><?php _e( 'Site Related', 'mycred' ); ?></h3>
664
+ <div class="row">
665
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
666
+ <strong>%blog_name%</strong>
667
+ </div>
668
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
669
+ <div><?php _e( 'Your websites title', 'mycred' ); ?></div>
670
+ </div>
671
+ </div>
672
+ <div class="row">
673
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
674
+ <strong>%blog_url%</strong>
675
+ </div>
676
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
677
+ <div><?php _e( 'Your websites address', 'mycred' ); ?></div>
678
+ </div>
679
+ </div>
680
+ <div class="row">
681
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
682
+ <strong>%blog_info%</strong>
683
+ </div>
684
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
685
+ <div><?php _e( 'Your websites tagline (description)', 'mycred' ); ?></div>
686
+ </div>
687
+ </div>
688
+ <div class="row">
689
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
690
+ <strong>%admin_email%</strong>
691
+ </div>
692
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
693
+ <div><?php _e( 'Your websites admin email', 'mycred' ); ?></div>
694
+ </div>
695
+ </div>
696
+ <div class="row">
697
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
698
+ <strong>%num_members%</strong>
699
+ </div>
700
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
701
+ <div><?php _e( 'Total number of blog members', 'mycred' ); ?></div>
702
+ </div>
703
+ </div>
704
+ </div>
705
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
706
+ <h3><?php _e( 'Instance Related', 'mycred' ); ?></h3>
707
+ <div class="row">
708
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
709
+ <strong>%new_balance%</strong>
710
+ </div>
711
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
712
+ <div><?php _e( 'The users new balance', 'mycred' ); ?></div>
713
+ </div>
714
+ </div>
715
+ <div class="row">
716
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
717
+ <strong>%old_balance%</strong>
718
+ </div>
719
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
720
+ <div><?php _e( 'The users old balance', 'mycred' ); ?></div>
721
+ </div>
722
+ </div>
723
+ <div class="row">
724
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
725
+ <strong>%amount%</strong>
726
+ </div>
727
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
728
+ <div><?php _e( 'The amount of points gained or lost in this instance', 'mycred' ); ?></div>
729
+ </div>
730
+ </div>
731
+ <div class="row">
732
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
733
+ <strong>%entry%</strong>
734
+ </div>
735
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
736
+ <div><?php _e( 'The log entry', 'mycred' ); ?></div>
737
+ </div>
738
+ </div>
739
+ <div class="row">
740
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
741
+ <div><?php printf( __( 'You can also use %s.', 'mycred' ), '<a href="http://codex.mycred.me/category/template-tags/temp-user/" target="_blank">' . __( 'user related template tags', 'mycred' ) . '</a>' ); ?></div>
742
+ </div>
743
+ </div>
744
+ </div>
745
+ </div>
746
+ <?php
747
 
748
+ }
 
749
 
750
+ /**
751
+ * Save Email Notice Details
752
+ * @since 1.1
753
+ * @version 1.2
754
+ */
755
+ public function save_email_notice( $post_id, $post = NULL ) {
756
 
757
+ if ( $post === NULL || ! current_user_can( $this->core->edit_creds_cap() ) || ! isset( $_POST['mycred_email'] ) ) return $post_id;
 
 
758
 
759
+ // Update Instance
760
+ if ( ! empty( $_POST['mycred_email']['instance'] ) ) {
761
 
762
+ $instance_key = sanitize_text_field( $_POST['mycred_email']['instance'] );
763
+ $keys = explode( '|', $instance_key );
764
+ if ( ! empty( $keys ) );
765
+ update_post_meta( $post_id, 'mycred_email_instance', $instance_key );
766
 
767
+ }
 
768
 
769
+ // Construct new settings
770
+ $settings = array();
 
 
771
 
772
+ if ( ! empty( $_POST['mycred_email']['recipient'] ) )
773
+ $settings['recipient'] = sanitize_text_field( $_POST['mycred_email']['recipient'] );
774
+ else
775
+ $settings['recipient'] = 'user';
776
 
777
+ if ( ! empty( $_POST['mycred_email']['senders_name'] ) )
778
+ $settings['senders_name'] = sanitize_text_field( $_POST['mycred_email']['senders_name'] );
779
+ else
780
+ $settings['senders_name'] = $this->emailnotices['from']['name'];
781
 
782
+ if ( ! empty( $_POST['mycred_email']['senders_email'] ) )
783
+ $settings['senders_email'] = sanitize_text_field( $_POST['mycred_email']['senders_email'] );
784
+ else
785
+ $settings['senders_email'] = $this->emailnotices['from']['email'];
786
 
787
+ if ( ! empty( $_POST['mycred_email']['reply_to'] ) )
788
+ $settings['reply_to'] = sanitize_text_field( $_POST['mycred_email']['reply_to'] );
789
+ else
790
+ $settings['reply_to'] = $this->emailnotices['from']['reply_to'];
791
 
792
+ $settings['label'] = sanitize_text_field( $_POST['mycred_email']['label'] );
 
 
 
 
 
793
 
794
+ // Save settings
795
+ update_post_meta( $post_id, 'mycred_email_settings', $settings );
 
796
 
797
+ $point_type = sanitize_key( $_POST['mycred_email']['ctype'] );
798
+ if ( mycred_point_type_exists( $point_type ) )
799
+ update_post_meta( $post_id, 'mycred_email_ctype', $point_type );
 
 
 
 
 
800
 
801
+ // If rich editing is disabled bail now
802
+ if ( $this->emailnotices['use_html'] === false ) return;
803
 
804
+ // Save styling
805
+ if ( ! empty( $_POST['mycred_email']['styling'] ) )
806
+ update_post_meta( $post_id, 'mycred_email_styling', wp_kses_post( $_POST['mycred_email']['styling'] ) );
807
 
808
  }
809
 
810
  /**
811
+ * Admin Header
812
  * @since 1.1
813
  * @version 1.0
814
  */
815
+ public function admin_header() {
 
 
 
 
 
 
 
816
 
817
+ $screen = get_current_screen();
818
+ if ( $screen->id == 'mycred_email_notice' && $this->emailnotices['use_html'] === false ) {
819
+ remove_action( 'media_buttons', 'media_buttons' );
820
+ echo '<style type="text/css">#ed_toolbar { display: none !important; }</style>';
821
+ }
822
 
823
  }
824
 
825
  /**
826
+ * Register Scripts & Styles
827
+ * @since 1.7
828
  * @version 1.0
829
  */
830
+ public function scripts_and_styles() {
831
 
832
+ // Register Email List Styling
833
+ wp_register_style(
834
+ 'mycred-email-notices',
835
+ plugins_url( 'assets/css/email-notice.css', myCRED_EMAIL ),
836
+ false,
837
+ myCRED_EMAIL_VERSION . '.1',
838
+ 'all'
839
+ );
840
 
841
+ // Register Edit Email Notice Styling
842
+ wp_register_style(
843
+ 'mycred-email-edit-notice',
844
+ plugins_url( 'assets/css/edit-email-notice.css', myCRED_EMAIL ),
845
+ false,
846
+ myCRED_EMAIL_VERSION . '.1',
847
+ 'all'
848
+ );
849
 
850
  }
851
 
852
  /**
853
+ * Setup Instances
854
  * @since 1.1
855
  * @version 1.1
856
  */
857
+ public function setup_instances() {
858
 
859
+ $instances[''] = __( 'Select', 'mycred' );
860
+ $instances['general'] = array(
861
+ 'label' => __( 'General', 'mycred' ),
862
+ 'all' => __( 'users balance changes', 'mycred' ),
863
+ 'positive' => __( 'user gains %_plural%', 'mycred' ),
864
+ 'negative' => __( 'user lose %_plural%', 'mycred' ),
865
+ 'zero' => __( 'users balance reaches zero', 'mycred' ),
866
+ 'minus' => __( 'users balance goes minus', 'mycred' ),
867
+ 'end' => ''
868
+ );
869
 
870
+ if ( class_exists( 'myCRED_Badge_Module' ) ) {
871
+ $instances['badges'] = array(
872
+ 'label' => __( 'Badge Add-on', 'mycred' ),
873
+ 'positive' => __( 'user gains a badge', 'mycred' ),
874
+ 'end' => ''
875
+ );
876
+ }
877
 
878
+ if ( class_exists( 'myCRED_Sell_Content_Module' ) ) {
879
+ $instances['buy_content'] = array(
880
+ 'label' => __( 'Sell Content Add-on', 'mycred' ),
881
+ 'negative' => __( 'user buys content', 'mycred' ),
882
+ 'positive' => __( 'authors content gets sold', 'mycred' ),
883
+ 'end' => ''
884
+ );
885
+ }
886
 
887
+ if ( class_exists( 'myCRED_buyCRED_Module' ) ) {
888
+ $instances['buy_creds'] = array(
889
+ 'label' => __( 'buyCREDs Add-on', 'mycred' ),
890
+ 'positive' => __( 'user buys %_plural%', 'mycred' ),
891
+ 'end' => ''
892
+ );
893
+ }
894
+
895
+ if ( class_exists( 'myCRED_Transfer_Module' ) ) {
896
+ $instances['transfer'] = array(
897
+ 'label' => __( 'Transfer Add-on', 'mycred' ),
898
+ 'negative' => __( 'user sends %_plural%', 'mycred' ),
899
+ 'positive' => __( 'user receives %_plural%', 'mycred' ),
900
+ 'end' => ''
901
+ );
902
+ }
903
+
904
+ if ( class_exists( 'myCRED_Ranks_Module' ) ) {
905
+ $instances['ranks'] = array(
906
+ 'label' => __( 'Ranks Add-on', 'mycred' ),
907
+ 'negative' => __( 'user is demoted', 'mycred' ),
908
+ 'positive' => __( 'user is promoted', 'mycred' ),
909
+ 'end' => ''
910
+ );
911
+ }
912
+
913
+ $this->instances = apply_filters( 'mycred_email_instances', $instances );
914
 
915
  }
916
 
917
  /**
918
+ * Get Instance
919
  * @since 1.1
920
  * @version 1.0
921
  */
922
+ public function get_instance( $key = '', $detail = NULL ) {
 
 
 
 
923
 
924
+ $instance_keys = explode( '|', $key );
925
+ if ( $instance_keys === false || empty( $instance_keys ) || count( $instance_keys ) != 2 ) return NULL;
926
 
927
+ // By default we return the entire array for the given key
928
+ if ( $detail === NULL && array_key_exists( $instance_keys[0], $this->instances ) )
929
+ return $this->core->template_tags_general( $this->instances[ $instance_keys[0] ][ $instance_keys[1] ] );
930
 
931
+ if ( $detail !== NULL && array_key_exists( $detail, $this->instances[ $instance_keys[0] ] ) )
932
+ return $this->core->template_tags_general( $this->instances[ $instance_keys[0] ][ $detail ] );
933
 
934
+ return NULL;
 
 
 
 
 
 
935
 
936
+ }
937
 
938
+ /**
939
+ * Email Notice Check
940
+ * @since 1.4.6
941
+ * @version 1.2
942
+ */
943
+ public function get_events_from_instance( $request, $mycred ) {
944
 
945
+ extract( $request );
 
 
946
 
947
+ $events = array( 'general|all' );
 
 
 
948
 
949
+ // Events based on amount being given or taken
950
+ if ( $amount < $mycred->zero() )
951
+ $events[] = 'general|negative';
952
+ else
953
+ $events[] = 'general|positive';
954
 
955
+ // Events based on this transaction leading to the users balance
956
+ // reaching or surpassing zero
957
+ $users_current_balance = $mycred->get_users_balance( $user_id, $type );
958
+ if ( ( $users_current_balance - $amount ) < $mycred->zero() )
959
+ $events[] = 'general|minus';
960
+ elseif ( ( $users_current_balance - $amount ) == $mycred->zero() )
961
+ $events[] = 'general|zero';
962
 
963
+ // Ranks Related
964
+ if ( function_exists( 'mycred_get_users_rank' ) ) {
965
 
966
+ $rank = mycred_find_users_rank( $user_id, $type, false );
 
967
 
968
+ if ( isset( $rank->rank_id ) && $rank->rank_id !== $rank->current_id ) {
 
969
 
970
+ if ( $rank->current_id !== NULL && get_post_meta( $results->current_id, 'mycred_rank_max', true ) > $results->maximum )
971
+ $events[] = 'ranks|negative';
972
 
973
+ else
974
+ $events[] = 'ranks|positive';
975
 
976
+ }
 
977
 
978
  }
979
 
980
+ // Let others play
981
+ return apply_filters( 'mycred_get_email_events', $events, $request, $mycred );
982
+
983
  }
984
 
985
  /**
986
+ * Email Notice Check
987
  * @since 1.1
988
+ * @version 1.5
989
  */
990
+ public function email_check( $ran, $request, $mycred ) {
991
 
992
+ // Exit now if $ran is false or new settings is not yet saved.
993
+ if ( $ran === false || ! isset( $this->emailnotices['send'] ) ) return $ran;
 
 
 
 
 
 
994
 
995
+ $user_id = absint( $request['user_id'] );
 
 
 
 
 
 
 
996
 
997
+ // Construct events
998
+ $events = $this->get_events_from_instance( $request, $mycred );
999
 
1000
+ // Do not send emails now
1001
+ if ( $this->emailnotices['send'] != '' ) {
 
 
 
 
 
 
1002
 
1003
+ // Save for cron job
1004
+ mycred_add_user_meta( $user_id, 'mycred_scheduled_email_notices', '', array(
1005
+ 'events' => $events,
1006
+ 'request' => $request
1007
+ ) );
1008
 
1009
+ }
 
 
 
 
 
1010
 
1011
+ // Send emails now
1012
+ else {
1013
 
1014
+ $this->do_email_notices( $events, $request );
 
1015
 
1016
+ }
1017
+
1018
+ return $ran;
1019
 
1020
  }
1021
 
1022
  /**
1023
+ * Badge Check
1024
+ * @since 1.7
1025
  * @version 1.0
1026
  */
1027
+ public function badge_check( $user_id, $badge_id, $level_reached ) {
1028
+
1029
+ if ( $level_reached === false ) return;
1030
+
1031
+ $events = array( 'badges|positive' );
1032
+ $request = array(
1033
+ 'user_id' => $user_id,
1034
+ 'creds' => 0,
1035
+ 'entry' => 'New Badge',
1036
+ 'ctype' => MYCRED_DEFAULT_TYPE_KEY,
1037
+ 'time' => current_time( 'timestamp' ),
1038
+ 'ref_id' => $badge_id,
1039
+ 'data' => array( 'ref_type' => 'post' )
1040
+ );
1041
 
1042
+ // Do not send emails now
1043
+ if ( $this->emailnotices['send'] != '' ) {
1044
 
1045
+ // Save for cron job
1046
+ mycred_add_user_meta( $user_id, 'mycred_scheduled_email_notices', '', array(
1047
+ 'events' => $events,
1048
+ 'request' => $request
1049
+ ) );
1050
 
1051
+ }
1052
+
1053
+ // Send emails now
1054
+ else {
1055
+
1056
+ $this->do_email_notices( $events, $request );
1057
+
1058
+ }
1059
 
1060
  }
1061
 
1062
  /**
1063
+ * Do Email Notices
1064
  * @since 1.1
1065
+ * @version 1.2
1066
  */
1067
+ public function do_email_notices( $events = array(), $request = array() ) {
1068
 
1069
+ if ( ! isset( $request['user_id'] ) || empty( $events ) ) return;
1070
 
1071
+ extract( $request );
 
1072
 
1073
+ // Get all notices that a user has unsubscribed to
1074
+ $unsubscriptions = (array) mycred_get_user_meta( $user_id, 'mycred_email_unsubscriptions', '', true );
1075
+
1076
+ global $wpdb;
1077
+
1078
+ // Loop though events
1079
+ foreach ( $events as $event ) {
1080
+
1081
+ // Get the email notice post object
1082
+ $notice = $wpdb->get_row( $wpdb->prepare( "
1083
+ SELECT *
1084
+ FROM {$wpdb->posts} notices
1085
+
1086
+ LEFT JOIN {$wpdb->postmeta} instances
1087
+ ON ( notices.ID = instances.post_id AND instances.meta_key = 'mycred_email_instance' )
1088
+
1089
+ LEFT JOIN {$wpdb->postmeta} pointtype
1090
+ ON ( notices.ID = pointtype.post_id AND pointtype.meta_key = 'mycred_email_ctype' )
1091
+
1092
+ WHERE instances.meta_value = %s
1093
+ AND pointtype.meta_value IN (%s,'all')
1094
+ AND notices.post_type = 'mycred_email_notice'
1095
+ AND notices.post_status = 'publish';", $event, $request['type'] ) );
1096
+
1097
+ // Notice found
1098
+ if ( $notice !== NULL ) {
1099
+
1100
+ // Ignore unsubscribed events
1101
+ if ( in_array( $notice->ID, $unsubscriptions ) ) continue;
1102
+
1103
+ // Get notice setup
1104
+ $settings = $this->get_email_settings( $notice->ID );
1105
+
1106
+ // Send to user
1107
+ if ( $settings['recipient'] == 'user' || $settings['recipient'] == 'both' ) {
1108
+ $user = get_user_by( 'id', $user_id );
1109
+ $to = $user->user_email;
1110
+ unset( $user );
1111
+ }
1112
+
1113
+ elseif ( $settings['recipient'] == 'admin' ) {
1114
+ $to = get_option( 'admin_email' );
1115
+ }
1116
+
1117
+ // Filtered Subject
1118
+ if ( $this->emailnotices['filter']['subject'] === true )
1119
+ $subject = get_the_title( $notice->ID );
1120
+
1121
+ // Unfiltered Subject
1122
+ else $subject = $notice->post_title;
1123
+
1124
+ // Filtered Content
1125
+ if ( $this->emailnotices['filter']['content'] === true )
1126
+ $message = apply_filters( 'the_content', $notice->post_content );
1127
+
1128
+ // Unfiltered Content
1129
+ else $message = $notice->post_content;
1130
+
1131
+ $headers = array();
1132
+ $attachments = '';
1133
+
1134
+ if ( ! $this->emailnotices['override'] ) {
1135
+
1136
+ // Construct headers
1137
+ if ( $this->emailnotices['use_html'] === true ) {
1138
+ $headers[] = 'MIME-Version: 1.0';
1139
+ $headers[] = 'Content-Type: text/HTML; charset="' . get_option( 'blog_charset' ) . '"';
1140
+ }
1141
+ $headers[] = 'From: ' . $settings['senders_name'] . ' <' . $settings['senders_email'] . '>';
1142
+
1143
+ // Reply-To
1144
+ if ( $settings['reply_to'] != '' )
1145
+ $headers[] = 'Reply-To: ' . $settings['reply_to'];
1146
+
1147
+ // Both means we blank carbon copy the admin so the user does not see email
1148
+ if ( $settings['recipient'] == 'both' )
1149
+ $headers[] = 'Bcc: ' . get_option( 'admin_email' );
1150
+
1151
+ // If email was successfully sent we update 'last_run'
1152
+ if ( $this->wp_mail( $to, $subject, $message, $headers, $attachments, $request, $notice->ID ) === true )
1153
+ update_post_meta( $notice->ID, 'mycred_email_last_run', time() );
1154
+
1155
+ }
1156
+ else {
1157
+
1158
+ // If email was successfully sent we update 'last_run'
1159
+ if ( $this->wp_mail( $to, $subject, $message, $headers, $attachments, $request, $notice->ID ) === true ) {
1160
+ update_post_meta( $notice->ID, 'mycred_email_last_run', time() );
1161
+
1162
+ if ( $settings['recipient'] == 'both' )
1163
+ $this->wp_mail( get_option( 'admin_email' ), $subject, $message, $headers, $attachments, $request, $notice->ID );
1164
+ }
1165
+
1166
+ }
1167
+
1168
+ }
1169
+
1170
+ }
1171
 
1172
  }
1173
 
1174
  /**
1175
+ * WP Mail
1176
  * @since 1.1
1177
+ * @version 1.3.1
1178
  */
1179
+ public function wp_mail( $to, $subject, $message, $headers, $attachments, $request, $email_id ) {
 
 
 
1180
 
1181
+ // Let others play before we do our thing
1182
+ $filtered = apply_filters( 'mycred_email_before_send', compact( 'to', 'subject', 'message', 'headers', 'attachments', 'request', 'email_id' ) );
1183
 
1184
+ if ( ! isset( $filtered['request'] ) || ! is_array( $filtered['request'] ) ) return false;
 
 
1185
 
1186
+ $subject = $this->template_tags_request( $filtered['subject'], $filtered['request'] );
1187
+ $message = $this->template_tags_request( $filtered['message'], $filtered['request'] );
 
 
 
 
1188
 
1189
+ $entry = new stdClass();
1190
+ foreach ( $filtered['request'] as $key => $value )
1191
+ $entry->$key = $value;
 
1192
 
1193
+ $mycred = mycred( $filtered['request']['type'] );
 
1194
 
1195
+ $subject = $mycred->template_tags_user( $subject, $filtered['request']['user_id'] );
1196
+ $message = $mycred->template_tags_user( $message, $filtered['request']['user_id'] );
1197
 
1198
+ $subject = $mycred->template_tags_amount( $subject, $filtered['request']['amount'] );
1199
+ $message = $mycred->template_tags_amount( $message, $filtered['request']['amount'] );
1200
+
1201
+ $subject = $mycred->parse_template_tags( $subject, $entry );
1202
+ $message = $mycred->parse_template_tags( $message, $entry );
1203
 
1204
+ // Construct HTML Content
1205
+ if ( $this->emailnotices['use_html'] === true ) {
1206
+ $styling = $this->get_email_styling( $email_id );
1207
+ $message = '<html><head><title>' . $subject . '</title><style type="text/css" media="all"> ' . trim( $styling ) . '</style></head><body>' . nl2br( $message ) . '</body></html>';
1208
+ }
1209
 
1210
+ // Send Email
1211
+ add_filter( 'wp_mail_content_type', array( $this, 'get_email_format' ) );
1212
+ $result = wp_mail( $filtered['to'], $subject, $message, $filtered['headers'], $filtered['attachments'] );
1213
+ remove_filter( 'wp_mail_content_type', array( $this, 'get_email_format' ) );
1214
 
1215
+ // Let others play
1216
+ do_action( 'mycred_email_sent', $filtered );
 
 
 
 
1217
 
1218
+ return $result;
1219
 
1220
+ }
1221
 
1222
+ /**
1223
+ * Get Email Format
1224
+ * @since 1.1
1225
+ * @version 1.0
1226
+ */
1227
+ public function get_email_format() {
1228
 
1229
+ if ( $this->emailnotices['use_html'] === false )
1230
+ return 'text/plain';
 
 
 
 
 
 
 
 
 
 
 
1231
 
1232
+ return 'text/html';
1233
 
1234
+ }
 
 
 
1235
 
1236
+ /**
1237
+ * Request Related Template Tags
1238
+ * @since 1.1
1239
+ * @version 1.3
1240
+ */
1241
+ public function template_tags_request( $content, $request ) {
1242
 
1243
+ $type = $this->core;
1244
+ if ( $request['type'] != MYCRED_DEFAULT_TYPE_KEY )
1245
+ $type = mycred( $request['type'] );
 
 
1246
 
1247
+ $user_id = absint( $request['user_id'] );
1248
+ $old_balance = $type->get_users_balance( $user_id, $request['type'] );
 
1249
 
1250
+ $content = str_replace( '%old_balance%', $old_balance, $content );
1251
+ $content = str_replace( '%old_balance_f%', $type->format_creds( $old_balance ), $content );
1252
 
1253
+ if ( $request['amount'] > 0 )
1254
+ $new_balance = $type->number( $old_balance - $request['amount'] );
1255
+ else
1256
+ $new_balance = $type->number( $old_balance + $request['amount'] );
1257
 
1258
+ $content = str_replace( '%new_balance%', $new_balance, $content );
1259
+ $content = str_replace( '%new_balance_f%', $type->format_creds( $new_balance ), $content );
1260
 
1261
+ $content = str_replace( '%amount%', $request['amount'], $content );
1262
+ $content = str_replace( '%entry%', $request['entry'], $content );
1263
+ $content = str_replace( '%data%', maybe_serialize( $request['data'] ), $content );
 
 
 
 
 
1264
 
1265
+ $content = str_replace( '%blog_name%', get_option( 'blogname' ), $content );
1266
+ $content = str_replace( '%blog_url%', get_option( 'home' ), $content );
1267
+ $content = str_replace( '%blog_info%', get_option( 'blogdescription' ), $content );
1268
+ $content = str_replace( '%admin_email%', get_option( 'admin_email' ), $content );
1269
+ $content = str_replace( '%num_members%', $this->core->count_members(), $content );
1270
 
1271
+ return $content;
 
 
 
1272
 
1273
  }
1274
 
1275
  /**
1276
+ * Get Email Settings
1277
  * @since 1.1
1278
+ * @version 1.1
1279
  */
1280
+ public function get_email_settings( $post_id ) {
1281
 
1282
+ $settings = get_post_meta( $post_id, 'mycred_email_settings', true );
1283
+ if ( $settings == '' )
1284
+ $settings = array();
1285
 
1286
+ // Defaults
1287
+ $default = array(
1288
+ 'recipient' => 'user',
1289
+ 'senders_name' => $this->emailnotices['from']['name'],
1290
+ 'senders_email' => $this->emailnotices['from']['email'],
1291
+ 'reply_to' => $this->emailnotices['from']['reply_to'],
1292
+ 'label' => ''
1293
+ );
1294
 
1295
+ $settings = mycred_apply_defaults( $default, $settings );
1296
+
1297
+ return apply_filters( 'mycred_email_notice_settings', $settings, $post_id );
1298
 
1299
  }
1300
 
1301
  /**
1302
+ * Get Email Styling
1303
  * @since 1.1
1304
+ * @version 1.0
1305
  */
1306
+ public function get_email_styling( $post_id ) {
1307
+
1308
+ if ( $this->emailnotices['use_html'] === false ) return '';
1309
+ $style = get_post_meta( $post_id, 'mycred_email_styling', true );
1310
+
1311
+ // Defaults
1312
+ if ( empty( $style ) )
1313
+ return $this->emailnotices['styling'];
1314
+
1315
+ return $style;
 
 
 
 
 
 
 
 
 
1316
 
1317
  }
1318
 
1319
  /**
1320
+ * Add to General Settings
1321
  * @since 1.1
1322
+ * @version 1.1
1323
  */
1324
+ public function after_general_settings( $mycred = NULL ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1325
 
1326
+ $this->emailnotices = mycred_apply_defaults( $this->default_prefs, $this->emailnotices );
 
 
 
 
1327
 
1328
+ ?>
1329
+ <h4><span class="dashicons dashicons-admin-plugins static"></span><?php _e( 'Email Notices', 'mycred' ); ?></h4>
1330
+ <div class="body" style="display:none;">
1331
+ <p><?php _e( 'Settings that apply to all email notices and can not be overridden for individual emails.', 'mycred' ); ?></p>
1332
+ <label class="subheader" for="<?php echo $this->field_id( array( 'use_html' => 'no' ) ); ?>"><?php _e( 'Email Format', 'mycred' ); ?></label>
1333
+ <ol id="myCRED-email-notice-use-html">
1334
+ <li>
1335
+ <input type="radio" name="<?php echo $this->field_name( 'use_html' ); ?>" id="<?php echo $this->field_id( array( 'use_html' => 'no' ) ); ?>" <?php checked( $this->emailnotices['use_html'], 0 ); ?> value="0" />
1336
+ <label for="<?php echo $this->field_id( array( 'use_html' => 'no' ) ); ?>"><?php _e( 'Plain text emails only.', 'mycred' ); ?></label>
1337
+ </li>
1338
+ <li>
1339
+ <input type="radio" name="<?php echo $this->field_name( 'use_html' ); ?>" id="<?php echo $this->field_id( array( 'use_html' => 'yes' ) ); ?>" <?php checked( $this->emailnotices['use_html'], 1 ); ?> value="1" />
1340
+ <label for="<?php echo $this->field_id( array( 'use_html' => 'yes' ) ); ?>"><?php _e( 'HTML or Plain text emails.', 'mycred' ); ?></label>
1341
+ </li>
1342
+ </ol>
1343
+ <label class="subheader" for="<?php echo $this->field_id( array( 'filter' => 'subject' ) ); ?>"><?php _e( 'Filters', 'mycred' ); ?></label>
1344
+ <ol id="myCRED-email-notice-allow-filters">
1345
+ <li>
1346
+ <input type="checkbox" name="<?php echo $this->field_name( array( 'filter' => 'subject' ) ); ?>" id="<?php echo $this->field_id( array( 'filter' => 'subject' ) ); ?>" <?php checked( $this->emailnotices['filter']['subject'], 1 ); ?> value="1" />
1347
+ <label for="<?php echo $this->field_id( array( 'filter' => 'subject' ) ); ?>"><?php _e( 'Allow WordPress and Third Party Plugins to filter the email subject before an email is sent.', 'mycred' ); ?></label>
1348
+ </li>
1349
+ <li>
1350
+ <input type="checkbox" name="<?php echo $this->field_name( array( 'filter' => 'content' ) ); ?>" id="<?php echo $this->field_id( array( 'filter' => 'content' ) ); ?>" <?php checked( $this->emailnotices['filter']['content'], 1 ); ?> value="1" />
1351
+ <label for="<?php echo $this->field_id( array( 'filter' => 'content' ) ); ?>"><?php _e( 'Allow WordPress and Third Party Plugins to filter the email content before an email is sent.', 'mycred' ); ?></label>
1352
+ </li>
1353
+ </ol>
1354
 
1355
+ <?php if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) : ?>
 
 
 
 
1356
 
1357
+ <label class="subheader" for="<?php echo $this->field_id( 'send' ); ?>"><?php _e( 'Email Schedule', 'mycred' ); ?></label>
1358
+ <ol id="myCRED-email-notice-schedule">
1359
+ <li><?php _e( 'WordPress Cron is disabled. Emails will be sent immediately.', 'mycred' ); ?><input type="hidden" name="<?php echo $this->field_name( 'send' ); ?>" value="" /></li>
1360
+ </ol>
1361
 
1362
+ <?php else : ?>
 
1363
 
1364
+ <label class="subheader" for="<?php echo $this->field_id( 'send' ); ?>"><?php _e( 'Email Schedule', 'mycred' ); ?></label>
1365
+ <ol id="myCRED-email-notice-schedule">
1366
+ <li>
1367
+ <input type="radio" name="<?php echo $this->field_name( 'send' ); ?>" id="<?php echo $this->field_id( 'send' ); ?>-hourly" <?php checked( $this->emailnotices['send'], '' ); ?> value="" />
1368
+ <label for="<?php echo $this->field_id( 'send' ); ?>-hourly"><?php _e( 'Send emails immediately', 'mycred' ); ?></label>
1369
+ </li>
1370
+ <li>
1371
+ <input type="radio" name="<?php echo $this->field_name( 'send' ); ?>" id="<?php echo $this->field_id( 'send' ); ?>" <?php checked( $this->emailnotices['send'], 'hourly' ); ?> value="hourly" />
1372
+ <label for="<?php echo $this->field_id( 'send' ); ?>"><?php _e( 'Send emails once an hour', 'mycred' ); ?></label>
1373
+ </li>
1374
+ <li>
1375
+ <input type="radio" name="<?php echo $this->field_name( 'send' ); ?>" id="<?php echo $this->field_id( 'send' ); ?>-daily" <?php checked( $this->emailnotices['send'], 'daily' ); ?> value="daily" />
1376
+ <label for="<?php echo $this->field_id( 'send' ); ?>-daily"><?php _e( 'Send emails once a day', 'mycred' ); ?></label>
1377
+ </li>
1378
+ </ol>
1379
+ <label class="subheader" for="<?php echo $this->field_id( 'send' ); ?>"><?php _e( 'Subscriptions', 'mycred' ); ?></label>
1380
+ <ol id="myCRED-email-notice-schedule">
1381
+ <li><?php printf( __( 'Use the %s shortcode to allow users to subscribe / unsubscribe to email updates.', 'mycred' ), '<a href="http://codex.mycred.me/shortcodes/mycred_email_subscriptions/">mycred_email_subscriptions</a>' ); ?></p></li>
1382
+ </ol>
1383
 
1384
+ <?php endif; ?>
 
1385
 
1386
+ <label class="subheader" for="<?php echo $this->field_id( 'override' ); ?>"><?php _e( 'SMTP Override', 'mycred' ); ?></label>
1387
+ <ol id="myCRED-email-notice-override">
1388
+ <li>
1389
+ <input type="checkbox" name="<?php echo $this->field_name( 'override' ); ?>" id="<?php echo $this->field_id( 'override' ); ?>" <?php checked( $this->emailnotices['override'], 1 ); ?> value="1" />
1390
+ <label for="<?php echo $this->field_id( 'override' ); ?>"><?php _e( 'SMTP Debug. Enable if you are experiencing issues with wp_mail() or if you use a SMTP plugin for emails.', 'mycred' ); ?></label>
1391
+ </li>
1392
+ </ol>
1393
+ <p><?php _e( 'Default email settings. These settings can be individually overridden when editing emails.', 'mycred' ); ?></p>
1394
+ <label class="subheader"><?php _e( 'Email Settings', 'mycred' ); ?></label>
1395
+ <ol id="myCRED-email-default-sender">
1396
+ <li>
1397
+ <label for="<?php echo $this->field_id( array( 'from' => 'name' ) ); ?>"><?php _e( 'Senders Name:', 'mycred' ); ?></label>
1398
+ <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'from' => 'name' ) ); ?>" id="<?php echo $this->field_id( array( 'from' => 'name' ) ); ?>" value="<?php echo $this->emailnotices['from']['name']; ?>" class="long" /></div>
1399
+ </li>
1400
+ <li>
1401
+ <label for="<?php echo $this->field_id( array( 'from' => 'email' ) ); ?>"><?php _e( 'Senders Email:', 'mycred' ); ?></label>
1402
+ <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'from' => 'email' ) ); ?>" id="<?php echo $this->field_id( array( 'from' => 'email' ) ); ?>" value="<?php echo $this->emailnotices['from']['email']; ?>" class="long" /></div>
1403
+ </li>
1404
+ <li>
1405
+ <label for="<?php echo $this->field_id( array( 'from' => 'reply_to' ) ); ?>"><?php _e( 'Reply-To:', 'mycred' ); ?></label>
1406
+ <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'from' => 'reply_to' ) ); ?>" id="<?php echo $this->field_id( array( 'from' => 'reply_to' ) ); ?>" value="<?php echo $this->emailnotices['from']['reply_to']; ?>" class="long" /></div>
1407
+ </li>
1408
+ </ol>
1409
+ <label class="subheader" for="<?php echo $this->field_id( 'content' ); ?>"><?php _e( 'Default Email Content', 'mycred' ); ?></label>
1410
+ <ol id="myCRED-email-notice-defaults">
1411
+ <li>
1412
+ <textarea rows="10" cols="50" name="<?php echo $this->field_name( 'content' ); ?>" id="<?php echo $this->field_id( 'content' ); ?>" class="large-text code"><?php echo esc_attr( $this->emailnotices['content'] ); ?></textarea>
1413
+ <span class="description"><?php _e( 'Default email content.', 'mycred' ); ?></span>
1414
+ </li>
1415
+ </ol>
1416
+ <label class="subheader" for="<?php echo $this->field_id( 'styling' ); ?>"><?php _e( 'Default Email Styling', 'mycred' ); ?></label>
1417
+ <ol>
1418
+ <li>
1419
+ <textarea rows="10" cols="50" name="<?php echo $this->field_name( 'styling' ); ?>" id="<?php echo $this->field_id( 'styling' ); ?>" class="large-text code"><?php echo esc_attr( $this->emailnotices['styling'] ); ?></textarea>
1420
+ <span class="description"><?php _e( 'Ignored if HTML is not allowed in emails.', 'mycred' ); ?></span>
1421
+ </li>
1422
+ </ol>
1423
+ </div>
1424
+ <?php
1425
 
1426
  }
1427
 
1428
  /**
1429
+ * Save Settings
1430
  * @since 1.1
1431
+ * @version 1.1
1432
  */
1433
+ public function sanitize_extra_settings( $new_data, $data, $core ) {
1434
 
1435
+ if ( ! isset( $data['emailnotices']['use_html'] ) )
1436
+ $data['emailnotices']['use_html'] = 0;
1437
 
1438
+ $new_data['emailnotices']['use_html'] = ( $data['emailnotices']['use_html'] == 1 ) ? true : false;
 
 
 
 
 
 
 
 
 
 
 
 
1439
 
1440
+ $new_data['emailnotices']['filter']['subject'] = ( isset( $data['emailnotices']['filter']['subject'] ) ) ? true : false;
1441
+ $new_data['emailnotices']['filter']['content'] = ( isset( $data['emailnotices']['filter']['content'] ) ) ? true : false;
1442
 
1443
+ $new_data['emailnotices']['from']['name'] = sanitize_text_field( $data['emailnotices']['from']['name'] );
1444
+ $new_data['emailnotices']['from']['email'] = sanitize_text_field( $data['emailnotices']['from']['email'] );
1445
+ $new_data['emailnotices']['from']['reply_to'] = sanitize_text_field( $data['emailnotices']['from']['reply_to'] );
1446
 
1447
+ $new_data['emailnotices']['content'] = sanitize_text_field( $data['emailnotices']['content'] );
1448
+ $new_data['emailnotices']['styling'] = sanitize_text_field( $data['emailnotices']['styling'] );
 
 
 
 
1449
 
1450
+ $new_data['emailnotices']['send'] = sanitize_text_field( $data['emailnotices']['send'] );
1451
 
1452
+ if ( ! isset( $data['emailnotices']['override'] ) )
1453
+ $data['emailnotices']['override'] = 0;
1454
 
1455
+ $new_data['emailnotices']['override'] = ( $data['emailnotices']['override'] == 1 ) ? true : false;
1456
+
1457
+ return $new_data;
 
 
 
1458
 
1459
  }
1460
 
1466
  public function render_subscription_shortcode( $attr, $content = NULL ) {
1467
 
1468
  extract( shortcode_atts( array(
1469
+ 'success' => __( 'Settings Updated', 'mycred' )
1470
  ), $attr ) );
1471
 
1472
  if ( ! is_user_logged_in() ) return $content;
1559
 
1560
  }
1561
 
1562
+ endif;
1563
+
1564
+ /**
1565
+ * Load Email Notice Module
1566
+ * @since 1.7
1567
+ * @version 1.0
1568
+ */
1569
+ if ( ! function_exists( 'mycred_load_email_notice_addon' ) ) :
1570
+ function mycred_load_email_notice_addon( $modules, $point_types ) {
1571
+
1572
+ $modules['solo']['emails'] = new myCRED_Email_Notice_Module();
1573
+ $modules['solo']['emails']->load();
1574
 
1575
+ return $modules;
1576
+
1577
+ }
1578
  endif;
1579
+ add_filter( 'mycred_load_modules', 'mycred_load_email_notice_addon', 60, 2 );
1580
 
1581
  /**
1582
  * myCRED Email Notifications Cron Job
addons/gateway/carts/mycred-marketpress.php CHANGED
@@ -4,52 +4,52 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
4
  /**
5
  * MarketPress Payment Gateway
6
  * @since 1.1
7
- * @version 1.3
8
  */
9
- add_action( 'mp_load_gateway_plugins', 'mycred_init_marketpress_gateway' );
10
- if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
11
- function mycred_init_marketpress_gateway()
12
- {
13
  if ( ! class_exists( 'MP_Gateway_API' ) ) return;
14
-
15
  class MP_Gateway_myCRED extends MP_Gateway_API {
16
 
17
- var $plugin_name = 'mycred';
18
- var $admin_name = 'myCRED';
19
- var $public_name = 'myCRED';
20
- var $mycred_type = 'mycred_default';
21
- var $method_img_url = '';
22
  var $method_button_img_url = '';
23
- var $force_ssl = false;
24
  var $ipn_url;
25
- var $skip_form = false;
26
 
27
  /**
28
  * Runs when your class is instantiated. Use to setup your plugin instead of __construct()
29
  */
30
  function on_creation() {
 
31
  global $mp;
32
- $settings = get_option( 'mp_settings' );
33
 
34
- //set names here to be able to translate
35
- $this->admin_name = 'myCRED';
 
36
 
37
- $this->public_name = mycred_label( true );
38
  if ( isset( $settings['gateways']['mycred']['name'] ) && ! empty( $settings['gateways']['mycred']['name'] ) )
39
  $this->public_name = $settings['gateways']['mycred']['name'];
40
 
41
- $this->method_img_url = plugins_url( 'assets/images/cred-icon32.png', myCRED_THIS );
42
  if ( isset( $settings['gateways']['mycred']['logo'] ) && ! empty( $settings['gateways']['mycred']['logo'] ) )
43
  $this->method_img_url = $settings['gateways']['mycred']['logo'];
44
 
45
  $this->method_button_img_url = $this->public_name;
46
-
47
  if ( ! isset( $settings['gateways']['mycred']['type'] ) )
48
- $this->mycred_type = 'mycred_default';
49
  else
50
  $this->mycred_type = $settings['gateways']['mycred']['type'];
51
 
52
- $this->mycred = mycred( $this->mycred_type );
 
53
  }
54
 
55
  /**
@@ -59,11 +59,14 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
59
  * @version 1.0
60
  */
61
  function use_exchange() {
 
62
  global $mp;
63
 
64
  $settings = get_option( 'mp_settings' );
65
  if ( $settings['currency'] == 'POINTS' ) return false;
 
66
  return true;
 
67
  }
68
 
69
  /**
@@ -72,6 +75,7 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
72
  * @version 1.0
73
  */
74
  function get_cart_total( $cart = NULL ) {
 
75
  global $mp;
76
 
77
  // Get total
@@ -81,28 +85,26 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
81
  $totals[] = $mp->before_tax_price( $data['price'], $product_id ) * $data['quantity'];
82
  }
83
  }
84
- $total = array_sum( $totals );
85
 
86
  // Apply Coupons
87
- if ( $coupon = $mp->coupon_value( $mp->get_coupon_code(), $total ) ) {
88
  $total = $coupon['new_total'];
89
- }
90
 
91
  // Shipping Cost
92
- if ( ( $shipping_price = $mp->shipping_price() ) !== false ) {
93
  $total = $total + $shipping_price;
94
- }
95
 
96
  // Tax
97
- if ( ( $tax_price = $mp->tax_price() ) !== false ) {
98
  $total = $total + $tax_price;
99
- }
100
 
101
  $settings = get_option( 'mp_settings' );
102
  if ( $this->use_exchange() )
103
  return $this->mycred->apply_exchange_rate( $total, $settings['gateways']['mycred']['exchange'] );
104
  else
105
  return $this->mycred->number( $total );
 
106
  }
107
 
108
  /**
@@ -114,28 +116,37 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
114
  * @version 1.1
115
  */
116
  function payment_form( $cart, $shipping_info ) {
 
117
  global $mp;
118
 
119
  $settings = get_option( 'mp_settings' );
120
 
121
  if ( ! is_user_logged_in() ) {
 
122
  $message = str_replace( '%login_url_here%', wp_login_url( mp_checkout_step_url( 'checkout' ) ), $settings['gateways']['mycred']['visitors'] );
123
  $message = $this->mycred->template_tags_general( $message );
 
124
  return '<div id="mp-mycred-balance">' . $message . '</div>';
 
125
  }
126
 
127
- $balance = $this->mycred->get_users_cred( get_current_user_id(), $this->mycred_type );
128
- $total = $this->get_cart_total( $cart );
129
 
130
  // Low balance
131
  if ( $balance-$total < 0 ) {
132
- $message = $this->mycred->template_tags_user( $settings['gateways']['mycred']['lowfunds'], false, wp_get_current_user() );
 
133
  $instructions = '<div id="mp-mycred-balance">' . $message . '</div>';
134
- $red = ' style="color: red;"';
 
135
  }
136
  else {
 
 
137
  $instructions = $this->mycred->template_tags_general( $settings['gateways']['mycred']['instructions'] );
138
- $red = '';
 
139
  }
140
 
141
  // Return Cost
@@ -157,6 +168,7 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
157
  </tr>
158
  </table>
159
  </div>';
 
160
  }
161
 
162
  /**
@@ -169,24 +181,29 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
169
  * @version 1.1
170
  */
171
  function confirm_payment_form( $cart, $shipping_info ) {
 
172
  global $mp;
173
 
174
  $settings = get_option( 'mp_settings' );
175
- $user_id = get_current_user_id();
176
- $balance = $this->mycred->get_users_cred( get_current_user_id(), $this->mycred_type );
177
- $total = $this->get_cart_total( $cart );
178
 
179
- $table = '<table class="mycred-cart-cost"><thead><tr><th>' . __( 'Payment', 'mycred' ) . '</th></tr></thead>';
180
  if ( $balance-$total < 0 ) {
 
181
  $message = $this->mycred->template_tags_user( $settings['gateways']['mycred']['lowfunds'], false, wp_get_current_user() );
182
  $table .= '<tr><td id="mp-mycred-cost" style="color: red; font-weight: bold;"><p>' . $message . ' <a href="' . mp_checkout_step_url( 'checkout' ) . '">' . __( 'Go Back', 'mycred' ) . '</a></td></tr>';
 
183
  }
 
184
  else
185
  $table .= '<tr><td id="mp-mycred-cost" class="mycred-ok">' . $this->mycred->format_creds( $total ) . ' ' . __( 'will be deducted from your account.', 'mycred' ) . '</td></tr>';
186
 
187
  return $table . '</table>';
 
188
  }
189
-
190
  function process_payment_form( $cart, $shipping_info ) { }
191
 
192
  /**
@@ -202,17 +219,20 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
202
  * @version 1.3
203
  */
204
  function process_payment( $cart, $shipping_info ) {
 
205
  global $mp;
206
 
207
- $settings = get_option('mp_settings');
208
- $user_id = get_current_user_id();
209
  $insolvent = $this->mycred->template_tags_user( $settings['gateways']['mycred']['lowfunds'], false, wp_get_current_user() );
210
  $timestamp = time();
211
 
212
  // This gateway requires buyer to be logged in
213
  if ( ! is_user_logged_in() ) {
 
214
  $message = str_replace( '%login_url_here%', wp_login_url( mp_checkout_step_url( 'checkout' ) ), $settings['gateways']['mycred']['visitors'] );
215
  $mp->cart_checkout_error( $this->mycred->template_tags_general( $message ) );
 
216
  }
217
 
218
  // Make sure current user is not excluded from using myCRED
@@ -222,37 +242,42 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
222
  );
223
 
224
  // Get users balance
225
- $balance = $this->mycred->get_users_cred( $user_id, $this->mycred_type );
226
- $total = $this->get_cart_total( $cart );
227
 
228
  // Low balance or Insolvent
229
  if ( $balance <= $this->mycred->zero() || $balance-$total < $this->mycred->zero() ) {
 
230
  $mp->cart_checkout_error(
231
  $insolvent . ' <a href="' . mp_checkout_step_url( 'checkout' ) . '">' . __( 'Go Back', 'mycred' ) . '</a>'
232
  );
233
  return;
 
234
  }
235
 
236
  // Let others decline a store order
237
  $decline = apply_filters( 'mycred_decline_store_purchase', false, $cart, $this );
238
  if ( $decline !== false ) {
 
239
  $mp->cart_checkout_error( $decline );
240
  return;
 
241
  }
242
 
243
  // Create MarketPress order
244
- $order_id = $mp->generate_order_id();
245
- $payment_info['gateway_public_name'] = $this->public_name;
246
  $payment_info['gateway_private_name'] = $this->admin_name;
247
  $payment_info['status'][ $timestamp ] = __( 'Paid', 'mycred' );
248
- $payment_info['total'] = $total;
249
- $payment_info['currency'] = $settings['currency'];
250
- $payment_info['method'] = __( 'myCRED', 'mycred' );
251
- $payment_info['transaction_id'] = $order_id;
252
- $paid = true;
 
253
  $result = $mp->create_order( $order_id, $cart, $shipping_info, $payment_info, $paid );
254
 
255
- $order = get_page_by_title( $result, 'OBJECT', 'mp_order' );
256
 
257
  // Deduct cost
258
  $this->mycred->add_creds(
@@ -268,23 +293,25 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
268
  // Profit Sharing
269
  if ( $settings['gateways']['mycred']['profit_share_percent'] > 0 ) {
270
  foreach ( $cart as $product_id => $variations ) {
 
271
  // Get Product
272
  $product = get_post( (int) $product_id );
273
-
274
  // Continue if product has just been deleted or owner is buyer
275
  if ( $product === NULL || $product->post_author == $cui ) continue;
276
-
277
  foreach ( $variations as $data ) {
278
- $price = $data['price'];
279
- $quantity = $data['quantity'];
280
- $cost = $price*$quantity;
 
281
 
282
  // Get profit share
283
  $percentage = apply_filters( 'mycred_marketpress_profit_share', $settings['gateways']['mycred']['profit_share_percent'], $order, $product, $this );
284
  if ( $percentage == 0 ) continue;
285
 
286
  // Calculate Share
287
- $share = ( $percentage / 100 ) * $cost;
288
 
289
  // Payout
290
  $this->mycred->add_creds(
@@ -296,11 +323,14 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
296
  array( 'ref_type' => 'post' ),
297
  $this->mycred_type
298
  );
 
299
  }
 
300
  }
301
  }
 
302
  }
303
-
304
  function order_confirmation( $order ) { }
305
 
306
  /**
@@ -310,7 +340,9 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
310
  * @version 1.0
311
  */
312
  function order_confirmation_email( $msg, $order ) {
 
313
  global $mp;
 
314
  $settings = get_option('mp_settings');
315
 
316
  if ( isset( $settings['gateways']['mycred']['email'] ) )
@@ -319,6 +351,7 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
319
  $msg = $settings['email']['new_order_txt'];
320
 
321
  return $msg;
 
322
  }
323
 
324
  /**
@@ -328,17 +361,19 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
328
  * @version 1.1
329
  */
330
  function order_confirmation_msg( $content, $order ) {
 
331
  global $mp;
 
332
  $settings = get_option('mp_settings');
 
 
333
 
334
- $mycred = mycred();
335
- $user_id = get_current_user_id();
336
-
337
  return $content . str_replace(
338
  'TOTAL',
339
  $mp->format_currency( $order->mp_payment_info['currency'], $order->mp_payment_info['total'] ),
340
  $mycred->template_tags_user( $settings['gateways']['mycred']['confirmation'], false, wp_get_current_user() )
341
  );
 
342
  }
343
 
344
  /**
@@ -347,15 +382,17 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
347
  * @version 1.3
348
  */
349
  function gateway_settings_box( $settings ) {
 
350
  global $mp;
 
351
  $settings = get_option( 'mp_settings' );
352
- $mycred = mycred();
353
-
354
- $name = mycred_label( true );
355
  $settings['gateways']['mycred'] = shortcode_atts( array(
356
  'name' => $name . ' ' . $mycred->template_tags_general( __( '%_singular% Balance', 'mycred' ) ),
357
  'logo' => $this->method_button_img_url,
358
- 'type' => 'mycred_default',
359
  'log_template' => __( 'Payment for Order: #%order_id%', 'mycred' ),
360
  'exchange' => 1,
361
  'profit_share_percent' => 0,
@@ -365,8 +402,9 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
365
  'lowfunds' => __( 'Insufficient funds.', 'mycred' ),
366
  'visitors' => __( 'You must be logged in to pay with %_plural%. Please <a href="%login_url_here%">login</a>.', 'mycred' ),
367
  'email' => $settings['email']['new_order_txt']
368
- ), ( isset( $settings['gateways']['mycred'] ) ) ? $settings['gateways']['mycred'] : array() ); ?>
369
 
 
370
  <div id="mp_mycred_payments" class="postbox mp-pages-msgs">
371
  <h3 class="handle"><span><?php echo $name . ' ' . __( 'Settings', 'mycred' ); ?></span></h3>
372
  <div class="inside">
@@ -399,12 +437,15 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
399
  </td>
400
  </tr>
401
  <?php
 
402
  // Exchange rate
403
  if ( $this->use_exchange() ) :
 
404
  $exchange_desc = __( 'How much is 1 %_singular% worth in %currency%?', 'mycred' );
405
  $exchange_desc = $mycred->template_tags_general( $exchange_desc );
406
- $exchange_desc = str_replace( '%currency%', $settings['currency'], $exchange_desc ); ?>
407
 
 
408
  <tr>
409
  <th scope="row"><label for="mycred-exchange-rate"><?php _e( 'Exchange Rate', 'mycred' ); ?></label></th>
410
  <td>
@@ -412,8 +453,11 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
412
  <p><input value="<?php echo esc_attr( $settings['gateways']['mycred']['exchange'] ); ?>" size="8" name="mp[gateways][mycred][exchange]" id="mycred-exchange-rate" type="text" /></p>
413
  </td>
414
  </tr>
415
- <?php endif; ?>
416
 
 
 
 
417
  <tr>
418
  <td colspan="2"><h4><?php _e( 'Profit Sharing', 'mycred' ); ?></h4></td>
419
  </tr>
@@ -478,6 +522,7 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
478
  </div>
479
  </div>
480
  <?php
 
481
  }
482
 
483
  /**
@@ -486,47 +531,52 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
486
  * @version 1.3
487
  */
488
  function process_gateway_settings( $settings ) {
 
489
  // Name (no html)
490
- $settings['gateways']['mycred']['name'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['name'] ) );
491
 
492
  // Log Template (no html)
493
- $settings['gateways']['mycred']['log_template'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['log_template'] ) );
494
- $settings['gateways']['mycred']['type'] = sanitize_text_field( $settings['gateways']['mycred']['type'] );
495
- $settings['gateways']['mycred']['logo'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['logo'] ) );
496
 
497
  // Exchange rate (if used)
498
  if ( $this->use_exchange() ) {
 
499
  // Decimals must start with a zero
500
- if ( $settings['gateways']['mycred']['exchange'] != 1 && substr( $settings['gateways']['mycred']['exchange'], 0, 1 ) != '0' ) {
501
  $settings['gateways']['mycred']['exchange'] = (float) '0' . $settings['gateways']['mycred']['exchange'];
502
- }
503
  // Decimal seperator must be punctuation and not comma
504
  $settings['gateways']['mycred']['exchange'] = str_replace( ',', '.', $settings['gateways']['mycred']['exchange'] );
 
505
  }
506
  else
507
  $settings['gateways']['mycred']['exchange'] = 1;
508
 
509
- $settings['gateways']['mycred']['profit_share_percent'] = stripslashes( trim( $settings['gateways']['mycred']['profit_share_percent'] ) );
510
- $settings['gateways']['mycred']['profit_share_log'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['profit_share_log'] ) );
511
 
512
- $settings['gateways']['mycred']['lowfunds'] = stripslashes( wp_filter_post_kses( $settings['gateways']['mycred']['lowfunds'] ) );
513
- $settings['gateways']['mycred']['visitors'] = stripslashes( wp_filter_post_kses( $settings['gateways']['mycred']['visitors'] ) );
514
 
515
- $allowed_tags = $this->mycred->allowed_html_tags();
516
- $settings['gateways']['mycred']['instructions'] = wp_kses( $settings['gateways']['mycred']['instructions'], $allowed_tags );
517
- $settings['gateways']['mycred']['confirmation'] = wp_kses( $settings['gateways']['mycred']['confirmation'], $allowed_tags );
518
 
519
  // Email (no html)
520
- $settings['gateways']['mycred']['email'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['email'] ) );
521
 
522
  return $settings;
 
523
  }
 
524
  }
 
525
  mp_register_gateway_plugin( 'MP_Gateway_myCRED', 'mycred', 'myCRED' );
526
- }
527
- }
528
 
529
-
 
 
530
 
531
  /**
532
  * Filter the myCRED Log
@@ -534,19 +584,19 @@ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) {
534
  * @since 1.1
535
  * @version 1.1
536
  */
537
- if ( ! function_exists( 'mycred_marketpress_parse_log' ) ) {
538
- add_filter( 'mycred_parse_log_entry_marketpress_payment', 'mycred_marketpress_parse_log', 90, 2 );
539
- function mycred_marketpress_parse_log( $content, $log_entry )
540
- {
541
  // Prep
542
  global $mp;
543
- $mycred = mycred( $log_entry->ctype );
544
- $order = get_post( $log_entry->ref_id );
 
545
  $order_id = $order->post_title;
546
- $user_id = get_current_user_id();
547
 
548
  // Order ID
549
- $content = str_replace( '%order_id%', $order->post_title, $content );
550
 
551
  // Link to order if we can edit plugin or are the user who made the order
552
  if ( $user_id == $log_entry->user_id || $mycred->can_edit_plugin( $user_id ) ) {
@@ -558,28 +608,37 @@ if ( ! function_exists( 'mycred_marketpress_parse_log' ) ) {
558
  }
559
 
560
  return $content;
 
561
  }
562
- }
 
563
 
564
  /**
565
  * Parse Email Notice
566
  * @since 1.2.2
567
  * @version 1.0
568
  */
569
- if ( ! function_exists( 'mycred_market_parse_email' ) ) {
570
- add_filter( 'mycred_email_before_send', 'mycred_market_parse_email' );
571
- function mycred_market_parse_email( $email )
572
- {
573
  if ( $email['request']['ref'] == 'marketpress_payment' ) {
 
574
  $order = get_post( (int) $email['request']['ref_id'] );
575
  if ( isset( $order->id ) ) {
 
576
  $track_link = '<a href="' . mp_orderstatus_link( false, true ) . $order_id . '/' . '">#' . $order->post_title . '/' . '</a>';
 
577
 
578
- $content = str_replace( '%order_id%', $order->post_title, $email['request']['entry'] );
579
  $email['request']['entry'] = str_replace( '%order_link%', $track_link, $content );
 
580
  }
 
581
  }
 
582
  return $email;
 
583
  }
584
- }
 
 
585
  ?>
4
  /**
5
  * MarketPress Payment Gateway
6
  * @since 1.1
7
+ * @version 1.3.1
8
  */
9
+ if ( ! function_exists( 'mycred_init_marketpress_gateway' ) ) :
10
+ function mycred_init_marketpress_gateway() {
11
+
 
12
  if ( ! class_exists( 'MP_Gateway_API' ) ) return;
13
+
14
  class MP_Gateway_myCRED extends MP_Gateway_API {
15
 
16
+ var $plugin_name = MYCRED_SLUG;
17
+ var $admin_name = MYCRED_DEFAULT_LABEL;
18
+ var $public_name = MYCRED_DEFAULT_LABEL;
19
+ var $mycred_type = MYCRED_DEFAULT_TYPE_KEY;
20
+ var $method_img_url = '';
21
  var $method_button_img_url = '';
22
+ var $force_ssl = false;
23
  var $ipn_url;
24
+ var $skip_form = false;
25
 
26
  /**
27
  * Runs when your class is instantiated. Use to setup your plugin instead of __construct()
28
  */
29
  function on_creation() {
30
+
31
  global $mp;
 
32
 
33
+ $settings = get_option( 'mp_settings' );
34
+ $this->admin_name = 'myCRED';
35
+ $this->public_name = mycred_label( true );
36
 
 
37
  if ( isset( $settings['gateways']['mycred']['name'] ) && ! empty( $settings['gateways']['mycred']['name'] ) )
38
  $this->public_name = $settings['gateways']['mycred']['name'];
39
 
40
+ $this->method_img_url = plugins_url( 'assets/images/cred-icon32.png', myCRED_THIS );
41
  if ( isset( $settings['gateways']['mycred']['logo'] ) && ! empty( $settings['gateways']['mycred']['logo'] ) )
42
  $this->method_img_url = $settings['gateways']['mycred']['logo'];
43
 
44
  $this->method_button_img_url = $this->public_name;
45
+
46
  if ( ! isset( $settings['gateways']['mycred']['type'] ) )
47
+ $this->mycred_type = MYCRED_DEFAULT_TYPE_KEY;
48
  else
49
  $this->mycred_type = $settings['gateways']['mycred']['type'];
50
 
51
+ $this->mycred = mycred( $this->mycred_type );
52
+
53
  }
54
 
55
  /**
59
  * @version 1.0
60
  */
61
  function use_exchange() {
62
+
63
  global $mp;
64
 
65
  $settings = get_option( 'mp_settings' );
66
  if ( $settings['currency'] == 'POINTS' ) return false;
67
+
68
  return true;
69
+
70
  }
71
 
72
  /**
75
  * @version 1.0
76
  */
77
  function get_cart_total( $cart = NULL ) {
78
+
79
  global $mp;
80
 
81
  // Get total
85
  $totals[] = $mp->before_tax_price( $data['price'], $product_id ) * $data['quantity'];
86
  }
87
  }
88
+ $total = array_sum( $totals );
89
 
90
  // Apply Coupons
91
+ if ( $coupon = $mp->coupon_value( $mp->get_coupon_code(), $total ) )
92
  $total = $coupon['new_total'];
 
93
 
94
  // Shipping Cost
95
+ if ( ( $shipping_price = $mp->shipping_price() ) !== false )
96
  $total = $total + $shipping_price;
 
97
 
98
  // Tax
99
+ if ( ( $tax_price = $mp->tax_price() ) !== false )
100
  $total = $total + $tax_price;
 
101
 
102
  $settings = get_option( 'mp_settings' );
103
  if ( $this->use_exchange() )
104
  return $this->mycred->apply_exchange_rate( $total, $settings['gateways']['mycred']['exchange'] );
105
  else
106
  return $this->mycred->number( $total );
107
+
108
  }
109
 
110
  /**
116
  * @version 1.1
117
  */
118
  function payment_form( $cart, $shipping_info ) {
119
+
120
  global $mp;
121
 
122
  $settings = get_option( 'mp_settings' );
123
 
124
  if ( ! is_user_logged_in() ) {
125
+
126
  $message = str_replace( '%login_url_here%', wp_login_url( mp_checkout_step_url( 'checkout' ) ), $settings['gateways']['mycred']['visitors'] );
127
  $message = $this->mycred->template_tags_general( $message );
128
+
129
  return '<div id="mp-mycred-balance">' . $message . '</div>';
130
+
131
  }
132
 
133
+ $balance = $this->mycred->get_users_balance( get_current_user_id(), $this->mycred_type );
134
+ $total = $this->get_cart_total( $cart );
135
 
136
  // Low balance
137
  if ( $balance-$total < 0 ) {
138
+
139
+ $message = $this->mycred->template_tags_user( $settings['gateways']['mycred']['lowfunds'], false, wp_get_current_user() );
140
  $instructions = '<div id="mp-mycred-balance">' . $message . '</div>';
141
+ $red = ' style="color: red;"';
142
+
143
  }
144
  else {
145
+
146
+ $message = '';
147
  $instructions = $this->mycred->template_tags_general( $settings['gateways']['mycred']['instructions'] );
148
+ $red = '';
149
+
150
  }
151
 
152
  // Return Cost
168
  </tr>
169
  </table>
170
  </div>';
171
+
172
  }
173
 
174
  /**
181
  * @version 1.1
182
  */
183
  function confirm_payment_form( $cart, $shipping_info ) {
184
+
185
  global $mp;
186
 
187
  $settings = get_option( 'mp_settings' );
188
+ $user_id = get_current_user_id();
189
+ $balance = $this->mycred->get_users_balance( get_current_user_id(), $this->mycred_type );
190
+ $total = $this->get_cart_total( $cart );
191
 
192
+ $table = '<table class="mycred-cart-cost"><thead><tr><th>' . __( 'Payment', 'mycred' ) . '</th></tr></thead>';
193
  if ( $balance-$total < 0 ) {
194
+
195
  $message = $this->mycred->template_tags_user( $settings['gateways']['mycred']['lowfunds'], false, wp_get_current_user() );
196
  $table .= '<tr><td id="mp-mycred-cost" style="color: red; font-weight: bold;"><p>' . $message . ' <a href="' . mp_checkout_step_url( 'checkout' ) . '">' . __( 'Go Back', 'mycred' ) . '</a></td></tr>';
197
+
198
  }
199
+
200
  else
201
  $table .= '<tr><td id="mp-mycred-cost" class="mycred-ok">' . $this->mycred->format_creds( $total ) . ' ' . __( 'will be deducted from your account.', 'mycred' ) . '</td></tr>';
202
 
203
  return $table . '</table>';
204
+
205
  }
206
+
207
  function process_payment_form( $cart, $shipping_info ) { }
208
 
209
  /**
219
  * @version 1.3
220
  */
221
  function process_payment( $cart, $shipping_info ) {
222
+
223
  global $mp;
224
 
225
+ $settings = get_option('mp_settings');
226
+ $user_id = get_current_user_id();
227
  $insolvent = $this->mycred->template_tags_user( $settings['gateways']['mycred']['lowfunds'], false, wp_get_current_user() );
228
  $timestamp = time();
229
 
230
  // This gateway requires buyer to be logged in
231
  if ( ! is_user_logged_in() ) {
232
+
233
  $message = str_replace( '%login_url_here%', wp_login_url( mp_checkout_step_url( 'checkout' ) ), $settings['gateways']['mycred']['visitors'] );
234
  $mp->cart_checkout_error( $this->mycred->template_tags_general( $message ) );
235
+
236
  }
237
 
238
  // Make sure current user is not excluded from using myCRED
242
  );
243
 
244
  // Get users balance
245
+ $balance = $this->mycred->get_users_balance( $user_id, $this->mycred_type );
246
+ $total = $this->get_cart_total( $cart );
247
 
248
  // Low balance or Insolvent
249
  if ( $balance <= $this->mycred->zero() || $balance-$total < $this->mycred->zero() ) {
250
+
251
  $mp->cart_checkout_error(
252
  $insolvent . ' <a href="' . mp_checkout_step_url( 'checkout' ) . '">' . __( 'Go Back', 'mycred' ) . '</a>'
253
  );
254
  return;
255
+
256
  }
257
 
258
  // Let others decline a store order
259
  $decline = apply_filters( 'mycred_decline_store_purchase', false, $cart, $this );
260
  if ( $decline !== false ) {
261
+
262
  $mp->cart_checkout_error( $decline );
263
  return;
264
+
265
  }
266
 
267
  // Create MarketPress order
268
+ $order_id = $mp->generate_order_id();
269
+ $payment_info['gateway_public_name'] = $this->public_name;
270
  $payment_info['gateway_private_name'] = $this->admin_name;
271
  $payment_info['status'][ $timestamp ] = __( 'Paid', 'mycred' );
272
+ $payment_info['total'] = $total;
273
+ $payment_info['currency'] = $settings['currency'];
274
+ $payment_info['method'] = __( 'myCRED', 'mycred' );
275
+ $payment_info['transaction_id'] = $order_id;
276
+
277
+ $paid = true;
278
  $result = $mp->create_order( $order_id, $cart, $shipping_info, $payment_info, $paid );
279
 
280
+ $order = get_page_by_title( $result, 'OBJECT', 'mp_order' );
281
 
282
  // Deduct cost
283
  $this->mycred->add_creds(
293
  // Profit Sharing
294
  if ( $settings['gateways']['mycred']['profit_share_percent'] > 0 ) {
295
  foreach ( $cart as $product_id => $variations ) {
296
+
297
  // Get Product
298
  $product = get_post( (int) $product_id );
299
+
300
  // Continue if product has just been deleted or owner is buyer
301
  if ( $product === NULL || $product->post_author == $cui ) continue;
302
+
303
  foreach ( $variations as $data ) {
304
+
305
+ $price = $data['price'];
306
+ $quantity = $data['quantity'];
307
+ $cost = $price*$quantity;
308
 
309
  // Get profit share
310
  $percentage = apply_filters( 'mycred_marketpress_profit_share', $settings['gateways']['mycred']['profit_share_percent'], $order, $product, $this );
311
  if ( $percentage == 0 ) continue;
312
 
313
  // Calculate Share
314
+ $share = ( $percentage / 100 ) * $cost;
315
 
316
  // Payout
317
  $this->mycred->add_creds(
323
  array( 'ref_type' => 'post' ),
324
  $this->mycred_type
325
  );
326
+
327
  }
328
+
329
  }
330
  }
331
+
332
  }
333
+
334
  function order_confirmation( $order ) { }
335
 
336
  /**
340
  * @version 1.0
341
  */
342
  function order_confirmation_email( $msg, $order ) {
343
+
344
  global $mp;
345
+
346
  $settings = get_option('mp_settings');
347
 
348
  if ( isset( $settings['gateways']['mycred']['email'] ) )
351
  $msg = $settings['email']['new_order_txt'];
352
 
353
  return $msg;
354
+
355
  }
356
 
357
  /**
361
  * @version 1.1
362
  */
363
  function order_confirmation_msg( $content, $order ) {
364
+
365
  global $mp;
366
+
367
  $settings = get_option('mp_settings');
368
+ $mycred = mycred();
369
+ $user_id = get_current_user_id();
370
 
 
 
 
371
  return $content . str_replace(
372
  'TOTAL',
373
  $mp->format_currency( $order->mp_payment_info['currency'], $order->mp_payment_info['total'] ),
374
  $mycred->template_tags_user( $settings['gateways']['mycred']['confirmation'], false, wp_get_current_user() )
375
  );
376
+
377
  }
378
 
379
  /**
382
  * @version 1.3
383
  */
384
  function gateway_settings_box( $settings ) {
385
+
386
  global $mp;
387
+
388
  $settings = get_option( 'mp_settings' );
389
+ $mycred = mycred();
390
+ $name = mycred_label( true );
391
+
392
  $settings['gateways']['mycred'] = shortcode_atts( array(
393
  'name' => $name . ' ' . $mycred->template_tags_general( __( '%_singular% Balance', 'mycred' ) ),
394
  'logo' => $this->method_button_img_url,
395
+ 'type' => MYCRED_DEFAULT_TYPE_KEY,
396
  'log_template' => __( 'Payment for Order: #%order_id%', 'mycred' ),
397
  'exchange' => 1,
398
  'profit_share_percent' => 0,
402
  'lowfunds' => __( 'Insufficient funds.', 'mycred' ),
403
  'visitors' => __( 'You must be logged in to pay with %_plural%. Please <a href="%login_url_here%">login</a>.', 'mycred' ),
404
  'email' => $settings['email']['new_order_txt']
405
+ ), ( isset( $settings['gateways']['mycred'] ) ) ? $settings['gateways']['mycred'] : array() );
406
 
407
+ ?>
408
  <div id="mp_mycred_payments" class="postbox mp-pages-msgs">
409
  <h3 class="handle"><span><?php echo $name . ' ' . __( 'Settings', 'mycred' ); ?></span></h3>
410
  <div class="inside">
437
  </td>
438
  </tr>
439
  <?php
440
+
441
  // Exchange rate
442
  if ( $this->use_exchange() ) :
443
+
444
  $exchange_desc = __( 'How much is 1 %_singular% worth in %currency%?', 'mycred' );
445
  $exchange_desc = $mycred->template_tags_general( $exchange_desc );
446
+ $exchange_desc = str_replace( '%currency%', $settings['currency'], $exchange_desc );
447
 
448
+ ?>
449
  <tr>
450
  <th scope="row"><label for="mycred-exchange-rate"><?php _e( 'Exchange Rate', 'mycred' ); ?></label></th>
451
  <td>
453
  <p><input value="<?php echo esc_attr( $settings['gateways']['mycred']['exchange'] ); ?>" size="8" name="mp[gateways][mycred][exchange]" id="mycred-exchange-rate" type="text" /></p>
454
  </td>
455
  </tr>
456
+ <?php
457
 
458
+ endif;
459
+
460
+ ?>
461
  <tr>
462
  <td colspan="2"><h4><?php _e( 'Profit Sharing', 'mycred' ); ?></h4></td>
463
  </tr>
522
  </div>
523
  </div>
524
  <?php
525
+
526
  }
527
 
528
  /**
531
  * @version 1.3
532
  */
533
  function process_gateway_settings( $settings ) {
534
+
535
  // Name (no html)
536
+ $settings['gateways']['mycred']['name'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['name'] ) );
537
 
538
  // Log Template (no html)
539
+ $settings['gateways']['mycred']['log_template'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['log_template'] ) );
540
+ $settings['gateways']['mycred']['type'] = sanitize_text_field( $settings['gateways']['mycred']['type'] );
541
+ $settings['gateways']['mycred']['logo'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['logo'] ) );
542
 
543
  // Exchange rate (if used)
544
  if ( $this->use_exchange() ) {
545
+
546
  // Decimals must start with a zero
547
+ if ( $settings['gateways']['mycred']['exchange'] != 1 && substr( $settings['gateways']['mycred']['exchange'], 0, 1 ) != '0' )
548
  $settings['gateways']['mycred']['exchange'] = (float) '0' . $settings['gateways']['mycred']['exchange'];
549
+
550
  // Decimal seperator must be punctuation and not comma
551
  $settings['gateways']['mycred']['exchange'] = str_replace( ',', '.', $settings['gateways']['mycred']['exchange'] );
552
+
553
  }
554
  else
555
  $settings['gateways']['mycred']['exchange'] = 1;
556
 
557
+ $settings['gateways']['mycred']['profit_share_percent'] = stripslashes( sanitize_text_field( $settings['gateways']['mycred']['profit_share_percent'] ) );
558
+ $settings['gateways']['mycred']['profit_share_log'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['profit_share_log'] ) );
559
 
560
+ $settings['gateways']['mycred']['lowfunds'] = stripslashes( wp_filter_post_kses( $settings['gateways']['mycred']['lowfunds'] ) );
561
+ $settings['gateways']['mycred']['visitors'] = stripslashes( wp_filter_post_kses( $settings['gateways']['mycred']['visitors'] ) );
562
 
563
+ $settings['gateways']['mycred']['instructions'] = wp_kses_post( $settings['gateways']['mycred']['instructions'] );
564
+ $settings['gateways']['mycred']['confirmation'] = wp_kses_post( $settings['gateways']['mycred']['confirmation'] );
 
565
 
566
  // Email (no html)
567
+ $settings['gateways']['mycred']['email'] = stripslashes( wp_filter_nohtml_kses( $settings['gateways']['mycred']['email'] ) );
568
 
569
  return $settings;
570
+
571
  }
572
+
573
  }
574
+
575
  mp_register_gateway_plugin( 'MP_Gateway_myCRED', 'mycred', 'myCRED' );
 
 
576
 
577
+ }
578
+ endif;
579
+ add_action( 'mp_load_gateway_plugins', 'mycred_init_marketpress_gateway' );
580
 
581
  /**
582
  * Filter the myCRED Log
584
  * @since 1.1
585
  * @version 1.1
586
  */
587
+ if ( ! function_exists( 'mycred_marketpress_parse_log' ) ) :
588
+ function mycred_marketpress_parse_log( $content, $log_entry ) {
589
+
 
590
  // Prep
591
  global $mp;
592
+
593
+ $mycred = mycred( $log_entry->ctype );
594
+ $order = get_post( $log_entry->ref_id );
595
  $order_id = $order->post_title;
596
+ $user_id = get_current_user_id();
597
 
598
  // Order ID
599
+ $content = str_replace( '%order_id%', $order->post_title, $content );
600
 
601
  // Link to order if we can edit plugin or are the user who made the order
602
  if ( $user_id == $log_entry->user_id || $mycred->can_edit_plugin( $user_id ) ) {
608
  }
609
 
610
  return $content;
611
+
612
  }
613
+ endif;
614
+ add_filter( 'mycred_parse_log_entry_marketpress_payment', 'mycred_marketpress_parse_log', 90, 2 );
615
 
616
  /**
617
  * Parse Email Notice
618
  * @since 1.2.2
619
  * @version 1.0
620
  */
621
+ if ( ! function_exists( 'mycred_market_parse_email' ) ) :
622
+ function mycred_market_parse_email( $email ) {
623
+
 
624
  if ( $email['request']['ref'] == 'marketpress_payment' ) {
625
+
626
  $order = get_post( (int) $email['request']['ref_id'] );
627
  if ( isset( $order->id ) ) {
628
+
629
  $track_link = '<a href="' . mp_orderstatus_link( false, true ) . $order_id . '/' . '">#' . $order->post_title . '/' . '</a>';
630
+ $content = str_replace( '%order_id%', $order->post_title, $email['request']['entry'] );
631
 
 
632
  $email['request']['entry'] = str_replace( '%order_link%', $track_link, $content );
633
+
634
  }
635
+
636
  }
637
+
638
  return $email;
639
+
640
  }
641
+ endif;
642
+ add_filter( 'mycred_email_before_send', 'mycred_market_parse_email', 20 );
643
+
644
  ?>
addons/gateway/carts/mycred-woocommerce.php CHANGED
@@ -7,9 +7,8 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
7
  * Custom Payment Gateway for WooCommerce.
8
  * @see http://docs.woothemes.com/document/payment-gateway-api/
9
  * @since 0.1
10
- * @version 1.4.2
11
  */
12
- add_action( 'after_setup_theme', 'mycred_init_woo_gateway' );
13
  if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
14
  function mycred_init_woo_gateway() {
15
 
@@ -24,21 +23,21 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
24
  */
25
  public function __construct() {
26
 
27
- $this->id = 'mycred';
28
- $this->icon = '';
29
- $this->has_fields = false;
30
- $this->method_title = __( 'myCRED', 'mycred' );
31
- $this->method_description = __( 'Let users pay using their myCRED balance.', 'mycred' );
32
- $this->supports = array(
33
  'products',
34
  'refunds'
35
  );
36
 
37
  $this->mycred_type = $this->get_option( 'point_type' );
38
  if ( $this->mycred_type === NULL || $this->mycred_type == '' )
39
- $this->mycred_type = 'mycred_default';
40
 
41
- $this->mycred = mycred( $this->mycred_type );
42
 
43
  // Load the settings.
44
  $this->init_form_fields();
@@ -121,13 +120,13 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
121
  'type' => 'select',
122
  'label' => __( 'Select the point type users can use to pay.', 'mycred' ),
123
  'options' => $mycred_types,
124
- 'default' => 'mycred_default'
125
  );
126
  }
127
  else {
128
  $fields['point_type'] = array(
129
  'type' => 'hidden',
130
- 'value' => 'mycred_default'
131
  );
132
  }
133
 
@@ -212,7 +211,6 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
212
 
213
  ?>
214
  <h3><?php _e( 'myCRED Payment', 'mycred' ); ?></h3>
215
- <p><?php echo $this->mycred->template_tags_general( __( 'Allows users to pay using their myCRED %_singular% balance. Please note that users with insufficient funds and users who are not logged in will not see this payment gateway on the checkout page.', 'mycred' ) ); ?></p>
216
  <table class="form-table">
217
  <?php
218
 
@@ -233,7 +231,6 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
233
  function process_payment( $order_id ) {
234
 
235
  global $woocommerce;
236
- $cui = get_current_user_id();
237
 
238
  // Make sure we are still logged in
239
  if ( ! is_user_logged_in() ) {
@@ -241,8 +238,10 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
241
  return;
242
  }
243
 
 
 
244
  // Make sure we have not been excluded
245
- if ( $this->mycred->exclude_user( $cui ) ) {
246
  wc_add_notice( $this->mycred->template_tags_general( __( 'You can not use this gateway. Please try a different payment option.', 'mycred' ) ), 'error' );
247
  return;
248
  }
@@ -259,7 +258,7 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
259
  $cost = apply_filters( 'mycred_woo_order_cost', $cost, $order, false, $this );
260
 
261
  // Check funds
262
- if ( $this->mycred->get_users_cred( $cui, $this->mycred_type ) < $cost ) {
263
  wc_add_notice( __( 'Insufficient funds.', 'mycred' ), 'error' );
264
  return;
265
  }
@@ -274,33 +273,36 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
274
  // Charge
275
  $this->mycred->add_creds(
276
  'woocommerce_payment',
277
- $cui,
278
- 0-$cost,
279
  $this->log_template,
280
  $order_id,
281
  array( 'ref_type' => 'post' ),
282
  $this->mycred_type
283
  );
 
284
  $order->payment_complete();
285
 
286
  // Profit Sharing
287
  if ( $this->profit_sharing_percent > 0 ) {
 
288
  // Get Items
289
  $items = $order->get_items();
290
 
291
  // Loop though items
292
  foreach ( $items as $item ) {
 
293
  // Get Product
294
- $product = get_post( (int) $item['product_id'] );
295
 
296
  // Continue if product has just been deleted or owner is buyer
297
- if ( $product === NULL || $product->post_author == $cui ) continue;
298
 
299
  // Calculate Share
300
  $percentage = apply_filters( 'mycred_woo_profit_share', $this->profit_sharing_percent, $order, $product, $this );
301
  if ( $percentage == 0 ) continue;
302
 
303
- $share = ( $percentage / 100 ) * $item['line_total'];
304
 
305
  // Payout
306
  $this->mycred->add_creds(
@@ -312,11 +314,13 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
312
  array( 'ref_type' => 'post' ),
313
  $this->mycred_type
314
  );
 
315
  }
 
316
  }
317
 
318
  // Let others play
319
- do_action( 'mycred_paid_for_woo', $order, $cui, $this );
320
 
321
  // Return the good news
322
  return array(
@@ -367,6 +371,7 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
367
 
368
  // Loop though items
369
  foreach ( $items as $item ) {
 
370
  // Get Product
371
  $product = get_post( (int) $item['product_id'] );
372
 
@@ -375,7 +380,7 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
375
 
376
  // Calculate Share
377
  // by: Leonie Heinle
378
- $share = ( $this->profit_sharing_percent / 100 ) * $item['line_total'];
379
 
380
  // Payout
381
  $this->mycred->add_creds(
@@ -387,6 +392,7 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
387
  array( 'ref_type' => 'post', 'order_id' => $order_id ),
388
  $this->mycred_type
389
  );
 
390
  }
391
 
392
  }
@@ -413,13 +419,13 @@ if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
413
 
414
  }
415
  endif;
 
416
 
417
  /**
418
  * Log Entry: Payment
419
  * @since 0.1
420
  * @version 1.4
421
  */
422
- add_filter( 'mycred_parse_log_entry_woocommerce_payment', 'mycred_woo_log_entry_payment', 90, 2 );
423
  if ( ! function_exists( 'mycred_woo_log_entry_payment' ) ) :
424
  function mycred_woo_log_entry_payment( $content, $log_entry ) {
425
 
@@ -442,20 +448,20 @@ if ( ! function_exists( 'mycred_woo_log_entry_payment' ) ) :
442
 
443
  }
444
  endif;
 
445
 
446
  /**
447
  * Log Entry: Refund
448
  * @since 1.5.4
449
  * @version 1.0
450
  */
451
- add_filter( 'mycred_parse_log_entry_woocommerce_refund', 'mycred_woo_log_entry_refunds', 90, 2 );
452
  if ( ! function_exists( 'mycred_woo_log_entry_refunds' ) ) :
453
  function mycred_woo_log_entry_refunds( $content, $log_entry ) {
454
 
455
  $content = mycred_woo_log_entry_payment( $content, $log_entry );
456
 
457
- $data = maybe_unserialize( $log_entry->data );
458
- $reason = '-';
459
  if ( isset( $data['reason'] ) && $data['reason'] != '' )
460
  $reason = $data['reason'];
461
 
@@ -465,70 +471,72 @@ if ( ! function_exists( 'mycred_woo_log_entry_refunds' ) ) :
465
 
466
  }
467
  endif;
 
468
 
469
  /**
470
  * Log Entry: Profit Share Refund
471
  * @since 1.5.4
472
  * @version 1.0
473
  */
474
- add_filter( 'mycred_parse_log_entry_store_sale_refund', 'mycred_woo_log_entry_profit_refund', 90, 2 );
475
  if ( ! function_exists( 'mycred_woo_log_entry_profit_refund' ) ) :
476
  function mycred_woo_log_entry_profit_refund( $content, $log_entry ) {
477
 
478
- $data = maybe_unserialize( $log_entry->data );
479
-
480
  $order_id = '';
481
  if ( isset( $data['order_id'] ) && $data['order_id'] != '' )
482
  $order_id = '#' . $data['order_id'];
483
 
484
- $content = str_replace( '%order_id%', $order_id, $content );
485
 
486
- $reason = '-';
487
  if ( isset( $data['reason'] ) && $data['reason'] != '' )
488
  $reason = $data['reason'];
489
 
490
- $content = str_replace( '%reason%', $reason, $content );
491
 
492
  return $content;
493
 
494
  }
495
  endif;
 
496
 
497
  /**
498
  * Parse Email Notice
499
  * @since 1.2.2
500
  * @version 1.0.2
501
  */
502
- add_filter( 'mycred_email_before_send', 'mycred_woo_parse_email' );
503
  if ( ! function_exists( 'mycred_woo_parse_email' ) ) :
504
  function mycred_woo_parse_email( $email ) {
505
 
506
  if ( $email['request']['ref'] == 'woocommerce_payment' && function_exists( 'woocommerce_get_page_id' ) ) {
507
-
508
  if ( function_exists( 'wc_get_order' ) )
509
  $order = wc_get_order( (int) $email['request']['ref_id'] );
510
  else
511
  $order = new WC_Order( (int) $email['request']['ref_id'] );
512
 
513
  if ( isset( $order->id ) ) {
514
- $url = esc_url( add_query_arg( 'order', $order->id, get_permalink( woocommerce_get_page_id( 'view_order' ) ) ) );
515
 
 
516
  $content = str_replace( '%order_id%', $order->id, $email['request']['entry'] );
 
517
  $email['request']['entry'] = str_replace( '%order_link%', '<a href="' . esc_url( $url ) . '">#' . $order->id . '</a>', $content );
 
518
  }
519
 
520
  }
 
521
  return $email;
522
 
523
  }
524
  endif;
 
525
 
526
  /**
527
  * Register Gateway
528
  * @since 0.1
529
  * @version 1.0
530
  */
531
- add_filter( 'woocommerce_payment_gateways', 'mycred_register_woo_gateway' );
532
  if ( ! function_exists( 'mycred_register_woo_gateway' ) ) :
533
  function mycred_register_woo_gateway( $methods ) {
534
 
@@ -537,6 +545,7 @@ if ( ! function_exists( 'mycred_register_woo_gateway' ) ) :
537
 
538
  }
539
  endif;
 
540
 
541
  /**
542
  * Available Gateways
@@ -548,7 +557,6 @@ endif;
548
  * @since 0.1
549
  * @version 1.2.1
550
  */
551
- add_filter( 'woocommerce_available_payment_gateways', 'mycred_woo_available_gateways' );
552
  if ( ! function_exists( 'mycred_woo_available_gateways' ) ) :
553
  function mycred_woo_available_gateways( $gateways ) {
554
 
@@ -559,23 +567,29 @@ if ( ! function_exists( 'mycred_woo_available_gateways' ) ) :
559
 
560
  // Check if we are logged in
561
  if ( ! is_user_logged_in() ) {
 
562
  unset( $gateways['mycred'] );
 
563
  return $gateways;
 
564
  }
565
 
566
- $type = $gateways['mycred']->get_option( 'point_type' );
567
  if ( $type === NULL )
568
- $type = 'mycred_default';
569
 
570
  // Get myCRED
571
- $mycred = mycred( $type );
572
- $cui = get_current_user_id();
573
 
574
  // Check if we are excluded from myCRED usage
575
- if ( $mycred->exclude_user( $cui ) ) {
 
576
  unset( $gateways['mycred'] );
577
  unset( $mycred );
 
578
  return $gateways;
 
579
  }
580
 
581
  global $woocommerce;
@@ -587,19 +601,21 @@ if ( ! function_exists( 'mycred_woo_available_gateways' ) ) :
587
  else
588
  $cost = $woocommerce->cart->total;
589
 
590
- $cost = apply_filters( 'mycred_woo_order_cost', $cost, $woocommerce->cart, true, $mycred );
591
 
592
  // Check if we have enough points
593
- if ( $mycred->get_users_cred( $cui, $type ) < $cost ) {
594
  $gateways['mycred']->enabled = 'no';
595
  }
596
 
597
  // Clean up and return
598
  unset( $mycred );
 
599
  return $gateways;
600
 
601
  }
602
  endif;
 
603
 
604
  /**
605
  * Add Currency
@@ -607,25 +623,27 @@ endif;
607
  * @since 0.1
608
  * @version 1.1
609
  */
610
- add_filter( 'woocommerce_currencies', 'mycred_woo_add_currency' );
611
  if ( ! function_exists( 'mycred_woo_add_currency' ) ) :
612
  function mycred_woo_add_currency( $currencies ) {
613
 
614
  $settings = get_option( 'woocommerce_mycred_settings', false );
615
  if ( $settings === false ) return $currencies;
616
 
617
- $type = 'mycred_default';
618
  if ( isset( $settings['point_type'] ) && ! empty( $settings['point_type'] ) )
619
  $type = $settings['point_type'];
620
 
621
- $mycred = mycred( $type );
 
622
  $currencies['MYC'] = $mycred->plural();
623
 
624
  unset( $mycred );
 
625
  return $currencies;
626
 
627
  }
628
  endif;
 
629
 
630
  /**
631
  * Currency Symbol
@@ -633,30 +651,35 @@ endif;
633
  * @since 0.1
634
  * @version 1.1
635
  */
636
- add_filter( 'woocommerce_currency_symbol', 'mycred_woo_currency', 10, 2 );
637
  if ( ! function_exists( 'mycred_woo_currency' ) ) :
638
  function mycred_woo_currency( $currency_symbol, $currency ) {
639
 
640
  $settings = get_option( 'woocommerce_mycred_settings', false );
641
  if ( $settings === false ) return $currency_symbol;
642
 
643
- $type = 'mycred_default';
644
  if ( isset( $settings['point_type'] ) && ! empty( $settings['point_type'] ) )
645
  $type = $settings['point_type'];
646
 
647
  switch ( $currency ) {
648
  case 'MYC':
 
649
  $mycred = mycred( $type );
 
650
  if ( ! empty( $mycred->before ) )
651
  $currency_symbol = $mycred->before;
 
652
  elseif ( ! empty( $mycred->after ) )
653
  $currency_symbol = $mycred->after;
 
654
  break;
655
  }
 
656
  return $currency_symbol;
657
 
658
  }
659
  endif;
 
660
 
661
  /**
662
  * Add CRED Cost
@@ -664,8 +687,6 @@ endif;
664
  * @since 0.1
665
  * @version 1.2.1
666
  */
667
- add_action( 'woocommerce_review_order_after_order_total', 'mycred_woo_after_order_total' );
668
- add_action( 'woocommerce_cart_totals_after_order_total', 'mycred_woo_after_order_total' );
669
  if ( ! function_exists( 'mycred_woo_after_order_total' ) ) :
670
  function mycred_woo_after_order_total() {
671
 
@@ -677,17 +698,18 @@ if ( ! function_exists( 'mycred_woo_after_order_total' ) ) :
677
  $available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
678
  if ( ! isset( $available_gateways['mycred'] ) ) return;
679
 
680
- $type = $available_gateways['mycred']->get_option( 'point_type' );
681
  if ( $type === NULL )
682
- $type = 'mycred_default';
683
 
684
- $mycred = mycred( $type );
685
- $cui = get_current_user_id();
686
 
687
- if ( $mycred->exclude_user( $cui ) ) return;
688
 
689
  // Check if enabled
690
  $show = $available_gateways['mycred']->get_option( 'show_total' );
 
691
  if ( empty( $show ) ) return;
692
  elseif ( $show === 'cart' && ! is_cart() ) return;
693
  elseif ( $show === 'checkout' && ! is_checkout() ) return;
@@ -698,37 +720,46 @@ if ( ! function_exists( 'mycred_woo_after_order_total' ) ) :
698
 
699
  // Apply Exchange Rate
700
  $cost = $mycred->number( ( $woocommerce->cart->total / $available_gateways['mycred']->get_option( 'exchange_rate' ) ) );
 
701
 
702
- $cost = apply_filters( 'mycred_woo_order_cost', $cost, $woocommerce->cart, true, $mycred ); ?>
703
-
704
  <tr class="total">
705
  <th><strong><?php echo $mycred->template_tags_general( $available_gateways['mycred']->get_option( 'total_label' ) ); ?></strong></th>
706
  <td>
707
- <?php
708
 
709
  // Balance
710
- $balance = $mycred->get_users_cred( $cui, $type );
711
 
712
  // Insufficient Funds
713
- if ( $balance < $cost ) { ?>
714
 
 
715
  <strong class="mycred-low-funds" style="color:red;"><?php echo $mycred->format_creds( $cost ); ?></strong>
716
  <?php
 
717
  }
 
718
  // Funds exist
719
- else { ?>
720
 
 
721
  <strong class="mycred-funds"><?php echo $mycred->format_creds( $cost ); ?></strong>
722
  <?php
723
- } ?>
724
 
 
 
 
725
  <small class="mycred-current-balance"><?php
726
 
727
  // Current balance
728
- echo sprintf( '( %1s: %2s )', __( 'Your current balance', 'mycred' ), $mycred->format_creds( $balance ) ); ?></small>
 
 
729
  </td>
730
  </tr>
731
  <?php
 
732
  unset( $available_gateways );
733
 
734
  }
@@ -737,4 +768,7 @@ if ( ! function_exists( 'mycred_woo_after_order_total' ) ) :
737
 
738
  }
739
  endif;
 
 
 
740
  ?>
7
  * Custom Payment Gateway for WooCommerce.
8
  * @see http://docs.woothemes.com/document/payment-gateway-api/
9
  * @since 0.1
10
+ * @version 1.4.3
11
  */
 
12
  if ( ! function_exists( 'mycred_init_woo_gateway' ) ) :
13
  function mycred_init_woo_gateway() {
14
 
23
  */
24
  public function __construct() {
25
 
26
+ $this->id = 'mycred';
27
+ $this->icon = '';
28
+ $this->has_fields = true;
29
+ $this->method_title = __( 'myCRED', 'mycred' );
30
+ $this->method_description = __( 'Let users pay using their myCRED balance.', 'mycred' );
31
+ $this->supports = array(
32
  'products',
33
  'refunds'
34
  );
35
 
36
  $this->mycred_type = $this->get_option( 'point_type' );
37
  if ( $this->mycred_type === NULL || $this->mycred_type == '' )
38
+ $this->mycred_type = MYCRED_DEFAULT_TYPE_KEY;
39
 
40
+ $this->mycred = mycred( $this->mycred_type );
41
 
42
  // Load the settings.
43
  $this->init_form_fields();
120
  'type' => 'select',
121
  'label' => __( 'Select the point type users can use to pay.', 'mycred' ),
122
  'options' => $mycred_types,
123
+ 'default' => MYCRED_DEFAULT_TYPE_KEY
124
  );
125
  }
126
  else {
127
  $fields['point_type'] = array(
128
  'type' => 'hidden',
129
+ 'value' => MYCRED_DEFAULT_TYPE_KEY
130
  );
131
  }
132
 
211
 
212
  ?>
213
  <h3><?php _e( 'myCRED Payment', 'mycred' ); ?></h3>
 
214
  <table class="form-table">
215
  <?php
216
 
231
  function process_payment( $order_id ) {
232
 
233
  global $woocommerce;
 
234
 
235
  // Make sure we are still logged in
236
  if ( ! is_user_logged_in() ) {
238
  return;
239
  }
240
 
241
+ $user_id = get_current_user_id();
242
+
243
  // Make sure we have not been excluded
244
+ if ( $this->mycred->exclude_user( $user_id ) ) {
245
  wc_add_notice( $this->mycred->template_tags_general( __( 'You can not use this gateway. Please try a different payment option.', 'mycred' ) ), 'error' );
246
  return;
247
  }
258
  $cost = apply_filters( 'mycred_woo_order_cost', $cost, $order, false, $this );
259
 
260
  // Check funds
261
+ if ( $this->mycred->get_users_balance( $user_id, $this->mycred_type ) < $cost ) {
262
  wc_add_notice( __( 'Insufficient funds.', 'mycred' ), 'error' );
263
  return;
264
  }
273
  // Charge
274
  $this->mycred->add_creds(
275
  'woocommerce_payment',
276
+ $user_id,
277
+ 0 - $cost,
278
  $this->log_template,
279
  $order_id,
280
  array( 'ref_type' => 'post' ),
281
  $this->mycred_type
282
  );
283
+
284
  $order->payment_complete();
285
 
286
  // Profit Sharing
287
  if ( $this->profit_sharing_percent > 0 ) {
288
+
289
  // Get Items
290
  $items = $order->get_items();
291
 
292
  // Loop though items
293
  foreach ( $items as $item ) {
294
+
295
  // Get Product
296
+ $product = get_post( (int) $item['product_id'] );
297
 
298
  // Continue if product has just been deleted or owner is buyer
299
+ if ( $product === NULL || $product->post_author == $user_id ) continue;
300
 
301
  // Calculate Share
302
  $percentage = apply_filters( 'mycred_woo_profit_share', $this->profit_sharing_percent, $order, $product, $this );
303
  if ( $percentage == 0 ) continue;
304
 
305
+ $share = ( $percentage / 100 ) * $item['line_total'];
306
 
307
  // Payout
308
  $this->mycred->add_creds(
314
  array( 'ref_type' => 'post' ),
315
  $this->mycred_type
316
  );
317
+
318
  }
319
+
320
  }
321
 
322
  // Let others play
323
+ do_action( 'mycred_paid_for_woo', $order, $user_id, $this );
324
 
325
  // Return the good news
326
  return array(
371
 
372
  // Loop though items
373
  foreach ( $items as $item ) {
374
+
375
  // Get Product
376
  $product = get_post( (int) $item['product_id'] );
377
 
380
 
381
  // Calculate Share
382
  // by: Leonie Heinle
383
+ $share = ( $this->profit_sharing_percent / 100 ) * $item['line_total'];
384
 
385
  // Payout
386
  $this->mycred->add_creds(
392
  array( 'ref_type' => 'post', 'order_id' => $order_id ),
393
  $this->mycred_type
394
  );
395
+
396
  }
397
 
398
  }
419
 
420
  }
421
  endif;
422
+ add_action( 'after_setup_theme', 'mycred_init_woo_gateway' );
423
 
424
  /**
425
  * Log Entry: Payment
426
  * @since 0.1
427
  * @version 1.4
428
  */
 
429
  if ( ! function_exists( 'mycred_woo_log_entry_payment' ) ) :
430
  function mycred_woo_log_entry_payment( $content, $log_entry ) {
431
 
448
 
449
  }
450
  endif;
451
+ add_filter( 'mycred_parse_log_entry_woocommerce_payment', 'mycred_woo_log_entry_payment', 90, 2 );
452
 
453
  /**
454
  * Log Entry: Refund
455
  * @since 1.5.4
456
  * @version 1.0
457
  */
 
458
  if ( ! function_exists( 'mycred_woo_log_entry_refunds' ) ) :
459
  function mycred_woo_log_entry_refunds( $content, $log_entry ) {
460
 
461
  $content = mycred_woo_log_entry_payment( $content, $log_entry );
462
 
463
+ $data = maybe_unserialize( $log_entry->data );
464
+ $reason = '-';
465
  if ( isset( $data['reason'] ) && $data['reason'] != '' )
466
  $reason = $data['reason'];
467
 
471
 
472
  }
473
  endif;
474
+ add_filter( 'mycred_parse_log_entry_woocommerce_refund', 'mycred_woo_log_entry_refunds', 90, 2 );
475
 
476
  /**
477
  * Log Entry: Profit Share Refund
478
  * @since 1.5.4
479
  * @version 1.0
480
  */
 
481
  if ( ! function_exists( 'mycred_woo_log_entry_profit_refund' ) ) :
482
  function mycred_woo_log_entry_profit_refund( $content, $log_entry ) {
483
 
484
+ $data = maybe_unserialize( $log_entry->data );
 
485
  $order_id = '';
486
  if ( isset( $data['order_id'] ) && $data['order_id'] != '' )
487
  $order_id = '#' . $data['order_id'];
488
 
489
+ $content = str_replace( '%order_id%', $order_id, $content );
490
 
491
+ $reason = '-';
492
  if ( isset( $data['reason'] ) && $data['reason'] != '' )
493
  $reason = $data['reason'];
494
 
495
+ $content = str_replace( '%reason%', $reason, $content );
496
 
497
  return $content;
498
 
499
  }
500
  endif;
501
+ add_filter( 'mycred_parse_log_entry_store_sale_refund', 'mycred_woo_log_entry_profit_refund', 90, 2 );
502
 
503
  /**
504
  * Parse Email Notice
505
  * @since 1.2.2
506
  * @version 1.0.2
507
  */
 
508
  if ( ! function_exists( 'mycred_woo_parse_email' ) ) :
509
  function mycred_woo_parse_email( $email ) {
510
 
511
  if ( $email['request']['ref'] == 'woocommerce_payment' && function_exists( 'woocommerce_get_page_id' ) ) {
512
+
513
  if ( function_exists( 'wc_get_order' ) )
514
  $order = wc_get_order( (int) $email['request']['ref_id'] );
515
  else
516
  $order = new WC_Order( (int) $email['request']['ref_id'] );
517
 
518
  if ( isset( $order->id ) ) {
 
519
 
520
+ $url = esc_url( add_query_arg( 'order', $order->id, get_permalink( woocommerce_get_page_id( 'view_order' ) ) ) );
521
  $content = str_replace( '%order_id%', $order->id, $email['request']['entry'] );
522
+
523
  $email['request']['entry'] = str_replace( '%order_link%', '<a href="' . esc_url( $url ) . '">#' . $order->id . '</a>', $content );
524
+
525
  }
526
 
527
  }
528
+
529
  return $email;
530
 
531
  }
532
  endif;
533
+ add_filter( 'mycred_email_before_send', 'mycred_woo_parse_email', 10 );
534
 
535
  /**
536
  * Register Gateway
537
  * @since 0.1
538
  * @version 1.0
539
  */
 
540
  if ( ! function_exists( 'mycred_register_woo_gateway' ) ) :
541
  function mycred_register_woo_gateway( $methods ) {
542
 
545
 
546
  }
547
  endif;
548
+ add_filter( 'woocommerce_payment_gateways', 'mycred_register_woo_gateway' );
549
 
550
  /**
551
  * Available Gateways
557
  * @since 0.1
558
  * @version 1.2.1
559
  */
 
560
  if ( ! function_exists( 'mycred_woo_available_gateways' ) ) :
561
  function mycred_woo_available_gateways( $gateways ) {
562
 
567
 
568
  // Check if we are logged in
569
  if ( ! is_user_logged_in() ) {
570
+
571
  unset( $gateways['mycred'] );
572
+
573
  return $gateways;
574
+
575
  }
576
 
577
+ $type = $gateways['mycred']->get_option( 'point_type' );
578
  if ( $type === NULL )
579
+ $type = MYCRED_DEFAULT_TYPE_KEY;
580
 
581
  // Get myCRED
582
+ $mycred = mycred( $type );
583
+ $user_id = get_current_user_id();
584
 
585
  // Check if we are excluded from myCRED usage
586
+ if ( $mycred->exclude_user( $user_id ) ) {
587
+
588
  unset( $gateways['mycred'] );
589
  unset( $mycred );
590
+
591
  return $gateways;
592
+
593
  }
594
 
595
  global $woocommerce;
601
  else
602
  $cost = $woocommerce->cart->total;
603
 
604
+ $cost = apply_filters( 'mycred_woo_order_cost', $cost, $woocommerce->cart, true, $mycred );
605
 
606
  // Check if we have enough points
607
+ if ( $mycred->get_users_balance( $user_id, $type ) < $cost ) {
608
  $gateways['mycred']->enabled = 'no';
609
  }
610
 
611
  // Clean up and return
612
  unset( $mycred );
613
+
614
  return $gateways;
615
 
616
  }
617
  endif;
618
+ add_filter( 'woocommerce_available_payment_gateways', 'mycred_woo_available_gateways' );
619
 
620
  /**
621
  * Add Currency
623
  * @since 0.1
624
  * @version 1.1
625
  */
 
626
  if ( ! function_exists( 'mycred_woo_add_currency' ) ) :
627
  function mycred_woo_add_currency( $currencies ) {
628
 
629
  $settings = get_option( 'woocommerce_mycred_settings', false );
630
  if ( $settings === false ) return $currencies;
631
 
632
+ $type = MYCRED_DEFAULT_TYPE_KEY;
633
  if ( isset( $settings['point_type'] ) && ! empty( $settings['point_type'] ) )
634
  $type = $settings['point_type'];
635
 
636
+ $mycred = mycred( $type );
637
+
638
  $currencies['MYC'] = $mycred->plural();
639
 
640
  unset( $mycred );
641
+
642
  return $currencies;
643
 
644
  }
645
  endif;
646
+ add_filter( 'woocommerce_currencies', 'mycred_woo_add_currency' );
647
 
648
  /**
649
  * Currency Symbol
651
  * @since 0.1
652
  * @version 1.1
653
  */
 
654
  if ( ! function_exists( 'mycred_woo_currency' ) ) :
655
  function mycred_woo_currency( $currency_symbol, $currency ) {
656
 
657
  $settings = get_option( 'woocommerce_mycred_settings', false );
658
  if ( $settings === false ) return $currency_symbol;
659
 
660
+ $type = MYCRED_DEFAULT_TYPE_KEY;
661
  if ( isset( $settings['point_type'] ) && ! empty( $settings['point_type'] ) )
662
  $type = $settings['point_type'];
663
 
664
  switch ( $currency ) {
665
  case 'MYC':
666
+
667
  $mycred = mycred( $type );
668
+
669
  if ( ! empty( $mycred->before ) )
670
  $currency_symbol = $mycred->before;
671
+
672
  elseif ( ! empty( $mycred->after ) )
673
  $currency_symbol = $mycred->after;
674
+
675
  break;
676
  }
677
+
678
  return $currency_symbol;
679
 
680
  }
681
  endif;
682
+ add_filter( 'woocommerce_currency_symbol', 'mycred_woo_currency', 10, 2 );
683
 
684
  /**
685
  * Add CRED Cost
687
  * @since 0.1
688
  * @version 1.2.1
689
  */
 
 
690
  if ( ! function_exists( 'mycred_woo_after_order_total' ) ) :
691
  function mycred_woo_after_order_total() {
692
 
698
  $available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
699
  if ( ! isset( $available_gateways['mycred'] ) ) return;
700
 
701
+ $type = $available_gateways['mycred']->get_option( 'point_type' );
702
  if ( $type === NULL )
703
+ $type = MYCRED_DEFAULT_TYPE_KEY;
704
 
705
+ $mycred = mycred( $type );
706
+ $user_id = get_current_user_id();
707
 
708
+ if ( $mycred->exclude_user( $user_id ) ) return;
709
 
710
  // Check if enabled
711
  $show = $available_gateways['mycred']->get_option( 'show_total' );
712
+
713
  if ( empty( $show ) ) return;
714
  elseif ( $show === 'cart' && ! is_cart() ) return;
715
  elseif ( $show === 'checkout' && ! is_checkout() ) return;
720
 
721
  // Apply Exchange Rate
722
  $cost = $mycred->number( ( $woocommerce->cart->total / $available_gateways['mycred']->get_option( 'exchange_rate' ) ) );
723
+ $cost = apply_filters( 'mycred_woo_order_cost', $cost, $woocommerce->cart, true, $mycred );
724
 
725
+ ?>
 
726
  <tr class="total">
727
  <th><strong><?php echo $mycred->template_tags_general( $available_gateways['mycred']->get_option( 'total_label' ) ); ?></strong></th>
728
  <td>
729
+ <?php
730
 
731
  // Balance
732
+ $balance = $mycred->get_users_balance( $user_id, $type );
733
 
734
  // Insufficient Funds
735
+ if ( $balance < $cost ) {
736
 
737
+ ?>
738
  <strong class="mycred-low-funds" style="color:red;"><?php echo $mycred->format_creds( $cost ); ?></strong>
739
  <?php
740
+
741
  }
742
+
743
  // Funds exist
744
+ else {
745
 
746
+ ?>
747
  <strong class="mycred-funds"><?php echo $mycred->format_creds( $cost ); ?></strong>
748
  <?php
 
749
 
750
+ }
751
+
752
+ ?>
753
  <small class="mycred-current-balance"><?php
754
 
755
  // Current balance
756
+ echo sprintf( '( %1s: %2s )', __( 'Your current balance', 'mycred' ), $mycred->format_creds( $balance ) );
757
+
758
+ ?></small>
759
  </td>
760
  </tr>
761
  <?php
762
+
763
  unset( $available_gateways );
764
 
765
  }
768
 
769
  }
770
  endif;
771
+ add_action( 'woocommerce_review_order_after_order_total', 'mycred_woo_after_order_total' );
772
+ add_action( 'woocommerce_cart_totals_after_order_total', 'mycred_woo_after_order_total' );
773
+
774
  ?>
addons/gateway/carts/mycred-wpecommerce.php CHANGED
@@ -9,23 +9,16 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
9
  * @since 1.3
10
  * @version 1.1
11
  */
12
- add_action( 'after_setup_theme', 'mycred_init_wpecom_construct_gateway' );
13
- if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
14
-
15
- /**
16
- * Construct Gateway
17
- * @since 1.3
18
- * @version 1.0
19
- */
20
- function mycred_init_wpecom_construct_gateway()
21
- {
22
  if ( ! class_exists( 'wpsc_merchant' ) ) return;
23
 
24
  global $nzshpcrt_gateways, $mycred_wpecom_settings;
25
 
26
  $mycred_wpecom_settings = shortcode_atts( array(
27
  'log' => __( 'Payment for Order: #%order_id%', 'mycred' ),
28
- 'type' => 'mycred_default',
29
  'share' => 0,
30
  'share_log' => __( 'Store sale', 'mycred' ),
31
  'rate' => 1,
@@ -50,28 +43,29 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
50
 
51
  class myCRED_WP_E_Commerce_Gateway {
52
 
53
- public $core = '';
54
- public $prefs = array();
55
- public $mycred_type = 'mycred_default';
56
 
57
  /**
58
  * Construct
59
  */
60
  function __construct() {
 
61
  global $mycred_wpecom_settings;
62
 
63
  $this->prefs = $mycred_wpecom_settings;
64
-
65
- $type = 'mycred_default';
66
  if ( isset( $mycred_wpecom_settings['type'] ) )
67
  $type = $mycred_wpecom_settings['type'];
68
-
69
- $this->core = mycred( $type );
70
  $this->mycred_type = $type;
71
 
72
  add_action( 'wpsc_submit_checkout_gateway', array( $this, 'process_gateway' ), 1, 2 );
73
  add_filter( 'wpsc_gateway_checkout_form_mycred', array( $this, 'checkout_form' ) );
74
  add_filter( 'mycred_parse_log_entry_wpecom_payment', array( $this, 'parse_template_tags' ), 10, 2 );
 
75
  }
76
 
77
  /**
@@ -80,6 +74,7 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
80
  * @version 1.1
81
  */
82
  function process_gateway( $gateway, $purchase_log ) {
 
83
  if ( $gateway != 'mycred' ) return;
84
 
85
  // Prep
@@ -95,6 +90,7 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
95
  // Charge
96
  do_action_ref_array( 'wpsc_pre_submit_gateway', array( &$merchant_instance ) );
97
  $merchant_instance->submit();
 
98
  }
99
 
100
  /**
@@ -103,25 +99,26 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
103
  * @version 1.1
104
  */
105
  function checkout_form() {
106
- $output = '';
107
- if ( ! is_user_logged_in() ) {
108
- $output .= '<tr><td>' . $this->core->template_tags_general( $this->prefs['visitor'] ) . '</td></tr>';
109
- return $output;
110
- }
111
 
112
  $output .= '<tr><td><table width="100%"><thead><th class="cart-item">' . __( 'Item', 'mycred' ) . '</th><th class="cart-item-qt"></th><th class="cart-item-cost">' . $this->core->plural() . '</th></thead><tbody>';
113
 
114
- $total = 0;
115
  while ( wpsc_have_cart_items() ) : wpsc_the_cart_item();
 
116
  $price = wpsc_cart_item_price( false );
117
- if ( $this->prefs['rate'] != 1 ) {
118
  $price = $this->prefs['rate']*$price;
119
- }
120
  $total = $total+$price;
 
121
  endwhile;
122
 
123
  $output .= '<tr><td colspan="2">' . __( 'Total Cost', 'mycred' ) . '</td><td class="cart-item-cost">' . $this->core->format_creds( $total ) . '</td></tr>';
124
- $balance = $this->core->get_users_cred( get_current_user_id(), $this->mycred_type );
125
 
126
  if ( $balance < $total )
127
  $highlight = ' style="color:red;"';
@@ -130,11 +127,11 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
130
 
131
  $output .= '<tr><td class="cart-item" colspan="2">' . __( 'Your current balance', 'mycred' ) . '</td><td class="cart-item-cost"' . $highlight . '>' . $this->core->format_creds( $balance ) . '</td></tr></tdody></table></tr>';
132
 
133
- if ( ! empty( $this->prefs['message'] ) ) {
134
  $output .= '<tr><td>' . $this->core->template_tags_general( $this->prefs['message'] ) . '</td></tr>';
135
- }
136
 
137
  return apply_filters( 'mycred_wpecom_form', $output );
 
138
  }
139
 
140
  /**
@@ -143,32 +140,38 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
143
  * @version 1.0
144
  */
145
  function parse_template_tags( $content, $log_entry ) {
 
146
  if ( ! empty( $log_entry->data ) )
147
  $content = str_replace( '%order_id%', $log_entry->data, $content );
148
  else
149
  $content = str_replace( '%order_id%', 'missing', $content );
150
 
151
  return $content;
 
152
  }
 
153
  }
 
154
  new myCRED_WP_E_Commerce_Gateway();
155
 
156
  class wpsc_merchant_mycred extends wpsc_merchant {
157
 
158
- var $prefs = array();
159
- var $core = '';
160
- var $mycred_type = 'mycred_default';
161
- var $cost = 0;
162
  var $transaction_id = '';
163
 
164
  /**
165
  * Construct
166
  */
167
- function __construct( $purchase_id = NULL, $is_receiving = false, $prefs = NULL, $mycred = NULL, $type = 'mycred_default' ) {
 
168
  parent::__construct( $purchase_id, $is_receiving );
169
- $this->prefs = $prefs;
170
- $this->core = $mycred;
171
  $this->mycred_type = $type;
 
172
  }
173
 
174
  /**
@@ -178,50 +181,58 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
178
  * @version 1.1
179
  */
180
  function validate( $purchase_log ) {
181
- $error = false;
182
- $user_id = get_current_user_id();
 
183
 
184
  // Get cost
185
  $cart_total = $this->cart_data['total_price'];
186
- if ( $this->prefs['rate'] != 1 ) {
187
  $cart_total = $this->prefs['rate']*$cart_total;
188
- }
189
  $cart_total = $this->core->number( $cart_total );
190
  $this->cost = $cart_total;
191
 
192
  // User is not logged in
193
- if ( ! is_user_logged_in() ) {
194
  $error = $this->core->template_tags_general( $this->prefs['visitor'] );
195
- }
196
 
197
  // Else if user is excluded
198
- elseif ( $this->core->exclude_user( $user_id ) ) {
199
  $error = __( 'You can not use this gateway.', 'mycred' );
200
- }
201
 
202
  // Else check balance
203
  else {
 
204
  // Rate
205
- $balance = $this->core->get_users_cred( $user_id, $this->mycred_type );
206
  if ( $balance < $this->cost ) {
207
  $error = $this->core->template_tags_general( $this->prefs['low_funds'] );
208
  }
 
209
  }
210
 
211
  // Let others decline a store order
212
  $decline = apply_filters( 'mycred_decline_store_purchase', $error, $purchase_log, $this );
213
  if ( $decline !== false ) {
 
214
  wpsc_delete_customer_meta( 'selected_gateway' );
 
215
  $this->set_error_message( $decline );
216
  $purchase_log->delete( $this->purchase_id );
 
217
  unset( $_SESSION['WpscGatewayErrorMessage'] );
 
218
  $this->return_to_checkout();
 
219
  exit;
 
220
  }
221
 
222
  // Prep for payment
223
- $this->user_id = $user_id;
224
- $this->transaction_id = 'MYCRED' . $user_id . time();
 
225
  }
226
 
227
  /**
@@ -232,8 +243,10 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
232
  * @version 1.3
233
  */
234
  function submit() {
 
235
  // Since the wpsc_pre_submit_gateway action could change these values, we need to check
236
  if ( $this->cost > 0 && $this->user_id != 0 && !empty( $this->transaction_id ) ) {
 
237
  // Let other play before we start
238
  do_action_ref_array( 'mycred_wpecom_charg', array( &$this ) );
239
 
@@ -254,24 +267,26 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
254
 
255
  // Payout Share
256
  if ( $this->prefs['share'] > 0 ) {
 
257
  // Loop though items
258
  foreach ( (array) $this->cart_items as $item ) {
 
259
  // Get product
260
- $product = get_post( (int) $item['product_id'] );
261
 
262
  // Continue if product has just been deleted or owner is buyer
263
  if ( $product === NULL || $product->post_author == $this->user_id ) continue;
264
 
265
  // Calculate Cost
266
- $price = $item['price'];
267
  $quantity = $item['quantity'];
268
- $cost = $price*$quantity;
269
 
270
  // Calculate Share
271
  $percentage = apply_filters( 'mycred_wpecom_profit_share', $this->prefs['share'], $this, $product );
272
  if ( $percentage == 0 ) continue;
273
 
274
- $share = ( $percentage / 100 ) * $cost;
275
 
276
  // Payout
277
  $this->core->add_creds(
@@ -283,7 +298,9 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
283
  array( 'ref_type' => 'post' ),
284
  $this->mycred_type
285
  );
 
286
  }
 
287
  }
288
 
289
  // Let others play before we end
@@ -291,18 +308,27 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
291
 
292
  // Empty Cart, Redirect & Exit
293
  wpsc_empty_cart();
 
294
  $this->go_to_transaction_results( $this->cart_data['session_id'] );
 
295
  exit;
 
296
  }
297
 
298
  // Else save this as pending
299
  elseif ( ! empty( $this->transaction_id ) ) {
 
300
  $this->set_transaction_details( $this->transaction_id, 2 );
 
301
  }
 
302
  }
 
303
  }
 
304
  }
305
- }
 
306
 
307
  /**
308
  * Gateway Settings
@@ -310,86 +336,90 @@ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) {
310
  * @since 1.3
311
  * @version 1.1
312
  */
313
- if ( ! function_exists( 'mycred_wpecom_gateway_settings' ) ) {
314
  function mycred_wpecom_gateway_settings() {
 
315
  global $wpdb, $mycred_wpecom_settings;
316
 
317
  if ( ! isset( $mycred_wpecom_settings['type'] ) )
318
- $type = 'mycred_default';
319
  else
320
  $type = $mycred_wpecom_settings['type'];
321
 
322
  $mycred = mycred( $type );
323
 
324
  // Get current currency
325
- $currency_data = $wpdb->get_results( "SELECT * FROM `" . WPSC_TABLE_CURRENCY_LIST . "` ORDER BY `country` ASC", ARRAY_A );
326
  $selected_currency = esc_attr( get_option( 'currency_type' ) );
 
327
  foreach ( $currency_data as $currency ) {
 
328
  if ( $selected_currency != $currency['id'] ) continue;
329
- else {
330
- if ( ! empty( $currency['symbol_html'] ) )
331
- $selected_currency = $currency['symbol_html'];
332
- else
333
- $selected_currency = $currency['code'];
334
 
335
- break;
336
- }
 
 
 
 
 
337
  }
338
 
339
  $output = '
340
- <tr>
341
- <td colspan="2"><strong>' . __( 'General', 'mycred' ) . '</strong></td>
342
- </tr>
343
- <tr>
344
- <td width="150">' . __( 'Log Template for Payments', 'mycred' ) . '</td>
345
- <td><input type="text" name="mycred_gateway[log]" value="' . esc_attr( $mycred_wpecom_settings['log'] ) . '" style="width:50%;" /><br /><span class="description">' . $mycred->available_template_tags( array( 'general' ), '%order_id%' ) . '</span></td>
346
- </tr>';
347
 
348
  $mycred_types = mycred_get_types();
349
  if ( count( $mycred_types ) == 1 )
350
  $output .= '<input type="hidden" name="mycred_gateway[type]" value="mycred_default" />';
351
  else
352
  $output .= '
353
- <tr>
354
- <td width="150">' . __( 'Point Type', 'mycred' ) . '</td>
355
- <td>' . mycred_types_select_from_dropdown( 'mycred_gateway[type]', 'mycred-point-type', $type, true ) . '</td>
356
- </tr>';
357
 
358
  $output .= '
359
- <tr>
360
- <td width="150">' . __( 'Exchange Rate', 'mycred' ) . '</td>
361
- <td><input type="text" name="mycred_gateway[rate]" value="' . esc_attr( $mycred_wpecom_settings['rate'] ) . '" style="width:50px;" /><br /><span class="description">' . sprintf( __( 'How much is 1 %s worth in %s', 'mycred' ), $selected_currency, $mycred->plural() ) . '</span></td>
362
- </tr>
363
- <tr>
364
- <td colspan="2"><strong>' . __( 'Profit Sharing', 'mycred' ) . '</strong></td>
365
- </tr>
366
- <tr>
367
- <td width="150">' . __( 'Payout', 'mycred' ) . '</td>
368
- <td><input type="text" name="mycred_gateway[share]" value="' . esc_attr( $mycred_wpecom_settings['share'] ) . '" style="width:50px;" /> %<br /><span class="description">' . __( 'Option to share sales with the product owner. Use zero to disable.', 'mycred' ) . '</span></td>
369
- </tr>
370
- <tr>
371
- <td width="150">' . __( 'Log Template', 'mycred' ) . '</td>
372
- <td><input type="text" name="mycred_gateway[share_log]" value="' . esc_attr( $mycred_wpecom_settings['share_log'] ) . '" style="width:50%;" /><br /><span class="description">' . $mycred->available_template_tags( array( 'general', 'post' ) ) . '</span></td>
373
- </tr>
374
- <tr>
375
- <td colspan="2"><strong>' . __( 'Messages', 'mycred' ) . '</strong></td>
376
- </tr>
377
- <tr>
378
- <td width="150">' . __( 'Instructions', 'mycred' ) . '</td>
379
- <td><input type="text" name="mycred_gateway[message]" value="' . esc_attr( $mycred_wpecom_settings['message'] ) . '" style="width:50%;" /><br /><span class="description">' . __( 'Optional instructions to show users when selecting this gateway. Leave empty to hide.', 'mycred' ) . '</span></td>
380
- </tr>
381
- <tr>
382
- <td width="150">' . __( 'Visitors', 'mycred' ) . '</td>
383
- <td><input type="text" name="mycred_gateway[visitor]" value="' . esc_attr( $mycred_wpecom_settings['visitor'] ) . '" style="width:50%;" /><br /><span class="description">' . __( 'Message to show visitors who are not logged in.', 'mycred' ) . '</span></td>
384
- </tr>
385
- <tr>
386
- <td width="150">' . __( 'Insufficient Funds', 'mycred' ) . '</td>
387
- <td><input type="text" name="mycred_gateway[low_funds]" value="' . esc_attr( $mycred_wpecom_settings['low_funds'] ) . '" style="width:50%;" /><br /><span class="description">' . $mycred->template_tags_general( __( 'Message to show when users does not have enough %plural% to pay using this gateway.', 'mycred' ) ) . '</span></td>
388
- </tr>';
389
 
390
  return apply_filters( 'mycred_wpecom_settings', $output );
 
391
  }
392
- }
393
 
394
  /**
395
  * Save Gateway Settings
@@ -397,38 +427,43 @@ if ( ! function_exists( 'mycred_wpecom_gateway_settings' ) ) {
397
  * @since 1.3
398
  * @version 1.1
399
  */
400
- if ( ! function_exists( 'mycred_wpecom_gateway_settings_save' ) ) {
401
  function mycred_wpecom_gateway_settings_save() {
 
402
  if ( isset( $_POST['mycred_gateway'] ) ) {
403
  $new_settings = apply_filters( 'mycred_wpecom_save_settings', array(
404
- 'log' => trim( $_POST['mycred_gateway']['log'] ),
405
- 'type' => trim( $_POST['mycred_gateway']['type'] ),
406
  'share' => abs( $_POST['mycred_gateway']['share'] ),
407
- 'share_log' => trim( $_POST['mycred_gateway']['share_log'] ),
408
- 'rate' => trim( $_POST['mycred_gateway']['rate'] ),
409
  'visitor' => sanitize_text_field( $_POST['mycred_gateway']['visitor'] ),
410
  'low_funds' => sanitize_text_field( $_POST['mycred_gateway']['low_funds'] ),
411
  'message' => sanitize_text_field( $_POST['mycred_gateway']['message'] )
412
  ) );
413
  update_option( 'mycred_wpecom_settings', $new_settings );
414
  }
 
415
  return true;
 
416
  }
417
- }
418
 
419
  /**
420
  * Parse Email Notice
421
  * @since 1.2.2
422
  * @version 1.0
423
  */
424
- if ( ! function_exists( 'mycred_wpecom_parse_email' ) ) {
425
- add_filter( 'mycred_email_before_send', 'mycred_wpecom_parse_email' );
426
- function mycred_wpecom_parse_email( $email )
427
- {
428
- if ( $email['request']['ref'] == 'wpecom_payment' ) {
429
  $email['request']['entry'] = str_replace( '%order_id%', $email['request']['data'], $email['request']['entry'] );
430
- }
431
  return $email;
 
432
  }
433
- }
 
 
434
  ?>
9
  * @since 1.3
10
  * @version 1.1
11
  */
12
+ if ( ! function_exists( 'mycred_init_wpecom_construct_gateway' ) ) :
13
+ function mycred_init_wpecom_construct_gateway() {
14
+
 
 
 
 
 
 
 
15
  if ( ! class_exists( 'wpsc_merchant' ) ) return;
16
 
17
  global $nzshpcrt_gateways, $mycred_wpecom_settings;
18
 
19
  $mycred_wpecom_settings = shortcode_atts( array(
20
  'log' => __( 'Payment for Order: #%order_id%', 'mycred' ),
21
+ 'type' => MYCRED_DEFAULT_TYPE_KEY,
22
  'share' => 0,
23
  'share_log' => __( 'Store sale', 'mycred' ),
24
  'rate' => 1,
43
 
44
  class myCRED_WP_E_Commerce_Gateway {
45
 
46
+ public $core = '';
47
+ public $prefs = array();
48
+ public $mycred_type = MYCRED_DEFAULT_TYPE_KEY;
49
 
50
  /**
51
  * Construct
52
  */
53
  function __construct() {
54
+
55
  global $mycred_wpecom_settings;
56
 
57
  $this->prefs = $mycred_wpecom_settings;
58
+ $type = MYCRED_DEFAULT_TYPE_KEY;
 
59
  if ( isset( $mycred_wpecom_settings['type'] ) )
60
  $type = $mycred_wpecom_settings['type'];
61
+
62
+ $this->core = mycred( $type );
63
  $this->mycred_type = $type;
64
 
65
  add_action( 'wpsc_submit_checkout_gateway', array( $this, 'process_gateway' ), 1, 2 );
66
  add_filter( 'wpsc_gateway_checkout_form_mycred', array( $this, 'checkout_form' ) );
67
  add_filter( 'mycred_parse_log_entry_wpecom_payment', array( $this, 'parse_template_tags' ), 10, 2 );
68
+
69
  }
70
 
71
  /**
74
  * @version 1.1
75
  */
76
  function process_gateway( $gateway, $purchase_log ) {
77
+
78
  if ( $gateway != 'mycred' ) return;
79
 
80
  // Prep
90
  // Charge
91
  do_action_ref_array( 'wpsc_pre_submit_gateway', array( &$merchant_instance ) );
92
  $merchant_instance->submit();
93
+
94
  }
95
 
96
  /**
99
  * @version 1.1
100
  */
101
  function checkout_form() {
102
+
103
+ $output = '';
104
+ if ( ! is_user_logged_in() )
105
+ return '<tr><td>' . $this->core->template_tags_general( $this->prefs['visitor'] ) . '</td></tr>';
 
106
 
107
  $output .= '<tr><td><table width="100%"><thead><th class="cart-item">' . __( 'Item', 'mycred' ) . '</th><th class="cart-item-qt"></th><th class="cart-item-cost">' . $this->core->plural() . '</th></thead><tbody>';
108
 
109
+ $total = 0;
110
  while ( wpsc_have_cart_items() ) : wpsc_the_cart_item();
111
+
112
  $price = wpsc_cart_item_price( false );
113
+ if ( $this->prefs['rate'] != 1 )
114
  $price = $this->prefs['rate']*$price;
115
+
116
  $total = $total+$price;
117
+
118
  endwhile;
119
 
120
  $output .= '<tr><td colspan="2">' . __( 'Total Cost', 'mycred' ) . '</td><td class="cart-item-cost">' . $this->core->format_creds( $total ) . '</td></tr>';
121
+ $balance = $this->core->get_users_balance( get_current_user_id(), $this->mycred_type );
122
 
123
  if ( $balance < $total )
124
  $highlight = ' style="color:red;"';
127
 
128
  $output .= '<tr><td class="cart-item" colspan="2">' . __( 'Your current balance', 'mycred' ) . '</td><td class="cart-item-cost"' . $highlight . '>' . $this->core->format_creds( $balance ) . '</td></tr></tdody></table></tr>';
129
 
130
+ if ( ! empty( $this->prefs['message'] ) )
131
  $output .= '<tr><td>' . $this->core->template_tags_general( $this->prefs['message'] ) . '</td></tr>';
 
132
 
133
  return apply_filters( 'mycred_wpecom_form', $output );
134
+
135
  }
136
 
137
  /**
140
  * @version 1.0
141
  */
142
  function parse_template_tags( $content, $log_entry ) {
143
+
144
  if ( ! empty( $log_entry->data ) )
145
  $content = str_replace( '%order_id%', $log_entry->data, $content );
146
  else
147
  $content = str_replace( '%order_id%', 'missing', $content );
148
 
149
  return $content;
150
+
151
  }
152
+
153
  }
154
+
155
  new myCRED_WP_E_Commerce_Gateway();
156
 
157
  class wpsc_merchant_mycred extends wpsc_merchant {
158
 
159
+ var $prefs = array();
160
+ var $core = '';
161
+ var $mycred_type = MYCRED_DEFAULT_TYPE_KEY;
162
+ var $cost = 0;
163
  var $transaction_id = '';
164
 
165
  /**
166
  * Construct
167
  */
168
+ function __construct( $purchase_id = NULL, $is_receiving = false, $prefs = NULL, $mycred = NULL, $type = MYCRED_DEFAULT_TYPE_KEY ) {
169
+
170
  parent::__construct( $purchase_id, $is_receiving );
171
+ $this->prefs = $prefs;
172
+ $this->core = $mycred;
173
  $this->mycred_type = $type;
174
+
175
  }
176
 
177
  /**
181
  * @version 1.1
182
  */
183
  function validate( $purchase_log ) {
184
+
185
+ $error = false;
186
+ $user_id = get_current_user_id();
187
 
188
  // Get cost
189
  $cart_total = $this->cart_data['total_price'];
190
+ if ( $this->prefs['rate'] != 1 )
191
  $cart_total = $this->prefs['rate']*$cart_total;
192
+
193
  $cart_total = $this->core->number( $cart_total );
194
  $this->cost = $cart_total;
195
 
196
  // User is not logged in
197
+ if ( ! is_user_logged_in() )
198
  $error = $this->core->template_tags_general( $this->prefs['visitor'] );
 
199
 
200
  // Else if user is excluded
201
+ elseif ( $this->core->exclude_user( $user_id ) )
202
  $error = __( 'You can not use this gateway.', 'mycred' );
 
203
 
204
  // Else check balance
205
  else {
206
+
207
  // Rate
208
+ $balance = $this->core->get_users_balance( $user_id, $this->mycred_type );
209
  if ( $balance < $this->cost ) {
210
  $error = $this->core->template_tags_general( $this->prefs['low_funds'] );
211
  }
212
+
213
  }
214
 
215
  // Let others decline a store order
216
  $decline = apply_filters( 'mycred_decline_store_purchase', $error, $purchase_log, $this );
217
  if ( $decline !== false ) {
218
+
219
  wpsc_delete_customer_meta( 'selected_gateway' );
220
+
221
  $this->set_error_message( $decline );
222
  $purchase_log->delete( $this->purchase_id );
223
+
224
  unset( $_SESSION['WpscGatewayErrorMessage'] );
225
+
226
  $this->return_to_checkout();
227
+
228
  exit;
229
+
230
  }
231
 
232
  // Prep for payment
233
+ $this->user_id = $user_id;
234
+ $this->transaction_id = strtoupper( MYCRED_SLUG ) . $user_id . time();
235
+
236
  }
237
 
238
  /**
243
  * @version 1.3
244
  */
245
  function submit() {
246
+
247
  // Since the wpsc_pre_submit_gateway action could change these values, we need to check
248
  if ( $this->cost > 0 && $this->user_id != 0 && !empty( $this->transaction_id ) ) {
249
+
250
  // Let other play before we start
251
  do_action_ref_array( 'mycred_wpecom_charg', array( &$this ) );
252
 
267
 
268
  // Payout Share
269
  if ( $this->prefs['share'] > 0 ) {
270
+
271
  // Loop though items
272
  foreach ( (array) $this->cart_items as $item ) {
273
+
274
  // Get product
275
+ $product = get_post( (int) $item['product_id'] );
276
 
277
  // Continue if product has just been deleted or owner is buyer
278
  if ( $product === NULL || $product->post_author == $this->user_id ) continue;
279
 
280
  // Calculate Cost
281
+ $price = $item['price'];
282
  $quantity = $item['quantity'];
283
+ $cost = $price*$quantity;
284
 
285
  // Calculate Share
286
  $percentage = apply_filters( 'mycred_wpecom_profit_share', $this->prefs['share'], $this, $product );
287
  if ( $percentage == 0 ) continue;
288
 
289
+ $share = ( $percentage / 100 ) * $cost;
290
 
291
  // Payout
292
  $this->core->add_creds(
298
  array( 'ref_type' => 'post' ),
299
  $this->mycred_type
300
  );
301
+
302
  }
303
+
304
  }
305
 
306
  // Let others play before we end
308
 
309
  // Empty Cart, Redirect & Exit
310
  wpsc_empty_cart();
311
+
312
  $this->go_to_transaction_results( $this->cart_data['session_id'] );
313
+
314
  exit;
315
+
316
  }
317
 
318
  // Else save this as pending
319
  elseif ( ! empty( $this->transaction_id ) ) {
320
+
321
  $this->set_transaction_details( $this->transaction_id, 2 );
322
+
323
  }
324
+
325
  }
326
+
327
  }
328
+
329
  }
330
+ endif;
331
+ add_action( 'after_setup_theme', 'mycred_init_wpecom_construct_gateway' );
332
 
333
  /**
334
  * Gateway Settings
336
  * @since 1.3
337
  * @version 1.1
338
  */
339
+ if ( ! function_exists( 'mycred_wpecom_gateway_settings' ) ) :
340
  function mycred_wpecom_gateway_settings() {
341
+
342
  global $wpdb, $mycred_wpecom_settings;
343
 
344
  if ( ! isset( $mycred_wpecom_settings['type'] ) )
345
+ $type = MYCRED_DEFAULT_TYPE_KEY;
346
  else
347
  $type = $mycred_wpecom_settings['type'];
348
 
349
  $mycred = mycred( $type );
350
 
351
  // Get current currency
352
+ $currency_data = $wpdb->get_results( "SELECT * FROM `" . WPSC_TABLE_CURRENCY_LIST . "` ORDER BY `country` ASC", ARRAY_A );
353
  $selected_currency = esc_attr( get_option( 'currency_type' ) );
354
+
355
  foreach ( $currency_data as $currency ) {
356
+
357
  if ( $selected_currency != $currency['id'] ) continue;
 
 
 
 
 
358
 
359
+ if ( ! empty( $currency['symbol_html'] ) )
360
+ $selected_currency = $currency['symbol_html'];
361
+ else
362
+ $selected_currency = $currency['code'];
363
+
364
+ break;
365
+
366
  }
367
 
368
  $output = '
369
+ <tr>
370
+ <td colspan="2"><strong>' . __( 'General', 'mycred' ) . '</strong></td>
371
+ </tr>
372
+ <tr>
373
+ <td width="150">' . __( 'Log Template for Payments', 'mycred' ) . '</td>
374
+ <td><input type="text" name="mycred_gateway[log]" value="' . esc_attr( $mycred_wpecom_settings['log'] ) . '" style="width:50%;" /><br /><span class="description">' . $mycred->available_template_tags( array( 'general' ), '%order_id%' ) . '</span></td>
375
+ </tr>';
376
 
377
  $mycred_types = mycred_get_types();
378
  if ( count( $mycred_types ) == 1 )
379
  $output .= '<input type="hidden" name="mycred_gateway[type]" value="mycred_default" />';
380
  else
381
  $output .= '
382
+ <tr>
383
+ <td width="150">' . __( 'Point Type', 'mycred' ) . '</td>
384
+ <td>' . mycred_types_select_from_dropdown( 'mycred_gateway[type]', 'mycred-point-type', $type, true ) . '</td>
385
+ </tr>';
386
 
387
  $output .= '
388
+ <tr>
389
+ <td width="150">' . __( 'Exchange Rate', 'mycred' ) . '</td>
390
+ <td><input type="text" name="mycred_gateway[rate]" value="' . esc_attr( $mycred_wpecom_settings['rate'] ) . '" style="width:50px;" /><br /><span class="description">' . sprintf( __( 'How much is 1 %s worth in %s', 'mycred' ), $selected_currency, $mycred->plural() ) . '</span></td>
391
+ </tr>
392
+ <tr>
393
+ <td colspan="2"><strong>' . __( 'Profit Sharing', 'mycred' ) . '</strong></td>
394
+ </tr>
395
+ <tr>
396
+ <td width="150">' . __( 'Payout', 'mycred' ) . '</td>
397
+ <td><input type="text" name="mycred_gateway[share]" value="' . esc_attr( $mycred_wpecom_settings['share'] ) . '" style="width:50px;" /> %<br /><span class="description">' . __( 'Option to share sales with the product owner. Use zero to disable.', 'mycred' ) . '</span></td>
398
+ </tr>
399
+ <tr>
400
+ <td width="150">' . __( 'Log Template', 'mycred' ) . '</td>
401
+ <td><input type="text" name="mycred_gateway[share_log]" value="' . esc_attr( $mycred_wpecom_settings['share_log'] ) . '" style="width:50%;" /><br /><span class="description">' . $mycred->available_template_tags( array( 'general', 'post' ) ) . '</span></td>
402
+ </tr>
403
+ <tr>
404
+ <td colspan="2"><strong>' . __( 'Messages', 'mycred' ) . '</strong></td>
405
+ </tr>
406
+ <tr>
407
+ <td width="150">' . __( 'Instructions', 'mycred' ) . '</td>
408
+ <td><input type="text" name="mycred_gateway[message]" value="' . esc_attr( $mycred_wpecom_settings['message'] ) . '" style="width:50%;" /><br /><span class="description">' . __( 'Optional instructions to show users when selecting this gateway. Leave empty to hide.', 'mycred' ) . '</span></td>
409
+ </tr>
410
+ <tr>
411
+ <td width="150">' . __( 'Visitors', 'mycred' ) . '</td>
412
+ <td><input type="text" name="mycred_gateway[visitor]" value="' . esc_attr( $mycred_wpecom_settings['visitor'] ) . '" style="width:50%;" /><br /><span class="description">' . __( 'Message to show visitors who are not logged in.', 'mycred' ) . '</span></td>
413
+ </tr>
414
+ <tr>
415
+ <td width="150">' . __( 'Insufficient Funds', 'mycred' ) . '</td>
416
+ <td><input type="text" name="mycred_gateway[low_funds]" value="' . esc_attr( $mycred_wpecom_settings['low_funds'] ) . '" style="width:50%;" /><br /><span class="description">' . $mycred->template_tags_general( __( 'Message to show when users does not have enough %plural% to pay using this gateway.', 'mycred' ) ) . '</span></td>
417
+ </tr>';
418
 
419
  return apply_filters( 'mycred_wpecom_settings', $output );
420
+
421
  }
422
+ endif;
423
 
424
  /**
425
  * Save Gateway Settings
427
  * @since 1.3
428
  * @version 1.1
429
  */
430
+ if ( ! function_exists( 'mycred_wpecom_gateway_settings_save' ) ) :
431
  function mycred_wpecom_gateway_settings_save() {
432
+
433
  if ( isset( $_POST['mycred_gateway'] ) ) {
434
  $new_settings = apply_filters( 'mycred_wpecom_save_settings', array(
435
+ 'log' => sanitize_text_field( $_POST['mycred_gateway']['log'] ),
436
+ 'type' => sanitize_key( $_POST['mycred_gateway']['type'] ),
437
  'share' => abs( $_POST['mycred_gateway']['share'] ),
438
+ 'share_log' => sanitize_text_field( $_POST['mycred_gateway']['share_log'] ),
439
+ 'rate' => sanitize_text_field( $_POST['mycred_gateway']['rate'] ),
440
  'visitor' => sanitize_text_field( $_POST['mycred_gateway']['visitor'] ),
441
  'low_funds' => sanitize_text_field( $_POST['mycred_gateway']['low_funds'] ),
442
  'message' => sanitize_text_field( $_POST['mycred_gateway']['message'] )
443
  ) );
444
  update_option( 'mycred_wpecom_settings', $new_settings );
445
  }
446
+
447
  return true;
448
+
449
  }
450
+ endif;
451
 
452
  /**
453
  * Parse Email Notice
454
  * @since 1.2.2
455
  * @version 1.0
456
  */
457
+ if ( ! function_exists( 'mycred_wpecom_parse_email' ) ) :
458
+ function mycred_wpecom_parse_email( $email ) {
459
+
460
+ if ( $email['request']['ref'] == 'wpecom_payment' )
 
461
  $email['request']['entry'] = str_replace( '%order_id%', $email['request']['data'], $email['request']['entry'] );
462
+
463
  return $email;
464
+
465
  }
466
+ endif;
467
+ add_filter( 'mycred_email_before_send', 'mycred_wpecom_parse_email', 30 );
468
+
469
  ?>
addons/gateway/event-booking/mycred-eventespresso3.php CHANGED
@@ -4,29 +4,29 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
4
  /**
5
  * Event Espresso Payment Gateway
6
  * @since 1.2
7
- * @version 1.1
8
  */
9
- if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
10
  class myCRED_Espresso_Gateway {
11
 
12
- public $label = '';
13
- public $core = NULL;
14
- public $mycred_type = 'mycred_default';
15
- public $prefs = array();
16
- public $update = false;
17
 
18
  /**
19
  * Construct
20
  */
21
  function __construct() {
22
- // Default settings
23
  $defaults = array(
24
  'labels' => array(
25
  'gateway' => $this->label,
26
  'payment' => $this->label . ' ' . __( 'Payments', 'mycred' ),
27
  'button' => __( 'Pay Now', 'mycred' )
28
  ),
29
- 'type' => 'mycred_default',
30
  'rate' => 100,
31
  'share' => 0,
32
  'log' => __( 'Payment for Event Registration', 'mycred' ),
@@ -38,13 +38,11 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
38
  );
39
 
40
  // Settings
41
- $settings = get_option( 'mycred_espresso_gateway_prefs' );
42
- $this->prefs = mycred_apply_defaults( $defaults, $settings );
43
-
44
  $this->mycred_type = $this->prefs['type'];
 
45
 
46
- // Load myCRED
47
- $this->core = mycred( $this->mycred_type );
48
  }
49
 
50
  /**
@@ -54,8 +52,9 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
54
  * @version 1.0
55
  */
56
  public function load() {
 
57
  add_action( 'init', array( $this, 'gateway_setup' ) );
58
-
59
  }
60
 
61
  /**
@@ -65,13 +64,15 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
65
  * @version 1.0
66
  */
67
  public function gateway_active( $id = 'mycred' ) {
 
68
  global $active_gateways;
69
-
70
  if ( ! isset( $active_gateways ) || ! is_array( $active_gateways ) )
71
  $active_gateways = get_option( 'event_espresso_active_gateways', array() );
72
 
73
  if ( array_key_exists( $id, $active_gateways ) ) return true;
74
  return false;
 
75
  }
76
 
77
  /**
@@ -80,17 +81,18 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
80
  * @version 1.0
81
  */
82
  public function gateway_setup() {
 
83
  // Capture Settings Update
84
- if ( isset( $_REQUEST['mycred-gateway-action'] ) && isset( $_REQUEST['mycred-gateway-token'] ) ) {
85
  $this->update_settings();
86
- }
87
-
88
- add_filter( 'action_hook_espresso_gateway_formal_name', array( $this, 'formal_name' ) );
89
- add_filter( 'action_hook_espresso_gateway_payment_type', array( $this, 'paymenttype_name' ) );
90
  add_action( 'action_hook_espresso_display_gateway_settings', array( $this, 'gateway_settings_page' ), 11 );
91
-
92
  // Make sure gateway is enabled
93
  if ( $this->gateway_active() ) {
 
94
  // Hook into Payment Page
95
  add_action( 'action_hook_espresso_display_onsite_payment_gateway', array( $this, 'payment_page' ) );
96
 
@@ -102,7 +104,9 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
102
 
103
  add_action( 'action_hook_espresso_display_onsite_payment_header', 'espresso_display_onsite_payment_header' );
104
  add_action( 'action_hook_espresso_display_onsite_payment_footer', 'espresso_display_onsite_payment_footer' );
 
105
  }
 
106
  }
107
 
108
  /**
@@ -111,11 +115,13 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
111
  * @version 1.0
112
  */
113
  public function is_payment() {
 
114
  if (
115
  ( isset( $_REQUEST['payment_type'] ) && $_REQUEST['payment_type'] == 'mycred' ) &&
116
  ( isset( $_REQUEST['token'] ) && wp_verify_nonce( $_REQUEST['token'], 'pay-with-mycred' ) ) ) return true;
117
-
118
  return false;
 
119
  }
120
 
121
  /**
@@ -124,8 +130,11 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
124
  * @version 1.0
125
  */
126
  public function formal_name( $gateway_formal_names ) {
 
127
  $gateway_formal_names['mycred'] = $this->prefs['labels']['gateway'];
 
128
  return $gateway_formal_names;
 
129
  }
130
 
131
  /**
@@ -134,8 +143,11 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
134
  * @version 1.0
135
  */
136
  public function paymenttype_name( $gateway_payment_types ) {
 
137
  $gateway_payment_types['mycred'] = $this->prefs['labels']['payment'];
 
138
  return $gateway_payment_types;
 
139
  }
140
 
141
  /**
@@ -144,10 +156,12 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
144
  * @version 1.0
145
  */
146
  public function set_attendee_id( $attendee_id ) {
 
147
  if ( isset( $_REQUEST['id'] ) )
148
  $attendee_id = $_REQUEST['id'];
149
-
150
  return $attendee_id;
 
151
  }
152
 
153
  /**
@@ -156,6 +170,7 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
156
  * @version 1.2
157
  */
158
  public function process_payment( $payment_data ) {
 
159
  if ( ! is_user_logged_in() ) return $payment_data;
160
 
161
  // Security
@@ -167,19 +182,19 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
167
  // Check if this event does not accept myCRED payments
168
  if ( isset( $event_meta['mycred_no_sale'] ) ) return;
169
 
170
- $user_id = get_current_user_id();
171
 
172
  // Make sure this is unique
173
  if ( $this->core->has_entry( 'event_payment', $payment_data['event_id'], $user_id, $payment_data['registration_id'] ) ) return $payment_data;
174
-
175
- $balance = $this->core->get_users_cred( $user_id, $this->mycred_type );
176
- $event_cost = $this->prefs['rate']*$payment_data['total_cost'];
177
  $after_purchase = $balance-$event_cost;
178
 
179
  // This should never happen
180
  if ( $after_purchase < 0 ) return $payment_data;
181
 
182
- $entry = $this->prefs['log'];
183
 
184
  // Deduct
185
  $this->core->add_creds(
@@ -194,18 +209,22 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
194
 
195
  // Update Payment Data
196
  $payment_data['payment_status'] = 'Completed';
197
- $payment_data['txn_type'] = $this->prefs['labels']['payment'];
198
- $payment_data['txn_id'] = $payment_data['attendee_session'];
199
- $payment_data['txn_details'] = $this->core->template_tags_general( $entry );
200
 
201
  // Let others play
202
  do_action( 'mycred_espresso_processed', $payment_data, $this->prefs, $this->core );
203
 
204
  // Profit sharing
205
  if ( $this->prefs['share'] != 0 ) {
 
206
  $event_post = get_post( (int) $payment_data['event_id'] );
 
207
  if ( $event_post !== NULL ) {
 
208
  $share = ( $this->prefs['share']/100 ) * $price;
 
209
  $this->core->add_creds(
210
  'event_sale',
211
  $event_post->post_author,
@@ -215,10 +234,13 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
215
  $payment_data['registration_id'],
216
  $this->mycred_type
217
  );
 
218
  }
 
219
  }
220
 
221
  return $payment_data;
 
222
  }
223
 
224
  /**
@@ -227,26 +249,29 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
227
  * @version 1.1.1
228
  */
229
  public function payment_page( $payment_data ) {
 
230
  extract( $payment_data );
231
-
232
  // Check if this event does not accept myCRED payments
233
  if ( isset( $event_meta['mycred_no_sale'] ) ) return;
234
-
235
  global $org_options;
236
-
237
  $member = $solvent = $user_id = false;
 
238
  if ( is_user_logged_in() ) {
239
- $member = true;
240
- $user_id = get_current_user_id();
241
- $balance = $this->core->get_users_cred( $user_id, $this->mycred_type );
 
242
 
243
  // Calculate Cost
244
- $event_cost = $this->prefs['rate']*$event_cost;
245
  $after_purchase = $balance-$event_cost;
246
 
247
  if ( $after_purchase >= 0 )
248
  $solvent = true;
249
-
250
  $args = array(
251
  'page_id' => $org_options['return_url'],
252
  'r_id' => $registration_id,
@@ -255,8 +280,10 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
255
  'token' => wp_create_nonce( 'pay-with-mycred' )
256
  );
257
  $finalize_link = add_query_arg( $args, home_url() );
258
- } ?>
259
 
 
 
 
260
  <div id="mycred-payment-option-dv" class="payment-option-dv">
261
  <a id="mycred-payment-option-lnk" class="payment-option-lnk algn-vrt display-the-hidden" rel="mycred-payment-option-form" style="display: table-cell">
262
  <div class="vrt-cell">
@@ -264,10 +291,13 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
264
  </div>
265
  </a><br/>
266
  <div id="mycred-payment-option-form-dv" class="hide-if-js">
267
- <?php if ( $member && $solvent ) : ?>
 
 
268
  <?php if ( trim( $this->prefs['messages']['solvent'] ) != '' ) : ?>
269
 
270
  <p><?php echo $this->core->template_tags_general( $this->prefs['messages']['solvent'] ); ?></p>
 
271
  <?php endif; ?>
272
 
273
  <div class="event-display-boxes">
@@ -288,7 +318,8 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
288
  </table>
289
  <p><a href="<?php echo esc_url( $finalize_link ); ?>" class="button button-large button-primary" style="float:right;"><?php echo $this->prefs['labels']['button']; ?></a></p>
290
  </div>
291
- <?php elseif ( $member && ! $solvent ) : ?>
 
292
 
293
  <div class="event_espresso_attention event-messages ui-state-highlight">
294
  <span class="ui-icon ui-icon-alert"></span>
@@ -311,17 +342,20 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
311
  </tr>
312
  </table>
313
  </div>
 
314
  <?php else : ?>
315
 
316
  <div class="event_espresso_attention event-messages ui-state-highlight">
317
  <span class="ui-icon ui-icon-alert"></span>
318
  <p><?php echo $this->core->template_tags_general( $this->prefs['messages']['visitors'] ); ?></p>
319
  </div>
 
320
  <?php endif; ?>
321
 
322
  </div>
323
  </div>
324
  <?php
 
325
  }
326
 
327
  /**
@@ -330,7 +364,9 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
330
  * @version 1.0
331
  */
332
  public function gateway_settings_page( ) {
 
333
  global $espresso_premium, $active_gateways, $org_options;
 
334
  if ( ! $espresso_premium )
335
  return;
336
 
@@ -339,26 +375,25 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
339
  $active_gateways['mycred'] = myCRED_GATE_CART_DIR . 'mycred-eventespresso3.php';
340
  update_option( 'event_espresso_active_gateways', $active_gateways );
341
  }
342
- $activate_url = admin_url( 'admin.php?page=payment_gateways&activate_mycred_payment=true' );
343
- $activate_text = sprintf( __( 'Activate %s', 'mycred' ), $this->label );
344
-
 
345
  // deactivate
346
  if ( ! empty( $_REQUEST['deactivate_check_payment'] ) ) {
347
  unset( $active_gateways['mycred'] );
348
  update_option( 'event_espresso_active_gateways', $active_gateways );
349
  }
350
- $deactivate_url = admin_url( 'admin.php?page=payment_gateways&deactivate_mycred_payment=true' );
 
351
  $deactivate_text = sprintf( __( 'Deactivate %s', 'mycred' ), $this->label );
352
 
353
  //Open or close the postbox div
354
- if ( empty( $_REQUEST['deactivate_mycred_payment'] )
355
- && ( ! empty( $_REQUEST['deactivate_mycred_payment'] )
356
- || array_key_exists( 'mycred', $active_gateways ) ) ) {
357
  $postbox_style = '';
358
- } else {
359
- $postbox_style = 'closed';
360
- } ?>
361
 
 
362
  <p id="mycred-gate">&nbsp;</p>
363
  <div class="metabox-holder">
364
  <div class="postbox <?php echo $postbox_style; ?>" style="background-image: url(http://mycred.me/wp-content/uploads/login-bg.png); background-position: center bottom; background-repeat: no-repeat; background-size: contain;">
@@ -369,21 +404,28 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
369
  <div class="inside">
370
  <div class="padding">
371
  <ul>
372
- <?php if ( array_key_exists( 'mycred', $active_gateways ) ) {
373
- echo '<li id="deactivate_check" style="width:30%;" onclick="location.href=\'' . $deactivate_url . '\';" class="red_alert pointer"><strong>' . $deactivate_text . '</strong></li>';
 
374
 
 
375
  $this->gateway_settings();
 
376
  }
377
  else {
 
378
  echo '<li id="activate_check" style="width:30%;" onclick="location.href=\'' . $activate_url . '\';" class="green_alert pointer"><strong>' . $activate_text . '</strong></li>';
 
379
  }
380
- echo '</ul>'; ?>
381
 
 
382
  </div>
383
  </div>
384
  </div>
385
  </div>
386
  <?php
 
387
  }
388
 
389
  /**
@@ -393,20 +435,23 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
393
  * @version 1.2
394
  */
395
  public function gateway_settings() {
 
396
  global $org_options;
397
-
398
  $exchange_message = sprintf(
399
  __( 'How many %s is 1 %s worth?', 'mycred' ),
400
  $this->core->plural(),
401
  $org_options['currency_symbol']
402
  );
403
-
404
- $mycred_types = mycred_get_types(); ?>
405
 
 
 
 
406
  <?php if ( $this->update ) : ?>
407
- <h2 style="color: green;"><?php _e( 'Gateways Settings Successfully Updated', 'mycred' ); ?></h2>
408
  <?php endif; ?>
409
  <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>#mycred-gate">
 
410
  <?php do_action( 'mycred_espresso_before_prefs' ); ?>
411
 
412
  <table width="99%" border="0" cellspacing="5" cellpadding="5">
@@ -430,6 +475,7 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
430
  <span class="description"><?php _e( 'Pay Button', 'mycred' ); ?></span>
431
  </li>
432
  </ul>
 
433
  <?php if ( count( $mycred_types ) > 1 ) : ?>
434
 
435
  <ul>
@@ -439,9 +485,11 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
439
 
440
  </li>
441
  </ul>
 
442
  <?php else : ?>
443
 
444
  <input type="hidden" name="mycred_prefs[type]" id="mycred-prefs-payment-type" value="mycred_default" />
 
445
  <?php endif; ?>
446
 
447
  <h4><?php _e( 'Price', 'mycred' ); ?></h4>
@@ -495,6 +543,7 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
495
  </td>
496
  </tr>
497
  </table>
 
498
  <?php do_action( 'mycred_espresso_after_prefs' ); ?>
499
 
500
  <input type="hidden" name="mycred-gateway-action" value="update-settings" />
@@ -502,6 +551,7 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
502
  <p><input class="button-primary" type="submit" name="Submit" value="<?php _e( 'Update Settings', 'mycred' ); ?>" /></p>
503
  </form>
504
  <?php
 
505
  }
506
 
507
  /**
@@ -510,6 +560,7 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
510
  * @version 1.2
511
  */
512
  public function update_settings() {
 
513
  // Apply Whitelabeling
514
  $this->label = mycred_label();
515
 
@@ -519,43 +570,46 @@ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) {
519
 
520
  // Prep
521
  $new_settings = array();
522
- $post = $_POST['mycred_prefs'];
 
523
  if ( ! is_array( $post ) || empty( $post ) ) return;
524
 
525
  // Labels
526
  $new_settings['labels']['gateway'] = strip_tags( $post['labels']['gateway'], '<strong><em><span>' );
527
  $new_settings['labels']['payment'] = strip_tags( $post['labels']['payment'], '<strong><em><span>' );
528
- $new_settings['labels']['button'] = sanitize_text_field( $post['labels']['button'] );
529
 
530
  // Point Type
531
- $new_settings['type'] = sanitize_text_field( $post['type'] );
532
 
533
  // Exchange Rate
534
- $new_settings['rate'] = sanitize_text_field( $post['rate'] );
535
 
536
  // Profit Share
537
  $new_settings['share'] = abs( $post['share'] );
538
 
539
  // Log
540
- $new_settings['log'] = sanitize_text_field( $post['log'] );
541
 
542
  // Messages
543
- $new_settings['messages']['solvent'] = sanitize_text_field( stripslashes( $post['messages']['solvent'] ) );
544
  $new_settings['messages']['insolvent'] = sanitize_text_field( stripslashes( $post['messages']['insolvent'] ) );
545
- $new_settings['messages']['visitors'] = sanitize_text_field( stripslashes( $post['messages']['visitors'] ) );
546
 
547
  // Let others play
548
  $new_settings = apply_filters( 'mycred_espresso_save_pref', $new_settings );
549
 
550
  // Save new settings
551
- $current = $this->prefs;
552
  $this->prefs = mycred_apply_defaults( $current, $new_settings );
553
  update_option( 'mycred_espresso_gateway_prefs', $this->prefs );
554
 
555
  // Flag update
556
  $this->update = true;
 
557
  }
 
558
  }
559
- }
560
 
561
  ?>
4
  /**
5
  * Event Espresso Payment Gateway
6
  * @since 1.2
7
+ * @version 1.2
8
  */
9
+ if ( ! class_exists( 'myCRED_Espresso_Gateway' ) ) :
10
  class myCRED_Espresso_Gateway {
11
 
12
+ public $label = '';
13
+ public $core = NULL;
14
+ public $mycred_type = MYCRED_DEFAULT_TYPE_KEY;
15
+ public $prefs = array();
16
+ public $update = false;
17
 
18
  /**
19
  * Construct
20
  */
21
  function __construct() {
22
+
23
  $defaults = array(
24
  'labels' => array(
25
  'gateway' => $this->label,
26
  'payment' => $this->label . ' ' . __( 'Payments', 'mycred' ),
27
  'button' => __( 'Pay Now', 'mycred' )
28
  ),
29
+ 'type' => MYCRED_DEFAULT_TYPE_KEY,
30
  'rate' => 100,
31
  'share' => 0,
32
  'log' => __( 'Payment for Event Registration', 'mycred' ),
38
  );
39
 
40
  // Settings
41
+ $settings = get_option( 'mycred_espresso_gateway_prefs' );
42
+ $this->prefs = mycred_apply_defaults( $defaults, $settings );
 
43
  $this->mycred_type = $this->prefs['type'];
44
+ $this->core = mycred( $this->mycred_type );
45
 
 
 
46
  }
47
 
48
  /**
52
  * @version 1.0
53
  */
54
  public function load() {
55
+
56
  add_action( 'init', array( $this, 'gateway_setup' ) );
57
+
58
  }
59
 
60
  /**
64
  * @version 1.0
65
  */
66
  public function gateway_active( $id = 'mycred' ) {
67
+
68
  global $active_gateways;
69
+
70
  if ( ! isset( $active_gateways ) || ! is_array( $active_gateways ) )
71
  $active_gateways = get_option( 'event_espresso_active_gateways', array() );
72
 
73
  if ( array_key_exists( $id, $active_gateways ) ) return true;
74
  return false;
75
+
76
  }
77
 
78
  /**
81
  * @version 1.0
82
  */
83
  public function gateway_setup() {
84
+
85
  // Capture Settings Update
86
+ if ( isset( $_REQUEST['mycred-gateway-action'] ) && isset( $_REQUEST['mycred-gateway-token'] ) )
87
  $this->update_settings();
88
+
89
+ add_filter( 'action_hook_espresso_gateway_formal_name', array( $this, 'formal_name' ) );
90
+ add_filter( 'action_hook_espresso_gateway_payment_type', array( $this, 'paymenttype_name' ) );
 
91
  add_action( 'action_hook_espresso_display_gateway_settings', array( $this, 'gateway_settings_page' ), 11 );
92
+
93
  // Make sure gateway is enabled
94
  if ( $this->gateway_active() ) {
95
+
96
  // Hook into Payment Page
97
  add_action( 'action_hook_espresso_display_onsite_payment_gateway', array( $this, 'payment_page' ) );
98
 
104
 
105
  add_action( 'action_hook_espresso_display_onsite_payment_header', 'espresso_display_onsite_payment_header' );
106
  add_action( 'action_hook_espresso_display_onsite_payment_footer', 'espresso_display_onsite_payment_footer' );
107
+
108
  }
109
+
110
  }
111
 
112
  /**
115
  * @version 1.0
116
  */
117
  public function is_payment() {
118
+
119
  if (
120
  ( isset( $_REQUEST['payment_type'] ) && $_REQUEST['payment_type'] == 'mycred' ) &&
121
  ( isset( $_REQUEST['token'] ) && wp_verify_nonce( $_REQUEST['token'], 'pay-with-mycred' ) ) ) return true;
122
+
123
  return false;
124
+
125
  }
126
 
127
  /**
130
  * @version 1.0
131
  */
132
  public function formal_name( $gateway_formal_names ) {
133
+
134
  $gateway_formal_names['mycred'] = $this->prefs['labels']['gateway'];
135
+
136
  return $gateway_formal_names;
137
+
138
  }
139
 
140
  /**
143
  * @version 1.0
144
  */
145
  public function paymenttype_name( $gateway_payment_types ) {
146
+
147
  $gateway_payment_types['mycred'] = $this->prefs['labels']['payment'];
148
+
149
  return $gateway_payment_types;
150
+
151
  }
152
 
153
  /**
156
  * @version 1.0
157
  */
158
  public function set_attendee_id( $attendee_id ) {
159
+
160
  if ( isset( $_REQUEST['id'] ) )
161
  $attendee_id = $_REQUEST['id'];
162
+
163
  return $attendee_id;
164
+
165
  }
166
 
167
  /**
170
  * @version 1.2
171
  */
172
  public function process_payment( $payment_data ) {
173
+
174
  if ( ! is_user_logged_in() ) return $payment_data;
175
 
176
  // Security
182
  // Check if this event does not accept myCRED payments
183
  if ( isset( $event_meta['mycred_no_sale'] ) ) return;
184
 
185
+ $user_id = get_current_user_id();
186
 
187
  // Make sure this is unique
188
  if ( $this->core->has_entry( 'event_payment', $payment_data['event_id'], $user_id, $payment_data['registration_id'] ) ) return $payment_data;
189
+
190
+ $balance = $this->core->get_users_balance( $user_id, $this->mycred_type );
191
+ $event_cost = $this->prefs['rate']*$payment_data['total_cost'];
192
  $after_purchase = $balance-$event_cost;
193
 
194
  // This should never happen
195
  if ( $after_purchase < 0 ) return $payment_data;
196
 
197
+ $entry = $this->prefs['log'];
198
 
199
  // Deduct
200
  $this->core->add_creds(
209
 
210
  // Update Payment Data
211
  $payment_data['payment_status'] = 'Completed';
212
+ $payment_data['txn_type'] = $this->prefs['labels']['payment'];
213
+ $payment_data['txn_id'] = $payment_data['attendee_session'];
214
+ $payment_data['txn_details'] = $this->core->template_tags_general( $entry );
215
 
216
  // Let others play
217
  do_action( 'mycred_espresso_processed', $payment_data, $this->prefs, $this->core );
218
 
219
  // Profit sharing
220
  if ( $this->prefs['share'] != 0 ) {
221
+
222
  $event_post = get_post( (int) $payment_data['event_id'] );
223
+
224
  if ( $event_post !== NULL ) {
225
+
226
  $share = ( $this->prefs['share']/100 ) * $price;
227
+
228
  $this->core->add_creds(
229
  'event_sale',
230
  $event_post->post_author,
234
  $payment_data['registration_id'],
235
  $this->mycred_type
236
  );
237
+
238
  }
239
+
240
  }
241
 
242
  return $payment_data;
243
+
244
  }
245
 
246
  /**
249
  * @version 1.1.1
250
  */
251
  public function payment_page( $payment_data ) {
252
+
253
  extract( $payment_data );
254
+
255
  // Check if this event does not accept myCRED payments
256
  if ( isset( $event_meta['mycred_no_sale'] ) ) return;
257
+
258
  global $org_options;
259
+
260
  $member = $solvent = $user_id = false;
261
+
262
  if ( is_user_logged_in() ) {
263
+
264
+ $member = true;
265
+ $user_id = get_current_user_id();
266
+ $balance = $this->core->get_users_balance( $user_id, $this->mycred_type );
267
 
268
  // Calculate Cost
269
+ $event_cost = $this->prefs['rate']*$event_cost;
270
  $after_purchase = $balance-$event_cost;
271
 
272
  if ( $after_purchase >= 0 )
273
  $solvent = true;
274
+
275
  $args = array(
276
  'page_id' => $org_options['return_url'],
277
  'r_id' => $registration_id,
280
  'token' => wp_create_nonce( 'pay-with-mycred' )
281
  );
282
  $finalize_link = add_query_arg( $args, home_url() );
 
283
 
284
+ }
285
+
286
+ ?>
287
  <div id="mycred-payment-option-dv" class="payment-option-dv">
288
  <a id="mycred-payment-option-lnk" class="payment-option-lnk algn-vrt display-the-hidden" rel="mycred-payment-option-form" style="display: table-cell">
289
  <div class="vrt-cell">
291
  </div>
292
  </a><br/>
293
  <div id="mycred-payment-option-form-dv" class="hide-if-js">
294
+
295
+ <?php if ( $member && $solvent ) : ?>
296
+
297
  <?php if ( trim( $this->prefs['messages']['solvent'] ) != '' ) : ?>
298
 
299
  <p><?php echo $this->core->template_tags_general( $this->prefs['messages']['solvent'] ); ?></p>
300
+
301
  <?php endif; ?>
302
 
303
  <div class="event-display-boxes">
318
  </table>
319
  <p><a href="<?php echo esc_url( $finalize_link ); ?>" class="button button-large button-primary" style="float:right;"><?php echo $this->prefs['labels']['button']; ?></a></p>
320
  </div>
321
+
322
+ <?php elseif ( $member && ! $solvent ) : ?>
323
 
324
  <div class="event_espresso_attention event-messages ui-state-highlight">
325
  <span class="ui-icon ui-icon-alert"></span>
342
  </tr>
343
  </table>
344
  </div>
345
+
346
  <?php else : ?>
347
 
348
  <div class="event_espresso_attention event-messages ui-state-highlight">
349
  <span class="ui-icon ui-icon-alert"></span>
350
  <p><?php echo $this->core->template_tags_general( $this->prefs['messages']['visitors'] ); ?></p>
351
  </div>
352
+
353
  <?php endif; ?>
354
 
355
  </div>
356
  </div>
357
  <?php
358
+
359
  }
360
 
361
  /**
364
  * @version 1.0
365
  */
366
  public function gateway_settings_page( ) {
367
+
368
  global $espresso_premium, $active_gateways, $org_options;
369
+
370
  if ( ! $espresso_premium )
371
  return;
372
 
375
  $active_gateways['mycred'] = myCRED_GATE_CART_DIR . 'mycred-eventespresso3.php';
376
  update_option( 'event_espresso_active_gateways', $active_gateways );
377
  }
378
+
379
+ $activate_url = admin_url( 'admin.php?page=payment_gateways&activate_mycred_payment=true' );
380
+ $activate_text = sprintf( __( 'Activate %s', 'mycred' ), $this->label );
381
+
382
  // deactivate
383
  if ( ! empty( $_REQUEST['deactivate_check_payment'] ) ) {
384
  unset( $active_gateways['mycred'] );
385
  update_option( 'event_espresso_active_gateways', $active_gateways );
386
  }
387
+
388
+ $deactivate_url = admin_url( 'admin.php?page=payment_gateways&deactivate_mycred_payment=true' );
389
  $deactivate_text = sprintf( __( 'Deactivate %s', 'mycred' ), $this->label );
390
 
391
  //Open or close the postbox div
392
+ $postbox_style = 'closed';
393
+ if ( empty( $_REQUEST['deactivate_mycred_payment'] ) && ( ! empty( $_REQUEST['deactivate_mycred_payment'] ) || array_key_exists( 'mycred', $active_gateways ) ) )
 
394
  $postbox_style = '';
 
 
 
395
 
396
+ ?>
397
  <p id="mycred-gate">&nbsp;</p>
398
  <div class="metabox-holder">
399
  <div class="postbox <?php echo $postbox_style; ?>" style="background-image: url(http://mycred.me/wp-content/uploads/login-bg.png); background-position: center bottom; background-repeat: no-repeat; background-size: contain;">
404
  <div class="inside">
405
  <div class="padding">
406
  <ul>
407
+ <?php
408
+
409
+ if ( array_key_exists( 'mycred', $active_gateways ) ) {
410
 
411
+ echo '<li id="deactivate_check" style="width:30%;" onclick="location.href=\'' . $deactivate_url . '\';" class="red_alert pointer"><strong>' . $deactivate_text . '</strong></li>';
412
  $this->gateway_settings();
413
+
414
  }
415
  else {
416
+
417
  echo '<li id="activate_check" style="width:30%;" onclick="location.href=\'' . $activate_url . '\';" class="green_alert pointer"><strong>' . $activate_text . '</strong></li>';
418
+
419
  }
420
+ echo '</ul>';
421
 
422
+ ?>
423
  </div>
424
  </div>
425
  </div>
426
  </div>
427
  <?php
428
+
429
  }
430
 
431
  /**
435
  * @version 1.2
436
  */
437
  public function gateway_settings() {
438
+
439
  global $org_options;
440
+
441
  $exchange_message = sprintf(
442
  __( 'How many %s is 1 %s worth?', 'mycred' ),
443
  $this->core->plural(),
444
  $org_options['currency_symbol']
445
  );
 
 
446
 
447
+ $mycred_types = mycred_get_types();
448
+
449
+ ?>
450
  <?php if ( $this->update ) : ?>
451
+ <h2 style="color: green;"><?php _e( 'Settings Updated', 'mycred' ); ?></h2>
452
  <?php endif; ?>
453
  <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>#mycred-gate">
454
+
455
  <?php do_action( 'mycred_espresso_before_prefs' ); ?>
456
 
457
  <table width="99%" border="0" cellspacing="5" cellpadding="5">
475
  <span class="description"><?php _e( 'Pay Button', 'mycred' ); ?></span>
476
  </li>
477
  </ul>
478
+
479
  <?php if ( count( $mycred_types ) > 1 ) : ?>
480
 
481
  <ul>
485
 
486
  </li>
487
  </ul>
488
+
489
  <?php else : ?>
490
 
491
  <input type="hidden" name="mycred_prefs[type]" id="mycred-prefs-payment-type" value="mycred_default" />
492
+
493
  <?php endif; ?>
494
 
495
  <h4><?php _e( 'Price', 'mycred' ); ?></h4>
543
  </td>
544
  </tr>
545
  </table>
546
+
547
  <?php do_action( 'mycred_espresso_after_prefs' ); ?>
548
 
549
  <input type="hidden" name="mycred-gateway-action" value="update-settings" />
551
  <p><input class="button-primary" type="submit" name="Submit" value="<?php _e( 'Update Settings', 'mycred' ); ?>" /></p>
552
  </form>
553
  <?php
554
+
555
  }
556
 
557
  /**
560
  * @version 1.2
561
  */
562
  public function update_settings() {
563
+
564
  // Apply Whitelabeling
565
  $this->label = mycred_label();
566
 
570
 
571
  // Prep
572
  $new_settings = array();
573
+ $post = $_POST['mycred_prefs'];
574
+
575
  if ( ! is_array( $post ) || empty( $post ) ) return;
576
 
577
  // Labels
578
  $new_settings['labels']['gateway'] = strip_tags( $post['labels']['gateway'], '<strong><em><span>' );
579
  $new_settings['labels']['payment'] = strip_tags( $post['labels']['payment'], '<strong><em><span>' );
580
+ $new_settings['labels']['button'] = sanitize_text_field( $post['labels']['button'] );
581
 
582
  // Point Type
583
+ $new_settings['type'] = sanitize_text_field( $post['type'] );
584
 
585
  // Exchange Rate
586
+ $new_settings['rate'] = sanitize_text_field( $post['rate'] );
587
 
588
  // Profit Share
589
  $new_settings['share'] = abs( $post['share'] );
590
 
591
  // Log
592
+ $new_settings['log'] = sanitize_text_field( $post['log'] );
593
 
594
  // Messages
595
+ $new_settings['messages']['solvent'] = sanitize_text_field( stripslashes( $post['messages']['solvent'] ) );
596
  $new_settings['messages']['insolvent'] = sanitize_text_field( stripslashes( $post['messages']['insolvent'] ) );
597
+ $new_settings['messages']['visitors'] = sanitize_text_field( stripslashes( $post['messages']['visitors'] ) );
598
 
599
  // Let others play
600
  $new_settings = apply_filters( 'mycred_espresso_save_pref', $new_settings );
601
 
602
  // Save new settings
603
+ $current = $this->prefs;
604
  $this->prefs = mycred_apply_defaults( $current, $new_settings );
605
  update_option( 'mycred_espresso_gateway_prefs', $this->prefs );
606
 
607
  // Flag update
608
  $this->update = true;
609
+
610
  }
611
+
612
  }
613
+ endif;
614
 
615
  ?>
addons/gateway/event-booking/mycred-eventsmanager-pro.php CHANGED
@@ -304,7 +304,7 @@ if ( ! class_exists( 'EM_Gateway_myCRED' ) && class_exists( 'EM_Gateway' ) ) :
304
  if ( $capture ) {
305
 
306
  // Set booking status, but no emails sent
307
- if ( ! get_option( 'em_'.$this->gateway.'_manual_approval', false ) || ! get_option( 'dbem_bookings_approval' ) ) {
308
  $EM_Booking->set_status( 1, true ); //Approve
309
  }
310
 
304
  if ( $capture ) {
305
 
306
  // Set booking status, but no emails sent
307
+ if ( ! get_option( 'em_' . $this->gateway . '_manual_approval', false ) || ! get_option( 'dbem_bookings_approval' ) ) {
308
  $EM_Booking->set_status( 1, true ); //Approve
309
  }
310
 
addons/gateway/event-booking/mycred-eventsmanager.php CHANGED
@@ -11,7 +11,7 @@ if ( ! class_exists( 'myCRED_Events_Manager_Gateway' ) && defined( 'EM_VERSION'
11
 
12
  public $label = '';
13
  public $prefs;
14
- public $mycred_type = 'mycred_default';
15
  public $core;
16
  public $booking_cols = 0;
17
 
@@ -22,7 +22,7 @@ if ( ! class_exists( 'myCRED_Events_Manager_Gateway' ) && defined( 'EM_VERSION'
22
  // Default settings
23
  $defaults = array(
24
  'setup' => 'off',
25
- 'type' => 'mycred_default',
26
  'rate' => 100,
27
  'share' => 0,
28
  'log' => array(
@@ -159,7 +159,7 @@ if ( ! class_exists( 'myCRED_Events_Manager_Gateway' ) && defined( 'EM_VERSION'
159
 
160
  // Only pending events can be paid for
161
  if ( $EM_Event->get_bookings()->has_open_time() ) {
162
- $balance = $this->core->get_users_cred( $EM_Booking->person->ID, $this->mycred_type );
163
  if ( $balance <= 0 ) return false;
164
 
165
  $price = $this->core->number( $EM_Booking->booking_price );
@@ -391,7 +391,7 @@ if ( ! class_exists( 'myCRED_Events_Manager_Gateway' ) && defined( 'EM_VERSION'
391
  global $mycred_em_pay;
392
 
393
  if ( $mycred_em_pay && is_object( $EM_Booking ) ) {
394
- $balance = $this->core->get_users_cred( $EM_Booking->person->ID, $this->mycred_type );
395
  if ( $balance <= 0 ) return;
396
 
397
  $price = $EM_Booking->booking_price;
11
 
12
  public $label = '';
13
  public $prefs;
14
+ public $mycred_type = MYCRED_DEFAULT_TYPE_KEY;
15
  public $core;
16
  public $booking_cols = 0;
17
 
22
  // Default settings
23
  $defaults = array(
24
  'setup' => 'off',
25
+ 'type' => MYCRED_DEFAULT_TYPE_KEY,
26
  'rate' => 100,
27
  'share' => 0,
28
  'log' => array(
159
 
160
  // Only pending events can be paid for
161
  if ( $EM_Event->get_bookings()->has_open_time() ) {
162
+ $balance = $this->core->get_users_balance( $EM_Booking->person->ID, $this->mycred_type );
163
  if ( $balance <= 0 ) return false;
164
 
165
  $price = $this->core->number( $EM_Booking->booking_price );
391
  global $mycred_em_pay;
392
 
393
  if ( $mycred_em_pay && is_object( $EM_Booking ) ) {
394
+ $balance = $this->core->get_users_balance( $EM_Booking->person->ID, $this->mycred_type );
395
  if ( $balance <= 0 ) return;
396
 
397
  $price = $EM_Booking->booking_price;
addons/gateway/myCRED-addon-gateway.php CHANGED
@@ -3,9 +3,6 @@
3
  * Addon: Gateway
4
  * Addon URI: http://mycred.me/add-ons/gateway/
5
  * Version: 1.4
6
- * Description: Let your users pay using their <strong>my</strong>CRED points balance. Supported Carts: WooCommerce, MarketPress and WP E-Commerce. Supported Event Bookings: Event Espresso, Events Manager.
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
 
@@ -27,7 +24,6 @@ require_once myCRED_GATE_CART_DIR . 'mycred-wpecommerce.php';
27
  /**
28
  * Event Espresso
29
  */
30
- add_action( 'mycred_init', 'mycred_load_event_espresso3' );
31
  function mycred_load_event_espresso3() {
32
 
33
  if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) return;
@@ -37,27 +33,33 @@ function mycred_load_event_espresso3() {
37
  $gateway->load();
38
 
39
  }
 
40
 
41
  /**
42
  * Events Manager
43
  */
44
- add_action( 'mycred_init', 'mycred_load_events_manager' );
45
  function mycred_load_events_manager() {
46
 
47
  if ( ! defined( 'EM_VERSION' ) ) return;
48
 
49
  // Pro
50
  if ( class_exists( 'EM_Pro' ) && class_exists( 'EM_Gateways' ) ) {
 
51
  require_once myCRED_GATE_EVENT_DIR . 'mycred-eventsmanager-pro.php';
52
  EM_Gateways::register_gateway( 'mycred', 'EM_Gateway_myCRED' );
 
53
  }
 
54
  // Free
55
  else {
 
56
  require_once myCRED_GATE_EVENT_DIR . 'mycred-eventsmanager.php';
57
  $events = new myCRED_Events_Manager_Gateway();
58
  $events->load();
 
59
  }
60
 
61
  }
 
62
 
63
  ?>
3
  * Addon: Gateway
4
  * Addon URI: http://mycred.me/add-ons/gateway/
5
  * Version: 1.4
 
 
 
6
  */
7
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
 
24
  /**
25
  * Event Espresso
26
  */
 
27
  function mycred_load_event_espresso3() {
28
 
29
  if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) return;
33
  $gateway->load();
34
 
35
  }
36
+ add_action( 'mycred_init', 'mycred_load_event_espresso3' );
37
 
38
  /**
39
  * Events Manager
40
  */
 
41
  function mycred_load_events_manager() {
42
 
43
  if ( ! defined( 'EM_VERSION' ) ) return;
44
 
45
  // Pro
46
  if ( class_exists( 'EM_Pro' ) && class_exists( 'EM_Gateways' ) ) {
47
+
48
  require_once myCRED_GATE_EVENT_DIR . 'mycred-eventsmanager-pro.php';
49
  EM_Gateways::register_gateway( 'mycred', 'EM_Gateway_myCRED' );
50
+
51
  }
52
+
53
  // Free
54
  else {
55
+
56
  require_once myCRED_GATE_EVENT_DIR . 'mycred-eventsmanager.php';
57
  $events = new myCRED_Events_Manager_Gateway();
58
  $events->load();
59
+
60
  }
61
 
62
  }
63
+ add_action( 'mycred_init', 'mycred_load_events_manager' );
64
 
65
  ?>
addons/notifications/myCRED-addon-notifications.php CHANGED
@@ -3,14 +3,11 @@
3
  * Addon: Notifications
4
  * Addon URI: http://mycred.me/add-ons/notifications/
5
  * Version: 1.1.2
6
- * Description: Notify your users when their balances changes.
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
 
12
  define( 'myCRED_NOTE', __FILE__ );
13
- define( 'myCRED_NOTE_VERSION', myCRED_VERSION . '.1' );
14
 
15
  /**
16
  * myCRED_Notifications class
@@ -37,7 +34,7 @@ if ( ! class_exists( 'myCRED_Notifications_Module' ) ) :
37
  'add_to_core' => true
38
  ) );
39
 
40
- add_filter( 'mycred_add_finished', array( $this, 'mycred_finished' ), 90, 3 );
41
 
42
  }
43
 
@@ -191,7 +188,7 @@ if ( ! class_exists( 'myCRED_Notifications_Module' ) ) :
191
  $prefs = $this->notifications;
192
 
193
  ?>
194
- <h4><div class="icon icon-active"></div><?php _e( 'Notifications', 'mycred' ); ?></h4>
195
  <div class="body" style="display:none;">
196
  <label class="subheader" for="<?php echo $this->field_id( 'use_css' ); ?>"><?php _e( 'Styling', 'mycred' ); ?></label>
197
  <ol>
@@ -254,11 +251,24 @@ jQuery(function($) {
254
 
255
  }
256
  }
 
257
 
258
- $notice = new myCRED_Notifications_Module();
259
- $notice->load();
 
 
 
 
 
260
 
 
 
 
 
 
 
261
  endif;
 
262
 
263
  /**
264
  * Add Notice
3
  * Addon: Notifications
4
  * Addon URI: http://mycred.me/add-ons/notifications/
5
  * Version: 1.1.2
 
 
 
6
  */
7
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
 
9
  define( 'myCRED_NOTE', __FILE__ );
10
+ define( 'myCRED_NOTE_VERSION', '1.1.2' );
11
 
12
  /**
13
  * myCRED_Notifications class
34
  'add_to_core' => true
35
  ) );
36
 
37
+ add_filter( 'mycred_add_finished', array( $this, 'mycred_finished' ), 40, 3 );
38
 
39
  }
40
 
188
  $prefs = $this->notifications;
189
 
190
  ?>
191
+ <h4><span class="dashicons dashicons-admin-plugins static"></span><?php _e( 'Notifications', 'mycred' ); ?></h4>
192
  <div class="body" style="display:none;">
193
  <label class="subheader" for="<?php echo $this->field_id( 'use_css' ); ?>"><?php _e( 'Styling', 'mycred' ); ?></label>
194
  <ol>
251
 
252
  }
253
  }
254
+ endif;
255
 
256
+ /**
257
+ * Load Notifications Module
258
+ * @since 1.7
259
+ * @version 1.0
260
+ */
261
+ if ( ! function_exists( 'mycred_load_notices_addon' ) ) :
262
+ function mycred_load_notices_addon( $modules, $point_types ) {
263
 
264
+ $modules['solo']['notices'] = new myCRED_Notifications_Module();
265
+ $modules['solo']['notices']->load();
266
+
267
+ return $modules;
268
+
269
+ }
270
  endif;
271
+ add_filter( 'mycred_load_modules', 'mycred_load_notices_addon', 70, 2 );
272
 
273
  /**
274
  * Add Notice
addons/ranks/assets/js/management.js CHANGED
@@ -3,7 +3,7 @@
3
  * @since 1.3
4
  * @version 1.1.1
5
  */
6
- (function($) {
7
 
8
  var mycred_action_delete_ranks = function( button, pointtype ) {
9
  var label = button.val();
@@ -91,4 +91,4 @@
91
  }
92
  });
93
 
94
- })( jQuery );
3
  * @since 1.3
4
  * @version 1.1.1
5
  */
6
+ jQuery(function($){
7
 
8
  var mycred_action_delete_ranks = function( button, pointtype ) {
9
  var label = button.val();
91
  }
92
  });
93
 
94
+ });
addons/ranks/includes/mycred-rank-functions.php CHANGED
@@ -6,39 +6,37 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
6
  * Checks if there are any rank posts.
7
  * @returns (bool) true or false
8
  * @since 1.1
9
- * @version 1.5
10
  */
11
  if ( ! function_exists( 'mycred_have_ranks' ) ) :
12
- function mycred_have_ranks( $type = NULL ) {
13
 
14
  global $wpdb;
15
 
16
  if ( ! mycred_override_settings() ) {
17
- $posts = $wpdb->posts;
18
  $postmeta = $wpdb->postmeta;
19
  }
20
  else {
21
- $posts = $wpdb->base_prefix . 'posts';
22
  $postmeta = $wpdb->base_prefix . 'postmeta';
23
  }
24
 
25
  $type_filter = '';
26
- if ( $type !== NULL && sanitize_text_field( $type ) != '' )
27
- $type_filter = $wpdb->prepare( "
28
- INNER JOIN {$postmeta} ctype
29
- ON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s AND ctype.meta_value = %s )", 'ctype', $type );
30
 
31
  $mycred_ranks = $wpdb->get_var( "
32
  SELECT COUNT(*)
33
- FROM {$posts}
34
  {$type_filter}
35
- WHERE post_type = 'mycred_rank';" );
36
 
37
  $return = false;
38
- if ( $mycred_ranks > 0 )
39
  $return = true;
40
 
41
- return apply_filters( 'mycred_have_ranks', $return, $mycred_ranks );
42
 
43
  }
44
  endif;
@@ -51,24 +49,22 @@ endif;
51
  * @version 1.2
52
  */
53
  if ( ! function_exists( 'mycred_get_published_ranks_count' ) ) :
54
- function mycred_get_published_ranks_count( $type = NULL ) {
55
 
56
  global $wpdb;
57
 
58
  if ( ! mycred_override_settings() ) {
59
- $posts = $wpdb->posts;
60
  $postmeta = $wpdb->postmeta;
61
  }
62
  else {
63
- $posts = $wpdb->base_prefix . 'posts';
64
  $postmeta = $wpdb->base_prefix . 'postmeta';
65
  }
66
 
67
  $type_filter = '';
68
- if ( $type !== NULL && sanitize_text_field( $type ) != '' )
69
- $type_filter = $wpdb->prepare( "
70
- INNER JOIN {$postmeta} ctype
71
- ON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s AND ctype.meta_value = %s )", 'ctype', $type );
72
 
73
  $count = $wpdb->get_var( "
74
  SELECT COUNT(*)
@@ -77,233 +73,193 @@ if ( ! function_exists( 'mycred_get_published_ranks_count' ) ) :
77
  WHERE ranks.post_type = 'mycred_rank'
78
  AND ranks.post_status = 'publish';" );
79
 
80
- return apply_filters( 'mycred_get_published_ranks_count', $count );
 
 
81
 
82
  }
83
  endif;
84
 
85
  /**
86
- * Assign Ranks
87
- * Runs though all user balances and assigns each users their
88
- * appropriate ranks.
89
- * @returns (int) number of users effected by rank change or -1 if all users were effected.
90
- * @since 1.3.2
91
- * @version 1.4
92
  */
93
- if ( ! function_exists( 'mycred_assign_ranks' ) ) :
94
- function mycred_assign_ranks( $type = 'mycred_default' ) {
95
 
96
- global $wpdb;
97
 
98
- $mycred = mycred( $type );
99
 
100
- // Get rank key
101
- $rank_meta_key = 'mycred_rank';
102
- if ( $mycred->is_multisite && $GLOBALS['blog_id'] > 1 && ! $mycred->use_master_template )
103
- $rank_meta_key .= '_' . $GLOBALS['blog_id'];
104
 
105
- do_action( 'mycred_assign_ranks_start' );
 
 
 
 
 
 
 
 
 
106
 
107
- if ( ! mycred_override_settings() ) {
108
- $posts = $wpdb->posts;
109
- $postmeta = $wpdb->postmeta;
110
- }
111
- else {
112
- $posts = $wpdb->base_prefix . 'posts';
113
- $postmeta = $wpdb->base_prefix . 'postmeta';
114
  }
115
 
116
- // Check for published ranks
117
- $published_ranks = mycred_get_published_ranks_count( $type );
118
 
119
- // Point Type Filter
120
- $type_filter = '';
121
- if ( $type !== NULL && sanitize_text_field( $type ) != '' ) {
122
 
123
- $type_filter = $wpdb->prepare( "
124
- INNER JOIN {$postmeta} ctype
125
- ON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s AND ctype.meta_value = %s )", 'ctype', $type );
126
 
127
- if ( $type != 'mycred_default' )
128
- $rank_meta_key .= $type;
129
 
130
- }
 
 
131
 
132
- $result = 0;
133
-
134
- if ( $published_ranks > 0 ) {
135
-
136
- // Get balance key for this type
137
- $balance_key = $type;
138
-
139
- if ( $mycred->is_multisite && $GLOBALS['blog_id'] > 1 && ! $mycred->use_central_logging )
140
- $balance_key .= '_' . $GLOBALS['blog_id'];
141
-
142
- if ( isset( $mycred->rank['base'] ) && $mycred->rank['base'] == 'total' )
143
- $balance_key .= '_total';
144
-
145
- /*
146
- Get all user balances
147
- $users = array(
148
- [0] => array(
149
- [ID] => user id
150
- [balance] => current balance
151
- )
152
- );
153
- */
154
- $users = $wpdb->get_results( $wpdb->prepare( "
155
- SELECT user_id AS ID, meta_value AS balance
156
- FROM {$wpdb->usermeta}
157
- WHERE meta_key = %s;", $balance_key ), 'ARRAY_A' );
158
-
159
- /*
160
- Get rank ids with each ranks min and max values
161
- $ranks = array(
162
- [0] => array(
163
- [ID] => rank id
164
- [min] => mycred_rank_min value
165
- [max] => mycred_rank_max value
166
- )
167
- );
168
- */
169
- $ranks = $wpdb->get_results( "
170
- SELECT ranks.ID AS ID, min.meta_value AS min, max.meta_value AS max
171
- FROM {$posts} ranks
172
- {$type_filter}
173
- INNER JOIN {$postmeta} min
174
- ON ( min.post_id = ranks.ID AND min.meta_key = 'mycred_rank_min' )
175
- INNER JOIN {$postmeta} max
176
- ON ( max.post_id = ranks.ID AND max.meta_key = 'mycred_rank_max' )
177
- WHERE ranks.post_type = 'mycred_rank'
178
- AND ranks.post_status = 'publish';", 'ARRAY_A' );
179
-
180
- $count = 0;
181
- foreach ( $users as $user ) {
182
- foreach ( $ranks as $rank ) {
183
- if ( $user['balance'] >= $rank['min'] && $user['balance'] <= $rank['max'] ) {
184
-
185
- $end = '';
186
- if ( $type != 'mycred_default' )
187
- $end = $type;
188
-
189
- mycred_update_user_meta( $user['ID'], 'mycred_rank', $end, $rank['ID'] );
190
- $count = $count+1;
191
- break 1;
192
-
193
- }
194
- }
195
  }
196
- $wpdb->flush();
197
-
198
- $result = $count;
199
- }
200
 
201
- do_action( 'mycred_assign_ranks_end' );
202
 
203
- return $result;
 
 
204
 
205
  }
206
  endif;
207
 
 
208
  /**
209
- * Get Ranks
210
- * Retreaves a given rank.
211
- * @param $rank_title (string) the rank title (case sensitive)
212
- * @param $format (string) optional output type: OBJECT, ARRAY_N or ARRAY_A
213
- * @uses get_page_by_title()
214
- * @returns empty string if title is missing, NULL if rank is not found else the output type
215
  * @since 1.1
216
  * @version 1.3
217
  */
218
  if ( ! function_exists( 'mycred_get_rank' ) ) :
219
- function mycred_get_rank( $rank_title = '', $format = 'OBJECT' ) {
220
 
221
- if ( empty( $rank_title ) ) return $rank_title;
 
222
 
223
- if ( ! mycred_override_settings() )
224
- $rank = get_page_by_title( $rank_title, $format, 'mycred_rank' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
 
226
  else {
 
227
  $original_blog_id = get_current_blog_id();
228
  switch_to_blog( 1 );
229
 
230
- $rank = get_page_by_title( $rank_title, $format, 'mycred_rank' );
 
231
 
232
  switch_to_blog( $original_blog_id );
 
233
  }
234
 
235
- return apply_filters( 'mycred_get_rank', $rank, $rank_title, $format );
236
 
237
  }
238
  endif;
239
 
240
  /**
241
- * Get Users Rank
242
- * Retreaves the users current saved rank or if rank is missing
243
- * finds the appropriate rank and saves it.
244
- * @param $user_id (int) required user id to check
245
- * @param $return (string) post detail to return, defaults to post_title
246
- * @param $logo_size (string) if $return is set to 'logo', the size of the logo to return
247
- * @param $attr (array) if $return is set to 'logo', optional logo image attributes
248
- * @param $type (string) optional point type
249
- * @uses mycred_find_users_rank()
250
- * @uses get_the_title()
251
- * @returns (string) rank object item requested or - if no ranks exists
252
  * @since 1.1
253
- * @version 1.4
254
  */
255
- if ( ! function_exists( 'mycred_get_users_rank' ) ) :
256
- function mycred_get_users_rank( $user_id = NULL, $return = 'post_title', $logo_size = 'post-thumbnail', $attr = NULL, $type = 'mycred_default' ) {
257
 
258
- // User ID is required
259
- if ( $user_id === NULL )
260
- return __( 'mycred_get_users_rank() : Missing required user id', 'mycred' );
261
 
262
- $end = '';
263
- if ( $type != 'mycred_default' )
264
- $end = $type;
265
 
266
- // Get users rank
267
- $rank_id = mycred_get_user_meta( $user_id, 'mycred_rank', $end, true );
268
 
269
- // If empty, get the users rank now and save it
270
- if ( $rank_id == '' )
271
- $rank_id = mycred_find_users_rank( $user_id, true, $type );
272
 
273
- $reply = __( 'no rank', 'mycred' );
 
274
 
275
- // Have rank
276
- if ( $rank_id != '' && $rank_id !== NULL ) {
277
 
278
- // If we want to see the logo
279
- if ( $return == 'logo' )
280
- $reply = mycred_get_rank_logo( (int) $rank_id, $logo_size, $attr );
281
-
282
- // Else get the post object
283
- else {
284
 
285
- // Not using master template
286
- if ( ! mycred_override_settings() )
287
- $rank = get_post( (int) $rank_id );
288
 
289
- // Master template enforced
290
- else {
291
- $original_blog_id = get_current_blog_id();
292
- switch_to_blog( 1 );
293
 
294
- $rank = get_post( (int) $rank_id );
 
 
 
 
 
 
 
 
 
295
 
296
- switch_to_blog( $original_blog_id );
297
- }
298
 
299
- // If the requested detail exists, return it
300
- if ( isset( $rank->$return ) )
301
- $reply = $rank->$return;
302
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
 
304
  }
305
 
306
- return apply_filters( 'mycred_get_users_rank', $reply, $user_id, $return, $logo_size );
307
 
308
  }
309
  endif;
@@ -315,15 +271,24 @@ endif;
315
  * @version 1.0
316
  */
317
  if ( ! function_exists( 'mycred_get_users_rank_id' ) ) :
318
- function mycred_get_users_rank_id( $user_id = NULL, $type = 'mycred_default' ) {
319
 
320
  $end = '';
321
- if ( $type != 'mycred_default' )
322
  $end = $type;
323
 
324
  $rank_id = mycred_get_user_meta( $user_id, 'mycred_rank', $end, true );
325
- if ( $rank_id == '' )
326
- $rank_id = mycred_find_users_rank( $user_id, true, $type );
 
 
 
 
 
 
 
 
 
327
 
328
  return $rank_id;
329
 
@@ -331,49 +296,104 @@ if ( ! function_exists( 'mycred_get_users_rank_id' ) ) :
331
  endif;
332
 
333
  /**
334
- * Find Users Rank
335
- * Compares the given users points balance with existing ranks to determain
336
- * where the user fits in.
337
- * @param $user_id (int) required user id
338
- * @param $save (bool) option to save the rank to the given users meta data
339
- * @param $type (string) optional point type
340
- * @uses $wpdb
341
- * @returns (string) users rank ID.
 
 
 
 
 
 
 
 
 
 
 
342
  * @since 1.1
343
  * @version 1.5
344
  */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
  if ( ! function_exists( 'mycred_find_users_rank' ) ) :
346
- function mycred_find_users_rank( $user_id = NULL, $save = false, $type = 'mycred_default' ) {
347
 
348
  global $wpdb;
349
 
350
- $mycred = mycred( $type );
 
 
 
 
351
 
352
  // In case user id is not set
353
  if ( $user_id === NULL )
354
  $user_id = get_current_user_id();
355
 
356
- // Get current balanace
357
- $current_balance = $wpdb->get_var( $wpdb->prepare( "
358
- SELECT meta_value
359
- FROM {$wpdb->usermeta}
360
- WHERE user_id = %d
361
- AND meta_key = %s;", $user_id, $type ) );
362
-
363
- if ( $current_balance === NULL )
364
- $current_balance = 0;
365
 
366
  // If ranks are based on total we get the total balance which in turn
367
  // if not set will default to the users current balance.
368
- if ( mycred_rank_based_on_total( $type ) ) {
369
 
370
- $balance = mycred_query_users_total( $user_id, $type );
371
- if ( $balance == 0 )
372
- $balance = $current_balance;
 
 
 
 
 
 
 
373
 
374
  }
375
- else
376
- $balance = $current_balance;
377
 
378
  // Prep format for the db query
379
  $balance_format = '%d';
@@ -382,89 +402,96 @@ if ( ! function_exists( 'mycred_find_users_rank' ) ) :
382
 
383
  // Get the appropriate post tables
384
  if ( ! mycred_override_settings() ) {
385
- $posts = $wpdb->posts;
386
  $postmeta = $wpdb->postmeta;
387
  }
388
  else {
389
- $posts = $wpdb->base_prefix . 'posts';
390
  $postmeta = $wpdb->base_prefix . 'postmeta';
391
  }
392
 
393
- $type_filter = $wpdb->prepare( "
394
- INNER JOIN {$postmeta} ctype
395
- ON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s AND ctype.meta_value = %s )", 'ctype', $type );
396
-
397
- // Get the rank based on balance
398
- $rank_id = $wpdb->get_var( $wpdb->prepare( "
399
- SELECT ranks.ID
400
  FROM {$posts} ranks
401
- {$type_filter}
402
- INNER JOIN {$postmeta} min
403
- ON ( ranks.ID = min.post_id AND min.meta_key = 'mycred_rank_min' )
404
- INNER JOIN {$postmeta} max
405
- ON ( ranks.ID = max.post_id AND max.meta_key = 'mycred_rank_max' )
406
- WHERE ranks.post_type = 'mycred_rank'
407
  AND ranks.post_status = 'publish'
408
  AND {$balance_format} BETWEEN min.meta_value AND max.meta_value
409
- LIMIT 0,1;", $balance ) );
410
 
411
- // Let others play
412
- if ( $rank_id !== NULL ) {
413
 
414
- if ( mycred_user_got_demoted( $user_id, $rank_id ) )
415
- do_action( 'mycred_user_got_demoted', $user_id, $rank_id );
 
416
 
417
- elseif ( mycred_user_got_promoted( $user_id, $rank_id ) )
418
- do_action( 'mycred_user_got_promoted', $user_id, $rank_id );
 
419
 
420
- }
 
 
421
 
422
- $end = '';
423
- if ( $type != 'mycred_default' )
424
- $end = $type;
425
 
426
- // Save if requested
427
- if ( $save && $rank_id !== NULL )
428
- mycred_update_user_meta( $user_id, 'mycred_rank', $end, $rank_id );
429
 
430
- return apply_filters( 'mycred_find_users_rank', $rank_id, $user_id, $save, $type );
431
 
432
  }
433
  endif;
434
 
435
  /**
436
- * Get Rank ID from Title
437
- * Used to get the rank object based on the ranks title.
438
- * @param $title (string) required rank title
439
- * @uses mycred_get_rank()
440
- * @returns empty (string) if title is missing, NULL if rank is not found else (string) the rank.
441
- * @since 1.1
442
- * @version 1.1
443
  */
444
- if ( ! function_exists( 'mycred_get_rank_id_from_title' ) ) :
445
- function mycred_get_rank_id_from_title( $title ) {
446
 
447
- $rank = mycred_get_rank( $title );
448
- if ( ! isset( $rank->ID ) )
449
- $return = '';
450
- else
451
- $return = $rank->ID;
452
 
453
- return apply_filters( 'mycred_get_rank_id_from_title', $return, $title );
454
 
455
- }
456
- endif;
 
457
 
458
- /**
459
- * Get My Rank
460
- * Returns the current users rank
461
- * @since 1.1
462
- * @version 1.0
463
- */
464
- if ( ! function_exists( 'mycred_get_my_rank' ) ) :
465
- function mycred_get_my_rank() {
466
 
467
- return mycred_get_users_rank( get_current_user_id() );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
 
469
  }
470
  endif;
@@ -478,10 +505,10 @@ endif;
478
  * @param $type (string) optional point type
479
  * @returns (array) empty if no ranks are found or associative array with post ID as key and title as value
480
  * @since 1.1
481
- * @version 1.4
482
  */
483
  if ( ! function_exists( 'mycred_get_ranks' ) ) :
484
- function mycred_get_ranks( $status = 'publish', $number = '-1', $order = 'DESC', $type = NULL ) {
485
 
486
  global $wpdb;
487
 
@@ -491,7 +518,7 @@ if ( ! function_exists( 'mycred_get_ranks' ) ) :
491
 
492
  // Limit
493
  if ( $number != '-1' )
494
- $limit = ' 0,' . absint( $number );
495
  else
496
  $limit = '';
497
 
@@ -505,33 +532,27 @@ if ( ! function_exists( 'mycred_get_ranks' ) ) :
505
  }
506
 
507
  $type_filter = '';
508
- if ( $type !== NULL && sanitize_text_field( $type ) != '' )
509
- $type_filter = $wpdb->prepare( "
510
- INNER JOIN {$postmeta} ctype
511
- ON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s AND ctype.meta_value = %s )", 'ctype', $type );
512
 
513
  // Get ranks
514
- $all_ranks = $wpdb->get_results( $wpdb->prepare( "
515
- SELECT *
 
516
  FROM {$posts} ranks
517
- INNER JOIN {$postmeta} min
518
- ON ( ranks.ID = min.post_id AND min.meta_key = %s )
519
  {$type_filter}
520
- WHERE ranks.post_type = %s
 
521
  AND ranks.post_status = %s
522
 
523
- ORDER BY min.meta_value+0 {$order} {$limit};", 'mycred_rank_min', 'mycred_rank', $status ) );
524
-
525
- // Sort
526
- $ranks = array();
527
- if ( ! empty( $all_ranks ) ) {
528
-
529
- foreach ( $all_ranks as $rank )
530
- $ranks[ $rank->ID ] = $rank;
531
 
 
 
 
532
  }
533
 
534
- return apply_filters( 'mycred_get_ranks', $ranks, $status, $number, $order );
535
 
536
  }
537
  endif;
@@ -544,274 +565,53 @@ endif;
544
  * @param $number (int) number of users to return
545
  * @returns (array) empty if no users were found or associative array with user ID as key and display name as value
546
  * @since 1.1
547
- * @version 1.4
548
  */
549
  if ( ! function_exists( 'mycred_get_users_of_rank' ) ) :
550
- function mycred_get_users_of_rank( $rank_id, $number = NULL, $order = 'DESC', $type = 'mycred_default' ) {
551
-
552
- if ( ! is_numeric( $rank_id ) )
553
- $rank_id = mycred_get_rank_id_from_title( $rank_id );
554
-
555
- if ( $rank_id === NULL ) return '';
556
-
557
- global $wpdb;
558
-
559
- $mycred = mycred( $type );
560
-
561
- $balance_key = $type;
562
-
563
- if ( $mycred->is_multisite && $GLOBALS['blog_id'] > 1 && ! $mycred->use_central_logging )
564
- $balance_key .= '_' . $GLOBALS['blog_id'];
565
-
566
- if ( mycred_rank_based_on_total( $type ) )
567
- $balance_key .= '_total';
568
-
569
- $rank_meta_key = 'mycred_rank';
570
- if ( $mycred->is_multisite && $GLOBALS['blog_id'] > 1 && ! $mycred->use_master_template )
571
- $rank_meta_key .= '_' . $GLOBALS['blog_id'];
572
-
573
- if ( $type != 'mycred_default' )
574
- $rank_meta_key .= $type;
575
-
576
- $sql = "
577
- SELECT rank.user_id
578
- FROM {$wpdb->usermeta} rank
579
- INNER JOIN {$wpdb->usermeta} balance
580
- ON ( rank.user_id = balance.user_id AND balance.meta_key = %s )";
581
-
582
- $sql .= "
583
- WHERE rank.meta_key = %s
584
- AND rank.meta_value = %d
585
- ORDER BY balance.meta_value+0";
586
-
587
- // Order
588
- if ( $order == 'ASC' )
589
- $sql .= ' ASC';
590
- else
591
- $sql .= ' DESC';
592
-
593
- // Limit
594
- if ( $number !== NULL )
595
- $sql .= ' LIMIT 0,' . abs( $number ) . ';';
596
- else
597
- $sql .= ';';
598
-
599
- // Run query
600
- $users = $wpdb->get_results( $wpdb->prepare( $sql, $balance_key, $rank_meta_key, $rank_id ), 'ARRAY_A' );
601
- $wpdb->flush();
602
-
603
- return apply_filters( 'mycred_get_users_of_rank', $users, $rank_id, $number, $order, $type );
604
-
605
- }
606
- endif;
607
-
608
- /**
609
- * Count Users with Rank
610
- * @since 1.6
611
- * @version 1.0
612
- */
613
- if ( ! function_exists( 'mycred_count_users_with_rank' ) ) :
614
- function mycred_count_users_with_rank( $rank_id = NULL ) {
615
-
616
- if ( $rank_id === NULL ) return 0;
617
-
618
- $type = get_post_meta( $rank_id, 'ctype', true );
619
- if ( $type == '' ) return 0;
620
 
621
- $mycred = mycred( $type );
622
-
623
- $rank_meta_key = 'mycred_rank';
624
- if ( $mycred->is_multisite && $GLOBALS['blog_id'] > 1 && ! $mycred->use_master_template )
625
- $rank_meta_key .= '_' . $GLOBALS['blog_id'];
626
-
627
- if ( $type != 'mycred_default' )
628
- $rank_meta_key .= $type;
629
 
630
  global $wpdb;
631
 
632
- $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( user_id ) FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %d;", $rank_meta_key, $rank_id ) );
633
-
634
- if ( $count === NULL )
635
- $count = 0;
636
-
637
- return $count;
638
-
639
- }
640
- endif;
641
-
642
- /**
643
- * Rank Has Logo
644
- * Checks if a given rank has a logo.
645
- * @param $rank_id (int|string) either the rank id or the rank title
646
- * @uses mycred_get_rank_id_from_title()
647
- * @uses has_post_thumbnail()
648
- * @returns (bool) true or false
649
- * @since 1.1
650
- * @version 1.2
651
- */
652
- if ( ! function_exists( 'mycred_rank_has_logo' ) ) :
653
- function mycred_rank_has_logo( $rank_id ) {
654
-
655
- if ( ! is_numeric( $rank_id ) )
656
- $rank_id = mycred_get_rank_id_from_title( $rank_id );
657
-
658
- $return = false;
659
- if ( ! mycred_override_settings() ) {
660
- if ( has_post_thumbnail( $rank_id ) )
661
- $return = true;
662
- }
663
- else {
664
- $original_blog_id = get_current_blog_id();
665
- switch_to_blog( 1 );
666
-
667
- if ( has_post_thumbnail( $rank_id ) )
668
- $return = true;
669
-
670
- switch_to_blog( $original_blog_id );
671
- }
672
-
673
- return apply_filters( 'mycred_rank_has_logo', $return, $rank_id );
674
-
675
- }
676
- endif;
677
-
678
- /**
679
- * Get Rank Logo
680
- * Returns the given ranks logo.
681
- * @param $rank_id (int|string) either the rank id or the rank title
682
- * @param $size (string|array) see http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
683
- * @param $attr (string|array) see http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
684
- * @uses mycred_get_rank_id_from_title()
685
- * @uses mycred_rank_has_logo()
686
- * @uses get_the_post_thumbnail()
687
- * @returns empty string if rank does not that logo or the HTML IMG element with given size and attribute
688
- * @since 1.1
689
- * @version 1.2
690
- */
691
- if ( ! function_exists( 'mycred_get_rank_logo' ) ) :
692
- function mycred_get_rank_logo( $rank_id, $size = 'post-thumbnail', $attr = NULL ) {
693
-
694
- if ( ! is_numeric( $rank_id ) )
695
- $rank_id = mycred_get_rank_id_from_title( $rank_id );
696
-
697
- if ( ! mycred_rank_has_logo( $rank_id ) ) return '';
698
-
699
- if ( is_numeric( $size ) )
700
- $size = array( $size, $size );
701
-
702
- if ( ! mycred_override_settings() )
703
- $logo = get_the_post_thumbnail( $rank_id, $size, $attr );
704
-
705
- else {
706
- $original_blog_id = get_current_blog_id();
707
- switch_to_blog( 1 );
708
-
709
- $logo = get_the_post_thumbnail( $rank_id, $size, $attr );
710
-
711
- switch_to_blog( $original_blog_id );
712
- }
713
-
714
- return apply_filters( 'mycred_get_rank_logo', $logo, $rank_id, $size, $attr );
715
-
716
- }
717
- endif;
718
-
719
- /**
720
- * User Got Demoted
721
- * Checks if a user got demoted.
722
- * @since 1.3.3
723
- * @version 1.4
724
- */
725
- if ( ! function_exists( 'mycred_user_got_demoted' ) ) :
726
- function mycred_user_got_demoted( $user_id = NULL, $rank_id = NULL ) {
727
-
728
- $type = get_post_meta( $rank_id, 'ctype', true );
729
- if ( $type == '' ) {
730
- $type = 'mycred_default';
731
- update_post_meta( $rank_id, 'ctype', $type );
732
- }
733
 
734
  $end = '';
735
- if ( $type != 'mycred_default' )
736
- $end = $type;
737
-
738
- $current_rank_id = mycred_get_user_meta( $user_id, 'mycred_rank', $end, true );
739
 
740
- // No demotion
741
- if ( $current_rank_id == $rank_id ) return false;
742
 
743
- // User did not have a rank before but will have now, that is assumed to be a promotion
744
- if ( empty( $current_rank_id ) && ! empty( $rank_id ) ) return false;
745
-
746
- // Get minimums
747
  if ( ! mycred_override_settings() ) {
748
- $current_min = get_post_meta( $current_rank_id, 'mycred_rank_min', true );
749
- $new_min = get_post_meta( $rank_id, 'mycred_rank_min', true );
750
  }
751
  else {
752
- $original_blog_id = get_current_blog_id();
753
- switch_to_blog( 1 );
754
-
755
- $current_min = get_post_meta( $current_rank_id, 'mycred_rank_min', true );
756
- $new_min = get_post_meta( $rank_id, 'mycred_rank_min', true );
757
-
758
- switch_to_blog( $original_blog_id );
759
- }
760
-
761
- // Compare
762
- if ( $new_min < $current_min ) return true;
763
-
764
- return false;
765
-
766
- }
767
- endif;
768
-
769
- /**
770
- * User Got Promoted
771
- * Checks if a user got promoted.
772
- * @since 1.3.3
773
- * @version 1.4
774
- */
775
- if ( ! function_exists( 'mycred_user_got_promoted' ) ) :
776
- function mycred_user_got_promoted( $user_id = NULL, $rank_id = NULL ) {
777
-
778
- $type = get_post_meta( $rank_id, 'ctype', true );
779
- if ( $type == '' ) {
780
- $type = 'mycred_default';
781
- update_post_meta( $rank_id, 'ctype', $type );
782
  }
783
 
784
- $end = '';
785
- if ( $type != 'mycred_default' )
786
- $end = $type;
787
-
788
- $current_rank_id = mycred_get_user_meta( $user_id, 'mycred_rank', $end, true );
789
-
790
- // No promotion
791
- if ( $current_rank_id == $rank_id ) return false;
792
-
793
- // User did not have a rank before but will have now, that is assumed to be a promotion
794
- if ( empty( $current_rank_id ) && ! empty( $rank_id ) ) return true;
795
-
796
- // Get minimums
797
- if ( ! mycred_override_settings() ) {
798
- $current_min = get_post_meta( $current_rank_id, 'mycred_rank_min', true );
799
- $new_min = get_post_meta( $rank_id, 'mycred_rank_min', true );
800
- }
801
- else {
802
- $original_blog_id = get_current_blog_id();
803
- switch_to_blog( 1 );
804
-
805
- $current_min = get_post_meta( $current_rank_id, 'mycred_rank_min', true );
806
- $new_min = get_post_meta( $rank_id, 'mycred_rank_min', true );
807
 
808
- switch_to_blog( $original_blog_id );
809
- }
 
 
 
810
 
811
- // Compare
812
- if ( $new_min > $current_min ) return true;
 
 
 
 
 
813
 
814
- return false;
815
 
816
  }
817
  endif;
@@ -824,10 +624,10 @@ endif;
824
  * @version 1.0
825
  */
826
  if ( ! function_exists( 'mycred_rank_based_on_total' ) ) :
827
- function mycred_rank_based_on_total( $type = 'mycred_default' ) {
828
 
829
  $prefs_key = 'mycred_pref_core';
830
- if ( $type != 'mycred_default' )
831
  $prefs_key .= '_' . $type;
832
 
833
  $prefs = get_option( $prefs_key );
@@ -845,16 +645,12 @@ endif;
845
  * Rank Shown in BuddyPress
846
  * Returns either false or the location where the rank is to be shown in BuddyPress.
847
  * @since 1.6
848
- * @version 1.0
849
  */
850
  if ( ! function_exists( 'mycred_show_rank_in_buddypress' ) ) :
851
- function mycred_show_rank_in_buddypress( $type = 'mycred_default' ) {
852
-
853
- $prefs_key = 'mycred_pref_core';
854
- if ( $type != 'mycred_default' )
855
- $prefs_key .= '_' . $type;
856
 
857
- $prefs = get_option( $prefs_key );
858
 
859
  $result = false;
860
  if ( isset( $prefs['rank']['bb_location'] ) && $prefs['rank']['bb_location'] != '' )
@@ -869,16 +665,12 @@ endif;
869
  * Rank Shown in bbPress
870
  * Returns either false or the location where the rank is to be shown in bbPress.
871
  * @since 1.6
872
- * @version 1.0
873
  */
874
  if ( ! function_exists( 'mycred_show_rank_in_bbpress' ) ) :
875
- function mycred_show_rank_in_bbpress( $type = 'mycred_default' ) {
876
 
877
- $prefs_key = 'mycred_pref_core';
878
- if ( $type != 'mycred_default' )
879
- $prefs_key .= '_' . $type;
880
-
881
- $prefs = get_option( $prefs_key );
882
 
883
  $result = false;
884
  if ( isset( $prefs['rank']['bp_location'] ) && $prefs['rank']['bp_location'] != '' )
6
  * Checks if there are any rank posts.
7
  * @returns (bool) true or false
8
  * @since 1.1
9
+ * @version 1.5.1
10
  */
11
  if ( ! function_exists( 'mycred_have_ranks' ) ) :
12
+ function mycred_have_ranks( $point_type = NULL ) {
13
 
14
  global $wpdb;
15
 
16
  if ( ! mycred_override_settings() ) {
17
+ $posts = $wpdb->posts;
18
  $postmeta = $wpdb->postmeta;
19
  }
20
  else {
21
+ $posts = $wpdb->base_prefix . 'posts';
22
  $postmeta = $wpdb->base_prefix . 'postmeta';
23
  }
24
 
25
  $type_filter = '';
26
+ if ( $point_type !== NULL && mycred_point_type_exists( sanitize_key( $point_type ) ) )
27
+ $type_filter = $wpdb->prepare( "INNER JOIN {$postmeta} ctype ON ( ranks.ID = ctype.post_id AND ctype.meta_key = 'ctype' AND ctype.meta_value = %s )", $point_type );
 
 
28
 
29
  $mycred_ranks = $wpdb->get_var( "
30
  SELECT COUNT(*)
31
+ FROM {$posts} ranks
32
  {$type_filter}
33
+ WHERE ranks.post_type = 'mycred_rank';" );
34
 
35
  $return = false;
36
+ if ( $mycred_ranks !== NULL && $mycred_ranks > 0 )
37
  $return = true;
38
 
39
+ return apply_filters( 'mycred_have_ranks', $return, $point_type );
40
 
41
  }
42
  endif;
49
  * @version 1.2
50
  */
51
  if ( ! function_exists( 'mycred_get_published_ranks_count' ) ) :
52
+ function mycred_get_published_ranks_count( $point_type = NULL ) {
53
 
54
  global $wpdb;
55
 
56
  if ( ! mycred_override_settings() ) {
57
+ $posts = $wpdb->posts;
58
  $postmeta = $wpdb->postmeta;
59
  }
60
  else {
61
+ $posts = $wpdb->base_prefix . 'posts';
62
  $postmeta = $wpdb->base_prefix . 'postmeta';
63
  }
64
 
65
  $type_filter = '';
66
+ if ( $point_type !== NULL && mycred_point_type_exists( sanitize_key( $point_type ) ) )
67
+ $type_filter = $wpdb->prepare( "INNER JOIN {$postmeta} ctype ON ( ranks.ID = ctype.post_id AND ctype.meta_key = 'ctype' AND ctype.meta_value = %s )", $point_type );
 
 
68
 
69
  $count = $wpdb->get_var( "
70
  SELECT COUNT(*)
73
  WHERE ranks.post_type = 'mycred_rank'
74
  AND ranks.post_status = 'publish';" );
75
 
76
+ if ( $count === NULL ) $count = 0;
77
+
78
+ return apply_filters( 'mycred_get_published_ranks_count', $count, $point_type );
79
 
80
  }
81
  endif;
82
 
83
  /**
84
+ * Get Rank Object ID
85
+ * Makes sure a given post ID is a rank post ID or converts a rank title into a rank ID.
86
+ * @since 1.7
87
+ * @version 1.0
 
 
88
  */
89
+ if ( ! function_exists( 'mycred_get_rank_object_id' ) ) :
90
+ function mycred_get_rank_object_id( $identifier = NULL ) {
91
 
92
+ if ( $identifier === NULL ) return false;
93
 
94
+ $rank_id = false;
95
 
96
+ if ( ! mycred_override_settings() ) {
 
 
 
97
 
98
+ if ( absint( $identifier ) !== 0 && get_post_type( absint( $identifier ) ) === 'mycred_rank' )
99
+ $rank_id = absint( $identifier );
100
+
101
+ else {
102
+
103
+ $rank = get_page_by_title( $identifier, OBJECT, 'mycred_rank' );
104
+ if ( isset( $rank->post_type ) && $rank->post_type === 'mycred_rank' )
105
+ $rank_id = $rank->ID;
106
+
107
+ }
108
 
 
 
 
 
 
 
 
109
  }
110
 
111
+ else {
 
112
 
113
+ $original_blog_id = get_current_blog_id();
114
+ switch_to_blog( 1 );
 
115
 
116
+ if ( absint( $identifier ) !== 0 && get_post_type( absint( $identifier ) ) === 'mycred_rank' )
117
+ $rank_id = absint( $identifier );
 
118
 
119
+ else {
 
120
 
121
+ $rank = get_page_by_title( $identifier, OBJECT, 'mycred_rank' );
122
+ if ( isset( $rank->post_type ) && $rank->post_type === 'mycred_rank' )
123
+ $rank_id = $rank->ID;
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  }
 
 
 
 
126
 
127
+ switch_to_blog( $original_blog_id );
128
 
129
+ }
130
+
131
+ return $rank_id;
132
 
133
  }
134
  endif;
135
 
136
+
137
  /**
138
+ * Get Rank
139
+ * Returns the rank object.
 
 
 
 
140
  * @since 1.1
141
  * @version 1.3
142
  */
143
  if ( ! function_exists( 'mycred_get_rank' ) ) :
144
+ function mycred_get_rank( $rank_identifier = NULL ) {
145
 
146
+ $rank_id = mycred_get_rank_object_id( $rank_identifier );
147
+ if ( $rank_id === false ) return false;
148
 
149
+ $rank = new myCRED_Rank( $rank_id );
150
+
151
+ return $rank;
152
+
153
+ }
154
+ endif;
155
+
156
+ /**
157
+ * Rank Has Logo
158
+ * Checks if a given rank has a logo.
159
+ * @since 1.1
160
+ * @version 1.3
161
+ */
162
+ if ( ! function_exists( 'mycred_rank_has_logo' ) ) :
163
+ function mycred_rank_has_logo( $rank_identifier = NULL ) {
164
+
165
+ $rank_id = mycred_get_rank_object_id( $rank_identifier );
166
+ if ( $rank_id === false ) return false;
167
+
168
+ $return = false;
169
+ if ( ! mycred_override_settings() ) {
170
+
171
+ if ( has_post_thumbnail( $rank_id ) )
172
+ $return = true;
173
 
174
+ }
175
  else {
176
+
177
  $original_blog_id = get_current_blog_id();
178
  switch_to_blog( 1 );
179
 
180
+ if ( has_post_thumbnail( $rank_id ) )
181
+ $return = true;
182
 
183
  switch_to_blog( $original_blog_id );
184
+
185
  }
186
 
187
+ return apply_filters( 'mycred_rank_has_logo', $return, $rank_id );
188
 
189
  }
190
  endif;
191
 
192
  /**
193
+ * Get Rank Logo
194
+ * Returns the given ranks logo.
 
 
 
 
 
 
 
 
 
195
  * @since 1.1
196
+ * @version 1.3
197
  */
198
+ if ( ! function_exists( 'mycred_get_rank_logo' ) ) :
199
+ function mycred_get_rank_logo( $rank_identifier = NULL, $size = 'post-thumbnail', $attr = NULL ) {
200
 
201
+ $rank_id = mycred_get_rank_object_id( $rank_identifier );
202
+ if ( $rank_id === false ) return false;
 
203
 
204
+ if ( is_numeric( $size ) )
205
+ $size = array( $size, $size );
 
206
 
207
+ if ( ! mycred_override_settings() )
208
+ $logo = get_the_post_thumbnail( $rank_id, $size, $attr );
209
 
210
+ else {
 
 
211
 
212
+ $original_blog_id = get_current_blog_id();
213
+ switch_to_blog( 1 );
214
 
215
+ $logo = get_the_post_thumbnail( $rank_id, $size, $attr );
 
216
 
217
+ switch_to_blog( $original_blog_id );
 
 
 
 
 
218
 
219
+ }
 
 
220
 
221
+ return apply_filters( 'mycred_get_rank_logo', $logo, $rank_id, $size, $attr );
 
 
 
222
 
223
+ }
224
+ endif;
225
+
226
+ /**
227
+ * Count Users with Rank
228
+ * @since 1.6
229
+ * @version 1.1
230
+ */
231
+ if ( ! function_exists( 'mycred_count_users_with_rank' ) ) :
232
+ function mycred_count_users_with_rank( $rank_identifier = NULL ) {
233
 
234
+ $rank_id = mycred_get_rank_object_id( $rank_identifier );
235
+ if ( $rank_id === false ) return 0;
236
 
237
+ $user_count = get_post_meta( $rank_id, 'mycred_rank_users', true );
238
+ if ( $user_count == '' ) {
239
+
240
+ $type = get_post_meta( $rank_id, 'ctype', true );
241
+ if ( $type == '' ) return 0;
242
+
243
+ $mycred = mycred( $type );
244
+
245
+ $rank_meta_key = 'mycred_rank';
246
+ if ( $mycred->is_multisite && $GLOBALS['blog_id'] > 1 && ! $mycred->use_master_template )
247
+ $rank_meta_key .= '_' . $GLOBALS['blog_id'];
248
+
249
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY )
250
+ $rank_meta_key .= $type;
251
+
252
+ global $wpdb;
253
+
254
+ $user_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( user_id ) FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %d;", $rank_meta_key, $rank_id ) );
255
+
256
+ if ( $user_count === NULL ) $user_count = 0;
257
+
258
+ update_post_meta( $rank_id, 'mycred_rank_users', $user_count );
259
 
260
  }
261
 
262
+ return $user_count;
263
 
264
  }
265
  endif;
271
  * @version 1.0
272
  */
273
  if ( ! function_exists( 'mycred_get_users_rank_id' ) ) :
274
+ function mycred_get_users_rank_id( $user_id = NULL, $type = MYCRED_DEFAULT_TYPE_KEY ) {
275
 
276
  $end = '';
277
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY )
278
  $end = $type;
279
 
280
  $rank_id = mycred_get_user_meta( $user_id, 'mycred_rank', $end, true );
281
+ if ( $rank_id == '' ) {
282
+
283
+ $rank = mycred_find_users_rank( $user_id, $type );
284
+
285
+ // Found a rank, save it
286
+ if ( $rank !== false ) {
287
+ mycred_update_user_meta( $user_id, 'mycred_rank', $end, $rank->rank_id );
288
+ $rank_id = $rank->rank_id;
289
+ }
290
+
291
+ }
292
 
293
  return $rank_id;
294
 
296
  endif;
297
 
298
  /**
299
+ * Get My Rank
300
+ * Returns the current users rank
301
+ * @since 1.1
302
+ * @version 1.1
303
+ */
304
+ if ( ! function_exists( 'mycred_get_my_rank' ) ) :
305
+ function mycred_get_my_rank() {
306
+
307
+ if ( ! is_user_logged_in() ) return;
308
+
309
+ return mycred_get_users_rank( get_current_user_id() );
310
+
311
+ }
312
+ endif;
313
+
314
+ /**
315
+ * Get Users Rank
316
+ * Retreaves the users current saved rank or if rank is missing
317
+ * finds the appropriate rank and saves it.
318
  * @since 1.1
319
  * @version 1.5
320
  */
321
+ if ( ! function_exists( 'mycred_get_users_rank' ) ) :
322
+ function mycred_get_users_rank( $user_id = NULL, $type = MYCRED_DEFAULT_TYPE_KEY ) {
323
+
324
+ // User ID is required
325
+ if ( $user_id === NULL || ! is_numeric( $user_id ) ) return false;
326
+
327
+ $end = '';
328
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY )
329
+ $end = $type;
330
+
331
+ // Get users rank
332
+ $rank_id = mycred_get_user_meta( $user_id, 'mycred_rank', $end, true );
333
+
334
+ // No rank, try to assign one
335
+ if ( $rank_id == '' ) {
336
+
337
+ $rank = mycred_find_users_rank( $user_id, $type );
338
+
339
+ // Found a rank, save it
340
+ if ( $rank !== false ) {
341
+ mycred_update_user_meta( $user_id, 'mycred_rank', $end, $rank->rank_id );
342
+ $rank_id = $rank->rank_id;
343
+ }
344
+
345
+ }
346
+
347
+ // Get Rank object
348
+ $rank = mycred_get_rank( $rank_id );
349
+
350
+ return apply_filters( 'mycred_get_users_rank', $rank, $user_id, $rank_id );
351
+
352
+ }
353
+ endif;
354
+
355
+ /**
356
+ * Find Users Rank
357
+ * Attenots to find a particular users rank for a particular point type.
358
+ * @uses mycred_user_got_demoted if user got demoted to a lower rank.
359
+ * @uses mycred_user_got_promoted if user got promoted to a higher rank.
360
+ * @since 1.1
361
+ * @version 1.6
362
+ */
363
  if ( ! function_exists( 'mycred_find_users_rank' ) ) :
364
+ function mycred_find_users_rank( $user_id = NULL, $point_type = MYCRED_DEFAULT_TYPE_KEY, $act = true ) {
365
 
366
  global $wpdb;
367
 
368
+ $mycred = mycred( $point_type );
369
+
370
+ $end = '';
371
+ if ( $point_type != MYCRED_DEFAULT_TYPE_KEY )
372
+ $end = $point_type;
373
 
374
  // In case user id is not set
375
  if ( $user_id === NULL )
376
  $user_id = get_current_user_id();
377
 
378
+ // Non logged in users have ID 0.
379
+ if ( absint( $user_id ) === 0 ) return false;
 
 
 
 
 
 
 
380
 
381
  // If ranks are based on total we get the total balance which in turn
382
  // if not set will default to the users current balance.
383
+ if ( mycred_rank_based_on_total( $point_type ) ) {
384
 
385
+ // Since usermeta might be cached we can not trust the amount so we must query the DB for a fresh value.
386
+ $balance = mycred_query_users_total( $user_id, $point_type );
387
+
388
+ }
389
+ else {
390
+
391
+ // Since usermeta might be cached we can not trust the amount so we must query the DB for a fresh value.
392
+ $balance = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key = %s;", $user_id, mycred_get_meta_key( $point_type ) ) );
393
+ if ( $balance === NULL )
394
+ $balance = 0;
395
 
396
  }
 
 
397
 
398
  // Prep format for the db query
399
  $balance_format = '%d';
402
 
403
  // Get the appropriate post tables
404
  if ( ! mycred_override_settings() ) {
405
+ $posts = $wpdb->posts;
406
  $postmeta = $wpdb->postmeta;
407
  }
408
  else {
409
+ $posts = $wpdb->base_prefix . 'posts';
410
  $postmeta = $wpdb->base_prefix . 'postmeta';
411
  }
412
 
413
+ // See where the users balance fits in
414
+ $results = $wpdb->get_row( $wpdb->prepare( "
415
+ SELECT ranks.ID AS rank_id, min.meta_value AS minimum, max.meta_value AS maximum, meta.meta_value AS current_id
 
 
 
 
416
  FROM {$posts} ranks
417
+ INNER JOIN {$postmeta} ctype ON ( ranks.ID = ctype.post_id AND ctype.meta_key = 'ctype' AND ctype.meta_value = %s )
418
+ INNER JOIN {$postmeta} min ON ( ranks.ID = min.post_id AND min.meta_key = 'mycred_rank_min' )
419
+ INNER JOIN {$postmeta} max ON ( ranks.ID = max.post_id AND max.meta_key = 'mycred_rank_max' )
420
+ LEFT JOIN {$wpdb->usermeta} meta ON ( meta.user_id = %d AND meta.meta_key = %s )
421
+ WHERE ranks.post_type = 'mycred_rank'
 
422
  AND ranks.post_status = 'publish'
423
  AND {$balance_format} BETWEEN min.meta_value AND max.meta_value
424
+ LIMIT 0,1;", $point_type, $user_id, mycred_get_meta_key( 'mycred_rank', $end ), $balance ) );
425
 
426
+ // Found a new rank
427
+ if ( $act === true && isset( $results->rank_id ) && $results->rank_id !== $results->current_id ) {
428
 
429
+ // Demotions
430
+ if ( $results->current_id !== NULL && get_post_meta( $results->current_id, 'mycred_rank_max', true ) > $results->maximum )
431
+ do_action( 'mycred_user_got_demoted', $user_id, $results->rank_id, $results );
432
 
433
+ // Promotions
434
+ else
435
+ do_action( 'mycred_user_got_promoted', $user_id, $results->rank_id, $results );
436
 
437
+ // Reset counters
438
+ delete_post_meta( $results->current_id, 'mycred_rank_users' );
439
+ delete_post_meta( $results->rank_id, 'mycred_rank_users' );
440
 
441
+ }
 
 
442
 
443
+ if ( $results === NULL )
444
+ $results = false;
 
445
 
446
+ return apply_filters( 'mycred_find_users_rank', $results, $user_id, $point_type );
447
 
448
  }
449
  endif;
450
 
451
  /**
452
+ * Assign Ranks
453
+ * Runs though all user balances and assigns each users their
454
+ * appropriate ranks.
455
+ * @returns void
456
+ * @since 1.3.2
457
+ * @version 1.5
 
458
  */
459
+ if ( ! function_exists( 'mycred_assign_ranks' ) ) :
460
+ function mycred_assign_ranks( $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
461
 
462
+ global $wpdb;
 
 
 
 
463
 
464
+ $mycred = mycred( $point_type );
465
 
466
+ $end = '';
467
+ if ( $point_type != MYCRED_DEFAULT_TYPE_KEY )
468
+ $end = $point_type;
469
 
470
+ do_action( 'mycred_assign_ranks_start' );
 
 
 
 
 
 
 
471
 
472
+ $balance_format = '%d';
473
+ if ( isset( $mycred->format['decimals'] ) && $mycred->format['decimals'] > 0 )
474
+ $balance_format = 'CAST( %f AS DECIMAL( 10, ' . $mycred->format['decimals'] . ' ) )';
475
+
476
+ $ranks = mycred_get_ranks( 'publish', '-1', 'ASC', $point_type );
477
+
478
+ $count = 0;
479
+ if ( ! empty( $ranks ) ) {
480
+ foreach ( $ranks as $rank ) {
481
+
482
+ $count = $wpdb->query( $wpdb->prepare( "
483
+ UPDATE {$wpdb->usermeta} ranks
484
+ INNER JOIN {$wpdb->usermeta} balance ON ( ranks.user_id = balance.user_id AND balance.meta_key = %s )
485
+ SET ranks.meta_value = %d
486
+ WHERE ranks.meta_key = %s
487
+ AND balance.meta_value BETWEEN {$balance_format} AND {$balance_format};", mycred_get_meta_key( $point_type ), $rank->post_id, mycred_get_meta_key( 'mycred_rank', $end ), $rank->minimum, $rank->maximum ) );
488
+
489
+ }
490
+ }
491
+
492
+ do_action( 'mycred_assign_ranks_end' );
493
+
494
+ return $count;
495
 
496
  }
497
  endif;
505
  * @param $type (string) optional point type
506
  * @returns (array) empty if no ranks are found or associative array with post ID as key and title as value
507
  * @since 1.1
508
+ * @version 1.5
509
  */
510
  if ( ! function_exists( 'mycred_get_ranks' ) ) :
511
+ function mycred_get_ranks( $status = 'publish', $number = '-1', $order = 'DESC', $point_type = NULL ) {
512
 
513
  global $wpdb;
514
 
518
 
519
  // Limit
520
  if ( $number != '-1' )
521
+ $limit = 'LIMIT 0,' . absint( $number );
522
  else
523
  $limit = '';
524
 
532
  }
533
 
534
  $type_filter = '';
535
+ if ( $point_type !== NULL && mycred_point_type_exists( sanitize_key( $point_type ) ) )
536
+ $type_filter = $wpdb->prepare( "INNER JOIN {$postmeta} ctype ON ( ranks.ID = ctype.post_id AND ctype.meta_key = 'ctype' AND ctype.meta_value = %s )", $point_type );
 
 
537
 
538
  // Get ranks
539
+ $results = array();
540
+ $rank_ids = $wpdb->get_col( $wpdb->prepare( "
541
+ SELECT ranks.ID
542
  FROM {$posts} ranks
 
 
543
  {$type_filter}
544
+ LEFT JOIN {$postmeta} min ON ( ranks.ID = min.post_id AND min.meta_key = 'mycred_rank_min' )
545
+ WHERE ranks.post_type = 'mycred_rank'
546
  AND ranks.post_status = %s
547
 
548
+ ORDER BY min.meta_value+0 {$order} {$limit};", $status ) );
 
 
 
 
 
 
 
549
 
550
+ if ( ! empty( $rank_ids ) ) {
551
+ foreach ( $rank_ids as $rank_id )
552
+ $results[] = mycred_get_rank( $rank_id );
553
  }
554
 
555
+ return apply_filters( 'mycred_get_ranks', $results, $status, $number, $order );
556
 
557
  }
558
  endif;
565
  * @param $number (int) number of users to return
566
  * @returns (array) empty if no users were found or associative array with user ID as key and display name as value
567
  * @since 1.1
568
+ * @version 1.5
569
  */
570
  if ( ! function_exists( 'mycred_get_users_of_rank' ) ) :
571
+ function mycred_get_users_of_rank( $rank_identifier = NULL, $number = NULL, $order = 'DESC', $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
572
 
573
+ $rank_id = mycred_get_rank_object_id( $rank_identifier );
574
+ if ( $rank_id === false ) return false;
 
 
 
 
 
 
575
 
576
  global $wpdb;
577
 
578
+ $mycred = mycred( $point_type );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579
 
580
  $end = '';
581
+ if ( $point_type != MYCRED_DEFAULT_TYPE_KEY )
582
+ $end = $point_type;
 
 
583
 
584
+ $rank_meta_key = mycred_get_meta_key( 'mycred_rank', $end );
585
+ $balance_key = mycred_get_meta_key( $point_type );
586
 
 
 
 
 
587
  if ( ! mycred_override_settings() ) {
588
+ $posts = $wpdb->posts;
589
+ $postmeta = $wpdb->postmeta;
590
  }
591
  else {
592
+ $posts = $wpdb->base_prefix . 'posts';
593
+ $postmeta = $wpdb->base_prefix . 'postmeta';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594
  }
595
 
596
+ // Order
597
+ if ( ! in_array( $order, array( 'ASC', 'DESC' ) ) )
598
+ $order = 'DESC';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599
 
600
+ // Limit
601
+ if ( $number != '-1' )
602
+ $limit = 'LIMIT 0,' . absint( $number );
603
+ else
604
+ $limit = '';
605
 
606
+ $users = $wpdb->get_results( $wpdb->prepare( "
607
+ SELECT users.*, creds.meta_value AS balance
608
+ FROM {$wpdb->users} users
609
+ LEFT JOIN {$wpdb->usermeta} rank ON ( users.ID = rank.user_id AND rank.meta_key = %s
610
+ LEFT JOIN {$wpdb->usermeta} creds ON ( users.ID = creds.user_id AND creds.meta_key = %s
611
+ WHERE rank.meta_value = %d
612
+ ORDER BY creds.meta_value+0 {$order} {$limit}", $rank_meta_key, $balance_key ) );
613
 
614
+ return apply_filters( 'mycred_get_users_of_rank', $users, $rank_id, $number, $order, $point_type );
615
 
616
  }
617
  endif;
624
  * @version 1.0
625
  */
626
  if ( ! function_exists( 'mycred_rank_based_on_total' ) ) :
627
+ function mycred_rank_based_on_total( $type = MYCRED_DEFAULT_TYPE_KEY ) {
628
 
629
  $prefs_key = 'mycred_pref_core';
630
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY )
631
  $prefs_key .= '_' . $type;
632
 
633
  $prefs = get_option( $prefs_key );
645
  * Rank Shown in BuddyPress
646
  * Returns either false or the location where the rank is to be shown in BuddyPress.
647
  * @since 1.6
648
+ * @version 1.1
649
  */
650
  if ( ! function_exists( 'mycred_show_rank_in_buddypress' ) ) :
651
+ function mycred_show_rank_in_buddypress( $type = MYCRED_DEFAULT_TYPE_KEY ) {
 
 
 
 
652
 
653
+ $prefs = mycred_get_option( 'mycred_pref_core' );
654
 
655
  $result = false;
656
  if ( isset( $prefs['rank']['bb_location'] ) && $prefs['rank']['bb_location'] != '' )
665
  * Rank Shown in bbPress
666
  * Returns either false or the location where the rank is to be shown in bbPress.
667
  * @since 1.6
668
+ * @version 1.1
669
  */
670
  if ( ! function_exists( 'mycred_show_rank_in_bbpress' ) ) :
671
+ function mycred_show_rank_in_bbpress( $type = MYCRED_DEFAULT_TYPE_KEY ) {
672
 
673
+ $prefs = mycred_get_option( 'mycred_pref_core' );
 
 
 
 
674
 
675
  $result = false;
676
  if ( isset( $prefs['rank']['bp_location'] ) && $prefs['rank']['bp_location'] != '' )
addons/ranks/includes/mycred-rank-object.php ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * myCRED_Rank class
6
+ * @see http://codex.mycred.me/classes/mycred_rank/
7
+ * @since 1.7
8
+ * @version 1.0
9
+ */
10
+ if ( ! class_exists( 'myCRED_Rank' ) ) :
11
+ class myCRED_Rank extends myCRED_Object {
12
+
13
+ public $post_id = false;
14
+ public $post = false;
15
+ public $title = '';
16
+ public $minimum = NULL;
17
+ public $maximum = NULL;
18
+ public $count = 0;
19
+
20
+ public $has_logo = false;
21
+ public $logo_id = false;
22
+ public $logo_url = false;
23
+ public $image_width = false;
24
+ public $image_height = false;
25
+
26
+ public $point_type = false;
27
+
28
+ /**
29
+ * Construct
30
+ */
31
+ function __construct( $rank_id = NULL ) {
32
+
33
+ parent::__construct();
34
+
35
+ $rank_id = absint( $rank_id );
36
+ if ( $rank_id === 0 ) return;
37
+
38
+ if ( get_post_type( $rank_id ) != 'mycred_rank' ) return;
39
+
40
+ $this->image_width = MYCRED_RANK_WIDTH;
41
+ $this->image_height = MYCRED_RANK_HEIGHT;
42
+
43
+ $this->populate( $rank_id );
44
+
45
+ }
46
+
47
+ protected function populate( $rank_id = NULL ) {
48
+
49
+ $this->post_id = absint( $rank_id );
50
+ $this->post = get_post( $this->post_id );
51
+ $this->title = get_the_title( $this->post_id );
52
+ $this->minimum = get_post_meta( $this->post_id, 'mycred_rank_min', true );
53
+ $this->maximum = get_post_meta( $this->post_id, 'mycred_rank_max', true );
54
+ $this->count = mycred_count_users_with_rank( $this->post_id );
55
+
56
+ $this->has_logo = mycred_rank_has_logo( $this->post_id );
57
+ $this->logo_id = get_post_thumbnail_id( $this->post );
58
+ $this->logo_url = wp_get_attachment_url( $this->logo_id );
59
+
60
+ $point_type = get_post_meta( $this->post_id, 'ctype', true );
61
+ if ( ! mycred_point_type_exists( $point_type ) )
62
+ $point_type = MYCRED_DEFAULT_TYPE_KEY;
63
+
64
+ $this->point_type = new myCRED_Point_Type( $point_type );
65
+
66
+ }
67
+
68
+ public function get_image( $image = 'logo' ) {
69
+
70
+ if ( $image === 'logo' )
71
+ return '<img src="' . esc_url( $this->logo_url ) . '" alt="' . esc_attr( $this->title ) . '" width="' . $this->image_width . '" height="' . $this->image_height . '" />';
72
+
73
+ return '';
74
+
75
+ }
76
+
77
+ }
78
+ endif;
79
+
80
+ ?>
addons/ranks/includes/mycred-rank-shortcodes.php CHANGED
@@ -6,14 +6,14 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
6
  * Returns a given users rank
7
  * @see http://mycred.me/shortcodes/mycred_my_rank/
8
  * @since 1.1
9
- * @version 1.2
10
  */
11
  if ( ! function_exists( 'mycred_render_my_rank' ) ) :
12
- function mycred_render_my_rank( $atts, $content = NULL ) {
13
 
14
  extract( shortcode_atts( array(
15
- 'user_id' => '',
16
- 'ctype' => 'mycred_default',
17
  'show_title' => 1,
18
  'show_logo' => 0,
19
  'logo_size' => 'post-thumbnail',
@@ -22,29 +22,27 @@ if ( ! function_exists( 'mycred_render_my_rank' ) ) :
22
 
23
  if ( $user_id == '' && ! is_user_logged_in() ) return;
24
 
25
- if ( $user_id == 'author' ) {
26
- global $post;
27
- if ( ! isset( $post->ID ) ) return;
28
- $user_id = $post->post_author;
29
- }
30
-
31
- if ( $user_id == '' )
32
- $user_id = get_current_user_id();
33
-
34
- $rank_id = mycred_get_users_rank_id( $user_id, $ctype );
35
  $show = array();
36
-
37
- if ( $show_logo )
38
- $show[] = mycred_get_rank_logo( $rank_id, $logo_size );
 
 
 
 
39
 
40
- if ( $show_title )
41
- $show[] = get_the_title( $rank_id );
42
 
43
- if ( $first != 'logo' )
44
- $show = array_reverse( $show );
 
 
45
 
46
- if ( empty( $show ) ) return;
47
- return '<div class="mycred-my-rank">' . implode( ' ', $show ) . '</div>';
 
 
48
 
49
  }
50
  endif;
@@ -54,47 +52,59 @@ endif;
54
  * Returns the given users ranks.
55
  * @see http://mycred.me/shortcodes/mycred_my_rank/
56
  * @since 1.6
57
- * @version 1.0
58
  */
59
  if ( ! function_exists( 'mycred_render_my_ranks' ) ) :
60
- function mycred_render_my_ranks( $atts, $content = NULL ) {
61
 
62
  extract( shortcode_atts( array(
63
- 'user_id' => NULL,
64
  'show_title' => 1,
65
  'show_logo' => 0,
66
  'logo_size' => 'post-thumbnail',
67
  'first' => 'logo'
68
  ), $atts ) );
69
 
70
- if ( $user_id === NULL && ! is_user_logged_in() ) return;
71
- if ( $user_id === NULL )
72
- $user_id = get_current_user_id();
73
-
74
- $mycred_types = mycred_get_types();
75
- $show = array();
 
 
 
76
 
77
- foreach ( $mycred_types as $type_id => $label ) {
 
78
 
79
- $row = array();
80
- $rank_id = mycred_get_users_rank_id( $user_id, $type_id );
81
 
82
- if ( $show_logo )
83
- $row[] = mycred_get_rank_logo( $rank_id, $logo_size );
84
 
85
- if ( $show_title )
86
- $row[] = get_the_title( $rank_id );
 
 
 
87
 
88
- if ( $first != 'logo' )
89
- $row = array_reverse( $row );
 
 
 
 
 
90
 
91
- $show = array_merge( $row, $show );
92
 
93
  }
94
 
95
- if ( empty( $show ) ) return;
 
96
 
97
- return '<div class="mycred-all-ranks">' . implode( ' ', $show ) . '</div>';
98
 
99
  }
100
  endif;
@@ -112,11 +122,11 @@ if ( ! function_exists( 'mycred_render_users_of_rank' ) ) :
112
  extract( shortcode_atts( array(
113
  'rank_id' => NULL,
114
  'login' => '',
115
- 'number' => NULL,
116
  'wrap' => 'div',
117
  'col' => 1,
118
  'nothing' => __( 'No users found with this rank', 'mycred' ),
119
- 'ctype' => 'mycred_default',
120
  'order' => 'DESC'
121
  ), $atts ) );
122
 
@@ -125,7 +135,7 @@ if ( ! function_exists( 'mycred_render_users_of_rank' ) ) :
125
  return '<strong>ERROR</strong> ' . __( 'Rank ID is required!', 'mycred' );
126
 
127
  if ( $ctype == '' )
128
- $ctype = 'mycred_default';
129
 
130
  $mycred = mycred( $ctype );
131
 
@@ -144,8 +154,7 @@ if ( ! function_exists( 'mycred_render_users_of_rank' ) ) :
144
  $output = '';
145
 
146
  // Make sure rank exist
147
- if ( ! isset( $rank->ID ) )
148
- return '<strong>ERROR</strong> ' . __( 'Rank not found. Please check the id and try again.', 'mycred' );
149
 
150
  if ( $row_template === NULL || empty( $row_template ) )
151
  $row_template = '<p class="user-row">%user_profile_link% with %balance% %_plural%</p>';
@@ -163,7 +172,7 @@ if ( ! function_exists( 'mycred_render_users_of_rank' ) ) :
163
 
164
  // Loop
165
  foreach ( $users as $user )
166
- $output .= $mycred->template_tags_user( $row_template, $user['user_id'] );
167
 
168
  // Add support for table
169
  if ( $wrap != 'table' && ! empty( $wrap ) )
@@ -198,14 +207,14 @@ endif;
198
  * Returns all users fore every registered rank in order.
199
  * @see http://mycred.me/shortcodes/mycred_users_of_all_ranks/
200
  * @since 1.1
201
- * @version 1.1
202
  */
203
  if ( ! function_exists( 'mycred_render_users_of_all_ranks' ) ) :
204
  function mycred_render_users_of_all_ranks( $atts, $row_template = NULL ) {
205
 
206
  extract( shortcode_atts( array(
207
  'login' => '',
208
- 'number' => NULL,
209
  'ctype' => NULL,
210
  'show_logo' => 1,
211
  'logo_size' => 'post-thumbnail',
@@ -235,25 +244,23 @@ if ( ! function_exists( 'mycred_render_users_of_all_ranks' ) ) :
235
  $output .= '<div class="mycred-all-ranks-wrapper">' . "\n";
236
 
237
  // Loop though all ranks
238
- foreach ( $all_ranks as $rank_id => $rank ) {
239
 
240
  // Prep Slug
241
- $slug = $rank->post_name;
242
- if ( empty( $slug ) )
243
- $slug = str_replace( ' ', '-', strtolower( $rank->post_title ) );
244
 
245
  // Rank wrapper
246
- $output .= '<div class="mycred-rank rank-' . $slug . ' rank-' . $rank_id . '"><h2>';
247
 
248
  // Insert Logo
249
  if ( $show_logo )
250
- $output .= mycred_get_rank_logo( $rank_id, $logo_size );
251
 
252
  // Rank title
253
- $output .= $rank->post_title . '</h2>' . "\n";
254
 
255
  $attr = array(
256
- 'rank_id' => $rank_id,
257
  'number' => $number,
258
  'nothing' => $nothing,
259
  'wrap' => $wrap,
@@ -279,21 +286,20 @@ endif;
279
  * Returns a list of ranks with minimum and maximum point requirements.
280
  * @see http://mycred.me/shortcodes/mycred_list_ranks/
281
  * @since 1.1.1
282
- * @version 1.1
283
  */
284
  if ( ! function_exists( 'mycred_render_rank_list' ) ) :
285
  function mycred_render_rank_list( $atts, $content = NULL ) {
286
 
287
  extract( shortcode_atts( array(
288
- 'order' => 'DESC',
289
- 'ctype' => 'mycred_default',
290
- 'wrap' => 'div'
291
  ), $atts ) );
292
 
293
  if ( $content === NULL || empty( $content ) )
294
  $content = '<p>%rank% <span class="min">%min%</span> - <span class="max">%max%</span></p>';
295
 
296
- $mycred = mycred();
297
  $output = '';
298
  $all_ranks = mycred_get_ranks( 'publish', '-1', $order, $ctype );
299
 
@@ -302,13 +308,16 @@ if ( ! function_exists( 'mycred_render_rank_list' ) ) :
302
  $output .= '<' . $wrap . ' class="mycred-rank-list">';
303
  $content = apply_filters( 'mycred_rank_list', $content, $atts, $mycred );
304
 
305
- foreach ( $all_ranks as $rank_id => $rank ) {
 
 
306
 
307
- $row = str_replace( '%rank%', $rank->post_title, $content );
308
  $row = str_replace( '%rank_logo%', mycred_get_rank_logo( $rank_id ), $row );
309
- $row = str_replace( '%min%', get_post_meta( $rank_id, 'mycred_rank_min', true ), $row );
310
- $row = str_replace( '%max%', get_post_meta( $rank_id, 'mycred_rank_max', true ), $row );
311
- $row = str_replace( '%count%', count( mycred_get_users_of_rank( $rank_id ) ), $row );
 
312
  $row = $mycred->template_tags_general( $row );
313
 
314
  $output .= $row . "\n";
6
  * Returns a given users rank
7
  * @see http://mycred.me/shortcodes/mycred_my_rank/
8
  * @since 1.1
9
+ * @version 1.3
10
  */
11
  if ( ! function_exists( 'mycred_render_my_rank' ) ) :
12
+ function mycred_render_my_rank( $atts, $content = '' ) {
13
 
14
  extract( shortcode_atts( array(
15
+ 'user_id' => 'current',
16
+ 'ctype' => MYCRED_DEFAULT_TYPE_KEY,
17
  'show_title' => 1,
18
  'show_logo' => 0,
19
  'logo_size' => 'post-thumbnail',
22
 
23
  if ( $user_id == '' && ! is_user_logged_in() ) return;
24
 
 
 
 
 
 
 
 
 
 
 
25
  $show = array();
26
+ $user_id = mycred_get_user_id( $user_id );
27
+ $rank = mycred_get_users_rank( $user_id, $ctype );
28
+
29
+ if ( $rank !== false ) {
30
+
31
+ if ( $show_logo == 1 && $rank->has_logo )
32
+ $show[] = mycred_get_rank_logo( $rank->post_id, $logo_size );
33
 
34
+ if ( $show_title == 1 )
35
+ $show[] = $rank->title;
36
 
37
+ if ( $first != 'logo' )
38
+ $show = array_reverse( $show );
39
+
40
+ }
41
 
42
+ if ( ! empty( $show ) )
43
+ $content = '<div class="mycred-my-rank">' . implode( ' ', $show ) . '</div>';
44
+
45
+ return apply_filters( 'mycred_my_rank', $content, $user_id, $rank );
46
 
47
  }
48
  endif;
52
  * Returns the given users ranks.
53
  * @see http://mycred.me/shortcodes/mycred_my_rank/
54
  * @since 1.6
55
+ * @version 1.2
56
  */
57
  if ( ! function_exists( 'mycred_render_my_ranks' ) ) :
58
+ function mycred_render_my_ranks( $atts, $content = '' ) {
59
 
60
  extract( shortcode_atts( array(
61
+ 'user_id' => 'current',
62
  'show_title' => 1,
63
  'show_logo' => 0,
64
  'logo_size' => 'post-thumbnail',
65
  'first' => 'logo'
66
  ), $atts ) );
67
 
68
+ if ( $user_id == '' && ! is_user_logged_in() ) return;
69
+
70
+ $user_id = mycred_get_user_id( $user_id );
71
+ $mycred_types = mycred_get_usable_types();
72
+ $show = array();
73
+ $ranks = array();
74
+
75
+ // This user does not have any usable point types
76
+ if ( empty( $mycred_types ) ) return;
77
 
78
+ // Get the rank for each type
79
+ foreach ( $mycred_types as $type_id ) {
80
 
81
+ $row = array();
82
+ $rank = mycred_get_users_rank( $user_id, $type_id );
83
 
84
+ if ( $rank !== false ) {
 
85
 
86
+ if ( $show_logo == 1 && $rank->has_logo )
87
+ $show[] = mycred_get_rank_logo( $rank->post_id, $logo_size );
88
+
89
+ if ( $show_title == 1 )
90
+ $show[] = $rank->title;
91
 
92
+ if ( $first != 'logo' )
93
+ $show = array_reverse( $show );
94
+
95
+ }
96
+
97
+ if ( ! empty( $row ) )
98
+ $show = array_merge( '<div class="mycred-my-rank ' . $type_id . '">' . $row . '</div>', $show );
99
 
100
+ $ranks[] = $rank;
101
 
102
  }
103
 
104
+ if ( ! empty( $show ) )
105
+ $content = '<div class="mycred-all-my-ranks">' . implode( ' ', $show ) . '</div>';
106
 
107
+ return apply_filters( 'mycred_my_ranks', $content, $user_id, $ranks );
108
 
109
  }
110
  endif;
122
  extract( shortcode_atts( array(
123
  'rank_id' => NULL,
124
  'login' => '',
125
+ 'number' => 10,
126
  'wrap' => 'div',
127
  'col' => 1,
128
  'nothing' => __( 'No users found with this rank', 'mycred' ),
129
+ 'ctype' => MYCRED_DEFAULT_TYPE_KEY,
130
  'order' => 'DESC'
131
  ), $atts ) );
132
 
135
  return '<strong>ERROR</strong> ' . __( 'Rank ID is required!', 'mycred' );
136
 
137
  if ( $ctype == '' )
138
+ $ctype = MYCRED_DEFAULT_TYPE_KEY;
139
 
140
  $mycred = mycred( $ctype );
141
 
154
  $output = '';
155
 
156
  // Make sure rank exist
157
+ if ( ! isset( $rank->ID ) ) return;
 
158
 
159
  if ( $row_template === NULL || empty( $row_template ) )
160
  $row_template = '<p class="user-row">%user_profile_link% with %balance% %_plural%</p>';
172
 
173
  // Loop
174
  foreach ( $users as $user )
175
+ $output .= $mycred->template_tags_user( $row_template, false, $user );
176
 
177
  // Add support for table
178
  if ( $wrap != 'table' && ! empty( $wrap ) )
207
  * Returns all users fore every registered rank in order.
208
  * @see http://mycred.me/shortcodes/mycred_users_of_all_ranks/
209
  * @since 1.1
210
+ * @version 1.2
211
  */
212
  if ( ! function_exists( 'mycred_render_users_of_all_ranks' ) ) :
213
  function mycred_render_users_of_all_ranks( $atts, $row_template = NULL ) {
214
 
215
  extract( shortcode_atts( array(
216
  'login' => '',
217
+ 'number' => 10,
218
  'ctype' => NULL,
219
  'show_logo' => 1,
220
  'logo_size' => 'post-thumbnail',
244
  $output .= '<div class="mycred-all-ranks-wrapper">' . "\n";
245
 
246
  // Loop though all ranks
247
+ foreach ( $all_ranks as $rank ) {
248
 
249
  // Prep Slug
250
+ $slug = str_replace( ' ', '-', strtolower( $rank->title ) );
 
 
251
 
252
  // Rank wrapper
253
+ $output .= '<div class="mycred-rank rank-' . $slug . ' rank-' . $rank->post_id . '"><h2>';
254
 
255
  // Insert Logo
256
  if ( $show_logo )
257
+ $output .= mycred_get_rank_logo( $rank->post_id, $logo_size );
258
 
259
  // Rank title
260
+ $output .= $rank->title . '</h2>' . "\n";
261
 
262
  $attr = array(
263
+ 'rank_id' => $rank->post_id,
264
  'number' => $number,
265
  'nothing' => $nothing,
266
  'wrap' => $wrap,
286
  * Returns a list of ranks with minimum and maximum point requirements.
287
  * @see http://mycred.me/shortcodes/mycred_list_ranks/
288
  * @since 1.1.1
289
+ * @version 1.2
290
  */
291
  if ( ! function_exists( 'mycred_render_rank_list' ) ) :
292
  function mycred_render_rank_list( $atts, $content = NULL ) {
293
 
294
  extract( shortcode_atts( array(
295
+ 'order' => 'DESC',
296
+ 'ctype' => MYCRED_DEFAULT_TYPE_KEY,
297
+ 'wrap' => 'div'
298
  ), $atts ) );
299
 
300
  if ( $content === NULL || empty( $content ) )
301
  $content = '<p>%rank% <span class="min">%min%</span> - <span class="max">%max%</span></p>';
302
 
 
303
  $output = '';
304
  $all_ranks = mycred_get_ranks( 'publish', '-1', $order, $ctype );
305
 
308
  $output .= '<' . $wrap . ' class="mycred-rank-list">';
309
  $content = apply_filters( 'mycred_rank_list', $content, $atts, $mycred );
310
 
311
+ foreach ( $all_ranks as $rank ) {
312
+
313
+ $mycred = mycred( $rank->point_type );
314
 
315
+ $row = str_replace( '%rank%', $rank->title, $content );
316
  $row = str_replace( '%rank_logo%', mycred_get_rank_logo( $rank_id ), $row );
317
+ $row = str_replace( '%min%', $mycred->format_creds( $rank->minimum ), $row );
318
+ $row = str_replace( '%max%', $mycred->format_creds( $rank->maximum ), $row );
319
+ $row = str_replace( '%count%', $rank->count, $row );
320
+
321
  $row = $mycred->template_tags_general( $row );
322
 
323
  $output .= $row . "\n";
addons/ranks/myCRED-addon-ranks.php CHANGED
@@ -2,18 +2,24 @@
2
  /**
3
  * Addon: Ranks
4
  * Addon URI: http://mycred.me/add-ons/ranks/
5
- * Version: 1.4.1
6
- * Description: Award your users rank based on their current or total balance. Supports multiple point types.
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
 
12
  define( 'myCRED_RANKS', __FILE__ );
13
  define( 'myCRED_RANKS_DIR', myCRED_ADDONS_DIR . 'ranks/' );
14
- define( 'myCRED_RANKS_VERSION', myCRED_VERSION . '.1' );
 
 
 
 
 
 
 
 
15
 
16
  require_once myCRED_RANKS_DIR . 'includes/mycred-rank-functions.php';
 
17
  require_once myCRED_RANKS_DIR . 'includes/mycred-rank-shortcodes.php';
18
 
19
  /**
@@ -22,7 +28,7 @@ require_once myCRED_RANKS_DIR . 'includes/mycred-rank-shortcodes.php';
22
  * points, ranks are titles that can be given to users when their reach a certain
23
  * amount.
24
  * @since 1.1
25
- * @version 1.3
26
  */
27
  if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
28
  class myCRED_Ranks_Module extends myCRED_Module {
@@ -52,7 +58,8 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
52
  )
53
  ),
54
  'register' => false,
55
- 'add_to_core' => false
 
56
  ) );
57
 
58
  if ( ! isset( $this->rank['order'] ) )
@@ -105,20 +112,20 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
105
  */
106
  public function module_init() {
107
 
108
- $this->register_post_type();
109
  $this->add_default_rank();
110
  $this->add_multiple_point_types_support();
111
 
112
- add_action( 'pre_get_posts', array( $this, 'adjust_wp_query' ), 20 );
113
- add_action( 'mycred_admin_enqueue', array( $this, 'enqueue_scripts' ) );
114
 
115
  // Instances to update ranks
116
- add_action( 'transition_post_status', array( $this, 'post_status_change' ), 99, 3 );
117
 
118
  // BuddyPress
119
  if ( class_exists( 'BuddyPress' ) ) {
120
- add_action( 'bp_before_member_header_meta', array( $this, 'insert_rank_header' ) );
121
- add_action( 'bp_after_profile_loop_content', array( $this, 'insert_rank_profile' ) );
122
  }
123
 
124
  // bbPress
@@ -128,14 +135,14 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
128
  }
129
 
130
  // Shortcodes
131
- add_shortcode( 'mycred_my_rank', 'mycred_render_my_rank' );
132
- add_shortcode( 'mycred_my_ranks', 'mycred_render_my_ranks' );
133
- add_shortcode( 'mycred_users_of_rank', 'mycred_render_users_of_rank' );
134
- add_shortcode( 'mycred_users_of_all_ranks', 'mycred_render_users_of_all_ranks' );
135
- add_shortcode( 'mycred_list_ranks', 'mycred_render_rank_list' );
136
 
137
  // Admin Management items
138
- add_action( 'wp_ajax_mycred-calc-totals', array( $this, 'calculate_totals' ) );
139
 
140
  }
141
 
@@ -146,23 +153,25 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
146
  */
147
  public function module_admin_init() {
148
 
149
- add_filter( 'parent_file', array( $this, 'parent_file' ) );
 
 
150
 
151
  add_filter( 'manage_mycred_rank_posts_columns', array( $this, 'adjust_column_headers' ), 50 );
152
  add_action( 'manage_mycred_rank_posts_custom_column', array( $this, 'adjust_column_content' ), 10, 2 );
153
 
154
- add_filter( 'mycred_users_balance_column', array( $this, 'custom_user_column_content' ), 10, 3 );
155
 
156
- add_filter( 'post_row_actions', array( $this, 'adjust_row_actions' ), 10, 2 );
 
 
157
 
158
- add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
159
- add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
160
 
161
- add_action( 'add_meta_boxes_mycred_rank', array( $this, 'add_meta_boxes' ) );
162
- add_action( 'save_post_mycred_rank', array( $this, 'save_rank_settings' ) );
163
 
164
- add_action( 'wp_ajax_mycred-action-delete-ranks', array( $this, 'action_delete_ranks' ) );
165
- add_action( 'wp_ajax_mycred-action-assign-ranks', array( $this, 'action_assign_ranks' ) );
166
 
167
  }
168
 
@@ -177,9 +186,9 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
177
  add_action( 'mycred_after_core_prefs', array( $this, 'after_general_settings' ) );
178
  add_filter( 'mycred_save_core_prefs', array( $this, 'sanitize_extra_settings' ), 90, 3 );
179
 
180
- if ( count( $this->point_types ) > 1 ) {
181
 
182
- add_action( 'admin_menu', array( $this, 'add_menus' ) );
183
 
184
  $priority = 10;
185
  foreach ( $this->point_types as $type_id => $label ) {
@@ -196,6 +205,111 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
196
 
197
  }
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  /**
200
  * Add Admin Menu Item
201
  * @since 1.6
@@ -203,12 +317,12 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
203
  */
204
  public function add_menus() {
205
 
206
- $cap = $this->core->edit_plugin_cap();
207
 
208
  foreach ( $this->point_types as $type_id => $label ) {
209
 
210
  add_submenu_page(
211
- ( $type_id == 'mycred_default' ) ? 'myCRED' : 'myCRED_' . $type_id,
212
  __( 'Ranks', 'mycred' ),
213
  __( 'Ranks', 'mycred' ),
214
  $cap,
@@ -230,128 +344,54 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
230
 
231
  if ( ( $pagenow == 'edit.php' || $pagenow == 'post-new.php' ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_rank' ) {
232
 
233
- if ( isset( $_GET['ctype'] ) && $_GET['ctype'] != 'mycred_default' )
234
- return 'myCRED_' . $_GET['ctype'];
235
  else
236
- return 'myCRED';
237
 
238
  }
239
 
240
- return $parent;
241
-
242
- }
243
 
244
- /**
245
- * Zero Balance Action
246
- * When an admin selects to zero out all balances
247
- * we want to remove all ranks as well.
248
- * @since 1.6
249
- * @version 1.0
250
- */
251
- public function zero_balance_action( $type = '' ) {
252
-
253
- global $wpdb;
254
-
255
- // Get rank key
256
- $rank_meta_key = 'mycred_rank';
257
- if ( $this->core->is_multisite && $GLOBALS['blog_id'] > 1 && ! $this->core->use_master_template )
258
- $rank_meta_key .= '_' . $GLOBALS['blog_id'];
259
 
260
- if ( array_key_exists( $type, $this->point_types ) && $type != 'mycred_default' )
261
- $rank_meta_key .= $type;
262
 
263
- $wpdb->delete(
264
- $wpdb->usermeta,
265
- array( 'meta_key' => $rank_meta_key ),
266
- array( '%s' )
267
- );
268
 
269
  }
270
 
271
  /**
272
- * Delete Ranks
273
- * @since 1.3.2
274
- * @version 1.1
275
  */
276
- public function action_delete_ranks() {
277
 
278
- // Security
279
- check_ajax_referer( 'mycred-management-actions-roles', 'token' );
280
-
281
- // Define type
282
- $type = 'mycred_default';
283
- if ( isset( $_POST['ctype'] ) && array_key_exists( $_POST['ctype'], $this->point_types ) )
284
- $type = sanitize_text_field( $_POST['ctype'] );
285
 
286
- global $wpdb;
287
 
288
- // Get the appropriate tables based on setup
289
- if ( ! mycred_override_settings() ) {
290
- $posts = $wpdb->posts;
291
- $postmeta = $wpdb->postmeta;
292
- }
293
- else {
294
- $posts = $wpdb->base_prefix . 'posts';
295
- $postmeta = $wpdb->base_prefix . 'postmeta';
296
  }
297
 
298
- // First get the ids of all existing ranks
299
- $rank_ids = $wpdb->get_col( $wpdb->prepare( "
300
- SELECT DISTINCT ranks.ID
301
- FROM {$posts} ranks
302
- INNER JOIN {$postmeta} ctype
303
- ON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s )
304
- WHERE ranks.post_type = %s
305
- AND ctype.meta_value = %s;", 'ctype', 'mycred_rank', $type ) );
306
-
307
- // If ranks were found
308
- $rows = 0;
309
- if ( ! empty( $rank_ids ) ) {
310
-
311
- $id_list = implode( ',', $rank_ids );
312
 
313
- // Remove posts
314
- $wpdb->query( "
315
- DELETE FROM {$posts}
316
- WHERE post_type = 'mycred_rank'
317
- AND post_id IN ({$id_list});" );
318
-
319
- // Remove post meta
320
- $wpdb->query( "
321
- DELETE FROM {$postmeta}
322
- WHERE post_id IN ({$id_list});" );
323
-
324
- // Confirm that ranks are gone by counting ranks
325
- // If all went well this should return zero.
326
- $rows = $wpdb->get_var( $wpdb->prepare( "
327
- SELECT COUNT(*)
328
- FROM {$posts} ranks
329
- INNER JOIN {$postmeta} ctype
330
- ON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s )
331
- WHERE ranks.post_type = %s
332
- AND ctype.meta_value = %s;", 'ctype', 'mycred_rank', $type ) );
333
 
334
  }
335
 
336
- die( json_encode( array( 'status' => 'OK', 'rows' => $rows ) ) );
337
-
338
- }
339
-
340
- /**
341
- * Assign Ranks
342
- * @since 1.3.2
343
- * @version 1.1
344
- */
345
- public function action_assign_ranks() {
346
-
347
- check_ajax_referer( 'mycred-management-actions-roles', 'token' );
348
-
349
- $type = 'mycred_default';
350
- if ( isset( $_POST['ctype'] ) && array_key_exists( $_POST['ctype'], $this->point_types ) )
351
- $type = sanitize_text_field( $_POST['ctype'] );
352
-
353
- $adjustments = mycred_assign_ranks( $type );
354
- die( json_encode( array( 'status' => 'OK', 'rows' => $adjustments ) ) );
355
 
356
  }
357
 
@@ -367,153 +407,17 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
367
 
368
  }
369
 
370
- /**
371
- * Enqueue Scripts & Styles
372
- * @since 1.1
373
- * @version 1.3
374
- */
375
- public function enqueue_scripts() {
376
-
377
- $adjust_header = false;
378
- $screen = get_current_screen();
379
-
380
- // Ranks List Page
381
- if ( preg_match( '/(edit-mycred_rank)/', $screen->id, $matches ) ) {
382
-
383
- wp_enqueue_style( 'mycred-admin' );
384
-
385
- if ( isset( $_GET['ctype'] ) && array_key_exists( $_GET['ctype'], $this->point_types ) ) :
386
-
387
- wp_register_script(
388
- 'mycred-rank-tweaks',
389
- plugins_url( 'assets/js/tweaks.js', myCRED_RANKS ),
390
- array( 'jquery' ),
391
- myCRED_VERSION . '.1'
392
- );
393
- wp_localize_script(
394
- 'mycred-rank-tweaks',
395
- 'myCRED_Ranks',
396
- array(
397
- 'rank_ctype' => $_GET['ctype']
398
- )
399
- );
400
- wp_enqueue_script( 'mycred-rank-tweaks' );
401
-
402
- endif;
403
-
404
- }
405
-
406
- // Edit Rank Page
407
- if ( preg_match( '/(mycred_rank)/', $screen->id, $matches ) ) {
408
- wp_enqueue_style( 'mycred-admin' );
409
- wp_dequeue_script( 'autosave' );
410
- }
411
-
412
- // Insert management script
413
- if ( preg_match( '/(myCRED_page_settings)/', $screen->id, $matches ) || substr( $screen->id, -14, 14 ) == '_page_settings' ) {
414
- wp_register_script(
415
- 'mycred-rank-management',
416
- plugins_url( 'assets/js/management.js', myCRED_RANKS ),
417
- array( 'jquery' ),
418
- myCRED_VERSION . '.1'
419
- );
420
- wp_localize_script(
421
- 'mycred-rank-management',
422
- 'myCRED_Ranks',
423
- array(
424
- 'ajaxurl' => admin_url( 'admin-ajax.php' ),
425
- 'token' => wp_create_nonce( 'mycred-management-actions-roles' ),
426
- 'working' => esc_attr__( 'Processing...', 'mycred' ),
427
- 'confirm_del' => esc_attr__( 'Warning! All ranks will be deleted! This can not be undone!', 'mycred' ),
428
- 'confirm_assign' => esc_attr__( 'Are you sure you want to re-assign user ranks?', 'mycred' )
429
- )
430
- );
431
- wp_enqueue_script( 'mycred-rank-management' );
432
- }
433
-
434
- }
435
-
436
- /**
437
- * Register Rank Post Type
438
- * @since 1.1
439
- * @version 1.3.1
440
- */
441
- public function register_post_type() {
442
-
443
- if ( isset( $_GET['ctype'] ) && array_key_exists( $_GET['ctype'], $this->point_types ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_rank' )
444
- $name = sprintf( __( 'Ranks for %s', 'mycred' ), $this->point_types[ $_GET['ctype'] ] );
445
- else
446
- $name = __( 'Ranks', 'mycred' );
447
-
448
- $labels = array(
449
- 'name' => $name,
450
- 'singular_name' => __( 'Rank', 'mycred' ),
451
- 'add_new' => __( 'Add New', 'mycred' ),
452
- 'add_new_item' => __( 'Add New Rank', 'mycred' ),
453
- 'edit_item' => __( 'Edit Rank', 'mycred' ),
454
- 'new_item' => __( 'New Rank', 'mycred' ),
455
- 'all_items' => __( 'Ranks', 'mycred' ),
456
- 'view_item' => __( 'View Rank', 'mycred' ),
457
- 'search_items' => __( 'Search Ranks', 'mycred' ),
458
- 'featured_image' => __( 'Rank Logo', 'mycred' ),
459
- 'set_featured_image' => __( 'Set rank logo', 'mycred' ),
460
- 'remove_featured_image' => __( 'Remove rank logo', 'mycred' ),
461
- 'use_featured_image' => __( 'Use as Logo', 'mycred' ),
462
- 'not_found' => __( 'No ranks found', 'mycred' ),
463
- 'not_found_in_trash' => __( 'No ranks found in Trash', 'mycred' ),
464
- 'parent_item_colon' => '',
465
- 'menu_name' => __( 'Ranks', 'mycred' )
466
- );
467
-
468
- // Support
469
- $supports = array( 'title', 'thumbnail' );
470
- if ( isset( $this->rank['support']['content'] ) && $this->rank['support']['content'] )
471
- $supports[] = 'editor';
472
- if ( isset( $this->rank['support']['excerpt'] ) && $this->rank['support']['excerpt'] )
473
- $supports[] = 'excerpts';
474
- if ( isset( $this->rank['support']['comments'] ) && $this->rank['support']['comments'] )
475
- $supports[] = 'comments';
476
- if ( isset( $this->rank['support']['page-attributes'] ) && $this->rank['support']['page-attributes'] )
477
- $supports[] = 'page-attributes';
478
- if ( isset( $this->rank['support']['custom-fields'] ) && $this->rank['support']['custom-fields'] )
479
- $supports[] = 'custom-fields';
480
-
481
- // Where to show the menu
482
- $show = false;
483
- if ( count( $this->point_types ) == 1 )
484
- $show = 'myCRED';
485
-
486
- // Custom Post Type Attributes
487
- $args = array(
488
- 'labels' => $labels,
489
- 'public' => (bool) $this->rank['public'],
490
- 'publicly_queryable' => (bool) $this->rank['public'],
491
- 'has_archive' => (bool) $this->rank['public'],
492
- 'show_ui' => true,
493
- 'show_in_menu' => $show,
494
- 'capability_type' => 'page',
495
- 'supports' => $supports
496
- );
497
-
498
- // Rewrite
499
- if ( $this->rank['public'] && ! empty( $this->rank['slug'] ) )
500
- $args['rewrite'] = array( 'slug' => $this->rank['slug'] );
501
-
502
- register_post_type( 'mycred_rank', apply_filters( 'mycred_register_ranks', $args, $this ) );
503
-
504
- }
505
-
506
  /**
507
  * AJAX: Calculate Totals
508
  * @since 1.2
509
- * @version 1.3.1
510
  */
511
  public function calculate_totals() {
512
 
513
  // Security
514
  check_ajax_referer( 'mycred-calc-totals', 'token' );
515
 
516
- $type = 'mycred_default';
517
  if ( isset( $_POST['ctype'] ) && array_key_exists( $_POST['ctype'], $this->point_types ) )
518
  $type = sanitize_text_field( $_POST['ctype'] );
519
 
@@ -545,7 +449,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
545
 
546
  }
547
 
548
- die( json_encode( sprintf( __( 'Completed - Total of %d users effected', 'mycred' ), $count ) ) );
549
 
550
  }
551
 
@@ -553,20 +457,29 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
553
  * Balance Adjustment
554
  * Check if users rank should change.
555
  * @since 1.1
556
- * @version 1.4
557
  */
558
  public function balance_adjustment( $result, $request, $mycred ) {
559
 
560
  // If the result was declined
561
- if ( $result === false )
562
- return $result;
 
563
 
564
  // If ranks for this type is based on total and this is not a admin adjustment
565
- if ( mycred_rank_based_on_total( $request['type'] ) && $request['amount'] < 0 && $request['ref'] != 'manual' )
566
  return $result;
567
 
 
 
 
 
568
  // Find users rank
569
- mycred_find_users_rank( (int) $request['user_id'], true, $request['type'] );
 
 
 
 
570
 
571
  return $result;
572
 
@@ -587,7 +500,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
587
 
588
  $type = get_post_meta( $post->ID, 'ctype', true );
589
  if ( $type == '' ) {
590
- $type = 'mycred_default';
591
  update_post_meta( $post->ID, 'ctype', $type );
592
  }
593
 
@@ -601,71 +514,6 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
601
 
602
  }
603
 
604
- /**
605
- * Adjust Rank Sort Order
606
- * Adjusts the wp query when viewing ranks to order by the min. point requirement.
607
- * @since 1.1.1
608
- * @version 1.2
609
- */
610
- public function adjust_wp_query( $query ) {
611
-
612
- // Front End Queries
613
- if ( ! is_admin() ) {
614
-
615
- if ( ! is_post_type_archive( 'mycred_rank' ) ) return;
616
-
617
- // By default we want to only show ranks for the main point type
618
- if ( ! isset( $_GET['ctype'] ) && $query->is_main_query() ) {
619
- $query->set( 'meta_query', array(
620
- array(
621
- 'key' => 'ctype',
622
- 'value' => 'mycred_default',
623
- 'compare' => '='
624
- )
625
- ) );
626
- }
627
-
628
- // Otherwise if ctype is set and it is a point type filter the results
629
- elseif ( isset( $_GET['ctype'] ) && array_key_exists( $_GET['ctype'], $this->point_types ) ) {
630
- $query->set( 'meta_query', array(
631
- array(
632
- 'key' => 'ctype',
633
- 'value' => $_GET['ctype'],
634
- 'compare' => '='
635
- )
636
- ) );
637
- }
638
-
639
- }
640
-
641
- // Admin Queries
642
- else {
643
-
644
- if ( ! isset( $query->query['post_type'] ) || $query->query['post_type'] != 'mycred_rank' ) return;
645
-
646
- // If ctype is set, filter ranks according to it's value
647
- if ( isset( $_GET['ctype'] ) && array_key_exists( $_GET['ctype'], $this->point_types ) ) {
648
- $query->set( 'meta_query', array(
649
- array(
650
- 'key' => 'ctype',
651
- 'value' => $_GET['ctype'],
652
- 'compare' => '='
653
- )
654
- ) );
655
- }
656
-
657
- }
658
-
659
- // Sort by meta value
660
- $query->set( 'meta_key', 'mycred_rank_min' );
661
- $query->set( 'orderby', 'meta_value_num' );
662
-
663
- // Sort order
664
- if ( ! isset( $this->rank['order'] ) ) $this->rank['order'] = 'ASC';
665
- $query->set( 'order', $this->rank['order'] );
666
-
667
- }
668
-
669
  /**
670
  * User Related Template Tags
671
  * Adds support for ranks of custom point types.
@@ -680,7 +528,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
680
 
681
  foreach ( $this->point_types as $type_id => $label ) {
682
 
683
- if ( $type_id == 'mycred_default' ) continue;
684
  $tags[] = 'rank_' . $type_id;
685
  $tags[] = 'rank_logo_' . $type_id;
686
 
@@ -694,7 +542,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
694
  * Parse Rank
695
  * Parses the %rank% and %rank_logo% template tags.
696
  * @since 1.1
697
- * @version 1.2.1
698
  */
699
  public function parse_rank( $content, $user = '', $data = '' ) {
700
 
@@ -704,7 +552,8 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
704
  // User ID does not exist ( user no longer exists )
705
  if ( ! isset( $user->ID ) ) {
706
  foreach ( $this->point_types as $type_id => $label ) {
707
- if ( $type_id == 'mycred_default' ) {
 
708
  $content = str_replace( '%rank%', '', $content );
709
  $content = str_replace( '%rank_logo%', '', $content );
710
  }
@@ -712,6 +561,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
712
  $content = str_replace( '%rank_' . $type_id . '%', '', $content );
713
  $content = str_replace( '%rank_logo_' . $type_id . '%', '', $content );
714
  }
 
715
  }
716
  }
717
 
@@ -720,19 +570,23 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
720
 
721
  // Loop the point types and replace template tags
722
  foreach ( $this->point_types as $type_id => $label ) {
 
723
  $rank_id = mycred_get_users_rank_id( $user->ID, $type_id );
724
- if ( $rank_id === NULL ) {
725
- if ( $type_id == 'mycred_default' ) {
 
726
  $content = str_replace( '%rank%', '', $content );
727
  $content = str_replace( '%rank_logo%', '', $content );
728
  }
729
  else {
730
- $content = str_replace( '%rank%', '', $content );
731
- $content = str_replace( '%rank_logo%', '', $content );
732
  }
 
733
  }
734
  else {
735
- if ( $type_id == 'mycred_default' ) {
 
736
  $content = str_replace( '%rank%', get_the_title( $rank_id ), $content );
737
  $content = str_replace( '%rank_logo%', mycred_get_rank_logo( $rank_id ), $content );
738
  }
@@ -740,7 +594,9 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
740
  $content = str_replace( '%rank_' . $type_id . '%', get_the_title( $rank_id ), $content );
741
  $content = str_replace( '%rank_logo_' . $type_id . '%', mycred_get_rank_logo( $rank_id ), $content );
742
  }
 
743
  }
 
744
  }
745
  }
746
 
@@ -755,31 +611,32 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
755
  */
756
  public function insert_rank_header() {
757
 
758
- $output = '';
759
- $user_id = bp_displayed_user_id();
 
760
 
761
- foreach ( $this->point_types as $type_id => $label ) {
762
 
763
  // Load type
764
  $mycred = mycred( $type_id );
765
 
766
- // User is excluded from this type
767
- if ( $mycred->exclude_user( $user_id ) ) continue;
768
-
769
  // No settings
770
  if ( ! isset( $mycred->rank['bb_location'] ) ) continue;
771
 
772
  // Not shown
773
- if ( ! in_array( $mycred->rank['bb_location'], array( 'top', 'both' ) ) ) continue;
774
 
775
- // Get rank (if user has one
776
  $users_rank = mycred_get_users_rank_id( $user_id, $type_id );
777
- if ( $users_rank === NULL ) continue;
778
 
779
  // Parse template
780
  $template = $mycred->rank['bb_template'];
781
  $template = str_replace( '%rank_title%', get_the_title( $users_rank ), $template );
782
- $template = str_replace( '%rank_logo%', mycred_get_rank_logo( $users_rank ), $template );
 
 
 
783
 
784
  // Let others play
785
  $output .= apply_filters( 'mycred_bp_header_ranks_row', $template, $user_id, $users_rank, $mycred, $this );
@@ -788,44 +645,45 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
788
 
789
  if ( $output == '' ) return;
790
 
791
- echo '<div id="mycred-my-rank">' . apply_filters( 'mycred_bp_rank_in_header', $output, $user_id, $this ) . '</div>';
792
 
793
  }
794
 
795
  /**
796
  * Insert Rank In Profile Details
797
  * @since 1.1
798
- * @version 1.3
799
  */
800
  public function insert_rank_profile() {
801
 
802
- $output = '';
803
- $user_id = bp_displayed_user_id();
 
804
 
805
  $count = 0;
806
- foreach ( $this->point_types as $type_id => $label ) {
807
 
808
  // Load type
809
  $mycred = mycred( $type_id );
810
 
811
- // User is excluded from this type
812
- if ( $mycred->exclude_user( $user_id ) ) continue;
813
-
814
  // No settings
815
  if ( ! isset( $mycred->rank['bb_location'] ) ) continue;
816
 
817
  // Not shown
818
- if ( ! in_array( $mycred->rank['bb_location'], array( 'profile_tab', 'both' ) ) ) continue;
819
 
820
- // Get rank (if user has one
821
  $users_rank = mycred_get_users_rank_id( $user_id, $type_id );
822
- if ( $users_rank === NULL ) continue;
823
 
824
  // Parse template
825
  $template = $mycred->rank['bb_template'];
826
  $template = str_replace( '%rank_title%', get_the_title( $users_rank ), $template );
827
  $template = str_replace( '%rank_logo%', mycred_get_rank_logo( $users_rank ), $template );
828
 
 
 
 
829
  // Let others play
830
  $output .= apply_filters( 'mycred_bp_profile_ranks_row', $template, $user_id, $users_rank, $mycred, $this );
831
  $count ++;
@@ -853,7 +711,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
853
  /**
854
  * Insert Rank In bbPress Reply
855
  * @since 1.6
856
- * @version 1.0
857
  */
858
  public function insert_rank_bb_reply() {
859
 
@@ -861,29 +719,31 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
861
  $user_id = bbp_get_reply_author_id();
862
  if ( $user_id == 0 ) return;
863
 
864
- foreach ( $this->point_types as $type_id => $label ) {
 
 
865
 
866
  // Load type
867
  $mycred = mycred( $type_id );
868
 
869
- // User is excluded from this type
870
- if ( $mycred->exclude_user( $user_id ) ) continue;
871
-
872
  // No settings
873
  if ( ! isset( $mycred->rank['bp_location'] ) ) continue;
874
 
875
  // Not shown
876
- if ( ! in_array( $mycred->rank['bp_location'], array( 'reply', 'both' ) ) ) continue;
877
 
878
  // Get rank (if user has one
879
  $users_rank = mycred_get_users_rank_id( $user_id, $type_id );
880
- if ( $users_rank === NULL ) continue;
881
 
882
  // Parse template
883
  $template = $mycred->rank['bp_template'];
884
  $template = str_replace( '%rank_title%', get_the_title( $users_rank ), $template );
885
  $template = str_replace( '%rank_logo%', mycred_get_rank_logo( $users_rank ), $template );
886
 
 
 
 
887
  // Let others play
888
  $output .= apply_filters( 'mycred_bb_reply_ranks_row', $template, $user_id, $users_rank, $mycred, $this );
889
 
@@ -891,7 +751,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
891
 
892
  if ( $output == '' ) return;
893
 
894
- echo '<div id="mycred-my-rank">' . apply_filters( 'mycred_bb_rank_in_reply', $output, $user_id, $this ) . '</div>';
895
 
896
  }
897
 
@@ -902,32 +762,33 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
902
  */
903
  public function insert_rank_bb_profile() {
904
 
905
- $output = '';
906
- $user_id = bbp_get_displayed_user_id();
 
907
 
908
- foreach ( $this->point_types as $type_id => $label ) {
909
 
910
  // Load type
911
  $mycred = mycred( $type_id );
912
 
913
- // User is excluded from this type
914
- if ( $mycred->exclude_user( $user_id ) ) continue;
915
-
916
  // No settings
917
  if ( ! isset( $mycred->rank['bp_location'] ) ) continue;
918
 
919
  // Not shown
920
- if ( ! in_array( $mycred->rank['bp_location'], array( 'profile', 'both' ) ) ) continue;
921
 
922
  // Get rank (if user has one
923
  $users_rank = mycred_get_users_rank_id( $user_id, $type_id );
924
- if ( $users_rank === NULL ) continue;
925
 
926
  // Parse template
927
  $template = $mycred->rank['bp_template'];
928
  $template = str_replace( '%rank_title%', get_the_title( $users_rank ), $template );
929
  $template = str_replace( '%rank_logo%', mycred_get_rank_logo( $users_rank ), $template );
930
 
 
 
 
931
  // Let others play
932
  $output .= apply_filters( 'mycred_bb_profile_ranks_row', $template, $user_id, $users_rank, $mycred, $this );
933
 
@@ -935,7 +796,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
935
 
936
  if ( $output == '' ) return;
937
 
938
- echo '<div id="mycred-my-rank">' . apply_filters( 'mycred_bb_rank_in_profile', $output, $user_id, $this ) . '</div>';
939
 
940
  }
941
 
@@ -943,15 +804,13 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
943
  * Add Default Rank
944
  * Adds the default "Newbie" rank and adds all non-exluded user to this rank.
945
  * Note! This method is only called when there are zero ranks as this will create the new default rank.
946
- * @uses wp_insert_port()
947
- * @uses update_post_meta()
948
- * @uses get_users()
949
- * @uses update_user_meta()
950
  * @since 1.1
951
- * @version 1.1
952
  */
953
  public function add_default_rank() {
954
 
 
 
955
  // If there are no ranks at all
956
  if ( ! mycred_have_ranks() ) {
957
 
@@ -967,7 +826,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
967
  // Update min and max values
968
  update_post_meta( $rank_id, 'mycred_rank_min', 0 );
969
  update_post_meta( $rank_id, 'mycred_rank_max', 9999999 );
970
- update_post_meta( $rank_id, 'ctype', 'mycred_default' );
971
 
972
  $mycred_ranks = 1;
973
  mycred_assign_ranks();
@@ -977,62 +836,195 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
977
  }
978
 
979
  /**
980
- * Adjust Post Updated Messages
981
- * @since 1.1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
982
  * @version 1.2
983
  */
984
- public function post_updated_messages( $messages ) {
985
-
986
- $messages['mycred_rank'] = array(
987
- 0 => '',
988
- 1 => __( 'Rank Updated.', 'mycred' ),
989
- 2 => __( 'Rank Updated.', 'mycred' ),
990
- 3 => __( 'Rank Updated.', 'mycred' ),
991
- 4 => __( 'Rank Updated.', 'mycred' ),
992
- 5 => __( 'Rank Updated.', 'mycred' ),
993
- 6 => __( 'Rank Enabled', 'mycred' ),
994
- 7 => __( 'Rank Saved', 'mycred' ),
995
- 8 => __( 'Rank Updated.', 'mycred' ),
996
- 9 => __( 'Rank Updated.', 'mycred' ),
997
- 10 => ''
998
- );
999
 
1000
- return $messages;
 
1001
 
1002
- }
1003
 
1004
- /**
1005
- * Adjust Row Actions
1006
- * @since 1.1
1007
- * @version 1.0
1008
- */
1009
- public function adjust_row_actions( $actions, $post ) {
 
 
 
 
1010
 
1011
- if ( $post->post_type == 'mycred_rank' ) {
1012
- unset( $actions['inline hide-if-no-js'] );
 
 
 
 
 
 
 
 
1013
 
1014
- if ( ! $this->rank['public'] )
1015
- unset( $actions['view'] );
1016
  }
1017
 
1018
- return $actions;
 
1019
 
1020
- }
1021
 
1022
- /**
1023
- * Custom User Balance Content
1024
- * Inserts a users rank for each point type.
1025
- * @since 1.6
1026
- * @version 1.0
1027
- */
1028
- public function custom_user_column_content( $balance, $user_id, $type ) {
 
 
 
1029
 
1030
- $rank = mycred_get_users_rank( $user_id, 'post_title', NULL, NULL, $type );
1031
- if ( $rank == '' )
1032
- $rank = '-';
1033
 
1034
- $balance .= '<small style="display:block;">' . sprintf( __( '<strong>Rank:</strong> %s', 'mycred' ), $rank ) . '</small>';
1035
- return $balance;
 
 
 
 
 
1036
 
1037
  }
1038
 
@@ -1069,7 +1061,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
1069
 
1070
  $type = get_post_meta( $post_id, 'ctype', true );
1071
  if ( $type == '' )
1072
- $type = 'mycred_default';
1073
 
1074
  // Rank Logo (thumbnail)
1075
  if ( $column_name == 'mycred-rank-logo' ) {
@@ -1085,7 +1077,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
1085
  elseif ( $column_name == 'mycred-rank-req' ) {
1086
 
1087
  $mycred = $this->core;
1088
- if ( $type != 'mycred_default' )
1089
  $mycred = mycred( $type );
1090
 
1091
  $min = get_post_meta( $post_id, 'mycred_rank_min', true );
@@ -1121,6 +1113,24 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
1121
 
1122
  }
1123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1124
  /**
1125
  * Adjust Enter Title Here
1126
  * @since 1.1
@@ -1141,11 +1151,11 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
1141
  * @since 1.1
1142
  * @version 1.0
1143
  */
1144
- public function add_meta_boxes() {
1145
 
1146
  add_meta_box(
1147
- 'mycred_rank_settings',
1148
- __( 'Rank Settings', 'mycred' ),
1149
  array( $this, 'rank_settings' ),
1150
  'mycred_rank',
1151
  'normal',
@@ -1157,24 +1167,24 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
1157
  /**
1158
  * Rank Settings Metabox
1159
  * @since 1.1
1160
- * @version 1.1
1161
  */
1162
  public function rank_settings( $post ) {
1163
 
1164
  // Get type
1165
  $type = get_post_meta( $post->ID, 'ctype', true );
1166
  if ( $type == '' ) {
1167
- $type = 'mycred_default';
1168
  update_post_meta( $post->ID, 'ctype', $type );
1169
  }
1170
 
1171
  // If a custom type has been requested via the URL
1172
  if ( isset( $_REQUEST['ctype'] ) && ! empty( $_REQUEST['ctype'] ) )
1173
- $type = sanitize_text_field( $_REQUEST['ctype'] );
1174
 
1175
  // Load the appropriate type object
1176
  $mycred = $this->core;
1177
- if ( $type != 'mycred_default' )
1178
  $mycred = mycred( $type );
1179
 
1180
  // Get min and max setting for this rank
@@ -1182,77 +1192,74 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
1182
  $max = get_post_meta( $post->ID, 'mycred_rank_max', true );
1183
 
1184
  ?>
1185
- <style type="text/css">
1186
- #submitdiv .misc-pub-curtime { display: none; }
1187
- #mycred_rank_settings .inside { margin: 0; padding: 0; clear: both; }
1188
- #mycred_rank_settings .inside #mycred-rank-setup-wrapper { float: none; clear: both; }
1189
- #mycred_rank_settings .inside #mycred-rank-setup-wrapper .box { width: 48%; float: left; margin: 0; padding: 0; }
1190
- #mycred_rank_settings .inside #mycred-rank-setup-wrapper .box .wrap { padding: 12px; margin: 0; }
1191
- #mycred_rank_settings .inside #mycred-rank-setup-wrapper .border { border-right: 1px solid #ddd; margin-right: -1px; }
1192
- #mycred_rank_settings .inside #mycred-rank-setup-wrapper p { margin: 0 0 0 0; }
1193
- #mycred_rank_settings .inside #mycred-rank-setup-wrapper ul li strong { display: inline-block; width: 60%; }
1194
- #mycred_rank_settings .inside #mycred-rank-setup-wrapper label { font-weight: bold; display: block; }
1195
- #mycred_rank_settings .inside #mycred-rank-setup-wrapper input[type="text"],
1196
- #mycred_rank_settings .inside #mycred-rank-setup-wrapper select { min-width: 60%; margin-bottom: 12px; }
1197
- </style>
1198
- <div id="mycred-rank-setup-wrapper">
1199
- <div class="box border">
1200
- <div class="wrap">
1201
- <p>
1202
- <label for="mycred-rank-min"><?php echo $mycred->template_tags_general( __( 'Minimum %plural% to reach this rank', 'mycred' ) ); ?>:</label>
1203
- <input type="text" name="mycred_rank[min]" id="mycred-rank-min" value="<?php echo $min; ?>" />
1204
- </p>
1205
- <p>
1206
- <label for="mycred-rank-max"><?php echo $mycred->template_tags_general( __( 'Maximum %plural% to be included in this rank', 'mycred' ) ); ?>:</label>
1207
- <input type="text" name="mycred_rank[max]" id="mycred-rank-max" value="<?php echo $max; ?>" />
1208
- </p>
1209
-
1210
- <?php if ( count( $this->point_types ) > 1 ) : ?>
1211
-
1212
- <p>
1213
- <label for="mycred-rank-point-type"><?php _e( 'Point Type', 'mycred' ); ?></label>
1214
- <?php mycred_types_select_from_dropdown( 'mycred_rank[ctype]', 'mycred-rank-point-type', $type ); ?>
1215
- </p>
1216
-
1217
- <?php else : ?>
1218
-
1219
- <input type="hidden" name="mycred_rank[ctype]" value="mycred_default" />
1220
-
1221
- <?php endif; ?>
1222
-
1223
- <?php do_action( 'mycred_rank_after_req', $post, $this->core ); ?>
1224
-
1225
  </div>
1226
- </div>
1227
- <div class="box">
1228
- <div class="wrap">
1229
- <p><?php _e( 'All Published Ranks', 'mycred' ); ?>:</p>
1230
  <?php
1231
 
1232
  // Get all published ranks for this type
1233
- $all = mycred_get_ranks( 'publish', '-1', 'DESC', $type );
 
 
1234
 
1235
- if ( ! empty( $all ) ) {
1236
  echo '<ul>';
1237
- foreach ( $all as $rank_id => $rank ) {
1238
- $_min = get_post_meta( $rank_id, 'mycred_rank_min', true );
1239
- if ( empty( $_min ) && (int) $_min !== 0 ) $_min = __( 'Not Set', 'mycred' );
1240
- $_max = get_post_meta( $rank_id, 'mycred_rank_max', true );
1241
- if ( empty( $_max ) ) $_max = __( 'Not Set', 'mycred' );
1242
- echo '<li><strong>' . $rank->post_title . '</strong> ' . $_min . ' - ' . $_max . '</li>';
 
1243
  }
1244
  echo '</ul>';
 
1245
  }
1246
  else {
1247
- echo '<p>' . __( 'No Ranks found', 'mycred' ) . '.</p>';
 
 
1248
  }
1249
 
1250
  ?>
1251
-
1252
- <input type="hidden" name="mycred_rank[token]" value="<?php echo wp_create_nonce( 'mycred-edit-rank' . $post->ID ); ?>" />
1253
  </div>
1254
  </div>
1255
- <div class="clear clearfix"></div>
1256
  </div>
1257
  <?php
1258
 
@@ -1261,39 +1268,29 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
1261
  /**
1262
  * Save Rank Details
1263
  * @since 1.1
1264
- * @version 1.3
1265
  */
1266
- public function save_rank_settings( $post_id ) {
1267
 
1268
  // Make sure fields exists
1269
- if ( ! isset( $_POST['mycred_rank'] ) || ! is_array( $_POST['mycred_rank'] ) || ! wp_verify_nonce( $_POST['mycred_rank']['token'], 'mycred-edit-rank' . $post_id ) ) return;
1270
-
1271
- // Minimum can not be empty
1272
- if ( empty( $_POST['mycred_rank']['min'] ) )
1273
- $min = 0;
1274
- else
1275
- $min = sanitize_key( $_POST['mycred_rank']['min'] );
1276
-
1277
- update_post_meta( $post_id, 'mycred_rank_min', $min );
1278
 
1279
- // Maximum can not be empty
1280
- if ( empty( $_POST['mycred_rank']['max'] ) )
1281
- $max = 999;
1282
- else
1283
- $max = sanitize_key( $_POST['mycred_rank']['max'] );
1284
 
1285
- update_post_meta( $post_id, 'mycred_rank_max', $max );
1286
 
1287
- // Save type
1288
- if ( empty( $_POST['mycred_rank']['ctype'] ) )
1289
- $type = 'mycred_default';
1290
- else
1291
- $type = sanitize_text_field( $_POST['mycred_rank']['ctype'] );
 
1292
 
1293
- update_post_meta( $post_id, 'ctype', $type );
1294
 
1295
- if ( get_post_status( $post_id ) == 'publish' )
1296
- mycred_assign_ranks( $type );
1297
 
1298
  }
1299
 
@@ -1306,7 +1303,7 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
1306
 
1307
  $prefs = $this->rank;
1308
  $this->add_to_core = true;
1309
- if ( $mycred->mycred_type != 'mycred_default' ) {
1310
 
1311
  if ( ! isset( $mycred->rank ) )
1312
  $prefs = $this->default_prefs;
@@ -1323,10 +1320,10 @@ if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
1323
  $box = 'display: block;';
1324
 
1325
  ?>
1326
- <h4><div class="icon icon-active"></div><?php _e( 'Ranks', 'mycred' ); ?></h4>
1327
  <div class="body" style="display:none;">
1328
 
1329
- <?php if ( $mycred->mycred_type == 'mycred_default' ) { ?>
1330
 
1331
  <label class="subheader" for="<?php echo $this->field_id( 'public' ); ?>"><?php _e( 'Rank Features', 'mycred' ); ?></label>
1332
  <ol id="myCRED-rank-supports">
@@ -1609,7 +1606,7 @@ jQuery(function($){
1609
  if ( $this->core->is_multisite && $GLOBALS['blog_id'] > 1 && ! $this->core->use_master_template )
1610
  $rank_meta_key .= '_' . $GLOBALS['blog_id'];
1611
 
1612
- if ( $mycred->mycred_type != 'mycred_default' )
1613
  $rank_meta_key .= $mycred->mycred_type;
1614
 
1615
  ?>
@@ -1632,11 +1629,138 @@ jQuery(function($){
1632
 
1633
  }
1634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635
  }
 
 
 
 
 
 
 
 
 
1636
 
1637
- $rank = new myCRED_Ranks_Module();
1638
- $rank->load();
1639
 
 
 
 
1640
  endif;
 
1641
 
1642
  ?>
2
  /**
3
  * Addon: Ranks
4
  * Addon URI: http://mycred.me/add-ons/ranks/
5
+ * Version: 1.5
 
 
 
6
  */
7
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
 
9
  define( 'myCRED_RANKS', __FILE__ );
10
  define( 'myCRED_RANKS_DIR', myCRED_ADDONS_DIR . 'ranks/' );
11
+ define( 'myCRED_RANKS_VERSION', '1.5' );
12
+
13
+ // Default badge width
14
+ if ( ! defined( 'MYCRED_RANK_WIDTH' ) )
15
+ define( 'MYCRED_RANK_WIDTH', 250 );
16
+
17
+ // Default badge height
18
+ if ( ! defined( 'MYCRED_RANK_HEIGHT' ) )
19
+ define( 'MYCRED_RANK_HEIGHT', 250 );
20
 
21
  require_once myCRED_RANKS_DIR . 'includes/mycred-rank-functions.php';
22
+ require_once myCRED_RANKS_DIR . 'includes/mycred-rank-object.php';
23
  require_once myCRED_RANKS_DIR . 'includes/mycred-rank-shortcodes.php';
24
 
25
  /**
28
  * points, ranks are titles that can be given to users when their reach a certain
29
  * amount.
30
  * @since 1.1
31
+ * @version 1.4
32
  */
33
  if ( ! class_exists( 'myCRED_Ranks_Module' ) ) :
34
  class myCRED_Ranks_Module extends myCRED_Module {
58
  )
59
  ),
60
  'register' => false,
61
+ 'add_to_core' => false,
62
+ 'menu_pos' => 100
63
  ) );
64
 
65
  if ( ! isset( $this->rank['order'] ) )
112
  */
113
  public function module_init() {
114
 
115
+ $this->register_ranks();
116
  $this->add_default_rank();
117
  $this->add_multiple_point_types_support();
118
 
119
+ add_action( 'pre_get_posts', array( $this, 'adjust_wp_query' ), 20 );
120
+ add_action( 'mycred_admin_enqueue', array( $this, 'enqueue_scripts' ) );
121
 
122
  // Instances to update ranks
123
+ add_action( 'transition_post_status', array( $this, 'post_status_change' ), 99, 3 );
124
 
125
  // BuddyPress
126
  if ( class_exists( 'BuddyPress' ) ) {
127
+ add_action( 'bp_before_member_header_meta', array( $this, 'insert_rank_header' ) );
128
+ add_action( 'bp_after_profile_loop_content', array( $this, 'insert_rank_profile' ) );
129
  }
130
 
131
  // bbPress
135
  }
136
 
137
  // Shortcodes
138
+ add_shortcode( 'mycred_my_rank', 'mycred_render_my_rank' );
139
+ add_shortcode( 'mycred_my_ranks', 'mycred_render_my_ranks' );
140
+ add_shortcode( 'mycred_users_of_rank', 'mycred_render_users_of_rank' );
141
+ add_shortcode( 'mycred_users_of_all_ranks', 'mycred_render_users_of_all_ranks' );
142
+ add_shortcode( 'mycred_list_ranks', 'mycred_render_rank_list' );
143
 
144
  // Admin Management items
145
+ add_action( 'wp_ajax_mycred-calc-totals', array( $this, 'calculate_totals' ) );
146
 
147
  }
148
 
153
  */
154
  public function module_admin_init() {
155
 
156
+ add_filter( 'parent_file', array( $this, 'parent_file' ) );
157
+ add_filter( 'submenu_file', array( $this, 'subparent_file' ), 10, 2 );
158
+ add_filter( 'admin_url', array( $this, 'replace_add_new_rank_url' ), 10, 3 );
159
 
160
  add_filter( 'manage_mycred_rank_posts_columns', array( $this, 'adjust_column_headers' ), 50 );
161
  add_action( 'manage_mycred_rank_posts_custom_column', array( $this, 'adjust_column_content' ), 10, 2 );
162
 
163
+ add_filter( 'post_row_actions', array( $this, 'adjust_row_actions' ), 10, 2 );
164
 
165
+ add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
166
+ add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
167
+ add_action( 'save_post_mycred_rank', array( $this, 'save_rank' ), 10, 2 );
168
 
169
+ add_action( 'wp_ajax_mycred-action-delete-ranks', array( $this, 'action_delete_ranks' ) );
170
+ add_action( 'wp_ajax_mycred-action-assign-ranks', array( $this, 'action_assign_ranks' ) );
171
 
172
+ add_filter( 'mycred_users_balance_column', array( $this, 'custom_user_column_content' ), 10, 3 );
 
173
 
174
+ add_action( 'mycred_user_edit_after_balance', array( $this, 'show_rank_in_user_editor' ), 40, 3 );
 
175
 
176
  }
177
 
186
  add_action( 'mycred_after_core_prefs', array( $this, 'after_general_settings' ) );
187
  add_filter( 'mycred_save_core_prefs', array( $this, 'sanitize_extra_settings' ), 90, 3 );
188
 
189
+ add_action( 'mycred_add_menu', array( $this, 'add_menus' ), $this->menu_pos );
190
 
191
+ if ( count( $this->point_types ) > 1 ) {
192
 
193
  $priority = 10;
194
  foreach ( $this->point_types as $type_id => $label ) {
205
 
206
  }
207
 
208
+ /**
209
+ * Register Rank Post Type
210
+ * @since 1.1
211
+ * @version 1.3.1
212
+ */
213
+ public function register_ranks() {
214
+
215
+ if ( isset( $_GET['ctype'] ) && array_key_exists( $_GET['ctype'], $this->point_types ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_rank' )
216
+ $name = sprintf( __( 'Ranks for %s', 'mycred' ), $this->point_types[ $_GET['ctype'] ] );
217
+ else
218
+ $name = __( 'Ranks', 'mycred' );
219
+
220
+ $labels = array(
221
+ 'name' => $name,
222
+ 'singular_name' => __( 'Rank', 'mycred' ),
223
+ 'add_new' => __( 'Add New', 'mycred' ),
224
+ 'add_new_item' => __( 'Add New', 'mycred' ),
225
+ 'edit_item' => __( 'Edit Rank', 'mycred' ),
226
+ 'new_item' => __( 'New Rank', 'mycred' ),
227
+ 'all_items' => __( 'Ranks', 'mycred' ),
228
+ 'view_item' => __( 'View Rank', 'mycred' ),
229
+ 'search_items' => __( 'Search Ranks', 'mycred' ),
230
+ 'featured_image' => __( 'Rank Logo', 'mycred' ),
231
+ 'set_featured_image' => __( 'Set rank logo', 'mycred' ),
232
+ 'remove_featured_image' => __( 'Remove rank logo', 'mycred' ),
233
+ 'use_featured_image' => __( 'Use as Logo', 'mycred' ),
234
+ 'not_found' => __( 'No ranks found', 'mycred' ),
235
+ 'not_found_in_trash' => __( 'No ranks found in Trash', 'mycred' ),
236
+ 'parent_item_colon' => '',
237
+ 'menu_name' => __( 'Ranks', 'mycred' )
238
+ );
239
+
240
+ // Support
241
+ $supports = array( 'title', 'thumbnail' );
242
+ if ( isset( $this->rank['support']['content'] ) && $this->rank['support']['content'] )
243
+ $supports[] = 'editor';
244
+ if ( isset( $this->rank['support']['excerpt'] ) && $this->rank['support']['excerpt'] )
245
+ $supports[] = 'excerpts';
246
+ if ( isset( $this->rank['support']['comments'] ) && $this->rank['support']['comments'] )
247
+ $supports[] = 'comments';
248
+ if ( isset( $this->rank['support']['page-attributes'] ) && $this->rank['support']['page-attributes'] )
249
+ $supports[] = 'page-attributes';
250
+ if ( isset( $this->rank['support']['custom-fields'] ) && $this->rank['support']['custom-fields'] )
251
+ $supports[] = 'custom-fields';
252
+
253
+ // Custom Post Type Attributes
254
+ $args = array(
255
+ 'labels' => $labels,
256
+ 'public' => (bool) $this->rank['public'],
257
+ 'publicly_queryable' => (bool) $this->rank['public'],
258
+ 'has_archive' => (bool) $this->rank['public'],
259
+ 'show_ui' => true,
260
+ 'show_in_menu' => false,
261
+ 'capability_type' => 'page',
262
+ 'supports' => $supports,
263
+ 'register_meta_box_cb' => array( $this, 'add_metaboxes' )
264
+ );
265
+
266
+ // Rewrite
267
+ if ( $this->rank['public'] && ! empty( $this->rank['slug'] ) )
268
+ $args['rewrite'] = array( 'slug' => $this->rank['slug'] );
269
+
270
+ register_post_type( 'mycred_rank', apply_filters( 'mycred_register_ranks', $args, $this ) );
271
+
272
+ }
273
+
274
+ /**
275
+ * Adjust Post Updated Messages
276
+ * @since 1.1
277
+ * @version 1.2
278
+ */
279
+ public function post_updated_messages( $messages ) {
280
+
281
+ $messages['mycred_rank'] = array(
282
+ 0 => '',
283
+ 1 => __( 'Rank Updated.', 'mycred' ),
284
+ 2 => __( 'Rank Updated.', 'mycred' ),
285
+ 3 => __( 'Rank Updated.', 'mycred' ),
286
+ 4 => __( 'Rank Updated.', 'mycred' ),
287
+ 5 => __( 'Rank Updated.', 'mycred' ),
288
+ 6 => __( 'Rank Enabled.', 'mycred' ),
289
+ 7 => __( 'Rank Saved.', 'mycred' ),
290
+ 8 => __( 'Rank Updated.', 'mycred' ),
291
+ 9 => __( 'Rank Updated.', 'mycred' ),
292
+ 10 => ''
293
+ );
294
+
295
+ return $messages;
296
+
297
+ }
298
+
299
+ /**
300
+ * Replace Add New Rank URL
301
+ * @since 1.7
302
+ * @version 1.0
303
+ */
304
+ public function replace_add_new_rank_url( $url, $path, $blog_id ) {
305
+
306
+ if ( $path == 'post-new.php?post_type=mycred_rank' )
307
+ return get_site_url( $blog_id, 'wp-admin/', 'admin' ) . 'post-new.php?post_type=mycred_rank&ctype=' . ( ( isset( $_GET['ctype'] ) ) ? $_GET['ctype'] : MYCRED_DEFAULT_TYPE_KEY );
308
+
309
+ return $url;
310
+
311
+ }
312
+
313
  /**
314
  * Add Admin Menu Item
315
  * @since 1.6
317
  */
318
  public function add_menus() {
319
 
320
+ $cap = $this->core->edit_creds_cap();
321
 
322
  foreach ( $this->point_types as $type_id => $label ) {
323
 
324
  add_submenu_page(
325
+ ( $type_id == MYCRED_DEFAULT_TYPE_KEY ) ? 'mycred' : 'mycred_' . $type_id,
326
  __( 'Ranks', 'mycred' ),
327
  __( 'Ranks', 'mycred' ),
328
  $cap,
344
 
345
  if ( ( $pagenow == 'edit.php' || $pagenow == 'post-new.php' ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_rank' ) {
346
 
347
+ if ( isset( $_GET['ctype'] ) && $_GET['ctype'] != MYCRED_DEFAULT_TYPE_KEY )
348
+ return 'mycred_' . $_GET['ctype'];
349
  else
350
+ return 'mycred';
351
 
352
  }
353
 
354
+ elseif ( $pagenow == 'post.php' && isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'mycred_rank' ) {
 
 
355
 
356
+ if ( isset( $_GET['ctype'] ) && $_GET['ctype'] != MYCRED_DEFAULT_TYPE_KEY )
357
+ return 'mycred_' . $_GET['ctype'];
358
+ else
359
+ return 'mycred';
 
 
 
 
 
 
 
 
 
 
 
360
 
361
+ }
 
362
 
363
+ return $parent;
 
 
 
 
364
 
365
  }
366
 
367
  /**
368
+ * Sub Parent File
369
+ * @since 1.7
370
+ * @version 1.0
371
  */
372
+ public function subparent_file( $subparent = '', $parent = '' ) {
373
 
374
+ global $pagenow;
 
 
 
 
 
 
375
 
376
+ if ( ( $pagenow == 'edit.php' || $pagenow == 'post-new.php' ) && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'mycred_rank' ) {
377
 
378
+ if ( isset( $_GET['ctype'] ) )
379
+ return 'edit.php?post_type=mycred_rank&ctype=' . $_GET['ctype'];
380
+ else
381
+ return 'edit.php?post_type=mycred_rank&ctype=mycred_default';
382
+
 
 
 
383
  }
384
 
385
+ elseif ( $pagenow == 'post.php' && isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'mycred_rank' ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
386
 
387
+ if ( isset( $_GET['ctype'] ) )
388
+ return 'edit.php?post_type=mycred_rank&ctype=' . $_GET['ctype'];
389
+ else
390
+ return 'edit.php?post_type=mycred_rank&ctype=mycred_default';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
 
392
  }
393
 
394
+ return $subparent;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
 
396
  }
397
 
407
 
408
  }
409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410
  /**
411
  * AJAX: Calculate Totals
412
  * @since 1.2
413
+ * @version 1.3.2
414
  */
415
  public function calculate_totals() {
416
 
417
  // Security
418
  check_ajax_referer( 'mycred-calc-totals', 'token' );
419
 
420
+ $type = MYCRED_DEFAULT_TYPE_KEY;
421
  if ( isset( $_POST['ctype'] ) && array_key_exists( $_POST['ctype'], $this->point_types ) )
422
  $type = sanitize_text_field( $_POST['ctype'] );
423
 
449
 
450
  }
451
 
452
+ wp_send_json( sprintf( __( 'Completed - Total of %d users effected', 'mycred' ), $count ) );
453
 
454
  }
455
 
457
  * Balance Adjustment
458
  * Check if users rank should change.
459
  * @since 1.1
460
+ * @version 1.5
461
  */
462
  public function balance_adjustment( $result, $request, $mycred ) {
463
 
464
  // If the result was declined
465
+ if ( $result === false ) return $result;
466
+
467
+ extract( $request );
468
 
469
  // If ranks for this type is based on total and this is not a admin adjustment
470
+ if ( mycred_rank_based_on_total( $type ) && $amount < 0 && $ref != 'manual' )
471
  return $result;
472
 
473
+ $end = '';
474
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY )
475
+ $end = $type;
476
+
477
  // Find users rank
478
+ $rank = mycred_find_users_rank( $user_id, $type );
479
+
480
+ // If users rank changed, save it now.
481
+ if ( isset( $rank->rank_id ) && $rank->rank_id !== $rank->current_id )
482
+ mycred_update_user_meta( $user_id, 'mycred_rank', $end, $rank->rank_id );
483
 
484
  return $result;
485
 
500
 
501
  $type = get_post_meta( $post->ID, 'ctype', true );
502
  if ( $type == '' ) {
503
+ $type = MYCRED_DEFAULT_TYPE_KEY;
504
  update_post_meta( $post->ID, 'ctype', $type );
505
  }
506
 
514
 
515
  }
516
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517
  /**
518
  * User Related Template Tags
519
  * Adds support for ranks of custom point types.
528
 
529
  foreach ( $this->point_types as $type_id => $label ) {
530
 
531
+ if ( $type_id == MYCRED_DEFAULT_TYPE_KEY ) continue;
532
  $tags[] = 'rank_' . $type_id;
533
  $tags[] = 'rank_logo_' . $type_id;
534
 
542
  * Parse Rank
543
  * Parses the %rank% and %rank_logo% template tags.
544
  * @since 1.1
545
+ * @version 1.3
546
  */
547
  public function parse_rank( $content, $user = '', $data = '' ) {
548
 
552
  // User ID does not exist ( user no longer exists )
553
  if ( ! isset( $user->ID ) ) {
554
  foreach ( $this->point_types as $type_id => $label ) {
555
+
556
+ if ( $type_id == MYCRED_DEFAULT_TYPE_KEY ) {
557
  $content = str_replace( '%rank%', '', $content );
558
  $content = str_replace( '%rank_logo%', '', $content );
559
  }
561
  $content = str_replace( '%rank_' . $type_id . '%', '', $content );
562
  $content = str_replace( '%rank_logo_' . $type_id . '%', '', $content );
563
  }
564
+
565
  }
566
  }
567
 
570
 
571
  // Loop the point types and replace template tags
572
  foreach ( $this->point_types as $type_id => $label ) {
573
+
574
  $rank_id = mycred_get_users_rank_id( $user->ID, $type_id );
575
+ if ( $rank_id === false ) {
576
+
577
+ if ( $type_id == MYCRED_DEFAULT_TYPE_KEY ) {
578
  $content = str_replace( '%rank%', '', $content );
579
  $content = str_replace( '%rank_logo%', '', $content );
580
  }
581
  else {
582
+ $content = str_replace( '%rank_' . $type_id . '%', '', $content );
583
+ $content = str_replace( '%rank_logo_' . $type_id . '%', '', $content );
584
  }
585
+
586
  }
587
  else {
588
+
589
+ if ( $type_id == MYCRED_DEFAULT_TYPE_KEY ) {
590
  $content = str_replace( '%rank%', get_the_title( $rank_id ), $content );
591
  $content = str_replace( '%rank_logo%', mycred_get_rank_logo( $rank_id ), $content );
592
  }
594
  $content = str_replace( '%rank_' . $type_id . '%', get_the_title( $rank_id ), $content );
595
  $content = str_replace( '%rank_logo_' . $type_id . '%', mycred_get_rank_logo( $rank_id ), $content );
596
  }
597
+
598
  }
599
+
600
  }
601
  }
602
 
611
  */
612
  public function insert_rank_header() {
613
 
614
+ $output = '';
615
+ $user_id = bp_displayed_user_id();
616
+ $mycred_types = mycred_get_usable_types();
617
 
618
+ foreach ( $mycred_types as $type_id ) {
619
 
620
  // Load type
621
  $mycred = mycred( $type_id );
622
 
 
 
 
623
  // No settings
624
  if ( ! isset( $mycred->rank['bb_location'] ) ) continue;
625
 
626
  // Not shown
627
+ if ( ! in_array( $mycred->rank['bb_location'], array( 'top', 'both' ) ) || $mycred->rank['bb_template'] == '' ) continue;
628
 
629
+ // Get rank (if user has one)
630
  $users_rank = mycred_get_users_rank_id( $user_id, $type_id );
631
+ if ( $users_rank === false ) continue;
632
 
633
  // Parse template
634
  $template = $mycred->rank['bb_template'];
635
  $template = str_replace( '%rank_title%', get_the_title( $users_rank ), $template );
636
+ $template = str_replace( '%rank_logo%', mycred_get_rank_logo( $users_rank, 'full' ), $template );
637
+
638
+ $template = $mycred->template_tags_general( $template );
639
+ $template = '<div class="mycred-my-rank ' . $type_id . '">' . $template . '</div>';
640
 
641
  // Let others play
642
  $output .= apply_filters( 'mycred_bp_header_ranks_row', $template, $user_id, $users_rank, $mycred, $this );
645
 
646
  if ( $output == '' ) return;
647
 
648
+ echo '<div id="mycred-my-ranks">' . apply_filters( 'mycred_bp_rank_in_header', $output, $user_id, $this ) . '</div>';
649
 
650
  }
651
 
652
  /**
653
  * Insert Rank In Profile Details
654
  * @since 1.1
655
+ * @version 1.4
656
  */
657
  public function insert_rank_profile() {
658
 
659
+ $output = '';
660
+ $user_id = bp_displayed_user_id();
661
+ $mycred_types = mycred_get_usable_types();
662
 
663
  $count = 0;
664
+ foreach ( $mycred_types as $type_id ) {
665
 
666
  // Load type
667
  $mycred = mycred( $type_id );
668
 
 
 
 
669
  // No settings
670
  if ( ! isset( $mycred->rank['bb_location'] ) ) continue;
671
 
672
  // Not shown
673
+ if ( ! in_array( $mycred->rank['bb_location'], array( 'profile_tab', 'both' ) ) || $mycred->rank['bb_template'] == '' ) continue;
674
 
675
+ // Get rank (if user has one)
676
  $users_rank = mycred_get_users_rank_id( $user_id, $type_id );
677
+ if ( $users_rank === false ) continue;
678
 
679
  // Parse template
680
  $template = $mycred->rank['bb_template'];
681
  $template = str_replace( '%rank_title%', get_the_title( $users_rank ), $template );
682
  $template = str_replace( '%rank_logo%', mycred_get_rank_logo( $users_rank ), $template );
683
 
684
+ $template = $mycred->template_tags_general( $template );
685
+ $template = '<div class="mycred-my-rank ' . $type_id . '">' . $template . '</div>';
686
+
687
  // Let others play
688
  $output .= apply_filters( 'mycred_bp_profile_ranks_row', $template, $user_id, $users_rank, $mycred, $this );
689
  $count ++;
711
  /**
712
  * Insert Rank In bbPress Reply
713
  * @since 1.6
714
+ * @version 1.1
715
  */
716
  public function insert_rank_bb_reply() {
717
 
719
  $user_id = bbp_get_reply_author_id();
720
  if ( $user_id == 0 ) return;
721
 
722
+ $mycred_types = mycred_get_usable_types();
723
+
724
+ foreach ( $mycred_types as $type_id ) {
725
 
726
  // Load type
727
  $mycred = mycred( $type_id );
728
 
 
 
 
729
  // No settings
730
  if ( ! isset( $mycred->rank['bp_location'] ) ) continue;
731
 
732
  // Not shown
733
+ if ( ! in_array( $mycred->rank['bp_location'], array( 'reply', 'both' ) ) || $mycred->rank['bp_template'] == '' ) continue;
734
 
735
  // Get rank (if user has one
736
  $users_rank = mycred_get_users_rank_id( $user_id, $type_id );
737
+ if ( $users_rank === false ) continue;
738
 
739
  // Parse template
740
  $template = $mycred->rank['bp_template'];
741
  $template = str_replace( '%rank_title%', get_the_title( $users_rank ), $template );
742
  $template = str_replace( '%rank_logo%', mycred_get_rank_logo( $users_rank ), $template );
743
 
744
+ $template = $mycred->template_tags_general( $template );
745
+ $template = '<div class="mycred-my-rank ' . $type_id . '">' . $template . '</div>';
746
+
747
  // Let others play
748
  $output .= apply_filters( 'mycred_bb_reply_ranks_row', $template, $user_id, $users_rank, $mycred, $this );
749
 
751
 
752
  if ( $output == '' ) return;
753
 
754
+ echo '<div id="mycred-my-ranks">' . apply_filters( 'mycred_bb_rank_in_reply', $output, $user_id, $this ) . '</div>';
755
 
756
  }
757
 
762
  */
763
  public function insert_rank_bb_profile() {
764
 
765
+ $output = '';
766
+ $user_id = bbp_get_displayed_user_id();
767
+ $mycred_types = mycred_get_usable_types();
768
 
769
+ foreach ( $mycred_types as $type_id => $label ) {
770
 
771
  // Load type
772
  $mycred = mycred( $type_id );
773
 
 
 
 
774
  // No settings
775
  if ( ! isset( $mycred->rank['bp_location'] ) ) continue;
776
 
777
  // Not shown
778
+ if ( ! in_array( $mycred->rank['bp_location'], array( 'profile', 'both' ) ) || $mycred->rank['bp_template'] == '' ) continue;
779
 
780
  // Get rank (if user has one
781
  $users_rank = mycred_get_users_rank_id( $user_id, $type_id );
782
+ if ( $users_rank === false ) continue;
783
 
784
  // Parse template
785
  $template = $mycred->rank['bp_template'];
786
  $template = str_replace( '%rank_title%', get_the_title( $users_rank ), $template );
787
  $template = str_replace( '%rank_logo%', mycred_get_rank_logo( $users_rank ), $template );
788
 
789
+ $template = $mycred->template_tags_general( $template );
790
+ $template = '<div class="mycred-my-rank ' . $type_id . '">' . $template . '</div>';
791
+
792
  // Let others play
793
  $output .= apply_filters( 'mycred_bb_profile_ranks_row', $template, $user_id, $users_rank, $mycred, $this );
794
 
796
 
797
  if ( $output == '' ) return;
798
 
799
+ echo '<div id="mycred-my-ranks">' . apply_filters( 'mycred_bb_rank_in_profile', $output, $user_id, $this ) . '</div>';
800
 
801
  }
802
 
804
  * Add Default Rank
805
  * Adds the default "Newbie" rank and adds all non-exluded user to this rank.
806
  * Note! This method is only called when there are zero ranks as this will create the new default rank.
 
 
 
 
807
  * @since 1.1
808
+ * @version 1.2
809
  */
810
  public function add_default_rank() {
811
 
812
+ global $mycred_ranks;
813
+
814
  // If there are no ranks at all
815
  if ( ! mycred_have_ranks() ) {
816
 
826
  // Update min and max values
827
  update_post_meta( $rank_id, 'mycred_rank_min', 0 );
828
  update_post_meta( $rank_id, 'mycred_rank_max', 9999999 );
829
+ update_post_meta( $rank_id, 'ctype', MYCRED_DEFAULT_TYPE_KEY );
830
 
831
  $mycred_ranks = 1;
832
  mycred_assign_ranks();
836
  }
837
 
838
  /**
839
+ * Custom User Balance Content
840
+ * Inserts a users rank for each point type.
841
+ * @since 1.6
842
+ * @version 1.1
843
+ */
844
+ public function custom_user_column_content( $balance, $user_id, $type ) {
845
+
846
+ $rank = mycred_get_users_rank( $user_id, $type );
847
+ if ( $rank !== false )
848
+ $balance .= '<small style="display:block;">' . sprintf( '<strong>%s:</strong> %s', __( 'Rank', 'mycred' ), $rank->title ) . '</small>';
849
+
850
+ else
851
+ $balance .= '<small style="display:block;">' . sprintf( '<strong>%s:</strong> -', __( 'Rank', 'mycred' ) ) . '</small>';
852
+
853
+ return $balance;
854
+
855
+ }
856
+
857
+ /**
858
+ * Show Rank in User Editor
859
+ * @since 1.7
860
+ * @version 1.1
861
+ */
862
+ public function show_rank_in_user_editor( $point_type, $user, $mycred ) {
863
+
864
+ $rank = mycred_get_users_rank( $user->ID, $point_type );
865
+ $rank_title = '-';
866
+ if ( isset( $rank->title ) )
867
+ $rank_title = $rank->title;
868
+
869
+ echo '<div class="current-rank">' . sprintf( '<strong>%s:</strong> %s', __( 'Rank', 'mycred' ), $rank_title ) . '</div>';
870
+
871
+ }
872
+
873
+ /**
874
+ * Register Scripts & Styles
875
+ * @since 1.7
876
+ * @version 1.0
877
+ */
878
+ public function scripts_and_styles() {
879
+
880
+
881
+
882
+ }
883
+
884
+ /**
885
+ * Enqueue Scripts & Styles
886
+ * @since 1.1
887
+ * @version 1.3.1
888
+ */
889
+ public function enqueue_scripts() {
890
+
891
+ $adjust_header = false;
892
+ $screen = get_current_screen();
893
+
894
+ wp_register_script(
895
+ 'mycred-rank-tweaks',
896
+ plugins_url( 'assets/js/tweaks.js', myCRED_RANKS ),
897
+ array( 'jquery' ),
898
+ myCRED_VERSION . '.1'
899
+ );
900
+
901
+ wp_register_script(
902
+ 'mycred-rank-management',
903
+ plugins_url( 'assets/js/management.js', myCRED_RANKS ),
904
+ array( 'jquery' ),
905
+ myCRED_VERSION . '.1'
906
+ );
907
+
908
+ // Ranks List Page
909
+ if ( strpos( 'edit-mycred_rank', $screen->id ) > -1 ) {
910
+
911
+ wp_enqueue_style( 'mycred-admin' );
912
+
913
+ if ( isset( $_GET['ctype'] ) && array_key_exists( $_GET['ctype'], $this->point_types ) ) :
914
+
915
+ wp_localize_script(
916
+ 'mycred-rank-tweaks',
917
+ 'myCRED_Ranks',
918
+ array(
919
+ 'rank_ctype' => $_GET['ctype']
920
+ )
921
+ );
922
+ wp_enqueue_script( 'mycred-rank-tweaks' );
923
+
924
+ endif;
925
+
926
+ }
927
+
928
+ // Edit Rank Page
929
+ if ( strpos( 'mycred_rank', $screen->id ) > -1 ) {
930
+
931
+ wp_dequeue_script( 'autosave' );
932
+ wp_enqueue_style( 'mycred-bootstrap-grid' );
933
+ wp_enqueue_style( 'mycred-forms' );
934
+
935
+ add_filter( 'postbox_classes_mycred_rank_mycred-rank-setup', array( $this, 'metabox_classes' ) );
936
+
937
+ ?>
938
+ <style type="text/css">
939
+ #misc-publishing-actions .misc-pub-curtime { display: none; }
940
+ #misc-publishing-actions #visibility { display: none; }
941
+ </style>
942
+ <?php
943
+
944
+ }
945
+
946
+ // Insert management script
947
+ if ( strpos( MYCRED_SLUG . '-settings', $screen->id ) > -1 || substr( $screen->id, -14, 14 ) == '-settings' ) {
948
+
949
+ wp_localize_script(
950
+ 'mycred-rank-management',
951
+ 'myCRED_Ranks',
952
+ array(
953
+ 'ajaxurl' => admin_url( 'admin-ajax.php' ),
954
+ 'token' => wp_create_nonce( 'mycred-management-actions-roles' ),
955
+ 'working' => esc_attr__( 'Processing...', 'mycred' ),
956
+ 'confirm_del' => esc_attr__( 'Warning! All ranks will be deleted! This can not be undone!', 'mycred' ),
957
+ 'confirm_assign' => esc_attr__( 'Are you sure you want to re-assign user ranks?', 'mycred' )
958
+ )
959
+ );
960
+ wp_enqueue_script( 'mycred-rank-management' );
961
+
962
+ }
963
+
964
+ }
965
+
966
+ /**
967
+ * Adjust Rank Sort Order
968
+ * Adjusts the wp query when viewing ranks to order by the min. point requirement.
969
+ * @since 1.1.1
970
  * @version 1.2
971
  */
972
+ public function adjust_wp_query( $query ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
973
 
974
+ // Front End Queries
975
+ if ( ! is_admin() ) {
976
 
977
+ if ( ! is_post_type_archive( 'mycred_rank' ) ) return;
978
 
979
+ // By default we want to only show ranks for the main point type
980
+ if ( ! isset( $_GET['ctype'] ) && $query->is_main_query() ) {
981
+ $query->set( 'meta_query', array(
982
+ array(
983
+ 'key' => 'ctype',
984
+ 'value' => MYCRED_DEFAULT_TYPE_KEY,
985
+ 'compare' => '='
986
+ )
987
+ ) );
988
+ }
989
 
990
+ // Otherwise if ctype is set and it is a point type filter the results
991
+ elseif ( isset( $_GET['ctype'] ) && array_key_exists( $_GET['ctype'], $this->point_types ) ) {
992
+ $query->set( 'meta_query', array(
993
+ array(
994
+ 'key' => 'ctype',
995
+ 'value' => $_GET['ctype'],
996
+ 'compare' => '='
997
+ )
998
+ ) );
999
+ }
1000
 
 
 
1001
  }
1002
 
1003
+ // Admin Queries
1004
+ else {
1005
 
1006
+ if ( ! isset( $query->query['post_type'] ) || $query->query['post_type'] != 'mycred_rank' ) return;
1007
 
1008
+ // If ctype is set, filter ranks according to it's value
1009
+ if ( isset( $_GET['ctype'] ) && array_key_exists( $_GET['ctype'], $this->point_types ) ) {
1010
+ $query->set( 'meta_query', array(
1011
+ array(
1012
+ 'key' => 'ctype',
1013
+ 'value' => $_GET['ctype'],
1014
+ 'compare' => '='
1015
+ )
1016
+ ) );
1017
+ }
1018
 
1019
+ }
 
 
1020
 
1021
+ // Sort by meta value
1022
+ $query->set( 'meta_key', 'mycred_rank_min' );
1023
+ $query->set( 'orderby', 'meta_value_num' );
1024
+
1025
+ // Sort order
1026
+ if ( ! isset( $this->rank['order'] ) ) $this->rank['order'] = 'ASC';
1027
+ $query->set( 'order', $this->rank['order'] );
1028
 
1029
  }
1030
 
1061
 
1062
  $type = get_post_meta( $post_id, 'ctype', true );
1063
  if ( $type == '' )
1064
+ $type = MYCRED_DEFAULT_TYPE_KEY;
1065
 
1066
  // Rank Logo (thumbnail)
1067
  if ( $column_name == 'mycred-rank-logo' ) {
1077
  elseif ( $column_name == 'mycred-rank-req' ) {
1078
 
1079
  $mycred = $this->core;
1080
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY )
1081
  $mycred = mycred( $type );
1082
 
1083
  $min = get_post_meta( $post_id, 'mycred_rank_min', true );
1113
 
1114
  }
1115
 
1116
+ /**
1117
+ * Adjust Row Actions
1118
+ * @since 1.1
1119
+ * @version 1.0
1120
+ */
1121
+ public function adjust_row_actions( $actions, $post ) {
1122
+
1123
+ if ( $post->post_type == 'mycred_rank' ) {
1124
+ unset( $actions['inline hide-if-no-js'] );
1125
+
1126
+ if ( ! $this->rank['public'] )
1127
+ unset( $actions['view'] );
1128
+ }
1129
+
1130
+ return $actions;
1131
+
1132
+ }
1133
+
1134
  /**
1135
  * Adjust Enter Title Here
1136
  * @since 1.1
1151
  * @since 1.1
1152
  * @version 1.0
1153
  */
1154
+ public function add_metaboxes() {
1155
 
1156
  add_meta_box(
1157
+ 'mycred-rank-setup',
1158
+ __( 'Rank Setup', 'mycred' ),
1159
  array( $this, 'rank_settings' ),
1160
  'mycred_rank',
1161
  'normal',
1167
  /**
1168
  * Rank Settings Metabox
1169
  * @since 1.1
1170
+ * @version 1.2
1171
  */
1172
  public function rank_settings( $post ) {
1173
 
1174
  // Get type
1175
  $type = get_post_meta( $post->ID, 'ctype', true );
1176
  if ( $type == '' ) {
1177
+ $type = MYCRED_DEFAULT_TYPE_KEY;
1178
  update_post_meta( $post->ID, 'ctype', $type );
1179
  }
1180
 
1181
  // If a custom type has been requested via the URL
1182
  if ( isset( $_REQUEST['ctype'] ) && ! empty( $_REQUEST['ctype'] ) )
1183
+ $type = sanitize_key( $_REQUEST['ctype'] );
1184
 
1185
  // Load the appropriate type object
1186
  $mycred = $this->core;
1187
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY )
1188
  $mycred = mycred( $type );
1189
 
1190
  // Get min and max setting for this rank
1192
  $max = get_post_meta( $post->ID, 'mycred_rank_max', true );
1193
 
1194
  ?>
1195
+ <div class="form">
1196
+ <div class="row">
1197
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
1198
+ <div class="row">
1199
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
1200
+ <div class="form-group">
1201
+ <label for="mycred-rank-min"><?php _e( 'Minimum Balance Requirement', 'mycred' ); ?></label>
1202
+ <input type="text" name="mycred_rank[mycred_rank_min]" id="mycred-rank-min" class="form-control" value="<?php echo $mycred->number( $min ); ?>" />
1203
+ </div>
1204
+ </div>
1205
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
1206
+ <div class="form-group">
1207
+ <label for="mycred-rank-max"><?php _e( 'Maximum Balance Requirement', 'mycred' ); ?></label>
1208
+ <input type="text" name="mycred_rank[mycred_rank_max]" id="mycred-rank-max" class="form-control" value="<?php echo $mycred->number( $max ); ?>" />
1209
+ </div>
1210
+ </div>
1211
+ </div>
1212
+ <div class="row">
1213
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
1214
+
1215
+ <?php if ( count( $this->point_types ) > 1 ) : ?>
1216
+
1217
+ <div class="form-group">
1218
+ <label for="mycred-rank-point-type"><?php _e( 'Point Type', 'mycred' ); ?></label>
1219
+ <?php mycred_types_select_from_dropdown( 'mycred_rank[ctype]', 'mycred-rank-point-type', $type, false, ' class="form-control"' ); ?>
1220
+ </div>
1221
+
1222
+ <?php else : ?>
1223
+
1224
+ <div class="form-group">
1225
+ <p class="form-control-static"><?php echo $mycred->plural(); ?></p>
1226
+ <input type="hidden" name="mycred_rank[ctype]" value="mycred_default" />
1227
+ </div>
1228
+
1229
+ <?php endif; ?>
1230
+
1231
+ </div>
1232
+ </div>
 
 
1233
  </div>
1234
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
 
 
 
1235
  <?php
1236
 
1237
  // Get all published ranks for this type
1238
+ $all_ranks = mycred_get_ranks( 'publish', '-1', 'DESC', $type );
1239
+
1240
+ if ( ! empty( $all_ranks ) ) {
1241
 
 
1242
  echo '<ul>';
1243
+ foreach ( $all_ranks as $rank ) {
1244
+
1245
+ if ( $rank->minimum == '' ) $rank->minimum = __( 'Not Set', 'mycred' );
1246
+ if ( $rank->maximum == '' ) $rank->maximum = __( 'Not Set', 'mycred' );
1247
+
1248
+ echo '<li><strong>' . $rank->title . '</strong> ' . $rank->minimum . ' - ' . $rank->maximum . '</li>';
1249
+
1250
  }
1251
  echo '</ul>';
1252
+
1253
  }
1254
  else {
1255
+
1256
+ echo '<p>' . __( 'No ranks found', 'mycred' ) . '.</p>';
1257
+
1258
  }
1259
 
1260
  ?>
 
 
1261
  </div>
1262
  </div>
 
1263
  </div>
1264
  <?php
1265
 
1268
  /**
1269
  * Save Rank Details
1270
  * @since 1.1
1271
+ * @version 1.4
1272
  */
1273
+ public function save_rank( $post_id, $post ) {
1274
 
1275
  // Make sure fields exists
1276
+ if ( $post === NULL || ! current_user_can( $this->core->edit_creds_cap() ) || ! isset( $_POST['mycred_rank'] ) ) return;
 
 
 
 
 
 
 
 
1277
 
1278
+ $changed = 0;
1279
+ $point_type = sanitize_key( $_POST['mycred_rank']['ctype'] );
 
 
 
1280
 
1281
+ foreach ( $_POST['mycred_rank'] as $meta_key => $meta_value ) {
1282
 
1283
+ $new_value = sanitize_text_field( $meta_value );
1284
+ $old_value = get_post_meta( $post_id, $meta_key, true );
1285
+ if ( $new_value != $old_value ) {
1286
+ update_post_meta( $post_id, $meta_key, $new_value );
1287
+ $changed ++;
1288
+ }
1289
 
1290
+ }
1291
 
1292
+ if ( $changed > 0 )
1293
+ mycred_assign_ranks( $point_type );
1294
 
1295
  }
1296
 
1303
 
1304
  $prefs = $this->rank;
1305
  $this->add_to_core = true;
1306
+ if ( $mycred->mycred_type != MYCRED_DEFAULT_TYPE_KEY ) {
1307
 
1308
  if ( ! isset( $mycred->rank ) )
1309
  $prefs = $this->default_prefs;
1320
  $box = 'display: block;';
1321
 
1322
  ?>
1323
+ <h4><span class="dashicons dashicons-admin-plugins static"></span><?php _e( 'Ranks', 'mycred' ); ?></h4>
1324
  <div class="body" style="display:none;">
1325
 
1326
+ <?php if ( $mycred->mycred_type == MYCRED_DEFAULT_TYPE_KEY ) { ?>
1327
 
1328
  <label class="subheader" for="<?php echo $this->field_id( 'public' ); ?>"><?php _e( 'Rank Features', 'mycred' ); ?></label>
1329
  <ol id="myCRED-rank-supports">
1606
  if ( $this->core->is_multisite && $GLOBALS['blog_id'] > 1 && ! $this->core->use_master_template )
1607
  $rank_meta_key .= '_' . $GLOBALS['blog_id'];
1608
 
1609
+ if ( $mycred->mycred_type != MYCRED_DEFAULT_TYPE_KEY )
1610
  $rank_meta_key .= $mycred->mycred_type;
1611
 
1612
  ?>
1629
 
1630
  }
1631
 
1632
+ /**
1633
+ * Zero Balance Action
1634
+ * When an admin selects to zero out all balances
1635
+ * we want to remove all ranks as well.
1636
+ * @since 1.6
1637
+ * @version 1.0
1638
+ */
1639
+ public function zero_balance_action( $type = '' ) {
1640
+
1641
+ global $wpdb;
1642
+
1643
+ // Get rank key
1644
+ $rank_meta_key = 'mycred_rank';
1645
+ if ( $this->core->is_multisite && $GLOBALS['blog_id'] > 1 && ! $this->core->use_master_template )
1646
+ $rank_meta_key .= '_' . $GLOBALS['blog_id'];
1647
+
1648
+ if ( array_key_exists( $type, $this->point_types ) && $type != MYCRED_DEFAULT_TYPE_KEY )
1649
+ $rank_meta_key .= $type;
1650
+
1651
+ $wpdb->delete(
1652
+ $wpdb->usermeta,
1653
+ array( 'meta_key' => $rank_meta_key ),
1654
+ array( '%s' )
1655
+ );
1656
+
1657
+ }
1658
+
1659
+ /**
1660
+ * Delete Ranks
1661
+ * @since 1.3.2
1662
+ * @version 1.1
1663
+ */
1664
+ public function action_delete_ranks() {
1665
+
1666
+ // Security
1667
+ check_ajax_referer( 'mycred-management-actions-roles', 'token' );
1668
+
1669
+ // Define type
1670
+ $type = MYCRED_DEFAULT_TYPE_KEY;
1671
+ if ( isset( $_POST['ctype'] ) && array_key_exists( $_POST['ctype'], $this->point_types ) )
1672
+ $type = sanitize_text_field( $_POST['ctype'] );
1673
+
1674
+ global $wpdb;
1675
+
1676
+ // Get the appropriate tables based on setup
1677
+ if ( ! mycred_override_settings() ) {
1678
+ $posts = $wpdb->posts;
1679
+ $postmeta = $wpdb->postmeta;
1680
+ }
1681
+ else {
1682
+ $posts = $wpdb->base_prefix . 'posts';
1683
+ $postmeta = $wpdb->base_prefix . 'postmeta';
1684
+ }
1685
+
1686
+ // First get the ids of all existing ranks
1687
+ $rank_ids = $wpdb->get_col( $wpdb->prepare( "
1688
+ SELECT DISTINCT ranks.ID
1689
+ FROM {$posts} ranks
1690
+ INNER JOIN {$postmeta} ctype
1691
+ ON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s )
1692
+ WHERE ranks.post_type = %s
1693
+ AND ctype.meta_value = %s;", 'ctype', 'mycred_rank', $type ) );
1694
+
1695
+ // If ranks were found
1696
+ $rows = 0;
1697
+ if ( ! empty( $rank_ids ) ) {
1698
+
1699
+ $id_list = implode( ',', $rank_ids );
1700
+
1701
+ // Remove posts
1702
+ $wpdb->query( "
1703
+ DELETE FROM {$posts}
1704
+ WHERE post_type = 'mycred_rank'
1705
+ AND post_id IN ({$id_list});" );
1706
+
1707
+ // Remove post meta
1708
+ $wpdb->query( "
1709
+ DELETE FROM {$postmeta}
1710
+ WHERE post_id IN ({$id_list});" );
1711
+
1712
+ // Confirm that ranks are gone by counting ranks
1713
+ // If all went well this should return zero.
1714
+ $rows = $wpdb->get_var( $wpdb->prepare( "
1715
+ SELECT COUNT(*)
1716
+ FROM {$posts} ranks
1717
+ INNER JOIN {$postmeta} ctype
1718
+ ON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s )
1719
+ WHERE ranks.post_type = %s
1720
+ AND ctype.meta_value = %s;", 'ctype', 'mycred_rank', $type ) );
1721
+
1722
+ }
1723
+
1724
+ die( json_encode( array( 'status' => 'OK', 'rows' => $rows ) ) );
1725
+
1726
+ }
1727
+
1728
+ /**
1729
+ * Assign Ranks
1730
+ * @since 1.3.2
1731
+ * @version 1.1
1732
+ */
1733
+ public function action_assign_ranks() {
1734
+
1735
+ check_ajax_referer( 'mycred-management-actions-roles', 'token' );
1736
+
1737
+ $type = MYCRED_DEFAULT_TYPE_KEY;
1738
+ if ( isset( $_POST['ctype'] ) && array_key_exists( $_POST['ctype'], $this->point_types ) )
1739
+ $type = sanitize_text_field( $_POST['ctype'] );
1740
+
1741
+ $adjustments = mycred_assign_ranks( $type );
1742
+ die( json_encode( array( 'status' => 'OK', 'rows' => $adjustments ) ) );
1743
+
1744
+ }
1745
+
1746
  }
1747
+ endif;
1748
+
1749
+ /**
1750
+ * Load Ranks Module
1751
+ * @since 1.7
1752
+ * @version 1.0
1753
+ */
1754
+ if ( ! function_exists( 'mycred_load_ranks_addon' ) ) :
1755
+ function mycred_load_ranks_addon( $modules, $point_types ) {
1756
 
1757
+ $modules['solo']['ranks'] = new myCRED_Ranks_Module();
1758
+ $modules['solo']['ranks']->load();
1759
 
1760
+ return $modules;
1761
+
1762
+ }
1763
  endif;
1764
+ add_filter( 'mycred_load_modules', 'mycred_load_ranks_addon', 80, 2 );
1765
 
1766
  ?>
addons/sell-content/assets/js/buy-content.js CHANGED
@@ -1,46 +1,60 @@
1
  /**
2
  * myCRED Sell Content
3
  * @since 1.1
4
- * @version 1.0.1
5
  */
6
  (function($) {
7
 
8
- var mycred_buy_content = function( button, label ) {
9
 
10
- wrapper = button.parents( 'div.mycred-content-forsale' );
 
 
 
 
11
 
12
  $.ajax({
13
  type : "POST",
14
  data : {
15
  action : 'mycred-buy-content',
16
- postid : button.attr( 'data-id' ),
17
- token : myCREDsell.token
 
18
  },
19
- dataType : "HTML",
20
- url : myCREDsell.ajaxurl,
21
- // Before we start
22
  beforeSend : function() {
23
 
24
- button.attr( 'value', myCREDsell.working );
25
- button.attr( 'disabled', 'disabled' );
26
- wrapper.slideUp();
27
 
28
  },
29
- // On Successful Communication
30
- success : function( data ) {
31
 
32
- wrapper.empty();
33
- wrapper.append( data );
34
- wrapper.slideDown();
35
 
36
- }
37
- });
 
 
 
 
 
 
 
 
 
38
 
39
- };
40
-
41
- $( '.mycred-sell-this-button' ).click(function(){
42
 
43
- mycred_buy_content( $(this), $(this).attr( 'value' ) );
 
 
 
 
 
 
 
44
 
45
  });
46
 
1
  /**
2
  * myCRED Sell Content
3
  * @since 1.1
4
+ * @version 1.1
5
  */
6
  (function($) {
7
 
8
+ $( '.mycred-sell-this-wrapper' ).on( 'click', '.mycred-buy-this-content-button', function(){
9
 
10
+ var button = $(this);
11
+ var post_id = button.data( 'pid' );
12
+ var point_type = button.data( 'type' );
13
+ var buttonlabel = button.html();
14
+ var content_for_sale = $( '#mycred-buy-content' + post_id );
15
 
16
  $.ajax({
17
  type : "POST",
18
  data : {
19
  action : 'mycred-buy-content',
20
+ token : myCREDBuyContent.token,
21
+ postid : post_id,
22
+ ctype : point_type
23
  },
24
+ dataType : "JSON",
25
+ url : myCREDBuyContent.ajaxurl,
 
26
  beforeSend : function() {
27
 
28
+ button.attr( 'disabled', 'disabled' ).html( myCREDBuyContent.working );
 
 
29
 
30
  },
31
+ success : function( response ) {
 
32
 
33
+ if ( response.success === undefined || ( response.success === true && myCREDBuyContent.reload === '1' ) )
34
+ location.reload();
 
35
 
36
+ else {
37
+
38
+ if ( response.success ) {
39
+ content_for_sale.fadeOut(function(){
40
+ content_for_sale.removeClass( 'mycred-sell-this-wrapper mycred-sell-entire-content mycred-sell-partial-content' ).empty().append( response.data ).fadeIn();
41
+ });
42
+ }
43
+
44
+ else {
45
+
46
+ button.removeAttr( 'disabled' ).html( buttonlabel );
47
 
48
+ alert( response.data.message );
 
 
49
 
50
+ }
51
+
52
+ }
53
+
54
+ console.log( response );
55
+
56
+ }
57
+ });
58
 
59
  });
60
 
addons/sell-content/includes/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ ?>
addons/sell-content/includes/mycred-sell-functions.php ADDED
@@ -0,0 +1,805 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * Sell Content Settings
6
+ * Returns the sell content add-ons settings.
7
+ * @since 1.7
8
+ * @version 1.0
9
+ */
10
+ if ( ! function_exists( 'mycred_sell_content_settings' ) ) :
11
+ function mycred_sell_content_settings() {
12
+
13
+ $mycred = mycred();
14
+ $settings = $mycred->sell_content;
15
+
16
+ return $settings;
17
+
18
+ }
19
+ endif;
20
+
21
+ /**
22
+ * Get Post Types
23
+ * Returns an array of sellable post types. In order for a post type to be
24
+ * considered "usable", it must be public.
25
+ * @since 1.7
26
+ * @version 1.0
27
+ */
28
+ if ( ! function_exists( 'mycred_sell_content_post_types' ) ) :
29
+ function mycred_sell_content_post_types() {
30
+
31
+ $args = array(
32
+ 'public' => true,
33
+ '_builtin' => false
34
+ );
35
+
36
+ $post_types = get_post_types( $args, 'objects', 'OR' );
37
+
38
+ $eligeble_types = array();
39
+ $mycred_post_types = get_mycred_post_types();
40
+
41
+ if ( ! empty( $post_types ) ) {
42
+ foreach ( $post_types as $post_type => $setup ) {
43
+
44
+ if ( $setup->public != 1 ) continue;
45
+
46
+ if ( in_array( $post_type, $mycred_post_types ) ) continue;
47
+
48
+ $eligeble_types[ $post_type ] = $setup->labels->name;
49
+
50
+ }
51
+ }
52
+
53
+ return $eligeble_types;
54
+
55
+ }
56
+ endif;
57
+
58
+ /**
59
+ * Get The Post ID
60
+ * Will attempt to get the current posts ID. Also supports bbPress
61
+ * where it will return either the form ID (if viewing a forum), the topic ID
62
+ * (if viewing a topic) or the reply ID (if viewing a reply).
63
+ * @since 1.7
64
+ * @version 1.0
65
+ */
66
+ if ( ! function_exists( 'mycred_sell_content_post_id' ) ) :
67
+ function mycred_sell_content_post_id() {
68
+
69
+ $post_id = $bbp_topic_id = $bbp_reply_id = false;
70
+
71
+ // Check if we are selling access to bbPress forum, topic or reply
72
+ if ( function_exists( 'bbpress' ) ) {
73
+
74
+ global $wp_query;
75
+
76
+ $bbp = bbpress();
77
+
78
+ // Currently inside a topic loop
79
+ if ( ! empty( $bbp->topic_query->in_the_loop ) && isset( $bbp->topic_query->post->ID ) )
80
+ $bbp_topic_id = $bbp->topic_query->post->ID;
81
+
82
+ // Currently inside a search loop
83
+ elseif ( ! empty( $bbp->search_query->in_the_loop ) && isset( $bbp->search_query->post->ID ) && bbp_is_topic( $bbp->search_query->post->ID ) )
84
+ $bbp_topic_id = $bbp->search_query->post->ID;
85
+
86
+ // Currently viewing/editing a topic, likely alone
87
+ elseif ( ( bbp_is_single_topic() || bbp_is_topic_edit() ) && ! empty( $bbp->current_topic_id ) )
88
+ $bbp_topic_id = $bbp->current_topic_id;
89
+
90
+ // Currently viewing/editing a topic, likely in a loop
91
+ elseif ( ( bbp_is_single_topic() || bbp_is_topic_edit() ) && isset( $wp_query->post->ID ) )
92
+ $bbp_topic_id = $wp_query->post->ID;
93
+
94
+ // So far, no topic found, check if we are in a reply
95
+ if ( $bbp_topic_id === false ) {
96
+
97
+ // Currently inside a replies loop
98
+ if ( ! empty( $bbp->reply_query->in_the_loop ) && isset( $bbp->reply_query->post->ID ) )
99
+ $bbp_reply_id = $bbp->reply_query->post->ID;
100
+
101
+ // Currently inside a search loop
102
+ elseif ( ! empty( $bbp->search_query->in_the_loop ) && isset( $bbp->search_query->post->ID ) && bbp_is_reply( $bbp->search_query->post->ID ) )
103
+ $bbp_reply_id = $bbp->search_query->post->ID;
104
+
105
+ // Currently viewing a forum
106
+ elseif ( ( bbp_is_single_reply() || bbp_is_reply_edit() ) && ! empty( $bbp->current_reply_id ) )
107
+ $bbp_reply_id = $bbp->current_reply_id;
108
+
109
+ // Currently viewing a reply
110
+ elseif ( ( bbp_is_single_reply() || bbp_is_reply_edit() ) && isset( $wp_query->post->ID ) )
111
+ $bbp_reply_id = $wp_query->post->ID;
112
+
113
+ if ( $bbp_reply_id !== false )
114
+ $post_id = $bbp_reply_id;
115
+
116
+ }
117
+
118
+ // Else we are in a topic
119
+ else $post_id = $bbp_topic_id;
120
+
121
+ }
122
+
123
+ if ( $post_id === false ) {
124
+
125
+ global $post;
126
+
127
+ if ( isset( $post->ID ) )
128
+ $post_id = $post->ID;
129
+
130
+ }
131
+
132
+ return apply_filters( 'mycred_sell_this_get_post_ID', $post_id );
133
+
134
+ }
135
+ endif;
136
+
137
+ /**
138
+ * Post Type for Sale
139
+ * Returns either true (post type is for sale) or false (post type not for sale).
140
+ * @since 1.7
141
+ * @version 1.0
142
+ */
143
+ if ( ! function_exists( 'mycred_post_type_for_sale' ) ) :
144
+ function mycred_post_type_for_sale( $post_type = NULL ) {
145
+
146
+ $settings = mycred_sell_content_settings();
147
+ $for_sale = false;
148
+
149
+ if ( array_key_exists( 'post_types', $settings ) && ! empty( $settings['post_types'] ) ) {
150
+
151
+ $post_types = explode( ',', $settings['post_types'] );
152
+ if ( in_array( $post_type, $post_types ) )
153
+ $for_sale = true;
154
+
155
+ }
156
+
157
+ return $for_sale;
158
+
159
+ }
160
+ endif;
161
+
162
+ /**
163
+ * Post is for Sale
164
+ * Returns true (post is for sale) or false (post is not for sale).
165
+ * @since 1.7
166
+ * @version 1.0
167
+ */
168
+ if ( ! function_exists( 'mycred_post_is_for_sale' ) ) :
169
+ function mycred_post_is_for_sale( $post = NULL ) {
170
+
171
+ if ( ! is_object( $post ) )
172
+ $post = get_post( $post );
173
+
174
+ // Invalid post - not for sale
175
+ if ( ! isset( $post->ID ) ) return false;
176
+
177
+ $settings = mycred_sell_content_settings();
178
+ $point_types = $settings['type'];
179
+ $for_sale = false;
180
+
181
+ // We start with checking the post type.
182
+ if ( mycred_post_type_for_sale( $post->post_type ) ) {
183
+
184
+ $filter = $settings['filters'][ $post->post_type ]['by'];
185
+ $list = explode( ',', $settings['filters'][ $post->post_type ]['list'] );
186
+
187
+ // Manual filter - check saved settings
188
+ if ( $filter === 'manual' ) {
189
+
190
+ // Loop through each point type we allow and check the settings to see if anyone is enabled
191
+ foreach ( $point_types as $type_id ) {
192
+
193
+ $suffix = '_' . $type_id;
194
+ if ( $type_id == MYCRED_DEFAULT_TYPE_KEY )
195
+ $suffix = '';
196
+
197
+ $sale_setup = (array) get_post_meta( $post->ID, 'myCRED_sell_content' . $suffix, true );
198
+ if ( array_key_exists( 'status', $sale_setup ) && $sale_setup['status'] === 'enabled' )
199
+ $for_sale = true;
200
+
201
+ }
202
+
203
+ }
204
+
205
+ // All posts for sale
206
+ elseif ( $filter === 'all' ) {
207
+
208
+ $for_sale = true;
209
+
210
+ }
211
+
212
+ // Posts are set for sale but some are excluded
213
+ elseif ( $filter === 'exclude' ) {
214
+
215
+ // If post is not excluded, it is for sale
216
+ if ( ! in_array( $post->ID, $list ) )
217
+ $for_sale = true;
218
+
219
+ }
220
+
221
+ // Posts are not for sale but some are
222
+ elseif ( $filter === 'include' ) {
223
+
224
+ // If post is included, it is for sale
225
+ if ( in_array( $post->ID, $list ) )
226
+ $for_sale = true;
227
+
228
+ }
229
+
230
+ // Taxonomy check
231
+ else {
232
+
233
+ $check = 'include';
234
+ $taxonomy = $filter;
235
+
236
+ if ( substr( $taxonomy, 0, 1 ) === '-' ) {
237
+ $check = 'exclude';
238
+ $taxonomy = ltrim( $taxonomy );
239
+ }
240
+
241
+ // Get post terms
242
+ $terms = wp_get_post_terms( $post->ID, $taxonomy );
243
+
244
+ // Taxonomy exclude check
245
+ if ( $check === 'exclude' ) {
246
+
247
+ if ( ! empty( $terms ) ) {
248
+ foreach ( $terms as $term ) {
249
+
250
+ if ( in_array( $term->slug, $list ) ) continue;
251
+ $for_sale = true;
252
+
253
+ }
254
+ }
255
+
256
+ // No terms - not excluded
257
+ else {
258
+ $for_sale = true;
259
+ }
260
+
261
+ }
262
+
263
+ // Taxonomy include check
264
+ else {
265
+
266
+ if ( ! empty( $terms ) ) {
267
+ foreach ( $terms as $term ) {
268
+
269
+ if ( ! in_array( $term->slug, $list ) ) continue;
270
+ $for_sale = true;
271
+
272
+ }
273
+ }
274
+
275
+ }
276
+
277
+ }
278
+
279
+ }
280
+
281
+ return apply_filters( 'mycred_post_is_for_sale', $for_sale, $post, $settings );
282
+
283
+ }
284
+ endif;
285
+
286
+ /**
287
+ * User Has Paid
288
+ * Checks if a user has paid for the given post. Will also take into account
289
+ * if a purchase has expired (if used).
290
+ * @since 1.7
291
+ * @version 1.0
292
+ */
293
+ if ( ! function_exists( 'mycred_user_paid_for_content' ) ) :
294
+ function mycred_user_paid_for_content( $user_id = NULL, $post_id = NULL ) {
295
+
296
+ global $wpdb, $mycred;
297
+
298
+ $has_paid = false;
299
+ $last_payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$mycred->log_table} WHERE user_id = %d AND ref = 'buy_content' AND ref_id = %d ORDER BY time DESC LIMIT 1;", $user_id, $post_id ) );
300
+
301
+ // Found a payment
302
+ if ( $last_payment !== NULL ) {
303
+
304
+ $has_paid = true;
305
+
306
+ // Check for expirations
307
+ if ( mycred_content_purchase_has_expired( $last_payment ) )
308
+ $has_paid = false;
309
+
310
+ }
311
+
312
+ // All else there are no purchases
313
+ return apply_filters( 'mycred_user_has_paid_for_content', $has_paid, $user_id, $post_id, $last_payment );
314
+
315
+ }
316
+ endif;
317
+
318
+ /**
319
+ * Sale Expired
320
+ * Checks if a given purchase has expired. Left this in place from the old version
321
+ * for backwards comp.
322
+ * @since 1.7
323
+ * @version 1.0
324
+ */
325
+ if ( ! function_exists( 'mycred_content_purchase_has_expired' ) ) :
326
+ function mycred_content_purchase_has_expired( $payment = NULL ) {
327
+
328
+ if ( ! is_object( $payment ) ) return false;
329
+
330
+ $settings = mycred_sell_content_settings();
331
+ $post = get_post( $payment->ref_id );
332
+ $point_types = $settings['type'];
333
+ $has_expired = false;
334
+ $length = 0;
335
+
336
+ // Invalid post
337
+ if ( ! isset( $post->ID ) ) return $has_expired;
338
+
339
+ $filter = $settings['filters'][ $post->post_type ]['by'];
340
+
341
+ // Manual mode - expiration settings are found in the post setting
342
+ if ( $filter === 'manual' ) {
343
+
344
+ $suffix = '_' . $payment->ctype;
345
+ if ( $payment->ctype == MYCRED_DEFAULT_TYPE_KEY )
346
+ $suffix = '';
347
+
348
+ $sale_setup = (array) get_post_meta( $post->ID, 'myCRED_sell_content' . $suffix, true );
349
+ if ( array_key_exists( 'expire', $sale_setup ) && $sale_setup['expire'] > 0 )
350
+ $length = $sale_setup['expire'];
351
+
352
+ }
353
+
354
+ // Else we need to check the point type setup in our add-on settings.
355
+ else {
356
+
357
+ $point_type_setup = mycred_get_option( 'mycred_sell_this_' . $payment->ctype );
358
+ if ( array_key_exists( 'expire', $point_type_setup ) && $point_type_setup['expire'] > 0 )
359
+ $length = $point_type_setup['expire'];
360
+
361
+ }
362
+
363
+ // If expiration is set
364
+ if ( $length > 0 ) {
365
+
366
+ $expiration = apply_filters( 'mycred_sell_expire_calc', absint( $length * 3600 ), $length, $user_id, $post->ID ) + $payment->time;
367
+
368
+ if ( $expiration <= current_time( 'timestamp' ) )
369
+ $has_expired = true;
370
+
371
+ }
372
+
373
+ return apply_filters( 'mycred_sell_content_sale_expired', $has_expired, $user_id, $post->ID, $payment->time, $length );
374
+
375
+ }
376
+ endif;
377
+
378
+ /**
379
+ * Get Payment Buttons
380
+ * Returns all payment buttons a user can use to pay for a given post.
381
+ * @since 1.7
382
+ * @version 1.0
383
+ */
384
+ if ( ! function_exists( 'mycred_sell_content_payment_buttons' ) ) :
385
+ function mycred_sell_content_payment_buttons( $user_id = NULL, $post_id = NULL ) {
386
+
387
+ if ( $user_id === NULL || $post_id === NULL ) return false;
388
+
389
+ $settings = mycred_sell_content_settings();
390
+ $post = get_post( $post_id );
391
+ $result = false;
392
+
393
+ if ( ! empty( $settings['type'] ) ) {
394
+
395
+ $buttons = array();
396
+
397
+ foreach ( $settings['type'] as $point_type ) {
398
+
399
+ // Load point type
400
+ $mycred = mycred( $point_type );
401
+ $setup = mycred_get_option( 'mycred_sell_this_' . $point_type );
402
+ $price = mycred_get_content_price( $post_id, $point_type, $user_id );
403
+
404
+ // Point type not enabled
405
+ if ( $setup['status'] === 'disabled' ) continue;
406
+
407
+ // Make sure we are not excluded from this type
408
+ if ( $mycred->exclude_user( $user_id ) ) continue;
409
+
410
+ // Make sure we can afford to pay
411
+ if ( $mycred->get_users_balance( $user_id, $point_type ) < $price ) continue;
412
+
413
+ $button_label = str_replace( '%price%', $mycred->format_creds( $price ), $setup['button_label'] );
414
+
415
+ $button = '<button type="button" class="mycred-buy-this-content-button ' . $setup['button_classes'] . '" data-pid="' . $post_id . '" data-type="' . $point_type . '">' . $button_label . '</button>';
416
+ $buttons[] = apply_filters( 'mycred_sell_this_button', $button, $post, $setup, $mycred );
417
+
418
+ }
419
+
420
+ if ( ! empty( $buttons ) )
421
+ $result = implode( ' ', $buttons );
422
+
423
+ }
424
+
425
+ // Return a string of buttons or false if user can not afford
426
+ return apply_filters( 'mycred_sellcontent_buttons', $result, $user_id, $post_id );
427
+
428
+ }
429
+ endif;
430
+
431
+ /**
432
+ * Sell Content Template
433
+ * Parses a particular template.
434
+ * @since 1.7
435
+ * @version 1.0
436
+ */
437
+ if ( ! function_exists( 'mycred_sell_content_template' ) ) :
438
+ function mycred_sell_content_template( $template = '', $post = NULL, $type = 'mycred-sell-partial-content', $status = 'visitor' ) {
439
+
440
+ if ( ! is_object( $post ) || strlen( $template ) === 0 ) return $template;
441
+
442
+ $post_type = get_post_type_object( $post->post_type );
443
+ $url = get_permalink( $post->ID );
444
+
445
+ // Remove old tags that are no longer supported
446
+ $template = str_replace( array( '%price%', '%expires%', ), '', $template );
447
+
448
+ $template = str_replace( '%post_title%', get_the_title( $post->ID ), $template );
449
+ $template = str_replace( '%post_type%', $post_type->labels->singular_name, $template );
450
+ $template = str_replace( '%post_url%', $url, $template );
451
+ $template = str_replace( '%link_with_title%', '<a href="' . $url . '">' . $post->post_title . '</a>', $template );
452
+
453
+ $template = apply_filters( 'mycred_sell_content_template', $template, $post, $type );
454
+
455
+ $wrapper_classes = array();
456
+ $wrapper_classes[] = 'mycred-sell-this-wrapper';
457
+ $wrapper_classes[] = $type;
458
+ $wrapper_classes[] = $status;
459
+
460
+ $wrapper_classes = apply_filters( 'mycred_sell_template_class', $wrapper_classes, $post );
461
+
462
+ return '<div id="mycred-buy-content' . $post->ID . '" class="' . implode( ' ', $wrapper_classes ) . '" data-pid="' . $post->ID . '">' . $template . '</div>';
463
+
464
+ }
465
+ endif;
466
+
467
+ /**
468
+ * New Purchase
469
+ * Handles the purchase of a particular post by a given user.
470
+ * @returns true (bool) on success or an error message (string)
471
+ * @since 1.7
472
+ * @version 1.0
473
+ */
474
+ if ( ! function_exists( 'mycred_sell_content_new_purchase' ) ) :
475
+ function mycred_sell_content_new_purchase( $post = NULL, $user_id = NULL, $point_type = NULL ) {
476
+
477
+ if ( ! is_object( $post ) )
478
+ $post = get_post( $post );
479
+
480
+ if ( ! isset( $post->ID ) ) return false;
481
+
482
+ $mycred = mycred( $point_type );
483
+ $settings = mycred_sell_content_settings();
484
+ $setup = mycred_get_option( 'mycred_sell_this_' . $point_type );
485
+ $result = apply_filters( 'mycred_before_content_purchase', false, $post->ID, $user_id, $point_type );
486
+
487
+ // We handle payment
488
+ if ( $result === false ) {
489
+
490
+ // Disabled point type or user is excluded.
491
+ if ( $setup['status'] === 'disabled' || $mycred->exclude_user( $user_id ) )
492
+ $result = sprintf( _x( 'You can not pay using %s', 'Point type name', 'mycred' ), $mycred->plural() );
493
+
494
+ else {
495
+
496
+ $balance = $mycred->get_users_balance( $user_id, $point_type );
497
+ $price = mycred_get_content_price( $post->ID, $point_type, $user_id );
498
+
499
+ // Insufficient funds (not free)
500
+ if ( $price > 0 && $balance < $price )
501
+ $result = __( 'Insufficient funds.', 'mycred' );
502
+
503
+ // Content is not free
504
+ elseif ( $price > 0 ) {
505
+
506
+ // Need a unqiue transaction id
507
+ $transaction_id = 'TXID' . $user_id . current_time( 'timestamp' ) . $post->ID;
508
+
509
+ // Charge buyer
510
+ $mycred->add_creds(
511
+ 'buy_content',
512
+ $user_id,
513
+ 0 - $price,
514
+ $setup['log_payment'],
515
+ $post->ID,
516
+ array(
517
+ 'ref_type' => 'post',
518
+ 'purchase_id' => $transaction_id,
519
+ 'seller' => $post->post_author
520
+ ),
521
+ $point_type
522
+ );
523
+
524
+ // Profit Sharing
525
+ if ( $setup['profit_share'] > 0 ) {
526
+
527
+ // Let others play with the users profit share
528
+ $percentage = mycred_get_authors_profit_share( $post->post_author, $point_type, $setup['profit_share'] );
529
+ if ( $percentage !== false ) {
530
+
531
+ // Convert percentage to a share amount
532
+ $share = ( $percentage / 100 ) * $price;
533
+
534
+ // Pay the author
535
+ $mycred->add_creds(
536
+ 'sell_content',
537
+ $post->post_author,
538
+ $share,
539
+ $setup['log_sale'],
540
+ $post->ID,
541
+ array(
542
+ 'ref_type' => 'post',
543
+ 'purchase_id' => $transaction_id,
544
+ 'buyer' => $user_id
545
+ ),
546
+ $point_type
547
+ );
548
+
549
+ }
550
+
551
+ }
552
+
553
+ $result = true;
554
+
555
+ // Delete counters to trigger new db query
556
+ delete_post_meta( $post->ID, '_mycred_content_sales' );
557
+ delete_post_meta( $post->ID, '_mycred_content_buyers' );
558
+
559
+ }
560
+
561
+ // Free
562
+ else {
563
+ $result = true;
564
+ }
565
+
566
+ }
567
+
568
+ }
569
+
570
+ return apply_filters( 'mycred_after_content_purchase', $result, $post, $user_id, $point_type );
571
+
572
+ }
573
+ endif;
574
+
575
+ /**
576
+ * Get Content Price
577
+ * Returns the contents price.
578
+ * @since 1.7
579
+ * @version 1.0
580
+ */
581
+ if ( ! function_exists( 'mycred_get_content_price' ) ) :
582
+ function mycred_get_content_price( $post_id = NULL, $point_type = NULL, $user_id = NULL ) {
583
+
584
+ $mycred = mycred( $point_type );
585
+ $settings = mycred_sell_content_settings();
586
+
587
+ $setup = mycred_get_option( 'mycred_sell_this_' . $point_type );
588
+ $price = $mycred->number( $setup['price'] );
589
+
590
+ if ( $settings['filters'][ get_post_type( $post_id ) ]['by'] === 'manual' ) {
591
+
592
+ $suffix = '_' . $point_type;
593
+ if ( $point_type == MYCRED_DEFAULT_TYPE_KEY )
594
+ $suffix = '';
595
+
596
+ $sale_setup = (array) get_post_meta( $post_id, 'myCRED_sell_content' . $suffix, true );
597
+ if ( array_key_exists( 'price', $sale_setup ) )
598
+ $price = $mycred->number( $sale_setup['price'] );
599
+
600
+ }
601
+
602
+ return apply_filters( 'mycred_get_content_price', $price, $post_id, $point_type, $user_id );
603
+
604
+ }
605
+ endif;
606
+
607
+ /**
608
+ * Get Users Purchased Content
609
+ * Returns an array of log entries for content purchases.
610
+ * @since 1.7
611
+ * @version 1.0
612
+ */
613
+ if ( ! function_exists( 'mycred_get_users_purchased_content' ) ) :
614
+ function mycred_get_users_purchased_content( $user_id = NULL, $number = 25, $order = 'DESC', $point_type = NULL ) {
615
+
616
+ global $wpdb, $mycred;
617
+
618
+ $limit = '';
619
+ if ( absint( $number ) > 0 )
620
+ $limit = $wpdb->prepare( "LIMIT 0,%d", $number );
621
+
622
+ $wheres = array();
623
+ $wheres[] = "ref = 'buy_content'";
624
+ $wheres[] = $wpdb->prepare( "user_id = %d", $user_id );
625
+
626
+ if ( $point_type !== NULL && mycred_point_type_exists( $point_type ) )
627
+ $wheres[] = $wpdb->prepare( "ctype = %s", $point_type );
628
+
629
+ $wheres = 'WHERE ' . implode( ' AND ', $wheres );
630
+
631
+ if ( ! in_array( $order, array( 'ASC', 'DESC' ) ) )
632
+ $order = 'DESC';
633
+
634
+ $sql = apply_filters( 'mycred_get_users_purchased_content', "SELECT * FROM {$mycred->log_table} {$wheres} ORDER BY time {$order} {$limit};", $user_id, $number, $order, $point_type );
635
+
636
+ return $wpdb->get_results( $sql );
637
+
638
+ }
639
+ endif;
640
+
641
+ /**
642
+ * Get Posts Buyers
643
+ * Returns an array of User IDs of the user that has purchased
644
+ * a given post.
645
+ * @since 1.7
646
+ * @version 1.0
647
+ */
648
+ if ( ! function_exists( 'mycred_get_posts_buyers' ) ) :
649
+ function mycred_get_posts_buyers( $post_id = NULL, $number = 25, $point_type = NULL ) {
650
+
651
+ global $wpdb, $mycred;
652
+
653
+ $limit = '';
654
+ if ( absint( $number ) > 0 )
655
+ $limit = $wpdb->prepare( "LIMIT 0,%d", $number );
656
+
657
+ $wheres = array();
658
+ $wheres[] = "ref = 'buy_content'";
659
+ $wheres[] = $wpdb->prepare( "ref_id = %d", $post_id );
660
+
661
+ if ( $point_type !== NULL && mycred_point_type_exists( $point_type ) )
662
+ $wheres[] = $wpdb->prepare( "ctype = %s", $point_type );
663
+
664
+ $wheres = 'WHERE ' . implode( ' AND ', $wheres );
665
+
666
+ return $wpdb->get_col( "SELECT user_id FROM {$mycred->log_table} {$wheres} ORDER BY time DESC {$limit};" );
667
+
668
+ }
669
+ endif;
670
+
671
+ /**
672
+ * Get Content Sales Count
673
+ * Returns the number of times a content has been purchased.
674
+ * @since 1.7
675
+ * @version 1.0
676
+ */
677
+ if ( ! function_exists( 'mycred_get_content_sales_count' ) ) :
678
+ function mycred_get_content_sales_count( $post_id = NULL ) {
679
+
680
+ $count = get_post_meta( $post_id, '_mycred_content_sales', true );
681
+ if ( strlen( $count ) == 0 ) {
682
+
683
+ $count = mycred_count_ref_id_instances( 'buy_content', $post_id );
684
+ add_post_meta( $post_id, '_mycred_content_sales', $count, true );
685
+
686
+ }
687
+
688
+ return apply_filters( 'mycred_content_sales_count', $count, $post_id );
689
+
690
+ }
691
+ endif;
692
+
693
+ /**
694
+ * Get Content Buyer Count
695
+ * Returns the number of buyers a particular content has.
696
+ * @since 1.7
697
+ * @version 1.0
698
+ */
699
+ if ( ! function_exists( 'mycred_get_content_buyers_count' ) ) :
700
+ function mycred_get_content_buyers_count( $post_id = NULL ) {
701
+
702
+ $count = get_post_meta( $post_id, '_mycred_content_buyers', true );
703
+ if ( strlen( $count ) == 0 ) {
704
+
705
+ global $wpdb, $mycred;
706
+
707
+ $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( DISTINCT user_id ) FROM {$mycred->log_table} WHERE ref = 'buy_content' AND ref_id = %d;", $post_id ) );
708
+ if ( $count === NULL ) $count = 0;
709
+
710
+ add_post_meta( $post_id, '_mycred_content_buyers', $count, true );
711
+
712
+ }
713
+
714
+ return apply_filters( '_mycred_content_buyers', $count, $post_id );
715
+
716
+ }
717
+ endif;
718
+
719
+ /**
720
+ * Get Authors Profit Share
721
+ * Get a particular users profit share percentage. If not set, the general setup for the given
722
+ * point type is returned instead.
723
+ * @since 1.7
724
+ * @version 1.0
725
+ */
726
+ if ( ! function_exists( 'mycred_get_authors_profit_share' ) ) :
727
+ function mycred_get_authors_profit_share( $user_id = NULL, $point_type = NULL, $default = false ) {
728
+
729
+ $users_share = mycred_get_user_meta( $user_id, 'mycred_sell_content_share_' . $point_type, '', true );
730
+ if ( strlen( $users_share ) == 0 )
731
+ $users_share = $default;
732
+
733
+ return apply_filters( 'mycred_sell_content_profit_share', $users_share, $user_id, $point_type, $default );
734
+
735
+ }
736
+ endif;
737
+
738
+ /**
739
+ * Get Post Type Options
740
+ * Returns an array of filter options for post types.
741
+ * @since 1.7
742
+ * @version 1.0
743
+ */
744
+ if ( ! function_exists( 'mycred_get_post_type_options' ) ) :
745
+ function mycred_get_post_type_options( $post_type ) {
746
+
747
+ $type = get_post_type_object( $post_type );
748
+ if ( ! is_object( $type ) ) return;
749
+
750
+ $options = array();
751
+ $options['all'] = array(
752
+ 'label' => sprintf( _x( 'All %s', 'all post type name', 'mycred' ), $type->labels->name ),
753
+ 'data' => ''
754
+ );
755
+ $options['manual'] = array(
756
+ 'label' => sprintf( _x( '%s I manually select', 'all post type name', 'mycred' ), $type->labels->name ),
757
+ 'data' => ''
758
+ );
759
+ $options['exclude'] = array(
760
+ 'label' => sprintf( _x( 'All %s except', '%s = post type name', 'mycred' ), $type->labels->name ),
761
+ 'data' => sprintf( _x( 'Comma separated list of %s IDs to exclude', '%s = post type name', 'mycred' ), $type->labels->singular_name )
762
+ );
763
+ $options['include'] = array(
764
+ 'label' => sprintf( _x( 'Only %s', '%s = post type name', 'mycred' ), $type->labels->name ),
765
+ 'data' => sprintf( _x( 'Comma separated list of %s IDs', '%s = post type name', 'mycred' ), $type->labels->singular_name )
766
+ );
767
+
768
+ $taxonomies = get_object_taxonomies( $post_type, 'objects' );
769
+ if ( ! empty( $taxonomies ) ) {
770
+
771
+ foreach ( $taxonomies as $taxonomy_id => $term ) {
772
+
773
+ if ( ! $term->public ) continue;
774
+
775
+ if ( $term->hierarchical ) {
776
+ $options[ $taxonomy_id ] = array(
777
+ 'label' => sprintf( _x( 'Only %s in %s', 'e.g. Only "Posts" in "Categories"', 'mycred' ), $type->labels->name, $term->labels->name ),
778
+ 'data' => sprintf( _x( 'Comma separated list of %s slugs', '%s = taxonomy name', 'mycred' ), $term->labels->singular_name )
779
+ );
780
+ $options[ '_' . $taxonomy_id ] = array(
781
+ 'label' => sprintf( _x( 'Only %s not in %s', 'e.g. Only "Posts" not in "Categories"', 'mycred' ), $type->labels->name, $term->labels->name ),
782
+ 'data' => sprintf( _x( 'Comma separated list of %s slugs', '%s = taxonomy name', 'mycred' ), $term->labels->singular_name )
783
+ );
784
+ }
785
+ else {
786
+ $options[ $taxonomy_id ] = array(
787
+ 'label' => sprintf( _x( 'Only %s with %s', 'e.g. Only "Posts" with "Tags"', 'mycred' ), $type->labels->name, $term->labels->name ),
788
+ 'data' => sprintf( _x( 'Comma separated list of %s slugs', '%s = taxonomy name', 'mycred' ), $term->labels->singular_name )
789
+ );
790
+ $options[ '_' . $taxonomy_id ] = array(
791
+ 'label' => sprintf( _x( 'Only %s without %s', 'e.g. Only "Posts" without "Tags"', 'mycred' ), $type->labels->name, $term->labels->name ),
792
+ 'data' => sprintf( _x( 'Comma separated list of %s slugs', '%s = taxonomy name', 'mycred' ), $term->labels->singular_name )
793
+ );
794
+ }
795
+
796
+ }
797
+
798
+ }
799
+
800
+ return apply_filters( 'mycred_sell_post_type_options', $options, $post_type );
801
+
802
+ }
803
+ endif;
804
+
805
+ ?>
addons/sell-content/includes/mycred-sell-shortcodes.php ADDED
@@ -0,0 +1,319 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * Shortcode: Sell This
6
+ * This shortcode is intended to be used when selling parts of a content.
7
+ * Can only be used once per content.
8
+ * @since 1.7
9
+ * @version 1.0
10
+ */
11
+ if ( ! function_exists( 'mycred_render_sell_this' ) ) :
12
+ function mycred_render_sell_this( $atts, $content = '' ) {
13
+
14
+ global $mycred_partial_content_sale, $mycred_sell_content;
15
+
16
+ $post_id = mycred_sell_content_post_id();
17
+ $post = get_post( $post_id );
18
+ $user_id = get_current_user_id();
19
+
20
+ $mycred_partial_content_sale = true;
21
+
22
+ // Logged in users
23
+ if ( is_user_logged_in() ) {
24
+
25
+ // Authors and admins do not pay
26
+ if ( ! mycred_is_admin() && $post->post_author !== $user_id ) {
27
+
28
+ $status = 'mycred-sell-paid';
29
+
30
+ // In case we have not paid
31
+ if ( ! mycred_user_paid_for_content( $user_id, $post_id ) ) {
32
+
33
+ // Get Payment Options
34
+ $payment_options = mycred_sell_content_payment_buttons( $user_id, $post_id );
35
+
36
+ // User can buy
37
+ if ( $payment_options !== false ) {
38
+
39
+ $content = $mycred_sell_content->sell_content['templates']['members'];
40
+ $content = str_replace( '%buy_button%', $payment_options, $content );
41
+ $content = mycred_sell_content_template( $content, $post, 'mycred-sell-partial-content', 'mycred-sell-unpaid' );
42
+
43
+ }
44
+
45
+ // Can not afford to buy
46
+ else {
47
+
48
+ $content = $mycred_sell_content->sell_content['templates']['cantafford'];
49
+ $content = mycred_sell_content_template( $content, $post, 'mycred-sell-partial-content', 'mycred-sell-insufficient' );
50
+
51
+ }
52
+
53
+ }
54
+
55
+ }
56
+
57
+ }
58
+
59
+ // Visitors
60
+ else {
61
+
62
+ $content = $mycred_sell_content->sell_content['templates']['visitors'];
63
+ $content = mycred_sell_content_template( $content, $post, 'mycred-sell-partial-content', 'mycred-sell-visitor' );
64
+
65
+ }
66
+
67
+ return $content;
68
+
69
+ }
70
+ endif;
71
+
72
+ /**
73
+ * Shortcode: Sell This AJAX
74
+ * Depreciated as of version 1.7 and will be removed in version 1.8
75
+ * @since 1.3
76
+ * @version 1.0
77
+ */
78
+ if ( ! function_exists( 'mycred_render_sell_this_ajax' ) ) :
79
+ function mycred_render_sell_this_ajax( $atts, $content = '' ) {
80
+
81
+ _doing_it_wrong( 'mycred_render_sell_this_ajax', 'The mycred_sell_this_ajax shortcode has been depreciated and will be removed in version 1.8.', '1.7' );
82
+
83
+ return mycred_render_sell_this( $atts, $content );
84
+
85
+ }
86
+ endif;
87
+
88
+ /**
89
+ * Shortcode: Sales Counter
90
+ * Renders the total number of times this post has been purchased or the total number of
91
+ * active sales right now, if sales expire.
92
+ * @attribute wrapper (string) - optional html element to wrap around the value.
93
+ * @attribute post_id (int) - option to get the count for the provided post ID.
94
+ * @since 1.7
95
+ * @version 1.0
96
+ */
97
+ if ( ! function_exists( 'mycred_render_sell_count' ) ) :
98
+ function mycred_render_sell_count( $atts, $content = '' ) {
99
+
100
+ extract( shortcode_atts( array(
101
+ 'wrapper' => '',
102
+ 'post_id' => NULL
103
+ ), $atts ) );
104
+
105
+ if ( $post_id === NULL )
106
+ $post_id = mycred_sell_content_post_id();
107
+
108
+ $content = '';
109
+
110
+ if ( $wrapper != '' )
111
+ $content .= '<' . $wrapper . ' class="mycred-sell-this-sales-count">';
112
+
113
+ $content .= mycred_get_content_sales_count( $post_id );
114
+
115
+ if ( $wrapper != '' )
116
+ $content .= '</' . $wrapper . '>';
117
+
118
+ return $content;
119
+
120
+ }
121
+ endif;
122
+
123
+ /**
124
+ * Shortcode: Sales Buyer Counter
125
+ * Renders the total number of unique users that has purchased this content.
126
+ * @attribute wrapper (string) - optional html element to wrap around the value.
127
+ * @attribute post_id (int) - option to get the count for the provided post ID.
128
+ * @since 1.7
129
+ * @version 1.0
130
+ */
131
+ if ( ! function_exists( 'mycred_render_sell_buyer_count' ) ) :
132
+ function mycred_render_sell_buyer_count( $atts, $content = '' ) {
133
+
134
+ extract( shortcode_atts( array(
135
+ 'wrapper' => '',
136
+ 'post_id' => NULL
137
+ ), $atts ) );
138
+
139
+ if ( $post_id === NULL )
140
+ $post_id = mycred_sell_content_post_id();
141
+
142
+ $content = '';
143
+
144
+ if ( $wrapper != '' )
145
+ $content .= '<' . $wrapper . ' class="mycred-sell-this-author-count">';
146
+
147
+ $content .= mycred_get_content_buyers_count( $post_id );
148
+
149
+ if ( $wrapper != '' )
150
+ $content .= '</' . $wrapper . '>';
151
+
152
+ return $content;
153
+
154
+ }
155
+ endif;
156
+
157
+ /**
158
+ * Shortcode: Sales History
159
+ * Will show a given users payment history with links to the posts
160
+ * they have purchased.
161
+ * @since 1.7
162
+ * @version 1.0
163
+ */
164
+ if ( ! function_exists( 'mycred_render_sell_history' ) ) :
165
+ function mycred_render_sell_history( $atts, $content = '' ) {
166
+
167
+ extract( shortcode_atts( array(
168
+ 'user_id' => 'current',
169
+ 'number' => 25,
170
+ 'nothing' => 'No purchases found',
171
+ 'ctype' => NULL,
172
+ 'order' => 'DESC'
173
+ ), $atts ) );
174
+
175
+ // Not logged in
176
+ if ( ! is_user_logged_in() && $user_id == 'current' )
177
+ return $content;
178
+
179
+ $user_id = mycred_get_user_id( $user_id );
180
+ $date_format = get_option( 'date_format' );
181
+ $expiration = apply_filters( 'mycred_sell_exp_title', __( 'Hour(s)', 'mycred' ) );
182
+ $purchases = mycred_get_users_purchased_content( $user_id, $number, $order, $ctype );
183
+
184
+ $columns = apply_filters( 'mycred_sales_history_columns', array(
185
+ 'col-date' => __( 'Date', 'mycred' ),
186
+ 'col-title' => __( 'Title', 'mycred' ),
187
+ 'col-amount' => __( 'Cost', 'mycred' ),
188
+ 'col-expires' => __( 'Expires', 'mycred' )
189
+ ), $atts );
190
+
191
+ if ( empty( $purchases ) && $no_result == '' ) return;
192
+
193
+ ob_start();
194
+
195
+ ?>
196
+ <div class="table-responsive mycred-sell-this-history">
197
+ <table class="table">
198
+ <thead>
199
+ <tr>
200
+ <?php
201
+
202
+ foreach ( $columns as $column_id => $column_label )
203
+ echo '<th class="mycred-sell-' . $column_id . ' ' . $column_id . '">' . $column_label . '</th>';
204
+
205
+ ?>
206
+ </thead>
207
+ <tbody>
208
+ <?php
209
+
210
+ if ( ! empty( $purchases ) ) {
211
+ foreach ( $purchases as $entry ) {
212
+
213
+ $mycred = mycred( $entry->ctype );
214
+ $prefs = mycred_get_post_sale_setup( $entry->ref_id, $entry->ctype );
215
+
216
+ echo '<td class="mycred-sell-' . $column_id . ' ' . $column_id . '">';
217
+
218
+ foreach ( $columns as $column_id => $column_label ) {
219
+
220
+ if ( $column_id == 'col-date' )
221
+ echo date( $date_format, $entry->time );
222
+
223
+ elseif ( $column_id == 'col-title' )
224
+ echo '<a href="' . get_permalink( $entry->ref_id ) . '">' . get_the_title( $entry->ref_id ) . '</a>';
225
+
226
+ elseif ( $column_id == 'col-amount' )
227
+ echo '<td class="">' . $mycred->format_creds( abs( $entry->creds ) ) . '</td>';
228
+
229
+ elseif ( $column_id == 'col-expires' ) {
230
+
231
+ $expires = __( 'Never', 'mycred' );
232
+ if ( $prefs['expire'] > 0 )
233
+ $expires = sprintf( _x( 'Purchase expires in %s', 'e.g. 10 hours', 'mycred' ), ' ' . $prefs['expire'] . ' ' . $expiration );
234
+
235
+ echo '<td class="">' . $expires . '</td>';
236
+
237
+ }
238
+ else {
239
+
240
+ do_action( 'mycred_sales_history_column', $column_id, $entry, $prefs );
241
+ do_action( 'mycred_sales_history_column_' . $column_id, $entry, $prefs );
242
+
243
+ }
244
+
245
+ }
246
+
247
+ echo '</td>';
248
+
249
+ }
250
+ }
251
+ else {
252
+
253
+ echo '<tr><td class="no-results" colspan="' . count( $columns ) . '">' . $no_result . '</td></tr>';
254
+
255
+ }
256
+
257
+ ?>
258
+ </tbody>
259
+ </table>
260
+ </div>
261
+ <?php
262
+
263
+ $content = ob_get_contents();
264
+ ob_end_clean();
265
+
266
+ return $content;
267
+
268
+ }
269
+ endif;
270
+
271
+ /**
272
+ * Shortcode: Buyer Avatars
273
+ * Renders a given number of avatars of past buyers for this post.
274
+ * @since 1.7
275
+ * @version 1.0
276
+ */
277
+ if ( ! function_exists( 'mycred_render_sell_buyer_avatars' ) ) :
278
+ function mycred_render_sell_buyer_avatars( $atts, $content = '' ) {
279
+
280
+ extract( shortcode_atts( array(
281
+ 'post_id' => NULL,
282
+ 'number' => 10,
283
+ 'size' => 42,
284
+ 'ctype' => NULL,
285
+ 'use_email' => 0,
286
+ 'default' => '',
287
+ 'alt' => ''
288
+ ), $atts ) );
289
+
290
+ if ( $post_id === NULL )
291
+ $post_id = mycred_sell_content_post_id();
292
+
293
+ $buyers = mycred_get_posts_buyers( $post_id, $number, $ctype );
294
+
295
+ $content = '';
296
+ if ( ! empty( $buyers ) ) {
297
+ foreach ( $buyers as $buyer_id ) {
298
+
299
+ $identification = $buyer_id;
300
+ if ( absint( $use_email ) === 1 ) {
301
+ $buyer_object = get_userdata( $buyer_id );
302
+ if ( ! isset( $buyer_object->ID ) ) continue;
303
+ $identification = $buyer_object->user_email;
304
+ }
305
+
306
+ $avatar = get_avatar( $identification, $size, $default, $alt );
307
+ $avatar = apply_filters( 'mycred_sell_content_buyer_avatar', $avatar, $buyer_id, $post_id );
308
+ if ( $avatar !== false )
309
+ $content .= $avatar;
310
+
311
+ }
312
+ }
313
+
314
+ return '<div class="mycred-sell-this-buyers">' . $content . '</div>';
315
+
316
+ }
317
+ endif;
318
+
319
+ ?>
addons/sell-content/myCRED-addon-sell-content.php CHANGED
@@ -2,24 +2,29 @@
2
  /**
3
  * Addon: Sell Content
4
  * Addon URI: http://mycred.me/add-ons/sell-content/
5
- * Version: 1.4.1
6
- * Description: This add-on allows you to sell posts, pages or any public post types on your website. You can either sell the entire content or using our shortcode, sell parts of your content allowing you to offer "teasers".
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
 
12
- define( 'myCRED_SELL', __FILE__ );
13
- define( 'myCRED_SELL_VERSION', myCRED_VERSION . '.1' );
 
 
 
 
 
 
14
 
15
  /**
16
  * myCRED_Sell_Content_Module class
17
  * @since 0.1
18
- * @version 1.2
19
  */
20
  if ( ! class_exists( 'myCRED_Sell_Content_Module' ) ) :
21
  class myCRED_Sell_Content_Module extends myCRED_Module {
22
 
 
 
23
  /**
24
  * Construct
25
  */
@@ -30,380 +35,246 @@ if ( ! class_exists( 'myCRED_Sell_Content_Module' ) ) :
30
  'register' => false,
31
  'defaults' => array(
32
  'post_types' => 'post,page',
33
- 'type' => 'mycred_default',
34
- 'pay' => 'none',
35
- 'pay_percent' => 100,
 
36
  'templates' => array(
37
- 'members' => '<p>Buy this %post_type% for only %price% %buy_button%</p>',
38
- 'visitors' => '<p><a href="%login_url_here%">Login</a> to buy access to this %post_type%.</p>',
39
- 'cantafford' => "<p>You do not have enough %plural% to buy access to this %post_type%.</p>\n<p><strong>Price</strong>: %price%</p>"
40
- ),
41
- 'defaults' => array(
42
- 'price' => 10,
43
- 'overwrite_price' => 0,
44
- 'button_label' => __( 'Buy Now', 'mycred' ),
45
- 'overwrite_buttonlabel' => 0,
46
- 'expire' => 0
47
- ),
48
- 'logs' => array(
49
- 'buy' => 'Purchase of %link_with_title%',
50
- 'sell' => 'Sale of %link_with_title%'
51
  )
52
  ),
53
  'add_to_core' => true
54
  ) );
55
 
56
- // Adjust Module to the selected point type
57
- $this->mycred_type = 'mycred_default';
58
- if ( isset( $this->sell_content['type'] ) )
59
- $this->mycred_type = $this->sell_content['type'];
60
-
61
- $this->core = mycred( $this->mycred_type );
62
-
63
- add_filter( 'mycred_get_email_events', array( $this, 'email_notice_instance' ), 10, 2 );
64
- add_filter( 'mycred_email_before_send', array( $this, 'email_notices' ), 10, 2 );
65
 
66
  }
67
 
68
  /**
69
- * Load
70
  * @since 0.1
71
  * @version 1.2
72
  */
73
  public function module_init() {
74
 
75
- $this->make_purchase();
76
- $this->exp_title = apply_filters( 'mycred_sell_exp_title', __( 'Hours', 'mycred' ) );
77
 
78
- add_filter( 'the_content', array( $this, 'the_content' ), 30 );
 
 
79
 
80
- add_shortcode( 'mycred_sell_this', array( $this, 'render_shortcode' ) );
81
- add_shortcode( 'mycred_sell_this_ajax', array( $this, 'render_ajax_shortcode' ) );
82
- add_shortcode( 'mycred_sales_history', array( $this, 'render_sales_history' ) );
83
- add_shortcode( 'mycred_content_sale_count', array( $this, 'render_no_of_sales' ) );
84
 
85
- add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
86
- add_action( 'save_post', array( $this, 'save_metabox' ) );
 
 
 
 
 
87
 
88
- add_action( 'mycred_admin_enqueue', array( $this, 'admin_enqueue' ) );
89
- add_action( 'mycred_front_enqueue', array( $this, 'front_enqueue' ) );
90
- add_action( 'wp_footer', array( $this, 'footer' ) );
91
- add_action( 'wp_ajax_mycred-buy-content', array( $this, 'make_purchase_ajax' ) );
92
-
93
- add_action( 'mycred_edit_profile', array( $this, 'user_level_profit_shares' ), 20, 2 );
94
- add_action( 'mycred_edit_profile_action', array( $this, 'save_user_override' ) );
95
- add_action( 'mycred_admin_notices', array( $this, 'update_profile_notice' ) );
96
 
97
  }
98
 
99
  /**
100
- * User Level Override
101
- * @since 1.5
102
  * @version 1.0
103
  */
104
- function user_level_profit_shares( $user, $type ) {
105
-
106
- if ( $this->sell_content['pay'] != 'author' || $this->sell_content['type'] != $type ) return;
107
-
108
- $users_share = mycred_get_user_meta( $user->ID, 'mycred_sell_content_share_' . $type, '', true );
109
-
110
- ?>
111
- <h3>Sell Content</h3>
112
- <table class="form-table">
113
- <tr>
114
- <th scope="row"><?php _e( 'Profit Share', 'mycred' ); ?></th>
115
- <td>
116
- <input type="text" name="mycred_adjust_users_profitshare[share]" id="mycred-adjust-users-profit-share" value="<?php echo $users_share; ?>" placeholder="<?php echo $this->sell_content['pay_percent']; ?>" size="8" /> %<br />
117
- <span class="description"><?php _e( 'Leave empty to use the default value.', 'mycred' ); ?></span>
118
- </td>
119
- </tr>
120
- <tr>
121
- <th scope="row"></th>
122
- <td><?php submit_button( __( 'Save Profit Share', 'mycred' ), 'primary medium', 'mycred_adjust_users_profitshare_run', false ); ?></td>
123
- </tr>
124
- </table>
125
- <?php
126
-
127
- }
128
-
129
- /**
130
- * Save Override
131
- * @since 1.5
132
- * @version 1.1.1
133
- */
134
- function save_user_override() {
135
 
136
- if ( isset( $_POST['mycred_adjust_users_profitshare_run'] ) && isset( $_POST['mycred_adjust_users_profitshare'] ) ) {
137
 
138
- $ctype = sanitize_key( $_GET['ctype'] );
139
- $user_id = absint( $_GET['user_id'] );
140
- $share = $_POST['mycred_adjust_users_profitshare']['share'];
141
 
142
- if ( $share != '' ) {
143
-
144
- if ( isfloat( $share ) )
145
- $share = (float) $share;
146
- else
147
- $share = (int) $share;
148
 
149
- mycred_update_user_meta( $user_id, 'mycred_sell_content_share_' . $ctype, '', $share );
150
 
151
- }
 
152
 
153
- else {
 
154
 
155
- mycred_delete_user_meta( $user_id, 'mycred_sell_content_share_' . $ctype );
156
 
157
- }
 
 
 
 
 
158
 
159
- wp_safe_redirect( add_query_arg( array( 'result' => 'sell_content_share' ) ) );
160
 
161
- exit;
162
 
163
- }
 
 
 
 
 
164
 
165
  }
166
 
167
  /**
168
- * Override Update Notice
169
- * @since 1.5
170
  * @version 1.0
171
  */
172
- function update_profile_notice() {
173
 
174
- if ( isset( $_GET['page'] ) && $_GET['page'] == 'mycred-edit-balance' && isset( $_GET['result'] ) && $_GET['result'] == 'sell_content_share' )
175
- echo '<div class="updated"><p>' . __( 'Profit Share override saved', 'mycred' ) . '</p></div>';
 
 
 
 
 
176
 
177
  }
178
 
179
  /**
180
- * Make Purchase
181
- * @since 0.1
182
- * @version 1.3.1
183
  */
184
- public function make_purchase() {
185
-
186
- global $mycred_content_purchase;
187
-
188
- $mycred_content_purchase = false;
189
- if ( ! $this->is_installed() ) return;
190
- if ( ! isset( $_POST['mycred_purchase_token'] ) || ! isset( $_POST['mycred_purchase'] ) || ! isset( $_POST['mycred_purchase']['mst_action'] ) || ! isset( $_POST['mycred_purchase']['mst_author'] ) || ! isset( $_POST['mycred_purchase']['mst_post_id'] ) || ! isset( $_POST['mycred_purchase']['mst_post_type'] ) || ! isset( $_POST['mycred_purchase']['mst_user_id'] ) ) return;
191
- if ( ! wp_verify_nonce( $_POST['mycred_purchase_token'], 'buy-content' ) ) return;
192
-
193
- $action = sanitize_key( $_POST['mycred_purchase']['mst_action'] );
194
- $post_id = absint( $_POST['mycred_purchase']['mst_post_id'] );
195
- $post_type = sanitize_key( $_POST['mycred_purchase']['mst_post_type'] );
196
- $user_id = absint( $_POST['mycred_purchase']['mst_user_id'] );
197
- $author = absint( $_POST['mycred_purchase']['mst_author'] );
198
-
199
- $sell_content = $this->sell_content;
200
- $prefs = $this->get_sale_prefs( $post_id );
201
-
202
- $request = compact( 'action', 'post_id', 'user_id', 'author', 'post_type', 'sell_content', 'prefs' );
203
- do_action( 'mycred_sell_content_purchase_request', $request );
204
-
205
- if ( is_user_logged_in() && ! $this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
206
-
207
- // Charge
208
- $this->core->add_creds(
209
- 'buy_content',
210
- $user_id,
211
- 0-$prefs['price'],
212
- $sell_content['logs']['buy'],
213
- $post_id,
214
- array(
215
- 'ref_type' => 'post',
216
- 'purchase_id' => 'TXID' . date_i18n( 'U' ),
217
- 'seller' => $author
218
- ),
219
- $this->mycred_type
220
- );
221
-
222
- do_action( 'mycred_sell_content_purchase_ready', $request );
223
-
224
- // Payout
225
- if ( $sell_content['pay'] == 'author' ) {
226
-
227
- // Check if author has a custom share
228
- $users_share = mycred_get_user_meta( $author, 'mycred_sell_content_share_' . $this->mycred_type, '', true );
229
- if ( $users_share == '' )
230
- $users_share = $sell_content['pay_percent'];
231
-
232
- if ( isfloat( $users_share ) )
233
- $users_share = (float) $users_share;
234
- else
235
- $users_share = (int) $users_share;
236
-
237
- $payout = ( $users_share / 100 ) * $prefs['price'];
238
-
239
- $this->core->add_creds(
240
- 'sell_content',
241
- $author,
242
- $payout,
243
- $sell_content['logs']['sell'],
244
- $post_id,
245
- array(
246
- 'ref_type' => 'post',
247
- 'purchase_id' => 'TXID' . date_i18n( 'U' ),
248
- 'buyer' => $user_id
249
- ),
250
- $this->mycred_type
251
- );
252
- }
253
-
254
- $mycred_content_purchase = true;
255
- do_action( 'mycred_sell_content_payment_complete', $request );
256
 
257
- }
258
 
259
  }
260
 
261
  /**
262
- * Make Purchase AJAX
263
- * @since 1.1
264
- * @version 1.3
265
  */
266
- public function make_purchase_ajax() {
267
 
268
- // We must be logged in
269
- if ( ! is_user_logged_in() ) die;
270
 
271
- // Security
272
- check_ajax_referer( 'mycred-buy-content-ajax', 'token' );
273
-
274
- // Prep
275
- $post_id = $_POST['postid'];
276
- $user_id = get_current_user_id();
277
- $action = 'buy-content-ajax';
278
- $sell_content = $this->sell_content;
279
- $prefs = $this->get_sale_prefs( $post_id );
280
-
281
- if ( ! $this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
282
-
283
- $post = get_post( $post_id );
284
-
285
- // Charge
286
- $this->core->add_creds(
287
- 'buy_content',
288
- $user_id,
289
- 0-$prefs['price'],
290
- $sell_content['logs']['buy'],
291
- $post_id,
292
- array(
293
- 'ref_type' => 'post',
294
- 'purchase_id' => 'TXID' . date_i18n( 'U' ),
295
- 'seller' => $post->post_author
296
- ),
297
- $this->mycred_type
298
- );
299
-
300
- $request = compact( 'action', 'post_id', 'user_id', 'author', 'post_type', 'sell_content', 'prefs' );
301
- do_action( 'mycred_sell_content_purchase_ready', $request );
302
-
303
- // Pay
304
- if ( $sell_content['pay'] == 'author' ) {
305
-
306
- // Check if author has a custom share
307
- $users_share = mycred_get_user_meta( $post->post_author, 'mycred_sell_content_share_' . $this->mycred_type, '', true );
308
- if ( $users_share == '' )
309
- $users_share = $sell_content['pay_percent'];
310
-
311
- if ( isfloat( $users_share ) )
312
- $users_share = (float) $users_share;
313
- else
314
- $users_share = (int) $users_share;
315
-
316
- $payout = ( $users_share / 100 ) * $prefs['price'];
317
-
318
- $this->core->add_creds(
319
- 'sell_content',
320
- $post->post_author,
321
- $payout,
322
- $sell_content['logs']['sell'],
323
- $post_id,
324
- array(
325
- 'ref_type' => 'post',
326
- 'purchase_id' => 'TXID' . date_i18n( 'U' ),
327
- 'buyer' => $user_id
328
- ),
329
- $this->mycred_type
330
- );
331
 
332
- }
 
 
 
 
 
333
 
334
- // $match[1] = start tag, $match[2] = settings, $match[3] = content, $match[4] = end tag
335
- preg_match( "'(\[mycred_sell_this_ajax(.{1,})\])(.*?)(\[\/mycred_sell_this_ajax\])'si", $post->post_content, $match );
336
 
337
- // Filter content before returning
338
- $content = apply_filters( 'the_content', $match[3] );
339
- $content = str_replace( ']]>', ']]&gt;', $content );
340
- $content = do_shortcode( $content );
 
 
 
 
341
 
342
- }
 
343
 
344
- // Someone is trying to make a purchase but not allowed
345
- else {
 
346
 
347
- $content = '<p>' . __( 'You can not buy this content.', 'mycred' ) . '</p>';
 
 
 
 
 
348
 
 
349
  }
350
 
351
- die( $content );
 
 
 
 
 
352
 
353
  }
354
 
355
  /**
356
- * Enqueue Admin
357
- * @since 0.1
358
- * @version 1.0
359
  */
360
- public function admin_enqueue() { }
361
 
362
- /**
363
- * Enqueue Front
364
- * @since 1.1
365
- * @version 1.0
366
- */
367
- public function front_enqueue() {
368
 
369
- global $mycred_buy_content;
370
 
371
- wp_register_script(
372
- 'mycred-buy-content',
373
- plugins_url( 'assets/js/buy-content.js', myCRED_SELL ),
374
- array( 'jquery' ),
375
- myCRED_SELL_VERSION . '.1',
376
- true
377
- );
 
 
 
 
378
 
379
  }
380
 
381
  /**
382
- * Footer
383
- * @since 1.1
384
  * @version 1.0
385
  */
386
- public function footer() {
387
-
388
- global $mycred_buy_content;
389
-
390
- if ( $mycred_buy_content === true ) {
391
 
392
- wp_localize_script(
393
- 'mycred-buy-content',
394
- 'myCREDsell',
395
- array(
396
- 'ajaxurl' => admin_url( 'admin-ajax.php' ),
397
- 'working' => esc_attr__( 'Processing...', 'mycred' ),
398
- 'error' => esc_attr__( 'Error. Try Again', 'mycred' ),
399
- 'token' => wp_create_nonce( 'mycred-buy-content-ajax' )
400
- )
401
- );
402
-
403
- wp_enqueue_script( 'mycred-buy-content' );
404
 
 
 
 
 
 
405
  }
406
 
 
 
407
  }
408
 
409
  /**
@@ -413,1007 +284,785 @@ if ( ! class_exists( 'myCRED_Sell_Content_Module' ) ) :
413
  */
414
  public function after_general_settings( $mycred = NULL ) {
415
 
416
- $sell_content = $this->sell_content;
417
- if ( ! isset( $sell_content['defaults']['expire'] ) )
418
- $sell_content['defaults']['expire'] = 0;
419
-
420
- $before = $this->core->before;
421
- $after = $this->core->after;
422
- $payees = array(
423
- 'none' => __( 'No Payout. Just charge.' ),
424
- 'author' => __( 'Pay Content Author.' )
425
- );
426
 
427
- $available_payees = apply_filters( 'mycred_sell_content_payees', $payees, $sell_content );
428
 
429
  ?>
430
- <h4><div class="icon icon-active"></div><?php _e( 'Sell Content', 'mycred' ); ?></h4>
431
- <div class="body" style="display:none;">
432
- <label class="subheader" for="<?php echo $this->field_id( 'post_types' ); ?>"><?php _e( 'Post Types', 'mycred' ); ?></label>
433
- <ol id="myCRED-buy-postypes">
434
- <li>
435
- <div class="h2"><input type="text" name="<?php echo $this->field_name( 'post_types' ); ?>" id="<?php echo $this->field_id( 'post_types' ); ?>" value="<?php echo $sell_content['post_types']; ?>" class="long" /></div>
436
- <span class="description"><?php _e( 'Comma separated list of post types that can be sold.', 'mycred' ); ?></span>
437
- </li>
438
- </ol>
439
 
440
- <?php if ( count( $this->point_types ) > 1 ) : ?>
 
441
 
442
- <label class="subheader" for="<?php echo $this->field_id( 'type' ); ?>"><?php _e( 'Point Type', 'mycred' ); ?></label>
443
- <ol id="myCRED-buy-point-type">
444
- <li>
445
 
446
- <?php mycred_types_select_from_dropdown( $this->field_name( 'type' ), $this->field_name( 'type' ), $sell_content['type'] ); ?>
447
 
448
- </li>
 
 
 
 
449
  </ol>
 
 
450
 
451
- <?php else : ?>
 
452
 
453
- <input type="hidden" name="<?php echo $this->field_name( 'type' ); ?>" id="<?php echo $this->field_name( 'type' ); ?>" value="mycred_default" />
 
 
454
 
455
- <?php endif; ?>
 
 
456
 
457
- <label class="subheader"><?php _e( 'Payments', 'mycred' ); ?></label>
458
- <ol id="myCRED-buy-payments">
 
 
 
 
 
459
  <?php
460
 
461
- if ( ! empty( $available_payees ) ) {
462
- foreach ( $available_payees as $key => $description ) {
 
 
 
 
 
 
 
 
463
 
464
  ?>
465
- <li>
466
- <input type="radio" name="<?php echo $this->field_name( 'pay' ); ?>" id="<?php echo $this->field_id( array( 'pay' => $key ) ); ?>" <?php checked( $sell_content['pay'], $key ); ?> value="<?php echo $key; ?>" />
467
- <label for="<?php echo $this->field_id( array( 'pay' => $key ) ); ?>"><?php echo $description; ?></label>
468
- </li>
 
 
 
469
  <?php
470
 
471
- if ( $key == 'author' ) {
 
472
 
473
  ?>
474
- <li>
475
- <label for="<?php echo $this->field_id( 'pay_percent' ); ?>"><?php _e( 'Percentage to pay Author', 'mycred' ); ?></label>
476
- <div class="h2"><input type="text" size="5" maxlength="3" name="<?php echo $this->field_name( 'pay_percent' ); ?>" id="<?php echo $this->field_id( 'pay_percent' ); ?>" value="<?php echo $sell_content['pay_percent']; ?>" /> %</div>
477
- <span class="description"><?php _e( 'Percentage of the price to pay the author. Can not be zero and is ignored if authors are not paid.', 'mycred' ); ?></span>
478
- </li>
479
  <?php
480
 
481
- }
 
 
 
 
 
 
 
 
 
 
482
 
483
  }
484
  }
485
 
486
  ?>
 
487
  </ol>
488
- <label class="subheader"><?php _e( 'Defaults', 'mycred' ); ?></label>
489
- <ol id="myCRED-buy-defaults">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
490
  <li>
491
- <label for="<?php echo $this->field_id( array( 'defaults' => 'price' ) ); ?>"><?php _e( 'Price', 'mycred' ); ?></label>
492
- <div class="h2"><?php echo $before; ?> <input type="text" name="<?php echo $this->field_name( array( 'defaults' => 'price' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'price' ) ); ?>" value="<?php echo $sell_content['defaults']['price']; ?>" size="8" /> <?php echo $after; ?></div>
493
  </li>
494
  <li>
495
- <input type="checkbox" name="<?php echo $this->field_name( array( 'defaults' => 'overwrite_price' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'overwrite_price' ) ); ?>" <?php checked( $sell_content['defaults']['overwrite_price'], 1 ); ?> value="1" />
496
- <label for="<?php echo $this->field_id( array( 'defaults' => 'overwrite_price' ) ); ?>"><?php _e( 'Allow authors to change price.', 'mycred' ); ?></label>
 
497
  </li>
498
- <li class="empty">&nbsp;</li>
 
 
499
  <li>
500
- <label for="<?php echo $this->field_id( array( 'defaults' => 'button_label' ) ); ?>"><?php _e( 'Button Label', 'mycred' ); ?></label>
501
- <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'defaults' => 'button_label' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'button_label' ) ); ?>" value="<?php echo $sell_content['defaults']['button_label']; ?>" size="12" /></div>
502
  </li>
503
  <li>
504
- <input type="checkbox" name="<?php echo $this->field_name( array( 'defaults' => 'overwrite_buttonlabel' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'overwrite_buttonlabel' ) ); ?>" <?php checked( $sell_content['defaults']['overwrite_buttonlabel'], 1 ); ?> value="1" />
505
- <label for="<?php echo $this->field_id( array( 'defaults' => 'overwrite_buttonlabel' ) ); ?>"><?php _e( 'Allow authors to change button label.', 'mycred' ); ?></label>
 
 
 
 
 
 
 
 
506
  </li>
507
  <li class="empty">&nbsp;</li>
508
  <li>
509
- <label for="<?php echo $this->field_id( array( 'defaults' => 'expire' ) ); ?>"><?php _e( 'Purchases expire after', 'mycred' ); ?></label>
510
- <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'defaults' => 'expire' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'expire' ) ); ?>" value="<?php echo $sell_content['defaults']['expire']; ?>" size="6" /> <?php echo $this->exp_title; ?></div>
511
- <span class="description"><?php _e( 'Use zero for permanent sales.', 'mycred' ); ?></span>
512
  </li>
513
- </ol>
514
- <label class="subheader" for="<?php echo $this->field_id( array( 'templates' => 'visitors' ) ); ?>"><?php _e( 'Templates', 'mycred' ); ?></label>
515
- <ol id="myCRED-buy-template-visitors">
516
  <li>
517
- <p><strong><?php _e( 'For Visitors', 'mycred' ); ?></strong></p>
518
  <?php
519
 
520
- $id = str_replace( array( '-', '_' ), '', $this->field_id( array( 'templates' => 'visitors' ) ) );
521
- wp_editor( $sell_content['templates']['visitors'], $id, array(
522
  'textarea_name' => $this->field_name( array( 'templates' => 'visitors' ) ),
523
- 'textarea_rows' => 6
524
  ) );
525
 
526
- ?>
527
- <span class="description"><?php _e( 'Do <strong>not</strong> use the %buy_button% in this template as a user must be logged in to buy content!', 'mycred' ); ?><br />
528
 
529
- <?php echo $this->core->available_template_tags( array( 'general', 'post' ), '%price%' ); ?></span>
530
 
531
  </li>
532
  <li class="empty">&nbsp;</li>
533
  <li>
534
- <p><strong><?php _e( 'For Members', 'mycred' ); ?></strong></p>
535
- <?php
536
-
537
- $id = str_replace( array( '-', '_' ), '', $this->field_id( array( 'templates' => 'members' ) ) );
538
- wp_editor( $sell_content['templates']['members'], $id, array(
539
- 'textarea_name' => $this->field_name( array( 'templates' => 'members' ) ),
540
- 'textarea_rows' => 6
541
- ) );
542
-
543
- ?>
544
- <span class="description"><?php _e( 'Your template must contain the %buy_button% tag for purchases to work!', 'mycred' ); ?><br />
545
- <?php echo $this->core->available_template_tags( array( 'general', 'post' ), '%buy_button%, %price%' ); ?></span>
546
  </li>
547
- <li class="empty">&nbsp;</li>
548
  <li>
549
- <p><strong><?php _e( 'For members that can not afford to buy', 'mycred' ); ?></strong></p>
550
  <?php
551
 
552
- $id = str_replace( array( '-', '_' ), '', $this->field_id( array( 'templates' => 'cantafford' ) ) );
553
- wp_editor( $sell_content['templates']['cantafford'], $id, array(
554
  'textarea_name' => $this->field_name( array( 'templates' => 'cantafford' ) ),
555
- 'textarea_rows' => 6
556
  ) );
557
 
 
 
558
  ?>
559
- <span class="description"><?php _e( 'Your template must contain the %buy_button% tag for purchases to work!', 'mycred' ); ?><br />
560
- <?php echo $this->core->available_template_tags( array( 'general', 'post' ), '%buy_button%, %price%' ); ?></span>
561
- </li>
562
- </ol>
563
- <label class="subheader" for="<?php echo $this->field_id( array( 'logs' => 'buy' ) ); ?>"><?php _e( 'Log template for Purchases', 'mycred' ); ?></label>
564
- <ol id="myCRED-buy-template-purchase">
565
- <li>
566
- <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'logs' => 'buy' ) ); ?>" id="<?php echo $this->field_id( array( 'logs' => 'buy' ) ); ?>" value="<?php echo $sell_content['logs']['buy']; ?>" class="long" /></div>
567
- <?php echo $this->core->available_template_tags( array( 'general', 'post' ) ); ?></span>
568
- </li>
569
- </ol>
570
- <label class="subheader" for="<?php echo $this->field_id( array( 'logs' => 'sell' ) ); ?>"><?php _e( 'Log template for Sales', 'mycred' ); ?></label>
571
- <ol id="myCRED-buy-template-sale">
572
- <li>
573
- <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'logs' => 'sell' ) ); ?>" id="<?php echo $this->field_id( array( 'logs' => 'sell' ) ); ?>" value="<?php echo $sell_content['logs']['sell']; ?>" class="long" /></div>
574
- <span class="description"><?php echo $this->core->available_template_tags( array( 'general', 'post' ) ); ?></span>
575
  </li>
576
  </ol>
577
  </div>
578
- <?php
579
-
580
- }
581
-
582
- /**
583
- * Sanitize & Save Settings
584
- * @since 0.1
585
- * @version 1.3
586
- */
587
- public function sanitize_extra_settings( $new_data, $data, $general ) {
588
-
589
- // Post Types
590
- $settings = $data['sell_content'];
591
-
592
- $new_data['sell_content']['post_types'] = sanitize_text_field( $settings['post_types'] );
593
- $new_data['sell_content']['type'] = sanitize_text_field( $settings['type'] );
594
- $new_data['sell_content']['pay'] = sanitize_text_field( $settings['pay'] );
595
- $new_data['sell_content']['pay_percent'] = abs( $settings['pay_percent'] );
596
-
597
- if ( $new_data['sell_content']['pay_percent'] == 0 || $new_data['sell_content']['pay_percent'] > 100 )
598
- $new_data['sell_content']['pay_percent'] = 100;
599
 
600
- $new_data['sell_content']['defaults']['price'] = $this->core->format_number( $settings['defaults']['price'] );
601
- $new_data['sell_content']['defaults']['overwrite_price'] = ( isset( $settings['defaults']['overwrite_price'] ) ) ? 1 : 0;
602
- $new_data['sell_content']['defaults']['button_label'] = sanitize_text_field( $settings['defaults']['button_label'] );
603
- $new_data['sell_content']['defaults']['overwrite_buttonlabel'] = ( isset( $settings['defaults']['overwrite_buttonlabel'] ) ) ? 1 : 0;
604
- $new_data['sell_content']['defaults']['expire'] = abs( $settings['defaults']['expire'] );
605
 
606
- $allowed_tags = $this->core->allowed_html_tags();
607
- $new_data['sell_content']['templates']['members'] = wp_kses( $settings['templates']['members'], $allowed_tags );
608
- $new_data['sell_content']['templates']['visitors'] = wp_kses( $settings['templates']['visitors'], $allowed_tags );
609
- $new_data['sell_content']['templates']['cantafford'] = wp_kses( $settings['templates']['cantafford'], $allowed_tags );
610
 
611
- $new_data['sell_content']['logs']['buy'] = sanitize_text_field( $settings['logs']['buy'] );
612
- $new_data['sell_content']['logs']['sell'] = sanitize_text_field( $settings['logs']['sell'] );
613
 
614
- return $new_data;
615
 
616
  }
 
 
 
 
 
 
617
 
618
- /**
619
- * Add Meta Box to Content
620
- * @since 0.1
621
- * @version 1.0.1
622
- */
623
- public function add_metabox() {
624
-
625
- $sell_content = $this->sell_content;
626
- $post_types = explode( ',', $sell_content['post_types'] );
627
- $name = sprintf( __( '%s Sell This', 'mycred' ), mycred_label() );
628
- $name = apply_filters( 'mycred_sell_this_label', $name, $this );
629
-
630
- foreach ( (array) $post_types as $post_type ) {
631
-
632
- $post_type = trim( $post_type );
633
- add_meta_box(
634
- 'mycred_sell_content',
635
- $name,
636
- array( $this, 'metabox' ),
637
- $post_type,
638
- 'side',
639
- 'high'
640
- );
641
-
642
- }
643
-
644
  }
645
 
646
- /**
647
- * Sale Preference
648
- * Returns a given posts sale preferences. If none exists a new one is buildt and returned.
649
- *
650
- * @return (array) sales settings
651
- * @since 0.1
652
- * @version 1.1
653
- */
654
- public function get_sale_prefs( $post_id ) {
655
-
656
- $sell_content = $this->sell_content;
657
- if ( ! isset( $sell_content['defaults']['expire'] ) )
658
- $sell_content['defaults']['expire'] = 0;
659
-
660
- $prefs = get_post_meta( $post_id, 'myCRED_sell_content', true );
661
- if ( $prefs == '' ) {
662
-
663
- $sales_data = array(
664
- 'status' => 'disabled',
665
- 'price' => $sell_content['defaults']['price'],
666
- 'button_label' => $sell_content['defaults']['button_label'],
667
- 'expire' => $sell_content['defaults']['expire']
668
- );
669
-
670
- }
671
- else {
672
-
673
- if ( ! isset( $prefs['expire'] ) )
674
- $prefs['expire'] = $sell_content['defaults']['expire'];
675
 
676
- $sales_data = $prefs;
677
 
678
- }
 
679
 
680
- return apply_filters( 'mycred_sell_content_post_prefs', $sales_data, $post_id, $this );
681
 
682
  }
 
 
 
 
 
 
683
 
684
- /**
685
- * Sell Meta Box
686
- * @since 0.1
687
- * @version 1.2
688
- */
689
- public function metabox( $post ) {
690
-
691
- // Make sure add-on has been setup
692
- if ( ! $this->is_installed() ) {
693
 
694
- echo sprintf( __( '%s Sell Content needs to be setup before you can use this feature.', 'mycred' ), mycred_label() );
695
 
696
- // Settings Link
697
- if ( $this->core->can_edit_plugin( get_current_user_id() ) )
698
- echo ' <a href="' . $this->get_settings_url( 'sell_content_module' ) . '" title="' . __( 'Setup add-on', 'mycred' ) . '">' . __( 'Lets do it', 'mycred' ) . '</a>';
699
 
700
- return;
 
 
701
 
702
- }
 
 
 
703
 
704
- $admin = false;
705
- $post_id = $post->ID;
706
- $post_type = $post->post_type;
707
- $user_id = get_current_user_id();
708
- $sell_content = $this->sell_content;
709
- $sales_data = $this->get_sale_prefs( $post_id );
710
-
711
- // Mark admins
712
- if ( $this->core->can_edit_plugin( $user_id ) )
713
- $admin = true;
714
-
715
- // Empty $sales_data means disabled same if the status is actually set to "disabled"
716
- if ( empty( $sales_data ) || ( isset( $sales_data['status'] ) && $sales_data['status'] == 'disabled' ) ) {
717
- $style = 'display:none;';
718
- $status = 'disabled';
719
- }
720
 
721
- else {
722
- $style = 'display:block;';
723
- $status = 'enabled';
724
- }
725
 
726
- $op = (bool) $sell_content['defaults']['overwrite_price'];
727
- $ob = (bool) $sell_content['defaults']['overwrite_buttonlabel'];
728
 
729
- ?>
730
- <style type="text/css">
731
- #mycred_sell_content .inside { margin: 0; padding: 0; }
732
- #mycred_sell_content .inside p { padding: 0 12px; }
733
- #mycred_sell_content .inside ul { margin: 0; padding: 0 0 12px 0; background-color: #F5F5F5; border-top: 1px solid #ccc; }
734
- #mycred_sell_content .inside ul li { padding: 8px 12px 0 12px; margin: 0 0 0 0; }
735
- #mycred_sell_content .inside ul li label { font-weight: bold; display: block; }
736
- #mycred_sell_content .inside ul li.disabled { color: #ccc; }
737
- input[name="myCRED_sell_content[button_label]"] { width: 100%; }
738
- </style>
739
- <p><label for="mycred-sell-this"><input type="checkbox" name="mycred_sell_this" id="mycred-sell-this"<?php checked( $status, 'enabled' ); ?> value="enabled" /> <?php printf( __( 'Enable sale of this %s', 'mycred' ), $post_type ); ?></label></p>
740
- <div id="mycred-sale-settings" style="<?php echo $style; ?>">
741
- <input type="hidden" name="mycred-sell-this-token" value="<?php echo wp_create_nonce( 'mycred-sell-this' ); ?>" />
742
- <input type="hidden" name="mycred-sell-this-status" value="<?php echo $status; ?>" />
743
- <ul>
744
- <li<?php if ( $op === false && ! $admin ) echo ' class="disabled"'; ?>>
745
- <label for="mycred-buy-prefs-"><?php _e( 'Price', 'mycred' ); ?></label>
746
- <div class="formated"><?php echo $this->core->before; ?> <input type="text" name="myCRED_sell_content[price]" id="mycred-buy-prefs-price" value="<?php echo $sales_data['price']; ?>" <?php if ( $op === false && ! $admin ) echo 'disabled="disabled" class="disabled"'; ?> size="12" /> <?php echo $this->core->after; ?></div>
747
- </li>
748
- <li<?php if ( $ob === false && ! $admin ) echo ' class="disabled"'; ?>>
749
- <label for="mycred-buy-prefs-"><?php _e( 'Button Label', 'mycred' ); ?></label>
750
- <input type="text" name="myCRED_sell_content[button_label]" id="mycred-buy-prefs-button" value="<?php echo $sales_data['button_label']; ?>" <?php if ( $ob === false && ! $admin ) echo 'disabled="disabled" class="disabled"'; ?> />
751
- </li>
752
- <li<?php if ( $op === false && ! $admin ) echo ' class="disabled"'; ?>>
753
- <label for="mycred-buy-prefs-"><?php _e( 'Purchase expires after', 'mycred' ); ?></label>
754
- <div class="formated"><input type="text" name="myCRED_sell_content[expire]" id="mycred-buy-prefs-expire" value="<?php echo $sales_data['expire']; ?>" <?php if ( $op === false && ! $admin ) echo 'disabled="disabled" class="disabled"'; ?> size="12" /> <?php echo $this->exp_title; ?></div>
755
- </li>
756
- </ul>
757
- </div>
758
- <div class="clear"></div>
759
- <script type="text/javascript">
760
- jQuery(function($) {
761
- $( '#mycred-sell-this' ).click(function(){
762
- $( '#mycred-sale-settings' ).toggle();
763
  });
764
- });
 
765
  </script>
766
  <?php
767
 
768
  }
769
 
770
  /**
771
- * Save Sell Meta Box
772
  * @since 0.1
773
- * @version 1.0
774
  */
775
- public function save_metabox( $post_id ) {
776
-
777
- // Make sure sale is enabled
778
- if ( ! isset( $_POST['mycred-sell-this-status'] ) || ! isset( $_POST['mycred-sell-this-token'] ) ) return;
779
-
780
- // Verify token
781
- if ( wp_verify_nonce( $_POST['mycred-sell-this-token'], 'mycred-sell-this' ) === false ) return;
782
-
783
- // Status
784
- if ( ! isset( $_POST['mycred_sell_this'] ) )
785
- $status = 'disabled';
786
- else
787
- $status = 'enabled';
788
-
789
- $prefs = get_post_meta( $post_id, 'myCRED_sell_content', true );
790
- // If sale has never been set and is not enabled bail
791
- if ( empty( $prefs ) && $status == 'disabled' ) return;
792
-
793
- $sell_content = $this->sell_content;
794
- $is_admin = $this->core->can_edit_plugin();
795
-
796
- // Status
797
- $prefs['status'] = $status;
798
-
799
- // Prefs
800
- $op = (bool) $sell_content['defaults']['overwrite_price'];
801
- $prefs['price'] = ( $op === true || $is_admin === true ) ? $_POST['myCRED_sell_content']['price'] : $sell_content['defaults']['price'];
802
-
803
- $ob = (bool) $sell_content['defaults']['overwrite_buttonlabel'];
804
- $prefs['button_label'] = ( $ob === true || $is_admin === true ) ? $_POST['myCRED_sell_content']['button_label'] : $sell_content['defaults']['button_label'];
805
 
806
- // Expiration
807
- $prefs['expire'] = ( $is_admin === true ) ? abs( $_POST['myCRED_sell_content']['expire'] ) : $sell_content['defaults']['expire'];
808
 
809
- update_post_meta( $post_id, 'myCRED_sell_content', $prefs );
 
 
810
 
811
- }
 
 
812
 
813
- /**
814
- * Get the Post ID
815
- * Added support for sale of bbPress items.
816
- * @since 1.3.3.2
817
- * @version 1.0
818
- */
819
- public function get_post_ID() {
820
 
821
- $post_id = $bbp_topic_id = $bbp_reply_id = 0;
 
 
822
 
823
- if ( function_exists( 'bbpress' ) ) {
824
 
825
- global $wp_query;
826
 
827
- $bbp = bbpress();
828
 
829
- // Currently inside a topic loop
830
- if ( ! empty( $bbp->topic_query->in_the_loop ) && isset( $bbp->topic_query->post->ID ) )
831
- $bbp_topic_id = $bbp->topic_query->post->ID;
832
 
833
- // Currently inside a search loop
834
- elseif ( ! empty( $bbp->search_query->in_the_loop ) && isset( $bbp->search_query->post->ID ) && bbp_is_topic( $bbp->search_query->post->ID ) )
835
- $bbp_topic_id = $bbp->search_query->post->ID;
836
 
837
- // Currently viewing/editing a topic, likely alone
838
- elseif ( ( bbp_is_single_topic() || bbp_is_topic_edit() ) && ! empty( $bbp->current_topic_id ) )
839
- $bbp_topic_id = $bbp->current_topic_id;
 
 
 
 
 
 
 
 
840
 
841
- // Currently viewing/editing a topic, likely in a loop
842
- elseif ( ( bbp_is_single_topic() || bbp_is_topic_edit() ) && isset( $wp_query->post->ID ) )
843
- $bbp_topic_id = $wp_query->post->ID;
844
-
845
- // So far, no topic found, check if we are in a reply
846
- if ( $bbp_topic_id == 0 ) {
847
 
848
- // Currently inside a replies loop
849
- if ( ! empty( $bbp->reply_query->in_the_loop ) && isset( $bbp->reply_query->post->ID ) )
850
- $bbp_reply_id = $bbp->reply_query->post->ID;
851
 
852
- // Currently inside a search loop
853
- elseif ( ! empty( $bbp->search_query->in_the_loop ) && isset( $bbp->search_query->post->ID ) && bbp_is_reply( $bbp->search_query->post->ID ) )
854
- $bbp_reply_id = $bbp->search_query->post->ID;
855
 
856
- // Currently viewing a forum
857
- elseif ( ( bbp_is_single_reply() || bbp_is_reply_edit() ) && ! empty( $bbp->current_reply_id ) )
858
- $bbp_reply_id = $bbp->current_reply_id;
859
 
860
- // Currently viewing a reply
861
- elseif ( ( bbp_is_single_reply() || bbp_is_reply_edit() ) && isset( $wp_query->post->ID ) )
862
- $bbp_reply_id = $wp_query->post->ID;
863
-
864
- if ( $bbp_reply_id != 0 )
865
- $post_id = $bbp_reply_id;
866
 
867
  }
868
-
869
- // Else we are in a topic
870
- else $post_id = $bbp_topic_id;
871
 
872
  }
 
873
 
874
- if ( $post_id == 0 && isset( $GLOBALS['post'] ) )
875
- $post_id = $GLOBALS['post']->ID;
876
-
877
- return apply_filters( 'mycred_sell_this_get_post_ID', $post_id, $this );
878
-
879
- }
880
-
881
- /**
882
- * For Sale
883
- * Checks if a given post is for sale.
884
- * @param $post_id (int) required post id
885
- * @returns (bool) true or false
886
- * @since 0.1
887
- * @version 1.1
888
- */
889
- public function for_sale( $post_id ) {
890
-
891
- $prefs = get_post_meta( $post_id, 'myCRED_sell_content', true );
892
- $reply = false;
893
 
894
- if ( ! empty( $prefs ) && isset( $prefs['status'] ) && $prefs['status'] == 'enabled' )
895
- $reply = true;
896
-
897
- return apply_filters( 'mycred_is_content_for_sale', $reply, $post_id );
898
-
899
- }
900
-
901
- /**
902
- * User Paid
903
- * Checks if a given user has paid for a specific post.
904
- * Will return true if the user can edit this plugin or creds.
905
- * @param $user_id (int) required user id
906
- * @param $post_id (int) required post id
907
- * @returns (bool) true or false
908
- * @since 0.1
909
- * @version 1.3.2
910
- */
911
- public function user_paid( $user_id, $post_id ) {
912
-
913
- // Admins can view
914
- if ( $this->core->can_edit_plugin( $user_id ) || $this->core->can_edit_creds( $user_id ) ) return true;
915
-
916
- // Authors can view
917
- $the_post = get_post( $post_id );
918
- if ( ! isset( $the_post->post_author ) || $the_post->post_author == $user_id ) return true;
919
-
920
- global $wpdb;
921
-
922
- $sell_content = $this->sell_content;
923
 
924
- $purchases = $wpdb->get_results( $wpdb->prepare( "
925
- SELECT *
926
- FROM {$this->core->log_table}
927
- WHERE user_id = %d
928
- AND ref = %s
929
- AND ref_id = %d
930
- AND ctype = %s
931
- ORDER BY time DESC LIMIT 0,1;", $user_id, 'buy_content', $post_id, $this->mycred_type ) );
932
 
933
- // We have found purchase records
934
- if ( ! empty( $purchases ) ) {
935
 
936
- // Since individual posts can override the default settings we need to check sales prefs
937
- $prefs = $this->get_sale_prefs( $post_id );
938
- if ( ! isset( $prefs['expire'] ) )
939
- $prefs['expire'] = ( isset( $sell_content['defaults']['expire'] ) ) ? $sell_content['defaults']['expire'] : 0;
940
 
941
- // If purchases never expire just return true here and now
942
- if ( $prefs['expire'] == 0 ) return true;
 
 
 
 
 
 
 
 
943
 
944
- // Check if purchase has expired
945
- if ( ! $this->purchase_has_expired( $purchases[0]->time, $prefs['expire'], $user_id, $post_id ) ) return true;
946
 
 
947
  }
948
 
949
- // All else there are no purchases
950
- return apply_filters( 'mycred_user_has_paid_for_content', false, $user_id, $post_id );
951
-
952
- }
953
-
954
- /**
955
- * Purchase Has Expired
956
- * Makes a time comparison to check if a given timestamp is in the future (not expired) or
957
- * in the past (expired).
958
- * @param $timestamp (int) The UNIX timestamp to wich we apply the expiration check
959
- * @param $length (int) Length of expiration time to check by default this is the number of hours.
960
- * @filter 'mycred_sell_expire_calc'
961
- * @returns (bool) true or false
962
- * @since 1.1
963
- * @version 1.0
964
- */
965
- public function purchase_has_expired( $timestamp, $length = 0, $user_id, $post_id ) {
966
-
967
- if ( $length == 0 ) return false;
968
 
969
- $expiration = apply_filters( 'mycred_sell_expire_calc', abs( $length*3600 ), $length, $user_id, $post_id );
970
- $expiration = $expiration+$timestamp;
 
 
971
 
972
- if ( $expiration > date_i18n( 'U' ) ) return false;
973
 
974
- return true;
975
 
976
  }
977
 
978
  /**
979
- * User Can Buy
980
- * Checks if a given user can afford the given price.
981
- * @param $user_id (int) required user id
982
- * @param $price (int|float) required price to check
983
- * @returns (bool) true or false
984
- * @since 0.1
985
  * @version 1.0
986
  */
987
- public function user_can_buy( $user_id, $price ) {
988
-
989
- $balance = $this->core->get_users_cred( $user_id, $this->mycred_type );
990
 
991
- if ( $balance-$price < 0 ) return false;
992
 
993
- return true;
 
 
 
994
 
995
  }
996
 
997
  /**
998
- * Get Button
999
- * Replaces the %buy_button% template tag with the submit button along
1000
- * with the set button label. If no template tag is found one is inserted in the end of the given string.
1001
- * @param $text (string) text to check for template tag.
1002
- * @param $post (object) optional post object to allow post template tags.
1003
- * @returns (string) formated string.
1004
  * @since 0.1
1005
  * @version 1.1
1006
  */
1007
- public function get_button( $text, $post ) {
1008
 
1009
- $sell_content = $this->sell_content;
1010
- $prefs = $this->get_sale_prefs( $post->ID );
1011
-
1012
- // Button Label
1013
- if ( isset( $prefs['button_label'] ) )
1014
- $button_text = $prefs['button_label'];
1015
- else
1016
- $button_text = $sell_content['defaults']['button_label'];
1017
 
1018
- // Button element
1019
- $button = '<input type="submit" name="mycred-buy-button" value="' . $this->core->template_tags_post( $button_text, $post ) . '" class="button large" />';
1020
 
1021
- // Make sure there is a button
1022
- if ( ! preg_match( '/%buy_button%/', $text ) )
1023
- $text .= ' %buy_button% ';
1024
-
1025
- $content = str_replace( '%buy_button%', $button, $text );
 
 
 
1026
 
1027
- return apply_filters( 'mycred_sell_this_button', $content, $post );
1028
 
1029
  }
1030
 
1031
  /**
1032
- * The Content Overwrite
1033
- * If the current post is set for sale we apply the appropirate template.
1034
- * Uses 3 different templates. a) Visitors Template b) Members Template and c) Cant Afford Template
1035
- * @returns (string) content
1036
  * @since 0.1
1037
- * @version 1.1.1
1038
  */
1039
- public function the_content( $content ) {
1040
-
1041
- global $mycred_content_purchase;
1042
-
1043
- $post_id = $this->get_post_ID();
1044
- $the_post = get_post( $post_id );
1045
-
1046
- // If content is for sale
1047
- if ( $this->for_sale( $post_id ) ) {
1048
 
1049
- // Prep
1050
- $user_id = get_current_user_id();
1051
- $sell_content = $this->sell_content;
1052
- $prefs = $this->get_sale_prefs( $post_id );
1053
 
1054
- // Visitors
1055
- if ( ! is_user_logged_in() ) {
 
 
 
 
 
 
 
 
 
 
 
1056
 
1057
- $template = $sell_content['templates']['visitors'];
1058
- $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
1059
- $template = $this->core->template_tags_general( $template );
1060
- $template = $this->core->template_tags_post( $template, $the_post );
1061
 
1062
- return '<div class="mycred-content-forsale">' . $template . '</div>';
1063
 
1064
- }
1065
 
1066
- // We are logged in, have not purchased this item and can make a purchase
1067
- elseif ( is_user_logged_in() && ! $this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
1068
-
1069
- $template = $sell_content['templates']['members'];
1070
- $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
1071
- $template = $this->core->template_tags_general( $template );
1072
- $template = $this->core->template_tags_post( $template, $the_post );
1073
- $template = $this->get_button( $template, $the_post );
1074
-
1075
- return '
1076
- <form action="" method="post" class="mycred-buy-content-form">
1077
- <input type="hidden" name="mycred_purchase[mst_post_id]" value="' . $post_id . '" />
1078
- <input type="hidden" name="mycred_purchase[mst_post_type]" value="' . $the_post->post_type . '" />
1079
- <input type="hidden" name="mycred_purchase[mst_user_id]" value="' . get_current_user_id() . '" />
1080
- <input type="hidden" name="mycred_purchase[mst_author]" value="' . $the_post->post_author . '" />
1081
- <input type="hidden" name="mycred_purchase_token" value="' . wp_create_nonce( 'buy-content' ) . '" />
1082
- <input type="hidden" name="mycred_purchase[mst_action]" value="buy" />
1083
- <div class="mycred-content-forsale">' . $template . '</div>
1084
- </form>';
1085
 
1086
- }
 
 
1087
 
1088
- // We are logged in, have not purchased this item and can not afford to buy this
1089
- elseif ( is_user_logged_in() && ! $this->user_paid( $user_id, $post_id ) && ! $this->user_can_buy( $user_id, $prefs['price'] ) ) {
 
 
 
 
1090
 
1091
- $template = $sell_content['templates']['cantafford'];
1092
- $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
1093
- $template = $this->core->template_tags_general( $template );
1094
- $template = $this->core->template_tags_post( $template, $the_post );
1095
 
1096
- return '<div class="mycred-content-forsale">' . $template . '</div>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1097
 
1098
  }
1099
-
1100
  }
1101
 
1102
- // Mark purchases
1103
- if ( $mycred_content_purchase === true ) {
 
 
 
1104
 
1105
- $thank_you = __( 'Thank you for your purchase!', 'mycred' );
1106
- $wrapper = '<div id="mycred-thank-you"><p>' . $thank_you . '</p></div>';
1107
- $content = $wrapper . $content;
 
 
 
1108
 
1109
- }
1110
 
1111
- return do_shortcode( $content );
 
 
 
1112
 
1113
  }
1114
 
1115
  /**
1116
- * Render Shortcode
1117
- * Just as protecting the entire content, the mycred_sell_this shortcode protects
1118
- * parts of the content.
1119
- * @returns (string) content
1120
  * @since 0.1
1121
- * @version 1.3.1
1122
  */
1123
- public function render_shortcode( $atts, $content ) {
1124
-
1125
- // The Post
1126
- $post_id = $this->get_post_ID();
1127
- $the_post = get_post( $post_id );
1128
-
1129
- // The User
1130
- $user_id = get_current_user_id();
1131
- $sell_content = $this->sell_content;
1132
-
1133
- $prefs = shortcode_atts( array(
1134
- 'price' => $sell_content['defaults']['price'],
1135
- 'button_label' => $sell_content['defaults']['button_label'],
1136
- 'expire' => $sell_content['defaults']['expire']
1137
- ), $atts );
1138
 
1139
- $sales_prefs = $this->get_sale_prefs( $post_id );
 
1140
 
1141
- // If we are not using defaults save these settings.
1142
- if ( ( $sales_prefs['price'] != $prefs['price'] ) || ( $sales_prefs['button_label'] != $prefs['button_label'] ) || ( $sales_prefs['expire'] != $prefs['expire'] ) ) {
1143
 
1144
- update_post_meta( $post_id, 'myCRED_sell_content', array(
1145
- 'price' => $prefs['price'],
1146
- 'status' => $sales_prefs['status'],
1147
- 'button_label' => $prefs['button_label'],
1148
- 'expire' => $prefs['expire']
1149
- ) );
1150
 
1151
- }
 
1152
 
1153
- // Not logged in
1154
- if ( ! is_user_logged_in() ) {
1155
 
1156
- $template = $sell_content['templates']['visitors'];
1157
- $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
1158
- $template = $this->core->template_tags_general( $template );
1159
- $template = $this->core->template_tags_post( $template, $the_post );
1160
 
1161
- return '<div class="mycred-content-forsale">' . $template . '</div>';
 
 
 
 
 
1162
 
1163
- }
 
 
1164
 
1165
- // Can buy
1166
- elseif ( is_user_logged_in() && ! $this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
1167
-
1168
- $template = $sell_content['templates']['members'];
1169
- $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
1170
- $template = $this->core->template_tags_general( $template );
1171
- $template = $this->core->template_tags_post( $template, $the_post );
1172
- $template = $this->get_button( $template, $the_post );
1173
-
1174
- return '
1175
- <form action="" method="post" class="mycred-buy-content-form">
1176
- <input type="hidden" name="mycred_purchase[mst_post_id]" value="' . $post_id . '" />
1177
- <input type="hidden" name="mycred_purchase[mst_post_type]" value="' . $the_post->post_type . '" />
1178
- <input type="hidden" name="mycred_purchase[mst_user_id]" value="' . get_current_user_id() . '" />
1179
- <input type="hidden" name="mycred_purchase[mst_author]" value="' . $the_post->post_author . '" />
1180
- <input type="hidden" name="mycred_purchase_token" value="' . wp_create_nonce( 'buy-content' ) . '" />
1181
- <input type="hidden" name="mycred_purchase[mst_action]" value="buy" />
1182
- <div class="mycred-content-forsale">' . $template . '</div>
1183
- </form>';
1184
 
1185
- }
 
 
1186
 
1187
- // We are logged in, have not purchased this item and can not afford to buy this
1188
- elseif ( is_user_logged_in() && ! $this->user_paid( $user_id, $post_id ) && ! $this->user_can_buy( $user_id, $prefs['price'] ) ) {
 
1189
 
1190
- $template = $sell_content['templates']['cantafford'];
1191
- $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
1192
- $template = $this->core->template_tags_general( $template );
1193
- $template = $this->core->template_tags_post( $template, $the_post );
1194
 
1195
- return '<div class="mycred-content-forsale">' . $template . '</div>';
 
1196
 
1197
  }
1198
 
1199
- // Admin and Author Wrapper for highlight of content set for sale
1200
- if ( mycred_is_admin() || $the_post->post_author == $user_id )
1201
- $content = '<div class="mycred-mark-title">' . __( 'The following content is set for sale:', 'mycred' ) . '</div><div class="mycred-mark-content">' . $content . '</div>';
1202
-
1203
- return do_shortcode( $content );
1204
-
1205
  }
1206
 
1207
  /**
1208
- * Render Shortcode AJAX
1209
- * Just as protecting the entire content, the mycred_sell_this_ajax shortcode protects
1210
- * parts of the content and uses AJAX to make the purchase
1211
- * @returns (string) content
1212
  * @since 0.1
1213
- * @version 1.0.1
1214
  */
1215
- public function render_ajax_shortcode( $atts, $content ) {
1216
 
1217
- global $mycred_buy_content;
1218
 
1219
- // The Post
1220
- $post_id = $this->get_post_ID();
1221
- $the_post = get_post( $post_id );
1222
- $user_id = get_current_user_id();
1223
- $sell_content = $this->sell_content;
1224
 
1225
- $prefs = shortcode_atts( array(
1226
- 'price' => $sell_content['defaults']['price'],
1227
- 'button_label' => $sell_content['defaults']['button_label'],
1228
- 'expire' => $sell_content['defaults']['expire']
1229
- ), $atts );
1230
 
1231
- $sales_prefs = $this->get_sale_prefs( $post_id );
 
1232
 
1233
- // If we are not using defaults save these settings.
1234
- if ( ( $sales_prefs['price'] != $prefs['price'] ) || ( $sales_prefs['button_label'] != $prefs['button_label'] ) || ( $sales_prefs['expire'] != $prefs['expire'] ) ) {
1235
 
1236
- update_post_meta( $post_id, 'myCRED_sell_content', array(
1237
- 'price' => $prefs['price'],
1238
- 'status' => $sales_prefs['status'],
1239
- 'button_label' => $prefs['button_label'],
1240
- 'expire' => $prefs['expire']
1241
- ) );
1242
 
1243
- }
 
 
1244
 
1245
- // Not logged in
1246
- if ( ! is_user_logged_in() ) {
1247
 
1248
- $template = $sell_content['templates']['visitors'];
1249
- $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
1250
- $template = $this->core->template_tags_general( $template );
1251
- $template = $this->core->template_tags_post( $template, $the_post );
1252
 
1253
- return '<div class="mycred-content-forsale">' . $template . '</div>';
 
1254
 
1255
- }
 
1256
 
1257
- // Can buy
1258
- elseif ( is_user_logged_in() && ! $this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
1259
 
1260
- $template = $sell_content['templates']['members'];
1261
- $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
1262
- $template = $this->core->template_tags_general( $template );
1263
- $template = $this->core->template_tags_post( $template, $the_post );
1264
 
1265
- if ( isset( $prefs['button_label'] ) )
1266
- $button_text = $prefs['button_label'];
1267
- else
1268
- $button_text = $sell_content['defaults']['button_label'];
1269
 
1270
- $button = '<input type="button" data-id="' . $the_post->ID . '" name="mycred-buy-button" value="' . $this->core->template_tags_post( $button_text, $the_post ) . '" class="mycred-sell-this-button button large" />';
1271
- $template = str_replace( '%buy_button%', $button, $template );
1272
 
1273
- $mycred_buy_content = true;
 
1274
 
1275
- return '<div class="mycred-content-forsale">' . $template . '</div>';
1276
 
1277
- }
1278
 
1279
- // We are logged in, have not purchased this item and can not afford to buy this
1280
- elseif ( is_user_logged_in() && ! $this->user_paid( $user_id, $post_id ) && ! $this->user_can_buy( $user_id, $prefs['price'] ) ) {
1281
 
1282
- $template = $sell_content['templates']['cantafford'];
1283
- $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
1284
- $template = $this->core->template_tags_general( $template );
1285
- $template = $this->core->template_tags_post( $template, $the_post );
1286
 
1287
- return '<div class="mycred-content-forsale">' . $template . '</div>';
 
1288
 
1289
- }
 
1290
 
1291
- // Admin and Author Wrapper for highlight of content set for sale
1292
- if ( mycred_is_admin() || $the_post->post_author == $user_id )
1293
- $content = '<div class="mycred-mark-title">' . __( 'The following content is set for sale:', 'mycred' ) . '</div><div class="mycred-mark-content">' . $content . '</div>';
1294
 
1295
- return do_shortcode( $content );
 
 
1296
 
1297
  }
1298
 
1299
  /**
1300
- * Render Sales History Shortcode
1301
- * @see http://mycred.me/shortcodes/mycred_sales_history/
1302
- * @since 1.0.9
1303
- * @version 1.3
1304
  */
1305
- public function render_sales_history( $atts ) {
1306
-
1307
- extract( shortcode_atts( array(
1308
- 'login' => NULL,
1309
- 'user_id' => NULL,
1310
- 'title' => '',
1311
- 'title_el' => 'h1',
1312
- 'title_class' => '',
1313
- 'include_date' => true,
1314
- 'no_result' => __( 'No purchases found', 'mycred' )
1315
- ), $atts ) );
1316
-
1317
- // Not logged in
1318
- if ( $user_id === NULL && ! is_user_logged_in() ) {
1319
 
1320
- if ( $login != NULL )
1321
- return '<div class="mycred-not-logged-in">' . $login . '</div>';
1322
-
1323
- return;
1324
-
1325
- }
1326
 
1327
  // Prep
1328
- $output = '<div class="mycred-sales-history-wrapper">';
 
1329
 
1330
- if ( $user_id === NULL )
1331
- $user_id = get_current_user_id();
1332
-
1333
- global $wpdb;
1334
-
1335
- // Title
1336
- if ( ! empty( $title ) ) {
1337
 
1338
- if ( ! empty( $title_class ) )
1339
- $title_class = ' class="' . $title_class . '"';
1340
 
1341
- $output .= '<' . $title_el . $title_class . '>' . $title . '</' . $title_el . '>';
 
1342
 
1343
- }
 
1344
 
1345
- // Query
1346
- $results = $wpdb->get_results( $wpdb->prepare( "
1347
- SELECT *
1348
- FROM {$this->core->log_table}
1349
- WHERE user_id = %d
1350
- AND ref = %s
1351
- AND ctype = %s
1352
- ORDER BY time;", $user_id, 'buy_content', $this->mycred_type ) );
1353
 
1354
- // Results
1355
- $rows = array();
1356
- if ( ! empty( $results ) ) {
1357
- foreach ( $results as $item ) {
1358
 
1359
- $row = '<span class="item-link"><a href="' . get_permalink( $item->ref_id ) . '">' . get_the_title( $item->ref_id ) . '</a></span>';
 
1360
 
1361
- // Add Date to row
1362
- if ( $include_date )
1363
- $row .= '<span class="purchased">' . __( 'Purchased', 'mycred' ) . ' ' . date_i18n( get_option( 'date_format' ), $item->time ) . '</span>';
1364
 
1365
- // Construct row (and let others play)
1366
- $rows[] = apply_filters( 'mycred_sale_history_row', $row, $item );
 
1367
 
1368
- }
1369
 
1370
- }
 
 
 
 
1371
 
1372
- // Implode rows if there are any
1373
- if ( ! empty( $rows ) )
1374
- $output .= '<ul class="mycred-purchase-history"><li>' . implode( '</li><li>', $rows ) . '</li></ul>';
1375
 
1376
- // No results
1377
  else {
1378
 
1379
- if ( ! empty( $no_result ) )
1380
- $output .= '<p>' . $no_result . '</p>';
1381
 
1382
  }
1383
 
1384
- $output .= '</div>';
 
1385
 
1386
- return $output;
1387
-
1388
- }
1389
-
1390
- /**
1391
- * Shortcode: Number of Sales
1392
- * @since 1.4.7
1393
- * @version 1.0
1394
- */
1395
- function render_no_of_sales( $atts ) {
1396
 
1397
- extract( shortcode_atts( array(
1398
- 'post_id' => NULL
1399
- ), $atts ) );
1400
-
1401
- if ( $post_id === NULL )
1402
- $post_id = $this->get_post_ID();
1403
-
1404
- global $wpdb;
1405
-
1406
- $total = $wpdb->get_var( "
1407
- SELECT COUNT( * )
1408
- FROM {$this->core->log_table}
1409
- WHERE ref = 'buy_content'
1410
- AND creds < 0
1411
- AND ref_id = {$post_id};" );
1412
-
1413
- if ( $total === NULL )
1414
- $total = 0;
1415
-
1416
- return apply_filters( 'mycred_content_sales_count', $total, $post_id );
1417
 
1418
  }
1419
 
@@ -1443,7 +1092,7 @@ jQuery(function($) {
1443
  public function email_notices( $data ) {
1444
 
1445
  if ( $data['request']['ref'] == 'buy_content' ) {
1446
- $message = $data['message'];
1447
  $data['message'] = $this->core->template_tags_post( $message, $data['request']['ref_id'] );
1448
  }
1449
 
@@ -1453,9 +1102,23 @@ jQuery(function($) {
1453
 
1454
  }
1455
 
1456
- $sell_content = new myCRED_Sell_Content_Module();
1457
- $sell_content->load();
 
 
 
 
 
 
 
 
 
 
 
 
1458
 
 
1459
  endif;
 
1460
 
1461
  ?>
2
  /**
3
  * Addon: Sell Content
4
  * Addon URI: http://mycred.me/add-ons/sell-content/
5
+ * Version: 2.0
 
 
 
6
  */
7
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
 
9
+ define( 'myCRED_SELL', __FILE__ );
10
+ define( 'myCRED_SELL_VERSION', '1.5' );
11
+ define( 'MYCRED_SELL_DIR', myCRED_ADDONS_DIR . 'sell-content/' );
12
+ define( 'MYCRED_SELL_ASSETS_DIR', MYCRED_SELL_DIR . 'assets/' );
13
+ define( 'MYCRED_SELL_INCLUDES_DIR', MYCRED_SELL_DIR . 'includes/' );
14
+
15
+ require_once MYCRED_SELL_INCLUDES_DIR . 'mycred-sell-functions.php';
16
+ require_once MYCRED_SELL_INCLUDES_DIR . 'mycred-sell-shortcodes.php';
17
 
18
  /**
19
  * myCRED_Sell_Content_Module class
20
  * @since 0.1
21
+ * @version 2.0
22
  */
23
  if ( ! class_exists( 'myCRED_Sell_Content_Module' ) ) :
24
  class myCRED_Sell_Content_Module extends myCRED_Module {
25
 
26
+ public $current_user_id = 0;
27
+
28
  /**
29
  * Construct
30
  */
35
  'register' => false,
36
  'defaults' => array(
37
  'post_types' => 'post,page',
38
+ 'filters' => array(),
39
+ 'type' => array( MYCRED_DEFAULT_TYPE_KEY ),
40
+ 'reload' => 0,
41
+ 'working' => 'Processing ...',
42
  'templates' => array(
43
+ 'members' => '<div class="text-center"><h3>Premium Content</h3><p>Buy access to this content.</p><p>%buy_button%</p></div>',
44
+ 'visitors' => '<div class="text-center"><h3>Premium Content</h3><p>Buy access to this content.</p><p><strong>Insufficient Funds</strong></p></div>',
45
+ 'cantafford' => '<div class="text-center"><h3>Premium Content</h3><p>Login to buy access to this content.</p></div>'
 
 
 
 
 
 
 
 
 
 
 
46
  )
47
  ),
48
  'add_to_core' => true
49
  ) );
50
 
51
+ if ( ! is_array( $this->sell_content['type'] ) )
52
+ $this->sell_content['type'] = array( $this->sell_content['type'] );
 
 
 
 
 
 
 
53
 
54
  }
55
 
56
  /**
57
+ * Module Init
58
  * @since 0.1
59
  * @version 1.2
60
  */
61
  public function module_init() {
62
 
63
+ $this->current_user_id = get_current_user_id();
 
64
 
65
+ // Email add-on support
66
+ add_filter( 'mycred_get_email_events', array( $this, 'email_notice_instance' ), 10, 2 );
67
+ add_filter( 'mycred_email_before_send', array( $this, 'email_notices' ), 40, 2 );
68
 
69
+ // Setup Content Override
70
+ add_action( 'template_redirect', array( $this, 'template_redirect' ), 99990 );
 
 
71
 
72
+ // Register shortcodes
73
+ add_shortcode( 'mycred_sell_this', 'mycred_render_sell_this' );
74
+ add_shortcode( 'mycred_sell_this_ajax', 'mycred_render_sell_this_ajax' );
75
+ add_shortcode( 'mycred_sales_history', 'mycred_render_sell_history' );
76
+ add_shortcode( 'mycred_content_sale_count', 'mycred_render_sell_count' );
77
+ add_shortcode( 'mycred_content_buyer_count', 'mycred_render_sell_buyer_count' );
78
+ add_shortcode( 'mycred_content_buyer_avatars', 'mycred_render_sell_buyer_avatars' );
79
 
80
+ // Setup AJAX handler
81
+ add_action( 'mycred_register_assets', array( $this, 'register_assets' ) );
82
+ add_action( 'mycred_front_enqueue_footer', array( $this, 'enqueue_footer' ) );
83
+ add_action( 'wp_ajax_mycred-buy-content', array( $this, 'action_buy_content' ) );
 
 
 
 
84
 
85
  }
86
 
87
  /**
88
+ * Module Admin Init
89
+ * @since 1.7
90
  * @version 1.0
91
  */
92
+ public function module_admin_init() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
+ add_action( 'admin_notices', array( $this, 'one_seven_update_notice' ), 90 );
95
 
96
+ // Setup the "Sell This" Metaboxes
97
+ $post_types = explode( ',', $this->sell_content['post_types'] );
98
+ if ( ! empty( $post_types ) ) {
99
 
100
+ foreach ( $post_types as $type ) {
101
+ add_action( "add_meta_boxes_{$type}", array( $this, 'add_metabox' ) );
102
+ add_action( "save_post_{$type}", array( $this, 'save_metabox' ) );
103
+ }
 
 
104
 
105
+ }
106
 
107
+ // User Override
108
+ add_action( 'mycred_user_edit_after_balances', array( $this, 'sell_content_user_screen' ), 50 );
109
 
110
+ add_action( 'personal_options_update', array( $this, 'save_manual_profit_share' ), 50 );
111
+ add_action( 'edit_user_profile_update', array( $this, 'save_manual_profit_share' ), 50 );
112
 
113
+ }
114
 
115
+ /**
116
+ * 1.7 Update Notice
117
+ * @since 1.7
118
+ * @version 1.0
119
+ */
120
+ public function one_seven_update_notice() {
121
 
122
+ if ( ! current_user_can( $this->core->edit_plugin_cap() ) || get_option( 'mycred_sell_content_one_seven_updated', false ) !== false ) return;
123
 
124
+ if ( isset( $_GET['page'] ) && $_GET['page'] === MYCRED_SLUG . '-settings' ) return;
125
 
126
+ echo '
127
+ <div id="message" class="notice notice-info">
128
+ <h2>' . __( 'Sell Content Add-on Update Required', 'mycred' ) . '</h2>
129
+ <p>' . __( 'Before continuing to use this add-on you must setup and save your settings.', 'mycred' ) . '</p>
130
+ <p><a href="' . esc_url( add_query_arg( array( 'page' => MYCRED_SLUG . '-settings', 'open-tab' => 'sell_content_module' ), admin_url( 'admin.php' ) ) ) . '" class="button button-secondary">Go to Settings</a> <a href="' . get_mycred_addon_deactivation_url( 'sell-content' ) . '" class="button button-secondary">Disable Add-on</a></p>
131
+ </div>';
132
 
133
  }
134
 
135
  /**
136
+ * Register Assets
137
+ * @since 1.7
138
  * @version 1.0
139
  */
140
+ public function register_assets() {
141
 
142
+ wp_register_script(
143
+ 'mycred-sell-this',
144
+ plugins_url( 'assets/js/buy-content.js', myCRED_SELL ),
145
+ array( 'jquery' ),
146
+ '1.1',
147
+ true
148
+ );
149
 
150
  }
151
 
152
  /**
153
+ * Load Script
154
+ * @since 1.7
155
+ * @version 1.0
156
  */
157
+ public function enqueue_footer() {
158
+
159
+ wp_localize_script(
160
+ 'mycred-sell-this',
161
+ 'myCREDBuyContent',
162
+ array(
163
+ 'ajaxurl' => admin_url( 'admin-ajax.php' ),
164
+ 'token' => wp_create_nonce( 'mycred-buy-this-content' ),
165
+ 'working' => esc_js( $this->sell_content['working'] ),
166
+ 'reload' => $this->sell_content['reload']
167
+ )
168
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
+ wp_enqueue_script( 'mycred-sell-this' );
171
 
172
  }
173
 
174
  /**
175
+ * Setup Content Filter
176
+ * @since 1.7
177
+ * @version 1.0
178
  */
179
+ public function template_redirect() {
180
 
181
+ // Filter the content
182
+ add_filter( 'the_content', array( $this, 'the_content' ), 5 );
183
 
184
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
 
186
+ /**
187
+ * User Level Override
188
+ * @since 1.5
189
+ * @version 1.2
190
+ */
191
+ public function sell_content_user_screen( $user ) {
192
 
193
+ // Only visible to admins
194
+ if ( ! mycred_is_admin() ) return;
195
 
196
+ ?>
197
+ <table class="form-table">
198
+ <tr>
199
+ <th scope="row"><?php _e( 'Sell Content', 'mycred' ); ?></th>
200
+ <td>
201
+ <fieldset id="mycred-badge-list" class="badge-list">
202
+ <legend class="screen-reader-text"><span><?php _e( 'Sell Content', 'mycred' ); ?></span></legend>
203
+ <?php
204
 
205
+ if ( ! empty( $this->sell_content['type'] ) ) {
206
+ foreach ( $this->sell_content['type'] as $point_type ) {
207
 
208
+ $mycred = mycred( $point_type );
209
+ $setup = mycred_get_option( 'mycred_sell_this_' . $point_type );
210
+ $users_share = mycred_get_user_meta( $user->ID, 'mycred_sell_content_share_' . $point_type, '', true );
211
 
212
+ ?>
213
+ <div class="profit-share-wrapper color-option<?php if ( strlen( $users_share ) > 0 ) echo ' selected'; ?>" id="mycred-sell-content-profit-<?php echo $point_type; ?>-wrapper">
214
+ <label for="mycred-sell-content-profit-<?php echo $point_type; ?>"><?php printf( '%s - %s', __( 'Profit Share', 'mycred' ), $mycred->plural() ); ?></label><br />
215
+ <input type="text" size="8" name="mycred_sell_this[<?php echo $point_type; ?>]" id="mycred-sell-content-profit-<?php echo $point_type; ?>" value="<?php echo esc_attr( $users_share ); ?>" placeholder="<?php echo esc_attr( $setup['profit_share'] ); ?>" /> %
216
+ </div>
217
+ <?php
218
 
219
+ }
220
  }
221
 
222
+ ?>
223
+ </fieldset>
224
+ </td>
225
+ </tr>
226
+ </table>
227
+ <?php
228
 
229
  }
230
 
231
  /**
232
+ * Save Override
233
+ * @since 1.5
234
+ * @version 1.2
235
  */
236
+ function save_manual_profit_share( $user_id ) {
237
 
238
+ // Only visible to admins
239
+ if ( ! mycred_is_admin() ) return;
 
 
 
 
240
 
241
+ if ( isset( $_POST['mycred_sell_this'] ) && ! empty( $_POST['mycred_sell_this'] ) ) {
242
 
243
+ foreach ( $_POST['mycred_sell_this'] as $point_type => $share ) {
244
+
245
+ $share = sanitize_text_field( $share );
246
+
247
+ mycred_delete_user_meta( $user_id, 'mycred_sell_content_share_' . $point_type );
248
+ if ( $share != '' && is_numeric( $share ) )
249
+ mycred_update_user_meta( $user_id, 'mycred_sell_content_share_' . $point_type, '', $share );
250
+
251
+ }
252
+
253
+ }
254
 
255
  }
256
 
257
  /**
258
+ * Enabled / Disabled Select Options
259
+ * @since 1.7
260
  * @version 1.0
261
  */
262
+ protected function enabled_options( $selected = '' ) {
 
 
 
 
263
 
264
+ $options = array(
265
+ 'disabled' => __( 'Disabled', 'mycred' ),
266
+ 'enabled' => __( 'Enabled', 'mycred' )
267
+ );
 
 
 
 
 
 
 
 
268
 
269
+ $output = '';
270
+ foreach ( $options as $value => $label ) {
271
+ $output .= '<option value="' . $value . '"';
272
+ if ( $selected == $value ) $output .= ' selected="selected"';
273
+ $output .= '>' . $label . '</option>';
274
  }
275
 
276
+ return $output;
277
+
278
  }
279
 
280
  /**
284
  */
285
  public function after_general_settings( $mycred = NULL ) {
286
 
287
+ $post_types = mycred_sell_content_post_types();
288
+ $selected_types = explode( ',', $this->sell_content['post_types'] );
 
 
 
 
 
 
 
 
289
 
290
+ $point_types = mycred_get_types( true );
291
 
292
  ?>
293
+ <h4><span class="dashicons dashicons-admin-plugins static"></span><?php _e( 'Sell Content', 'mycred' ); ?></h4>
294
+ <div class="body" style="display: none;">
295
+ <label class="subheader"><?php _e( 'Post Types', 'mycred' ); ?></label>
296
+ <ol id="myCRED-sell-post-types" class="inline sub-bordered">
297
+ <li class="block"><span class="description"><?php _e( 'Select all the post types you want to sell.', 'mycred' ); ?></span></li>
298
+ <?php
 
 
 
299
 
300
+ if ( ! empty( $post_types ) ) {
301
+ foreach ( $post_types as $post_type => $post_type_label ) {
302
 
303
+ $selected = '';
304
+ if ( in_array( $post_type, $selected_types ) )
305
+ $selected = ' checked="checked"';
306
 
307
+ echo '<li><label for="mycred-sell-' . $this->field_name( array( 'post_types' => $post_type ) ) . '-post-type"><input type="checkbox" name="' . $this->field_name( array( 'post_types' => $post_type ) ) . '"' . $selected . ' class="mycred-check-count" id="mycred-sell-' . $this->field_name( array( 'post_types' => $post_type ) ) . '-post-type" value="' . $post_type . '" data-type="' . $post_type . '" />' . $post_type_label . '</label></li>';
308
 
309
+ }
310
+ }
311
+
312
+ ?>
313
+ <li id="mycred-sell-content-post-type-warning" class="block" style="display: none; clear: both;"><div class="inline-warning"><p><?php _e( 'You must select at least one post type to sell.', 'mycred' ); ?></p></div></li>
314
  </ol>
315
+ <div id="mycred-sell-this-post-type-filter">
316
+ <?php
317
 
318
+ if ( ! empty( $post_types ) ) {
319
+ foreach ( $post_types as $post_type => $post_type_label ) {
320
 
321
+ $settings = array( 'by' => 'all', 'list' => '' );
322
+ if ( array_key_exists( $post_type, $this->sell_content['filters'] ) )
323
+ $settings = $this->sell_content['filters'][ $post_type ];
324
 
325
+ $selected = 'none';
326
+ if ( in_array( $post_type, $selected_types ) )
327
+ $selected = 'block';
328
 
329
+ $options = mycred_get_post_type_options( $post_type );
330
+
331
+ ?>
332
+ <div id="mycred-sell-post-type-<?php echo $post_type; ?>-wrap" style="display: <?php echo $selected; ?>;">
333
+ <ol class="inline sub-bordered slim">
334
+ <li style="width: 30%;">
335
+ <select name="<?php echo $this->field_name( array( 'filters' => $post_type ) ); ?>[by]" class="toggle-filter-menu" data-type="<?php echo $post_type; ?>">
336
  <?php
337
 
338
+ if ( ! empty( $options ) ) {
339
+ foreach ( $options as $value => $option ) {
340
+
341
+ echo '<option value="' . $value . '"';
342
+ if ( $value == $settings['by'] ) echo ' selected="selected"';
343
+ if ( $option['data'] != '' ) echo ' data-place="' . $option['data'] . '"';
344
+ echo '>' . $option['label'] . '</option>';
345
+
346
+ }
347
+ }
348
 
349
  ?>
350
+ </select>
351
+ </li>
352
+ <li id="post-type-filter-<?php echo $post_type; ?>" style="width: 65%; display: <?php if ( ! in_array( $settings['by'], array( 'all', 'manual' ) ) ) echo 'block'; else echo 'none'; ?>;">
353
+ <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'filters' => $post_type ) ); ?>[list]" id="" value="<?php echo esc_attr( $settings['list'] ); ?>" placeholder="<?php if ( array_key_exists( $settings['by'], $options ) ) echo esc_attr( $options[ $settings['by'] ]['data'] ); ?>" class="long" /></div>
354
+ </li>
355
+ </ol>
356
+ </div>
357
  <?php
358
 
359
+ }
360
+ }
361
 
362
  ?>
363
+ </div>
364
+
365
+ <label class="subheader"><?php _e( 'Point Types', 'mycred' ); ?></label>
366
+ <ol id="myCRED-sell-point-type" class="inline">
367
+ <li class="block"><span class="description"><?php _e( 'Select all the point types accepted as payment.', 'mycred' ); ?></span></li>
368
  <?php
369
 
370
+ if ( ! empty( $point_types ) ) {
371
+ foreach ( $point_types as $point_type => $point_type_label ) {
372
+
373
+ $selected = '';
374
+ if ( in_array( $point_type, $this->sell_content['type'] ) )
375
+ $selected = ' checked="checked"';
376
+
377
+ if ( count( $point_types ) === 1 )
378
+ $selected = ' checked="checked" disabled="disabled"';
379
+
380
+ echo '<li><label for="mycred-sell-' . $this->field_name( array( 'type' => $point_type ) ) . '-point-type"><input type="checkbox" name="mycred_pref_core[sell_content][type][]"' . $selected . ' class="mycred-check-count" id="mycred-sell-' . $this->field_name( array( 'type' => $point_type ) ) . '-point-type" value="' . $point_type . '" data-type="' . $point_type . '" />' . $point_type_label . '</label></li>';
381
 
382
  }
383
  }
384
 
385
  ?>
386
+ <li id="mycred-sell-content-point-type-warning" class="block" style="display: none; clear: both;"><div class="inline-warning"><p><?php _e( 'You must select at least one point type to accept as payment.', 'mycred' ); ?></p></div></li>
387
  </ol>
388
+ <ol class="slim sub-bordered">
389
+ <li></li>
390
+ </ol>
391
+ <?php
392
+
393
+ if ( ! empty( $point_types ) ) {
394
+ foreach ( $point_types as $point_type => $point_type_label ) {
395
+
396
+ $selected = 'none';
397
+ if ( in_array( $point_type, $this->sell_content['type'] ) )
398
+ $selected = 'block';
399
+
400
+ if ( count( $point_types ) === 1 )
401
+ $selected = 'block';
402
+
403
+ $mycred = mycred( $point_type );
404
+ $type_setup = mycred_get_option( 'mycred_sell_this_' . $point_type );
405
+ $type_setup = wp_parse_args( $type_setup, array(
406
+ 'status' => 'disabled',
407
+ 'price' => 0,
408
+ 'expire' => 0,
409
+ 'profit_share' => 0,
410
+ 'button_label' => 'Pay %price%',
411
+ 'button_classes' => 'btn btn-primary btn-lg',
412
+ 'log_payment' => 'Purchase of %link_with_title%',
413
+ 'log_sale' => 'Sale of %link_with_title%'
414
+ ) );
415
+
416
+ ?>
417
+ <div id="mycred-sell-<?php echo $point_type; ?>-wrap" style="display: <?php echo $selected; ?>;">
418
+ <label class="subheader"><?php printf( __( '%s Setup', 'mycred' ), $point_type_label ); ?></label>
419
+ <ol class="inline slim">
420
+ <li style="min-width: 200px;">
421
+ <label for="mycred-sell-<?php echo $point_type; ?>-setup-status"><?php _e( 'Status', 'mycred' ); ?></label>
422
+ <div class="h2"><select name="mycred_pref_core[sell_content][post_type_setup][<?php echo $point_type; ?>][status]" id="mycred-sell-<?php echo $point_type; ?>-setup-status"><?php echo $this->enabled_options( $type_setup['status'] ); ?></select></div>
423
+ </li>
424
+ <li style="min-width: 200px;">
425
+ <label for="mycred-sell-<?php echo $point_type; ?>-setup-price"><?php _e( 'Price', 'mycred' ); ?></label>
426
+ <div class="h2"><?php echo $mycred->before; ?><input type="text" name="mycred_pref_core[sell_content][post_type_setup][<?php echo $point_type; ?>][price]" id="mycred-sell-<?php echo $point_type; ?>-setup-price" value="<?php echo $mycred->number( $type_setup['price'] ); ?>" size="8" /><?php echo $mycred->after; ?></div>
427
+ </li>
428
+ <li style="min-width: 200px;">
429
+ <label for="mycred-sell-<?php echo $point_type; ?>-setup-expire"><?php _e( 'Expiration', 'mycred' ); ?></label>
430
+ <div class="h2"><input type="text" name="mycred_pref_core[sell_content][post_type_setup][<?php echo $point_type; ?>][expire]" id="mycred-sell-<?php echo $point_type; ?>-setup-expire" value="<?php echo absint( $type_setup['expire'] ); ?>" size="8" /> <?php echo apply_filters( 'mycred_sell_exp_title', __( 'Hour(s)', 'mycred' ) ); ?></div>
431
+ <span class="description"><?php _e( 'Use zero to disable.', 'mycred' ); ?></span>
432
+ </li>
433
+ <li style="min-width: 200px;">
434
+ <label for="mycred-sell-<?php echo $point_type; ?>-setup-profit_share"><?php _e( 'Profit Share', 'mycred' ); ?></label>
435
+ <div class="h2"><input type="text" name="mycred_pref_core[sell_content][post_type_setup][<?php echo $point_type; ?>][profit_share]" id="mycred-sell-<?php echo $point_type; ?>-setup-profit_share" value="<?php echo esc_attr( $type_setup['profit_share'] ); ?>" size="8" /> %</div>
436
+ <span class="description"><?php _e( 'Use zero to disable.', 'mycred' ); ?></span>
437
+ </li>
438
+ </ol>
439
+ <ol class="inline slim">
440
+ <li>
441
+ <label for="mycred-sell-<?php echo $point_type; ?>-setup-button_label"><?php _e( 'Button Label', 'mycred' ); ?></label>
442
+ <div class="h2"><input type="text" size="30" class="medium" name="mycred_pref_core[sell_content][post_type_setup][<?php echo $point_type; ?>][button_label]" id="mycred-sell-<?php echo $point_type; ?>-setup-button_label" value="<?php echo esc_attr( $type_setup['button_label'] ); ?>" /></div>
443
+ <span class="description"><?php _e( 'Use %price% to show the price set for each post.', 'mycred' ); ?></span>
444
+ </li>
445
+ <li>
446
+ <label for="mycred-sell-<?php echo $point_type; ?>-setup-button_classes"><?php _e( 'Button CSS Classes', 'mycred' ); ?></label>
447
+ <div class="h2"><input type="text" size="30" class="medium" name="mycred_pref_core[sell_content][post_type_setup][<?php echo $point_type; ?>][button_classes]" id="mycred-sell-<?php echo $point_type; ?>-setup-button_classes" value="<?php echo esc_attr( $type_setup['button_classes'] ); ?>" /></div>
448
+ </li>
449
+ </ol>
450
+ <ol class="sub-bordered">
451
+ <li>
452
+ <label for="mycred-sell-<?php echo $point_type; ?>-setup-log_payment"><?php _e( 'Payment log entry template', 'mycred' ); ?></label>
453
+ <div class="h2"><input type="text" size="30" class="long" name="mycred_pref_core[sell_content][post_type_setup][<?php echo $point_type; ?>][log_payment]" id="mycred-sell-<?php echo $point_type; ?>-setup-log_payment" value="<?php echo esc_attr( $type_setup['log_payment'] ); ?>" /></div>
454
+ <?php echo $this->core->available_template_tags( array( 'general', 'post' ) ); ?></span>
455
+ </li>
456
+ <li>
457
+ <label for="mycred-sell-<?php echo $point_type; ?>-setup-log_sale"><?php _e( 'Profit Share payout log entry template', 'mycred' ); ?></label>
458
+ <div class="h2"><input type="text" size="30" class="long" name="mycred_pref_core[sell_content][post_type_setup][<?php echo $point_type; ?>][log_sale]" id="mycred-sell-<?php echo $point_type; ?>-setup-log_sale" value="<?php echo esc_attr( $type_setup['log_sale'] ); ?>" /></div>
459
+ <span class="description"><?php _e( 'Only used if profit sharing is enabled for this point type.', 'mycred' ); ?></span>
460
+ <?php echo $this->core->available_template_tags( array( 'general', 'post' ) ); ?></span>
461
+ </li>
462
+ </ol>
463
+ </div>
464
+ <?php
465
+
466
+ }
467
+ }
468
+
469
+ ?>
470
+ <label class="subheader" for="<?php echo $this->field_id( 'reload' ); ?>"><?php _e( 'Transactions', 'mycred' ); ?></label>
471
+ <ol id="myCRED-buy-template-button">
472
  <li>
473
+ <label for="<?php echo $this->field_id( 'reload' ); ?>"><input type="checkbox" name="<?php echo $this->field_name( 'reload' ); ?>" id="<?php echo $this->field_id( 'reload' ); ?>"<?php checked( $this->sell_content['reload'], 1 ); ?> value="1" /> <?php _e( 'Reload page after successful payments.', 'mycred' ); ?></label>
 
474
  </li>
475
  <li>
476
+ <label for="<?php echo $this->field_id( 'working' ); ?>"><?php _e( 'Button Label', 'mycred' ); ?></label>
477
+ <div class="h2"><input type="text" size="30" class="long code" name="<?php echo $this->field_name( 'working' ); ?>" id="<?php echo $this->field_id( 'working' ); ?>" value="<?php echo esc_attr( $this->sell_content['working'] ); ?>" /></div>
478
+ <span class="description"><?php _e( 'Option to show a custom button label while the payment is being processed. HTML is allowed.', 'mycred' ); ?></span>
479
  </li>
480
+ </ol>
481
+ <label class="subheader"><?php _e( 'Templates', 'mycred' ); ?></label>
482
+ <ol>
483
  <li>
484
+ <h3><?php _e( 'Members', 'mycred' ); ?></h3>
485
+ <p class="description"><?php _e( 'The template to use when a content is viewed by a member that is logged in and can afford to pay. Only applied to content that is set for sale.', 'mycred' ); ?></p>
486
  </li>
487
  <li>
488
+ <?php
489
+
490
+ wp_editor( $this->sell_content['templates']['members'], $this->field_id( array( 'templates' => 'members' ) ), array(
491
+ 'textarea_name' => $this->field_name( array( 'templates' => 'members' ) ),
492
+ 'textarea_rows' => 10
493
+ ) );
494
+
495
+ echo '<p>' . $this->core->available_template_tags( array( 'post' ), '%buy_button%' ) . '</p>';
496
+
497
+ ?>
498
  </li>
499
  <li class="empty">&nbsp;</li>
500
  <li>
501
+ <h3><?php _e( 'Visitors', 'mycred' ); ?></h3>
502
+ <p class="description"><?php _e( 'The template to use when a content is viewed by someone who is not logged in. Only applied to content that is set for sale.', 'mycred' ); ?></p>
 
503
  </li>
 
 
 
504
  <li>
 
505
  <?php
506
 
507
+ wp_editor( $this->sell_content['templates']['visitors'], $this->field_id( array( 'templates' => 'visitors' ) ), array(
 
508
  'textarea_name' => $this->field_name( array( 'templates' => 'visitors' ) ),
509
+ 'textarea_rows' => 10
510
  ) );
511
 
512
+ echo '<p>' . $this->core->available_template_tags( array( 'post' ) ) . '</p>';
 
513
 
514
+ ?>
515
 
516
  </li>
517
  <li class="empty">&nbsp;</li>
518
  <li>
519
+ <h3><?php _e( 'Insufficient Funds', 'mycred' ); ?></h3>
520
+ <p class="description"><?php _e( 'The template to use when a content is viewed by a member that is logged but can not afford to buy. Only applied to content that is set for sale.', 'mycred' ); ?></p>
 
 
 
 
 
 
 
 
 
 
521
  </li>
 
522
  <li>
 
523
  <?php
524
 
525
+ wp_editor( $this->sell_content['templates']['cantafford'], $this->field_id( array( 'templates' => 'cantafford' ) ), array(
 
526
  'textarea_name' => $this->field_name( array( 'templates' => 'cantafford' ) ),
527
+ 'textarea_rows' => 10
528
  ) );
529
 
530
+ echo '<p>' . $this->core->available_template_tags( array( 'post' ) ) . '</p>';
531
+
532
  ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
533
  </li>
534
  </ol>
535
  </div>
536
+ <script type="text/javascript">
537
+ (function($) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
538
 
539
+ var selectedposttypes = <?php echo count( $selected_types ); ?>;
540
+ var selectedpointtypes = <?php echo count( $this->sell_content['type'] ); ?>;
541
+
542
+ $( '#myCRED-sell-post-types .mycred-check-count' ).click(function(){
 
543
 
544
+ if ( $(this).is( ':checked' ) ) {
 
 
 
545
 
546
+ selectedposttypes++;
547
+ $( '#mycred-sell-content-post-type-warning' ).hide();
548
 
549
+ $( '#mycred-sell-post-type-' + $(this).data( 'type' ) + '-wrap' ).show();
550
 
551
  }
552
+ else {
553
+ selectedposttypes--;
554
+ if ( selectedposttypes <= 0 )
555
+ $( '#mycred-sell-content-post-type-warning' ).show();
556
+ else
557
+ $( '#mycred-sell-content-post-type-warning' ).hide();
558
 
559
+ $( '#mycred-sell-post-type-' + $(this).data( 'type' ) + '-wrap' ).hide();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
  }
561
 
562
+ });
563
+
564
+ $( '#myCRED-sell-point-type .mycred-check-count' ).click(function(){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565
 
566
+ if ( $(this).is( ':checked' ) ) {
567
 
568
+ selectedpointtypes++;
569
+ $( '#mycred-sell-content-point-type-warning' ).hide();
570
 
571
+ $( '#mycred-sell-' + $(this).data( 'type' ) + '-wrap' ).show();
572
 
573
  }
574
+ else {
575
+ selectedpointtypes--;
576
+ if ( selectedpointtypes <= 0 )
577
+ $( '#mycred-sell-content-point-type-warning' ).show();
578
+ else
579
+ $( '#mycred-sell-content-point-type-warning' ).hide();
580
 
581
+ $( '#mycred-sell-' + $(this).data( 'type' ) + '-wrap' ).hide();
582
+ }
 
 
 
 
 
 
 
583
 
584
+ });
585
 
586
+ $( '#mycred-sell-this-post-type-filter' ).on( 'change', 'select.toggle-filter-menu', function(){
 
 
587
 
588
+ var post_type = $(this).data( 'type' );
589
+ var selectedfilter = $(this).find( ':selected' );
590
+ var placeholder = selectedfilter.data( 'place' );
591
 
592
+ if ( selectedfilter === undefined || selectedfilter.val() == 'all' || selectedfilter.val() == 'manual' ) {
593
+ $( '#post-type-filter-' + post_type ).hide();
594
+ $( '#post-type-filter-' + post_type + ' input' ).val( '' );
595
+ }
596
 
597
+ else {
598
+ $( '#post-type-filter-' + post_type ).show();
599
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
600
 
601
+ if ( placeholder === undefined )
602
+ $( '#post-type-filter-' + post_type + ' input' ).attr( 'placeholder', '' );
 
 
603
 
604
+ else
605
+ $( '#post-type-filter-' + post_type + ' input' ).attr( 'placeholder', placeholder );
606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607
  });
608
+
609
+ })( jQuery );
610
  </script>
611
  <?php
612
 
613
  }
614
 
615
  /**
616
+ * Sanitize & Save Settings
617
  * @since 0.1
618
+ * @version 1.4
619
  */
620
+ public function sanitize_extra_settings( $new_data, $data, $general ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
621
 
622
+ $settings = $data['sell_content'];
 
623
 
624
+ // Post Types
625
+ $post_types = array();
626
+ if ( array_key_exists( 'post_types', $settings ) && is_array( $settings['post_types'] ) && ! empty( $settings['post_types'] ) ) {
627
 
628
+ foreach ( $settings['post_types'] as $post_type ) {
629
+ $post_types[] = sanitize_text_field( $post_type );
630
+ }
631
 
632
+ }
633
+ $new_data['sell_content']['post_types'] = implode( ',', $post_types );
 
 
 
 
 
634
 
635
+ // Post Type Filter
636
+ $filters = array();
637
+ if ( array_key_exists( 'filters', $settings ) && is_array( $settings['filters'] ) && ! empty( $settings['filters'] ) ) {
638
 
639
+ foreach ( $settings['filters'] as $post_type => $setup ) {
640
 
641
+ if ( ! in_array( $post_type, $post_types ) ) continue;
642
 
643
+ $filters[ $post_type ] = array( 'by' => 'all', 'list' => '' );
644
 
645
+ $by = sanitize_text_field( $setup['by'] );
646
+ if ( $by != '' ) {
 
647
 
648
+ // Unless we selected all, we need to check the list
649
+ if ( $by !== 'all' && $by !== 'manual' ) {
 
650
 
651
+ // Clean up list by sanitizing and removing stray empty spaces
652
+ $list = sanitize_text_field( $setup['list'] );
653
+ if ( $list != '' ) {
654
+ $_list = array();
655
+ foreach ( explode( ',', $list ) as $object_slug ) {
656
+ $object_slug = sanitize_text_field( $object_slug );
657
+ $object_slug = trim( $object_slug );
658
+ $_list[] = $object_slug;
659
+ }
660
+ $list = implode( ',', $_list );
661
+ }
662
 
663
+ $filters[ $post_type ]['by'] = $by;
664
+ $filters[ $post_type ]['list'] = $list;
 
 
 
 
665
 
666
+ }
667
+ elseif ( $by === 'manual' ) {
 
668
 
669
+ $filters[ $post_type ]['by'] = 'manual';
 
 
670
 
671
+ }
 
 
672
 
673
+ }
 
 
 
 
 
674
 
675
  }
 
 
 
676
 
677
  }
678
+ $new_data['sell_content']['filters'] = $filters;
679
 
680
+ // Point Types
681
+ $point_types = array();
682
+ if ( array_key_exists( 'type', $settings ) && is_array( $settings['type'] ) && ! empty( $settings['type'] ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
683
 
684
+ foreach ( $settings['type'] as $point_type ) {
685
+ $point_types[] = sanitize_key( $point_type );
686
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
687
 
688
+ }
689
+ if ( empty( $point_types ) )
690
+ $point_types[] = MYCRED_DEFAULT_TYPE_KEY;
 
 
 
 
 
691
 
692
+ $new_data['sell_content']['type'] = $point_types;
 
693
 
694
+ // Point type default setup
695
+ if ( array_key_exists( 'post_type_setup', $settings ) ) {
696
+ foreach ( $settings['post_type_setup'] as $point_type => $setup ) {
 
697
 
698
+ $new = wp_parse_args( $setup, array(
699
+ 'status' => 'disabled',
700
+ 'price' => 0,
701
+ 'expire' => 0,
702
+ 'profit_share' => 0,
703
+ 'button_label' => '',
704
+ 'button_classes' => '',
705
+ 'log_payment' => '',
706
+ 'log_sale' => ''
707
+ ) );
708
 
709
+ mycred_update_option( 'mycred_sell_this_' . $point_type, $new );
 
710
 
711
+ }
712
  }
713
 
714
+ $new_data['sell_content']['reload'] = ( ( isset( $settings['reload'] ) ) ? absint( $settings['reload'] ) : 0 );
715
+ $new_data['sell_content']['working'] = wp_kses_post( $settings['working'] );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
716
 
717
+ // Templates
718
+ $new_data['sell_content']['templates']['members'] = wp_kses_post( $settings['templates']['members'] );
719
+ $new_data['sell_content']['templates']['visitors'] = wp_kses_post( $settings['templates']['visitors'] );
720
+ $new_data['sell_content']['templates']['cantafford'] = wp_kses_post( $settings['templates']['cantafford'] );
721
 
722
+ update_option( 'mycred_sell_content_one_seven_updated', time() );
723
 
724
+ return $new_data;
725
 
726
  }
727
 
728
  /**
729
+ * Scripts & Styles
730
+ * @since 1.7
 
 
 
 
731
  * @version 1.0
732
  */
733
+ public function scripts_and_styles() {
 
 
734
 
735
+ $screen = get_current_screen();
736
 
737
+ if ( in_array( $screen->id, explode( ',', $this->sell_content['post_types'] ) ) ) {
738
+ wp_enqueue_style( 'mycred-bootstrap-grid' );
739
+ wp_enqueue_style( 'mycred-forms' );
740
+ }
741
 
742
  }
743
 
744
  /**
745
+ * Add Meta Box to Content
 
 
 
 
 
746
  * @since 0.1
747
  * @version 1.1
748
  */
749
+ public function add_metabox( $post ) {
750
 
751
+ $settings = mycred_sell_content_settings();
 
 
 
 
 
 
 
752
 
753
+ // Do not add the metabox unless we set this post type to be "manual"
754
+ if ( $settings['filters'][ $post->post_type ]['by'] !== 'manual' ) return;
755
 
756
+ add_meta_box(
757
+ 'mycred-sell-content-setup',
758
+ apply_filters( 'mycred_sell_this_label', __( 'Sell Content', 'mycred' ), $this ),
759
+ array( $this, 'metabox' ),
760
+ $post->post_type,
761
+ 'side',
762
+ 'high'
763
+ );
764
 
765
+ add_filter( 'postbox_classes_' . $post->post_type . '_mycred-sell-content-setup', array( $this, 'metabox_classes' ) );
766
 
767
  }
768
 
769
  /**
770
+ * Sell Meta Box
 
 
 
771
  * @since 0.1
772
+ * @version 1.2
773
  */
774
+ public function metabox( $post ) {
 
 
 
 
 
 
 
 
775
 
776
+ $settings = mycred_sell_content_settings();
777
+ $expiration = apply_filters( 'mycred_sell_exp_title', __( 'Hour(s)', 'mycred' ) );
778
+ $is_author = ( ( $post->post_author == $this->current_user_id ) ? true : false );
 
779
 
780
+ ?>
781
+ <style type="text/css">
782
+ #mycred-sell-content-setup .inside { padding: 0 !important; }
783
+ #mycred-sell-content-setup .inside .row { margin-bottom: 0; }
784
+ #mycred-sell-content-setup .inside .container-fluid { padding-left: 0; padding-right: 0; }
785
+ #mycred-sell-content-setup .inside .row .col-lg-12 .form-group { padding: 12px 12px 10px 12px; background-color: white; border-bottom: 1px solid #ddd; }
786
+ #mycred-sell-content-types .point-type-setup .cover { border-bottom: 1px solid #ddd; }
787
+ #mycred-sell-content-types .point-type-setup .cover > .row { padding-top: 6px; padding-bottom: 12px; }
788
+ #mycred-sell-content-types .point-type-setup:last-child .cover { border-bottom: none; }
789
+ </style>
790
+ <div id="mycred-sell-content-types" class="container-fluid">
791
+ <input type="hidden" name="mycred-sell-this-setup-token" value="<?php echo wp_create_nonce( 'mycred-sell-this-content' ); ?>" />
792
+ <?php
793
 
794
+ if ( ! empty( $settings['type'] ) ) {
795
+ foreach ( $settings['type'] as $point_type ) {
 
 
796
 
797
+ $setup = mycred_get_option( 'mycred_sell_this_' . $point_type );
798
 
799
+ if ( $setup['status'] === 'disabled' ) continue;
800
 
801
+ $mycred = mycred( $point_type );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
802
 
803
+ $suffix = '_' . $point_type;
804
+ if ( $point_type == MYCRED_DEFAULT_TYPE_KEY )
805
+ $suffix = '';
806
 
807
+ $sale_setup = (array) get_post_meta( $post->ID, 'myCRED_sell_content' . $suffix, true );
808
+ $sale_setup = wp_parse_args( $sale_setup, array(
809
+ 'status' => 'disabled',
810
+ 'price' => 0,
811
+ 'expire' => 0
812
+ ) );
813
 
814
+ $expiration_description = __( 'Never expires', 'mycred' );
815
+ if ( absint( $sale_setup['expire'] ) > 0 )
816
+ $expiration_description = $sale_setup['expire'] . ' ' . $expiration;
 
817
 
818
+ ?>
819
+ <div class="form point-type-setup">
820
+ <div class="row row-narrow">
821
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
822
+ <div class="form-group slim">
823
+ <label for="mycred-sell-this-<?php echo $point_type; ?>-status" class="slim"><input type="checkbox" name="mycred_sell_this[<?php echo $point_type; ?>][status]" id="mycred-sell-this-<?php echo $point_type; ?>-status"<?php if ( $sale_setup['status'] === 'enabled' ) echo ' checked="checked"'; ?> value="enabled" class="toggle-setup" data-type="<?php echo $point_type; ?>" /> <?php printf( __( 'Sell using %s', 'Point types name', 'mycred' ), $mycred->plural() ); ?></label>
824
+ </div>
825
+ </div>
826
+ </div>
827
+ <div class="cover">
828
+ <div class="row row-narrow padded-row mycred-sell-setup-container" id="mycred-sell-content-<?php echo $point_type; ?>-wrap" style="display: <?php if ( $sale_setup['status'] === 'enabled' ) echo 'block'; else echo 'none'; ?>;">
829
+ <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
830
+ <div class="form-group slim">
831
+ <label for="mycred-sell-this-<?php echo $point_type; ?>-price"><?php _e( 'Price', 'mycred' ); ?></label>
832
+ <input type="text" name="mycred_sell_this[<?php echo $point_type; ?>][price]" id="mycred-sell-this-<?php echo $point_type; ?>-price" class="form-control" value="<?php echo esc_attr( $sale_setup['price'] ); ?>" />
833
+ </div>
834
+ </div>
835
+ <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
836
+ <div class="form-group slim">
837
+ <label for="mycred-sell-this-<?php echo $point_type; ?>-expire"><?php _e( 'Expiration', 'mycred' ); ?></label>
838
+ <input type="text" name="mycred_sell_this[<?php echo $point_type; ?>][expire]" id="mycred-sell-this-<?php echo $point_type; ?>-expire" class="form-control" value="<?php echo absint( $sale_setup['expire'] ); ?>" />
839
+ </div>
840
+ </div>
841
+ </div>
842
+ </div>
843
+ </div>
844
+ <?php
845
 
846
  }
 
847
  }
848
 
849
+ ?>
850
+ <script type="text/javascript">
851
+ (function($) {
852
+
853
+ $( '#mycred-sell-content-types .toggle-setup' ).click(function(){
854
 
855
+ if ( $(this).is( ':checked' ) ) {
856
+ $( '#mycred-sell-content-types #mycred-sell-content-' + $(this).data( 'type' ) + '-wrap' ).slideDown();
857
+ }
858
+ else {
859
+ $( '#mycred-sell-content-types #mycred-sell-content-' + $(this).data( 'type' ) + '-wrap' ).slideUp();
860
+ }
861
 
862
+ });
863
 
864
+ })( jQuery );
865
+ </script>
866
+ </div>
867
+ <?php
868
 
869
  }
870
 
871
  /**
872
+ * Save Sell Meta Box
 
 
 
873
  * @since 0.1
874
+ * @version 1.1
875
  */
876
+ public function save_metabox( $post_id ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
877
 
878
+ // Minimum requirement
879
+ if ( ! isset( $_POST['mycred_sell_this'] ) || ! is_array( $_POST['mycred_sell_this'] ) || empty( $_POST['mycred_sell_this'] ) ) return;
880
 
881
+ // Verify nonce
882
+ if ( isset( $_POST['mycred-sell-this-setup-token'] ) && wp_verify_nonce( $_POST['mycred-sell-this-setup-token'], 'mycred-sell-this-content' ) ) {
883
 
884
+ $settings = mycred_sell_content_settings();
 
 
 
 
 
885
 
886
+ if ( ! empty( $settings['type'] ) ) {
887
+ foreach ( $settings['type'] as $point_type ) {
888
 
889
+ if ( ! array_key_exists( $point_type, $_POST['mycred_sell_this'] ) ) continue;
 
890
 
891
+ $mycred = mycred( $point_type );
 
 
 
892
 
893
+ $new_setup = array( 'status' => '', 'price' => 0, 'expire' => 0 );
894
+ $submission = wp_parse_args( $_POST['mycred_sell_this'][ $point_type ], array(
895
+ 'status' => 'disabled',
896
+ 'price' => '',
897
+ 'expire' => ''
898
+ ) );
899
 
900
+ // If not empty and different from the general setup, save<
901
+ if ( in_array( $submission['status'], array( 'enabled', 'disabled' ) ) )
902
+ $new_setup['status'] = sanitize_key( $submission['status'] );
903
 
904
+ // If not empty and different from the general setup, save<
905
+ if ( strlen( $submission['price'] ) > 0 )
906
+ $new_setup['price'] = $mycred->number( sanitize_text_field( $submission['price'] ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
907
 
908
+ // If not empty and different from the general setup, save<
909
+ if ( strlen( $submission['expire'] ) > 0 )
910
+ $new_setup['expire'] = absint( sanitize_text_field( $submission['expire'] ) );
911
 
912
+ $suffix = '_' . $point_type;
913
+ if ( $point_type == MYCRED_DEFAULT_TYPE_KEY )
914
+ $suffix = '';
915
 
916
+ update_post_meta( $post_id, 'myCRED_sell_content' . $suffix, $new_setup );
 
 
 
917
 
918
+ }
919
+ }
920
 
921
  }
922
 
 
 
 
 
 
 
923
  }
924
 
925
  /**
926
+ * The Content Overwrite
927
+ * Handles content sales by replacing the posts content with the appropriate template
928
+ * for those who have not paid. Admins and authors are excluded.
 
929
  * @since 0.1
930
+ * @version 1.2
931
  */
932
+ public function the_content( $content ) {
933
 
934
+ global $mycred_partial_content_sale, $mycred_sell_this;
935
 
936
+ $mycred_partial_content_sale = false;
 
 
 
 
937
 
938
+ $post_id = mycred_sell_content_post_id();
939
+ $post = get_post( $post_id );
 
 
 
940
 
941
+ // If content is for sale
942
+ if ( mycred_post_is_for_sale( $post_id ) ) {
943
 
944
+ $mycred_sell_this = true;
 
945
 
946
+ // Parse shortcodes now to see if mycred_sell_this has been used
947
+ $content = do_shortcode( $content );
 
 
 
 
948
 
949
+ // Partial Content Sale - We have already done the work in the shortcode
950
+ if ( $mycred_partial_content_sale === true )
951
+ return $content;
952
 
953
+ // Logged in users
954
+ if ( is_user_logged_in() ) {
955
 
956
+ // Authors and admins do not pay
957
+ if ( ! mycred_is_admin() && $post->post_author !== $this->current_user_id ) {
 
 
958
 
959
+ // In case we have not paid
960
+ if ( ! mycred_user_paid_for_content( $this->current_user_id, $post_id ) ) {
961
 
962
+ // Get Payment Options
963
+ $payment_options = mycred_sell_content_payment_buttons( $this->current_user_id, $post_id );
964
 
965
+ // User can buy
966
+ if ( $payment_options !== false ) {
967
 
968
+ $content = $this->sell_content['templates']['members'];
969
+ $content = str_replace( '%buy_button%', $payment_options, $content );
970
+ $content = mycred_sell_content_template( $content, $post, 'mycred-sell-entire-content', 'mycred-sell-unpaid' );
 
971
 
972
+ }
 
 
 
973
 
974
+ // Can not afford to buy
975
+ else {
976
 
977
+ $content = $this->sell_content['templates']['cantafford'];
978
+ $content = mycred_sell_content_template( $content, $post, 'mycred-sell-entire-content', 'mycred-sell-insufficient' );
979
 
980
+ }
981
 
982
+ }
983
 
984
+ }
 
985
 
986
+ }
 
 
 
987
 
988
+ // Visitors
989
+ else {
990
 
991
+ $content = $this->sell_content['templates']['visitors'];
992
+ $content = mycred_sell_content_template( $content, $post, 'mycred-sell-entire-content', 'mycred-sell-visitor' );
993
 
994
+ }
 
 
995
 
996
+ }
997
+
998
+ return $content;
999
 
1000
  }
1001
 
1002
  /**
1003
+ * Make Purchase AJAX
1004
+ * @since 1.1
1005
+ * @version 1.4
 
1006
  */
1007
+ public function action_buy_content() {
 
 
 
 
 
 
 
 
 
 
 
 
 
1008
 
1009
+ // Security
1010
+ check_ajax_referer( 'mycred-buy-this-content', 'token' );
 
 
 
 
1011
 
1012
  // Prep
1013
+ $post_id = absint( $_POST['postid'] );
1014
+ $user_id = get_current_user_id();
1015
 
1016
+ // Current User
1017
+ $user_id = get_current_user_id();
 
 
 
 
 
1018
 
1019
+ if ( mycred_force_singular_session( $user_id, 'mycred-last-content-purchase' ) )
1020
+ wp_send_json( 101 );
1021
 
1022
+ $point_type = sanitize_key( $_POST['ctype'] );
1023
+ $post = get_post( $post_id );
1024
 
1025
+ // Minimum requirements
1026
+ if ( $post_id === 0 || ! mycred_point_type_exists( $point_type ) || ! isset( $post->post_author ) ) die( 0 );
1027
 
1028
+ // Attempt purchase
1029
+ $purchase = mycred_sell_content_new_purchase( $post, $user_id, $point_type );
 
 
 
 
 
 
1030
 
1031
+ $content = '';
 
 
 
1032
 
1033
+ // Successfull purchase
1034
+ if ( $purchase === true ) {
1035
 
1036
+ preg_match('/\[mycred_sell_this[^\]]*](.*)\[\/mycred_sell_this[^\]]*]/uis', $post->post_content , $match );
 
 
1037
 
1038
+ $content = $post->post_content;
1039
+ if ( is_array( $match ) && array_key_exists( 1, $match ) )
1040
+ $content = $match[1];
1041
 
1042
+ do_action( 'mycred_sell_before_content_render' );
1043
 
1044
+ remove_filter( 'the_content', array( $this, 'the_content' ), 5 );
1045
+ $content = apply_filters( 'the_content', $content );
1046
+ $content = str_replace( ']]>', ']]&gt;', $content );
1047
+ $content = do_shortcode( $content );
1048
+ add_filter( 'the_content', array( $this, 'the_content' ), 5 );
1049
 
1050
+ }
 
 
1051
 
1052
+ // Something went wrong
1053
  else {
1054
 
1055
+ $content = str_replace( '%error%', $purchase, $content );
 
1056
 
1057
  }
1058
 
1059
+ // Let others play
1060
+ $content = apply_filters( 'mycred_content_purchase_ajax', $content, $purchase );
1061
 
1062
+ if ( $purchase !== true )
1063
+ wp_send_json_error( $purchase );
 
 
 
 
 
 
 
 
1064
 
1065
+ wp_send_json_success( $content );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1066
 
1067
  }
1068
 
1092
  public function email_notices( $data ) {
1093
 
1094
  if ( $data['request']['ref'] == 'buy_content' ) {
1095
+ $message = $data['message'];
1096
  $data['message'] = $this->core->template_tags_post( $message, $data['request']['ref_id'] );
1097
  }
1098
 
1102
 
1103
  }
1104
 
1105
+ endif;
1106
+
1107
+ /**
1108
+ * Load Sell Content Module
1109
+ * @since 1.7
1110
+ * @version 1.0
1111
+ */
1112
+ if ( ! function_exists( 'mycred_load_sell_content_addon' ) ) :
1113
+ function mycred_load_sell_content_addon( $modules, $point_types ) {
1114
+
1115
+ $modules['solo']['content'] = new myCRED_Sell_Content_Module();
1116
+ $modules['solo']['content']->load();
1117
+
1118
+ return $modules;
1119
 
1120
+ }
1121
  endif;
1122
+ add_filter( 'mycred_load_modules', 'mycred_load_sell_content_addon', 90, 2 );
1123
 
1124
  ?>
addons/stats/abstracts/mycred-abstract-stat-widget.php CHANGED
@@ -87,24 +87,22 @@ if ( ! class_exists( 'myCRED_Statistics_Widget' ) ) :
87
  */
88
  function action_buttons() {
89
 
90
- $screen_id = 'myCRED';
91
- $buttons = array();
92
 
93
- if ( $this->args['ctypes'] == 'all' ) {
94
-
95
- }
96
- else {
97
 
98
- if ( $this->args['ctypes'] != 'mycred_default' )
 
99
  $screen_id .= '_' . $this->args['ctypes'];
100
 
101
  $url = add_query_arg( array( 'page' => $screen_id ), admin_url( 'admin.php' ) );
102
  $buttons[] = '<a href="' . esc_url( $url ) . '" class="button button-secondary button-large">' . __( 'View Log', 'mycred' ) . '</a>';
103
 
104
- $url = add_query_arg( array( 'page' => $screen_id . '_page_hooks' ), admin_url( 'admin.php' ) );
105
  $buttons[] = '<a href="' . esc_url( $url ) . '" class="button button-secondary button-large">' . __( 'Hooks', 'mycred' ) . '</a>';
106
 
107
- $url = add_query_arg( array( 'page' => $screen_id . '_page_settings' ), admin_url( 'admin.php' ) );
108
  $buttons[] = '<a href="' . esc_url( $url ) . '" class="button button-secondary button-large">' . __( 'Settings', 'mycred' ) . '</a>';
109
 
110
  }
@@ -164,7 +162,7 @@ if ( ! class_exists( 'myCRED_Statistics_Widget' ) ) :
164
  else {
165
 
166
  // Default type - always available under $this->core
167
- if ( $selected_type == 'mycred_default' )
168
  $result = $this->core->format_number( $value );
169
 
170
  // Custom type
87
  */
88
  function action_buttons() {
89
 
90
+ $screen_id = MYCRED_SLUG;
91
+ $buttons = array();
92
 
93
+ if ( $this->args['ctypes'] != 'all' ) {
 
 
 
94
 
95
+ $this->args['ctypes'] = str_replace( 'view_', '', $this->args['ctypes'] );
96
+ if ( $this->args['ctypes'] != MYCRED_DEFAULT_TYPE_KEY )
97
  $screen_id .= '_' . $this->args['ctypes'];
98
 
99
  $url = add_query_arg( array( 'page' => $screen_id ), admin_url( 'admin.php' ) );
100
  $buttons[] = '<a href="' . esc_url( $url ) . '" class="button button-secondary button-large">' . __( 'View Log', 'mycred' ) . '</a>';
101
 
102
+ $url = add_query_arg( array( 'page' => $screen_id . '-hooks' ), admin_url( 'admin.php' ) );
103
  $buttons[] = '<a href="' . esc_url( $url ) . '" class="button button-secondary button-large">' . __( 'Hooks', 'mycred' ) . '</a>';
104
 
105
+ $url = add_query_arg( array( 'page' => $screen_id . '-settings' ), admin_url( 'admin.php' ) );
106
  $buttons[] = '<a href="' . esc_url( $url ) . '" class="button button-secondary button-large">' . __( 'Settings', 'mycred' ) . '</a>';
107
 
108
  }
162
  else {
163
 
164
  // Default type - always available under $this->core
165
+ if ( $selected_type == MYCRED_DEFAULT_TYPE_KEY )
166
  $result = $this->core->format_number( $value );
167
 
168
  // Custom type
addons/stats/assets/css/{stats-page.css → mycred-statistics-admin.css} RENAMED
@@ -1,4 +1,4 @@
1
- ul#section-nav { border-bottom:solid 1px #ccc; width:100%; margin-top:1em; margin-bottom: 24px; padding:1em 0; padding-bottom: 0; height:2.4em; }
2
  ul#section-nav li { margin-left:0.4em; float:left;font-weight: bold;font-size: 15px;line-height: 24px;}
3
  ul#section-nav li a {text-decoration: none;color:#888;}
4
  ul#section-nav li a:hover, ul#section-nav li.nav-tab-active a {text-decoration: none;color:#000; }
1
+ ul#section-nav { border-bottom:solid 1px #ccc; width:100%; margin-top:1em; margin-bottom: 24px; padding:1em 0; padding-bottom: 0; height: 35px; }
2
  ul#section-nav li { margin-left:0.4em; float:left;font-weight: bold;font-size: 15px;line-height: 24px;}
3
  ul#section-nav li a {text-decoration: none;color:#888;}
4
  ul#section-nav li a:hover, ul#section-nav li.nav-tab-active a {text-decoration: none;color:#000; }
addons/stats/includes/classes/class.query-stats.php ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
+
4
+ /**
5
+ * Query Statistics
6
+ * @see http://codex.mycred.me/classes/mycred_query_stats/
7
+ * @since 1.7
8
+ * @version 1.0
9
+ */
10
+ if ( ! class_exists( 'myCRED_Query_Stats' ) ) :
11
+ class myCRED_Query_Stats {
12
+
13
+ protected $db = '';
14
+
15
+ /**
16
+ * Construct
17
+ */
18
+ public function __construct() {
19
+
20
+ global $mycred;
21
+
22
+ $this->db = $mycred->log_table;
23
+
24
+ }
25
+
26
+ }
27
+ endif;
28
+
29
+ ?>
addons/stats/includes/classes/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ ?>
addons/stats/includes/mycred-stats-functions.php CHANGED
@@ -83,12 +83,20 @@ if ( ! function_exists( 'mycred_get_type_color' ) ) :
83
  //$set = array( 'rgba(213,78,33,1)', 'rgba(46,162,204,1)', 'rgba(34,34,34,1)' );
84
  $set = array( 'rgba(204,175,11,1)', 'rgba(221,130,59,1)', 'rgba(207,73,68,1)', 'rgba(180,60,56,1)', 'rgba(34,34,34,1)' );
85
  $types = mycred_get_types();
 
86
 
87
  $colors = array();
88
  $row = 0;
89
  foreach ( $types as $type_id => $label ) {
90
- $colors[ $type_id ] = $set[ $row ];
 
 
 
 
 
 
91
  $row ++;
 
92
  }
93
 
94
  $result = $colors;
83
  //$set = array( 'rgba(213,78,33,1)', 'rgba(46,162,204,1)', 'rgba(34,34,34,1)' );
84
  $set = array( 'rgba(204,175,11,1)', 'rgba(221,130,59,1)', 'rgba(207,73,68,1)', 'rgba(180,60,56,1)', 'rgba(34,34,34,1)' );
85
  $types = mycred_get_types();
86
+ $saved = mycred_get_option( 'mycred-point-colors', $set );
87
 
88
  $colors = array();
89
  $row = 0;
90
  foreach ( $types as $type_id => $label ) {
91
+
92
+ if ( array_key_exists( $type_id, $saved ) )
93
+ $value = $saved[ $type_id ];
94
+ else
95
+ $value = $set[ $row ];
96
+
97
+ $colors[ $type_id ] = $value;
98
  $row ++;
99
+
100
  }
101
 
102
  $result = $colors;
addons/stats/includes/mycred-stats-shortcodes.php CHANGED
@@ -10,28 +10,21 @@ if ( ! function_exists( 'mycred_statistics_shortcode_render' ) ) :
10
  function mycred_statistics_shortcode_render( $atts ) {
11
 
12
  extract( shortcode_atts( array(
13
- 'id' => 'overallcirculation'
 
 
 
14
  ), $atts ) );
15
 
16
  global $mycred_load_stats;
17
 
18
  $mycred_load_stats = true;
19
 
20
- $args = array( 'ctypes' => 'mycred_deed' );
21
 
22
  ob_start();
23
 
24
- if ( class_exists( 'myCRED_Stats_Widget_Circulation' ) ) {
25
-
26
- $w = new myCRED_Stats_Widget_Circulation( $id, $args );
27
-
28
- echo '<div class="mycred-stat-widget">';
29
-
30
- $w->front_widget();
31
-
32
- echo '</div>';
33
-
34
- }
35
 
36
  $content = ob_get_contents();
37
  ob_end_clean();
10
  function mycred_statistics_shortcode_render( $atts ) {
11
 
12
  extract( shortcode_atts( array(
13
+ 'id' => 'overallcirculation',
14
+ 'show' => 'total',
15
+ 'ctype' => '',
16
+ 'chart' => 'lines'
17
  ), $atts ) );
18
 
19
  global $mycred_load_stats;
20
 
21
  $mycred_load_stats = true;
22
 
23
+ $args = array();
24
 
25
  ob_start();
26
 
27
+
 
 
 
 
 
 
 
 
 
 
28
 
29
  $content = ob_get_contents();
30
  ob_end_clean();
addons/stats/myCRED-addon-stats.php CHANGED
@@ -2,15 +2,12 @@
2
  /**
3
  * Addon: Stats
4
  * Addon URI: http://mycred.me/add-ons/stats/
5
- * Version: 1.1
6
- * Description: Gives you access to your myCRED Staticstics based on your users gains and loses.
7
- * Author: Gabriel S Merovingi
8
- * Author URI: http://www.merovingi.com
9
  */
10
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
 
12
  define( 'myCRED_STATS', __FILE__ );
13
- define( 'myCRED_STATS_VERSION', myCRED_VERSION . '.1' );
14
  define( 'myCRED_STATS_DIR', myCRED_ADDONS_DIR . 'stats/' );
15
  define( 'myCRED_STATS_WIDGETS_DIR', myCRED_STATS_DIR . 'widgets/' );
16
 
@@ -19,6 +16,9 @@ define( 'myCRED_STATS_WIDGETS_DIR', myCRED_STATS_DIR . 'widgets/' );
19
  */
20
  require_once myCRED_STATS_DIR . 'includes/mycred-stats-functions.php';
21
  require_once myCRED_STATS_DIR . 'includes/mycred-stats-shortcodes.php';
 
 
 
22
  require_once myCRED_STATS_DIR . 'abstracts/mycred-abstract-stat-widget.php';
23
 
24
  /**
@@ -46,11 +46,13 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
46
  /**
47
  * Construct
48
  */
49
- function __construct( $type = 'mycred_default' ) {
50
 
51
  parent::__construct( 'myCRED_Stats_Module', array(
52
  'module_name' => 'stats',
 
53
  'register' => false,
 
54
  ), $type );
55
 
56
  $this->label = sprintf( '%s %s', mycred_label(), __( 'Statistics', 'mycred' ) );
@@ -65,61 +67,151 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
65
  */
66
  public function module_init() {
67
 
68
- global $mycred_load_stats;
69
 
 
 
 
70
  add_action( 'mycred_front_enqueue', array( $this, 'register_scripts' ) );
 
 
71
 
 
72
  add_shortcode( 'mycred_statistics', 'mycred_statistics_shortcode_render' );
73
 
74
- add_action( 'wp_footer', array( $this, 'load_front_scripts' ), 5 );
 
 
 
 
 
 
75
 
76
- add_action( 'admin_menu', array( $this, 'add_menu' ) );
77
- add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts_admin' ) );
78
 
79
  }
80
 
81
  /**
82
- * Add Menu
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  * @since 1.6.8
84
- * @version 1.0
85
  */
86
  public function load_front_scripts() {
87
 
88
  global $mycred_load_stats;
89
 
90
- if ( $mycred_load_stats !== true ) {
91
- wp_dequeue_script( 'chart-js' );
92
- }
93
- else {
94
  wp_enqueue_style( 'mycred-stats' );
 
 
95
  }
96
 
97
  }
98
 
99
  /**
100
- * Register Front Scripts
101
- * @since 1.6
102
  * @version 1.0
103
  */
104
- public function register_scripts() {
105
 
106
- // Scripts
107
- wp_enqueue_script(
108
- 'chart-js',
109
- plugins_url( 'assets/js/Chart.js', myCRED_STATS ),
110
- array( 'jquery' ),
111
- '1.1',
112
- true
113
- );
 
 
114
 
115
- // Stylesheets
116
- wp_register_style(
117
- 'mycred-stats',
118
- plugins_url( 'assets/css/mycred-statistics.css', myCRED_STATS ),
119
- array(),
120
- myCRED_VERSION,
121
- 'all'
122
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  }
125
 
@@ -142,45 +234,14 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
142
 
143
  }
144
 
145
- /**
146
- * Register Admin Scripts
147
- * @since 1.6
148
- * @version 1.0
149
- */
150
- public function register_scripts_admin() {
151
-
152
- // Scripts
153
- wp_register_script(
154
- 'chart-js',
155
- plugins_url( 'assets/js/Chart.js', myCRED_STATS ),
156
- array( 'jquery' ),
157
- '1.1'
158
- );
159
-
160
- // Stylesheets
161
- wp_register_style(
162
- 'mycred-stats',
163
- plugins_url( 'assets/css/stats-page.css', myCRED_STATS ),
164
- array(),
165
- myCRED_VERSION
166
- );
167
-
168
- }
169
-
170
  public function get_tabs() {
171
 
172
  $tabs = array();
173
- $tabs['overview'] = array(
174
- 'label' => __( 'Overview', 'mycred' ),
175
- 'class' => array( $this, 'overview_screen' )
176
- );
177
 
178
  foreach ( $this->point_types as $type_id => $label ) {
179
  $mycred = mycred( $type_id );
180
- $tabs[ $type_id ] = array(
181
- 'label' => $mycred->plural(),
182
- 'class' => array( $this, 'ctype_screen' )
183
- );
184
  }
185
 
186
  return apply_filters( 'mycred_statistics_tabs', $tabs );
@@ -190,12 +251,13 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
190
  /**
191
  * Admin Page Header
192
  * @since 1.6
193
- * @version 1.0
194
  */
195
  public function admin_page_header() {
196
 
197
- wp_enqueue_script( 'chart-js' );
198
  wp_enqueue_style( 'mycred-stats' );
 
 
199
 
200
  do_action( 'mycred_stats_page_header', $this );
201
 
@@ -225,9 +287,9 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
225
  * @version 1.0.1
226
  */
227
  public function admin_page() {
 
228
  // Security
229
- if ( ! $this->core->can_edit_creds() )
230
- wp_die( __( 'Access Denied', 'mycred' ) );
231
 
232
  $current = 'overview';
233
  if ( isset( $_GET['view'] ) )
@@ -237,7 +299,7 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
237
 
238
  ?>
239
  <div id="mycred-stats" class="wrap">
240
- <h2><?php echo $this->label; ?><a href="javascript:void(0);" onClick="window.location.href=window.location.href" class="add-new-h2" id="refresh-mycred-stats"><?php _e( 'Refresh', 'mycred' ); ?></a></h2>
241
  <?php
242
 
243
  do_action( 'mycred_stats_page_before', $this );
@@ -249,7 +311,7 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
249
  <ul id="section-nav" class="nav-tab-wrapper">
250
  <?php
251
 
252
- foreach ( $tabs as $tab_id => $tab ) {
253
 
254
  $classes = 'nav-tab';
255
  if ( $current == $tab_id ) $classes .= ' nav-tab-active';
@@ -259,7 +321,7 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
259
  else
260
  $url = add_query_arg( array( 'page' => $_GET['page'] ), admin_url( 'admin.php' ) );
261
 
262
- echo '<li class="' . $classes . '"><a href="' . esc_url( $url ) . '">' . $tab['label'] . '</a></li>';
263
 
264
  }
265
 
@@ -271,20 +333,71 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
271
  <?php
272
 
273
  // Render tab
274
- if ( isset( $tabs[ $current ]['class'] ) && $tabs[ $current ]['class'] != '' ) {
275
 
276
- $method = $tabs[ $current ]['class'];
277
- if ( is_array( $method ) ) {
278
- if ( $method[0] == $this )
279
- $this->$method[1]( $current );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
 
281
- elseif ( class_exists( $method[0] ) ) {
282
- $class = new $method[0]();
283
- $class->$method[1]( $current );
284
  }
285
  }
286
- elseif ( ! is_array( $method ) && function_exists( $method ) )
287
- $method( $current );
288
 
289
  }
290
 
@@ -310,77 +423,24 @@ if ( ! class_exists( 'myCRED_Stats_Module' ) ) :
310
 
311
  }
312
 
313
- /**
314
- * Overview Screen
315
- * @since 1.6
316
- * @version 1.0
317
- */
318
- public function overview_screen( $current = '' ) {
319
-
320
- $widgets = apply_filters( 'mycred_stats_overview_widgets', array(
321
- 0 => array( 'id' => 'overallcirculation', 'class' => 'myCRED_Stats_Widget_Circulation', 'args' => array( 'ctypes' => 'all' ) ),
322
- 1 => array( 'id' => 'overallgains', 'class' => 'myCRED_Stats_Widget_Daily_Gains', 'args' => array( 'ctypes' => 'all', 'span' => 10, 'number' => 5 ) ),
323
- 2 => array( 'id' => 'overallloses', 'class' => 'myCRED_Stats_Widget_Daily_Loses', 'args' => array( 'ctypes' => 'all', 'span' => 10, 'number' => 5 ) )
324
- ), $this );
325
-
326
- if ( ! empty( $widgets ) ) {
327
- foreach ( $widgets as $num => $swidget ) {
328
-
329
- $widget = $swidget['class'];
330
- if ( class_exists( $widget ) ) {
331
- $w = new $widget( $swidget['id'], $swidget['args'] );
332
-
333
- echo '<div class="mycred-stat-widget">';
334
-
335
- $w->widget();
336
-
337
- echo '</div>';
338
-
339
- }
340
-
341
- }
342
- }
343
-
344
- }
345
-
346
- /**
347
- * Point Type Screen
348
- * @since 1.6
349
- * @version 1.0
350
- */
351
- public function ctype_screen( $current = '' ) {
352
-
353
- $widgets = apply_filters( 'mycred_stats_' . $current . '_widgets', array(
354
- 0 => array( 'id' => $current . 'circulation', 'class' => 'myCRED_Stats_Widget_Circulation', 'args' => array( 'ctypes' => $current ) ),
355
- 1 => array( 'id' => $current . 'gains', 'class' => 'myCRED_Stats_Widget_Daily_Gains', 'args' => array( 'ctypes' => $current, 'span' => 10, 'number' => 5 ) ),
356
- 2 => array( 'id' => $current . 'loses', 'class' => 'myCRED_Stats_Widget_Daily_Loses', 'args' => array( 'ctypes' => $current, 'span' => 10, 'number' => 5 ) )
357
- ), $this );
358
-
359
- if ( ! empty( $widgets ) ) {
360
- foreach ( $widgets as $num => $swidget ) {
361
-
362
- $widget = $swidget['class'];
363
- if ( class_exists( $widget ) ) {
364
- $w = new $widget( $swidget['id'], $swidget['args'] );
365
-
366
- echo '<div class="mycred-stat-widget">';
367
-
368
- $w->widget();
369
-
370
- echo '</div>';
371
 
372
- }
 
 
 
 
 
 
373
 
374
- }
375
- }
376
 
377
- }
378
 
379
  }
380
-
381
- $mycred_stats = new myCRED_Stats_Module();
382
- $mycred_stats->load();
383
-
384
  endif;
 
385
 
386
  ?>
2
  /**
3
  * Addon: Stats
4
  * Addon URI: http://mycred.me/add-ons/stats/
5
+ * Version: 1.2
 
 
 
6
  */
7
  if ( ! defined( 'myCRED_VERSION' ) ) exit;
8
 
9
  define( 'myCRED_STATS', __FILE__ );
10
+ define( 'myCRED_STATS_VERSION', '1.2' );
11
  define( 'myCRED_STATS_DIR', myCRED_ADDONS_DIR . 'stats/' );
12
  define( 'myCRED_STATS_WIDGETS_DIR', myCRED_STATS_DIR . 'widgets/' );
13
 
16
  */
17
  require_once myCRED_STATS_DIR . 'includes/mycred-stats-functions.php';
18
  require_once myCRED_STATS_DIR . 'includes/mycred-stats-shortcodes.php';
19
+
20
+ require_once myCRED_STATS_DIR . 'includes/classes/class.query-stats.php';
21
+
22
  require_once myCRED_STATS_DIR . 'abstracts/mycred-abstract-stat-widget.php';
23
 
24
  /**
46
  /**
47
  * Construct
48
  */
49
+ function __construct( $type = MYCRED_DEFAULT_TYPE_KEY ) {
50
 
51
  parent::__construct( 'myCRED_Stats_Module', array(
52
  'module_name' => 'stats',
53
+ 'accordion' => false,
54
  'register' => false,
55
+ 'add_to_core' => false
56
  ), $type );
57
 
58
  $this->label = sprintf( '%s %s', mycred_label(), __( 'Statistics', 'mycred' ) );
67
  */
68
  public function module_init() {
69
 
70
+ global $mycred_types, $mycred_load_stats;
71
 
72
+ $mycred_load_stats = false;
73
+
74
+ // Scripts & Styles
75
  add_action( 'mycred_front_enqueue', array( $this, 'register_scripts' ) );
76
+ add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
77
+ add_action( 'wp_footer', array( $this, 'load_front_scripts' ), 5 );
78
 
79
+ // Add shortcode
80
  add_shortcode( 'mycred_statistics', 'mycred_statistics_shortcode_render' );
81
 
82
+ // Add color options to each point type
83
+ add_action( 'mycred_core_prefs', array( $this, 'color_settings' ) );
84
+ foreach ( $mycred_types as $type_id => $type ) {
85
+ if ( $type_id === MYCRED_DEFAULT_TYPE_KEY ) continue;
86
+ add_action( 'mycred_core_prefs' . $type_id, array( $this, 'color_settings' ) );
87
+ }
88
+ add_filter( 'mycred_save_core_prefs', array( $this, 'sanitize_extra_settings' ), 80, 3 );
89
 
90
+ // Add admin screen
91
+ add_action( 'admin_menu', array( $this, 'add_menu' ) );
92
 
93
  }
94
 
95
  /**
96
+ * Register Front Scripts
97
+ * @since 1.6
98
+ * @version 1.1
99
+ */
100
+ public function register_scripts() {
101
+
102
+ // Stylesheets
103
+ wp_register_style(
104
+ 'mycred-stats',
105
+ plugins_url( 'assets/css/mycred-statistics.css', myCRED_STATS ),
106
+ array(),
107
+ '1.1',
108
+ 'all'
109
+ );
110
+
111
+ // Stylesheets
112
+ wp_register_style(
113
+ 'mycred-stats-admin',
114
+ plugins_url( 'assets/css/mycred-statistics-admin.css', myCRED_STATS ),
115
+ array(),
116
+ myCRED_STATS_VERSION,
117
+ 'all'
118
+ );
119
+
120
+ // Scripts
121
+ wp_register_script(
122
+ 'chart-js',
123
+ plugins_url( 'assets/js/Chart.js', myCRED_STATS ),
124
+ array( 'jquery' ),
125
+ myCRED_STATS_VERSION,
126
+ true
127
+ );
128
+
129
+ }
130
+
131
+ /**
132
+ * Admin Screen Styles
133
  * @since 1.6.8
134
+ * @version 1.1
135
  */
136
  public function load_front_scripts() {
137
 
138
  global $mycred_load_stats;
139
 
140
+ if ( $mycred_load_stats === true ) {
141
+
 
 
142
  wp_enqueue_style( 'mycred-stats' );
143
+ wp_enqueue_script( 'chart-js' );
144
+
145
  }
146
 
147
  }
148
 
149
  /**
150
+ * Color Settings
151
+ * @since 1.7
152
  * @version 1.0
153
  */
154
+ public function color_settings( $settings ) {
155
 
156
+ $colors = mycred_get_type_color( $settings->mycred_type );
157
+ if ( ! is_array( $colors ) ) {
158
+ $colors = array(
159
+ 'positive' => mycred_rgb_to_hex( $colors ),
160
+ 'negative' => ''
161
+ );
162
+ $colors['negative'] = mycred_inverse_hex_color( $colors['positive'] );
163
+ }
164
+ elseif ( is_array( $colors ) && $colors['positive'] != '' && $colors['negative'] == '' )
165
+ $colors['negative'] = mycred_rgb_to_hex( $colors );
166
 
167
+ ?>
168
+ <h3><?php _e( 'Statistics Color', 'mycred' ); ?></h3>
169
+ <div class="row">
170
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
171
+ <div class="form-group">
172
+ <label for="<?php echo $settings->field_id( array( 'colors' => 'positive' ) ); ?>"><?php _e( 'Positive Values', 'mycred' ); ?></label>
173
+ <input type="text" name="<?php echo $settings->field_name( array( 'colors' => 'positive' ) ); ?>" id="<?php echo $settings->field_id( array( 'colors' => 'positive' ) ); ?>" value="<?php echo esc_attr( $colors['positive'] ); ?>" class="wp-color-picker-field" data-default-color="#dedede" />
174
+ </div>
175
+ </div>
176
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
177
+ <div class="form-group">
178
+ <label for="<?php echo $settings->field_id( array( 'colors' => 'negative' ) ); ?>"><?php _e( 'Negative Values', 'mycred' ); ?></label>
179
+ <input type="text" name="<?php echo $settings->field_name( array( 'colors' => 'negative' ) ); ?>" id="<?php echo $settings->field_id( array( 'colors' => 'negative' ) ); ?>" value="<?php echo esc_attr( $colors['negative'] ); ?>" class="wp-color-picker-field" data-default-color="" />
180
+ </div>
181
+ </div>
182
+ </div>
183
+ <script type="text/javascript">
184
+ jQuery(document).ready(function($){
185
+
186
+ // Load wp color picker
187
+ $( '.wp-color-picker-field' ).wpColorPicker();
188
+
189
+ });
190
+ </script>
191
+ <?php
192
+
193
+ }
194
+
195
+ /**
196
+ * Sanitize & Save Settings
197
+ * @since 1.7
198
+ * @version 1.0
199
+ */
200
+ public function sanitize_extra_settings( $new_data, $data, $general ) {
201
+
202
+ $colors = array();
203
+ $colors['positive'] = sanitize_text_field( $data['colors']['positive'] );
204
+ $colors['negative'] = sanitize_text_field( $data['colors']['negative'] );
205
+
206
+ $saved = mycred_get_type_color();
207
+ $saved[ $this->mycred_type ] = $colors;
208
+
209
+ mycred_update_option( 'mycred-point-colors', $saved );
210
+
211
+ if ( array_key_exists( 'colors', $new_data ) )
212
+ unset( $new_data['colors'] );
213
+
214
+ return $new_data;
215
 
216
  }
217
 
234
 
235
  }
236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  public function get_tabs() {
238
 
239
  $tabs = array();
240
+ $tabs['overview'] = __( 'Overview', 'mycred' );
 
 
 
241
 
242
  foreach ( $this->point_types as $type_id => $label ) {
243
  $mycred = mycred( $type_id );
244
+ $tabs[ 'view_' . $type_id ] = $mycred->plural();
 
 
 
245
  }
246
 
247
  return apply_filters( 'mycred_statistics_tabs', $tabs );
251
  /**
252
  * Admin Page Header
253
  * @since 1.6
254
+ * @version 1.1
255
  */
256
  public function admin_page_header() {
257
 
 
258
  wp_enqueue_style( 'mycred-stats' );
259
+ wp_enqueue_style( 'mycred-stats-admin' );
260
+ wp_enqueue_script( 'chart-js' );
261
 
262
  do_action( 'mycred_stats_page_header', $this );
263
 
287
  * @version 1.0.1
288
  */
289
  public function admin_page() {
290
+
291
  // Security
292
+ if ( ! $this->core->can_edit_creds() ) wp_die( 'Access Denied' );
 
293
 
294
  $current = 'overview';
295
  if ( isset( $_GET['view'] ) )
299
 
300
  ?>
301
  <div id="mycred-stats" class="wrap">
302
+ <h1><?php echo $this->label; ?><a href="javascript:void(0);" onClick="window.location.href=window.location.href" class="add-new-h2" id="refresh-mycred-stats"><?php _e( 'Refresh', 'mycred' ); ?></a></h1>
303
  <?php
304
 
305
  do_action( 'mycred_stats_page_before', $this );
311
  <ul id="section-nav" class="nav-tab-wrapper">
312
  <?php
313
 
314
+ foreach ( $tabs as $tab_id => $tab_label ) {
315
 
316
  $classes = 'nav-tab';
317
  if ( $current == $tab_id ) $classes .= ' nav-tab-active';
321
  else
322
  $url = add_query_arg( array( 'page' => $_GET['page'] ), admin_url( 'admin.php' ) );
323
 
324
+ echo '<li class="' . $classes . '"><a href="' . esc_url( $url ) . '">' . $tab_label . '</a></li>';
325
 
326
  }
327
 
333
  <?php
334
 
335
  // Render tab
336
+ if ( has_action( 'mycred_stats_screen_' . $current ) ) {
337
 
338
+ do_action( 'mycred_stats_screen_' . $current );
339
+
340
+ }
341
+
342
+ elseif ( $current === 'overview' ) {
343
+
344
+ $widgets = apply_filters( 'mycred_stats_overview_widgets', array(
345
+ 0 => array( 'id' => 'overallcirculation', 'class' => 'myCRED_Stats_Widget_Circulation', 'args' => array( 'ctypes' => 'all' ) ),
346
+ 1 => array( 'id' => 'overallgains', 'class' => 'myCRED_Stats_Widget_Daily_Gains', 'args' => array( 'ctypes' => 'all', 'span' => 10, 'number' => 5 ) ),
347
+ 2 => array( 'id' => 'overallloses', 'class' => 'myCRED_Stats_Widget_Daily_Loses', 'args' => array( 'ctypes' => 'all', 'span' => 10, 'number' => 5 ) )
348
+ ), $this );
349
+
350
+ if ( ! empty( $widgets ) ) {
351
+ foreach ( $widgets as $num => $swidget ) {
352
+
353
+ $widget = $swidget['class'];
354
+ if ( class_exists( $widget ) ) {
355
+
356
+ $w = new $widget( $swidget['id'], $swidget['args'] );
357
+
358
+ echo '<div class="mycred-stat-widget">';
359
+
360
+ $w->widget();
361
+
362
+ echo '</div>';
363
+
364
+ $w = NULL;
365
+
366
+ }
367
+
368
+ }
369
+ }
370
+
371
+ }
372
+
373
+ elseif ( substr( $current, 0, 5 ) === 'view_' ) {
374
+
375
+ $widgets = apply_filters( 'mycred_stats_' . $current . '_widgets', array(
376
+ 0 => array( 'id' => $current . 'circulation', 'class' => 'myCRED_Stats_Widget_Circulation', 'args' => array( 'ctypes' => $current ) ),
377
+ 1 => array( 'id' => $current . 'gains', 'class' => 'myCRED_Stats_Widget_Daily_Gains', 'args' => array( 'ctypes' => $current, 'span' => 10, 'number' => 5 ) ),
378
+ 2 => array( 'id' => $current . 'loses', 'class' => 'myCRED_Stats_Widget_Daily_Loses', 'args' => array( 'ctypes' => $current, 'span' => 10, 'number' => 5 ) )
379
+ ), $this );
380
+
381
+ if ( ! empty( $widgets ) ) {
382
+ foreach ( $widgets as $num => $swidget ) {
383
+
384
+ $widget = $swidget['class'];
385
+ if ( class_exists( $widget ) ) {
386
+
387
+ $w = new $widget( $swidget['id'], $swidget['args'] );
388
+
389
+ echo '<div class="mycred-stat-widget">';
390
+
391
+ $w->widget();
392
+
393
+ echo '</div>';
394
+
395
+ $w = NULL;
396
+
397
+ }
398
 
 
 
 
399
  }
400
  }
 
 
401
 
402
  }
403
 
423
 
424
  }
425
 
426
+ }
427
+ endif;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
 
429
+ /**
430
+ * Load Statistics Module
431
+ * @since 1.7
432
+ * @version 1.0
433
+ */
434
+ if ( ! function_exists( 'mycred_load_statistics_addon' ) ) :
435
+ function mycred_load_statistics_addon( $modules, $point_types ) {
436
 
437
+ $modules['solo']['stats'] = new myCRED_Stats_Module();
438
+ $modules['solo']['stats']->load();
439
 
440
+ return $modules;
441
 
442
  }
 
 
 
 
443
  endif;
444
+ add_filter( 'mycred_load_modules', 'mycred_load_statistics_addon', 100, 2 );
445
 
446
  ?>
addons/stats/widgets/mycred-stats-widget-circulation.php CHANGED
@@ -216,8 +216,8 @@ if ( ! class_exists( 'myCRED_Stats_Widget_Circulation' ) ) :
216
  <td class="rowtitle"><?php echo $this->ctypes[ $type_id ]; ?></td>
217
  <?php
218
 
219
- $page_id = 'myCRED';
220
- if ( $type_id != 'mycred_default' )
221
  $page_id .= '_' . $type_id;
222
 
223
  $base_url = add_query_arg( array( 'page' => $page_id ), admin_url( 'admin.php' ) );
@@ -257,7 +257,7 @@ jQuery(function($) {
257
 
258
  /**
259
  * Display
260
- * @version 1.0.1
261
  */
262
  function widget() {
263
 
@@ -292,8 +292,11 @@ jQuery(function($) {
292
  $loses = $this->get_total( $this->args['ctypes'], false );
293
 
294
  $total = $gains + abs( $loses );
295
- $gains_p = number_format( ( ( $gains / $total ) * 100 ), 0 );
296
- $gains_l = number_format( ( 100 - $gains_p ), 0 );
 
 
 
297
 
298
  $color = $gain_color = $this->colors[ $this->args['ctypes'] ];
299
  $circulation['series'][] = "{ value: {$gains_p}, color: '" . $color . "', highlight: '" . $color . "', label: '" . esc_attr__( 'Total gains (%)', 'mycred' ) . "' }";
@@ -302,7 +305,6 @@ jQuery(function($) {
302
  $color = $lose_color = mycred_inverse_rgb_color( $this->colors[ $this->args['ctypes'] ] );
303
  $circulation['series'][] = "{ value: {$gains_l}, color: '" . $color . "', highlight: '" . $color . "', label: '" . esc_attr__( 'Total loses (%)', 'mycred' ) . "' }";
304
 
305
- //
306
  ?>
307
  <ul id="total-per-point-type"><li><?php printf( __( 'Total Gained: %s', 'mycred' ), '<span style="color:' . $gain_color . '">' . $mycred->format_creds( $gains ) . '</span>' ); ?></li><li><?php printf( __( 'Total Spent: %s', 'mycred' ), '<span style="color:' . $lose_color . '">' . $mycred->format_creds( $loses ) . '</span>' ); ?></li></ul>
308
  <?php
@@ -337,15 +339,16 @@ jQuery(function($) {
337
 
338
  foreach ( $gains_loses as $type_id => $dates ) {
339
 
340
- $mycred = mycred( $type_id );
 
341
 
342
  ?>
343
  <tr style="color: <?php echo $this->colors[ $type_id ]; ?>;">
344
  <td class="rowtitle"><?php echo $this->ctypes[ $type_id ]; ?></td>
345
  <?php
346
 
347
- $page_id = 'myCRED';
348
- if ( $type_id != 'mycred_default' )
349
  $page_id .= '_' . $type_id;
350
 
351
  $base_url = add_query_arg( array( 'page' => $page_id ), admin_url( 'admin.php' ) );
216
  <td class="rowtitle"><?php echo $this->ctypes[ $type_id ]; ?></td>
217
  <?php
218
 
219
+ $page_id = MYCRED_SLUG;
220
+ if ( $type_id != MYCRED_DEFAULT_TYPE_KEY )
221
  $page_id .= '_' . $type_id;
222
 
223
  $base_url = add_query_arg( array( 'page' => $page_id ), admin_url( 'admin.php' ) );
257
 
258
  /**
259
  * Display
260
+ * @version 1.0.2
261
  */
262
  function widget() {
263
 
292
  $loses = $this->get_total( $this->args['ctypes'], false );
293
 
294
  $total = $gains + abs( $loses );
295
+ $gains_p = $gains_l = 0;
296
+ if ( $total != 0 ) {
297
+ $gains_p = number_format( ( ( $gains / $total ) * 100 ), 0 );
298
+ $gains_l = number_format( ( 100 - $gains_p ), 0 );
299
+ }
300
 
301
  $color = $gain_color = $this->colors[ $this->args['ctypes'] ];
302
  $circulation['series'][] = "{ value: {$gains_p}, color: '" . $color . "', highlight: '" . $color . "', label: '" . esc_attr__( 'Total gains (%)', 'mycred' ) . "' }";
305
  $color = $lose_color = mycred_inverse_rgb_color( $this->colors[ $this->args['ctypes'] ] );
306
  $circulation['series'][] = "{ value: {$gains_l}, color: '" . $color . "', highlight: '" . $color . "', label: '" . esc_attr__( 'Total loses (%)', 'mycred' ) . "' }";
307
 
 
308
  ?>
309
  <ul id="total-per-point-type"><li><?php printf( __( 'Total Gained: %s', 'mycred' ), '<span style="color:' . $gain_color . '">' . $mycred->format_creds( $gains ) . '</span>' ); ?></li><li><?php printf( __( 'Total Spent: %s', 'mycred' ), '<span style="color:' . $lose_color . '">' . $mycred->format_creds( $loses ) . '</span>' ); ?></li></ul>
310
  <?php
339
 
340
  foreach ( $gains_loses as $type_id => $dates ) {
341
 
342
+ $type_id = str_replace( 'view_', '', $type_id );
343
+ $mycred = mycred( $type_id );
344
 
345
  ?>
346
  <tr style="color: <?php echo $this->colors[ $type_id ]; ?>;">
347
  <td class="rowtitle"><?php echo $this->ctypes[ $type_id ]; ?></td>
348
  <?php
349
 
350
+ $page_id = MYCRED_SLUG;
351
+ if ( $type_id != MYCRED_DEFAULT_TYPE_KEY )
352
  $page_id .= '_' . $type_id;
353
 
354
  $base_url = add_query_arg( array( 'page' => $page_id ), admin_url( 'admin.php' ) );
addons/stats/widgets/mycred-stats-widget-daily-gains.php CHANGED
@@ -138,8 +138,8 @@ if ( ! class_exists( 'myCRED_Stats_Widget_Daily_Gains' ) ) :
138
  else
139
  $label = ucfirst( str_replace( '_', ' ', $item->ref ) );
140
 
141
- $page_id = 'myCRED';
142
- if ( $item->type != 'mycred_default' )
143
  $page_id .= '_' . $item->type;
144
 
145
  $base_url = admin_url( 'admin.php' );
138
  else
139
  $label = ucfirst( str_replace( '_', ' ', $item->ref ) );
140
 
141
+ $page_id = MYCRED_SLUG;
142
+ if ( $item->type != MYCRED_DEFAULT_TYPE_KEY )
143
  $page_id .= '_' . $item->type;
144
 
145
  $base_url = admin_url( 'admin.php' );
addons/stats/widgets/mycred-stats-widget-daily-loses.php CHANGED
@@ -138,8 +138,8 @@ if ( ! class_exists( 'myCRED_Stats_Widget_Daily_Loses' ) ) :
138
  else
139
  $label = ucfirst( str_replace( '_', ' ', $item->ref ) );
140
 
141
- $page_id = 'myCRED';
142
- if ( $item->type != 'mycred_default' )
143
  $page_id .= '_' . $item->type;
144
 
145
  $base_url = admin_url( 'admin.php' );
138
  else
139
  $label = ucfirst( str_replace( '_', ' ', $item->ref ) );
140
 
141
+ $page_id = MYCRED_SLUG;
142
+ if ( $item->type != MYCRED_DEFAULT_TYPE_KEY )
143
  $page_id .= '_' . $item->type;
144
 
145
  $base_url = admin_url( 'admin.php' );
addons/transfer/assets/css/transfer.css DELETED
@@ -1,33 +0,0 @@
1
- /**
2
- * Transfer Styling
3
- * @since 0.1
4
- * @version 1.1
5
- */
6
- .mycred-transfer-cred-wrapper { display: block; float: none; clear: both; margin: 0; padding: 0; }
7
- .mycred-transfer-cred-wrapper>p { margin-bottom: 6px; }
8
- .mycred-transfer-cred-wrapper ol { margin: 0; padding: 0; float: none; clear: both; }
9
- .mycred-transfer-cred-wrapper ol li { list-style-type: none; float: left; margin: 0; padding: 0; }
10
- .widget .mycred-transfer-cred-wrapper ol li { float: none; }
11
-
12
- .mycred-transfer-cred-wrapper ol li.mycred-send-to { width: 40%; }
13
- .mycred-transfer-cred-wrapper ol li.mycred-send-to div.transfer-to { height: 100px; }
14
- .widget .mycred-transfer-cred-wrapper ol li.mycred-send-to { width: 100%; }
15
-
16
- .mycred-transfer-cred-wrapper ol li.mycred-send-amount { width: 60%; }
17
- .widget .mycred-transfer-cred-wrapper ol li.mycred-send-amount { width: 100%; }
18
- .mycred-transfer-cred-wrapper ol li.mycred-send-details span:empty { display: none; }
19
- .mycred-transfer-cred-wrapper .mycred-click { float: right; }
20
-
21
- .mycred-transfer-cred-wrapper ol li label { display: block; }
22
- .mycred-transfer-cred-wrapper ol li input[type="text"] { width: 90%; }
23
- .mycred-transfer-cred-wrapper ol li input[type="text"].short { width: 40%; }
24
- .mycred-transfer-cred-wrapper ol li input[type="button"] { }
25
-
26
- .mycred-transfer-info { margin: 0; padding: 6px 0; display: block; clear: both; float: none; }
27
- .mycred-transfer-info p { font-size: smaller; line-height: 16px; margin: 0; padding: 0; }
28
-
29
- ul.ui-autocomplete { border: none; margin: 0; padding: 6px 0 0 0; width: 100%; z-index: 10 !important; }
30
- ul.ui-autocomplete li { background-color: white; padding: 0 4px; display: block; margin: 0; width: 90%; text-align: left; border-bottom: 1px solid silver; list-style-type: none; }
31
- ul.ui-autocomplete li:first-child { border-top: 1px solid silver; }
32
- ul.ui-autocomplete li:hover { background-color: #eee; }
33
- ul.ui-autocomplete li a { display: block; cursor: pointer; width: 100%; margin: 0; padding: 8px 0; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
addons/transfer/assets/js/mycred-transfer.js ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * myCRED Transfer jQuery
3
+ * Handles transfer requests and autocomplete of recipient search.
4
+ *
5
+ * @requires jQuery
6
+ * @requires jQuery UI
7
+ * @requires jQuery Autocomplete
8
+ * @since 0.1
9
+ * @version 1.5
10
+ */
11
+ (function($) {
12
+
13
+ var mycred_transfer_cache = {};
14
+
15
+ // Autocomplete
16
+ // @api http://api.jqueryui.com/autocomplete/
17
+ var mycred_transfer_autofill = $( 'input.mycred-autofill' ).autocomplete({
18
+
19
+ minLength : 2,
20
+ source : function( request, response ) {
21
+
22
+ var term = request.term;
23
+ if ( term in mycred_transfer_cache ) {
24
+ response( mycred_transfer_cache[ term ] );
25
+ return;
26
+ }
27
+
28
+ var send = {
29
+ action : "mycred-autocomplete",
30
+ token : myCREDTransfer.token,
31
+ string : request
32
+ };
33
+
34
+ $.getJSON( myCREDTransfer.ajaxurl, send, function( data, status, xhr ) {
35
+ mycred_transfer_cache[ term ] = data;
36
+ response( data );
37
+ });
38
+
39
+ },
40
+ messages: {
41
+ noResults : '',
42
+ results : function() {}
43
+ },
44
+ position: { my : "right top", at: "right bottom" }
45
+
46
+ });
47
+
48
+ $( 'input.mycred-autofill' ).click(function(){
49
+
50
+ var formfieldid = $(this).data( 'form' );
51
+ mycred_transfer_autofill.autocomplete( "option", "appendTo", '#mycred-transfer-form-' + formfieldid + ' .select-recipient-wrapper' );
52
+ console.log( formfieldid );
53
+
54
+ });
55
+
56
+ // Transfer form submissions
57
+ // @since 1.6.3
58
+ $( 'html body' ).on( 'submit', 'form.mycred-transfer-form', function(e){
59
+
60
+ console.log( 'new transfer' );
61
+
62
+ var transferform = $(this);
63
+ var formrefid = transferform.data( 'ref' );
64
+ var formid = '#mycred-transfer-form-' + formrefid;
65
+ var submitbutton = $( formid + ' input.mycred-submit-transfer' );
66
+ var buttonlabel = submitbutton.val();
67
+
68
+ e.preventDefault();
69
+
70
+ $.ajax({
71
+ type : "POST",
72
+ data : {
73
+ action : 'mycred-new-transfer',
74
+ form : transferform.serialize(),
75
+ },
76
+ dataType : "JSON",
77
+ url : myCREDTransfer.ajaxurl,
78
+ beforeSend : function() {
79
+
80
+ $( formid + 'input.form-control' ).each(function(index){
81
+ $(this).attr( 'disabled', 'disabled' );
82
+ });
83
+
84
+ submitbutton.val( myCREDTransfer.working );
85
+
86
+ },
87
+ success : function( response ) {
88
+
89
+ console.log( response );
90
+
91
+ $( formid + ' input.form-control' ).each(function(index){
92
+ $(this).removeAttr( 'disabled' );
93
+ });
94
+
95
+ submitbutton.val( buttonlabel );
96
+
97
+ if ( response.success !== undefined ) {
98
+
99
+ if ( response.success ) {
100
+
101
+ alert( myCREDTransfer.completed );
102
+
103
+ if ( $( response.data.css ) !== undefined )
104
+ $( response.data.css ).empty().html( response.data.balance );
105
+
106
+ $( formid + ' input.form-control' ).each(function(index){
107
+ $(this).val( '' );
108
+ });
109
+
110
+ $( formid + ' select' ).each(function(index){
111
+ var selecteditem = $(this).find( ':selected' );
112
+ if ( selecteditem !== undefined )
113
+ selecteditem.removeAttr( 'selected' );
114
+ });
115
+
116
+ if ( myCREDTransfer.reload == '1' ) location.reload();
117
+
118
+ }
119
+
120
+ else if ( myCREDTransfer[ response.data ] !== undefined )
121
+ alert( myCREDTransfer[ response.data ] );
122
+
123
+ }
124
+
125
+ }
126
+
127
+ });
128
+
129
+ return false;
130
+
131
+ });
132
+
133
+ })( jQuery );
addons/transfer/assets/js/transfer.js DELETED
@@ -1,105 +0,0 @@
1
- /**
2
- * myCRED Transfer jQuery
3
- * Handles transfer requests and autocomplete of recipient search.
4
- *
5
- * @requires jQuery
6
- * @requires jQuery UI
7
- * @requires jQuery Autocomplete
8
- * @since 0.1
9
- * @version 1.5
10
- */
11
- (function($) {
12
-
13
- var mycred_transfer_submit = '';
14
- var mycred_transfer_cache = {};
15
-
16
- $( 'input.mycred-click' ).on( 'click', function(){
17
-
18
- mycred_transfer_submit = $(this);
19
-
20
- });
21
-
22
- // Autocomplete
23
- // @api http://api.jqueryui.com/autocomplete/
24
- $( 'input.mycred-autofill' ).autocomplete({
25
-
26
- minLength: 2,
27
- source: function( request, response ) {
28
-
29
- var term = request.term;
30
- if ( term in mycred_transfer_cache ) {
31
- response( mycred_transfer_cache[ term ] );
32
- return;
33
- }
34
-
35
- var send = {
36
- action : "mycred-autocomplete",
37
- token : myCRED.atoken,
38
- string : request
39
- };
40
-
41
- $.getJSON( myCRED.ajaxurl, send, function( data, status, xhr ) {
42
- mycred_transfer_cache[ term ] = data;
43
- response( data );
44
- });
45
-
46
- },
47
- messages: {
48
- noResults: '',
49
- results: function() {}
50
- },
51
- appendTo : 'div.transfer-to'
52
-
53
- });
54
-
55
- // Transfer form submissions
56
- // @since 1.6.3
57
- $( 'div.mycred-transfer-cred-wrapper' ).on( 'submit', 'form.mycred-transfer', function(e){
58
-
59
- var buttonlabel = mycred_transfer_submit.val();
60
-
61
- e.preventDefault();
62
-
63
- $.ajax({
64
- type : "POST",
65
- data : {
66
- action : 'mycred-transfer-creds',
67
- form : $(this).serialize(),
68
- token : myCRED.token
69
- },
70
- dataType : "JSON",
71
- url : myCRED.ajaxurl,
72
- beforeSend : function() {
73
-
74
- mycred_transfer_submit.attr( 'disabled', 'disabled' ).val( myCRED.working );
75
-
76
- },
77
- success : function( data ) {
78
-
79
- mycred_transfer_submit.removeAttr( 'disabled' ).val( buttonlabel );
80
-
81
- // Error
82
- if ( myCRED[ data ] !== undefined ) {
83
-
84
- alert( myCRED[ data ] );
85
-
86
- }
87
-
88
- // Completed
89
- else if ( data == 'ok' ) {
90
-
91
- alert( myCRED.completed );
92
-
93
- }
94
-
95
- if ( myCRED.reload == '1' ) location.reload();
96
-
97
- }
98
-
99
- });
100
-
101
- return false;
102
-
103
- });
104
-
105
- })( jQuery );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
addons/transfer/includes/mycred-transfer-functions.php CHANGED
@@ -11,61 +11,69 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
11
  * @filter 'mycred_transfer_limit'
12
  * @filter 'mycred_transfer_acc_limit'
13
  * @since 0.1
14
- * @version 1.3.1
15
  */
16
  if ( ! function_exists( 'mycred_user_can_transfer' ) ) :
17
- function mycred_user_can_transfer( $user_id = NULL, $amount = NULL, $type = 'mycred_default', $ref = NULL ) {
18
 
19
- if ( $user_id === NULL ) $user_id = get_current_user_id();
 
20
 
21
- if ( $ref === NULL )
22
- $ref = 'mycred_transactions';
 
 
 
23
 
24
  // Grab Settings (from main type where the settings are saved)
25
- $mycred = mycred();
26
- $pref = $mycred->transfers;
27
- $zero = $mycred->zero();
 
 
 
 
28
 
29
  // Get users balance
30
- $balance = $mycred->get_users_cred( $user_id, $type );
31
 
32
  // Get Transfer Max
33
- $max = apply_filters( 'mycred_transfer_limit', $mycred->number( $pref['limit']['amount'] ), $user_id, $amount, $pref, $mycred );
34
 
35
  // If an amount is given, deduct this amount to see if the transaction
36
  // brings us over the account limit
37
  if ( $amount !== NULL )
38
- $balance = $mycred->number( $balance-$amount );
39
 
40
- // Account Limit
41
  // The lowest amount a user can have on their account. By default, this
42
  // is zero. But you can override this via the mycred_transfer_acc_limit hook.
43
- $account_limit = $mycred->number( apply_filters( 'mycred_transfer_acc_limit', $zero, $type, $user_id, $ref ) );
44
 
45
- // Check if users balance is below the account limit
46
  if ( $balance < $account_limit ) return 'low';
47
 
48
  // If there are no limits, return the current balance
49
- if ( $pref['limit']['limit'] == 'none' ) return $balance;
50
 
51
  // Else we have a limit to impose
52
  $now = current_time( 'timestamp' );
53
- $max = $mycred->number( $pref['limit']['amount'] );
54
 
55
  // Daily limit
56
- if ( $pref['limit']['limit'] == 'daily' )
57
- $total = mycred_get_total_by_time( 'today', 'now', $ref, $user_id, $type );
58
 
59
  // Weekly limit
60
- elseif ( $pref['limit']['limit'] == 'weekly' ) {
61
  $this_week = mktime( 0, 0, 0, date( 'n', $now ), date( 'j', $now ) - date( 'n', $now ) + 1 );
62
- $total = mycred_get_total_by_time( $this_week, 'now', $ref, $user_id, $type );
63
  }
64
 
65
  // Custom limits will need to return the result
66
  // here and now. Accepted answers are 'limit', 'low' or the amount left on limit.
67
  else {
68
- return apply_filters( 'mycred_user_can_transfer', 'limit', $user_id, $amount, $pref, $mycred );
69
  }
70
 
71
  // We are adding up point deducations.
@@ -99,12 +107,12 @@ endif;
99
  * @version 1.0
100
  */
101
  if ( ! function_exists( 'mycred_get_users_transfer_history' ) ) :
102
- function mycred_get_users_transfer_history( $user_id, $type = 'mycred_default', $key = NULL ) {
103
 
104
  if ( $key === NULL )
105
  $key = 'mycred_transactions';
106
 
107
- if ( $type != 'mycred_default' && $type != '' )
108
  $key .= '_' . $type;
109
 
110
  $default = array(
@@ -122,12 +130,12 @@ endif;
122
  * @version 1.0
123
  */
124
  if ( ! function_exists( 'mycred_update_users_transfer_history' ) ) :
125
- function mycred_update_users_transfer_history( $user_id, $history, $type = 'mycred_default', $key = NULL ) {
126
 
127
  if ( $key === NULL )
128
  $key = 'mycred_transactions';
129
 
130
- if ( $type != 'mycred_default' && $type != '' )
131
  $key .= '_' . $type;
132
 
133
  // Get current history
11
  * @filter 'mycred_transfer_limit'
12
  * @filter 'mycred_transfer_acc_limit'
13
  * @since 0.1
14
+ * @version 1.4
15
  */
16
  if ( ! function_exists( 'mycred_user_can_transfer' ) ) :
17
+ function mycred_user_can_transfer( $user_id = NULL, $amount = NULL, $type = MYCRED_DEFAULT_TYPE_KEY, $reference = NULL ) {
18
 
19
+ if ( $user_id === NULL )
20
+ $user_id = get_current_user_id();
21
 
22
+ if ( $reference === NULL )
23
+ $reference = 'transfer';
24
+
25
+ if ( ! mycred_point_type_exists( $type ) )
26
+ $type = MYCRED_DEFAULT_TYPE_KEY;
27
 
28
  // Grab Settings (from main type where the settings are saved)
29
+ $mycred = mycred();
30
+ $settings = $mycred->transfers;
31
+
32
+ if ( $type !== MYCRED_DEFAULT_TYPE_KEY )
33
+ $mycred = mycred( $type );
34
+
35
+ $zero = $mycred->zero();
36
 
37
  // Get users balance
38
+ $balance = $mycred->get_users_balance( $user_id, $type );
39
 
40
  // Get Transfer Max
41
+ $max = apply_filters( 'mycred_transfer_limit', $mycred->number( $settings['limit']['amount'] ), $user_id, $amount, $settings, $reference );
42
 
43
  // If an amount is given, deduct this amount to see if the transaction
44
  // brings us over the account limit
45
  if ( $amount !== NULL )
46
+ $balance = $mycred->number( $balance - $amount );
47
 
48
+ // Zero
49
  // The lowest amount a user can have on their account. By default, this
50
  // is zero. But you can override this via the mycred_transfer_acc_limit hook.
51
+ $account_limit = $mycred->number( apply_filters( 'mycred_transfer_acc_limit', $zero, $type, $user_id, $reference ) );
52
 
53
+ // Check if we would go minus
54
  if ( $balance < $account_limit ) return 'low';
55
 
56
  // If there are no limits, return the current balance
57
+ if ( $settings['limit']['limit'] == 'none' ) return $balance;
58
 
59
  // Else we have a limit to impose
60
  $now = current_time( 'timestamp' );
61
+ $max = $mycred->number( $settings['limit']['amount'] );
62
 
63
  // Daily limit
64
+ if ( $settings['limit']['limit'] == 'daily' )
65
+ $total = mycred_get_total_by_time( 'today', 'now', $reference, $user_id, $type );
66
 
67
  // Weekly limit
68
+ elseif ( $settings['limit']['limit'] == 'weekly' ) {
69
  $this_week = mktime( 0, 0, 0, date( 'n', $now ), date( 'j', $now ) - date( 'n', $now ) + 1 );
70
+ $total = mycred_get_total_by_time( $this_week, 'now', $reference, $user_id, $type );
71
  }
72
 
73
  // Custom limits will need to return the result
74
  // here and now. Accepted answers are 'limit', 'low' or the amount left on limit.
75
  else {
76
+ return apply_filters( 'mycred_user_can_transfer', 'limit', $user_id, $amount, $settings, $reference );
77
  }
78
 
79
  // We are adding up point deducations.
107
  * @version 1.0
108
  */
109
  if ( ! function_exists( 'mycred_get_users_transfer_history' ) ) :
110
+ function mycred_get_users_transfer_history( $user_id, $type = MYCRED_DEFAULT_TYPE_KEY, $key = NULL ) {
111
 
112
  if ( $key === NULL )
113
  $key = 'mycred_transactions';
114
 
115
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY && $type != '' )
116
  $key .= '_' . $type;
117
 
118
  $default = array(
130
  * @version 1.0
131
  */
132
  if ( ! function_exists( 'mycred_update_users_transfer_history' ) ) :
133
+ function mycred_update_users_transfer_history( $user_id, $history, $type = MYCRED_DEFAULT_TYPE_KEY, $key = NULL ) {
134
 
135
  if ( $key === NULL )
136
  $key = 'mycred_transactions';
137
 
138
+ if ( $type != MYCRED_DEFAULT_TYPE_KEY && $type != '' )
139
  $key .= '_' . $type;
140
 
141
  // Get current history
addons/transfer/includes/mycred-transfer-shortcodes.php CHANGED
@@ -3,59 +3,52 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
3
 
4
  /**
5
  * Transfer Shortcode Render
 
6
  * @see http://mycred.me/functions/mycred_transfer_render/
7
- * @attribute $charge_from (int) optional user ID from whom the points to be deducted, defaults to current user
8
- * @attribute $pay_to (int) optional user ID to whom the transfer is made, if left empty the user will be able to search for a user
9
- * @attribute $show_balance (bool) set to true to show current users balance, defaults to true
10
- * @attribute $show_limit (bool) set to true to show current users limit. If limit is set to 'none' and $show_limit is set to true nothing will be returned
11
  * @since 0.1
12
- * @version 1.5.1
13
  */
14
  if ( ! function_exists( 'mycred_transfer_render' ) ) :
15
  function mycred_transfer_render( $atts, $content = NULL ) {
16
 
17
- global $mycred_load_transfer;
18
 
19
  // Settings
20
  $mycred = mycred();
21
  $pref = $mycred->transfers;
22
- $user_id = get_current_user_id();
23
 
24
  // Get Attributes
25
  extract( shortcode_atts( array(
26
- 'button' => '',
27
- 'charge_from' => $user_id,
28
- 'pay_to' => '',
29
- 'show_balance' => 0,
30
- 'show_limit' => 0,
31
- 'ref' => 'transfer',
32
- 'placeholder' => '',
33
- 'types' => $pref['types'],
34
- 'excluded' => ''
 
 
 
35
  ), $atts ) );
36
 
37
  $output = '';
38
- $mycred_load_transfer = false;
 
 
39
 
40
  // If we are not logged in
41
  if ( ! is_user_logged_in() ) {
42
 
43
  if ( isset( $pref['templates']['login'] ) && ! empty( $pref['templates']['login'] ) )
44
- $output .= '<p class="mycred-transfer-login">' . $mycred->template_tags_general( $pref['templates']['login'] ) . '</p>';
45
 
46
- return $output;
47
 
48
  }
49
 
50
- // Who to charge
51
- $charge_other = false;
52
- if ( $charge_from != $user_id ) {
53
- $charge_other = true;
54
- $charge_from = apply_filters( 'mycred_transfer_charge_from', $charge_from, $prefs, $atts );
55
- }
56
- else {
57
- $charge_from = $user_id;
58
- }
59
 
60
  // Point Types
61
  if ( ! is_array( $types ) )
@@ -72,16 +65,18 @@ if ( ! function_exists( 'mycred_transfer_render' ) ) :
72
 
73
  }
74
 
75
- $available_types = array();
 
76
 
77
  // Default
78
- if ( count( $clean ) == 1 && in_array( 'mycred_default', $clean ) ) {
79
 
80
  // Make sure user is not excluded
81
- if ( $mycred->exclude_user( $charge_from ) ) return '';
82
 
83
- $status = mycred_user_can_transfer( $charge_from, NULL, 'mycred_default', $ref );
84
- $my_balance = $mycred->get_users_cred( $charge_from );
 
85
 
86
  // Error. Not enough creds
87
  if ( $status === 'low' ) {
@@ -93,7 +88,7 @@ if ( ! function_exists( 'mycred_transfer_render' ) ) :
93
  $output .= '<p class="mycred-transfer-low">' . $mycred->template_tags_general( $no_cred ) . '</p>';
94
  }
95
 
96
- return $output;
97
 
98
  }
99
 
@@ -107,11 +102,15 @@ if ( ! function_exists( 'mycred_transfer_render' ) ) :
107
  $output .= '<p class="mycred-transfer-over">' . $mycred->template_tags_general( $no_cred ) . '</p>';
108
  }
109
 
110
- return $output;
111
 
112
  }
113
 
114
- $available_types['mycred_default'] = $mycred->plural();
 
 
 
 
115
 
116
  }
117
 
@@ -123,46 +122,57 @@ if ( ! function_exists( 'mycred_transfer_render' ) ) :
123
  $points = mycred( $point_type );
124
  if ( $points->exclude_user( $charge_from ) ) continue;
125
 
126
- $status = mycred_user_can_transfer( $charge_from, NULL, $point_type, $ref );
 
127
  if ( $status === 'low' || $status === 'limit' ) continue;
128
 
129
- $available_types[ $point_type ] = $points->plural();
 
 
 
 
 
 
130
 
131
  }
132
 
133
  // User does not have access
134
  if ( count( $available_types ) == 0 )
135
- return $excluded;
136
 
137
  }
138
 
139
  // Flag for scripts & styles
140
- $mycred_load_transfer = true;
141
 
142
  // Placeholder
143
- if ( $pref['autofill'] == 'user_login' )
144
- $pln = __( 'username', 'mycred' );
 
 
 
 
 
145
 
146
- elseif ( $pref['autofill'] == 'user_email' )
147
- $pln = __( 'email', 'mycred' );
148
 
149
- $placeholder = apply_filters( 'mycred_transfer_to_placeholder', __( 'recipients %s', 'mycred' ), $pref, $mycred );
150
- $placeholder = sprintf( $placeholder, $pln );
151
 
152
  // Recipient Input field
153
- $to_input = '<input type="text" name="mycred-transfer-to" value="" aria-required="true" class="mycred-autofill" placeholder="' . $placeholder . '" />';
154
 
155
  // If recipient is set, pre-populate it with the recipients details
156
  if ( $pay_to != '' ) {
157
 
158
- $user = get_user_by( 'id', $pay_to );
 
159
  if ( $user !== false ) {
160
 
161
  $value = $user->display_name;
162
  if ( isset( $user->$pref['autofill'] ) )
163
  $value = $user->$pref['autofill'];
164
 
165
- $to_input = '<input type="text" name="mycred-transfer-to" value="' . $value . '" readonly="readonly" />';
166
 
167
  }
168
 
@@ -173,48 +183,18 @@ if ( ! function_exists( 'mycred_transfer_render' ) ) :
173
  if ( count( $clean ) == 1 )
174
  $mycred = mycred( $clean[0] );
175
 
176
- // Only use prefix / suffix if we have 1 type.
177
- if ( count( $clean ) == 1 ) {
178
-
179
- if ( ! empty( $mycred->before ) )
180
- $before = $mycred->before . ' ';
181
-
182
- else
183
- $before = '';
184
-
185
- if ( ! empty( $mycred->after ) )
186
- $after = ' ' . $mycred->after;
187
-
188
- else
189
- $after = '';
190
-
191
- }
192
-
193
- else $before = $after = '';
194
-
195
- // Select Point type
196
- if ( count( $available_types ) == 1 )
197
- $type_input = '<input type="hidden" name="mycred-transfer-type" value="' . $clean[0] . '" />';
198
-
199
- else {
200
 
201
- $type_input = '<select name="mycred-transfer-type" id="mycred-transfer-type" class="form-control">';
202
- foreach ( $available_types as $type => $plural ) {
203
- $type_input .= '<option value="' . $type . '">' . $plural . '</option>';
 
204
  }
205
- $type_input .= '</select>';
206
 
207
  }
208
 
209
- $extras = array();
210
-
211
- // Show Balance
212
- if ( (bool) $show_balance === true && ! empty( $pref['templates']['balance'] ) && count( $available_types ) == 1 ) {
213
-
214
- $balance_text = str_replace( '%balance%', $mycred->format_creds( $my_balance ), $pref['templates']['balance'] );
215
- $extras[] = $mycred->template_tags_general( $balance_text );
216
-
217
- }
218
 
219
  // Show Limits
220
  if ( (bool) $show_limit === true && ! empty( $pref['templates']['limit'] ) && $pref['limit']['limit'] != 'none' && count( $available_types ) == 1 ) {
@@ -234,45 +214,117 @@ if ( ! function_exists( 'mycred_transfer_render' ) ) :
234
 
235
  ?>
236
  <div class="mycred-transfer-cred-wrapper"<?php if ( $ref != '' ) echo ' id="transfer-form-' . $ref . '"'; ?>>
237
- <form class="mycred-transfer" method="post" action="">
238
 
239
  <?php do_action( 'mycred_transfer_form_start', $atts, $pref ); ?>
240
 
241
- <ol style="list-style-type:none;">
242
- <li class="mycred-send-to">
243
- <label><?php _e( 'To:', 'mycred' ); ?></label>
244
- <div class="transfer-to"><?php echo $to_input; ?></div>
 
 
 
245
  <?php do_action( 'mycred_transfer_form_to', $atts, $pref ); ?>
 
246
 
247
- </li>
248
- <li class="mycred-send-amount">
249
- <label><?php _e( 'Amount:', 'mycred' ); ?></label>
250
- <div class="transfer-amount"><?php echo $before; ?><input type="text" class="short" name="mycred-transfer-amount" value="<?php echo $mycred->zero(); ?>" size="8" aria-required="true" /><?php echo $after . ' ' . $type_input; ?></div>
251
- <?php if ( $charge_other ) : ?><input type="hidden" name="mycred-sender" value="<?php echo absint( $charge_from ); ?>" /><?php endif; ?>
252
- <?php if ( $ref != '' ) : ?><input type="hidden" name="mycred-transfer-ref" value="<?php echo esc_attr( $ref ); ?>" /><?php endif; ?>
253
- <input type="submit" class="button button-primary button-large mycred-click btn btn-primary btn-lg"<?php if ( $pay_to == get_current_user_id() ) echo ' disabled="disabled"'; ?> value="<?php echo esc_attr( $button ); ?>" />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
 
 
 
 
 
 
 
 
 
 
 
255
  <?php do_action( 'mycred_transfer_form_amount', $atts, $pref ); ?>
 
 
 
 
 
 
 
 
 
 
 
 
256
 
257
- </li>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
 
259
- <?php if ( ! empty( $extras ) ) { ?>
260
 
261
- <li class="mycred-transfer-info">
262
- <p><?php echo implode( '</p><p>', $extras ); ?></p>
263
- <?php do_action( 'mycred_transfer_form_extra', $atts, $pref ); ?>
264
 
265
- </li>
 
 
 
 
 
 
 
 
 
 
266
 
267
- <?php } ?>
 
 
 
 
268
 
269
- </ol>
270
 
271
  <?php do_action( 'mycred_transfer_form_end', $atts, $pref ); ?>
272
 
273
- <div class="clear clearfix"></div>
274
  </form>
275
- <div class="clear clearfix clr"></div>
276
  </div>
277
  <?php
278
 
3
 
4
  /**
5
  * Transfer Shortcode Render
6
+ * Renders a transfer form that allows users to send points to other users.
7
  * @see http://mycred.me/functions/mycred_transfer_render/
 
 
 
 
8
  * @since 0.1
9
+ * @version 1.6
10
  */
11
  if ( ! function_exists( 'mycred_transfer_render' ) ) :
12
  function mycred_transfer_render( $atts, $content = NULL ) {
13
 
14
+ global $mycred_do_transfer;
15
 
16
  // Settings
17
  $mycred = mycred();
18
  $pref = $mycred->transfers;
 
19
 
20
  // Get Attributes
21
  extract( shortcode_atts( array(
22
+ 'button' => '',
23
+ 'pay_to' => '',
24
+ 'show_balance' => 0,
25
+ 'show_limit' => 0,
26
+ 'ref' => 'transfer',
27
+ 'amount' => '',
28
+ 'placeholder' => '',
29
+ 'types' => $pref['types'],
30
+ 'excluded' => '',
31
+ 'recipient_label' => __( 'Recipient', 'mycred' ),
32
+ 'amount_label' => __( 'Amount', 'mycred' ),
33
+ 'balance_label' => __( 'Balance', 'mycred' )
34
  ), $atts ) );
35
 
36
  $output = '';
37
+
38
+ if ( $ref == '' )
39
+ $ref = 'transfer';
40
 
41
  // If we are not logged in
42
  if ( ! is_user_logged_in() ) {
43
 
44
  if ( isset( $pref['templates']['login'] ) && ! empty( $pref['templates']['login'] ) )
45
+ $content .= '<p class="mycred-transfer-login">' . $mycred->template_tags_general( $pref['templates']['login'] ) . '</p>';
46
 
47
+ return do_shortcode( $content );
48
 
49
  }
50
 
51
+ $charge_from = get_current_user_id();
 
 
 
 
 
 
 
 
52
 
53
  // Point Types
54
  if ( ! is_array( $types ) )
65
 
66
  }
67
 
68
+ $available_types = array();
69
+ $available_balances = array();
70
 
71
  // Default
72
+ if ( count( $clean ) == 1 && in_array( MYCRED_DEFAULT_TYPE_KEY, $clean ) ) {
73
 
74
  // Make sure user is not excluded
75
+ if ( $mycred->exclude_user( $charge_from ) ) return '<div class="row"><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>' . $excluded . '</p></div></div>';
76
 
77
+ // See if we can send the lowest value
78
+ $status = mycred_user_can_transfer( $charge_from, $mycred->get_lowest_value(), MYCRED_DEFAULT_TYPE_KEY, $ref );
79
+ $balance = $mycred->get_users_balance( $charge_from );
80
 
81
  // Error. Not enough creds
82
  if ( $status === 'low' ) {
88
  $output .= '<p class="mycred-transfer-low">' . $mycred->template_tags_general( $no_cred ) . '</p>';
89
  }
90
 
91
+ return do_shortcode( $output );
92
 
93
  }
94
 
102
  $output .= '<p class="mycred-transfer-over">' . $mycred->template_tags_general( $no_cred ) . '</p>';
103
  }
104
 
105
+ return do_shortcode( $output );
106
 
107
  }
108
 
109
+ $available_types[ MYCRED_DEFAULT_TYPE_KEY ] = $mycred->plural();
110
+ $balance_template = $pref['templates']['balance'];
111
+ $balance_template = str_replace( '%cred%', '<span class="mycred-balance-' . MYCRED_DEFAULT_TYPE_KEY . '">' . $balance . '</span>', $balance_template );
112
+ $balance_template = str_replace( array( '%cred_f%', '%balance%' ), '<span class="mycred-balance-' . MYCRED_DEFAULT_TYPE_KEY . '">' . $mycred->format_creds( $balance ) . '</span>', $balance_template );
113
+ $available_balances[ MYCRED_DEFAULT_TYPE_KEY ] = $mycred->template_tags_general( $balance_template );
114
 
115
  }
116
 
122
  $points = mycred( $point_type );
123
  if ( $points->exclude_user( $charge_from ) ) continue;
124
 
125
+ // See if we can send the lowest value
126
+ $status = mycred_user_can_transfer( $charge_from, $points->get_lowest_value(), $point_type, $ref );
127
  if ( $status === 'low' || $status === 'limit' ) continue;
128
 
129
+ $balance = $points->get_users_balance( $charge_from );
130
+
131
+ $available_types[ $point_type ] = $points->plural();
132
+ $balance_template = $pref['templates']['balance'];
133
+ $balance_template = str_replace( '%cred%', '<span class="mycred-balance-' . $point_type . '">' . $balance . '</span>', $balance_template );
134
+ $balance_template = str_replace( array( '%cred_f%', '%balance%' ), '<span class="mycred-balance-' . $point_type . '">' . $points->format_creds( $balance ) . '</span>', $balance_template );
135
+ $available_balances[ $point_type ] = $points->template_tags_general( $balance_template );
136
 
137
  }
138
 
139
  // User does not have access
140
  if ( count( $available_types ) == 0 )
141
+ return '<div class="row"><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>' . $excluded . '</p></div></div>';
142
 
143
  }
144
 
145
  // Flag for scripts & styles
146
+ $mycred_do_transfer = true;
147
 
148
  // Placeholder
149
+ if ( $placeholder == '' ) {
150
+
151
+ if ( $pref['autofill'] == 'user_login' )
152
+ $pln = __( 'username', 'mycred' );
153
+
154
+ elseif ( $pref['autofill'] == 'user_email' )
155
+ $pln = __( 'email', 'mycred' );
156
 
157
+ $placeholder = sprintf( apply_filters( 'mycred_transfer_to_placeholder', __( 'recipients %s', 'mycred' ), $pref, $atts ), $pln );
 
158
 
159
+ }
 
160
 
161
  // Recipient Input field
162
+ $to_input = '<input type="text" name="mycred_new_transfer[recipient_id]" value="" aria-required="true" class="mycred-autofill form-control" data-form="' . $ref . '" placeholder="' . $placeholder . '" />';
163
 
164
  // If recipient is set, pre-populate it with the recipients details
165
  if ( $pay_to != '' ) {
166
 
167
+ $pay_to = mycred_get_user_id( $pay_to );
168
+ $user = get_userdata( $pay_to );
169
  if ( $user !== false ) {
170
 
171
  $value = $user->display_name;
172
  if ( isset( $user->$pref['autofill'] ) )
173
  $value = $user->$pref['autofill'];
174
 
175
+ $to_input = '<p class="form-control-static">' . $value . '</p><input type="hidden" name="mycred_new_transfer[recipient_id]" value="' . $pay_to . '" />';
176
 
177
  }
178
 
183
  if ( count( $clean ) == 1 )
184
  $mycred = mycred( $clean[0] );
185
 
186
+ // Show Balance
187
+ $extras = array();
188
+ if ( (bool) $show_balance && ! empty( $pref['templates']['balance'] ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
 
190
+ if ( ! empty( $available_balances ) ) {
191
+ foreach ( $available_balances as $balance_type => $balance ) {
192
+ $extras[] = $balance;
193
+ }
194
  }
 
195
 
196
  }
197
 
 
 
 
 
 
 
 
 
 
198
 
199
  // Show Limits
200
  if ( (bool) $show_limit === true && ! empty( $pref['templates']['limit'] ) && $pref['limit']['limit'] != 'none' && count( $available_types ) == 1 ) {
214
 
215
  ?>
216
  <div class="mycred-transfer-cred-wrapper"<?php if ( $ref != '' ) echo ' id="transfer-form-' . $ref . '"'; ?>>
217
+ <form class="form mycred-transfer mycred-transfer-form" id="mycred-transfer-form-<?php echo esc_attr( $ref ); ?>" method="post" data-ref="<?php echo esc_attr( $ref ); ?>" action="">
218
 
219
  <?php do_action( 'mycred_transfer_form_start', $atts, $pref ); ?>
220
 
221
+ <div class="row">
222
+
223
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
224
+ <div class="form-group select-recipient-wrapper">
225
+ <?php if ( $recipient_label != '' ) : ?><label><?php echo $recipient_label; ?>:</label><?php endif; ?>
226
+ <?php echo $to_input; ?>
227
+ </div>
228
  <?php do_action( 'mycred_transfer_form_to', $atts, $pref ); ?>
229
+ </div>
230
 
231
+ <?php
232
+
233
+ if ( count( $available_types ) == 1 ) {
234
+
235
+ ?>
236
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
237
+ <div class="form-group select-amount-wrapper">
238
+ <?php if ( $amount_label != '' ) : ?><label><?php echo $amount_label; ?>:</label><?php endif; ?>
239
+ <?php if ( $amount == '' ) : ?>
240
+ <input type="text" name="mycred_new_transfer[amount]" placeholder="<?php echo $mycred->zero(); ?>" class="form-control" value="" />
241
+ <?php else : ?>
242
+ <p class="form-control-static"><?php echo $mycred->format_creds( $amount ); ?></p>
243
+ <?php endif; ?>
244
+ </div>
245
+ <?php do_action( 'mycred_transfer_form_amount', $atts, $pref ); ?>
246
+ </div>
247
+ <?php
248
+
249
+ }
250
+
251
+ else {
252
 
253
+ ?>
254
+ <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
255
+ <div class="form-group select-amount-wrapper">
256
+ <?php if ( $amount_label != '' ) : ?><label><?php echo $amount_label; ?>:</label><?php endif; ?>
257
+ <?php if ( $amount == '' ) : ?>
258
+ <input type="text" name="mycred_new_transfer[amount]" placeholder="<?php echo $mycred->zero(); ?>" class="form-control" value="" />
259
+ <?php else : ?>
260
+ <p class="form-control-static"><?php echo $mycred->format_creds( $amount ); ?></p>
261
+ <?php endif; ?>
262
+ </div>
263
  <?php do_action( 'mycred_transfer_form_amount', $atts, $pref ); ?>
264
+ </div>
265
+ <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
266
+ <div class="form-group select-point-type-wrapper">
267
+ <?php if ( $balance_label != '' ) : ?><label><?php echo $balance_label; ?>:</label><?php endif; ?>
268
+ <select name="mycred_new_transfer[ctype]" class="form-control">
269
+ <?php foreach ( $available_types as $type => $plural ) echo '<option value="' . $type . '">' . $plural . '</option>'; ?>
270
+ </select>
271
+ </div>
272
+ </div>
273
+ <?php
274
+
275
+ }
276
 
277
+ ?>
278
+
279
+ </div>
280
+ <?php
281
+
282
+ // Show extras
283
+ if ( ! empty( $extras ) ) {
284
+
285
+ $extras_to_show = count( $extras );
286
+ $col = 'col-lg-12 col-md-12 col-sm-12 col-xs-12';
287
+ if ( $extras_to_show == 2 )
288
+ $col = 'col-lg-6 col-md-6 col-sm-12 col-xs-12';
289
+ elseif ( $extras_to_show == 3 )
290
+ $col = 'col-lg-4 col-md-4 col-sm-12 col-xs-12';
291
+ elseif ( $extras_to_show == 4 )
292
+ $col = 'col-lg-3 col-md-3 col-sm-12 col-xs-12';
293
+ elseif ( $extras_to_show > 4 )
294
+ $col = 'col-lg-2 col-md-2 col-sm-12 col-xs-12';
295
+
296
+ ?>
297
+ <div class="row">
298
 
299
+ <?php foreach ( $extras as $extra_content ) { ?>
300
 
301
+ <div class="<?php echo $col; ?>">
302
+ <?php echo do_shortcode( $extra_content ); ?>
303
+ </div>
304
 
305
+ <?php } ?>
306
+
307
+ </div>
308
+ <?php
309
+
310
+ }
311
+
312
+ do_action( 'mycred_transfer_form_extra', $atts, $pref );
313
+
314
+ ?>
315
+ <div class="row">
316
 
317
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
318
+ <input type="hidden" name="mycred_new_transfer[token]" value="<?php echo wp_create_nonce( 'mycred-new-transfer-' . $ref ); ?>" />
319
+ <input type="hidden" name="mycred_new_transfer[reference]" value="<?php echo esc_attr( $ref ); ?>" />
320
+ <input type="submit" class="btn btn-primary btn-block btn-lg mycred-submit-transfer" value="<?php echo esc_attr( $button ); ?>" />
321
+ </div>
322
 
323
+ </div>
324
 
325
  <?php do_action( 'mycred_transfer_form_end', $atts, $pref ); ?>
326
 
 
327
  </form>
 
328
  </div>
329
  <?php
330
 
addons/transfer/includes/mycred-transfer-widgets.php CHANGED
@@ -4,7 +4,7 @@ if ( ! defined( 'myCRED_VERSION' ) ) exit;
4
  /**
5
  * Widget: myCRED Transfer
6
  * @since 0.1
7
- * @version 1.1.2
8
  */
9
  if ( ! class_exists( 'myCRED_Widget_Transfer' ) ) :
10
  class myCRED_Widget_Transfer extends WP_Widget {
@@ -34,72 +34,29 @@ if ( ! class_exists( 'myCRED_Widget_Transfer' ) ) :
34
 
35
  extract( $args, EXTR_SKIP );
36
 
37
- // Prep
38
- $title = $instance['title'];
39
- $mycred = mycred();
40
-
41
- if ( ! isset( $mycred->transfers ) )
42
- return '<p>' . __( 'The myCRED Transfer add-on has not yet been setup!', 'mycred' ) . '</p>';
43
-
44
- $pref = $mycred->transfers;
45
-
46
- global $mycred_load_transfer;
47
-
48
- // Members
49
- if ( is_user_logged_in() ) {
50
-
51
- // Excluded users
52
- $user_id = get_current_user_id();
53
- if ( $mycred->exclude_user( $user_id ) ) return;
54
-
55
- echo $before_widget;
56
-
57
- // Title
58
- if ( ! empty( $title ) ) {
59
- echo $before_title;
60
- echo $mycred->template_tags_general( $title );
61
- echo $after_title;
62
- }
63
-
64
- // Prep shortcode
65
- $attr = array(
66
- 'show_balance' => $instance['show_balance'],
67
- 'show_limit' => $instance['show_limit']
68
- );
69
-
70
- if ( isset( $instance['button'] ) && ! empty( $instance['button'] ) )
71
- $attr['button'] = $instance['button'];
72
-
73
- echo mycred_transfer_render( $attr, '' );
74
-
75
- $mycred_load_transfer = true;
76
- echo $after_widget;
77
 
 
 
 
 
 
78
  }
79
 
80
- // Visitors
81
- else {
82
-
83
- $mycred_load = false;
84
-
85
- // If login message is set
86
- if ( ! empty( $pref['templates']['login'] ) ) {
87
-
88
- echo $before_widget;
89
- if ( ! empty( $instance['title'] ) ) {
90
- echo $before_title;
91
- echo $mycred->template_tags_general( $title );
92
- echo $after_title;
93
- }
94
-
95
- // Show login message
96
- echo '<p>' . $mycred->template_tags_general( $pref['templates']['login'] ) . '</p>';
97
- echo $after_widget;
98
-
99
- }
100
- return;
101
-
102
- }
103
 
104
  }
105
 
@@ -113,6 +70,12 @@ if ( ! class_exists( 'myCRED_Widget_Transfer' ) ) :
113
  $show_balance = isset( $instance['show_balance'] ) ? $instance['show_balance'] : 0;
114
  $show_limit = isset( $instance['show_limit'] ) ? $instance['show_balance'] : 0;
115
  $button = isset( $instance['button'] ) ? esc_attr( $instance['button'] ) : 'Transfer';
 
 
 
 
 
 
116
 
117
  ?>
118
  <!-- Widget Options -->
@@ -121,17 +84,39 @@ if ( ! class_exists( 'myCRED_Widget_Transfer' ) ) :
121
  <input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" class="widefat" />
122
  </p>
123
  <p class="myCRED-widget-field">
124
- <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_balance' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_balance' ) ); ?>" value="1"<?php checked( $show_balance, true ); ?> class="checkbox" />
125
- <label for="<?php echo esc_attr( $this->get_field_id( 'show_balance' ) ); ?>"><?php _e( 'Show users balance', 'mycred' ); ?></label>
126
  </p>
127
  <p class="myCRED-widget-field">
128
- <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_limit' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_limit' ) ); ?>" value="1"<?php checked( $show_balance, true ); ?> class="checkbox" />
129
- <label for="<?php echo esc_attr( $this->get_field_id( 'show_limit' ) ); ?>"><?php _e( 'Show users limit', 'mycred' ); ?></label>
130
  </p>
131
  <p class="myCRED-widget-field">
132
  <label for="<?php echo esc_attr( $this->get_field_id( 'button' ) ); ?>"><?php _e( 'Button Label', 'mycred' ); ?>:</label>
133
  <input id="<?php echo esc_attr( $this->get_field_id( 'button' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'button' ) ); ?>" type="text" value="<?php echo esc_attr( $button ); ?>" class="widefat" />
134
  </p>
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  /**
5
  * Widget: myCRED Transfer
6
  * @since 0.1
7
+ * @version 1.2
8
  */
9
  if ( ! class_exists( 'myCRED_Widget_Transfer' ) ) :
10
  class myCRED_Widget_Transfer extends WP_Widget {
34
 
35
  extract( $args, EXTR_SKIP );
36
 
37
+ echo $before_widget;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ // Title
40
+ if ( ! empty( $instance['title'] ) ) {
41
+ echo $before_title;
42
+ echo $instance['title'];
43
+ echo $after_title;
44
  }
45
 
46
+ // Let the shortcode to the job
47
+ echo mycred_transfer_render( array(
48
+ 'button' => $instance['button'],
49
+ 'pay_to' => $instance['pay_to'],
50
+ 'show_balance' => $instance['show_balance'],
51
+ 'show_limit' => $instance['show_limit'],
52
+ 'ref' => $instance['reference'],
53
+ 'amount' => $instance['amount'],
54
+ 'excluded' => $instance['excluded'],
55
+ 'types' => $instance['types'],
56
+ 'placeholder' => $instance['placeholder']
57
+ ) );
58
+
59
+ echo $after_widget;
 
 
 
 
 
 
 
 
 
60
 
61
  }
62
 
70
  $show_balance = isset( $instance['show_balance'] ) ? $instance['show_balance'] : 0;
71
  $show_limit = isset( $instance['show_limit'] ) ? $instance['show_balance'] : 0;
72
  $button = isset( $instance['button'] ) ? esc_attr( $instance['button'] ) : 'Transfer';
73
+ $amount = isset( $instance['amount'] ) ? esc_attr( $instance['amount'] ) : '';
74
+ $reference = isset( $instance['reference'] ) ? esc_attr( $instance['reference'] ) : 'transfer';
75
+ $recipient = isset( $instance['pay_to'] ) ? esc_attr( $instance['pay_to'] ) : '';
76
+ $point_types = isset( $instance['types'] ) ? esc_attr( $instance['types'] ) : '';
77
+ $excluded = isset( $instance['excluded'] ) ? esc_attr( $instance['excluded'] ) : '';
78
+ $placeholder = isset( $instance['placeholder'] ) ? esc_attr( $instance['placeholder'] ) : '';
79
 
80
  ?>
81
  <!-- Widget Options -->
84
  <input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" class="widefat" />
85
  </p>
86
  <p class="myCRED-widget-field">
87
+ <label for="<?php echo esc_attr( $this->get_field_id( 'show_balance' ) ); ?>"><input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_balance' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_balance' ) ); ?>" value="1"<?php checked( $show_balance, true ); ?> class="checkbox" /> <?php _e( 'Show users balance', 'mycred' ); ?></label>
 
88
  </p>
89
  <p class="myCRED-widget-field">
90
+ <label for="<?php echo esc_attr( $this->get_field_id( 'show_limit' ) ); ?>"><input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_limit' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_limit' ) ); ?>" value="1"<?php checked( $show_balance, true ); ?> class="checkbox" /> <?php _e( 'Show users limit', 'mycred' ); ?></label>
 
91
  </p>
92
  <p class="myCRED-widget-field">
93
  <label for="<?php echo esc_attr( $this->get_field_id( 'button' ) ); ?>"><?php _e( 'Button Label', 'mycred' ); ?>:</label>
94
  <input id="<?php echo esc_attr( $this->get_field_id( 'button' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'button' ) ); ?>" type="text" value="<?php echo esc_attr( $button ); ?>" class="widefat" />
95
  </p>
96
+ <p class="myCRED-widget-field">
97
+ <label for="<?php echo esc_attr( $this->get_field_id( 'amount' ) ); ?>"><?php _e( 'Amount', 'mycred' ); ?>:</label>
98
+ <input id="<?php echo esc_attr( $this->get_field_id( 'amount' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'amount' ) ); ?>" type="text" value="<?php echo esc_attr( $amount ); ?>" class="widefat" />
99
+ </p>
100
+ <p class="myCRED-widget-field">
101
+ <label for="<?php echo esc_attr( $this->get_field_id( 'reference' ) ); ?>"><?php _e( 'Reference', 'mycred' ); ?>:</label>
102
+ <input id="<?php echo esc_attr( $this->get_field_id( 'reference' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'reference' ) ); ?>" type="text" value="<?php echo esc_attr( $reference ); ?>" class="widefat" />
103
+ </p>
104
+ <p class="myCRED-widget-field">
105
+ <label for="<?php echo esc_attr( $this->get_field_id( 'pay_to' ) ); ?>"><?php _e( 'Recipient', 'mycred' ); ?>:</label>
106
+ <input id="<?php echo esc_attr( $this->get_field_id( 'pay_to' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'pay_to' ) ); ?>" type="text" value="<?php echo esc_attr( $recipient ); ?>" class="widefat" />
107
+ </p>
108
+ <p class="myCR