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,441 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WCS_Before_End extends BWFAN_Event {
private static $instance = null;
public $subscription = null;
public $subscription_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact', 'wc_subscription' );
$this->event_name = esc_html__( 'Subscriptions Before End', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs every day and checks for the subscriptions which are about to expire/ end.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'wc_subscription',
'wc_customer',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' );
$this->support_lang = true;
$this->priority = 25.4;
$this->is_time_independent = true;
$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( 'bwfan_subscription_before_end_triggered', array( $this, 'bwfan_trigger_before_end' ), 10, 2 );
}
/**
* Localize data for html fields for the current event.
*/
public function admin_enqueue_assets() {
if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'last_run', $data['last_run'] );
}
}
public function get_view_data() {
$last_run = '';
if ( isset( $_GET['edit'] ) ) {
$last_run = BWFAN_Model_Automationmeta::get_meta( sanitize_text_field( $_GET['edit'] ), 'last_run' );
if ( false !== $last_run ) {
$last_run = date( get_option( 'date_format' ), strtotime( $last_run ) );
}
}
return [
'last_run' => $last_run,
];
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/html" id="tmpl-event-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
days_entered = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'days_before')) ? data.eventSavedData.days_before : '15';
hours_entered = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'hours')) ? data.eventSavedData.hours : 11;
minutes_entered = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'minutes')) ? data.eventSavedData.minutes : '';
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-12 bwfan-p-0">
<label for="bwfan-days_before" class="bwfan-label-title"><?php echo esc_html__( 'Days before subscription end', 'wp-marketing-automations-pro' ); ?></label>
<input required type="number" name="event_meta[days_before]" id="bwfan-days_before" class="bwfan-input-wrapper" value="{{days_entered}}"/>
</div>
<div class="bwfan-col-sm-12 bwfan_mt15 bwfan-p-0">
<label for="bwfan-hours" class="bwfan-label-title"><?php echo esc_html__( 'Run at following (Store) Time of a Day', 'wp-marketing-automations-pro' ); ?></label>
<input max="23" min="0" type="number" name="event_meta[hours]" id="bwfan-hours" class="bwfan-input-wrapper bwfan-input-inline" value="{{hours_entered}}" placeholder="<?php echo esc_html__( 'HH', 'wp-marketing-automations-pro' ); ?>"/>
:
<input max="59" min="0" type="number" name="event_meta[minutes]" id="bwfan-minutes" class="bwfan-input-wrapper bwfan-input-inline" value="{{minutes_entered}}" placeholder="<?php echo esc_html__( 'MM', 'wp-marketing-automations-pro' ); ?>"/>
<# if( _.has(data.eventFieldsOptions, 'last_run') && '' != data.eventFieldsOptions.last_run ) { #>
<div class="clearfix bwfan_field_desc"><?php echo esc_html__( 'This automation last ran on ', 'wp-marketing-automations-pro' ); ?>{{data.eventFieldsOptions.last_run}}</div>
<# } #>
</div>
</script>
<?php
}
/**
* This is a time independent event. A cron is run once a day and it makes all the tasks for the current event.
*
* @param $automation_id
* @param $automation_details
*
* @throws Exception
*/
public function make_task_data( $automation_id, $automation_details ) {
$date_time = new DateTime();
$current_day = $date_time->format( 'Y-m-d' );
$last_run = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'last_run' );
if ( false !== $last_run ) {
$where = [
'bwfan_automation_id' => $automation_id,
'meta_key' => 'last_run',
];
$data = [
'meta_value' => $current_day,
];
BWFAN_Model_Automationmeta::update( $data, $where );
} else {
$meta = [
'bwfan_automation_id' => $automation_id,
'meta_key' => 'last_run',
'meta_value' => $current_day,
];
BWFAN_Model_Automationmeta::insert( $meta );
}
/** @todo check the below date logic */
$days_before_end = $automation_details['meta']['event_meta']['days_before'];
$date = new \DateTime();
$date->modify( '+' . BWFAN_Common::get_timezone_offset() * HOUR_IN_SECONDS . ' seconds' ); // get site time
$date->modify( "+$days_before_end days" );
$day_start = clone $date;
$day_end = clone $date;
$day_start->setTime( 00, 00, 01 );
$day_end->setTime( 23, 59, 59 );
$day_start->modify( '-' . BWFAN_Common::get_timezone_offset() * HOUR_IN_SECONDS . ' seconds' );
$day_end->modify( '-' . BWFAN_Common::get_timezone_offset() * HOUR_IN_SECONDS . ' seconds' );
$statuses = apply_filters( 'bwfan_event_wcs_before_end_statuses', array( 'wc-active' ) );
$start_date = $day_start->format( 'Y-m-d H:i:s' );
$end_date = $day_end->format( 'Y-m-d H:i:s' );
if ( method_exists( 'BWF_WC_Compatibility', 'is_hpos_enabled' ) && BWF_WC_Compatibility::is_hpos_enabled() ) {
global $wpdb;
$status_placeholders = array_fill( 0, count( $statuses ), '%s' );
$status_placeholders = implode( ', ', $status_placeholders );
$args = [ $start_date, $end_date ];
$args = array_merge( $args, $statuses );
$query = "SELECT orders.id FROM {$wpdb->prefix}wc_orders AS orders INNER JOIN {$wpdb->prefix}wc_orders_meta AS meta ON orders.id = meta.order_id WHERE 1=1 AND ( meta.meta_key = '_schedule_end' AND meta.meta_value > %s ) AND ( meta.meta_key = '_schedule_end' AND meta.meta_value < %s ) AND orders.type = 'shop_subscription' AND orders.status IN ( $status_placeholders ) GROUP BY orders.id ORDER BY orders.date_created_gmt DESC";
$query = $wpdb->prepare( $query, $args );
$posts = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$posts = ! empty( $posts ) ? array_column( $posts, 'id' ) : [];
} else {
$query = new \WP_Query( [
'post_type' => 'shop_subscription',
'post_status' => $statuses,
'fields' => 'ids',
'posts_per_page' => - 1,
'no_found_rows' => true,
'meta_query' => [
[
'key' => '_schedule_end',
'compare' => '>',
'value' => $start_date,
],
[
'key' => '_schedule_end',
'compare' => '<',
'value' => $end_date,
],
],
] );
$posts = $query->posts;
}
if ( ! is_array( $posts ) || count( $posts ) === 0 ) {
return;
}
/** Check if automations v1 or v2 exists */
BWFAN_Core()->public->load_active_automations( $this->get_slug() );
BWFAN_Core()->public->load_active_v2_automations( $this->get_slug() );
if ( ( ! is_array( $this->automations_arr ) || count( $this->automations_arr ) === 0 ) && ( ! is_array( $this->automations_v2_arr ) || count( $this->automations_v2_arr ) === 0 ) ) {
return;
}
/**
* recurring action schedule 2 mins interval
* event slug as option key - bwf_async_event_{slug} = subscription_ids_array
* after running capture_subscription method, update the subscription id value in the option key
*/
$key = 'bwf_async_event_' . $this->get_slug() . '_' . time();
$args = [ 'key' => $key, 'aid' => $automation_id ];
if ( ! bwf_has_action_scheduled( 'bwfan_subscription_before_end_triggered', $args ) ) {
bwf_schedule_recurring_action( time(), 120, 'bwfan_subscription_before_end_triggered', $args );
update_option( $key, $posts, false );
}
}
/**
* trigger subscription before end automations
*/
public function bwfan_trigger_before_end( $option_key, $automation_id ) {
$subscriptions = get_option( $option_key, [] );
if ( empty( $subscriptions ) ) {
delete_option( $option_key );
bwf_unschedule_actions( 'bwfan_subscription_before_end_triggered', [ 'key' => $option_key, 'aid' => $automation_id ] );
return;
}
/** Validate automation */
$automation = BWFAN_Model_Automations::get_automation_with_data( $automation_id );
if ( isset( $automation['meta'] ) ) {
$meta = $automation['meta'];
unset( $automation['meta'] );
$automation = array_merge( $automation, $meta );
}
if ( 1 !== intval( $automation['status'] ) || ( 0 === absint( $automation['start'] ) && 2 === absint( $automation['v'] ) ) ) {
delete_option( $option_key );
bwf_unschedule_actions( 'bwfan_subscription_before_end_triggered', [ 'key' => $option_key, 'aid' => $automation_id ] );
return;
}
$updated_subscriptions = $subscriptions;
$start_time = time();
foreach ( $subscriptions as $key => $subscription_id ) {
/**checking 10 seconds of processing */
if ( ( time() - $start_time ) > 10 ) {
return;
}
$this->capture_subscription( $subscription_id, $automation );
unset( $updated_subscriptions[ $key ] );
update_option( $option_key, $updated_subscriptions, false );
}
}
public function capture_subscription( $subscription_id, $automation ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return;
}
/** v2 event execution start */
$this->subscription_id = $subscription_id;
$this->subscription = wcs_get_subscription( $subscription_id );
if ( 2 === absint( $automation['v'] ) ) {
return $this->run_v2( $automation, $this->get_slug() );
}
/** v2 event execution end */
$this->run_automations();
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_subscription( $data );
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
return $this->validate_subscription( $automation_data );
}
/**
* 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() {
$user_id = $this->get_user_id_event();
$email = ! empty( $user_id ) ? BWFAN_PRO_Common::get_contact_email( $user_id ) : '';
if ( empty( $email ) && is_object( $this->subscription ) ) {
$email = $this->subscription->get_billing_email();
}
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['wc_subscription_id'] = is_object( $this->subscription ) ? $this->subscription->get_id() : '';
$data_to_send['global']['wc_subscription'] = is_object( $this->subscription ) ? $this->subscription : '';
$data_to_send['global']['email'] = $email;
if ( intval( $user_id ) > 0 ) {
$data_to_send['global']['user_id'] = $user_id;
}
return $data_to_send;
}
public function get_user_id_event() {
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_user_id();
}
return 0;
}
/**
* 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();
?>
<li>
<strong><?php echo esc_html__( 'Subscription ID:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo get_edit_post_link( $global_data['wc_subscription_id'] ); //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( '#' . $global_data['wc_subscription_id'] ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Subscription Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['email'] ); ?>
</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( 'wc_subscription_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['wc_subscription_id'] ) ) && function_exists( 'wcs_get_subscription' ) ) {
$set_data = array(
'wc_subscription_id' => intval( $task_meta['global']['wc_subscription_id'] ),
'email' => $task_meta['global']['email'],
'user_id' => $task_meta['global']['user_id'],
'wc_subscription' => $task_meta['global']['wc_subscription'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->subscription, 'wc_subscription' );
BWFAN_Core()->rules->setRulesData( $this->event_automation_id, 'automation_id' );
BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->get_email_event(), $this->get_user_id_event() ), 'bwf_customer' );
}
public function get_email_event() {
return $this->subscription->get_billing_email();
}
/**
* v2 Method: Get fields schema
* @return array
*/
public function get_fields_schema() {
return [
[
'id' => 'days_before',
'type' => 'number',
'value' => "15",
"min" => '0',
'label' => __( 'Days before subscription end', 'wp-marketing-automations-pro' ),
"description" => ""
],
[
'id' => 'scheduled-everyday-at',
'type' => 'expression',
'expression' => " {{hours /}} {{minutes /}}",
'label' => 'Run at following (Store) Time of a Day',
'fields' => [
[
"id" => 'hours',
"label" => '',
"type" => 'number',
"max" => '23',
"min" => '0',
"class" => 'bwfan-input-wrapper bwfan-input-s',
"placeholder" => "HH",
"description" => "",
"required" => false,
],
[
"id" => 'minutes',
"label" => '',
"type" => 'number',
"max" => '59',
"min" => '0',
"class" => 'bwfan-input-wrapper bwfan-input-s',
"placeholder" => "MM",
"description" => "",
"required" => false,
]
],
"description" => ""
],
];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
return 'BWFAN_WCS_Before_End';
}

