| 1 |
<?php |
| 2 |
/** |
| 3 |
* Earn Points |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\GamiPress\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_GamiPress_Earn_Points extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'gamipress'; |
| 15 |
public $trigger = 'gamipress_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' => 'gamipress_update_user_points', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 8, |
| 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' => array( |
| 50 |
'from' => 'points_type', |
| 51 |
'fields' => array( |
| 52 |
'points_type' => array( |
| 53 |
'name' => __( 'Points Type:', 'automatorwp' ), |
| 54 |
'type' => 'select', |
| 55 |
'option_none' => true, |
| 56 |
'option_none_label' => __( 'points of any type', 'automatorwp' ), |
| 57 |
'options_cb' => 'automatorwp_gamipress_points_types_options_cb', |
| 58 |
'default' => 'any', |
| 59 |
) |
| 60 |
) |
| 61 |
), |
| 62 |
), |
| 63 |
'tags' => array_merge( |
| 64 |
array( |
| 65 |
'points_earned' => array( |
| 66 |
'label' => __( 'Points earned', 'automatorwp' ), |
| 67 |
'type' => 'integer', |
| 68 |
'preview' => __( '100', 'automatorwp' ), |
| 69 |
), |
| 70 |
), |
| 71 |
automatorwp_utilities_post_tags() |
| 72 |
) |
| 73 |
) ); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Trigger listener |
| 79 |
* |
| 80 |
* @since 1.0.0 |
| 81 |
* |
| 82 |
* @param int $user_id The user ID |
| 83 |
* @param int $new_points Points amount earned |
| 84 |
* @param int $total_points User points balance |
| 85 |
* @param int $admin_id The admin that awarded those rank |
| 86 |
* @param int $achievement_id The achievement ID |
| 87 |
* @param string $points_type The points type slug |
| 88 |
* @param string $reason Reason of this award |
| 89 |
* @param string $log_type The log type |
| 90 |
*/ |
| 91 |
public function listener( $user_id, $new_points, $total_points, $admin_id, $achievement_id, $points_type, $reason, $log_type ) { |
| 92 |
|
| 93 |
automatorwp_trigger_event( array( |
| 94 |
'trigger' => $this->trigger, |
| 95 |
'user_id' => $user_id, |
| 96 |
'points_earned' => $new_points, |
| 97 |
'points' => $total_points, |
| 98 |
'points_type' => $points_type, |
| 99 |
) ); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* User deserves check |
| 105 |
* |
| 106 |
* @since 1.0.0 |
| 107 |
* |
| 108 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 109 |
* @param stdClass $trigger The trigger object |
| 110 |
* @param int $user_id The user ID |
| 111 |
* @param array $event Event information |
| 112 |
* @param array $trigger_options The trigger's stored options |
| 113 |
* @param stdClass $automation The trigger's automation object |
| 114 |
* |
| 115 |
* @return bool True if user deserves trigger, false otherwise |
| 116 |
*/ |
| 117 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 118 |
|
| 119 |
// Don't deserve if points is not received |
| 120 |
if( ! isset( $event['points'] ) ) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
// Don't deserve if points type is not received |
| 125 |
if( ! isset( $event['points_type'] ) ) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
// Ensure points amount required as integer |
| 130 |
$trigger_options['points'] = absint( $trigger_options['points'] ); |
| 131 |
|
| 132 |
if( $trigger_options['points'] > 0 ) { |
| 133 |
|
| 134 |
// Don't deserve if points earned are lower than trigger option |
| 135 |
if( $event['points_earned'] < $trigger_options['points'] ) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
// Don't deserve if points type doesn't match with the trigger option |
| 142 |
if( $trigger_options['points_type'] !== 'any' && $trigger_options['points_type'] !== $event['points_type'] ) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
return $deserves_trigger; |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Register the required hooks |
| 152 |
* |
| 153 |
* @since 1.0.0 |
| 154 |
*/ |
| 155 |
public function hooks() { |
| 156 |
|
| 157 |
// Log meta data |
| 158 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 159 |
|
| 160 |
// Trigger tags replacements |
| 161 |
add_filter( 'automatorwp_trigger_tags_replacements', array( $this, 'tags_replacements' ), 10, 4 ); |
| 162 |
|
| 163 |
parent::hooks(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Trigger custom log meta |
| 168 |
* |
| 169 |
* @since 1.0.0 |
| 170 |
* |
| 171 |
* @param array $log_meta Log meta data |
| 172 |
* @param stdClass $trigger The trigger object |
| 173 |
* @param int $user_id The user ID |
| 174 |
* @param array $event Event information |
| 175 |
* @param array $trigger_options The trigger's stored options |
| 176 |
* @param stdClass $automation The trigger's automation object |
| 177 |
* |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 181 |
|
| 182 |
// Bail if action type don't match this action |
| 183 |
if( $trigger->type !== $this->trigger ) { |
| 184 |
return $log_meta; |
| 185 |
} |
| 186 |
|
| 187 |
$log_meta['points_earned'] = ( isset( $event['points_earned'] ) ? $event['points_earned'] : 0 ); |
| 188 |
|
| 189 |
return $log_meta; |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Filter to setup custom trigger tags replacements |
| 195 |
* |
| 196 |
* Note: Post and times tags replacements are already passed |
| 197 |
* |
| 198 |
* @since 1.0.0 |
| 199 |
* |
| 200 |
* @param array $replacements The trigger replacements |
| 201 |
* @param stdClass $trigger The trigger object |
| 202 |
* @param int $user_id The user ID |
| 203 |
* @param stdClass $log The last trigger log object |
| 204 |
* |
| 205 |
* @return array |
| 206 |
*/ |
| 207 |
public function tags_replacements( $replacements, $trigger, $user_id, $log ) { |
| 208 |
|
| 209 |
// Bail if trigger type don't match this trigger |
| 210 |
if( $trigger->type !== $this->trigger ) { |
| 211 |
return $replacements; |
| 212 |
} |
| 213 |
|
| 214 |
// Times replacement by default |
| 215 |
$replacements['points_earned'] = ct_get_object_meta( $log->id, 'points_earned', true ); |
| 216 |
|
| 217 |
return $replacements; |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
new AutomatorWP_GamiPress_Earn_Points(); |