PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / integrations / mycred / includes / triggers / earn-points.php

earn-points.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.3, at integrations/mycred/includes/triggers/earn-points.php

144 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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();