View File

@@ -0,0 +1,602 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WCS_Before_Renewal extends BWFAN_Event {
private static $instance = null;
public $subscription = null;
public $subscription_id = null;
public $email = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact', 'wc_subscription' );
$this->event_name = esc_html__( 'Subscriptions Before Renewal', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs every day and checks for the subscriptions which are about to occur.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'wc_subscription',
'wc_customer',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' );
$this->support_lang = true;
$this->priority = 25.3;
$this->is_time_independent = true;
$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( 'bwfan_subscription_before_renewal_triggered', [ $this, 'bwfan_trigger_before_renewal' ], 10, 3 );
}
/**
* Localize data for html fields for the current event.
*/
public function admin_enqueue_assets() {
if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'last_run', $data['last_run'] );
}
}
public function get_view_data() {
$last_run = '';
if ( isset( $_GET['edit'] ) ) {
$last_run = BWFAN_Model_Automationmeta::get_meta( sanitize_text_field( $_GET['edit'] ), 'last_run' );
if ( false !== $last_run ) {
$last_run = date( get_option( 'date_format' ), strtotime( $last_run ) );
}
}
return [
'last_run' => $last_run,
];
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/html" id="tmpl-event-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
days_entered = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'days_before')) ? data.eventSavedData.days_before : '15';
hours_entered = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'hours')) ? data.eventSavedData.hours : 11;
minutes_entered = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'minutes')) ? data.eventSavedData.minutes : '';
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-12 bwfan-p-0">
<label for="bwfan-days_before" class="bwfan-label-title"><?php echo esc_html__( 'Days before subscription renewal', 'wp-marketing-automations-pro' ); ?></label>
<input required type="number" name="event_meta[days_before]" id="bwfan-days_before" class="bwfan-input-wrapper" value="{{days_entered}}"/>
</div>
<div class="bwfan-col-sm-12 bwfan_mt15 bwfan-p-0">
<label for="bwfan-hours" class="bwfan-label-title"><?php echo esc_html__( 'Run at following (Store) Time of a Day', 'wp-marketing-automations-pro' ); ?></label>
<input max="23" min="0" type="number" name="event_meta[hours]" id="bwfan-hours" class="bwfan-input-wrapper bwfan-input-inline" value="{{hours_entered}}" placeholder="<?php echo esc_html__( 'HH', 'wp-marketing-automations-pro' ); ?>"/>
:
<input max="59" min="0" type="number" name="event_meta[minutes]" id="bwfan-minutes" class="bwfan-input-wrapper bwfan-input-inline" value="{{minutes_entered}}"
placeholder="<?php echo esc_html__( 'MM', 'wp-marketing-automations-pro' ); ?>"/>
<# if( _.has(data.eventFieldsOptions, 'last_run') && '' != data.eventFieldsOptions.last_run ) { #>
<div class="clearfix bwfan_field_desc"><?php echo esc_html__( 'This automation last ran on ', 'wp-marketing-automations-pro' ); ?>{{data.eventFieldsOptions.last_run}}</div>
<# } #>
</div>
</script>
<?php
}
/**
* This is a time independent event. A cron is run once a day and it makes all the tasks for the current event.
*
* @param $automation_id
* @param $automation_details
*
* @return void
* @throws Exception
*/
public function make_task_data( $automation_id, $automation_details ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return;
}
/** Check if automations v1 or v2 exists */
BWFAN_Core()->public->load_active_automations( $this->get_slug() );
BWFAN_Core()->public->load_active_v2_automations( $this->get_slug() );
if ( ( ! is_array( $this->automations_arr ) || count( $this->automations_arr ) === 0 ) && ( ! is_array( $this->automations_v2_arr ) || count( $this->automations_v2_arr ) === 0 ) ) {
return;
}
$this->schedule_task( $automation_id, $automation_details );
}
/**
* scheduling the task for current day and all the missing days before current day
*
* @param $automation_id
* @param $automation_details
*
* @return void
* @throws Exception
*/
public function schedule_task( $automation_id, $automation_details ) {
$date_time = new DateTime();
$current_day = $date_time->format( 'Y-m-d' );
$last_run = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'last_run' );
if ( false !== $last_run ) {
$last_run = new DateTime( date( 'Y-m-d H:i:s', strtotime( $last_run ) ) );
$last_run->modify( '+1 days' );
$last_day = $last_run->format( 'Y-m-d' );
$this->update_last_run( $automation_id, $last_day );
} else {
$last_run = clone $date_time;
$meta = [
'bwfan_automation_id' => $automation_id,
'meta_key' => 'last_run',
'meta_value' => $current_day,
];
BWFAN_Model_Automationmeta::insert( $meta );
}
$days_before_renewal = isset( $automation_details['meta']['event_meta']['days_before'] ) ? $automation_details['meta']['event_meta']['days_before'] : 0;
$timezone_offset = BWFAN_Common::get_timezone_offset();
$schedule_time = time() + MINUTE_IN_SECONDS;
do {
$date = clone $last_run;
$date->modify( '+' . $timezone_offset * HOUR_IN_SECONDS . ' seconds' ); // get site time
$date->modify( "+$days_before_renewal days" );
$day_start = clone $date;
$day_end = clone $date;
$day_start->setTime( 00, 00, 01 );
$day_end->setTime( 23, 59, 59 );
$day_start->modify( '-' . $timezone_offset * HOUR_IN_SECONDS . ' seconds' );
$day_end->modify( '-' . $timezone_offset * HOUR_IN_SECONDS . ' seconds' );
if ( method_exists( 'BWF_WC_Compatibility', 'is_hpos_enabled' ) && BWF_WC_Compatibility::is_hpos_enabled() ) {
global $wpdb;
$query = "SELECT orders.id FROM {$wpdb->prefix}wc_orders AS orders INNER JOIN {$wpdb->prefix}wc_orders_meta AS meta ON orders.id = meta.order_id WHERE 1=1 AND ( meta.meta_key = '_schedule_next_payment' AND meta.meta_value > %s ) AND ( meta.meta_key = '_schedule_next_payment' AND meta.meta_value < %s ) AND orders.type = 'shop_subscription' AND orders.status = 'wc-active' GROUP BY orders.id ORDER BY orders.date_created_gmt DESC";
$query = $wpdb->prepare( $query, $day_start->format( 'Y-m-d H:i:s' ), $day_end->format( 'Y-m-d H:i:s' ) );
$posts = $wpdb->get_col( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
} else {
$query = new WP_Query( [
'post_type' => 'shop_subscription',
'post_status' => 'wc-active',
'fields' => 'ids',
'posts_per_page' => - 1,
'no_found_rows' => true,
'meta_query' => [
[
'key' => '_schedule_next_payment',
'compare' => '>',
'value' => $day_start->format( 'Y-m-d H:i:s' ),
],
[
'key' => '_schedule_next_payment',
'compare' => '<',
'value' => $day_end->format( 'Y-m-d H:i:s' ),
],
],
] );
$posts = $query->posts;
}
if ( ! is_array( $posts ) || empty( $posts ) ) {
$last_run->modify( '+1 days' );
$last_day = $last_run->format( 'Y-m-d' );
if ( $last_run->getTimestamp() > $date_time->getTimestamp() ) {
break;
}
$this->update_last_run( $automation_id, $last_day );
continue;
}
/**
* recurring action schedule 2 mins interval
* event slug as option key - bwf_async_event_{slug} = subscription_ids_array
* after running capture_subscription method, update the subscription id value in the option key
*/
$key = 'bwf_async_event_' . $this->get_slug() . '_' . microtime( true );
$args = [ 'key' => $key, 'aid' => $automation_id, 'new' => 1 ];
if ( ! bwf_has_action_scheduled( 'bwfan_subscription_before_renewal_triggered', $args ) ) {
bwf_schedule_recurring_action( $schedule_time, 120, 'bwfan_subscription_before_renewal_triggered', $args );
bwf_options_update( $key, $posts );
$schedule_time += MINUTE_IN_SECONDS;
}
$last_run->modify( '+1 days' );
$last_day = $last_run->format( 'Y-m-d' );
if ( $last_run->getTimestamp() > $date_time->getTimestamp() ) {
break;
}
$this->update_last_run( $automation_id, $last_day );
} while ( $last_run->getTimestamp() <= $date_time->getTimestamp() );
}
/**
* updating the last day of the scheduler run
*
* @param $automation_id
* @param $last_day
*
* @return void
*/
public function update_last_run( $automation_id, $last_day ) {
$where = [
'bwfan_automation_id' => $automation_id,
'meta_key' => 'last_run',
];
BWFAN_Model_Automationmeta::update( [
'meta_value' => $last_day,
], $where );
}
/**
* Subscription action callback
*
* @param $option_key
* @param $automation_id
* @param $new
*
* @return void
*/
public function bwfan_trigger_before_renewal( $option_key, $automation_id, $new = 0 ) {
if ( 1 === $new ) {
$subscriptions = bwf_options_get( $option_key );
} else {
$subscriptions = get_option( $option_key, [] );
}
$action_args = [ 'key' => $option_key, 'aid' => $automation_id ];
if ( 1 === $new ) {
$action_args['new'] = 1;
}
if ( empty( $subscriptions ) ) {
if ( 1 === $new ) {
bwf_options_delete( $option_key );
} else {
delete_option( $option_key );
}
bwf_unschedule_actions( 'bwfan_subscription_before_renewal_triggered', $action_args );
return;
}
/** Validate automation */
$automation = BWFAN_Model_Automations::get_automation_with_data( $automation_id );
if ( isset( $automation['meta'] ) ) {
$meta = $automation['meta'];
unset( $automation['meta'] );
$automation = array_merge( $automation, $meta );
}
if ( 1 !== intval( $automation['status'] ) || ( 0 === absint( $automation['start'] ) && 2 === absint( $automation['v'] ) ) ) {
if ( 1 === $new ) {
bwf_options_delete( $option_key );
} else {
delete_option( $option_key );
}
bwf_unschedule_actions( 'bwfan_subscription_before_renewal_triggered', $action_args );
return;
}
$updated_subscriptions = $subscriptions;
$start_time = time();
foreach ( $subscriptions as $key => $subscription_id ) {
/** Checking 10 seconds of processing */
if ( ( time() - $start_time ) > 10 ) {
return;
}
$this->capture_subscription( $subscription_id, $automation );
unset( $updated_subscriptions[ $key ] );
if ( 1 === $new ) {
bwf_options_update( $option_key, $updated_subscriptions );
} else {
update_option( $option_key, $updated_subscriptions, false );
}
}
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_subscription_status( $data );
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
return $this->validate_subscription_status( $automation_data );
}
/**
* @param $data
*
* @return bool
*/
protected function validate_subscription_status( $data ) {
if ( ! isset( $data['wc_subscription_id'] ) || ! function_exists( 'wcs_get_subscription' ) ) {
$this->message_validate_event = __( 'Subscription ID is missing or WC Subscription plugin is not available.', 'wp-marketing-automations-pro' );
return false;
}
$subscription = wcs_get_subscription( $data['wc_subscription_id'] );
if ( ! $subscription instanceof WC_Subscription ) {
$this->message_validate_event = __( 'Subscription is not valid.', 'wp-marketing-automations-pro' );
return false;
}
$subscription_status = $subscription->get_status();
if ( 'active' !== $subscription_status ) {
$this->message_validate_event = __( 'Subscription is not Active.', 'wp-marketing-automations-pro' );
return false;
}
return true;
}
/**
* Run automation
*
* @param $subscription_id
* @param $automation
*
* @return false|void|null
*/
public function capture_subscription( $subscription_id, $automation ) {
$subscription_failed_status = array( 'wc-cancelled', 'wc-expired', 'wc-pending-cancel' );
$subscription = wcs_get_subscription( $subscription_id );
if ( ! $subscription instanceof WC_Subscription ) {
return;
}
$subscription_status = $subscription->get_status();
if ( in_array( $subscription_status, $subscription_failed_status, true ) ) {
return;
}
/** v2 event execution start */
$this->subscription = $subscription;
$this->email = $this->get_email_event();
if ( 2 === absint( $automation['v'] ) ) {
return $this->run_v2( $automation, $this->get_slug() );
}
/** v2 event execution end */
$this->run_automations();
}
/**
* 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() {
$user_id = $this->get_user_id_event();
$data_to_send = [ 'global' => [] ];
$this->subscription_id = is_object( $this->subscription ) ? $this->subscription->get_id() : '';
$this->subscription = $this->subscription instanceof WC_Subscription ? $this->subscription : wcs_get_subscription( $this->subscription_id );
$data_to_send['global']['wc_subscription_id'] = $this->subscription_id;
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['phone'] = $this->subscription instanceof WC_Subscription ? $this->subscription->get_billing_phone() : '';
$data_to_send['global']['wc_subscription'] = $this->subscription;
if ( intval( $user_id ) > 0 ) {
$data_to_send['global']['user_id'] = $user_id;
$email = BWFAN_PRO_Common::get_contact_email( $user_id );
if ( ! empty( $email ) ) {
$data_to_send['global']['email'] = $email;
}
}
return $data_to_send;
}
public function get_user_id_event() {
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_user_id();
}
if ( ! empty( intval( $this->subscription_id ) ) && function_exists( 'wcs_get_subscription' ) ) {
$subscription = wcs_get_subscription( intval( $this->subscription_id ) );
return $subscription instanceof WC_Subscription ? $subscription->get_user_id() : 0;
}
return 0;
}
/**
* 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();
?>
<li>
<strong><?php echo esc_html__( 'Subscription ID:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo get_edit_post_link( $global_data['wc_subscription_id'] ); //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( '#' . $global_data['wc_subscription_id'] ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Subscription Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['email'] ); ?>
</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( 'wc_subscription_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['wc_subscription_id'] ) ) && function_exists( 'wcs_get_subscription' ) ) {
$set_data = array(
'wc_subscription_id' => intval( $task_meta['global']['wc_subscription_id'] ),
'email' => $task_meta['global']['email'],
'user_id' => $task_meta['global']['user_id'],
'wc_subscription' => $task_meta['global']['wc_subscription'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->subscription, 'wc_subscription' );
BWFAN_Core()->rules->setRulesData( $this->event_automation_id, 'automation_id' );
BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->get_email_event(), $this->get_user_id_event() ), 'bwf_customer' );
}
public function get_email_event() {
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_billing_email();
}
if ( ! empty( absint( $this->subscription_id ) ) && function_exists( 'wcs_get_subscription' ) ) {
$subscription = wcs_get_subscription( absint( $this->subscription_id ) );
return $subscription instanceof WC_Subscription ? $subscription->get_billing_email() : false;
}
return false;
}
/**
* v2 Method: Get fields schema
* @return array
*/
public function get_fields_schema() {
return [
[
'id' => 'days_before',
'type' => 'number',
'value' => "15",
'label' => 'Days before subscription renewal',
"description" => ""
],
[
'id' => 'scheduled-everyday-at',
'type' => 'expression',
'expression' => " {{hours/}} {{minutes /}}",
'label' => 'Run at following (Store) Time of a Day',
'fields' => [
[
"id" => 'hours',
"label" => '',
"type" => 'number',
"max" => '23',
"class" => 'bwfan-input-wrapper bwfan-input-s',
"placeholder" => "HH",
"description" => "",
"required" => false,
],
[
"id" => 'minutes',
"label" => '',
"type" => 'number',
"max" => '59',
"class" => 'bwfan-input-wrapper bwfan-input-s',
"placeholder" => "MM",
"description" => "",
"required" => false,
]
],
"description" => ""
],
];
}
/**
* Before starting automation on a contact, validating if subscription is renewed already
*
* @param $row
*
* @return bool
*/
public function validate_v2_before_start( $row ) {
$c_date = isset( $row['c_date'] ) ? $row['c_date'] : '';
$data = isset( $row['data'] ) && ! empty( $row['data'] ) ? json_decode( $row['data'], true ) : [];
if ( empty( $c_date ) || empty( $data ) ) {
return false;
}
$subscription_id = isset( $data['global']['wc_subscription_id'] ) ? $data['global']['wc_subscription_id'] : 0;
$subscription = wcs_get_subscription( $subscription_id );
if ( ! $subscription instanceof WC_Subscription ) {
return false;
}
$subscription_status = $subscription->get_status() ?? '';
if ( in_array( $subscription_status, [ 'cancelled', 'pending-cancel' ], true ) ) {
return false;
}
$last_order_id = $subscription->get_last_order();
/** @var $last_order WC_Order */
$last_order = wc_get_order( $last_order_id );
$last_order_date = $last_order instanceof WC_Order ? $last_order->get_date_created()->getTimestamp() : 0;
if ( $last_order_date > strtotime( $c_date ) ) {
return false;
}
return true;
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
return 'BWFAN_WCS_Before_Renewal';
}

