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