| 1 |
<?php |
| 2 |
/** |
| 3 |
* Join Group |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\BuddyPress\Triggers\Join_Group |
| 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_BuddyPress_Join_Group extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'buddypress'; |
| 15 |
public $trigger = 'buddypress_join_group'; |
| 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 joins a group', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User <strong>joins</strong> a group', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Group. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User joins %1$s %2$s time(s)', 'automatorwp' ), '{group}', '{times}' ), |
| 30 |
/* translators: %1$s: Group. */ |
| 31 |
'log_label' => sprintf( __( 'User joins %1$s', 'automatorwp' ), '{group}' ), |
| 32 |
'action' => 'groups_join_group', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 2, |
| 36 |
'options' => array( |
| 37 |
'group' => automatorwp_utilities_ajax_selector_option( array( |
| 38 |
'field' => 'group', |
| 39 |
'name' => __( 'Group:', 'automatorwp' ), |
| 40 |
'option_none_value' => 'any', |
| 41 |
'option_none_label' => __( 'any group', 'automatorwp' ), |
| 42 |
'action_cb' => 'automatorwp_buddypress_get_groups', |
| 43 |
'options_cb' => 'automatorwp_buddypress_options_cb_group', |
| 44 |
'default' => 'any' |
| 45 |
) ), |
| 46 |
'times' => automatorwp_utilities_times_option(), |
| 47 |
), |
| 48 |
'tags' => array_merge( |
| 49 |
automatorwp_buddypress_get_group_tags(), |
| 50 |
automatorwp_utilities_times_tag() |
| 51 |
) |
| 52 |
) ); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Trigger listener |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
* |
| 61 |
* @param int $group_id |
| 62 |
* @param int $user_id |
| 63 |
*/ |
| 64 |
public function listener( $group_id, $user_id ) { |
| 65 |
|
| 66 |
// Trigger the join group |
| 67 |
automatorwp_trigger_event( array( |
| 68 |
'trigger' => $this->trigger, |
| 69 |
'user_id' => $user_id, |
| 70 |
'group_id' => $group_id, |
| 71 |
'post_id' => $group_id, |
| 72 |
) ); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* User deserves check |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* |
| 81 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 82 |
* @param stdClass $trigger The trigger object |
| 83 |
* @param int $user_id The user ID |
| 84 |
* @param array $event Event information |
| 85 |
* @param array $trigger_options The trigger's stored options |
| 86 |
* @param stdClass $automation The trigger's automation object |
| 87 |
* |
| 88 |
* @return bool True if user deserves trigger, false otherwise |
| 89 |
*/ |
| 90 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 91 |
|
| 92 |
// Don't deserve if group is not received |
| 93 |
if( ! isset( $event['group_id'] ) ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
// Don't deserve if group doesn't match with the trigger option |
| 98 |
if( $trigger_options['group'] !== 'any' && absint( $event['group_id'] ) !== absint( $trigger_options['group'] ) ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
return $deserves_trigger; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Register the required hooks |
| 108 |
* |
| 109 |
* @since 1.0.0 |
| 110 |
*/ |
| 111 |
public function hooks() { |
| 112 |
|
| 113 |
// Log meta data |
| 114 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 115 |
|
| 116 |
parent::hooks(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Trigger custom log meta |
| 121 |
* |
| 122 |
* @since 1.0.0 |
| 123 |
* |
| 124 |
* @param array $log_meta Log meta data |
| 125 |
* @param stdClass $trigger The trigger object |
| 126 |
* @param int $user_id The user ID |
| 127 |
* @param array $event Event information |
| 128 |
* @param array $trigger_options The trigger's stored options |
| 129 |
* @param stdClass $automation The trigger's automation object |
| 130 |
* |
| 131 |
* @return array |
| 132 |
*/ |
| 133 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 134 |
|
| 135 |
// Bail if action type don't match this action |
| 136 |
if( $trigger->type !== $this->trigger ) { |
| 137 |
return $log_meta; |
| 138 |
} |
| 139 |
|
| 140 |
$log_meta['group_id'] = ( isset( $event['group_id'] ) ? $event['group_id'] : '' ); |
| 141 |
|
| 142 |
return $log_meta; |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
new AutomatorWP_BuddyPress_Join_Group(); |