| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create Group |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\LearnDash\Actions\Create_Group |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_LearnDash_Create_Group extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'learndash'; |
| 15 |
public $action = 'learndash_create_group'; |
| 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' => __( 'Create a group', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Create a <strong>group</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Group. */ |
| 29 |
'edit_label' => sprintf( __( 'Create a %1$s', 'automatorwp' ), '{group}' ), |
| 30 |
/* translators: %1$s: Group. */ |
| 31 |
'log_label' => sprintf( __( 'Create a %1$s', 'automatorwp' ), '{group}' ), |
| 32 |
'options' => array( |
| 33 |
'group' => array( |
| 34 |
'default' => __( 'group', 'automatorwp' ), |
| 35 |
'fields' => array( |
| 36 |
'post_title' => array( |
| 37 |
'name' => __( 'Group name:', 'automatorwp' ), |
| 38 |
'type' => 'text', |
| 39 |
), |
| 40 |
'post' => automatorwp_utilities_post_field( array( |
| 41 |
'name' => __( 'Group course:', 'automatorwp' ), |
| 42 |
'option_none' => false, |
| 43 |
'option_custom' => true, |
| 44 |
'option_custom_desc' => __( 'Course ID or comma-separated list of courses IDs', 'automatorwp' ), |
| 45 |
'post_type' => 'sfwd-courses', |
| 46 |
'placeholder' => __( 'Select a course', 'automatorwp' ), |
| 47 |
'default' => '', |
| 48 |
'multiple' => true |
| 49 |
) ), |
| 50 |
'post_custom' => automatorwp_utilities_custom_field( array( |
| 51 |
'option_custom_desc' => __( 'Course ID or comma-separated list of courses IDs', 'automatorwp' ), |
| 52 |
) ), |
| 53 |
'leader_role_assignment' => array( |
| 54 |
'name' => __( 'If user doesn\'t have the "Group Leader" role:', 'automatorwp' ), |
| 55 |
'type' => 'select', |
| 56 |
'options' => array( |
| 57 |
'nothing' => __( 'Do nothing', 'automatorwp' ), |
| 58 |
'add' => __( 'Add the role to their existing roles', 'automatorwp' ), |
| 59 |
'set' => __( 'Replace their existing roles with the "Group Leader" role', 'automatorwp' ), |
| 60 |
), |
| 61 |
'default' => 'nothing' |
| 62 |
), |
| 63 |
) |
| 64 |
), |
| 65 |
), |
| 66 |
) ); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Action execution function |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* |
| 75 |
* @param stdClass $action The action object |
| 76 |
* @param int $user_id The user ID |
| 77 |
* @param array $action_options The action's stored options (with tags already passed) |
| 78 |
* @param stdClass $automation The action's automation object |
| 79 |
*/ |
| 80 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 81 |
|
| 82 |
// Shorthand |
| 83 |
$post_title = $action_options['post_title']; |
| 84 |
$course_ids = $action_options['post']; |
| 85 |
$leader_role_assignment = $action_options['leader_role_assignment']; |
| 86 |
|
| 87 |
$user = get_user_by( 'ID', $user_id ); |
| 88 |
|
| 89 |
// Bail if user doesn't exists |
| 90 |
if ( is_wp_error( $user_id ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Bail if action is configured to don't continue if the user hasn't the role |
| 95 |
if ( ! user_can( $user, 'group_leader' ) && $leader_role_assignment === 'nothing' ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
// If user hasn't the role, add or assign it |
| 100 |
if ( ! user_can( $user, 'group_leader' ) ) { |
| 101 |
switch ( $leader_role_assignment ) { |
| 102 |
case 'add': |
| 103 |
$user->add_role( 'group_leader' ); |
| 104 |
break; |
| 105 |
case 'set': |
| 106 |
$user->set_role( 'group_leader' ); |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// Create the group |
| 112 |
$group_id = wp_insert_post( array( |
| 113 |
'post_title' => $post_title, |
| 114 |
'post_content' => '', |
| 115 |
'post_type' => 'groups', |
| 116 |
'post_status' => 'publish', |
| 117 |
'post_author' => $user_id, |
| 118 |
) ); |
| 119 |
|
| 120 |
// Bail if can't create the group |
| 121 |
if ( is_wp_error( $group_id ) ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
// Set the user as leader |
| 126 |
ld_update_leader_group_access( $user_id, $group_id ); |
| 127 |
|
| 128 |
if( ! is_array( $course_ids ) ) { |
| 129 |
$course_ids = explode( ',', $course_ids ); |
| 130 |
} |
| 131 |
|
| 132 |
foreach ($course_ids as $course_id) { |
| 133 |
|
| 134 |
//Sanitize $id |
| 135 |
$course_id = absint( trim( $course_id ) ); |
| 136 |
|
| 137 |
$course = get_post( $course_id ); |
| 138 |
|
| 139 |
// Skip this ID if course not found |
| 140 |
if( ! $course ) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
// Add the course to the group |
| 145 |
ld_update_course_group_access( (int) $course_id, (int) $group_id, false ); |
| 146 |
|
| 147 |
// Delete the course groups transient |
| 148 |
delete_transient( 'learndash_course_groups_' . $course_id ); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
new AutomatorWP_LearnDash_Create_Group(); |