View File

@@ -0,0 +1,490 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WCS_Card_Expiry extends BWFAN_Event {
private static $instance = null;
public $expire_token_id = null;
public $expire_token_user_id = null;
public $expire_token_meta = [];
private $data = [];
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact', 'wcs_card_expiry' );
$this->event_rule_groups = array(
'wc_customer',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Customer Before Card Expiry', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs every day and checks for user\'s saved cards which are about to expire.', 'wp-marketing-automations-pro' );
$this->priority = 55;
$this->is_time_independent = true;
$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( 'bwfan_subscription_card_expiry_triggered', array( $this, 'bwfan_trigger_card_expiry' ), 10, 2 );
}
/**
* Localize data for html fields for the current event.
*/
public function admin_enqueue_assets() {
if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'last_run', $data['last_run'] );
}
}
public function get_view_data() {
$last_run = '';
if ( isset( $_GET['edit'] ) ) {
$last_run = BWFAN_Model_Automationmeta::get_meta( sanitize_text_field( $_GET['edit'] ), 'last_run' );
if ( false !== $last_run ) {
$last_run = date( get_option( 'date_format' ), strtotime( $last_run ) );
}
}
return [
'last_run' => $last_run,
];
}
/**
* 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_id = $global_data['expire_token_user_id'];
$user = get_user_by( 'ID', $user_id );
if ( false === $user ) {
return esc_html__( 'Customer not exists', 'wp-marketing-automations-pro' );
}
ob_start();
?>
<li>
<strong><?php echo esc_html__( 'Name:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $user->display_name ); ?>
</li>
<li>
<strong><?php echo esc_html__( 'Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $user->user_email ); ?>
</li>
<li>
<strong><?php echo esc_html__( 'Card:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( 'xxxx-xxxx-xxxx-' . $global_data['expire_token_meta']['last4'] ); ?>
</li>
<li>
<strong><?php echo esc_html__( 'Expiry:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['expire_token_meta']['expiry_month'] . '/' . $global_data['expire_token_meta']['expiry_year'] ); ?>
</li>
<?php
return ob_get_clean();
}
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/html" id="tmpl-event-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
days_before=(_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'days_before')) ? data.eventSavedData.days_before : '15';
hours_entered=(_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'hours')) ? data.eventSavedData.hours : 11;
minutes_entered=(_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'minutes')) ? data.eventSavedData.minutes : '';
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-12 bwfan-p-0">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Days before card expiry', 'wp-marketing-automations-pro' ); ?></label>
<input type="number" required id="" class="bwfan-input-wrapper" name="event_meta[days_before]" value="{{days_before}}">
</div>
<div class="bwfan-col-sm-12 bwfan_mt15 bwfan-p-0">
<label for="bwfan-hours" class="bwfan-label-title"><?php echo esc_html__( 'Run at following (Store) Time of a Day', 'wp-marketing-automations-pro' ); ?></label>
<input max="23" min="0" type="number" name="event_meta[hours]" id="bwfan-hours" class="bwfan-input-wrapper bwfan-input-inline" value="{{hours_entered}}" placeholder="<?php echo esc_html__( 'HH', 'wp-marketing-automations-pro' ); ?>"/>
:
<input max="59" min="0" type="number" name="event_meta[minutes]" id="bwfan-minutes" class="bwfan-input-wrapper bwfan-input-inline" value="{{minutes_entered}}" placeholder="<?php echo esc_html__( 'MM', 'wp-marketing-automations-pro' ); ?>"/>
<# if( _.has(data.eventFieldsOptions, 'last_run') && '' != data.eventFieldsOptions.last_run ) { #>
<div class="clearfix bwfan_field_desc"><?php echo esc_html__( 'This automation last ran on ', 'wp-marketing-automations-pro' ); ?>{{data.eventFieldsOptions.last_run}}</div>
<# } #>
</div>
</script>
<?php
}
private function process( $expire_token, $automation ) {
global $wpdb;
$sql = $wpdb->prepare( "select * from $wpdb->payment_tokenmeta where payment_token_id= %s;", $expire_token['token_id'] );
$token_data = $wpdb->get_results( $sql, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $token_data ) ) {
return;
}
$token_meta = [];
foreach ( $token_data as $token ) {
$meta_key = $token['meta_key'];
$meta_value = $token['meta_value'];
$token_meta[ $meta_key ] = $meta_value;
}
$this->expire_token_id = $expire_token['token_id'];
$this->expire_token_user_id = $expire_token['user_id'];
$this->expire_token_meta = $token_meta;
if ( 2 === absint( $automation['v'] ) ) {
if ( $this->may_be_running_v2_automation( $automation['ID'], $this->expire_token_user_id ) ) {
return;
}
return $this->run_v2( $automation, $this->get_slug() );
}
$this->run_automations();
}
/**
* 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']['expire_token_id'] = $this->expire_token_id;
$data_to_send['global']['expire_token_user_id'] = $this->expire_token_user_id;
$data_to_send['global']['expire_token_meta'] = $this->expire_token_meta;
$data_to_send['global']['user_id'] = $this->expire_token_user_id;
return $data_to_send;
}
/**
* 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 ) {
$meta = $task_meta['global']['expire_token_meta'];
$user_id = $task_meta['global']['expire_token_user_id'];
$user = get_user_by( 'ID', $user_id );
if ( false === $user ) {
return;
}
$set_data = array(
'credit_card_type' => $meta['card_type'],
'credit_card_expiry_month' => $meta['expiry_month'],
'credit_card_expiry_year' => $meta['expiry_year'],
'credit_card_last4' => $meta['last4'],
'email' => $user->user_email,
'user_id' => $user_id,
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
public function pre_executable_actions( $value ) {
}
public function get_email_event() {
if ( ! empty( absint( $this->expire_token_user_id ) ) ) {
$user = get_user_by( 'id', absint( $this->expire_token_user_id ) );
return ( $user instanceof WP_User ) ? $user->user_email : false;
}
return false;
}
public function get_user_id_event() {
return ! empty( absint( $this->expire_token_user_id ) ) ? absint( $this->expire_token_user_id ) : false;
}
/**
* This is a time independent event. A cron is run once a day and it makes all the tasks for the current event.
*
* @param $automation_id
* @param $automation_details
*
* @throws Exception
*/
public function make_task_data( $automation_id, $automation_details ) {
global $wpdb;
$date_time = new DateTime();
$current_day = $date_time->format( 'Y-m-d' );
$last_run = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'last_run' );
if ( false !== $last_run ) {
$where = [
'bwfan_automation_id' => $automation_id,
'meta_key' => 'last_run',
];
$data = [
'meta_value' => $current_day,
];
BWFAN_Model_Automationmeta::update( $data, $where );
} else {
$meta = [
'bwfan_automation_id' => $automation_id,
'meta_key' => 'last_run',
'meta_value' => $current_day,
];
BWFAN_Model_Automationmeta::insert( $meta );
}
$days_before = isset( $automation_details['meta']['event_meta']['days_before'] ) ? $automation_details['meta']['event_meta']['days_before'] : 0;
$date = new DateTime();
BWFAN_Common::convert_from_gmt( $date ); // get cards based on the sites timezone
$date->modify( "+$days_before days" );
$key = $date->format( 'Y' ) . '-' . $date->format( 'm' );
if ( isset( $this->data[ $key ] ) ) {
return;
}
$sql = "SELECT token_id,user_id FROM {$wpdb->prefix}woocommerce_payment_tokens as tokens
LEFT JOIN {$wpdb->payment_tokenmeta} AS m1 ON tokens.token_id = m1.payment_token_id
LEFT JOIN {$wpdb->payment_tokenmeta} AS m2 ON tokens.token_id = m2.payment_token_id
WHERE tokens.type = 'CC'
AND m1.meta_key = 'expiry_year'
AND m1.meta_value = '{$date->format('Y')}'
AND m2.meta_key = 'expiry_month'
AND m2.meta_value = '{$date->format('m')}'
AND tokens.token !=''
";
$this->data[ $key ] = $wpdb->get_results( $sql, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $this->data[ $key ] ) ) {
return;
}
/** Check if automations v1 or v2 exists */
BWFAN_Core()->public->load_active_automations( $this->get_slug() );
BWFAN_Core()->public->load_active_v2_automations( $this->get_slug() );
if ( ( ! is_array( $this->automations_arr ) || count( $this->automations_arr ) === 0 ) && ( ! is_array( $this->automations_v2_arr ) || count( $this->automations_v2_arr ) === 0 ) ) {
return;
}
/**
* recurring action schedule 2 mins interval
* event slug as option key - bwf_async_event_{slug} = subscription_ids_array
* after running capture_subscription method, update the subscription id value in the option key
*/
$option_key = 'bwf_async_event_' . $this->get_slug() . '_' . time();
$args = [ 'key' => $option_key, 'aid' => $automation_id ];
if ( ! bwf_has_action_scheduled( 'bwfan_subscription_card_expiry_triggered', $args ) ) {
bwf_schedule_recurring_action( time(), 120, 'bwfan_subscription_card_expiry_triggered', $args );
update_option( $option_key, $this->data[ $key ], false );
}
}
/**
* trigger card expiry
*/
public function bwfan_trigger_card_expiry( $option_key, $automation_id ) {
$card_token = get_option( $option_key, [] );
if ( empty( $card_token ) ) {
delete_option( $option_key );
bwf_unschedule_actions( 'bwfan_subscription_card_expiry_triggered', [ 'key' => $option_key, 'aid' => $automation_id ] );
return;
}
/** Validate automation */
$automation = BWFAN_Model_Automations::get_automation_with_data( $automation_id );
if ( isset( $automation['meta'] ) ) {
$meta = $automation['meta'];
unset( $automation['meta'] );
$automation = array_merge( $automation, $meta );
}
if ( ! function_exists( 'wcs_user_has_subscription' ) || 1 !== intval( $automation['status'] ) || ( 0 === absint( $automation['start'] ) && 2 === absint( $automation['v'] ) ) ) {
delete_option( $option_key );
bwf_unschedule_actions( 'bwfan_subscription_card_expiry_triggered', [ 'key' => $option_key, 'aid' => $automation_id ] );
return;
}
$updated_card_token = $card_token;
$start_time = time();
foreach ( $card_token as $token_key => $token ) {
if ( ! is_array( $token ) || ! isset( $token['user_id'] ) || $this->did_ran_for_user( $token['user_id'], $automation_id ) ) {
unset( $updated_card_token[ $token_key ] );
continue;
}
/** If subscription is not active for user */
if ( ! wcs_user_has_subscription( $token['user_id'], '', 'active' ) ) {
unset( $updated_card_token[ $token_key ] );
continue;
}
/** Checking 10 seconds of processing */
if ( ( time() - $start_time ) > 10 ) {
return;
}
$this->process( $token, $automation );
unset( $updated_card_token[ $token_key ] );
}
update_option( $option_key, $updated_card_token, false );
}
public function did_ran_for_user( $user_id, $automation_id ) {
if ( empty( $user_id ) || empty( $automation_id ) ) {
return false;
}
$user_data = get_userdata( $user_id );
$email = $user_data instanceof WP_USER ? $user_data->user_email : '';
/** @var WooFunnels_Contact $bwf_contact */
$bwf_contact = bwf_get_contact( absint( $user_id ), $email );
if ( 0 === absint( $bwf_contact->get_id() ) ) {
return false;
}
$contact_id = absint( $bwf_contact->get_id() );
$automation_details = BWFAN_Core()->automations->get_automation_data_meta( $contact_id );
$automation_version = isset( $automation_details['v'] ) ? intval( $automation_details['v'] ) : 1;
/** If automation is v1 */
if ( 1 === $automation_version && BWFAN_Common::is_automation_v1_active() ) {
$run_count = BWFAN_Model_Contact_Automations::get_contact_automations_run_count( $contact_id, $automation_id );
} else {
$run_count = BWFAN_PRO_Common::get_v2_automation_contact_run_count( $contact_id, $automation_id );
}
return ( $run_count > 0 );
}
/**
* checking if automation is running or has run in 30 days
*
* @param $automation_id
* @param $user_id
*
* @return bool
*/
public static function may_be_running_v2_automation( $automation_id = '', $user_id = '' ) {
if ( empty( $automation_id ) || empty( $user_id ) ) {
return false;
}
$contact_id = BWFAN_PRO_Common::get_cid_from_contact( '', $user_id );
if ( empty( $contact_id ) ) {
return false;
}
global $wpdb;
$automation_contact_table = $wpdb->prefix . 'bwfan_automation_contact';
$automation_contact_complete_table = $wpdb->prefix . 'bwfan_automation_complete_contact';
$query = $wpdb->prepare( "SELECT count(*) as count FROM {$automation_contact_table} WHERE `cid` = %d AND `aid` = %d", $contact_id, $automation_id ); //phpcs:ignore WordPress.DB.PreparedSQL
$count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( absint( $count ) > 0 ) {
return true;
}
$query = $wpdb->prepare( "SELECT count(*) as count FROM {$automation_contact_complete_table} WHERE `cid` = %d AND `aid` = %d AND DATE_FORMAT(c_date, '%%Y-%%m-%%d') >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND DATE_FORMAT(c_date, '%%Y-%%m-%%d') <= CURDATE()", $contact_id, $automation_id ); //phpcs:ignore WordPress.DB.PreparedSQL
$count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return ( absint( $count ) > 0 );
}
/**
* v2 Method: Get fields schema
* @return array
*/
public function get_fields_schema() {
return [
[
'id' => 'days_before',
'type' => 'number',
"min" => '0',
'value' => "15",
'label' => 'Days before subscription expiry',
"description" => ""
],
[
'id' => 'scheduled-everyday-at',
'type' => 'expression',
'expression' => " {{hours/}} {{minutes /}}",
'label' => 'Schedule this automation to run everyday at',
'fields' => [
[
"id" => 'hours',
"label" => '',
"type" => 'number',
"max" => '23',
"min" => '0',
"class" => 'bwfan-input-wrapper bwfan-input-s',
"placeholder" => "HH",
"description" => "",
"required" => false,
],
[
"id" => 'minutes',
"label" => '',
"type" => 'number',
"max" => '59',
"min" => '0',
"class" => 'bwfan-input-wrapper bwfan-input-s',
"placeholder" => "MM",
"description" => "",
"required" => false,
]
],
"description" => ""
],
];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
return 'BWFAN_WCS_Card_Expiry';
}

