Commit inicial - WordPress Análisis de Precios Unitarios

- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-11-03 21:04:30 -06:00
commit a22573bf0b
24068 changed files with 4993111 additions and 0 deletions

View File

@@ -0,0 +1,224 @@
<?php
final class BWFAN_LD_Add_User_To_Group extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Add User to Group', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action adds a user to the group', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'user_id', 'group_id' );
$this->action_priority = 15;
$this->support_v2 = true;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Localize data for html fields for the current action.
*/
public function admin_enqueue_assets() {
if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'groups', $data );
}
}
public function get_view_data() {
$groups = get_posts( array(
'post_type' => 'groups',
'posts_per_page' => - 1,
'status' => 'publish',
'suppress_filters' => false
) );
$group_array = array();
foreach ( $groups as $group ) {
$group_array[ $group->ID ] = $group->post_title;
}
return $group_array;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
?>
<script type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
selected_group = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'group')) ? data.actionSavedData.data.group : '';
#>
<div class="bwfan-<?php echo esc_html__( $this->get_slug() ); ?>">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Group', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="bwfan[{{data.action_id}}][data][group]">
<option value=""><?php echo esc_html__( 'Choose any Group', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'groups') && _.isObject(data.actionFieldsOptions.groups) ) {
_.each( data.actionFieldsOptions.groups, function( value, key ){
selected = (key == selected_group) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
<div class="clearfix bwfan_field_desc bwfan-mb20">Select the group in which you want to add user</div>
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array();
$data_to_set['user_id'] = $task_meta['global']['user_id'];
$data_to_set['group_id'] = $task_meta['data']['group'];
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $task_meta['global']['email'] ) && is_email( $task_meta['global']['email'] ) ) ? $task_meta['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['user_id'] = isset( $step_data['user_id'] ) ? $step_data['user_id'] : 0;
$data_to_set['group_id'] = isset( $step_data['group'][0]['id'] ) ? $step_data['group'][0]['id'] : 0;
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $automation_data['global']['email'] ) && is_email( $automation_data['global']['email'] ) ) ? $automation_data['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->set_data( $action_data['processed_data'] );
$result = $this->process();
if ( true === $result ) {
return array(
'status' => 3,
'message' => 'User added to the group'
);
}
return $result;
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
if ( false === $is_required_fields_present ) {
return $this->show_fields_error();
}
$group_id = absint( $this->data['group_id'] );
if ( empty( $group_id ) ) {
return array(
'status' => 4,
'message' => __( 'Group was not selected', 'wp-marketing-automations-pro' ),
);
}
$user_id = $this->data['user_id'];
$user = get_userdata( $user_id );
if ( false === $user ) {
return array(
'status' => 4,
'message' => __( 'User does not exists', 'wp-marketing-automations-pro' ),
);
}
ld_update_group_access( $user_id, $group_id );
return true;
}
public function process_v2() {
$group_id = absint( $this->data['group_id'] );
if ( empty( $group_id ) ) {
return $this->skipped_response( __( 'Group was not selected', 'wp-marketing-automations-pro' ) );
}
$user_id = $this->data['user_id'];
$user = get_userdata( $user_id );
if ( false === $user ) {
return $this->skipped_response( __( 'User does not exists', 'wp-marketing-automations-pro' ) );
}
ld_update_group_access( $user_id, $group_id );
return $this->success_message( 'User added in group.' );
}
public function get_fields_schema() {
return [
[
"id" => 'group',
"label" => __( 'Group', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_groups',
'slug' => 'ld_groups',
'labelText' => 'group'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Group is required", 'wp-marketing-automations-pro' ),
"multiple" => false
],
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['group'] ) || empty( $data['group'] ) ) {
return '';
}
$groups = [];
foreach ( $data['group'] as $group ) {
if ( ! isset( $group['name'] ) || empty( $group['name'] ) ) {
continue;
}
$groups[] = $group['name'];
}
return $groups;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_LD_Add_User_To_Group';

View File

@@ -0,0 +1,193 @@
<?php
final class BWFAN_LD_Enroll_User_Into_Course extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Enroll User into Course(s)', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action enrolls a user into a selected courses', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'user_id', 'courses' );
$this->action_priority = 5;
$this->support_v2 = true;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
?>
<script>
jQuery(document).ready(function ($) {
$('body').on('select2:select', '.bwfan-course-search', function (e) {
var temp_courses = {};
var course_data = $(this).select2('data');
course_data.forEach(function (item) {
temp_courses[item.id] = item.text;
});
$(this).parent().find('.bwfan_searched_course_name').val(JSON.stringify(temp_courses));
});
});
</script>
<script type="text/html" id="tmpl-action-<?php esc_attr_e( $this->get_slug() ); ?>">
<#
searched_courses = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'searched_courses')) ? data.actionSavedData.data.searched_courses : '';
selected_courses = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'courses')) ? data.actionSavedData.data.courses : {};
#>
<div class="bwfan-input-form clearfix">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Select Courses', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" data-search="sfwd-courses" data-search-text="<?php esc_attr_e( 'Select Course', 'wp-marketing-automations-pro' ); ?>" class="bwfan-select2ajax-single bwfan-course-search bwfan-input-wrapper" name="bwfan[{{data.action_id}}][data][courses][]" multiple>
<#
if(_.size(searched_courses) >0) {
temp_selected_course = JSON.parse(searched_courses);
_.each( selected_courses, function( v ){
#>
<option value="{{v}}" selected>{{temp_selected_course[v]}}</option>
<#
})
}
#>
</select>
<input type="hidden" class="bwfan_searched_course_name" name="bwfan[{{data.action_id}}][data][searched_courses]" value="{{searched_courses}}"/>
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array(
'courses' => $task_meta['data']['courses'],
'user_id' => absint( $task_meta['global']['user_id'] ),
);
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $task_meta['global']['email'] ) && is_email( $task_meta['global']['email'] ) ) ? $task_meta['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['user_id'] = isset( $step_data['user_id'] ) ? $step_data['user_id'] : 0;
$data_to_set['courses'] = isset( $step_data['courses'] ) && is_array( $step_data['courses'] ) ? array_column( $step_data['courses'], 'id' ) : [];
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $automation_data['global']['email'] ) && is_email( $automation_data['global']['email'] ) ) ? $automation_data['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->set_data( $action_data['processed_data'] );
$result = $this->process();
if ( true === $result ) {
return array(
'status' => 3,
);
}
return array(
'status' => 4,
'message' => ( is_array( $result ) && isset( $result['bwfan_response'] ) ) ? $result['bwfan_response'] : __( 'User could not be enrolled in the course.', 'wp-marketing-automations-pro' ),
);
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
if ( false === $is_required_fields_present ) {
return $this->show_fields_error();
}
foreach ( $this->data['courses'] as $course_id ) {
ld_update_course_access( $this->data['user_id'], $course_id );
}
return true;
}
public function process_v2() {
if ( empty( $this->data['courses'] ) && ! is_array( $this->data['courses'] ) ) {
return $this->skipped_response( __( 'Courses not selected', 'wp-marketing-automations-pro' ) );
}
foreach ( $this->data['courses'] as $course_id ) {
ld_update_course_access( $this->data['user_id'], $course_id );
}
return $this->success_message( 'User added in Courses.' );
}
public function get_fields_schema() {
return [
[
"id" => 'courses',
"label" => __( 'Select Courses', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_courses',
'slug' => 'ld_courses',
'labelText' => 'plan'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Course is required", 'wp-marketing-automations-pro' ),
],
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['courses'] ) || empty( $data['courses'] ) ) {
return '';
}
$courses = [];
foreach ( $data['courses'] as $course ) {
if ( ! isset( $course['name'] ) || empty( $course['name'] ) ) {
continue;
}
$courses[] = $course['name'];
}
return $courses;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_LD_Enroll_User_Into_Course';

View File

@@ -0,0 +1,193 @@
<?php
final class BWFAN_LD_Remove_User_From_Course extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Remove User from Course(s)', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action removes a user from a selected courses', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'user_id', 'courses' );
$this->action_priority = 10;
$this->support_v2 = true;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
?>
<script>
jQuery(document).ready(function ($) {
$('body').on('select2:select', '.bwfan-course-search', function (e) {
var temp_courses = {};
var course_data = $(this).select2('data');
course_data.forEach(function (item) {
temp_courses[item.id] = item.text;
});
$(this).parent().find('.bwfan_searched_course_name').val(JSON.stringify(temp_courses));
});
});
</script>
<script type="text/html" id="tmpl-action-<?php esc_attr_e( $this->get_slug() ); ?>">
<#
searched_courses = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'searched_courses')) ? data.actionSavedData.data.searched_courses : '';
selected_courses = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'courses')) ? data.actionSavedData.data.courses : {};
#>
<div class="bwfan-input-form clearfix">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Select Courses', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" data-search="sfwd-courses" data-search-text="<?php esc_attr_e( 'Select Course', 'wp-marketing-automations-pro' ); ?>" class="bwfan-select2ajax-single bwfan-course-search bwfan-input-wrapper" name="bwfan[{{data.action_id}}][data][courses][]" multiple>
<#
if(_.size(searched_courses) >0) {
temp_selected_course = JSON.parse(searched_courses);
_.each( selected_courses, function( v ){
#>
<option value="{{v}}" selected>{{temp_selected_course[v]}}</option>
<#
})
}
#>
</select>
<input type="hidden" class="bwfan_searched_course_name" name="bwfan[{{data.action_id}}][data][searched_courses]" value="{{searched_courses}}"/>
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array(
'courses' => $task_meta['data']['courses'],
'user_id' => $task_meta['global']['user_id'],
);
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $task_meta['global']['email'] ) && is_email( $task_meta['global']['email'] ) ) ? $task_meta['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['user_id'] = isset( $step_data['user_id'] ) ? $step_data['user_id'] : 0;
$data_to_set['courses'] = isset( $step_data['courses'] ) && is_array( $step_data['courses'] ) ? array_column( $step_data['courses'], 'id' ) : [];
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $automation_data['global']['email'] ) && is_email( $automation_data['global']['email'] ) ) ? $automation_data['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->set_data( $action_data['processed_data'] );
$result = $this->process();
if ( true === $result ) {
return array(
'status' => 3,
);
}
return array(
'status' => 4,
'message' => ( is_array( $result ) && isset( $result['bwfan_response'] ) ) ? $result['bwfan_response'] : __( 'User could not be removed from the course.', 'wp-marketing-automations-pro' ),
);
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
if ( false === $is_required_fields_present ) {
return $this->show_fields_error();
}
foreach ( $this->data['courses'] as $course_id ) {
ld_update_course_access( $this->data['user_id'], $course_id, true );
}
return true;
}
public function process_v2() {
if ( empty( $this->data['courses'] ) && ! is_array( $this->data['courses'] ) ) {
return $this->skipped_response( __( 'Courses not selected', 'wp-marketing-automations-pro' ) );
}
foreach ( $this->data['courses'] as $course_id ) {
ld_update_course_access( $this->data['user_id'], $course_id, true );
}
return $this->success_message( 'User removed from Course(s).' );
}
public function get_fields_schema() {
return [
[
"id" => 'courses',
"label" => __( 'Select Courses', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_courses',
'slug' => 'ld_courses',
'labelText' => 'plan'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Course is required", 'wp-marketing-automations-pro' ),
],
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['courses'] ) || empty( $data['courses'] ) ) {
return '';
}
$courses = [];
foreach ( $data['courses'] as $course ) {
if ( ! isset( $course['name'] ) || empty( $course['name'] ) ) {
continue;
}
$courses[] = $course['name'];
}
return $courses;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_LD_Remove_User_From_Course';

View File

@@ -0,0 +1,224 @@
<?php
final class BWFAN_LD_Add_User_From_Group extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Remove User from Group', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action removed a user from a selected group', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'user_id', 'group_id' );
$this->action_priority = 20;
$this->support_v2 = true;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Localize data for html fields for the current action.
*/
public function admin_enqueue_assets() {
if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'groups', $data );
}
}
public function get_view_data() {
$groups = get_posts( array(
'post_type' => 'groups',
'posts_per_page' => - 1,
'status' => 'publish',
'suppress_filters' => false
) );
$group_array = array();
foreach ( $groups as $group ) {
$group_array[ $group->ID ] = $group->post_title;
}
return $group_array;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
?>
<script type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
selected_group = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'group')) ? data.actionSavedData.data.group : '';
#>
<div class="bwfan-<?php echo esc_html__( $this->get_slug() ); ?>">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Group', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="bwfan[{{data.action_id}}][data][group]">
<option value=""><?php echo esc_html__( 'Choose any Group', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'groups') && _.isObject(data.actionFieldsOptions.groups) ) {
_.each( data.actionFieldsOptions.groups, function( value, key ){
selected = (key == selected_group) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
<div class="clearfix bwfan_field_desc bwfan-mb20">Select the group from which you want to remove user</div>
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array();
$data_to_set['user_id'] = $task_meta['global']['user_id'];
$data_to_set['group_id'] = $task_meta['data']['group'];
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $task_meta['global']['email'] ) && is_email( $task_meta['global']['email'] ) ) ? $task_meta['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['user_id'] = isset( $step_data['user_id'] ) ? $step_data['user_id'] : 0;
$data_to_set['group_id'] = isset( $step_data['group'][0]['id'] ) ? $step_data['group'][0]['id'] : 0;
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $automation_data['global']['email'] ) && is_email( $automation_data['global']['email'] ) ) ? $automation_data['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->set_data( $action_data['processed_data'] );
$result = $this->process();
if ( true === $result ) {
return array(
'status' => 3,
'message' => 'User removed from a group'
);
}
return $result;
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
if ( false === $is_required_fields_present ) {
return $this->show_fields_error();
}
$group_id = absint( $this->data['group_id'] );
if ( empty( $group_id ) ) {
return array(
'status' => 4,
'message' => __( 'Group was not selected', 'wp-marketing-automations-pro' ),
);
}
$user_id = $this->data['user_id'];
$user = get_userdata( $user_id );
if ( false === $user ) {
return array(
'status' => 4,
'message' => __( 'User does not exists', 'wp-marketing-automations-pro' ),
);
}
ld_update_group_access( $user_id, $group_id, true );
return true;
}
public function process_v2() {
$group_id = absint( $this->data['group_id'] );
if ( empty( $group_id ) ) {
return $this->skipped_response( __( 'Group was not selected', 'wp-marketing-automations-pro' ) );
}
$user_id = $this->data['user_id'];
$user = get_userdata( $user_id );
if ( false === $user ) {
return $this->skipped_response( __( 'User does not exists', 'wp-marketing-automations-pro' ) );
}
ld_update_group_access( $user_id, $group_id, true );
return $this->success_message( 'User removed from group.' );
}
public function get_fields_schema() {
return [
[
"id" => 'group',
"label" => __( 'Group', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_groups',
'slug' => 'ld_groups',
'labelText' => 'group'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Group is required", 'wp-marketing-automations-pro' ),
"multiple" => false
],
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['group'] ) || empty( $data['group'] ) ) {
return '';
}
$groups = [];
foreach ( $data['group'] as $group ) {
if ( ! isset( $group['name'] ) || empty( $group['name'] ) ) {
continue;
}
$groups[] = $group['name'];
}
return $groups;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_LD_Add_User_From_Group';

View File

@@ -0,0 +1,224 @@
<?php
final class BWFAN_LD_Reset_Course_Progress extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Reset Course Progress', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action resets the course progress of a user', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'user_id', 'course_id' );
$this->action_priority = 30;
$this->support_v2 = true;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Localize data for html fields for the current action.
*/
public function admin_enqueue_assets() {
if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'courses', $data );
}
}
public function get_view_data() {
if ( ! function_exists( 'learndash_get_courses_count' ) ) {
return array();
}
/** @var WP_Query $courses */
$courses = learndash_get_courses_count( array(), '' );
$course_array = [];
foreach ( $courses->posts as $course_id ) {
$course_array[ $course_id ] = get_the_title( $course_id );
}
return $course_array;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
?>
<script type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
selected_course = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'course')) ? data.actionSavedData.data.course : '';
#>
<div class="bwfan-<?php echo esc_html__( $this->get_slug() ); ?>">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Course', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="bwfan[{{data.action_id}}][data][course]">
<option value=""><?php echo esc_html__( 'Choose any Course', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'courses') && _.isObject(data.actionFieldsOptions.courses) ) {
_.each( data.actionFieldsOptions.courses, function( value, key ){
selected = (key == selected_course) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
<div class="clearfix bwfan_field_desc bwfan-mb20">Select the course which you want to reset the progress from</div>
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array();
$data_to_set['user_id'] = $task_meta['global']['user_id'];
$data_to_set['course_id'] = $task_meta['data']['course'];
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $task_meta['global']['email'] ) && is_email( $task_meta['global']['email'] ) ) ? $task_meta['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['user_id'] = isset( $step_data['user_id'] ) ? $step_data['user_id'] : 0;
$data_to_set['course_id'] = isset( $step_data['course'][0]['id'] ) ? $step_data['course'][0]['id'] : 0;
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $automation_data['global']['email'] ) && is_email( $automation_data['global']['email'] ) ) ? $automation_data['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->set_data( $action_data['processed_data'] );
$result = $this->process();
if ( true === $result ) {
return array(
'status' => 3,
'message' => 'Course Progress Reset Done'
);
}
return $result;
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
if ( false === $is_required_fields_present ) {
return $this->show_fields_error();
}
$course_id = absint( $this->data['course_id'] );
if ( empty( $course_id ) ) {
return array(
'status' => 4,
'message' => __( 'Course was not selected', 'wp-marketing-automations-pro' ),
);
}
$user_id = $this->data['user_id'];
$user = get_userdata( $user_id );
if ( false === $user ) {
return array(
'status' => 4,
'message' => __( 'User does not exists', 'wp-marketing-automations-pro' ),
);
}
learndash_delete_course_progress( $course_id, $user_id );
return true;
}
public function process_v2() {
$course_id = absint( $this->data['course_id'] );
if ( empty( $course_id ) ) {
return $this->skipped_response( __( 'Course was not selected', 'wp-marketing-automations-pro' ) );
}
$user_id = $this->data['user_id'];
$user = get_userdata( $user_id );
if ( false === $user ) {
return $this->skipped_response( __( 'User does not exists', 'wp-marketing-automations-pro' ) );
}
learndash_delete_course_progress( $course_id, $user_id );
return $this->success_message( __( 'Course reset successfully.', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
return [
[
"id" => 'course',
"label" => __( 'Select Course', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_courses',
'slug' => 'ld_courses',
'labelText' => 'plan'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Course is required", 'wp-marketing-automations-pro' ),
"multiple" => false
],
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['courses'] ) || empty( $data['courses'] ) ) {
return '';
}
$courses = [];
foreach ( $data['courses'] as $course ) {
if ( ! isset( $course['name'] ) || empty( $course['name'] ) ) {
continue;
}
$courses[] = $course['name'];
}
return $courses;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_LD_Reset_Course_Progress';

View File

@@ -0,0 +1,225 @@
<?php
final class BWFAN_LD_Reset_Quiz_Attempts extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Reset Quiz Attempts', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action resets a user quiz attempt count', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'user_id', 'quiz_id' );
$this->action_priority = 35;
$this->support_v2 = true;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Localize data for html fields for the current action.
*/
public function admin_enqueue_assets() {
if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'quizzes', $data );
}
}
public function get_view_data() {
$quizzes = get_posts( array(
'post_type' => 'sfwd-quiz',
'posts_per_page' => - 1,
'status' => 'publish',
'suppress_filters' => false
) );
$quiz_array = array();
foreach ( $quizzes as $quiz ) {
$quiz_array[ $quiz->ID ] = $quiz->post_title;
}
return $quiz_array;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
?>
<script type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
selected_quiz = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'quiz')) ? data.actionSavedData.data.quiz : '';
#>
<div class="bwfan-<?php echo esc_html__( $this->get_slug() ); ?>">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Quiz', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="bwfan[{{data.action_id}}][data][quiz]">
<option value=""><?php echo esc_html__( 'Choose any Quiz', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'quizzes') && _.isObject(data.actionFieldsOptions.quizzes) ) {
_.each( data.actionFieldsOptions.quizzes, function( value, key ){
selected = (key == selected_quiz) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
<div class="clearfix bwfan_field_desc bwfan-mb20">Select the quiz for which you want to reset the attempts of</div>
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array();
$data_to_set['user_id'] = $task_meta['global']['user_id'];
$data_to_set['quiz_id'] = $task_meta['data']['quiz'];
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $task_meta['global']['email'] ) && is_email( $task_meta['global']['email'] ) ) ? $task_meta['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['user_id'] = isset( $step_data['user_id'] ) ? $step_data['user_id'] : 0;
$data_to_set['quiz_id'] = isset( $step_data['quiz'][0]['id'] ) ? $step_data['quiz'][0]['id'] : 0;
if ( empty( $data_to_set['user_id'] ) ) {
$email = ( isset( $automation_data['global']['email'] ) && is_email( $automation_data['global']['email'] ) ) ? $automation_data['global']['email'] : '';
$user = is_email( $email ) ? get_user_by( 'email', $email ) : '';
$data_to_set['user_id'] = $user instanceof WP_User ? $user->ID : 0;
}
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->set_data( $action_data['processed_data'] );
$result = $this->process();
if ( true === $result ) {
return array(
'status' => 3,
'message' => 'Quiz Attempts Reset Done'
);
}
return $result;
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
if ( false === $is_required_fields_present ) {
return $this->show_fields_error();
}
$quiz_id = absint( $this->data['quiz_id'] );
if ( empty( $quiz_id ) ) {
return array(
'status' => 4,
'message' => __( 'Quiz was not selected', 'wp-marketing-automations-pro' ),
);
}
$user_id = $this->data['user_id'];
$user = get_userdata( $user_id );
if ( false === $user ) {
return array(
'status' => 4,
'message' => __( 'User does not exists', 'wp-marketing-automations-pro' ),
);
}
learndash_remove_user_quiz_attempt( $user_id, array( 'quiz' => $quiz_id ) );
return true;
}
public function process_v2() {
$quiz_id = absint( $this->data['quiz_id'] );
if ( empty( $quiz_id ) ) {
return $this->skipped_response( __( 'Course was not selected', 'wp-marketing-automations-pro' ) );
}
$user_id = $this->data['user_id'];
$user = get_userdata( $user_id );
if ( false === $user ) {
return $this->skipped_response( __( 'User does not exists', 'wp-marketing-automations-pro' ) );
}
learndash_remove_user_quiz_attempt( $user_id, array( 'quiz' => $quiz_id ) );
return $this->success_message( __( 'Quiz attempt reset successfully.', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
return [
[
"id" => 'quiz',
"label" => __( 'Quiz', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_quiz',
'slug' => 'ld_quiz',
'labelText' => 'quiz'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Quiz is required", 'wp-marketing-automations-pro' ),
"multiple" => false,
"description" => __( "Select the quiz for which you want to reset the attempts of", 'wp-marketing-automations-pro' )
],
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['quiz'] ) || empty( $data['quiz'] ) ) {
return '';
}
$quizzes = [];
foreach ( $data['quiz'] as $quiz ) {
if ( ! isset( $quiz['name'] ) || empty( $quiz['name'] ) ) {
continue;
}
$quizzes[] = $quiz['name'];
}
return $quizzes;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_LD_Reset_Quiz_Attempts';

View File

@@ -0,0 +1,2 @@
<?php
// Silence is golden.

View File

@@ -0,0 +1,34 @@
<?php
final class BWFAN_LD_Integration extends BWFAN_Integration {
private static $instance = null;
/**
* BWFAN_LD_Integration constructor.
*/
private function __construct() {
$this->action_dir = __DIR__;
$this->nice_name = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->group_name = __( 'LMS', 'wp-marketing-automations-pro' );
$this->group_slug = 'lms';
$this->priority = 65;
}
/**
* Ensures only one instance of the class is loaded or can be loaded.
*
* @return BWFAN_LD_Integration|null
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
if ( bwfan_is_learndash_active() ) {
BWFAN_Load_Integrations::register( 'BWFAN_LD_Integration' );
}

View File

@@ -0,0 +1,34 @@
<?php
class BWFAN_LD_Source extends BWFAN_Source {
private static $instance = null;
public function __construct() {
$this->event_dir = __DIR__;
$this->nice_name = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->group_name = __( 'LMS', 'wp-marketing-automations-pro' );
$this->group_slug = 'lms';
$this->priority = 50;
}
/**
* Ensures only one instance of the class is loaded or can be loaded.
*
* @return BWFAN_LD_Source|null
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
/**
* Register this as a source.
*/
if ( bwfan_is_learndash_active() ) {
BWFAN_Load_Sources::register( 'BWFAN_LD_Source' );
}

View File

@@ -0,0 +1,359 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_LD_User_Added_To_Group extends BWFAN_Event {
private static $instance = null;
public $user_id = 0;
public $group_id = 0;
public $email = '';
public $group_categories = [];
private function __construct() {
$this->event_merge_tag_groups = array( 'learndash_course', 'learndash_user', 'learndash_group', 'bwf_contact' );
$this->event_name = __( 'User Added to Group', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs when the user is added to a group.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'learndash_group',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->priority = 80;
$this->v2 = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'ld_added_group_access', [ $this, 'process' ], 10, 2 );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $user_id
* @param $group_id
*
* @return void
*/
public function process( $user_id, $group_id ) {
$data = $this->get_default_data();
$data['user_id'] = $user_id;
$data['group_id'] = $group_id;
$user = get_user_by( 'id', absint( $user_id ) );
$data['email'] = $user instanceof WP_User && is_email( $user->user_email ) ? $user->user_email : '';
$this->send_async_call( $data );
}
/**
* Set up rules data
*
* @param $automation_data
*/
public function pre_executable_actions( $automation_data ) {
BWFAN_Core()->rules->setRulesData( $this->user_id, 'user_id' );
BWFAN_Core()->rules->setRulesData( $this->group_id, 'group_id' );
BWFAN_Core()->rules->setRulesData( $this->group_categories, 'group_categories' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['user_id'] = $this->user_id;
$data_to_send['global']['group_id'] = $this->group_id;
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['group_categories'] = $this->group_categories;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$user = get_user_by( 'id', $global_data['user_id'] );
$group = get_post( $global_data['group_id'] );
?>
<li>
<strong><?php echo esc_html__( 'User:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . '?user-edit.php?user_id=' . $user->ID; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $user->user_nicename ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Assigned to Group:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $group->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $group->post_title ); ?></a>
</li>
<?php
return ob_get_clean();
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'group_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['group_id'] ) ) ) {
$set_data = array(
'user_id' => intval( $task_meta['global']['user_id'] ),
'group_id' => intval( $task_meta['global']['group_id'] ),
'email' => $task_meta['global']['email'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function get_email_event() {
return $this->email;
}
public function get_user_id_event() {
return $this->user_id;
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->user_id = BWFAN_Common::$events_async_data['user_id'];
$this->group_id = BWFAN_Common::$events_async_data['group_id'];
$group_categories = get_the_terms( $this->group_id, 'ld_group_category' );
$categories = [];
if ( is_array( $group_categories ) && count( $group_categories ) > 0 ) {
foreach ( $group_categories as $group_category ) {
$categories[] = $group_category->term_id;
}
}
$this->group_categories = $categories;
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
/** check any group case */
if ( ! isset( $automation_data['event_meta'] ) || ! isset( $automation_data['event_meta']['groups'] ) || 'any' === $automation_data['event_meta']['groups'] ) {
return true;
}
/** check if specific group selected */
if ( ! is_array( $automation_data['event_meta']['group_id'] ) || 0 === count( $automation_data['event_meta']['group_id'] ) ) {
return false;
}
$ids = array_column( $automation_data['event_meta']['group_id'], 'id' );
return in_array( intval( $automation_data['group_id'] ), array_map( 'intval', $ids ), true );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->user_id = BWFAN_Common::$events_async_data['user_id'];
$this->group_id = BWFAN_Common::$events_async_data['group_id'];
$group_categories = get_the_terms( $this->group_id, 'ld_group_category' );
$categories = [];
if ( is_array( $group_categories ) && count( $group_categories ) > 0 ) {
foreach ( $group_categories as $group_category ) {
$categories[] = $group_category->term_id;
}
}
$this->group_categories = $categories;
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['user_id'] = $this->user_id;
$automation_data['group_id'] = $this->group_id;
$automation_data['group_categories'] = $this->group_categories;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [
[
'id' => 'groups',
'label' => __( 'Groups', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Group', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Groups', 'wp-marketing-automations-pro' ),
'value' => 'selected_group'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => "",
],
[
"id" => 'group_id',
"label" => __( 'Select Groups', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_groups',
'slug' => 'ld_groups',
'labelText' => 'group'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Group is required", 'wp-marketing-automations-pro' ),
"multiple" => true,
'toggler' => [
'fields' => [
[
'id' => 'groups',
'value' => 'selected_group',
]
]
],
]
];
}
/** set default values */
public function get_default_values() {
return array( "groups" => "any" );
}
/**
* store data in normalize table or not
*
* @return bool
*/
public function is_db_normalize() {
return true;
}
/**
* store data in normalized table
*
* @return void
*/
public function execute_normalization( $post_parameters ) {
if ( ! class_exists( 'BWFCRM_DB_Normalization_Learndash' ) ) {
return;
}
$db_ins = BWFCRM_DB_Normalization_Learndash::get_instance();
$db_ins->add_group_access( $post_parameters['user_id'], $post_parameters['group_id'] );
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
if ( 0 === intval( $contact->get_wpid() ) ) {
return [ 'status' => 0, 'type' => 'user_not_found' ];
}
$group_id = BWFAN_Learndash_Common::get_last_user_activity_by_type( $contact->get_wpid(), 'group_progress' );
/** return if no group available as event is added to group */
if ( empty( $group_id ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'User is not added to any group', 'wp-marketing-automations-pro' ) ];
}
/** handling if select_group is selected */
if ( isset( $automation_data['event_meta']['groups'] ) && 'selected_group' === $automation_data['event_meta']['groups'] ) {
$event_group_ids = array_column( $automation_data['event_meta']['group_id'], 'id' );
if ( ! in_array( $group_id, $event_group_ids ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No groups are matching with event groups', 'wp-marketing-automations-pro' ) ];
}
}
$this->email = $contact->get_email();
$this->contact_id = $cid;
$this->user_id = $contact->get_wpid();
$this->group_id = $group_id;
$group_categories = get_the_terms( $this->group_id, 'ld_group_category' );
$categories = [];
if ( is_array( $group_categories ) && count( $group_categories ) > 0 ) {
foreach ( $group_categories as $group_category ) {
$categories[] = $group_category->term_id;
}
}
$this->group_categories = $categories;
return array_merge( $automation_data, [
'contact_id' => $this->contact_id,
'email' => $this->email,
'user_id' => $this->user_id,
'group_id' => $this->group_id,
'group_categories' => $this->group_categories
] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_learndash_active() ) {
return 'BWFAN_LD_User_Added_To_Group';
}

View File

@@ -0,0 +1,356 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_LD_User_Completes_Course extends BWFAN_Event {
private static $instance = null;
/** @var WP_User $user */
public $user = null;
/** @var WP_Post $course */
public $course = null;
public $progress = null;
public $email = '';
private function __construct() {
$this->event_merge_tag_groups = array( 'learndash_user', 'learndash_course', 'learndash_group', 'bwf_contact' );
$this->event_name = __( 'User Completes a Course', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs when the user completes a course.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'learndash_course',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->priority = 30;
$this->v2 = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'learndash_course_completed', [ $this, 'process' ] );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $course : is an array with 4 keys
* 1) user: user object
* 2) course: course object
* 3) progress: course progress array
* 4) completed_time: The time at which the course is marked as complete
*/
public function process( $course ) {
$data = $this->get_default_data();
$data['user_id'] = $course['user']->ID;
$data['course_id'] = $course['course']->ID;
$data['progress'] = $course['progress'];
$user = get_user_by( 'id', absint( $course['user']->ID ) );
$data['email'] = $user instanceof WP_User && is_email( $user->user_email ) ? $user->user_email : '';
$this->send_async_call( $data );
}
/**
* Set up rules data
*
* @param $automation_data
*/
public function pre_executable_actions( $automation_data ) {
BWFAN_Core()->rules->setRulesData( $this->user->ID, 'user_id' );
BWFAN_Core()->rules->setRulesData( $this->user, 'user' );
BWFAN_Core()->rules->setRulesData( $this->course->ID, 'course_id' );
BWFAN_Core()->rules->setRulesData( $this->course, 'course' );
BWFAN_Core()->rules->setRulesData( $this->progress, 'progress' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['user_id'] = $this->user->ID;
$data_to_send['global']['user'] = $this->user;
$data_to_send['global']['course'] = $this->course;
$data_to_send['global']['course_id'] = $this->course->ID;
$data_to_send['global']['progress'] = $this->progress;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
$user = get_user_by( 'ID', $global_data['user_id'] );
$course = get_post( $global_data['course_id'] );
ob_start();
?>
<li>
<strong><?php echo esc_html__( 'User:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . '?user-edit.php?user_id=' . $user->ID; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $user->display_name ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Course:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $course->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $course->post_title ); ?></a>
</li>
<?php
return ob_get_clean();
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'course' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['course'] ) ) ) {
$set_data = array(
'user_id' => $task_meta['global']['user_id'],
'user' => $task_meta['global']['user'],
'course' => $task_meta['global']['course'],
'course_id' => $task_meta['global']['course_id'],
'progress' => $task_meta['global']['progress'],
'email' => $task_meta['global']['email'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function get_email_event() {
return $this->email;
}
public function get_user_id_event() {
return $this->user->ID;
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->user = get_user_by( 'ID', BWFAN_Common::$events_async_data['user_id'] );
$this->course = get_post( BWFAN_Common::$events_async_data['course_id'] );
$this->progress = BWFAN_Common::$events_async_data['progress'];
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
/** check any course case */
if ( ! isset( $automation_data['event_meta'] ) || ! isset( $automation_data['event_meta']['courses'] ) || 'any' === $automation_data['event_meta']['courses'] ) {
return true;
}
/** check if specific course selected */
if ( ! is_array( $automation_data['event_meta']['course_id'] ) || 0 === count( $automation_data['event_meta']['course_id'] ) ) {
return false;
}
$ids = array_column( $automation_data['event_meta']['course_id'], 'id' );
return in_array( intval( $automation_data['course_id'] ), array_map( 'intval', $ids ), true );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->user = get_user_by( 'ID', BWFAN_Common::$events_async_data['user_id'] );
$this->course = get_post( BWFAN_Common::$events_async_data['course_id'] );
$this->progress = BWFAN_Common::$events_async_data['progress'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['user_id'] = $this->user->ID;
$automation_data['user'] = $this->user;
$automation_data['course_id'] = $this->course->ID;
$automation_data['course'] = $this->course;
$automation_data['progress'] = $this->progress;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [
[
'id' => 'courses',
'label' => __( 'Courses', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Course', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Courses', 'wp-marketing-automations-pro' ),
'value' => 'selected_course'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => "",
],
[
"id" => 'course_id',
"label" => __( 'Select Course', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_courses',
'slug' => 'ld_courses',
'labelText' => 'course'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Course is required", 'wp-marketing-automations-pro' ),
"multiple" => true,
'toggler' => [
'fields' => [
[
'id' => 'courses',
'value' => 'selected_course',
]
]
],
]
];
}
/** set default values */
public function get_default_values() {
return array( "courses" => "any" );
}
/**
* store data in normalize table
*
* @return bool
*/
public function is_db_normalize() {
return true;
}
/**
* store data in normalized table
*
* @return void
*/
public function execute_normalization( $post_parameters ) {
if ( ! class_exists( 'BWFCRM_DB_Normalization_Learndash' ) ) {
return;
}
$db_ins = BWFCRM_DB_Normalization_Learndash::get_instance();
$db_ins->add_course_completed( $post_parameters['user_id'], $post_parameters['course_id'] );
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
if ( 0 === intval( $contact->get_wpid() ) ) {
return [ 'status' => 0, 'type' => 'user_not_found' ];
}
/** fetching last course id */
$course_id = BWFAN_Learndash_Common::get_last_user_activity_by_type( $contact->get_wpid(), 'course' );
/** return if no course available as event is completed a course */
if ( empty( $course_id ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'User is not enrolled to any course', 'wp-marketing-automations-pro' ) ];
}
/** handling if selected_course is selected */
if ( isset( $automation_data['event_meta']['courses'] ) && 'selected_course' === $automation_data['event_meta']['courses'] ) {
$event_course_ids = array_column( $automation_data['event_meta']['course_id'], 'id' );
if ( ! in_array( $course_id, $event_course_ids ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No courses are matching with event courses', 'wp-marketing-automations-pro' ) ];
}
}
$this->email = $contact->get_email();
$this->contact_id = $cid;
$this->user_id = $contact->get_wpid();
$this->course_id = $course_id;
$this->user = get_user_by( 'ID', $contact->get_wpid() );
$this->course = get_post( $course_id );
$course_progress = learndash_user_get_course_progress( $contact->get_wpid(), $course_id, 'legacy' );
$this->progress = array( $course_id => $course_progress );
return array_merge( $automation_data, [
'contact_id' => $this->contact_id,
'email' => $this->email,
'user_id' => $this->user_id,
'user' => $this->user,
'course_id' => $this->course_id,
'course' => $this->course,
'progress' => $this->progress
] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_learndash_active() ) {
return 'BWFAN_LD_User_Completes_Course';
}

View File

@@ -0,0 +1,376 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_LD_User_Completes_Lesson extends BWFAN_Event {
private static $instance = null;
/** @var WP_User $user */
public $user = null;
/** @var WP_Post $course */
public $course = null;
/** @var WP_Post $lesson */
public $lesson = null;
public $progress = null;
public $email = '';
private function __construct() {
$this->event_merge_tag_groups = array( 'learndash_course', 'learndash_user', 'learndash_lesson', 'bwf_contact' );
$this->event_name = __( 'User Completes a Lesson', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs when the user completes a lesson.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'learndash_lesson',
'learndash_course',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->priority = 40;
$this->v2 = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'learndash_lesson_completed', [ $this, 'process' ] );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $lesson : is an array with 4 keys
* 1) user: user object
* 2) course: course object
* 3) lesson: lesson object
* 4) progress: course progress array
*/
public function process( $lesson ) {
$data = $this->get_default_data();
$data['user_id'] = $lesson['user']->ID;
$data['course_id'] = $lesson['course']->ID;
$data['lesson_id'] = $lesson['lesson']->ID;
$data['progress'] = $lesson['progress'];
$user = get_user_by( 'id', absint( $lesson['user']->ID ) );
$data['email'] = $user instanceof WP_User && is_email( $user->user_email ) ? $user->user_email : '';
$this->send_async_call( $data );
}
/**
* Set up rules data
*
* @param $automation_data
*/
public function pre_executable_actions( $automation_data ) {
BWFAN_Core()->rules->setRulesData( $this->user->ID, 'user_id' );
BWFAN_Core()->rules->setRulesData( $this->user, 'user' );
BWFAN_Core()->rules->setRulesData( $this->course, 'course' );
BWFAN_Core()->rules->setRulesData( $this->course->ID, 'course_id' );
BWFAN_Core()->rules->setRulesData( $this->lesson, 'lesson' );
BWFAN_Core()->rules->setRulesData( $this->lesson->ID, 'lesson_id' );
BWFAN_Core()->rules->setRulesData( $this->progress, 'progress' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['user_id'] = $this->user->ID;
$data_to_send['global']['user'] = $this->user;
$data_to_send['global']['course_id'] = $this->course->ID;
$data_to_send['global']['course'] = $this->course;
$data_to_send['global']['lesson_id'] = $this->lesson->ID;
$data_to_send['global']['lesson'] = $this->lesson;
$data_to_send['global']['progress'] = $this->progress;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
$user = get_user_by( 'ID', $global_data['user_id'] );
$course = get_post( $global_data['course_id'] );
$lesson = get_post( $global_data['lesson_id'] );
ob_start();
?>
<li>
<strong><?php echo esc_html__( 'User:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . '?user-edit.php?user_id=' . $user->ID; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $user->user_nicename ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Course:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $course->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $course->post_title ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Lesson:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $lesson->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $lesson->post_title ); ?></a>
</li>
<?php
return ob_get_clean();
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'lesson' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['lesson'] ) ) ) {
$set_data = array(
'user_id' => $task_meta['global']['user_id'],
'email' => $task_meta['global']['email'],
'user' => $task_meta['global']['user'],
'course' => $task_meta['global']['course'],
'course_id' => $task_meta['global']['course_id'],
'lesson' => $task_meta['global']['lesson'],
'lesson_id' => $task_meta['global']['lesson_id'],
'progress' => $task_meta['global']['progress'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function get_email_event() {
return $this->email;
}
public function get_user_id_event() {
return $this->user->ID;
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->user = get_user_by( 'ID', BWFAN_Common::$events_async_data['user_id'] );
$this->course = get_post( BWFAN_Common::$events_async_data['course_id'] );
$this->lesson = get_post( BWFAN_Common::$events_async_data['lesson_id'] );
$this->progress = BWFAN_Common::$events_async_data['progress'];
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
/** check any lesson case */
if ( ! isset( $automation_data['event_meta'] ) || ! isset( $automation_data['event_meta']['lessons'] ) || 'any' === $automation_data['event_meta']['lessons'] ) {
return true;
}
/** check if specific any selected */
if ( ! is_array( $automation_data['event_meta']['lesson_id'] ) || 0 === count( $automation_data['event_meta']['lesson_id'] ) ) {
return false;
}
$ids = array_column( $automation_data['event_meta']['lesson_id'], 'id' );
return in_array( intval( $automation_data['lesson_id'] ), array_map( 'intval', $ids ), true );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->user = get_user_by( 'ID', BWFAN_Common::$events_async_data['user_id'] );
$this->course = get_post( BWFAN_Common::$events_async_data['course_id'] );
$this->lesson = get_post( BWFAN_Common::$events_async_data['lesson_id'] );
$this->progress = BWFAN_Common::$events_async_data['progress'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['user'] = $this->user;
$automation_data['course'] = $this->course;
$automation_data['lesson'] = $this->lesson;
$automation_data['progress'] = $this->progress;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [
[
'id' => 'lessons',
'label' => __( 'Lessons', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Lesson', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Lessons', 'wp-marketing-automations-pro' ),
'value' => 'selected_lesson'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => "",
],
[
"id" => 'lesson_id',
"label" => __( 'Select Lesson', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_lessons',
'slug' => 'ld_lessons',
'labelText' => 'lesson'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Lesson is required", 'wp-marketing-automations-pro' ),
"multiple" => true,
'toggler' => [
'fields' => [
[
'id' => 'lessons',
'value' => 'selected_lesson',
]
]
],
]
];
}
/** set default values */
public function get_default_values() {
return array( "lessons" => "any" );
}
/**
* store data in normalize table
*
* @return bool
*/
public function is_db_normalize() {
return true;
}
/**
* store data in normalized table
*
* @return void
*/
public function execute_normalization( $post_parameters ) {
if ( ! class_exists( 'BWFCRM_DB_Normalization_Learndash' ) ) {
return;
}
$db_ins = BWFCRM_DB_Normalization_Learndash::get_instance();
$db_ins->add_lesson_completed( $post_parameters['user_id'], $post_parameters['lesson_id'] );
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
if ( 0 === intval( $contact->get_wpid() ) ) {
return [ 'status' => 0, 'type' => 'user_not_found' ];
}
/** fetching last lesson id */
$lesson_id = BWFAN_Learndash_Common::get_last_user_activity_by_type( $contact->get_wpid(), 'lesson' );
/** return if no lesson available as event is completed a lesson */
if ( empty( $lesson_id ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'User has not completed any lesson', 'wp-marketing-automations-pro' ) ];
}
/** handling if selected_lesson is selected */
if ( isset( $automation_data['event_meta']['lessons'] ) && 'selected_lesson' === $automation_data['event_meta']['lessons'] ) {
$event_lesson_ids = array_column( $automation_data['event_meta']['lesson_id'], 'id' );
if ( ! in_array( $lesson_id, $event_lesson_ids ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No lessons are matching with event lessons', 'wp-marketing-automations-pro' ) ];
}
}
$this->email = $contact->get_email();
$this->contact_id = $cid;
$this->user_id = $contact->get_wpid();
$this->user = get_user_by( 'ID', $contact->get_wpid() );
$this->lesson_id = $lesson_id;
$this->lesson = get_post( $lesson_id );
$this->course_id = learndash_get_course_id( $lesson_id );
$this->course = get_post( $this->course_id );
$course_progress = learndash_user_get_course_progress( $contact->get_wpid(), $this->course_id, 'legacy' );
$this->progress = array( $this->course_id => $course_progress );
return array_merge( $automation_data, [
'contact_id' => $this->contact_id,
'email' => $this->email,
'user_id' => $this->user_id,
'user' => $this->user,
'lesson_id' => $this->lesson_id,
'lesson' => $this->lesson,
'course_id' => $this->course_id,
'course' => $this->course,
'progress' => $this->progress
] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_learndash_active() ) {
return 'BWFAN_LD_User_Completes_Lesson';
}

View File

@@ -0,0 +1,560 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_LD_User_Completes_Quiz extends BWFAN_Event {
private static $instance = null;
/** @var WP_User $user */
public $user = null;
public $quiz_data = [];
/** @var WP_Post $quiz */
public $quiz = null;
public $quiz_answers = array();
public $email = '';
private function __construct() {
$this->event_merge_tag_groups = array( 'learndash_course', 'learndash_user', 'learndash_quiz', 'bwf_contact' );
$this->event_name = __( 'User Completes a Quiz', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs when the user completes a quiz.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'learndash_quiz',
'learndash_quiz_result',
'learndash_lesson',
'learndash_course',
'learndash_topic',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->priority = 70;
$this->v2 = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'learndash_quiz_completed', [ $this, 'process' ], 10, 2 );
add_filter( 'bwfan_all_event_js_data', array( $this, 'add_form_data' ), 10, 2 );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $quizdata : array( 'quiz' => $quiz, 'course' => $course, 'questions' => $questions, 'score' => $score, 'count' => $count, 'pass' => $pass, 'rank' => '-', 'time' => time(), 'pro_quizid' => $quiz_id, 'points' => $points, 'total_points' => $total_points, 'percentage' => $result, 'timespent' => $timespent);
* @param $current_user
*/
public function process( $quizdata, $current_user ) {
$data = $this->get_default_data();
$data['quiz_data'] = $quizdata;
$data['quiz_id'] = ( isset( $quizdata['quiz'] ) && $quizdata['quiz'] instanceof WP_Post ) ? $quizdata['quiz']->ID : 0;
$data['user_id'] = $current_user->ID;
$user = get_user_by( 'id', absint( $current_user->ID ) );
$data['email'] = $user instanceof WP_User && is_email( $user->user_email ) ? $user->user_email : '';
$this->send_async_call( $data );
}
public function add_form_data( $event_js_data, $automation_meta ) {
if ( ! isset( $automation_meta['event_meta'] ) || ! isset( $event_js_data['ld_user_completes_quiz'] ) || ! isset( $automation_meta['event_meta']['quiz_id'] ) ) {
return $event_js_data;
}
if ( isset( $automation_meta['event'] ) && ! empty( $automation_meta['event'] ) && 'ld_user_completes_quiz' !== $automation_meta['event'] ) {
return $event_js_data;
}
$event_js_data['ld_user_completes_quiz']['selected_quiz'] = $automation_meta['event_meta']['quiz_id'];
//$questions = BWFAN_Learndash_Common::get_learndash_quiz_questions( $automation_meta['event_meta']['quiz_id'] );
$questions = $this->get_quiz_questions( $automation_meta['event_meta']['quiz_id'] );
$event_js_data['ld_user_completes_quiz']['selected_questions'] = $questions;
return $event_js_data;
}
public function get_quiz_questions( $quiz_id ) {
$questions = BWFAN_Learndash_Common::get_learndash_quiz_questions_models( $quiz_id );
$options = array();
if ( is_array( $questions ) ) {
/** @var WpProQuiz_Model_Question $question */
foreach ( $questions as $question ) {
$options[ $question->getId() ] = esc_html( $question->getQuestion() );
}
}
return $options;
}
/**
* Set up rules data
*
* @param $automation_data
*/
public function pre_executable_actions( $automation_data ) {
BWFAN_Core()->rules->setRulesData( $this->user->ID, 'user_id' );
BWFAN_Core()->rules->setRulesData( $this->user, 'user' );
BWFAN_Core()->rules->setRulesData( $this->quiz_data, 'quiz_data' );
BWFAN_Core()->rules->setRulesData( $this->quiz->ID, 'quiz_id' );
BWFAN_Core()->rules->setRulesData( $this->quiz_answers, 'quiz_answers' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['user_id'] = $this->user->ID;
$data_to_send['global']['user'] = $this->user;
$data_to_send['global']['quiz_data'] = $this->quiz_data;
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['quiz_id'] = $this->quiz->ID;
$data_to_send['global']['quiz_answers'] = $this->quiz_answers;
return $data_to_send;
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script>
jQuery(document).ready(function ($) {
$(document.body).on('click', '.item_modify_trigger', function () {
setTimeout(select2Quiz, 300);
function select2Quiz() {
$('#bwfan-ld_quiz_id').select2({
placeholder: 'Search quiz',
minimumInputLength: 2,
quizs: false,
data: [],
escapeMarkup(m) {
return m;
},
ajax: {
url: ajaxurl,
dataType: 'json',
type: 'POST',
delay: 250,
data(term) {
return {
search_term: term,
action: 'bwfan_select2ajax',
type: $('#bwfan-ld_quiz_id').attr('data-search'),
_wpnonce: bwfanParams.ajax_nonce,
};
},
processResults: function (data) {
var options = [];
if (data) {
// data is the array of arrays, and each of them contains ID and the Label of the option
$.each(data, function (index, text) { // do not forget that "index" is just auto incremented value
options.push({id: text.id, text: text.text});
});
}
return {
results: options
};
},
cache: true
}
});
}
});
$('body').on('change', '.bwfan-quiz-search', function () {
var temp_quiz = {id: $(this).val(), name: $(this).find(':selected').text()};
$(this).parent().find('.bwfan_searched_quiz').val(JSON.stringify(temp_quiz));
});
});
</script>
<script type="text/html" id="tmpl-event-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
selected_quiz_id = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'quiz_id')) ? data.eventSavedData.quiz_id : '';
searched_quiz = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'searched_quiz')) ? data.eventSavedData.searched_quiz : '';
if(!_.isEmpty(searched_quiz)) {
try {
searched_quiz = JSON.parse(searched_quiz);
}
catch(e) {
//Do Nothing
}
}
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-12 bwfan-p-0">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Select Quiz', 'wp-marketing-automations-pro' ); ?></label>
<select id="bwfan-ld_quiz_id" data-search="quiz" data-search-text="<?php esc_attr_e( 'Select Quiz', 'wp-marketing-automations-pro' ); ?>" class="bwfan-select2ajax-single bwfan-quiz-search bwfan-input-wrapper" name="event_meta[quiz_id]">
<option value="bwfan-ld-any-quiz"><?php esc_html_e( 'Any Quiz', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.size(searched_quiz) > 0) {
temp_selected_quiz = _.isObject(searched_quiz) ? searched_quiz : JSON.parse(searched_quiz);
if(temp_selected_quiz.id == selected_quiz_id){
#>
<option value="{{temp_selected_quiz.id}}" selected>{{temp_selected_quiz.name}}</option>
<#
}
}
stringify_searched_quiz = _.isObject(searched_quiz) ? JSON.stringify(searched_quiz) :
searched_quiz;
#>
</select>
<input type="hidden" class="bwfan_searched_quiz" name="event_meta[searched_quiz]" value="{{stringify_searched_quiz}}"/>
</div>
</script>
<?php
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
$user = get_user_by( 'ID', absint( $global_data['user_id'] ) );
ob_start();
?>
<li>
<strong><?php echo esc_html__( 'User:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . '?user-edit.php?user_id=' . $user->ID; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $user->first_name . ' ' . $user->last_name ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Result:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo ( ! empty( $global_data['quiz_data']['pass'] ) ) ? 'Pass' : 'Fail'; ?>
</li>
<li>
<strong><?php echo esc_html__( 'Score:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html( $global_data['quiz_data']['score'] ); ?>
</li>
<li>
<strong><?php echo esc_html__( 'Percentage:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html( $global_data['quiz_data']['percentage'] ); ?>
</li>
<?php
return ob_get_clean();
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'quiz_data' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['quiz_data'] ) ) ) {
$set_data = array(
'user_id' => $task_meta['global']['user_id'],
'email' => $task_meta['global']['email'],
'user' => $task_meta['global']['user'],
'quiz_data' => $task_meta['global']['quiz_data'],
'quiz_answers' => $task_meta['global']['quiz_answers'],
'quiz_id' => $task_meta['global']['quiz_id']
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function get_email_event() {
return $this->email;
}
public function get_user_id_event() {
return $this->user->ID;
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->user = get_user_by( 'ID', BWFAN_Common::$events_async_data['user_id'] );
$this->quiz_data = BWFAN_Common::$events_async_data['quiz_data'];
$this->quiz = get_post( BWFAN_Common::$events_async_data['quiz_id'] );
$this->quiz_answers = $this->get_answer_data();
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
public function get_answer_data() {
$answers = array();
if ( isset( $this->quiz_data['statistic_ref_id'] ) && $this->quiz_data['pro_quizid'] ) {
/** @var WpProQuiz_Model_StatisticUser[] $stats */
$stats = ( new WpProQuiz_Model_StatisticUserMapper() )->fetchUserStatistic( $this->quiz_data['statistic_ref_id'], $this->quiz_data['pro_quizid'] );
foreach ( $stats as $stat ) {
$user_answer = $stat->getStatisticAnswerData();
$user_question_id = $stat->getQuestionId();
/** @var WpProQuiz_Model_AnswerTypes[] $questions_answer_data */
$questions_answer_data = $stat->getQuestionAnswerData();
foreach ( $user_answer as $index => $option ) {
if ( 1 === $option ) {
$answers[ $user_question_id ] = $questions_answer_data[ $index ]->getAnswer();
break;
}
}
}
}
return $answers;
}
/**
* Validating form id after submission with the selected form id in the event
*
* @param $automations_arr
*
* @return mixed
*/
public function validate_event_data_before_creating_task( $automations_arr ) {
$automations_arr_temp = $automations_arr;
foreach ( $automations_arr as $automation_id => $automation_data ) {
if ( isset( $automation_data['event_meta']['quiz_id'] ) && isset( $this->quiz_data['pro_quizid'] ) && $automation_data['event_meta']['quiz_id'] !== 'bwfan-ld-any-quiz' && absint( $this->quiz_data['pro_quizid'] ) !== absint( $automation_data['event_meta']['quiz_id'] ) ) {
unset( $automations_arr_temp[ $automation_id ] );
}
}
return $automations_arr_temp;
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
/** check any quiz case */
if ( ! isset( $automation_data['event_meta'] ) || ! isset( $automation_data['event_meta']['quizzes'] ) || 'any' === $automation_data['event_meta']['quizzes'] ) {
return true;
}
/** check if specific quiz selected */
if ( ! is_array( $automation_data['event_meta']['quiz_id'] ) || 0 === count( $automation_data['event_meta']['quiz_id'] ) ) {
return false;
}
$ids = array_column( $automation_data['event_meta']['quiz_id'], 'id' );
return in_array( intval( $automation_data['quiz_data']['quiz'] ), array_map( 'intval', $ids ), true );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->user = get_user_by( 'ID', BWFAN_Common::$events_async_data['user_id'] );
$this->quiz_data = BWFAN_Common::$events_async_data['quiz_data'];
$this->quiz = get_post( BWFAN_Common::$events_async_data['quiz_id'] );
$this->quiz_answers = $this->get_answer_data();
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['user'] = $this->user;
$automation_data['quiz_data'] = $this->quiz_data;
$automation_data['quiz'] = $this->quiz;
$automation_data['quiz_answers'] = $this->quiz_answers;
$automation_data['email'] = $this->email;
return $automation_data;
}
/**
* v2 Method: Get field Schema
*
* @return array[]
*/
public function get_fields_schema() {
return [
[
'id' => 'quizzes',
'label' => __( 'Quizzes', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Quiz', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Quiz', 'wp-marketing-automations-pro' ),
'value' => 'selected_quiz'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => "",
],
[
"id" => 'quiz_id',
"label" => __( 'Select Quizzes', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_quiz',
'slug' => 'ld_quiz',
'labelText' => 'quiz'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Quiz is required", 'wp-marketing-automations-pro' ),
"multiple" => true,
'toggler' => [
'fields' => [
[
'id' => 'quizzes',
'value' => 'selected_quiz',
]
]
],
]
];
}
/** set default values */
public function get_default_values() {
return array( "quizzes" => "any" );
}
/**
* store data in normalize table
*
* @return bool
*/
public function is_db_normalize() {
return true;
}
/**
* store data in normalized table
*
* @return void
*/
public function execute_normalization( $post_parameters ) {
if ( ! class_exists( 'BWFCRM_DB_Normalization_Learndash' ) ) {
return;
}
$db_ins = BWFCRM_DB_Normalization_Learndash::get_instance();
$db_ins->add_quiz_completed( $post_parameters['quiz_data'], $post_parameters['user_id'] );
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
if ( 0 === intval( $contact->get_wpid() ) ) {
return [ 'status' => 0, 'type' => 'user_not_found' ];
}
/** fetching last quiz id */
$quiz_id = BWFAN_Learndash_Common::get_last_user_activity_by_type( $contact->get_wpid(), 'quiz' );
/** return if no quiz available as event is completed a quiz */
if ( empty( $quiz_id ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'User has not completed any quiz', 'wp-marketing-automations-pro' ) ];
}
/** handling if selected_quiz is selected */
if ( isset( $automation_data['event_meta']['quizzes'] ) && 'selected_quiz' === $automation_data['event_meta']['quizzes'] ) {
$event_quiz_ids = array_column( $automation_data['event_meta']['quiz_id'], 'id' );
if ( ! in_array( $quiz_id, $event_quiz_ids ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No quizzes are matching with event quizzes', 'wp-marketing-automations-pro' ) ];
}
}
$this->email = $contact->get_email();
$this->contact_id = $cid;
$this->user_id = $contact->get_wpid();
$this->user = get_user_by( 'ID', $contact->get_wpid() );
$this->quiz = get_post( $quiz_id );
$quiz_data = get_user_meta( $this->user_id, '_sfwd-quizzes' );
$quiz_data = array_filter( $quiz_data[0], function ( $quizdata ) use ( $quiz_id ) {
return intval( $quizdata['quiz'] ) === intval( $quiz_id );
} );
// taking the last of the array as there could be more attempts of same quiz
$this->quiz_data = ! empty( $quiz_data ) ? end( $quiz_data ) : [];
$this->quiz_answers = ! empty( $this->quiz_data ) ? $this->get_answer_data() : [];
return array_merge( $automation_data, [
'contact_id' => $this->contact_id,
'email' => $this->email,
'user_id' => $this->user_id,
'user' => $this->user,
'quiz_data' => $this->quiz_data,
'quiz_id' => $quiz_id,
'quiz' => $this->quiz,
'quiz_answers' => $this->quiz_answers
] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_learndash_active() ) {
return 'BWFAN_LD_User_Completes_Quiz';
}

View File

@@ -0,0 +1,377 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_LD_User_Completes_Topic extends BWFAN_Event {
private static $instance = null;
/** @var WP_User $user */
public $user = null;
/** @var WP_Post $course */
public $course = null;
/** @var WP_Post $lesson */
public $lesson = null;
/** @var WP_Post $topic */
public $topic = null;
public $progress = null;
public $email = '';
private function __construct() {
$this->event_merge_tag_groups = array( 'learndash_course', 'learndash_user', 'learndash_topic', 'bwf_contact' );
$this->event_name = __( 'User Completes a Topic', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs when the user completes a topic.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'learndash_lesson',
'learndash_course',
'learndash_topic',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->priority = 50;
$this->v2 = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'learndash_topic_completed', [ $this, 'process' ] );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $lesson : is an array with 4 keys
* 1) user: user object
* 2) course: course object
* 3) lesson: lesson object
* 4) topic: topic object
* 5) progress: course progress array
*/
public function process( $lesson ) {
$data = $this->get_default_data();
$data['user_id'] = $lesson['user']->ID;
$data['course_id'] = $lesson['course']->ID;
$data['lesson_id'] = $lesson['lesson']->ID;
$data['topic_id'] = $lesson['topic']->ID;
$data['progress'] = $lesson['progress'];
$user = get_user_by( 'id', absint( $lesson['user']->ID ) );
$data['email'] = $user instanceof WP_User && is_email( $user->user_email ) ? $user->user_email : '';
$this->send_async_call( $data );
}
/**
* Set up rules data
*
* @param $automation_data
*/
public function pre_executable_actions( $automation_data ) {
BWFAN_Core()->rules->setRulesData( $this->user->ID, 'user_id' );
BWFAN_Core()->rules->setRulesData( $this->user, 'user' );
BWFAN_Core()->rules->setRulesData( $this->course, 'course' );
BWFAN_Core()->rules->setRulesData( $this->course->ID, 'course_id' );
BWFAN_Core()->rules->setRulesData( $this->lesson, 'lesson' );
BWFAN_Core()->rules->setRulesData( $this->lesson->ID, 'lesson_id' );
BWFAN_Core()->rules->setRulesData( $this->topic, 'topic' );
BWFAN_Core()->rules->setRulesData( $this->topic->ID, 'topic_id' );
BWFAN_Core()->rules->setRulesData( $this->progress, 'progress' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['user_id'] = $this->user->ID;
$data_to_send['global']['user'] = $this->user;
$data_to_send['global']['course_id'] = $this->course->ID;
$data_to_send['global']['course'] = $this->course;
$data_to_send['global']['lesson_id'] = $this->lesson->ID;
$data_to_send['global']['lesson'] = $this->lesson;
$data_to_send['global']['topic'] = $this->topic;
$data_to_send['global']['topic_id'] = $this->topic->ID;
$data_to_send['global']['progress'] = $this->progress;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
$user = get_user_by( 'ID', $global_data['user_id'] );
$course = get_post( $global_data['course_id'] );
$lesson = get_post( $global_data['lesson_id'] );
$topic = get_post( $global_data['topic_id'] );
ob_start();
?>
<li>
<strong><?php echo esc_html__( 'User:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . '?user-edit.php?user_id=' . $user->ID; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $user->user_nicename ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Course:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $course->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $course->post_title ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Lesson:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $lesson->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $lesson->post_title ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Lesson:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $topic->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $topic->post_title ); ?></a>
</li>
<?php
return ob_get_clean();
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'topic' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['topic'] ) ) ) {
$set_data = array(
'user_id' => $task_meta['global']['user_id'],
'email' => $task_meta['global']['email'],
'user' => $task_meta['global']['user'],
'course' => $task_meta['global']['course'],
'course_id' => $task_meta['global']['course_id'],
'lesson' => $task_meta['global']['lesson'],
'lesson_id' => $task_meta['global']['lesson_id'],
'topic' => $task_meta['global']['topic'],
'topic_id' => $task_meta['global']['topic_id'],
'progress' => $task_meta['global']['progress'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function get_email_event() {
return $this->email;
}
public function get_user_id_event() {
return $this->user->ID;
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->user = get_user_by( 'ID', BWFAN_Common::$events_async_data['user_id'] );
$this->course = get_post( BWFAN_Common::$events_async_data['course_id'] );
$this->lesson = get_post( BWFAN_Common::$events_async_data['lesson_id'] );
$this->topic = get_post( BWFAN_Common::$events_async_data['topic_id'] );
$this->progress = BWFAN_Common::$events_async_data['progress'];
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
/** check any topic case */
if ( ! isset( $automation_data['event_meta'] ) || ! isset( $automation_data['event_meta']['topics'] ) || 'any' === $automation_data['event_meta']['topics'] ) {
return true;
}
/** check if specific topic selected */
if ( ! is_array( $automation_data['event_meta']['topic_id'] ) || 0 === count( $automation_data['event_meta']['topic_id'] ) ) {
return false;
}
$ids = array_column( $automation_data['event_meta']['topic_id'], 'id' );
return in_array( intval( $automation_data['topic_id'] ), array_map( 'intval', $ids ), true );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->user = get_user_by( 'ID', BWFAN_Common::$events_async_data['user_id'] );
$this->course = get_post( BWFAN_Common::$events_async_data['course_id'] );
$this->lesson = get_post( BWFAN_Common::$events_async_data['lesson_id'] );
$this->topic = get_post( BWFAN_Common::$events_async_data['topic_id'] );
$this->progress = BWFAN_Common::$events_async_data['progress'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['user'] = $this->user;
$automation_data['course'] = $this->course;
$automation_data['lesson'] = $this->lesson;
$automation_data['topic'] = $this->topic;
$automation_data['progress'] = $this->progress;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [
[
'id' => 'topics',
'label' => __( 'Topics', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Topic', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Topics', 'wp-marketing-automations-pro' ),
'value' => 'selected_topic'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => "",
],
[
"id" => 'topic_id',
"label" => __( 'Select Topic', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_topics',
'slug' => 'ld_topics',
'labelText' => 'topic'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Topic is required", 'wp-marketing-automations-pro' ),
"multiple" => true,
'toggler' => [
'fields' => [
[
'id' => 'topics',
'value' => 'selected_topic',
]
]
],
]
];
}
/** set default values */
public function get_default_values() {
return array( "topics" => "any" );
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
if ( 0 === intval( $contact->get_wpid() ) ) {
return [ 'status' => 0, 'type' => 'user_not_found' ];
}
/** fetching last topic id */
$topic_id = BWFAN_Learndash_Common::get_last_user_activity_by_type( $contact->get_wpid(), 'topic' );
/** return if no topic available as event is completed a topic */
if ( empty( $topic_id ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'User has not completed any topic', 'wp-marketing-automations-pro' ) ];
}
/** handling if selected_topic is selected */
if ( isset( $automation_data['event_meta']['topics'] ) && 'selected_topic' === $automation_data['event_meta']['topics'] ) {
$event_topic_ids = array_column( $automation_data['event_meta']['topic_id'], 'id' );
if ( ! in_array( $topic_id, $event_topic_ids ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No topics are matching with event topics', 'wp-marketing-automations-pro' ) ];
}
}
$this->email = $contact->get_email();
$this->contact_id = $cid;
$this->user_id = $contact->get_wpid();
$this->user = get_user_by( 'ID', $contact->get_wpid() );
$this->topic_id = $topic_id;
$this->topic = get_post( $topic_id );
$this->lesson_id = learndash_get_lesson_id( $topic_id );
$this->lesson = get_post( $this->lesson_id );
$this->course_id = learndash_get_course_id( $this->lesson_id );
$this->course = get_post( $this->course_id );
$course_progress = learndash_user_get_course_progress( $contact->get_wpid(), $this->course_id, 'legacy' );
$this->progress = array( $this->course_id => $course_progress );
return array_merge( $automation_data, [
'contact_id' => $this->contact_id,
'email' => $this->email,
'user_id' => $this->user_id,
'user' => $this->user,
'topic_id' => $this->topic_id,
'topic' => $this->topic,
'lesson_id' => $this->lesson_id,
'lesson' => $this->lesson,
'course_id' => $this->course_id,
'course' => $this->course,
'progress' => $this->progress
] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_learndash_active() ) {
return 'BWFAN_LD_User_Completes_Topic';
}

View File

@@ -0,0 +1,345 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_LD_User_Enrolled_Into_Course extends BWFAN_Event {
private static $instance = null;
public $user_id = 0;
public $course_id = 0;
public $access_list = 0;
public $email = '';
private function __construct() {
$this->event_merge_tag_groups = array( 'learndash_course', 'learndash_user', 'learndash_course', 'learndash_group', 'bwf_contact' );
$this->event_name = __( 'User Enrolled into a Course', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs when the user is enrolled into a course.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'learndash_course',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->v2 = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'learndash_update_course_access', [ $this, 'process' ], 10, 4 );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $user_id
* @param $course_id
* @param $access_list
* @param $remove
*/
public function process( $user_id, $course_id, $access_list, $remove ) {
if ( false !== $remove ) {
return;
}
$data = $this->get_default_data();
$data['user_id'] = $user_id;
$data['course_id'] = $course_id;
$data['access_list'] = $access_list ? $access_list : '';
$user = get_user_by( 'id', absint( $user_id ) );
$data['email'] = $user instanceof WP_User && is_email( $user->user_email ) ? $user->user_email : '';
$this->send_async_call( $data );
}
/**
* Set up rules data
*
* @param $automation_data
*/
public function pre_executable_actions( $automation_data ) {
BWFAN_Core()->rules->setRulesData( $this->user_id, 'user_id' );
BWFAN_Core()->rules->setRulesData( $this->course_id, 'course_id' );
BWFAN_Core()->rules->setRulesData( $this->access_list, 'access_list' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['user_id'] = $this->user_id;
$data_to_send['global']['course_id'] = $this->course_id;
$data_to_send['global']['access_list'] = $this->access_list;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$user = get_user_by( 'id', $global_data['user_id'] );
$course = get_post( $global_data['course_id'] );
?>
<li>
<strong><?php echo esc_html__( 'User:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . '?user-edit.php?user_id=' . $user->ID; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $user->user_nicename ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Course:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $course->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $course->post_title ); ?></a>
</li>
<?php
return ob_get_clean();
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'course_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['course_id'] ) ) ) {
$set_data = array(
'user_id' => intval( $task_meta['global']['user_id'] ),
'course_id' => intval( $task_meta['global']['course_id'] ),
'access_list' => $task_meta['global']['access_list'],
'email' => $task_meta['global']['email'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function get_email_event() {
return $this->email;
}
public function get_user_id_event() {
return $this->user_id;
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->user_id = BWFAN_Common::$events_async_data['user_id'];
$this->course_id = BWFAN_Common::$events_async_data['course_id'];
$this->access_list = BWFAN_Common::$events_async_data['access_list'];
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
/** check any course case */
if ( ! isset( $automation_data['event_meta'] ) || ! isset( $automation_data['event_meta']['courses'] ) || 'any' === $automation_data['event_meta']['courses'] ) {
return true;
}
/** check if specific course selected */
if ( ! is_array( $automation_data['event_meta']['course_id'] ) || 0 === count( $automation_data['event_meta']['course_id'] ) ) {
return false;
}
$ids = array_column( $automation_data['event_meta']['course_id'], 'id' );
return in_array( intval( $automation_data['course_id'] ), array_map( 'intval', $ids ), true );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->user_id = BWFAN_Common::$events_async_data['user_id'];
$this->course_id = BWFAN_Common::$events_async_data['course_id'];
$this->access_list = BWFAN_Common::$events_async_data['access_list'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['user_id'] = $this->user_id;
$automation_data['course_id'] = $this->course_id;
$automation_data['email'] = $this->email;
$automation_data['access_list'] = $this->access_list;
return $automation_data;
}
public function get_fields_schema() {
return [
[
'id' => 'courses',
'label' => __( 'Courses', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Course', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Courses', 'wp-marketing-automations-pro' ),
'value' => 'selected_course'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => "",
],
[
"id" => 'course_id',
"label" => __( 'Select Course', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_courses',
'slug' => 'ld_courses',
'labelText' => 'course'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Course is required", 'wp-marketing-automations-pro' ),
"multiple" => true,
'toggler' => [
'fields' => [
[
'id' => 'courses',
'value' => 'selected_course',
]
]
],
]
];
}
/** set default values */
public function get_default_values() {
return array( "courses" => "any" );
}
/**
* store data in normalize table
*
* @return bool
*/
public function is_db_normalize() {
return true;
}
/**
* store data in normalized table
*
* @return void
*/
public function execute_normalization( $post_parameters ) {
if ( ! class_exists( 'BWFCRM_DB_Normalization_Learndash' ) ) {
return;
}
$db_ins = BWFCRM_DB_Normalization_Learndash::get_instance();
$db_ins->add_course_enrolled( $post_parameters['user_id'], $post_parameters['course_id'] );
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
if ( 0 === intval( $contact->get_wpid() ) ) {
return [ 'status' => 0, 'type' => 'user_not_found' ];
}
/** fetching last course id */
$course_id = BWFAN_Learndash_Common::get_last_user_activity_by_type( $contact->get_wpid(), 'access' );
/** return if no course available as event is enrolled to course */
if ( empty( $course_id ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'User is not enrolled to any course', 'wp-marketing-automations-pro' ) ];
}
/** handling if selected_course is selected */
if ( isset( $automation_data['event_meta']['courses'] ) && 'selected_course' === $automation_data['event_meta']['courses'] ) {
$event_course_ids = array_column( $automation_data['event_meta']['course_id'], 'id' );
if ( ! in_array( $course_id, $event_course_ids ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No courses are matching with event courses', 'wp-marketing-automations-pro' ) ];
}
}
$this->email = $contact->get_email();
$this->contact_id = $cid;
$this->user_id = $contact->get_wpid();
$this->course_id = $course_id;
$this->access_list = BWFAN_Learndash_Common::get_course_access_lists( $course_id );
return array_merge( $automation_data, [
'contact_id' => $this->contact_id,
'email' => $this->email,
'user_id' => $this->user_id,
'course_id' => $this->course_id,
'access_list' => $this->access_list
] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_learndash_active() ) {
return 'BWFAN_LD_User_Enrolled_Into_Course';
}

View File

@@ -0,0 +1,278 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_LD_User_Remove_From_Group extends BWFAN_Event {
private static $instance = null;
public $user_id = 0;
public $group_id = 0;
public $email = '';
private function __construct() {
$this->event_merge_tag_groups = array( 'learndash_course', 'learndash_user', 'learndash_group', 'bwf_contact' );
$this->event_name = __( 'User Removed From Group', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs when the user is removed from a group.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'learndash_group',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->priority = 80;
$this->v2 = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'ld_removed_group_access', [ $this, 'process' ], 10, 2 );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $user_id
* @param $group_id
*
* @return void
*/
public function process( $user_id, $group_id ) {
$data = $this->get_default_data();
$data['user_id'] = $user_id;
$data['group_id'] = $group_id;
$user = get_user_by( 'id', absint( $user_id ) );
$data['email'] = $user instanceof WP_User && is_email( $user->user_email ) ? $user->user_email : '';
$this->send_async_call( $data );
}
/**
* Set up rules data
*
* @param $automation_data
*/
public function pre_executable_actions( $automation_data ) {
BWFAN_Core()->rules->setRulesData( $this->user_id, 'user_id' );
BWFAN_Core()->rules->setRulesData( $this->group_id, 'group_id' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['user_id'] = $this->user_id;
$data_to_send['global']['group_id'] = $this->group_id;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$user = get_user_by( 'id', $global_data['user_id'] );
$group = get_post( $global_data['group_id'] );
?>
<li>
<strong><?php echo esc_html__( 'User:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . '?user-edit.php?user_id=' . $user->ID; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $user->user_nicename ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Removed from Group:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $group->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $group->post_title ); ?></a>
</li>
<?php
return ob_get_clean();
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'group_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['group_id'] ) ) ) {
$set_data = array(
'user_id' => intval( $task_meta['global']['user_id'] ),
'group_id' => intval( $task_meta['global']['group_id'] ),
'email' => $task_meta['global']['email'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function get_email_event() {
return $this->email;
}
public function get_user_id_event() {
return $this->user_id;
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->user_id = BWFAN_Common::$events_async_data['user_id'];
$this->group_id = BWFAN_Common::$events_async_data['group_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
/** check any group case */
if ( ! isset( $automation_data['event_meta'] ) || ! isset( $automation_data['event_meta']['groups'] ) || 'any' === $automation_data['event_meta']['groups'] ) {
return true;
}
/** check if specific group selected */
if ( ! is_array( $automation_data['event_meta']['group_id'] ) || 0 === count( $automation_data['event_meta']['group_id'] ) ) {
return false;
}
$ids = array_column( $automation_data['event_meta']['group_id'], 'id' );
return in_array( intval( $automation_data['group_id'] ), array_map( 'intval', $ids ), true );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->user_id = BWFAN_Common::$events_async_data['user_id'];
$this->group_id = BWFAN_Common::$events_async_data['group_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['user_id'] = $this->user_id;
$automation_data['group_id'] = $this->group_id;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [
[
'id' => 'groups',
'label' => __( 'Groups', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Group', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Groups', 'wp-marketing-automations-pro' ),
'value' => 'selected_group'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => "",
],
[
"id" => 'group_id',
"label" => __( 'Select Groups', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_groups',
'slug' => 'ld_groups',
'labelText' => 'group'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Group is required", 'wp-marketing-automations-pro' ),
"multiple" => true,
'toggler' => [
'fields' => [
[
'id' => 'groups',
'value' => 'selected_group',
]
]
],
]
];
}
/** set default values */
public function get_default_values() {
return array( "groups" => "any" );
}
/**
* store data in normalize table
*
* @return bool
*/
public function is_db_normalize() {
return true;
}
/**
* store data in normalized table
*
* @return void
*/
public function execute_normalization( $post_parameters ) {
if ( ! class_exists( 'BWFCRM_DB_Normalization_Learndash' ) ) {
return;
}
$db_ins = BWFCRM_DB_Normalization_Learndash::get_instance();
$db_ins->remove_group_access( $post_parameters['user_id'], $post_parameters['group_id'] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_learndash_active() ) {
return 'BWFAN_LD_User_Remove_From_Group';
}

View File

@@ -0,0 +1,292 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_LD_User_Removed_From_Course extends BWFAN_Event {
private static $instance = null;
public $user_id = 0;
public $course_id = 0;
public $access_list = 0;
public $email = '';
private function __construct() {
$this->event_merge_tag_groups = array( 'learndash_user', 'learndash_course', 'bwf_contact' );
$this->event_name = __( 'User Removed from a Course', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs when the user is removed from a course.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'learndash_course',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = __( 'LearnDash', 'wp-marketing-automations-pro' );
$this->priority = 20;
$this->v2 = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'learndash_update_course_access', [ $this, 'process' ], 10, 4 );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $user_id
* @param $course_id
* @param $access_list
* @param $remove
*/
public function process( $user_id, $course_id, $access_list, $remove ) {
if ( true !== $remove ) {
return;
}
$data = $this->get_default_data();
$data['user_id'] = $user_id;
$data['course_id'] = $course_id;
$data['access_list'] = $access_list ? $access_list : '';
$user = get_user_by( 'id', absint( $user_id ) );
$data['email'] = $user instanceof WP_User && is_email( $user->user_email ) ? $user->user_email : '';
$this->send_async_call( $data );
}
/**
* Set up rules data
*
* @param $automation_data
*/
public function pre_executable_actions( $automation_data ) {
BWFAN_Core()->rules->setRulesData( $this->user_id, 'user_id' );
BWFAN_Core()->rules->setRulesData( $this->course_id, 'course_id' );
BWFAN_Core()->rules->setRulesData( $this->access_list, 'access_list' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['user_id'] = $this->user_id;
$data_to_send['global']['course_id'] = $this->course_id;
$data_to_send['global']['access_list'] = $this->access_list;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$user = get_user_by( 'id', $global_data['user_id'] );
$course = get_post( $global_data['course_id'] );
?>
<li>
<strong><?php echo esc_html__( 'User:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . '?user-edit.php?user_id=' . $user->ID; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $user->user_nicename ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Course:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo admin_url() . 'post.php?post=' . $course->ID . '&action=edit'; //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( $course->post_title ); ?></a>
</li>
<?php
return ob_get_clean();
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'course_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['course_id'] ) ) ) {
$set_data = array(
'user_id' => intval( $task_meta['global']['user_id'] ),
'course_id' => intval( $task_meta['global']['course_id'] ),
'access_list' => $task_meta['global']['access_list'],
'email' => $task_meta['global']['email'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function get_email_event() {
return $this->email;
}
public function get_user_id_event() {
return $this->user_id;
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->user_id = BWFAN_Common::$events_async_data['user_id'];
$this->course_id = BWFAN_Common::$events_async_data['course_id'];
$this->access_list = BWFAN_Common::$events_async_data['access_list'];
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
/** check any course case */
if ( ! isset( $automation_data['event_meta'] ) || ! isset( $automation_data['event_meta']['courses'] ) || 'any' === $automation_data['event_meta']['courses'] ) {
return true;
}
/** check if specific course selected */
if ( ! is_array( $automation_data['event_meta']['course_id'] ) || 0 === count( $automation_data['event_meta']['course_id'] ) ) {
return false;
}
$ids = array_column( $automation_data['event_meta']['course_id'], 'id' );
return in_array( intval( $automation_data['course_id'] ), array_map( 'intval', $ids ), true );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->user_id = BWFAN_Common::$events_async_data['user_id'];
$this->course_id = BWFAN_Common::$events_async_data['course_id'];
$this->access_list = BWFAN_Common::$events_async_data['access_list'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['user_id'] = $this->user_id;
$automation_data['course_id'] = $this->course_id;
$automation_data['email'] = $this->email;
$automation_data['access_list'] = $this->access_list;
return $automation_data;
}
public function get_fields_schema() {
return [
[
'id' => 'courses',
'label' => __( 'Courses', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Course', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Courses', 'wp-marketing-automations-pro' ),
'value' => 'selected_course'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => "",
],
[
"id" => 'course_id',
"label" => __( 'Select Course', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'ld_courses',
'slug' => 'ld_courses',
'labelText' => 'course'
],
"allowFreeTextSearch" => false,
"required" => true,
"errorMsg" => __( "Course is required", 'wp-marketing-automations-pro' ),
"multiple" => true,
'toggler' => [
'fields' => [
[
'id' => 'courses',
'value' => 'selected_course',
]
]
],
]
];
}
/** set default values */
public function get_default_values() {
return array( "courses" => "any" );
}
/**
* store data in normalize table
*
* @return bool
*/
public function is_db_normalize() {
return true;
}
/**
* store data in normalized table
*
* @return void
*/
public function execute_normalization( $post_parameters ) {
if ( ! class_exists( 'BWFCRM_DB_Normalization_Learndash' ) ) {
return;
}
$db_ins = BWFCRM_DB_Normalization_Learndash::get_instance();
$db_ins->remove_course_access( $post_parameters['user_id'], $post_parameters['course_id'] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_learndash_active() ) {
return 'BWFAN_LD_User_Removed_From_Course';
}

View File

@@ -0,0 +1,2 @@
<?php
// Silence is golden.

View File

@@ -0,0 +1,2 @@
<?php
//silence is golden

View File

@@ -0,0 +1,40 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class BWFAN_LD_Courses {
private static $ins = null;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function get_slug() {
return 'ld_courses';
}
public function get_options( $search = '' ) {
$courses = BWFAN_Learndash_Common::get_learndash_courses( $search, 10 );
$data = isset( $courses['results'] ) ? $courses['results'] : [];
$prepared_data = [];
foreach ( $data as $course ) {
$prepared_data[ $course['id'] ] = $course['text'];
}
return $prepared_data;
}
}
if ( class_exists( 'BWFAN_Load_Custom_Search' ) ) {
BWFAN_Load_Custom_Search::register( 'BWFAN_LD_Courses' );
}

View File

@@ -0,0 +1,46 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class BWFAN_LD_Groups {
private static $ins = null;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function get_slug() {
return 'ld_groups';
}
public function get_options( $search ) {
$groups = get_posts( array(
'post_type' => 'groups',
'posts_per_page' => 10,
'status' => 'publish',
's' => $search,
'suppress_filters' => false
) );
$group_array = array();
foreach ( $groups as $group ) {
$group_array[ $group->ID ] = $group->post_title;
}
return $group_array;
}
}
if ( class_exists( 'BWFAN_Load_Custom_Search' ) ) {
BWFAN_Load_Custom_Search::register( 'BWFAN_LD_Groups' );
}

View File

@@ -0,0 +1,44 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class BWFAN_LD_Lessons {
private static $ins = null;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function get_slug() {
return 'ld_lessons';
}
public function get_options( $search ) {
$lessons = get_posts( array(
'post_type' => 'sfwd-lessons',
'posts_per_page' => 10,
'status' => 'publish',
's' => $search,
'suppress_filters' => false
) );
$lesson_array = array();
foreach ( $lessons as $lesson ) {
$lesson_array[ $lesson->ID ] = $lesson->post_title;
}
return $lesson_array;
}
}
if ( class_exists( 'BWFAN_Load_Custom_Search' ) ) {
BWFAN_Load_Custom_Search::register( 'BWFAN_LD_Lessons' );
}

View File

@@ -0,0 +1,47 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class BWFAN_LD_Quiz {
private static $ins = null;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function get_slug() {
return 'ld_quiz';
}
public function get_options( $search ) {
$quizzes = get_posts( array(
'post_type' => 'sfwd-quiz',
'posts_per_page' => 10,
'status' => 'publish',
's' => $search,
'suppress_filters' => false
) );
$quiz_array = array();
foreach ( $quizzes as $quiz ) {
$quiz_array[ $quiz->ID ] = $quiz->post_title;
}
return $quiz_array;
}
}
if ( class_exists( 'BWFAN_Load_Custom_Search' ) ) {
BWFAN_Load_Custom_Search::register( 'BWFAN_LD_Quiz' );
}

View File

@@ -0,0 +1,46 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class BWFAN_LD_Topics {
private static $ins = null;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function get_slug() {
return 'ld_topics';
}
public function get_options( $search ) {
$topics = get_posts( array(
'post_type' => 'sfwd-topic',
'posts_per_page' => 10,
'status' => 'publish',
's' => $search,
'suppress_filters' => false
) );
$topic_array = array();
foreach ( $topics as $topic ) {
$topic_array[ $topic->ID ] = $topic->post_title;
}
return $topic_array;
}
}
if ( class_exists( 'BWFAN_Load_Custom_Search' ) ) {
BWFAN_Load_Custom_Search::register( 'BWFAN_LD_Topics' );
}

View File

@@ -0,0 +1,2 @@
<?php
//silence is golden