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

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,469 @@
<?php
/**
* Class BWFAN_AJAX_Controller
* Handles All the request came from front end or the backend
*/
abstract class BWFAN_AJAX_Controller {
public static function init() {
/**
* Run on front end backend
*/
add_action( 'wp_ajax_bwfan_update_automation', array( __CLASS__, 'update_automation' ) );
add_action( 'wp_ajax_bwfan_toggle_automation_state', array( __CLASS__, 'toggle_automation_state' ) );
add_action( 'wp_ajax_bwfan_select2ajax', array( __CLASS__, 'bwfan_select2ajax' ) );
add_action( 'wp_ajax_bwfan_show_email_preview', array( __CLASS__, 'bwfan_save_temporary_preview_data' ) );
add_action( 'wp_ajax_bwfan_test_email', array( __CLASS__, 'test_email' ) );
add_action( 'wp_ajax_bwfan_test_sms', array( __CLASS__, 'test_sms' ) );
add_action( 'wp_ajax_bwfan_automation_submit', array( __CLASS__, 'handle_automation_post_submit' ) );
}
public static function bwfan_select2ajax() {
$callback = apply_filters( 'bwfan_select2_ajax_callable', '', $_POST ); //phpcs:ignore WordPress.Security.NonceVerification
if ( ! is_callable( $callback ) ) {
wp_send_json( [] );
}
$items = call_user_func( $callback, sanitize_text_field( $_POST['search_term']['term'] ) );//phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput
wp_send_json( $items );
}
/**
* Runs when an automation is saved from single automation screen.
* @throws Exception
*/
public static function handle_automation_post_submit() {
BWFAN_Common::check_nonce();
//phpcs:disable WordPress.Security.NonceVerification
if ( ! isset( $_POST['automation_id'] ) && empty( $_POST['automation_id'] ) ) { //phpcs:ignore WordPress.Security.ValidatedSanitizedInput
return;
}
$automation_id = $_POST['automation_id']; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$a_track_id = ( isset( $_POST['a_track_id'] ) && ! empty( $_POST['a_track_id'] ) ) ? $_POST['a_track_id'] : 0; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$t_to_delete = ( isset( $_POST['t_to_delete'] ) && ! empty( $_POST['t_to_delete'] ) ) ? stripslashes( $_POST['t_to_delete'] ) : null; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput
//make sure following is in array just like following
$data = stripslashes( $_POST['data'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$data = json_decode( $data, true );
$actions = ( isset( $data['actions'] ) && is_array( $data['actions'] ) ) ? $data['actions'] : [];
/** Make actions array if not */
if ( ! array( $actions ) ) {
$actions = [];
}
foreach ( $actions as $group_id => $action_data ) {
if ( null === $action_data ) {
continue;
}
$actions[ $group_id ] = BWFAN_Common::remove_back_slash_from_automation( $action_data );
}
$actions = BWFAN_Common::sort_actions( $actions );
/** Validate action data before save - unset temp_action_slug as of no use */
$actions = BWFAN_Common::validate_action_date_before_save( $actions );
$ui = stripslashes( $_POST['ui'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$ui = json_decode( $ui, true );
$uiData = stripslashes( $_POST['uiData'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$uiData = json_decode( $uiData, true );
$automation_data = [];
$automation_data['event'] = $data['trigger']['event'];
$automation_data['source'] = $data['trigger']['source'];
$where = [];
$where['ID'] = $automation_id;
BWFAN_Model_Automations::update( $automation_data, $where );
BWFAN_Core()->automations->set_automation_data( 'event', $data['trigger']['event'] );
BWFAN_Core()->automations->set_automation_data( 'source', $data['trigger']['source'] );
$automation_meta_data = [];
$automation_meta_data['condition'] = [];
if ( isset( $data['condition'] ) ) {
$automation_meta_data['condition'] = $data['condition'];
}
$automation_meta_data['actions'] = $actions;
$automation_meta_data['event_meta'] = ( isset( $data['trigger']['event_meta'] ) ) ? $data['trigger']['event_meta'] : [];
$automation_meta_data['ui'] = $ui;
$automation_meta_data['uiData'] = $uiData;
$automation_meta_data['a_track_id'] = $a_track_id;
$automation_meta = BWFAN_Model_Automationmeta::get_automation_meta( $automation_id );
$db_a_track_id = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'a_track_id' );
/** For saving subject of send email action of automation meta for tracking purpose */
do_action( 'bwfan_automation_email_tracking_post_data', $automation_id, $automation_meta, $automation_meta_data['actions'], $db_a_track_id );
/** Update automation meta */
foreach ( $automation_meta_data as $meta_key => $meta_value ) {
$meta_value = maybe_serialize( $meta_value );
BWFAN_Core()->automations->set_automation_data( $meta_key, $meta_value );
$where = [];
$update_data = [
'bwfan_automation_id' => $automation_id,
'meta_key' => $meta_key,
'meta_value' => $meta_value,
];
if ( array_key_exists( $meta_key, $automation_meta ) ) {
// Update Meta
$where['bwfan_automation_id'] = $automation_id;
$where['meta_key'] = $meta_key;
}
if ( count( $where ) > 0 ) {
BWFAN_Model_Automationmeta::update( $update_data, $where );
} else {
BWFAN_Model_Automationmeta::insert( $update_data );
}
}
/** Update the modified date of automation */
$meta_data = array(
'meta_value' => current_time( 'mysql', 1 ),
);
$where = array(
'bwfan_automation_id' => $automation_id,
'meta_key' => 'm_date',
);
BWFAN_Model_Automationmeta::update( $meta_data, $where );
BWFAN_Core()->automations->set_automation_data( 'm_date', $meta_data['meta_value'] );
BWFAN_Core()->automations->set_automation_data( 'run_count', isset( $automation_meta['run_count'] ) ? $automation_meta['run_count'] : 0 );
// Update requires_update key to 0 on update which implies that user has verified and saved the automation
$meta_data = array(
'meta_value' => 0,
);
$where = array(
'bwfan_automation_id' => $automation_id,
'meta_key' => 'requires_update',
);
BWFAN_Model_Automationmeta::update( $meta_data, $where );
BWFAN_Core()->automations->set_automation_data( 'requires_update', 0 );
BWFAN_Core()->automations->set_automation_id( $automation_id );
do_action( 'bwfan_automation_saved', $automation_id );
// Send async call to delete all the tasks except for completed tasks (actually logs)
if ( ! is_null( $t_to_delete ) ) {
$url = rest_url( '/autonami/v1/delete-tasks' );
$body_data = array(
'automation_id' => $automation_id,
'a_track_id' => $db_a_track_id,
't_to_delete' => $t_to_delete,
'unique_key' => get_option( 'bwfan_u_key', false ),
);
$args = bwf_get_remote_rest_args( $body_data );
wp_remote_post( $url, $args );
}
$resp = array(
'id' => $automation_id,
'status' => true,
'msg' => __( 'Automation Updated', 'wp-marketing-automations' ),
);
wp_send_json( $resp );
//phpcs:enable WordPress.Security.NonceVerification
}
/**
* Runs when the title of the automation is updated from single automation screen.
*/
public static function update_automation() {
BWFAN_Common::check_nonce();
$resp = array(
'msg' => 'automation not found',
'status' => false,
);
if ( ! isset( $_POST['automation_id'] ) || empty( $_POST['automation_id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification
wp_send_json( $resp );
}
$automation_id = sanitize_text_field( $_POST['automation_id'] ); //phpcs:ignore WordPress.Security.NonceVerification
$where = [
'bwfan_automation_id' => $automation_id,
];
$meta = array();
$where['meta_key'] = 'm_date';
$meta['meta_key'] = 'm_date';
$meta['meta_value'] = current_time( 'mysql', 1 );
BWFAN_Model_Automationmeta::update( $meta, $where );
$where['meta_key'] = 'title';
$meta['meta_key'] = 'title';
$meta['meta_value'] = sanitize_text_field( stripslashes( $_POST['title'] ) ); //phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput
BWFAN_Model_Automationmeta::update( $meta, $where );
$resp['msg'] = __( 'Automation Successfully Updated', 'wp-marketing-automations' );
$resp['status'] = true;
$resp['automation_name'] = sanitize_text_field( stripslashes( $_POST['title'] ) ); //phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput
BWFAN_Core()->automations->set_automation_id( $automation_id );
do_action( 'bwfan_automation_saved', $automation_id );
wp_send_json( $resp );
}
/**
* Runs when automation is activated/deactivated
*/
public static function toggle_automation_state() {
BWFAN_Common::check_nonce();
$resp = array(
'msg' => '',
'status' => true,
);
// phpcs:disable WordPress.Security.NonceVerification
if ( empty( $_POST['id'] ) ) {
$resp = array(
'msg' => 'Automation ID is missing',
'status' => false,
);
wp_send_json( $resp );
}
$automation_id = sanitize_text_field( $_POST['id'] );
$automation = array();
$automation['status'] = 2;
if ( isset( $_POST['state'] ) && 'true' === $_POST['state'] ) {
$automation['status'] = 1;
}
BWFAN_Core()->automations->toggle_state( $automation_id, $automation );
//phpcs:enable WordPress.Security.NonceVerification
wp_send_json( $resp );
}
public static function is_wfocu_front_ajax() {
if ( defined( 'DOING_AJAX' ) && true === DOING_AJAX && null !== filter_input( INPUT_POST, 'action' ) && false !== strpos( filter_input( INPUT_POST, 'action' ), 'wfocu_front' ) ) {
return true;
}
return false;
}
/**
* Runs when `preview` option is clicked in email action. It temporarily saved data in options table.
*/
public static function bwfan_save_temporary_preview_data() {
BWFAN_Common::check_nonce();
//phpcs:disable WordPress.Security.NonceVerification
$automation_id = sanitize_text_field( $_POST['automation_id'] );
if ( absint( $automation_id ) < 1 ) {
wp_send_json( array(
'status' => false,
) );
}
$post = $_POST;
$post['data']['to'] = stripslashes( sanitize_text_field( $post['data']['to'] ) );
$post['data']['subject'] = stripslashes( sanitize_text_field( $post['data']['subject'] ) );
$post['data']['body'] = stripslashes( $post['data']['body'] );//phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$post['data']['body_raw'] = stripslashes( $post['data']['body_raw'] );//phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$meta = array();
$meta['meta_key'] = 'email_preview';
$meta['meta_value'] = maybe_serialize( $post );
$current_data = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'email_preview' );
if ( false === $current_data ) {
$meta['bwfan_automation_id'] = $automation_id;
BWFAN_Model_Automationmeta::insert( $meta );
} else {
$where = [
'bwfan_automation_id' => $automation_id,
'meta_key' => 'email_preview',
];
BWFAN_Model_Automationmeta::update( $meta, $where );
}
//phpcs:enable WordPress.Security.NonceVerification
wp_send_json( array(
'status' => true,
) );
}
public static function test_email() {
BWFAN_Common::check_nonce();
// phpcs:disable WordPress.Security.NonceVerification
/** Check if user has manage_options permission */
if ( ! current_user_can( BWFAN_admin::menu_cap() ) ) {
wp_send_json( array(
'status' => false,
'msg' => __( 'You are not authorized to perform this action', 'wp-marketing-automations' ),
) );
}
$result = array(
'status' => false,
'msg' => __( 'Error', 'wp-marketing-automations' ),
);
if ( ! isset( $_POST['email'] ) || ! filter_var( $_POST['email'], FILTER_VALIDATE_EMAIL ) ) {
$result['msg'] = __( 'Email not valid', 'wp-marketing-automations' );
wp_send_json( $result );
}
$post = $_POST;
$automation_id = sanitize_text_field( $post['automation_id'] );
if ( absint( $automation_id ) < 1 ) {
$result['msg'] = __( 'Automation ID missing', 'wp-marketing-automations' );
$result['status'] = false;
wp_send_json( $result );
}
$post['data']['to'] = sanitize_email( $post['email'] );
$post['data']['subject'] = isset( $post['data']['subject'] ) && ! empty( $post['data']['subject'] ) ? stripslashes( $post['data']['subject'] ) : __( 'This is a fake subject line, enter subject to fix it', 'wp-marketing-automations' );
$post['data']['preheading'] = isset( $post['data']['preheading'] ) ? stripslashes( $post['data']['preheading'] ) : '';
$post['data']['body'] = stripslashes( $post['data']['body'] );//phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$post['data']['body_raw'] = stripslashes( $post['data']['body_raw'] );//phpcs:ignore WordPress.Security.ValidatedSanitizedInput
if ( isset( $post['data']['editor'] ) ) {
$post['data']['editor']['body'] = stripslashes( $post['data']['editor']['body'] );//phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$post['data']['editor']['design'] = stripslashes( $post['data']['editor']['design'] );//phpcs:ignore WordPress.Security.ValidatedSanitizedInput
}
$meta = array();
$meta['meta_key'] = 'email_preview';
$meta['meta_value'] = maybe_serialize( $post );
$current_data = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'email_preview' );
if ( false === $current_data ) {
$meta['bwfan_automation_id'] = $automation_id;
BWFAN_Model_Automationmeta::insert( $meta );
} else {
$where = [
'bwfan_automation_id' => $automation_id,
'meta_key' => 'email_preview',
];
BWFAN_Model_Automationmeta::update( $meta, $where );
}
BWFAN_Merge_Tag_Loader::set_data( array(
'is_preview' => true,
'test_email' => $post['email'],
) );
$post['event_data']['event_slug'] = $post['event'];
$action_object = BWFAN_Core()->integration->get_action( 'wp_sendemail' );
$action_object->is_preview = true;
$data_to_set = $action_object->make_data( '', $post );
$data_to_set['test'] = true;
$action_object->set_data( $data_to_set );
$response = $action_object->send_email();
if ( true === $response ) {
$result['msg'] = __( 'Test email sent.', 'wp-marketing-automations' );
$result['status'] = true;
} elseif ( is_array( $response ) && isset( $response['message'] ) ) {
$result['msg'] = $response['message'];
$result['status'] = false;
} else {
$result['msg'] = __( 'Server does not support email facility', 'wp-marketing-automations' );
$result['status'] = false;
}
//phpcs:enable WordPress.Security.NonceVerification
wp_send_json( $result );
}
public static function test_sms() {
BWFAN_Common::check_nonce();
/** If FKA pro is not active */
if ( ! bwfan_is_autonami_pro_active() ) {
return array(
'status' => false,
'msg' => __( 'FunnelKit Automations Pro is not active', 'wp-marketing-automations' ),
);
}
// phpcs:disable WordPress.Security.NonceVerification
$result = array(
'status' => false,
'msg' => __( 'Error', 'wp-marketing-automations' ),
);
if ( ! isset( $_POST['data']['sms_to'] ) && ! isset( $_POST['test_sms_to'] ) ) {
$result['msg'] = __( 'Phone number can\'t be blank', 'wp-marketing-automations' );
wp_send_json( $result );
}
$post = $_POST;
$mediaUrl = '';
if ( isset( $post['v'] ) && 2 === absint( $post['v'] ) ) {
$sms_to = $post['test_sms_to'];
$sms_body = isset( $post['sms_body'] ) ? stripslashes( $post['sms_body'] ) : '';
$sms_provider = isset( $post['sms_provider'] ) ? $post['sms_provider'] : '';
} else {
$sms_to = $post['data']['sms_to'];
$sms_body = isset( $post['data']['sms_body'] ) ? stripslashes( $post['data']['sms_body'] ) : '';
$sms_provider = isset( $post['data']['sms_provider'] ) ? $post['data']['sms_provider'] : '';
}
/** Append UTM parameters */
if ( class_exists( 'BWFAN_UTM_Tracking' ) ) {
$utm = BWFAN_UTM_Tracking::get_instance();
if ( version_compare( BWFAN_PRO_VERSION, '2.0.4', '>' ) ) {
$sms_body = $utm->maybe_add_utm_parameters( $sms_body, $post, 'sms' );
} else {
$sms_body = $utm->maybe_add_utm_parameters( $sms_body, $post );
}
}
/** Media handling */
if ( ! empty( $post['data']['attach_custom_img'] ) ) {
$img = stripslashes( $post['data']['attach_custom_img'] );
$img = json_decode( $img, true );
if ( is_array( $img ) && count( $img ) > 0 ) {
$mediaUrl = $img[0];
}
}
// is_preview set to true for merge tag before sending data for sms;
BWFAN_Merge_Tag_Loader::set_data( array(
'is_preview' => true,
) );
$sms_body = BWFAN_Common::decode_merge_tags( $sms_body );
$send_sms_result = BWFCRM_Common::send_sms( array(
'to' => $sms_to,
'body' => $sms_body,
'is_test' => true,
'image_url' => $mediaUrl,
'sms_provider' => $sms_provider
) );
if ( $send_sms_result instanceof WP_Error ) {
wp_send_json( array(
'status' => false,
'msg' => $send_sms_result->get_error_message(),
) );
}
$message = __( 'SMS sent successfully', 'wp-marketing-automations' );
wp_send_json( array(
'status' => true,
'msg' => $message,
) );
}
}
BWFAN_AJAX_Controller::init();

View File

@@ -0,0 +1,213 @@
<?php
#[AllowDynamicProperties]
abstract class BWFAN_API_Base {
/**
* @var string $route
*/
public $route = null;
/**
* @var string $method
*/
public $method = null;
/**
* @var stdClass $pagination
*
* It contains two keys: Limit and Offset, for pagination purposes
*/
public $pagination = null;
public $response_code = 200;
public $args = array();
public $request_args = array();
public $public_api = false;
public function __construct() {
$this->pagination = new stdClass();
$this->pagination->limit = 0;
$this->pagination->offset = 0;
}
public function api_call( WP_REST_Request $request ) {
BWFAN_Common::nocache_headers();
$params = WP_REST_Server::EDITABLE === $this->method ? $request->get_params() : false;
if ( false === $params ) {
$query_params = $request->get_query_params();
$query_params = is_array( $query_params ) ? $query_params : array();
$request_params = $request->get_params();
$request_params = is_array( $request_params ) ? $request_params : array();
$params = array_replace( $query_params, $request_params );
}
if ( isset( $params['content'] ) && ! empty( $params['content'] ) && is_string( $params['content'] ) ) {
$replace_array = BWFAN_Common::get_mail_replace_string();
if ( ! empty( $replace_array ) ) {
$params['content'] = str_replace( array_values( $replace_array ), array_keys( $replace_array ), $params['content'] );
}
}
$params['files'] = $request->get_file_params();
$this->pagination->limit = ! empty( $params['limit'] ) ? absint( $params['limit'] ) : $this->pagination->limit;
$this->pagination->offset = ! empty( $params['offset'] ) ? absint( $params['offset'] ) : 0;
$this->args = wp_parse_args( $params, $this->default_args_values() );
try {
return $this->process_api_call();
} catch ( Error $e ) {
$this->response_code = 500;
return $this->error_response( $e->getMessage() );
}
}
public function default_args_values() {
return array();
}
/** To be implemented in Child Class. Override in Child Class */
public function get_result_total_count() {
return 0;
}
/** To set count data */
public function get_result_count_data() {
return 0;
}
public function error_response( $message = '', $wp_error = null, $code = 0 ) {
if ( 0 !== absint( $code ) ) {
$this->response_code = $code;
}
$data = array();
if ( $wp_error instanceof WP_Error ) {
$message = $wp_error->get_error_message();
$data = $wp_error->get_error_data();
}
return new WP_Error( $this->response_code, $message, array( 'status' => $this->response_code, 'error_data' => $data ) );
}
public function error_response_200( $message = '', $wp_error = null, $code = 0 ) {
if ( 0 !== absint( $code ) ) {
$this->response_code = $code;
} else if ( empty( $this->response_code ) ) {
$this->response_code = 500;
}
$data = array();
if ( $wp_error instanceof WP_Error ) {
$message = $wp_error->get_error_message();
$data = $wp_error->get_error_data();
}
return new WP_Error( $this->response_code, $message, array( 'status' => 200, 'error_data' => $data ) );
}
public function success_response( $result_array, $message = '' ) {
$response = BWFAN_Common::format_success_response( $result_array, $message, $this->response_code );
/** Total Count */
$total_count = $this->get_result_total_count();
if ( ! empty( $total_count ) ) {
$response['total_count'] = $total_count;
}
/** Count Data */
$count_data = $this->get_result_count_data();
if ( ! empty( $count_data ) ) {
$response['count_data'] = $count_data;
}
/** Pagination */
if ( isset( $this->pagination->limit ) && ( 0 === $this->pagination->limit || ! empty( $this->pagination->limit ) ) ) {
$response['limit'] = absint( $this->pagination->limit );
}
if ( isset( $this->pagination->offset ) && ( 0 === $this->pagination->offset || ! empty( $this->pagination->offset ) ) ) {
$response['offset'] = absint( $this->pagination->offset );
}
return rest_ensure_response( $response );
}
/**
* @param string $key
* @param string $is_a
* @param string $collection
*
* @return bool|array|mixed
*/
public function get_sanitized_arg( $key = '', $is_a = 'key', $collection = '' ) {
$sanitize_method = ( 'bool' === $is_a ? 'rest_sanitize_boolean' : 'sanitize_' . $is_a );
if ( ! is_array( $collection ) ) {
$collection = $this->args;
}
if ( ! empty( $key ) && isset( $collection[ $key ] ) && ! empty( $collection[ $key ] ) ) {
return call_user_func( $sanitize_method, $collection[ $key ] );
}
if ( ! empty( $key ) ) {
return false;
}
return array_map( $sanitize_method, $collection );
}
/**
* rest api permission callback
*
* @return bool
*/
public function rest_permission_callback( WP_REST_Request $request ) {
$permissions = BWFAN_Common::access_capabilities();
foreach ( $permissions as $permission ) {
if ( current_user_can( $permission ) ) {
return true;
}
}
return false;
}
abstract public function process_api_call();
/**
* @param $id_key
* @param $email_key
*
* @return BWFCRM_Contact|WP_Error
*/
public function get_contact_by_id_or_email( $id_key, $email_key ) {
$email = $this->get_sanitized_arg( $email_key, 'text_field' );
$id = $this->get_sanitized_arg( $id_key, 'text_field' );
$id_or_email = ( is_numeric( $id ) && absint( $id ) > 0 ? absint( $id ) : ( is_email( $email ) ? $email : '' ) );
$contact = new BWFCRM_Contact( $id_or_email, true );
if ( ! $contact->is_contact_exists() ) {
$this->response_code = 404;
if ( is_numeric( $id_or_email ) ) {
$response = __( 'Unable to get contact with contact ID : ', 'wp-marketing-automations' ) . $id_or_email;
} else {
$response = __( 'Contact not exists with email: ', 'wp-marketing-automations' ) . $id_or_email;
}
return $this->error_response( $response );
}
return $contact;
}
}

View File

@@ -0,0 +1,76 @@
<?php
#[AllowDynamicProperties]
abstract class BWFAN_Base_Step_Controller {
public $automation_data = array();
public $step_data = array();
public $action_data = array();
public $automation_contact_id = 0;
public $automation_id = 0;
public $contact_id = 0;
public $step_id = 0;
public $attempts = 0;
public $max_attempts = 0;
public $reattempt_time = 0;
public function __construct() {
$this->max_attempts = apply_filters( 'bwfan_automation_contact_max_attempts', 3 );
$this->reattempt_time = apply_filters( 'bwfan_automation_contact_reattempt_time', 30 );
}
public function populate_automation_contact_data( $db_aContact = array() ) {
if ( ! empty( $db_aContact ) && is_array( $db_aContact ) && isset( $db_aContact['ID'] ) && isset( $db_aContact['data'] ) ) {
$aContact = $db_aContact;
$this->automation_data = json_decode( $aContact['data'], true );
$this->attempts = absint( $aContact['attempts'] );
$this->contact_id = absint( $aContact['cid'] );
$this->automation_id = absint( $aContact['aid'] );
$this->automation_contact_id = absint( $aContact['ID'] );
return true;
}
if ( empty( $this->contact_id ) || empty( $this->automation_id ) ) {
return false;
}
$aContact = BWFAN_Model_Automation_Contact::get_automation_contact( $this->automation_id, $this->contact_id );
if ( ! is_array( $aContact ) || ! isset( $aContact['data'] ) ) {
return false;
}
$this->automation_contact_id = absint( $aContact['ID'] );
$this->automation_data = json_decode( $aContact['data'], true );
$this->attempts = absint( $aContact['attempts'] );
return true;
}
public function populate_step_data( $db_step = array() ) {
$step = $db_step;
if ( is_array( $db_step ) && isset( $db_step['ID'] ) ) {
$this->step_data = isset( $db_step['data'] ) ? json_decode( $db_step['data'], true ) : [];
$this->step_id = absint( $db_step['ID'] );
}
if ( empty( $this->step_id ) ) {
return false;
}
if ( empty( $this->step_data ) ) {
$step = BWFAN_Model_Automation_Step::get_step_data_by_id( $this->step_id );
if ( ! is_array( $step ) ) {
return false;
}
$this->step_data = isset( $step['data'] ) ? json_decode( $step['data'], true ) : [];
}
if ( ! empty( $step['action'] ) ) {
$this->action_data = json_decode( $step['action'], true );
}
return true;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,152 @@
<?php
namespace BWFAN\Exporter;
/**
* Calls base class
*/
abstract class Base {
/** Handle exporter response type */
public static $EXPORTER_ONGOING = 1;
public static $EXPORTER_SUCCESS = 2;
public static $EXPORTER_FAILED = 3;
public static $export_folder = BWFAN_SINGLE_EXPORT_DIR;
/**
* Exporter Type
*
* @var string
*/
protected $type = '';
/**
* Abstract function to process export action
*
* @param int $user_id
* @param int $export_id
*
* @return void
*/
abstract public function handle_export( $user_id, $export_id );
/**
* Save processed and count data in table
*
* @param array $extra_data (optional)
*
* @return array|int
*/
public function insert_data_in_table( $extra_data = [] ) {
if ( ! class_exists( 'BWFAN_Model_Import_Export' ) || ( ! file_exists( self::$export_folder ) && ! wp_mkdir_p( self::$export_folder ) ) ) {
return 0;
}
$file_name = $this->get_file_name();
if ( empty( $file_name ) ) {
return 0;
}
$meta = [ 'file' => $file_name ];
// Add extra data to meta
if ( ! empty( $extra_data ) ) {
$meta = array_merge( $meta, $extra_data );
}
$data = [
'offset' => 0,
'processed' => 0,
'type' => 3,
'status' => self::$EXPORTER_ONGOING,
'count' => 0,
'meta' => wp_json_encode( $meta ),
'created_date' => current_time( 'mysql', 1 ),
'last_modified' => current_time( 'mysql', 1 )
];
\BWFAN_Model_Import_Export::insert( $data );
$export_id = \BWFAN_Model_Import_Export::insert_id();
if ( empty( $export_id ) ) {
return 0;
}
return [
'id' => $export_id,
'file' => self::$export_folder . '/' . $file_name
];
}
/**
* Get total count
*
* @param array $data
*
* @return mixed
*/
public function get_total_count( $data = [] ) {
return 0;
}
/**
* Get export file name
*
* @return string
*/
public function get_file_name() {
return '';
}
/**
* Update offset and processed count
*
* @return void
*/
public function update_export_offset( $export_id, $offset, $processed, $count ) {
\BWFAN_Model_Import_Export::update( [
"offset" => $offset,
"processed" => $processed,
"count" => $count
], [ 'id' => absint( $export_id ) ] );
}
/**
* End export process
*
* @param $user_id
* @param $export_id
* @param $file_name
* @param $status
* @param $status_message
*
* @return void
*/
public function end_user_export( $user_id, $export_id, $file_name, $status, $status_message ) {
if ( empty( $status_message ) && $status === self::$EXPORTER_FAILED ) {
$status_message = __( 'Export error. Export ID: ', 'wp-marketing-automations' ) . $export_id;
}
$user_data = get_user_meta( $user_id, 'bwfan_single_export_status', true ) ?: [];
$user_data[ $this->type ] = [
'status' => $status,
'url' => self::$export_folder . '/' . $file_name,
'msg' => [ $status_message ]
];
update_user_meta( $user_id, 'bwfan_single_export_status', $user_data );
BWFAN_Core()->exporter->unschedule_export_action( [
'type' => $this->type,
'user_id' => $user_id,
'export_id' => $export_id
] );
$export_meta = [
'status_msg' => $status_message
];
\BWFAN_Model_Import_Export::update( [
'status' => $status,
'meta' => wp_json_encode( $export_meta )
], [ 'id' => absint( $export_id ) ] );
}
}

View File

@@ -0,0 +1,183 @@
<?php
#[AllowDynamicProperties]
abstract class BWFAN_Integration {
/**
* Return Save setting of integration
* @var array
*/
protected $integration_settings = [];
/**
* this property use for admin builder ui
* @var array
*/
protected $localize_data = [];
/**
* Return Nice Human readable name of integration
* @var null
*/
protected $nice_name = 'WordPress';
/**
* Return integration belong native WordPress or other like activecampaign
* @var bool
*/
protected $native_integration = false;
/**
* If you add new integration and have respective action then please override this property in child class
* @var string
*/
protected $action_dir = __DIR__;
/**
* Return Unique Slug of Integration
* @var string
*/
protected $slug = '';
/**
* Return used connector slug in integration
* @var string
*/
protected $connector_slug = '';
/**
* Return integration need a separate connector
* @var bool
*/
protected $need_connector = false;
protected $group_name = '';
protected $group_slug = '';
protected $priority = 10;
/**
* Loads all actions of current integration
*/
public function load_actions() {
$resource_dir = $this->action_dir . '/actions';
if ( file_exists( $resource_dir ) ) {
foreach ( glob( $resource_dir . '/class-*.php' ) as $_field_filename ) {
$file_data = pathinfo( $_field_filename );
if ( isset( $file_data['basename'] ) && 'index.php' === $file_data['basename'] ) {
continue;
}
$action_class = require_once( $_field_filename );
if ( is_string( $action_class ) && method_exists( $action_class, 'get_instance' ) ) {
/**
* @var $action_obj BWFAN_Action
*/
$action_obj = $action_class::get_instance();
$action_obj->load_hooks();
if ( method_exists( $action_obj, 'admin_enqueue_assets' ) && is_admin() && BWFAN_Common::is_automation_v1_active() && BWFAN_Common::is_autonami_page() ) {
// Add action to avoid enqueueing assets on every admin page load
add_action( 'admin_enqueue_scripts', array( $action_obj, 'admin_enqueue_assets' ), 98 );
}
$action_obj->set_integration_type( $this->get_slug() );
BWFAN_Load_Integrations::register_actions( $action_obj );
$this->do_after_action_registration( $action_obj );
}
}
}
do_action( 'bwfan_' . $this->get_slug() . '_actions_loaded', $this );
}
/**
* Get slug of an integration
* Note: Used in change_in_automations method of BWFAN_Automations class
*
* @return mixed|string
*/
public function get_slug() {
$this->slug = str_replace( array( 'bwfan_', '_integration' ), '', sanitize_title( get_class( $this ) ) );
return $this->slug;
}
protected function do_after_action_registration( BWFAN_Action $action_object ) {
}
public function get_localize_data() {
$this->localize_data = [
'nice_name' => $this->get_name(),
'slug' => $this->get_slug(),
'connector_slug' => $this->get_connector_slug(),
'native_integration' => $this->native_integration(),
'group_slug' => $this->get_group_slug(),
'group_name' => $this->get_group_name(),
'priority' => $this->get_priority()
];
return $this->localize_data;
}
public function get_name() {
return trim( $this->nice_name );
}
public function get_connector_slug() {
return $this->connector_slug;
}
public function get_group_slug() {
return $this->group_slug;
}
public function get_group_name() {
return $this->group_name;
}
public function get_priority() {
return $this->priority;
}
public function native_integration() {
return $this->native_integration;
}
public function set_settings( $data ) {
if ( is_array( $data ) ) {
$this->integration_settings = $data;
}
}
public function get_settings( $key = '' ) {
if ( ! empty( $key ) ) {
return isset( $this->integration_settings[ $key ] ) ? $this->integration_settings[ $key ] : '';
}
return $this->integration_settings;
}
public function need_connector() {
return $this->need_connector;
}
public function handle_response( $result, $connector_slug, $action_call_class_slug, $action_data = null ) {
return $result;
}
/**
* to avoid unserialize of the current class
*/
public function __wakeup() {
throw new ErrorException( 'BWFAN_Core can`t converted to string' );
}
/**
* to avoid serialize of the current class
*/
public function __sleep() {
throw new ErrorException( 'BWFAN_Core can`t converted to string' );
}
/**
* To avoid cloning of current class
*/
protected function __clone() {
}
}

View File

@@ -0,0 +1,561 @@
<?php
#[AllowDynamicProperties]
abstract class BWFAN_Merge_Tag {
public $date_formats = array(
array(
'format' => 'j M Y',
),
array(
'format' => 'jS M Y',
),
array(
'format' => 'M j Y',
),
array(
'format' => 'M jS Y',
),
array(
'format' => 'd/m/Y',
),
array(
'format' => 'd-m-Y',
),
array(
'format' => 'Y/m/d',
),
array(
'format' => 'Y-m-d',
),
array(
'format' => 'd/m/Y H:i:s',
),
array(
'format' => 'd-m-Y H:i:s',
),
array(
'format' => 'Y/m/d H:i:s',
),
array(
'format' => 'Y-m-d H:i:s',
),
array(
'format' => 'd.m.Y',
),
);
protected $support_fallback = false;
protected $support_date = false;
protected $support_modify = true;
protected $fallback = '';
protected $tag_name = '';
protected $tag_description = '';
protected $support_v2 = true;
protected $support_v1 = true;
protected $priority = 10;
protected $is_delay_variable = false;
protected $is_crm_broadcast = false;
public function get_coupon_fields() {
$time_types = array(
'days' => __( 'Days', 'wp-marketing-automations' ),
);
?>
<div class="bwfan_mtag_wrap">
<div class="bwfan_label">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Select coupon', 'wp-marketing-automations' ); ?></label>
</div>
<div class="bwfan_label_val">
<select required id="" data-search="coupon" data-search-text="<?php esc_attr_e( 'Select Coupon', 'wp-marketing-automations' ); ?>" class="bwfan-select2ajax-single bwfan-input-wrapper bwfan_tag_select" name="parent_coupon">
<option value=""><?php esc_html_e( 'Choose Coupon', 'wp-marketing-automations' ); ?></option>
</select>
</div>
</div>
<div class="bwfan_mtag_wrap">
<div class="bwfan_label">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Coupon name', 'wp-marketing-automations' ); ?></label>
</div>
<div class="bwfan_label_val">
<input required type="text" class="bwfan-input-wrapper bwfan_tag_input" name="coupon_name"/>
</div>
</div>
<div class="bwfan_mtag_wrap">
<div class="bwfan_label">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Select Expiry Type', 'wp-marketing-automations' ); ?></label>
</div>
<div class="bwfan_label_val">
<select required id="" class="bwfan-input-wrapper bwfan_tag_select" name="expiry_type">
<?php
foreach ( $time_types as $value1 => $text ) {
?>
<option value="<?php echo esc_attr( $value1 ); ?>"><?php echo esc_attr( $text ); ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="bwfan_mtag_wrap">
<div class="bwfan_label">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Expiry', 'wp-marketing-automations' ); ?></label>
</div>
<div class="bwfan_label_val">
<input min="0" type="number" class="bwfan-input-wrapper bwfan_tag_input" name="expiry"/>
</div>
</div>
<div class="bwfan_mtag_wrap">
<label for="bwfan-restrict" class="bwfan-label-title"><input type="checkbox" name="restrict" id="bwfan-restrict" class="bwfan_tag_select" value="yes"/><?php esc_html_e( 'Restrict user email with coupon', 'wp-marketing-automations' ); ?>
</label>
</div>
<?php
}
/**
* Show the html in popup for the merge tag.
*/
public function get_view() {
$this->get_back_button();
if ( $this->support_date ) {
$this->date_format();
}
if ( $this->support_fallback ) {
$this->get_fallback();
}
$this->get_preview();
$this->get_copy_button();
}
public function get_back_button() {
?>
<div class="bwfan_inner_merge_tag_desc"></div>
<?php
}
public function date_format() {
$formats = $this->date_formats;
echo '<div class="bwfan_mtag_wrap">';
echo '<div class="bwfan_label">';
echo '<label class="bwfan-label-fallback-title">' . __( 'Select Date Format', 'wp-marketing-automations' ) . '</label>'; //phpcs:ignore WordPress.Security.EscapeOutput
echo '</div>';
echo '<div class="bwfan_label_val">';
echo '<select class="bwfan_date_format bwfan-input-wrapper bwfan_tag_select" name="format" style="width:100%;">';
foreach ( $formats as $parameters ) {
$date_time = $this->format_datetime( date( 'Y-m-d H:i:s' ), $parameters );
echo '<option value="' . $parameters['format'] . '">' . $date_time . '</option>'; //phpcs:ignore WordPress.Security.EscapeOutput
}
echo '</select>';
echo '</div></div>';
if ( true === $this->support_modify ) {
echo '<div class="bwfan_mtag_wrap">
<div class="bwfan_label">
<label for="" class="bwfan-label-fallback-title">' . esc_html__( 'Modify (Optional)', 'wp-marketing-automations' ) . '</label>
</div>
<div class="bwfan_label_val">
<input type="text" class="bwfan-input-wrapper bwfan_tag_input" name="modify" placeholder="e.g. +2 months, -1 day, +6 hours"/>
</div>
</div>';
}
}
public static function maybe_parse_nested_merge_tags( $string ) {
$position_end = strpos( $string, ']' );
if ( false === $position_end ) {
return $string;
}
$split = str_split( $string, $position_end );
$position_start = strrpos( $split[0], '[', - 1 );
if ( false === $position_start ) {
return $string;
}
$shortcode_array = explode( '[', $split[0] );
$shortcode = end( $shortcode_array );
$result = do_shortcode( '[' . $shortcode . ']' );
/** Handling in case shortcode is not available and the output again contains the shortcode that would results in infinite loop */
$result = str_replace( '[', '&#91;', $result );
$result = str_replace( ']', '&#93;', $result );
$string = str_replace( '[' . $shortcode . ']', $result, $string );
return unescape_invalid_shortcodes( self::maybe_parse_nested_merge_tags( $string ) );
}
public function format_datetime( $input, $parameters, $is_gmt = false ) {
if ( ! $input ) {
return false;
}
/** Date saved value format */
$input_format = ( isset( $parameters['input_format'] ) && ! empty( $parameters['input_format'] ) ) ? $parameters['input_format'] : 'Y-m-d H:i:s';
/** Format in which fetch date value (output format) */
$output_format = ( isset( $parameters['format'] ) && ! empty( $parameters['format'] ) ) ? $parameters['format'] : 'Y-m-d H:i:s';
/** For delay variable */
if ( isset( $parameters['from'] ) && 'delay' === $parameters['from'] ) {
$input_format = ( isset( $parameters['format'] ) && ! empty( $parameters['format'] ) ) ? $parameters['format'] : 'Y-m-d H:i:s';
$output_format = 'Y-m-d H:i:s';
}
if ( is_a( $input, 'DateTime' ) ) {
$date = $input;
} else {
if ( is_numeric( $input ) ) {
$date = new DateTime();
$date->setTimestamp( $input );
} else {
try {
$date = false !== DateTime::createFromFormat( $input_format, $input ) ? DateTime::createFromFormat( $input_format, $input ) : new DateTime( $input );
} catch ( Exception $e ) {
return '';
}
}
}
/** Convert time in store time if gmt is false */
if ( false === $is_gmt ) {
$date = BWFAN_Common::convert_to_site_time( $date );
}
if ( isset( $parameters['modify'] ) && ! empty( $parameters['modify'] ) ) {
$date->modify( $parameters['modify'] );
}
return date_i18n( $output_format, $date->getTimestamp() );
}
public function get_fallback() {
?>
<div class="bwfan_mtag_wrap">
<div class="bwfan_label">
<label for="" class="bwfan-label-fallback-title"><?php esc_html_e( 'Fallback', 'wp-marketing-automations' ); ?></label>
</div>
<div class="bwfan_label_val">
<input type="text" class="bwfan-input-wrapper bwfan_tag_input" name="fallback"/>
</div>
</div>
<?php
}
public function get_preview() {
?>
<textarea style="margin: 5px 20px 15px; width: 93%;" class="bwfan-preview-merge-tag bwfan-input-wrapper" readonly></textarea>
<?php
}
public function get_copy_button() {
?>
<span style="line-height: 70px;" class="bwfan-use-merge-tag"><?php esc_html_e( 'Copy To Clipboard', 'wp-marketing-automations' ); ?></span>
<input type="submit" class="bwfan-display-none"/>
<?php
}
public function parse_shortcode_output( $output, $atts ) {
$default = $output;
if ( empty( $output ) && isset( $atts['fallback'] ) && ! empty( $atts['fallback'] ) ) {
$this->fallback = $atts['fallback'];
$output = $this->fallback;
}
/** If value is array */
$output = is_array( $output ) && count( $output ) > 0 ? implode( ', ', $output ) : $output;
if ( ! empty( $output ) && isset( $atts['prefix'] ) && ! empty( $atts['prefix'] ) ) {
$output = $atts['prefix'] . $output;
}
if ( ! empty( $output ) && isset( $atts['suffix'] ) && ! empty( $atts['suffix'] ) ) {
$output = $output . $atts['suffix'];
}
return apply_filters( 'bwfan_parse_merge_tag_output', $output, $atts, $default );
}
public function data_key() {
?>
<div class="bwfan_mtag_wrap">
<div class="bwfan_label">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Meta Key', 'wp-marketing-automations' ); ?></label>
</div>
<div class="bwfan_label_val">
<input type="text" class="bwfan-input-wrapper bwfan_tag_input" name="key" required/>
<div class="clearfix bwfan_field_desc"><?php esc_html_e( 'Input the correct meta key in order to get the data', 'wp-marketing-automations' ); ?></div>
</div>
</div>
<?php
}
public function get_customer_city() {
$customer_city = '';
$order = BWFAN_Merge_Tag_Loader::get_data( 'wc_order' );
$user_id = BWFAN_Merge_Tag_Loader::get_data( 'user_id' );
if ( ! empty( $order ) ) {
return BWFAN_Woocommerce_Compatibility::get_order_billing_city( $order );
}
if ( ! empty( $user_id ) ) {
return get_user_meta( (int) $user_id, 'billing_city', true );
}
return $customer_city;
}
public function get_customer_country() {
$customer_country = '';
$country_slug = '';
$order = BWFAN_Merge_Tag_Loader::get_data( 'wc_order' );
$user_id = BWFAN_Merge_Tag_Loader::get_data( 'user_id' );
if ( ! empty( $order ) ) {
$country_slug = BWFAN_Woocommerce_Compatibility::get_billing_country_from_order( $order );
}
if ( ! empty( $user_id ) ) {
$country_slug = get_user_meta( (int) $user_id, 'billing_country', true );
}
if ( ! empty( $country_slug ) ) {
$countries_obj = new WC_Countries();
$countries = $countries_obj->__get( 'countries' );
$customer_country = $countries[ $country_slug ];
}
return $customer_country;
}
public function initialize_product_details() {
$product_details = BWFAN_Merge_Tag_Loader::get_data( 'product_details' );
$product_id = BWFAN_Merge_Tag_Loader::get_data( 'product_id' );
$product = BWFAN_Merge_Tag_Loader::get_data( 'product' );
if ( empty( $product_details ) ) {
BWFAN_Merge_Tag_Loader::set_data( array(
'product_details' => get_post( $product_id ),
) );
}
if ( empty( $product ) ) {
BWFAN_Merge_Tag_Loader::set_data( array(
'product' => wc_get_product( $product_id ),
) );
}
}
public function get_slug() {
return sanitize_title( get_class( $this ) );
}
public function get_name() {
return $this->tag_name;
}
public function get_description() {
return $this->tag_description;
}
public function get_localize_data() {
return [
'tag_name' => $this->tag_name,
'tag_description' => $this->tag_description,
'support_v1' => $this->support_v1,
];
}
public function get_formatted_billing_address( $empty_content = '' ) {
$order = BWFAN_Merge_Tag_Loader::get_data( 'wc_order' );
if ( $order instanceof WC_Order ) {
$address = apply_filters( 'woocommerce_order_formatted_billing_address', $order->get_address( 'billing' ), $order );
$separator = apply_filters( 'bwfan_' . $this->tag_name . '_separator', '</br>' );
$address = WC()->countries->get_formatted_address( $address, $separator );
return $address ? $address : $empty_content;
}
return '';
}
public function get_formatted_shipping_address( $empty_content = '' ) {
$order = BWFAN_Merge_Tag_Loader::get_data( 'wc_order' );
if ( $order instanceof WC_Order ) {
$address = apply_filters( 'woocommerce_order_formatted_shipping_address', $order->get_address( 'shipping' ), $order );
$separator = apply_filters( 'bwfan_' . $this->tag_name . '_separator', '</br>' );
$address = WC()->countries->get_formatted_address( $address, $separator );
return $address ? $address : $empty_content;
}
return '';
}
/**
* Get date value in WordPress set date format
*
* @param $date_value
*
* @return string
*/
public function get_formatted_date_value( $date_value ) {
if ( empty( $date_value ) ) {
return '';
}
if ( false === $this->validate_date( $date_value ) ) {
return $date_value;
}
$date_format = BWFAN_Common::bwfan_get_date_format();
$date_value = date( $date_format, strtotime( $date_value ) );
return $date_value;
}
/**
* Validate date
*
* @param $date
* @param string $format
*
* @return bool
*/
public function validate_date( $date, $format = 'Y-m-d' ) {
$d = DateTime::createFromFormat( $format, $date );
return $d && $d->format( $format ) === $date;
}
/** contact data will be used in the contact merge tag
* @return false|WooFunnels_Contact
*/
public function get_contact_data() {
/** Check if contact id is present */
$get_data = BWFAN_Merge_Tag_Loader::get_data();
if ( isset( $get_data['contact_id'] ) && 0 < intval( $get_data['contact_id'] ) ) {
$contact = new WooFunnels_Contact( '', '', '', intval( $get_data['contact_id'] ) );
if ( $contact instanceof WooFunnels_Contact && $contact->get_id() > 0 ) {
return $contact;
}
}
$user = wp_get_current_user();
if ( ! $user instanceof WP_USER ) {
return false;
}
if ( $user->ID === 0 ) {
return false;
}
/** get user email */
$user_email = $user->user_email;
$contact = new WooFunnels_Contact( $user->ID, $user_email );
if ( $contact instanceof WooFunnels_Contact && $contact->get_id() > 0 ) {
return $contact;
}
return false;
}
/**
* Get order object
*
* @param $get_data
*
* @return string|WC_Order
*/
public function get_order_object( $get_data ) {
if ( ! is_array( $get_data ) || ! bwfan_is_woocommerce_active() ) {
return '';
}
$order = isset( $get_data['wc_order'] ) ? $get_data['wc_order'] : '';
if ( $order instanceof WC_Order ) {
return $order;
}
$order = ( isset( $get_data['order_id'] ) && intval( $get_data['order_id'] ) > 0 ) ? wc_get_order( $get_data['order_id'] ) : '';
if ( $order instanceof WC_Order ) {
return $order;
}
return '';
}
public function is_support_v2() {
return $this->support_v2;
}
public function get_priority() {
return $this->priority;
}
/**
* to avoid unserialize of the current class
*/
public function __wakeup() {
throw new ErrorException( __CLASS__ . ' can`t converted to string' );
}
/**
* to avoid serialize of the current class
*/
public function __sleep() {
throw new ErrorException( __CLASS__ . ' can`t converted to string' );
}
/**
* Returns true if support for v1
*
* @return bool
*/
public function is_support_v1() {
return $this->support_v1;
}
/**
* Returns true if it is a delay variable
* @return bool|mixed
*/
public function is_delay_variable() {
return $this->is_delay_variable;
}
/**
* To avoid cloning of current class
*/
protected function __clone() {
}
public function is_crm_broadcast() {
return $this->is_crm_broadcast;
}
/**
* Get affiliate id from contact
*
* @return mixed|void
*/
public function get_affiliate_id() {
$data = BWFAN_Merge_Tag_Loader::get_data();
if ( ! isset( $data['contact_id'] ) || empty( $data['contact_id'] ) ) {
return '';
}
if ( ! class_exists( 'BWFAN_PRO_Common' ) || ! bwfan_is_affiliatewp_active() ) {
return '';
}
$contact_id = absint( $data['contact_id'] );
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
return '';
}
$user_id = $contact->contact->get_wpid();
if ( empty( $user_id ) ) {
return '';
}
$affiliate_id = BWFAN_PRO_Common::get_aff_id_by_user_id( $user_id );
return ! $affiliate_id ? '' : $affiliate_id;
}
}

View File

@@ -0,0 +1,54 @@
<?php
#[AllowDynamicProperties]
abstract class Cart_Merge_Tag extends BWFAN_Merge_Tag {
public $checkout_data = null;
public $cart_details = null;
/**
* Get cart value which is captured at the checkout page
*
* @param $key
* @param $cart_details
*
* @return mixed|string
*/
public function get_cart_value( $key, $cart_details ) {
if ( empty( $cart_details ) ) {
return '';
}
$this->cart_details = $cart_details;
$checkout_data = $cart_details['checkout_data'] ?? '';
if ( empty( $checkout_data ) ) {
return '';
}
$field_value = '';
$checkout_data = json_decode( $checkout_data, true );
$this->checkout_data = $checkout_data;
if ( isset( $checkout_data['fields'][ $key ] ) ) {
$field_value = $checkout_data['fields'][ $key ];
$field_value = $this->post_value_check( $field_value );
}
if ( empty( $field_value ) ) {
return $checkout_data[ $key ] ?? '';
}
return $field_value;
}
/**
* Individual merge tags can decode the value as they need
*
* @param $field_value
*
* @return mixed
*/
public function post_value_check( $field_value ) {
return $field_value;
}
}

View File

@@ -0,0 +1,227 @@
<?php
#[AllowDynamicProperties]
class BWFAN_Cart_Display extends BWFAN_Merge_Tag {
public $item_type = null;
public $cart_number = null;
public function get_currency_option() {
return array(
'symbol' => __( 'Currency Symbol', 'wp-marketing-automations' ),
'code' => __( '3 Letter Currency code', 'wp-marketing-automations' ),
);
}
/**
* Show the html in popup for the merge tag.
*/
public function get_view() {
$this->get_back_button();
$this->get_item_number_html();
if ( $this->support_fallback ) {
$this->get_fallback();
}
$this->get_preview();
$this->get_copy_button();
}
public function get_item_number_html() {
$templates = $this->get_view_data();
?>
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Select Item Number', 'wp-marketing-automations' ); ?></label>
<select id="" class="bwfan-input-wrapper bwfan-mb-15 bwfan_tag_select" name="item_no" required>
<option value=""><?php esc_html_e( 'Select', 'wp-marketing-automations' ); ?></option>
<?php
foreach ( $templates as $slug => $name ) {
echo '<option value="' . esc_attr( $slug ) . '">' . esc_html( $name ) . '</option>';
}
?>
</select>
<?php
}
public function get_view_data() {
$templates = array(
'1' => __( 'First Item', 'wp-marketing-automations' ),
'2' => __( 'Second Item', 'wp-marketing-automations' ),
'3' => __( 'Third Item', 'wp-marketing-automations' ),
'4' => __( 'Fourth Item', 'wp-marketing-automations' ),
'5' => __( 'Fifth Item', 'wp-marketing-automations' ),
'6' => __( 'Sixth Item', 'wp-marketing-automations' ),
'7' => __( 'Seventh Item', 'wp-marketing-automations' ),
'8' => __( 'Eighth Item', 'wp-marketing-automations' ),
'9' => __( 'Ninth Item', 'wp-marketing-automations' ),
'10' => __( 'Tenth Item', 'wp-marketing-automations' ),
);
return $templates;
}
public function get_item_value() {
$cart_row_details = BWFAN_Merge_Tag_Loader::get_data( 'cart_details' );
$cart_items = maybe_unserialize( $cart_row_details['items'] );
$return_value = '';
$count = 1;
foreach ( $cart_items as $item_data ) {
if ( absint( $this->cart_number ) === $count ) {
$product_id = ( isset( $item_data['product_id'] ) ) ? $item_data['product_id'] : 0;
$quantity = ( isset( $item_data['quantity'] ) ) ? $item_data['quantity'] : 0;
$_pf = new WC_Product_Factory();
$product = $_pf->get_product( $product_id );
switch ( $this->item_type ) {
case 'image':
$product_image = BWFAN_Common::get_product_image( $product, 'shop_catalog', true );
$return_value = $product_image;
break;
case 'name':
$product_name = BWFAN_Common::get_name( $product );
$return_value = $product_name;
break;
case 'price':
$product_price = $product->get_price();
$return_value = $product_price;
break;
case 'quantity':
$return_value = $quantity;
break;
case 'url':
$product_url = $product->get_permalink();
$return_value = $product_url;
break;
}
break;
}
$count ++;
}
return $return_value;
}
public function get_item_details( $parameters = null ) {
$item_id = BWFAN_Merge_Tag_Loader::get_data( 'wc_single_item_id' );
/** If item id is empty */
if ( empty( $item_id ) ) {
return '';
}
try {
$item = new WC_Order_Item_Product( $item_id );
} catch ( Exception $e ) {
return '';
}
if ( ! $item instanceof WC_Order_Item_Product || empty( $item->get_id() ) ) {
return '';
}
switch ( $this->item_type ) {
case 'image':
$product_image = BWFAN_Common::get_product_image( $item->get_product(), 'shop_catalog', true );
$return_value = $product_image;
break;
case 'name':
$return_value = $item->get_name();
break;
case 'price':
$return_value = $item->get_total();
$formatting = BWFAN_Common::get_formatting_for_wc_price( $parameters, $item->get_order() );
$return_value = BWFAN_Common::get_formatted_price_wc( $return_value, $formatting['raw'], $formatting['currency'] );
break;
case 'quantity':
$return_value = $item->get_quantity();
break;
case 'stock':
$product = $item->get_product();
$return_value = $product->get_stock_quantity();
break;
case 'url':
$product = $item->get_product();
$return_value = $product->get_permalink();
break;
case 'item_data':
if ( is_array( $parameters ) && isset( $parameters['key'] ) ) {
$return_value = wc_get_order_item_meta( (int) $item_id, $parameters['key'], true );
} else {
$return_value = '';
}
break;
case 'item_attribute':
if ( is_array( $parameters ) && isset( $parameters['key'] ) ) {
$attribute = 'pa_' . $parameters['key'];
$term = $item->get_meta( $attribute );
if ( ! $term ) {
return false;
}
$term_obj = get_term_by( 'slug', $term, $attribute );
if ( ! $term_obj || is_wp_error( $term_obj ) ) {
return false;
}
return $term_obj->name;
} else {
$product = $item->get_product();
if ( ! $product instanceof WC_Product ) {
return false;
}
$attributes = $product->get_attributes();
if ( empty( $attributes ) ) {
return false;
}
$variation_names1 = array();
foreach ( $attributes as $key => $value ) {
$term = $item->get_meta( $key );
$term_obj = get_term_by( 'slug', $term, $key );
if ( ! $term_obj || is_wp_error( $term_obj ) ) {
continue;
}
$variation_names1[] = $term_obj->name;
}
$return_value = implode( ', ', $variation_names1 );
}
break;
case 'item_meta':
$product = $item->get_product();
$product_id = $product->get_id();
if ( is_array( $parameters ) && isset( $parameters['key'] ) ) {
$return_value = get_post_meta( (int) $product_id, $parameters['key'], true );
} else {
$return_value = '';
}
break;
case 'item_id':
$product = $item->get_product();
$return_value = $product->get_id();
break;
case 'item_sku':
$product = $item->get_product();
$return_value = $product->get_sku();
break;
}
return $return_value;
}
}

View File

@@ -0,0 +1,261 @@
<?php
#[AllowDynamicProperties]
abstract class Merge_Tag_Abstract_Product_Display extends BWFAN_Merge_Tag {
public $support_limit_field = false;
public $supports_order_table = false;
public $supports_cart_table = false;
public $template = null;
public $fallback = null;
public $products = [];
public $cart = null;
public $data = null;
public $order = null;
public $products_quantity = null;
public $products_sku = [];
public $products_price = null;
/**
* Show the html in popup for the merge tag.
*/
public function get_view() {
$templates = $this->get_view_data();
$this->get_back_button();
?>
<div class="bwfan_mtag_wrap">
<div class="bwfan_label">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Select Template', 'wp-marketing-automations' ); ?></label>
</div>
<div class="bwfan_label_val">
<select id="" class="bwfan-input-wrapper bwfan_tag_select" name="template">
<?php
foreach ( $templates as $slug => $name ) {
echo '<option value="' . esc_attr( $slug ) . '">' . esc_attr( $name ) . '</option>';
}
?>
</select>
</div>
</div>
<?php
if ( $this->support_fallback ) {
$this->get_fallback();
}
$this->get_preview();
$this->get_copy_button();
}
public function get_view_data() {
$templates = array(
'' => __( 'Product Grid - 2 Column', 'wp-marketing-automations' ),
'product-grid-3-col' => __( 'Product Grid - 3 Column', 'wp-marketing-automations' ),
'product-rows' => __( 'Product Rows', 'wp-marketing-automations' ),
'review-rows' => __( 'Product Rows (With Review Button)', 'wp-marketing-automations' ),
'order-table' => __( 'WooCommerce Order Summary Layout', 'wp-marketing-automations' ),
'cart-table' => __( 'Cart Table Layout', 'wp-marketing-automations' ),
'list-comma-separated' => __( 'List - Comma Separated (Product Names only)', 'wp-marketing-automations' ),
'list-comma-separated-with-quantity' => __( 'List - Comma Separated (Product Names with Quantity)', 'wp-marketing-automations' ),
);
if ( ! $this->supports_cart_table ) {
unset( $templates['cart-table'] );
}
if ( ! $this->supports_order_table ) {
unset( $templates['order-table'] );
unset( $templates['review-rows'] );
}
return $templates;
}
public function process_shortcode( $attr ) {
$cart = false;
$data = null;
$products = [];
$products_quantity = [];
$products_price = [];
if ( ! is_null( $this->cart ) && is_array( $this->cart ) && count( $this->cart ) > 0 ) {
$cart = $this->cart;
}
if ( ! is_null( $this->data ) && is_array( $this->data ) && count( $this->data ) > 0 ) {
$data = $this->data;
}
if ( is_array( $this->products ) && count( $this->products ) > 0 ) {
$products = $this->products;
}
if ( is_array( $this->products_quantity ) && count( $this->products_quantity ) > 0 ) {
$products_quantity = $this->products_quantity;
}
if ( is_array( $this->products_sku ) && count( $this->products_sku ) > 0 ) {
$products_sku = $this->products_sku;
}
if ( is_array( $this->products_price ) && count( $this->products_price ) > 0 ) {
$products_price = $this->products_price;
}
$this->template = 'product-grid-2-col';
if ( isset( $attr['template'] ) ) {
$this->template = $attr['template'];
}
/** Allowed templates for product display settings */
$allowed_templates = array( 'product-grid-2-col', 'product-grid-3-col' );
/** if display product count is set in mergetag then remove remaining products */
if ( in_array( $this->template, $allowed_templates, false ) && isset( $attr['display'] ) && absint( $attr['display'] ) >= 1 && absint( $attr['display'] ) < count( $products ) ) {
$products = array_splice( $products, 0, absint( $attr['display'] ) );
}
/** Filter products in case want to hide free products */
$hide_free_products = BWFAN_Common::hide_free_products_cart_order_items();
if ( true === $hide_free_products ) {
/** $products */
$products_mod = array_filter( $products, function ( $single_product ) {
return ( $single_product->get_price() > 0 );
} );
$products = $products_mod;
}
if ( true === BWFAN_Merge_Tag_Loader::get_data( 'is_preview' ) && empty( $products ) ) {
$products = wc_get_products( array(
'numberposts' => 3,
'post_status' => 'published', // Only published products
) );
}
if ( apply_filters( 'bwfan_current_integration_action', false ) ) {
$product_names = [];
foreach ( $products as $single_product ) {
$product_names[] = BWFAN_Common::get_name( $single_product );
}
$product_names = wp_json_encode( $product_names );
return $product_names;
}
if ( isset( $attr['fallback'] ) ) {
$this->fallback = $attr['fallback'];
}
if ( 'list' === $this->template ) {
$type = 'comma-separated';
if ( isset( $attr['type'] ) && ! empty( $attr['type'] ) ) {
$type = $attr['type'];
}
$this->template = $this->template . '-' . $type;
}
/** Handle line-separated output */
if ( 'line' === $this->template ) {
$type_line = isset( $attr['type_line'] ) && ! empty( $attr['type_line'] ) ? $attr['type_line'] : 'separated-product-name';
$items = $cart ? $cart : $products;
$is_cart = $cart ? true : false;
return self::get_formatted_items_in_line( $items, $type_line, $products_quantity, $is_cart, $attr );
}
if ( isset( $data['currency'] ) && ! empty( $data['currency'] ) ) {
remove_all_filters( 'woocommerce_currency_symbol' );
}
$file_path = BWFAN_PLUGIN_DIR . '/templates/' . $this->template . '.php';
$file_path = apply_filters( 'bwfan_cart_items_template_path', $file_path, $cart, $data, $attr );
if ( ! file_exists( $file_path ) ) {
return '';
}
ob_start();
include $file_path;
$response = ob_get_clean();
return apply_filters( 'bwfan_alter_email_body', $response, $products, $this->template, $products_quantity );
}
/**
* Format items into lines with quantity and names.
*
* @param $items
* @param $type_line
* @param $products_quantity
* @param $is_cart
* @param $attr
*
* @return array|string
*/
public static function get_formatted_items_in_line( $items, $type_line, $products_quantity, $is_cart, $attr ) {
if ( $is_cart && isset( $items['cart_items'] ) ) {
$items = $items['cart_items'];
}
if ( empty( $items ) || ! is_array( $items ) ) {
return [];
}
$result = [];
foreach ( $items as $item ) {
$product = $is_cart ? ( $item['data'] ?? null ) : $item;
if ( empty( $product ) || ! $product instanceof WC_Product ) {
continue;
}
$product_quantity = $products_quantity[ $product->get_id() ] ?? 1;
$bwfan_separator = apply_filters( 'bwfan_line_separator_symbol', '-', $product, $attr );
$formatted_product = apply_filters( 'bwfan_line_separator_formate', $product->get_name() . " X " . $product_quantity, 'X', $product->get_name(), $product_quantity, $attr );
$result[] = $type_line !== 'separated-product-name' ? "$bwfan_separator $formatted_product" : "$bwfan_separator " . $product->get_name();
}
$bwfan_line_separator = apply_filters( 'bwfan_line_separator', "\n\r", $product, $attr );
return implode( $bwfan_line_separator, $result );
}
public function prepare_products( $product_ids, $orderby = 'date', $order = 'DESC', $limit = 8 ) {
if ( empty( $product_ids ) ) {
return [];
}
$product_ids = array_filter( $product_ids );
$args = [
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $limit,
'post__in' => $product_ids,
'fields' => 'ids',
'orderby' => $orderby,
'order' => $order,
'tax_query' => $this->get_taxonomy_query(),
'meta_query' => WC()->query->get_meta_query(),
];
if ( 'popularity' === $orderby ) {
$args['meta_key'] = 'total_sales';
$args['orderby'] = 'meta_value_num';
}
$query = new WP_Query( $args );
return array_map( 'wc_get_product', $query->posts );
}
protected function get_taxonomy_query( $taxonomy_query = [] ) {
$product_visibility_not_in = [];
$product_visibility_terms = wc_get_product_visibility_term_ids();
// Hide out of stock products.
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
$product_visibility_not_in[] = $product_visibility_terms['outofstock'];
}
if ( ! empty( $product_visibility_not_in ) ) {
$taxonomy_query[] = [
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_not_in,
'operator' => 'NOT IN',
];
}
return $taxonomy_query;
}
}

View File

@@ -0,0 +1,264 @@
<?php
#[AllowDynamicProperties]
abstract class BWFAN_Model {
static $primary_key = 'id';
static $count = 20;
static function set_id() {
}
static function get( $value ) {
global $wpdb;
$query = self::_fetch_sql( $value );
return $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders, WordPress.DB.PreparedSQL
}
private static function _fetch_sql( $value ) {
global $wpdb;
$sql = sprintf( 'SELECT * FROM %s WHERE %s = %%s', self::_table(), static::$primary_key );
return $wpdb->prepare( $sql, $value ); // WPCS: unprepared SQL OK
}
protected static function _table() {
global $wpdb;
$tablename = strtolower( get_called_class() );
$tablename = str_replace( 'bwfan_model_', 'bwfan_', $tablename );
return $wpdb->prefix . $tablename;
}
static function insert( $data ) {
global $wpdb;
$wpdb->insert( self::_table(), $data ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders, WordPress.DB.PreparedSQL
}
static function update( $data, $where ) {
global $wpdb;
return $wpdb->update( self::_table(), $data, $where ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders, WordPress.DB.PreparedSQL
}
static function delete( $value ) {
global $wpdb;
$sql = sprintf( 'DELETE FROM %s WHERE %s = %%s', self::_table(), static::$primary_key );
return $wpdb->query( $wpdb->prepare( $sql, $value ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
static function insert_id() {
global $wpdb;
return $wpdb->insert_id;
}
static function now() {
return self::time_to_date( time() );
}
static function time_to_date( $time ) {
return gmdate( 'Y-m-d H:i:s', $time );
}
static function date_to_time( $date ) {
return strtotime( $date . ' GMT' );
}
static function num_rows() {
global $wpdb;
return $wpdb->num_rows;
}
static function count_rows( $dependency = null ) {
global $wpdb;
$sql = 'SELECT COUNT(*) FROM ' . self::_table();
if ( ! is_null( $dependency ) ) {
$sql = 'SELECT COUNT(*) FROM ' . self::_table() . ' INNER JOIN ' . $dependency['dependency_table'] . ' on ' . self::_table() . '.' . $dependency['dependent_col'] . '=' . $dependency['dependency_table'] . '.' . $dependency['dependency_col'] . ' WHERE ' . $dependency['dependency_table'] . '.' . $dependency['col_name'] . '=' . $dependency['col_value'];
if ( isset( $dependency['automation_id'] ) ) {
$sql = 'SELECT COUNT(*) FROM ' . self::_table() . ' INNER JOIN ' . $dependency['dependency_table'] . ' on ' . self::_table() . '.' . $dependency['dependent_col'] . '=' . $dependency['dependency_table'] . '.' . $dependency['dependency_col'] . ' WHERE ' . $dependency['dependency_table'] . '.' . $dependency['col_name'] . '=' . $dependency['col_value'] . ' AND ' . $dependency['automation_table'] . '.' . $dependency['automation_col'] . '=' . $dependency['automation_id'];
if ( 'any' === $dependency['col_value'] ) {
$sql = 'SELECT COUNT(*) FROM ' . self::_table() . ' INNER JOIN ' . $dependency['dependency_table'] . ' on ' . self::_table() . '.' . $dependency['dependent_col'] . '=' . $dependency['dependency_table'] . '.' . $dependency['dependency_col'] . ' WHERE ' . $dependency['automation_table'] . '.' . $dependency['automation_col'] . '=' . $dependency['automation_id'];
}
}
}
return $wpdb->get_var( $sql ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders, WordPress.DB.PreparedSQL
}
static function count( $data = array() ) {
global $wpdb;
$sql = 'SELECT COUNT(*) as `count` FROM ' . self::_table() . ' WHERE 1=1';
$sql_params = [];
if ( is_array( $data ) && count( $data ) > 0 ) {
foreach ( $data as $key => $val ) {
$sql .= " AND `{$key}` LIKE {$val['operator']}";
$sql_params[] = $val['value'];
}
if ( ! empty( $sql_params ) ) {
$sql = $wpdb->prepare( $sql, $sql_params ); // WPCS: unprepared SQL OK
}
}
return $wpdb->get_var( $sql ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders, WordPress.DB.PreparedSQL
}
static function get_specific_rows( $where_key, $where_value, $offset = 0, $limit = 0 ) {
$pagination = '';
if ( ! empty( $offset ) && ! empty( $limit ) ) {
$pagination = " LIMIT $offset, $limit";
}
global $wpdb;
$table_name = self::_table();
$query = "SELECT * FROM $table_name WHERE $where_key = '$where_value'$pagination";
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders, WordPress.DB.PreparedSQL
}
static function get_rows( $only_query = false, $automation_ids = array() ) {
global $wpdb;
$table_name = self::_table();
$page_number = 1;
$count_per_page = self::$count;
$next_offset = ( $page_number - 1 ) * $count_per_page;
$sql_query = $wpdb->prepare( "SELECT * FROM $table_name ORDER BY c_date DESC LIMIT %d OFFSET %d", $count_per_page, $next_offset );
if ( isset( $_GET['paged'] ) && $_GET['paged'] > 1 ) { // WordPress.CSRF.NonceVerification.NoNonceVerification
$page_number = sanitize_text_field( $_GET['paged'] ); // WordPress.CSRF.NonceVerification.NoNonceVerification
$next_offset = ( $page_number - 1 ) * $count_per_page;
$sql_query = $wpdb->prepare( "SELECT * FROM $table_name ORDER BY c_date DESC LIMIT %d OFFSET %d", $count_per_page, $next_offset );
}
if ( isset( $_GET['status'] ) && 'all' !== $_GET['status'] ) { // WordPress.CSRF.NonceVerification.NoNonceVerification
$status = sanitize_text_field( $_GET['status'] ); // WordPress.CSRF.NonceVerification.NoNonceVerification
$status = ( 'active' === $status ) ? 1 : 2;
$sql_query = $wpdb->prepare( "SELECT * FROM $table_name WHERE status = %d ORDER BY c_date DESC LIMIT %d OFFSET %d", $status, $count_per_page, $next_offset );
}
if ( ( isset( $_GET['paged'] ) && $_GET['paged'] > 0 ) && ( isset( $_GET['status'] ) && '' !== $_GET['status'] ) ) { // WordPress.CSRF.NonceVerification.NoNonceVerification
$page_number = sanitize_text_field( $_GET['paged'] ); // WordPress.CSRF.NonceVerification.NoNonceVerification
$next_offset = ( $page_number - 1 ) * $count_per_page;
$status = sanitize_text_field( $_GET['status'] ); // WordPress.CSRF.NonceVerification.NoNonceVerification
$sql_query = $wpdb->prepare( "SELECT * FROM $table_name WHERE status = %d ORDER BY c_date DESC LIMIT %d OFFSET %d", $status, $count_per_page, $next_offset );
}
$result = $wpdb->get_results( $sql_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $result;
}
static function get_results( $query ) {
global $wpdb;
$query = str_replace( '{table_name}', self::_table(), $query );
$results = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $results;
}
static function get_var( $query ) {
global $wpdb;
$query = str_replace( '{table_name}', self::_table(), $query );
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
static function delete_multiple( $query ) {
self::query( $query );
}
static function query( $query ) {
global $wpdb;
$query = str_replace( '{table_name}', self::_table(), $query );
$wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
static function update_multiple( $query ) {
self::query( $query );
}
static function get_current_date_time() {
return date( 'Y-m-d H:i:s' );
}
static function insert_multiple( $values, $keys, $formats = [] ) {
if ( ( ! is_array( $keys ) || empty( $keys ) ) || ( ! is_array( $values ) || empty( $values ) ) ) {
return false;
}
global $wpdb;
$values = array_map( function ( $value ) use ( $keys, $formats ) {
global $wpdb;
$return = array();
foreach ( $keys as $index => $key ) {
$format = is_array( $formats ) && isset( $formats[ $index ] ) ? $formats[ $index ] : false;
$format = ! empty( $format ) ? $format : ( is_numeric( $value[ $key ] ) ? '%d' : '%s' );
$return[] = $wpdb->prepare( $format, $value[ $key ] );
}
return '(' . implode( ',', $return ) . ')';
}, $values );
$values = implode( ', ', $values );
$keys = '(' . implode( ', ', $keys ) . ')';
$query = 'INSERT INTO ' . self::_table() . ' ' . $keys . ' VALUES ' . $values;
return $wpdb->query( $wpdb->prepare( "$query ", $values ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Insert query with Ignore
*
* @param $data
* @param $format
*
* @return bool|int|mysqli_result|null
*/
static function insert_ignore( $data, $format = null ) {
if ( empty( $data ) || ! is_array( $data ) ) {
return false;
}
// Validate format if provided
if ( ! is_null( $format ) && count( $format ) !== count( $data ) ) {
$format = null; // Reset format if it doesn't match data count
}
$placeholders = is_null( $format ) ? array_fill( 0, count( $data ), '%s' ) : $format;
$columns = array_keys( $data );
$table = self::_table();
global $wpdb;
$sql = "INSERT IGNORE INTO `$table` (`" . implode( '`,`', $columns ) . "`) VALUES (" . implode( ',', $placeholders ) . ")";
//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$result = $wpdb->query( $wpdb->prepare( $sql, array_values( $data ) ) );
if ( ! empty( $result ) ) {
return $result;
}
/** If duplicate entry DB error come */
if ( 0 === $result ) {
$warnings = $wpdb->get_results( "SHOW WARNINGS", ARRAY_A );//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( ! empty( $warnings ) ) {
foreach ( $warnings as $warning ) {
if ( empty( $warning['Message'] ) || false === strpos( $warning['Message'], 'Duplicate entry' ) ) {
continue;
}
BWFAN_Common::log_test_data( 'WP db error in ' . $table . ' : ' . $warning['Message'], 'fka-db-duplicate-error', true );
BWFAN_Common::log_test_data( $data, 'fka-db-duplicate-error', true );
}
}
}
return false;
}
}

View File

@@ -0,0 +1,420 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
#[AllowDynamicProperties]
abstract class BWFAN_Recipes {
public $data = [];
/**
* get data of the recipe
*/
public function get_data() {
return $this->data;
}
/**
* get slug of recipe
*/
public function get_slug() {
return get_class( $this );
}
/**
* get global settings
* @return mixed|void
*/
public function get_settings() {
$settings = BWFAN_Common::get_global_settings();
return $settings;
}
/**
* get plugin name
*
* @param $slug
*
* @return string
*/
public function get_plugin_name( $slug ) {
$name = '';
switch ( $slug ) {
case 'wc':
$name = __( 'WooCommerce', 'wp-marketing-automations' );
break;
case 'wc-subscription':
$name = __( 'WooCommerce Subscription', 'wp-marketing-automations' );
break;
case 'autonami-pro':
$name = __( 'FunnelKit Automations Pro', 'wp-marketing-automations' );
break;
case 'autonami-conn':
$name = __( 'FunnelKit Automations Connector', 'wp-marketing-automations' );
break;
default:
break;
}
return $name;
}
/**
* get plugin name
*
* @param $slug
*
* @return string
*/
public function get_integration_name( $slug ) {
$name = '';
switch ( $slug ) {
case 'wc':
$name = __( 'WooCommerce', 'wp-marketing-automations' );
break;
case 'wc-subscription':
$name = __( 'WooCommerce Subscription', 'wp-marketing-automations' );
break;
case 'autonami-pro':
$name = __( 'FunnelKit Automations Pro', 'wp-marketing-automations' );
break;
case 'autonami-conn':
$name = __( 'FunnelKit Automations Connector', 'wp-marketing-automations' );
break;
default:
break;
}
return $name;
}
/**
* get channel name
*
* @param $slug
*
* @return string
*/
public function get_channel_name( $slug ) {
$name = '';
switch ( $slug ) {
case 'email':
$name = __( 'Email', 'wp-marketing-automations' );
break;
default:
break;
}
return $name;
}
/**
* get goal name
*
* @param $slug
*
* @return string
*/
public function get_goal_name( $slug ) {
$name = '';
switch ( $slug ) {
case 'convert_sales':
$name = __( 'Convert Sales', 'wp-marketing-automations' );
break;
default:
break;
}
return $name;
}
/**
* get type name
*
* @param $slug
*
* @return string
*/
public function get_type_name( $slug ) {
$name = '';
switch ( $slug ) {
case 'abandoned_cart':
$name = __( 'Abandoned Cart', 'wp-marketing-automations' );
break;
case 'browse_abandonment':
$name = __( 'Browse Abandonment', 'wp-marketing-automations' );
break;
case 'customer_winback':
$name = __( 'Customer Winback', 'wp-marketing-automations' );
break;
default:
break;
}
return $name;
}
/**
* checking recipe dependency
*/
public function check_dependency() {
/** get plugin dependency */
$plugin_depend = $this->data['plugin-dependencies'];
$plugin_depend_check = BWFAN_Common::plugin_dependency_check( $plugin_depend );
/** get connector dependency */
$connnect_depend = $this->data['connector-dependencies'];
$connect_depend_check = self::connector_dependency_check( $connnect_depend );
/** check data dependency */
$data_depend = $this->data['data-dependencies'];
$data_depend_check = self::data_dependency_check( $data_depend );
/** @var $depend_error */
$depend_error = array();
// check if plugin and connector dependency match
if ( true !== $connect_depend_check || true !== $plugin_depend_check || true !== $data_depend_check ) {
if ( is_array( $plugin_depend_check ) && count( $plugin_depend_check ) > 0 ) {
$depend_error = $plugin_depend_check;
}
if ( is_array( $connect_depend_check ) && count( $connect_depend_check ) > 0 ) {
$depend_error = array_merge( $connect_depend_check, $depend_error );
}
if ( is_array( $data_depend_check ) && count( $data_depend_check ) > 0 ) {
$depend_error = array_merge( $data_depend_check, $depend_error );
}
return $depend_error;
}
return empty( $depend_error ) ? true : $depend_error;
}
/**
* checking recipe connector dependency
*
* @param $connect_depend
*
* @return array|bool
*/
public static function connector_dependency_check( $connect_depend ) {
if ( empty( $connect_depend ) ) {
return true;
}
$connect_error = [];
$connect_key = '';
// get all connectors
$all_connectors = WFCO_Load_Connectors::get_all_connectors();
if ( empty( $all_connectors ) ) {
foreach ( $connect_depend as $connect ) {
$connect_name = self::get_connector_name( $connect );
$connect_error[] = "{$connect_name} is not activate";
}
return $connect_error;
}
$saved_connectors = WFCO_Common::$connectors_saved_data;
foreach ( $connect_depend as $connect ) {
$connect_name = self::get_connector_name( $connect );
$conn_name = str_replace( ' ', '', strtolower( $connect_name ) );
$connect_key = 'bwfco_' . $conn_name;
if ( ! array_key_exists( $connect_key, $saved_connectors ) ) {
$connect_error[] = "{$connect_name} is not connected";
}
}
return empty( $connect_error ) ? true : $connect_error;
}
/**
* get connector name
*
* @param $slug
*
* @return string
*/
public static function get_connector_name( $slug ) {
$name = '';
switch ( $slug ) {
case 'ac':
$name = __( 'Active Campaign', 'wp-marketing-automations' );
break;
case 'drip':
$name = __( 'Drip', 'wp-marketing-automations' );
break;
case 'ck':
$name = __( 'Kit ( Formerly ConvertKit )', 'wp-marketing-automations' );
break;
case 'mailchimp':
$name = __( 'Mailchimp', 'wp-marketing-automations' );
break;
default:
break;
}
return $name;
}
/**
* checking data dependency
*
* @param $data_depend
*
* @return array|bool
*/
protected static function data_dependency_check( $data_depend ) {
if ( empty( $data_depend ) ) {
return true;
}
$data_error = [];
foreach ( $data_depend as $data ) {
$operator = isset( $data['operator'] ) ? $data['operator'] : '';
$current_value = isset( $data['current_value'] ) ? $data['current_value'] : '';
$check_value = isset( $data['check_value'] ) ? $data['check_value'] : '';
$data_check = self::check_data_value( $current_value, $operator, $check_value );
if ( false === $data_check ) {
$data_error[] = $data['message'];
return $data_error;
}
}
return empty( $data_error ) ? true : $data_error;
}
/**
* get operand to check the data dependency
*
* @param $operator
*
* @return string
*/
protected static function check_data_value( $current_value, $operator, $check_value ) {
switch ( $operator ) {
case '=':
return $current_value === $check_value ? true : false;
case '!=':
return $current_value !== $check_value ? true : false;
default:
return true;
}
}
/**
* creating recipe automation
*/
public function create_automation( $json_files, $title = '' ) {
if ( ! is_array( $json_files ) || empty( $json_files ) ) {
$resp = array(
'msg' => __( 'Recipe json files are missing', 'wp-marketing-automations' ),
'status' => false,
);
return $resp;
}
foreach ( $json_files as $file ) {
$json_path = plugin_dir_path( __FILE__ ) . '../recipes/json/' . $file . '.json';
/** checking is the json file exist then return */
if ( ! file_exists( $json_path ) ) {
continue;
}
$automation_data = json_decode( file_get_contents( $json_path ), true );
if ( empty( $automation_data ) && ! is_array( $automation_data ) ) {
continue;
}
foreach ( $automation_data as $import_data ) {
if ( ! isset( $import_data['meta']['title'] ) || '' === $import_data['meta']['title'] ) {
continue;
}
$post = array();
$post['status'] = 2;
$post['source'] = isset( $import_data['data']['source'] ) ? $import_data['data']['source'] : '';
$post['event'] = isset( $import_data['data']['event'] ) ? $import_data['data']['event'] : '';
$post['priority'] = 0;
if ( empty( $post ) ) {
continue;
}
BWFAN_Model_Automations::insert( $post );
$automation_id = BWFAN_Model_Automations::insert_id();
BWFAN_Core()->automations->set_automation_id( $automation_id );
BWFAN_Core()->automations->set_automation_data( 'status', $post['status'] );
if ( 0 === $automation_id && is_wp_error( $automation_id ) ) {
continue;
}
if ( ! empty( $import_data['meta'] ) ) {
foreach ( $import_data['meta'] as $key => $auto_meta ) {
if ( is_array( $auto_meta ) ) {
$auto_meta = maybe_serialize( $auto_meta );
} else {
$auto_meta = $auto_meta;
}
$meta = array();
$meta['bwfan_automation_id'] = $automation_id;
$meta['meta_key'] = $key;
if ( $key == 'title' && ! empty( $title ) ) {
$meta['meta_value'] = $title;
} else {
$meta['meta_value'] = $auto_meta;
}
BWFAN_Model_Automationmeta::insert( $meta );
BWFAN_Core()->automations->set_automation_data( $key, $meta['meta_value'] );
}
}
$meta = array();
$meta['bwfan_automation_id'] = $automation_id;
$meta['meta_key'] = 'c_date';
$meta['meta_value'] = current_time( 'mysql', 1 );
BWFAN_Model_Automationmeta::insert( $meta );
BWFAN_Core()->automations->set_automation_data( 'c_date', $meta['meta_value'] );
$meta = array();
$meta['bwfan_automation_id'] = $automation_id;
$meta['meta_key'] = 'm_date';
$meta['meta_value'] = current_time( 'mysql', 1 );
BWFAN_Model_Automationmeta::insert( $meta );
BWFAN_Core()->automations->set_automation_data( 'm_date', $meta['meta_value'] );
do_action( 'bwfan_automation_saved', $automation_id );
$resp['status'] = true;
$resp['redirect_url'] = add_query_arg( array(
'page' => 'autonami-automations',
), admin_url( 'admin.php' ) );
$resp['id'] = $automation_id;
$resp['msg'] = __( 'Recipe Successfully Imported', 'wp-marketing-automations' );
}
}
return $resp;
}
/**
* Create automation by recipe
*
* @param $recipe_slug
* @param $title
*/
public static function create_automation_by_recipe( $recipe_slug, $title ) {
$recipe_instance = $recipe_slug::get_instance();
$recipe_json = array_filter( $recipe_instance->data['json'] );
$automation = $recipe_instance->create_automation( $recipe_json, $title );
return $automation;
}
}

View File

@@ -0,0 +1,117 @@
<?php
/**
* Class BWFAN_Source
*/
#[AllowDynamicProperties]
abstract class BWFAN_Source {
protected $show_in_ui = true;
protected $localize_data = [];
protected $nice_name = '';
protected $event_dir = __DIR__;
protected $slug = '';
protected $group_slug = '';
protected $group_name = '';
protected $priority = 0;
/**
* Loads all events of current trigger
*/
public function load_events() {
$resource_dir = $this->event_dir . '/events';
if ( false === @file_exists( $resource_dir ) ) { //phpcs:ignore PHP_CodeSniffer - Generic.PHP.NoSilencedErrors, Generic.PHP.NoSilencedErrors
return;
}
foreach ( glob( $resource_dir . '/class-*.php' ) as $_field_filename ) {
$file_data = pathinfo( $_field_filename );
if ( isset( $file_data['basename'] ) && 'index.php' === $file_data['basename'] ) {
continue;
}
$event_class = require_once( $_field_filename );
if ( ! is_string( $event_class ) || ! method_exists( $event_class, 'get_instance' ) ) {
continue;
}
/**
* @var $event_obj BWFAN_Event
*/
$event_obj = $event_class::get_instance();
BWFAN_Load_Sources::$all_events[ $this->get_name() ][ $event_obj->get_slug() ] = $event_obj->get_name();
$event_obj->load_hooks();
if ( method_exists( $event_obj, 'admin_enqueue_assets' ) && is_admin() && BWFAN_Common::is_automation_v1_active() && BWFAN_Common::is_autonami_page() ) {
// Add action to avoid enqueueing assets on every admin page load
add_action( 'admin_enqueue_scripts', array( $event_obj, 'admin_enqueue_assets' ), 98 );
}
$event_obj->set_source_type( $this->get_slug() );
BWFAN_Load_Sources::register_events( $event_obj );
}
do_action( 'bwfan_' . $this->get_slug() . '_events_loaded' );
}
public function get_slug() {
$this->slug = str_replace( array( 'bwfan_', '_source' ), '', sanitize_title( get_class( $this ) ) );
return $this->slug;
}
public function get_group_slug() {
return $this->group_slug;
}
public function get_group_name() {
return $this->group_name;
}
public function get_localize_data() {
$this->localize_data = [
'show_in_ui' => $this->show_in_ui(),
'slug' => $this->get_slug(),
'group_slug' => $this->get_group_slug(),
'group_name' => $this->get_group_name(),
'nice_name' => $this->get_name(),
'priority' => $this->priority,
'available' => 'yes',
];
return $this->localize_data;
}
public function show_in_ui() {
return $this->show_in_ui;
}
public function get_name() {
return $this->nice_name;
}
public function get_priority() {
return $this->priority;
}
/**
* to avoid unserialize of the current class
*/
public function __wakeup() {
throw new ErrorException( esc_html__( 'BWFAN_Core cannot be converted to string', 'wp-marketing-automations' ) );
}
/**
* to avoid serialize of the current class
*/
public function __sleep() {
throw new ErrorException( esc_html__( 'BWFAN_Core can`t converted to string', 'wp-marketing-automations' ) );
}
/**
* To avoid cloning of current class
*/
protected function __clone() {
}
}

View File

@@ -0,0 +1,21 @@
<?php
if ( ! class_exists( 'BWFCRM_Term_Type' ) && BWFAN_Common::is_pro_3_0() ) {
abstract class BWFCRM_Term_Type {
public static $TAG = 1;
public static $LIST = 2;
}
if ( ! function_exists( 'bwfcrm_get_term_type_text' ) ) {
function bwfcrm_get_term_type_text( $type = 1 ) {
switch ( $type ) {
case BWFCRM_Term_Type::$TAG:
return 'Tag';
case BWFCRM_Term_Type::$LIST:
return 'List';
}
return 'Tag';
}
}
}

View File

@@ -0,0 +1,419 @@
<?php
if ( ! class_exists( 'BWFCRM_Term' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFCRM_Term {
protected $_id = null;
protected $_name = null;
protected $_type = null;
protected $_created_at = null;
protected $_updated_at = null;
protected $_newly_created = false;
protected $_data = '';
public static $tags_by_id = array();
public function __construct( $data = false, $type = 1, $force_create = false ) {
if ( empty( $data ) ) {
$this->_type = $type;
return;
}
/** If data is BWFCRM_Term */
if ( $data instanceof BWFCRM_Term ) {
$this->init_term( $data );
}
/** Check if the term is already stored in cache */
$term = self::maybe_get_term( $data );
if ( false !== $term && $term instanceof BWFCRM_Term ) {
$this->init_term( $term );
}
/** If data is an array */
if ( is_array( $data ) ) {
$this->init_term( $data );
}
/** If data is an integer */
if ( is_numeric( $data ) && 0 < absint( $data ) ) {
$this->init_term( BWFAN_Model_Terms::get( absint( $data ) ) );
}
/** If data is a string */
if ( ! empty( $data ) && is_string( $data ) ) {
$term_row = BWFAN_Model_Terms::get_term_by_name( $data, $type );
/** in case of not found then insert */
if ( empty( $term_row ) && $force_create ) {
$term_row = self::add_term_to_db( $data, $type );
}
if ( ! empty( $term_row ) ) {
$this->init_term( $term_row );
$this->_newly_created = true;
}
}
/** Store terms for further use, within current HTTP Request */
if ( ! empty( $this->get_id() ) ) {
self::$tags_by_id[ $this->get_id() ] = $this;
}
}
public static function maybe_get_term( $data = false ) {
if ( is_array( $data ) && isset( $data['ID'] ) && isset( self::$tags_by_id[ absint( $data['ID'] ) ] ) ) {
return self::$tags_by_id[ absint( $data['ID'] ) ];
}
if ( is_numeric( $data ) && absint( $data ) > 0 ) {
if ( isset( self::$tags_by_id[ absint( $data ) ] ) ) {
return self::$tags_by_id[ absint( $data ) ];
}
}
return false;
}
public function is_exists() {
return is_numeric( $this->get_id() ) && ! empty( $this->get_id() );
}
public function is_newly_created() {
return $this->_newly_created;
}
public static function add_term_to_db( $term_name, $type = 0 ) {
BWFAN_Model_Terms::insert( array(
'name' => $term_name,
'type' => $type,
'created_at' => current_time( 'mysql', 1 ),
) );
return BWFAN_Model_Terms::get( BWFAN_Model_Terms::insert_id() );
}
private function init_term( $db_term ) {
if ( $db_term instanceof BWFCRM_Term ) {
$db_term = array(
'ID' => $db_term->get_id(),
'name' => $db_term->get_name(),
'type' => $db_term->get_type(),
'created_at' => $db_term->get_created_at(),
);
}
$this->_id = isset( $db_term['ID'] ) ? absint( $db_term['ID'] ) : 0;
if ( empty( $this->_id ) && isset( $db_term['_id'] ) ) {
$this->_id = $db_term['_id'];
}
$this->_type = isset( $db_term['type'] ) ? $db_term['type'] : - 1;
if ( empty( $this->_type ) && isset( $db_term['_type'] ) ) {
$this->_type = $db_term['_type'];
}
$this->_name = isset( $db_term['name'] ) ? $db_term['name'] : '';
if ( empty( $this->_name ) && isset( $db_term['_name'] ) ) {
$this->_name = $db_term['_name'];
}
$this->_created_at = isset( $db_term['created_at'] ) ? $db_term['created_at'] : null;
if ( empty( $this->_created_at ) && isset( $db_term['_created_at'] ) ) {
$this->_created_at = $db_term['_created_at'];
}
$this->_updated_at = isset( $db_term['updated_at'] ) ? $db_term['updated_at'] : null;
if ( empty( $this->_updated_at ) && isset( $db_term['_updated_at'] ) ) {
$this->_updated_at = $db_term['_updated_at'];
}
$this->_data = isset( $db_term['data'] ) && ! empty( $db_term['data'] ) ? json_decode( $db_term['data'], true ) : null;
if ( empty( $this->_data ) && isset( $db_term['_data'] ) && ! empty( $db_term['_data'] ) ) {
$this->_data = json_decode( $db_term['_data'], true );
}
}
public function save() {
if ( empty( $this->_name ) || empty( $this->_type ) ) {
return BWFAN_Common::crm_error( __( 'Required term data is missing.', 'wp-marketing-automations' ) );
}
$term = array(
'name' => $this->_name,
'type' => $this->_type,
'data' => empty( $this->_data ) ? '' : wp_json_encode( $this->_data ),
'updated_at' => current_time( 'mysql', 1 ),
);
if ( ! empty( $this->_id ) ) {
return BWFAN_Model_Terms::update( $term, array( 'ID' => absint( $this->_id ) ) );
} else {
$term['created_at'] = $term['updated_at'];
BWFAN_Model_Terms::insert( $term );
return BWFAN_Model_Terms::insert_id();
}
}
public function get_data() {
return $this->_data;
}
public function get_id() {
return absint( $this->_id );
}
public function get_name() {
return $this->_name;
}
public function set_name( $name ) {
$this->_name = $name;
}
public function get_type() {
return $this->_type;
}
public function get_created_at() {
return $this->_created_at;
}
public function get_updated_at() {
return $this->_updated_at;
}
public function get_array() {
return array(
'ID' => $this->get_id(),
'name' => $this->get_name(),
'type' => $this->get_type(),
'created_at' => $this->get_created_at(),
'updated_at' => $this->get_updated_at(),
'data' => $this->get_data(),
);
}
/**
* @param BWFCRM_Term[] $objects
* @param string $key
*
* @return array[]
*/
public static function get_collection_array( $objects, $key = '' ) {
if ( ! is_array( $objects ) || empty( $objects ) ) {
return array();
}
$array = array_map( function ( $object ) use ( $key ) {
if ( ! $object instanceof BWFCRM_Term ) {
return false;
}
if ( ! empty( $key ) ) {
$value = call_user_func( array( $object, 'get_' . $key ) );
return $value;
}
return $object->get_array();
}, $objects );
return array_filter( $array );
}
public static function get_objects_from_db_rows( $data = array() ) {
if ( ! is_array( $data ) || empty( $data ) ) {
return array();
}
return array_map( function ( $single_data ) {
if ( $single_data instanceof BWFCRM_Term ) {
return $single_data;
}
return ( BWFCRM_Term_Type::$TAG === absint( $single_data['type'] ) ? ( new BWFCRM_Tag( $single_data ) ) : ( new BWFCRM_Lists( $single_data ) ) );
}, $data );
}
public static function get_array_from_db_rows( $data = array() ) {
if ( ! is_array( $data ) || empty( $data ) ) {
return array();
}
return array_map( function ( $single_data ) {
/** To store the db_row in cache i.e.: BWFCRM_Term::$tags_by_id & BWFCRM_Term::$tags_by_slug */
$term = BWFCRM_Term_Type::$TAG === absint( $single_data['type'] ) ? ( new BWFCRM_Tag( $single_data ) ) : ( new BWFCRM_Lists( $single_data ) );
return $term->get_array();
}, $data );
}
public static function get_terms( $type = 1, $ids = array(), $search = '', $offset = 0, $limit = 0, $return = ARRAY_A, $search_nature = '', $use_cache = false ) {
$db_terms = BWFAN_Model_Terms::get_terms( $type, $offset, $limit, $search, $ids, $search_nature, $use_cache );
return ( ARRAY_A === $return ? self::get_array_from_db_rows( $db_terms ) : self::get_objects_from_db_rows( $db_terms ) );
}
public static function get_terms_by_type( $type = 1, $return = ARRAY_A ) {
$db_terms = BWFAN_Model_Terms::get_specific_rows( 'type', $type );
return ARRAY_A === $return ? self::get_array_from_db_rows( $db_terms ) : self::get_objects_from_db_rows( $db_terms );
}
/**
* @param $terms
* @param $type
*
* @return bool
*/
public static function add_terms_to_db( $terms, $type ) {
$terms = array_map( function ( $term ) use ( $type ) {
return array(
'name' => $term,
'type' => $type,
'created_at' => current_time( 'mysql', 1 ),
);
}, $terms );
$result = BWFAN_Model_Terms::insert_multiple( $terms, array( 'name', 'type', 'created_at' ) );
if ( false === $result ) {
return $result;
}
return true;
}
/**
* Terms in Apply Contact Tag will be in this format:
* [
* [ 'id'=> 3, 'name'=>'Product' ],
* [ 'id'=> 1, 'name'=>'Hello' ],
* [ 'id'=> 0, 'name'=>'Hi' ],
* [ 'id'=> 0, 'name'=>'Done' ],
* ]
*
* So we need to differentiate which terms are new and which are already available in DB.
* This method differentiate that
*
* @param $terms
* @param int $type
* @param bool $use_cache
*
* @return array
*/
public static function parse_terms_request( $terms, $type = 1, $use_cache = false ) {
/** Separate new terms from existing terms */
$existing_terms = array();
$new_terms_to_add = array_map( function ( $term ) use ( &$existing_terms ) {
if ( 0 !== absint( $term['id'] ) ) {
$existing_terms[] = $term;
return false;
}
return $term['value'];
}, $terms );
$new_terms_to_add = array_filter( $new_terms_to_add );
/** Get terms by IDs */
$existing_term_ids = array_map( 'absint', array_filter( array_column( $existing_terms, 'id' ) ) );
$db_existing_terms = array();
if ( ! empty( $existing_term_ids ) ) {
$db_existing_terms = BWFAN_Model_Terms::get_terms( $type, 0, count( $existing_term_ids ), '', $existing_term_ids, '', $use_cache );
}
/** Check if any term by IDs missing from DB, then add that term name to "need to be created terms" */
$db_term_ids = array_map( 'absint', array_column( $db_existing_terms, 'ID' ) );
if ( count( $db_existing_terms ) !== count( $existing_terms ) ) {
foreach ( $existing_terms as $e_term ) {
if ( false === array_search( absint( $e_term['id'] ), $db_term_ids ) ) {
$new_terms_to_add[] = $e_term['value'];
}
}
}
/** Array Unique => the (To be Created) term names, and run SQL to check if any term exists by their name */
$new_terms_to_add = array_unique( $new_terms_to_add );
$db_new_terms_to_add = array();
if ( ! empty( $new_terms_to_add ) ) {
$db_new_terms_to_add = BWFAN_Model_Terms::get_terms( $type, 0, 0, $new_terms_to_add, array(), 'exact', $use_cache );
}
/** If any exists, add the term ID to "Need to assigned terms" and remove from "to be created" terms names */
if ( ! empty( $db_new_terms_to_add ) ) {
foreach ( $db_new_terms_to_add as $e_term ) {
if ( false === array_search( $e_term['name'], $new_terms_to_add ) ) {
continue;
}
$new_terms_to_add = array_diff( $new_terms_to_add, array( $e_term['name'] ) );
if ( false === array_search( $e_term['ID'], $db_term_ids ) ) {
$db_term_ids[] = $e_term['ID'];
}
}
}
/** Array Unique => the (To be Assigned) term IDs. (In any case if duplication exists) */
$db_term_ids = array_unique( $db_term_ids );
return array(
'ids' => $db_term_ids,
'names' => $new_terms_to_add,
);
}
/**
* @param array $terms
* @param int $type
* @param bool $create_if_not_exists
* @param bool $separate_created_terms
* @param bool $use_cache
*
* @return array|array[]|BWFCRM_Lists[]|BWFCRM_Tag[]|BWFCRM_Term[]
*/
public static function get_or_create_terms( $terms = array(), $type = 1, $create_if_not_exists = false, $separate_created_terms = false, $use_cache = false ) {
$terms = self::parse_terms_request( $terms, $type, $use_cache );
if ( ! isset( $terms['ids'] ) || ! isset( $terms['names'] ) ) {
return true === $separate_created_terms ? array(
'created' => array(),
'existing' => array(),
) : array();
}
$existing_terms = array();
if ( ! empty( $terms['ids'] ) ) {
$existing_terms = self::get_terms( $type, $terms['ids'], '', 0, 0, OBJECT, '', $use_cache );
}
if ( false === $create_if_not_exists || empty( $terms['names'] ) ) {
return true === $separate_created_terms ? array(
'created' => array(),
'existing' => $existing_terms,
) : $existing_terms;
}
/** Create rest of the terms */
$terms_created = self::add_terms_to_db( $terms['names'], $type );
$new_terms = array();
if ( $terms_created ) {
$new_terms = self::get_terms( $type, array(), $terms['names'], 0, 0, OBJECT, 'exact', $use_cache );
}
/** Get added terms */
if ( false === $separate_created_terms ) {
return array_merge( $existing_terms, $new_terms );
}
return array(
'created' => $new_terms,
'existing' => $existing_terms,
);
}
}
}

View File

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

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' );

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