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,561 @@
<?php
final class BWFAN_Wp_CreateUser extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Create User', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action creates a WordPress user', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'email' );
$this->excluded_events = array(
'wcs_before_end',
'wcs_before_renewal',
'wcs_card_expiry',
'wcs_created',
'wcs_renewal_payment_complete',
'wcs_renewal_payment_failed',
'wcs_status_changed',
'wcs_trial_end',
'wp_user_creation',
'wp_user_login',
);
$this->action_priority = 5;
$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' ) ) {
global $wp_roles;
$roles = $wp_roles->roles;
$data = $roles;
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'user_roles', $data );
}
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-action-<?php esc_html_e( $unique_slug ); ?>">
<#
selected_event = BWFAN_Auto.uiDataDetail.trigger.event;
selected_user_role = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'user_role')) ? data.actionSavedData.data.user_role : '';
email = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'email')) ? data.actionSavedData.data.email : '';
first_name = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'first_name')) ? data.actionSavedData.data.first_name : '';
last_name = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'last_name')) ? data.actionSavedData.data.last_name : '';
is_allow = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'allow_notification_email')) ? 'checked' : '';
is_allow_temp_pass = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'allow_temp_pass')) ? 'checked' : '';
#>
<div data-element-type="bwfan-editor" class="bwfan-<?php esc_html_e( $unique_slug ); ?>">
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Email (optional)', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<input required type="text"
class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>"
name="bwfan[{{data.action_id}}][data][email]" placeholder="E.g. customer_email@gmail.com"
value="{{email}}"/>
<div class="clearfix bwfan_field_desc"><?php esc_html_e( "Leave blank unless you want to create an account with an email using dynamic merge tags.", 'wp-marketing-automations-pro' ); ?></div>
<div class="clearfix bwfan_field_desc"><?php esc_html_e( "Action will be skipped if a user already exists.", 'wp-marketing-automations-pro' ); ?></div>
</div>
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'First Name (optional)', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<input required type="text"
class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>"
name="bwfan[{{data.action_id}}][data][first_name]" placeholder="E.g. John"
value="{{first_name}}"/>
</div>
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Last Name (optional)', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<input required type="text"
class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>"
name="bwfan[{{data.action_id}}][data][last_name]" placeholder="E.g. Doe"
value="{{last_name}}"/>
</div>
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Role', 'wp-marketing-automations-pro' ); ?>
<?php
$message = __( 'Roles which defines user authority and responsibility', 'wp-marketing-automations-pro' );
echo $this->add_description( esc_html__( $message ), '2xl', 'right' ); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<select required id="bwfan_user_role"
class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>"
name="bwfan[{{data.action_id}}][data][user_role]">
<option value="">Choose role</option>
<#
if(_.has(data.actionFieldsOptions, 'user_roles') &&
_.isObject(data.actionFieldsOptions.user_roles) ) {
_.each( data.actionFieldsOptions.user_roles, function( value, key ){
selected = (key == selected_user_role) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value.name}}</option>
<# })
}
#>
</select>
</div>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<label for="bwfan_allow_temp_pass">
<input type="checkbox" name="bwfan[{{data.action_id}}][data][allow_temp_pass]"
id="bwfan_allow_temp_pass" value="1" {{is_allow_temp_pass}}/>
<?php
esc_html_e( "Save password in plain text for one time use via merge tag &#123;&#123;new_user_password&#125;&#125;", 'wp-marketing-automations-pro' );
$message = __( 'By enabling this option, I understand the security implication that the password will be temporarily saved in plain text. This password will automatically be deleted after a one-time use of the merge tag.', 'wp-marketing-automations-pro' );
echo $this->add_description( esc_html__( $message ), 'l' ); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
</div>
<div class="bwfan_email_tracking bwfan-mb-15">
<label for="bwfan_allow_notification_email">
<input type="checkbox" name="bwfan[{{data.action_id}}][data][allow_notification_email]"
id="bwfan_allow_notification_email" value="1" {{is_allow}}/>
<?php
esc_html_e( 'Send WordPress User notification email', 'wp-marketing-automations-pro' );
$message = __( 'Send default WordPress user email notification on user creation', 'wp-marketing-automations-pro' );
echo $this->add_description( esc_html__( $message ), 'l' ); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
</div>
<?php
do_action( 'bwfan_' . $this->get_slug() . '_setting_html', $this )
?>
</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(
'email' => ! empty( $task_meta['data']['email'] ) ? BWFAN_Common::decode_merge_tags( $task_meta['data']['email'] ) : '',
'user_role' => isset( $task_meta['data']['user_role'] ) ? BWFAN_Common::decode_merge_tags( $task_meta['data']['user_role'] ) : 'subscriber',
'first_name' => isset( $task_meta['data']['first_name'] ) ? BWFAN_Common::decode_merge_tags( $task_meta['data']['first_name'] ) : '',
'last_name' => isset( $task_meta['data']['last_name'] ) ? BWFAN_Common::decode_merge_tags( $task_meta['data']['last_name'] ) : '',
'allow_notification_email' => ( isset( $task_meta['data']['allow_notification_email'] ) ) ? 1 : 0,
'allow_temp_pass' => ( isset( $task_meta['data']['allow_temp_pass'] ) ) ? 1 : 0,
);
/** passing the global email in case email blank in data */
if ( empty( $data_to_set['email'] ) && ! empty( $task_meta['global']['email'] ) ) {
$data_to_set['email'] = $task_meta['global']['email'];
}
remove_action( 'network_site_new_created_user', 'wp_send_new_user_notifications' );
remove_action( 'network_site_users_created_user', 'wp_send_new_user_notifications' );
remove_action( 'network_user_new_created_user', 'wp_send_new_user_notifications' );
remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
if ( bwfan_is_woocommerce_active() ) {
remove_action( 'woocommerce_created_customer_notification', array( WC_Emails::instance(), 'customer_new_account' ) );
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array(
'email' => ! empty( $step_data['email'] ) ? BWFAN_Common::decode_merge_tags( $step_data['email'] ) : '',
'username' => isset( $step_data['username'] ) && ! empty( $step_data['username'] ) ? BWFAN_Common::decode_merge_tags( $step_data['username'] ) : '',
'user_role' => isset( $step_data['user_role'] ) ? BWFAN_Common::decode_merge_tags( $step_data['user_role'] ) : 'subscriber',
'first_name' => isset( $step_data['first_name'] ) ? BWFAN_Common::decode_merge_tags( $step_data['first_name'] ) : '',
'last_name' => isset( $step_data['last_name'] ) ? BWFAN_Common::decode_merge_tags( $step_data['last_name'] ) : '',
'allow_notification_email' => isset( $step_data['allow_notification_email'] ) && 1 === absint( $step_data['allow_notification_email'] ) ? 1 : 0,
'allow_temp_pass' => isset( $step_data['allow_temp_pass'] ) && 1 === absint( $step_data['allow_temp_pass'] ) ? 1 : 0,
);
/** passing the global email in case email blank in data */
if ( empty( $data_to_set['email'] ) && ! empty( $automation_data['global']['email'] ) ) {
$data_to_set['email'] = $automation_data['global']['email'];
}
remove_action( 'network_site_new_created_user', 'wp_send_new_user_notifications' );
remove_action( 'network_site_users_created_user', 'wp_send_new_user_notifications' );
remove_action( 'network_user_new_created_user', 'wp_send_new_user_notifications' );
remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
if ( bwfan_is_woocommerce_active() ) {
remove_action( 'woocommerce_created_customer_notification', array( WC_Emails::instance(), 'customer_new_account' ) );
}
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 || ( isset( $result['status'] ) && 3 === intval( $result['status'] ) ) ) {
return array(
'status' => 3,
'message' => isset( $result['message'] ) ? $result['message'] : __( 'User Created Successfully', 'wp-marketing-automations-pro' ),
);
}
if ( is_array( $result ) && isset( $result['message'] ) ) {
return array(
'status' => 4,
'message' => $result['message'],
);
}
return array(
'status' => 4,
'message' => __( 'Some error occurred in creating user', '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();
}
$user_email = sanitize_text_field( $this->data['email'] );
/** Verifying if email is valid or not */
if ( ! is_email( trim( $user_email ) ) ) {
$resp = array(
'status' => 4,
'message' => __( 'Email ID is not valid', 'wp-marketing-automations-pro' ),
);
return $resp;
}
$user_first_name = isset( $this->data['first_name'] ) ? $this->data['first_name'] : '';
$user_last_name = isset( $this->data['last_name'] ) ? $this->data['last_name'] : '';
$user_role = isset( $this->data['user_role'] ) && ! empty( $this->data['user_role'] ) ? sanitize_text_field( $this->data['user_role'] ) : '';
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
/** if order id present */
if ( isset( $this->data['order_id'] ) && ! empty( $this->data['order_id'] && bwfan_is_woocommerce_active() ) ) {
$order_object = wc_get_order( $this->data['order_id'] );
$user_first_name = $order_object instanceof WC_order ? $order_object->get_billing_first_name() : '';
$user_last_name = $order_object instanceof WC_order ? $order_object->get_billing_last_name() : '';
}
/** if wc subscription id present */
if ( isset( $this->data['subscription_id'] ) && ! empty( $this->data['subscription_id'] ) && bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
$subscription = wcs_get_subscription( $this->data['subscription_id'] );
$order_id = ! empty( $subscription ) ? $subscription->get_parent_id() : 0;
if ( ! empty( $order_id ) ) {
$order_object = wc_get_order( $order_id );
$user_first_name = $order_object instanceof WC_order ? $order_object->get_billing_first_name() : '';
$user_last_name = $order_object instanceof WC_order ? $order_object->get_billing_last_name() : '';
}
}
$password = substr( str_shuffle( $chars ), 0, 8 );
/** checking user_id exists with the user_email */
$user_data = get_user_by( 'email', $user_email );
if ( $user_data instanceof WP_User ) {
return array(
'status' => 3,
'message' => __( 'User already exists with ' . $user_email, 'wp-marketing-automations-pro' ),
);
}
$user_id = 0;
try {
$user_id = wp_create_user( $user_email, $password, $user_email );
} catch ( Error $e ) {
$err_message = $e->getMessage();
}
/** if user already exists with the given email id */
if ( is_wp_error( $user_id ) ) {
$user_error = $user_id->errors;
$resp = array(
'status' => 4,
'message' => end( $user_error ),
);
return $resp;
}
$user = new WP_User( $user_id );
$user->set_role( $user_role );
$user_first_name = trim( $user_first_name );
$user_last_name = trim( $user_last_name );
! empty( $user_first_name ) && update_user_meta( $user_id, 'first_name', $user_first_name );
! empty( $user_last_name ) && update_user_meta( $user_id, 'last_name', $user_last_name );
/** Send new user email if 'Allow send email' option ticked */
if ( isset( $this->data['allow_notification_email'] ) && 1 === absint( $this->data['allow_notification_email'] ) ) {
wp_send_new_user_notifications( $user_id, 'user' );
}
/** create bwf contact and maintain the password of user in contact meta table */
$created_date = date( 'Y-m-d H:i:s' );
$contact = new WooFunnels_Contact( $user_id, $user_email );
$contact->set_wpid( $user_id );
$contact->set_email( $user_email );
$contact->set_f_name( $user_first_name );
$contact->set_l_name( $user_last_name );
$contact->set_creation_date( $created_date );
$contact->save();
/** only allow saving of contact password when checked in create user action */
if ( $this->data['allow_temp_pass'] ) {
$contact->update_meta( 'bwfan_userpassword', $password );
}
$success_mesg = empty( $err_message ) ? __( 'User Created.', 'wp-marketing-automations-pro' ) : $err_message;
return [
'status' => 3,
'message' => $success_mesg,
];
}
public function process_v2() {
$user_email = sanitize_text_field( $this->data['email'] );
$username = sanitize_text_field( $this->data['username'] );
$username = ! empty( $username ) && ! username_exists( $username ) ? $username : $user_email;
/** Verifying if email is valid or not */
if ( ! is_email( trim( $user_email ) ) ) {
return $this->skipped_response( __( 'Email is not valid', 'wp-marketing-automations-pro' ) );
}
$user_first_name = isset( $this->data['first_name'] ) ? $this->data['first_name'] : '';
$user_last_name = isset( $this->data['last_name'] ) ? $this->data['last_name'] : '';
$user_role = isset( $this->data['user_role'] ) && ! empty( $this->data['user_role'] ) ? sanitize_text_field( $this->data['user_role'] ) : '';
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
/** if order id present */
if ( isset( $this->data['order_id'] ) && ! empty( $this->data['order_id'] ) ) {
$order_object = wc_get_order( $this->data['order_id'] );
$user_first_name = $order_object->get_billing_first_name();
$user_last_name = $order_object->get_billing_last_name();
}
/** if wc subscription id present */
if ( isset( $this->data['subscription_id'] ) && ! empty( $this->data['subscription_id'] ) ) {
$subscription = wcs_get_subscription( $this->data['subscription_id'] );
$order_id = $subscription->get_parent_id();
$order_object = wc_get_order( $order_id );
$user_first_name = $order_object->get_billing_first_name();
$user_last_name = $order_object->get_billing_last_name();
}
$password = substr( str_shuffle( $chars ), 0, 8 );
/** checking user_id exists with the user_email */
$user_data = get_user_by( 'email', $user_email );
if ( $user_data instanceof WP_User ) {
return $this->success_message( __( 'User already exists with ' . $user_email, 'wp-marketing-automations-pro' ) );
}
$user_id = 0;
try {
$user_id = wp_create_user( $username, $password, $user_email );
} catch ( Error $e ) {
return $this->error_response( "Error: " . $e->getMessage() );
}
if ( is_wp_error( $user_id ) ) {
return $this->error_response( $user_id->errors );
}
/** create display name using first name and appending lastname to it if available*/
$display_name = trim( $user_first_name );
$display_name = empty( $display_name ) ? trim( $user_last_name ) : $display_name . ' ' . trim( $user_last_name );
if ( ! empty( trim( $display_name ) ) ) {
wp_update_user( array( 'ID' => $user_id, 'display_name' => trim( $display_name ) ) );
}
$user = new WP_User( $user_id );
$user->set_role( $user_role );
$user_first_name = trim( $user_first_name );
$user_last_name = trim( $user_last_name );
$nickname = trim( $user_first_name );
! empty( $user_first_name ) && update_user_meta( $user_id, 'first_name', $user_first_name );
! empty( $user_last_name ) && update_user_meta( $user_id, 'last_name', $user_last_name );
! empty( $nickname ) && update_user_meta( $user_id, 'nickname', $nickname );
/** Send new user email if 'Allow send email' option ticked */
if ( isset( $this->data['allow_notification_email'] ) && 1 === absint( $this->data['allow_notification_email'] ) ) {
wp_send_new_user_notifications( $user_id, 'user' );
}
/** create bwf contact and maintain the password of user in contact meta table */
$created_date = date( 'Y-m-d H:i:s' );
$contact = new WooFunnels_Contact( $user_id, $user_email );
$contact->set_wpid( $user_id );
$contact->set_email( $user_email );
$contact->set_f_name( $user_first_name );
$contact->set_l_name( $user_last_name );
$contact->set_creation_date( $created_date );
$contact->save();
/** only allow saving of contact password when checked in create user action */
if ( $this->data['allow_temp_pass'] ) {
$contact->update_meta( 'bwfan_userpassword', $password );
}
return $this->success_message( __( 'User Created.', 'wp-marketing-automations-pro' ) );
}
/**
* Add back all the action which we unhooked before creating a user
*/
public function after_executing_task() {
add_action( 'network_site_new_created_user', 'wp_send_new_user_notifications' );
add_action( 'network_site_users_created_user', 'wp_send_new_user_notifications' );
add_action( 'network_user_new_created_user', 'wp_send_new_user_notifications' );
add_action( 'register_new_user', 'wp_send_new_user_notifications' );
add_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10, 2 );
if ( bwfan_is_woocommerce_active() ) {
add_action( 'woocommerce_created_customer_notification', array( WC_Emails::instance(), 'customer_new_account' ) );
}
}
public function get_fields_schema() {
global $wp_roles;
$roles = $wp_roles->roles;
$data = [];
foreach ( $roles as $key => $role ) {
$data[ $key ] = $role['name'];
}
$data = BWFAN_PRO_Common::prepared_field_options( $data );
return [
[
"id" => 'email',
"label" => __( 'Email', 'wp-marketing-automations-pro' ),
"type" => 'text',
"placeholder" => __( 'E.g. customer_email@gmail.com', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper bwfan-field-wp_createuser',
"description" => __( "If user already exists with the above email, action will be ignored", 'wp-marketing-automations-pro' ),
"required" => true,
],
[
"id" => 'username',
"label" => __( 'Username', 'wp-marketing-automations-pro' ),
"type" => 'text',
"placeholder" => __( 'E.g. username123', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper bwfan-field-wp_createuser',
"hint" => __( "If a username is not set or already exists, your email will be used as the username", 'wp-marketing-automations-pro' ),
"required" => false,
],
[
"id" => 'first_name',
"label" => __( 'First Name', 'wp-marketing-automations-pro' ),
"type" => 'text',
"placeholder" => __( 'E.g. John', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper bwfan-field-wp_createuser',
"description" => "",
"required" => false,
],
[
"id" => 'last_name',
"label" => __( 'Last Name', 'wp-marketing-automations-pro' ),
"type" => 'text',
"placeholder" => __( 'E.g. Doe', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper bwfan-field-wp_createuser',
"description" => "",
"required" => false,
],
[
"id" => 'user_role',
"label" => __( 'Role', 'wp-marketing-automations-pro' ),
"type" => 'radio',
"options" => $data,
'placeholder' => __( 'Choose role', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper bwfan-field-wp_createuser',
"tip" => __( "Roles which defines user authority and responsibility", 'wp-marketing-automations-pro' ),
"description" => "",
"required" => false,
],
[
"id" => 'allow_temp_pass',
"checkboxlabel" => __( 'Save password in plain text for one time use via merge tag {{new_user_password}} ', 'wp-marketing-automations-pro' ),
"type" => 'checkbox',
"class" => '',
"hint" => __( "By enabling this option, I understand the security implication that the password will be temporarily saved in plain text. This password will automatically be deleted after a one-time use of the merge tag.", 'wp-marketing-automations-pro' ),
"required" => false,
],
[
"id" => 'allow_notification_email',
"checkboxlabel" => __( 'Allow WordPress user notification email ', 'wp-marketing-automations-pro' ),
"type" => 'checkbox',
"class" => '',
"hint" => __( "Send default WordPress user email notification on user creation.", 'wp-marketing-automations-pro' ),
"required" => false,
]
];
}
public function get_default_values() {
return [
'user_role' => 'subscriber',
];
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_Wp_CreateUser';

View File

@@ -0,0 +1,191 @@
<?php
final class BWFAN_WP_Custom_Callback extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Custom Callback', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action is used by developers to run the custom code through callback', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'callback_name' );
$this->action_priority = 15;
$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 type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
callback_name = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'callback_name')) ? data.actionSavedData.data.callback_name : '';
#>
<div class="bwfan-<?php echo esc_html__( $this->get_slug() ); ?> bwfan-mb-15">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Callback Name', 'wp-marketing-automations-pro' ); ?></label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0">
<input class="bwfan-input-wrapper" type="text" name="bwfan[{{data.action_id}}][data][callback_name]" id="bwfan_callback_name" value="{{callback_name}}"/>
</div>
<div class="bwfan_field_desc"><?php echo esc_html__( 'Enter the callback name', 'wp-marketing-automations-pro' ); ?></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 ) {
$this->set_data_for_merge_tags( $task_meta );
$callback = $task_meta['data']['callback_name'];
$data_to_set = array();
$data_to_set['callback_name'] = $callback;
foreach ( $task_meta['global'] as $key1 => $value1 ) {
$data_to_set[ $key1 ] = $value1;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$callback = $step_data['callback_name'];
$data_to_set = array();
$data_to_set['callback_name'] = $callback;
foreach ( $automation_data['global'] as $key1 => $value1 ) {
$data_to_set[ $key1 ] = $value1;
}
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'] );
$status = $this->process();
/** Checking if required field error */
if ( isset( $status['bwfan_response'] ) ) {
return array(
'status' => 4,
'message' => $status['bwfan_response'],
);
}
return $status;
}
/**
* 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();
}
$callback = $this->data['callback_name'];
if ( empty( $callback ) ) {
return array(
'status' => 4,
'message' => __( 'No callback provided', 'wp-marketing-automations-pro' ),
);
}
if ( false === has_action( $callback ) ) {
return array(
'status' => 4,
'message' => __( 'No callback found', 'wp-marketing-automations-pro' ),
);
}
try {
do_action( $callback, $this->data );
} catch ( Error $e ) {
$error_msg = $e->getMessage();
return array(
'status' => '', // Will retry
'message' => __( 'PHP Fatal error occurred', 'wp-marketing-automations-pro' ) . ( $error_msg ? ': ' . $error_msg : '' ),
);
}
return array(
'status' => 3,
'message' => __( 'Callback Executed', 'wp-marketing-automations-pro' ),
);
}
public function process_v2() {
$callback = $this->data['callback_name'];
if ( empty( $callback ) ) {
return $this->skipped_response( __( 'No callback provided', 'wp-marketing-automations-pro' ) );
}
if ( false === has_action( $callback ) ) {
return $this->skipped_response( __( 'No callback found', 'wp-marketing-automations-pro' ) );
}
try {
do_action( $callback, $this->data );
} catch ( Error $e ) {
$error_msg = $e->getMessage();
return $this->error_response( $error_msg );
}
return $this->success_message( __( 'Callback Executed', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
return [
[
'id' => 'callback_name',
'type' => 'text',
'label' => __( 'Callback Name', 'wp-marketing-automations-pro' ),
'tip' => __( "Enter the callback name", 'wp-marketing-automations-pro' ),
"required" => true,
]
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['callback_name'] ) || empty( $data['callback_name'] ) ) {
return '';
}
return $data['callback_name'];
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_WP_Custom_Callback';

View File

@@ -0,0 +1,154 @@
<?php
final class BWFAN_WP_Debug extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Debug', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action is used by the developers for debugging of action\'s data', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'body' );
$this->action_priority = 20;
$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 type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
body = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'body')) ? data.actionSavedData.data.body : '';
#>
<div class="bwfan-<?php echo esc_html__( $this->get_slug() ); ?>">
<label for="" class="bwfan-label-title">
<?php echo esc_html__( 'Message', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput ?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<textarea required class="bwfan-input-wrapper" rows="4" placeholder="<?php echo esc_html__( 'Message', 'wp-marketing-automations-pro' ); ?>" name="bwfan[{{data.action_id}}][data][body]">{{body}}</textarea>
</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 ) {
$this->set_data_for_merge_tags( $task_meta );
$body = $task_meta['data']['body'];
$body = BWFAN_Common::decode_merge_tags( $body );
$data_to_set = array();
$data_to_set['body'] = $body;
foreach ( $task_meta['global'] as $key1 => $value1 ) {
$data_to_set[ $key1 ] = $value1;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$body = BWFAN_Common::decode_merge_tags( $step_data['body'] );
$data_to_set = array();
$data_to_set['body'] = $body;
foreach ( $automation_data['global'] as $key1 => $value1 ) {
$data_to_set[ $key1 ] = $value1;
}
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 ( $result ) {
return array(
'status' => 3,
);
}
return array(
'status' => 4,
'message' => __( 'Something went wrong', '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();
}
BWFAN_Common::log_test_data( $this->data['body'], 'debug-action', true );
return 1;
}
public function process_v2() {
BWFAN_Common::log_test_data( $this->data['body'], 'debug-action', true );
return $this->success_message( __( 'Message logged', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
return [
[
'id' => 'body',
'type' => 'textarea',
'label' => __( 'Message', 'wp-marketing-automations-pro' ),
'tip' => "",
'placeholder' => __( "Message", 'wp-marketing-automations-pro' ),
'hint' => __( 'Logs will be saved to FunnelKit Automations > Settings > Logs. Look for file with debug-action name followed by a date.', 'wp-marketing-automations-pro' ),
"description" => "",
"required" => true,
]
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['body'] ) || empty( $data['body'] ) ) {
return '';
}
return $data['body'];
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_WP_Debug';

View File

@@ -0,0 +1,606 @@
<?php
final class BWFAN_WP_HTTP_Post extends BWFAN_Action {
private static $ins = null;
public $required_fields = array( 'url', 'custom_fields' );
protected function __construct() {
$this->action_name = __( 'Send Data To Any Source (HTTP Request)', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action sends a HTTP request with key value pairs data to the entered URL', 'wp-marketing-automations-pro' );
$this->action_priority = 10;
$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 ( false === BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
return;
}
$method_data = $this->get_method_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'method_data', $method_data );
}
/**
* @return string[]
*/
public function get_method_data() {
return array(
1 => 'GET',
2 => 'POST',
4 => 'PUT',
);
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
$unique_slug2 = $unique_slug . '_2';
?>
<script type="text/html" id="tmpl-action-repeater-ui-<?php echo esc_html__( $unique_slug ); ?>">
<div class="bwfan-input-form clearfix gs-repeater-fields gs-repeater-custom-fields">
<div class="bwfan-col-sm-5 bwfan-pl-0">
<input required type="text" placeholder="Key" class="bwfan-input-wrapper" value="" name="bwfan[{{data.action_id}}][data][custom_fields][field][{{data.index}}]"/>
</div>
<div class="bwfan-col-sm-6 bwfan-p-0">
<input required type="text" placeholder="Value" class="bwfan-input-wrapper bwfan-input-merge-tags" value="" name="bwfan[{{data.action_id}}][data][custom_fields][field_value][{{data.index}}]"/>
</div>
<div class="bwfan-col-sm-1 bwfan-pr-0">
<span class="bwfan-remove-repeater-field" data-groupid="{{data.action_id}}">&#10006;</span>
</div>
</div>
</script>
<script type="text/html" id="tmpl-action-repeater-ui-<?php echo esc_html__( $unique_slug2 ); ?>">
<div class="bwfan-input-form clearfix gs-repeater-fields">
<div class="bwfan-col-sm-5 bwfan-pl-0">
<input required type="text" placeholder="Key" class="bwfan-input-wrapper" value="" name="bwfan[{{data.action_id}}][data][headers][field][{{data.index}}]"/>
</div>
<div class="bwfan-col-sm-6 bwfan-p-0">
<input required type="text" placeholder="Value" class="bwfan-input-wrapper bwfan-input-merge-tags" value="" name="bwfan[{{data.action_id}}][data][headers][field_value][{{data.index}}]"/>
</div>
<div class="bwfan-col-sm-1 bwfan-pr-0">
<span class="bwfan-remove-repeater-field" data-groupid="{{data.action_id}}">&#10006;</span>
</div>
</div>
</script>
<script type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
entered_url = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'url')) ? data.actionSavedData.data.url : '';
selected_method = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'http_method')) ? data.actionSavedData.data.http_method : 2;
#>
<div class="bwfan-input-form clearfix">
<label for="" class="bwfan-label-title">
<?php
echo esc_html__( 'Enter URL', 'wp-marketing-automations-pro' );
$message = __( 'Enter a URL where data will be sent.', 'wp-marketing-automations-pro' );
echo $this->add_description( $message, '2xl', 'right' ); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<textarea required="" class="bwfan-input-wrapper" rows="3" placeholder="Webhook URL" name="bwfan[{{data.action_id}}][data][url]" spellcheck="false">{{entered_url}}</textarea>
</div>
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Method', 'wp-marketing-automations-pro' ); ?>
<?php
$message = __( 'Method to pass this data on webhook', 'wp-marketing-automations-pro' );
echo $this->add_description( esc_html__( $message ), '2xl', 'right' ); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<select required id="bwfan_http_method" class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>" name="bwfan[{{data.action_id}}][data][http_method]">
<option value="">Choose method</option>
<#
if(_.has(data.actionFieldsOptions, 'method_data') && _.isObject(data.actionFieldsOptions.method_data) ) {
_.each( data.actionFieldsOptions.method_data, function( value, key ){
selected = (key == selected_method) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
</div>
<div class="clearfix bwfan-mb-15">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Add Contact Details', 'wp-marketing-automations-pro' ); ?></label>
<div class="bwfan_field_desc bwfan-mb10"><?php esc_html_e( 'This will add the Contact ID, First Name, Last Name, Email and Phone automatically to the data', 'wp-marketing-automations-pro' ); ?></div>
<input type="button" id="bwfan_append_contact_data" class="button" value="<?php esc_html_e( 'Add Now', 'wp-marketing-automations-pro' ); ?>">
</div>
<div class="clearfix bwfan-repeater-wrap bwfan-mb10">
<label for="" class="bwfan-label-title">
<?php echo esc_html__( 'Data', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput ?>
</label>
<div class="clearfix bwfan-input-repeater bwfan-custom-fields bwfan-mb10">
<#
repeaterArr = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'custom_fields')) ? data.actionSavedData.data.custom_fields : {};
repeaterCount = _.size(repeaterArr.field);
if(repeaterCount == 0) {
repeaterArr = {field:{0:''}, field_value:{0:''}};
}
if(repeaterCount >= 0) {
h=0;
_.each( repeaterArr.field, function( value, key ){
#>
<div class="bwfan-input-form clearfix gs-repeater-fields gs-repeater-custom-fields">
<div class="bwfan-col-sm-5 bwfan-pl-0">
<input required type="text" placeholder="Key" class="bwfan-input-wrapper" value="{{repeaterArr.field[key]}}" name="bwfan[{{data.action_id}}][data][custom_fields][field][{{h}}]"/>
</div>
<div class="bwfan-col-sm-6 bwfan-p-0">
<input required type="text" placeholder="Value" class="bwfan-input-wrapper" value="{{repeaterArr.field_value[key]}}" name="bwfan[{{data.action_id}}][data][custom_fields][field_value][{{h}}]"/>
</div>
<div class="bwfan-col-sm-1 bwfan-pr-0">
<span class="bwfan-remove-repeater-field" data-groupid="{{data.action_id}}">&#10006;</span>
</div>
</div>
<# h++;
});
}
#>
</div>
<div class="bwfan-col-sm-12 bwfan-custom-fields-repeater-data bwfan-pl-0 bwfan-mb10">
<a href="#" class="bwfan-add-repeater-data bwfan-repeater-ui" data-repeater-slug="<?php echo esc_html__( $unique_slug ); ?>" data-groupid="{{data.action_id}}" data-count="{{repeaterCount}}"><i class="dashicons dashicons-plus-alt"></i></a>
</div>
</div>
<div class="clearfix bwfan-repeater-wrap bwfan-mb10">
<label for="" class="bwfan-label-title">
<?php echo esc_html__( 'Headers', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput ?>
</label>
<div class="clearfix bwfan-input-repeater bwfan-mb10">
<#
repeaterArr2 = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'headers')) ? data.actionSavedData.data.headers : {};
repeaterCount2 = _.size(repeaterArr2.field);
if(repeaterCount2 == 0) {
repeaterArr2 = {field:{0:''}, field_value:{0:''}};
}
if(repeaterCount2 >= 0) {
h=0;
_.each( repeaterArr2.field, function( value, key ){
#>
<div class="bwfan-input-form clearfix gs-repeater-fields">
<div class="bwfan-col-sm-5 bwfan-pl-0">
<input type="text" placeholder="Key" class="bwfan-input-wrapper" value="{{repeaterArr2.field[key]}}" name="bwfan[{{data.action_id}}][data][headers][field][{{h}}]"/>
</div>
<div class="bwfan-col-sm-6 bwfan-p-0">
<input type="text" placeholder="Value" class="bwfan-input-wrapper" value="{{repeaterArr2.field_value[key]}}" name="bwfan[{{data.action_id}}][data][headers][field_value][{{h}}]"/>
</div>
<div class="bwfan-col-sm-1 bwfan-pr-0">
<span class="bwfan-remove-repeater-field" data-groupid="{{data.action_id}}">&#10006;</span>
</div>
</div>
<# h++;
});
}
#>
</div>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-mb-15">
<a href="#" class="bwfan-add-repeater-data bwfan-repeater-ui" data-repeater-slug="<?php echo esc_html__( $unique_slug2 ); ?>" data-groupid="{{data.action_id}}" data-count="{{repeaterCount2}}"><i class="dashicons dashicons-plus-alt"></i></a>
</div>
</div>
<div class="clearfix bwfan-mb-15">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Send test data via HTTP Post', 'wp-marketing-automations-pro' ); ?></label>
<div class="bwfan_field_desc bwfan-mb10"><?php esc_html_e( 'This will POST the key value pairs with dummy data to the specified URL', 'wp-marketing-automations-pro' ); ?></div>
<input type="button" id="bwfan_test_http_post_btn" class="button" value="<?php esc_html_e( 'Send Now', 'wp-marketing-automations-pro' ); ?>">
</div>
</script>
<script>
jQuery(document).ready(function ($) {
const contact_data = {
'contact_id': '{{contact_id}}',
'contact_email': '{{contact_email}}',
'contact_first_name': '{{contact_first_name}}',
'contact_last_name': '{{contact_last_name}}',
'contact_phone': '{{contact_phone}}'
}
/* Send test data to zap */
$(document).on('click', '#bwfan_test_http_post_btn', function () {
var el = $(this);
var form_data = $('#bwfan-actions-form-container').bwfan_serializeAndEncode();
form_data = bwfan_deserialize_obj(form_data);
var group_id = $('.bwfan-selected-action').attr('data-group-id');
var data_to_send = form_data.bwfan[group_id];
data_to_send.source = BWFAN_Auto.uiDataDetail.trigger.source;
data_to_send.event = BWFAN_Auto.uiDataDetail.trigger.event;
data_to_send._wpnonce = bwfanParams.ajax_nonce;
data_to_send.automation_id = bwfan_automation_data.automation_id;
el.prop('disabled', true);
var ajax = new bwf_ajax();
ajax.ajax('send_test_http_post', data_to_send);
ajax.success = function (resp) {
el.prop('disabled', false);
if (resp.status == true) {
let $iziWrap = $("#modal_automation_success");
if ($iziWrap.length > 0) {
$iziWrap.iziModal('setTitle', resp.msg);
$iziWrap.iziModal('open');
}
} else {
swal({
type: 'error',
title: resp.msg,
});
}
};
});
$(document).on('click', '#bwfan_append_contact_data', function () {
const elem = $(".bwfan-custom-fields-repeater-data a");
let new_row_index = elem.attr('data-count');
const action_id = $('.bwfan-remove-repeater-field').data('groupid');
/** Clean empty rows */
$(".gs-repeater-custom-fields").each(function () {
let elem = $(this);
if ('' === elem.find("input.bwfan-input-wrapper").val()) {
elem.remove();
}
});
let html = "";
_.each(contact_data, function (value, key) {
html += '<div class="bwfan-input-form clearfix gs-repeater-fields gs-repeater-custom-fields">';
html += '<div class="bwfan-col-sm-5 bwfan-pl-0">';
html += '<input required type="text" placeholder="Key" class="bwfan-input-wrapper" value="' + key + '" name="bwfan[' + action_id + '][data][custom_fields][field][' + new_row_index + ']"/>'
html += '</div>';
html += '<div class="bwfan-col-sm-6 bwfan-p-0">';
html += '<input required type="text" placeholder="Value" class="bwfan-input-wrapper" value="' + value + '" name="bwfan[' + action_id + '][data][custom_fields][field_value][' + new_row_index + ']"/>'
html += '</div>';
html += '<div class="bwfan-col-sm-1 bwfan-pr-0">';
html += '<span class="bwfan-remove-repeater-field" data-groupid="' + action_id + '">&#10006;</span>';
html += '</div></div>';
new_row_index++;
});
elem.attr('data-count', new_row_index);
$(".bwfan-custom-fields").append(html);
});
});
</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['email'] = $task_meta['global']['email'];
$data_to_set['url'] = BWFAN_Common::decode_merge_tags( $task_meta['data']['url'] );
$fields = $task_meta['data']['custom_fields']['field'];
$fields_value = $task_meta['data']['custom_fields']['field_value'];
$http_method = $task_meta['data']['http_method'];
$custom_fields = array();
foreach ( $fields as $key1 => $field_id ) {
$custom_fields[ $field_id ] = BWFAN_Common::decode_merge_tags( $fields_value[ $key1 ] );
}
$data_to_set['custom_fields'] = $custom_fields;
$header_fields = $task_meta['data']['headers']['field'];
$header_fields_value = $task_meta['data']['headers']['field_value'];
$header_fields_final = array();
foreach ( $header_fields as $key1 => $field_id ) {
$header_fields_final[ $field_id ] = BWFAN_Common::decode_merge_tags( $header_fields_value[ $key1 ] );
}
$data_to_set['headers'] = $header_fields_final;
$data_to_set['method'] = $http_method;
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['email'] = $automation_data['global']['email'];
$data_to_set['url'] = BWFAN_Common::decode_merge_tags( $step_data['url'] );
$fields = isset( $step_data['custom_fields'] ) ? $step_data['custom_fields'] : [];
$http_method = $step_data['http_method'];
$custom_fields = array();
foreach ( $fields as $field ) {
$key = BWFAN_Common::decode_merge_tags( $field['field'] );
$custom_fields[ $key ] = BWFAN_Common::decode_merge_tags( $field['field_value'] );
}
$data_to_set['custom_fields'] = $custom_fields;
$header_fields = isset( $step_data['headers'] ) ? $step_data['headers'] : [];
$header_fields_final = array();
foreach ( $header_fields as $field ) {
$header_fields_final[ $field['field'] ] = BWFAN_Common::decode_merge_tags( $field['field_value'] );
}
$data_to_set['headers'] = $header_fields_final;
$data_to_set['method'] = $http_method;
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 ( isset( $result['response'] ) && 200 === $result['response'] ) {
return array(
'status' => 3
);
}
$error_message = 'Response Status Code: ' . $result['response'];
if ( is_array( $result['body'] ) ) {
$error_message .= isset( $result['body']['message'] ) ? ' Error: ' . $result['body']['message'] : ( isset( $result['body']['msg'] ) ? ' Error: ' . $result['body']['msg'] : '' );
}
return array(
'status' => 1,
'message' => $error_message
);
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$endpoint_url = $this->data['url'];
$params_data = $this->data['custom_fields'];
$headers = [];
if ( isset( $this->data['headers'] ) ) {
foreach ( $this->data['headers'] as $header_key => $header_value ) {
if ( empty( $header_key ) || empty( $header_value ) ) {
continue;
}
$headers[ $header_key ] = $header_value;
}
}
/** Check if nested data */
$params_data = $this->check_for_nested_data( $params_data );
/** if content type is application/json then send parameters in json format */
if ( 1 !== intval( $this->data['method'] ) && isset( $headers['Content-Type'] ) && 'application/json' === strtolower( $headers['Content-Type'] ) ) {
$params_data = wp_json_encode( $params_data );
}
return $this->make_wp_requests( $endpoint_url, $params_data, $headers, $this->data['method'] );
}
public function process_v2() {
$endpoint_url = $this->data['url'];
$params_data = $this->data['custom_fields'];
$headers = [];
if ( isset( $this->data['headers'] ) ) {
foreach ( $this->data['headers'] as $header_key => $header_value ) {
if ( empty( $header_key ) || empty( $header_value ) ) {
continue;
}
$headers[ $header_key ] = $header_value;
}
}
/** Checking if nested data exists */
$params_data = $this->check_for_nested_data( $params_data );
/** if content type is application/json then send parameters in json format */
if ( 1 !== intval( $this->data['method'] ) && isset( $headers['Content-Type'] ) && 'application/json' === strtolower( $headers['Content-Type'] ) ) {
$params_data = wp_json_encode( $params_data );
}
$this->make_wp_requests( $endpoint_url, $params_data, $headers, $this->data['method'] );
return $this->success_message( __( 'Data sent successfully.', 'wp-marketing-automations-pro' ) );
}
/**
* Get nested data
*
* @param $data
*
* @return array|mixed
*/
public function check_for_nested_data( $data ) {
if ( empty( $data ) ) {
return [];
}
$prepared_data = $data;
foreach ( $data as $key => $value ) {
if ( ! strpos( $key, '.' ) ) {
continue;
}
unset( $prepared_data[ $key ] );
$nested_keys = $this->get_nested_keys( $key, $value, $prepared_data );
$prepared_data = array_replace( $prepared_data, $nested_keys );
}
return $prepared_data;
}
/**
* Get nested data
*
* @param $total_keys
* @param $value
*
* @return array
*/
private function get_nested_keys( $total_keys, $value, $final_data ) {
$total_keys = explode( '.', $total_keys );
return $this->add_keys_dynamic( $final_data, $total_keys, $value );
}
public function add_keys_dynamic( $main_array, $keys, $value ) {
$tmp_array = &$main_array;
while ( count( $keys ) > 0 ) {
$k = array_shift( $keys );
if ( ! is_array( $tmp_array ) ) {
$tmp_array = [];
}
$tmp_array = &$tmp_array[ $k ];
}
$tmp_array = $value;
return $main_array;
}
public function get_fields_schema() {
return [
[
'id' => 'url',
'type' => 'textarea',
'label' => __( 'Enter URL', 'wp-marketing-automations-pro' ),
'placeholder' => __( 'Webhook URL', 'wp-marketing-automations-pro' ),
'tip' => __( "Enter a URL where data will be sent.", 'wp-marketing-automations-pro' ),
"description" => "",
"required" => true,
],
[
'id' => 'http_method',
'type' => 'select',
'label' => __( 'Method', 'wp-marketing-automations-pro' ),
'options' => [
[
'label' => __( 'GET', 'wp-marketing-automations-pro' ),
'value' => 1
],
[
'label' => __( 'POST', 'wp-marketing-automations-pro' ),
'value' => 2
],
[
'label' => __( 'PUT', 'wp-marketing-automations-pro' ),
'value' => 4
],
],
'placeholder' => __( 'Select', 'wp-marketing-automations-pro' ),
'tip' => "Method to pass this data on webhook",
"description" => "",
"required" => true,
],
[
'id' => 'custom_fields',
'type' => 'repeater',
'label' => __( 'Data', 'wp-marketing-automations-pro' ),
"fields" => [
[
'id' => 'field',
'label' => "",
'type' => 'text',
'placeholder' => __( 'Key', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper',
'tip' => "",
"description" => "",
"required" => false,
],
[
"id" => 'field_value',
"label" => "",
"type" => 'text',
'placeholder' => __( 'Value', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper',
"description" => "",
"required" => false,
]
]
],
[
'id' => 'headers',
'type' => 'repeater',
'label' => __( 'Headers', 'wp-marketing-automations-pro' ),
"fields" => [
[
'id' => 'field',
'label' => "",
'type' => 'text',
'placeholder' => __( 'Key', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper',
'tip' => "",
"description" => "",
"required" => false,
],
[
"id" => 'field_value',
"label" => "",
"type" => 'text',
'placeholder' => __( 'Value', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper',
"description" => "",
"required" => false,
]
]
],
[
'id' => 'send_test_data',
'type' => 'send_data',
'label' => __( 'Send Test Data via HTTP Request', 'wp-marketing-automations-pro' ),
'send_action' => 'bwf_send_test_http_post',
'send_field' => [
'url' => 'url',
'http_method' => 'http_method',
'headers' => 'headers',
'custom_fields' => 'custom_fields',
],
"hint" => __( "Send sample key-value pair data with headers to the specified URL using the selected method.", 'wp-marketing-automations-pro' )
],
];
}
public function get_default_values() {
return [
'http_method' => 2,
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['url'] ) || empty( $data['url'] ) ) {
return '';
}
return $data['url'];
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_WP_HTTP_Post';

View File

@@ -0,0 +1,124 @@
<?php
final class BWFAN_Wp_Remove_User extends BWFAN_Action {
private static $ins = null;
private function __construct() {
$this->action_name = __( 'Remove User Role', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action remove the role of user', 'wp-marketing-automations-pro' );
$this->action_priority = 10;
$this->required_fields = array( 'email', 'user_role' );
$this->support_v2 = true;
$this->support_v1 = false;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function get_view_data() {
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
$roles = get_editable_roles();
if ( empty( $roles ) ) {
return array();
}
$roles = array_map( function ( $role ) {
return $role['name'];
}, $roles );
return $roles;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['email'] = $automation_data['global']['email'];
$data_to_set['user_id'] = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : '';
$data_to_set['user_role'] = $step_data['user_role'];
return $data_to_set;
}
public function process_v2() {
$user_email = $this->data['email'];
$user_id = $this->data['user_id'];
$user = get_user_by( 'email', $user_email );
if ( ! $user instanceof WP_User && isset( $user_id ) ) {
$user = get_user_by( 'ID', $user_id );
}
if ( ! $user instanceof WP_User ) {
return $this->skipped_response( __( 'User doesn\'t exists', 'wp-marketing-automations-pro' ) );
}
$user_role = $this->data['user_role'];
/** get_editable_roles() not exists then include admin user.php file */
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once( ABSPATH . '/wp-admin/includes/user.php' );
}
$editable_roles = get_editable_roles();
if ( ! in_array( $user_role, array_keys( $editable_roles ), true ) ) {
return $this->skipped_response( __( 'Invalid user role.', 'wp-marketing-automations-pro' ) );
}
if ( ! user_can( $user->ID, $user_role ) ) {
return $this->success_message( __( 'User does not have the specified role.', 'wp-marketing-automations-pro' ) );
}
try {
// Attempt to remove the user role
$user->remove_role( $user_role );
return $this->success_message( __( 'User role removed.', 'wp-marketing-automations-pro' ) );
} catch ( Error $e ) {
return $this->skipped_response( __( 'Error While removing user role ' . $e->getMessage(), 'wp-marketing-automations-pro' ) );
}
}
public function get_fields_schema() {
global $wp_roles;
$roles = $wp_roles->roles;
$data = [];
$data[] = [
'label' => __( 'Select', 'wp-marketing-automations-pro' ),
'value' => ''
];
foreach ( $roles as $key => $role ) {
$data[] = [
'label' => $role['name'],
'value' => $key
];
}
return [
[
"id" => 'user_role',
"label" => __( 'Select User Role', 'wp-marketing-automations-pro' ),
"type" => 'wp_select',
"options" => $data,
"placeholder" => "Choose role",
"class" => '',
"tip" => '',
"description" => "",
"required" => true,
],
];
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_Wp_Remove_User';

View File

@@ -0,0 +1,306 @@
<?php
final class BWFAN_WP_Update_User_Meta extends BWFAN_Action {
private static $ins = null;
public $required_fields = array( 'custom_fields' );
protected function __construct() {
$this->action_name = __( 'Update User Meta', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action updates the WordPress user\'s meta field(s)', 'wp-marketing-automations-pro' );
$this->action_priority = 10;
$this->excluded_events = array();
$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() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-action-repeater-ui-<?php echo esc_html__( $unique_slug ); ?>">
<div class="bwfan-input-form clearfix gs-repeater-fields">
<div class="bwfan-col-sm-5 bwfan-pl-0">
<input required type="text" placeholder="Meta key" class="bwfan-input-wrapper" value="" name="bwfan[{{data.action_id}}][data][custom_fields][field][{{data.index}}]"/>
</div>
<div class="bwfan-col-sm-6 bwfan-p-0">
<input required type="text" placeholder="Meta value" class="bwfan-input-wrapper bwfan-input-merge-tags" value="" name="bwfan[{{data.action_id}}][data][custom_fields][field_value][{{data.index}}]"/>
</div>
<div class="bwfan-col-sm-1 bwfan-pr-0">
<span class="bwfan-remove-repeater-field" data-groupid="{{data.action_id}}">&#10006;</span>
</div>
</div>
</script>
<script type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<div class="bwfan-repeater-wrap">
<label for="" class="bwfan-label-title">
<?php echo esc_html__( 'Data', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput ?>
</label>
<div class="clearfix bwfan-input-repeater bwfan-mb10">
<#
repeaterArr = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'custom_fields')) ? data.actionSavedData.data.custom_fields : {};
repeaterCount = _.size(repeaterArr.field);
if(repeaterCount == 0) {
repeaterArr = {field:{0:''}, field_value:{0:''}};
}
if(repeaterCount >= 0) {
h=0;
_.each( repeaterArr.field, function( value, key ){
#>
<div class="bwfan-input-form clearfix gs-repeater-fields">
<div class="bwfan-col-sm-5 bwfan-pl-0">
<input required type="text" placeholder="Meta key" class="bwfan-input-wrapper" value="{{repeaterArr.field[key]}}" name="bwfan[{{data.action_id}}][data][custom_fields][field][{{h}}]"/>
</div>
<div class="bwfan-col-sm-6 bwfan-p-0">
<input required type="text" placeholder="Meta value" class="bwfan-input-wrapper" value="{{repeaterArr.field_value[key]}}" name="bwfan[{{data.action_id}}][data][custom_fields][field_value][{{h}}]"/>
</div>
<div class="bwfan-col-sm-1 bwfan-pr-0">
<span class="bwfan-remove-repeater-field" data-groupid="{{data.action_id}}">&#10006;</span>
</div>
</div>
<# h++;
});
}
repeaterCount = repeaterCount + 1;
#>
</div>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-mb-15">
<a href="#" class="bwfan-add-repeater-data bwfan-repeater-ui" data-repeater-slug="<?php echo esc_html__( $unique_slug ); ?>" data-groupid="{{data.action_id}}" data-count="{{repeaterCount}}"><i class="dashicons dashicons-plus-alt"></i></a>
</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();
$fields = $task_meta['data']['custom_fields']['field'];
$order_id = isset( $task_meta['global']['order_id'] ) ? $task_meta['global']['order_id'] : '';
$email = isset( $task_meta['global']['email'] ) ? $task_meta['global']['email'] : '';
$affiliate_id = isset( $task_meta['global']['affiliate_id'] ) ? $task_meta['global']['affiliate_id'] : '';
$user_id = isset( $task_meta['global']['user_id'] ) ? $task_meta['global']['user_id'] : '';
$user_empty = empty( $user_id ) ? true : false;
// get user id by order ID
if ( true === $user_empty && bwfan_is_woocommerce_active() ) {
$order_object = ! empty( $order_id ) ? wc_get_order( $order_id ) : '';
if ( $order_object instanceof WC_Order ) {
$user_id = $order_object->get_user_id();
}
$user_empty = ! empty( $user_id ) ? false : true;
}
// get user id by email if still user id is blank
if ( true === $user_empty ) {
$user_object = ! empty( $email ) ? get_user_by( 'email', $email ) : '';
if ( $user_object instanceof WP_User ) {
$user_id = $user_object->ID;
}
$user_empty = ! empty( $user_id ) ? false : true;
}
// get user id by affiliate
if ( true === $user_empty && bwfan_is_affiliatewp_active() ) {
$user_id = ! empty( $affiliate_id ) ? affwp_get_affiliate_user_id( $affiliate_id ) : '';
}
$fields_value = $task_meta['data']['custom_fields']['field_value'];
$custom_fields = array();
foreach ( $fields as $key1 => $field_id ) {
$custom_fields[ $field_id ] = BWFAN_Common::decode_merge_tags( $fields_value[ $key1 ] );
}
$data_to_set['custom_fields'] = $custom_fields;
$data_to_set['user_id'] = $user_id;
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$fields = $step_data['custom_fields'];
$order_id = isset( $automation_data['global']['order_id'] ) ? $automation_data['global']['order_id'] : '';
$email = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
$affiliate_id = isset( $automation_data['global']['affiliate_id'] ) ? $automation_data['global']['affiliate_id'] : '';
$user_id = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : '';
$user_empty = empty( $user_id ) ? true : false;
// get user id by order ID
if ( true === $user_empty && bwfan_is_woocommerce_active() ) {
$order_object = ! empty( $order_id ) ? wc_get_order( $order_id ) : '';
if ( $order_object instanceof WC_Order ) {
$user_id = $order_object->get_user_id();
}
$user_empty = ! empty( $user_id ) ? false : true;
}
// get user id by email if still user id is blank
if ( true === $user_empty ) {
$user_object = ! empty( $email ) ? get_user_by( 'email', $email ) : '';
if ( $user_object instanceof WP_User ) {
$user_id = $user_object->ID;
}
$user_empty = ! empty( $user_id ) ? false : true;
}
// get user id by affiliate
if ( true === $user_empty && bwfan_is_affiliatewp_active() ) {
$user_id = ! empty( $affiliate_id ) ? affwp_get_affiliate_user_id( $affiliate_id ) : '';
}
$custom_fields = [];
foreach ( $fields as $field ) {
$key = BWFAN_Common::decode_merge_tags( $field['field'] );
$custom_fields[ $key ] = BWFAN_Common::decode_merge_tags( $field['field_value'] );
}
$data_to_set['custom_fields'] = $custom_fields;
$data_to_set['user_id'] = $user_id;
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'] );
return $this->process();
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$params_data = $this->data['custom_fields'];
$user_id = $this->data['user_id'];
$result = array();
if ( empty( $user_id ) ) {
$result['status'] = 4;
$result['body'] = 'No user found';
return $result;
}
if ( empty( $params_data ) ) {
$result['status'] = 4;
$result['body'] = 'No data available to update';
return $result;
}
foreach ( $params_data as $key => $data ) {
if ( empty( $key ) ) {
continue;
}
update_user_meta( $user_id, $key, $data );
}
return array(
'status' => 3,
);
}
public function process_v2() {
$params_data = $this->data['custom_fields'];
$user_id = $this->data['user_id'];
if ( empty( $user_id ) ) {
return $this->skipped_response( __( 'No user found', 'wp-marketing-automations-pro' ) );
}
if ( empty( $params_data ) ) {
return $this->skipped_response( __( 'No data available to update', 'wp-marketing-automations-pro' ) );
}
foreach ( $params_data as $key => $data ) {
if ( empty( $key ) ) {
continue;
}
update_user_meta( $user_id, $key, $data );
}
return $this->success_message( __( 'Data updated in meta.', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
return [
[
'id' => 'custom_fields',
'type' => 'repeater',
'label' => __( 'Data', 'wp-marketing-automations-pro' ),
"fields" => [
[
'id' => 'field',
'label' => "",
'type' => 'text',
'placeholder' => __( "Meta key", 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper',
'tip' => "",
"description" => "",
"required" => false,
],
[
"id" => 'field_value',
"label" => "",
"type" => 'text',
'placeholder' => __( "Meta value", 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper',
"description" => "",
"required" => false,
]
]
],
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['custom_fields'] ) || empty( $data['custom_fields'] ) ) {
return '';
}
$count = count( $data['custom_fields'] );
return ( $count > 1 ) ? $count . ' fields' : $count . ' field';
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_WP_Update_User_Meta';

View File

@@ -0,0 +1,319 @@
<?php
final class BWFAN_WP_Update_User_Role extends BWFAN_Action {
private static $ins = null;
private function __construct() {
$this->action_name = __( 'Update User Role', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action updates the role of user', 'wp-marketing-automations-pro' );
$this->action_priority = 10;
$this->required_fields = array( 'email', 'user_role' );
$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(), 'user_roles_options', $data );
}
}
public function get_view_data() {
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
$roles = get_editable_roles();
if ( empty( $roles ) ) {
return array();
}
$roles = array_map( function ( $role ) {
return $role['name'];
}, $roles );
return $roles;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-action-<?php echo esc_attr__( $unique_slug ); ?>">
<#
selected_user_role = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'user_role')) ? data.actionSavedData.data.user_role : '';
selected_user_role_action = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'user_role_action')) ? data.actionSavedData.data.user_role_action : 'update';
#>
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Select User Role', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper bwfan-single-select" name="bwfan[{{data.action_id}}][data][user_role]">
<option value=""><?php echo esc_html__( 'Choose a User Role', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'user_roles_options') && _.isObject(data.actionFieldsOptions.user_roles_options) ) {
_.each( data.actionFieldsOptions.user_roles_options, function( value, key ){
selected = (key == selected_user_role) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
<label for="" class="bwfan-label-title">
<?php echo esc_html__( 'Action', 'wp-marketing-automations-pro' ); ?>
</label>
<#
actionUpdate = ( selected_user_role_action === "update" ) ? 'checked' : '';
actionAssign = ( selected_user_role_action === "assign" ) ? 'checked' : '';
#>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-pt-5 bwfan-mb10">
<label for="bwfan_user_role_update" style="padding-right:15px;">
<input type="radio" name="bwfan[{{data.action_id}}][data][user_role_action]" id="bwfan_user_role_update" value="update" {{actionUpdate}}/>
<?php esc_html_e( 'Update Existing', 'wp-marketing-automations-pro' ); ?>
</label>
<label for="bwfan_user_role_assign">
<input type="radio" name="bwfan[{{data.action_id}}][data][user_role_action]" id="bwfan_user_role_assign" value="assign" {{actionAssign}}/>
<?php
esc_html_e( 'Assign New', 'wp-marketing-automations-pro' );
?>
</label>
</div>
<div class="clearfix bwfan_field_desc bwfan-pt-5 bwfan-mb10">
Note: This action won't update the role of Administrator.
</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['email'] = $task_meta['global']['email'];
$data_to_set['user_role'] = $task_meta['data']['user_role'];
$data_to_set['user_role_action'] = $task_meta['data']['user_role_action'];
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['email'] = $automation_data['global']['email'];
$data_to_set['user_id'] = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : '';
$data_to_set['user_role'] = $step_data['user_role'];
$data_to_set['user_role_action'] = $step_data['user_role_action'];
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['status'] ) {
return array(
'status' => 3,
'message' => isset( $result['message'] ) ? $result['message'] : '',
);
}
if ( is_array( $result ) ) {
return array(
'status' => 4,
'message' => isset( $result['message'] ) ? $result['message'] : ( isset( $result['bwfan_response'] ) ? $result['bwfan_response'] : __( 'Unknown Error Occurred', '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();
}
$user_email = $this->data['email'];
$user = get_user_by( 'email', $user_email );
if ( ! $user instanceof WP_User ) {
return array(
'message' => 'User does not exists'
);
}
/** Case when no action was passed, older scheduled tasks */
$this->data['user_role_action'] = ( ! isset( $this->data['user_role_action'] ) || empty( $this->data['user_role_action'] ) ) ? 'update' : $this->data['user_role_action'];
if ( in_array( 'administrator', $user->roles, true ) && 'update' === $this->data['user_role_action'] ) {
return array(
'message' => 'User is administrator, can\'t update its role'
);
}
$user_role = $this->data['user_role'];
/** get_editable_roles() not exists then include admin user.php file */
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once( ABSPATH . '/wp-admin/includes/user.php' );
}
$editable_roles = get_editable_roles();
if ( ! in_array( $user_role, array_keys( $editable_roles ), true ) ) {
return array(
'message' => 'Invalid user role'
);
}
if ( 'assign' === $this->data['user_role_action'] ) {
$user->add_role( $user_role );
return array(
'status' => true,
'message' => 'Role `' . $user_role . '` is assigned to the user'
);
}
$user->set_role( $user_role );
return array(
'status' => true,
'message' => 'Role `' . $user_role . '` is updated to the user'
);
}
public function process_v2() {
$user_email = $this->data['email'];
$user_id = $this->data['user_id'];
$user = get_user_by( 'email', $user_email );
if ( ! $user instanceof WP_User && isset( $user_id ) ) {
$user = get_user_by( 'ID', $user_id );
}
if ( ! $user instanceof WP_User ) {
return $this->skipped_response( __( 'User doesn\'t exists', 'wp-marketing-automations-pro' ) );
}
/** Case when no action was passed, older scheduled tasks */
$this->data['user_role_action'] = ( ! isset( $this->data['user_role_action'] ) || empty( $this->data['user_role_action'] ) ) ? 'update' : $this->data['user_role_action'];
if ( in_array( 'administrator', $user->roles, true ) && 'update' === $this->data['user_role_action'] ) {
return $this->skipped_response( __( 'User is administrator, can\'t update its role', 'wp-marketing-automations-pro' ) );
}
$user_role = $this->data['user_role'];
/** get_editable_roles() not exists then include admin user.php file */
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once( ABSPATH . '/wp-admin/includes/user.php' );
}
$editable_roles = get_editable_roles();
if ( ! in_array( $user_role, array_keys( $editable_roles ), true ) ) {
return $this->skipped_response( __( 'Invalid user role', 'wp-marketing-automations-pro' ) );
}
if ( 'assign' === $this->data['user_role_action'] ) {
$user->add_role( $user_role );
return $this->success_message( __( 'Role `' . $user_role . '` is assigned to the user', 'wp-marketing-automations-pro' ) );
}
$user->set_role( $user_role );
return $this->success_message( __( 'User role updated.', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
global $wp_roles;
$roles = $wp_roles->roles;
$data = [];
$data[] = [
'label' => __( 'Select', 'wp-marketing-automations-pro' ),
'value' => ''
];
foreach ( $roles as $key => $role ) {
$data[] = [
'label' => $role['name'],
'value' => $key
];
}
return [
[
"id" => 'user_role',
"label" => __( 'Select User Role', 'wp-marketing-automations-pro' ),
"type" => 'wp_select',
"options" => $data,
"placeholder" => "Choose role",
"class" => '',
"tip" => '',
"description" => "",
"required" => true,
],
[
'id' => 'user_role_action',
'label' => __( "Action", 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( "Update Existing Role", 'wp-marketing-automations-pro' ),
'value' => 'update'
],
[
'label' => __( "Assign New Role", 'wp-marketing-automations-pro' ),
'value' => 'assign',
'tooltip' => __( "This will attach the image of highest price product of a cart/ order in the message.", 'wp-marketing-automations-pro' )
]
],
"description" => __( "Note: This action won't update the role of Administrator.", 'wp-marketing-automations-pro' ),
]
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['user_role'] ) || empty( $data['user_role'] ) ) {
return '';
}
global $wp_roles;
$roles = $wp_roles->roles;
return $roles[ $data['user_role'] ]['name'];
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_WP_Update_User_Role';

View File

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

View File

@@ -0,0 +1,29 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WP_Adv_Integration extends BWFAN_Integration {
public static $integration_type = null;
public static $headers = null;
private static $ins = null;
private function __construct() {
$this->action_dir = __DIR__;
$this->nice_name = __( 'WordPress', 'wp-marketing-automations-pro' );
$this->group_name = __( 'WordPress', 'wp-marketing-automations-pro' );
$this->group_slug = 'wp';
$this->priority = 25;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
}
/**
* Register this class as an integration.
*/
BWFAN_Load_Integrations::register( 'BWFAN_WP_Adv_Integration' );

View File

@@ -0,0 +1,52 @@
<?php
class BWFAN_WP_Adv_Source {
private static $instance = null;
public function __construct() {
add_action( 'bwfan_wp_source_loaded', [ $this, 'include_event_files' ] );
}
/**
* Ensures only one instance of the class is loaded or can be loaded.
*
* @return BWFAN_WP_Adv_Source|null
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function include_event_files( $wp_source ) {
$resource_dir = __DIR__ . '/events';
if ( false === @file_exists( $resource_dir ) ) { //phpcs:ignore PHP_CodeSniffer - Generic.PHP.NoSilencedErrors, Generic.PHP.NoSilencedErrors
return;
}
foreach ( glob( $resource_dir . '/class-*.php' ) as $_field_filename ) {
$file_data = pathinfo( $_field_filename );
if ( isset( $file_data['basename'] ) && 'index.php' === $file_data['basename'] ) {
continue;
}
$event_class = require_once( $_field_filename );
if ( ! is_string( $event_class ) || ! method_exists( $event_class, 'get_instance' ) ) {
continue;
}
/** @var $event_obj BWFAN_Event */
$event_obj = $event_class::get_instance();
BWFAN_Load_Sources::$all_events[ $wp_source->get_name() ][ $event_obj->get_slug() ] = $event_obj->get_name();
$event_obj->load_hooks();
$event_obj->set_source_type( $wp_source->get_slug() );
BWFAN_Load_Sources::register_events( $event_obj );
}
}
}
BWFAN_WP_Adv_Source::get_instance();

View File

@@ -0,0 +1,231 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WP_User_Role_Changed extends BWFAN_Event {
private static $instance = null;
public $user_id = null;
public $level_id = null;
public $email = null;
public $new_role = null;
public $old_role = null;
private function __construct() {
$this->optgroup_label = esc_html__( 'User', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'User Role Updated', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after a user role is changed.', 'wp-marketing-automations-pro' );
$this->event_merge_tag_groups = array( 'bwf_contact' );
$this->event_rule_groups = array(
'wp_user',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 105.2;
$this->support_v1 = false;
$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( 'set_user_role', [ $this, 'user_role_changed' ], 20, 3 );
}
public function user_role_changed( $user_id, $new_role, $old_role ) {
$this->process( $user_id, $new_role, $old_role );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $user_id
* @param $new_role
* @param $old_role
*
* @return void
*/
public function process( $user_id, $new_role, $old_role ) {
$data = $this->get_default_data();
$user_data = get_userdata( $user_id );
if ( ! $user_data instanceof WP_User ) {
return;
}
$data['user_id'] = $user_id;
$data['new_role'] = strtolower( $new_role );
$data['old_role'] = array_map( 'strtolower', $old_role );
$data['email'] = $user_data->user_email;
$this->send_async_call( $data );
}
/**
* fetching user roles
* @return array
*/
public function get_view_data() {
global $wp_roles;
$roles = $wp_roles->get_names();
$options = array();
if ( ! empty( $roles ) ) {
foreach ( $roles as $key => $role ) {
$options[ $key ] = $role;
}
}
return $options;
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : null;
}
public function get_user_id_event() {
return $this->user_id;
}
/**
* @return array
*/
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['user_id'] = $this->user_id;
$data_to_send['global']['new_role'] = $this->new_role;
$data_to_send['global']['old_role'] = $this->old_role;
$data_to_send['global']['email'] = $this->get_email_event();
return $data_to_send;
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
if ( ! isset( $automation_data['event_meta'] ) || empty( $automation_data['event_meta'] ) ) {
return false;
}
$from_user_roles = $automation_data['event_meta']['from_user_roles'];
$to_user_roles = $automation_data['event_meta']['to_user_roles'];
/** Status Any to Any case */
if ( 'any' === $from_user_roles && 'any' === $to_user_roles ) {
return true;
}
$old_role = $automation_data['old_role'];
$new_role = $automation_data['new_role'];
/** Status Any to User role case */
if ( 'any' === $from_user_roles ) {
return ( $new_role === $to_user_roles );
}
/** In case of new user created, old role will always be empty */
/** In case old role and from role are no equal */
if ( empty( $old_role ) || ! in_array( $from_user_roles, $old_role ) ) {
return false;
}
/** Status (user role to Any case) OR (user role to user role case) */
return ( 'any' === $to_user_roles || $new_role === $to_user_roles );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$user_id = BWFAN_Common::$events_async_data['user_id'];
$new_role = BWFAN_Common::$events_async_data['new_role'];
$old_role = BWFAN_Common::$events_async_data['old_role'];
$this->user_id = $user_id;
$this->email = BWFAN_Common::$events_async_data['email'];
$this->new_role = $new_role;
$this->old_role = $old_role;
$automation_data['user_id'] = $this->user_id;
$automation_data['email'] = $this->email;
$automation_data['new_role'] = $this->new_role;
$automation_data['old_role'] = $this->old_role;
return $automation_data;
}
/**
* v2 Method: Get field Schema
*
* @return array[]
*/
public function get_fields_schema() {
$default = [
'any' => 'Any'
];
$options = array_replace( $default, $this->get_view_data() );
$options = BWFAN_PRO_Common::prepared_field_options( $options );
return [
[
'id' => 'from_user_roles',
'type' => 'wp_select',
'label' => __( 'From User Role', 'wp-marketing-automations-pro' ),
'options' => $options,
'placeholder' => __( 'Select User Role', 'wp-marketing-automations-pro' ),
'tip' => "",
"description" => "",
"required" => true,
"errorMsg" => __( "User Role is required", 'wp-marketing-automations-pro' ),
],
[
'id' => 'to_user_roles',
'type' => 'wp_select',
'label' => __( 'To User Role', 'wp-marketing-automations-pro' ),
'options' => $options,
'placeholder' => __( 'Select User Role', 'wp-marketing-automations-pro' ),
'tip' => "",
"description" => "",
"required" => true,
"errorMsg" => __( "User Role is required", 'wp-marketing-automations-pro' ),
],
];
}
/**
* Returns default values
*
* @return string[]
*/
public function get_default_values() {
return [
'from_user_roles' => 'any',
'to_user_roles' => 'any',
];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
return 'BWFAN_WP_User_Role_Changed';

View File

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