| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Points |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\GamiPress\Actions\User_Points |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_GamiPress_User_Points extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'gamipress'; |
| 15 |
public $action = 'gamipress_user_points'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_action( $this->action, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'Award points to user', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Award <strong>points</strong> to user', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Points amount. %2$s: Post title. %3$s: User. */ |
| 29 |
'edit_label' => sprintf( __( 'Award %1$s %2$s to %3$s', 'automatorwp' ), '{points}', '{points_type}', '{user}' ), |
| 30 |
/* translators: %1$s: Points amount. %2$s: Post title. %3$s: User. */ |
| 31 |
'log_label' => sprintf( __( 'Award %1$s %2$s to %3$s', 'automatorwp' ), '{points}', '{points_type}', '{user}' ), |
| 32 |
'options' => array( |
| 33 |
'points' => array( |
| 34 |
'from' => 'points', |
| 35 |
'fields' => array( |
| 36 |
'points' => array( |
| 37 |
'name' => __( 'Points amount:', 'automatorwp' ), |
| 38 |
'type' => 'text', |
| 39 |
'default' => '1' |
| 40 |
), |
| 41 |
'earning_text' => array( |
| 42 |
'name' => __( 'User earning text:', 'automatorwp' ), |
| 43 |
'desc' => __( 'Enter the text for the user earning entry. Leave blank to do not register a new user earning entry.', 'automatorwp' ), |
| 44 |
'type' => 'text', |
| 45 |
'default' => '' |
| 46 |
) |
| 47 |
) |
| 48 |
), |
| 49 |
'points_type' => array( |
| 50 |
'from' => 'points_type', |
| 51 |
'default' => __( 'Choose a points type', 'automatorwp' ), |
| 52 |
'fields' => array( |
| 53 |
'points_type' => array( |
| 54 |
'name' => __( 'Points Type:', 'automatorwp' ), |
| 55 |
'type' => 'select', |
| 56 |
'option_none' => false, |
| 57 |
'options_cb' => 'automatorwp_gamipress_points_types_options_cb' |
| 58 |
) |
| 59 |
) |
| 60 |
), |
| 61 |
'user' => array( |
| 62 |
'from' => 'user', |
| 63 |
'default' => __( 'user', 'automatorwp' ), |
| 64 |
'fields' => array( |
| 65 |
'user' => array( |
| 66 |
'name' => __( 'User ID:', 'automatorwp' ), |
| 67 |
'desc' => __( 'User ID that will receive this points. Leave blank to award the points to the user that completes the automation.', 'automatorwp' ), |
| 68 |
'type' => 'text', |
| 69 |
'default' => '' |
| 70 |
), |
| 71 |
) |
| 72 |
), |
| 73 |
), |
| 74 |
) ); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Action execution function |
| 80 |
* |
| 81 |
* @since 1.0.0 |
| 82 |
* |
| 83 |
* @param stdClass $action The action object |
| 84 |
* @param int $user_id The user ID |
| 85 |
* @param array $action_options The action's stored options (with tags already passed) |
| 86 |
* @param stdClass $automation The action's automation object |
| 87 |
*/ |
| 88 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 89 |
|
| 90 |
// Shorthand |
| 91 |
$points = absint( $action_options['points'] ); |
| 92 |
$points_type = $action_options['points_type']; |
| 93 |
$earning_text = $action_options['earning_text']; |
| 94 |
$user_id_to_award = absint( $action_options['user'] ); |
| 95 |
|
| 96 |
if( $user_id_to_award === 0 ) { |
| 97 |
$user_id_to_award = $user_id; |
| 98 |
} |
| 99 |
|
| 100 |
$user = get_userdata( $user_id_to_award ); |
| 101 |
|
| 102 |
// Bail if user does not exists |
| 103 |
if( ! $user ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
// Bail if no points to award |
| 108 |
if( $points === 0 ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
// Bail if post is not a points type |
| 113 |
if( ! gamipress_get_points_type( $points_type ) ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
// Award the points |
| 118 |
gamipress_award_points_to_user( $user_id_to_award, $points, $points_type ); |
| 119 |
|
| 120 |
if( ! empty( $earning_text ) ) { |
| 121 |
// Insert the custom user earning |
| 122 |
gamipress_insert_user_earning( $user_id, array( |
| 123 |
'title' => $earning_text, |
| 124 |
'user_id' => $user_id_to_award, |
| 125 |
'post_id' => gamipress_get_points_type_id( $points_type ), |
| 126 |
'post_type' => 'points-type', |
| 127 |
'points' => $points, |
| 128 |
'points_type' => $points_type, |
| 129 |
'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ), |
| 130 |
) ); |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
new AutomatorWP_GamiPress_User_Points(); |