View File

@@ -0,0 +1,374 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WCS_Created extends BWFAN_Event {
private static $instance = null;
public $subscription = null;
public $order = null;
public $email = null;
public $subscription_id = null;
public $order_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact', 'wc_subscription' );
$this->event_name = esc_html__( 'Subscriptions Created', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after a new subscription is created.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'wc_subscription',
'wc_customer',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' );
$this->support_lang = true;
$this->priority = 25;
$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( 'woocommerce_checkout_subscription_created', [ $this, 'subscription_created' ], 20, 2 );
add_filter( 'bwfan_before_making_logs', array( $this, 'check_if_bulk_process_executing' ), 10, 1 );
add_action( 'wcs_api_subscription_created', [ $this, 'subscription_created' ], 20 );
add_action( 'woocommerce_admin_created_subscription', [ $this, 'subscription_created' ], 20 );
add_action( 'wfocu_subscription_created_for_upsell', [ $this, 'upsell_subscription_created' ], 10, 3 );
}
/**
* @param $subscription WC_Subscription
* @param $order WC_Order
*
* @return void
*/
public function subscription_created( $subscription, $order = '' ) {
$subscription_id = $subscription->get_id();
$this->email = $subscription->get_billing_email();
$order_id = $order instanceof WC_Order ? $order->get_id() : $subscription->get_last_order();
$this->process( $subscription_id, $order_id );
}
/**
* @param $subscription WC_Subscription
* @param $product_hash
* @param $subscription_order WC_Order
*
* @return void
*/
public function upsell_subscription_created( $subscription, $product_hash, $subscription_order ) {
$subscription_id = $subscription->get_id();
$this->email = $subscription->get_billing_email();
$order_id = $subscription_order instanceof WC_Order ? $subscription_order->get_id() : $subscription->get_last_order();
$this->process( $subscription_id, $order_id );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $subscription_id
* @param $order_id
*/
public function process( $subscription_id, $order_id ) {
$data = $this->get_default_data();
$data['wc_subscription_id'] = $subscription_id;
$data['order_id'] = $order_id;
$data['email'] = $this->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->order, 'wc_order' );
BWFAN_Core()->rules->setRulesData( $this->subscription, 'wc_subscription' );
BWFAN_Core()->rules->setRulesData( $this->event_automation_id, 'automation_id' );
BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->subscription->get_billing_email(), $this->subscription->get_user_id() ), 'bwf_customer' );
}
/**
* 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() {
$user_id = $this->get_user_id_event();
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['wc_order'] = is_object( $this->order ) ? $this->order : '';
$data_to_send['global']['order_id'] = $this->order_id;
$data_to_send['global']['wc_subscription_id'] = $this->subscription_id;
$data_to_send['global']['wc_subscription'] = is_object( $this->subscription ) ? $this->subscription : '';
$data_to_send['global']['email'] = $this->email;
if ( intval( $user_id ) > 0 ) {
$data_to_send['global']['user_id'] = $user_id;
$email = BWFAN_PRO_Common::get_contact_email( $user_id );
if ( ! empty( $email ) ) {
$data_to_send['global']['email'] = $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();
?>
<li>
<strong><?php echo esc_html__( 'Subscription ID:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo get_edit_post_link( $global_data['wc_subscription_id'] ); //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( '#' . $global_data['wc_subscription_id'] ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Subscription Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['email'] ); ?>
</li>
<?php
return ob_get_clean();
}
public function get_email_event() {
if ( $this->order instanceof WC_Order ) {
return $this->order->get_billing_email();
}
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_billing_email();
}
return false;
}
public function get_user_id_event() {
if ( $this->order instanceof WC_Order ) {
return $this->order->get_user_id();
}
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_user_id();
}
return 0;
}
/**
* 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( 'wc_subscription_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['wc_subscription_id'] ) ) && function_exists( 'wcs_get_subscription' ) ) {
$set_data = array(
'wc_subscription_id' => intval( $task_meta['global']['wc_subscription_id'] ),
'email' => $task_meta['global']['email'],
'wc_subscription' => $task_meta['global']['wc_subscription'],
'wc_order' => $task_meta['global']['wc_order'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_subscription( $data );
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
if ( ! $this->validate_subscription( $automation_data ) ) {
return false;
}
$event_meta = isset( $automation_data['event_meta'] ) ? $automation_data['event_meta'] : [];
$subscription_contains = isset( $event_meta['subscription-contains'] ) ? $event_meta['subscription-contains'] : '';
/** Any product case */
if ( empty( $subscription_contains ) || 'any' === $subscription_contains ) {
return true;
}
/** Specific product case */
$subscription = wcs_get_subscription( $automation_data['wc_subscription_id'] );
$get_selected_product = $event_meta['products'];
$ordered_products = array();
foreach ( $subscription->get_items() as $subscription_product ) {
$ordered_products[] = $subscription_product->get_product_id();
/** In case variation */
if ( $subscription_product->get_variation_id() ) {
$ordered_products[] = $subscription_product->get_variation_id();
}
}
/** Selected product and ordered products */
$product_selected = array_column( $get_selected_product, 'id' );
$ordered_products = array_unique( $ordered_products );
sort( $ordered_products );
return count( array_intersect( $product_selected, $ordered_products ) ) > 0;
}
/**
* Recalculate action's execution time with respect to order date.
* eg.
* today is 22 jan.
* order was placed on 17 jan.
* user set an email to send after 10 days of order placing.
* user setup the sync process.
* email should be sent on 27 Jan as the order date was 17 jan.
*
* @param $actions
*
* @return mixed
* @throws Exception
*/
public function recalculate_actions_time( $actions ) {
$subscription_date = $this->subscription->get_date( 'start' );
$subscription_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $subscription_date );
$actions = $this->calculate_actions_time( $actions, $subscription_date );
return $actions;
}
/**
* Capture the async data for the current event.
*
* @return false|void
*/
public function capture_async_data() {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return false;
}
$this->subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$this->order_id = BWFAN_Common::$events_async_data['order_id'];
$subscription = wcs_get_subscription( $this->subscription_id );
$order = wc_get_order( $this->order_id );
$this->subscription = $subscription;
$this->order = $order;
$this->email = BWFAN_Common::$events_async_data['email'];
$this->run_automations();
}
/**
* v2 capture data.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return $automation_data;
}
$this->subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$this->subscription = wcs_get_subscription( $this->subscription_id );
$this->order_id = BWFAN_Common::$events_async_data['order_id'];
$this->order = wc_get_order( $this->order_id );
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['wc_subscription_id'] = $this->subscription_id;
$automation_data['order_id'] = $this->order_id;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [
[
'id' => 'subscription-contains',
'label' => __( 'Subscription Contains', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Product', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Products', 'wp-marketing-automations-pro' ),
'value' => 'selected_product'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => ""
],
[
'id' => 'products',
'label' => __( 'Select Products', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'wcs_products',
'slug' => 'wcs_products',
'labelText' => 'WooCommerce Subscription products '
],
'class' => '',
'placeholder' => '',
'required' => true,
'toggler' => [
'fields' => [
[
'id' => 'subscription-contains',
'value' => 'selected_product',
]
]
],
],
];
}
/** Set default setting */
public function get_default_values() {
return [
'subscription-contains' => 'any',
];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
return 'BWFAN_WCS_Created';
}

