Commit inicial - WordPress Análisis de Precios Unitarios

- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-11-03 21:04:30 -06:00
commit a22573bf0b
24068 changed files with 4993111 additions and 0 deletions

View File

@@ -0,0 +1,300 @@
<?php
/**
* Class BWFAN_Rest_API_Base
*/
if ( ! class_exists( 'BWFAN_Rest_API_Base' ) ) {
abstract class BWFAN_Rest_API_Base {
public $route = null;
public $method = null;
public $required = [];
public $validate = [];
public $response_code = 200;
/**
* @var stdClass $pagination
*
* It contains two keys: Limit and Offset, for pagination purposes
*/
public $pagination = null;
public $args = array();
public $request_args = array();
public function __construct() {
$this->pagination = new stdClass();
$this->pagination->limit = 0;
$this->pagination->offset = 0;
}
/**
* @param WP_REST_Request $request
* common function for all the apis
* it collects the data and prepare it for processing
*
* @return WP_Error
*/
public function api_call( WP_REST_Request $request ) {
$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 );
}
$params['limit'] = $params['limit'] ?? 25;
$params['files'] = $request->get_file_params();
$this->pagination->limit = $params['limit'] ?? $this->pagination->limit;
$page = isset( $params['page'] ) && ! empty( $params['page'] ) ? intval( $params['page'] ) : 0;
if ( isset( $params['offset'] ) && ! empty( $params['offset'] ) ) {
$this->pagination->offset = $params['offset'];
} else {
$this->pagination->offset = ( $page > 0 ) ? ( ( $page - 1 ) * $this->pagination->limit ) : 0;
$params['offset'] = $this->pagination->offset;
}
$this->args = wp_parse_args( $params, $this->default_args_values() );
try {
/** Check if required fields missing */
$this->check_required_fields();
$data_obj = $this->validate_data();
return $this->process_api_call( $data_obj );
} catch ( Error $e ) {
$this->error_response( $e->getMessage(), 500 );
}
}
protected function get_current_api_method() {
return $_SERVER['REQUEST_METHOD'];
}
public function check_required_fields() {
if ( empty( $this->required ) || ! is_array( $this->required ) ) {
return;
}
if ( array_intersect( array_keys( $this->args ), $this->required ) ) {
$verified = true;
foreach ( $this->required as $field ) {
/**
* Checking required field is empty
*/
if ( empty( $this->args[ $field ] ) ) {
$verified = false;
break;
}
}
if ( true === $verified ) {
return;
}
}
BWFCRM_Core()->response_handler->error_response( '', 400, null, 'required_fields_missing' );
}
/**
* Validate requested data
* @return void
*/
public function validate_data() {
if ( empty( $this->validate ) || ! is_array( $this->validate ) ) {
return;
}
$validated = true;
$err_code = '';
$data_obj = '';
$data_obj_id = [];
$code = 422;
$id = $this->get_data_id();
foreach ( $this->validate as $data ) {
switch ( true ) {
case 'contact_already_exists' === $data:
if ( ! isset( $data_obj_id['contact'] ) || ! isset( $data_obj_id['contact']['id'] ) ) {
$data_obj_id['contact']['id'] = $id;
$data_obj = BWFAN_Rest_API_Common::is_contact_exists( $id );
}
if ( false !== $data_obj ) {
$validated = false;
$err_code = 'already_exists';
}
break;
case 'contact_not_exists' === $data:
if ( ! isset( $data_obj_id['contact'] ) || ! isset( $data_obj_id['contact']['id'] ) ) {
$data_obj_id['contact']['id'] = $id;
$data_obj = BWFAN_Rest_API_Common::is_contact_exists( $id );
}
if ( false === $data_obj ) {
$validated = false;
$err_code = 'contact_not_exists';
}
break;
case 'field_not_exists' === $data:
if ( ! isset( $data_obj_id['field'] ) || ! isset( $data_obj_id['field']['id'] ) ) {
$data_obj_id['field']['id'] = $id;
$data_obj = BWFCRM_Fields::is_field_exists( $id );
}
if ( false === $data_obj ) {
$validated = false;
$code = 404;
$err_code = 'data_not_found';
}
break;
case 'list_not_exists' === $data:
if ( ! isset( $data_obj_id['list'] ) || ! isset( $data_obj_id['list']['id'] ) ) {
$data_obj_id['list']['id'] = $id;
$data_obj = new BWFCRM_Lists( $id );
}
if ( false === $data_obj->is_exists() ) {
$validated = false;
$code = 404;
$err_code = 'data_not_found';
}
break;
case 'tag_not_exists' === $data:
if ( ! isset( $data_obj_id['tag'] ) || ! isset( $data_obj_id['tag']['id'] ) ) {
$data_obj_id['tag']['id'] = $id;
$data_obj = new BWFCRM_Tag( $id );
}
if ( false === $data_obj->is_exists() ) {
$validated = false;
$code = 404;
$err_code = 'data_not_found';
}
break;
}
if ( false === $validated ) {
break;
}
}
/** Return if data is validated */
if ( true === $validated ) {
return $data_obj;
}
BWFCRM_Core()->response_handler->error_response( '', $code, null, $err_code );
}
/**
* Get requested data id
* @return mixed|string
*/
public function get_data_id() {
$email = isset( $this->args['email'] ) ? $this->args['email'] : '';
return isset( $this->args['id'] ) ? $this->args['id'] : $email;
}
public function error_response( $message = '', $code = 0, $err_code = '', $wp_error = null ) {
BWFCRM_Core()->response_handler->error_response( $message, $code, $wp_error, $err_code );
}
/**
* @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 );
}
/** To be implemented in Child Class. Override in Child Class */
public function get_result_total_count() {
return 0;
}
/** To be implemented in Child Class. Override in Child Class */
public function get_result_count_data() {
return 0;
}
/** To be implemented in Child Class. Override in Child Class */
public function get_result_extra_data() {
return [];
}
public function default_args_values() {
return array();
}
/**
* @param $result_array
* @param string $message
* adding extra response data and sending to response handler
*
* @return mixed
*/
public function success_response( $result_array, $message = '' ) {
BWFCRM_Core()->response_handler->response_code = 200;
$extra_data = array();
/** Total Count */
$total_count = $this->get_result_total_count();
if ( ! empty( $total_count ) ) {
$extra_data['total_count'] = $total_count;
}
/** Count Data */
$count_data = $this->get_result_count_data();
if ( ! empty( $count_data ) ) {
$extra_data['count_data'] = $count_data;
}
/** Extra data */
$extra_data = $this->get_result_extra_data();
if ( ! empty( $extra_data ) ) {
$extra_data['extra_data'] = $extra_data;
}
$api_method = ! empty( $this->get_current_api_method() ) ? $this->get_current_api_method() : 'GET';
/** Pagination */
if ( isset( $this->pagination->limit ) && ( 0 === $this->pagination->limit || ! empty( $this->pagination->limit ) ) && 'GET' === $api_method ) {
$extra_data['limit'] = absint( $this->pagination->limit );
}
if ( isset( $this->pagination->offset ) && ( 0 === $this->pagination->offset || ! empty( $this->pagination->offset ) ) && 'GET' === $api_method ) {
$extra_data['offset'] = absint( $this->pagination->offset );
}
return BWFCRM_Core()->response_handler->success_response( $result_array, $message, $extra_data );
}
/**
* This function should be present in all the classes inheriting this class
*
* @param $data_obj
*
* @return mixed
*/
abstract public function process_api_call();
protected function get_contact_statuses() {
return array( "unverified" => 0, "subscribed" => 1, "bounced" => 2, "unsubscribed" => 3 );
}
}
}

