| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add User Group |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\BuddyPress\Actions\Add_User_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_Add_User_Group extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'buddypress'; |
| 15 |
public $action = 'buddypress_add_user_group'; |
| 16 |
|
| 17 |
/** |
| 18 |
* The action result |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
* |
| 22 |
* @var string $result |
| 23 |
*/ |
| 24 |
public $result = ''; |
| 25 |
|
| 26 |
/** |
| 27 |
* Register the trigger |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
public function register() { |
| 32 |
|
| 33 |
automatorwp_register_action( $this->action, array( |
| 34 |
'integration' => $this->integration, |
| 35 |
'label' => __( 'Add user to group', 'automatorwp' ), |
| 36 |
'select_option' => __( 'Add user to <strong>group</strong>', 'automatorwp' ), |
| 37 |
/* translators: %1$s: Group. */ |
| 38 |
'edit_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{group}' ), |
| 39 |
/* translators: %1$s: Group. */ |
| 40 |
'log_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{group}' ), |
| 41 |
'options' => array( |
| 42 |
'group' => automatorwp_utilities_ajax_selector_option( array( |
| 43 |
'field' => 'group', |
| 44 |
'option_default' => __( 'group', 'automatorwp' ), |
| 45 |
'name' => __( 'Group:', 'automatorwp' ), |
| 46 |
'option_none' => false, |
| 47 |
'option_custom' => true, |
| 48 |
'option_custom_desc' => __( 'Group ID', 'automatorwp' ), |
| 49 |
'action_cb' => 'automatorwp_buddypress_get_groups', |
| 50 |
'options_cb' => 'automatorwp_buddypress_options_cb_group', |
| 51 |
'placeholder' => __( 'Select a group', 'automatorwp' ), |
| 52 |
'default' => '', |
| 53 |
) ) |
| 54 |
), |
| 55 |
) ); |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Action execution function |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
* @param stdClass $action The action object |
| 65 |
* @param int $user_id The user ID |
| 66 |
* @param array $action_options The action's stored options (with tags already passed) |
| 67 |
* @param stdClass $automation The action's automation object |
| 68 |
*/ |
| 69 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 70 |
|
| 71 |
$this->result = ''; |
| 72 |
|
| 73 |
// Bail if BuddyPress function does not exist |
| 74 |
if ( ! function_exists( 'groups_join_group' ) ) { |
| 75 |
$this->result = __( 'Groups component is not active.', 'automatorwp' ); |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// Shorthand |
| 80 |
$group_id = absint( $action_options['group'] ); |
| 81 |
|
| 82 |
// Bail if group not provided |
| 83 |
if( $group_id === 0 ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
// Add the user to the group |
| 88 |
groups_join_group( $group_id, $user_id ); |
| 89 |
$this->result = __( 'User added to group successfully.', 'automatorwp' ); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Register required hooks |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
*/ |
| 98 |
public function hooks() { |
| 99 |
|
| 100 |
// Log meta data |
| 101 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 102 |
|
| 103 |
// Log fields |
| 104 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 105 |
|
| 106 |
parent::hooks(); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Action custom log meta |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* |
| 115 |
* @param array $log_meta Log meta data |
| 116 |
* @param stdClass $action The action object |
| 117 |
* @param int $user_id The user ID |
| 118 |
* @param array $action_options The action's stored options (with tags already passed) |
| 119 |
* @param stdClass $automation The action's automation object |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 124 |
|
| 125 |
// Bail if action type don't match this action |
| 126 |
if( $action->type !== $this->action ) { |
| 127 |
return $log_meta; |
| 128 |
} |
| 129 |
|
| 130 |
$log_meta['result'] = $this->result; |
| 131 |
|
| 132 |
return $log_meta; |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Action custom log fields |
| 138 |
* |
| 139 |
* @since 1.0.0 |
| 140 |
* |
| 141 |
* @param array $log_fields The log fields |
| 142 |
* @param stdClass $log The log object |
| 143 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 144 |
* |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
public function log_fields( $log_fields, $log, $object ) { |
| 148 |
|
| 149 |
// Bail if log is not assigned to an action |
| 150 |
if( $log->type !== 'action' ) { |
| 151 |
return $log_fields; |
| 152 |
} |
| 153 |
|
| 154 |
// Bail if action type don't match this action |
| 155 |
if( $object->type !== $this->action ) { |
| 156 |
return $log_fields; |
| 157 |
} |
| 158 |
|
| 159 |
$log_fields['result'] = array( |
| 160 |
'name' => __( 'Result:', 'automatorwp' ), |
| 161 |
'type' => 'text', |
| 162 |
); |
| 163 |
|
| 164 |
return $log_fields; |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
new AutomatorWP_BuddyPress_Add_User_Group(); |