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,51 @@
<?php
class BWFAN_API_Add_Automation_Step extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/add-step';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$type = isset( $this->args['type'] ) ? $this->get_sanitized_arg( 'type', 'text_field' ) : 'action';
$data = isset( $this->args['data'] ) ? $this->args['data'] : [];
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
/** Add new step and get id */
$step_id = $automation_obj->add_new_automation_step( $type, $data );
$this->response_code = 200;
return $this->success_response( [ 'step_id' => $step_id ], __( 'Data updated', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Add_Automation_Step' );

View File

@@ -0,0 +1,54 @@
<?php
class BWFAN_API_Add_Contact_To_Automation extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automation/add-contact';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to add contact to', 'wp-marketing-automations' ),
'type' => 'integer',
),
'contact_id' => array(
'description' => __( 'Contact ID to add into automation', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id' );
$contact_id = $this->get_sanitized_arg( 'contact_id' );
if ( empty( $automation_id ) ) {
return $this->error_response( __( 'Invalid / Empty automation ID provided', 'wp-marketing-automations' ), null, 400 );
}
if ( empty( $contact_id ) ) {
return $this->error_response( __( 'Invalid / Empty contact ID provided', 'wp-marketing-automations' ), null, 400 );
}
$ins = new BWFAN_Add_Contact_To_Automation_Controller( $automation_id, $contact_id );
$response = $ins->add_contact_to_automation();
$this->response_code = $response['code'] ?? 200;
$message = $response['message'] ?? __( 'Unknown error occurred', 'wp-marketing-automations' );
return $this->success_response( [], $message );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Add_Contact_To_Automation' );

View File

@@ -0,0 +1,60 @@
<?php
if ( ! class_exists( 'BWFAN_API_Automation_Bulk_Action' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_API_Automation_Bulk_Action extends BWFAN_API_Base {
public static $ins;
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = 'v3/automation/(?P<automation_id>[\\d]+)/bulk-action';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$action = $this->get_sanitized_arg( 'action', 'text_field' );
$status = $this->get_sanitized_arg( 'status', 'text_field' );
$a_cids = isset( $this->args['a_cids'] ) ? $this->args['a_cids'] : [];
$a_cids = array_map( 'absint', $a_cids );
if ( empty( $a_cids ) ) {
return $this->error_response( [], __( 'Required parameter is missing', 'wp-marketing-automations' ) );
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
$dynamic_string = BWFAN_Common::get_dynamic_string();
$args = [ 'key' => $dynamic_string, 'aid' => $automation_id, 'action' => $action, 'status' => $status ];
sort( $a_cids );
update_option( "bwfan_bulk_automation_contact_{$action}_{$dynamic_string}", $a_cids );
bwf_schedule_recurring_action( time(), 60, "bwfan_automation_contact_bulk_action", $args );
BWFCRM_Common::ping_woofunnels_worker();
$this->response_code = 200;
return $this->success_response( [], __( 'Process is scheduled. Will soon be executed.', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Automation_Bulk_Action' );
}

View File

@@ -0,0 +1,44 @@
<?php
class BWFAN_API_Automation_Contact_Bulk_Action extends BWFAN_API_Base {
public static $ins;
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automation/bulk-action';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$action = $this->get_sanitized_arg( 'action', 'text_field' );
$status = $this->get_sanitized_arg( 'status', 'text_field' );
$a_cids = isset( $this->args['a_cids'] ) ? $this->args['a_cids'] : [];
if ( empty( $a_cids ) ) {
return $this->error_response( [], __( 'Required parameter is missing', 'wp-marketing-automations' ) );
}
$dynamic_string = BWFAN_Common::get_dynamic_string();
$args = [ 'key' => $dynamic_string, 'action' => $action, 'status' => $status ];
sort( $a_cids );
update_option( "bwfan_bulk_automation_all_contact_{$action}_{$dynamic_string}", $a_cids );
bwf_schedule_recurring_action( time(), 60, "bwfan_automation_all_contact_bulk_action", $args );
BWFAN_Common::ping_woofunnels_worker();
$this->response_code = 200;
return $this->success_response( [], __( 'Process is scheduled. Will soon be executed.', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Automation_Contact_Bulk_Action' );

View File

@@ -0,0 +1,59 @@
<?php
class BWFAN_API_Bulk_Action extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/bulk-action/(?P<type>[a-zA-Z0-9_-]+)';
}
public function default_args_values() {
$args = [
'ids' => []
];
return $args;
}
public function process_api_call() {
$type = $this->get_sanitized_arg( 'type' );
$ids = ! empty( $this->args['ids'] ) ? array_map( 'absint', $this->args['ids'] ) : [];
$cart_type = isset( $this->args['cart_type'] ) ? $this->args['cart_type'] : '';
if ( empty( $ids ) ) {
return $this->error_response( __( 'Invalid or empty ids', 'wp-marketing-automations' ), null, 400 );
}
$dynamic_string = BWFAN_Common::get_dynamic_string();
$args = [ 'key' => $dynamic_string, 'type' => $type, 'cart_type' => $cart_type ];
sort( $ids );
update_option( "bwfan_bulk_action_{$dynamic_string}", $ids );
bwf_schedule_recurring_action( time(), 60, "bwfan_bulk_action", $args );
$response = BWFAN_Common::bwfan_bulk_action( $dynamic_string, $type, $cart_type );
if ( empty( $response ) ) {
delete_option( "bwfan_bulk_action_{$dynamic_string}" );
if ( bwf_has_action_scheduled( 'bwfan_bulk_action', $args ) ) {
bwf_unschedule_actions( "bwfan_bulk_action", $args );
}
return $this->success_response( [], __( 'Bulk action run successfully', 'wp-marketing-automations' ) );
}
return $this->success_response( [], __( 'Bulk action has been scheduled', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Bulk_Action' );

View File

@@ -0,0 +1,91 @@
<?php
class BWFAN_API_Change_Automation_Status extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automation/(?P<contact_automation_id>[\\d]+)/contact/status';
$this->request_args = array(
'contact_automation_id' => array(
'description' => __( 'Automation contact ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function process_api_call() {
$a_cid = $this->get_sanitized_arg( 'contact_automation_id', 'text_field' );
if ( empty( $a_cid ) ) {
return $this->error_response( [], __( 'Automation contact ID is missing', 'wp-marketing-automations' ) );
}
/** To be changed status*/
$to = $this->get_sanitized_arg( 'to', 'text_field' );
if ( empty( $to ) ) {
return $this->error_response( [], __( 'Status is missing', 'wp-marketing-automations' ) );
}
$data = ( 're_run' === $to ) ? BWFAN_Model_Automation_Complete_Contact::get( $a_cid ) : BWFAN_Model_Automation_Contact::get_data( $a_cid );
$aid = isset( $data['aid'] ) ? $data['aid'] : 0;
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $aid );
/** Check for automation exists */
if ( ! empty( $automation_obj->errors ) ) {
return $this->error_response( __( 'Automation not found', 'wp-marketing-automations' ), $automation_obj->errors );
}
switch ( $to ) {
case 'end' === $to:
$trail = isset( $data['trail'] ) ? $data['trail'] : '';
/** Update the step trail status as complete if active */
BWFAN_Model_Automation_Contact_Trail::update_all_step_trail_status_complete( $trail );
$result = BWFAN_Common::end_v2_automation( $a_cid, $data, 'manually' );
break;
case 're_run' === $to:
global $wpdb;
$query = " SELECT `ID`,`cid`,`aid`,`trail`,`event`,`data` FROM {$wpdb->prefix}bwfan_automation_complete_contact WHERE `ID` = '$a_cid' ";
$query_result = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
BWFAN_Common::insert_automations( $query_result );
$result = true;
break;
case 'startbegin' === $to:
global $wpdb;
$query = " SELECT `ID`,`cid`,`aid`,`trail` FROM {$wpdb->prefix}bwfan_automation_contact WHERE `ID` = '$a_cid'";
$query_result = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$trails = array_column( $query_result, 'trail' );
BWFAN_Common::update_automation_status( array( $a_cid ), 1, $trails, current_time( 'timestamp', 1 ), true );
$result = true;
break;
default:
$result = $automation_obj->change_automation_status( $to, $a_cid );
break;
}
if ( $result ) {
$this->response_code = 200;
return $this->success_response( [], __( 'Automation contact updated', 'wp-marketing-automations' ) );
}
$this->response_code = 404;
return $this->error_response( [], __( 'Unable to update automation contact', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Change_Automation_Status' );

View File

@@ -0,0 +1,42 @@
<?php
class BWFAN_API_Get_Custom_Search extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/custom-search/(?P<type>[a-zA-Z0-9_]+)';
}
public function process_api_call() {
do_action( 'bwfan_load_custom_search_classes' );
$type = $this->get_sanitized_arg( 'type' );
// removed get_sanitized_arg function because that was merging two or more substrings and making one
// for example : xl autonami. with get_sanitized_arg, it is becoming xlautonami, which making issue in search
$search = isset( $this->args['search'] ) && ! empty( $this->args['search'] ) ? $this->args['search'] : '';
$extra_data = $this->args;
if ( empty( $type ) ) {
return $this->error_response( __( 'Invalid or empty type', 'wp-marketing-automations' ), null, 400 );
}
if ( ! class_exists( 'BWFAN_' . $type ) ) {
return $this->error_response( __( 'Invalid or empty type', 'wp-marketing-automations' ), null, 400 );
}
$type = BWFAN_Core()->custom_search->get_custom_search( $type );
$options = $type->get_options( $search, $extra_data );
return $this->success_response( $options );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Custom_Search' );

View File

@@ -0,0 +1,146 @@
<?php
class BWFAN_API_Delete_Automation_Contacts extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/delete-contact';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID', 'wp-marketing-automations' ),
'type' => 'string',
)
);
}
public function process_api_call() {
$trail_id = $this->get_sanitized_arg( 'trail_id', 'text_field' );
$ac_id = $this->get_sanitized_arg( 'ac_id', 'text_field' );
$type = $this->get_sanitized_arg( 'type', 'text_field' );
if ( empty( $trail_id ) && empty( $ac_id ) ) {
return $this->error_response( __( 'Invalid / Empty Contact data provided', 'wp-marketing-automations' ), null, 400 );
}
/** Automation Contact table */
if ( empty( $trail_id ) && 'completed' !== $type ) {
$row = BWFAN_Model_Automation_Contact::get( $ac_id );
} else {
$row = BWFAN_Model_Automation_Contact::get_row_by_trail_id( $trail_id );
}
if ( ! empty( $row ) ) {
$resp = $this->delete_data( $row );
if ( is_array( $resp ) && isset( $resp['status'] ) && 'error' === $resp['status'] ) {
return $this->error_response( $resp['msg'], null, 400 );
}
$this->response_code = 200;
return $this->success_response( $trail_id, '' );
}
/** Search for automation contact complete row */
if ( empty( $trail_id ) && 'completed' === $type ) {
$row = BWFAN_Model_Automation_Complete_Contact::get( $ac_id );
} else {
$row = BWFAN_Model_Automation_Complete_Contact::get_row_by_trail_id( $trail_id );
}
if ( ! empty( $row ) ) {
$resp = $this->delete_data( $row, 2 );
if ( is_array( $resp ) && isset( $resp['status'] ) && 'error' === $resp['status'] ) {
return $this->error_response( $resp['msg'], null, 400 );
}
}
$this->response_code = 200;
return $this->success_response( $trail_id, __( 'Automation contact deleted', 'wp-marketing-automations' ) );
}
/**
* @param $row
* @param $mode 'default 1 - Automation contact 2 - Automation complete contact'
*
* @return string[]|void
*/
public function delete_data( $row, $mode = 1 ) {
if ( empty( $row ) ) {
return;
}
$id = $row['ID'];
$aid = $row['aid'];
$cid = $row['cid'];
$trail_id = $row['trail'];
/** Automation complete contact table */
$start_date = isset( $row['s_date'] ) ? $row['s_date'] : '';
$end_date = isset( $row['c_date'] ) ? $row['c_date'] : '';
if ( 1 === absint( $mode ) ) {
/** Automation contact table */
$start_date = $row['c_date'];
$end_date = absint( $row['last_time'] );
$end_date = ( $end_date > 0 ) ? $end_date : time();
$end_date = date( 'Y-m-d H:i:s', $end_date + 120 );
}
try {
/** Delete db row */
if ( 1 === absint( $mode ) ) {
BWFAN_Model_Automation_Contact::delete( $id );
} else {
BWFAN_Model_Automation_Complete_Contact::delete( $id );
}
BWFAN_Common::maybe_remove_aid_from_contact_fields( $cid, $aid );
/** Delete automation contact trail */
BWFAN_Model_Automation_Contact_Trail::delete_row_by_trail_by( $trail_id );
} catch ( Error $e ) {
$msg = "Error occurred in deleting the automation contact db row {$e->getMessage()}";
BWFAN_Common::log_test_data( $msg, 'automation_contact_delete_fail', true );
return [ 'status' => 'error', 'msg' => $msg ];
}
if ( empty( $start_date ) || empty( $end_date ) ) {
return;
}
if ( false === bwfan_is_autonami_pro_active() ) {
return;
}
/** Fetch engagements */
$engagements = BWFAN_Model_Engagement_Tracking::get_contact_engagements( $aid, $cid, $start_date, $end_date );
if ( empty( $engagements ) ) {
return;
}
try {
/** Delete engagement meta */
BWFAN_Model_Engagement_Trackingmeta::delete_engagements_meta( $engagements );
/** Delete engagements */
BWFAN_Model_Engagement_Tracking::delete_contact_engagements( $engagements );
/** Delete conversions */
BWFAN_Model_Conversions::delete_conversions_by_track_id( $engagements );
} catch ( Error $e ) {
}
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Automation_Contacts' );

View File

@@ -0,0 +1,112 @@
<?php
class BWFAN_API_Delete_Automation_Step extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/step/(?P<step_id>[\\d]+)';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
'step_id' => array(
'description' => __( 'Step ID to delete', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$step_id = $this->get_sanitized_arg( 'step_id', 'text_field' );
$node_type = $this->get_sanitized_arg( 'nodeType', 'text_field' );
$arg_data = $this->args;
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
/** Step data */
if ( isset( $arg_data['steps'] ) && ! empty( $arg_data['steps'] ) ) {
$steps = $arg_data['steps'];
}
/** Link data */
if ( isset( $arg_data['links'] ) && ! empty( $arg_data['links'] ) ) {
$links = $arg_data['links'];
}
/** Node count */
if ( isset( $arg_data['count'] ) && intval( $arg_data['count'] ) > 0 ) {
$count = intval( $arg_data['count'] );
}
/** Update automation data */
if ( ! empty( $steps ) && ! empty( $links ) ) {
$automation_obj->update_automation_meta_data( [], $steps, $links, $count );
}
/** Delete step and get response */
$response = $automation_obj->delete_automation_step( $step_id );
if ( ! $response ) {
return $this->error_response( [], 'Unable to delete the node' );
}
if ( 'benchmark' === $node_type || 'wait' === $node_type ) {
$status = 'wait' === $node_type ? 1 : 4;
$queued_automations = BWFAN_Model_Automation_Contact::get_automation_contact_by_sid( $step_id, '', $status );
if ( ! empty( $queued_automations ) && is_array( $queued_automations ) ) {
$key = 'bwf_queued_automations_' . $step_id;
$args = [ 'sid' => $step_id ];
/** Un-schedule action */
if ( bwf_has_action_scheduled( 'bwfan_automation_step_deleted', $args ) ) {
bwf_unschedule_actions( 'bwfan_automation_step_deleted', $args );
}
$ids = array_column( $queued_automations, 'ID' );
$queued_automations = wp_json_encode( $ids );
update_option( $key, $queued_automations, false );
bwf_schedule_recurring_action( time(), 120, 'bwfan_automation_step_deleted', $args );
}
}
$automation_obj->fetch_automation_metadata( false );
$automation_meta = $automation_obj->get_automation_meta_data();
if ( ! empty( $automation_meta['steps'] ) ) {
unset( $automation_meta['steps'] );
}
if ( ! empty( $automation_meta['links'] ) ) {
unset( $automation_meta['links'] );
}
if ( ! empty( $automation_meta['count'] ) ) {
unset( $automation_meta['count'] );
}
if ( isset( $automation_meta['event_meta'] ) ) {
$automation_meta['event_meta'] = BWFAN_Common::fetch_updated_data( $automation_meta['event_meta'] );
}
$this->response_code = 200;
return $this->success_response( [ 'meta' => $automation_meta ], __( 'Data updated', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Automation_Step' );

View File

@@ -0,0 +1,41 @@
<?php
class BWFAN_API_Get_Actions_Search_Suggestion extends BWFAN_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/actions/(?P<action>[a-zA-Z0-9_-]+)/suggestions';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$action = $this->get_sanitized_arg( 'action' );
if ( empty( $action ) ) {
return $this->error_response_200( __( 'Invalid or empty action', 'wp-marketing-automations' ), null, 400 );
}
$search = $this->get_sanitized_arg( 'search' );
$search = ! empty( $search ) ? $search : '';
$identifier = $this->get_sanitized_arg( 'identifier' );
$identifier = ! empty( $identifier ) ? $identifier : '';
$action = BWFAN_Core()->integration->get_action( $action );
$result = [];
if ( ! empty( $action ) ) {
$result = $action->get_options( $search, $identifier );
}
return $this->success_response( $result );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Actions_Search_Suggestion' );

View File

@@ -0,0 +1,99 @@
<?php
class BWFAN_API_Get_All_Automation_Contacts extends BWFAN_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automations/contacts/';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/** Customer journey Api call */
public function process_api_call() {
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? intval( $this->get_sanitized_arg( 'offset', 'text_field' ) ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
$type = ! empty( $this->get_sanitized_arg( 'type', 'text_field' ) ) ? $this->get_sanitized_arg( 'type', 'text_field' ) : 'active';
$search = ! empty( $this->get_sanitized_arg( 'search', 'text_field' ) ) ? $this->get_sanitized_arg( 'search', 'text_field' ) : '';
$cid = ! empty( $this->get_sanitized_arg( 'contact', 'text_field' ) ) ? intval( $this->get_sanitized_arg( 'contact', 'text_field' ) ) : '';
$aid = ! empty( $this->get_sanitized_arg( 'automation', 'text_field' ) ) ? intval( $this->get_sanitized_arg( 'automation', 'text_field' ) ) : '';
$contacts = BWFAN_Common::get_automation_contacts( $aid, $cid, $search, $limit, $offset, $type );
$message = __( 'Successfully fetched Automations', 'wp-marketing-automations' );
if ( ! isset( $contacts['contacts'] ) || empty( $contacts['contacts'] ) || ! is_array( $contacts['contacts'] ) ) {
$message = __( 'No data found', 'wp-marketing-automations' );
}
$completed_count = BWFAN_Model_Automation_Complete_Contact::get_complete_count( $aid, $cid );
/** active contacts = active + wait + retry */
$active_count = BWFAN_Model_Automation_Contact::get_active_count( $aid, 'active', '', $cid );
$countdata = [
'active' => $active_count,
'paused' => BWFAN_Model_Automation_Contact::get_active_count( $aid, 3, '', $cid ),
'failed' => BWFAN_Model_Automation_Contact::get_active_count( $aid, 2, '', $cid ),
'completed' => $completed_count,
'delayed' => BWFAN_Model_Automation_Contact::get_active_count( $aid, 'delayed', '', $cid, 'AND cc.e_time < ' . current_time( 'timestamp', 1 ) ),
'inactive' => BWFAN_Model_Automation_Contact::get_active_count( $aid, 'inactive', '', $cid ),
];
$all = 0;
foreach ( $countdata as $data ) {
$all += intval( $data );
}
// adding automation title and url
$final_contacts = [];
$total = isset( $contacts['total'] ) ? $contacts['total'] : 0;
foreach ( $contacts['contacts'] as $contact ) {
$automation_obj = BWFAN_Automation_V2::get_instance( $contact['aid'] );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
continue;
}
if ( ! empty( $contact['error_msg'] ) && is_array( $contact['error_msg'] ) ) {
$contact['error_msg'] = $this->get_error_message( $contact['error_msg'] );
}
$title = isset( $automation_obj->automation_data['title'] ) && ! empty( $automation_obj->automation_data['title'] ) ? $automation_obj->automation_data['title'] : '';
$event_slug = isset( $automation_obj->automation_data['event'] ) && ! empty( $automation_obj->automation_data['event'] ) ? $automation_obj->automation_data['event'] : '';
$event_obj = BWFAN_Core()->sources->get_event( $event_slug );
$event = ! empty( $event_obj ) ? $event_obj->get_name() : '';
$final_contacts[] = array_merge( $contact, array(
'title' => $title,
'event' => $event
) );
BWFAN_Automation_V2::unset_instance(); //unsetting the current instance
}
$countdata['all'] = $all;
$contacts_data = [
'total' => $total,
'data' => $final_contacts,
'count_data' => $countdata
];
return $this->success_response( $contacts_data, $message );
}
public function get_error_message( $res = '' ) {
if ( is_array( $res ) ) {
$res = $this->get_error_message( array_values( $res )[0] );
}
return $res;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_All_Automation_Contacts' );

View File

@@ -0,0 +1,62 @@
<?php
class BWFAN_API_Get_Automation_Contacts_Journey extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/contacts/journey';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id' );
$search = isset( $this->args['search'] ) ? $this->args['search'] : '';
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? absint( $this->get_sanitized_arg( 'offset', 'text_field' ) ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
/** If step id is 0 , event data to be returned */
if ( empty( $automation_id ) ) {
return $this->error_response( __( 'Invalid / Empty automation ID provided', 'wp-marketing-automations' ), null, 400 );
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
$contacts = BWFAN_Common::get_automation_contacts_journey( $automation_id, $search );
if ( empty( $contacts ) || ! is_array( $contacts ) ) {
return $this->error_response( [], __( 'No data found', 'wp-marketing-automations' ) );
}
$this->response_code = 200;
return $this->success_response( $contacts, __( 'Successfully fetched contacts', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_Contacts_Journey' );

View File

@@ -0,0 +1,478 @@
<?php
class BWFAN_API_Get_Automation_Contact_Trail extends BWFAN_API_Base {
public static $ins;
public $trail = null;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/trail/';
$this->request_args = array(
'tid' => array(
'description' => __( 'Trail ID to retrieve', 'wp-marketing-automations' ),
'type' => 'string',
),
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/** Customer journey Api call */
public function process_api_call() {
$tid = $this->get_sanitized_arg( 'tid' );
$aid = empty( $this->get_sanitized_arg( 'automation_id' ) ) ? 0 : intval( $this->get_sanitized_arg( 'automation_id' ) );
/** If step id is 0 , event data to be returned */
if ( empty( $tid ) ) {
return $this->error_response( __( 'Invalid/ Empty trail ID provided', 'wp-marketing-automations' ), null, 400 );
}
if ( empty( $aid ) ) {
return $this->error_response( __( 'Invalid/ Empty Automation ID provided', 'wp-marketing-automations' ), null, 400 );
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $aid );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
/** Get trail data from DB */
$this->trail = BWFAN_Model_Automation_Contact_Trail::get_trail( $tid );
//$this->trail = $this->maybe_alter_trail( $this->trail );
/** Check for empty data */
if ( empty( $this->trail ) || ! is_array( $this->trail ) ) {
return $this->error_response( [], 'No data found' );
}
/** Get event slug, name and source*/
$event_slug = BWFAN_Model_Automations::get_event_name( $aid );
$event_obj = BWFAN_Core()->sources->get_event( $event_slug );
$event_name = ! empty( $event_obj ) ? $event_obj->get_name() : '-';
$source = $event_obj->get_source();
$integration = BWFAN_Core()->sources->get_source( $source );
$source_name = ! empty( $integration ) ? $integration->get_name() : '-';
/** Set start array */
$trail_data = [
[
'id' => 'start',
'type' => 'start',
'data' => [
'event' => $event_slug,
'nice_name' => $event_name,
'source' => $source_name,
'date' => ''
],
'hidden' => false,
'position' => [ 'x' => 250, 'y' => 25 ],
]
];
$last_sid = 'start';
foreach ( $this->trail as $index => $data ) {
/** Set time in start node if not set */
if ( isset( $trail_data[0]['data']['date'] ) && empty( $trail_data[0]['data']['date'] ) ) {
$trail_data[0]['data']['date'] = date( 'Y-m-d H:i:s', $data['c_time'] );
}
/** Get data trail data by step type */
$tdata = $this->get_trail_data_by_step_type( $data, $event_slug, $index );
/** Merge with trail main array */
$trail_data = array_merge( $trail_data, $tdata );
/** Set new link data */
$trail_data[] = [
'id' => $last_sid . '-' . $data['sid'] . '-' . $data['ID'],
'source' => $last_sid,
'target' => $data['sid'] . '-' . $data['ID'],
'sourceHandle' => '',
'animated' => false,
];
/** Check if conditional or split node and set last step connecting */
switch ( $data['type'] ) {
case BWFAN_Automation_Controller::$TYPE_CONDITIONAL:
$last_sid = 1 === count( $tdata ) ? $data['sid'] : ( $data['sid'] . '-condi-' . $data['ID'] );
break;
case BWFAN_Automation_Controller::$TYPE_SPLIT:
$last_sid = $data['sid'] . '-split-' . $data['ID'];
break;
default:
$last_sid = $data['sid'] . '-' . $data['ID'];
break;
}
}
/** Get automation contact complete data */
$complete_data = BWFAN_Model_Automation_Complete_Contact::get_data_by_trail( $tid );
$end_reason = $complete_data['end_reason'] ?? [];
$end_reason = ! empty( $end_reason ) ? BWFAN_Common::get_end_reason_message( $end_reason ) : '';
/** Set end node */
$trail_data[] = [
'id' => 'end',
'type' => 'end',
'data' => [
'reason' => $end_reason,
'label' => __( 'End Automation', 'wp-marketing-automations' ),
],
'hidden' => false,
'position' => [ 'x' => 250, 'y' => 1100 ],
];
/** Set last node link with last step id */
$trail_data[] = [
'id' => $last_sid . '-end',
'source' => $last_sid,
'target' => 'end',
'sourceHandle' => '',
'animated' => empty( $complete_data ),
];
$this->response_code = 200;
return $this->success_response( $trail_data, __( 'Automation contact data found', 'wp-marketing-automations' ) );
}
/**
* Get trail message if multilevel array
*
* @param $res
*
* @return mixed|string
*/
public function get_trail_message( $res = '' ) {
if ( is_array( $res ) ) {
$res = $this->get_trail_message( array_values( $res )[0] );
}
return $res;
}
/**
* Maybe alter trail if same steps found multiple times
*
* @param $trail
*
* @return array
*/
public function maybe_alter_trail( $trail ) {
if ( empty( $trail ) ) {
return [];
}
$new_trail = [];
$step_key = [];
foreach ( $trail as $key => $single ) {
if ( isset( $step_key[ $single['sid'] ] ) ) {
unset( $new_trail[ $step_key[ $single['sid'] ] ] );
}
$step_key[ $single['sid'] ] = $key;
$new_trail[ $key ] = $single;
}
sort( $new_trail );
return $new_trail;
}
/**
* Format step
*
* @param $data
* @param $event
*
* @return array|array[]
*/
public function get_trail_data_by_step_type( $data, $event, $current_step_index ) {
$status = intval( $data['status'] );
$response = isset( $data['data'] ) ? json_decode( $data['data'], true ) : '';
$res_msg = isset( $response['msg'] ) ? $this->get_trail_message( $response['msg'] ) : '';
$time = $this->get_step_execution_time( $data, $current_step_index );
$trail_data = [];
/** Set step node and data by type */
switch ( absint( $data['type'] ) ) {
case BWFAN_Automation_Controller::$TYPE_WAIT :
$type_data = isset( $data['step_data'] ) ? json_decode( $data['step_data'], true ) : [];
$delay_data = isset( $type_data['sidebarData']['data'] ) ? $type_data['sidebarData']['data'] : [];
$res_msg = ( empty( $res_msg ) && isset( $response['error_msg'] ) ) ? $this->get_trail_message( $response['error_msg'] ) : $res_msg;
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'wait',
'data' => [
'value' => [
'type' => 1,
'delayData' => $delay_data,
],
'date' => date( 'Y-m-d H:i:s', $time ),
'status' => $status,
'msg' => $res_msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
]
];
break;
case BWFAN_Automation_Controller::$TYPE_ACTION :
$action = isset( $data['action'] ) ? json_decode( $data['action'], true ) : [];
$action_slug = isset( $action['action'] ) ? $action['action'] : '';
$res_msg = ( empty( $res_msg ) && isset( $response['error_msg'] ) ) ? $this->get_trail_message( $response['error_msg'] ) : $res_msg;
/** check for action slug */
if ( empty( $action_slug ) ) {
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'action',
'data' => [
'selected' => '',
'nice_name' => __( 'Not Configured', 'wp-marketing-automations' ),
'integration' => '',
'date' => date( 'Y-m-d H:i:s', $time ),
'status' => $status,
'msg' => $res_msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
]
];
break;
}
/** Action object */
$action_obj = ! empty( $action_slug ) ? BWFAN_Core()->integration->get_action( $action_slug ) : '';
/** check for action object */
if ( ! $action_obj instanceof BWFAN_Action ) {
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'action',
'data' => [
'selected' => '',
'nice_name' => __( 'Action Not Found', 'wp-marketing-automations' ),
'integration' => '',
'date' => date( 'Y-m-d H:i:s', $time ),
'status' => $status,
'msg' => $res_msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
]
];
break;
}
$integration_slug = $action_obj->get_integration_type();
$integration = BWFAN_Core()->integration->get_integration( $integration_slug );
/** Get integration name */
$integration_name = ! empty( $integration ) ? $integration->get_name() : '-';
$action_name = ! empty( $action_obj ) ? $action_obj->get_name() : '-';
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'action',
'data' => [
'selected' => $action_slug,
'nice_name' => $action_name,
'integration' => $integration_name,
'date' => date( 'Y-m-d H:i:s', $time ),
'status' => $status,
'msg' => $res_msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
]
];
break;
case BWFAN_Automation_Controller::$TYPE_GOAL :
$action = isset( $data['action'] ) ? json_decode( $data['action'], true ) : [];
$benchmark = isset( $action['benchmark'] ) ? $action['benchmark'] : '';
/** Get event object */
$event_obj = BWFAN_Core()->sources->get_event( $benchmark );
$event_name = ! empty( $event_obj ) ? $event_obj->get_name() : '';
$source_slug = ! empty( $event_obj ) ? $event_obj->get_source() : '';
$res_msg = ( empty( $res_msg ) && isset( $response['error_msg'] ) ) ? $this->get_trail_message( $response['error_msg'] ) : $res_msg;
$source = ! empty( $source_slug ) ? BWFAN_Core()->sources->get_source( $source_slug ) : '';
/** Get source name */
$source_name = ! empty( $source_slug ) ? $source->get_name() : '-';
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'benchmark',
'data' => [
'benchmark' => $benchmark,
'nice_name' => $event_name,
'source' => $source_name,
'date' => date( 'Y-m-d H:i:s', $time ),
'status' => $status,
'msg' => $res_msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
]
];
break;
case BWFAN_Automation_Controller::$TYPE_CONDITIONAL :
$type_data = isset( $data['step_data'] ) ? json_decode( $data['step_data'], true ) : [];
$sidebar_data = isset( $type_data['sidebarData'] ) ? $type_data['sidebarData'] : [];
$conditional_result = isset( $data['data'] ) ? json_decode( $data['data'], true ) : [];
$direction = isset( $conditional_result['msg'] ) && 1 === absint( $conditional_result['msg'] ) ? 'yes' : 'no';
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'conditional',
'data' => [
'sidebarValues' => $sidebar_data,
'event' => $event,
'date' => date( 'Y-m-d H:i:s', $time ),
'status' => $status,
'msg' => $res_msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
]
];
if ( 3 !== $status ) {
$trail_data[] = [
'id' => $data['sid'] . '-condi-' . $data['ID'],
'type' => 'yesNoNode',
'data' => [
'direction' => $direction,
'parent' => $data['sid'],
'date' => date( 'Y-m-d H:i:s', $time ),
]
];
$trail_data[] = [
'id' => $data['sid'] . '-condi-edge-' . $data['ID'],
'source' => $data['sid'] . '-' . $data['ID'],
'target' => $data['sid'] . '-condi-' . $data['ID'],
'sourceHandle' => '',
'animated' => false,
];
}
break;
case BWFAN_Automation_Controller::$TYPE_EXIT :
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'exit',
'data' => [
'date' => date( 'Y-m-d H:i:s', $time ),
'status' => $status,
'msg' => $res_msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
]
];
break;
case BWFAN_Automation_Controller::$TYPE_JUMP :
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'jump',
'data' => [
'date' => date( 'Y-m-d H:i:s', $time ),
'status' => $status,
'msg' => $res_msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
]
];
break;
case BWFAN_Automation_Controller::$TYPE_SPLIT :
$type_data = isset( $data['step_data'] ) ? json_decode( $data['step_data'], true ) : [];
$sidebar_data = isset( $type_data['sidebarData'] ) ? $type_data['sidebarData'] : [];
$conditional_result = isset( $data['data'] ) ? json_decode( $data['data'], true ) : [];
$msg = isset( $conditional_result['msg'] ) ? $conditional_result['msg'] : '';
$path = isset( $conditional_result['path'] ) ? $conditional_result['path'] : 0;
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'split',
'data' => [
'sidebarValues' => $sidebar_data,
'event' => $event,
'date' => date( 'Y-m-d H:i:s', $data['c_time'] ),
'status' => $status,
'msg' => $msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
],
[
'id' => $data['sid'] . '-split-' . $data['ID'],
'type' => 'splitpath',
'data' => [
'path' => $path,
'parent' => $data['sid'],
'date' => date( 'Y-m-d H:i:s', $data['c_time'] ),
]
],
[
'id' => $data['sid'] . '-split-edge',
'source' => $data['sid'] . '-' . $data['ID'],
'target' => $data['sid'] . '-split-' . $data['ID'],
'sourceHandle' => '',
'animated' => false,
]
];
break;
default:
$trail_data = [
[
'id' => $data['sid'] . '-' . $data['ID'],
'type' => 'unknown',
'data' => [
'date' => date( 'Y-m-d H:i:s', $time ),
'status' => $status,
'msg' => $res_msg,
'step_status' => isset( $data['step_status'] ) ? $data['step_status'] : 0
],
]
];
}
return $trail_data;
}
/**
* Get step execution or next run time
*
* @param $data
* @param $current_step_index
*
* @return mixed|string
*/
private function get_step_execution_time( $data, $current_step_index ) {
$time = $data['c_time'];
/** If not goal or delay step */
if ( ! in_array( intval( $data['type'] ), [ BWFAN_Automation_Controller::$TYPE_WAIT, BWFAN_Automation_Controller::$TYPE_GOAL ], true ) ) {
return $time;
}
if ( BWFAN_Automation_Controller::$TYPE_WAIT === intval( $data['type'] ) && 2 === intval( $data['status'] ) ) {
return BWFAN_Model_Automation_Contact::get_next_run_time_by_trail( $data['tid'] );
}
if ( 1 === intval( $data['status'] ) ) {
/** If current step has run then get c_time of next step */
$next_step_index = $current_step_index + 1;
/** There is next node in trail, pick their time */
if ( isset( $this->trail[ $next_step_index ]['c_time'] ) ) {
return $this->trail[ $next_step_index ]['c_time'];
}
/** There is no next node and automation would have been completed. Checking completion time from automation contact table */
$complete_data = BWFAN_Model_Automation_Complete_Contact::get_row_by_trail_id( $data['tid'] );
return isset( $complete_data['c_date'] ) ? strtotime( $complete_data['c_date'] ) : $time;
}
return $time;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_Contact_Trail' );

View File

@@ -0,0 +1,110 @@
<?php
class BWFAN_API_Get_Automation_Contacts extends BWFAN_API_Base {
public static $ins;
private $aid = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/contacts/';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation id to retrieve contacts', 'wp-marketing-automations' ),
'type' => 'string',
),
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/** Customer journey Api call */
public function process_api_call() {
$aid = empty( $this->get_sanitized_arg( 'automation_id' ) ) ? 0 : $this->get_sanitized_arg( 'automation_id' );
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? absint( $this->get_sanitized_arg( 'offset', 'text_field' ) ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
$type = ! empty( $this->get_sanitized_arg( 'type', 'text_field' ) ) ? $this->get_sanitized_arg( 'type', 'text_field' ) : 'active';
$search = ! empty( $this->get_sanitized_arg( 'search', 'text_field' ) ) ? $this->get_sanitized_arg( 'search', 'text_field' ) : '';
if ( empty( $aid ) ) {
return $this->error_response( __( 'Invalid / Empty Automation ID provided', 'wp-marketing-automations' ), null, 400 );
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $aid );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
$this->aid = $aid;
$contacts = BWFAN_Common::get_automation_contacts( $aid, '', $search, $limit, $offset, $type );
$message = __( 'Successfully fetched trails', 'wp-marketing-automations' );
if ( ! isset( $contacts['contacts'] ) || empty( $contacts['contacts'] ) || ! is_array( $contacts['contacts'] ) ) {
$message = __( 'No data found', 'wp-marketing-automations' );
}
$completed_count = BWFAN_Model_Automation_Complete_Contact::get_complete_count( $aid );
/** active contacts = active + wait + retry */
$active_count = BWFAN_Model_Automation_Contact::get_active_count( $aid, 'active' );
$countdata = [
'active' => $active_count,
'paused' => BWFAN_Model_Automation_Contact::get_active_count( $aid, 3 ),
'failed' => BWFAN_Model_Automation_Contact::get_active_count( $aid, 2 ),
'completed' => $completed_count,
'inactive' => BWFAN_Model_Automation_Contact::get_active_count( $aid, 'inactive' ),
];
$all = 0;
foreach ( $countdata as $data ) {
$all += intval( $data );
}
$countdata['all'] = $all;
$final_contacts = [];
if ( 'failed' === $type ) {
foreach ( $contacts['contacts'] as $contact ) {
if ( ! empty( $contact['error_msg'] ) && is_array( $contact['error_msg'] ) ) {
$contact['error_msg'] = $this->get_error_message( $contact['error_msg'] );
}
$final_contacts[] = $contact;
}
} else {
$final_contacts = $contacts['contacts'];
}
$contacts_data = [
'total' => isset( $contacts['total'] ) ? $contacts['total'] : 0,
'data' => $final_contacts,
'automation_data' => $automation_obj->automation_data,
'count_data' => $countdata
];
return $this->success_response( $contacts_data, $message );
}
public function get_error_message( $res = '' ) {
if ( is_array( $res ) ) {
$res = $this->get_error_message( array_values( $res )[0] );
}
return $res;
}
/**
* @return array
*/
public function get_result_count_data() {
return [
'failed' => BWFAN_Model_Automation_Contact::get_active_count( $this->aid, 2 )
];
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_Contacts' );

View File

@@ -0,0 +1,77 @@
<?php
/**
* Get Automation Merger Points
*
* @package WP Marketing Automations
*/
class BWFAN_API_Get_Automation_Merger_Points extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/merger-points';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$arg_data = $this->args;
/** Get automation */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
/** Step data */
if ( ! empty( $arg_data['steps'] ) ) {
$steps = $arg_data['steps'];
}
/** Link data */
if ( ! empty( $arg_data['links'] ) ) {
$links = $arg_data['links'];
}
/** Node count */
if ( isset( $arg_data['count'] ) && intval( $arg_data['count'] ) > 0 ) {
$count = intval( $arg_data['count'] );
}
/** Update automation data */
if ( ! empty( $steps ) && ! empty( $links ) ) {
$automation_obj->update_automation_meta_data( [], $steps, $links, $count );
}
$automation_obj->fetch_automation_metadata( false );
$automation_meta = $automation_obj->get_automation_meta_data();
if ( ! empty( $automation_meta['merger_points'] ) ) {
$this->response_code = 200;
return $this->success_response( [ 'merger_points' => $automation_meta['merger_points'] ], __( 'Merger Points Updated', 'wp-marketing-automations' ) );
}
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_Merger_Points' );

View File

@@ -0,0 +1,105 @@
<?php
class BWFAN_API_Get_Automation_Step_Contacts extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/step/(?P<step_id>[\\d]+)/contacts';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
'step_id' => array(
'description' => __( 'Step ID data to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id' );
$step_id = $this->get_sanitized_arg( 'step_id' );
$type = $this->get_sanitized_arg( 'type' );
$path = $this->get_sanitized_arg( 'path' );
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? absint( $this->get_sanitized_arg( 'offset', 'text_field' ) ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
/** If step id is 0 , event data to be returned */
if ( empty( $automation_id ) ) {
return $this->error_response( __( 'Invalid / Empty automation ID provided', 'wp-marketing-automations' ), null, 400 );
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
$step = BWFAN_Model_Automation_Step::get( $step_id );
$path = isset( $step['type'] ) && 7 === intval( $step['type'] ) ? $path : '';
$data = BWFAN_Common::get_completed_contacts( $automation_id, $step_id, $type, $offset, $limit, $path );
if ( ! isset( $data['contacts'] ) || ! is_array( $data['contacts'] ) ) {
return $this->error_response( [], __( 'No data found', 'wp-marketing-automations' ) );
}
$contacts = array_map( function ( $contact ) {
$trail = isset( $contact['trail'] ) ? $contact['trail'] : '';
$time = isset( $contact['c_date'] ) ? strtotime( $contact['c_date'] ) : '';
$prepared_data = [];
$prepared_data['id'] = $contact['cid'];
$prepared_data['name'] = ! empty( $contact['f_name'] ) || ! empty( $contact['l_name'] ) ? $contact['f_name'] . ' ' . $contact['l_name'] : '';
$prepared_data['phone'] = isset( $contact['contact_no'] ) ? $contact['contact_no'] : '';
$prepared_data['email'] = isset( $contact['email'] ) ? $contact['email'] : '';
$prepared_data['trail'] = isset( $contact['tid'] ) ? $contact['tid'] : $trail;
$prepared_data['time'] = isset( $contact['c_time'] ) ? $contact['c_time'] : $time;
/** Set path if path is available */
if ( isset( $contact['data'] ) && ! empty( $contact['data'] ) ) {
$trail_data = json_decode( $contact['data'], true );
if ( isset( $trail_data['path'] ) ) {
$prepared_data['path'] = $trail_data['path'];
}
}
return $prepared_data;
}, $data['contacts'] );
$total_contacts = isset( $data['total'] ) ? intval( $data['total'] ) : 0;
$path_total = 0;
// /** Get split step's contact count */
// if ( intval( $step_id ) > 0 ) {
// $path_total = $total_contacts;
// $total_contacts = BWFAN_Model_Automation_Contact_Trail::get_step_count( $step_id );
// }
$automation = [
'total' => $total_contacts,
'path_total' => $path_total,
'data' => $contacts
];
$this->response_code = 200;
return $this->success_response( $automation, ! empty( $automation_data['message'] ) ? $automation_data['message'] : __( 'Automation contact found', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_Step_Contacts' );

View File

@@ -0,0 +1,53 @@
<?php
class BWFAN_API_Get_Automation_To_Add_Contact_Manually extends BWFAN_API_Base {
public static $ins;
public $total_count = 0;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/allowed-automations';
}
public function process_api_call() {
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? $this->get_sanitized_arg( 'offset', 'text_field' ) : 0;
$search = ! empty( $this->get_sanitized_arg( 'search', 'text_field' ) ) ? $this->get_sanitized_arg( 'search', 'text_field' ) : '';
$status = ! empty( $this->get_sanitized_arg( 'status', 'text_field' ) ) ? $this->get_sanitized_arg( 'status', 'text_field' ) : 1;
$version = ! empty( $this->get_sanitized_arg( 'version', 'text_field' ) ) ? $this->get_sanitized_arg( 'version', 'text_field' ) : 2;
$events = BWFAN_Core()->sources->get_events_to_add_contact_manually();
if ( empty( $events ) ) {
return $this->success_response( [], __( 'No allowed events found', 'wp-marketing-automations' ) );
}
$placeholder = array_fill( 0, count( $events ), '%s' );
$placeholder = implode( ", ", $placeholder );
$args = $events;
$args = array_merge( $args, [ $status, $version, '%' . $search . '%', $offset, $limit ] );
global $wpdb;
$automations = $wpdb->get_results( $wpdb->prepare( "SELECT `ID` as `key`,`title` as `value` FROM `{$wpdb->prefix}bwfan_automations` WHERE `event` IN($placeholder) AND `status`=%d AND `v`=%d AND `title` LIKE %s LIMIT %d, %d", $args ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $automations ) ) {
return $this->success_response( [], __( 'No automations found in which contact can be added manually', 'wp-marketing-automations' ) );
}
$this->response_code = 200;
return $this->success_response( $automations, __( 'Automations found', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_To_Add_Contact_Manually' );

View File

@@ -0,0 +1,69 @@
<?php
class BWFAN_API_Get_Automation extends BWFAN_API_Base {
public static $ins;
private $aid = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id' );
if ( empty( $automation_id ) ) {
return $this->error_response( __( 'Invalid / Empty automation ID provided', 'wp-marketing-automations' ), null, 400 );
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
$this->aid = $automation_id;
/** Fetch Automation data */
$automation_data = $automation_obj->get_automation_API_data();
if ( ! $automation_data['status'] ) {
return $this->error_response( ! empty( $automation_data['message'] ) ? $automation_data['message'] : __( 'Automation not found with provided ID', 'wp-marketing-automations' ), null, 400 );
} else {
$automation = $automation_data['data'];
}
$this->response_code = 200;
return $this->success_response( $automation, ! empty( $automation_data['message'] ) ? $automation_data['message'] : __( 'Successfully fetched automation', 'wp-marketing-automations' ) );
}
/**
* @return array
*/
public function get_result_count_data() {
return [
'failed' => BWFAN_Model_Automation_Contact::get_active_count( $this->aid, 2 )
];
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation' );

View File

@@ -0,0 +1,311 @@
<?php
class BWFAN_API_Get_Node_Analytics extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/analytics/(?P<step_id>[\\d]+)';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID', 'wp-marketing-automations' ),
'type' => 'integer',
),
'step_id' => array(
'description' => __( 'Step ID ', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id' );
$step_id = $this->get_sanitized_arg( 'step_id' );
$mode = $this->get_sanitized_arg( 'mode' );
$mode = ! empty( $mode ) ? $mode : 'email';
if ( empty( $automation_id ) || empty( $step_id ) ) {
return $this->error_response( __( 'Invalid / Empty automation ID provided', 'wp-marketing-automations' ), null, 400 );
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
$data = [
'status' => false,
'data' => [],
];
switch ( $mode ) {
case 'email':
case 'sms':
$data = $this->get_step_mail_sms_analytics( $automation_id, $step_id, $mode );
break;
case 'split':
$data = $this->get_split_step_analytics( $automation_obj, $automation_id, $step_id );
break;
default:
$data = apply_filters( 'bwfan_automation_node_analytics', $data, $automation_id, $step_id, $mode );
}
if ( ! $data['status'] ) {
return $this->error_response( ! empty( $automation_data['message'] ) ? $automation_data['message'] : __( 'Automation step analytics not found.', 'wp-marketing-automations' ), null, 400 );
}
$this->response_code = 200;
return $this->success_response( $data['data'], ! empty( $automation_data['message'] ) ? $automation_data['message'] : __( 'Step analytics fetched successfully.', 'wp-marketing-automations' ) );
}
/**
* Returns email/sms analytics
*
* @param $automation_id
* @param $step_id
* @param $mode
*
* @return array
*/
public function get_step_mail_sms_analytics( $automation_id, $step_id, $mode ) {
if ( class_exists( 'BWFAN_Model_Engagement_Tracking' ) || method_exists( 'BWFAN_Model_Engagement_Tracking', 'get_automation_step_analytics' ) ) {
$data = BWFAN_Model_Engagement_Tracking::get_automation_step_analytics( $automation_id, $step_id );
}
$open_rate = isset( $data['open_rate'] ) ? number_format( $data['open_rate'], 1 ) : 0;
$click_rate = isset( $data['click_rate'] ) ? number_format( $data['click_rate'], 1 ) : 0;
$revenue = isset( $data['revenue'] ) ? floatval( $data['revenue'] ) : 0;
$unsubscribes = isset( $data['unsubscribers'] ) ? absint( $data['unsubscribers'] ) : 0;
$conversions = isset( $data['conversions'] ) ? absint( $data['conversions'] ) : 0;
$sent = isset( $data['sent'] ) ? absint( $data['sent'] ) : 0;
$open_count = isset( $data['open_count'] ) ? absint( $data['open_count'] ) : 0;
$click_count = isset( $data['click_count'] ) ? absint( $data['click_count'] ) : 0;
$contacts_count = isset( $data['contacts_count'] ) ? absint( $data['contacts_count'] ) : 1;
$rev_per_person = empty( $contacts_count ) || empty( $revenue ) ? 0 : number_format( $revenue / $contacts_count, 1 );
$unsubscribe_rate = empty( $contacts_count ) || empty( $unsubscribes ) ? 0 : ( $unsubscribes / $contacts_count ) * 100;
/** Tile for sms */
if ( 'sms' === $mode ) {
$tiles = [
[
'label' => __( 'Sent', 'wp-marketing-automations' ),
'value' => $sent,
],
[
'label' => __( 'Click Rate', 'wp-marketing-automations' ),
'value' => $click_rate . '% (' . $click_count . ')',
]
];
} else {
/** Get click rate from total opens */
$click_to_open_rate = ( empty( $click_count ) || empty( $open_count ) ) ? 0 : number_format( ( $click_count / $open_count ) * 100, 1 );
$tiles = [
[
'label' => __( 'Sent', 'wp-marketing-automations' ),
'value' => $sent,
],
[
'label' => __( 'Open Rate', 'wp-marketing-automations' ),
'value' => $open_rate . '% (' . $open_count . ')',
],
[
'label' => __( 'Click Rate', 'wp-marketing-automations' ),
'value' => $click_rate . '% (' . $click_count . ')',
],
[
'label' => __( 'Click to Open Rate', 'wp-marketing-automations' ),
'value' => $click_to_open_rate . '%',
]
];
}
if ( bwfan_is_woocommerce_active() ) {
$currency_symbol = get_woocommerce_currency_symbol();
$revenue = html_entity_decode( $currency_symbol . $revenue );
$rev_per_person = html_entity_decode( $currency_symbol . $rev_per_person );
$revenue_tiles = [
[
'label' => __( 'Revenue', 'wp-marketing-automations' ),
'value' => $revenue . ' (' . $conversions . ')',
],
[
'label' => __( 'Revenue/Contact', 'wp-marketing-automations' ),
'value' => $rev_per_person,
]
];
$tiles = array_merge( $tiles, $revenue_tiles );
}
$tiles[] = [
'label' => __( 'Unsubscribe Rate', 'wp-marketing-automations' ),
'value' => number_format( $unsubscribe_rate, 2 ) . '% (' . $unsubscribes . ')',
];
return [
'status' => true,
'data' => [
'analytics' => [],
'tile' => $tiles,
]
];
}
public function get_split_step_analytics( $automation_obj, $automation_id, $step_id ) {
$automation_meta = $automation_obj->get_automation_meta_data();
$split_steps = isset( $automation_meta['split_steps'] ) ? $automation_meta['split_steps'] : [];
$data = BWFAN_Model_Automation_Step::get_step_data( $step_id );
$started_at = $data['created_at'];
$paths = $split_steps[ $step_id ];
$path_stats = [ 'started_at' => $started_at ];
foreach ( $paths as $path => $step_ids ) {
$path_num = str_replace( 'p-', '', $path );
$stats = $this->get_path_stats( $automation_id, $step_ids );
$contact_count = BWFAN_Model_Automation_Contact_Trail::get_path_contact_count( $step_id, $path_num );
$stats[0] = [
'l' => 'Contacts',
'v' => ! empty( $contact_count ) ? intval( $contact_count ) : '-',
];
$path_stats['paths'][] = [
'path' => $path_num,
'stats' => $stats
];
}
return [
'status' => true,
'data' => [
'data' => $path_stats
]
];
}
/**
* @param $split_steps
* @param $current_step
*
* @return mixed|string
*/
private function get_split_step_creation_itme( $split_steps, $current_step ) {
if ( empty( $split_steps ) ) {
return '';
}
$split_step_id = 0;
foreach ( $split_steps as $split_id => $paths ) {
foreach ( $paths as $step_ids ) {
if ( ! in_array( intval( $current_step ), array_map( 'intval', $step_ids ), true ) ) {
continue;
}
$split_step_id = $split_id;
break;
}
if ( intval( $split_step_id ) > 0 ) {
break;
}
}
if ( 0 === intval( $split_step_id ) ) {
return '';
}
$split_data = BWFAN_Model_Automation_Step::get( $split_id );
return isset( $split_data['created_at'] ) ? $split_data['created_at'] : '';;
}
/**
* Get path's stats
*
* @param $automation_id
* @param $step_ids
*
* @return array|WP_Error
*/
public function get_path_stats( $automation_id, $step_ids, $after_date = '' ) {
$data = BWFAN_Model_Engagement_Tracking::get_automation_step_analytics( $automation_id, $step_ids, $after_date );
$open_rate = isset( $data['open_rate'] ) ? number_format( $data['open_rate'], 1 ) : 0;
$click_rate = isset( $data['click_rate'] ) ? number_format( $data['click_rate'], 1 ) : 0;
$revenue = isset( $data['revenue'] ) ? floatval( $data['revenue'] ) : 0;
$unsubscribes = isset( $data['unsubscribers'] ) ? absint( $data['unsubscribers'] ) : 0;
$conversions = isset( $data['conversions'] ) ? absint( $data['conversions'] ) : 0;
$sent = isset( $data['sent'] ) ? absint( $data['sent'] ) : 0;
$open_count = isset( $data['open_count'] ) ? absint( $data['open_count'] ) : 0;
$click_count = isset( $data['click_count'] ) ? absint( $data['click_count'] ) : 0;
$contacts_count = isset( $data['contacts_count'] ) ? absint( $data['contacts_count'] ) : 1;
$rev_per_person = empty( $contacts_count ) || empty( $revenue ) ? 0 : number_format( $revenue / $contacts_count, 1 );
$unsubscribe_rate = empty( $contacts_count ) || empty( $unsubscribes ) ? 0 : ( $unsubscribes / $contacts_count ) * 100;
/** Get click rate from total opens */
$click_to_open_rate = ( empty( $click_count ) || empty( $open_count ) ) ? 0 : number_format( ( $click_count / $open_count ) * 100, 1 );
$tiles = [
[
'l' => __( 'Contact', 'wp-marketing-automations' ),
'v' => '',
],
[
'l' => __( 'Sent', 'wp-marketing-automations' ),
'v' => empty( $sent ) ? '-' : $sent,
],
[
'l' => __( 'Opened', 'wp-marketing-automations' ),
'v' => empty( $open_count ) ? '-' : $open_count . ' (' . $open_rate . '%)',
],
[
'l' => __( 'Clicked', 'wp-marketing-automations' ),
'v' => empty( $click_count ) ? '-' : $click_count . ' (' . $click_rate . '%)',
],
[
'l' => __( 'Click to Open', 'wp-marketing-automations' ),
'v' => empty( $click_to_open_rate ) ? '-' : $click_to_open_rate . '%',
]
];
if ( bwfan_is_woocommerce_active() ) {
$currency_symbol = get_woocommerce_currency_symbol();
$revenue = empty( $revenue ) ? '' : html_entity_decode( $currency_symbol . $revenue );
$rev_per_person = empty( $rev_per_person ) ? '' : html_entity_decode( $currency_symbol . $rev_per_person );
$revenue_tiles = [
[
'l' => __( 'Rev.', 'wp-marketing-automations' ),
'v' => empty( $conversions ) ? '-' : $revenue . ' (' . $conversions . ')',
],
[
'l' => __( 'Rev./Contact', 'wp-marketing-automations' ),
'v' => empty( $rev_per_person ) ? '-' : $rev_per_person,
]
];
$tiles = array_merge( $tiles, $revenue_tiles );
}
$tiles[] = [
'l' => __( 'Unsubscribed', 'wp-marketing-automations' ),
'v' => empty( $unsubscribes ) ? '-' : $unsubscribes . ' (' . number_format( $unsubscribe_rate, 1 ) . '%)',
];
return $tiles;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Node_Analytics' );

View File

@@ -0,0 +1,37 @@
<?php
class BWFAN_API_Get_Rule_Search_Suggestion extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/rules/(?P<rule>[a-zA-Z0-9_-]+)/suggestions';
}
public function process_api_call() {
$rule = $this->get_sanitized_arg( 'rule' );
if ( empty( $rule ) ) {
return $this->error_response_200( __( 'Invalid or empty rule', 'wp-marketing-automations' ), null, 400 );
}
// removed get_sanitized_arg function because that was merging two or more substrings and making one
// for example : cart abandonment. with get_sanitized_arg, it is becoming cartabandonment, which making issue in search
$search = $this->args['search'] ? $this->args['search'] : '';
$result = BWFAN_Core()->rules->get_rule_search_suggestions( $search, $rule );
return $this->success_response( $result );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Rule_Search_Suggestion' );

View File

@@ -0,0 +1,34 @@
<?php
class BWFAN_API_Get_Rules extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automation/event-rules/(?P<event>[a-zA-Z0-9_]+)';
}
public function process_api_call() {
$event = $this->get_sanitized_arg( 'event' );
if ( empty( $event ) ) {
return $this->error_response_200( __( 'Invalid or empty event', 'wp-marketing-automations' ), null, 400 );
}
$aid = $this->get_sanitized_arg( 'automation_id' );
BWFAN_Core()->rules->load_rules_classes();
$rules = BWFAN_Core()->rules->get_rules( $event, absint( $aid ) );
return $this->success_response( $rules );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Rules' );

View File

@@ -0,0 +1,237 @@
<?php
class BWFAN_API_Get_Single_Automation_Stats extends BWFAN_API_Base {
public static $ins;
private $automation_obj = null;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automations-stats/(?P<automation_id>[\\d]+)/';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation id to stats', 'wp-marketing-automations' ),
'type' => 'string',
),
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/** Customer journey Api call */
public function process_api_call() {
$aid = empty( $this->get_sanitized_arg( 'automation_id' ) ) ? 0 : $this->get_sanitized_arg( 'automation_id' );
if ( empty( $aid ) ) {
return $this->error_response( __( 'Invalid / Empty automation ID provided', 'wp-marketing-automations' ), null, 400 );
}
/** Initiate automation object */
$this->automation_obj = BWFAN_Automation_V2::get_instance( $aid );
/** Check for automation exists */
if ( ! empty( $this->automation_obj->error ) ) {
return $this->error_response( [], $this->automation_obj->error );
}
$data = [
'start' => [
'queued' => 0,
'active' => $this->automation_obj->get_active_count(),
'completed' => $this->automation_obj->get_complete_count(),
]
];
$step_ids = BWFAN_Model_Automation_Step::get_automation_step_ids( $aid );
if ( empty( $step_ids ) ) {
return $this->success_response( $data, __( 'Automation stats found', 'wp-marketing-automations' ) );
}
$step_ids = array_column( $step_ids, 'ID' );
$completed_steps = BWFAN_Model_Automation_Contact_Trail::get_bulk_step_count( $step_ids );
$completed_sids = empty( $completed_steps ) ? [] : array_column( $completed_steps, 'sid' );
$queued_steps = BWFAN_Model_Automation_Contact_Trail::get_bulk_step_count( $step_ids, 2 );
$queued_sids = empty( $queued_steps ) ? [] : array_column( $queued_steps, 'sid' );
$skipped_steps = BWFAN_Model_Automation_Contact_Trail::get_bulk_step_count( $step_ids, 4 );
$skipped_sids = empty( $skipped_steps ) ? [] : array_column( $skipped_steps, 'sid' );
$failed_steps = BWFAN_Model_Automation_Contact_Trail::get_bulk_step_count( $step_ids, 3 );
$failed_sids = empty( $failed_steps ) ? [] : array_column( $failed_steps, 'sid' );
foreach ( $step_ids as $sid ) {
$index = array_search( $sid, $completed_sids );
$completed_count = ( false !== $index && isset( $completed_steps[ $index ]['count'] ) ) ? $completed_steps[ $index ]['count'] : 0;
$index = array_search( $sid, $queued_sids );
$queued_count = ( false !== $index && isset( $queued_steps[ $index ]['count'] ) ) ? $queued_steps[ $index ]['count'] : 0;
$index = array_search( $sid, $skipped_sids );
$skipped_count = ( false !== $index && isset( $skipped_steps[ $index ]['count'] ) ) ? $skipped_steps[ $index ]['count'] : 0;
$index = array_search( $sid, $failed_sids );
$failed_count = ( false !== $index && isset( $failed_steps[ $index ]['count'] ) ) ? $failed_steps[ $index ]['count'] : 0;
$data[ $sid ] = [
'queued' => $queued_count,
'active' => 0,
'completed' => $completed_count,
'skipped' => $skipped_count,
'failed' => $failed_count,
];
}
$meta = $this->automation_obj->get_automation_meta_data();
$split_steps = isset( $meta['split_steps'] ) ? $meta['split_steps'] : [];
$data = $this->get_split_steps_stats( $split_steps, $data );
/** Get path stats */
// $path_stats = $this->get_split_path_stats( $aid, $split_steps );
return $this->success_response( $data, __( 'Automation stats found', 'wp-marketing-automations' ) );
}
public function get_split_steps_stats( $split_steps, $data ) {
/** Get split step's step ids */
$split_step_ids = [];
foreach ( $split_steps as $split_id => $paths ) {
foreach ( $paths as $step_ids ) {
$current_step_ids = isset( $split_step_ids[ $split_id ] ) ? $split_step_ids[ $split_id ] : [];
$split_step_ids[ $split_id ] = array_merge( $current_step_ids, $step_ids );
}
}
$split_steps_data = [];
foreach ( $split_step_ids as $split_id => $step_ids ) {
$step_data = BWFAN_Model_Automation_Step::get( $split_id );
$created_at = isset( $step_data['created_at'] ) ? strtotime( $step_data['created_at'] ) : '';
$split_steps_data[ $split_id ]['completed'] = BWFAN_Model_Automation_Contact_Trail::get_bulk_step_count( $step_ids, 1, $created_at );
$split_steps_data[ $split_id ]['queued'] = BWFAN_Model_Automation_Contact_Trail::get_bulk_step_count( $step_ids, 2, $created_at );
}
foreach ( $split_steps_data as $steps ) {
foreach ( $steps as $key => $step_data ) {
if ( ! is_array( $step_data ) ) {
continue;
}
foreach ( $step_data as $s_data ) {
if ( ! isset( $data[ $s_data['sid'] ] ) ) {
continue;
}
$data[ $s_data['sid'] ][ $key ] = $s_data['count'];
}
}
}
return $data;
}
public function get_split_path_stats( $aid, $split_steps ) {
$path_stats = [];
foreach ( $split_steps as $split_id => $paths ) {
$step_data = BWFAN_Model_Automation_Step::get_step_data_by_id( $split_id );
$after_date = isset( $step_data['created_at'] ) ? $step_data['created_at'] : '';
$split_node_id = $this->automation_obj->get_steps_node_id( $split_id );
foreach ( $paths as $path => $step_ids ) {
$path_num = str_replace( 'p-', '', $path );
$path_name = $split_node_id . '-path-' . $path_num;
$path_stats[ $path_name ] = $this->get_path_stats( $aid, $step_ids, $after_date );
$contact_count = BWFAN_Model_Automation_Contact_Trail::get_path_contact_count( $split_id, $path_num );
$path_stats[ $path_name ][] = [
'l' => 'Contacts',
'v' => intval( $contact_count ),
];
}
}
return $path_stats;
}
/**
* Get path's stats
*
* @param $automation_id
* @param $step_ids
*
* @return array|WP_Error
*/
public function get_path_stats( $automation_id, $step_ids, $after_date ) {
$data = BWFAN_Model_Engagement_Tracking::get_automation_step_analytics( $automation_id, $step_ids, $after_date );
$open_rate = isset( $data['open_rate'] ) ? number_format( $data['open_rate'], 2 ) : 0;
$click_rate = isset( $data['click_rate'] ) ? number_format( $data['click_rate'], 2 ) : 0;
$revenue = isset( $data['revenue'] ) ? floatval( $data['revenue'] ) : 0;
$unsubscribes = isset( $data['unsbuscribers'] ) ? absint( $data['unsbuscribers'] ) : 0;
$conversions = isset( $data['conversions'] ) ? absint( $data['conversions'] ) : 0;
$sent = isset( $data['sent'] ) ? absint( $data['sent'] ) : 0;
$open_count = isset( $data['open_count'] ) ? absint( $data['open_count'] ) : 0;
$click_count = isset( $data['click_count'] ) ? absint( $data['click_count'] ) : 0;
$contacts_count = isset( $data['contacts_count'] ) ? absint( $data['contacts_count'] ) : 1;
$rev_per_person = empty( $contacts_count ) || empty( $revenue ) ? 0 : number_format( $revenue / $contacts_count, 2 );
$unsubscribe_rate = empty( $contacts_count ) || empty( $unsubscribes ) ? 0 : ( $unsubscribes / $contacts_count ) * 100;
/** Get click rate from total opens */
$click_to_open_rate = ( empty( $click_count ) || empty( $open_count ) ) ? 0 : number_format( ( $click_count / $open_count ) * 100, 2 );
$tiles = [
[
'l' => __( 'Sent', 'wp-marketing-automations' ),
'v' => empty( $sent ) ? '-' : $sent,
],
[
'l' => __( 'Open Rate', 'wp-marketing-automations' ),
'v' => empty( $open_count ) ? '-' : $open_rate . '% (' . $open_count . ')',
],
[
'l' => __( 'Click Rate', 'wp-marketing-automations' ),
'v' => empty( $click_count ) ? '-' : $click_rate . '% (' . $click_count . ')',
],
[
'l' => __( 'Click to Open Rate', 'wp-marketing-automations' ),
'v' => empty( $click_to_open_rate ) ? '-' : $click_to_open_rate . '%',
]
];
if ( bwfan_is_woocommerce_active() ) {
$currency_symbol = get_woocommerce_currency_symbol();
$revenue = empty( $revenue ) ? '' : html_entity_decode( $currency_symbol . $revenue );
$rev_per_person = empty( $rev_per_person ) ? '' : html_entity_decode( $currency_symbol . $rev_per_person );
$revenue_tiles = [
[
'l' => __( 'Revenue', 'wp-marketing-automations' ),
'v' => empty( $conversions ) ? '-' : $revenue . ' (' . $conversions . ')',
],
[
'l' => __( 'Revenue/Contact', 'wp-marketing-automations' ),
'v' => empty( $rev_per_person ) ? '-' : $rev_per_person,
]
];
$tiles = array_merge( $tiles, $revenue_tiles );
}
$tiles[] = [
'l' => __( 'Unsubscribe Rate', 'wp-marketing-automations' ),
'v' => empty( $unsubscribes ) ? '-' : number_format( $unsubscribe_rate, 2 ) . '% (' . $unsubscribes . ')',
];
return $tiles;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Single_Automation_Stats' );

View File

@@ -0,0 +1,38 @@
<?php
class BWFAN_API_Remove_Automation_Automation_Meta extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/delete_meta';
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id' );
$meta_key = $this->get_sanitized_arg( 'meta_key' );
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
BWFAN_Model_Automationmeta::delete_automation_meta( $automation_id, $meta_key );
return $this->success_response( [], __( 'Automation meta deleted', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Remove_Automation_Automation_Meta' );

View File

@@ -0,0 +1,240 @@
<?php
class BWFAN_API_Update_Automation_Step extends BWFAN_API_Base {
public static $ins;
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)/step/(?P<step_id>[\\d]+)';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
'step_id' => array(
'description' => __( 'Step ID to update', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$step_id = $this->get_sanitized_arg( 'step_id', 'text_field' );
$arg_data = $this->args;
if ( isset( $arg_data['content'] ) ) {
$content = BWFAN_Common::is_json( $arg_data['content'] ) ? json_decode( $arg_data['content'], true ) : [];
$arg_data = wp_parse_args( $content, $arg_data );
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [ "Step Not Updated" ], $automation_obj->error );
}
// Update step data
$data = $steps = $links = [];
$count = 0;
$optionUpdated = false;
/** Action table data */
if ( isset( $arg_data['data'] ) && ! empty( $arg_data['data'] ) ) {
$data = $arg_data['data'];
}
/** Step data */
if ( isset( $arg_data['steps'] ) && ! empty( $arg_data['steps'] ) ) {
$steps = $arg_data['steps'];
}
/** Link data */
if ( isset( $arg_data['links'] ) && ! empty( $arg_data['links'] ) ) {
$links = $arg_data['links'];
}
/** Node count */
if ( isset( $arg_data['count'] ) && intval( $arg_data['count'] ) > 0 ) {
$count = intval( $arg_data['count'] );
}
/** Update automation data */
if ( ! empty( $steps ) && ! empty( $links ) ) {
$automation_obj->update_automation_meta_data( [], $steps, $links, $count );
}
/** Benchmark Option Updated data */
if ( isset( $arg_data['optionUpdated'] ) && ! empty( $arg_data['optionUpdated'] ) ) {
$optionUpdated = $arg_data['optionUpdated'];
}
$automation_obj->fetch_automation_metadata( false );
$automation_meta = $automation_obj->get_automation_meta_data();
if ( ! empty( $automation_meta['steps'] ) ) {
unset( $automation_meta['steps'] );
}
if ( ! empty( $automation_meta['links'] ) ) {
unset( $automation_meta['links'] );
}
if ( ! empty( $automation_meta['count'] ) ) {
unset( $automation_meta['count'] );
}
if ( isset( $automation_meta['event_meta'] ) ) {
$automation_meta['event_meta'] = BWFAN_Common::fetch_updated_data( $automation_meta['event_meta'] );
}
/** Save mail and sms steps in split steps */
if ( isset( $automation_meta['split_steps'] ) ) {
$this->save_mail_steps( $automation_meta['split_steps'] );
}
$return_val['meta'] = $automation_meta;
if ( empty( $data ) ) {
$this->response_code = 200;
return $this->success_response( $return_val, __( 'Step Updated', 'wp-marketing-automations' ) );
}
$update_status = 1;
/** Update status */
if ( isset( $arg_data['updateStatus'] ) && ! empty( $arg_data['updateStatus'] ) ) {
$update_status = intval( $arg_data['updateStatus'] );
}
$step_data = BWFAN_Model_Automation_Step::get_step_data_by_id( $step_id );
$action = isset( $step_data['action'] ) ? json_decode( $step_data['action'], true ) : [];
$action_name = isset( $action['action'] ) ? $action['action'] : '';
$benchmark_name = isset( $action['benchmark'] ) ? $action['benchmark'] : '';
if ( in_array( $action_name, [ 'crm_add_to_list', 'crm_add_tag' ] ) ) {
/** Create List/tags if not exists */
$terms = [];
if ( class_exists( 'BWFCRM_Term_Type' ) ) {
$term_type = BWFCRM_Term_Type::$TAG;
$terms = isset( $data['data']['sidebarData']['tags'] ) ? $data['data']['sidebarData']['tags'] : [];
if ( empty( $terms ) && 'crm_add_to_list' === $action_name ) {
$term_type = BWFCRM_Term_Type::$LIST;
$terms = isset( $data['data']['sidebarData']['list_id'] ) ? $data['data']['sidebarData']['list_id'] : [];
}
}
$terms_with_mergetags = [];
foreach ( $terms as $index => $term ) {
if ( 0 !== absint( $term['id'] ) || false === strpos( $term['name'], '}}' ) ) {
continue;
}
unset( $terms[ $index ] );
$terms_with_mergetags[] = $term;
}
if ( is_array( $terms ) && ! empty( $terms ) ) {
$terms = BWFAN_Common::get_or_create_terms( $terms, $term_type );
}
$terms = array_merge( $terms, $terms_with_mergetags );
/** Checking action is Added to List and Add Tag */
if ( 'crm_add_to_list' === $action_name && ! empty( $terms ) ) {
$data['data']['sidebarData']['list_id'] = $terms;
} elseif ( 'crm_add_tag' === $action_name && ! empty( $terms ) ) {
$data['data']['sidebarData']['tags'] = $terms;
}
}
/** If no setting in action */
if ( empty( $action_name ) && isset( $data['action'] ) && isset( $data['action']['action'] ) ) {
$action_name = $data['action']['action'];
$action = BWFAN_Core()->integration->get_action( $action_name );
$fields_schema = $action instanceof BWFAN_Action ? $action->get_fields_schema() : null;
if ( ! is_null( $fields_schema ) && empty( $fields_schema ) ) {
$data['data'] = [ 'sidebarData' => [] ];
$update_status = 1;
}
}
/** update step data */
$result = $automation_obj->update_automation_step_data( $step_id, $data, $update_status, $optionUpdated );
if ( $result ) {
if ( ! empty( $action_name ) ) {
$action = ( $action instanceof BWFAN_Action ) ? $action : BWFAN_Core()->integration->get_action( $action_name );
if ( ! empty( $action ) && method_exists( $action, 'get_desc_text' ) && isset( $data['data'] ) && isset( $data['data']['sidebarData'] ) ) {
$return_val['desc_text'] = $action->get_desc_text( $data['data']['sidebarData'] );
}
}
if ( ! empty( $benchmark_name ) ) {
$benchmark = BWFAN_Core()->sources->get_event( $benchmark_name );
if ( ! empty( $action ) && method_exists( $benchmark, 'get_desc_text' ) && isset( $data['data'] ) && isset( $data['data']['sidebarData'] ) ) {
$return_val['desc_text'] = $benchmark->get_desc_text( $data['data']['sidebarData'] );
}
}
$this->response_code = 200;
return $this->success_response( $return_val, __( 'Step Updated', 'wp-marketing-automations' ) );
} else {
return $this->error_response( [ __( "Step Not Updated", 'wp-marketing-automations' ) ], __( 'Unable to update action data', 'wp-marketing-automations' ) );
}
}
public function save_mail_steps( $split_steps ) {
foreach ( $split_steps as $step_id => $paths ) {
/** Get email and sms steps of all paths */
$mail_steps = [];
foreach ( $paths as $path_name => $step_ids ) {
if ( empty( $step_ids ) ) {
$mail_steps[ $path_name ] = [];
continue;
}
$email_steps = BWFAN_Model_Automation_Step::get_messaging_steps( $step_ids );
$sms_steps = BWFAN_Model_Automation_Step::get_messaging_steps( $step_ids, '_sms' );
$mail_steps[ $path_name ] = array_merge( $email_steps, $sms_steps );
}
/** Save mail steps in step */
$this->save_mail_step( $step_id, $mail_steps );
}
}
/**
* Save mail and sms step
*
* @param $step_id
* @param $all_steps
* @param $automation_obj
*
* @return bool|void
*/
public function save_mail_step( $step_id, $mail_steps ) {
$data = BWFAN_Model_Automation_Step::get_step_data( $step_id );
$data = isset( $data['data'] ) ? $data['data'] : [];
$sidebarData = isset( $data['sidebarData'] ) ? $data['sidebarData'] : [];
$sidebarData['mail_steps'] = $mail_steps;
$updated_data = [
'sidebarData' => $sidebarData
];
BWFAN_Model_Automation_Step::update( array(
'data' => wp_json_encode( $updated_data )
), array(
'ID' => $step_id,
) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Update_Automation_Step' );

View File

@@ -0,0 +1,142 @@
<?php
class BWFAN_API_Update_Automation extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automation/(?P<automation_id>[\\d]+)';
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$arg_data = $this->args['data'];
if ( empty( $arg_data ) ) {
return $this->error_response( [], __( 'Automation Data is missing.', 'wp-marketing-automations' ) );
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
$data = $steps = $links = $meta = [];
$count = 0;
$updated = false;
/** Main table data */
if ( isset( $arg_data['data'] ) && ! empty( $arg_data['data'] ) ) {
$data = $arg_data['data'];
}
/** Step data */
if ( isset( $arg_data['steps'] ) && ! empty( $arg_data['steps'] ) ) {
$steps = $arg_data['steps'];
}
/** Link data */
if ( isset( $arg_data['links'] ) && ! empty( $arg_data['links'] ) ) {
$links = $arg_data['links'];
}
/** Node count */
if ( isset( $arg_data['count'] ) && intval( $arg_data['count'] ) > 0 ) {
$count = intval( $arg_data['count'] );
}
/** Update automation meta */
if ( isset( $arg_data['meta'] ) && ! empty( $arg_data['meta'] ) ) {
$meta = $arg_data['meta'];
}
/** Check for unique key */
if ( isset( $arg_data['need_unique_key'] ) && is_bool( $arg_data['need_unique_key'] ) && $arg_data['need_unique_key'] == true ) {
$meta['event_meta'] = [
'bwfan_unique_key' => md5( uniqid( time(), true ) )
];
}
/** Check for unique key */
if ( isset( $arg_data['isWebhook'] ) && is_bool( $arg_data['isWebhook'] ) && $arg_data['isWebhook'] == true && isset( $meta['event_meta'] ) ) {
$automation_meta = $automation_obj->get_automation_meta_data();
$ameta = [];
if ( isset( $automation_meta['event_meta'] ) ) {
$ameta = $automation_meta['event_meta'];
}
$exclude_key = [ 'bwfan_unique_key', 'received_at', 'referer', 'webhook_data' ];
if ( ! empty( $meta['event_meta'] ) ) {
foreach ( $meta['event_meta'] as $key => $value ) {
if ( ! in_array( $key, $exclude_key ) ) {
$ameta[ $key ] = $value;
}
}
}
$meta['event_meta'] = $ameta;
}
/** Check for data */
if ( empty( $data ) && empty( $steps ) && empty( $links ) && $count == 0 && empty( $meta ) ) {
return $this->error_response( [], __( 'Automation Data is missing.', 'wp-marketing-automations' ) );
}
if ( ! empty( $data ) ) {
if ( isset( $data['start'] ) ) {
unset( $data['start'] );
}
/** Update main table data */
$updated = $automation_obj->update_automation_main_table( $data );
}
if ( ! empty( $steps ) || ! empty( $links ) || $count !== 0 || ! empty( $meta ) ) {
/** Update automation data with meta data */
$updated = $automation_obj->update_automation_meta_data( $meta, $steps, $links, $count );
}
if ( $updated ) {
$this->response_code = 200;
$automation_obj->fetch_automation_metadata( false );
$automation_meta = $automation_obj->get_automation_meta_data();
if ( ! empty( $automation_meta['steps'] ) ) {
unset( $automation_meta['steps'] );
}
if ( ! empty( $automation_meta['links'] ) ) {
unset( $automation_meta['links'] );
}
if ( ! empty( $automation_meta['count'] ) ) {
unset( $automation_meta['count'] );
}
if ( isset( $automation_meta['event_meta'] ) ) {
$automation_meta['event_meta'] = BWFAN_Common::fetch_updated_data( $automation_meta['event_meta'] );
}
return $this->success_response( [
'meta' => $automation_meta,
], __( 'Automation Data Updated', 'wp-marketing-automations' ) );
} else {
$this->response_code = 404;
return $this->error_response( [], __( 'Unable to updated data', 'wp-marketing-automations' ) );
}
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Update_Automation' );

View File

@@ -0,0 +1,90 @@
<?php
class BWFAN_API_Get_Automation_Conversions extends BWFAN_API_Base {
public static $ins;
private $aid = 0;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = 'v3/automation/(?P<automation_id>[\\d]+)/conversions/';
$this->pagination->offset = 0;
$this->pagination->limit = 10;
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
'offset' => array(
'description' => __( 'Contacts list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
$this->aid = $automation_id;
$conversions = $this->get_conversions( $automation_id, $this->pagination->offset, $this->pagination->limit );
$conversions['automation_data'] = $automation_obj->automation_data;
if ( isset( $conversions['automation_data']['v'] ) && 1 === absint( $conversions['automation_data']['v'] ) ) {
$meta = BWFAN_Model_Automationmeta::get_automation_meta( $automation_id );
$conversions['automation_data']['title'] = isset( $meta['title'] ) ? $meta['title'] : '';
}
if ( empty( $conversions['total'] ) ) {
$this->response_code = 404;
$response = __( "No orders found", "wp-marketing-automations" );
return $this->success_response( $conversions, $response );
}
$this->total_count = $conversions['total'];
return $this->success_response( $conversions, __( 'Got All Orders', 'wp-marketing-automations' ) );
}
public function get_result_total_count() {
return $this->total_count;
}
/**
* @return array
*/
public function get_result_count_data() {
return [
'failed' => BWFAN_Model_Automation_Contact::get_active_count( $this->aid, 2 )
];
}
public function get_conversions( $automation_id, $offset = 0, $limit = 25 ) {
if ( empty( $automation_id ) ) {
return [ 'conversions' => [], 'total' => 0 ];
}
return BWFAN_Model_Conversions::get_conversions_by_source_type( $automation_id, BWFAN_Email_Conversations::$TYPE_AUTOMATION, $limit, $offset );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_Conversions' );

View File

@@ -0,0 +1,53 @@
<?php
class BWFAN_API_Get_Automation_Recipient_Timeline extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $recipients_timeline = [];
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = 'v3/automation/(?P<automation_id>[\\d]+)/recipients/(?P<contact_id>[\\d]+)/timeline';
$this->pagination->offset = 0;
$this->pagination->limit = 10;
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve engagements', 'wp-marketing-automations' ),
'type' => 'integer',
),
'contact_id' => array(
'description' => __( 'Contact ID to retrieve recipient timeline', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$contact_id = $this->get_sanitized_arg( 'contact_id', 'text_field' );
$mode = ! empty( absint( $this->get_sanitized_arg( 'mode', 'text_field' ) ) ) ? $this->get_sanitized_arg( 'mode', 'text_field' ) : 1;
$recipients_timeline = BWFAN_Model_Engagement_Tracking::get_automation_recipient_timeline( $automation_id, $contact_id, $mode );
if ( empty( $recipients_timeline ) ) {
return $this->success_response( [], __( 'No engagement found', 'wp-marketing-automations' ) );
}
$this->recipients_timeline = $recipients_timeline;
return $this->success_response( $recipients_timeline, __( 'Got All timeline', 'wp-marketing-automations' ) );
}
public function get_result_total_count() {
return count( $this->recipients_timeline );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_Recipient_Timeline' );

View File

@@ -0,0 +1,85 @@
<?php
class BWFAN_API_Get_Automation_Recipients extends BWFAN_API_Base {
public static $ins;
private $aid = 0;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = 'v3/automation/(?P<automation_id>[\\d]+)/recipients/';
$this->pagination->offset = 0;
$this->pagination->limit = 10;
$this->request_args = array(
'automation_id' => array(
'description' => __( 'Automation ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
'offset' => array(
'description' => __( 'Contacts list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
/** Check for automation exists */
if ( ! empty( $automation_obj->error ) ) {
return $this->error_response( [], $automation_obj->error );
}
$this->aid = $automation_id;
$recipients = BWFAN_Model_Engagement_Tracking::get_automation_recipents( $automation_id, $this->pagination->offset, $this->pagination->limit );
$recipients['recipients'] = $recipients['conversations'];
unset( $recipients['conversations'] );
$recipients['automation_data'] = $automation_obj->automation_data;
if ( isset( $recipients['automation_data']['v'] ) && 1 === absint( $recipients['automation_data']['v'] ) ) {
$meta = BWFAN_Model_Automationmeta::get_automation_meta( $automation_id );
$recipients['automation_data']['title'] = isset( $meta['title'] ) ? $meta['title'] : '';
}
if ( empty( $recipients['total'] ) ) {
$this->response_code = 404;
$response = __( "No recipients found", "wp-marketing-automations" );
return $this->success_response( $recipients, $response );
}
$this->total_count = $recipients['total'];
return $this->success_response( $recipients, __( 'Got All Recipients', 'wp-marketing-automations' ) );
}
public function get_result_total_count() {
return $this->total_count;
}
/**
* @return array
*/
public function get_result_count_data() {
return [
'failed' => BWFAN_Model_Automation_Contact::get_active_count( $this->aid, 2 )
];
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_Recipients' );

View File

@@ -0,0 +1,96 @@
<?php
/**
* Autonami Email Preview API class
*/
class BWFAN_API_Campaign_Email_Preview extends BWFAN_API_Base {
public static $ins;
/**
* Return class instance
*/
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Class constructor
*/
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/autonami/email-preview';
}
/**
* Default arg.
*/
public function default_args_values() {
return array(
'content' => 0
);
}
/**
* API callback
*/
public function process_api_call() {
$content = '';
$type = ! empty( $this->args['type'] ) ? $this->args['type'] : 'rich';
if ( ! empty( $this->args['content'] ) ) {
$content = $this->args['content'];
}
$data = [];
/** getting the template type in id */
switch ( $type ) {
case 'rich':
$data['template'] = 1;
break;
case 'wc':
if ( class_exists( 'WooCommerce' ) ) {
$data['template'] = 2;
}
break;
case 'html':
$data['template'] = 3;
break;
case 'editor':
if ( bwfan_is_autonami_pro_active() ) {
$data['template'] = 4;
}
break;
case 'block':
if ( bwfan_is_autonami_pro_active() ) {
$data['template'] = 5;
}
break;
default:
$data['template'] = 1;
}
BWFAN_Common::bwfan_before_send_mail( $type );
BWFAN_Merge_Tag_Loader::set_data( array(
'is_preview' => true,
) );
$body = BWFAN_Common::correct_shortcode_string( $content, $type );
$body = BWFAN_Common::decode_merge_tags( $body );
$body = BWFAN_Common::bwfan_correct_protocol_url( $body );
$data['body'] = $body;
$action_object = BWFAN_Core()->integration->get_action( 'wp_sendemail' );
$body = $action_object->email_content_v2( $data );
$this->response_code = 200;
return $this->success_response( [ 'body' => $body ] );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Campaign_Email_Preview' );

View File

@@ -0,0 +1,68 @@
<?php
class BWFAN_API_Create_Automation extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/automations/create';
}
public function default_args_values() {
$args = [
'title' => '',
];
return $args;
}
public function process_api_call() {
$title = $this->args['title'];
$version = $this->args['version'];
if ( empty( $title ) ) {
return $this->error_response( __( 'Title is missing', 'wp-marketing-automations' ) );
}
if ( $version ) {
$data = [
'title' => $title,
'status' => 2,
'v' => 2
];
$automation_id = BWFAN_Model_Automations_V2::create_new_automation( $data );
if ( intval( $automation_id ) > 0 ) {
global $wpdb;
$metatable = $wpdb->prefix . 'bwfan_automationmeta';
//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "INSERT INTO $metatable ( bwfan_automation_id, meta_key, meta_value ) VALUES
( $automation_id, 'steps', '' ),
( $automation_id, 'links', '' ),
( $automation_id, 'count', 0 ),
( $automation_id, 'requires_update', 0 ),
( $automation_id, 'step_iteration_array', '' ),
( $automation_id, 'merger_points', '' )" );
}
} else {
$automation_id = BWFAN_Core()->automations->create_automation( $title );
}
$this->response_code = 200;
return $this->success_response( [ 'automation_id' => $automation_id ], __( 'Automation created', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Create_Automation' );

View File

@@ -0,0 +1,86 @@
<?php
class BWFAN_API_Delete_Automations extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;;
$this->route = '/automations/';
}
public function default_args_values() {
$args = [
'automation_ids' => []
];
return $args;
}
public function process_api_call() {
$automation_ids = $this->args['automation_ids'];
$not_deleted_automations = array();
if ( empty( $automation_ids ) || ! is_array( $automation_ids ) ) {
return $this->error_response( __( 'Automations ids is missing.', 'wp-marketing-automations' ) );
}
foreach ( $automation_ids as $automation_id ) {
$event_details = BWFAN_Model_Automations::get( $automation_id );
$ids = array( $automation_id );
if ( empty( $event_details ) ) {
$not_deleted_automations[] = $automation_id;
continue;
}
/** Initiate automation object */
$automation_obj = BWFAN_Automation_V2::get_instance( $automation_id );
if ( ! empty( $automation_obj->error ) ) {
continue;
}
$data = $automation_obj instanceof BWFAN_Automation_V2 ? $automation_obj->get_automation_data() : [];
if ( 2 === intval( $data['v'] ) ) {
$automation_obj->delete_migrations( $automation_id );
}
BWFAN_Core()->automations->delete_automation( $ids );
BWFAN_Core()->automations->delete_automationmeta( $ids );
if ( 1 === intval( $data['v'] ) ) {
BWFAN_Core()->tasks->delete_tasks( array(), $ids );
BWFAN_Core()->logs->delete_logs( array(), $ids );
// Set status of logs to 0, so that run now option for those logs can be hide
BWFAN_Model_Logs::update( array(
'status' => 0,
), array(
'automation_id' => $automation_id,
) );
}
BWFAN_Core()->automations->set_automation_id( $automation_id );
do_action( 'bwfan_automation_deleted', $automation_id );
}
if ( ! empty( $not_deleted_automations ) ) {
$message = __( 'Unable to Delete Automations with ids', 'wp-marketing-automations' ) . ': ' . implode( ', ', $not_deleted_automations );
return $this->success_response( [], $message );
}
return $this->success_response( [], __( 'Automations deleted', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Automations' );

View File

@@ -0,0 +1,43 @@
<?php
class BWFAN_API_Delete_Logs extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/automations/logs/';
}
public function default_args_values() {
$args = [
'log_ids' => []
];
return $args;
}
public function process_api_call() {
$log_ids = $this->args['log_ids'];
if ( empty( $log_ids ) || ! is_array( $log_ids ) ) {
return $this->error_response( __( 'Logs ids is missing.', 'wp-marketing-automations' ) );
}
BWFAN_Core()->logs->delete_logs( $log_ids );
return $this->success_response( [], __( 'Logs deleted', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Logs' );

View File

@@ -0,0 +1,44 @@
<?php
class BWFAN_API_Delete_Tasks extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/automations/tasks/';
}
public function default_args_values() {
$args = [
'task_ids' => []
];
return $args;
}
public function process_api_call() {
$task_ids = $this->args['task_ids'];
if ( empty( $task_ids ) || ! is_array( $task_ids ) ) {
return $this->error_response( __( 'Tasks ids is missing.', 'wp-marketing-automations' ), null, 404 );
}
BWFAN_Core()->tasks->delete_tasks( $task_ids, array() );
BWFAN_Core()->logs->delete_logs( $task_ids, array() );
return $this->success_response( [], __( 'Tasks deleted', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Tasks' );

View File

@@ -0,0 +1,55 @@
<?php
class BWFAN_API_Duplicate_Automation extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/automations/(?P<automation_id>[\\d]+)/duplicate/';
}
public function default_args_values() {
$args = [
'automation_id' => null,
];
return $args;
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'key' );
if ( empty( $automation_id ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Automation id is missing', 'wp-marketing-automations' ) );
}
$id = BWFAN_Core()->automations->duplicate( $automation_id );
if ( empty( $id ) ) {
$this->response_code = 400;
/* translators: 1: Automation ID */
return $this->error_response( sprintf( __( 'Unable to create Duplicate automation for id: %1$d', 'wp-marketing-automations' ), $automation_id ) );
}
$automation_data = BWFAN_Model_Automations::get( $id );
$this->response_code = 200;
return $this->success_response( $automation_data, __( 'Automation duplicated', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Duplicate_Automation' );

View File

@@ -0,0 +1,45 @@
<?php
class BWFAN_API_Execute_Automation_Tasks extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automations/execute-tasks/';
}
public function default_args_values() {
$args = [
'task_ids' => '',
];
return $args;
}
public function process_api_call() {
$task_ids = $this->args['task_ids'];
if ( empty( $task_ids ) ) {
return $this->error_response( __( 'Task Id is missing', 'wp-marketing-automations' ) );
}
BWFAN_Core()->tasks->rescheduled_tasks( true, $task_ids );
BWFAN_Core()->logs->rescheduled_logs( $task_ids );
$this->response_code = 200;
return $this->success_response( [], __( 'Tasks rescheduled', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Execute_Automation_Tasks' );

View File

@@ -0,0 +1,39 @@
<?php
class BWFAN_API_Automation_Export_Single extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automations/(?P<automation_id>[\\d]+)/export/';
}
public function default_args_values() {
$args = [
'automation_id' => null,
];
return $args;
}
public function process_api_call() {
$automation_id = $this->get_sanitized_arg( 'automation_id', 'key' );
$get_export_automations_data = BWFAN_Core()->automations->get_json( $automation_id );
$this->response_code = 200;
return $this->success_response( $get_export_automations_data, __( 'Automation exported', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Automation_Export_Single' );

View File

@@ -0,0 +1,31 @@
<?php
class BWFAN_API_Export_Automations extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automations/export/';
}
public function process_api_call() {
$version = ( isset( $this->args['version'] ) && '' !== $this->args['version'] ) ? $this->args['version'] : 1;
$ids = isset( $this->args['ids'] ) ? explode( ',', $this->args['ids'] ) : [];
$get_export_automations_data = BWFAN_Core()->automations->get_json( $ids, $version );
$this->response_code = 200;
return $this->success_response( $get_export_automations_data, __( 'Automations exported', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Export_Automations' );

View File

@@ -0,0 +1,37 @@
<?php
class BWFAN_API_Get_Actions extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/actions';
}
public function process_api_call() {
$actions = BWFAN_Core()->automations->get_all_actions();
if ( ! is_array( $actions ) || empty( $actions ) ) {
return $this->error_response( __( 'Unable to fetch actions', 'wp-marketing-automations' ), null, 500 );
}
return $this->success_response( $actions, __( 'Actions found', 'wp-marketing-automations' ) );
}
public function get_result_total_count() {
return $this->total_count;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Actions' );

View File

@@ -0,0 +1,152 @@
<?php
class BWFAN_API_Get_Automation_stats extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public $count_data = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automations-stats';
}
public function process_api_call() {
$automation_ids = isset( $this->args['automation_ids'] ) ? $this->args['automation_ids'] : [];
$version = $this->get_sanitized_arg( 'version', 'text_field' );
$ids = implode( ',', array_filter( $automation_ids ) );
if ( 2 === absint( $version ) ) {
$data = [];
$cached_key = 'bwfan_automation_v2_stats';
$force = filter_input( INPUT_GET, 'force' );
$exp = BWFAN_Common::get_admin_analytics_cache_lifespan();
/** Get active, paused, completed and failed count for v2 automations */
if ( 'false' === $force ) {
$stats = get_transient( $cached_key );
if ( ! empty( $stats ) ) {
$automation_ids = array_filter( $automation_ids, function ( $id ) use ( $stats ) {
return ! array_key_exists( $id, $stats );
} );
if ( count( $automation_ids ) > 0 ) {
sort( $automation_ids );
}
$data = $stats;
}
}
if ( is_array( $automation_ids ) && count( $automation_ids ) > 0 ) {
$active_automation = BWFAN_Model_Automation_Contact::get_active_count( $automation_ids, 'active' );
$complete_automation = BWFAN_Model_Automation_Complete_Contact::get_automation_complete_contact_count( $ids );
$failed_automation = BWFAN_Model_Automation_Contact::get_active_count( $automation_ids, BWFAN_Automation_Controller::$STATUS_FAILED );
$paused_automation = BWFAN_Model_Automation_Contact::get_active_count( $automation_ids, BWFAN_Automation_Controller::$STATUS_PAUSED );
$active_aids = empty( $active_automation ) ? [] : array_column( $active_automation, 'aid' );
$complete_aids = empty( $complete_automation ) ? [] : array_column( $complete_automation, 'aid' );
$failed_aids = empty( $failed_automation ) ? [] : array_column( $failed_automation, 'aid' );
$paused_aids = empty( $paused_automation ) ? [] : array_column( $paused_automation, 'aid' );
foreach ( $automation_ids as $aid ) {
$active_index = array_search( $aid, $active_aids );
$complete_index = array_search( $aid, $complete_aids );
$failed_index = array_search( $aid, $failed_aids );
$paused_index = array_search( $aid, $paused_aids );
$data[ $aid ] = [
'active' => ( false !== $active_index && isset( $active_automation[ $active_index ]['count'] ) ) ? $active_automation[ $active_index ]['count'] : 0,
'complete' => ( false !== $complete_index && isset( $complete_automation[ $complete_index ]['count'] ) ) ? $complete_automation[ $complete_index ]['count'] : 0,
'failed' => ( false !== $failed_index && isset( $failed_automation[ $failed_index ]['count'] ) ) ? $failed_automation[ $failed_index ]['count'] : 0,
'paused' => ( false !== $paused_index && isset( $paused_automation[ $paused_index ]['count'] ) ) ? $paused_automation[ $paused_index ]['count'] : 0,
];
}
BWFAN_Common::validate_scheduled_recurring_actions();
set_transient( $cached_key, $data, $exp );
}
return $this->success_response( $data, __( 'Automations found', 'wp-marketing-automations' ) );
}
/** For v1 automations */
$data = $this->get_v1_automations_count( $automation_ids );
return $this->success_response( $data, __( 'Automations found', 'wp-marketing-automations' ) );
}
/**
* Get all scheduled and paused task count
*
* @param $automation_ids
*
* @return array
*/
public function get_v1_automations_count( $automation_ids ) {
global $wpdb;
$data = [];
$cached_key = 'bwfan_automation_v1_stats';
$force = filter_input( INPUT_GET, 'force' );
$exp = BWFAN_Common::get_admin_analytics_cache_lifespan();
if ( 'false' === $force ) {
$stats = get_transient( $cached_key );
if ( ! empty( $stats ) ) {
$automation_ids = array_filter( $automation_ids, function ( $id ) use ( $stats ) {
return ! array_key_exists( $id, $stats );
} );
if ( count( $automation_ids ) > 0 ) {
sort( $automation_ids );
}
$data = $stats;
}
}
if ( is_array( $automation_ids ) && count( $automation_ids ) > 0 ) {
$ids = implode( ',', array_filter( $automation_ids ) );
$tasks_table = "{$wpdb->prefix}bwfan_tasks";
$tasks_query = "SELECT `automation_id`, count(`ID`) AS `total_scheduled`, `status` FROM $tasks_table WHERE `automation_id` IN ($ids) GROUP BY `automation_id`, `status`";
$tasks = $wpdb->get_results( $tasks_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$total_tasks = array();
foreach ( $tasks as $automation_tasks ) {
$status = absint( $automation_tasks['status'] ) === 1 ? 'paused' : 'scheduled';
if ( absint( $automation_tasks['automation_id'] ) ) {
$total_tasks[ $status ][ absint( $automation_tasks['automation_id'] ) ] = $automation_tasks['total_scheduled'];
}
}
/** Get completed and failed task count */
$logs_table = "{$wpdb->prefix}bwfan_logs";
$logs_query = "SELECT `automation_id`, count(`ID`) AS `total_logs`, `status` FROM $logs_table WHERE `automation_id` IN ($ids) AND `status` IN (0, 1) GROUP BY `automation_id`, `status`";
$logs = $wpdb->get_results( $logs_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$total_logs = array();
foreach ( $logs as $automation_logs ) {
$status = absint( $automation_logs['status'] ) === 1 ? 'completed' : 'failed';
if ( absint( $automation_logs['automation_id'] ) ) {
$total_logs[ $status ][ absint( $automation_logs['automation_id'] ) ] = $automation_logs;
}
}
foreach ( $automation_ids as $id ) {
$data[ $id ]['scheduled'] = isset( $total_tasks['scheduled'][ $id ] ) ? $total_tasks['scheduled'][ $id ] : 0;
$data[ $id ]['paused'] = isset( $total_tasks['paused'][ $id ] ) ? $total_tasks['paused'][ $id ] : 0;
$data[ $id ]['completed'] = isset( $total_logs['completed'][ $id ]['total_logs'] ) ? $total_logs['completed'][ $id ]['total_logs'] : 0;
$data[ $id ]['failed'] = isset( $total_logs['failed'][ $id ]['total_logs'] ) ? $total_logs['failed'][ $id ]['total_logs'] : 0;
}
set_transient( $cached_key, $data, $exp );
}
return $data;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automation_stats' );

View File

@@ -0,0 +1,93 @@
<?php
class BWFAN_API_Get_Automations extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public $count_data = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automations';
$this->pagination->offset = 0;
$this->pagination->limit = 25;
$this->request_args = array(
'search' => array(
'description' => __( 'Autonami Search', 'wp-marketing-automations' ),
'type' => 'string',
),
'status' => array(
'description' => __( 'Autonami Status', 'wp-marketing-automations' ),
'type' => 'string',
),
'offset' => array(
'description' => __( 'Autonami list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function default_args_values() {
$args = [
'search' => '',
'status' => 'all',
'offset' => 0,
'limit' => 25
];
return $args;
}
public function process_api_call() {
$status = $this->get_sanitized_arg( 'status', 'text_field' );
$search = $this->get_sanitized_arg( 'search', 'text_field' );
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? $this->get_sanitized_arg( 'offset', 'text_field' ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
$version = isset( $this->args['version'] ) ? $this->args['version'] : 1;
$get_automations = BWFAN_Common::get_all_automations( $search, $status, $offset, $limit, false, $version );
if ( ! is_array( $get_automations ) || ! isset( $get_automations['automations'] ) || ! is_array( $get_automations['automations'] ) ) {
return $this->error_response( __( 'Unable to fetch automations', 'wp-marketing-automations' ), null, 500 );
}
/** Check if worker call is late */
$last_run = bwf_options_get( 'fk_core_worker_let' );
if ( '' !== $last_run && ( ( time() - $last_run ) > BWFAN_Common::get_worker_delay_timestamp() ) ) {
/** Worker is running late */
$get_automations['worker_delayed'] = time() - $last_run;
}
/** Check basic worker last run time and status code check */
$resp = BWFAN_Common::validate_core_worker();
if ( isset( $resp['response_code'] ) ) {
$get_automations['response_code'] = $resp['response_code'];
}
$this->total_count = isset( $get_automations['total_records'] ) ? absint( $get_automations['total_records'] ) : 0;
$this->count_data = BWFAN_Common::get_automation_data_count( $version );
return $this->success_response( $get_automations, __( 'Automations found', 'wp-marketing-automations' ) );
}
public function get_result_total_count() {
return $this->total_count;
}
public function get_result_count_data() {
return $this->count_data;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Automations' );

View File

@@ -0,0 +1,101 @@
<?php
class BWFAN_API_Get_Event_Data extends BWFAN_API_Base {
public static $ins;
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/event/';
$this->request_args = array(
'source' => array(
'description' => __( 'Source for get actions.', 'wp-marketing-automations' ),
'type' => 'string',
),
'event_slug' => array(
'description' => __( 'Event slug for get event\'s data', 'wp-marketing-automations' ),
'type' => 'string',
)
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$source = ! empty( $this->get_sanitized_arg( 'source', 'key' ) ) ? $this->get_sanitized_arg( 'source', 'text_field' ) : '';
$event_slug = ! empty( $this->get_sanitized_arg( 'event_slug', 'key' ) ) ? $this->get_sanitized_arg( 'event_slug', 'text_field' ) : '';
if ( empty( $source ) || empty( $event_slug ) ) {
return $this->error_response( __( 'Required parameter is missing', 'wp-marketing-automations' ), null, 500 );
}
$actions = BWFAN_Core()->automations->get_all_actions();
if ( ! isset( $actions[ $source ]['actions'] ) ) {
/* translators: 1: Event slug */
return $this->error_response( sprintf( __( 'Action not found for this source %1$s', 'wp-marketing-automations' ), $source ), null, 500 );
}
$event = BWFAN_Core()->sources->get_event( $event_slug );
if ( empty( $event ) ) {
return $this->error_response( __( 'Event not exist', 'wp-marketing-automations' ), null, 500 );
}
$data = [];
$data['actions'] = $actions[ $source ]['actions'];
/**
* Get Event's rules
* @var BWFAN_EVENT
**/
$event_rule_groups = $event->get_rule_group();
$all_rules_group = BWFAN_Core()->rules->get_all_groups();
$all_rules = apply_filters( 'bwfan_rule_get_rule_types', array() );
$rules = [];
foreach ( $event_rule_groups as $rule_group ) {
if ( isset( $all_rules_group[ $rule_group ] ) ) {
$rules[ $rule_group ] = $all_rules_group[ $rule_group ];
}
if ( isset( $all_rules[ $rule_group ] ) ) {
$rules[ $rule_group ]['rules'] = $all_rules[ $rule_group ];
}
if ( ! isset( $rules[ $rule_group ]['rules'] ) ) {
unset( $rules[ $rule_group ] );
}
}
$data['rules'] = $rules;
/**
* Get Event's all merge_tags
**/
$all_merge_tags = BWFAN_Core()->merge_tags->get_all_merge_tags();
$event_merge_tag_groups = $event->get_merge_tag_groups();
$mergetags = [];
foreach ( $event_merge_tag_groups as $merge_tag_group ) {
if ( isset( $all_merge_tags[ $merge_tag_group ] ) ) {
$tag_data = array_map( function ( $tags ) {
return [
'tag_name' => $tags->get_name(),
'tag_description' => $tags->get_description()
];
}, $all_merge_tags[ $merge_tag_group ] );
$mergetags[ $merge_tag_group ] = array_replace( $mergetags, $tag_data );
}
}
$data['merge_tags'] = $mergetags;
return $this->success_response( $data, __( 'Events found', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Event_Data' );

View File

@@ -0,0 +1,48 @@
<?php
class BWFAN_API_Get_Events extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/events';
}
public function process_api_call() {
$events = BWFAN_Load_Sources::get_sources_events_arr();
$all_triggers = BWFAN_Core()->sources->get_source_localize_data();
uasort( $all_triggers, function ( $a, $b ) {
return $a['priority'] <= $b['priority'] ? - 1 : 1;
} );
$event_data = array_map( function ( $all_trigger ) use ( $events ) {
if ( isset( $events[ $all_trigger['slug'] ] ) ) {
$all_trigger = array_replace( $all_trigger, $events[ $all_trigger['slug'] ] );
}
return $all_trigger;
}, $all_triggers );
if ( ! is_array( $event_data ) || empty( $event_data ) ) {
return $this->error_response( __( 'Unable to fetch events', 'wp-marketing-automations' ), null, 500 );
}
return $this->success_response( $event_data, __( 'Events found', 'wp-marketing-automations' ) );
}
public function get_result_total_count() {
return $this->total_count;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Events' );

View File

@@ -0,0 +1,37 @@
<?php
class BWFAN_API_Get_Task_Filters extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automations/task-filters';
}
public function default_args_values() {
$args = [];
return $args;
}
public function process_api_call() {
$all_actions = BWFAN_Common::get_actions_filter_data();
$all_automations = BWFAN_Common::get_automations_filter_data();
$result['automations'] = $all_automations;
$result['actions'] = $all_actions;
return $this->success_response( $result, __( 'Tasks found', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Task_Filters' );

View File

@@ -0,0 +1,110 @@
<?php
class BWFAN_API_Get_Task_History extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public $count_data = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/automations/task-history';
$this->pagination->offset = 0;
$this->pagination->limit = 25;
$this->request_args = array(
'status' => array(
'description' => __( 'Task Status', 'wp-marketing-automations' ),
'type' => 'string',
),
'search' => array(
'description' => __( 'Search', 'wp-marketing-automations' ),
'type' => 'string',
),
'automation_id' => array(
'description' => __( 'Automation ID', 'wp-marketing-automations' ),
'type' => 'integer',
),
'action_slug' => array(
'description' => __( 'Action Slug', 'wp-marketing-automations' ),
'type' => 'string',
),
'offset' => array(
'description' => __( 'Task list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function default_args_values() {
$args = [
'status' => 't_0',
'offset' => 0,
'automation_id' => null,
'action_slug' => null,
'search' => '',
];
return $args;
}
public function process_api_call() {
$status = $this->get_sanitized_arg( 'status', 'text_field' );
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$action_slug = $this->get_sanitized_arg( 'action_slug', 'text_field' );
$search = $this->get_sanitized_arg( 'search', 'text_field' );
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? $this->get_sanitized_arg( 'offset', 'text_field' ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
if ( $status === 't_0' || $status === 't_1' ) {
$get_task_history = BWFAN_Core()->tasks->get_history( $status, $automation_id, $action_slug, $search, $offset, $limit );
} else {
$get_task_history = BWFAN_Core()->logs->get_history( $status, $automation_id, $action_slug, $search, $offset, $limit );
}
$get_task_history['scheduled_count'] = BWFAN_Core()->tasks->fetch_tasks_count( 0, 0 );
$get_task_history['paused_count'] = BWFAN_Core()->tasks->fetch_tasks_count( 0, 1 );
$get_task_history['completed_count'] = BWFAN_Core()->logs->fetch_logs_count( 1 );
$get_task_history['failed_count'] = BWFAN_Core()->logs->fetch_logs_count( 0 );
$this->count_data = BWFAN_Common::get_automation_data_count();
if ( empty( $get_task_history ) ) {
$this->response_code = 200;
return $this->success_response( $get_task_history, __( 'No tasks found', 'wp-marketing-automations' ) );
}
if ( isset( $get_task_history['found_posts'] ) ) {
$this->total_count = $get_task_history['found_posts'];
unset( $get_task_history['found_posts'] );
}
$this->response_code = 200;
return $this->success_response( $get_task_history, __( 'Tasks found', 'wp-marketing-automations' ) );
}
public function get_result_total_count() {
return $this->total_count;
}
public function get_result_count_data() {
return $this->count_data;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Task_History' );

View File

@@ -0,0 +1,70 @@
<?php
class BWFAN_API_Import_Automations extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/automations/import';
}
public function default_args_values() {
$args = [
'json' => null,
];
return $args;
}
public function process_api_call() {
$this->response_code = 404;
$files = $this->args['files'];
$version = isset( $this->args['version'] ) ? $this->args['version'] : 1;
if ( empty( $files ) ) {
return $this->error_response( __( 'Import File missing.', 'wp-marketing-automations' ) );
}
$file_data = file_get_contents( $files['files']['tmp_name'] );
$import_file_data = json_decode( $file_data, true );
if ( empty( $import_file_data ) && ! is_array( $import_file_data ) ) {
return $this->error_response( __( 'Import file data missing', 'wp-marketing-automations' ) );
}
/** Importing Automation */
$automation_id = BWFAN_Core()->automations->import( $import_file_data, '', [], false, $version );
if ( empty( $automation_id ) ) {
return $this->error_response( __( 'Invalid json file', 'wp-marketing-automations' ) );
}
if ( is_array( $automation_id ) && isset( $automation_id['version'] ) ) {
$next_gen = ( 2 === absint( $version ) ) ? " Next Gen" : '';
/* translators: 1: Automation version */
return $this->error_response( sprintf( __( 'You are trying to import an unsupported JSON format. Ensure that imported file belongs to Autonami %1$s', 'wp-marketing-automations' ), $next_gen ) );
}
$this->response_code = 200;
return $this->success_response( [ 'automation_id' => $automation_id ], __( 'Automation imported', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Import_Automations' );

View File

@@ -0,0 +1,50 @@
<?php
class BWFAN_API_Import_Recipe extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/recipe/import';
}
public function default_args_values() {
$args = [
'recipe_slug' => '',
'title' => '',
];
return $args;
}
public function process_api_call() {
$recipe_slug = $this->args['recipe_slug'];
$title = $this->args['title'];
if ( empty( $recipe_slug ) ) {
return $this->error_response( __( 'Recipe is missing', 'wp-marketing-automations' ), null, 404 );
}
$automation = BWFAN_Recipes::create_automation_by_recipe( $recipe_slug, $title );
if ( $automation['status'] ) {
$this->response_code = 200;
return $this->success_response( [ 'automation_id' => $automation['id'] ], __( 'Recipe imported', 'wp-marketing-automations' ) );
} else {
return $this->error_response( __( 'Unable to import recipe', 'wp-marketing-automations' ), null, 404 );
}
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Import_Recipe' );

View File

@@ -0,0 +1,124 @@
<?php
class BWFAN_API_Automation_License extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/license';
}
public function default_args_values() {
$args = [
'action' => '',
'key' => '',
'name' => '',
];
return $args;
}
public function process_api_call() {
$action = $this->get_sanitized_arg( 'action', 'text_field', $this->args['action'] );
$key = $this->get_sanitized_arg( 'key', 'text_field', $this->args['key'] );
$plugin_name = $this->get_sanitized_arg( 'name', 'text_field', $this->args['name'] );
if ( empty( $key ) || empty( $plugin_name ) ) {
$this->response_code = 400;
return $this->error_response( __( 'License key is missing', 'wp-marketing-automations' ) );
}
$resp = call_user_func_array( array( $this, $plugin_name . '_license' ), [ 'action' => $action, 'key' => $key ] );
if ( isset( $resp['code'] ) && 200 === $resp['code'] ) {
$this->response_code = 200;
return $this->success_response( BWFAN_Common::get_setting_schema(), $resp['msg'] );
}
$this->response_code = 400;
return $this->error_response( $resp['msg'] );
}
protected function autonami_pro_license( $action, $key ) {
$return = [ 'code' => 400 ];
if ( false === class_exists( 'BWFAN_Pro_WooFunnels_Support' ) ) {
return $return;
}
$ins = BWFAN_Pro_WooFunnels_Support::get_instance();
$resp = $this->process_license_call( $ins, $key, $action );
return $resp;
}
protected function autonami_connector_license( $action, $key ) {
$return = [ 'code' => 400 ];
if ( false === class_exists( 'BWFAN_Basic_Connector_Support' ) ) {
return $return;
}
$ins = BWFAN_Basic_Connector_Support::get_instance();
$resp = $this->process_license_call( $ins, $key, $action );
return $resp;
}
protected function process_license_call( $ins, $key, $action ) {
BWFAN_Common::log_test_data( "Key: {$key} & Action: {$action}", 'bwfan-license' );
/** Deactivate call */
if ( 'deactivate' === $action ) {
$result = $ins->process_deactivation_api();
BWFAN_Common::log_test_data( $result, 'bwfan-license' );
if ( isset( $result['deactivated'] ) && $result['deactivated'] == true ) {
$msg = __( 'License deactivated successfully.', 'wp-marketing-automations' );
return [ 'code' => 200, 'msg' => $msg ];
} else {
$msg = __( 'Some error occurred', 'wp-marketing-automations' );
if ( isset( $result['error'] ) ) {
$msg = $result['error'];
}
return [ 'code' => 400, 'msg' => $msg ];
}
}
/** Activate call */
if ( 'activate' === $action ) {
$data = $ins->process_activation_api( $key );
BWFAN_Common::log_test_data( $data, 'bwfan-license' );
if ( isset( $data['error'] ) ) {
$msg = __( 'Some error occurred', 'wp-marketing-automations' );
if ( isset( $data['error'] ) ) {
$msg = $data['error'];
}
return [ 'code' => 400, 'msg' => $msg ];
}
$license_data = '';
if ( isset( $data['activated'] ) && true === $data['activated'] && isset( $data['data_extra'] ) ) {
$license_data = $data['data_extra'];
}
$msg = __( 'License activated successfully.', 'wp-marketing-automations' );
return [ 'code' => 200, 'msg' => $msg, 'license_data' => $license_data ];
}
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Automation_License' );

View File

@@ -0,0 +1,65 @@
<?php
class BWFAN_API_Run_Automation_Task extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automations/task/';
}
public function default_args_values() {
$args = [
'task_id' => '',
];
return $args;
}
public function process_api_call() {
$task_id = $this->args['task_id'];
if ( empty( $task_id ) ) {
return $this->error_response( __( 'Task Id is missing', 'wp-marketing-automations' ) );
}
$resp = array(
'msg' => __( 'Task Executed Successfully', 'wp-marketing-automations' ),
'status' => true,
);
try {
BWFAN_Core()->tasks->bwfan_ac_execute_task( $task_id );
} catch ( Exception $exception ) {
$resp['status'] = false;
$resp['msg'] = $exception->getMessage();
return $this->error_response( __( 'Unable to Execute tasks', 'wp-marketing-automations' ), $resp );
}
if ( BWFAN_Core()->tasks->ajax_status ) {
return $this->success_response( $resp );
}
$resp = array(
'msg' => BWFAN_Core()->tasks->ajax_msg,
'status' => BWFAN_Core()->tasks->ajax_status,
);
$this->response_code = 200;
return $this->success_response( $resp, __( 'Tasks executed', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Run_Automation_Task' );

View File

@@ -0,0 +1,74 @@
<?php
class BWFAN_API_Run_Automation_Tasks extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automations/run-tasks/';
}
public function default_args_values() {
$args = [
'task_ids' => '',
];
return $args;
}
public function process_api_call() {
$task_ids = $this->args['task_ids'];
if ( empty( $task_ids ) ) {
return $this->error_response( __( 'Task Id is missing', 'wp-marketing-automations' ) );
}
$resp = array(
'msg' => __( 'Task Executed Successfully', 'wp-marketing-automations' ),
'status' => true,
);
try {
BWFAN_Core()->tasks->bwfan_ac_execute_task( $task_ids );
} catch ( Exception $exception ) {
$resp['status'] = false;
$resp['msg'] = $exception->getMessage();
$this->response_code = 404;
return $this->error_response( $resp );
}
if ( BWFAN_Core()->tasks->ajax_status ) {
$this->response_code = 200;
return $this->success_response( $resp, __( 'Tasks executed', 'wp-marketing-automations' ) );
}
$resp = array(
'msg' => BWFAN_Core()->tasks->ajax_msg,
'status' => BWFAN_Core()->tasks->ajax_status,
);
if ( 3 !== absint( BWFAN_Core()->tasks->ajax_status ) ) {
$this->response_code = 404;
return $this->success_response( $resp, BWFAN_Core()->tasks->ajax_msg );
}
$this->response_code = 200;
return $this->success_response( $resp, __( 'Task executed', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Run_Automation_Tasks' );

View File

@@ -0,0 +1,63 @@
<?php
class BWFAN_API_Search_Automations extends BWFAN_API_Base {
public static $ins;
public $total_count = 0;
public $count_data = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/search/automations';
$this->pagination->offset = 0;
$this->pagination->limit = 25;
$this->request_args = array(
'search' => array(
'description' => __( 'Automation Search', 'wp-marketing-automations' ),
'type' => 'string',
)
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function default_args_values() {
$args = [
'search' => '',
'status' => 'all',
'offset' => 0,
'limit' => 10,
'ids' => [],
];
return $args;
}
public function process_api_call() {
$search = $this->get_sanitized_arg( 'search', 'text_field' );
$version = isset( $this->args['version'] ) ? intval( $this->args['version'] ) : 1;
$ids = empty( $this->args['ids'] ) ? array() : explode( ',', $this->args['ids'] );
$limit = isset( $this->args['limit'] ) ? intval( $this->args['limit'] ) : 10;
$offset = isset( $this->args['offset'] ) ? intval( $this->args['offset'] ) : 0;
$get_automations = BWFAN_Common::get_automation_by_title( $search, $version, $ids, $limit, $offset );
return $this->success_response( $get_automations, __( 'Automations found', 'wp-marketing-automations' ) );
}
public function get_result_total_count() {
return $this->total_count;
}
public function get_result_count_data() {
return $this->count_data;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Search_Automations' );

View File

@@ -0,0 +1,74 @@
<?php
/**
* Test Mail API file
*
* @package BWFAN_API_Base
*/
/**
* Test Mail API class
*/
class BWFAN_API_Send_Test_Mail extends BWFAN_API_Base {
/**
* BWFAN_API_Base obj
*
* @var BWFCRM_Core
*/
public static $ins;
/**
* Return class instance
*/
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Class constructor
*/
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/autonami/send-test-email';
}
/**
* Default arg.
*/
public function default_args_values() {
return array(
'email' => '',
'content' => 0
);
}
/**
* API callback
*/
public function process_api_call() {
$content = isset( $this->args['content'] ) ? $this->args['content'] : [];
if ( empty( $content ) ) {
return $this->error_response( __( 'No mail data found', 'wp-marketing-automations' ) );
}
$content = BWFAN_Common::is_json( $content ) ? json_decode( $content, true ) : $content;
if ( isset( $content['mail_data'] ) && is_array( $content['mail_data'] ) ) {
$mail_data = $content['mail_data'];
unset( $content['mail_data'] );
$content = array_replace( $content, $mail_data );
}
$content['email'] = $this->get_sanitized_arg( 'email', 'text_field' );
if ( BWFAN_Common::send_test_email( $content ) ) {
return $this->success_response( '', __( 'Test email sent', 'wp-marketing-automations' ) );
}
return $this->error_response( __( 'Unable to send test email', 'wp-marketing-automations' ), null, 500 );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Send_Test_Mail' );

View File

@@ -0,0 +1,60 @@
<?php
class BWFAN_API_Toggle_Automation_State extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/automations/toggle-state';
}
public function default_args_values() {
$args = [
'state' => false,
'automation_id' => null,
];
return $args;
}
public function process_api_call() {
$state = $this->get_sanitized_arg( 'state', 'text_field' );
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
if ( empty( $automation_id ) ) {
return $this->error_response( 'Automation ID is missing', 'wp-marketing-automations' );
}
$automation['status'] = 2;
if ( 1 === absint( $state ) ) {
$automation['status'] = 1;
}
if ( 4 === absint( $state ) ) {
$result = BWFAN_Model_Automationmeta::insert_automation_meta_data( $automation_id, [
'v1_migrate' => true,
] );
} else {
$result = BWFAN_Core()->automations->toggle_state( $automation_id, $automation );
}
if ( false === $result ) {
return $this->error_response( 'Unable to update automation', 'wp-marketing-automations' );
}
$this->response_code = 200;
return $this->success_response( [], __( 'Automation updated', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Toggle_Automation_State' );

View File

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

View File

@@ -0,0 +1,103 @@
<?php
class BWFAN_API_Cart_Analytics extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/analytics/carts/';
}
public function default_args_values() {
return array(
'no_of_days' => '',
'date_range_first' => '',
'date_range_second' => ''
);
}
public function process_api_call() {
$response['totals'] = $this->prepare_data_for_analytics();
$response['intervals'] = $this->prepare_data_for_analytics( 'interval' );
$this->response_code = 200;
return $this->success_response( $response );
}
public function prepare_data_for_analytics( $is_interval = '' ) {
require_once BWFAN_PLUGIN_DIR . '/includes/class-bwfan-cart-analytics.php';
$start_date = ( isset( $this->args['after'] ) && '' !== $this->args['after'] ) ? $this->args['after'] : BWFAN_Cart_Analytics::default_date( WEEK_IN_SECONDS )->format( BWFAN_Cart_Analytics::$sql_datetime_format );
$end_date = ( isset( $this->args['before'] ) && '' !== $this->args['before'] ) ? $this->args['before'] : BWFAN_Cart_Analytics::default_date()->format( BWFAN_Cart_Analytics::$sql_datetime_format );
$intervals_request = ( isset( $this->args['interval'] ) && '' !== $this->args['interval'] ) ? $this->args['interval'] : 'week';
$captured_carts = BWFAN_Cart_Analytics::get_captured_cart( $start_date, $end_date, $intervals_request, $is_interval );
$recovered_carts = BWFAN_Cart_Analytics::get_recovered_cart( $start_date, $end_date, $intervals_request, $is_interval );
$lost_carts = BWFAN_Cart_Analytics::get_lost_cart( $start_date, $end_date, $intervals_request, $is_interval );
$captured_cart_count = isset( $captured_carts[0]['count'] ) ? $captured_carts[0]['count'] : 0;
$captured_cart_count += isset( $lost_carts[0]['count'] ) ? $lost_carts[0]['count'] : 0;
$recovery_percentage = BWFAN_Cart_Analytics::get_recovery_rate( $captured_cart_count, isset( $recovered_carts[0]['count'] ) ? $recovered_carts[0]['count'] : 0 );
$result = [];
$intervals = [];
/** when interval present */
if ( ! empty( $is_interval ) ) {
$intervals_all = BWFAN_Cart_Analytics::intervals_between( $start_date, $end_date, $intervals_request );
foreach ( $intervals_all as $all_interval ) {
$interval = $all_interval['time_interval'];
$start_date = $all_interval['start_date'];
$end_date = $all_interval['end_date'];
$captured_cart = BWFAN_Cart_Analytics::maybe_interval_exists( $captured_carts, 'time_interval', $interval );
$recovered_cart = BWFAN_Cart_Analytics::maybe_interval_exists( $recovered_carts, 'time_interval', $interval );
$lost_cart = BWFAN_Cart_Analytics::maybe_interval_exists( $lost_carts, 'time_interval', $interval );
$recovery_percentage = BWFAN_Cart_Analytics::get_recovery_rate( isset( $captured_cart[0]['count'] ) ? $captured_cart[0]['count'] : 0, isset( $recovered_cart[0]['count'] ) ? $recovered_cart[0]['count'] : 0 );
$intervals['interval'] = $interval;
$intervals['start_date'] = $start_date;
$intervals['date_start_gmt'] = BWFAN_Cart_Analytics::convert_local_datetime_to_gmt( $start_date )->format( BWFAN_Dashboards::$sql_datetime_format );
$intervals['end_date'] = $end_date;
$intervals['date_end_gmt'] = BWFAN_Cart_Analytics::convert_local_datetime_to_gmt( $end_date )->format( BWFAN_Dashboards::$sql_datetime_format );
$intervals['subtotals'] = array(
'recoverable_carts' => isset( $captured_cart[0]['count'] ) ? $captured_cart[0]['count'] : 0,
'potential_revenue' => isset( $captured_cart[0]['sum'] ) ? $captured_cart[0]['sum'] : 0,
'recovered_cart' => isset( $recovered_cart[0]['count'] ) ? $recovered_cart[0]['count'] : 0,
'recovered_revenue' => isset( $recovered_cart[0]['sum'] ) ? $recovered_cart[0]['sum'] : 0,
'recovery_rate' => $recovery_percentage,
'lost_cart' => isset( $lost_cart[0]['count'] ) ? $lost_cart[0]['count'] : 0,
);
$result[] = $intervals;
}
return $result;
}
return [
'recoverable_carts' => isset( $captured_carts[0]['count'] ) ? $captured_carts[0]['count'] : 0,
'potential_revenue' => isset( $captured_carts[0]['sum'] ) ? $captured_carts[0]['sum'] : 0,
'recovered_cart' => isset( $recovered_carts[0]['count'] ) ? $recovered_carts[0]['count'] : 0,
'recovered_revenue' => isset( $recovered_carts[0]['sum'] ) ? $recovered_carts[0]['sum'] : 0,
'recovery_rate' => $recovery_percentage,
'lost_cart' => isset( $lost_carts[0]['count'] ) ? $lost_carts[0]['count'] : 0,
];
}
public function get_result_total_count() {
return $this->total_count;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Cart_Analytics' );

View File

@@ -0,0 +1,140 @@
<?php
class BWFAN_API_Carts_View_Tasks extends BWFAN_API_Base {
public static $ins;
public $task_localized = [];
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/carts/(?P<abandoned_id>[\\d]+)/tasks/';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function default_args_values() {
return [
'abandoned_id' => '',
];
}
public function process_api_call() {
$abandoned_id = $this->args['abandoned_id'];
if ( empty( $abandoned_id ) ) {
return $this->error_response( __( 'Abandoned cart is missing', 'wp-marketing-automations' ) );
}
$type = $this->args['type'];
global $wpdb;
$cart_details = [];
$basic_details = [];
if ( 'recovered' === $type ) {
if ( BWF_WC_Compatibility::is_hpos_enabled() ) {
$query = $wpdb->prepare( "SELECT `order_id` FROM {$wpdb->prefix}wc_orders_meta WHERE `meta_key` = %s AND `meta_value`= %d", '_bwfan_recovered_ab_id', $abandoned_id );
$order_id = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
} else {
$query = $wpdb->prepare( "SELECT `post_id` FROM {$wpdb->prefix}postmeta WHERE `meta_key` = %s AND `meta_value`= %d", '_bwfan_recovered_ab_id', $abandoned_id );
$order_id = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
if ( empty( $order_id ) ) {
return $this->error_response( __( 'Abandoned cart data is missing', 'wp-marketing-automations' ) );
}
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order ) {
return $this->error_response( __( 'Order is missing', 'wp-marketing-automations' ) );
}
$cart_details['email'] = $order->get_meta( '_billing_email' );
$basic_details['email'] = $cart_details['email'];
$basic_details['f_name'] = $order->get_meta( '_billing_first_name' );
$basic_details['l_name'] = $order->get_meta( '_billing_last_name' );
} else {
$cart_details = BWFAN_Model_Abandonedcarts::get( $abandoned_id );
if ( empty( $cart_details ) ) {
return $this->error_response( __( 'Abandoned cart data is missing', 'wp-marketing-automations' ) );
}
$cart_data = json_decode( $cart_details['checkout_data'], true );
$basic_details['email'] = $cart_details['email'];
$basic_details['f_name'] = isset( $cart_data['fields'] ) && isset( $cart_data['fields']['billing_first_name'] ) ? $cart_data['fields']['billing_first_name'] : '';
$basic_details['l_name'] = isset( $cart_data['fields'] ) && isset( $cart_data['fields']['billing_last_name'] ) ? $cart_data['fields']['billing_last_name'] : '';
}
$arr = [ 'v1' => [], 'v2' => [] ];
/** Automation v1 */
$cart_tasks = [];
/** Automation v2 */
$cart_automations = [];
if ( BWFAN_Common::is_automation_v1_active() ) {
$cart_tasks = BWFAN_Recoverable_Carts::get_cart_tasks( $abandoned_id );
$arr['v1'] = $cart_tasks;
}
$cart_email = $cart_details['email'];
$table1 = $wpdb->prefix . 'bwfan_automation_contact';
$table2 = $wpdb->prefix . 'bwfan_automation_complete_contact';
$query1 = "SELECT * FROM $table1 WHERE `event` LIKE 'ab_cart_abandoned' AND `data` LIKE '%cart_abandoned_id\":\"{$abandoned_id}\"%' AND data LIKE '%email\":\"{$cart_email}\"%'";
$result1 = $wpdb->get_results( $query1, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$query2 = "SELECT * FROM $table2 WHERE `event` LIKE 'ab_cart_abandoned' AND `data` LIKE '%cart_abandoned_id\":\"{$abandoned_id}\"%' AND data LIKE '%email\":\"{$cart_email}\"%'";
$result2 = $wpdb->get_results( $query2, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( ! empty( $result1 ) ) {
foreach ( $result1 as $a_contact ) {
$event_slug = $a_contact['event'];
$event_obj = BWFAN_Core()->sources->get_event( $event_slug );
$event_name = ! empty( $event_obj ) ? $event_obj->get_name() : '';
$automation_data = BWFAN_Model_Automations_V2::get_automation( $a_contact['aid'] );
unset( $a_contact['data'] );
$a_contact['event'] = $event_name;
$a_contact['automation_title'] = $automation_data['title'];
$a_contact['e_time'] = isset( $a_contact['e_time'] ) ? date( 'Y-m-d H:i:s', $a_contact['e_time'] ) : '';
$a_contact['last_time'] = isset( $a_contact['last_time'] ) ? date( 'Y-m-d H:i:s', $a_contact['last_time'] ) : '';
$cart_automations[] = array_merge( $a_contact, $basic_details );
}
}
if ( ! empty( $result2 ) ) {
foreach ( $result2 as $a_contact ) {
$event_slug = $a_contact['event'];
$event_obj = BWFAN_Core()->sources->get_event( $event_slug );
$event_name = ! empty( $event_obj ) ? $event_obj->get_name() : '';
$automation_data = BWFAN_Model_Automations_V2::get_automation( $a_contact['aid'] );
unset( $a_contact['data'] );
$a_contact['last_time'] = isset( $a_contact['c_date'] ) ? $a_contact['c_date'] : '';
$a_contact['event'] = $event_name;
$a_contact['automation_title'] = $automation_data['title'];
$cart_automations[] = array_merge( $a_contact, $basic_details );
}
}
$msg = __( 'Automation tasks found', 'wp-marketing-automations' );
if ( empty( $cart_tasks ) && empty( $cart_automations ) ) {
$msg = __( 'No automation found for cart id: ', 'wp-marketing-automations' ) . $abandoned_id;
}
if ( ! empty( $cart_automations ) ) {
$arr['v2'] = $cart_automations;
}
return $this->success_response( $arr, $msg );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Carts_View_Tasks' );

View File

@@ -0,0 +1,52 @@
<?php
class BWFAN_API_Delete_Abandoned_Carts extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/carts/';
}
public function default_args_values() {
return [
'abandoned_ids' => []
];
}
public function process_api_call() {
$abandoned_ids = $this->args['abandoned_ids'];
$type = isset( $this->args['type'] ) ? $this->args['type'] : 'cart';
if ( empty( $abandoned_ids ) || ! is_array( $abandoned_ids ) ) {
return $this->error_response( __( 'Cart IDs are missing', 'wp-marketing-automations' ) );
}
/** Delete recovered cart */
if ( 'order' === $type ) {
BWFAN_Recoverable_Carts::delete_recovered_carts( $abandoned_ids );
return $this->success_response( [], __( 'Cart deleted', 'wp-marketing-automations' ) );
}
$result = BWFAN_Recoverable_Carts::delete_abandoned_cart( $abandoned_ids );
if ( true !== $result && is_array( $result ) ) {
$message = 'Unable to delete cart with ID: ' . implode( ',', $result );
return $this->success_response( [], $message );
}
return $this->success_response( [], __( 'Cart deleted', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Abandoned_Carts' );

View File

@@ -0,0 +1,327 @@
<?php
class BWFAN_API_Get_Lost_Carts extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public $count_data = [];
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/carts/lost/';
$this->pagination->offset = 0;
$this->pagination->limit = 10;
$this->request_args = array(
'search' => array(
'description' => __( 'Search from email or checkout page id', 'wp-marketing-automations' ),
'type' => 'string',
),
'offset' => array(
'description' => __( 'Lost carts list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function default_args_values() {
return [
'search_by' => 'email',
'search' => '',
'offset' => 0,
'limit' => 10
];
}
public function process_api_call() {
$search_by = $this->get_sanitized_arg( 'search_by', 'text_field' );
$search_term = $this->get_sanitized_arg( 'search', 'text_field' );
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? $this->get_sanitized_arg( 'offset', 'text_field' ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
$lost_carts = BWFAN_Recoverable_Carts::get_abandoned_carts( $search_by, $search_term, $offset, $limit, 2 );
if ( isset( $lost_carts['total_count'] ) ) {
$this->total_count = $lost_carts['total_count'];
unset( $lost_carts['total_count'] );
}
$result = [];
$nowDate = new DateTime( 'now', new DateTimeZone( "UTC" ) );
foreach ( $lost_carts as $item ) {
$cartDate = new DateTime( $item->last_modified );
$diff = date_diff( $nowDate, $cartDate, true );
$diff = BWFAN_Common::get_difference_string( $diff );
$currency_data = BWFAN_Recoverable_Carts::get_currency( $item );
$total = ! is_null( $item->total ) ? $item->total : 0;
$fee_data = $this->get_formatted_fee_data( $item->fees );
$result[] = [
'id' => ! is_null( $item->ID ) ? $item->ID : 0,
'email' => ! is_null( $item->email ) ? $item->email : '',
'phone' => $this->get_phone( $item ),
'preview' => $this->get_preview_data( $item, $fee_data['total'] ),
'date' => get_date_from_gmt( $item->last_modified ),
'created_on' => get_date_from_gmt( $item->created_time ),
'status' => ! is_null( $item->status ) ? $item->status : '',
'diffstring' => $diff,
'items' => $this->get_items( $item ),
'fees' => ! empty( $fee_data['data'] ) ? $fee_data['data'] : [],
'total' => $total,
'currency' => $currency_data,
'buyer_name' => $this->get_order_name( $item ),
'order_id' => $this->get_order_id( $item ),
'user_id' => ! empty( $item->user_id ) ? $item->user_id : 0,
'checkout_data' => ! is_null( $item->checkout_data ) ? $item->checkout_data : '',
];
}
$result = BWFAN_Recoverable_Carts::populate_contact_info( $result );
$this->count_data = [];
return $this->success_response( $result, __( 'Lost carts found', 'wp-marketing-automations' ) );
}
/**
* Get formatted fee data
*
* @param $data
*
* @return array|string
*/
public function get_formatted_fee_data( $data ) {
$fee_data = [
'data' => [],
'total' => 0,
];
$data = maybe_unserialize( $data );
if ( empty( $data ) || ! is_array( $data ) ) {
return $fee_data;
}
foreach ( $data as $fee ) {
if ( ! isset( $fee->name ) || empty( $fee->total ) ) {
continue;
}
$amount = floatval( $fee->total );
if ( ! empty( $fee->tax ) ) {
$amount += floatval( $fee->tax );
}
$fee_data['data'][ ! empty( $fee->name ) ? $fee->name : __( 'Fee', 'wp-marketing-automations' ) ] = $amount;
$fee_data['total'] += $amount;
}
return $fee_data;
}
public function get_result_total_count() {
return $this->total_count;
}
public function get_result_count_data() {
return $this->count_data;
}
public function get_user_display_name( $item ) {
if ( empty( $item->user_id ) ) {
return '';
}
$user = get_user_by( 'id', absint( $item->user_id ) );
return $user instanceof WP_User ? $user->display_name : '';
}
public function get_phone( $item ) {
$checkout_data = json_decode( $item->checkout_data, true );
return ( is_array( $checkout_data ) && isset( $checkout_data['fields'] ) && is_array( $checkout_data['fields'] ) && isset( $checkout_data['fields']['billing_phone'] ) && ! empty( $checkout_data['fields']['billing_phone'] ) ) ? $checkout_data['fields']['billing_phone'] : '';
}
public function get_preview_data( $item, $fee_total = 0 ) {
$data = array();
$billing = array();
$shipping = array();
$others = array();
$products = array();
$products_data = maybe_unserialize( $item->items );
$nice_names = BWFAN_Abandoned_Cart::get_woocommerce_default_checkout_nice_names();
$checkout_data = json_decode( $item->checkout_data, true );
if ( is_array( $checkout_data ) && count( $checkout_data ) > 0 ) {
$fields = ( isset( $checkout_data['fields'] ) ) ? $checkout_data['fields'] : [];
$available_gateways = WC()->payment_gateways->payment_gateways();
if ( ! empty( $fields ) ) {
foreach ( $fields as $key => $value ) {
if ( 'billing_phone' === $key ) {
$others[ $nice_names[ $key ] ] = $value;
continue;
}
if ( false !== strpos( $key, 'billing' ) && isset( $nice_names[ $key ] ) ) {
$key = str_replace( 'billing_', '', $key );
$billing[ $key ] = $value;
continue;
}
if ( false !== strpos( $key, 'shipping' ) && isset( $nice_names[ $key ] ) ) {
$key = str_replace( 'shipping_', '', $key );
$shipping[ $key ] = $value;
continue;
}
if ( 'payment_method' === $key ) {
if ( isset( $available_gateways[ $value ] ) && 'yes' === $available_gateways[ $value ]->enabled ) {
$value = $available_gateways[ $value ]->method_title;
}
if ( isset( $nice_names[ $key ] ) ) {
$others[ $nice_names[ $key ] ] = $value;
}
continue;
}
if ( isset( $nice_names[ $key ] ) ) {
$others[ $nice_names[ $key ] ] = $value;
}
}
}
/** Remove WordPress page id in abandoned preview if WordPress page id is same as Aero page id. */
if ( isset( $checkout_data['current_page_id'] ) && isset( $checkout_data['aerocheckout_page_id'] ) && $checkout_data['current_page_id'] === $checkout_data['aerocheckout_page_id'] ) {
unset( $checkout_data['current_page_id'] );
}
foreach ( $checkout_data as $key => $value ) {
if ( isset( $nice_names[ $key ] ) ) {
$others[ $nice_names[ $key ] ] = $value;
}
}
}
$product_total = floatval( $fee_total );
if ( is_array( $products_data ) ) {
$hide_free_products = BWFAN_Common::hide_free_products_cart_order_items();
foreach ( $products_data as $product ) {
if ( true === $hide_free_products && empty( $product['line_total'] ) ) {
continue;
}
if ( ! $product['data'] instanceof WC_Product ) {
continue;
}
$products[] = array(
'name' => $product['data']->get_formatted_name(),
'qty' => $product['quantity'],
'price' => number_format( $product['line_subtotal'], 2, '.', '' ),
);
$product_total = $product_total + $product['line_subtotal'];
}
}
if ( isset( $billing['country'] ) && isset( $billing['state'] ) ) {
$country_states = WC()->countries->get_states( $billing['country'] );
if ( is_array( $country_states ) && isset( $country_states[ $billing['state'] ] ) ) {
$billing['state'] = $country_states[ $billing['state'] ];
}
}
add_filter( 'woocommerce_formatted_address_force_country_display', '__return_true' );
$data['billing'] = WC()->countries->get_formatted_address( $billing );
if ( isset( $shipping['country'] ) && isset( $shipping['state'] ) ) {
$country_states = WC()->countries->get_states( $shipping['country'] );
if ( is_array( $country_states ) && isset( $country_states[ $shipping['state'] ] ) ) {
$shipping['state'] = $country_states[ $shipping['state'] ];
}
}
$data['shipping'] = WC()->countries->get_formatted_address( $shipping );
add_filter( 'woocommerce_formatted_address_force_country_display', '__return_false' );
$data['others'] = $others;
$data['products'] = $products;
$coupon_data = maybe_unserialize( $item->coupons );
$data['currency'] = get_woocommerce_currency_symbol( $item->currency );
$data['discount'] = 0;
if ( is_array( $coupon_data ) && 0 !== count( $coupon_data ) ) {
foreach ( $coupon_data as $key => $coupon ) {
$data['discount'] += isset( $coupon['discount_incl_tax'] ) ? number_format( $coupon['discount_incl_tax'], 2, '.', '' ) : 0;
}
}
$data['total'] = $product_total - (int) $data['discount'];
$data['total'] = $data['total'] + $item->shipping_total;
$data['total'] = ! empty( $data['total'] ) ? number_format( $data['total'], 2, '.', '' ) : 0;
$data['shipping_total'] = ! empty( $item->shipping_total ) ? number_format( $item->shipping_total, 2, '.', '' ) : 0;
return $data;
}
public function get_items( $item ) {
$items = maybe_unserialize( $item->items );
if ( empty( $items ) ) {
return '';
}
$hide_free_products = BWFAN_Common::hide_free_products_cart_order_items();
$names = [];
foreach ( $items as $value ) {
if ( true === $hide_free_products && empty( $value['line_total'] ) ) {
continue;
}
if ( ! $value['data'] instanceof WC_Product ) {
continue;
}
$names[ $value['data']->get_id() ] = wp_strip_all_tags( $value['data']->get_name() );
}
return $names;
}
function get_order_name( $item ) {
if ( 0 === intval( $item->order_id ) ) {
return '';
}
$obj = wc_get_order( $item->order_id );
$buyer = '';
$output = '';
if ( ! $obj instanceof WC_Order ) {
return $output;
}
if ( $obj->get_billing_first_name() || $obj->get_billing_last_name() ) {
/* translators: 1: first name 2: last name */
$buyer = trim( sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $obj->get_billing_first_name(), $obj->get_billing_last_name() ) ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
} elseif ( $obj->get_billing_company() ) {
$buyer = trim( $obj->get_billing_company() );
} elseif ( $obj->get_customer_id() ) {
$user = get_user_by( 'id', $obj->get_customer_id() );
$buyer = ucwords( $user->display_name );
}
return apply_filters( 'woocommerce_admin_order_buyer_name', $buyer, $obj );
}
function get_order_id( $item ) {
if ( 0 === intval( $item->order_id ) ) {
return '';
}
$obj = wc_get_order( $item->order_id );
if ( $obj instanceof WC_Order ) {
return $item->order_id;
}
return '';
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Lost_Carts' );

View File

@@ -0,0 +1,377 @@
<?php
class BWFAN_API_Get_Recoverable_Carts extends BWFAN_API_Base {
public static $ins;
public $total_count = 0;
public $count_data = [];
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/carts/recoverable/';
$this->pagination->offset = 0;
$this->pagination->limit = 10;
$this->request_args = array(
'search' => array(
'description' => __( 'Search from email or checkout page id', 'wp-marketing-automations' ),
'type' => 'string',
),
'offset' => array(
'description' => __( 'Recoverable carts list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function default_args_values() {
return [
'search_by' => 'email',
'search' => '',
'offset' => 0,
'limit' => 10
];
}
public function process_api_call() {
$search_by = $this->get_sanitized_arg( 'search_by', 'text_field' );
$search_term = $this->get_sanitized_arg( 'search', 'text_field' );
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? $this->get_sanitized_arg( 'offset', 'text_field' ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
$recoverable_carts = BWFAN_Recoverable_Carts::get_abandoned_carts( $search_by, $search_term, $offset, $limit );
if ( isset( $recoverable_carts['total_count'] ) ) {
$this->total_count = $recoverable_carts['total_count'];
unset( $recoverable_carts['total_count'] );
}
if ( ! class_exists( 'WooCommerce' ) ) {
return $this->success_response( [], __( 'No recoverable carts found', 'wp-marketing-automations' ) );
}
$result = [];
$nowDate = new DateTime( 'now', new DateTimeZone( "UTC" ) );
foreach ( $recoverable_carts as $item ) {
$cartDate = new DateTime( $item->last_modified );
$diff = date_diff( $nowDate, $cartDate, true );
$diff = BWFAN_Common::get_difference_string( $diff );
$currency_data = BWFAN_Recoverable_Carts::get_currency( $item );
$total = ! is_null( $item->total ) ? $item->total : 0;
$fee_data = $this->get_formatted_fee_data( $item->fees );
$result[] = [
'id' => ! is_null( $item->ID ) ? $item->ID : 0,
'email' => ! is_null( $item->email ) ? $item->email : '',
'phone' => $this->get_phone( $item ),
'preview' => $this->get_preview_data( $item, $fee_data['total'] ),
'date' => get_date_from_gmt( $item->last_modified ),
'created_on' => get_date_from_gmt( $item->created_time ),
'diffstring' => $diff,
'status' => ! is_null( $item->status ) ? $item->status : '',
'items' => $this->get_items( $item ),
'fees' => ! empty( $fee_data['data'] ) ? $fee_data['data'] : [],
'total' => $total,
'currency' => $currency_data,
'buyer_name' => $this->get_order_name( $item ),
'order_id' => $this->get_order_id( $item ),
'user_id' => ! empty( $item->user_id ) ? $item->user_id : 0,
'checkout_data' => ! is_null( $item->checkout_data ) ? $item->checkout_data : '',
'recovery_link' => $this->get_recovery_link( $item ),
];
}
$result = BWFAN_Recoverable_Carts::populate_contact_info( $result );
$this->count_data = [];
return $this->success_response( $result, __( 'Recoverable carts found', 'wp-marketing-automations' ) );
}
/**
* Get formatted fee data
*
* @param $data
*
* @return array|string
*/
public function get_formatted_fee_data( $data ) {
$fee_data = [
'data' => [],
'total' => 0,
];
$data = maybe_unserialize( $data );
if ( empty( $data ) || ! is_array( $data ) ) {
return $fee_data;
}
foreach ( $data as $fee ) {
if ( ! isset( $fee->name ) || empty( $fee->total ) ) {
continue;
}
$amount = floatval( $fee->total );
if ( ! empty( $fee->tax ) ) {
$amount += floatval( $fee->tax );
}
$fee_data['data'][ ! empty( $fee->name ) ? $fee->name : __( 'Fee', 'wp-marketing-automations' ) ] = $amount;
$fee_data['total'] += $amount;
}
return $fee_data;
}
public function get_recovery_link( $item ) {
$checkout_data = json_decode( $item->checkout_data, true );
if ( empty( $checkout_data ) || ! is_array( $checkout_data ) || empty( $item->token ) ) {
return '';
}
$lang = isset( $checkout_data['lang'] ) ? $checkout_data['lang'] : '';
$cart_url = BWFAN_Common::wc_get_cart_recovery_url( $item->token, '', $lang, $checkout_data );
return $cart_url;
}
public function get_phone( $item ) {
$checkout_data = json_decode( $item->checkout_data, true );
if ( is_array( $checkout_data ) && isset( $checkout_data['fields'] ) && is_array( $checkout_data['fields'] ) ) {
if ( isset( $checkout_data['fields']['billing_phone'] ) && ! empty( $checkout_data['fields']['billing_phone'] ) ) {
return $checkout_data['fields']['billing_phone'];
}
if ( isset( $checkout_data['fields']['shipping_phone'] ) && ! empty( $checkout_data['fields']['shipping_phone'] ) ) {
return $checkout_data['fields']['shipping_phone'];
}
}
return '';
}
public function get_preview_data( $item, $fee_total = 0 ) {
$data = array();
$billing = array();
$shipping = array();
$others = array();
$products = array();
$products_data = maybe_unserialize( $item->items );
$nice_names = BWFAN_Abandoned_Cart::get_woocommerce_default_checkout_nice_names();
$checkout_data = json_decode( $item->checkout_data, true );
if ( is_array( $checkout_data ) && count( $checkout_data ) > 0 ) {
$fields = ( isset( $checkout_data['fields'] ) ) ? $checkout_data['fields'] : [];
if ( class_exists( 'WooCommerce' ) ) {
$available_gateways = WC()->payment_gateways->payment_gateways();
}
if ( ! empty( $fields ) ) {
foreach ( $fields as $key => $value ) {
if ( 'billing_phone' === $key ) {
$others[ $nice_names[ $key ] ] = $value;
continue;
}
if ( 'shipping_phone' === $key && isset( $fields['shipping_phone'] ) && ! empty( $fields['shipping_phone'] ) && empty( $fields['billing_phone'] ) ) {
$others[ $nice_names[ $key ] ] = $value;
continue;
}
if ( false !== strpos( $key, 'billing' ) && isset( $nice_names[ $key ] ) ) {
$key = str_replace( 'billing_', '', $key );
$billing[ $key ] = $value;
continue;
}
if ( false !== strpos( $key, 'shipping' ) && isset( $nice_names[ $key ] ) ) {
$key = str_replace( 'shipping_', '', $key );
$shipping[ $key ] = $value;
continue;
}
if ( 'payment_method' === $key ) {
if ( isset( $available_gateways[ $value ] ) && 'yes' === $available_gateways[ $value ]->enabled ) {
$value = $available_gateways[ $value ]->method_title;
}
if ( isset( $nice_names[ $key ] ) ) {
$others[ $nice_names[ $key ] ] = $value;
}
continue;
}
if ( isset( $nice_names[ $key ] ) ) {
$others[ $nice_names[ $key ] ] = $value;
}
}
}
/** Remove WordPress page id in abandoned preview if WordPress page id is same as Aero page id. */
if ( isset( $checkout_data['current_page_id'] ) && isset( $checkout_data['aerocheckout_page_id'] ) && $checkout_data['current_page_id'] === $checkout_data['aerocheckout_page_id'] ) {
unset( $checkout_data['current_page_id'] );
}
foreach ( $checkout_data as $key => $value ) {
if ( isset( $nice_names[ $key ] ) ) {
$others[ $nice_names[ $key ] ] = $value;
}
}
}
$product_total = floatval( $fee_total );
if ( is_array( $products_data ) ) {
$hide_free_products = BWFAN_Common::hide_free_products_cart_order_items();
foreach ( $products_data as $product ) {
if ( true === $hide_free_products && empty( $product['line_total'] ) ) {
continue;
}
if ( ! $product['data'] instanceof WC_Product ) {
continue;
}
$product_line_total = isset( $product['line_total'] ) ? $product['line_total'] : 0;
$product_line_tax = isset( $product['line_tax'] ) ? $product['line_tax'] : 0;
$product_price = $product_line_total + $product_line_tax;
$product_price = ! empty( $product_price ) ? number_format( ( $product_price ), 2, '.', '' ) : 0;
$product_sub_total = isset( $product['line_subtotal'] ) ? $product['line_subtotal'] : 0;
$line_subtotal_tax = isset( $product['line_subtotal_tax'] ) ? $product['line_subtotal_tax'] : 0;
$product_sub_total = $product_sub_total + $line_subtotal_tax;
$product_sub_total = ! empty( $product_sub_total ) ? number_format( ( $product_sub_total ), 2, '.', '' ) : 0;
$product_discount = $product_sub_total - $product_price;
$product_price = $product_price + $product_discount;
$products[] = array(
'name' => $product['data']->get_formatted_name(),
'qty' => $product['quantity'],
'price' => $product_price,
);
$product_total = $product_total + $product_price;
}
}
if ( isset( $billing['country'] ) && isset( $billing['state'] ) ) {
$country_states = WC()->countries->get_states( $billing['country'] );
if ( is_array( $country_states ) && isset( $country_states[ $billing['state'] ] ) ) {
$billing['state'] = $country_states[ $billing['state'] ];
}
}
add_filter( 'woocommerce_formatted_address_force_country_display', '__return_true' );
$data['billing'] = WC()->countries->get_formatted_address( $billing );
if ( isset( $shipping['country'] ) && isset( $shipping['state'] ) ) {
$country_states = WC()->countries->get_states( $shipping['country'] );
if ( is_array( $country_states ) && isset( $country_states[ $shipping['state'] ] ) ) {
$shipping['state'] = $country_states[ $shipping['state'] ];
}
}
$data['shipping'] = WC()->countries->get_formatted_address( $shipping );
add_filter( 'woocommerce_formatted_address_force_country_display', '__return_false' );
$data['others'] = $others;
$data['products'] = $products;
$coupon_data = maybe_unserialize( $item->coupons );
$data['currency'] = get_woocommerce_currency_symbol( $item->currency );
$data['discount'] = 0;
if ( is_array( $coupon_data ) && 0 !== count( $coupon_data ) ) {
foreach ( $coupon_data as $key => $coupon ) {
$data['discount'] += isset( $coupon['discount_incl_tax'] ) ? number_format( $coupon['discount_incl_tax'], 2, '.', '' ) : 0;
}
}
$product_total = $product_total - floatval( $data['discount'] );
$data['total'] = round( $product_total, 2 );
$shipping_total_value = ! empty( $item->shipping_total ) ? floatval( $item->shipping_total ) : 0;
$shipping_tax_value = ! empty( $item->shipping_tax_total ) ? floatval( $item->shipping_tax_total ) : 0;
$shipping_total = $shipping_total_value + $shipping_tax_value;
$data['total'] = $data['total'] + $shipping_total;
$data['total'] = ! empty( $data['total'] ) ? number_format( $data['total'], 2, '.', '' ) : 0;
$data['shipping_total'] = ! empty( $shipping_total ) ? number_format( $shipping_total, 2, '.', '' ) : 0;
$data['shipping_tax_total'] = ! empty( $shipping_tax_value ) ? number_format( $shipping_tax_value, 2, '.', '' ) : 0;
return $data;
}
public function get_items( $item ) {
$items = maybe_unserialize( $item->items );
if ( empty( $items ) ) {
return '';
}
$hide_free_products = BWFAN_Common::hide_free_products_cart_order_items();
$names = [];
foreach ( $items as $value ) {
if ( true === $hide_free_products && empty( $value['line_total'] ) ) {
continue;
}
if ( ! $value['data'] instanceof WC_Product ) {
continue;
}
$names[ $value['data']->get_id() ] = wp_strip_all_tags( $value['data']->get_name() );
}
return $names;
}
function get_order_name( $item ) {
if ( 0 === intval( $item->order_id ) ) {
return '';
}
$obj = wc_get_order( $item->order_id );
$buyer = '';
$output = '';
if ( ! $obj instanceof WC_Order ) {
return $output;
}
if ( $obj->get_billing_first_name() || $obj->get_billing_last_name() ) {
/* translators: 1: first name 2: last name */
$buyer = trim( sprintf( '%1$s %2$s', $obj->get_billing_first_name(), $obj->get_billing_last_name() ) );
} elseif ( $obj->get_billing_company() ) {
$buyer = trim( $obj->get_billing_company() );
} elseif ( $obj->get_customer_id() ) {
$user = get_user_by( 'id', $obj->get_customer_id() );
$buyer = ucwords( $user->display_name );
}
return apply_filters( 'woocommerce_admin_order_buyer_name', $buyer, $obj );
}
function get_order_id( $item ) {
if ( 0 === intval( $item->order_id ) ) {
return '';
}
$obj = wc_get_order( $item->order_id );
if ( $obj instanceof WC_Order ) {
return $item->order_id;
}
return '';
}
public function get_user_display_name( $item ) {
if ( empty( $item->user_id ) ) {
return '';
}
$user = get_user_by( 'id', absint( $item->user_id ) );
return $user instanceof WP_User ? $user->display_name : '';
}
public function get_result_total_count() {
return $this->total_count;
}
public function get_result_count_data() {
return $this->count_data;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Recoverable_Carts' );

View File

@@ -0,0 +1,254 @@
<?php
class BWFAN_API_Get_Recovered_Carts extends BWFAN_API_Base {
public static $ins;
public $total_count = 0;
public $count_data = [];
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/carts/recovered/';
$this->pagination->offset = 0;
$this->pagination->limit = 10;
$this->request_args = array(
'search' => array(
'description' => '',
'type' => 'string',
),
'offset' => array(
'description' => __( 'Recovered carts list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function default_args_values() {
return [
'search' => '',
'offset' => 0,
'limit' => 10
];
}
public function process_api_call() {
$search = $this->get_sanitized_arg( 'search', 'text_field' );
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? $this->get_sanitized_arg( 'offset', 'text_field' ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
$recovered_carts = BWFAN_Recoverable_Carts::get_recovered_carts( $search, $offset, $limit );
$result = [];
$this->count_data = [];
if ( ! isset( $recovered_carts['items'] ) ) {
return $this->success_response( [], __( 'No recovered carts found', 'wp-marketing-automations' ) );
}
$orders = $recovered_carts['items'];
$nowDate = new DateTime( 'now', new DateTimeZone( "UTC" ) );
foreach ( $orders as $order ) {
if ( ! $order instanceof WC_Order ) {
continue;
}
$cartDate = new DateTime( $order->get_date_created()->date( 'Y-m-d H:i:s' ) );
$diff = date_diff( $nowDate, $cartDate, true );
$diff = BWFAN_Common::get_difference_string( $diff );
$currency_data = BWFAN_Recoverable_Carts::get_currency( $order );
$result[] = [
'id' => $order->get_meta( '_bwfan_recovered_ab_id' ),
'order_id' => $order->get_id(),
'email' => $order->get_billing_email(),
'phone' => $order->get_billing_phone(),
'f_name' => $order->get_billing_first_name(),
'l_name' => $order->get_billing_last_name(),
'preview' => $this->get_preview( $order ),
'diffstring' => $diff,
'date' => $order->get_date_created()->date( 'Y-m-d H:i:s' ),
'items' => $this->get_items( $order ),
'total' => $order->get_total(),
'currency' => $currency_data,
'buyer_name' => $this->get_order_name( $order ),
'user_id' => ! empty( $order->get_customer_id() ) ? $order->get_customer_id() : 0,
'checkout_data' => ! is_null( $order->get_meta() ) ? $order->get_meta() : '',
];
}
$result = BWFAN_Recoverable_Carts::populate_contact_info( $result );
if ( isset( $recovered_carts['total_count'] ) ) {
$this->total_count = $recovered_carts['total_count'];
unset( $result['total_record'] );
}
return $this->success_response( $result, __( 'Recovered carts found', 'wp-marketing-automations' ) );
}
/**
* @param $order WC_Order
*
* @return array
*/
public function get_preview( $order ) {
$data = array();
$products = array();
$order_items = $order->get_items();
foreach ( $order_items as $product ) {
$products[] = array(
'name' => $product->get_name(),
'qty' => $product->get_quantity(),
'price' => number_format( $order->get_line_subtotal( $product ), 2, '.', '' ),
);
}
$data['order_id'] = $order->get_id();
$data['products'] = $products;
$data['billing'] = $order->get_formatted_billing_address();
$data['shipping'] = $order->get_formatted_shipping_address();
$data['discount'] = $order->get_total_discount();
$data['total'] = $order->get_total();
return $data;
}
/**
* @param $order WC_Order
*
* @return array
*/
public function get_items( $order ) {
$names = [];
foreach ( $order->get_items() as $value ) {
if ( ! $value instanceof WC_Order_Item ) {
continue;
}
$product_name = $value->get_name();
$product_id = $value->get_product_id();
if ( $value->is_type( 'variable' ) ) {
$product_id = $value->get_variation_id();
}
$names[ $product_id ] = wp_strip_all_tags( $product_name );
}
return $names;
}
/**
* @param $contact_id
* @param $order_id
*
* @return string[]
*/
public function get_name( $contact_id, $order_id ) {
$data = array( 'f_name' => '', 'l_name' => '' );
if ( ! empty( $contact_id ) ) {
$contact_array = new WooFunnels_Contact( '', '', '', $contact_id );
$data['f_name'] = $contact_array->get_f_name();
$data['l_name'] = $contact_array->get_l_name();
return $data;
}
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order ) {
return $data;
}
$data['f_name'] = $order->get_billing_first_name();
$data['l_name'] = $order->get_billing_last_name();
return $data;
}
/**
* @param $order WC_Order
*
* @return mixed|string|void
*/
function get_order_name( $order ) {
if ( ! $order instanceof WC_Order ) {
return '';
}
$buyer = '';
if ( $order->get_billing_first_name() || $order->get_billing_last_name() ) {
/* translators: 1: first name 2: last name */
$buyer = trim( sprintf( '%1$s %2$s', $order->get_billing_first_name(), $order->get_billing_last_name() ) );
} elseif ( $order->get_billing_company() ) {
$buyer = trim( $order->get_billing_company() );
} elseif ( $order->get_customer_id() ) {
$user = get_user_by( 'id', $order->get_customer_id() );
$buyer = ucwords( $user->display_name );
}
return apply_filters( 'woocommerce_admin_order_buyer_name', $buyer, $order );
}
public function get_result_total_count() {
return $this->total_count;
}
public function get_result_count_data() {
return $this->count_data;
}
/**
* @param $order WC_Order
*
* @return mixed|string|void
*/
public function get_full_name( $order ) {
if ( ! $order instanceof WC_Order ) {
return '';
}
$buyer = '';
if ( $order->get_billing_first_name() || $order->get_billing_last_name() ) {
/* translators: 1: first name 2: last name */
$buyer = trim( sprintf( '%1$s %2$s', $order->get_billing_first_name(), $order->get_billing_last_name() ) );
} elseif ( $order->get_billing_company() ) {
$buyer = trim( $order->get_billing_company() );
} elseif ( $order->get_customer_id() ) {
$user = get_user_by( 'id', $order->get_customer_id() );
$buyer = ucwords( $user->display_name );
}
return apply_filters( 'woocommerce_admin_order_buyer_name', $buyer, $order );
}
/**
* @param $order WC_Order
*
* @return mixed
*/
public function get_email( $order ) {
return $order->get_billing_email();
}
/**
* @param $order WC_Order
*
* @return string
*/
public function get_user_display_name( $order ) {
if ( empty( $order->get_customer_id() ) ) {
return '';
}
$user = get_user_by( 'id', absint( $order->get_customer_id() ) );
return $user instanceof WP_User ? $user->display_name : '';
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Recovered_Carts' );

View File

@@ -0,0 +1,40 @@
<?php
class BWFAN_API_Retry_Abandoned_Carts extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/carts/abandoned/retry/';
}
public function default_args_values() {
return [
'abandoned_ids' => []
];
}
public function process_api_call() {
$abandoned_ids = $this->args['abandoned_ids'];
if ( empty( $abandoned_ids ) || ! is_array( $abandoned_ids ) ) {
return $this->error_response( __( 'Abandoned carts missing', 'wp-marketing-automations' ) );
}
BWFAN_Recoverable_Carts::retry_abandoned_cart( $abandoned_ids );
return $this->success_response( [], __( 'Cart retry done', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Retry_Abandoned_Carts' );

View File

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

View File

@@ -0,0 +1,60 @@
<?php
class BWFAN_API_Delete_Connector extends BWFAN_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/connector/disconnect';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$slug = $this->get_sanitized_arg( 'wfco_connector', 'text_field' );
if ( empty( $slug ) ) {
return $this->error_response( __( 'Connector saved data missing, kindly disconnect and connect again.', 'wp-marketing-automations' ), null, 400 );
}
/** Handling for Bitly */
if ( 'bwfco_bitly' === $slug ) {
$bitly = BWFCO_Bitly::get_instance();
if ( $bitly->is_connected() ) {
$bitly->disconnect();
return $this->success_response( array(), __( 'Connector deleted', 'wp-marketing-automations' ) );
}
}
$connector_details = WFCO_Model_Connectors::get_specific_rows( 'slug', $slug );
if ( empty( $connector_details ) ) {
return $this->error_response( __( 'Connector not exist.', 'wp-marketing-automations' ), null, 500 );
}
$connector = WFCO_Load_Connectors::get_connector( $slug );
if ( method_exists( $connector, 'disconnect' ) ) {
$connector->disconnect();
}
$connector_ids = implode( ',', array_map( function ( $connector ) {
return $connector['ID'];
}, $connector_details ) );
$connector_sql = "DELETE from {table_name} where ID IN ($connector_ids)";
$connector_meta_sql = "DELETE from {table_name} where connector_id IN ($connector_ids)";
WFCO_Model_ConnectorMeta::delete_multiple( $connector_meta_sql );
WFCO_Model_Connectors::delete_multiple( $connector_sql );
do_action( 'bwfan_connector_disconnected', $slug );
return $this->success_response( array(), __( 'Connector deleted', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Connector' );

View File

@@ -0,0 +1,44 @@
<?php
class BWFAN_API_Get_Connectors extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/connectors';
}
public function process_api_call() {
$connectors = BWFAN_Core()->connectors->get_connectors_for_listing();
if ( ! bwfan_is_autonami_connector_active() ) {
$connectors = array_map( function ( $connector ) {
$name = $connector['name'];
$name = strtolower( str_replace( [ ' ', '/' ], [ '-', '' ], $name ) ) . '.png';
if ( isset( $connector['logo'] ) && ! empty( $connector['logo'] ) ) {
if ( wp_http_validate_url( $connector['logo'] ) ) {
return $connector;
}
$name = $connector['logo'] . '.png';
}
$connector['logo'] = BWFAN_PLUGIN_URL . '/includes/connectors-logo/' . $name;
return $connector;
}, $connectors );
}
return $this->success_response( $connectors, '' );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Connectors' );

View File

@@ -0,0 +1,74 @@
<?php
class BWFAN_API_Save_Connector extends BWFAN_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/connector/save';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$wfco_connector = $this->get_sanitized_arg( 'wfco_connector', 'text_field', $this->args['wfco_connector'] );
if ( empty( $wfco_connector ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Provided connector is empty.', 'wp-marketing-automations' ) );
}
$active_connectors = WFCO_Load_Connectors::get_active_connectors();
$connector = $active_connectors[ sanitize_text_field( $wfco_connector ) ];
if ( ! $connector instanceof BWF_CO ) {
$message = __( 'Something is wrong, connector isn\'t available.', 'wp-marketing-automations' );
$this->response_code = 500;
return $this->error_response( $message );
}
$id = $this->get_sanitized_arg( 'id', 'text_field' );
$action = empty( absint( $id ) ) ? 'save' : 'update';
/** Do Wizard Connector Handling (Mailchimp, GetResponse, Mautic) */
if ( BWFAN_Core()->connectors->is_wizard_connector( $connector ) ) {
$next_step = $connector->get_next_step( $this->args );
/** Error in Data provided for next step */
if ( is_wp_error( $next_step ) ) {
return $this->error_response( '', $next_step, 500 );
}
/** If step type = 'handle_settings_with_params', then go through handle_settings_form with new params */
if ( is_array( $next_step ) && isset( $next_step['step_type'] ) && 'handle_settings_with_params' === $next_step['step_type'] ) {
$this->args = $next_step['params'];
} elseif ( true !== $next_step ) {
do_action( 'bwfan_connector_connected', $wfco_connector );
/** If true, then go through handle_settings_form, else get next step data */
return $this->success_response( $next_step );
}
}
$response = $connector->handle_settings_form( $this->args, $action );
$status = in_array( $response['status'], [ 'success', 'rerun_api', 'not_connected' ] ) ? true : false;
$message = $response['message'];
/** Error occurred */
if ( false === $status ) {
$this->response_code = 500;
return $this->error_response( $message );
}
do_action( 'bwfan_connector_connected', $wfco_connector );
return $this->success_response( $response, __( 'Connector updated', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Save_Connector' );

View File

@@ -0,0 +1,52 @@
<?php
class BWFAN_API_Sync_Connector extends BWFAN_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/connector/sync';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$wfco_connector = $this->get_sanitized_arg( 'wfco_connector', 'text_field' );
$id = $this->get_sanitized_arg( 'id', 'text_field' );
if ( empty( $wfco_connector ) || empty( ( $id ) ) ) {
return $this->error_response( __( 'Connector saved data missing, kindly disconnect and connect again.', 'wp-marketing-automations' ), null, 400 );
}
$current_connector = WFCO_Load_Connectors::get_connector( $wfco_connector );
if ( ! $current_connector instanceof BWF_CO ) {
$message = __( 'Something is wrong, connector isn\'t available.', 'wp-marketing-automations' );
return $this->error_response( $message, null, 500 );
}
try {
$response = $current_connector->handle_settings_form( $this->args, 'sync' );
$status = ( 'success' === $response['status'] ) ? true : false;
$message = $response['message'];
} catch ( Error $e ) {
$message = $e->getMessage();
return $this->error_response( $message, null, 500 );
}
/** Error occurred */
if ( false === $status ) {
return $this->error_response( $message, null, 500 );
}
return $this->success_response( $response, __( 'Connector updated', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Sync_Connector' );

View File

@@ -0,0 +1,46 @@
<?php
class BWFAN_API_Update_Connector extends BWFAN_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/connector/update';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$wfco_connector = $this->get_sanitized_arg( 'wfco_connector', 'text_field' );
$id = $this->get_sanitized_arg( 'id', 'text_field' );
if ( empty( $wfco_connector ) || empty( $id ) ) {
return $this->error_response( __( 'Connector saved data missing, kindly disconnect and connect again.', 'wp-marketing-automations' ), null, 400 );
}
$active_connectors = WFCO_Load_Connectors::get_active_connectors();
if ( ! $active_connectors[ sanitize_text_field( $wfco_connector ) ] instanceof BWF_CO ) {
$message = __( 'Something is wrong, connector isn\'t available.', 'wp-marketing-automations' );
return $this->error_response( $message, null, 500 );
}
$response = $active_connectors[ sanitize_text_field( $wfco_connector ) ]->handle_settings_form( $this->args, 'update' );
$status = ( 'success' === $response['status'] ) ? true : false;
$message = $response['message'];
/** Error occurred */
if ( false === $status ) {
return $this->error_response( $message, null, 500 );
}
return $this->success_response( $response, __( 'Connector updated', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Update_Connector' );

View File

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

View File

@@ -0,0 +1,114 @@
<?php
class BWFAN_API_Apply_Field extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/fields';
}
public function default_args_values() {
return array(
'contact_id' => 0,
'email' => '',
'fields' => '',
);
}
public function process_api_call() {
$contact = $this->get_contact_by_id_or_email( 'contact_id', 'email' );
$email = $this->get_sanitized_arg( 'email', 'text_field' );
if ( is_wp_error( $contact ) ) {
return $contact;
}
if ( empty( $email ) || ! is_email( $email ) ) {
$email = $contact->contact->get_email();
}
$sanitize_cb = 'text_field';
if ( is_array( $this->args['fields'] ) ) {
$ids = array_keys( $this->args['fields'] );
$ids = array_filter( $ids, [ $this, 'check_field_type_textarea' ] );
if ( count( $ids ) > 0 ) {
$sanitize_cb = 'textarea_field';
}
}
$fields = $this->get_sanitized_arg( '', $sanitize_cb, $this->args['fields'] );
if ( empty( $fields ) ) {
$response = __( 'Required Fields missing', 'wp-marketing-automations' );
$this->response_code = 400;
return $this->error_response( $response );
}
$field_email = $this->get_sanitized_arg( 'email', 'text_field', $this->args['fields'] );
if ( ! empty( $field_email ) && $email !== $field_email ) {
if ( ! is_email( $field_email ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Email is not valid.', 'wp-marketing-automations' ) );
}
$check_contact = new BWFCRM_Contact( $field_email );
/** If email is already exists with other contacts*/
if ( $check_contact->is_contact_exists() ) {
/** If Only email field to be updated then return error response */
if ( 1 === count( $this->args['fields'] ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Email is already associated with other contact.', 'wp-marketing-automations' ) );
}
/**If other fields also available for update then unset the email */
unset( $fields['email'] );
}
}
$response = $contact->update_custom_fields( $fields );
if ( ! ( $response ) || empty( $response ) ) {
$this->response_code = 200;
return $this->success_response( '', __( 'Unable to Update fields', 'wp-marketing-automations' ) );
}
/** If email changed and email is set for change then hook added */
if ( ! empty( $field_email ) && $email !== $field_email ) {
do_action( 'bwfan_contact_email_changed', $field_email, $email, $contact );
}
return $this->success_response( $contact->get_array( false, true, true, true ), __( 'Contact updated', 'wp-marketing-automations' ) );
}
public function check_field_type_textarea( $id ) {
$fields = BWFCRM_Fields::get_custom_fields( null, null, null );
$fields = array_filter( $fields, function ( $val ) {
if ( isset( $val['type'] ) && 3 == $val['type'] ) {
/** 3 is textarea */
return true;
}
return false;
} );
if ( is_array( $fields ) && array_key_exists( $id, $fields ) ) {
return true;
}
return false;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Apply_Field' );

View File

@@ -0,0 +1,94 @@
<?php
class BWFAN_API_Apply_Lists extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/lists';
}
public function default_args_values() {
return array(
'contact_id' => 0,
'lists' => array(),
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id' );
if ( empty( $contact_id ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact ID is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$lists = $this->args['lists'];
$lists = array_filter( array_values( $lists ) );
if ( empty( $lists ) ) {
$response = __( 'Required Lists missing', 'wp-marketing-automations' );
$this->response_code = 404;
return $this->error_response( $response );
}
/** Checking for comma in tag values */
$lists = BWFAN_Common::check_for_comma_seperated( $lists );
$added_lists = $contact->add_lists( $lists );
if ( is_wp_error( $added_lists ) ) {
$this->response_code = 500;
return $this->error_response( '', $added_lists );
}
if ( empty( $added_lists ) ) {
$this->response_code = 200;
return $this->success_response( '', __( 'Provided lists are applied already.', 'wp-marketing-automations' ) );
}
$result = [];
$lists_added = array_map( function ( $list ) {
return $list->get_array();
}, $added_lists );
$message = __( 'List(s) assigned', 'wp-marketing-automations' );
if ( count( $lists ) !== count( $added_lists ) ) {
$added_lists_names = array_map( function ( $list ) {
return $list->get_name();
}, $added_lists );
$added_lists_names = implode( ', ', $added_lists_names );
$this->response_code = 200;
/* translators: 1: comma seperated list */
$message = sprintf( __( 'Some lists are applied already. Applied Lists are: %1$s', 'wp-marketing-automations' ), $added_lists_names );
}
$result['list_added'] = is_array( $lists_added ) ? array_values( $lists_added ) : $lists_added;
$result['last_modified'] = $contact->contact->get_last_modified();
return $this->success_response( $result, $message );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Apply_Lists' );

View File

@@ -0,0 +1,89 @@
<?php
class BWFAN_API_Apply_Tags extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/tags';
}
public function default_args_values() {
return array(
'contact_id' => 0,
'tags' => array(),
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'key' );
if ( empty( $contact_id ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact ID is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found related with contact id : %1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$tags = $this->args['tags'];
$tags = array_filter( array_values( $tags ) );
if ( empty( $tags ) ) {
$response = __( 'No Tags provided', 'wp-marketing-automations' );
$this->response_code = 400;
return $this->error_response( $response );
}
$tags = BWFAN_Common::check_for_comma_seperated( $tags );
$added_tags = $contact->add_tags( $tags );
if ( is_wp_error( $added_tags ) ) {
$this->response_code = 500;
return $this->error_response( '', $added_tags );
}
if ( empty( $added_tags ) ) {
$this->response_code = 200;
return $this->success_response( '', __( 'Provided tags are applied already.', 'wp-marketing-automations' ) );
}
$tags_added = array_map( function ( $tag ) {
return $tag->get_array();
}, $added_tags );
$result = [];
$message = __( 'Tag(s) added', 'wp-marketing-automations' );
if ( count( $tags ) !== count( $added_tags ) ) {
$applied_tags_names = array_map( function ( $tag ) {
return $tag->get_name();
}, $added_tags );
$applied_tags_names = implode( ', ', $applied_tags_names );
$this->response_code = 200;
/* translators: 1: comma seperated tags */
$message = sprintf( __( 'Some tags are applied already. Applied Tags are: %1$s', 'wp-marketing-automations' ), $applied_tags_names );
}
$result['tags_added'] = is_array( $tags_added ) ? array_values( $tags_added ) : $tags_added;
$result['last_modified'] = $contact->contact->get_last_modified();
return $this->success_response( $result, $message );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Apply_Tags' );

View File

@@ -0,0 +1,94 @@
<?php
class BWFAN_API_Contact_Listing extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/v3/contacts/listing';
$this->pagination->offset = 0;
$this->pagination->limit = 10;
}
/** Set default order & order by value */
public function default_args_values() {
return [
'order' => 'desc',
'limit' => 25,
'offset' => 0,
'order_by' => 'last_modified',
];
}
public function process_api_call() {
/** checking if search present in params */
$search = $this->get_sanitized_arg( 'search', 'text_field' );
$filters_collection = empty( $this->args['filters'] ) ? array() : $this->args['filters'];
$get_wc_data = $this->get_sanitized_arg( 'get_wc', 'bool' );
$grab_totals = $this->get_sanitized_arg( 'grab_totals', 'bool' );
$only_count = $this->get_sanitized_arg( 'only_count', 'bool' );
$contact_mode = $this->get_sanitized_arg( 'fetch_base', 'text_field' );
$exclude_unsubs = $this->get_sanitized_arg( 'exclude_unsubs', 'bool' );
$exclude_unsubs_lists = $this->get_sanitized_arg( 'exclude_unsubs_lists', 'bool' );
$grab_custom_fields = $this->get_sanitized_arg( 'grab_custom_fields', 'bool' );
$order = $this->get_sanitized_arg( 'order', 'text_field' );
$order_by = $this->get_sanitized_arg( 'order_by', 'text_field' );
$additional_info = array(
'grab_totals' => $grab_totals,
'only_count' => $only_count,
'fetch_base' => $contact_mode,
'exclude_unsubs' => $exclude_unsubs,
'exclude_unsubs_lists' => $exclude_unsubs_lists,
'grab_custom_fields' => $grab_custom_fields,
);
if ( ! empty( $order ) && ! empty( $order_by ) ) {
$additional_info['order'] = $order;
$additional_info['order_by'] = $order_by;
}
if ( class_exists( 'WooCommerce' ) ) {
$additional_info['customer_data'] = $get_wc_data;
}
$normalized_filters = [];
$filter_match = 'all';
if ( bwfan_is_autonami_pro_active() ) {
$filter_match = isset( $filters_collection['match'] ) && ! empty( $filters_collection['match'] ) ? $filters_collection['match'] : 'all';
$filter_match = ( 'any' === $filter_match ? ' OR ' : ' AND ' );
$normalized_filters = BWFCRM_Filters::_normalize_input_filters( $filters_collection );
}
$contacts = BWFCRM_Model_Contact::get_contact_listing( $search, $this->pagination->limit, $this->pagination->offset, $normalized_filters, $additional_info, $filter_match );
$this->count_data = BWFAN_Common::get_contact_data_counts();
$this->total_count = $contacts['total'];
$this->response_code = 200;
return $this->success_response( $contacts['contacts'] );
}
public function get_result_total_count() {
return $this->total_count;
}
public function get_result_count_data() {
return $this->count_data;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Contact_Listing' );

View File

@@ -0,0 +1,61 @@
<?php
class BWFAN_API_Contact_Resubscribe extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/resubscribe';
$this->request_args = array(
'contact_id' => array(
'description' => __( 'Contact ID to resubscribe contact', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function default_args_values() {
return array(
'contact_id' => ''
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'text_field' );
$contact = new BWFCRM_Contact( absint( $contact_id ) );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact doesn\'t exist', 'wp-marketing-automations' ) );
}
$unsubscribe_data = $contact->check_contact_unsubscribed( false );
if ( empty( $unsubscribe_data ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact already subscribed', 'wp-marketing-automations' ) );
}
foreach ( $unsubscribe_data as $data ) {
BWFAN_Model_Message_Unsubscribe::delete( $data['ID'] );
}
$contact->save_last_modified();
$this->response_code = 200;
return $this->success_response( [ 'contact_id' => $contact_id ], __( 'Contact subscribed', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Contact_Resubscribe' );

View File

@@ -0,0 +1,87 @@
<?php
class BWFAN_API_Contact_Status_Change extends BWFAN_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/execute_status_action/(?P<status>[a-zA-Z0-9-]+)';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'text_field' );
$status = $this->get_sanitized_arg( 'status', 'text_field' );
$contact = new BWFCRM_Contact( absint( $contact_id ) );
if ( ! $contact->is_contact_exists() ) {
return $this->error_response( __( 'Contact does not exists', 'wp-marketing-automations' ) );
}
$result = false;
$message = __( 'Unable to perform requested action', 'wp-marketing-automations' );
switch ( $status ) {
case 'resubscribe':
$result = $contact->resubscribe();
if ( true === $result ) {
$message = __( 'Contact resubscribed', 'wp-marketing-automations' );
}
break;
case 'unsubscribe':
$result = $contact->unsubscribe();
if ( true === $result ) {
$message = __( 'Contact unsubscribed', 'wp-marketing-automations' );
}
break;
case 'verify':
$result = $contact->verify();
if ( true === $result ) {
$message = __( 'Contact subscribed', 'wp-marketing-automations' );
}
break;
case 'unverify':
$result = $contact->unverify();
if ( true === $result ) {
$message = __( 'Contact unverified', 'wp-marketing-automations' );
}
break;
case 'bounced':
$result = $contact->mark_as_bounced();
if ( true === $result ) {
$message = __( 'Contact bounced', 'wp-marketing-automations' );
}
break;
case 'softbounced':
$result = $contact->mark_as_soft_bounced();
if ( true === $result || isset( $result['message'] ) ) {
$message = ! isset( $result['message'] ) ? __( 'Contact soft bounced', 'wp-marketing-automations' ) : $result['message'];
}
break;
case 'complaint':
$result = $contact->mark_as_complaint();
if ( true === $result ) {
$message = __( 'Contact complaint', 'wp-marketing-automations' );
}
break;
}
if ( ! $result ) {
return $this->error_response( $message, null, 500 );
}
return $this->success_response( [
'status' => $contact->get_display_status(),
'data' => $contact->get_array( false, true, true, true, true ),
], $message );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Contact_Status_Change' );

View File

@@ -0,0 +1,81 @@
<?php
class BWFAN_API_Contact_Unsubscribe extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/unsubscribe';
$this->request_args = array(
'contact_id' => array(
'description' => __( 'Contact ID to unsubscribe contact', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function default_args_values() {
return array(
'contact_id' => '',
'mode' => '',
'automation_id' => '',
'c_type' => ''
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'text_field' );
$automation_id = $this->get_sanitized_arg( 'automation_id', 'text_field' );
$c_type = $this->get_sanitized_arg( 'c_type', 'text_field' );
$contact = new BWFCRM_Contact( absint( $contact_id ) );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact doesn\'t exist', 'wp-marketing-automations' ) );
}
$unsubscribed = $contact->check_contact_unsubscribed();
if ( ! empty( $unsubscribed['ID'] ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact already unsubscribed', 'wp-marketing-automations' ) );
}
$recipients = [];
$email = $contact->contact->get_email();
$phone = $contact->contact->get_contact_no();
$recipients[] = $email;
if ( ! empty( $phone ) ) {
$recipients[] = $phone;
}
foreach ( $recipients as $recipient ) {
$insert_data = array(
'recipient' => $recipient,
'mode' => is_email( $recipient ) ? 1 : 2,
'c_date' => current_time( 'mysql', 1 ),
'automation_id' => ! empty( $automation_id ) ? absint( $automation_id ) : get_current_user_id(),
'c_type' => ! empty( $c_type ) ? absint( $c_type ) : 3
);
BWFAN_Model_Message_Unsubscribe::insert( $insert_data );
/** hook when any contact unsubscribed */
do_action( 'bwfcrm_after_contact_unsubscribed', $insert_data );
}
$contact->save_last_modified();
return $this->success_response( [ 'contact_id' => $contact_id ], __( 'Contact unsubscribed', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Contact_Unsubscribe' );

View File

@@ -0,0 +1,64 @@
<?php
class BWFAN_API_Create_Contact_Note extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/notes';
}
public function default_args_values() {
return array(
'contact_id' => 0,
'notes' => array(),
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'key' );
if ( empty( $contact_id ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact ID is mandatory', 'wp-marketing-automations' ) );
}
$notes = $this->args['notes'];
if ( empty( $notes ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Note is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$note_added = $contact->add_note_to_contact( $notes );
if ( false === $note_added || is_wp_error( $note_added ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Unable to add note to contact', 'wp-marketing-automations' ) );
}
$this->response_code = 200;
return $this->success_response( [], __( 'Contact note added', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Create_Contact_Note' );

View File

@@ -0,0 +1,75 @@
<?php
class BWFAN_API_Create_Contact extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = '/v3/contacts';
}
public function process_api_call() {
$email = $this->get_sanitized_arg( 'email', 'email' );
if ( false === $email || ! is_email( $email ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Email is not valid', 'wp-marketing-automations' ) );
}
$params = array(
'f_name' => isset( $this->args['f_name'] ) ? $this->get_sanitized_arg( 'f_name', 'text_field', $this->args['f_name'] ) : '',
'l_name' => isset( $this->args['l_name'] ) ? $this->get_sanitized_arg( 'l_name', 'text_field', $this->args['l_name'] ) : '',
'create_wp_user' => isset( $this->args['create_wp_user'] ) ? rest_sanitize_boolean( $this->args['create_wp_user'] ) : '',
'wp_password' => isset( $this->args['wp_password'] ) ? $this->args['wp_password'] : '',
'contact_no' => isset( $this->args['contact_no'] ) ? $this->get_sanitized_arg( 'contact_no', 'text_field', $this->args['contact_no'] ) : '',
'source' => isset( $this->args['source'] ) ? $this->get_sanitized_arg( 'source', 'text_field', $this->args['source'] ) : '',
'status' => isset( $this->args['status'] ) ? $this->get_sanitized_arg( 'status', 'text_field', $this->args['status'] ) : '',
);
foreach ( $this->args as $key => $value ) {
if ( ! is_numeric( $key ) ) {
continue;
}
$params[ $key ] = $value;
}
$contact = new BWFCRM_Contact( $email, true, $params );
if ( isset( $this->args['tags'] ) ) {
$contact->set_tags( $this->args['tags'], true, false );
}
if ( isset( $this->args['lists'] ) ) {
$contact->set_lists( $this->args['lists'], true, false );
}
$contact->save();
if ( $contact->already_exists ) {
$this->response_code = 422;
return $this->error_response( __( 'Contact already exists', 'wp-marketing-automations' ) );
}
if ( $contact->is_contact_exists() ) {
$this->response_code = 200;
return $this->success_response( $contact->get_array( false, class_exists( 'WooCommerce' ) ), __( 'Contact created', 'wp-marketing-automations' ) );
}
$this->response_code = 500;
return $this->error_response( __( 'Unable to create contact', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Create_Contact' );

View File

@@ -0,0 +1,63 @@
<?php
class BWFAN_API_Delete_Contact_Note extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/notes/(?P<note_id>[\\d]+)';
}
public function default_args_values() {
return array(
'contact_id' => 0,
'note_id' => 0,
);
}
public function process_api_call() {
/** checking if search present in params **/
$contact_id = $this->get_sanitized_arg( 'contact_id', 'text_field' );
if ( empty( $contact_id ) ) {
return $this->error_response( __( 'Contact id is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact instanceof BWFCRM_Contact || 0 === $contact->get_id() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$note_id = intval( $this->get_sanitized_arg( 'note_id', 'key' ) );
$delete_contact_note_result = $contact->delete_notes( $note_id );
if ( ! $delete_contact_note_result ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'Unable to delete the note for contact #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$this->response_code = 200;
$success_message = __( 'Contact notes deleted', 'wp-marketing-automations' );
return $this->success_response( array( 'contact_id' => $contact_id ), $success_message );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Contact_Note' );

View File

@@ -0,0 +1,60 @@
<?php
class BWFAN_API_Delete_Contact extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)';
}
public function default_args_values() {
return array(
'contact_id' => '',
);
}
public function process_api_call() {
/** checking if search present in params **/
$contact_id = intval( $this->get_sanitized_arg( 'contact_id', 'text_field' ) );
if ( empty( $contact_id ) ) {
return $this->error_response( __( 'Contact id is missing.', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact instanceof BWFCRM_Contact || 0 === $contact->get_id() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$delete_contact_result = BWFCRM_Model_Contact::delete_contact( $contact_id );
if ( ! $delete_contact_result ) {
$this->response_code = 404;
return $this->error_response( __( 'Some error occurred during deletion of contact and its data.', 'wp-marketing-automations' ) );
}
$this->response_code = 200;
$success_message = __( 'Contact deleted', 'wp-marketing-automations' );
return $this->success_response( array( 'contact_id' => $contact_id ), $success_message );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Contact' );

View File

@@ -0,0 +1,35 @@
<?php
class BWFAN_API_Delete_Contacts extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/v3/contacts';
}
public function process_api_call() {
$contact_ids = $this->args['contacts'];
if ( empty( $contact_ids ) || ! is_array( $contact_ids ) ) {
return $this->error_response( __( 'Contact ids are missing.', 'wp-marketing-automations' ), null, 500 );
}
BWFCRM_Model_Contact::delete_multiple_contacts( $contact_ids );
$this->response_code = 200;
return $this->success_response( array( 'contacts' => $contact_ids ), __( 'Contacts deleted!', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Delete_Contacts' );

View File

@@ -0,0 +1,95 @@
<?php
class BWFAN_API_Get_Contact_Automations extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public $contact;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/v3/contact/(?P<contact_id>[\\d]+)/automations';
}
public function default_args_values() {
return array(
'contact_id' => 0,
);
}
public function process_api_call() {
/** checking if id or email present in params **/
$contact_id = $this->get_sanitized_arg( 'contact_id', 'key' );
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? absint( $this->get_sanitized_arg( 'offset', 'text_field' ) ) : 0;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
/** contact id missing than return */
if ( empty( $contact_id ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Contact id is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$contact_automations = [];
if ( class_exists( 'BWFAN_Common' ) ) {
$contact_automations = BWFAN_Common::get_automations_for_contact( $contact_id, $limit, $offset );
}
if ( empty( $contact_automations ) ) {
$this->response_code = 200;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact automation found related with contact id : %1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$contact_automations['contacts'] = array_map( function ( $contact ) {
/** Get event name */
$event_slug = BWFAN_Model_Automations::get_event_name( $contact['aid'] );
$event_obj = BWFAN_Core()->sources->get_event( $event_slug );
$event_name = ! empty( $event_obj ) ? $event_obj->get_name() : '';
$automation_meta = BWFAN_Core()->automations->get_automation_data_meta( $contact['aid'] );
$contact['event'] = $event_name;
$contact['automation_title'] = $automation_meta['title'];
return $contact;
}, $contact_automations['contacts'] );
$this->total_count = isset( $contact_automations['total'] ) ? $contact_automations['total'] : 0;
$this->response_code = 200;
$success_message = __( 'Got all contact automations', 'wp-marketing-automations' );
return $this->success_response( $contact_automations, $success_message );
}
public function get_result_total_count() {
return $this->total_count;
}
}
if ( function_exists( 'BWFAN_Core' ) ) {
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contact_Automations' );
}

View File

@@ -0,0 +1,92 @@
<?php
class BWFAN_API_Get_Contact_Funnels extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/funnels';
}
public function default_args_values() {
return array(
'contact_id' => '',
);
}
public function process_api_call() {
/** checking if search present in params **/
$contact_id = $this->get_sanitized_arg( 'contact_id', 'key' );
$offset = $this->get_sanitized_arg( 'offset', 'key' );
$limit = $this->get_sanitized_arg( 'limit', 'key' );
$offset = empty( $offset ) ? $this->pagination->offset : $offset;
$limit = empty( $offset ) ? $this->pagination->limit : $limit;
if ( empty( $contact_id ) ) {
return $this->error_response( __( 'Contact ID is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$contacts_funnel = array();
$contacts_funnel['funnel'] = $contact->get_contact_funnels_array();
if ( function_exists( 'WFACP_Core' ) ) {
$contacts_funnel['funnel']['checkout'] = [];
}
if ( function_exists( 'WFOB_Core' ) ) {
$contacts_funnel['funnel']['order_bump'] = [];
}
if ( function_exists( 'WFOPP_Core' ) ) {
$optin_etries = $contact->get_contact_optin_array();
if ( is_array( $optin_etries ) && ! empty( $optin_etries ) ) {
/** Add email in optin entry */
$optin_etries = array_map( function ( $optin ) {
$entry = json_decode( $optin['entry'], true );
if ( ! empty( $optin['email'] ) && ! empty( $entry ) ) {
$entry['optin_email'] = $optin['email'];
}
$optin['entry'] = wp_json_encode( $entry );
return $optin;
}, $optin_etries );
}
$contacts_funnel['funnel']['optin'] = $optin_etries;
}
if ( function_exists( 'WFOCU_Core' ) ) {
$contacts_funnel['funnel']['upsells'] = [];
}
$this->response_code = 200;
$success_message = __( 'Contacts funnels', 'wp-marketing-automations' );
return $this->success_response( $contacts_funnel, $success_message );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contact_Funnels' );

View File

@@ -0,0 +1,93 @@
<?php
class BWFAN_API_Get_Contact_Lists extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/lists';
$this->pagination->offset = 0;
$this->pagination->limit = 30;
$this->request_args = array(
'search' => array(
'description' => __( 'Search from list name', 'wp-marketing-automations' ),
'type' => 'string',
),
'offset' => array(
'description' => __( 'list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function default_args_values() {
return array(
'contact_id' => '',
);
}
public function process_api_call() {
/** checking if search present in params **/
$contact_id = $this->get_sanitized_arg( 'contact_id', 'text_field' );
$search = $this->get_sanitized_arg( 'search', 'text_field' );
$offset = $this->get_sanitized_arg( 'offset', 'text_field' );
$limit = $this->get_sanitized_arg( 'limit', 'text_field' );
$offset = empty( $offset ) ? $this->pagination->offset : $offset;
$limit = empty( $offset ) ? $this->pagination->limit : $limit;
if ( empty( $contact_id ) ) {
return $this->error_response( __( 'Contact id is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
// $contact_terms_count = BWFCRM_Model_Contact_Terms::get_contact_terms_count( $contact->get_id(), $search, 1 );
// $contact_terms = BWFCRM_Model_Contact_Terms::get_contact_terms( $contact->get_id(), $offset, $limit, $search, 1 );
$contact_terms = $contact->get_all_lists();
if ( empty( $contact_terms ) ) {
$this->response_code = 404;
/* translators: 1: Search term */
$error_message = ! empty( $search ) ? sprintf( __( 'No Searched lists found for contact with name %1$s', 'wp-marketing-automations' ), " '$search'" ) : __( 'No contacts lists found', 'wp-marketing-automations' );
return $this->error_response( $error_message );
}
$this->total_count = count( $contact_terms );
$this->response_code = 200;
/* translators: 1: Search term */
$success_message = ! empty( $search ) ? sprintf( __( 'Searched lists for contact ', 'wp-marketing-automations' ), " '$search'" ) : __( 'Got all contacts lists', 'wp-marketing-automations' );
return $this->success_response( $contact_terms, $success_message );
}
public function get_result_total_count() {
return $this->total_count;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contact_Lists' );

View File

@@ -0,0 +1,72 @@
<?php
class BWFAN_API_Get_Contact_Note extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/notes';
$this->pagination->limit = 25;
$this->pagination->offset = 0;
}
public function default_args_values() {
return array(
'contact_id' => '',
);
}
public function process_api_call() {
/** checking if search present in params **/
$contact_id = $this->get_sanitized_arg( 'contact_id', 'text_field' );
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'key' ) ) ? $this->get_sanitized_arg( 'offset', 'text_field' ) : $this->pagination->offset;
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'key' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : $this->pagination->limit;
if ( empty( $contact_id ) ) {
return $this->error_response( __( 'Contact ID is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$contact_notes = $contact->get_contact_notes_array( $offset, $limit );
if ( empty( $contact_notes ) ) {
$this->response_code = 200;
/* translators: 1: Contact ID */
return $this->success_response( [], sprintf( __( 'No contact notes found related with contact id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$all_contact_notes = $contact->get_contact_notes_array( 0, 0 );
$this->total_count = count( $all_contact_notes );
$this->response_code = 200;
/* translators: 1: Contact ID */
$success_message = sprintf( __( 'Contact notes related to contact id : #%1$d', 'wp-marketing-automations' ), $contact_id );
return $this->success_response( $contact_notes, $success_message );
}
public function get_result_total_count() {
return $this->total_count;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contact_Note' );

View File

@@ -0,0 +1,300 @@
<?php
class BWFAN_API_Get_Contact_Orders extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public $contact;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/orders';
}
public function default_args_values() {
return array(
'contact_id' => 0,
);
}
public function process_api_call() {
/** checking if id or email present in params **/
$contact_id = $this->get_sanitized_arg( 'contact_id' );
/** contact id missing than return */
if ( empty( $contact_id ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact id is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$limit = $this->get_sanitized_arg( 'limit', 'text_field' );
$offset = $this->get_sanitized_arg( 'offset', 'text_field' );
$limit = ! empty( $limit ) ? $limit : 10;
$offset = ! empty( $offset ) ? $offset : 0;
$order_ids = $contact->get_orders( $offset, $limit );
$orders = $this->get_contact_activity_records( $contact_id, $order_ids );
if ( empty( $orders ) ) {
$response = [
'orders' => [],
];
$this->response_code = 200;
/* translators: 1: Contact ID */
return $this->success_response( $response, sprintf( __( 'No contact order found related with contact id : %1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$response = [
'orders' => $orders,
];
$this->total_count = $contact->get_orders_count();
$this->response_code = 200;
$success_message = __( 'Got all contact orders', 'wp-marketing-automations' );
return $this->success_response( $response, $success_message );
}
public function get_contact_activity_records( $cid, $order_ids, $funnel_id = '' ) {
if ( ! is_array( $order_ids ) || count( $order_ids ) === 0 ) {
return [];
}
$all_orders = [];
$sales_data = [];
foreach ( $order_ids as $order_id ) {
$conv_order = $tags = $categories = [];
$product_data = $this->get_single_order_info( $order_id, $cid );
$conv_data = apply_filters( 'wffn_conversion_tracking_data_activity', [], $cid, $order_id );
$conv_order = array_merge( $conv_order, $conv_data );
$conv_order['products'] = $product_data['products'];
/** Get Products Categories name */
if ( empty( $product_data['cat_ids'] ) ) {
$categories = array_map( function ( $term_id ) {
$cat = get_term( absint( $term_id ) );
return $cat instanceof WP_Term ? $cat->name : false;
}, array_unique( $product_data['cat_ids'] ) );
$categories = array_values( array_filter( $categories ) );
}
/** Get Products Tags name */
if ( ! empty( $product_data['tag_ids'] ) ) {
$tags = array_map( function ( $term_id ) {
$tag = get_term( absint( $term_id ) );
return $tag instanceof WP_Term ? $tag->name : false;
}, array_unique( $product_data['tag_ids'] ) );
$tags = array_values( array_filter( $tags ) );
}
$conv_order['tags'] = $tags;
$conv_order['categories'] = $categories;
$sales_data = array_merge( $sales_data, $product_data['timeline_data'] );
$funnel_ids = array_unique( array_column( $conv_order['products'], 'fid' ) );
$conv_order['order_id'] = $order_id;
if ( isset( $conv_order['overview'] ) ) {
unset( $conv_order['overview'] );
}
if ( isset( $conv_order['conversion'] ) ) {
$conv_order['conversion']['funnel_link'] = '';
$conv_order['conversion']['funnel_title'] = '';
$s_funnel_id = ! empty( $funnel_id ) ? $funnel_id : ( ( is_array( $funnel_ids ) && count( $funnel_ids ) > 0 ) ? $funnel_ids[0] : 0 );
$conv_order['conversion']['funnel_id'] = $s_funnel_id;
$get_funnel = new WFFN_Funnel( $s_funnel_id );
if ( $get_funnel instanceof WFFN_Funnel && 0 !== $get_funnel->get_id() ) {
$funnel_link = ( $get_funnel->get_id() === WFFN_Common::get_store_checkout_id() ) ? admin_url( "admin.php?page=bwf&path=/store-checkout" ) : admin_url( "admin.php?page=bwf&path=/funnels/$s_funnel_id" );
$conv_order['conversion']['funnel_link'] = $funnel_link;
$conv_order['conversion']['funnel_title'] = $get_funnel->get_title();
}
}
$conv_order['customer_info'] = array(
'email' => '',
'phone' => '',
'billing_address' => '',
'shipping_address' => '',
);
$conv_order['coupons'] = [];
if ( ! empty( $order_id ) && absint( $order_id ) > 0 && function_exists( 'wc_get_order' ) ) {
$order_data = wc_get_order( $order_id );
if ( $order_data instanceof WC_Order ) {
$conv_order['coupons'] = $order_data->get_coupon_codes();
$conv_order['currency'] = BWFAN_Automations::get_currency( $order_data->get_currency() );
$conv_order['customer_info'] = [
'email' => $order_data->get_billing_email(),
'phone' => $order_data->get_billing_phone(),
'billing_address' => wp_kses_post( $order_data->get_formatted_billing_address() ),
'shipping_address' => wp_kses_post( $order_data->get_formatted_shipping_address() ),
'purchased_on' => ! empty( $order_data->get_date_created() ) ? $order_data->get_date_created()->date( 'Y-m-d H:i:s' ) : $product_data['date_added'],
'payment_method' => $order_data->get_payment_method_title(),
];
$conv_order['status'] = [
'label' => ( 'trash' === $order_data->get_status() ) ? __( 'Trashed', 'wp-marketing-automations' ) : ucwords( wc_get_order_status_name( $order_data->get_status() ) ),
'value' => 'wc-' . $order_data->get_status()
];
}
}
$all_orders[] = $conv_order;
}
return $all_orders;
}
function get_currency( $currency ) {
$currency = ! is_null( $currency ) ? $currency : get_option( 'woocommerce_currency' );
$currency_symbol = get_woocommerce_currency_symbol( $currency );
return [
'code' => $currency,
'precision' => wc_get_price_decimals(),
'symbol' => html_entity_decode( $currency_symbol ),
'symbolPosition' => get_option( 'woocommerce_currency_pos' ),
'decimalSeparator' => wc_get_price_decimal_separator(),
'thousandSeparator' => wc_get_price_thousand_separator(),
'priceFormat' => html_entity_decode( get_woocommerce_price_format() ),
];
}
public function get_single_order_info( $order_id, $cid = '' ) {
$timeline_data = [];
$products = [];
$data = [
'products' => $products,
'date_added' => '',
'timeline_data' => $timeline_data,
'tag_ids' => [],
'cat_ids' => [],
];
$order = wc_get_order( $order_id );
if ( ! $order instanceof \WC_Order ) {
return $data;
}
$items = $order->get_items();
$subtotal = 0;
$i = 0;
foreach ( $items as $item ) {
$product = new stdClass();
$key = 'checkout';
$product->date = '';
/**
* create data for show timeline data
*/
if ( class_exists( 'WFACP_Contacts_Analytics' ) && ! empty( $cid ) && 0 === $i ) {
/*
* show checkout in timeline only one time per order
*/
$aero_obj = WFACP_Contacts_Analytics::get_instance();
$checkout_records = $aero_obj->get_contacts_revenue_records( $cid, $order_id );
if ( is_array( $checkout_records ) && ! isset( $checkout_records['db_error'] ) && isset( $checkout_records[0] ) ) {
$data['date_added'] = $checkout_records[0]->date;
$data['timeline_data'][] = $checkout_records[0];
}
}
if ( 'yes' === $item->get_meta( '_upstroke_purchase' ) ) {
$key = 'upsell';
if ( class_exists( 'WFOCU_Contacts_Analytics' ) ) {
$upsell_obj = WFOCU_Contacts_Analytics::get_instance();
$upsell_records = $upsell_obj->get_contacts_revenue_records( $cid, $order_id );
if ( is_array( $upsell_records ) && ! isset( $upsell_records['db_error'] ) && isset( $upsell_records[0] ) ) {
$data['date_added'] = empty( $data['date_added'] ) ? $upsell_records[0]->date : $data['date_added'];
$data['timeline_data'][] = $upsell_records[0];
}
}
}
if ( 'yes' === $item->get_meta( '_bump_purchase' ) ) {
$key = 'bump';
if ( class_exists( 'WFOB_Contacts_Analytics' ) ) {
$bump_obj = WFOB_Contacts_Analytics::get_instance();
$bump_records = $bump_obj->get_contacts_revenue_records( $cid, $order_id );
if ( is_array( $bump_records ) && ! isset( $bump_records['db_error'] ) && isset( $bump_records[0] ) ) {
$data['date_added'] = empty( $data['date_added'] ) ? $bump_records[0]->date : $data['date_added'];
$data['timeline_data'][] = $bump_records[0];
}
}
}
$sub_total = $item->get_subtotal();
$product->name = $item->get_name();
$product->revenue = $sub_total;
$product->type = $key;
$data['products'][] = $product;
$subtotal += $sub_total;
$i ++;
/** Fetch tags and categories */
$item_data = $item->get_data();
$wc_product = intval( $item_data['product_id'] ) ? wc_get_product( $item_data['product_id'] ) : '';
$tag_ids = $wc_product instanceof WC_Product ? $wc_product->get_tag_ids() : [];
$cat_ids = $wc_product instanceof WC_Product ? $wc_product->get_category_ids() : [];
$data['tag_ids'] = array_merge( $data['tag_ids'], $tag_ids );
$data['cat_ids'] = array_merge( $data['cat_ids'], $cat_ids );
}
$order_total = $order->get_total();
$total_discount = $order->get_total_discount();
$remaining_amount = $order_total - ( $subtotal - $total_discount );
if ( $remaining_amount > 0 ) {
$shipping_tax = new stdClass();
$shipping_tax->name = __( 'Including shipping and taxes ,other costs', 'wp-marketing-automations' );
$shipping_tax->revenue = round( $remaining_amount, 2 );
$shipping_tax->type = 'shipping';
$data['products'][] = $shipping_tax;
}
if ( $order->get_total_discount() > 0 ) {
$discount = new stdClass();
$discount->name = __( 'Discount', 'wp-marketing-automations' );
$discount->revenue = $order->get_total_discount();
$discount->type = 'discount';
$data['products'][] = $discount;
}
return $data;
}
public function get_result_total_count() {
return $this->total_count;
}
}
if ( class_exists( 'woocommerce' ) ) {
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contact_Orders' );
}

View File

@@ -0,0 +1,79 @@
<?php
class BWFAN_API_Get_Contact_Tags extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/tags';
$this->pagination->offset = 0;
$this->pagination->limit = 30;
$this->request_args = array(
'search' => array(
'description' => __( 'Search from tag name', 'wp-marketing-automations' ),
'type' => 'string',
),
'offset' => array(
'description' => __( 'Tags list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function default_args_values() {
return array(
'contact_id' => '',
);
}
public function process_api_call() {
/** checking if search present in params **/
$contact_id = $this->get_sanitized_arg( 'contact_id', 'text_field' );
$search = $this->get_sanitized_arg( 'search', 'text_field' );
$offset = $this->get_sanitized_arg( 'offset', 'text_field' );
$limit = $this->get_sanitized_arg( 'limit', 'text_field' );
$offset = empty( $offset ) ? $this->pagination->offset : $offset;
$limit = empty( $offset ) ? $this->pagination->limit : $limit;
if ( empty( $contact_id ) ) {
return $this->error_response( __( 'Contact ID is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( _sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
// $contact_terms = BWFCRM_Model_Contact_Terms::get_contact_terms( $contact->get_id(), $offset, $limit, $search, 0 );
$contact_terms = $contact->get_all_tags();
$this->response_code = 200;
return $this->success_response( $contact_terms );
}
public function get_result_total_count() {
return $this->total_count;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contact_Tags' );

View File

@@ -0,0 +1,57 @@
<?php
class BWFAN_API_Get_Contact extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $contact;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)';
$this->request_args = array(
'contact_id' => array(
'description' => __( 'Contact ID to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function default_args_values() {
return array(
'contact_id' => 0,
);
}
public function process_api_call() {
/** checking if id or email present in params **/
$id = $this->get_sanitized_arg( 'contact_id', 'key' );
$contact = new BWFCRM_Contact( $id );
if ( $contact->is_contact_exists() ) {
try {
$data = $contact->get_array( false, true, true, true, true );
} catch ( Error $e ) {
$message = $e->getMessage();
$this->response_code = 404;
return $this->error_response( $message );
}
return $this->success_response( $data );
}
$this->response_code = 404;
return $this->error_response( __( "No contact found with given id #", 'wp-marketing-automations' ) . $id );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contact' );

View File

@@ -0,0 +1,144 @@
<?php
class BWFAN_API_Get_Contacts extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $total_count = 0;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/v3/contacts';
$this->pagination->offset = 0;
$this->pagination->limit = 10;
$this->request_args = array(
'search' => array(
'description' => __( 'Search from email, first_name or last_name', 'wp-marketing-automations' ),
'type' => 'string',
),
'offset' => array(
'description' => __( 'Contacts list Offset', 'wp-marketing-automations' ),
'type' => 'integer',
),
'limit' => array(
'description' => __( 'Per page limit', 'wp-marketing-automations' ),
'type' => 'integer',
),
'get_wc' => array(
'description' => __( 'Get WC Data as well', 'wp-marketing-automations' ),
'type' => 'boolean',
),
'grab_totals' => array(
'description' => __( 'Grab total contact count as well', 'wp-marketing-automations' ),
'type' => 'boolean',
),
'start_indexing' => array(
'description' => __( 'Start Indexing of Contacts in case indexing is pending', 'wp-marketing-automations' ),
'type' => 'boolean',
),
);
}
public function default_args_values() {
return array(
'search' => '',
'filters' => array(),
);
}
public function process_api_call() {
$additional_info = array(
'grab_totals' => $this->get_sanitized_arg( 'grab_totals', 'bool' ),
'only_count' => $this->get_sanitized_arg( 'only_count', 'bool' ),
'fetch_base' => $this->get_sanitized_arg( 'fetch_base', 'text_field' ),
'exclude_unsubs' => $this->get_sanitized_arg( 'exclude_unsubs', 'bool' ),
'exclude_unsubs_lists' => $this->get_sanitized_arg( 'exclude_unsubs_lists', 'bool' ),
'grab_custom_fields' => $this->get_sanitized_arg( 'grab_custom_fields', 'bool' ),
'include_soft_bounce' => $this->get_sanitized_arg( 'includeSoftBounce', 'bool' ),
'include_unverified' => $this->get_sanitized_arg( 'includeUnverified', 'bool' ),
);
/** Un-Open contacts case */
$un_open_broadcast = $this->get_sanitized_arg( 'unopen_broadcast', 'key' );
if ( ! empty( $un_open_broadcast ) ) {
return $this->get_unopened_broadcast_contacts( $un_open_broadcast, $additional_info );
}
$order = $this->get_sanitized_arg( 'order', 'text_field' );
$order_by = $this->get_sanitized_arg( 'order_by', 'text_field' );
if ( ! empty( $order ) && ! empty( $order_by ) ) {
$additional_info['order'] = $order;
$additional_info['order_by'] = $order_by;
}
if ( class_exists( 'WooCommerce' ) ) {
$additional_info['customer_data'] = $this->get_sanitized_arg( 'get_wc', 'bool' );
}
/** checking if search present in params */
$search = $this->get_sanitized_arg( 'search', 'text_field' );
$filters_collection = empty( $this->args['filters'] ) ? array() : $this->args['filters'];
if ( false === $additional_info['exclude_unsubs'] ) {
$additional_info['exclude_unsubs'] = apply_filters( 'bwfan_force_exclude_unsubscribe_contact', false );
}
$contacts = BWFCRM_Contact::get_contacts( $search, $this->pagination->offset, $this->pagination->limit, $filters_collection, $additional_info );
if ( ! is_array( $contacts ) ) {
$this->response_code = 500;
return $this->error_response( is_string( $contacts ) ? $contacts : __( 'Unknown error occurred', 'wp-marketing-automations' ) );
}
if ( isset( $contacts['total_count'] ) ) {
$this->total_count = absint( $contacts['total_count'] );
}
if ( ! isset( $contacts['contacts'] ) || empty( $contacts['contacts'] ) ) {
return $this->success_response( array() );
}
$this->response_code = 200;
return $this->success_response( $contacts['contacts'] );
}
public function get_result_total_count() {
return $this->total_count;
}
public function get_unopened_broadcast_contacts( $broadcast_id, $additional_info ) {
$additional_info['offset'] = $this->pagination->offset;
$additional_info['limit'] = $this->pagination->limit;
$contacts = BWFCRM_Core()->campaigns->get_unopen_broadcast_contacts( absint( $broadcast_id ), $additional_info );
if ( ! is_array( $contacts ) ) {
$this->response_code = 500;
return $this->error_response( is_string( $contacts ) ? $contacts : __( 'Unknown error occurred', 'wp-marketing-automations' ) );
}
if ( isset( $contacts['total_count'] ) ) {
$this->total_count = absint( $contacts['total_count'] );
}
if ( ! isset( $contacts['contacts'] ) || empty( $contacts['contacts'] ) ) {
return $this->success_response( array() );
}
$this->response_code = 200;
return $this->success_response( $contacts['contacts'] );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contacts' );

View File

@@ -0,0 +1,80 @@
<?php
class BWFAN_API_Remove_Lists extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/lists';
}
public function default_args_values() {
return array(
'contact_id' => 0,
'lists' => '',
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'key' );
$lists = $this->get_sanitized_arg( '', 'key', $this->args['lists'] );
/** No Lists Provided */
if ( empty( $lists ) ) {
$response = __( 'No Lists provided', 'wp-marketing-automations' );
$this->response_code = 404;
return $this->error_response( $response );
}
/** No Contact found */
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
/** Check if provided lists ids are valid numbers */
$lists = array_map( 'absint', $lists );
$lists = array_filter( $lists );
if ( empty( absint( $lists ) ) ) {
$this->response_code = 400;
return $this->error_response( __( 'No list found', 'wp-marketing-automations' ) );
}
$removed_lists = $contact->remove_lists( $lists );
$contact->save();
$lists_not_removed = array_diff( $lists, $removed_lists );
if ( count( $lists_not_removed ) === count( $lists ) ) {
$response = __( 'Unable to remove the list', 'wp-marketing-automations' );
$this->response_code = 500;
return $this->error_response( $response );
}
$result = [];
$response = __( 'Lists Unassigned', 'wp-marketing-automations' );
if ( ! empty( $lists_not_removed ) ) {
$removed_lists_text = implode( ', ', $removed_lists );
/* translators: 1: comma seperated list */
$response = sprintf( __( 'Some lists removed: %1$s', 'wp-marketing-automations' ), $removed_lists_text );
}
$result['last_modified'] = $contact->contact->get_last_modified();
return $this->success_response( $result, $response );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Remove_Lists' );

View File

@@ -0,0 +1,99 @@
<?php
class BWFAN_API_Remove_Tags extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::DELETABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/tags';
$this->request_args = array(
'tags' => array(
'description' => __( 'Tags to remove', 'wp-marketing-automations' ),
'type' => 'array',
),
);
}
public function default_args_values() {
return array(
'contact_id' => 0,
'tags' => '',
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'key' );
$tags = $this->get_sanitized_arg( '', 'key', $this->args['tags'] );
/** No Tags Provided */
if ( empty( $tags ) ) {
$response = __( 'No Tags provided', 'wp-marketing-automations' );
$this->response_code = 404;
return $this->error_response( $response );
}
/** No Contact found */
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
/** If provided tags not array */
if ( ! is_array( $tags ) ) {
if ( is_string( $tags ) && false !== strpos( $tags, ',' ) ) {
$tags = explode( ',', $tags );
} else if ( empty( absint( $tags ) ) ) {
$this->response_code = 400;
return $this->error_response( __( 'No tag found', 'wp-marketing-automations' ) );
} else {
$tags = array( absint( $tags ) );
}
}
/** Check if provided tag ids are valid numbers */
$tags = array_map( 'absint', $tags );
$tags = array_filter( $tags );
if ( empty( absint( $tags ) ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Invalid Tags Provided', 'wp-marketing-automations' ) );
}
$removed_tags = $contact->remove_tags( $tags );
$contact->save();
$tags_not_removed = array_diff( $tags, $removed_tags );
if ( count( $tags_not_removed ) === count( $tags ) ) {
$response = __( 'Unable to remove any tag', 'wp-marketing-automations' );
$this->response_code = 500;
return $this->error_response( $response );
}
$result = [];
$response = __( 'Tag removed', 'wp-marketing-automations' );
if ( ! empty( $tags_not_removed ) ) {
$removed_tags_text = implode( ', ', $removed_tags );
/* translators: 1: comma seperated tags */
$response = sprintf( __( 'Some tags removed: %1$s', 'wp-marketing-automations' ), $removed_tags_text );
}
$result['last_modified'] = $contact->contact->get_last_modified();
return $this->success_response( $result, $response );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Remove_Tags' );

View File

@@ -0,0 +1,393 @@
<?php
class BWFAN_API_SendMessage extends BWFAN_API_Base {
public static $ins;
private $conversation = null;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::CREATABLE;
$this->route = 'v3/contacts/(?P<contact_id>[\\d]+)/sendmessage';
$this->request_args = array(
'contact_id' => array(
'description' => __( 'Contact ID to send message', 'wp-marketing-automations' ),
'type' => 'integer',
)
);
}
public function default_args_values() {
return array(
'contact_id' => '',
'title' => '',
'message' => '',
'type' => 'email'
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'text_field' );
$title = $this->get_sanitized_arg( 'title', 'text_field' );
$message = $this->args['message'];
$type = $this->get_sanitized_arg( 'type', 'text_field' );
if ( BWFAN_Common::is_pro_3_0() ) {
$this->conversation = BWFAN_Core()->conversation;
} else {
$this->conversation = BWFCRM_Core()->conversation;
}
$contact = new BWFCRM_Contact( absint( $contact_id ) );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 400;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
/** Check if contacts status is bounced */
if ( 4 === $contact->get_display_status() ) {
$this->response_code = 400;
return $this->error_response( __( 'Contact status is bounced', 'wp-marketing-automations' ) );
}
if ( 'email' === $type && empty( $title ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Title is mandatory', 'wp-marketing-automations' ) );
}
if ( empty( $message ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Message is mandatory', 'wp-marketing-automations' ) );
}
$author_id = get_current_user_id();
$mode = BWFAN_Email_Conversations::$MODE_EMAIL;
$this->conversation->engagement_type = BWFAN_Email_Conversations::$TYPE_EMAIL;
switch ( $type ) {
case 'sms':
$mode = BWFAN_Email_Conversations::$MODE_SMS;
$this->conversation->engagement_type = BWFAN_Email_Conversations::$TYPE_SMS;
break;
case 'whatsapp':
$mode = BWFAN_Email_Conversations::$MODE_WHATSAPP;
break;
case 'push-notification':
$mode = BWFAN_Email_Conversations::$MODE_NOTIFICATION;
break;
}
BWFAN_Merge_Tag_Loader::set_data( array(
'contact_id' => $contact->get_id(),
'contact_mode' => $mode
) );
$message = BWFAN_Common::decode_merge_tags( ( 1 === $mode ? wpautop( $message ) : $message ) );
$template = [
'subject' => ( empty( $title ) ? '' : $title ),
'template' => ( empty( $message ) ? '' : $message )
];
$message_data = [];
if ( BWFAN_Email_Conversations::$MODE_NOTIFICATION === $mode ) {
$url = $this->get_sanitized_arg( 'url', 'text_field' );
if ( ! empty( $url ) ) {
$template['url'] = $url;
$message_data['notification_url'] = $url;
}
$image_url = $this->get_sanitized_arg( 'image_url', 'text_field' );
if ( ! empty( $image_url ) ) {
$template['image_url'] = $image_url;
$message_data['notification_image'] = $image_url;
}
}
$conversation = $this->conversation->create_campaign_conversation( $contact, 0, 0, $author_id, $mode, true, $template, BWFAN_Email_Conversations::$MODE_NOTIFICATION === $mode ? 8 : 0 );
if ( empty( $conversation['conversation_id'] ) ) {
return $this->error_response( $conversation );
}
if ( class_exists( 'BWFAN_Message' ) ) {
$message_obj = new BWFAN_Message();
$message_obj->set_message( 0, $conversation['conversation_id'], $title, $message );
$message_obj->set_data( $message_data );
$messageid = $message_obj->save();
if ( $messageid && property_exists( BWFAN_Core()->conversation, 'template_id' ) ) {
/** Save the message id in conversation */
BWFAN_Core()->conversation->template_id = $messageid;
}
}
$conversation['template'] = $message;
$conversation['subject'] = $title;
if ( 'sms' === $type ) {
$sent = $this->send_single_sms( $conversation, $contact );
} else if ( 'whatsapp' === $type ) {
$sent = $this->send_single_whatsapp_message( $conversation, $contact );
} else if ( 'push-notification' === $type ) {
$sent = $this->send_single_push_notification( $conversation, $contact, $template );
} else {
$sent = $this->send_single_email( $title, $conversation, $contact );
}
if ( true === $sent ) {
return $this->success_response( [], __( 'Message sent', 'wp-marketing-automations' ) );
}
return $this->error_response( $sent, null, 500 );
}
public function send_single_push_notification( $conversation, $contact, $template ) {
if ( ! bwfan_is_autonami_pro_active() ) {
return __( 'FunnelKit automations Pro is not active ', 'wp-marketing-automations' );
}
$contact_id = $contact->contact->get_id();
$notification_message = $this->conversation->prepare_notification_data( $conversation['conversation_id'], $contact_id, $conversation['hash_code'], $conversation['template'] );
/** Append tracking code in url */
$notification_url = $this->conversation->prepare_notification_data( $conversation['conversation_id'], $contact_id, $conversation['hash_code'], $template['url'] );
$data = [
'contact_id' => $contact_id,
'notification_message' => $notification_message,
'notification_url' => $notification_url,
'notification_title' => $template['subject'],
'notification_image' => isset( $template['image_url'] ) ? $template['image_url'] : '',
];
$res = $this->send_notification( $data );
if ( $res instanceof WP_Error ) {
$this->conversation->fail_the_conversation( $conversation['conversation_id'], $res->get_error_message() );
return $res->get_error_message();
}
/** Save the date of last sent engagement **/
$data = array( 'cid' => $contact_id );
$this->conversation::save_last_sent_engagement( $data );
$this->conversation->update_conversation_status( $conversation['conversation_id'], BWFAN_Email_Conversations::$STATUS_SEND );
return true;
}
/**
* @param $title
* @param $conversation
* @param $contact BWFCRM_Contact
*
* @return string|true|null
*/
public function send_single_email( $title, $conversation, $contact ) {
$conversation_id = $conversation['conversation_id'];
$contact_id = $contact->contact->get_id();
$email_subject = $this->prepare_email_subject( $title, $contact_id );
try {
$email_body = $this->conversation->prepare_email_body( $conversation['conversation_id'], $contact_id, $conversation['hash_code'], 'rich', $conversation['template'] );
} catch ( Error $e ) {
$this->conversation->fail_the_conversation( $conversation_id, $e->getMessage() );
return $e->getMessage();
}
if ( is_wp_error( $email_body ) ) {
$this->conversation->fail_the_conversation( $conversation_id, $email_body->get_error_message() );
return $email_body->get_error_message();
}
$to = $contact->contact->get_email();
if ( ! is_email( $to ) ) {
$message = __( 'No email found for this contact: #', 'wp-marketing-automations' ) . $contact_id;
$this->conversation->fail_the_conversation( $conversation_id, $message );
return $message;
}
$global_email_settings = BWFAN_Common::get_global_settings();
$headers = array(
'MIME-Version: 1.0',
'Content-type: text/html;charset=UTF-8'
);
$from = '';
if ( isset( $global_email_settings['bwfan_email_from_name'] ) && ! empty( $global_email_settings['bwfan_email_from_name'] ) ) {
$from = 'From: ' . $global_email_settings['bwfan_email_from_name'];
}
if ( isset( $global_email_settings['bwfan_email_from'] ) && ! empty( $global_email_settings['bwfan_email_from'] ) ) {
$headers[] = $from . ' <' . $global_email_settings['bwfan_email_from'] . '>';
}
if ( isset( $global_email_settings['bwfan_email_reply_to'] ) && ! empty( $global_email_settings['bwfan_email_reply_to'] ) ) {
$headers[] = 'Reply-To: ' . $global_email_settings['bwfan_email_reply_to'];
}
/** Set unsubscribe link in header */
$unsubscribe_link = BWFAN_Common::get_unsubscribe_link( [ 'uid' => $contact->contact->get_uid() ] );
if ( ! empty( $unsubscribe_link ) ) {
$headers[] = "List-Unsubscribe: <$unsubscribe_link>";
$headers[] = "List-Unsubscribe-Post: List-Unsubscribe=One-Click";
}
$headers = apply_filters( 'bwfan_email_headers', $headers );
BWFAN_Common::bwf_remove_filter_before_wp_mail();
$result = wp_mail( $to, $email_subject, $email_body, $headers );
if ( true === $result ) {
/** Save the time of last sent engagement **/
$data = array( 'cid' => $contact_id );
$this->conversation::save_last_sent_engagement( $data );
$this->conversation->update_conversation_status( $conversation_id, BWFAN_Email_Conversations::$STATUS_SEND );
} else {
$this->conversation->fail_the_conversation( $conversation_id, __( 'Email not sent', 'wp-marketing-automations' ) );
return __( 'Email not sent', 'wp-marketing-automations' );
}
return $result;
}
public function send_single_sms( $conversation, $contact ) {
if ( ! bwfan_is_autonami_pro_active() ) {
return __( 'FunnelKit automations Pro is not active ', 'wp-marketing-automations' );
}
$conversation_id = $conversation['conversation_id'];
$contact_id = $contact->contact->get_id();
$sms_body = $this->conversation->prepare_sms_body( $conversation['conversation_id'], $contact_id, $conversation['hash_code'], $conversation['template'] );
if ( is_wp_error( $sms_body ) ) {
$this->conversation->fail_the_conversation( $conversation_id, $sms_body->get_error_message() );
return $sms_body->get_error_message();
}
$to = BWFAN_Common::get_contact_full_number( $contact->contact );
if ( empty( $to ) ) {
$message = __( 'No phone number found for this contact: #', 'wp-marketing-automations' ) . $contact_id;
$this->conversation->fail_the_conversation( $conversation_id, $message );
return $message;
}
$send_sms_result = BWFCRM_Common::send_sms( array(
'to' => $to,
'body' => $sms_body,
) );
if ( $send_sms_result instanceof WP_Error ) {
$this->conversation->fail_the_conversation( $conversation_id, $send_sms_result->get_error_message() );
return $send_sms_result->get_error_message();
}
/** Save the date of last sent engagement **/
$data = array( 'cid' => $contact_id );
$this->conversation::save_last_sent_engagement( $data );
$this->conversation->update_conversation_status( $conversation_id, BWFAN_Email_Conversations::$STATUS_SEND );
return true;
}
public function send_single_whatsapp_message( $conversation, $contact ) {
if ( ! bwfan_is_autonami_pro_active() ) {
return __( 'FunnelKit automations Pro is not active ', 'wp-marketing-automations' );
}
$conversation_id = $conversation['conversation_id'];
$contact_id = $contact->contact->get_id();
$message_body = $this->conversation->prepare_sms_body( $conversation['conversation_id'], $contact_id, $conversation['hash_code'], $conversation['template'] );
if ( is_wp_error( $message_body ) ) {
$this->conversation->fail_the_conversation( $conversation_id, $message_body->get_error_message() );
return $message_body->get_error_message();
}
$to = BWFAN_Common::get_contact_full_number( $contact->contact );
if ( empty( $to ) ) {
$message = __( 'No phone number found for this contact: #', 'wp-marketing-automations' ) . $contact_id;
$this->conversation->fail_the_conversation( $conversation_id, $message );
return $message;
}
$response = $this->conversation->send_whatsapp_message( $to, array(
array(
'type' => 'text',
'data' => $message_body,
)
) );
if ( ! empty( $response['status'] ) && $response['status'] == true ) {
/** Save the time of last sent engagement **/
$data = array( 'cid' => $contact_id );
$this->conversation::save_last_sent_engagement( $data );
$this->conversation->update_conversation_status( $conversation_id, BWFAN_Email_Conversations::$STATUS_SEND );
return true;
}
$error_message = isset( $response['msg'] ) && ! empty( $response['msg'] ) ? $response['msg'] : __( 'Unable to send message', 'wp-marketing-automations' );
$this->conversation->fail_the_conversation( $conversation_id, $error_message );
return $error_message;
}
public function prepare_email_subject( $subject, $contact_id ) {
BWFAN_Merge_Tag_Loader::set_data( array(
'contact_id' => $contact_id,
'contact_mode' => 1
) );
return BWFAN_Common::decode_merge_tags( $subject );
}
public function send_notification( $args ) {
// fetching provider for test sms
$provider = isset( $args['push_provider'] ) ? $args['push_provider'] : '';
if ( empty( $provider ) ) {
$global_settings = BWFAN_Common::get_global_settings();
$provider = isset( $global_settings['bwfan_push_service'] ) && ! empty( $global_settings['bwfan_push_service'] ) ? $global_settings['bwfan_push_service'] : BWFAN_Common::get_default_push_provider();
}
/** If no provider selected OR selected provider is not within active providers, then select default provider */
if ( empty( $provider ) || ! isset( $active_services[ $provider ] ) ) {
$provider = BWFAN_Common::get_default_push_provider();
}
$provider = explode( 'bwfco_', $provider );
$provider = isset( $provider[1] ) ? $provider[1] : '';
$provider = ! empty( $provider ) ? BWFAN_Core()->integration->get_integration( $provider ) : '';
if ( ! $provider instanceof BWFAN_Integration || ! method_exists( $provider, 'send_message' ) ) {
return new WP_Error( 'connector_not_found', __( 'Connector Integration not found', 'wp-marketing-automations' ) );
}
return $provider->send_message( $args );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_SendMessage' );

View File

@@ -0,0 +1,74 @@
<?php
class BWFAN_API_Update_Contact_Note extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)/notes/(?P<note_id>[\\d]+)';
}
public function default_args_values() {
return array(
'contact_id' => 0,
'note_id' => 0,
'notes' => array(),
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'key' );
if ( empty( $contact_id ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact ID is mandatory', 'wp-marketing-automations' ) );
}
$note_id = $this->get_sanitized_arg( 'note_id', 'key' );
if ( empty( $note_id ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Note ID is mandatory', 'wp-marketing-automations' ) );
}
$notes = $this->args['notes'];
if ( empty( $notes ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Notes are mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$note_updated = $contact->update_contact_note( $notes, $note_id );
if ( false === $note_updated || is_wp_error( $note_updated ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Unable to update note of contact', 'wp-marketing-automations' ) );
}
$this->response_code = 200;
return $this->success_response( [], __( 'Contact note updated', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Update_Contact_Note' );

View File

@@ -0,0 +1,113 @@
<?php
/**
* Class BWFAN_API_Update_Contact_Order for syncing contact order data
*/
class BWFAN_API_Update_Contact_Order extends BWFAN_API_Base {
public static $ins;
/**
* To create an instance of the current class
* @return self
*/
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* setting up the data
*/
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/contact/(?P<contact_id>[\\d]+)/resync-order/';
$this->response_code = 200;
}
/**
* setting up the default id for the contact
* @return int[]
*/
public function default_args_values() {
return array(
'contact_id' => 0,
);
}
/**
* this will update the order details of the particular contact based on the contact_id
*
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
*/
public function process_api_call() {
$cid = $this->get_sanitized_arg( 'contact_id', 'key' );
//Check contact id is founded
if ( empty( $cid ) ) {
$this->error_response( __( 'Contact ID no found', 'wp-marketing-automations' ), '', 404 );
}
// Check if contact exist
$contact = new BWFCRM_Contact( $cid );
if ( ! $contact->is_contact_exists() ) {
return $this->error_response( __( 'No Contact found', 'wp-marketing-automations' ), '', 404 );
}
// Check for dependencies
if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'as_has_scheduled_action' ) ) {
return $this->error_response( __( 'Dependencies Missing', 'wp-marketing-automations' ), '', 404 );
}
$args = [ 'cid' => $cid ];
$hook = 'bwf_reindex_contact_orders';
// If already scheduled
if ( as_has_scheduled_action( $hook, $args, 'funnelkit' ) ) {
return $this->success_response( [], __( 'Action already scheduled for the user', 'wp-marketing-automations' ) );
}
// Start the indexing for 10 seconds
$start_time = time();
$ins = WooFunnels_DB_Updater::get_instance();
do {
$response = $ins->bwf_reindex_contact_orders( $cid );
if ( 1 === intval( $response ) || false === $response ) {
break;
}
} while ( ( time() - $start_time ) < 10 );
switch ( $response ) {
case 0:
case '0': // If not completed in 10 seconds, schedule the action
$schedule_id = as_schedule_recurring_action( time(), 60, $hook, $args, 'funnelkit' );
// Unable to schedule action
if ( ! $schedule_id ) {
return $this->error_response( __( 'Unable to schedule action for syncing.', 'wp-marketing-automations' ), '', 404 );
}
return $this->success_response( [
'schedule_id' => $schedule_id,
], __( 'Syncing order has been started, data will be update in a while.', 'wp-marketing-automations' ) );
case 1:
case '1': // Completed successfully
break;
default: // Unknown error
return $this->error_response( __( 'Unknown error occurred.', 'wp-marketing-automations' ), '', 404 );
}
// Otherwise, return success response with contact data
$new_contact = $contact->get_customer_as_array();
return $this->success_response( [
'wc' => $new_contact,
], __( 'Contact orders synced successfully', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Update_Contact_Order' );

View File

@@ -0,0 +1,126 @@
<?php
class BWFAN_API_Update_Contact extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::EDITABLE;
$this->route = '/v3/contacts/(?P<contact_id>[\\d]+)';
}
public function default_args_values() {
return array(
'contact_id' => 0,
'first_name' => '',
'last_name' => '',
'meta' => [],
'lists' => [],
'tags' => [],
);
}
public function process_api_call() {
$contact_id = $this->get_sanitized_arg( 'contact_id', 'key' );
if ( empty( $contact_id ) ) {
$this->response_code = 404;
return $this->error_response( __( 'Contact ID is mandatory', 'wp-marketing-automations' ) );
}
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $contact_id ) );
}
$contact_basic_info = array();
$contact_first_name = $this->get_sanitized_arg( 'first_name', 'text_field' );
$contact_last_name = $this->get_sanitized_arg( 'last_name', 'text_field' );
$contact_email = $this->get_sanitized_arg( 'email', 'text_field' );
if ( ! empty( $contact_first_name ) ) {
$contact_basic_info['first_name'] = $contact_first_name;
}
if ( ! empty( $contact_last_name ) ) {
$contact_basic_info['last_name'] = $contact_last_name;
}
if ( ! empty( $contact_email ) ) {
$contact_basic_info['email'] = $contact_email;
}
if ( ! empty( $this->args['meta'] ) && is_array( $this->args['meta'] ) ) {
$contact_basic_info['meta'] = $this->args['meta'];
}
if ( empty( $contact_basic_info ) && empty( $this->args['lists'] ) && empty( $this->args['tags'] ) ) {
$this->response_code = 400;
return $this->error_response( __( 'Unable to update contact as update data missing', 'wp-marketing-automations' ) );
}
if ( ! empty( $contact_basic_info ) ) {
$contact_basic_info['id'] = $contact_id;
/** @var $update_contact_details */
$contact_field_updated = $contact->update( $contact_basic_info );
}
if ( ! empty( $this->args['lists'] ) && is_array( $this->args['lists'] ) ) {
$lists = $this->args['lists'];
$added_lists = $contact->set_lists( $lists );
$lists_added = array_map( function ( $list ) {
return $list->get_array();
}, $added_lists );
}
if ( ! empty( $this->args['tags'] ) && is_array( $this->args['tags'] ) ) {
$tags = $this->args['tags'];
$added_tags = $contact->set_tags( $tags );
$tags_added = array_map( function ( $tags ) {
return $tags->get_array();
}, $added_tags );
}
$contact->save();
if ( false === $contact_field_updated && empty( $added_lists ) && empty( $added_tags ) ) {
$this->response_code = 200;
return $this->error_response( __( 'Unable to update contact fields', 'wp-marketing-automations' ) );
}
$result = [];
if ( ! empty( $contact_field_updated ) ) {
$result['contact'] = $contact_field_updated;
}
if ( ! empty( $lists_added ) ) {
$result['lists'] = $lists_added;
}
if ( ! empty( $tags_added ) ) {
$result['tags'] = $tags_added;
}
return $this->success_response( $result, __( 'Contact updated', 'wp-marketing-automations' ) );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Update_Contact' );

View File

@@ -0,0 +1,61 @@
<?php
class BWFAN_API_Get_Contact_Conversation extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $contact;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = 'v3/contacts/(?P<contact_id>[\\d]+)/engagement/(?P<engagement_id>[\\d]+)';
$this->request_args = array(
'contact_id' => array(
'description' => __( 'Contact ID', 'wp-marketing-automations' ),
'type' => 'integer',
),
'engagement_id' => array(
'description' => __( 'Engagement ID', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function default_args_values() {
return array(
'contact_id' => 0,
);
}
public function process_api_call() {
/** checking if id or email present in params */
$id = $this->get_sanitized_arg( 'contact_id', 'key' );
$engagement_id = $this->get_sanitized_arg( 'engagement_id', 'key' );
if ( ! class_exists( 'BWFAN_Email_Conversations' ) || ! isset( BWFAN_Core()->conversations ) || ! BWFAN_Core()->conversations instanceof BWFAN_Email_Conversations ) {
return $this->error_response( __( 'Unable to find Funnelkit Automations message tracking module', 'wp-marketing-automations' ), null, 500 );
}
$this->contact = new BWFCRM_Contact( absint( $id ) );
if ( ! $this->contact instanceof BWFCRM_Contact || ! $this->contact->is_contact_exists() ) {
/* translators: 1: Contact ID */
return $this->error_response( sprintf( __( 'No contact found with given id #%1$d', 'wp-marketing-automations' ), $id ), null, 500 );
}
$conversation = $this->contact->get_conversation_email( $engagement_id );
if ( $conversation instanceof WP_Error || ( is_array( $conversation ) && isset( $conversation['error'] ) ) ) {
return $this->error_response( ! is_array( $conversation ) ? __( 'Unknown error occurred', 'wp-marketing-automations' ) : $conversation['error'], $conversation instanceof WP_Error ? $conversation : null, 500 );
}
return $this->success_response( $conversation );
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contact_Conversation' );

View File

@@ -0,0 +1,66 @@
<?php
class BWFAN_API_Get_Contact_Conversations extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $contact;
public $mode = null;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = 'v3/contacts/(?P<contact_id>[\\d]+)/engagements';
$this->request_args = array(
'contact_id' => array(
'description' => __( 'Contact ID for which engagements to retrieve', 'wp-marketing-automations' ),
'type' => 'integer',
),
);
}
public function default_args_values() {
return array(
'contact_id' => 0,
'mode' => 0,
);
}
public function process_api_call() {
/** checking if id or email present in params **/
$id = $this->get_sanitized_arg( 'contact_id', 'key' );
$this->mode = $this->get_sanitized_arg( 'mode', 'key' );
$limit = ! empty( $this->get_sanitized_arg( 'limit', 'text_field' ) ) ? $this->get_sanitized_arg( 'limit', 'text_field' ) : 25;
$offset = ! empty( $this->get_sanitized_arg( 'offset', 'text_field' ) ) ? $this->get_sanitized_arg( 'offset', 'text_field' ) : 0;
if ( ! class_exists( 'BWFAN_Email_Conversations' ) || ! isset( BWFAN_Core()->conversations ) || ! BWFAN_Core()->conversations instanceof BWFAN_Email_Conversations ) {
return $this->error_response( __( 'Unable to find conversations module', 'wp-marketing-automations' ), null, 500 );
}
$this->contact = new BWFCRM_Contact( absint( $id ) );
if ( ! $this->contact instanceof BWFCRM_Contact || ! $this->contact->is_contact_exists() ) {
return $this->error_response( __( 'Contact doesn\'t exists', 'wp-marketing-automations' ), null, 500 );
}
$conversation = $this->contact->get_conversations( $this->mode, $offset, $limit );
if ( $conversation instanceof WP_Error ) {
return $this->error_response( '', $conversation, 500 );
}
return $this->success_response( $conversation );
}
public function get_result_total_count() {
return $this->contact instanceof BWFCRM_Contact && $this->contact->is_contact_exists() ? $this->contact->get_conversations_total( $this->mode ) : 0;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Contact_Conversations' );

View File

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

View File

@@ -0,0 +1,58 @@
<?php
class BWFAN_API_Get_Autonami_Analytics extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $contact;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/dashboard/autonami-analytics';
}
public function default_args_values() {
return array();
}
public function process_api_call() {
$response['totals'] = $this->prepare_item_for_response();
$this->response_code = 200;
return $this->success_response( $response );
}
/**
*
* @return array
*/
public function prepare_item_for_response() {
$get_total_contacts = BWFAN_Dashboards::get_total_contacts();
$get_total_sents = BWFAN_Dashboards::get_total_engagement_sents( '', '', '', '' );
$get_total_orders = BWFAN_Dashboards::get_total_orders( '', '', '', '' );
$result = [
'total_contact' => intval( $get_total_contacts ),
'email_sents' => ! isset( $get_total_sents[0]['email_sents'] ) ? 0 : $get_total_sents[0]['email_sents'],
'sms_sent' => ! isset( $get_total_sents[0]['sms_sent'] ) ? 0 : $get_total_sents[0]['sms_sent'],
'total_orders' => ! isset( $get_total_orders[0]['total_orders'] ) ? 0 : $get_total_orders[0]['total_orders'],
'total_revenue' => ! isset( $get_total_orders[0]['total_revenue'] ) ? 0 : $get_total_orders[0]['total_revenue'],
];
return $result;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Autonami_Analytics' );

View File

@@ -0,0 +1,250 @@
<?php
class BWFAN_API_Get_Dashboard_Data extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $contact;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/dashboard';
}
public function default_args_values() {
return array();
}
public function process_api_call() {
$response = $this->prepare_item_for_response();
$this->response_code = 200;
return $this->success_response( $response );
}
/**
* @return array
*/
public function prepare_item_for_response() {
$force = filter_input( INPUT_GET, 'force' );
$force = ( 'false' === $force ) ? false : true;
$new_data = [];
/** Check if worker call is late */
$last_run = bwf_options_get( 'fk_core_worker_let' );
if ( '' !== $last_run && ( ( time() - $last_run ) > BWFAN_Common::get_worker_delay_timestamp() ) ) {
/** Worker is running late */
$new_data['worker_delayed'] = time() - $last_run;
}
/** Check basic worker last run time and status code check */
$resp = BWFAN_Common::validate_core_worker( $force );
if ( isset( $resp['response_code'] ) ) {
$new_data['response_code'] = $resp['response_code'];
}
/** Dashboard call */
$lite_key = 'bwfan_dashboard_report_lite';
$pro_key = 'bwfan_dashboard_report_pro';
$exp = BWFAN_Common::get_admin_analytics_cache_lifespan();
if ( 'false' === $force ) {
/** Check for cached data */
if ( ! bwfan_is_autonami_pro_active() ) {
/** Lite version active */
$data = get_transient( $lite_key );
if ( ! empty( $data ) ) {
return array_merge( $data, $new_data );
}
} else {
/** Pro version active */
$data = get_transient( $pro_key );
if ( ! empty( $data ) ) {
return array_merge( $data, $new_data );
}
}
}
$recovered_carts = [];
$recent_abandoned = [];
$lost_carts = [];
if ( function_exists( 'bwfan_is_woocommerce_active' ) && bwfan_is_woocommerce_active() ) {
$recovered_carts = BWFAN_Recoverable_Carts::get_recovered_carts( '', 0, 5 );
$recovered_carts = isset( $recovered_carts['items'] ) ? $this->get_recovered( $recovered_carts['items'] ) : [];
$recent_abandoned = BWFAN_Automations::get_recent_abandoned();
$lost_carts = BWFAN_Automations::get_recent_abandoned( 2 );
}
$unsubsribers = BWFAN_Dashboards::get_recent_unsubsribers();
$unsubsribers = array_map( function ( $unsubscribe ) {
$unsubscribe['type'] = 'unsubscribe';
return $unsubscribe;
}, $unsubsribers );
$new_contacts = BWFAN_Dashboards::get_recent_contacts();
$new_contacts = array_map( function ( $contact ) {
$contact['type'] = 'contact';
return $contact;
}, $new_contacts );
$recent_activities = array_merge( $new_contacts, $unsubsribers );
$recovered_carts = array_map( function ( $data ) {
$data['type'] = 1;
return $data;
}, ( array ) $recovered_carts );
$recent_abandoned = array_map( function ( $data ) {
$data['type'] = 2;
return $data;
}, ( array ) $recent_abandoned );
$lost_carts = array_map( function ( $data ) {
$data['type'] = 3;
return $data;
}, ( array ) $lost_carts );
$carts = array_merge( $recovered_carts, $recent_abandoned, $lost_carts );
uasort( $carts, function ( $a, $b ) {
return $a['created_on'] >= $b['created_on'] ? - 1 : 1;
} );
$carts = array_values( $carts );
$carts = count( $carts ) > 5 ? array_slice( $carts, 0, 5 ) : $carts;
$data = [
'carts' => $carts,
];
$data = array_merge( $data, $new_data );
$additional_info = [
'grab_totals' => true,
'only_count' => true
];
$contacts_count = BWFCRM_Contact::get_contacts( '', 0, 0, [], $additional_info );
$get_total_sents = BWFAN_Dashboards::get_total_engagement_sents( '', '', '', '' );
$get_total_orders = BWFAN_Dashboards::get_total_orders( '', '', '', '' );
$analytics_data = [
'total_contact' => ! isset( $contacts_count['total_count'] ) ? 0 : $contacts_count['total_count'],
'email_sents' => ! isset( $get_total_sents[0]['email_sents'] ) ? 0 : $get_total_sents[0]['email_sents'],
'sms_sent' => ! isset( $get_total_sents[0]['sms_sent'] ) ? 0 : $get_total_sents[0]['sms_sent'],
'total_orders' => ! isset( $get_total_orders[0]['total_orders'] ) ? 0 : $get_total_orders[0]['total_orders'],
'total_revenue' => ! isset( $get_total_orders[0]['total_revenue'] ) ? 0 : $get_total_orders[0]['total_revenue'],
];
$top_automations = BWFCRM_Automations::get_top_automations();
if ( ! bwfan_is_autonami_pro_active() ) {
uasort( $recent_activities, function ( $a, $b ) {
return $a['creation_date'] >= $b['creation_date'] ? - 1 : 1;
} );
$recent_activities = array_values( $recent_activities );
$recent_activities = count( $recent_activities ) > 10 ? array_slice( $recent_activities, 0, 9 ) : $recent_activities;
$data = array_merge( $data, [
'pro_active' => false,
'analytics_data' => $analytics_data,
'top_automations' => $top_automations['top_automations'],
'top_broadcast' => [],
'recent_activities' => $recent_activities,
] );
set_transient( $lite_key, $data, $exp );
BWFAN_Common::validate_scheduled_recurring_actions();
return $data;
}
$top_broadcast = BWFCRM_Campaigns::get_top_broadcast();
$top_broadcast_sms = BWFCRM_Campaigns::get_top_broadcast( 2 );
$recent_conversions = BWFAN_Dashboards::get_recent_conversions();
$recent_conversions = array_map( function ( $conversion ) {
$conversion['type'] = 'conversion';
return $conversion;
}, $recent_conversions );
$recent_activities = array_merge( $recent_activities, $recent_conversions );
uasort( $recent_activities, function ( $a, $b ) {
return $a['creation_date'] >= $b['creation_date'] ? - 1 : 1;
} );
$recent_activities = array_values( $recent_activities );
$recent_activities = count( $recent_activities ) > 10 ? array_slice( $recent_activities, 0, 9 ) : $recent_activities;
$data = array_merge( $data, [
'pro_active' => true,
'analytics_data' => $analytics_data,
'top_automations' => $top_automations['top_automations'],
'top_broadcast' => $top_broadcast['top_broadcast'],
'top_broadcast_sms' => $top_broadcast_sms['top_broadcast'],
'recent_activities' => $recent_activities,
] );
set_transient( $pro_key, $data, $exp );
BWFAN_Common::validate_scheduled_recurring_actions();
return $data;
}
public function get_recovered( $recovered_carts ) {
if ( empty( $recovered_carts ) ) {
return [];
}
$result = [];
foreach ( $recovered_carts as $item ) {
if ( ! $item instanceof WC_Order ) {
continue;
}
$order_date = $item->get_date_created();
$result[] = [
'order_id' => $item->get_id(),
'f_name' => $item->get_billing_first_name(),
'l_name' => $item->get_billing_last_name(),
'email' => $item->get_billing_email(),
'created_on' => ( $order_date instanceof WC_DateTime ) ? ( $order_date->date( 'Y-m-d H:i:s' ) ) : '',
'revenue' => $item->get_total(),
'currency' => BWFAN_Automations::get_currency( $item->get_currency() ),
'id' => $item->get_meta( '_woofunnel_cid' ),
];
}
return $result;
}
public function get_full_name( $item ) {
if ( ! $item instanceof WC_Order ) {
return '';
}
$buyer = '';
if ( $item->get_billing_first_name() || $item->get_billing_last_name() ) {
/* translators: 1: first name 2: last name */
$buyer = trim( sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $item->get_billing_first_name(), $item->get_billing_last_name() ) ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
} elseif ( $item->get_billing_company() ) {
$buyer = trim( $item->get_billing_company() );
} elseif ( $item->get_customer_id() ) {
$user = get_user_by( 'id', $item->get_customer_id() );
$buyer = ucwords( $user->display_name );
}
return apply_filters( 'woocommerce_admin_order_buyer_name', $buyer, $item );
}
public function get_items( $item ) {
$names = [];
foreach ( $item->get_items() as $value ) {
$names[] = $value->get_name();
}
return $names;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Dashboard_Data' );

View File

@@ -0,0 +1,54 @@
<?php
class BWFAN_API_Get_Recent_Abandoned extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $contact;
public $total_count;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/dashboard/recent-contacts-abandoned';
$this->pagination->limit = 10;
$this->pagination->offset = 0;
}
public function default_args_values() {
}
public function process_api_call() {
global $wpdb;
$abandoned_table = $wpdb->prefix . 'bwfan_abandonedcarts';
$contact_table = $wpdb->prefix . 'bwf_contact';
$query = "SELECT abandon.email,abandon.checkout_data, abandon.total as revenue, COALESCE(con.id, 0) as id, COALESCE(con.f_name, '') as f_name, COALESCE(con.l_name, '') as l_name from $abandoned_table as abandon LEFT JOIN $contact_table as con ON abandon.email = con.email ORDER BY abandon.ID DESC LIMIT 5 OFFSET 0";
$abandoned = $wpdb->get_results( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( ! is_array( $abandoned ) ) {
$this->response_code = 500;
return $this->error_response( is_string( $abandoned ) ? $abandoned : __( 'Unknown error', 'wp-marketing-automations' ) );
}
$this->response_code = 200;
$this->total_count = count( $abandoned );
return $this->success_response( $abandoned );
}
public function get_result_total_count() {
return $this->total_count;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Recent_Abandoned' );

View File

@@ -0,0 +1,68 @@
<?php
class BWFAN_API_Get_Recent_Contacts_Unsubscribe extends BWFAN_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public $contact;
public $total_count;
public function __construct() {
parent::__construct();
$this->method = WP_REST_Server::READABLE;
$this->route = '/dashboard/recent-contacts-unsubscribe';
$this->pagination->limit = 5;
$this->pagination->offset = 0;
}
/**
* API Call
*
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
*/
public function process_api_call() {
global $wpdb;
$contact_table = $wpdb->prefix . 'bwf_contact';
$unsubscribe_table = $wpdb->prefix . 'bwfan_message_unsubscribe';
$query = "SELECT sub.recipient as email, COALESCE(con.id, 0) as id, COALESCE(con.f_name, '') as f_name, COALESCE(con.l_name, '') as l_name, sub.c_date from $unsubscribe_table as sub LEFT JOIN $contact_table as con ON sub.recipient = con.email ORDER BY sub.ID DESC LIMIT 5 OFFSET 0";
$unsubscribes = $wpdb->get_results( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$contacts['contacts'] = $unsubscribes;
if ( ! is_array( $contacts ) ) {
$this->response_code = 500;
return $this->error_response( is_string( $contacts ) ? $contacts : __( 'Unknown Error', 'wp-marketing-automations' ) );
}
if ( ! isset( $contacts['contacts'] ) || empty( $contacts['contacts'] ) ) {
return $this->success_response( [] );
}
$this->response_code = 200;
$this->total_count = count( $contacts['contacts'] );
return $this->success_response( $contacts['contacts'] );
}
/**
* Get total counts
*
* @return int
*/
public function get_result_total_count() {
return $this->total_count;
}
}
BWFAN_API_Loader::register( 'BWFAN_API_Get_Recent_Contacts_Unsubscribe' );

Some files were not shown because too many files have changed in this diff Show More