View File

@@ -0,0 +1,136 @@
<?php
namespace BWFCRM\Actions;
/**
* Actions base class
*/
#[\AllowDynamicProperties]
abstract class Base {
/** Handle action response type */
public static $RESPONSE_FAILED = 1;
public static $RESPONSE_SUCCESS = 2;
public static $RESPONSE_SKIPPED = 3;
/**
* Action slug
*
* @var string
*/
protected $slug = '';
/**
* Action nice name
*
* @var string
*/
protected $nice_name = '';
/**
* Action group
*
* @var string
*/
protected $group = '';
/**
* Action group nice name
*
* @var string
*/
protected $group_label = '';
/**
* Action priority to show
*
* @var int
*/
protected $priority = 10;
/**
* Actions support 1 - link triggers, 2 - bulk actions
*
* @var array
*/
protected $support = [];
/**
* Autonami Event slug
* @var string
*/
protected $event_slug = '';
/**
* Returns action slug
*
* @return string
*/
public function get_action_slug() {
return $this->slug;
}
/**
* Returns Actions nice name
*
* @return string
*/
public function get_action_nice_name() {
return $this->nice_name;
}
/**
* Return Action Group slug
*
* @return string
*/
public function get_action_group() {
return $this->group;
}
/**
* Return Action Group nicename
*
* @return string
*/
public function get_action_group_nicename() {
return $this->group_label;
}
/**
* Return Action Group priority
*
* @return int
*/
public function get_action_priority() {
return $this->priority;
}
/**
* Return Action Group supported features
*
* @return array
*/
public function get_action_support() {
return $this->support;
}
/**
* Returns action event slug
*
* @return string
*/
public function get_action_event_slug() {
return $this->event_slug;
}
/**
* Abstract function to get schema
*/
abstract public function get_action_schema();
/**
* Abstract function to process action
*/
abstract public function handle_action( $contact, $data );
}

View File