View File

@@ -0,0 +1,432 @@
<?php
/**
* Class BWFAN_WCS_Note_Added
*/
#[AllowDynamicProperties]
final class BWFAN_WCS_Note_Added extends BWFAN_Event {
private static $instance = null;
public $subscription_id = null;
public $subscription = null;
private $is_customer_note = false;
private $comment_content = '';
private $subscriptions_note_types = [];
private $email = null;
private function __construct() {
$this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Subscription Note Added', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after a new subscriptions note is added.', 'wp-marketing-automations-pro' );
$this->event_merge_tag_groups = array( 'bwf_contact', 'wc_subscription', 'wc_subscription_note' );
$this->event_rule_groups = array(
'wc_subscription',
'wc_customer',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 30;
$this->support_lang = true;
$this->subscriptions_note_types = [
'both' => __( 'Both', 'wp-marketing-automations-pro' ),
'private' => __( 'Private', 'wp-marketing-automations-pro' ),
'public' => __( 'Customer', 'wp-marketing-automations-pro' ),
];
$this->v2 = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_filter( 'woocommerce_new_order_note_data', [ $this, 'process_note' ], 10, 2 );
}
/**
* Localize data for html fields for the current event.
*/
public function admin_enqueue_assets() {
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'subscription_note_type', $this->subscriptions_note_types );
}
/**
* Returns the current event settings set in the automation at the time of task creation.
*
* @param $value
*
* @return array
*/
public function get_automation_event_data( $value ) {
return [
'event_source' => $value['source'],
'event_slug' => $value['event'],
'validate_event' => ( isset( $value['event_meta']['validate_event'] ) ) ? 1 : 0,
];
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_subscription( $data );
}
/**
* 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();
$order = wc_get_order( $global_data['wc_subscription_id'] );
if ( $order instanceof WC_Order ) {
?>
<li>
<strong><?php esc_html_e( 'Subscription:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo get_edit_post_link( $global_data['wc_subscription_id'] ); //phpcs:ignore WordPress.Security.EscapeOutput
?>"><?php echo '#' . esc_html( $global_data['wc_subscription_id'] . ' ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ); ?></a>
</li>
<?php } ?>
<li>
<strong><?php esc_html_e( 'Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php esc_html_e( $global_data['email'] ); ?>
</li>
<li>
<strong><?php esc_html_e( 'Note:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo $global_data['wcs_note_content']; //phpcs:ignore WordPress.Security.EscapeOutput ?>
</li>
<li>
<strong><?php esc_html_e( 'Type:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php
if ( wc_string_to_bool( $global_data['wcs_customer_note_type'] ) ) {
esc_html_e( 'Note to customer', 'woocommerce' );
} else {
esc_html_e( 'Private note', 'woocommerce' );
}
?>
</li>
<?php
return ob_get_clean();
}
public function get_email_event() {
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_billing_email();
}
if ( ! empty( absint( $this->subscription_id ) ) && function_exists( 'wcs_get_subscription' ) ) {
$subscription = wcs_get_subscription( absint( $this->subscription_id ) );
return $subscription instanceof WC_Subscription ? $subscription->get_billing_email() : false;
}
return false;
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/html" id="tmpl-event-<?php esc_attr_e( $this->get_slug() ); ?>">
<div class="bwfan_mt15"></div>
<label for="bwfan-select-box-order-note" class="bwfan-label-title"><?php esc_html_e( 'Select Subscription Note Mode', 'wp-marketing-automations-pro' ); ?></label>
<div class="bwfan-select-box">
<#
selected_statuses = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'bwfan_subscription_note_type')) ? data.eventSavedData.bwfan_subscription_note_type : 'both';
#>
<select name="event_meta[bwfan_subscription_note_type]" id="bwfan-select-box-order-note" class="bwfan-input-wrapper">
<#
if(_.has(bwfan_events_js_data, 'wcs_note_added') && _.isObject(bwfan_events_js_data.wcs_note_added.subscription_note_type) ) {
_.each( bwfan_events_js_data.wcs_note_added.subscription_note_type, function( title, key ){
selected = (key == selected_statuses) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{title}}</option>
<# })
}
#>
</select>
</div>
</script>
<?php
}
/**
* Admin add customer note
*
* @param $comment_data
* @param $data
*
* @return mixed
*/
public function process_note( $comment_data, $data ) {
$subscription = wcs_get_subscription( $comment_data['comment_post_ID'] );
if ( ! $subscription instanceof WC_Subscription || 'shop_subscription' !== $subscription->get_type() || $comment_data['comment_type'] !== 'order_note' ) {
return $comment_data;
}
$data['comment_content'] = $comment_data['comment_content'];
$data['wc_subscription_id'] = $subscription->get_id();
$data['comment_data'] = $comment_data;
$data['email'] = $subscription->get_billing_email();
$this->process( $data );
return $comment_data;
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $order_id
*/
public function process( $comment_data ) {
$data = $this->get_default_data();
$data['wc_subscription_id'] = $comment_data['wc_subscription_id'];
$data['comment_content'] = $comment_data['comment_content'];
$data['is_customer_note'] = $comment_data['is_customer_note'];
$data['email'] = $comment_data['email'];
$this->send_async_call( $data );
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return false;
}
$subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$subscription = wcs_get_subscription( $subscription_id );
$this->comment_content = BWFAN_Common::$events_async_data['comment_content'];
$this->subscription = $subscription;
$this->subscription_id = $subscription_id;
$this->is_customer_note = BWFAN_Common::$events_async_data['is_customer_note'];
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
public function run_automations() {
BWFAN_Core()->public->load_active_automations( $this->get_slug() );
if ( ! is_array( $this->automations_arr ) || count( $this->automations_arr ) === 0 ) {
BWFAN_Core()->logger->log( 'Async callback: No active automations found. Event - ' . $this->get_slug(), $this->log_type );
return false;
}
$automation_actions = [];
foreach ( $this->automations_arr as $automation_id => $value1 ) {
if ( $this->get_slug() !== $value1['event'] ) {
wp_send_json( $this->get_slug() !== $value1['event'] );
continue;
}
$ran_actions = $this->handle_single_automation_run( $value1, $automation_id );
$automation_actions[ $automation_id ] = $ran_actions;
}
return $automation_actions;
}
public function get_user_id_event() {
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_user_id();
}
if ( ! empty( absint( $this->subscription_id ) ) && function_exists( 'wcs_get_subscription' ) ) {
$subscription = wcs_get_subscription( absint( $this->subscription_id ) );
return $subscription instanceof WC_Subscription ? $subscription->get_user_id() : 0;
}
return 0;
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->event_automation_id, 'automation_id' );
BWFAN_Core()->rules->setRulesData( $this->subscription, 'wc_subscription' );
BWFAN_Core()->rules->setRulesData( $this->comment_content, 'wcs_subscription_note' );
BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->subscription->get_billing_email(), $this->subscription->get_user_id() ), 'bwf_customer' );
}
/**
* 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;
}
$meta = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'event_meta' );
if ( '' === $meta || ! is_array( $meta ) || ! isset( $meta['bwfan_subscription_note_type'] ) ) {
return;
}
/** @var bool $note_type - if true then public */
$note_type = wc_string_to_bool( $this->is_customer_note );
$save_type = $meta['bwfan_subscription_note_type'];
$register_task = false;
if ( 'both' === $save_type ) {
$register_task = true;
} elseif ( 'public' === $save_type && true === $note_type ) {
$register_task = true;
} elseif ( 'private' === $save_type && true !== $note_type ) {
$register_task = true;
}
if ( false === $register_task ) {
return;
}
$data_to_send = $this->get_event_data( $save_type );
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data( $save_type = '' ) {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['wc_subscription_id'] = $this->subscription_id;
$data_to_send['global']['wcs_note_content'] = $this->comment_content;
$data_to_send['global']['wcs_customer_note_type'] = $this->is_customer_note;
$data_to_send['global']['wcs_save_note_type'] = $save_type;
$this->subscription = $this->subscription instanceof WC_Subscription ? $this->subscription : wcs_get_subscription( $this->subscription_id );
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['phone'] = $this->subscription instanceof WC_Subscription ? $this->subscription->get_billing_phone() : '';
$data_to_send['global']['wc_subscription'] = $this->subscription;
$user_id = $this->get_user_id_event();
if ( intval( $user_id ) > 0 ) {
$data_to_send['global']['user_id'] = $user_id;
$email = BWFAN_PRO_Common::get_contact_email( $user_id );
if ( ! empty( $email ) ) {
$data_to_send['global']['email'] = $email;
}
}
return $data_to_send;
}
public function set_merge_tags_data( $task_meta ) {
$subscription_id = BWFAN_Merge_Tag_Loader::get_data( 'wc_subscription_id' );
if ( ( empty( $subscription_id ) || intval( $subscription_id ) !== intval( $task_meta['global']['wc_subscription_id'] ) ) && function_exists( 'wcs_get_subscription' ) ) {
$set_data = array(
'wc_subscription_id' => intval( $task_meta['global']['wc_subscription_id'] ),
'email' => $task_meta['global']['email'],
'wc_subscription' => $task_meta['global']['wc_subscription'],
'wcs_note_content' => $task_meta['global']['wcs_note_content'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* 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;
}
$save_type = ( isset( $automation_data['event_meta'] ) && isset( $automation_data['event_meta']['bwfan_subscription_note_type'] ) ) ? $automation_data['event_meta']['bwfan_subscription_note_type'] : 'both';
/** @var bool $note_type - if true then public */
$note_type = wc_string_to_bool( $automation_data['is_customer_note'] );
if ( 'both' === $save_type ) {
return true;
} elseif ( 'public' === $save_type && true === $note_type ) {
return true;
} elseif ( 'private' === $save_type && true !== $note_type ) {
return true;
}
return false;
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return $automation_data;
}
$this->subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$this->subscription = wcs_get_subscription( $this->subscription_id );
$this->comment_content = BWFAN_Common::$events_async_data['comment_content'];
$this->is_customer_note = BWFAN_Common::$events_async_data['is_customer_note'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['wc_subscription_id'] = $this->subscription_id;
$automation_data['comment_content'] = $this->comment_content;
$automation_data['is_customer_note'] = $this->is_customer_note;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
$options = BWFAN_PRO_Common::prepared_field_options( $this->subscriptions_note_types );
return [
[
'id' => 'bwfan_subscription_note_type',
'label' => __( "Select Subscription Note Mode", 'wp-marketing-automations-pro' ),
'type' => 'wp_select',
'options' => $options,
'placeholder' => __( 'Choose note type', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper',
'tip' => "",
"description" => "",
"required" => true,
],
];
}
/** set default values */
public function get_default_values() {
return [
'bwfan_subscription_note_type' => 'both',
];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
return 'BWFAN_WCS_Note_Added';
}

View File

@@ -0,0 +1,337 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WCS_Renewal_Payment_Complete extends BWFAN_Event {
private static $instance = null;
public $subscription = null;
public $order = null;
public $email = null;
public $subscription_id = null;
public $order_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact', 'wc_subscription' );
$this->event_name = esc_html__( 'Subscriptions Renewal Payment Complete', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after a subscription renewal payment is completed.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'wc_subscription',
'wc_customer',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' );
$this->support_lang = true;
$this->priority = 25.5;
$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( 'woocommerce_subscription_renewal_payment_complete', [ $this, 'subscription_renewal_payment_complete' ], 11, 2 );
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/html" id="tmpl-event-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
is_validated = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'validate_event')) ? 'checked' : '';
#>
<div class="bwfan_mt15"></div>
<?php
$this->get_validation_html( $this->get_slug(), 'Active subscriptions only', 'Validate' );
?>
</script>
<?php
}
public function subscription_renewal_payment_complete( $subscription, $order ) {
$subscription_id = $subscription->get_id();
$order_id = $order->get_id();
$this->process( $subscription_id, $order_id );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $subscription_id
* @param $order_id
*/
public function process( $subscription_id, $order_id ) {
$data = $this->get_default_data();
$data['wc_subscription_id'] = $subscription_id;
$data['order_id'] = $order_id;
$subscription = wcs_get_subscription( $subscription_id );
$data['email'] = $subscription->get_billing_email();
$this->send_async_call( $data );
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->order, 'wc_order' );
BWFAN_Core()->rules->setRulesData( $this->subscription, 'wc_subscription' );
BWFAN_Core()->rules->setRulesData( $this->event_automation_id, 'automation_id' );
BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->order->get_billing_email(), $this->order->get_user_id() ), 'bwf_customer' );
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_subscription( $data );
}
/**
* 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() {
$subscription = wcs_get_subscription( $this->subscription_id );
$user_id = $this->get_user_id_event();
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['wc_subscription_id'] = $this->subscription_id;
$data_to_send['global']['wc_subscription'] = is_object( $subscription ) ? $subscription : '';
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['order_id'] = is_object( $this->order ) ? BWFAN_Woocommerce_Compatibility::get_order_id( $this->order ) : '';
$data_to_send['global']['wc_order'] = is_object( $this->order ) ? $this->order : '';
if ( intval( $user_id ) > 0 ) {
$data_to_send['global']['user_id'] = $user_id;
$email = BWFAN_PRO_Common::get_contact_email( $user_id );
if ( ! empty( $email ) ) {
$data_to_send['global']['email'] = $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();
?>
<li>
<strong><?php echo esc_html__( 'Subscription ID:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo get_edit_post_link( $global_data['wc_subscription_id'] ); //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( '#' . $global_data['wc_subscription_id'] ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Subscription Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['email'] ); ?>
</li>
<?php
return ob_get_clean();
}
/**
* This function decides if the task has to be executed or not.
* The event has validate checkbox in its meta fields.
*
* @param $task_details
*
* @return array|mixed
*/
public function validate_event( $task_details ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return array();
}
$result = [];
$task_id = $task_details['task_id'];
$task_global_data = BWFAN_Model_Taskmeta::get_meta( $task_id, 'integration_data' );
$subscription_id = $task_global_data['global']['wc_subscription_id'];
$subscription = wcs_get_subscription( $subscription_id );
$subscription_status = $subscription->get_status();
$task_event = $task_details['event_data']['event_slug'];
$automation_id = $task_details['processed_data']['automation_id'];
$automation_details = BWFAN_Model_Automations::get( $automation_id );
$current_automation_event = $automation_details['event'];
$current_automation_event_meta = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'event_meta' );
$current_automation_event_validation_status = ( isset( $current_automation_event_meta['validate_event'] ) ) ? $current_automation_event_meta['validate_event'] : 0;
// Current automation has no checking
if ( 0 === $current_automation_event_validation_status ) {
$result = $this->get_automation_event_validation();
return $result;
}
// Current automation event does not match with the event of task when the task was made
if ( $task_event !== $current_automation_event ) {
$result = $this->get_automation_event_status();
return $result;
}
if ( 'active' === $subscription_status ) {
$result = $this->get_automation_event_success();
return $result;
}
$result['status'] = 4;
$result['message'] = __( 'Subscription status has been changed', 'wp-marketing-automations-pro' );
return $result;
}
/**
* 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 ) {
$subscription_id = BWFAN_Merge_Tag_Loader::get_data( 'wc_subscription_id' );
$order_id = BWFAN_Merge_Tag_Loader::get_data( 'wc_order_id' );
if ( empty( $order_id ) || intval( $order_id ) !== intval( $task_meta['global']['order_id'] ) ) {
$set_data = array(
'wc_order_id' => intval( $task_meta['global']['order_id'] ),
'email' => $task_meta['global']['email'],
'wc_order' => $task_meta['global']['wc_order'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
if ( ( empty( $subscription_id ) || intval( $subscription_id ) !== intval( $task_meta['global']['wc_subscription_id'] ) ) && function_exists( 'wcs_get_subscription' ) ) {
$set_data = array(
'wc_subscription_id' => intval( $task_meta['global']['wc_subscription_id'] ),
'email' => $task_meta['global']['email'],
'wc_subscription' => $task_meta['global']['wc_subscription'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return false;
}
$subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$order_id = BWFAN_Common::$events_async_data['order_id'];
$subscription = wcs_get_subscription( $subscription_id );
$order = wc_get_order( $order_id );
$this->subscription = $subscription;
$this->order = $order;
$this->subscription_id = $subscription_id;
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
public function get_email_event() {
if ( $this->order instanceof WC_Order ) {
return $this->order->get_billing_email();
}
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_billing_email();
}
return false;
}
public function get_user_id_event() {
if ( $this->order instanceof WC_Order ) {
return $this->order->get_user_id();
}
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_user_id();
}
return 0;
}
/**
* v2 capture data.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return $automation_data;
}
$this->subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$this->subscription = wcs_get_subscription( $this->subscription_id );
$this->order_id = BWFAN_Common::$events_async_data['order_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
$this->order = wc_get_order( $this->order_id );
$automation_data['wc_subscription_id'] = $this->subscription_id;
$automation_data['order_id'] = $this->order_id;
$automation_data['email'] = $this->email;
return $automation_data;
}
/**
* v2 Method: Get fields schema
* @return array[][]
*/
public function get_fields_schema() {
return [
[
'id' => 'validate_event',
'label' => __( 'Validate Order status before executing task', 'wp-marketing-automations-pro' ),
'checkboxlabel' => __( 'Validate', 'wp-marketing-automations-pro' ),
'type' => 'checkbox',
'class' => '',
'required' => false,
'hint' => __( 'This setting is useful to verify time-delayed Actions. For instance, you can create a follow-up Action that runs after 30 days of placing an order. That Action won\'t trigger if the above selected Order Statuses are not matched to the order.', 'wp-marketing-automations-pro' ),
],
];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
return 'BWFAN_WCS_Renewal_Payment_Complete';
}

