| 1 |
<?php |
| 2 |
/** |
| 3 |
* Earn Points |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\MyCred\Triggers\Earn_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_MyCred_Earn_Points extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'mycred'; |
| 15 |
public $trigger = 'mycred_earn_points'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_trigger( $this->trigger, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'User earns points', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User earns <strong>points</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Points amount. %2$s: Post title. */ |
| 29 |
'edit_label' => sprintf( __( 'User earns %1$s %2$s', 'automatorwp' ), '{points}', '{points_type}' ), |
| 30 |
/* translators: %1$s: Points amount. %2$s: Post title. */ |
| 31 |
'log_label' => sprintf( __( 'User earns %1$s %2$s', 'automatorwp' ), '{points}', '{points_type}' ), |
| 32 |
'action' => 'mycred_add_finished', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 3, |
| 36 |
'options' => array( |
| 37 |
'points' => array( |
| 38 |
'from' => 'points', |
| 39 |
'default' => __( 'any amount of', 'automatorwp' ), |
| 40 |
'fields' => array( |
| 41 |
'points' => array( |
| 42 |
'name' => __( 'Points amount:', 'automatorwp' ), |
| 43 |
'desc' => __( 'Leave blank for any amount of points.', 'automatorwp' ), |
| 44 |
'type' => 'text', |
| 45 |
'default' => '' |
| 46 |
) |
| 47 |
) |
| 48 |
), |
| 49 |
'points_type' => automatorwp_utilities_ajax_selector_option( array( |
| 50 |
'field' => 'points_type', |
| 51 |
'option_default' => __( 'points type', 'automatorwp' ), |
| 52 |
'name' => __( 'Points Type:', 'automatorwp' ), |
| 53 |
'option_none' => false, |
| 54 |
'action_cb' => 'automatorwp_mycred_get_point_types', |
| 55 |
'options_cb' => 'automatorwp_mycred_options_cb_point_type', |
| 56 |
'placeholder' => 'Select a point type', |
| 57 |
'default' => '' |
| 58 |
) ), |
| 59 |
), |
| 60 |
'tags' => array( |
| 61 |
'times' => automatorwp_utilities_times_tag( true ) |
| 62 |
) |
| 63 |
) ); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Trigger listener |
| 69 |
* |
| 70 |
* @since 1.0.0 |
| 71 |
* |
| 72 |
* @param bool $result |
| 73 |
* @param array $request |
| 74 |
* @param object mycred User points balance |
| 75 |
*/ |
| 76 |
public function listener( $result, $request, $mycred ) { |
| 77 |
|
| 78 |
// Bail if no result |
| 79 |
if ( ! $result ) |
| 80 |
return; |
| 81 |
|
| 82 |
$user_id = $request['user_id']; |
| 83 |
|
| 84 |
// Bail if no user |
| 85 |
if ( $user_id === 0 ) |
| 86 |
return; |
| 87 |
|
| 88 |
$new_points = $request['amount']; |
| 89 |
|
| 90 |
// Bail if negative points |
| 91 |
if ( $new_points < 0 ) |
| 92 |
return; |
| 93 |
|
| 94 |
$points_type = $request['type']; |
| 95 |
|
| 96 |
automatorwp_trigger_event( array( |
| 97 |
'trigger' => $this->trigger, |
| 98 |
'user_id' => $user_id, |
| 99 |
'points_earned' => $new_points, |
| 100 |
'points_type' => $points_type, |
| 101 |
) ); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* User deserves check |
| 107 |
* |
| 108 |
* @since 1.0.0 |
| 109 |
* |
| 110 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 111 |
* @param stdClass $trigger The trigger object |
| 112 |
* @param int $user_id The user ID |
| 113 |
* @param array $event Event information |
| 114 |
* @param array $trigger_options The trigger's stored options |
| 115 |
* @param stdClass $automation The trigger's automation object |
| 116 |
* |
| 117 |
* @return bool True if user deserves trigger, false otherwise |
| 118 |
*/ |
| 119 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 120 |
|
| 121 |
// Don't deserve if points and points type are not received |
| 122 |
if( ! isset( $event['points_earned'] ) && ! isset( $event['points_type'] ) ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
$test = absint( $trigger_options['points'] ); |
| 127 |
|
| 128 |
// Don't deserve if points earned are lower than trigger option |
| 129 |
if( absint( $event['points_earned'] ) < absint( $trigger_options['points'] ) ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
// Don't deserve if points type doesn't match with the trigger option |
| 134 |
if( $trigger_options['points_type'] !== 'any' && $trigger_options['points_type'] !== $event['points_type'] ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
return $deserves_trigger; |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
new AutomatorWP_MyCred_Earn_Points(); |