| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Group Leader |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\LearnDash\Actions\User_Group_Leader |
| 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_LearnDash_User_Group_Leader extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'learndash'; |
| 15 |
public $action = 'learndash_user_group_leader'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_action( $this->action, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'Make user the leader of a group', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Make user the <strong>leader</strong> of a group', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Group. */ |
| 29 |
'edit_label' => sprintf( __( 'Make user the leader of a %1$s', 'automatorwp' ), '{group}' ), |
| 30 |
/* translators: %1$s: Group. */ |
| 31 |
'log_label' => sprintf( __( 'Make user the leader of a %1$s', 'automatorwp' ), '{group}' ), |
| 32 |
'options' => array( |
| 33 |
'group' => array( |
| 34 |
'default' => __( 'group', 'automatorwp' ), |
| 35 |
'from' => 'post', |
| 36 |
'fields' => array( |
| 37 |
'post' => automatorwp_utilities_post_field( array( |
| 38 |
'name' => __( 'Group:', 'automatorwp' ), |
| 39 |
'option_none' => false, |
| 40 |
'option_custom' => true, |
| 41 |
'option_custom_desc' => __( 'Group ID', 'automatorwp' ), |
| 42 |
'post_type' => 'groups', |
| 43 |
'placeholder' => __( 'Select a group', 'automatorwp' ), |
| 44 |
'default' => '' |
| 45 |
) ), |
| 46 |
'post_custom' => automatorwp_utilities_custom_field( array( |
| 47 |
'option_custom_desc' => __( 'Group ID', 'automatorwp' ), |
| 48 |
) ), |
| 49 |
'leader_role_assignment' => array( |
| 50 |
'name' => __( 'If user doesn\'t have the "Group Leader" role:', 'automatorwp' ), |
| 51 |
'type' => 'select', |
| 52 |
'options' => array( |
| 53 |
'nothing' => __( 'Do nothing', 'automatorwp' ), |
| 54 |
'add' => __( 'Add the role to their existing roles', 'automatorwp' ), |
| 55 |
'set' => __( 'Replace their existing roles with the "Group Leader" role', 'automatorwp' ), |
| 56 |
), |
| 57 |
'default' => 'nothing' |
| 58 |
), |
| 59 |
) |
| 60 |
), |
| 61 |
), |
| 62 |
) ); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Action execution function |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* |
| 71 |
* @param stdClass $action The action object |
| 72 |
* @param int $user_id The user ID |
| 73 |
* @param array $action_options The action's stored options (with tags already passed) |
| 74 |
* @param stdClass $automation The action's automation object |
| 75 |
*/ |
| 76 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 77 |
|
| 78 |
// Shorthand |
| 79 |
$group_id = $action_options['post']; |
| 80 |
$leader_role_assignment = $action_options['leader_role_assignment']; |
| 81 |
|
| 82 |
$user = get_user_by( 'ID', $user_id ); |
| 83 |
|
| 84 |
// Bail if user doesn't exists |
| 85 |
if ( is_wp_error( $user_id ) ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
// Bail if action is configured to don't continue if the user hasn't the role |
| 90 |
if ( ! user_can( $user, 'group_leader' ) && $leader_role_assignment === 'nothing' ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// If user hasn't the role, add or assign it |
| 95 |
if ( ! user_can( $user, 'group_leader' ) ) { |
| 96 |
switch ( $leader_role_assignment ) { |
| 97 |
case 'add': |
| 98 |
$user->add_role( 'group_leader' ); |
| 99 |
break; |
| 100 |
case 'set': |
| 101 |
$user->set_role( 'group_leader' ); |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// Set the user as leader |
| 107 |
ld_update_leader_group_access( $user_id, $group_id ); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
new AutomatorWP_LearnDash_User_Group_Leader(); |