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,55 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class BWFAN_AB_Load_Events {
private static $instance = null;
public function __construct() {
add_action( 'bwfan_wc_source_loaded', [ $this, 'load_events' ] );
}
/**
* Ensures only one instance of the class is loaded or can be loaded.
*
* @return BWFAN_AB_Load_Events|null
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Loads all events of current trigger
*/
public function load_events( $source ) {
if ( false === BWFAN_Common::is_cart_abandonment_active() ) {
return;
}
$resource_dir = __DIR__ . '/events';
if ( file_exists( $resource_dir ) ) {
foreach ( glob( $resource_dir . '/class-*.php' ) as $_field_filename ) {
$event_class = require_once( $_field_filename );
if ( ! is_null( $event_class ) && method_exists( $event_class, 'get_instance' ) ) {
$event_obj = $event_class::get_instance( $source->get_slug() );
$event_obj->load_hooks();
BWFAN_Load_Sources::register_events( $event_obj );
}
}
do_action( 'bwfanac_events_loaded' );
}
}
}
if ( bwfan_is_woocommerce_active() ) {
new BWFAN_AB_Load_Events();
}

View File

@@ -0,0 +1,647 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_AB_Cart_Abandoned extends BWFAN_Event {
private static $instance = null;
public $emails = [];
public $tokens = [];
public $cart_items = [];
public $abandoned_email = false;
public $abandoned_id = null;
public $token = null;
public $cart_item = null;
public $user_id = false;
public $abandoned_data = array();
public $abandoned_phone = null;
/** v2 */
public $contact_data_v2 = array();
public function __construct( $source_slug ) {
$this->source_type = $source_slug;
$this->optgroup_label = __( 'Cart', 'wp-marketing-automations' );
$this->event_name = __( 'Cart Abandoned', 'wp-marketing-automations' );
$this->event_desc = __( 'This automation would trigger when a user abandoned the cart.', 'wp-marketing-automations' );
$this->event_merge_tag_groups = array( 'wc_ab_cart', 'bwf_contact' );
$this->event_rule_groups = array(
'ab_cart',
'aerocheckout',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->support_lang = true;
$this->priority = 5;
$this->customer_email_tag = '{{cart_billing_email}}';
$this->v2 = true;
$this->optgroup_priority = 5;
$this->supported_blocks = [ 'cart' ];
$this->automation_add = true;
}
public function load_hooks() {
}
public static function get_instance( $source_slug ) {
if ( null === self::$instance ) {
self::$instance = new self( $source_slug );
}
return self::$instance;
}
/**
* Get all the abandoned rows from db table. It runs at every 2 minutes.
*/
public function get_eligible_abandoned_rows() {
global $wpdb;
$global_settings = BWFAN_Common::get_global_settings();
$abandoned_time_in_minutes = intval( $global_settings['bwfan_ab_init_wait_time'] );
/** Status 0: Pending, 4: Re-Scheduled */
$query = $wpdb->prepare( 'SELECT * FROM {table_name} WHERE TIMESTAMPDIFF(MINUTE,last_modified,UTC_TIMESTAMP) >= %d AND status IN (0,4)', $abandoned_time_in_minutes );
$active_abandoned_carts = BWFAN_Model_Abandonedcarts::get_results( $query );
if ( ! is_array( $active_abandoned_carts ) || count( $active_abandoned_carts ) === 0 ) {
return;
}
$active_abandoned_carts = BWFAN_Abandoned_Cart::remove_duplicate_cart( $active_abandoned_carts );
$ids = array_column( $active_abandoned_carts, 'ID', 'ID' );
/** Status 1: In-Progress (Automations Found), 3: Pending (No Tasks Found) */
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 ) ) {
/** Status 3 - No automation found */
BWFAN_Common::update_abandoned_rows( $ids, 3 );
foreach ( $active_abandoned_carts as $active_abandoned_cart ) {
BWFAN_Common::maybe_create_abandoned_contact( $active_abandoned_cart ); // create contact at the time of abandonment
}
return;
}
$days_to_check = isset( $global_settings['bwfan_disable_abandonment_days'] ) && intval( $global_settings['bwfan_disable_abandonment_days'] ) > 0 ? intval( $global_settings['bwfan_disable_abandonment_days'] ) : 0;
if ( ! empty( $days_to_check ) ) {
$after_date = date( 'Y-m-d H:i:s', strtotime( " -$days_to_check day" ) );
}
foreach ( $active_abandoned_carts as $active_abandoned_cart ) {
BWFAN_Common::maybe_create_abandoned_contact( $active_abandoned_cart );// create contact at the time of abandonment
if ( empty( $days_to_check ) ) {
$this->process( $active_abandoned_cart );
continue;
}
/** Cool Off period checking */
$query = "SELECT customers.l_order_date FROM {$wpdb->prefix}bwf_wc_customers AS customers
WHERE EXISTS (
SELECT 1
FROM {$wpdb->prefix}bwf_contact AS contact
WHERE contact.email = %s
AND contact.id = customers.cid
)
AND customers.l_order_date >= %s
LIMIT 1";
$last_order = $wpdb->get_var( $wpdb->prepare( $query, $active_abandoned_cart['email'], $after_date ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( ! empty( $last_order ) ) {
/** Order found. No need to process the cart */
/** Status 5 - Under Cool off period */
BWFAN_Common::update_abandoned_rows( [ $active_abandoned_cart['ID'] ], 5 );
continue;
}
$this->process( $active_abandoned_cart );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->abandoned_data, 'abandoned_data' );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $abandoned_cart
*
* @return array|bool|void
*/
public function process( $abandoned_cart ) {
$this->abandoned_id = $abandoned_cart['ID'];
$this->abandoned_data = BWFAN_Model_Abandonedcarts::get( $this->abandoned_id );
if ( ! is_array( $this->abandoned_data ) ) {
BWFAN_Common::update_abandoned_rows( [ $abandoned_cart['ID'] ], 3 );
return;
}
$this->abandoned_email = $this->abandoned_data['email'];
$this->token = $abandoned_cart['token'];
$this->cart_item = $this->abandoned_data['items'];
$this->user_id = $abandoned_cart['user_id'];
$this->abandoned_phone = $this->get_abandoned_phone( $this->abandoned_data );
$this->contact_data_v2 = array(
'abandoned_id' => absint( $this->abandoned_id ),
'email' => $this->abandoned_email,
'user_id' => $this->user_id,
'cart_item' => $this->abandoned_data['items'],
'phone' => $this->abandoned_phone,
'event' => $this->get_slug(),
'version' => 2
);
return $this->run_automations();
}
/** get abandoned phone number
*
* @param $abandoned_data
*
* @return mixed|string|void
*/
public function get_abandoned_phone( $abandoned_data ) {
if ( empty( $abandoned_data ) ) {
return '';
}
$phone = '';
$checkout_data = json_decode( $abandoned_data['checkout_data'], true );
if ( ! isset( $checkout_data['fields'] ) || empty( $checkout_data['fields'] ) ) {
return $phone;
}
$checkout_fields = $checkout_data['fields'];
/** check if the billing phone available */
if ( isset( $checkout_fields['billing_phone'] ) && ! empty( $checkout_fields['billing_phone'] ) ) {
$phone = $checkout_fields['billing_phone'];
}
/** check if the billing phone available */
if ( empty( $phone ) && isset( $checkout_fields['shipping_phone'] ) && ! empty( $checkout_fields['shipping_phone'] ) ) {
$phone = $checkout_fields['shipping_phone'];
}
/** still empty then return */
if ( empty( $phone ) ) {
return $phone;
}
$cart_phone = $phone;
$cart_country = '';
/** check for billing country */
if ( isset( $checkout_fields['billing_country'] ) && ! empty( $checkout_fields['billing_country'] ) ) {
$cart_country = $checkout_fields['billing_country'];
}
/** check for shipping country */
if ( empty( $cart_country ) && isset( $checkout_fields['shipping_country'] ) && ! empty( $checkout_fields['shipping_country'] ) ) {
$cart_country = $checkout_fields['shipping_country'];
}
/** cart country not exists than return cart phone without country */
if ( empty( $cart_country ) ) {
return $cart_phone;
}
$phone = BWFAN_Phone_Numbers::add_country_code( $cart_phone, $cart_country );
return $phone;
}
/**
* Override method to change the state of Cart based on Automations found
*
* @return array|bool
*/
public function run_automations() {
$any_automation_ran = BWFAN_Common::maybe_run_v2_automations( $this->get_slug(), $this->contact_data_v2 );
/** Run v1 automations */
$automation_actions = array();
foreach ( $this->automations_arr as $automation_id => $automation_data ) {
if ( $this->get_slug() !== $automation_data['event'] || 0 !== intval( $automation_data['requires_update'] ) ) {
continue;
}
$ran_actions = $this->handle_single_automation_run( $automation_data, $automation_id );
$automation_actions[ $automation_id ] = $ran_actions;
}
/**
* We found no tasks to create. And no v2 automation contact row created
* So, setting status 3 i.e. Pending (No Tasks Found)
*/
if ( 0 === array_sum( $automation_actions ) && ! $any_automation_ran ) {
BWFAN_Common::update_abandoned_rows( array( $this->abandoned_id ), 3 );
return $automation_actions;
}
/** Updating carts to in-progress i.e. 1 state */
BWFAN_Common::update_abandoned_rows( array( $this->abandoned_id ), 1 );
return $automation_actions;
}
/**
* Override method to change the state of Cart based on Tasks to be created
*
* @param $automation_data
* @param $automation_id
*
* @return bool|int
*/
public function handle_single_automation_run( $automation_data, $automation_id ) {
$this->event_automation_id = $automation_id;
/** Setup the rules data */
$this->pre_executable_actions( $automation_data );
/** get all the actions which have passed the rules */
$actions = $this->get_executable_actions( $automation_data );
if ( ! isset( $actions['actions'] ) || ! is_array( $actions['actions'] ) || count( $actions['actions'] ) === 0 ) {
return 0;
}
$event_data = $this->get_automation_event_data( $automation_data );
try {
/** Register all those tasks which passed through rules or which are direct actions. The following function is present in every event class. */
$this->register_tasks( $automation_id, $actions['actions'], $event_data );
} catch ( Exception $exception ) {
BWFAN_Core()->logger->log( 'Register task function not overrided by child class' . get_class( $this ), $this->log_type );
}
return count( $actions['actions'] );
}
/**
* 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 ) {
$data_to_send = $this->get_event_data();
add_action( 'bwfan_task_created_ab_cart_abandoned', [ $this, 'update_task_meta' ], 10, 2 );
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [];
$data_to_send['global']['email'] = $this->abandoned_email;
$data_to_send['global']['cart_abandoned_id'] = $this->abandoned_id;
$data_to_send['global']['cart_details'] = $this->abandoned_data;
$data_to_send['global']['phone'] = $this->abandoned_phone;
$data_to_send['global']['user_id'] = $this->user_id;
$checkout_data = isset( $this->abandoned_data['checkout_data'] ) ? json_decode( $this->abandoned_data['checkout_data'], true ) : [];
$data_to_send['global']['language'] = isset( $checkout_data['lang'] ) ? $checkout_data['lang'] : '';
return $data_to_send;
}
/**
* If any event has email and it does not contain order object, then following method must be overridden by child event class.
* Return email
* @return bool
*/
public function get_email_event() {
return $this->abandoned_email;
}
/**
* If any event has user id and it does not contain order object, then following method must be overridden by child event class.
* Return user id
* @return bool
*/
public function get_user_id_event() {
return $this->user_id;
}
public function update_task_meta( $index, $task_id ) {
BWFAN_Core()->tasks->insert_taskmeta( $task_id, 'c_a_id', $this->abandoned_id );
}
/**
* 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 esc_html_e( 'Abandoned Email:', 'wp-marketing-automations' ); ?> </strong>
<?php echo "<a href='" . site_url( 'wp-admin/admin.php?page=autonami&path=/carts/recoverable/' . $global_data['cart_abandoned_id'] . '/tasks' ) . "'>" . $global_data['email'] . '</a>'; //phpcs:ignore WordPress.Security.EscapeOutput ?>
</li>
<?php
if ( isset( $global_data['phone'] ) && ! empty( $global_data['phone'] ) ) { ?>
<li>
<strong><?php esc_html_e( 'Abandoned Phone:', 'wp-marketing-automations' ); ?> </strong>
<?php echo $global_data['phone']; //phpcs:ignore WordPress.Security.EscapeOutput ?>
</li>
<?php
}
return ob_get_clean();
}
public function validate_event( $task_details ) {
$cart_id = $task_details['processed_data']['cart_abandoned_id'];
$cart_data = BWFAN_Model_Abandonedcarts::get( $cart_id );
$email = is_email( $cart_data['email'] ) ? $cart_data['email'] : $task_details['processed_data']['email'];
if ( ! is_email( $email ) ) {
return $this->get_automation_event_success();
}
/** If order is pending or failed then cart is valid so continue */
$orders = wc_get_orders( array(
'billing_email' => $email,
'date_after' => $cart_data['created_time'],
) );
/** empty orders than return **/
if ( empty( $orders ) ) {
return $this->get_automation_event_success();
}
$orders = array_filter( $orders, function ( $order ) {
$failed_statuses = [ 'pending', 'failed', 'cancelled', 'trash' ];
if ( ! in_array( $order->get_status(), $failed_statuses, true ) ) {
return true;
}
return false;
} );
if ( empty( $orders ) ) {
return $this->get_automation_event_success();
}
/** in case order is not an instance than return success **/
if ( ! $orders[0] instanceof WC_Order ) {
return $this->get_automation_event_success();
}
/** Order is placed, discard the task execution */
$automation_id = $task_details['processed_data']['automation_id'];
/** Attributing the sale */
$orders[0]->update_meta_data( '_bwfan_ab_cart_recovered_a_id', $automation_id );
$task_data_meta = BWFAN_Model_Tasks::get_task_with_data( $task_details['task_id'] );
$track_id = $task_data_meta['meta']['t_track_id'];
if ( ! empty( $track_id ) ) {
$orders[0]->update_meta_data( '_bwfan_ab_cart_recovered_t_id', $track_id );
}
$orders[0]->update_meta_data( '_bwfan_recovered_ab_id', $cart_id );
$orders[0]->save_meta_data();
$cart_tasks = BWFAN_Common::get_schedule_task_by_email( [ $automation_id ], $cart_data['email'] );
$cart_tasks = $cart_tasks[ $automation_id ];
$cart_tasks = array_map( function ( $v ) {
return $v['ID'];
}, $cart_tasks );
$fail_resp = array(
'status' => 4,
'message' => 'Cart is recovered already',
);
if ( empty( $cart_tasks ) ) {
BWFAN_Model_Abandonedcarts::delete( $cart_id );
return $fail_resp;
}
/** Delete the tasks */
global $wpdb;
$tasks_count = count( $cart_tasks );
if ( in_array( $task_details['task_id'], $cart_tasks ) ) {
$cart_tasks = array_diff( $cart_tasks, [ $task_details['task_id'] ] );
sort( $cart_tasks );
$tasks_count = count( $cart_tasks );
}
$prepare_placeholders = array_fill( 0, $tasks_count, '%d' );
$prepare_placeholders = implode( ', ', $prepare_placeholders );
/** Delete Tasks */
$sql_query = "DELETE FROM {table_name} WHERE `ID` IN ($prepare_placeholders)";
$sql_query = $wpdb->prepare( $sql_query, $cart_tasks ); // WPCS: unprepared SQL OK
BWFAN_Model_Tasks::query( $sql_query );
/** Delete Tasks Meta */
$sql_query = "Delete FROM {table_name} WHERE `bwfan_task_id` IN ($prepare_placeholders)";
$sql_query = $wpdb->prepare( $sql_query, $cart_tasks ); // WPCS: unprepared SQL OK
BWFAN_Model_Taskmeta::query( $sql_query );
/** Delete the cart */
BWFAN_Model_Abandonedcarts::delete( $cart_id );
return $fail_resp;
}
/**
* 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 ) {
$cart_abandoned_id = BWFAN_Merge_Tag_Loader::get_data( 'cart_abandoned_id' );
if ( empty( $cart_abandoned_id ) || $cart_abandoned_id !== $task_meta['global']['cart_abandoned_id'] ) {
$set_data = array(
'cart_abandoned_id' => $task_meta['global']['cart_abandoned_id'],
'email' => $task_meta['global']['email'],
'cart_details' => BWFAN_Model_Abandonedcarts::get( $task_meta['global']['cart_abandoned_id'] ),
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* checking if the abandoned cart contain empty cart
*/
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_cart_details( $data );
}
/** validating abandoned cart contain item or not
*
* @param $data
*
* @return bool
*/
public function validate_cart_details( $data ) {
if ( ! isset( $data['cart_abandoned_id'] ) ) {
return false;
}
$cart_data = BWFAN_Model_Abandonedcarts::get( $data['cart_abandoned_id'] );
if ( empty( $cart_data ) ) {
return false;
}
$cart_items = maybe_unserialize( $cart_data['items'] );
if ( empty( $cart_items ) ) {
$this->message_validate_event = __( 'Cart does not contain any item.', 'wp-marketing-automations' );
return false;
}
return true;
}
public function validate_v2_event_settings( $automation_data ) {
if ( ! isset( $automation_data['abandoned_id'] ) ) {
return false;
}
$cart_data = BWFAN_Model_Abandonedcarts::get( $automation_data['abandoned_id'] );
if ( empty( $cart_data ) ) {
return false;
}
$cart_items = maybe_unserialize( $cart_data['items'] );
if ( empty( $cart_items ) ) {
return false;
}
return true;
}
/**
* Before starting automation on a contact, validating if cart row exists
*
* @param $row
*
* @return bool
*/
public function validate_v2_before_start( $row ) {
if ( empty( $row['data'] ) ) {
return false;
}
$data = json_decode( $row['data'], true );
if ( ! isset( $data['global'] ) || ! isset( $data['global']['cart_abandoned_id'] ) ) {
return false;
}
$goal_checking = BWFAN_Common::is_wc_order_goal( $data['global']['automation_id'] );
if ( $goal_checking ) {
return true;
}
$cart_id = $data['global']['cart_abandoned_id'];
$cart_data = BWFAN_Model_Abandonedcarts::get( $cart_id );
if ( empty( $cart_data ) ) {
return false;
}
$orders = wc_get_orders( array(
'billing_email' => $cart_data['email'],
'date_after' => $cart_data['created_time'],
) );
if ( empty( $orders ) ) {
return true;
}
$orders = array_filter( $orders, function ( $order ) {
return ! ( in_array( $order->get_status(), [ 'pending', 'failed', 'cancelled', 'trash' ], true ) );
} );
if ( empty( $orders ) ) {
return true;
}
/** Delete abandoned cart */
BWFAN_Model_Abandonedcarts::delete( $cart_id );
return false;
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new BWFCRM_Contact( $cid );
if ( ! $contact->is_contact_exists() ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
$email = $contact->contact->get_email();
$cart = BWFAN_Model_Abandonedcarts::get_abandoned_data( " WHERE `email`='$email' ", '', '', 'ID', ARRAY_A );
if ( empty( $cart ) ) {
return [ 'status' => 0, 'type' => '', 'message' => "Contact doesn't have any tracked cart." ];
}
$this->abandoned_id = $cart[0]['ID'];
$this->abandoned_email = $email;
$this->abandoned_data = $cart[0];
$data = array(
'abandoned_id' => absint( $this->abandoned_id ),
'email' => $this->abandoned_email,
'user_id' => $this->user_id,
'cart_item' => $this->abandoned_data['items'],
);
return array_merge( $automation_data, $data );
}
/**
* @return bool|void
*/
public static function maybe_clean() {
$duplicate_entries = BWFAN_Model_Abandonedcarts::get_duplicate_entry();
if ( empty( $duplicate_entries ) ) {
return;
}
global $wpdb;
$string_placeholder = array_fill( 0, count( $duplicate_entries ), '%d' );
$placeholder = implode( ', ', $string_placeholder );
$query = $wpdb->prepare( "DELETE FROM `{$wpdb->prefix}bwfan_abandonedcarts` WHERE `ID` IN ({$placeholder})", $duplicate_entries );
return ( $wpdb->query( $query ) > 0 ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() ) {
return 'BWFAN_AB_Cart_Abandoned';
}

View File

@@ -0,0 +1,251 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_AB_Cart_Recovered extends BWFAN_Event {
private static $instance = null;
public $order_id = null;
/** @var WC_Order $order */
public $order = null;
public $cart_details = array();
public function __construct( $source_slug ) {
$this->source_type = $source_slug;
$this->optgroup_label = __( 'Cart', 'wp-marketing-automations' );
$this->event_name = __( 'Cart Recovered', 'wp-marketing-automations' );
$this->event_desc = __( 'This automation would trigger when the user abandoned cart is recovered.', 'wp-marketing-automations' );
$this->event_merge_tag_groups = array( 'bwf_contact', 'wc_order' );
$this->event_rule_groups = array(
'wc_order',
'wc_customer',
'aerocheckout',
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->support_lang = true;
$this->priority = 5.1;
$this->customer_email_tag = '{{admin_email}}';
$this->v2 = true;
$this->optgroup_priority = 5;
$this->supported_blocks = [ 'cart' ];
$this->automation_add = true;
}
public function load_hooks() {
add_action( 'abandoned_cart_recovered', array( $this, 'process' ), 10, 2 );
}
public static function get_instance( $source_slug ) {
if ( null === self::$instance ) {
self::$instance = new self( $source_slug );
}
return self::$instance;
}
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->order, 'wc_order' );
BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->order->get_billing_email(), $this->order->get_user_id() ), 'bwf_customer' );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $order_id
*/
public function process( $cart_details, $order_id ) {
$data = $this->get_default_data();
$data['order_id'] = $order_id;
$data['cart_details'] = $cart_details;
$this->send_async_call( $data );
}
/**
* 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 ) {
$order_id = BWFAN_Merge_Tag_Loader::get_data( 'order_id' );
if ( empty( $order_id ) || $order_id !== $task_meta['global']['order_id'] ) {
$set_data = array(
'order_id' => $task_meta['global']['order_id'],
'email' => $task_meta['global']['email'],
'cart_details' => $task_meta['global']['cart_details']
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$order_id = BWFAN_Common::$events_async_data['order_id'];
$this->order_id = $order_id;
$order = wc_get_order( $order_id );
$this->order = $order;
$this->cart_details = BWFAN_Common::$events_async_data['cart_details'];
return $this->run_automations();
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->order_id = BWFAN_Common::$events_async_data['order_id'];
$order = wc_get_order( $this->order_id );
$this->order = $order;
$this->cart_details = [];
$automation_data['email'] = is_object( $order ) ? BWFAN_Woocommerce_Compatibility::get_billing_email( $order ) : '';
$automation_data['cart_details'] = [];
$automation_data['order_id'] = $this->order_id;
return $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 ) {
$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 = [];
$data_to_send['global']['order_id'] = $this->order_id;
$order = $this->order;
$data_to_send['global']['email'] = is_object( $order ) ? BWFAN_Woocommerce_Compatibility::get_billing_email( $order ) : '';
$data_to_send['global']['cart_details'] = $this->cart_details;
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();
$order = wc_get_order( $global_data['order_id'] );
if ( $order instanceof WC_Order ) {
?>
<li>
<strong><?php echo esc_html__( 'Recovered Order:', 'wp-marketing-automations' ); ?> </strong>
<a target="_blank" href="<?php echo get_edit_post_link( $global_data['order_id'] ); //phpcs:ignore WordPress.Security.EscapeOutput
?>"><?php echo '#' . esc_attr( $global_data['order_id'] . ' ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ); ?></a>
</li>
<?php } ?>
<li>
<strong><?php echo esc_html__( 'Email:', 'wp-marketing-automations' ); ?> </strong>
<span><?php echo $global_data['email']; //phpcs:ignore WordPress.Security.EscapeOutput ?></span>
</li>
<?php
return ob_get_clean();
}
public function get_email_event() {
if ( $this->order instanceof WC_Order ) {
return $this->order->get_billing_email();
}
if ( ! empty( absint( $this->order_id ) ) ) {
$order = wc_get_order( absint( $this->order_id ) );
return $order instanceof WC_Order ? $order->get_billing_email() : false;
}
return false;
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new BWFCRM_Contact( $cid );
if ( ! $contact->is_contact_exists() ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
$last_recovered_cart = $this->fetch_last_recovered_cart( $contact->contact->get_email() );
if ( empty( $last_recovered_cart ) ) {
return [ 'status' => 0, 'type' => '', 'message' => "Contact doesn't have any recovered order." ];
}
$this->order_id = $last_recovered_cart;
$this->order = wc_get_order( $this->order_id );
$data = array(
'contact_id' => $cid,
'order_id' => $this->order_id,
'email' => $contact->contact->get_email(),
);
return array_merge( $automation_data, $data );
}
/**
* Fetch last recovered cart's order id
*
* @param $email
*
* @return int
*/
public function fetch_last_recovered_cart( $email ) {
global $wpdb;
$post_statuses = apply_filters( 'bwfan_recovered_cart_excluded_statuses', array(
'wc-pending',
'wc-failed',
'wc-cancelled',
'wc-refunded',
'trash',
'draft'
) );
$post_statuses = implode( "','", $post_statuses );
if ( BWF_WC_Compatibility::is_hpos_enabled() ) {
$hpos_left_join = " LEFT JOIN {$wpdb->prefix}wc_orders_meta as m1 ON p.id = m1.order_id ";
$hpos_where = " AND p.billing_email LIKE '$email' "; //phpcs:ignore WordPress.Security.NonceVerification;
$query = $wpdb->prepare( "SELECT p.id as id FROM {$wpdb->prefix}wc_orders as p LEFT JOIN {$wpdb->prefix}wc_orders_meta as m ON p.id = m.order_id $hpos_left_join WHERE p.type = %s AND p.status NOT IN ('$post_statuses') AND m.meta_key = %s $hpos_where ORDER BY p.id DESC LIMIT 1", 'shop_order', '_bwfan_ab_cart_recovered_a_id' );
} else {
$left_join = " LEFT JOIN {$wpdb->prefix}postmeta as m1 ON p.ID = m1.post_id ";
$where = ' AND m1.meta_key = "_billing_email" ';
$where .= " AND m1.meta_value LIKE '$email' "; //phpcs:ignore WordPress.Security.NonceVerification
$query = $wpdb->prepare( "SELECT p.ID as id FROM {$wpdb->prefix}posts as p LEFT JOIN {$wpdb->prefix}postmeta as m ON p.ID = m.post_id $left_join WHERE p.post_type = %s AND p.post_status NOT IN ('$post_statuses') AND m.meta_key = %s $where ORDER BY p.ID DESC LIMIT 1", 'shop_order', '_bwfan_ab_cart_recovered_a_id' );
}
return intval( $wpdb->get_var( $query ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( bwfan_is_woocommerce_active() ) {
return 'BWFAN_AB_Cart_Recovered';
}

View File

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

View File

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

View File

@@ -0,0 +1,412 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
class BWFAN_Notification_Email_Controller {
private $frequency = '';
private $id = '';
private $data = array();
private $dates = array();
/**
* Constructor.
*
* @param array $data
*/
public function __construct( $frequency, $data = array(), $dates = array() ) {
$this->frequency = $frequency;
$this->id = $frequency . '_' . 'report';
$this->data = $data;
$this->dates = $dates;
}
/**
* Retrieves the email sections for the notification email.
*
* This function constructs an array of email sections that will be used to build the notification email.
* The email sections include headers, highlights, performance metrics, automation statuses, dynamic content, and footer.
*
* @return array The array of email sections.
*/
public function get_email_sections() {
$global_settings = BWFAN_Common::get_global_settings();
/* translators: 1: Dynamic From Date, 2: Dynamic to date */
$date_range = sprintf( __( '%1$s - %2$s', 'wp-marketing-automations' ), BWFAN_Notification_Email::format_date( $this->dates['from_date'] ), BWFAN_Notification_Email::format_date( $this->dates['to_date'] ) );
if ( 'daily' === $this->frequency ) {
$date_range = BWFAN_Notification_Email::format_date( $this->dates['from_date'] );
}
$upgrade_link = BWFAN_Common::get_fk_site_links();
$upgrade_link = isset( $upgrade_link['upgrade'] ) ? $upgrade_link['upgrade'] : '';
$highlight_subtitle = __( 'Analyse key automation metrics and see how well your store performed.', 'wp-marketing-automations' );
$highlight_button_text = __( 'View Detail Report', 'wp-marketing-automations' );
$highlight_button_url = admin_url( 'admin.php?page=autonami' );
if ( false === bwfan_is_autonami_pro_active() ) {
$highlight_subtitle = __( 'Analyse key automation metrics and see how well your store performed. Unlock more insights.', 'wp-marketing-automations' );
$highlight_button_text = __( 'Upgrade To PRO', 'wp-marketing-automations' );
$highlight_button_url = add_query_arg( [
'utm_campaign' => 'FKA+Lite+Notification',
'utm_medium' => 'Email+Highlight'
], $upgrade_link );
}
$get_total_orders = bwfan_is_woocommerce_active() ? BWFAN_Dashboards::get_total_orders( '', '', '', '' ) : [];
$total_revenue = ! isset( $get_total_orders[0]['total_revenue'] ) ? 0 : $get_total_orders[0]['total_revenue'];
$total_revenue = floatval( $total_revenue );
$theme = array(
'date' => $date_range,
'title' => __( 'Performance Report', 'wp-marketing-automations' ),
'subtitle' => $highlight_subtitle,
'button_text' => $highlight_button_text,
'button_url' => $highlight_button_url,
'theme' => 'light',
);
$time = strtotime( gmdate( 'c' ) );
if ( false === bwfan_is_autonami_pro_active() && $time >= 1732510800 && $time < 1733547600 ) {
$theme['theme'] = 'dark';
$theme['subtitle'] = __( '💰 Black Friday is HERE - Subscribe Now for Upto 55% Off 💰', 'wp-marketing-automations' );
}
$email_sections = array(
array(
'type' => 'email_header',
),
array(
'type' => 'highlight',
'data' => $theme,
),
array(
'type' => 'dynamic',
'callback' => array( $this, 'get_dynamic_content_1' ),
),
array(
'type' => 'bwfan_status_section',
'data' => apply_filters( 'bwfan_weekly_mail_status_section', [] ),
),
array(
'type' => 'section_header',
'data' => array(
'title' => __( 'Key Performance Metrics', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Data */
'subtitle' => sprintf( __( 'Change compared to previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_string( $this->frequency ) ),
),
),
);
// Chunk the original array into groups of 2
$chunks = array_chunk( $this->data['metrics'], 2, true );
$tile_data = [];
foreach ( $chunks as $chunk ) {
// If the chunk has less than 2 metrics, ignore it
if ( count( $chunk ) < 2 ) {
continue;
}
$tile_data[] = array(
reset( $chunk ), // First metric in the chunk
end( $chunk ), // Second metric in the chunk
);
}
if ( ! empty( $tile_data ) ) {
$email_sections[] = array(
'type' => 'metrics',
'data' => array(
'tile_data' => $tile_data,
),
);
}
if ( $total_revenue > 10 ) {
/* translators: 1: Dynamic Data, 2: Dynamic Revenue */
$cta_content = sprintf( __( 'Since installing %1$s you have captured additional revenue of %2$s.', 'wp-marketing-automations' ), '<strong>' . __( 'FunnelKit Automation', 'wp-marketing-automations' ) . '</strong>', '<strong>' . wc_price( $total_revenue ) . '</strong>' );
if ( false === bwfan_is_autonami_pro_active() ) {
/* translators: 1: Dynamic Data, 2: Dynamic Revenue */
$cta_content = sprintf( __( 'Since installing %1$s you have captured additional revenue of %2$s. Upgrade to Pro for even more revenue.', 'wp-marketing-automations' ), '<strong>' . __( 'FunnelKit Automation', 'wp-marketing-automations' ) . '</strong>', '<strong>' . wc_price( $total_revenue ) . '</strong>' );
$cta_link = add_query_arg( [
'utm_campaign' => 'FKA+Lite+Notification',
'utm_medium' => 'Total+Revenue'
], $upgrade_link );
$email_sections[] = array(
'type' => 'bwfan_status_section',
'data' => [
'content' => $cta_content,
'link' => $cta_link,
'link_text' => __( 'Upgrade To PRO', 'wp-marketing-automations' ),
'background_color' => '#FEF7E8',
'button_color' => '#FFC65C',
'button_text_color' => '#000000',
]
);
} else {
$email_sections[] = array(
'type' => 'bwfan_status_w_cta_section',
'data' => [
'content' => $cta_content,
'background_color' => '#FEF7E8',
'button_color' => '#FFC65C',
'button_text_color' => '#000000',
]
);
}
}
if ( class_exists( 'WooCommerce' ) ) {
$todos = $this->get_todo_lists();
if ( ! empty( $todos ) ) {
$email_sections[] = array(
'type' => 'section_header',
'data' => array(
'title' => __( 'Get More From FunnelKit', 'wp-marketing-automations' ),
'subtitle' => __( 'Go through the checklist and watch your sales soar', 'wp-marketing-automations' ),
),
);
$link = add_query_arg( [
'utm_campaign' => 'FKA+Lite+Notification',
'utm_medium' => 'Todo'
], $upgrade_link );
$email_sections[] = array(
'type' => 'todo_status',
'data' => array(
'todolist' => $todos,
'upgrade_link' => $link
),
);
}
}
$email_sections = array_merge( $email_sections, array(
array(
'type' => 'dynamic',
'callback' => array( $this, 'get_dynamic_content_2' ),
),
array(
'type' => 'email_footer',
'data' => array(
'date' => $date_range,
'business_name' => ! empty( $global_settings['bwfan_setting_business_name'] ) ? $global_settings['bwfan_setting_business_name'] : get_bloginfo( 'name' ),
'business_address' => ! empty( $global_settings['bwfan_setting_business_address'] ) ? $global_settings['bwfan_setting_business_address'] : '',
),
),
) );
return apply_filters( 'bwfan_weekly_notification_email_section', $email_sections );
}
/**
* Returns the HTML content for the email.
*
* @return string The HTML content of the email.
*/
public function get_content_html() {
$email_sections = $this->get_email_sections();
ob_start();
foreach ( $email_sections as $section ) {
if ( empty( $section['type'] ) ) {
continue;
}
switch ( $section['type'] ) {
case 'email_header':
echo BWFAN_Notification_Email::get_template_html( 'emails/email-header.php' ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
break;
case 'highlight':
echo BWFAN_Notification_Email::get_template_html( 'emails/admin-email-report-highlight.php', $section['data'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
break;
case 'metrics':
echo BWFAN_Notification_Email::get_template_html( 'emails/admin-email-report-metrics.php', $section['data'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
break;
case 'section_header':
echo BWFAN_Notification_Email::get_template_html( 'emails/email-section-header.php', $section['data'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
break;
case 'todo_status':
echo BWFAN_Notification_Email::get_template_html( 'emails/admin-email-report-todo-status.php', $section['data'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
break;
case 'divider':
echo BWFAN_Notification_Email::get_template_html( 'emails/email-divider.php' ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
break;
case 'email_footer':
echo BWFAN_Notification_Email::get_template_html( 'emails/email-footer.php', $section['data'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
break;
case 'dynamic':
if ( isset( $section['callback'] ) && is_callable( $section['callback'] ) ) {
call_user_func( $section['callback'], $section['data'] ?? [] );
}
break;
case 'bwfan_status_section':
if ( ! empty( $section['data'] ) ) {
echo BWFAN_Notification_Email::get_template_html( 'emails/email-bwfan-status-section.php', $section['data'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
break;
case 'bwfan_status_w_cta_section':
if ( ! empty( $section['data'] ) ) {
echo BWFAN_Notification_Email::get_template_html( 'emails/email-bwfan-status-w-btn-section.php', $section['data'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
break;
default:
do_action( 'bwfan_email_section_' . $section['type'], isset( $section['data'] ) ? $section['data'] : [] );
break;
}
}
return ob_get_clean();
}
/**
* Returns the dynamic content for the email.
*
* @return string The dynamic content of the email.
*/
public function get_dynamic_content_1() {
do_action( 'bwfan_email_dynamic_content_1', $this->id, $this->data, $this->dates );
}
/**
* Returns the dynamic content for the email.
*
* @return string The dynamic content of the email.
*/
public function get_dynamic_content_2() {
do_action( 'bwfan_email_dynamic_content_2', $this->id, $this->data, $this->dates );
}
/**
* Retrieves the active automations from the database.
*
* This function queries the database to fetch the distinct 'event' values from the 'bwfan_automations' table
* where the 'v' column is equal to 2 and the 'status' column is equal to 1.
*
* @return array An array of distinct 'event' values from the 'bwfan_automations' table.
* @global wpdb $wpdb The WordPress database object.
*/
public function get_active_automations() {
global $wpdb;
$table_name = $wpdb->prefix . 'bwfan_automations';
$results = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT `event` FROM `$table_name` WHERE `v` = %d AND `status` = %d", 2, 1 ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $results;
}
/**
* Get all todos with their status
*
* @return array|array[]
*/
public function get_todo_lists() {
$to_dos = array(
'contact_created' => array(
'title' => __( 'Create or Import Contacts', 'wp-marketing-automations' ),
'link' => esc_url( admin_url( 'admin.php?page=autonami&path=/contacts' ) ),
),
'automation_created' => array(
'title' => __( 'Create Automation', 'wp-marketing-automations' ),
'link' => esc_url( admin_url( 'admin.php?page=autonami&path=/automations' ) ),
),
'email_settings_saved' => array(
'title' => __( 'Complete Email Settings', 'wp-marketing-automations' ),
'link' => esc_url( admin_url( 'admin.php?page=autonami&path=/settings' ) ),
),
'audience_created' => array(
'title' => __( 'Create Audience', 'wp-marketing-automations' ),
'link' => esc_url( admin_url( 'admin.php?page=autonami&path=/manage/audiences' ) ),
),
'broadcast_created' => array(
'title' => __( 'Create Broadcast', 'wp-marketing-automations' ),
'link' => esc_url( admin_url( 'admin.php?page=autonami&path=/broadcasts/email' ) ),
'last' => true,
),
);
$incomplete_todo = 0;
foreach ( $to_dos as $key => $to_do ) {
$method_name = 'metric_' . $key;
$status = method_exists( $this, $method_name ) ? $this->$method_name() : false;
if ( 'active' !== $status ) {
$incomplete_todo = 1;
}
$to_dos[ $key ]['status'] = $status;
}
if ( 0 === intval( $incomplete_todo ) ) {
return [];
}
return $to_dos;
}
/**
* Returns the frequency string based on the given frequency.
*
* @param string $frequency The frequency value.
*
* @return string The frequency string.
*/
public function get_frequency_string( $frequency ) {
switch ( $frequency ) {
case 'daily':
return __( 'day', 'wp-marketing-automations' );
case 'weekly':
return __( 'week', 'wp-marketing-automations' );
case 'monthly':
return __( 'month', 'wp-marketing-automations' );
default:
return '';
}
}
protected function metric_contact_created() {
$id = BWFCRM_Model_Contact::get_first_contact_id();
return intval( $id ) > 0 ? 'active' : 'inactive';
}
protected function metric_email_settings_saved() {
$data = BWFAN_Common::get_global_settings();
if ( ! isset( $data['bwfan_setting_business_name'] ) || ! isset( $data['bwfan_setting_business_address'] ) || empty( $data['bwfan_setting_business_name'] ) || empty( $data['bwfan_setting_business_address'] ) ) {
return 'inactive';
}
return 'active';
}
protected function metric_automation_created() {
$id = BWFAN_Model_Automations::get_first_automation_id();
return intval( $id ) > 0 ? 'active' : 'inactive';
}
protected function metric_audience_created() {
if ( ! bwfan_is_autonami_pro_active() ) {
return 'pro';
}
$id = method_exists( 'BWFCRM_Audience', 'get_first_audience_id' ) ? BWFCRM_Audience::get_first_audience_id() : null;
return intval( $id ) > 0 ? 'active' : 'inactive';
}
protected function metric_broadcast_created() {
if ( ! bwfan_is_autonami_pro_active() ) {
return 'pro';
}
$id = BWFAN_Model_Broadcast::get_first_broadcast_id();
return intval( $id ) > 0 ? 'active' : 'inactive';
}
}

View File

@@ -0,0 +1,599 @@
<?php
#[AllowDynamicProperties]
class BWFAN_Notification_Email {
/**
* The single instance of the class.
*
* @var BWFAN_Notification_Email
*/
protected static $instance = null;
/**
* Global settings.
*
* @var array
*/
protected $global_settings = array();
/**
* Last executed notification.
*
* @var array
*/
protected $executed_last = array();
/**
* Constructor.
*/
public function __construct() {
$this->init();
}
/**
* Initialize the class.
*/
public function init() {
/** Schedule Email Notification WP cron event */
add_action( 'bwfan_after_save_global_settings', array( $this, 'set_scheduler' ), 10, 2 );
/** Email notification callback */
add_action( 'bwfan_run_notifications', array( $this, 'run_notifications' ) );
/** Testing */
add_action( 'admin_init', array( $this, 'test_notification_admin' ) );
add_action( 'wp_ajax_bwfan_send_test_email_notification', array( $this, 'send_test_email_notification' ) );
}
/**
* Get the instance of the class.
*
* @return BWFAN_Notification_Email
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Retrieves the HTML content of a template.
*
* This method includes the specified template file and allows passing arguments to it.
*
* @param $template
* @param $args
*
* @return false|string
*/
public static function get_template_html( $template, $args = array() ) {
if ( ! empty( $args ) && is_array( $args ) ) {
extract( $args ); // @codingStandardsIgnoreLine
}
ob_start();
include BWFAN_PLUGIN_DIR . '/templates/' . $template;
return ob_get_clean();
}
/**
* Set the scheduler for the BWFan notification email.
*
* This function is triggered when the 'bwfan_global_settings' option is updated.
* It checks if the 'bwfan_notification_time' value has changed and reschedules the 'bwfan_run_notifications' action accordingly.
*
* @param array $old_value The old value of the option.
* @param array $value The new value of the option.
*
* @return void
*/
public function set_scheduler( $old_value, $value ) {
if ( isset( $value['bwfan_enable_notification'] ) && $value['bwfan_enable_notification'] ) {
if ( isset( $old_value['bwfan_notification_time'] ) && $old_value['bwfan_notification_time'] !== $value['bwfan_notification_time'] ) {
if ( bwf_has_action_scheduled( 'bwfan_run_notifications' ) ) {
bwf_unschedule_actions( 'bwfan_run_notifications' );
}
}
if ( ! bwf_has_action_scheduled( 'bwfan_run_notifications' ) ) {
$notification_time = [];
if ( isset( $value['bwfan_notification_time'] ) && is_array( $value['bwfan_notification_time'] ) ) {
$notification_time = $value['bwfan_notification_time'];
}
$timestamp = $this->create_timestamp_from_array( $notification_time );
bwf_schedule_single_action( $timestamp, 'bwfan_run_notifications' );
}
return;
}
if ( bwf_has_action_scheduled( 'bwfan_run_notifications' ) ) {
bwf_unschedule_actions( 'bwfan_run_notifications' );
}
}
/**
* Create a timestamp from an array of time values.
*
* @param array $time_array An array of time values.
*
* @return int|bool The timestamp or false if required keys are missing.
*/
public function create_timestamp_from_array( $time_array ) {
// Check if required keys exist in the array
if ( isset( $time_array['hours'], $time_array['minutes'], $time_array['ampm'] ) ) {
$hours = intval( $time_array['hours'] );
$minutes = intval( $time_array['minutes'] );
$ampm = strtolower( $time_array['ampm'] );
if ( $ampm === 'am' && 12 === $hours ) {
$hours = 0;
} elseif ( $ampm === 'pm' && $hours < 12 ) {
// Convert 12-hour format to 24-hour format
$hours += 12;
}
return BWFAN_Common::get_store_time( $hours, $minutes, 0 );
}
return BWFAN_Common::get_store_time( 10 );
}
/**
* bwfan_run_notifications action callback
*
* @return void
* @throws DateMalformedStringException
*/
public function run_notifications() {
/** global settings */
$this->global_settings = BWFAN_Common::get_global_settings();
if ( false === $this->is_notification_active() ) {
return;
}
$frequencies = $this->get_frequencies();
if ( empty( $frequencies ) ) {
return;
}
/** Fetch the saved notifications data */
$this->executed_last = get_option( 'bwfan_email_notification_updated', array(
'daily' => '',
'weekly' => '',
'monthly' => '',
) );
$frequencies = $this->filter_frequencies( $frequencies );
if ( empty( $frequencies ) ) {
return;
}
$frequencies = self::prepare_frequencies( $frequencies );
if ( empty( $frequencies ) ) {
return;
}
foreach ( $frequencies as $frequency => $dates ) {
$this->send_email( $frequency, $dates );
}
}
/**
* Check if email notification is active.
*
* @return bool
*/
protected function is_notification_active() {
return isset( $this->global_settings['bwfan_enable_notification'] ) && $this->global_settings['bwfan_enable_notification'];
}
/**
* Get the frequencies for email notifications.
*
* @return array
*/
protected function get_frequencies() {
if ( isset( $this->global_settings['bwf_notification_frequency'] ) && is_array( $this->global_settings['bwf_notification_frequency'] ) ) {
return $this->global_settings['bwf_notification_frequency'];
}
return array();
}
/**
* Filter the frequencies based on the last saved option key.
*
* @param array $frequencies The frequencies to filter.
*
* @return array The filtered frequencies.
*/
protected function filter_frequencies( $frequencies = array() ) {
if ( empty( $frequencies ) ) {
return array();
}
/** Filter out the frequencies if an email was already sent */
return array_filter( $frequencies, function ( $frequency ) {
return ! $this->mail_sent( $frequency );
} );
}
/**
* Prepare frequencies
*
* @param $frequencies
*
* @return array
* @throws DateMalformedStringException
*/
public static function prepare_frequencies( $frequencies = [] ) {
$final = array();
if ( array_search( 'daily', $frequencies ) !== false ) {
$final['daily'] = BWFAN_Common::get_notification_day_range();
}
if ( array_search( 'weekly', $frequencies ) !== false ) {
$final['weekly'] = BWFAN_Common::get_notification_week_range();
}
if ( array_search( 'monthly', $frequencies ) !== false ) {
$final['monthly'] = BWFAN_Common::get_notification_month_range();
}
return $final;
}
/**
* Check if the email was sent for the given frequency.
*
* @param string $frequency The frequency to check.
*
* @return bool True if the email was sent, false otherwise.
*/
public function mail_sent( $frequency ) {
$today = new DateTime( 'now', wp_timezone() );
/** Check if the last execution time for the given frequency is not set */
if ( ! isset( $this->executed_last[ $frequency ] ) || empty( $this->executed_last[ $frequency ] ) ) {
return false;
}
try {
$last_sent = new DateTime( $this->executed_last[ $frequency ] );
$last_sent->setTimezone( wp_timezone() );
} catch ( Exception $e ) {
BWFAN_Common::log_test_data( "Frequency {$frequency} and value {$this->executed_last[ $frequency ]}", 'notification-error', true );
BWFAN_Common::log_test_data( "Exception {$e->getMessage()}", 'notification-error', true );
return false;
} catch ( Error $e ) {
BWFAN_Common::log_test_data( "Frequency {$frequency} and value {$this->executed_last[ $frequency ]}", 'notification-error', true );
BWFAN_Common::log_test_data( "Error {$e->getMessage()}", 'notification-error', true );
return false;
}
switch ( $frequency ) {
case 'daily':
return ! ( intval( $last_sent->format( 'Ymd' ) ) < intval( $today->format( 'Ymd' ) ) );
case 'weekly':
return ! ( intval( $last_sent->format( 'YW' ) ) < intval( $today->format( 'YW' ) ) );
case 'monthly':
return ! ( intval( $last_sent->format( 'Ym' ) ) < intval( $today->format( 'Ym' ) ) );
default:
return false;
}
}
/**
* Send email notification.
*
* @param $frequency
* @param $dates
*
* @return void
* @throws DateMalformedStringException
*/
public function send_email( $frequency, $dates ) {
/** Prepare metrics */
$metrics_controller = new BWFAN_Notification_Metrics_Controller( $dates, $frequency );
$metrics_controller->prepare_data();
/** Check if email has data */
if ( ! $metrics_controller->is_valid() ) {
return;
}
$data = $metrics_controller->get_data();
$email_controller = new BWFAN_Notification_Email_Controller( $frequency, $data, $dates );
/** Check if there are no recipients */
$to = $this->get_recipients();
if ( empty( $to ) ) {
return;
}
$subject = $this->get_email_subject( $frequency, $dates );
$body = $email_controller->get_content_html();
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
$sent = false;
if ( is_array( $to ) && count( $to ) > 0 ) {
foreach ( $to as $recipient ) {
$sent = wp_mail( $recipient, $subject, $body, $headers );
}
}
/** Update the last execution time if the email was sent */
if ( $sent ) {
$this->executed_last[ $frequency ] = date( 'c' );
update_option( 'bwfan_email_notification_updated', $this->executed_last, false );
}
}
/**
* Get the recipients for the email.
*
* @return array The recipients for the email.
*/
private function get_recipients() {
$recipients = [];
if ( isset( $this->global_settings['bwf_notification_user_selector'] ) && is_array( $this->global_settings['bwf_notification_user_selector'] ) ) {
foreach ( $this->global_settings['bwf_notification_user_selector'] as $user ) {
if ( isset( $user['id'] ) && ! empty( $user['id'] ) ) {
$user_data = get_userdata( $user['id'] );
if ( $user_data ) {
$recipients[] = $user_data->user_email;
}
}
}
}
if ( isset( $this->global_settings['bwfan_external_user'] ) && is_array( $this->global_settings['bwfan_external_user'] ) ) {
foreach ( $this->global_settings['bwfan_external_user'] as $user ) {
if ( isset( $user['mail'] ) && ! empty( $user['mail'] ) ) {
$recipients[] = $user['mail'];
}
}
}
if ( empty( $recipients ) ) {
$recipients[] = get_option( 'admin_email' );
}
/** Filter array */
$recipients = array_filter( $recipients, function ( $email ) {
return ( strpos( $email, 'support@' ) === false );
} );
if ( is_array( $recipients ) ) {
$recipients = array_unique( $recipients );
sort( $recipients );
}
/**
* Filter the email recipients before returning
*
* @param array $recipients Array of email addresses
*/
return apply_filters( 'bwfan_notification_recipients', $recipients );
}
/**
* Get the email subject.
*
* @param $frequency
* @param $dates
*
* @return string
* @throws DateMalformedStringException
*/
public static function get_email_subject( $frequency, $dates ) {
$date_string = self::get_date_string( $dates, $frequency );
switch ( $frequency ) {
case 'daily':
/* translators: 1: Dynamic Text, 2: Dynamic Date */ return sprintf( __( '%1$s - Daily Report for %2$s', 'wp-marketing-automations' ), get_bloginfo( 'name' ), $date_string );
case 'weekly':
/* translators: 1: Dynamic Text, 2: Dynamic Date */ return sprintf( __( '%1$s - Weekly Report for %2$s', 'wp-marketing-automations' ), get_bloginfo( 'name' ), $date_string );
case 'monthly':
/* translators: 1: Dynamic Text, 2: Dynamic Date */ return sprintf( __( '%1$s - Monthly Report for %2$s', 'wp-marketing-automations' ), get_bloginfo( 'name' ), $date_string );
default:
return '';
}
}
/**
* Get the date string for the email subject.
*
* @param $dates
* @param $frequency
*
* @return string
* @throws DateMalformedStringException
*/
public static function get_date_string( $dates = array(), $frequency = 'weekly' ) {
if ( 'daily' === $frequency && isset( $dates['from_date'] ) ) {
return self::format_date( $dates['from_date'] );
}
if ( isset( $dates['from_date'] ) && isset( $dates['to_date'] ) ) {
/* translators: 1: Dynamic From Date, 2: Dynamic to date */
return sprintf( __( '%1$s - %2$s', 'wp-marketing-automations' ), self::format_date( $dates['from_date'] ), self::format_date( $dates['to_date'] ) );
}
return '';
}
/**
* Formats a date string to the desired format.
*
* @param $date_string
*
* @return string
* @throws DateMalformedStringException
*/
public static function format_date( $date_string ) {
/** Convert date string to a DateTime object */
$date = new DateTime( $date_string );
$date->setTimezone( wp_timezone() );
return $date->format( 'F j' );
}
/**
* Testing email notification.
*/
public function test_notification_admin() {
if ( false === current_user_can( 'administrator' ) ) {
return;
}
if ( ! isset( $_GET['bwfan_email_preview'] ) ) {
return;
}
$mode = filter_input( INPUT_GET, 'bwfan_mode', FILTER_SANITIZE_STRING );
$mode = empty( $mode ) ? 'weekly' : $mode;
switch ( $mode ) {
case 'monthly':
$range = BWFAN_Common::get_notification_month_range();
break;
case 'daily':
$range = BWFAN_Common::get_notification_day_range();
break;
default:
$range = BWFAN_Common::get_notification_week_range();
$mode = 'weekly';
break;
}
$dates = array(
'from_date' => $range['from_date'],
'to_date' => $range['to_date'],
'from_date_previous' => $range['from_date_previous'],
'to_date_previous' => $range['to_date_previous'],
);
/** Prepare metrics */
$metrics_controller = new BWFAN_Notification_Metrics_Controller( $dates, $mode );
$metrics_controller->prepare_data();
$data = $metrics_controller->get_data();
$email_controller = new BWFAN_Notification_Email_Controller( $mode, $data, $dates );
echo $email_controller->get_content_html(); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit;
}
/**
* Send test email notification.
*/
public function send_test_email_notification() {
if ( ! current_user_can( 'administrator' ) ) {
wp_send_json_error( __( 'You do not have permission to perform this action.', 'wp-marketing-automations' ) );
}
$mode = filter_input( INPUT_GET, 'bwfan_mode', FILTER_SANITIZE_STRING );
$mode = empty( $mode ) ? 'weekly' : $mode;
switch ( $mode ) {
case 'monthly':
$range = BWFAN_Common::get_notification_month_range();
break;
case 'daily':
$range = BWFAN_Common::get_notification_day_range();
break;
default:
$range = BWFAN_Common::get_notification_week_range();
$mode = 'weekly';
break;
}
$dates = array(
'from_date' => $range['from_date'],
'to_date' => $range['to_date'],
'from_date_previous' => $range['from_date_previous'],
'to_date_previous' => $range['to_date_previous'],
);
/** Prepare metrics */
$metrics_controller = new BWFAN_Notification_Metrics_Controller( $dates, $mode );
$metrics_controller->prepare_data();
$data = $metrics_controller->get_data();
$email_controller = new BWFAN_Notification_Email_Controller( $mode, $data, $dates );
$to = get_option( 'admin_email' );
$subject = self::get_email_subject( $mode, $dates );
$body = $email_controller->get_content_html();
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
$sent = wp_mail( $to, $subject, $body, $headers );
if ( $sent ) {
wp_send_json_success( __( 'Email sent successfully.', 'wp-marketing-automations' ) );
} else {
wp_send_json_error( __( 'Failed to send email.', 'wp-marketing-automations' ) );
}
}
/**
* Function to set the default settings for notification settings
*
* @return void
*/
public static function set_bwfan_settings() {
$bwfan_settings = BWFAN_Common::get_global_settings();
if ( isset( $bwfan_settings['bwfan_enable_notification'] ) && ! empty( $bwfan_settings['bwfan_enable_notification'] ) ) {
return;
}
$new_settings = array(
'bwfan_enable_notification' => true,
'bwf_notification_frequency' => array( 'weekly', 'monthly' ),
'bwfan_notification_time' => array(
'hours' => '10',
'minutes' => '00',
'ampm' => 'am',
),
'bwfan_external_user' => array(),
);
$bwfan_settings = array_merge( $bwfan_settings, $new_settings );
$users = get_users( array( 'role' => 'administrator' ) );
$user_selector = array();
foreach ( $users as $user ) {
$user_selector[] = array(
'id' => $user->ID,
'name' => $user->display_name . ' ( ' . $user->user_email . ' )',
);
}
$bwfan_settings['bwf_notification_user_selector'] = $user_selector;
/** Set scheduler */
$old_settings = array(
'bwfan_notification_time' => array(
'hours' => '09',
'minutes' => '00',
'ampm' => 'am',
),
);
self::$instance->set_scheduler( $old_settings, $new_settings );
update_option( 'bwfan_global_settings', $bwfan_settings );
}
}
if ( class_exists( 'BWFAN_Core' ) ) {
BWFAN_Core::register( 'notification_email', 'BWFAN_Notification_Email' );
}

View File

@@ -0,0 +1,356 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
/**
* Class BWFAN_Notification_Metrics_Controller
*/
class BWFAN_Notification_Metrics_Controller {
protected $data = array();
protected $date_params = array();
private $frequency = '';
/**
* Constructor.
*
* @param array $date_params
*/
public function __construct( $date_params = array(), $frequency = 'weekly' ) {
$this->date_params = wp_parse_args( $date_params, array(
'from_date' => date( 'Y-m-d 00:00:00', strtotime( '-1 day' ) ),
'to_date' => date( 'Y-m-d 23:59:59', strtotime( '-1 day' ) ),
'from_date_previous' => date( 'Y-m-d 00:00:00', strtotime( '-2 day' ) ),
'to_date_previous' => date( 'Y-m-d 23:59:59', strtotime( '-2 day' ) ),
) );
$this->frequency = $frequency;
}
/**
* Get metrics.
*
* @return array
*/
public function get_data() {
return $this->data;
}
/**
* Prepare data.
*/
public function prepare_data() {
$this->data['metrics'] = array();
$this->data['metrics']['total_contacts'] = $this->get_total_contacts();
$this->data['metrics'] = array_merge( $this->data['metrics'], $this->get_total_engagement_sent() );
if ( class_exists( 'WooCommerce' ) ) {
$this->data['metrics'] = array_merge( $this->data['metrics'], $this->get_total_carts() );
$this->data['metrics'] = array_merge( $this->data['metrics'], $this->get_conversions() );
}
}
/**
* Get percentage change.
*
* @param int $previous_value The previous value.
* @param int $current_value The current value.
*
* @return float
*/
public function get_percentage_change( $previous_value = 0, $current_value = 0 ) {
switch ( $previous_value ) {
case 0:
return $current_value * 100;
default:
return ( ( $current_value - $previous_value ) / $previous_value ) * 100;
}
}
/**
* Get total contacts.
*
* @return array
*/
public function get_total_contacts() {
$contacts = BWFAN_Dashboards::get_total_contacts( $this->date_params['from_date'], $this->date_params['to_date'] );
$previous_contacts = BWFAN_Dashboards::get_total_contacts( $this->date_params['from_date_previous'], $this->date_params['to_date_previous'] );
// Calculate percentage change
$percentage_change = $this->get_percentage_change( $previous_contacts, $contacts );
return array(
'text' => __( 'New Contacts', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Text. */ 'previous_text' => sprintf( __( '- Previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_text() ),
'count' => $contacts,
'previous_count' => $previous_contacts,
'percentage_change' => sprintf( '%s%%', round( $percentage_change, 2 ) ),
'percentage_change_positive' => $percentage_change >= 0,
);
}
/**
* Get total engagement sent.
*
* @return array
*/
public function get_total_engagement_sent() {
$engagement_sent = BWFAN_Dashboards::get_total_engagement_sents( $this->date_params['from_date'], $this->date_params['to_date'], '', '' );
$previous_engagement_sent = BWFAN_Dashboards::get_total_engagement_sents( $this->date_params['from_date_previous'], $this->date_params['to_date_previous'], '', '' );
$engagement_open = BWFAN_Dashboards::get_total_email_open( $this->date_params['from_date'], $this->date_params['to_date'], '', '' );
$previous_engagement_open = BWFAN_Dashboards::get_total_email_open( $this->date_params['from_date_previous'], $this->date_params['to_date_previous'], '', '' );
$engagement_click = BWFAN_Dashboards::get_total_email_click( $this->date_params['from_date'], $this->date_params['to_date'], '', '' );
$previous_engagement_click = BWFAN_Dashboards::get_total_email_click( $this->date_params['from_date_previous'], $this->date_params['to_date_previous'], '', '' );
return array(
'email_sent' => $this->get_total_email_sent( $engagement_sent, $previous_engagement_sent ),
'email_open' => $this->get_total_email_open( $engagement_open, $previous_engagement_open ),
'email_click' => $this->get_total_email_click( $engagement_click, $previous_engagement_click ),
);
}
/**
* Get total email click.
*
* @param array $engagement_click The engagement sent data for the current date range.
* @param array $previous_engagement_click The engagement sent data for the previous date range.
*
* @return array
*/
public function get_total_email_click( $engagement_click, $previous_engagement_click ) {
$email_click = isset( $engagement_click[0]['email_click'] ) ? $engagement_click[0]['email_click'] : 0;
$previous_email_click = isset( $previous_engagement_click[0]['email_click'] ) ? $previous_engagement_click[0]['email_click'] : 0;
// Calculate percentage change
$percentage_change = $this->get_percentage_change( $previous_email_click, $email_click );
return array(
'text' => __( 'Emails Clicked', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Text. */ 'previous_text' => sprintf( __( '- Previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_text() ),
'count' => $email_click,
'previous_count' => $previous_email_click,
'percentage_change' => sprintf( '%s%%', round( $percentage_change, 2 ) ),
'percentage_change_positive' => $percentage_change >= 0,
);
}
/**
* Get total email open.
*
* @param array $engagement_sent The engagement sent data for the current date range.
* @param array $previous_engagement_sent The engagement sent data for the previous date range.
*
* @return array
*/
public function get_total_email_open( $engagement_open, $previous_engagement_open ) {
$email_open = isset( $engagement_open[0]['email_open'] ) ? $engagement_open[0]['email_open'] : 0;
$previous_email_open = isset( $previous_engagement_open[0]['email_open'] ) ? $previous_engagement_open[0]['email_open'] : 0;
// Calculate percentage change
$percentage_change = $this->get_percentage_change( $previous_email_open, $email_open );
return array(
'text' => __( 'Emails Opened', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Text. */ 'previous_text' => sprintf( __( '- Previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_text() ),
'count' => $email_open,
'previous_count' => $previous_engagement_open,
'percentage_change' => sprintf( '%s%%', round( $percentage_change, 2 ) ),
'percentage_change_positive' => $percentage_change >= 0,
);
}
/**
* Get total email sent.
*
* @param array $engagement_sent The engagement sent data for the current date range.
* @param array $previous_engagement_sent The engagement sent data for the previous date range.
*
* @return array
*/
public function get_total_email_sent( $engagement_sent, $previous_engagement_sent ) {
$email_sent = isset( $engagement_sent[0]['email_sents'] ) ? $engagement_sent[0]['email_sents'] : 0;
$previous_email_sent = isset( $previous_engagement_sent[0]['email_sents'] ) ? $previous_engagement_sent[0]['email_sents'] : 0;
// Calculate percentage change
$percentage_change = $this->get_percentage_change( $previous_email_sent, $email_sent );
return array(
'text' => __( 'Emails Sent', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Text. */ 'previous_text' => sprintf( __( '- Previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_text() ),
'count' => $email_sent,
'previous_count' => $previous_email_sent,
'percentage_change' => sprintf( '%s%%', round( $percentage_change, 2 ) ),
'percentage_change_positive' => $percentage_change >= 0,
);
}
/**
* Get total sms sent.
*
* @param array $engagement_sent The engagement sent data for the current date range.
* @param array $previous_engagement_sent The engagement sent data for the previous date range.
*
* @return array
*/
public function get_total_sms_sent( $engagement_sent, $previous_engagement_sent ) {
$sms_sent = isset( $engagement_sent[0]['sms_sent'] ) ? $engagement_sent[0]['sms_sent'] : 0;
$previous_sms_sent = isset( $previous_engagement_sent[0]['sms_sent'] ) ? $previous_engagement_sent[0]['sms_sent'] : 0;
// Calculate percentage change
$percentage_change = $this->get_percentage_change( $previous_sms_sent, $sms_sent );
return array(
'text' => __( 'SMS Sent', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Text. */ 'previous_text' => sprintf( __( '- Previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_text() ),
'count' => $sms_sent,
'previous_count' => $previous_sms_sent,
'percentage_change' => sprintf( '%s%%', round( $percentage_change, 2 ) ),
'percentage_change_positive' => $percentage_change >= 0,
);
}
/**
* Get total carts.
*
* @return array
*/
private function get_total_carts() {
require_once BWFAN_PLUGIN_DIR . '/includes/class-bwfan-cart-analytics.php';
$captured_cart = BWFAN_Cart_Analytics::get_captured_cart( $this->date_params['from_date'], $this->date_params['to_date'] );
$previous_captured_cart = BWFAN_Cart_Analytics::get_captured_cart( $this->date_params['from_date_previous'], $this->date_params['to_date_previous'] );
$recovered_cart = BWFAN_Cart_Analytics::get_recovered_cart( $this->date_params['from_date'], $this->date_params['to_date'] );
$previous_recovered_cart = BWFAN_Cart_Analytics::get_recovered_cart( $this->date_params['from_date_previous'], $this->date_params['to_date_previous'] );
$recovered_count = isset( $recovered_cart[0]['count'] ) ? $recovered_cart[0]['count'] : 0;
$previous_recovered_count = isset( $previous_recovered_cart[0]['count'] ) ? $previous_recovered_cart[0]['count'] : 0;
// Calculate percentage change
$recovered_percentage_change = $this->get_percentage_change( $previous_recovered_count, $recovered_count );
$count = isset( $captured_cart[0]['count'] ) ? $captured_cart[0]['count'] : 0;
$previous_count = isset( $previous_captured_cart[0]['count'] ) ? $previous_captured_cart[0]['count'] : 0;
// Calculate percentage change
$percentage_change = $this->get_percentage_change( $previous_count, $count );
return array(
array(
'text' => __( 'Carts Captured', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Text. */ 'previous_text' => sprintf( __( '- Previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_text() ),
'count' => $count,
'previous_count' => $previous_count,
'percentage_change' => sprintf( '%s%%', round( $percentage_change, 2 ) ),
'percentage_change_positive' => $percentage_change >= 0,
),
array(
'text' => __( 'Carts Recovered', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Text. */ 'previous_text' => sprintf( __( '- Previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_text() ),
'count' => $recovered_count,
'previous_count' => $previous_recovered_count,
'percentage_change' => sprintf( '%s%%', round( $recovered_percentage_change, 2 ) ),
'percentage_change_positive' => $recovered_percentage_change >= 0,
),
);
}
/**
* Get conversions.
*
* @return array
*/
public function get_conversions() {
$total_orders = BWFAN_Dashboards::get_total_orders( $this->date_params['from_date'], $this->date_params['to_date'], '', '' );
$previous_total_orders = BWFAN_Dashboards::get_total_orders( $this->date_params['from_date_previous'], $this->date_params['to_date_previous'], '', '' );
return array(
'total_orders' => $this->get_total_orders( $total_orders, $previous_total_orders ),
'total_revenue' => $this->get_total_revenue( $total_orders, $previous_total_orders ),
);
}
/**
* Get total orders.
*
* @param array $total_orders The total orders data for the current date range.
* @param array $previous_total_orders The total orders data for the previous date range.
*
* @return array
*/
public function get_total_orders( $total_orders, $previous_total_orders ) {
$total_orders = isset( $total_orders[0]['total_orders'] ) ? $total_orders[0]['total_orders'] : 0;
$previous_total_orders = isset( $previous_total_orders[0]['total_orders'] ) ? $previous_total_orders[0]['total_orders'] : 0;
// Calculate percentage change
$percentage_change = $this->get_percentage_change( $previous_total_orders, $total_orders );
return array(
'text' => __( 'Total Orders', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Text. */ 'previous_text' => sprintf( __( '- Previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_text() ),
'count' => $total_orders,
'previous_count' => $previous_total_orders,
'percentage_change' => sprintf( '%s%%', round( $percentage_change, 2 ) ),
'percentage_change_positive' => $percentage_change >= 0,
);
}
/**
* Get total revenue.
*
* @param array $total_orders The total orders data for the current date range.
* @param array $previous_total_orders The total orders data for the previous date range.
*
* @return array
*/
public function get_total_revenue( $total_orders, $previous_total_orders ) {
$total_revenue = isset( $total_orders[0]['total_revenue'] ) ? $total_orders[0]['total_revenue'] : 0;
$previous_total_revenue = isset( $previous_total_orders[0]['total_revenue'] ) ? $previous_total_orders[0]['total_revenue'] : 0;
// Calculate percentage change
$percentage_change = $this->get_percentage_change( $previous_total_revenue, $total_revenue );
return array(
'text' => __( 'Total Revenue', 'wp-marketing-automations' ),
/* translators: 1: Dynamic Text. */ 'previous_text' => sprintf( __( '- Previous %1$s', 'wp-marketing-automations' ), $this->get_frequency_text() ),
'count_suffix' => function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : __( 'USD', 'wp-marketing-automations' ),
'count' => round( $total_revenue, 2 ),
'previous_count' => $previous_total_revenue,
'percentage_change' => sprintf( '%s%%', round( $percentage_change, 2 ) ),
'percentage_change_positive' => $percentage_change >= 0,
);
}
/**
* Check if email has data and ready to go
*
* @return bool
*/
public function is_valid() {
$is_valid = false;
foreach ( $this->data['metrics'] as $metric ) {
if ( floatval( $metric['count'] ) > 0 ) {
$is_valid = true;
break;
}
}
return $is_valid;
}
protected function get_frequency_text( $capitalized = false ) {
if ( 'daily' === $this->frequency ) {
return $capitalized ? __( 'Day', 'wp-marketing-automations' ) : __( 'day', 'wp-marketing-automations' );
}
if ( 'monthly' === $this->frequency ) {
return $capitalized ? __( 'Month', 'wp-marketing-automations' ) : __( 'month', 'wp-marketing-automations' );
}
return $capitalized ? __( 'Week', 'wp-marketing-automations' ) : __( 'week', 'wp-marketing-automations' );
}
}

View File

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

File diff suppressed because it is too large Load Diff

View File

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