View File

@@ -0,0 +1,305 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WCS_Renewal_Payment_Failed extends BWFAN_Event {
private static $instance = null;
public $subscription = null;
public $order = null;
public $email = null;
public $subscription_id = null;
public $order_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact', 'wc_subscription' );
$this->event_name = esc_html__( 'Subscriptions Renewal Payment Failed', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after a subscription renewal payment is failed.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'wc_subscription',
'wc_customer',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' );
$this->support_lang = true;
$this->priority = 25.6;
$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( 'woocommerce_subscription_renewal_payment_failed', [ $this, 'subscription_renewal_payment_failed' ], 11, 2 );
add_action( 'woocommerce_order_status_failed', [ $this, 'early_subscription_renewal_payment_failed' ], 11, 1 );
}
public function subscription_renewal_payment_failed( $subscription, $order ) {
$subscription_id = $subscription->get_id();
$order_id = $order->get_id();
$this->email = $subscription->get_billing_email();
$this->process( $subscription_id, $order_id );
}
public function early_subscription_renewal_payment_failed( $order_id ) {
$order = wc_get_order( $order_id );
$subscription_id = $order->get_meta( '_subscription_renewal_early' );
if ( empty( $subscription_id ) || 'failed' !== $order->get_status() || ! empty( $order->get_meta( 'bwfan_renewal_payment_failed' ) ) ) {
return;
}
$this->email = $order->get_billing_email();
$this->process( $subscription_id, $order_id );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $subscription_id
* @param $order_id
*/
public function process( $subscription_id, $order_id ) {
$data = $this->get_default_data();
$data['wc_subscription_id'] = $subscription_id;
$data['order_id'] = $order_id;
$data['email'] = $this->email;
$this->send_async_call( $data );
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->order, 'wc_order' );
BWFAN_Core()->rules->setRulesData( $this->subscription, 'wc_subscription' );
BWFAN_Core()->rules->setRulesData( $this->event_automation_id, 'automation_id' );
BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->order->get_billing_email(), $this->order->get_user_id() ), 'bwf_customer' );
}
/**
* 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() {
$this->subscription = wcs_get_subscription( $this->subscription_id );
$user_id = $this->get_user_id_event();
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['wc_subscription_id'] = $this->subscription_id;
$data_to_send['global']['wc_subscription'] = is_object( $this->subscription ) ? $this->subscription : '';
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['order_id'] = is_object( $this->order ) ? BWFAN_Woocommerce_Compatibility::get_order_id( $this->order ) : '';
$data_to_send['global']['wc_order'] = is_object( $this->order ) ? $this->order : '';
if ( intval( $user_id ) > 0 ) {
$data_to_send['global']['user_id'] = $user_id;
$email = BWFAN_PRO_Common::get_contact_email( $user_id );
if ( ! empty( $email ) ) {
$data_to_send['global']['email'] = $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();
?>
<li>
<strong><?php echo esc_html__( 'Subscription ID:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo get_edit_post_link( $global_data['wc_subscription_id'] ); //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( '#' . $global_data['wc_subscription_id'] ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Subscription Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['email'] ); ?>
</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 ) {
$subscription_id = BWFAN_Merge_Tag_Loader::get_data( 'wc_subscription_id' );
$order_id = BWFAN_Merge_Tag_Loader::get_data( 'wc_order_id' );
if ( empty( $order_id ) || intval( $order_id ) !== intval( $task_meta['global']['order_id'] ) ) {
$set_data = array(
'wc_order_id' => intval( $task_meta['global']['order_id'] ),
'email' => $task_meta['global']['email'],
'wc_order' => $task_meta['global']['wc_order'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
if ( ( empty( $subscription_id ) || intval( $subscription_id ) !== intval( $task_meta['global']['wc_subscription_id'] ) ) && function_exists( 'wcs_get_subscription' ) ) {
$set_data = array(
'wc_subscription_id' => intval( $task_meta['global']['wc_subscription_id'] ),
'email' => $task_meta['global']['email'],
'wc_subscription' => $task_meta['global']['wc_subscription'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_subscription( $data );
}
/** validate subscription and also subscription status
*
* @param $data
*
* @return bool
*/
public function validate_subscription( $data ) {
if ( ! isset( $data['wc_subscription_id'] ) || ! function_exists( 'wcs_get_subscription' ) ) {
return false;
}
$subscription = wcs_get_subscription( $data['wc_subscription_id'] );
if ( ! $subscription instanceof WC_Subscription ) {
return false;
}
/** Check subscription early renewal */
$order = wc_get_order( $data['order_id'] );
$is_early_renewal = false;
if ( $order instanceof WC_Order ) {
$order->update_meta_data( 'bwfan_renewal_payment_failed', true );
$order->save_meta_data();
$is_early_renewal = $order->get_meta( '_subscription_renewal_early' );
}
return ( 'active' !== $subscription->get_status() || ! empty( $is_early_renewal ) );
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return false;
}
$this->subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$order_id = BWFAN_Common::$events_async_data['order_id'];
$subscription = wcs_get_subscription( $this->subscription_id );
$order = wc_get_order( $order_id );
$this->subscription = $subscription;
$this->order = $order;
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
public function get_email_event() {
if ( $this->order instanceof WC_Order ) {
return $this->order->get_billing_email();
}
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_billing_email();
}
return false;
}
public function get_user_id_event() {
if ( $this->order instanceof WC_Order ) {
return $this->order->get_user_id();
}
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_user_id();
}
return 0;
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
return $this->validate_subscription( $automation_data );
}
/**
* v2 capture data.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return $automation_data;
}
$this->subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$this->subscription = wcs_get_subscription( $this->subscription_id );
$this->order_id = BWFAN_Common::$events_async_data['order_id'];
$this->order = wc_get_order( $this->order_id );
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['wc_subscription_id'] = $this->subscription_id;
$automation_data['order_id'] = $this->order_id;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
return 'BWFAN_WCS_Renewal_Payment_Failed';
}

View File

@@ -0,0 +1,550 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WCS_Status_Changed extends BWFAN_Event {
private static $instance = null;
public $subscription = null;
public $from_status = null;
public $to_status = null;
public $email = null;
public $subscription_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact', 'wc_subscription' );
$this->event_name = esc_html__( 'Subscriptions Status Changed', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after a subscription status is changed.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'wc_subscription',
'wc_customer',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' );
$this->support_lang = true;
$this->priority = 25.1;
$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( 'woocommerce_subscription_status_updated', [ $this, 'subscription_status_changed' ], 11, 3 );
}
/**
* Localize data for html fields for the current event.
*/
public function admin_enqueue_assets() {
if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
$integration_data = $this->get_view_data();
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'from_options', $integration_data );
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'to_options', $integration_data );
}
}
public function get_view_data() {
if ( ! function_exists( 'wcs_get_subscription_statuses' ) ) {
return array();
}
$all_status = wcs_get_subscription_statuses();
$all_status['wc-on-hold'] = __( 'On Hold / Suspend', 'wp-marketing-automations-pro' );
return $all_status;
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/html" id="tmpl-event-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
is_validated = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'validate_event')) ? 'checked' : '';
selected_from_status = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'from')) ? data.eventSavedData.from : '';
selected_to_status = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'to')) ? data.eventSavedData.to : '';
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-6 bwfan-pl-0">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Subscription Status From', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="event_meta[from]">
<option value="wc-any"><?php echo esc_html__( 'Any', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.eventFieldsOptions, 'from_options') && _.isObject(data.eventFieldsOptions.from_options) ) {
_.each( data.eventFieldsOptions.from_options, function( value, key ){
selected = (key == selected_from_status) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
</div>
<div class="bwfan-col-sm-6 bwfan-pr-0">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Subscription Status To', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="event_meta[to]">
<option value="wc-any"><?php echo esc_html__( 'Any', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.eventFieldsOptions, 'to_options') && _.isObject(data.eventFieldsOptions.to_options) ) {
_.each( data.eventFieldsOptions.to_options, function( value, key ){
selected = (key == selected_to_status) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
</div>
<?php
$this->get_validation_html( $this->get_slug(), 'Validate order status before executing task', 'Validate' );
?>
</script>
<?php
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_subscription( $data );
}
/**
* 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'] ) || ! is_array( $automation_data['event_meta'] ) ) {
return false;
}
$current_automation_status_from = ( isset( $automation_data['event_meta']['from'] ) ) ? $automation_data['event_meta']['from'] : 'wc-any';
$current_automation_status_to = ( isset( $automation_data['event_meta']['to'] ) ) ? $automation_data['event_meta']['to'] : 'wc-any';
$subscription_contains = ( isset( $automation_data['event_meta']['subscription-contains'] ) ) ? $automation_data['event_meta']['subscription-contains'] : 'any';
if ( 'wc-any' === $current_automation_status_from && 'wc-any' === $current_automation_status_to && ( empty( $subscription_contains ) || 'any' === $subscription_contains ) ) {
return true;
}
/** Specific product case */
if ( 'selected_product' === $subscription_contains ) {
$subscription = wcs_get_subscription( absint( $automation_data['wc_subscription_id'] ) );
$ordered_products = array();
foreach ( $subscription->get_items() as $subscription_product ) {
$ordered_products[] = $subscription_product->get_product_id();
/** In case variation */
if ( $subscription_product->get_variation_id() ) {
$ordered_products[] = $subscription_product->get_variation_id();
}
}
$ordered_products = array_unique( $ordered_products );
$get_selected_product = $automation_data['event_meta']['products'];
$product_selected = array_column( $get_selected_product, 'id' );
$check_products = count( array_intersect( $product_selected, $ordered_products ) );
/** No selected products found */
if ( $check_products <= 0 ) {
return false;
}
}
$order_status_from = 'wc-' . $automation_data['from_status'];
$order_status_to = 'wc-' . $automation_data['to_status'];
/** checking from any to any status */
if ( 'wc-any' === $current_automation_status_from && 'wc-any' === $current_automation_status_to ) {
return true;
}
/** checking any status to selected status */
if ( 'wc-any' === $current_automation_status_from ) {
return ( $order_status_to === $current_automation_status_to );
}
/** checking selected status to any status */
if ( 'wc-any' === $current_automation_status_to ) {
return ( $order_status_from === $current_automation_status_from );
}
/** checking selected status to selected status */
return ( ( $order_status_from === $current_automation_status_from ) && ( $order_status_to === $current_automation_status_to ) );
}
public function handle_single_automation_run( $value1, $automation_id ) {
$is_register_task = false;
$to_status = $this->to_status;
$from_status = $this->from_status;
$event_meta = $value1['event_meta'];
$from = str_replace( 'wc-', '', $event_meta['from'] );
$to = str_replace( 'wc-', '', $event_meta['to'] );
if ( 'any' === $from && 'any' === $to ) {
$is_register_task = true;
} elseif ( 'any' === $from && $to_status === $to ) {
$is_register_task = true;
} elseif ( $from_status === $from && 'any' === $to ) {
$is_register_task = true;
} elseif ( $from_status === $from && $to_status === $to ) {
$is_register_task = true;
}
if ( $is_register_task && function_exists( 'wcs_get_subscription_statuses' ) ) {
$all_statuses = wcs_get_subscription_statuses();
$value1['from'] = $all_statuses[ 'wc-' . $from_status ];
$value1['to'] = $all_statuses[ 'wc-' . $to_status ];
return parent::handle_single_automation_run( $value1, $automation_id );
}
return '';
}
public function subscription_status_changed( $subscription, $to_status, $from_status ) {
$subscription_id = $subscription->get_id();
$this->process( $subscription_id, $from_status, $to_status );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $subscription_id
* @param $from_status
* @param $to_status
*/
public function process( $subscription_id, $from_status, $to_status ) {
$data = $this->get_default_data();
$data['wc_subscription_id'] = $subscription_id;
$data['from_status'] = $from_status;
$data['to_status'] = $to_status;
$subscription = wcs_get_subscription( $subscription_id );
$data['email'] = $subscription->get_billing_email();
$this->send_async_call( $data );
}
/**
* Returns the current event settings set in the automation at the time of task creation.
*
* @param $value
*
* @return array
*/
public function get_automation_event_data( $value ) {
$event_meta = $value['event_meta'];
$event_data = [
'event_source' => $value['source'],
'event_slug' => $value['event'],
'validate_event' => ( isset( $value['event_meta']['validate_event'] ) ) ? 1 : 0,
'from_status' => $event_meta['from'],
'to_status' => $event_meta['to'],
'from' => isset( $value['from'] ) ? $value['from'] : '',
'to' => isset( $value['to'] ) ? $value['to'] : '',
];
return $event_data;
}
/**
* 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() {
$this->subscription = wcs_get_subscription( $this->subscription_id );
$user_id = $this->get_user_id_event();
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['wc_subscription_id'] = $this->subscription_id;
$data_to_send['global']['wc_subscription'] = is_object( $this->subscription ) ? $this->subscription : '';
$data_to_send['global']['email'] = $this->email;
$data_to_send['from_status'] = $this->from_status;
$data_to_send['to_status'] = $this->to_status;
if ( intval( $user_id ) > 0 ) {
$data_to_send['global']['user_id'] = $user_id;
$email = BWFAN_PRO_Common::get_contact_email( $user_id );
if ( ! empty( $email ) ) {
$data_to_send['global']['email'] = $email;
}
}
return $data_to_send;
}
public function get_user_id_event() {
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_user_id();
}
return 0;
}
/**
* 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();
?>
<li>
<strong><?php echo esc_html__( 'Subscription ID:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo get_edit_post_link( $global_data['wc_subscription_id'] ); //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( '#' . $global_data['wc_subscription_id'] ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Subscription Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['email'] ); ?>
</li>
<?php
return ob_get_clean();
}
/**
* This function decides if the task has to be executed or not.
* The event has validate checkbox in its meta fields.
*
* @param $task_details
*
* @return array|mixed
*/
public function validate_event( $task_details ) {
$result = [];
$task_event = $task_details['event_data']['event_slug'];
$automation_id = $task_details['processed_data']['automation_id'];
$automation_details = BWFAN_Model_Automations::get( $automation_id );
$current_automation_event = $automation_details['event'];
$current_automation_event_meta = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'event_meta' );
$current_automation_event_validation_status = ( isset( $current_automation_event_meta['validate_event'] ) ) ? $current_automation_event_meta['validate_event'] : 0;
$current_automation_status_from = $current_automation_event_meta['from'];
$current_automation_status_to = $current_automation_event_meta['to'];
$task_status_from = $task_details['event_data']['from_status'];
$task_status_to = $task_details['event_data']['to_status'];
// Current automation has no checking
if ( 0 === $current_automation_event_validation_status ) {
$result = $this->get_automation_event_validation();
return $result;
}
// Current automation event does not match with the event of task when the task was made
if ( $task_event !== $current_automation_event ) {
$result = $this->get_automation_event_status();
return $result;
}
if ( ( $current_automation_status_from === $task_status_from ) && ( $current_automation_status_to === $task_status_to ) ) {
$result = $this->get_automation_event_success();
return $result;
}
$result['status'] = 4;
$result['message'] = __( 'Subscription Statuses in automation has been changed', 'wp-marketing-automations-pro' );
return $result;
}
/**
* 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 ) {
$subscription_id = BWFAN_Merge_Tag_Loader::get_data( 'wc_subscription_id' );
if ( ( empty( $subscription_id ) || intval( $subscription_id ) !== intval( $task_meta['global']['wc_subscription_id'] ) ) && function_exists( 'wcs_get_subscription' ) ) {
$set_data = array(
'wc_subscription_id' => intval( $task_meta['global']['wc_subscription_id'] ),
'email' => $task_meta['global']['email'],
'wc_order' => isset( $task_meta['global']['wc_order'] ) ? $task_meta['global']['wc_order'] : null,
'user_id' => $task_meta['global']['user_id'],
'wc_subscription' => $task_meta['global']['wc_subscription'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return false;
}
$subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$from_status = BWFAN_Common::$events_async_data['from_status'];
$to_status = BWFAN_Common::$events_async_data['to_status'];
$subscription = wcs_get_subscription( $subscription_id );
$this->subscription_id = $subscription_id;
$this->subscription = $subscription;
$this->from_status = $from_status;
$this->to_status = $to_status;
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->subscription, 'wc_subscription' );
BWFAN_Core()->rules->setRulesData( $this->from_status, 'wcs_old_status' );
BWFAN_Core()->rules->setRulesData( $this->event_automation_id, 'automation_id' );
BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->get_email_event(), $this->get_user_id_event() ), 'bwf_customer' );
}
public function get_email_event() {
return $this->subscription->get_billing_email();
}
/**
* v2 Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return $automation_data;
}
$this->subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$this->subscription = wcs_get_subscription( $this->subscription_id );
$this->from_status = BWFAN_Common::$events_async_data['from_status'];
$this->to_status = BWFAN_Common::$events_async_data['to_status'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['wc_subscription_id'] = $this->subscription_id;
$automation_data['from_status'] = $this->from_status;
$automation_data['to_status'] = $this->to_status;
$automation_data['email'] = $this->to_status;
return $automation_data;
}
public function get_fields_schema() {
$default = [
[
'label' => __( 'Any', 'wp-marketing-automations-pro' ),
'value' => 'wc-any',
]
];
$options = BWFAN_PRO_Common::prepared_field_options( $this->get_view_data() );
$options = array_merge( $default, $options );
return [
[
'id' => 'from',
'label' => __( "Subscription Status From", 'wp-marketing-automations-pro' ),
'type' => 'wp_select',
'options' => $options,
'placeholder' => __( 'Choose from Status', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper bwf-3-col-item',
'tip' => "",
"description" => "",
"required" => true,
"errorMsg" => __( "Status from is required.", 'wp-marketing-automations-pro' ),
],
[
'id' => 'to',
'label' => __( "Subscription Status To", 'wp-marketing-automations-pro' ),
'type' => 'wp_select',
'options' => $options,
'placeholder' => __( 'Choose to Status', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper bwf-3-col-item',
'tip' => "",
"description" => "",
"required" => true,
"errorMsg" => __( "Status to is required.", 'wp-marketing-automations-pro' ),
],
[
'id' => 'subscription-contains',
'label' => __( 'Subscription Contains', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any Product', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Products', 'wp-marketing-automations-pro' ),
'value' => 'selected_product'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => false,
"description" => ""
],
[
'id' => 'products',
'label' => __( 'Select Products', 'wp-marketing-automations-pro' ),
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'wcs_products',
'slug' => 'wcs_products',
'labelText' => 'WooCommerce Subscription products '
],
'class' => '',
'placeholder' => '',
'required' => true,
'toggler' => [
'fields' => [
[
'id' => 'subscription-contains',
'value' => 'selected_product',
]
]
],
],
];
}
public function get_default_values() {
return [
'from' => 'wc-any',
'to' => 'wc-any',
'subscription-contains' => 'any',
];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
return 'BWFAN_WCS_Status_Changed';
}

View File

@@ -0,0 +1,227 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_WCS_Trial_End extends BWFAN_Event {
private static $instance = null;
public $subscription_id = null;
public $subscription = null;
public $email = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact', 'wc_subscription' );
$this->event_name = esc_html__( 'Subscriptions Trial End', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after a subscription trial is ended.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'wc_subscription',
'wc_customer',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' );
$this->support_lang = true;
$this->priority = 25.2;
$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( 'woocommerce_scheduled_subscription_trial_end', [ $this, 'subscription_trial_end' ], 11, 1 );
}
public function subscription_trial_end( $subscription_id ) {
$this->process( $subscription_id );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $subscription_id
*/
public function process( $subscription_id ) {
$data = $this->get_default_data();
$data['wc_subscription_id'] = $subscription_id;
$subscription = wcs_get_subscription( $subscription_id );
$data['email'] = $subscription->get_billing_email();
$this->send_async_call( $data );
}
/**
* 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() {
$user_id = $this->get_user_id_event();
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['wc_subscription_id'] = is_object( $this->subscription ) ? $this->subscription->get_id() : '';
$data_to_send['global']['wc_subscription'] = is_object( $this->subscription ) ? $this->subscription : '';
$data_to_send['global']['email'] = $this->email;
if ( intval( $user_id ) > 0 ) {
$data_to_send['global']['user_id'] = $user_id;
$email = BWFAN_PRO_Common::get_contact_email( $user_id );
if ( ! empty( $email ) ) {
$data_to_send['global']['email'] = $email;
}
}
return $data_to_send;
}
public function get_user_id_event() {
if ( $this->subscription instanceof WC_Subscription ) {
return $this->subscription->get_user_id();
}
return 0;
}
/**
* 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();
?>
<li>
<strong><?php echo esc_html__( 'Subscription ID:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo get_edit_post_link( $global_data['wc_subscription_id'] ); //phpcs:ignore WordPress.Security.EscapeOutput ?>"><?php echo esc_html__( '#' . $global_data['wc_subscription_id'] ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Subscription Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['email'] ); ?>
</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 ) {
$subscription_id = BWFAN_Merge_Tag_Loader::get_data( 'wc_subscription_id' );
if ( ( empty( $subscription_id ) || intval( $subscription_id ) !== intval( $task_meta['global']['wc_subscription_id'] ) ) && function_exists( 'wcs_get_subscription' ) ) {
$set_data = array(
'wc_subscription_id' => intval( $task_meta['global']['wc_subscription_id'] ),
'email' => $task_meta['global']['email'],
'user_id' => $task_meta['global']['user_id'],
'wc_subscription' => $task_meta['global']['wc_subscription'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_subscription( $data );
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
return $this->validate_subscription( $automation_data );
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return false;
}
$subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$subscription = wcs_get_subscription( $subscription_id );
$this->subscription = $subscription;
$this->subscription_id = $subscription_id;
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->subscription, 'wc_subscription' );
BWFAN_Core()->rules->setRulesData( $this->event_automation_id, 'automation_id' );
BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->get_email_event(), $this->get_user_id_event() ), 'bwf_customer' );
}
public function get_email_event() {
return $this->subscription->get_billing_email();
}
/**
* v2 capture data.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return $automation_data;
}
$this->subscription_id = BWFAN_Common::$events_async_data['wc_subscription_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
$this->subscription = wcs_get_subscription( $this->subscription_id );
$automation_data['subscription_id'] = $this->subscription_id;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() && bwfan_is_woocommerce_subscriptions_active() ) {
return 'BWFAN_WCS_Trial_End';
}

View File

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