@@ -0,0 +1,244 @@
<?php
#[AllowDynamicProperties]
abstract class BWFCRM_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_PRO_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( method_exists( 'BWFAN_Common', 'get_mail_replace_string' ) && 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->limit = apply_filters( 'bwfcrm_api_pagination_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 {
/**
* Include necessary classes so that can run admin rest endpoints.
*/
do_action( 'bwfan_rest_call' );
return $this->process_api_call();
} catch ( Exception $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 be implemented in Child Class. Override in Child Class */
public function get_result_count_data() {
return 0;
}
/** To be implemented in Child Class. Override in Child Class */
public function get_result_extra_data() {
return [];
}
public function error_response( $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' => $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 = BWFCRM_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;
}
/** Extra data */
$extra_data = $this->get_result_extra_data();
if ( ! empty( $extra_data ) ) {
$response['extra_data'] = $extra_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 ) {
$default_permissions = array( 'manage_options' );
$permissions = method_exists( 'BWFAN_Common', 'access_capabilities' ) ? BWFAN_Common::access_capabilities() : $default_permissions;
foreach ( $permissions as $permission ) {
if ( current_user_can( $permission ) ) {
return true;
}
}
return false;
}
public function get_sanitized_url( $key = '' ) {
if ( empty( $key ) || ! isset( $this->args[ $key ] ) || empty( $this->args[ $key ] ) ) {
return false;
}
$url = esc_url_raw( $this->args[ $key ] );
if ( false === wp_http_validate_url( $url ) ) {
return false;
}
return $url;
}
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 : ' . $id_or_email, 'wp-marketing-automations-pro' );
} else {
$response = __( 'Contact not exists with email:' . $id_or_email, 'wp-marketing-automations-pro' );
}
return $this->error_response( $response );
}
return $contact;
}
}

View File

