award-badge-to-user.php
1 year ago
award-points-to-user.php
10 months ago
award-rank-to-user.php
1 year ago
revoke-badge-from-user.php
10 months ago
revoke-points-from-user.php
2 years ago
revoke-points-from-user.php
111 lines
| 1 | <?php |
| 2 | /** |
| 3 | * RevokePointsFromUser. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category RevokePointsFromUser |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\MyCred\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Integrations\WordPress\WordPress; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * RevokePointsFromUser |
| 22 | * |
| 23 | * @category RevokePointsFromUser |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class RevokePointsFromUser extends AutomateAction { |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'MyCred'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'revoke_points_from_user'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | |
| 57 | $actions[ $this->integration ][ $this->action ] = [ |
| 58 | 'label' => __( 'Revoke Badge', 'suretriggers' ), |
| 59 | 'action' => $this->action, |
| 60 | 'function' => [ $this, 'action_listener' ], |
| 61 | ]; |
| 62 | |
| 63 | return $actions; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Action listener. |
| 70 | * |
| 71 | * @param int $user_id user_id. |
| 72 | * @param int $automation_id automation_id. |
| 73 | * @param array $fields fields. |
| 74 | * @param array $selected_options selectedOptions. |
| 75 | * |
| 76 | * @return array|bool |
| 77 | */ |
| 78 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 79 | |
| 80 | $point_type = $selected_options['point_type']; |
| 81 | $points = $selected_options['points']; |
| 82 | |
| 83 | if ( empty( $point_type ) || empty( $user_id ) || ! function_exists( 'mycred_get_types' ) || ! function_exists( 'mycred_subtract' ) || ! function_exists( 'mycred_add' ) ) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | $description = ! empty( $selected_options['description'] ) ? $selected_options['description'] : __( 'Awarded by SureTriggers', 'suretriggers' ); |
| 88 | |
| 89 | if ( '-1' == $point_type ) { |
| 90 | $point_types = mycred_get_types(); |
| 91 | if ( is_array( $point_types ) && ! empty( $point_types ) ) { |
| 92 | foreach ( $point_types as $key => $value ) { |
| 93 | mycred_subtract( $value, absint( $user_id ), absint( - $points ), $description, '', '', $key ); |
| 94 | } |
| 95 | } |
| 96 | } else { |
| 97 | mycred_subtract( 'Points', absint( $user_id ), absint( - $points ), $description, '', '', $point_type ); |
| 98 | } |
| 99 | return array_merge( |
| 100 | WordPress::get_user_context( $user_id ), |
| 101 | [ |
| 102 | 'points' => $points, |
| 103 | 'point_type' => $point_type, |
| 104 | 'description' => $description, |
| 105 | ] |
| 106 | ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | RevokePointsFromUser::get_instance(); |
| 111 |