@@ -0,0 +1,59 @@
<?php
#[AllowDynamicProperties]
abstract class BWFCRM_API_Start_Import_Base extends BWFCRM_API_Base {
public $importer_slug = '';
public $importer_name = '';
protected function before_create_import( $importer ) {
return;
}
public function process_api_call() {
if ( empty( $this->importer_slug ) || empty( $this->importer_name ) ) {
return $this->error_response( __( 'Invalid Importer name or slug', 'wp-marketing-automations-pro' ), null, 500 );
}
/** @var BWFCRM_Importer_Base $importer */
$importer = BWFCRM_Core()->importer->get_importer( $this->importer_slug );
if ( ! $importer instanceof BWFCRM_Importer_Base ) {
$this->error_response( $this->importer_name . __( 'Importer not found', 'wp-marketing-automations-pro' ), null, 500 );
}
$start_import = $this->get_sanitized_arg( 'start_import', 'bool' );
$import_id = $this->get_sanitized_arg( 'import_id', 'text_field' );
$import_id = empty( $import_id ) ? 0 : absint( $import_id );
/** Get Status if not start import */
if ( ! $start_import ) {
if ( empty( $import_id ) ) {
return $this->error_response( __( 'Invalid Import ID provided.', 'wp-marketing-automations-pro' ), null, 500 );
}
$stats = $importer->get_import_status( $import_id );
return is_string( $stats ) ? $this->error_response( $stats, null, 500 ) : $this->success_response( $stats, '' );
}
/** Inputs */
$tags = isset( $this->args['tags'] ) && is_array( $this->args['tags'] ) ? $this->args['tags'] : array();
$lists = isset( $this->args['lists'] ) && is_array( $this->args['lists'] ) ? $this->args['lists'] : array();
$update_existing = $this->get_sanitized_arg( 'update_existing', 'bool' );
$marketing_status = $this->get_sanitized_arg( 'marketing_status', 'bool' );
$disable_events = $this->get_sanitized_arg( 'disable_events', 'bool' );
$imported_contact_status = $this->get_sanitized_arg( 'imported_contact_status', 'text_field' );
/** Before Creating Import */
$this->before_create_import( $importer );
/** Create Import & Start Importing */
$import_id = $importer->create_import( $tags, $lists, $update_existing, $marketing_status, ! $disable_events, $imported_contact_status );
if ( empty( $import_id ) ) {
return $this->error_response( __( 'Unable to create Import for: ', 'wp-marketing-automations-pro' ) . $this->importer_name, null, 500 );
}
/** Get and Send status */
$stats = $importer->get_import_status( $import_id );
return is_string( $stats ) ? $this->error_response( $stats, null, 500 ) : $this->success_response( $stats, '' );
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace BWFCRM\Calls;
/**
* Calls base class
*/
#[\AllowDynamicProperties]
abstract class Base {
/** Handle action response type */
public static $RESPONSE_FAILED = 1;
public static $RESPONSE_SUCCESS = 2;
public static $RESPONSE_SKIPPED = 3;
/**
* Abstract function to process call
*
* @param $contact
* @param $data
*/
abstract public function process_call( $contact, $data );
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* BWFCRM_Custom_Filter_Base Abstract Class
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class BWFCRM_Custom_Filter_Base
*/
#[AllowDynamicProperties]
abstract class BWFCRM_Custom_Filter_Base {
public $name = '';
/** Helper Functions */
public function maybe_get_current_filter( $custom_filters ) {
if ( ! is_array( $custom_filters ) ) {
return false;
}
foreach ( $custom_filters as $filter ) {
if ( ! is_array( $filter ) || ! isset( $filter['key'] ) || $this->name !== $filter['key'] ) {
continue;
}
return $filter;
}
return false;
}
/** Check if current filter exists in an array of filters */
public function is_current_filter_exists( $custom_filters ) {
$filter = $this->maybe_get_current_filter( $custom_filters );
return false !== $filter;
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* BWFCRM_Email_Webhook_Base Abstract Class
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class BWFCRM_Email_Webhook_Base
*
*/
#[\AllowDynamicProperties]
abstract class BWFCRM_Email_Webhook_Base {
protected $_data = [];
protected $_name = "";
public function set_data( $data = [] ) {
$this->_data = $data;
}
public function get_name() {
return $this->_name;
}
abstract public function handle_webhook();
protected function mark_contact_bounce( $contact_email ) {
$contact = new BWFCRM_Contact( $contact_email );
if ( ! $contact->mark_as_bounced() ) {
BWFAN_Core()->logger->log( "Unable to change contact status to bounce. Contact not found of email: $contact_email", 'crm_email_webhooks' );
}
}
protected function mark_contact_soft_bounce( $contact_email ) {
$contact = new BWFCRM_Contact( $contact_email );
if ( method_exists( $contact, 'mark_as_soft_bounced' ) && ! $contact->mark_as_soft_bounced() ) {
BWFAN_Core()->logger->log( "Unable to change contact status to soft bounce. Contact not found of email: $contact_email", 'crm_email_webhooks' );
}
/** Older FKA lite version do nothing */
}
protected function mark_contact_complaint( $contact_email ) {
$contact = new BWFCRM_Contact( $contact_email );
if ( method_exists( $contact, 'mark_as_complaint' ) ) {
if ( ! $contact->mark_as_complaint() ) {
BWFAN_Core()->logger->log( "Unable to change contact status to complaint. Contact not found of email: $contact_email", 'crm_email_webhooks' );
}
return;
}
/** Older FKA lite version */
if ( ! $contact->mark_as_bounced() ) {
BWFAN_Core()->logger->log( "Unable to change contact status to complaint first then bounce. Contact not found of email: $contact_email", 'crm_email_webhooks' );
}
}
}

View File

@@ -0,0 +1,279 @@
<?php
#[AllowDynamicProperties]
abstract class BWFCRM_Form_Base {
/** Active Feeds based on form, for Create/Update contact */
/** @var BWFCRM_Form_Feed[] $feeds */
protected $feeds = array();
protected $cid = 0;
abstract public function capture_async_submission();
public function find_feeds_and_create_contacts() {
/** Form Source */
$source = $this->get_source();
/** Find Active Feeds */
/** @var BWFCRM_Form_Feed[] $feeds */
$this->feeds = BWFCRM_Core()->forms->get_active_feeds_by_form( $source );
if ( ! is_array( $this->feeds ) || empty( $this->feeds ) ) {
return;
}
/** Filter for feeds related to current submitted form entry */
$this->feeds = $this->filter_feeds_for_current_entry();
if ( ! is_array( $this->feeds ) || empty( $this->feeds ) ) {
return;
}
/** Create Contacts based on those filtered & active feeds */
$this->create_contacts_from_feeds();
}
abstract public function filter_feeds_for_current_entry();
public function create_contacts_from_feeds() {
foreach ( $this->feeds as $feed ) {
$mapped_fields = $feed->get_data( 'mapped_fields' );
$update_existing = $feed->get_data( 'update_existing' );
// Parameter usage is different as per name ( if enabled restrict blank data from update )
$dont_update_blank = $feed->get_data( 'update_blank' );
$contact_data = $this->prepare_contact_data_from_feed_entry( $mapped_fields );
/** Filter blank value from data */
if ( $dont_update_blank ) {
$contact_data = array_filter( $contact_data, function ( $data ) {
return '' !== $data;
} );
}
if ( ! is_array( $contact_data ) || empty( $contact_data ) || ! isset( $contact_data['email'] ) ) {
continue;
}
$contact_data['email'] = trim( $contact_data['email'] );
$cid = isset( $this->cid ) && ! empty( $this->cid ) ? absint( $this->cid ) : 0;
/** Try to Get that Contact */
$contact = new BWFCRM_Contact( ! empty( $cid ) ? $cid : $contact_data['email'] );
/** Increment the Submissions */
$feed->increment_contacts();
$feed->save();
/** Conditions to check if contact is freshly created or not */
$contact_created_at = $contact->is_contact_exists() && ! empty( $contact->contact->get_creation_date() ) ? strtotime( $contact->contact->get_creation_date() ) : false;
$contact_recently_added = ! empty( $contact_created_at ) && ( strtotime( current_time( 'mysql' ) ) - $contact_created_at <= 120 );
/**
* If Contact not exists, or Recently Added, or on FLAG:update existing contact = true.
* Then save the contact, and it's fields
*/
if ( ! $contact->is_contact_exists() || $contact_recently_added || 1 === absint( $update_existing ) ) {
$newly_created = ( ! $contact->is_contact_exists() || $contact_recently_added );
$contact = $this->set_contact_data( $contact, $feed, $contact_data, $newly_created );
$trigger_events = $feed->get_data( 'trigger_events' );
if ( ! $trigger_events && isset( $contact->contact->is_subscribed ) ) {
$contact->contact->is_subscribed = false;
}
$contact->save();
$contact->save_fields();
}
if ( $feed->get_data( 'not_send_to_subscribed' ) && $contact->contact->get_status() == 1 && empty( $contact->check_contact_unsubscribed() ) ) { //also check for not unsubscribe
continue;
}
BWFCRM_Core()->forms->send_incentive_email( $contact, $feed );
}
}
/**
* @param BWFCRM_Contact $contact
* @param BWFCRM_Form_Feed $feed
* @param $contact_data
* @param $new Bool
*
* @return BWFCRM_Contact $contact
*/
public function set_contact_data( $contact, $feed, $contact_data, $new ) {
$cid = isset( $this->cid ) && ! empty( $this->cid ) ? absint( $this->cid ) : 0;
$contact->set_data( $contact_data, $cid );
if ( ! $contact->contact instanceof WooFunnels_Contact ) {
return $contact;
}
/** Set Source to current Form, if source is empty */
if ( empty( $contact->contact->get_source() ) ) {
$contact->contact->set_source( $this->get_source() );
}
/** Set Form Feed ID field, if empty */
if ( empty( $contact->get_field_by_slug( 'form-feed-id' ) ) ) {
$contact->set_field_by_slug( 'form-feed-id', $feed->get_id() );
}
$marketing_status = $feed->get_data( 'marketing_status' );
/** If no status available to set */
if ( 0 === intval( $marketing_status ) ) {
return $this->apply_terms_to_contact_from_feed( $contact, $feed );
}
/** Auto-confirm contacts is enable */
/** If contact unsubscribed then resubscribe */
if ( 1 === intval( $contact->contact->get_status() ) && $contact->check_contact_unsubscribed() ) {
$contact->resubscribe();
/** Set Tags and Lists */
return $this->apply_terms_to_contact_from_feed( $contact, $feed );
}
/** Set status */
$contact->contact->set_status( intval( $marketing_status ) );
/** Set Tags and Lists */
return $this->apply_terms_to_contact_from_feed( $contact, $feed );
}
/**
* @param BWFCRM_Contact $contact
* @param BWFCRM_Form_Feed $feed
*/
public function apply_terms_to_contact_from_feed( $contact, $feed ) {
$tags = $feed->get_data( 'tags' );
$lists = $feed->get_data( 'lists' );
$trigger_events = $feed->get_data( 'trigger_events' );
$tags = is_array( $tags ) ? array_filter( array_values( $tags ) ) : array();
$lists = is_array( $lists ) ? array_filter( array_values( $lists ) ) : array();
! empty( $tags ) && $contact->set_tags( $tags, false, ! $trigger_events );
! empty( $lists ) && $contact->set_lists( $lists, false, ! $trigger_events );
return $contact;
}
/**
* @param BWFCRM_Form_Feed $feed
*
* @return bool
*/
public function is_selected_form_valid( $feed ) {
if ( ! $feed instanceof BWFCRM_Form_Feed ) {
return false;
}
$data = $feed->get_data();
if ( ! is_array( $data ) || empty( $data ) ) {
return false;
}
$steps = $this->get_form_selection( $data, true );
return $this->verify_selected_data( $steps, $data );
}
/**
* Verify if the selected data is valid
*
* @param array $steps
* @param array $data
*
* @return bool
*/
public function verify_selected_data( $steps, $data ) {
if ( ! is_array( $steps ) || empty( $steps ) ) {
return false;
}
$form_meta = $this->get_meta();
$form_meta = array_keys( $form_meta['form_selection_fields'] );
foreach ( $form_meta as $index => $selection ) {
if ( ! isset( $data[ $selection ] ) || empty( $data[ $selection ] ) ) {
return false;
}
$step_no = absint( $index ) + 1;
if ( ! isset( $steps[ $step_no ] ) || ! is_array( $steps[ $step_no ] ) ) {
return false;
}
$step = $steps[ $step_no ]['options'];
$step_value = $data[ $selection ];
$value_found = false;
foreach ( $step as $group ) {
if ( is_array( $group ) && isset( $group[ $step_value ] ) ) {
$value_found = true;
break;
}
}
if ( false === $value_found ) {
return false;
}
}
return true;
}
/**
* @param array $mapped_fields
*
* @return mixed
*/
abstract public function prepare_contact_data_from_feed_entry( $mapped_fields );
abstract public function get_form_selection( $args, $return_all_available );
abstract public function update_form_selection( $args, $feed_id );
abstract public function get_total_selection_steps();
/**
* Returned Form Fields must be in this format ['field_slug'=>'field_name']
*
* @param $feed
*
* @return mixed
*/
abstract public function get_form_fields( $feed );
public function format_form_fields_for_mapping( $fields ) {
if ( ! is_array( $fields ) || empty( $fields ) ) {
return array();
}
$map_fields = array();
foreach ( $fields as $slug => $field ) {
$map_fields[] = array(
'index' => $slug,
'header' => $field,
);
}
return $map_fields;
}
/**
* @return array
*/
abstract public function get_meta();
/** Get Form Source Slug */
abstract public function get_source();
/** Provide link for the id */
abstract public function get_form_link( $feed );
public function get_step_selection_array( $step_name, $step_slug, $step_no, $step_options = array() ) {
return array(
$step_no => array(
'name' => $step_name,
'slug' => $step_slug,
'options' => $step_options,
),
);
}
}

View File

@@ -0,0 +1,6 @@
<?php
abstract class BWFCRM_Import_Export_Type {
public static $IMPORT = 1;
public static $EXPORT = 2;
}

View File

@@ -0,0 +1,23 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class BWFCRM_Importer
*
* @package Autonami CRM
*/
abstract class BWFCRM_Importer_Base {
protected $log_file = null;
public function get_import_status( $import_id ) {
return array();
}
public function create_import( $tags = array(), $lists = array(), $update_existing = false, $marketing_status = false, $disable_events = true, $imported_contact_status = 1 ) {
return 0;
}
}

View File

@@ -0,0 +1,99 @@
<?php
#[AllowDynamicProperties]
abstract class BWFCRM_Merge_Tag_Base {
protected $_tag_name = '';
protected $_tag_description = '';
public function shortcode_callback( $atts = array() ) {
$tag_data = BWFCRM_Core()->merge_tags->get_data();
$atts = is_array( $atts ) ? array_replace( $atts, $tag_data ) : $tag_data;
/** assign current user id */
if ( isset( $atts['is_preview'] ) && true === $atts['is_preview'] && empty( $atts['contact_id'] ) ) {
$contact = BWFCRM_Common::get_current_user_crm_object();
if ( ! empty( $contact ) ) {
$atts['contact_id'] = $contact->get_id();
}
}
$final_val = isset( $atts['is_preview'] ) && true === $atts['is_preview'] ? $this->get_dummy_value( $atts ) : $this->get_value( $atts );
if ( $final_val instanceof WP_Error ) {
BWFAN_Core()->logger->log( $final_val->get_error_message() . ': ' . get_class( $this ), 'crm_errors' );
return '';
}
return $final_val;
}
public function get_value( $data = array() ) {
return '';
}
public function get_dummy_value( $atts ) {
return '';
}
public function get_name() {
return $this->_tag_name;
}
public function get_description() {
return $this->_tag_description;
}
public function get_array() {
return [
'name' => $this->get_name(),
'description' => $this->get_description()
];
}
/**
* 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 = get_option( 'date_format' ); // e.g. "F j, Y"
$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 = $date !== null ? DateTime::createFromFormat( $format, $date ) : '';
return $d && $d->format( $format ) === $date;
}
public function maybe_get_contact( $data = array() ) {
if ( ! isset( $data['contact_id'] ) || empty( $data['contact_id'] ) ) {
return BWFCRM_Common::crm_error( __( 'No Contact ID passed in merge tag', 'wp-marketing-automations-pro' ) );
}
$contact_id = absint( $data['contact_id'] );
$contact = new BWFCRM_Contact( $contact_id );
if ( ! $contact->is_contact_exists() ) {
return BWFCRM_Common::crm_error( __( "No contact found with ID: $contact_id", 'wp-marketing-automations-pro' ) );
}
return $contact;
}
}

View File

@@ -0,0 +1,22 @@
<?php
if ( ! BWFAN_PRO_Common::is_lite_3_0() && ! class_exists( 'BWFCRM_Term_Type' ) ) {
#[\AllowDynamicProperties]
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 ( ! BWFAN_PRO_Common::is_lite_3_0() && ! class_exists( 'BWFCRM_Term' ) ) {
#[AllowDynamicProperties]
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 BWFCRM_Common::crm_error( __( 'Required term data is missing.', 'wp-marketing-automations-pro' ) );
}
$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,353 @@
<?php
#[AllowDynamicProperties]
abstract class BWFCRM_Transactional_Mail_Base {
/**
* Slug of mail should be same as class name BWFCRM_Transactional_Mail_Base{ slug }
*
* @var string
*/
protected $slug = '';
/**
* Name of mail
*
* @var string
*/
protected $name = '';
/**
* Description of mail
*
* @var string
*/
protected $description = '';
/**
* Is enabled by user
*
* @var bool
*/
protected $enabled = false;
/**
* Supported block
*
* @var array
*/
protected $supported_block = [];
/**
* Priority of mail to change position of mail in mail list
*
* @var int
*/
protected $priority = 10;
/**
* Supported merge tag groups
*
* @var array
*/
protected $merge_tag_group = [];
/**
* Recipient of mail Admin | Customer
*
* @var string
*/
protected $recipient = '';
/**
* Data for template data to make mail
*
* @var array
*/
protected $template_data = [];
/**
* Subject of mail
*
* @var string
*/
protected $subject = '';
/**
* WC mail id
*
* @var string
*/
protected $wc_mail_id = '';
/**
* Template url
*
* @var string
*/
protected $template_url = 'https://app.getautonami.com/transactional-mail/';
/**
* @function get_data
*/
public function get_data( $key ) {
if ( property_exists( $this, $key ) ) {
return $this->{$key};
}
return null;
}
/**
* Check if mail is valid with dependency
*
* @return bool
*/
public function is_valid() {
return true;
}
/**
* Set status based on template data
*
* @return void
*/
public function set_data_by_template_data() {
if ( empty( $this->template_data ) ) {
return;
}
if ( isset( $this->template_data['status'] ) && $this->template_data['status'] === 'enabled' ) {
$this->enabled = true;
}
}
/**
* Returns API data
*
* @return array
*/
public function get_api_data() {
return [
'slug' => $this->slug,
'name' => $this->name,
'description' => $this->description,
'enabled' => $this->enabled,
'priority' => $this->priority,
'recipient' => $this->recipient,
'template_data' => $this->template_data,
'merge_tag_group' => $this->merge_tag_group,
'supported_block' => $this->supported_block,
];
}
/**
* Create engagement tracking based on passed template id and merge tags data
*
* @param $template_id
* @param $transactional_data
* @param $merge_tags_data
*
* @return bool|mixed
*/
public function create_engagement_tracking( $template_id, $transactional_data, $merge_tags_data = [] ) {
$template_data = BWFCRM_Templates::get_transactional_mail_by_template_id( $template_id );
if ( empty( $template_data ) ) {
return false;
}
// Set flag for transactional mail
$merge_tags_data['bwfan_transactional_mail'] = true;
$email_content = isset( $template_data['data'] ) && BWFAN_Common::is_json( $template_data['data'] ) ? json_decode( $template_data['data'], true ) : [];
$template_email_body = ! empty( $template_data['template'] ) ? $template_data['template'] : '';
$email_subject = ! empty( $template_data['subject'] ) ? $template_data['subject'] : '';
$other_recipient = ! empty( $email_content['other_recipients'] ) ? $email_content['other_recipients'] : '';
if ( empty( $template_email_body ) || empty( $email_subject ) ) {
return false;
}
$recipients = [];
if ( ! empty( $other_recipient ) ) {
$data = explode( ',', $other_recipient );
if ( ! empty( $data ) ) {
foreach ( $data as $recipient ) {
$recipient = BWFAN_Common::decode_merge_tags( trim( $recipient ) );
if ( is_email( $recipient ) ) {
$recipients[] = $recipient;
}
}
}
}
// Set wc trancasactional recipient data
$merge_tags_data['bwfan_wc_transactional_recipient'] = $transactional_data['recipient'];
if ( $transactional_data['recipient'] === 'admin' && empty( $recipients ) ) {
$recipients[] = BWFAN_Common::$admin_email;
}
if ( empty( $recipients ) || $transactional_data['recipient'] === 'customer' ) {
// if customer is recipient then first priority is mail not contact id
$data = ! empty( $merge_tags_data['email'] ) ? $merge_tags_data['email'] : ( ! empty( $merge_tags_data['contact_id'] ) ? $merge_tags_data['contact_id'] : '' );
if ( ! empty( $data ) ) {
$recipients[] = $data;
}
}
/** @var $global_email_settings BWFAN_Common settings */
$global_email_settings = BWFAN_Common::get_global_settings();
/** Apply Click tracking and UTM Params */
$utm_enabled = isset( $email_content['utmEnabled'] ) && 1 === absint( $email_content['utmEnabled'] );
$utm_data = $utm_enabled && isset( $email_content['utm'] ) && is_array( $email_content['utm'] ) ? $email_content['utm'] : array();
/** load block files */
BWFAN_Common::bwfan_before_send_mail( 'block' );
$email_status = false;
foreach ( $recipients as $recipient ) {
$contact = new BWFCRM_Contact( $recipient, true );
if ( ! $contact instanceof BWFCRM_Contact || false === $contact->is_contact_exists() ) {
continue;
}
/** Set email to contact */
$merge_tags_data['email'] = $contact->contact->get_email();
$conversation = new BWFAN_Engagement_Tracking();
$conversation->set_template_id( $template_id );
$conversation->set_oid( isset( $merge_tags_data['oid'] ) ? $merge_tags_data['oid'] : '' );
$conversation->set_mode( BWFAN_Email_Conversations::$MODE_EMAIL );
$conversation->set_contact( $contact );
$conversation->set_send_to( $contact->contact->get_email() );
$conversation->enable_tracking();
$conversation->set_type( BWFAN_Email_Conversations::$TYPE_TRANSACTIONAL );
$conversation->set_template_id( $template_id );
$conversation->set_status( BWFAN_Email_Conversations::$STATUS_DRAFT );
$conversation->add_merge_tags_from_string( $template_email_body, $merge_tags_data );
$conversation->add_merge_tags_from_string( $email_subject, $merge_tags_data );
/** Email Subject */
$email_body = method_exists( 'BWFAN_Common', 'correct_shortcode_string' ) ? BWFAN_Common::correct_shortcode_string( $template_email_body, 5 ) : $template_email_body;
$subject = BWFAN_Common::decode_merge_tags( $email_subject );
$merge_tags = $conversation->get_merge_tags();
$email_body = BWFAN_Common::replace_merge_tags( $email_body, $merge_tags, $contact->get_id() );
$email_body = BWFAN_Common::decode_merge_tags( $email_body );
/** Email Body */
$body = BWFAN_Common::bwfan_correct_protocol_url( $email_body );
$body = BWFCRM_Core()->conversation->apply_template_by_type( $body, 'block', $subject );
$uid = $contact->contact->get_uid();
/** Set contact object */
BWFCRM_Core()->conversation->contact = $contact;
BWFCRM_Core()->conversation->engagement_type = BWFAN_Email_Conversations::$TYPE_TRANSACTIONAL;
if ( property_exists( BWFAN_Core()->conversation, 'template_id' ) ) {
BWFAN_Core()->conversation->template_id = $template_id;
}
$body = BWFCRM_Core()->conversation->append_to_email_body_links( $body, $utm_data, $conversation->get_hash(), $uid );
/** Apply Pre-Header and Hash ID (open pixel id) */
$pre_header = isset( $email_content['preheader'] ) && ! empty( $email_content['preheader'] ) ? $email_content['preheader'] : '';
$pre_header = BWFAN_Common::decode_merge_tags( $pre_header );
$body = BWFCRM_Core()->conversation->append_to_email_body( $body, $pre_header, $conversation->get_hash() );
/** Removed wp mail filters */
BWFCRM_Common::bwf_remove_filter_before_wp_mail();
/** Email Headers */
$reply_to_email = isset( $email_content['reply_to_email'] ) ? BWFAN_Common::decode_merge_tags( $email_content['reply_to_email'] ) : $global_email_settings['bwfan_email_reply_to'];
$from_email = isset( $email_content['from_email'] ) ? BWFAN_Common::decode_merge_tags( $email_content['from_email'] ) : $global_email_settings['bwfan_email_from'];
$from_name = isset( $email_content['from_name'] ) ? BWFAN_Common::decode_merge_tags( $email_content['from_name'] ) : $global_email_settings['bwfan_email_from_name'];
/** Setup Headers */
$header = [ 'MIME-Version: 1.0' ];
if ( ! empty( $from_email ) && ! empty( $from_name ) ) {
$header[] = 'From: ' . $from_name . ' <' . $from_email . '>';
}
if ( ! empty( $reply_to_email ) ) {
$header[] = 'Reply-To: ' . $reply_to_email;
}
$header[] = 'Content-type:text/html;charset=UTF-8';
$header = apply_filters( 'bwfan_email_headers', $header );
BWFCRM_Common::$captured_email_failed_message = null;
/** Send the Email */
$email_status = wp_mail( $conversation->get_send_to(), $subject, $body, $header, $this->get_attachments() );
/** Set the status of Email */
if ( true === $email_status ) {
$conversation->set_status( BWFAN_Email_Conversations::$STATUS_SEND );
} else {
$conversation->set_status( BWFAN_Email_Conversations::$STATUS_ERROR );
}
$conversation->save();
}
return $email_status;
}
/**
* Get email attachments.
*
* @return array
*/
public function get_attachments() {
$merge_tags_data = BWFAN_Merge_Tag_Loader::get_data();
return apply_filters( 'woocommerce_email_attachments', [], $this->wc_mail_id, ! empty( $merge_tags_data['order_id'] ) ? wc_get_order( $merge_tags_data['order_id'] ) : '', null );
}
/**
* Get default template data from api
*
* @param string $lang
*
* @return array
*/
public function get_default_template_data( $lang = '' ) {
$template_data = [
'title' => $this->slug,
'subject' => $this->subject,
'type' => 1,
'mode' => 7,
'canned' => 0,
];
$api_url = $this->template_url . $this->slug . ( ! empty( $lang ) ? '?lang=' . $lang : '' );
// Fetch data from api
$response = wp_remote_get( $api_url );
if ( is_wp_error( $response ) ) {
$fetched_data = [];
} else {
$body = wp_remote_retrieve_body( $response );
$fetched_data = json_decode( $body, true );
if ( json_last_error() !== JSON_ERROR_NONE ) {
$fetched_data = [];
}
}
// Set template data
$template_data['template'] = isset( $fetched_data['html'] ) ? $fetched_data['html'] : '';
$from_name = get_option( 'woocommerce_email_from_name' );
$from_mail = get_option( 'woocommerce_email_from_address' );
// Set mail data
$template_data['data'] = [
'block' => isset( $fetched_data['block'] ) ? $fetched_data['block'] : '',
'preheader' => '',
'utmEnabled' => false,
'utm' => [],
"from_name" => ! empty( $from_name ) ? $from_name : '',
"from_email" => ! empty( $from_mail ) ? $from_mail : '',
"reply_to_email" => ! empty( $from_mail ) ? $from_mail : '',
];
$template_data['data'] = wp_json_encode( $template_data['data'] );
return $template_data;
}
}

View File

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