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,213 @@
<?php
if ( ! class_exists( 'BWF_Model_Contact_Fields' ) && BWFAN_Common::is_pro_3_0() ) {
class BWF_Model_Contact_Fields {
static $primary_key = 'ID';
public static $db_field_types = [
'1' => [
'name' => 'Text Input',
'type' => 'VARCHAR',
'length' => 99
],
'2' => [
'name' => 'Text Number',
'type' => 'BIGINT',
'length' => 10,
'unsigned' => true
],
'3' => [
'name' => 'Text Area',
'type' => 'LONGTEXT'
],
'4' => [
'name' => 'Drop Down',
'type' => 'VARCHAR',
'length' => 99
],
'5' => [
'name' => 'Radio Button',
'type' => 'VARCHAR',
'length' => 99
],
'6' => [
'name' => 'Checkboxes',
'type' => 'LONGTEXT'
],
'7' => [
'name' => 'Date',
'type' => 'DATE'
],
'8' => [
'name' => 'Datetime',
'type' => 'DATETIME'
],
'9' => [
'name' => 'Time',
'type' => 'TIME'
]
];
static function _table() {
global $wpdb;
return "{$wpdb->prefix}bwf_contact_fields";
}
static function insert( $data ) {
global $wpdb;
$wpdb->insert( self::_table(), $data ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
static function update( $data, $where ) {
global $wpdb;
return $wpdb->update( self::_table(), $data, $where ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_contact_field_by_id( $contact_id ) {
global $wpdb;
$table = self::_table();
$query = "SELECT * from $table where cid = $contact_id LIMIT 0, 1";
$field = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return ! empty( $field ) ? $field : '';
}
public static function column_already_exists( $field_id ) {
global $wpdb;
$table = self::_table();
$column = "f{$field_id}";
$query = "SHOW COLUMNS FROM {$table} LIKE '" . esc_sql( $column ) . "'";
return $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function add_column_field( $field_id, $searchable = 2 ) {
global $wpdb;
$table = self::_table();
$field_type = BWFAN_Model_Fields::get_field_type( $field_id );
$db_details = self::$db_field_types[ $field_type ];
$data_type = $db_details['type'];
$length = '';
$unsigned = '';
$indexing = '';
$column = "f{$field_id}";
if ( isset( $db_details['length'] ) ) {
$length = "(" . $db_details['length'] . ")";
}
if ( isset( $db_details['unsigned'] ) ) {
$unsigned = " unsigned ";
}
if ( 1 === absint( $searchable ) && 'LONGTEXT' !== $db_details['type'] ) {
$indexing = ", ADD KEY ($column)";
}
$query = "ALTER TABLE $table ADD $column $data_type{$length} $unsigned DEFAULT NULL $indexing";
$wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return empty( $wpdb->last_error ) ? true : $wpdb->last_error;
}
public static function drop_contact_field_column( $field_id ) {
global $wpdb;
$table = self::_table();
$column = "f{$field_id}";
if ( ! empty( self::column_already_exists( $field_id ) ) ) {
$query = "ALTER TABLE $table DROP COLUMN $column";
$wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}
public static function get_contact_fields( $contact_id ) {
global $wpdb;
$table = self::_table();
$query = "SELECT * FROM $table WHERE cid = $contact_id LIMIT 0, 1";
return $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function update_contact_field_column_indexing( $field_id, $searchable = 1 ) {
global $wpdb;
$table = self::_table();
$already_exist = self::column_already_exists( $field_id );
if ( empty( $already_exist ) || ( ! empty( $already_exist['Type'] ) && 'longtext' === $already_exist['Type'] ) ) {
return false;
}
/** Checking indexing already set or not **/
if ( ( 1 === absint( $searchable ) && ! empty( $already_exist['Key'] ) ) || ( 2 === absint( $searchable ) && empty( $already_exist['Key'] ) ) ) {
return false;
}
$column = "f{$field_id}";
$indexing = " ADD KEY ($column)";
if ( 2 === absint( $searchable ) ) {
$indexing = " DROP KEY `$column` ";
}
$query = "ALTER TABLE $table $indexing";
$wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return empty( $wpdb->last_error ) ? true : false;
}
/**
* Insert query with Ignore
*
* @param $data
* @param $format
*
* @return bool|int|mysqli_result|null
*/
static function insert_ignore( $data, $format = null ) {
if ( ! is_array( $data ) || empty( $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
}
$columns = array_keys( $data );
$placeholders = is_null( $format ) ? array_fill( 0, count( $data ), '%s' ) : $format;
$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,97 @@
<?php
class BWFAN_Model_Abandonedcarts extends BWFAN_Model {
public static $primary_key = 'ID';
public static function get_abandoned_data( $where = '', $offset = '', $per_page = '', $order_by = 'ID', $output = OBJECT ) {
global $wpdb;
$limit_string = '';
if ( '' !== $offset ) {
$limit_string = "LIMIT {$offset}";
}
if ( '' !== $per_page && '' !== $limit_string ) {
$limit_string .= ',' . $per_page;
}
return $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bwfan_abandonedcarts {$where} ORDER BY {$order_by} DESC {$limit_string}", $output ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function delete_abandoned_cart_row( $data ) {
if ( ! is_array( $data ) || empty( $data ) ) {
return;
}
global $wpdb;
$where = '';
$count = count( $data );
$i = 0;
$table_name = $wpdb->prefix . 'bwfan_abandonedcarts';
foreach ( $data as $key => $value ) {
$i ++;
if ( 'string' === gettype( $value ) ) {
$where .= '`' . $key . '` = ' . "'" . $value . "'";
} else {
$where .= '`' . $key . '` = ' . $value;
}
if ( $i < $count ) {
$where .= ' AND ';
}
}
return $wpdb->query( 'DELETE FROM ' . $table_name . " WHERE $where" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Check if any carts available for execution.
*
* @param $abandoned_time
*
* @return bool
*/
public static function maybe_run( $abandoned_time = 0 ) {
global $wpdb;
$table = self::_table();
$abandoned_time = intval( $abandoned_time );
$query = "SELECT `ID` FROM {$table} WHERE `status` IN (0, 4)";
if ( $abandoned_time > 0 ) {
$query .= $wpdb->prepare( " AND TIMESTAMPDIFF(MINUTE,last_modified,UTC_TIMESTAMP) >= %d", $abandoned_time );
}
$count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return ! empty( $count );
}
/**
* Get duplicate cart in last 12 hrs
*
* @return array|object|stdClass[]|null
*/
public static function get_duplicate_entry() {
global $wpdb;
$start_time = date( 'Y-m-d H:i:s', strtotime( '-12 hours' ) );
$query = "SELECT GROUP_CONCAT(`ID`) AS `pkey` FROM `{$wpdb->prefix}bwfan_abandonedcarts` WHERE `created_time` > %s AND (`status` = 0 OR `status` = 3) GROUP BY `email` HAVING COUNT(`email`) > 1";
$query = $wpdb->prepare( $query, $start_time );
$result = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $result ) ) {
return [];
}
$result = array_column( $result, 'pkey' );
$final_ids = [];
foreach ( $result as $item ) {
$values = array_map( 'trim', explode( ',', $item ) );
$values = array_map( 'intval', $values );
array_pop( $values );
$final_ids = array_merge( $final_ids, $values );
};
return $final_ids;
}
}

View File

@@ -0,0 +1,350 @@
<?php
class BWFAN_Model_Automation_Complete_Contact extends BWFAN_Model {
static $primary_key = 'ID';
public static function get_automation_completed_contacts( $aid, $offset = 0, $limit = 25 ) {
global $wpdb;
$table_name = self::_table();
$query = "SELECT cc.ID,cc.trail as tid, cc.cid, cc.aid, cc.c_date AS c_time, c.email, c.f_name, c.l_name, c.contact_no FROM $table_name as cc JOIN {$wpdb->prefix}bwf_contact AS c ON cc.cid = c.ID WHERE 1 = 1 AND cc.aid = $aid ORDER BY cc.c_date DESC LIMIT $limit OFFSET $offset";
$contacts = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$contacts = array_map( function ( $contact ) {
$contact['c_time'] = strtotime( $contact['c_time'] );
return $contact;
}, $contacts );
return [
'contacts' => $contacts,
'total' => self::get_complete_count( $aid )
];
}
/**
* Return table name
*
* @return string
*/
protected static function _table() {
global $wpdb;
return $wpdb->prefix . 'bwfan_automation_complete_contact';
}
public static function get_complete_count( $aid = 0, $search = '' ) {
global $wpdb;
$table_name = self::_table();
$where = '';
$join = '';
$args = [];
if ( ! empty( $aid ) ) {
$where .= " AND cc.aid = %d";
$args[] = $aid;
}
if ( ! empty( $search ) ) {
$join = " JOIN {$wpdb->prefix}bwf_contact AS c ON cc.cid = c.ID ";
$where .= " AND ( c.f_name LIKE %s OR c.l_name LIKE %s OR c.email LIKE %s ) ";
$args[] = "%$search%";
$args[] = "%$search%";
$args[] = "%$search%";
}
$query = $wpdb->prepare( "SELECT COUNT(*) AS `count` FROM {$table_name} AS cc $join WHERE 1 = 1 $where", $args );
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_contacts_journey( $aid, $search = '', $limit = 10, $offset = 0, $contact_with_count = false, $more_data = false, $type = '', $cid = 0 ) {
$where = '';
if ( ! empty( $aid ) ) {
$where .= " AND cc.aid = $aid ";
}
if ( ! empty( $cid ) ) {
$where .= " AND cc.cid = $cid ";
}
$contacts = self::get_contacts( $where, $search, $limit, $offset, $more_data, $type );
if ( true === $contact_with_count ) {
return [
'contacts' => $contacts,
'total' => self::get_complete_count( $aid, $search )
];
}
return $contacts;
}
public static function get_contacts( $where, $search, $limit, $offset, $more_data = false, $status = '', $only_total = false ) {
global $wpdb;
$table_name = self::_table();
$limit = " LIMIT $limit OFFSET $offset";
if ( ! empty( $search ) ) {
$where .= " AND ( c.f_name LIKE '%$search%' OR c.l_name LIKE '%$search%' OR c.email LIKE '%$search%' )";
}
if ( true === $only_total ) {
$query = "SELECT COUNT(cc.ID) FROM $table_name as cc JOIN {$wpdb->prefix}bwf_contact AS c ON cc.cid = c.ID WHERE 1=1 $where ";
return intval( $wpdb->get_var( $query ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
$query = "SELECT cc.ID,cc.cid, cc.aid, cc.trail, cc.c_date, c.email, c.f_name, c.l_name, c.contact_no, cc.s_date FROM $table_name as cc JOIN {$wpdb->prefix}bwf_contact AS c ON cc.cid = c.ID WHERE 1 = 1 $where ORDER BY cc.c_date DESC $limit";
$contacts = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return array_map( function ( $contact ) use ( $more_data, $status ) {
$contact['e_time'] = $contact['c_date'];
$contact['c_date'] = $contact['s_date'];
unset( $contact['s_date'] );
/**Get trail data */
if ( true === $more_data ) {
$data = BWFAN_Common::get_step_by_trail( $contact['trail'] );
$contact['data'] = isset( $data[0] ) ? $data[0] : $data;
}
if ( 2 === intval( $status ) && isset( $contact['trail'] ) && isset( $contact['last'] ) ) {
$fail_data = BWFAN_Model_Automation_Contact_Trail::get_step_trail( $contact['trail'], $contact['last'] );
if ( isset( $fail_data['data'] ) ) {
$fail_data = json_decode( $fail_data['data'], true );
if ( isset( $fail_data['error_msg'] ) ) {
$contact['error_msg'] = $fail_data['error_msg'];
}
}
}
return $contact;
}, $contacts );
}
public static function get_automation_complete_contact_count( $aids ) {
global $wpdb;
$table_name = self::_table();
$query = "SELECT aid, count(aid) as count FROM $table_name WHERE aid IN ($aids) GROUP BY aid";
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_automation_contacts( $cid, $search = '', $limit = 10, $offset = 0, $more_data = false, $type = '', $only_total = false ) {
$where = " AND cc.cid = $cid ";
return self::get_contacts( $where, $search, $limit, $offset, $more_data, $type, $only_total );
}
public static function get_contact_automation_count( $cid ) {
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "SELECT COUNT(*) AS `count` FROM {$table_name} WHERE `cid` = %d", $cid );
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function delete_automation_contact_by_aid( $aid ) {
global $wpdb;
$table_name = self::_table();
$where = "aid = %d";
if ( is_array( $aid ) ) {
$where = "aid IN ('" . implode( "','", array_map( 'esc_sql', $aid ) ) . "')";
$aid = [];
}
$query = " DELETE FROM $table_name WHERE $where";
return $wpdb->query( $wpdb->prepare( $query, $aid ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**Get status */
public static function get_status( $status ) {
switch ( $status ) {
case 'success':
$status = 1;
break;
case 'wait':
$status = 2;
break;
case 'failed':
$status = 3;
break;
}
return $status;
}
public static function is_contact_completed( $tid ) {
if ( empty( $tid ) ) {
return false;
}
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "SELECT `ID` FROM {$table_name} WHERE `trail` = %s LIMIT 0,1", $tid );
$found = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return intval( $found ) > 0;
}
/** Get all the contacts where automations already ran for the given automation range */
public static function get_contacts_automation( $aid, $start_date, $end_date = '' ) {
global $wpdb;
$table_name = self::_table();
$args = [ $aid ];
if ( ! empty( $start_date ) && empty( $end_date ) ) {
$where = " AND `c_date` > %s";
$args[] = $start_date;
}
if ( ! empty( $start_date ) && ! empty( $end_date ) ) {
$where = " AND `c_date` > %s AND `c_date` < %s";
$args[] = $start_date;
$args[] = $end_date;
}
$query = $wpdb->prepare( "SELECT DISTINCT `cid` AS contact_id FROM {$table_name} WHERE `aid` = %d $where", $args );
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* @param $start_date
* @param $end_date
* @param $is_interval
* @param $interval
*
* @return array|object|null
*/
public static function get_total_contacts( $aid, $start_date, $end_date, $is_interval, $interval ) {
global $wpdb;
$table = self::_table();
$date_col = "s_date";
$interval_query = '';
$group_by = '';
$order_by = ' ID ';
if ( 'interval' === $is_interval ) {
$get_interval = BWFCRM_Dashboards::get_interval_format_query( $interval, $date_col );
$interval_query = $get_interval['interval_query'];
$interval_group = $get_interval['interval_group'];
$group_by = "GROUP BY " . $interval_group;
$order_by = ' time_interval ';
}
$base_query = "SELECT count(ID) as contact_counts" . $interval_query . " FROM `" . $table . "` WHERE 1=1 AND aid = $aid AND`" . $date_col . "` >= '" . $start_date . "' AND `" . $date_col . "` <= '" . $end_date . "' AND aid = $aid " . $group_by . " ORDER BY " . $order_by . " ASC";
return $wpdb->get_results( $base_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_row_by_trail_id( $trail_id ) {
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "SELECT * FROM $table_name WHERE trail = %s LIMIT 0,1", $trail_id );
return $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get automation complete count
*
* @param $cid
* @param $aid
*
* @return string|null
*/
public static function get_automation_count_by_cid( $cid, $aid ) {
global $wpdb;
$table = self::_table();
$query = $wpdb->prepare( 'SELECT COUNT(`aid`) as `count` FROM ' . $table . ' WHERE `cid` = %d AND `aid` = %d ORDER BY `ID` DESC', $cid, $aid );
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Check if contact recently (5 mins) completed, considering duplicate case
*
* @param $data
*
* @return bool
* @throws Exception
*/
public static function check_duplicate_automation_contact( $data, $wait_time = 5 ) {
if ( empty( $data ) ) {
return false;
}
$datetime = new DateTime( date( 'Y-m-d H:i:s', strtotime( $data['c_date'] ) ) );
$c_date = $datetime->modify( "-$wait_time mins" )->format( 'Y-m-d H:i:s' );
global $wpdb;
$query = "SELECT `ID` FROM `{$wpdb->prefix}bwfan_automation_complete_contact` WHERE `cid` = %d AND `aid` = %d AND `event` = %s AND `data` = %s AND `s_date` >= %s LIMIT 1";
return intval( $wpdb->get_var( $wpdb->prepare( $query, $data['cid'], $data['aid'], $data['event'], $data['data'], $c_date ) ) ) > 0; //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Check if contact has completed the automation for a particular order related event
*
* @param $aid
* @param $cid
* @param $order_id
* @param $single_item
* @param $event
*
* @return bool
*/
public static function is_contact_with_same_order( $aid, $cid, $order_id, $single_item = 0, $event = 'wc_new_order' ) {
global $wpdb;
$like1 = '%"order_id":"' . $order_id . '"%';
$like2 = '%"order_id":' . $order_id . '%';
$data = "( `data` LIKE '$like1' OR `data` LIKE '$like2' )";
if ( ! empty( $single_item ) ) {
$like1 = '%"wc_single_item_id":"' . $single_item . '"%';
$like2 = '%"wc_single_item_id":' . $single_item . '%';
$data .= " AND (`data` LIKE '$like1' OR `data` LIKE '$like2')";
}
$query = "SELECT `ID`, `data` FROM `{$wpdb->prefix}bwfan_automation_complete_contact` WHERE `cid` = %d AND `aid` = %d AND `event` = %s AND $data ORDER BY `ID` DESC LIMIT 0,1";
$res = $wpdb->get_row( $wpdb->prepare( $query, $cid, $aid, $event ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$data = isset( $res['data'] ) ? json_decode( $res['data'], true ) : [];
if ( empty( $data ) ) {
return false;
}
$order_validation = false;
if ( isset( $data['global']['order_id'] ) && ( intval( $order_id ) === intval( $data['global']['order_id'] ) ) ) {
$order_validation = true;
}
if ( empty( $single_item ) || false === $order_validation ) {
return $order_validation;
}
return ( isset( $data['global']['wc_single_item_id'] ) && ( intval( $single_item ) === intval( $data['global']['wc_single_item_id'] ) ) );
}
/**
* Get automation data by trail
*
* @param $trail_id
*
* @return array|mixed
*/
public static function get_data_by_trail( $trail_id ) {
$table_name = self::_table();
global $wpdb;
$query = $wpdb->prepare( "SELECT `data` FROM $table_name WHERE trail = %s LIMIT 0,1", $trail_id );
$data = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return ! empty( $data ) ? json_decode( $data, true ) : [];
}
}

View File

@@ -0,0 +1,271 @@
<?php
class BWFAN_Model_Automation_Contact_Trail extends BWFAN_Model {
static $primary_key = 'ID';
/**
* @param $step_ids
* @param $status
* @param $after_time
*
* @return array|false|object|stdClass[]|null
*/
public static function get_bulk_step_count( $step_ids = [], $status = 1, $after_time = 0 ) {
global $wpdb;
$table_name = self::_table();
if ( empty( $step_ids ) ) {
return false;
}
$string_placeholder = array_fill( 0, count( $step_ids ), '%d' );
$placeholder = implode( ', ', $string_placeholder );
$args = array_merge( $step_ids, [ $status ] );
$after_time_query = '';
if ( ! empty( $after_time ) ) {
$after_time_query .= " AND `c_time` > %d ";
$args[] = $after_time;
}
$query = "SELECT `sid`, count(`sid`) as `count` FROM {$table_name} WHERE `sid` IN ($placeholder) AND `status` = %d $after_time_query GROUP BY `sid`";
return $wpdb->get_results( $wpdb->prepare( $query, $args ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Return table name
*
* @return string
*/
protected static function _table() {
global $wpdb;
return $wpdb->prefix . 'bwfan_automation_contact_trail';
}
/**
* Update all steps trails status to success
*
* @param $trail_id
*
* @return false|void
*/
public static function update_all_step_trail_status_complete( $trail_id ) {
if ( empty( $trail_id ) ) {
return false;
}
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "UPDATE {$table_name} SET `status`= 1 WHERE `tid` = %s AND `status` = 2", $trail_id );
$wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get step trail data
*
* @param $trail_id
* @param $step_id
*
* @return array|false|object|void|null
*/
public static function get_step_trail( $trail_id, $step_id ) {
global $wpdb;
$table_name = self::_table();
if ( empty( $trail_id ) || empty( $step_id ) ) {
return false;
}
$query = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE `tid` = %s AND `sid` = %d ORDER BY ID DESC LIMIT 0,1", $trail_id, $step_id );
return $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get steps 'completed contacts' data
*
* @param $aid
* @param $step_id
* @param $queued
* @param $offset
* @param $limit
*
* @return array
*/
public static function get_step_completed_contacts( $aid, $step_id, $type = '', $offset = 0, $limit = 25, $path = 0 ) {
global $wpdb;
$table_name = self::_table();
$status = 'queued' === $type ? 2 : 1;
if ( 'failed' === $type ) {
$status = 3;
}
if ( 'skipped' === $type ) {
$status = 4;
}
$path_query = '';
if ( ! empty( $path ) ) {
$path_query = '%"path":"' . $path . '"%';
$path_query = " AND ct.data LIKE '$path_query' ";
}
$query = $wpdb->prepare( "SELECT ct.ID, ct.cid, ct.aid, ct.data, ct.tid, ct.c_time, c.email, c.f_name, c.l_name, c.contact_no FROM {$table_name} as ct JOIN {$wpdb->prefix}bwf_contact AS c ON ct.cid = c.ID WHERE 1=1 AND ct.aid = %d AND ct.sid = %d AND ct.status = %d $path_query ORDER BY ct.c_time DESC LIMIT %d OFFSET %d", $aid, $step_id, $status, $limit, $offset );
$contacts = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$completed = 2 === $status ? false : true;
return [
'contacts' => $contacts,
'total' => self::get_step_count( $step_id, $status, $path )
];
}
/**
* Get step count, complete or wait both
*
* @param $step_id
* @param $completed
*
* @return string|null
*/
public static function get_step_count( $step_id, $status = true, $path = '' ) {
global $wpdb;
$table_name = self::_table();
$status = ( true === $status ) ? 1 : $status;
$query = $wpdb->prepare( "SELECT COUNT(*) AS `count` FROM {$table_name} WHERE `sid` = %d AND `status` = %d ", $step_id, $status );
if ( ! empty( $path ) ) {
$path = '%"path":"' . $path . '"%';
$query .= " AND `data` LIKE '$path' ";
}
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get step trails for automation contact
*
* @param $tid
*
* @return array|object|null
*/
public static function get_trail( $tid ) {
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "SELECT tr.*, st.type, st.data AS step_data, st.action, st.status AS step_status FROM {$table_name} AS tr LEFT JOIN {$wpdb->prefix}bwfan_automation_step AS st ON tr.sid = st.ID WHERE tid = %s ", $tid );
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function delete_automation_trail_by_id( $aid ) {
global $wpdb;
$table_name = self::_table();
$where = "aid = %d";
if ( is_array( $aid ) ) {
$where = "aid IN ('" . implode( "','", array_map( 'esc_sql', $aid ) ) . "')";
$aid = [];
}
$query = $wpdb->prepare( "DELETE FROM $table_name WHERE $where", $aid );
return $wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function delete_row_by_trail_by( $trail_id ) {
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "DELETE FROM $table_name WHERE tid = %s", $trail_id );
return $wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function update_multiple_trail_status( $tids, $sid, $status = 1 ) {
if ( empty( $tids ) && ! is_array( $tids ) ) {
return false;
}
$tids = implode( "', '", $tids );
global $wpdb;
$table_name = self::_table();
$args = [ $tids ];
$query = "UPDATE {$table_name} SET `status`= $status WHERE `tid` IN (%s) ";
if ( absint( $sid ) > 0 ) {
$query .= " AND `sid` = %d ";
$args[] = $sid;
}
$query = $wpdb->prepare( $query, $args );
return $wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Delete trail if already exist for specific step id
*
* @param $arr
*
* @return void
*/
public static function delete_if_trail_exists( $arr ) {
if ( ! is_array( $arr ) || ! isset( $arr['tid'] ) || ! isset( $arr['aid'] ) || ! isset( $arr['cid'] ) || ! isset( $arr['sid'] ) ) {
return;
}
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "SELECT COUNT(*) AS `count` FROM {$table_name} WHERE `tid` = %s AND `cid` = %d AND `aid` = %d AND `sid` = %d", $arr['tid'], $arr['cid'], $arr['aid'], $arr['sid'] ); //phpcs:ignore WordPress.DB.PreparedSQL
$count = BWFAN_Model_Automation_Contact_Trail::get_var( $query );
if ( intval( $count ) > 0 ) {
/** Delete existing */
$query = $wpdb->prepare( "DELETE FROM {$table_name} WHERE `tid` = %s AND `cid` = %d AND `aid` = %d AND `sid` = %d", $arr['tid'], $arr['cid'], $arr['aid'], $arr['sid'] ); //phpcs:ignore WordPress.DB.PreparedSQL
BWFAN_Model_Automation_Contact_Trail::query( $query );
}
}
/**
* Get completed email step count
*
* @param $sids
*
* @return int
*/
public static function get_completed_email_steps_count( $sids ) {
global $wpdb;
$table_name = self::_table();
$string_placeholder = array_fill( 0, count( $sids ), '%d' );
$placeholder = implode( ', ', $string_placeholder );
$args = array_merge( $sids, [ 1 ] );
$query = $wpdb->prepare( "SELECT COUNT(ID) FROM {$table_name} WHERE `sid` IN ($placeholder) AND `status` = %d ", $args ); //phpcs:ignore WordPress.DB.PreparedSQL
return intval( $wpdb->get_var( $query ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get path's contact count
*
* @param $split_id
* @param $path
*
* @return string|null
*/
public static function get_path_contact_count( $split_id, $path ) {
global $wpdb;
$table_name = self::_table();
$path = '%"path":"' . $path . '"%';
$query = "SELECT count(`sid`) as `count` FROM {$table_name} WHERE `sid`= %d AND `data` LIKE '$path' GROUP BY `sid`";
$query = $wpdb->prepare( $query, $split_id );
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}

View File

@@ -0,0 +1,576 @@
<?php
class BWFAN_Model_Automation_Contact extends BWFAN_Model {
static $primary_key = 'ID';
public static function get_data( $id ) {
global $wpdb;
$table = self::_table();
$query = $wpdb->prepare( 'SELECT * FROM ' . $table . ' WHERE `ID` = %d', $id );
return $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Returns table name
*
* @return string
*/
protected static function _table() {
global $wpdb;
return $wpdb->prefix . 'bwfan_automation_contact';
}
public static function get_automation_contact( $automation_id, $contact_id ) {
global $wpdb;
$table = self::_table();
$query = $wpdb->prepare( 'SELECT * FROM ' . $table . ' WHERE `aid` = %d AND `cid` = %d ORDER BY `ID` DESC LIMIT 0,1', $automation_id, $contact_id );
return $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get Automation Contact by abandoned id
*
* */
public static function get_automation_contact_by_ab_id( $ab_id, $cid ) {
global $wpdb;
$table = self::_table();
$abandoned_id = '%"cart_abandoned_id":"' . $ab_id . '"%';
$query = $wpdb->prepare( "SELECT * FROM $table WHERE `cid` = %d AND `data` LIKE '$abandoned_id'", $cid );
return $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Check if any contact is in automation to execute
*
* @return bool
*/
public static function maybe_can_execute() {
global $wpdb;
$time = current_time( 'timestamp', 1 );
$table = self::_table();
$query = $wpdb->prepare( "SELECT MAX(`ID`) FROM {$table} WHERE `e_time` < %s AND `status` IN (1,6)", $time );
$count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $count ) ) {
return false;
}
return true;
}
/**
* Check if contact is active in the automation
*
* @param $cid
* @param $aid
*
* @return bool
*/
public static function maybe_contact_in_automation( $cid, $aid ) {
global $wpdb;
$table = self::_table();
$query = $wpdb->prepare( "SELECT MAX(`ID`) FROM {$table} WHERE `cid` = %d AND `aid` = %d AND `status` IN (%d,%d,%d) ", $cid, $aid, 1, 4, 6 );
$count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $count ) ) {
return false;
}
return true;
}
public static function get_contacts_journey( $aid, $search = '', $limit = 10, $offset = 0, $contact_with_count = false, $more_data = false, $status = '', $where = '', $cid = 0 ) {
if ( ! empty( $aid ) ) {
$where .= " AND cc.aid = $aid ";
}
if ( ! empty( $cid ) ) {
$where .= " AND cc.cid = $cid ";
}
$order_by = "ORDER BY cc.c_date DESC ";
/** If automation is inactive then no need to check automation contact status */
if ( ! empty( $status ) && 'inactive' !== $status ) {
if ( 'active' === $status ) {
$where .= " AND (cc.status = 1 OR cc.status = 4 OR cc.status = 6 ) ";
$order_by = " ORDER BY cc.c_date, cc.e_time ASC ";
} elseif ( 'delayed' === $status ) {
$where .= " AND (cc.status = 1 OR cc.status = 6 ) ";
$order_by = " ORDER BY cc.e_time ASC ";
} else {
$status = self::get_status( $status );
$where .= " AND cc.status = $status";
}
}
if ( 'inactive' === $status || 2 === intval( $status ) || 3 === intval( $status ) ) {
$order_by = " ORDER BY cc.last_time DESC ";
}
$automation_status = 'inactive' === $status ? " AND am.status = 2" : " AND am.status = 1";
global $wpdb;
$where .= " AND (EXISTS ( SELECT 1 FROM {$wpdb->prefix}bwfan_automations AS am WHERE am.ID = cc.aid $automation_status ) )";
$contacts = self::get_contacts( $where, $search, $limit, $offset, $more_data, $status, false, $order_by );
if ( true === $contact_with_count ) {
return [
'contacts' => $contacts,
'total' => self::get_active_count( $aid, $status, $search, $cid, $where )
];
}
return $contacts;
}
/**Get status */
public static function get_status( $status ) {
switch ( $status ) {
case 'active':
$status = 1;
break;
case 'failed':
$status = 2;
break;
case 'paused':
$status = 3;
break;
case 'wait':
$status = 4;
break;
case 'terminate':
$status = 5;
break;
case 'Retry':
$status = 6;
break;
}
return $status;
}
public static function get_contacts( $where, $search, $limit, $offset, $more_data, $status = '', $only_total = false, $order_by = '' ) {
global $wpdb;
$table_name = self::_table();
$limit = " LIMIT $limit OFFSET $offset";
if ( ! empty( $search ) ) {
$where .= " AND ( c.f_name LIKE '%$search%' OR c.l_name LIKE '%$search%' OR c.email LIKE '%$search%' )";
}
if ( true === $only_total ) {
$query = "SELECT COUNT(cc.ID) FROM $table_name as cc JOIN {$wpdb->prefix}bwf_contact AS c ON cc.cid = c.ID WHERE 1=1 $where ORDER BY cc.c_date DESC ";
return intval( $wpdb->get_var( $query ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
$more_columns = '';
if ( true === $more_data ) {
$more_columns = ", cc.e_time, cc.status, cc.last ";
}
$order_by = ! empty( $order_by ) ? $order_by : "ORDER BY cc.c_date DESC";
$query = "SELECT cc.ID,cc.cid, cc.aid, cc.trail, cc.c_date, c.email, c.f_name, c.l_name, c.contact_no, c.creation_date as date $more_columns FROM $table_name as cc JOIN {$wpdb->prefix}bwf_contact AS c ON cc.cid = c.ID WHERE 1=1 $where $order_by $limit";
$contacts = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return array_map( function ( $contact ) use ( $more_data, $status ) {
$contact['e_time'] = isset( $contact['e_time'] ) ? date( 'Y-m-d H:i:s', $contact['e_time'] ) : '';
if ( 2 === intval( $status ) && isset( $contact['trail'] ) && isset( $contact['last'] ) ) {
$fail_data = BWFAN_Model_Automation_Contact_Trail::get_step_trail( $contact['trail'], $contact['last'] );
if ( isset( $fail_data['data'] ) ) {
$fail_data = json_decode( $fail_data['data'], true );
if ( isset( $fail_data['error_msg'] ) ) {
$contact['error_msg'] = $fail_data['error_msg'];
}
}
}
/**Get trail data */
if ( true === $more_data ) {
$data = BWFAN_Common::get_step_by_trail( $contact['trail'] );
$contact['data'] = isset( $data[0] ) ? $data[0] : $data;
}
return $contact;
}, $contacts );
}
public static function get_active_count( $aid = '', $status = '', $search = '', $cid = '', $where = '' ) {
global $wpdb;
$table_name = self::_table();
$args = [];
$columns = '';
$join = '';
if ( is_array( $aid ) ) {
$columns = " cc.aid, ";
$aids = implode( "', '", $aid );
$where .= " AND cc.aid IN ('$aids')";
}
if ( empty( $where ) && absint( $aid ) > 0 ) {
$where .= " AND cc.aid = %d";
$args[] = $aid;
}
if ( intval( $cid ) > 1 ) {
$where .= " AND cc.cid = %d";
$args[] = $cid;
}
/** If automation is inactive then no need to check automation contact status */
if ( ! empty( $status ) && 'inactive' !== $status ) {
if ( 'active' === $status ) {
$where .= " AND ( cc.status = 1 OR cc.status = 4 OR cc.status = 6 )";
} elseif ( 'delayed' === $status ) {
$where .= " AND ( cc.status = 1 OR cc.status = 6 )";
} elseif ( absint( $status ) ) {
$where .= " AND cc.status = %d";
$args[] = $status;
}
}
if ( ! empty( $search ) ) {
$join = " JOIN {$wpdb->prefix}bwf_contact AS c ON cc.cid = c.ID ";
$where .= " AND ( c.f_name LIKE %s OR c.l_name LIKE %s OR c.email LIKE %s ) ";
$args[] = "%$search%";
$args[] = "%$search%";
$args[] = "%$search%";
}
$automation_status = 'inactive' === $status ? " AND am.status = 2" : " AND am.status = 1";
$where .= " AND (EXISTS ( SELECT 1 FROM {$wpdb->prefix}bwfan_automations AS am WHERE am.ID = cc.aid $automation_status ) )";
$where .= is_array( $aid ) ? " GROUP BY cc.aid " : '';
$query = $wpdb->prepare( "SELECT $columns COUNT(cc.ID) AS `count` FROM {$table_name} AS cc $join WHERE 1 = 1 $where ", $args );
if ( is_array( $aid ) ) {
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_automation_contact_count( $aids ) {
global $wpdb;
$table_name = self::_table();
$query = "SELECT aid, count(aid) as count FROM $table_name WHERE aid IN ($aids) GROUP BY aid";
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_automation_contacts( $cid, $search = '', $limit = 10, $offset = 0, $more_data = false, $only_total = false ) {
$where = " AND cc.cid = $cid ";
return self::get_contacts( $where, $search, $limit, $offset, $more_data, '', $only_total );
}
public static function delete_automation_contact_by_aid( $aid ) {
global $wpdb;
$table_name = self::_table();
$where = "aid = %d";
if ( is_array( $aid ) ) {
$where = "aid IN ('" . implode( "','", array_map( 'esc_sql', $aid ) ) . "')";
$aid = [];
}
$query = " DELETE FROM $table_name WHERE $where";
return $wpdb->query( $wpdb->prepare( $query, $aid ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_row_by_trail_id( $trail_id ) {
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "SELECT * FROM $table_name WHERE trail = %s LIMIT 0,1", $trail_id );
return $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/** Get all the contacts where automations already ran for the given automation range */
public static function get_contacts_automation( $aid, $start_date, $end_date = '' ) {
global $wpdb;
$table_name = self::_table();
$args = [ $aid ];
if ( ! empty( $start_date ) && empty( $end_date ) ) {
$where = " AND `c_date` > %s";
$args[] = $start_date;
}
if ( ! empty( $start_date ) && ! empty( $end_date ) ) {
$where = " AND `c_date` > %s AND `c_date` < %s";
$args[] = $start_date;
$args[] = $end_date;
}
$query = $wpdb->prepare( "SELECT DISTINCT `cid` AS contact_id FROM {$table_name} WHERE `aid` = %d $where", $args );
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* @param $start_date
* @param $end_date
* @param $is_interval
* @param $interval
*
* @return array|object|null
*/
public static function get_total_contacts( $aid, $start_date, $end_date, $is_interval, $interval ) {
global $wpdb;
$table = self::_table();
$date_col = "c_date";
$interval_query = '';
$group_by = '';
$order_by = ' ID ';
if ( 'interval' === $is_interval ) {
$get_interval = BWFCRM_Dashboards::get_interval_format_query( $interval, $date_col );
$interval_query = $get_interval['interval_query'];
$interval_group = $get_interval['interval_group'];
$group_by = "GROUP BY " . $interval_group;
$order_by = ' time_interval ';
}
$base_query = "SELECT count(ID) as contact_counts" . $interval_query . " FROM `" . $table . "` WHERE 1=1 AND aid = $aid AND `" . $date_col . "` >= '" . $start_date . "' AND `" . $date_col . "` <= '" . $end_date . "' AND aid = $aid " . $group_by . " ORDER BY " . $order_by . " ASC";
$contact_counts = $wpdb->get_results( $base_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $contact_counts;
}
/**
* Get automation contact by last step id
*
* @param $sid
*
* @return array
*/
public static function get_automation_contact_by_sid( $sid, $step_type = '', $status = 1 ) {
global $wpdb;
$table = self::_table();
$where = " AND `status` = $status ";
if ( 'goal' === $step_type ) {
$where = " AND ( `status`= 4 OR `status` = 1 ) ";
}
// if ( $aid > 0 ) {
// $where .= " AND aid = $aid";
// }
$query = "SELECT ID FROM {$table} WHERE `last` = %d $where";
$query = $wpdb->prepare( $query, $sid );
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_automation_count_by_cid( $cid, $aid ) {
global $wpdb;
$table = self::_table();
$query = $wpdb->prepare( 'SELECT COUNT(`aid`) as `count` FROM ' . $table . ' WHERE `cid` = %d AND `aid` = %d ORDER BY `ID` DESC', $cid, $aid );
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function update_status_by_multiple_ids( $ac_ids, $status = 1, $e_time = '', $last = false ) {
if ( empty( $ac_ids ) && ! is_array( $ac_ids ) ) {
return false;
}
global $wpdb;
$table_name = self::_table();
$set_e_time = '';
$args = [];
if ( ! empty( $e_time ) ) {
$set_e_time = " , `e_time` = %d ";
$args[] = $e_time;
}
/** Update last column if need */
$set_last = '';
if ( true === $last ) {
$set_last = " , `last_time` = %d, `last` = %d, `claim_id` = %d, `attempts` = %d";
array_push( $args, $e_time, 0, 0, 0 );
}
$string_placeholders = array_fill( 0, count( $ac_ids ), '%d' );
$prepared_placeholders = implode( ', ', $string_placeholders );
$args = array_merge( $args, $ac_ids );
$query = "UPDATE {$table_name} SET `status`= $status $set_e_time $set_last WHERE `ID` IN ($prepared_placeholders) ";
$query = $wpdb->prepare( $query, $args );
return $wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_multiple_automation_contact( $aids, $contact_id ) {
global $wpdb;
$args = [];
if ( ! is_array( $aids ) ) {
$args = [ $aids ];
} else {
$args = array_merge( $args, $aids );
}
$placeholders = array_fill( 0, count( $args ), '%d' );
$placeholders = implode( ', ', $placeholders );
$args[] = $contact_id;
$table = self::_table();
$query = $wpdb->prepare( 'SELECT * FROM ' . $table . ' WHERE `aid` IN (' . $placeholders . ') AND `cid` = %d ORDER BY `ID` DESC', $args );
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get next run time
*
* @param $trail
*
* @return string|null
*/
public static function get_next_run_time_by_trail( $trail ) {
global $wpdb;
$table = self::_table();
return $wpdb->get_var( $wpdb->prepare( "SELECT `e_time` FROM {$table} WHERE `trail` = %s ", $trail ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get last (time range) failed count
*
* @param $time_range
*
* @return string|null
*/
public static function get_failed_count( $time_range = '' ) {
global $wpdb;
$table_name = self::_table();
/** Failed status 2 */
$args = [ 2 ];
$where = '';
if ( ! empty( $time_range ) ) {
$date = new DateTime();
$date->modify( "- $time_range" );
$last = $date->format( 'Y-m-d H:i:s' );
$where .= " AND `c_date` > %s ";
$args[] = $last;
}
$query = $wpdb->prepare( "SELECT COUNT(*) AS `count` FROM {$table_name} WHERE 1 = 1 AND `status` = %d $where", $args );
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Check if duplicate contact data, already exists with same data in 5 mins in the same automation
*
* @param $data
*
* @return bool
* @throws Exception
*/
public static function check_duplicate_automation_contact( $data ) {
if ( empty( $data ) ) {
return false;
}
$wait_time = apply_filters( 'bwfan_duplicate_automation_contact_wait_time', 5, $data );
$wait_time = intval( $wait_time ) > 0 ? $wait_time : 5;
/** Check if contact already exists in automation contact complete table */
$already_exists = BWFAN_Model_Automation_Complete_Contact::check_duplicate_automation_contact( $data, $wait_time );
if ( $already_exists ) {
return true;
}
$datetime = new DateTime( date( 'Y-m-d H:i:s', strtotime( $data['c_date'] ) ) );
$c_date = $datetime->modify( "-$wait_time mins" )->format( 'Y-m-d H:i:s' );
global $wpdb;
$query = "SELECT `ID` FROM `{$wpdb->prefix}bwfan_automation_contact` WHERE `cid` = %d AND `aid` = %d AND `event` = %s AND `data` = %s AND `c_date` >= %s LIMIT 1";
return intval( $wpdb->get_var( $wpdb->prepare( $query, $data['cid'], $data['aid'], $data['event'], $data['data'], $c_date ) ) ) > 0; //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Check if contact is in automation for a particular order related event
*
* @param $aid
* @param $cid
* @param $order_id
* @param $single_item
* @param $event
*
* @return bool
*/
public static function is_contact_with_same_order( $aid, $cid, $order_id, $single_item = 0, $event = 'wc_new_order' ) {
global $wpdb;
$like1 = '%"order_id":"' . $order_id . '"%';
$like2 = '%"order_id":' . $order_id . '%';
$data = "( `data` LIKE '$like1' OR `data` LIKE '$like2' )";
if ( ! empty( $single_item ) ) {
$like1 = '%"wc_single_item_id":"' . $single_item . '"%';
$like2 = '%"wc_single_item_id":' . $single_item . '%';
$data .= " AND (`data` LIKE '$like1' OR `data` LIKE '$like2')";
}
$query = "SELECT `ID`, `data` FROM `{$wpdb->prefix}bwfan_automation_contact` WHERE `cid` = %d AND `aid` = %d AND `event` = %s AND $data ORDER BY `ID` DESC LIMIT 0,1";
$res = $wpdb->get_row( $wpdb->prepare( $query, $cid, $aid, $event ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$data = isset( $res['data'] ) ? json_decode( $res['data'], true ) : [];
if ( empty( $data ) ) {
return false;
}
$order_validation = false;
if ( isset( $data['global']['order_id'] ) && ( intval( $order_id ) === intval( $data['global']['order_id'] ) ) ) {
$order_validation = true;
}
if ( empty( $single_item ) || false === $order_validation ) {
return $order_validation;
}
return ( isset( $data['global']['wc_single_item_id'] ) && ( intval( $single_item ) === intval( $data['global']['wc_single_item_id'] ) ) );
}
/**
* Update e_time of multiple rows
*
* @param $ids
* @param $e_time
*
* @return bool|int|mysqli_result|null
*/
public static function update_e_time_col_of_ids( $ids, $e_time = '' ) {
if ( empty( $ids ) || ! is_array( $ids ) ) {
return 0;
}
global $wpdb;
$table_name = self::_table();
if ( empty( $e_time ) ) {
$e_time = current_time( 'timestamp', 1 );
}
$args = [ $e_time ];
$string_placeholders = array_fill( 0, count( $ids ), '%d' );
$prepared_placeholders = implode( ', ', $string_placeholders );
$args = array_merge( $args, $ids );
$query = "UPDATE {$table_name} SET `e_time` = %d WHERE `ID` IN ($prepared_placeholders) ";
$query = $wpdb->prepare( $query, $args );
return $wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}

View File

@@ -0,0 +1,23 @@
<?php
class BWFAN_Model_Automation_Events extends BWFAN_Model {
static $primary_key = 'ID';
/**
* Insert row
*
* @param $args
*
* @return int
*/
public static function insert_data( $args ) {
$event_data = [
'creation_time' => current_time( 'mysql', 1 ),
'execution_time' => current_time( 'timestamp', 1 ) + 30,
'args' => wp_json_encode( $args )
];
BWFAN_Model_Automation_Events::insert( $event_data );
return BWFAN_Model_Automation_Events::insert_id();
}
}

View File

@@ -0,0 +1,347 @@
<?php
/**
* Automation step modal class
*/
class BWFAN_Model_Automation_Step extends BWFAN_Model {
static $primary_key = 'ID';
/**
* Get Steps
*
* @param int $aid
* @param int $offset
* @param int $limit
* @param string $search
* @param string $order
* @param string $order_by
* @param array $ids
* @param bool $get_total
*
* @return array
*/
public static function get_all_automation_steps( $aid = 0, $offset = 0, $limit = 0, $search = '', $order = 'DESC', $order_by = 'ID', $ids = [], $get_total = false, $get_deleted_nodes = false ) {
global $wpdb;
/**
* Default response
*/
$response = [
'steps' => [],
'total' => 0
];
$table = self::_table();
$sql = "SELECT * FROM {$table} ";
$where_sql = ' WHERE 1=1';
/**
* If automation id is provided
*/
if ( 0 !== intval( $aid ) ) {
$where_sql .= " AND `aid` = {$aid}";
}
/**
* If search needed
*/
if ( ! empty( $search ) ) {
$where_sql .= " AND `title` LIKE '%$search%'";
}
/** Get by Status */
if ( ! $get_deleted_nodes ) {
$where_sql .= " AND `status` NOT IN ( 3, 4 )";
}
if ( ! empty( $ids ) ) {
$where_sql .= " AND `ID` IN(" . implode( ',', $ids ) . ")";
}
/** Set Pagination */
$pagination_sql = '';
$limit = ! empty( $limit ) ? absint( $limit ) : 0;
$offset = ! empty( $offset ) ? absint( $offset ) : 0;
if ( ! empty( $limit ) || ! empty( $offset ) ) {
$pagination_sql = " LIMIT $offset, $limit";
}
/** Order By */
$order = " ORDER BY {$order_by} {$order}";
/** Form sql query */
$sql = $sql . $where_sql . $order . $pagination_sql;
$response['steps'] = $wpdb->get_results( $sql, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
/**
* Get total
*/
if ( $get_total ) {
$total_sql = "SELECT count(*) FROM {$table} " . $where_sql;
$response['total'] = absint( $wpdb->get_var( $total_sql ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
return $response;
}
/**
* Return table name
*
* @return string
*/
protected static function _table() {
global $wpdb;
return $wpdb->prefix . 'bwfan_automation_step';
}
/**
* Insert new automation to db
*
* @param $data
*
* @return int
*/
public static function create_new_automation_step( $data ) {
if ( empty( $data ) ) {
return;
}
self::insert( $data );
return absint( self::insert_id() );
}
/**
* Update automation step data by id
*
* @param $id
* @param $data
*
* @return bool
*/
public static function update_automation_step_data( $id, $data ) {
if ( ! is_array( $data ) ) {
return false;
}
return ! ! self::update( $data, array(
'id' => absint( $id ),
) );
}
/**
* Delete Automation steps
*
* @param $ids
*
* @return mixed
*/
public static function delete_automation_steps( $ids = [] ) {
if ( empty( $ids ) ) {
return false;
}
global $wpdb;
$table_name = self::_table();
if ( ! is_array( $ids ) ) {
$ids = [ $ids ];
}
$ids = array_filter( array_map( 'intval', $ids ) );
$placeholders = array_fill( 0, count( $ids ), '%d' );
$query = $wpdb->prepare( "DELETE FROM $table_name WHERE `ID` IN ( " . implode( ',', $placeholders ) . " ) AND status = 0", $ids );
return $wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_step_data_by_id( $step_id ) {
$result = BWFAN_Model_Automation_Step::get_specific_rows( 'ID', $step_id );
if ( empty( $result ) && ! is_array( $result ) ) {
return false;
}
return isset( $result[0] ) ? $result[0] : false;
}
public static function delete_steps_by_aid( $aid ) {
global $wpdb;
$table_name = self::_table();
$where = "aid = %d";
if ( is_array( $aid ) ) {
$where = "aid IN ('" . implode( "','", array_map( 'esc_sql', $aid ) ) . "')";
$aid = [];
}
$query = " DELETE FROM $table_name WHERE $where";
$query = $wpdb->prepare( $query, $aid );
return $wpdb->query( $wpdb->prepare( $query, $aid ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_step_by_trail( $trail ) {
global $wpdb;
$table_name = self::_table();
$query = "SELECT ct.c_time AS run_time, st.action, st.type FROM {$wpdb->prefix}bwfan_automation_contact_trail AS ct JOIN {$table_name} AS st ON ct.sid=st.ID WHERE ct.tid='$trail' ORDER BY ct.ID DESC LIMIT 1";
$results = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $results;
}
/**
* Get automation steps ids
*
* @param int $aid
*
* @return array
*/
public static function get_automation_step_ids( $aid ) {
if ( empty( $aid ) ) {
return [];
}
global $wpdb;
$table = self::_table();
$query = $wpdb->prepare( "SELECT ID FROM {$table} WHERE `status` != %d AND aid = %d", 3, $aid );
$results = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $results;
}
/**
* Return ID of the active step i.e. not equal to 3
*
* @param $id
*
* @return int|string|null
*/
public static function is_step_active( $id ) {
if ( empty( $id ) ) {
return 0;
}
global $wpdb;
$table = self::_table();
$query = $wpdb->prepare( "SELECT ID FROM {$table} WHERE `status` != %d AND ID = %d", 3, $id );
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get email step ids
*
* @return array
*/
public static function get_email_step_ids() {
global $wpdb;
$table = self::_table();
$sql = "SELECT `ID` FROM {$table} WHERE `action` LIKE '%s' AND `status`= %d ";
return $wpdb->get_col( $wpdb->prepare( $sql, '%wp_sendemail%', 1 ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* @param $stepids
* @param $action
*
* @return array
*/
public static function get_messaging_steps( $stepids, $action = 'wp_sendemail' ) {
global $wpdb;
$placeholder = array_fill( 0, count( $stepids ), '%d' );
$placeholder = implode( ", ", $placeholder );
$args = $stepids;
$args[] = '%' . $action . '"%';
$query = "SELECT `ID` FROM {$wpdb->prefix}bwfan_automation_step WHERE `ID` IN($placeholder) AND `action` LIKE %s";
$query = $wpdb->prepare( $query, $args );
return $wpdb->get_col( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Change steps status to archive
*
* @param $step_ids
*
* @return bool|int|mysqli_result|resource|null
*/
public static function update_steps_status( $step_ids ) {
if ( empty( $step_ids ) ) {
return false;
}
global $wpdb;
$table_name = self::_table();
$placeholder = array_fill( 0, count( $step_ids ), '%d' );
$placeholder = implode( ", ", $placeholder );
$args = array_merge( [ 4 ], $step_ids );
$query = "UPDATE $table_name SET `status` = %d WHERE `ID` IN ($placeholder)";
return $wpdb->query( $wpdb->prepare( $query, $args ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_step_data( $step_id ) {
$step = self::get_step_data_by_id( $step_id );
if ( empty( $step ) ) {
return [];
}
$step['data'] = ! empty( $step['data'] ) ? json_decode( $step['data'], true ) : [];
return $step;
}
/**
* Get Benchmark event name by step id
*
* @param $stepids
*
* @return array
*/
public static function get_benchmark_events( $stepids = [] ) {
if ( empty( $stepids ) ) {
return [];
}
global $wpdb;
$placeholder = array_fill( 0, count( $stepids ), '%d' );
$placeholder = implode( ", ", $placeholder );
$args = $stepids;
$table_name = self::_table();
$query = "SELECT `ID`, `action` FROM $table_name WHERE `ID` IN ($placeholder) AND `type` = 3;";
$query = $wpdb->prepare( $query, $args );
$results = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$return_array = [];
if ( is_array( $results ) && count( $results ) > 0 ) {
foreach ( $results as $val ) {
if ( ! empty( $val['action'] ) ) {
$benchmark = json_decode( $val['action'], true );
$benchmark = $benchmark['benchmark'] ?? '';
if ( ! empty( $benchmark ) ) {
$return_array[ $val['ID'] ] = $benchmark;
}
}
}
}
return $return_array;
}
}

View File

@@ -0,0 +1,118 @@
<?php
class BWFAN_Model_Automationmeta extends BWFAN_Model {
static $primary_key = 'ID';
public static function get_meta( $id, $key ) {
$rows = self::get_automation_meta( $id );
$value = false;
if ( count( $rows ) > 0 && isset( $rows[ $key ] ) ) {
$value = $rows[ $key ];
}
return $value;
}
public static function get_automation_meta( $automation_id, $cache = true ) {
if ( empty( $automation_id ) ) {
return [];
}
$meta = [];
$WooFunnels_Cache_obj = WooFunnels_Cache::get_instance();
if ( $cache ) {
$meta = $WooFunnels_Cache_obj->get_cache( 'bwfan_automations_meta_' . $automation_id, 'autonami' );
}
if ( empty( $meta ) ) {
global $wpdb;
$table = self::_table();
$sql_query = "SELECT bwfan_automation_id, meta_key, meta_value FROM {$table} WHERE bwfan_automation_id =%d";
$sql_query = $wpdb->prepare( $sql_query, $automation_id ); // WPCS: unprepared SQL OK
$result = $wpdb->get_results( $sql_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$meta = [];
if ( is_array( $result ) && count( $result ) > 0 ) {
foreach ( $result as $meta_values ) {
$key = $meta_values['meta_key'];
$meta[ $key ] = maybe_unserialize( $meta_values['meta_value'] );
}
}
$WooFunnels_Cache_obj->set_cache( 'bwfan_automations_meta_' . $automation_id, $meta, 'autonami' );
}
return $meta;
}
public static function update_automation_meta_values( $automation_id, $data ) {
if ( empty( $automation_id ) || empty( $data ) ) {
return false;
}
global $wpdb;
$table = self::_table();
foreach ( $data as $key => $value ) {
//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->update( $table, [
'meta_value' => $value
], [
'bwfan_automation_id' => intval( $automation_id ),
'meta_key' => $key
] );
}
return true;
}
public static function insert_automation_meta_data( $automation_id, $data ) {
if ( empty( $automation_id ) || empty( $data ) ) {
return false;
}
global $wpdb;
$table = self::_table();
foreach ( $data as $key => $value ) {
//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->insert( $table, [
'bwfan_automation_id' => intval( $automation_id ),
'meta_key' => $key,
'meta_value' => $value,
] );
}
return true;
}
public static function get_automations_meta( $aids, $meta_key = '' ) {
if ( empty( $aids ) || ! is_array( $aids ) ) {
return [];
}
$aids = implode( ', ', $aids );
global $wpdb;
$table = self::_table();
$sql_query = "SELECT bwfan_automation_id, meta_key, meta_value FROM {$table} WHERE bwfan_automation_id IN ($aids)";
if ( ! empty( $meta_key ) ) {
$sql_query .= " AND meta_key= 'event_meta'";
}
$result = $wpdb->get_results( $sql_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$meta = [];
if ( is_array( $result ) && count( $result ) > 0 ) {
foreach ( $result as $meta_values ) {
$key = $meta_values['meta_key'];
$meta[ $meta_values['bwfan_automation_id'] ][ $key ] = maybe_unserialize( $meta_values['meta_value'] );
}
}
return $meta;
}
public static function delete_automation_meta( $aid, $meta_key ) {
global $wpdb;
$table = self::_table();
$query = "DELETE FROM {$table} WHERE `bwfan_automation_id` = %d AND `meta_key` = %s ";
$wpdb->query( $wpdb->prepare( $query, $aid, $meta_key ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}

View File

@@ -0,0 +1,377 @@
<?php
/**
* Automation V2 modal class
*/
class BWFAN_Model_Automations_V2 extends BWFAN_Model {
static $primary_key = 'ID';
/**
* Check if automation exists
*
* @param $field
* @param $data
*
* @return bool
*/
public static function check_if_automation_exists( $field, $data ) {
global $wpdb;
$exists = false;
$query = 'SELECT ID FROM ' . self::_table();
$query .= $wpdb->prepare( " WHERE {$field} = %s ", $data );
$result = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( ! empty( $result ) ) {
$exists = true;
}
return $exists;
}
/**
* Returns table name
*
* @return string
*/
protected static function _table() {
global $wpdb;
return $wpdb->prefix . 'bwfan_automations';
}
/**
* Insert a new automation to db
*
* @param $data
*
* @return int|void
*/
public static function create_new_automation( $data ) {
if ( empty( $data ) ) {
return;
}
global $wpdb;
$wpdb->insert( self::_table(), $data ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return absint( $wpdb->insert_id );
}
/**
* Update the automation data
*
* @param $id
* @param $data
*
* @return bool|int|void
*/
public static function update_automation( $id, $data ) {
if ( empty( $data ) || 0 === intval( $id ) ) {
return;
}
global $wpdb;
$data = self::verify_columns( $data );
//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $wpdb->update( self::_table(), $data, [
'ID' => $id
] );
}
/**
* Parse table columns as passing dynamically from JS
*
* @param $data
*
* @return array
*/
public static function verify_columns( $data ) {
$arr = [];
if ( isset( $data['source'] ) ) {
$arr['source'] = $data['source'];
}
if ( isset( $data['event'] ) ) {
$arr['event'] = $data['event'];
}
if ( isset( $data['status'] ) ) {
$arr['status'] = $data['status'];
}
if ( isset( $data['priority'] ) ) {
$arr['priority'] = $data['priority'];
}
if ( isset( $data['start'] ) ) {
$arr['start'] = $data['start'];
}
if ( isset( $data['v'] ) ) {
$arr['v'] = $data['v'];
}
if ( isset( $data['benchmark'] ) ) {
$arr['benchmark'] = $data['benchmark'];
}
if ( isset( $data['title'] ) ) {
$arr['title'] = $data['title'];
}
return $arr;
}
/**
* Get automation row by ID
*
* @param $automation_id
*
* @return array|false|object|void|null
*/
public static function get_automation( $automation_id ) {
if ( empty( $automation_id ) ) {
return false;
}
global $wpdb;
$table_name = self::_table();
$query = 'SELECT * FROM ' . $table_name . ' WHERE ID = ' . $automation_id;
$core_cache_obj = WooFunnels_Cache::get_instance();
$result = $core_cache_obj->get_cache( md5( $query ), 'fka-automation' );
if ( false === $result ) {
$result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$core_cache_obj->set_cache( md5( $query ), $result, 'fka-automation' );
}
return $result;
}
/**
* Get active automation that has a goal
*
* @param $event_slug
*
* @return array
*/
public static function get_goal_automations( $event_slug ) {
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "SELECT `ID` FROM {$table_name} WHERE `status` = %d AND `v` = %d AND `benchmark` LIKE %s", 1, 2, "%$event_slug%" );
$core_cache_obj = WooFunnels_Cache::get_instance();
$result = $core_cache_obj->get_cache( md5( $query ), 'fka-automation' );
if ( false === $result ) {
$result = $wpdb->get_col( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$core_cache_obj->set_cache( md5( $query ), $result, 'fka-automation' );
}
return $result;
}
/**
* Return automation should run or not for a contact based on automation setting
*
* @param $automation_id
* @param $contact_id
* @param $automation_data
*
* @return bool
*/
public static function validation_automation_run_count( $automation_id = '', $contact_id = '', $automation_data = [], $exclude_check = false ) {
if ( empty( $automation_id ) || empty( $contact_id ) || empty( $automation_data ) ) {
return false;
}
if ( ! isset( $automation_data['event_meta'] ) || ! is_array( $automation_data['event_meta'] ) ) {
$automation_data['event_meta'] = [];
}
// handling when event_meta is not saved by default. In that case automation will run once by default
if ( ! isset( $automation_data['event_meta']['bwfan_automation_run'] ) ) {
$automation_data['event_meta']['bwfan_automation_run'] = 'once';
}
$event_meta = $automation_data['event_meta'];
if ( ! isset( $event_meta['bwfan_automation_run'] ) || 'multiple' === $event_meta['bwfan_automation_run'] ) {
return true;
}
if ( true === $exclude_check ) {
/** optimized if contact active in automation check is excluded */
$has_run = self::get_contact_automation_run_count( $automation_id, $contact_id, 'bool' );
/** If already run then return false as run once case */
return ! $has_run;
}
$has_run = self::get_contact_complete_automation_run_count( $automation_id, $contact_id, 'bool' );
/** If already run then return false as run once case */
return ! $has_run;
}
/**
* Get automation run count for a contact
*
* @param $automation_id
* @param $contact_id
* @param $return
*
* @return bool|int
*/
public static function get_contact_automation_run_count( $automation_id = '', $contact_id = '', $return = '' ) {
if ( empty( $automation_id ) || empty( $contact_id ) ) {
return 0;
}
global $wpdb;
$automation_contact_table = $wpdb->prefix . 'bwfan_automation_contact';
$automation_contact_complete_table = $wpdb->prefix . 'bwfan_automation_complete_contact';
$query = $wpdb->prepare( "SELECT count(*) as count FROM {$automation_contact_table} WHERE `cid` = %d AND `aid` = %d", $contact_id, $automation_id );
$running_count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( absint( $running_count ) > 0 && 'bool' === $return ) {
return true;
}
$query = $wpdb->prepare( "SELECT count(*) as count FROM {$automation_contact_complete_table} WHERE `cid` = %d AND `aid` = %d", $contact_id, $automation_id );
$complete_count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$count = ( absint( $running_count ) + absint( $complete_count ) );
if ( 'bool' === $return ) {
return ( $count > 0 );
}
return $count;
}
/**
* Get complete automation run count for a contact
*
* @param $automation_id
* @param $contact_id
* @param $return
*
* @return bool|int
*/
public static function get_contact_complete_automation_run_count( $automation_id = '', $contact_id = '', $return = '' ) {
if ( empty( $automation_id ) || empty( $contact_id ) ) {
return 0;
}
global $wpdb;
$automation_contact_complete_table = $wpdb->prefix . 'bwfan_automation_complete_contact';
$query = $wpdb->prepare( "SELECT count(*) as count FROM {$automation_contact_complete_table} WHERE `cid` = %d AND `aid` = %d", $contact_id, $automation_id );
$complete_count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( absint( $complete_count ) > 0 && 'bool' === $return ) {
return true;
}
return absint( $complete_count );
}
/**
* Returns automations
*
* @param int $offset
* @param int $limit
* @param string $search
* @param string $order
* @param string $order_by
* @param array $id
*
* @return array
*/
public function get_all_automations( $offset = 0, $limit = 0, $search = '', $order = 'DESC', $order_by = 'ID', $id = [] ) {
global $wpdb;
$table_name = self::_table();
$query = "SELECT * FROM $table_name WHERE 1=1";
if ( ! empty( $id ) ) {
$query .= $wpdb->prepare( " AND ID in ( " . implode( ',', $id ) . " )" );
}
if ( ! empty( $search ) ) {
$query .= $wpdb->prepare( " AND title LIKE %s", "%$search%" );
}
$query .= " ORDER BY $order_by $order";
if ( intval( $limit ) > 0 ) {
$offset = ! empty( $offset ) ? intval( $offset ) : 0;
$query .= $wpdb->prepare( " LIMIT %d, %d", $offset, $limit );
}
$core_cache_obj = WooFunnels_Cache::get_instance();
$result = $core_cache_obj->get_cache( md5( $query ), 'fka-automation' );
if ( false === $result ) {
$result = self::get_results( $query );
$core_cache_obj->set_cache( md5( $query ), $result, 'fka-automation' );
}
return is_array( $result ) && ! empty( $result ) ? $result : array();
}
/**
* Delete Automations
*
* @param $ids
*
* @return bool|int
*/
public function delete_automation( $ids ) {
if ( empty( $ids ) ) {
return false;
}
global $wpdb;
$table_name = self::_table();
if ( ! is_array( $ids ) ) {
$ids = [ $ids ];
}
$ids = implode( ',', array_map( 'absint', $ids ) );
return $wpdb->query( "DELETE FROM $table_name WHERE `ID` IN( $ids )" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get v2 automations ids by event slug
*
* @param $event_slugs
*
* @return array|object|stdClass[]|null
*/
public static function get_automations_by_slugs( $event_slugs ) {
if ( ! is_array( $event_slugs ) ) {
if ( empty( $event_slugs ) ) {
return [];
}
$event_slugs = [ $event_slugs ];
}
global $wpdb;
$table_name = self::_table();
$args = $event_slugs;
$args[] = 2;
$placeholder = array_fill( 0, count( $event_slugs ), '%s' );
$placeholder = implode( ', ', $placeholder );
$query = $wpdb->prepare( "SELECT `ID` FROM {$table_name} WHERE `event` IN ($placeholder) AND `v` = %d", $args );
$core_cache_obj = WooFunnels_Cache::get_instance();
$result = $core_cache_obj->get_cache( md5( $query ), 'fka-automation' );
if ( false === $result ) {
$result = self::get_results( $query );
$core_cache_obj->set_cache( md5( $query ), $result, 'fka-automation' );
}
return $result;
}
}

View File

@@ -0,0 +1,164 @@
<?php
class BWFAN_Model_Automations extends BWFAN_Model {
static $primary_key = 'ID';
public static function count_rows( $dependency = null ) {
global $wpdb;
$table_name = self::_table();
$sql = 'SELECT COUNT(*) FROM ' . $table_name;
if ( isset( $_GET['status'] ) && 'all' !== sanitize_text_field( $_GET['status'] ) ) { // WordPress.CSRF.NonceVerification.NoNonceVerification
$status = sanitize_text_field( $_GET['status'] ); // WordPress.CSRF.NonceVerification.NoNonceVerification
$status = ( 'active' === $status ) ? 1 : 2;
$sql = $wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE status = %d", $status ); // WPCS: unprepared SQL OK
}
return $wpdb->get_var( $sql ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Return Automation detail with its meta details
*
* @param $automation_id
*
* @return array|object|void|null
*/
public static function get_automation_with_data( $automation_id ) {
$data = self::get( $automation_id );
if ( ! is_array( $data ) || empty( $data ) ) {
return [];
}
$data['meta'] = BWFAN_Model_Automationmeta::get_automation_meta( $automation_id );
return $data;
}
/**
* Get first automation id
*/
public static function get_first_automation_id() {
global $wpdb;
$query = "SELECT MIN(id) FROM " . self::_table();
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get automation name
*/
public static function get_event_name( $automation_id ) {
global $wpdb;
$table_name = self::_table();
$query = $wpdb->prepare( "SELECT event FROM {$table_name} WHERE ID = %d", $automation_id );
$result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return isset( $result['event'] ) ? $result['event'] : '';
}
/**
* Get automation run count for a contact
*
* @param $automation_id
* @param $contact_id
*
* @return int
*/
public static function get_contact_automation_run_count( $automation_id = '', $contact_id = '' ) {
if ( empty( $automation_id ) || empty( $contact_id ) ) {
return 0;
}
global $wpdb;
$table = $wpdb->prefix . 'bwfan_contact_automations';
$query = $wpdb->prepare( "SELECT count(*) as count FROM {$table} WHERE `contact_id` = %d AND `automation_id` = %d", $contact_id, $automation_id );
$running_count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return absint( $running_count );
}
/**
* Get top 5 automations
*
* @return array|object|stdClass[]|null
*/
public static function get_top_automations() {
global $wpdb;
$automation_table = $wpdb->prefix . 'bwfan_automations';
if ( ! bwfan_is_woocommerce_active() ) {
$base_query = "SELECT COALESCE(a.`ID`,'') AS `aid`, COALESCE(a.`title`,'') AS `name`, a.`v`, a.event , SUM(IF(open>0, 1, 0)) AS `open_count` FROM {$automation_table} AS a LEFT JOIN {$wpdb->prefix}bwfan_engagement_tracking AS et ON et.oid = a.ID WHERE et.type = 1 GROUP BY et.`oid` ORDER BY `open_count` DESC LIMIT 0,5";
} else {
$conversion_table = $wpdb->prefix . 'bwfan_conversions';
$base_query = "SELECT COALESCE(a.`ID`,'') AS `aid`, COALESCE(a.`title`,'') AS `name`, a.`v`, a.event, SUM(c.`wctotal`) AS `total_revenue` FROM $automation_table AS a LEFT JOIN $conversion_table AS c ON c.`oid` = a.`ID` WHERE c.`otype` = 1 GROUP BY a.`ID` ORDER BY `total_revenue` DESC LIMIT 0,5";
}
return $wpdb->get_results( $base_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* @param string $where
*
* @return array|object|null
*/
public static function get_last_abandoned_cart( $where = '' ) {
global $wpdb;
$results = $wpdb->get_results( "SELECT `last_modified`, `items`, `total` FROM {$wpdb->prefix}bwfan_abandonedcarts {$where} ORDER BY `last_modified` DESC LIMIT 0,1", ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $results;
}
public static function get_tasks_for_contact( $contact_id ) {
global $wpdb;
$scheduled_tasks = array();
$get_contact_automation_id = $wpdb->get_col( "SELECT DISTINCT automation_id from {$wpdb->prefix}bwfan_contact_automations where contact_id='" . $contact_id . "'" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $get_contact_automation_id ) ) {
return $scheduled_tasks;
}
$stringPlaceholders = array_fill( 0, count( $get_contact_automation_id ), '%s' );
$placeholdersautomation = implode( ', ', $stringPlaceholders );
$scheduled_tasks_query = $wpdb->prepare( "
SELECT t.ID as id, t.integration_slug as slug, t.integration_action as action, t.automation_id as a_id, t.status as status, t.e_date as date
FROM {$wpdb->prefix}bwfan_tasks as t
LEFT JOIN {$wpdb->prefix}bwfan_taskmeta as m
ON t.ID = m.bwfan_task_id
WHERE t.automation_id IN ($placeholdersautomation)
ORDER BY t.e_date DESC
", $get_contact_automation_id );
$scheduled_tasks = $wpdb->get_results( $scheduled_tasks_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $scheduled_tasks;
}
public static function get_logs_for_contact( $contact_id ) {
$contact_logs = array();
global $wpdb;
$get_contact_automation_id = $wpdb->get_col( "SELECT DISTINCT automation_id from {$wpdb->prefix}bwfan_contact_automations where contact_id='" . $contact_id . "'" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $get_contact_automation_id ) ) {
return $contact_logs;
}
$stringPlaceholders = array_fill( 0, count( $get_contact_automation_id ), '%s' );
$placeholdersautomation = implode( ', ', $stringPlaceholders );
$contact_logs_query = $wpdb->prepare( "
SELECT l.ID as id, l.integration_slug as slug, l.integration_action as action, l.automation_id as a_id, l.status as status, l.e_date as date
FROM {$wpdb->prefix}bwfan_logs as l
LEFT JOIN {$wpdb->prefix}bwfan_logmeta as m
ON l.ID = m.bwfan_log_id
WHERE l.automation_id IN ($placeholdersautomation)
ORDER BY l.e_date DESC
", $get_contact_automation_id );
$contact_logs = $wpdb->get_results( $contact_logs_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $contact_logs;
}
}

View File

@@ -0,0 +1,86 @@
<?php
class BWFAN_Model_Contact_Automations extends BWFAN_Model {
static $primary_key = 'ID';
/**
* Get last automation run time for a contact
*
* @param $contact_id
*
* @return mixed|string|void
*/
public static function get_contact_automation_last_run( $contact_id, $automation_id ) {
global $wpdb;
$sql_query = 'SELECT time FROM {table_name} WHERE contact_id = %d AND automation_id = %d ORDER BY time DESC LIMIT 1';
$sql_query = $wpdb->prepare( $sql_query, $contact_id, $automation_id ); // WPCS: unprepared SQL OK
$last_run = self::get_results( $sql_query );
if ( ! is_array( $last_run ) || 0 === count( $last_run ) ) {
return __( 'N/A', 'wp-marketing-automations' );
}
return $last_run[0]['time'];
}
/**
* Get count for an automation run on a contact
*
* @param $contact_id
*
* @return int
*/
public static function get_contact_automations_run_count( $contact_id, $automation_id ) {
global $wpdb;
$sql_query = 'SELECT count(time) as count FROM {table_name} WHERE contact_id = %d AND automation_id = %d';
$sql_query = $wpdb->prepare( $sql_query, $contact_id, $automation_id ); // WPCS: unprepared SQL OK
$run_count = self::get_results( $sql_query );
if ( ! is_array( $run_count ) || 0 === count( $run_count ) ) {
return 0;
}
return $run_count[0]['count'];
}
/**
* Get all automations for a contact
*
* @param $contact_id
*
* @return array|int|object|null
*/
public static function get_all_automations_for_contact( $contact_id ) {
global $wpdb;
$sql_query = 'SELECT automation_id FROM {table_name} WHERE contact_id = %d ';
$sql_query = $wpdb->prepare( $sql_query, $contact_id ); // WPCS: unprepared SQL OK
$automations = self::get_results( $sql_query );
if ( ! is_array( $automations ) || 0 === count( $automations ) ) {
return 0;
}
return $automations;
}
/**
* Get all contact for an automation
*
* @param $automation_id
*
* @return array|int|object|null
*/
public static function get_all_contacts_for_automation( $automation_id ) {
global $wpdb;
$sql_query = 'SELECT contact_id FROM {table_name} WHERE automation_id = %d ';
$sql_query = $wpdb->prepare( $sql_query, $automation_id ); // WPCS: unprepared SQL OK
$contacts = self::get_results( $sql_query );
if ( ! is_array( $contacts ) || 0 === count( $contacts ) ) {
return 0;
}
return $contacts;
}
}

View File

@@ -0,0 +1,44 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Contact_Note' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_Model_Contact_Note extends BWFAN_Model {
/**
* @param $contact_id
* @param int $offset
* @param int $limit
*
* @return array
*/
public static function get_contact_notes( $contact_id, $offset = 0, $limit = 0 ) {
if ( empty( $offset ) && empty( $limit ) ) {
$query = "SELECT * FROM {table_name} WHERE `cid`='" . $contact_id . "' ORDER BY `created_date` DESC";
} else {
$query = "SELECT * FROM {table_name} WHERE `cid`='" . $contact_id . "' ORDER BY `created_date` DESC LIMIT $offset,$limit";
}
return self::get_results( $query );
}
/**
* Update contact note
*
* @param $contact_id
* @param $notes
* @param $note_id
*
* @return bool|int|mysqli_result|resource|null
*/
public static function update_contact_note( $contact_id, $notes, $note_id ) {
$data = $notes;
$where = array(
'id' => $note_id,
'cid' => $contact_id,
);
return self::update( $data, $where );
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,131 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Conversions' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_Model_Conversions extends BWFAN_Model {
static $primary_key = 'ID';
public static function get_conversions_by_source_type( $source_id, $source_type = 1, $limit = 0, $offset = 25 ) {
global $wpdb;
$table = self::_table();
$query = "SELECT bwc.* FROM $table as bwc JOIN {$wpdb->prefix}posts as p ON bwc.wcid=p.ID WHERE bwc.oid = $source_id AND bwc.otype=$source_type ORDER BY bwc.wcid DESC LIMIT $limit OFFSET $offset";
$conversions = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $conversions ) ) {
return [ 'conversions' => array(), 'total' => 0 ];
}
$total_query = "SELECT COUNT(*) FROM $table as bwc JOIN {$wpdb->prefix}posts as p ON bwc.wcid=p.ID WHERE bwc.oid = $source_id AND bwc.otype=$source_type";
$total = absint( $wpdb->get_var( $total_query ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
foreach ( $conversions as $key => $conv ) {
$order = wc_get_order( absint( $conv['wcid'] ) );
/** unset the conversion if order deleted or not exists */
if ( ! $order instanceof WC_Order ) {
unset( $conversions[ $key ] );
continue;
}
$order_details = [];
$order_details['f_name'] = $order->get_billing_first_name();
$order_details['l_name'] = $order->get_billing_last_name();
$order_details['email'] = $order->get_billing_email();
$order_details['status'] = $order->get_status();
$order_items = $order->get_items();
$order_details['items'] = [];
$order_details['currency'] = BWFAN_Automations::get_currency( $order->get_currency() );
foreach ( $order_items as $item_key => $item ) {
$product_id = $item->get_product_id(); // the Product id
$variation_id = $item->get_variation_id();
if ( ! empty( $variation_id ) ) {
$order_details['items'][ $variation_id ] = $item->get_name();
} else {
$order_details['items'][ $product_id ] = $item->get_name();
}
}
$conversions[ $key ] = array_replace( $conv, $order_details );
$conversions[ $key ]['wctotal'] = $order->get_total();
}
return [
'conversions' => $conversions,
'total' => $total
];
}
public static function get_conversions_by_oid( $oid, $contact_id, $engagements_ids = [], $type = 1 ) {
global $wpdb;
$table = self::_table();
$query = "SELECT wcid,date,wctotal FROM $table WHERE otype=$type AND oid=$oid AND cid=$contact_id";
if ( ! empty( $engagements_ids ) ) {
$engagements_ids = implode( ', ', $engagements_ids );
$query .= " AND trackid IN($engagements_ids)";
}
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_conversions_for_check_validity( $saved_last_conversion_id ) {
if ( empty( absint( $saved_last_conversion_id ) ) ) {
return [];
}
global $wpdb;
$table = self::_table();
$and = '';
if ( ! empty( $saved_last_conversion_id ) ) {
$and .= " AND ID <= $saved_last_conversion_id";
}
$query = "SELECt ID,wcid FROM $table WHERE 1=1 $and ORDER BY ID DESC";
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_last_conversion_id() {
global $wpdb;
$table = self::_table();
$query = "SELECT MAX(`ID`) FROM $table";
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function delete_conversions_by_track_id( $ids ) {
if ( empty( $ids ) ) {
return;
}
global $wpdb;
$table = self::_table();
$placeholders = array_fill( 0, count( $ids ), '%d' );
$placeholders = implode( ', ', $placeholders );
$query = $wpdb->prepare( "DELETE FROM {$table} WHERE trackid IN ($placeholders)", $ids );
return $wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_automation_revenue( $aid, $start_date, $end_date, $is_interval, $interval ) {
global $wpdb;
$table = self::_table();
$date_col = "date";
$interval_query = '';
$group_by = '';
$order_by = ' ID ';
if ( 'interval' === $is_interval ) {
$get_interval = BWFCRM_Dashboards::get_interval_format_query( $interval, $date_col );
$interval_query = $get_interval['interval_query'];
$interval_group = $get_interval['interval_group'];
$group_by = "GROUP BY " . $interval_group;
$order_by = ' time_interval ';
}
$base_query = "SELECT count(ID) as conversions, SUM(wctotal) as revenue $interval_query FROM `" . $table . "` WHERE 1=1 AND oid = $aid AND otype = 1 AND `" . $date_col . "` >= '" . $start_date . "' AND `" . $date_col . "` <= '" . $end_date . "'" . $group_by . " ORDER BY " . $order_by . " ASC";
return $wpdb->get_results( $base_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}
}

View File

@@ -0,0 +1,18 @@
<?php
class BWFAN_Model_Customers extends BWFAN_Model {
static $primary_key = 'ID';
public static function bwf_get_customer_data_by_id( $id, $by_contact = true ) {
global $wpdb;
if ( $by_contact ) {
$query = $wpdb->prepare( "SELECT COALESCE(customer.total_order_count, 0) as orders, COALESCE(customer.total_order_value, 0) as revenue, contact.f_name, contact.l_name, contact.id as cid, contact.email FROM {$wpdb->prefix}bwf_contact as contact LEFT JOIN {$wpdb->prefix}bwf_wc_customers as customer ON contact.id = customer.cid WHERE contact.id = %d;", $id );
} else {
$query = $wpdb->prepare( "SELECT COALESCE(customer.total_order_count, 0) as orders, COALESCE(customer.total_order_value, 0) as revenue, contact.f_name, contact.l_name, contact.id as cid, contact.email FROM {$wpdb->prefix}bwf_contact as contact LEFT JOIN {$wpdb->prefix}bwf_wc_customers as customer ON contact.id = customer.cid WHERE contact.wpid = %d;", $id );
}
$data = $wpdb->get_row( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $data;
}
}

View File

@@ -0,0 +1,703 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Engagement_Tracking' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_Model_Engagement_Tracking extends BWFAN_Model {
static $primary_key = 'ID';
static function get_conversations_by_cid( $cid, $mode, $offset = 0, $limit = 25 ) {
global $wpdb;
$table = self::_table();
$and = '';
if ( ! empty( $mode ) ) {
$and = " AND con.mode = $mode";
}
$query = "SELECT con.*, ct.subject, ct.template as message FROM $table AS con LEFT JOIN {$wpdb->prefix}bwfan_templates AS ct ON con.tid=ct.ID WHERE cid = '$cid' $and ORDER BY created_at DESC LIMIT $limit OFFSET $offset";
return self::get_results( $query );
}
public static function get_total_engagements( $cid, $mode ) {
global $wpdb;
$table = self::_table();
$query = [];
$query[] = "SELECT COUNT(ID) FROM {$table} WHERE 1=1";
$query[] = $wpdb->prepare( "AND cid = %d", $cid );
$query[] = $wpdb->prepare( "AND mode = %d", $mode );
return $wpdb->get_var( implode( ' ', $query ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_recipents_by_type( $oid, $type, $offset = 0, $limit = 25 ) {
global $wpdb;
/** Fetching all Engagements for broadcast **/
$table = self::_table();
$query = "SELECT conv.ID AS conversation_id,c.f_name,c.l_name,c.wpid,conv.send_to,conv.cid,conv.mode,conv.type,conv.open,conv.click,conv.oid,if(conv.c_status=2,1,0) as sent,conv.created_at as sent_time FROM {table_name} AS conv LEFT JOIN {$wpdb->prefix}bwf_contact AS c ON c.ID = conv.cid WHERE conv.type = $type AND conv.oid = $oid AND (c_status = 2 OR c_status = 3) ORDER BY conv.updated_at DESC LIMIT $limit OFFSET $offset";
$results = self::get_results( $query );
/** Fetch Engagements total count **/
$query = "SELECT COUNT(conv.ID) FROM $table AS conv WHERE conv.type = $type AND conv.oid = $oid";
$total = absint( $wpdb->get_var( $query ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$conversations_ids = empty( $results ) ? [] : array_column( $results, 'conversation_id' );
$conversions = [];
/** Fetch Engagement's conversions **/
if ( ! empty( $conversations_ids ) ) {
$conv_ids = implode( ', ', $conversations_ids );
$conversion_query = "SELECT wcid,cid,trackid,wctotal FROM {$wpdb->prefix}bwfan_conversions WHERE trackid IN( $conv_ids ) AND otype = $type";
$conversions_result = self::get_results( $conversion_query );
foreach ( $conversions_result as $conversion ) {
if ( ! isset( $conversions[ absint( $conversion['cid'] ) ] ) ) {
$conversions[ absint( $conversion['cid'] ) ] = [];
}
if ( ! isset( $conversions[ absint( $conversion['cid'] ) ][ absint( $conversion['trackid'] ) ] ) ) {
$conversions[ absint( $conversion['cid'] ) ][ absint( $conversion['trackid'] ) ] = 0;
}
$conversions[ absint( $conversion['cid'] ) ][ absint( $conversion['trackid'] ) ] += floatval( $conversion['wctotal'] );
}
}
$send_to = empty( $results ) ? [] : array_column( $results, 'send_to' );
$recipients = implode( "', '", $send_to );
$unsubscribers = [];
/** Fetch Unsubsribers of broadcast **/
if ( ! empty( $recipients ) ) {
$unsubscribe_query = "SELECT ID,recipient FROM {$wpdb->prefix}bwfan_message_unsubscribe WHERE recipient IN ('$recipients') AND automation_id=$oid AND c_type = 2";
$unsubscribers_result = self::get_results( $unsubscribe_query );
foreach ( $unsubscribers_result as $unsubscriber ) {
$unsubscribers[ $unsubscriber['recipient'] ] = $unsubscriber;
}
}
$conversations = array_map( function ( $engagement ) use ( $conversions, $unsubscribers ) {
$conv_id = absint( $engagement['conversation_id'] );
$contact_id = absint( $engagement['cid'] );
$contact = new BWFCRM_Contact( $contact_id );
$deleted = ! $contact->is_contact_exists();
$revenue = ( isset( $conversions[ $contact_id ][ $conv_id ] ) ) ? $conversions[ $contact_id ][ $conv_id ] : 0;
$conversions_count = ( isset( $conversions[ $contact_id ][ $conv_id ] ) ) ? 1 : 0;
$unsubscribed = ( isset( $unsubscribers[ $engagement['send_to'] ] ) ) ? 1 : 0;
$engagement['revenue'] = $revenue;
$engagement['conversions'] = $conversions_count;
$engagement['unsubscribed'] = $unsubscribed;
$engagement['contact_deleted'] = $deleted;
return $engagement;
}, $results );
return array( 'conversations' => $conversations, 'total' => $total );
}
public static function get_automation_recipents( $oid, $offset = 0, $limit = 25 ) {
global $wpdb;
$table = self::_table();
$type = BWFAN_Email_Conversations::$TYPE_AUTOMATION;
/** Fetching all Engagements for automation **/
$query = "SELECT GROUP_CONCAT(DISTINCT ID) as track_ids,cid, send_to, SUM(open) as open,mode, SUM(click) as click, COUNT(*) as total, SUM(IF(c_status=2,1,0)) as sent,MAX(created_at) as sent_time FROM $table WHERE oid = $oid AND type = $type AND cid>0 AND (c_status = 2 OR c_status = 3) GROUP BY send_to,cid,mode ORDER BY sent_time DESC LIMIT $limit OFFSET $offset";
$engagements = self::get_results( $query );
if ( empty( $engagements ) ) {
array( 'conversations' => [], 'total' => 0 );
}
/** Get Engagements total count **/
$count_query = "SELECT count(send_to) FROM $table WHERE oid = $oid AND type = $type AND cid>0 AND (c_status = 2 OR c_status = 3) GROUP BY send_to,cid,mode";
$total = self::get_results( $count_query );
$c_ids = empty( $engagements ) ? [] : array_column( $engagements, 'cid' );
$conversions = [];
$contacts = [];
if ( ! empty( $c_ids ) ) {
/** Get contacts f_name, l_name **/
$cids = implode( ',', $c_ids );
$contact_query = "SELECT id,f_name,l_name,email,contact_no FROM {$wpdb->prefix}bwf_contact WHERE id IN($cids)";
$contact_data = self::get_results( $contact_query );
foreach ( $contact_data as $contact ) {
$contacts[ $contact['id'] ] = $contact;
}
/** Get contacts conversion **/
$conversion_query = "SELECT c.wcid,c.cid,c.trackid,c.wctotal FROM {$wpdb->prefix}bwfan_conversions as c JOIN {$wpdb->prefix}posts as p on c.wcid=p.ID WHERE 1=1 AND c.oid = $oid AND c.otype = $type AND c.cid IN( $cids )";
$conversion_result = self::get_results( $conversion_query );
foreach ( $conversion_result as $conversion ) {
if ( ! isset( $conversions[ absint( $conversion['cid'] ) ] ) ) {
$conversions[ absint( $conversion['cid'] ) ] = [];
}
if ( ! isset( $conversions[ absint( $conversion['cid'] ) ][ absint( $conversion['trackid'] ) ] ) ) {
$conversions[ absint( $conversion['cid'] ) ][ absint( $conversion['trackid'] ) ] = 0;
}
$conversions[ absint( $conversion['cid'] ) ][ absint( $conversion['trackid'] ) ] += floatval( $conversion['wctotal'] );
}
}
$send_to = empty( $engagements ) ? [] : array_column( $engagements, 'send_to' );
$recipients = implode( "', '", $send_to );
$unsubscribers = [];
if ( ! empty( $recipients ) ) {
/** Get unsubscribers data **/
$unsubscribe_query = "SELECT ID,recipient FROM {$wpdb->prefix}bwfan_message_unsubscribe WHERE recipient IN ('$recipients') AND automation_id=$oid AND c_type = $type";
$unsubscribers_result = self::get_results( $unsubscribe_query );
foreach ( $unsubscribers_result as $unsubscriber ) {
$unsubscribers[ $unsubscriber['recipient'] ] = $unsubscriber;
}
}
$conversations = array_map( function ( $engagement ) use ( $contacts, $conversions, $unsubscribers ) {
$contact_id = absint( $engagement['cid'] );
$deleted = true;
if ( isset( $contacts[ $contact_id ] ) ) {
$engagement['f_name'] = $contacts[ $contact_id ]['f_name'];
$engagement['l_name'] = $contacts[ $contact_id ]['l_name'];
$deleted = false;
}
$revenue = 0;
$conversions_count = 0;
$track_ids = $engagement['track_ids'];
$track_ids = explode( ',', $track_ids );
$track_ids = array_map( 'absint', $track_ids );
if ( isset( $conversions[ $contact_id ] ) ) {
$conversion_trackids = array_keys( $conversions[ $contact_id ] );
if ( array_intersect( $track_ids, $conversion_trackids ) ) {
$revenue = array_sum( $conversions[ $contact_id ] );
}
$conversions_count = count( $conversion_trackids );
}
$engagement['revenue'] = $revenue;
$engagement['conversions'] = $conversions_count;
$engagement['unsubscribed'] = isset( $unsubscribers[ $engagement['send_to'] ] ) ? 1 : 0;
$engagement['contact_deleted'] = $deleted;
return $engagement;
}, $engagements );
return array( 'conversations' => $conversations, 'total' => count( $total ) );
}
public static function get_engagement_recipient_timeline( $convid ) {
$conv = self::get( absint( $convid ) );
if ( empty( $conv ) || ! isset( $conv['o_interaction'] ) ) {
return array();
}
$conversions = BWFAN_Model_Conversions::get_specific_rows( 'trackid', absint( $convid ) );
$final_data = self::prepare_timeline_data( $conv, $conversions );
return $final_data;
}
public static function prepare_timeline_data( $conv, $conversions = [] ) {
$opens = ! empty( $conv['o_interaction'] ) ? json_decode( $conv['o_interaction'], true ) : array();
$clicks = ! empty( $conv['c_interaction'] ) ? json_decode( $conv['c_interaction'], true ) : array();
$mode = $conv['mode'];
$final_data = [
[
'type' => 2 === absint( $conv['c_status'] ) ? 'sent' : 'failed',
'mode' => $mode,
'date' => ! empty( $conv['created_at'] ) ? get_date_from_gmt( $conv['created_at'] ) : ''
]
];
if ( 3 === absint( $conv['c_status'] ) ) {
$msg = BWFAN_Model_Engagement_Trackingmeta::get_meta( $conv['ID'], 'error_msg' );
$final_data[0]['err_message'] = empty( $msg[0]['meta_value'] ) ? 'Email not sent' : $msg[0]['meta_value'];
}
/** Opens */
$final_data = array_merge( $final_data, array_map( function ( $open ) {
return array( 'type' => 'open', 'date' => $open );
}, $opens ) );
/** Clicks */
$final_data = array_merge( $final_data, array_map( function ( $click ) use ( $mode ) {
return array( 'type' => 'click', 'mode' => $mode, 'date' => $click );
}, $clicks ) );
if ( ! empty( $conversions ) ) {
/** Conversions */
$final_data = array_merge( $final_data, array_map( function ( $conversion ) {
$order = wc_get_order( $conversion['wcid'] );
return array(
'type' => 'conversion',
'date' => ! empty( $conversion['date'] ) ? get_date_from_gmt( $conversion['date'] ) : '',
'revenue' => $order->get_total(),
'order_id' => $conversion['wcid'],
'currency' => BWFAN_Automations::get_currency( $order->get_currency() )
);
}, $conversions ) );
}
usort( $final_data, function ( $datum1, $datum2 ) {
return strtotime( $datum1['date'] ) > strtotime( $datum2['date'] ) ? - 1 : 1;
} );
return $final_data;
}
/**
* @param $automation_id
* @param int $contat_id
*
* @return array
*/
public static function get_automation_recipient_timeline( $automation_id, $contact_id, $mode ) {
$table = self::_table();
$query = " SELECT ID,mode,c_status,c_interaction,o_interaction,created_at FROM $table WHERE type = 1 AND oid = $automation_id AND cid = $contact_id AND (c_status = 2 OR c_status = 3) AND mode=$mode";
$engagements = self::get_results( $query );
$engage_ids = array_map( function ( $engagement ) {
return $engagement['ID'];
}, $engagements );
$conversions = BWFAN_Model_Conversions::get_conversions_by_oid( $automation_id, $contact_id, $engage_ids );
$final_data = [];
foreach ( $engagements as $engagement ) {
$final_data = array_merge( $final_data, self::prepare_timeline_data( $engagement ) );
}
/** Conversions */
$final_data = array_merge( $final_data, array_map( function ( $conversion ) {
$order = wc_get_order( $conversion['wcid'] );
return array(
'type' => 'conversion',
'date' => ! empty( $conversion['date'] ) ? get_date_from_gmt( $conversion['date'] ) : '',
'revenue' => $order->get_total(),
'order_id' => $conversion['wcid'],
'currency' => BWFAN_Automations::get_currency( $order->get_currency() )
);
}, $conversions ) );
usort( $final_data, function ( $data1, $data2 ) {
return strtotime( $data1['date'] ) > strtotime( $data2['date'] ) ? - 1 : 1;
} );
return $final_data;
}
/**
* @param $oid (Conversation's Source ID)
* @param int $o_type (Conversation's Source Type)
*
* @return false|string|null
*/
public static function get_first_conversation_date( $oid, $o_type = 1 ) {
if ( empty( $oid ) ) {
return false;
}
global $wpdb;
$query = "SELECT MIN(created_at) FROM `{$wpdb->prefix}bwfan_engagement_tracking` where oid = $oid and type = $o_type";
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* @param $after
* @param $before
*
* @return array|object|null
*/
public static function get_stats( $after, $before ) {
global $wpdb;
$query = "select count( ID ) as sent_total, sum( open ) as total_open, sum( click ) as total_click from {$wpdb->prefix}bwfan_engagement_tracking where c_status = 2 group by c_status";
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* @return array|object|null
*/
public static function get_popular_emails() {
global $wpdb;
$template_table = $wpdb->prefix . 'bwfan_templates';
$engagement_table = $wpdb->prefix . 'bwfan_engagement_tracking';
$query = "SELECT `tid`, SUM(`open`) as `opens`, SUM(`click`) as `clicks` FROM $engagement_table WHERE tid IS NOT NULL AND open > 0 GROUP BY tid ORDER BY `opens` DESC LIMIT 0,5";
$results = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $results ) ) {
return [];
}
$tids = array_column( $results, 'tid' );
$placeholders = array_fill( 0, count( $tids ), '%d' );
$placeholders = implode( ', ', $placeholders );
$query = $wpdb->prepare( "SELECT `ID`, `subject` FROM $template_table WHERE `ID` IN ($placeholders)", $tids );
$result2 = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$subjects = [];
foreach ( $result2 as $val ) {
$subjects[ $val['ID'] ] = $val['subject'];
}
foreach ( $results as $key => $val ) {
if ( isset( $subjects[ $val['tid'] ] ) ) {
$results[ $key ]['subject'] = $subjects[ $val['tid'] ];
}
}
return $results;
}
public static function get_last_engagement_sent_time( $contact_id, $mode = 1 ) {
$query = "SELECT max(created_at) as last_sent FROM {table_name} WHERE cid = $contact_id AND mode = $mode AND c_status = 2";
$results = self::get_results( $query );
$res = '';
if ( ! empty( $results[0]['last_sent'] ) ) {
$res = $results[0]['last_sent'];
}
return $res;
}
public static function get_last_24_hours_conversations_count( $mode = 1 ) {
global $wpdb;
$start_time = date( 'Y-m-d H:i:s', strtotime( '-24 hours' ) );
$and_mode = ! empty( absint( $mode ) ) ? " AND mode = $mode " : "";
$query = "SELECT COUNT(ID) FROM {$wpdb->prefix}bwfan_engagement_tracking WHERE c_status = 2 AND created_at > '$start_time' $and_mode";
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_most_interacted_template( $oid, $otype, $mode ) {
global $wpdb;
$interaction = ( BWFAN_Email_Conversations::$MODE_SMS === intval( $mode ) ) ? 'click' : 'open';
$interaction = apply_filters( 'bwfan_most_interacted_template_based_on', $interaction, $mode, $oid );
$sql = "SELECT tid, SUM($interaction) AS $interaction FROM `{$wpdb->prefix}bwfan_engagement_tracking` WHERE oid=$oid AND type=$otype AND c_status=2 GROUP BY tid ORDER BY $interaction DESC LIMIT 0, 1";
return $wpdb->get_row( $sql, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Return first engagement id
*/
public static function get_first_engagement_id() {
global $wpdb;
$query = 'SELECT MIN(`ID`) FROM ' . self::_table();
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get automation step analytics
*
* @param $oid
* @param $step_ids
* @param $after_date
*
* @return array|object|stdClass
*/
public static function get_automation_step_analytics( $oid, $step_ids, $after_date = '', $end_date = '' ) {
global $wpdb;
if ( empty( $step_ids ) ) {
return [];
}
$table = "{$wpdb->prefix}bwfan_engagement_tracking";
$step_ids = ! is_array( $step_ids ) ? [ $step_ids ] : $step_ids;
$ids = implode( "','", $step_ids );
$conversions_query = "SELECT trackid, count(ID) as conversions, SUM(wctotal) as revenue, cid FROM {$wpdb->prefix}bwfan_conversions GROUP BY trackid,cid";
$query = "SELECT SUM(if(con.open>0,1,0)) AS open_count,(SUM(IF(con.open>0, 1, 0))/COUNT(con.ID)) * 100 as open_rate ,SUM(IF(con.c_status=2, 1, 0)) as sent,SUM(if(con.click>0,1,0)) AS click_count,(SUM(IF(con.click>0, 1, 0))/COUNT(con.ID)) * 100 as click_rate, SUM(conv.conversions) as conversions, SUM(conv.revenue) as revenue, COUNT(DISTINCT con.cid) as contacts_count FROM {$table} AS con LEFT JOIN ({$conversions_query}) as conv ON con.ID = conv.trackid WHERE 1=1 AND con.type = " . BWFAN_Email_Conversations::$TYPE_AUTOMATION . " AND con.sid IN('$ids') AND con.c_status = 2 ";
if ( ! empty( $oid ) ) {
$query .= " AND con.oid IN ($oid) ";
}
/** Add query for get data after date */
if ( ! empty( $after_date ) && empty( $end_date ) ) {
$query .= " AND con.created_at > '$after_date'";
}
if ( ! empty( $after_date ) && ! empty( $end_date ) ) {
$query .= " AND ( con.created_at > '$after_date' AND con.created_at < '$end_date' ) ";
}
$results = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$results['unsubscribers'] = self::get_automation_unsubscribers( $oid, $step_ids );
return $results;
}
public static function get_automation_unsubscribers( $aid, $step_ids = [] ) {
global $wpdb;
$query = "SELECT COUNT(*) FROM {$wpdb->prefix}bwfan_message_unsubscribe WHERE `automation_id`=%d AND `c_type`=1";
$args = [ $aid ];
if ( count( $step_ids ) > 0 ) {
$placeholders = array_fill( 0, count( $step_ids ), '%d' );
$placeholders = implode( ', ', $placeholders );
$query .= " AND `sid` IN($placeholders) ";
$args = array_merge( $args, $step_ids );
}
$query = $wpdb->prepare( $query, $args );
return absint( $wpdb->get_var( $query ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_contact_engagements( $aid, $cid, $start_date, $last_date ) {
global $wpdb;
$table = self::_table();
$query = $wpdb->prepare( "SELECT ID FROM {$table} WHERE cid = %d AND oid = %d AND type = 1 AND `created_at` BETWEEN %s AND %s", $cid, $aid, $start_date, $last_date );
return $wpdb->get_col( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function delete_contact_engagements( $ids ) {
if ( empty( $ids ) ) {
return;
}
global $wpdb;
$table = self::_table();
$placeholders = array_fill( 0, count( $ids ), '%d' );
$placeholders = implode( ', ', $placeholders );
$query = $wpdb->prepare( "DELETE FROM {$table} WHERE ID IN ($placeholders)", $ids );
return $wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_engagements_tid( $aid, $sids ) {
global $wpdb;
if ( ! is_array( $sids ) || empty( $sids ) ) {
return [];
}
$placeholders = array_fill( 0, count( $sids ), '%d' );
$placeholders = implode( ', ', $placeholders );
$args = [ $aid, 1 ];
$args = array_merge( $args, $sids );
$query = "SELECT `sid`, `tid` FROM {$wpdb->prefix}bwfan_engagement_tracking WHERE `oid` = %d AND `type` = %d AND `sid` IN ($placeholders)";
return $wpdb->get_results( $wpdb->prepare( $query, $args ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get email activity by filters
*
* @param string $search
* @param array $filters
* @param int $offset
* @param int $limit Limit
* @param int $mode
*
* @return array
*
*/
public static function get_engagements_activity( $search = '', $filters = [], $offset = 0, $limit = 10, $mode = 0 ) {
global $wpdb;
$table = self::_table();
$query = "SELECT `ID`,`cid`,`created_at`,`type`,`send_to`,`open`,`click`,`oid`,`author_id`,`tid`,`c_status`,`mode` FROM {$table} WHERE 1=1";
$count = "SELECT COUNT(`ID`) AS total_count FROM {$table} WHERE 1=1";
$con_data = [];
$filter_query = '';
if ( intval( $mode ) > 0 ) {
$mode_query = $wpdb->prepare( " AND mode = %d", intval( $mode ) );
$query .= $mode_query;
$count .= $mode_query;
}
if ( ! empty( $search ) ) {
$filter_query .= $wpdb->prepare( " AND send_to LIKE %s", "%" . esc_sql( $search ) . "%" );
}
/** Default status filter query to exclude drafts */
$status_filter_query = $wpdb->prepare( " AND c_status != %d", 1 );
/** Filters passed */
foreach ( $filters as $filter ) {
switch ( $filter['filter'] ) {
case 'source':
if ( ! empty( $filter['data'] ) ) {
$oid = array_column( $filter['data'], 'id' );
}
switch ( $filter['rule'] ) {
case 1:
if ( ! empty( $oid ) ) {
$filter_query .= " AND oid IN (" . implode( ', ', $oid ) . ") AND type = 1";
} else {
$filter_query .= " AND type = 1";
}
break;
case 2:
if ( ! empty( $oid ) ) {
$filter_query .= " AND oid IN (" . implode( ', ', $oid ) . ") AND type = 2";
} else {
$filter_query .= " AND type = 2";
}
break;
case 6:
$filter_query .= " AND type = 6";
break;
case 9:
$filter_query .= " AND type = 9";
break;
}
break;
case 'status':
$status = ! empty( $filter['data'] ) ? $filter['data'] : '';
if ( ! empty( $status ) ) {
$status_filter_query = $wpdb->prepare( " AND c_status = %d", $status );
}
break;
case 'period':
$start_date = ! empty( $filter['data']['after'] ) ? $filter['data']['after'] : '';
$end_date = ! empty( $filter['data']['before'] ) ? $filter['data']['before'] : '';
if ( ! empty( $start_date ) && ! empty( $end_date ) ) {
$filter_query .= " AND (created_at >= '{$start_date}' AND created_at <= '{$end_date}')";
}
break;
}
}
/** Append Status filter query */
$filter_query .= $status_filter_query;
$query .= $filter_query;
$query .= $wpdb->prepare( " ORDER BY ID DESC LIMIT %d OFFSET %d", $limit, $offset );
//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$results = $wpdb->get_results( $query, ARRAY_A );
if ( empty( $results ) ) {
return array(
'data' => [],
'total' => 0
);
}
/** All Contact IDs */
$cids = array_unique( array_column( $results, 'cid' ) );
$con_query = $wpdb->prepare( "SELECT ID, f_name, email, l_name FROM {$wpdb->prefix}bwf_contact WHERE ID IN (" . implode( ',', array_map( 'absint', $cids ) ) . ")" );
$contact_data = $wpdb->get_results( $con_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
/** Contact array with ID as key */
$contact_map = array_column( $contact_data, null, 'ID' );
/** Single Template IDs array */
$tids = [];
$message_track_ids = [];
foreach ( $results as $result ) {
if ( empty( $result['tid'] ) ) {
$message_track_ids[] = $result['ID'];
continue;
}
$tids[] = $result['tid'];
}
$template = $wpdb->prepare( "SELECT ct.ID, ct.subject, ct.title, ct.mode FROM {$wpdb->prefix}bwfan_templates AS ct WHERE ct.ID IN (" . implode( ',', array_map( 'absint', $tids ) ) . ")" );
$template_data = $wpdb->get_results( $template, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
/** Map subjects by ID */
$template_map = [];
$transactional_data = [];
foreach ( $template_data as $template ) {
if ( class_exists( 'BWFCRM_Core' ) && isset( BWFCRM_Core()->transactional_mails ) && ! empty( $template['mode'] ) && 7 === absint( $template['mode'] ) ) {
if ( ! isset( $transactional_data[ $template['title'] ] ) ) {
$mail_class = BWFCRM_Core()->transactional_mails;
$transactional_val = $mail_class->get_transactional_mail_by_slug( $template['title'] );
$title = ! empty( $transactional_val['title'] ) ? $transactional_val['title'] : $template['title'];
$transactional_data[ $template['title'] ] = $transactional_val['title'];
} else {
$transactional_data[ $template['title'] ] = $template['title'];
}
} else {
$title = '';
}
$template_map[ $template['ID'] ] = [
'subject' => $template['subject'],
'title' => $title
];
}
$message_data = [];
if ( ! empty( $message_track_ids ) ) {
$m_data = BWFAN_Model_Message::get_message_by_track_id( $message_track_ids );
foreach ( $m_data as $data ) {
$message_data[ $data['track_id'] ] = $data['subject'];
}
}
$con_class = new BWFAN_Email_Conversations();
foreach ( $results as $key => $val ) {
$con_data[ $key ]['ID'] = $val['ID'];
$con_data[ $key ]['mode'] = $val['mode'];
$con_data[ $key ]['type'] = $val['type'];
/** Retrieve contact details from map */
$contact = $contact_map[ $val['cid'] ] ?? array();
$con_data[ $key ]['contact'] = [];
if ( ! empty( $contact ) ) {
$con_data[ $key ]['contact'] = array(
'id' => $contact['ID'],
'f_name' => $contact['f_name'],
'l_name' => $contact['l_name'],
'email' => $contact['email'],
'link' => add_query_arg( array( 'page' => 'autonami', 'path' => '/contact/' . $contact['ID'] . '/profile' ), admin_url( 'admin.php' ) ),
);
}
/** Get source data */
$source = $con_class->get_source( [
'oid' => $val['oid'],
'type' => $val['type'],
'author_id' => $val['author_id']
], ! empty( $val['tid'] ) && isset( $template_map[ $val['tid'] ] ) ? $template_map[ $val['tid'] ] : [] );
if ( ! empty( $source ) ) {
$con_data[ $key ]['source'] = $source;
}
/** Set sent status and sent date */
$con_data[ $key ]['c_status'] = $val['c_status'];
$con_data[ $key ]['sent_on'] = $val['created_at'];
$con_data[ $key ]['open'] = $val['open'];
$con_data[ $key ]['click'] = $val['click'];
$con_data[ $key ]['send_to'] = $val['send_to'];
/** Retrieve subject from map */
$subject = ! empty( $val['tid'] ) && isset( $template_map[ $val['tid'] ] ) && $template_map[ $val['tid'] ]['subject'] ? $template_map[ $val['tid'] ]['subject'] : '';
$subject = empty( $subject ) && isset( $message_data[ $val['ID'] ] ) ? $message_data[ $val['ID'] ] : $subject;
$con_data[ $key ]['subject'] = ! empty( $subject ) ? $subject : __( 'No Subject', 'wp-marketing-automations' );
}
return [
'data' => $con_data,
'total' => $wpdb->get_var( $count . $filter_query ) //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
];
}
public static function get_engagements_by_tid( $tid, $only_count = false ) {
global $wpdb;
$table = self::_table();
if ( $only_count ) {
$query = "SELECT COUNT(ID) as count FROM {$table} WHERE tid = $tid";
} else {
$query = "SELECT * FROM {$table} WHERE tid = $tid";
}
return $only_count ? $wpdb->get_var( $query ) : $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}
}

View File

@@ -0,0 +1,63 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Engagement_Trackingmeta' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_Model_Engagement_Trackingmeta extends BWFAN_Model {
static $primary_key = 'ID';
static function get_meta( $con_id, $key = '' ) {
global $wpdb;
$table = self::_table();
$meta_key_query = '';
if ( ! empty( $key ) ) {
$meta_key_query = "meta_key = '$key'";
}
return $wpdb->get_results( "SELECT meta_key, meta_value from $table WHERE $meta_key_query AND eid=$con_id", ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
static function get_merge_tags( $con_id ) {
$result = self::get_meta( $con_id, 'merge_tags' );
if ( empty( $result ) ) {
return array();
}
try {
$merge_tags = json_decode( $result[0]['meta_value'], true );
} catch ( Exception $e ) {
return array();
}
return $merge_tags;
}
static function get_notification_data( $con_id ) {
$result = self::get_meta( $con_id, 'notification_data' );
if ( empty( $result ) ) {
return array();
}
try {
$merge_tags = json_decode( $result[0]['meta_value'], true );
} catch ( Exception $e ) {
return array();
}
return $merge_tags;
}
public static function delete_engagements_meta( $ids ) {
if ( empty( $ids ) ) {
return;
}
global $wpdb;
$table = self::_table();
$placeholders = array_fill( 0, count( $ids ), '%d' );
$placeholders = implode( ', ', $placeholders );
$query = $wpdb->prepare( "DELETE FROM {$table} WHERE eid IN ($placeholders)", $ids );
return $wpdb->query( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}
}

View File

@@ -0,0 +1,19 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Field_Groups' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_Model_Field_Groups extends BWFAN_Model {
static $primary_key = 'ID';
/**
* function to return group using term slug
**/
static function get_group_by_slug( $slug ) {
global $wpdb;
$query = "SELECT * from {table_name} where slug='" . $slug . "'";
$groupdata = self::get_results( $query );
$groupdata = is_array( $groupdata ) && ! empty( $groupdata ) ? $groupdata[0] : array();
return $groupdata;
}
}
}

View File

@@ -0,0 +1,202 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Fields' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_Model_Fields extends BWFAN_Model {
private static $fields_cache_by_slug = [];
static $primary_key = 'ID';
static $cache_query = [];
/**
* function to return field data using term slug
*
* @param $id
*
* @return array|mixed
*/
static function get_field_by_id( $id ) {
global $wpdb;
$query = "SELECT * from {table_name} where ID = $id ";
$fielddata = self::get_results( $query );
$fielddata = is_array( $fielddata ) && ! empty( $fielddata ) ? $fielddata[0] : array();
if ( isset( $fielddata['meta'] ) ) {
$fielddata['meta'] = json_decode( $fielddata['meta'] );
}
return $fielddata;
}
public static function get_group_fields( $group_id = 0, $mode = null, $vmode = null, $searchable = null ) {
global $wpdb;
$query = "SELECT fg.name AS group_name, cf.ID AS field_id, cf.name, cf.slug, cf.type, cf.gid, cf.meta, cf.search, cf.mode, cf.vmode, cf.created_at FROM {table_name} AS cf LEFT JOIN {$wpdb->prefix}bwfan_field_groups AS fg ON cf.gid = fg.ID ";
$query .= " WHERE 1=1";
! empty( $mode ) && $query .= " AND mode = $mode";
! empty( $vmode ) && $query .= " AND vmode = $vmode";
! empty( $searchable ) && $query .= " AND search = $searchable";
$group_id > 0 ? $query .= " AND fg.ID = $group_id" : $query .= " AND (EXISTS (SELECT 1 FROM {$wpdb->prefix}bwfan_field_groups WHERE cf.gid=ID) OR cf.gid=0 )";
$fields = self::get_results( $query );
return $fields;
}
public static function get_custom_fields( $mode = null, $vmode = null, $searchable = null, $viewable = null, $type = null, $limit = 0, $offset = 0 ) {
global $wpdb;
$query = "SELECT * FROM {table_name} WHERE 1=1";
! empty( $mode ) && $query .= $wpdb->prepare( " AND mode = %d", $mode );
! empty( $vmode ) && $query .= $wpdb->prepare( " AND vmode = %d", $vmode );
! empty( $searchable ) && $query .= $wpdb->prepare( " AND search = %d", $searchable );
! empty( $viewable ) && $query .= $wpdb->prepare( " AND view = %d", $viewable );
if ( ! empty( $type ) ) {
$type = ! is_array( $type ) ? explode( ',', $type ) : $type;
$type = array_filter( array_map( 'absint', $type ) );
if ( ! empty( $type ) ) {
$placeholder = implode( ',', array_fill( 0, count( $type ), '%d' ) );
$query .= $wpdb->prepare( " AND type IN ($placeholder)", $type );
}
}
if ( $limit > 0 ) {
$query .= $wpdb->prepare( " LIMIT %d, %d", $offset, $limit );
}
return self::get_results( $query );
}
public static function get_field_by_slug( $slug ) {
return self::get_fields_by_slugs_cache( [ $slug ] );
}
public static function get_fields_by_multiple_slugs( $slugs = [] ) {
$fields = self::get_fields_by_slugs_cache( $slugs );
return $fields;
}
/**
* Get fields by slugs
*
* @param $slugs
* @param string $return_by
*
* @return array
*/
public static function get_fields_by_slugs_cache( $slugs, $return_by = 'slug' ) {
if ( empty( $slugs ) ) {
return [];
}
$fields = self::$fields_cache_by_slug;
if ( 0 === count( $fields ) ) {
global $wpdb;
$table = self::_table();
$query = "SELECT * FROM $table";
$rest_fields = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$fields = [];
foreach ( $rest_fields as $field ) {
if ( ! is_array( $field ) ) {
continue;
}
$fields[ $field['slug'] ] = $field;
}
self::$fields_cache_by_slug = $fields;
}
if ( 1 === count( $slugs ) ) {
return $fields[ $slugs[0] ] ?? '';
}
$return = [];
foreach ( $slugs as $slug ) {
if ( ! isset( $fields[ $slug ] ) || ! is_array( $fields[ $slug ] ) ) {
continue;
}
$key = 'slug' === $return_by ? $slug : $fields[ $slug ]['id'];
$return[ $key ] = $fields[ $slug ];
}
return $return;
}
public static function get_field_type( $id ) {
global $wpdb;
$table = self::_table();
$query = "SELECT `type` FROM $table WHERE `ID` = $id LIMIT 0,1";
if ( isset( self::$cache_query[ md5( $query ) ] ) ) {
return self::$cache_query[ md5( $query ) ];
}
self::$cache_query[ md5( $query ) ] = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return self::$cache_query[ md5( $query ) ];
}
/**
* Get all fields and their slug, name, type details
*
* @return array|object|stdClass[]|null
*/
public static function get_field_types() {
global $wpdb;
$table = self::_table();
$query = "SELECT `ID`, `slug`, `type`, `name` FROM $table LIMIT 0,500";
if ( isset( self::$cache_query[ md5( $query ) ] ) ) {
return self::$cache_query[ md5( $query ) ];
}
self::$cache_query[ md5( $query ) ] = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return self::$cache_query[ md5( $query ) ];
}
/**Get total Count of fields for showing in frontend*/
public static function get_fields_count() {
global $wpdb;
$table = self::_table();
$addressfield = implode( "', '", array_keys( BWFCRM_Fields::$contact_address_fields ) );
$query = "SELECT COUNT(ID) FROM {$table} WHERE 1=1 AND vmode=1 AND slug NOT IN('$addressfield')";
$custom_fields = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$column_field = BWFCRM_Fields::$contact_columns;
if ( isset( $column_field['creation_date'] ) ) {
unset( $column_field['creation_date'] );
}
$fields_count = $custom_fields + count( $column_field ) + count( BWFCRM_Fields::$extra_columns );
return $fields_count;
}
/**
* Get multiple fields
*
* @param $ids
*
* @return array|object|stdClass[]|null
*/
public static function get_multiple_fields( $ids ) {
if ( empty( $ids ) ) {
return [];
}
global $wpdb;
$table = self::_table();
$ids = implode( ',', $ids );
$query = "SELECT * From $table WHERE ID IN ($ids)";
return $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}
}

View File

@@ -0,0 +1,17 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Link_Metrics' ) ) {
class BWFAN_Model_Link_Metrics extends BWFAN_Model {
public static function get_link_metrics( $link_id, $cid ) {
global $wpdb;
$query = " SELECT * FROM {$wpdb->prefix}bwfan_link_metrics WHERE `link_id` = %d AND `contact_id` = %d";
return $wpdb->get_row( $wpdb->prepare( $query, $link_id, $cid ), ARRAY_A );//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL
}
}
}

View File

@@ -0,0 +1,116 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Links' ) ) {
class BWFAN_Model_Links extends BWFAN_Model {
/**
* Check link is existing or not
*
* @param $value
* @param $search_by
* @param $return_col
* @param $type
*
* @return string|null
*/
public static function is_link_exists( $value, $search_by = 'ID', $return_col = '', $type = '' ) {
global $wpdb;
$type_query = '';
$return_col = empty( $return_col ) ? "`l_hash`" : $return_col;
$args = [ $value ];
if ( ! empty( $type ) ) {
$type_query = " AND `type` = %d";
$args[] = $type;
}
$query = "SELECT {$return_col} FROM {$wpdb->prefix}bwfan_links WHERE `{$search_by}` = %s {$type_query}";
return $wpdb->get_var( $wpdb->prepare( $query, $args ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL
}
/**
* Check link hash and cleaned url exists or not
*
* @param string $clean_url
* @param string $l_hash
*
* @return mixed
*/
public static function is_link_hash_exists( $clean_url = '', $l_hash = '' ) {
if ( empty( $l_hash ) || empty( $clean_url ) ) {
return false;
}
global $wpdb;
return $wpdb->get_var( $wpdb->prepare( "SELECT `ID` FROM {$wpdb->prefix}bwfan_links WHERE `l_hash` = %s AND `clean_url` = %s", $l_hash, $clean_url ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL
}
/**
* Check if links exist
*
* @param string $link
* @param array $data
*
* @return string|bool|null
*/
public static function check_if_link_exists( $link = '', $data = [] ) {
if ( empty( $link ) ) {
return false;
}
global $wpdb;
$where = " 1=1 ";
if ( ! empty( $data['clean_url'] ) ) {
$where .= $wpdb->prepare( " AND `clean_url` = %s", esc_sql( $data['clean_url'] ) );
}
if ( ! empty( $data['template_id'] ) ) {
$where .= $wpdb->prepare( " AND `tid` = %d", intval( $data['template_id'] ) );
}
if ( ! empty( $data['type'] ) ) {
$where .= $wpdb->prepare( " AND `type` = %d", intval( $data['type'] ) );
}
if ( ! empty( $data['oid'] ) ) {
$where .= $wpdb->prepare( " AND `oid` = %d", intval( $data['oid'] ) );
}
if ( ! empty( $data['sid'] ) ) {
$where .= $wpdb->prepare( " AND `sid` = %d", intval( $data['sid'] ) );
}
return $wpdb->get_var( "SELECT `l_hash` FROM {$wpdb->prefix}bwfan_links WHERE $where" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL
}
/**
* Get link ID by tid
*
* @param $cleaned_url
* @param $data
*
* @return string|null
*/
public static function get_link_id_by_tid( $cleaned_url, $data ) {
if ( empty( $cleaned_url ) || empty( $data ) ) {
return '';
}
$tid = $data['tid'] ?? 0;
$oid = $data['oid'] ?? 0;
$type = $data['type'] ?? 0;
global $wpdb;
$query = "SELECT `ID` FROM {$wpdb->prefix}bwfan_links WHERE `clean_url` = %s AND `tid` = %d AND `oid` = %d AND `type`=%d ORDER BY `ID` DESC LIMIT 1";
//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL
return $wpdb->get_var( $wpdb->prepare( $query, [
$cleaned_url,
$tid,
$oid,
$type
] ) );
}
}
}

View File

@@ -0,0 +1,56 @@
<?php
class BWFAN_Model_Logmeta extends BWFAN_Model {
static $primary_key = 'ID';
public static function get_rows( $only_query = false, $automation_ids = array() ) {
global $wpdb;
$table_name = self::_table();
if ( $only_query ) {
// For Fetching the meta of automations
$automationCount = count( $automation_ids );
$stringPlaceholders = array_fill( 0, $automationCount, '%s' );
$placeholdersautomation = implode( ', ', $stringPlaceholders );
$sql_query = "SELECT bwfan_log_id, meta_value, meta_key FROM $table_name WHERE bwfan_log_id IN ($placeholdersautomation)";
$sql_query = $wpdb->prepare( $sql_query, $automation_ids ); // WPCS: unprepared SQL OK
}
$result = $wpdb->get_results( $sql_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $result;
}
public static function get_meta( $id, $key ) {
$rows = self::get_log_meta( $id );
$value = '';
if ( count( $rows ) > 0 && isset( $rows[ $key ] ) ) {
$value = $rows[ $key ];
}
return $value;
}
public static function get_log_meta( $task_id ) {
if ( empty( $task_id ) ) {
return [];
}
global $wpdb;
$table = self::_table();
$sql_query = "SELECT * FROM $table WHERE bwfan_log_id =%d";
$sql_query = $wpdb->prepare( $sql_query, $task_id ); // WPCS: unprepared SQL OK
$result = $wpdb->get_results( $sql_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$meta = [];
if ( is_array( $result ) && count( $result ) > 0 ) {
foreach ( $result as $meta_values ) {
$key = $meta_values['meta_key'];
$meta[ $key ] = maybe_unserialize( $meta_values['meta_value'] );
}
}
return $meta;
}
}

View File

@@ -0,0 +1,39 @@
<?php
class BWFAN_Model_Logs extends BWFAN_Model {
static $primary_key = 'ID';
public static function get_logs( $all, $automation_ids, $status ) {
global $wpdb;
$all = absint( $all );
if ( is_array( $automation_ids ) && count( $automation_ids ) > 0 && 1 === $all ) {
$automationCount = count( $automation_ids );
$stringPlaceholders = array_fill( 0, $automationCount, '%s' );
$placeholdersautomation = implode( ', ', $stringPlaceholders );
$sql_query = "Select * FROM {table_name} WHERE automation_id IN ($placeholdersautomation)";
$sql_query = $wpdb->prepare( $sql_query, $automation_ids ); // WPCS: unprepared SQL OK
} elseif ( is_array( $automation_ids ) && count( $automation_ids ) > 0 && 1 === $all && '' !== $status ) {
$automationCount = count( $automation_ids );
$stringPlaceholders = array_fill( 0, $automationCount, '%s' );
$placeholdersautomation = implode( ', ', $stringPlaceholders );
$sql_query = "Select * FROM {table_name} WHERE automation_id IN ($placeholdersautomation) AND status = %d ";
$automation_ids = implode( ',', $automation_ids );
$sql_query = $wpdb->prepare( $sql_query, $automation_ids, $status ); // WPCS: unprepared SQL OK
}
$active_logs = self::get_results( $sql_query );
$final_data = [];
if ( is_array( $active_logs ) && count( $active_logs ) > 0 ) {
foreach ( $active_logs as $log ) {
$log_id = $log['id'];
$log['meta'] = BWFAN_Model_Logmeta::get_log_meta( $log_id );
$final_data[ $log_id ] = $log;
}
}
return $active_logs;
}
}

View File

@@ -0,0 +1,155 @@
<?php
class BWFAN_Model_Message_Unsubscribe extends BWFAN_Model {
static $primary_key = 'ID';
/**
* @param $where
* @param bool $single_row
*
* @return array|object|string|void|null
*/
public static function get_message_unsubscribe_row( $where, $single_row = true ) {
global $wpdb;
if ( empty( $where ) || ! is_array( $where ) ) {
return '';
}
if ( true === $single_row ) {
return $wpdb->get_row( self::prepare_message_unsubscribe_sql( $where ) . ' LIMIT 0, 1', ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
} else {
return $wpdb->get_results( self::prepare_message_unsubscribe_sql( $where ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}
/**
* @param $data
*
* @return string
*
* Function to create query to get data from table
*/
private static function prepare_message_unsubscribe_sql( $data ) {
global $wpdb;
$where = '';
$count = count( $data );
$i = 0;
$table_name = $wpdb->prefix . 'bwfan_message_unsubscribe';
foreach ( $data as $key => $value ) {
$i ++;
if ( 'string' === gettype( $value ) ) {
$where .= '`' . $key . '` = ' . "'" . $value . "'";
} elseif ( is_array( $value ) ) {
$where .= '`' . $key . "` IN ('" . implode( "','", $value ) . "')";
} else {
$where .= '`' . $key . '` = ' . $value;
}
if ( $i < $count ) {
$where .= ' AND ';
}
}
return 'SELECT * FROM ' . $table_name . " WHERE $where";
}
/**
* @param $data
*
* @return array|false|int|void
*
* Delete row from table
*/
public static function delete_message_unsubscribe_row( $data ) {
if ( ! is_array( $data ) || empty( $data ) ) {
return;
}
global $wpdb;
$where = '';
$count = count( $data );
$i = 0;
$table_name = $wpdb->prefix . 'bwfan_message_unsubscribe';
foreach ( $data as $key => $value ) {
$i ++;
if ( 'string' === gettype( $value ) ) {
$where .= '`' . $key . '` = ' . "'" . $value . "'";
} else {
$where .= '`' . $key . '` = ' . $value;
}
if ( $i < $count ) {
$where .= ' AND ';
}
}
return $wpdb->query( 'DELETE FROM ' . $table_name . " WHERE $where" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Delete email or phone row from the unsubscribe table
*
* @param array $emails_or_phones
*
* @return bool
*/
public static function delete_unsubscribers( $emails_or_phones = [] ) {
global $wpdb;
if ( ! is_array( $emails_or_phones ) || empty( $emails_or_phones ) ) {
return false;
}
$placeholders = [];
$where_args = [];
foreach ( $emails_or_phones as $recipient ) {
if ( empty( $recipient ) ) {
continue;
}
$placeholders[] = "%s";
$where_args[] = $recipient;
}
$placeholders = implode( ',', $placeholders );
$table = self::_table();
$sql = $wpdb->prepare( "DELETE FROM $table WHERE `recipient` IN ($placeholders)", $where_args );
$wpdb->query( $sql ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return true;
}
/**
* Add email or phone in the unsubscribe table
* May or may not be contact
*
* @param array $emails_or_phones
* @param int $object_id
* @param int $object_type
* @param false $stop_hooks
*
* @return bool
*/
public static function add_unsubscribers( $emails_or_phones = [], $object_id = 0, $object_type = 3, $stop_hooks = false ) {
$unsubscribers = array_map( function ( $recipient ) use ( $object_id, $object_type ) {
return array(
'recipient' => $recipient,
'mode' => is_email( $recipient ) ? 1 : 2,
'c_date' => current_time( 'mysql', 1 ),
'automation_id' => ! empty( $object_id ) ? absint( $object_id ) : 0,
'c_type' => ! empty( $object_type ) ? absint( $object_type ) : 3
);
}, $emails_or_phones );
BWFAN_Model_Message_Unsubscribe::insert_multiple( $unsubscribers, array( 'recipient', 'mode', 'c_date', 'automation_id', 'c_type' ), [ '%s', '%d', '%s', '%d', '%d' ] );
/** hook when any contact unsubscribed */
if ( false === $stop_hooks ) {
do_action( 'bwfcrm_after_contact_unsubscribed', $unsubscribers );
}
return true;
}
}

View File

@@ -0,0 +1,65 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Message' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_Model_Message extends BWFAN_Model {
static $primary_key = 'ID';
public static function get_messages( $args = array() ) {
global $wpdb;
$table = self::_table();
$sql = "SELECT * FROM {$table}";
if ( ! is_array( $args ) || empty( $args ) ) {
return $wpdb->get_results( $sql, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
$where_sql = ' WHERE 1=1';
/** Get by Track id */
if ( isset( $args['track_id'] ) && ! empty( $args['track_id'] ) ) {
$where_sql .= " AND track_id = '{$args['track_id']}'";
}
/** Get by Subject */
if ( isset( $args['sub'] ) && ! empty( $args['sub'] ) ) {
$where_sql .= " AND sub = {$args['sub']}";
}
/** Get by Body */
if ( isset( $args['body'] ) && ! empty( $args['body'] ) ) {
$where_sql .= " AND body = {$args['body']}";
}
/** Set Pagination */
$pagination_sql = '';
$limit = isset( $args['limit'] ) ? absint( $args['limit'] ) : 0;
$offset = isset( $args['offset'] ) ? absint( $args['offset'] ) : 0;
if ( ! empty( $limit ) || ! empty( $offset ) ) {
$pagination_sql = " limit $offset, $limit";
}
$sql = $sql . $where_sql . $pagination_sql;
// $total_sql = "SELECT count(*) FROM {$table}" . $where_sql;
// $grab_totals = isset( $args['grab_totals'] ) && ! empty( absint( $args['grab_totals'] ) );
return $wpdb->get_results( $sql, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
public static function get_message_by_track_id( $track_id ) {
global $wpdb;
$table = self::_table();
if ( is_array( $track_id ) ) {
$placeholders = array_fill( 0, count( $track_id ), '%d' );
$placeholders = implode( ', ', $placeholders );
$sql = "SELECT ID,sub as subject, body as template, data, track_id FROM {$table} WHERE track_id IN ($placeholders)";
return $wpdb->get_results( $wpdb->prepare( $sql, $track_id ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
$sql = "SELECT ID,sub as subject, body as template, data FROM {$table} WHERE track_id = %d LIMIT 0, 1";
return $wpdb->get_row( $wpdb->prepare( $sql, $track_id ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
}
}

View File

@@ -0,0 +1,5 @@
<?php
class BWFAN_Model_Settings extends BWFAN_Model {
static $primary_key = 'ID';
}

View File

@@ -0,0 +1,37 @@
<?php
class BWFAN_Model_Taskmeta extends BWFAN_Model {
static $primary_key = 'ID';
public static function get_meta( $id, $key ) {
$rows = self::get_task_meta( $id );
$value = '';
if ( count( $rows ) > 0 && isset( $rows[ $key ] ) ) {
$value = $rows[ $key ];
}
return $value;
}
public static function get_task_meta( $task_id ) {
if ( empty( $task_id ) ) {
return [];
}
global $wpdb;
$table = self::_table();
$sql_query = "SELECT * FROM $table WHERE bwfan_task_id =%d";
$sql_query = $wpdb->prepare( $sql_query, $task_id ); // WPCS: unprepared SQL OK
$result = $wpdb->get_results( $sql_query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$meta = [];
if ( is_array( $result ) && count( $result ) > 0 ) {
foreach ( $result as $meta_values ) {
$key = $meta_values['meta_key'];
$meta[ $key ] = maybe_unserialize( $meta_values['meta_value'] );
}
}
return $meta;
}
}

View File

@@ -0,0 +1,60 @@
<?php
class BWFAN_Model_Tasks extends BWFAN_Model {
static $primary_key = 'ID';
public static function get_tasks( $automation_ids ) {
global $wpdb;
if ( ! is_array( $automation_ids ) || count( $automation_ids ) === 0 ) {
return array();
}
$automationCount = count( $automation_ids );
$stringPlaceholders = array_fill( 0, $automationCount, '%s' );
$placeholders_automation = implode( ', ', $stringPlaceholders );
$sql_query = "Select * FROM {table_name} WHERE automation_id IN ($placeholders_automation)";
$sql_query = $wpdb->prepare( $sql_query, $automation_ids ); // WPCS: unprepared SQL OK
$active_tasks = self::get_results( $sql_query );
return $active_tasks;
}
/**
* Return Task detail with its meta details
*
* @param $task_id
*
* @return array|object|void|null
*/
public static function get_task_with_data( $task_id ) {
$task = self::get( $task_id );
if ( ! is_array( $task ) || empty( $task ) ) {
return [];
}
$task['meta'] = BWFAN_Model_Taskmeta::get_task_meta( $task_id );
return $task;
}
/**
* Check if any task is available for execution.
*
* @return bool
*/
public static function maybe_tasks_available() {
global $wpdb;
$time = current_time( 'timestamp', 1 );
$table = self::_table();
$query = $wpdb->prepare( "SELECT MAX(`ID`) FROM {$table} WHERE `e_date` < %s AND `status` = 0", $time );
$count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $count ) ) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,403 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Templates' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_Model_Templates extends BWFAN_Model {
static $primary_key = 'ID';
/**
* Get templates from db
*
* @param $offset
* @param $limit
* @param $search
* @param $id
* @param $get_template
* @param $mode
*
* @return array|object|stdClass[]
*/
public static function bwfan_get_templates( $offset, $limit, $search, $id, $get_template = true, $mode = '' ) {
global $wpdb;
$column = '*';
if ( ! $get_template ) {
$column = 'ID, title, mode, created_at, updated_at';
}
$query = "SELECT $column FROM {table_name} WHERE 1=1 AND type = 1 AND canned = 1";
if ( ! empty( $id ) ) {
$id = array_filter( array_map( 'intval', $id ) );
if ( ! empty( $id ) ) {
$placeholders = array_fill( 0, count( $id ), '%d' );
$query .= $wpdb->prepare( " AND ID in ( " . implode( ',', $placeholders ) . " )", $id );
}
}
if ( ! empty( $mode ) ) {
$query .= $wpdb->prepare( " AND mode = %d", $mode );
}
if ( ! empty( $search ) ) {
$query .= $wpdb->prepare( " AND title LIKE %s", "%" . esc_sql( $search ) . "%" );
}
$query .= ' ORDER BY updated_at DESC';
if ( intval( $limit ) > 0 ) {
$offset = ! empty( $offset ) ? intval( $offset ) : 0;
$query .= $wpdb->prepare( " LIMIT %d, %d", $offset, $limit );
}
$result = self::get_results( $query );
$result = is_array( $result ) && ! empty( $result ) ? $result : array();
return $result;
}
/**
* Get layouts from db
*
* @param $offset
* @param $limit
* @param $search
* @param $id
* @param bool $get_template
*
* @return array|object
*/
public static function bwfan_get_layouts( $offset, $limit, $search, $id ) {
global $wpdb;
$query = "SELECT * FROM {table_name} WHERE 1=1 AND type = 1 AND canned = 0 AND mode = 6";
if ( ! empty( $id ) ) {
$id = array_filter( array_map( 'intval', $id ) );
if ( ! empty( $id ) ) {
$placeholders = array_fill( 0, count( $id ), '%d' );
$query .= $wpdb->prepare( " AND ID in ( " . implode( ',', $placeholders ) . " )", $id );
}
}
if ( ! empty( $search ) ) {
$query .= $wpdb->prepare( " AND title LIKE %s", "%" . esc_sql( $search ) . "%" );
}
$query .= ' ORDER BY updated_at DESC';
if ( intval( $limit ) > 0 ) {
$offset = ! empty( $offset ) ? intval( $offset ) : 0;
$query .= $wpdb->prepare( " LIMIT %d, %d", $offset, $limit );
}
$result = self::get_results( $query );
$result = is_array( $result ) && ! empty( $result ) ? $result : array();
return $result;
}
/**
* Get templates count from db
*
* @param $search
* @param $id
* @param $mode
*
* @return int
*/
public static function bwfan_get_templates_count( $search = '', $id = [], $mode = 0 ) {
global $wpdb;
$table = $wpdb->prefix . 'bwfan_templates';
$query = 'SELECT count(ID) FROM ' . $table . ' WHERE 1=1 AND type = 1 AND canned = 1';
if ( ! empty( $id ) ) {
$id = array_filter( array_map( 'intval', $id ) );
if ( ! empty( $id ) ) {
$placeholders = array_fill( 0, count( $id ), '%d' );
$query .= $wpdb->prepare( " AND ID in ( " . implode( ',', $placeholders ) . " )", $id );
}
}
if ( ! empty( $mode ) ) {
$query .= $wpdb->prepare( " AND mode = %d", $mode );
}
if ( ! empty( $search ) ) {
$query .= $wpdb->prepare( " AND title LIKE %s", "%" . esc_sql( $search ) . "%" );
}
$result = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $result ? intval( $result ) : 0;
}
/**
* Get layout count from db
*
* @param $search
* @param $id
*
* @return int
*/
public static function bwfan_get_layouts_count( $search, $id ) {
global $wpdb;
$table = $wpdb->prefix . 'bwfan_templates';
$query = 'SELECT count(ID) FROM ' . $table . ' WHERE 1=1 AND type = 1 AND canned = 0 AND mode=6';
if ( ! empty( $id ) ) {
$id = array_filter( array_map( 'intval', $id ) );
if ( ! empty( $id ) ) {
$placeholders = array_fill( 0, count( $id ), '%d' );
$query .= $wpdb->prepare( " AND ID in ( " . implode( ',', $placeholders ) . " )", $id );
}
}
if ( ! empty( $search ) ) {
$query .= $wpdb->prepare( " AND title LIKE %s", "%" . esc_sql( $search ) . "%" );
}
$result = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $result ? intval( $result ) : 0;
}
/**
* Check if template already exists
*
* @param $field
* @param $data
*
* @return int
*/
public static function bwfan_check_template_exists( $field, $data ) {
global $wpdb;
$query = 'SELECT COUNT(ID) FROM ' . self::_table();
$string_with_dash = "$data - %";
$query .= $wpdb->prepare( " WHERE ( {$field} = %s OR {$field} LIKE %s ) AND canned = %d LIMIT 0,1", $data, esc_sql( $string_with_dash ), 1 );
$result = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $result;
}
/**
* Check if layout already exists
*
* @param $field
* @param $data
*
* @return int
*/
public static function bwfan_check_layout_exists( $field, $data ) {
global $wpdb;
$query = 'SELECT COUNT(ID) FROM ' . self::_table();
$string_with_dash = "$data - %";
$query .= $wpdb->prepare( " WHERE ( {$field} = %s OR {$field} LIKE %s ) AND canned = %d LIMIT 0,1", $data, esc_sql( $string_with_dash ), 0 );
$result = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $result;
}
/**
* Insert new template to db
*
* @param $data
*
* @return int|void
*/
public static function bwfan_create_new_template( $data ) {
if ( empty( $data ) ) {
return;
}
self::insert( $data );
$id = absint( self::insert_id() );
return $id;
}
/**
* Delete template
*
* @param $id
*
* @return bool
*/
public static function bwf_delete_template( $id ) {
if ( empty( $id ) ) {
return false;
}
global $wpdb;
$template_data = [
'canned' => 0,
];
$table_name = self::_table();
if ( is_array( $id ) ) {
/**Update multiple rows */
$id = array_filter( array_map( 'intval', $id ) );
if ( ! empty( $id ) ) {
$placeholders = array_fill( 0, count( $id ), '%d' );
$query = $wpdb->prepare( "UPDATE $table_name SET `canned` = 0 WHERE ID in ( " . implode( ',', $placeholders ) . " )", $id );
self::update_multiple( $query );
}
} else {
$delete_template = self::update( $template_data, array(
'id' => absint( $id ),
) );
if ( false === $delete_template ) {
return false;
}
}
return true;
}
/**
* Delete layout
*
* @param $id
*
* @return bool
*/
public static function bwf_delete_layout( $id ) {
global $wpdb;
$table_name = self::_table();
$delete_layout = $wpdb->delete( $table_name, array( 'ID' => $id ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( false === $delete_layout ) {
return false;
}
return true;
}
/**
* Fetch template by id
*
* @param $id
* @param $canned
*
* @return array|mixed|stdClass
*/
public static function bwfan_get_template( $id, $canned = 1 ) {
global $wpdb;
$query = $wpdb->prepare( 'SELECT * FROM {table_name} WHERE type = 1 AND canned = %d AND ID=%d', $canned, $id );
$result = self::get_results( $query );
return is_array( $result ) && ! empty( $result ) ? $result[0] : array();
}
/**
* Update Template data by id
*
* @param $id
* @param $data
*
* @return bool
*/
public static function bwfan_update_template( $id, $data ) {
if ( ! is_array( $data ) ) {
return false;
}
return ! ! self::update( $data, array(
'id' => absint( $id ),
) );
}
/**
* Return template id
*/
public static function get_first_template_id() {
global $wpdb;
$table = "{$wpdb->prefix}bwfan_templates";
$query = " SELECT MIN(`id`) FROM $table WHERE `type` = 1 AND `canned` = 1 ";
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Clone given template ID
*
* @param $template_id
*
* @return array
*/
public static function clone_template( $template_id ) {
$status = 404;
$message = __( 'Unable to find template with the given id.', 'wp-marketing-automations' );
$template = self::get_specific_rows( 'id', $template_id );
if ( ! empty( $template ) ) {
$create_time = current_time( 'mysql', 1 );
$template = $template[0];
unset( $template['ID'] );
$template['title'] = $template['title'] . ' ( Copy )';
$template['created_at'] = $create_time;
$template['updated_at'] = $create_time;
self::insert( $template );
$new_template_id = self::insert_id();
if ( $new_template_id ) {
$status = 200;
$message = __( 'Template cloned', 'wp-marketing-automations' );
}
}
return array(
'status' => $status,
'message' => $message,
);
}
/**
* Get templates by ids
*
* @param $tids
* @param array $columns
*
* @return array
*/
public static function get_templates_by_ids( $tids, $columns = [] ) {
global $wpdb;
if ( empty( $tids ) ) {
return [];
}
if ( empty( $columns ) ) {
$columns = [ 'ID', 'subject', 'template', 'type' ];
}
$placeholders = array_fill( 0, count( $tids ), '%d' );
$placeholders = implode( ', ', $placeholders );
$query = "SELECT " . implode( ', ', $columns ) . " FROM {$wpdb->prefix}bwfan_templates WHERE `ID` IN( $placeholders )";
$result = $wpdb->get_results( $wpdb->prepare( $query, $tids ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL
$data = [];
foreach ( $result as $template ) {
$data[ $template['ID'] ] = $template;
}
return $data;
}
/**
* Get templates
*
* @param $offset
*
* @return array|object|stdClass[]|null
*/
public static function get_templates( $offset = '' ) {
global $wpdb;
$where = '';
$args = [];
if ( ! empty( $offset ) ) {
$where = " WHERE `id` > %d ";
$args = [ intval( $offset ) ];
}
$query = "SELECT * FROM {$wpdb->prefix}bwfan_templates $where";
return $wpdb->get_results( $wpdb->prepare( $query, $args ), ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL
}
}
}

View File

@@ -0,0 +1,412 @@
<?php
if ( ! class_exists( 'BWFAN_Model_Terms' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_Model_Terms extends BWFAN_Model {
static $primary_key = 'ID';
/** @var array $terms_cache */
private static $terms_cache = [];
private static $query_cache = [];
/**
* function to return term_data using term slug
**/
static function get_term_by_name( $term_slug, $type = 1 ) {
$query = "SELECT * FROM `{table_name}` WHERE `name` LIKE '" . esc_sql( $term_slug ) . "' AND `type` = '" . $type . "'";
$query_md5 = md5( $query );
if ( isset( self::$query_cache[ $query_md5 ] ) && ! empty( self::$query_cache[ $query_md5 ] ) ) {
$term_data = self::$query_cache[ $query_md5 ];
} else {
$term_data = self::get_results( $query );
self::$query_cache[ $query_md5 ] = $term_data;
}
return is_array( $term_data ) && ! empty( $term_data ) ? $term_data[0] : array();
}
/**
* function to return term_data using term slug
**/
static function search_term( $term_slug, $type = 1 ) {
$query = "SELECT * FROM `{table_name}` WHERE `name` LIKE '%" . esc_sql( $term_slug ) . "%' AND `type` = '" . $type . "'";
$term_data = self::get_results( $query );
return is_array( $term_data ) && ! empty( $term_data ) ? $term_data : array();
}
/**
* @param int $type
* @param int $offset
* @param int $limit
* @param string $search
* @param array $ids
* @param string $search_nature
* @param bool $use_cache
*
* @return array|object
*/
static function get_terms( $type = 1, $offset = 0, $limit = 10, $search = '', $ids = array(), $search_nature = '', $use_cache = false ) {
$found_terms = [];
/** Cache Implementation: BEGIN */
/**
* Retrieving terms from cache is only possible when there is Exact names of terms to be searched.
* OR, we have IDs to retrieve
* Means either Term Name or ID is needed to retrieve from cache.
*
* Also don't use cache in case we have pagination parameters,
* which makes it difficult to populate terms from both cache & DB for a particular page
*/
$eligible_for_cache = ( 'exact' === $search_nature && ! empty( $search ) ) || ( ! empty( $ids ) && is_array( $ids ) );
if ( $use_cache && $eligible_for_cache ) {
$offset = 0;
$limit = 1000;
$result = self::_get_terms_from_cache( $search, $search_nature, $ids, $type );
if ( empty( $result['ids_not_found'] ) && empty( $result['search_not_found'] ) ) {
return $result['found_terms'];
}
$search = $result['search_not_found'];
$ids = $result['ids_not_found'];
$found_terms = $result['found_terms'];
}
/** Cache Implementation: END */
global $wpdb;
$search_terms = '';
$limit_query = '';
$order = ' ORDER BY `ID` DESC';
if ( ! empty( $search ) ) {
if ( ! is_array( $search ) ) {
$search = BWFAN_Common::get_formatted_value_for_dbquery( $search );
$search_terms = ( 'exact' === $search_nature ? " AND name = '$search'" : "AND name LIKE '%" . esc_sql( $search ) . "%'" );
} else if ( 'exact' === $search_nature ) {
$search = array_map( function ( $value ) {
return BWFAN_Common::get_formatted_value_for_dbquery( $value );
}, $search );
$search = implode( "','", $search );
$search_terms = " AND name IN ('$search')";
} else {
$search_terms = array_map( function ( $s_term ) {
$s_term = BWFAN_Common::get_formatted_value_for_dbquery( $s_term );
return "name LIKE '%" . esc_sql( $s_term ) . "%'";
}, $search );
$search_terms = implode( ' OR ', $search_terms );
$search_terms = " AND ($search_terms)";
}
$order = ' ORDER BY `name` ASC';
}
if ( ! empty( $ids ) ) {
$search_terms .= " AND ID IN(" . implode( ',', $ids ) . ")";
}
if ( ! empty( $limit ) ) {
$offset = ! empty( $offset ) ? $offset : 0;
$limit_query = " LIMIT $offset,$limit";
}
$type_query = empty( $type ) ? '' : " AND type='$type'";
$query = "SELECT * FROM `{$wpdb->prefix}bwfan_terms` WHERE 1=1 $type_query $search_terms $order $limit_query";
$term_data = self::get_results( $query );
$term_data = is_array( $term_data ) && ! empty( $term_data ) ? $term_data : array();
/** Store the retrieved terms into the cache */
if ( $use_cache && $eligible_for_cache ) {
self::$terms_cache = array_merge( self::$terms_cache, $term_data );
return array_merge( $found_terms, $term_data );
}
return $term_data;
}
private static function _get_terms_from_cache( $search, $search_nature, $ids, $type ) {
$found_terms = [];
$search_not_found = [];
$ids_not_found = [];
if ( empty( $ids ) && ( 'exact' !== $search_nature || empty( $search ) ) ) {
return [
'found_terms' => $found_terms,
'search_not_found' => $search_not_found,
'ids_not_found' => $ids_not_found
];
}
/**
* In search with exact words (not wildcard words eg %s%), we can get terms by term name.
*/
if ( 'exact' === $search_nature && ! empty( $search ) ) {
if ( ! is_array( $search ) ) {
$search = [ $search ];
}
foreach ( $search as $index => $s_term ) {
$found_term = false;
foreach ( self::$terms_cache as $c_term ) {
if ( ! is_array( $c_term ) || ! isset( $c_term['name'] ) || $s_term !== $c_term['name'] || $c_term['type'] !== $type ) {
continue;
}
$found_term = $c_term;
break;
}
if ( false === $found_term || ! is_array( $found_term ) ) {
$search_not_found[] = $s_term;
} else {
$found_terms[] = $found_term;
}
}
}
/**
* In case we have ids for terms to retrieve, we can get terms by term IDs from cache array.
*/
if ( ! empty( $ids ) && is_array( $ids ) ) {
foreach ( $ids as $term_id ) {
$found_term = false;
foreach ( self::$terms_cache as $c_term ) {
if ( ! is_array( $c_term ) || ! isset( $c_term['name'] ) || absint( $term_id ) !== absint( $c_term['ID'] ) ) {
continue;
}
$found_term = $c_term;
break;
}
if ( false === $found_term || ! is_array( $found_term ) ) {
$ids_not_found[] = absint( $term_id );
} else {
$found_terms[] = $found_term;
}
}
}
return [
'found_terms' => $found_terms,
'search_not_found' => $search_not_found,
'ids_not_found' => $ids_not_found
];
}
public static function checking_term( $term_id, $type = 1 ) {
$query = "SELECT ID from {table_name} where ID='" . $term_id . "' and type='" . $type . "'";
$term_result = self::get_results( $query );
return $term_result;
}
public static function get_all( $type = 1 ) {
$query = "select ID, name from {table_name} where type='" . $type . "'";
$term_results = self::get_results( $query );
return $term_results;
}
/**
* @param $id
*
* @return bool
*/
public static function delete_term( $id ) {
global $wpdb;
$term_table = $wpdb->prefix . 'bwfan_terms';
$delete_term = $wpdb->delete( $term_table, array( 'ID' => $id ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( false === $delete_term ) {
return false;
}
return true;
}
static function insert_multiple( $values, $keys, $format = [] ) {
if ( ( ! is_array( $keys ) || empty( $keys ) ) || ( ! is_array( $values ) || empty( $values ) ) ) {
return false;
}
global $wpdb;
$values = array_map( function ( $value ) use ( $keys ) {
$return = array();
foreach ( $keys as $key ) {
if ( is_numeric( $value[ $key ] ) && 'name' !== $key ) {
$return[] = absint( $value[ $key ] );
}
if ( is_string( $value[ $key ] ) ) {
$formatted_value = BWFAN_Common::get_formatted_value_for_dbquery( $value[ $key ] );
$return[] = "'" . $formatted_value . "'";
}
}
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
}
/**
* @param int $type
* @param string $search
* @param array $ids
* @param string $search_nature
*
* @return int
*/
static function get_terms_count( $type = 1, $search = '', $ids = array(), $search_nature = '' ) {
global $wpdb;
$search_terms = '';
if ( ! empty( $search ) ) {
if ( ! is_array( $search ) ) {
$search_terms = ( 'exact' === $search_nature ? " AND name = '$search'" : "AND name LIKE '%" . esc_sql( $search ) . "%'" );
$search_terms = 'name_with_dash' === $search_nature ? " AND ( name = '$search' OR name LIKE '" . esc_sql( $search ) . " - %' ) " : $search_terms;
} else if ( 'exact' === $search_nature ) {
$search = implode( "','", $search );
$search_terms = " AND name IN ('$search')";
} else {
$search_terms = array_map( function ( $s_term ) {
return "name LIKE '%" . esc_sql( $s_term ) . "%'";
}, $search );
$search_terms = implode( ' OR ', $search_terms );
$search_terms = " AND ($search_terms)";
}
}
if ( ! empty( $ids ) ) {
$search_terms .= " AND ID IN(" . implode( ',', $ids ) . ")";
}
$query = "SELECT COUNT(ID) from {$wpdb->prefix}bwfan_terms where type='$type' $search_terms ";
return $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Get term ids
* Create terms if 0 id passed
*
* @param $terms
* @param $type
*
* @return array
*/
public static function get_crm_term_ids( $terms = [], $type = 1 ) {
if ( empty( $terms ) || ! in_array( $type, [ BWFCRM_Term_Type::$TAG, BWFCRM_Term_Type::$LIST ], true ) ) {
return [];
}
foreach ( $terms as $k => $term ) {
if ( ! is_array( $term ) || 0 < intval( $term['id'] ) ) {
continue;
}
$name = ( isset( $term['name'] ) && ! empty( $term['name'] ) ) ? $term['name'] : '';
$name = ( empty( $name ) && isset( $term['value'] ) && ! empty( $term['value'] ) ) ? $term['value'] : $name;
if ( empty( $name ) ) {
continue;
}
/** check if term exists */
$term_row = self::get_term_by_name( $name, $type );
if ( ! empty( $term_row ) ) {
/** term found */
$terms[ $k ]['id'] = $term_row['ID'];
continue;
}
/** create term */
$id = self::create_single_term( $name, $type );
if ( ! empty( $id ) ) {
$terms[ $k ]['id'] = $id;
}
}
$ids = array_column( $terms, 'id' );
$ids = array_unique( $ids );
sort( $ids );
return $ids;
}
/**
* Get term objects
*
* @param $terms
*
* @return array
*/
public static function get_term_objects( $terms = [] ) {
if ( empty( $terms ) ) {
return [];
}
$term_obj = array_map( function ( $term_id ) {
$db_row = BWFAN_Model_Terms::get( $term_id );
if ( ! is_array( $db_row ) ) {
return false;
}
return ( BWFCRM_Term_Type::$TAG === absint( $db_row['type'] ) ? ( new BWFCRM_Tag( $db_row ) ) : ( new BWFCRM_Lists( $db_row ) ) );
}, $terms );
return array_filter( $term_obj );
}
/**
* Create a term
*
* @param $term_name
* @param $type
*
* @return false|int term id
*/
public static function create_single_term( $term_name, $type ) {
global $wpdb;
$data = array(
'name' => $term_name,
'type' => $type,
'created_at' => current_time( 'mysql', 1 ),
);
self::insert( $data );
if ( empty( $wpdb->last_error ) ) {
return $wpdb->insert_id;
}
return false;
}
/**
* Return first term id by type
*
* @param $type
*
* @return int|mixed
*/
public static function get_first_term_by_type( $type = 1 ) {
$query = "SELECT ID FROM `{table_name}` WHERE `type` = '" . $type . "' ORDER BY `ID` ASC LIMIT 1";
$term_data = self::get_results( $query );
return is_array( $term_data ) && isset( $term_data[0] ) && ! empty( $term_data ) && isset( $term_data[0]['ID'] ) ? $term_data[0]['ID'] : 0;
}
}
}

View File

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

View File

@@ -0,0 +1,82 @@
<?php
abstract class BWFAN_DB_Tables_Base {
public $table_name = '';
public $db_errors = '';
public $max_index_length = 191;
public $collation = null;
/**
* Checking table exists or not
*
* @return bool
*/
public function is_exists() {
global $wpdb;
return ! empty( $wpdb->query( "SHOW TABLES LIKE '{$wpdb->prefix}$this->table_name'" ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
/**
* Check missing columns and return missing ones only
*
* @return array
*/
public function check_missing_columns() {
global $wpdb;
/** Get defined columns */
$columns = $this->get_columns();
/** Get columns from db */
$db_columns = $wpdb->get_results( "DESCRIBE {$wpdb->prefix}$this->table_name", ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$result = array_diff( $columns, array_column( $db_columns, 'Field' ) );
sort( $result );
return $result;
}
public function get_columns() {
return [];
}
/**
* Create table
*
* @return void
*/
public function create_table() {
global $wpdb;
$sql = $this->get_create_table_query();
if ( empty( $sql ) ) {
return;
}
dbDelta( $sql );
if ( ! empty( $wpdb->last_error ) ) {
$this->db_errors = $this->table_name . ' create table method triggered an error - ' . $wpdb->last_error;
}
}
public function get_create_table_query() {
return '';
}
public function get_collation() {
if ( ! is_null( $this->collation ) ) {
return $this->collation;
}
global $wpdb;
$collate = '';
if ( $wpdb->has_cap( 'collation' ) ) {
$collate = $wpdb->get_charset_collate();
}
$this->collation = $collate;
return $collate;
}
}

View File

@@ -0,0 +1,74 @@
<?php
class BWFAN_DB_Table_AbandonedCarts extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_abandonedcarts';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"email",
"status",
"user_id",
"last_modified",
"created_time",
"items",
"coupons",
"fees",
"shipping_tax_total",
"shipping_total",
"total",
"total_base",
"token",
"currency",
"cookie_key",
"checkout_data",
"order_id",
"checkout_page_id",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`email` varchar(100) NOT NULL,
`status` int(1) NOT NULL default 0,
`user_id` bigint(20) NOT NULL default 0,
`last_modified` datetime NOT NULL,
`created_time` datetime NOT NULL,
`items` longtext,
`coupons` longtext,
`fees` longtext,
`shipping_tax_total` varchar(32),
`shipping_total` varchar(32),
`total` varchar(32),
`total_base` varchar(32),
`token` varchar(32) NOT NULL,
`currency` varchar(8) NOT NULL,
`cookie_key` varchar(32) NOT NULL,
`checkout_data` longtext,
`order_id` bigint(20) NOT NULL,
`checkout_page_id` bigint(20) NOT NULL,
PRIMARY KEY (`ID`),
KEY `ID` (`ID`),
KEY `status` (`status`),
KEY `user_id` (`user_id`),
KEY `email` (`email`),
KEY `last_modified` (`last_modified`),
KEY `token` (`token`),
KEY `cookie_key` (`cookie_key`)
) $collate;";
}
}

View File

@@ -0,0 +1,50 @@
<?php
class BWFAN_DB_Table_Automation_Complete_Contact extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_automation_complete_contact';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"cid",
"aid",
"event",
"s_date",
"c_date",
"data",
"trail",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`cid` bigint(20) unsigned NOT NULL,
`aid` bigint(10) unsigned NOT NULL,
`event` varchar(120) NOT NULL,
`s_date` datetime NOT NULL default '0000-00-00 00:00:00' COMMENT 'Start Date',
`c_date` datetime NOT NULL default '0000-00-00 00:00:00' COMMENT 'Completion Date',
`data` longtext,
`trail` varchar(40) NULL COMMENT 'Trail ID',
PRIMARY KEY (`ID`),
KEY `ID` (`ID`),
KEY `cid` (`cid`),
KEY `aid` (`aid`),
KEY `c_date` (`c_date`),
UNIQUE KEY `trail` (`trail`)
) $collate;";
}
}

View File

@@ -0,0 +1,33 @@
<?php
class BWFAN_DB_Table_Automation_Contact_Claim extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_automation_contact_claim';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"created_at",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) UNSIGNED NOT NULL auto_increment,
`created_at` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`ID`)
) $collate;";
}
}

View File

@@ -0,0 +1,50 @@
<?php
class BWFAN_DB_Table_Automation_Contact_Trail extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_automation_contact_trail';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"tid",
"cid",
"aid",
"sid",
"c_time",
"status",
"data",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) UNSIGNED NOT NULL auto_increment,
`tid` varchar(40) NOT NULL COMMENT 'Trail ID',
`cid` bigint(12) UNSIGNED NOT NULL COMMENT 'Contact ID',
`aid` bigint(10) UNSIGNED NOT NULL COMMENT 'Automation ID',
`sid` bigint(10) UNSIGNED NOT NULL COMMENT 'Step ID',
`c_time` bigint(12) UNSIGNED NOT NULL,
`status` tinyint(1) UNSIGNED NOT NULL DEFAULT 1 COMMENT '1 - Success | 2 - Wait | 3 - Failed | 4 - Skipped',
`data` varchar(255) NULL,
PRIMARY KEY (`ID`),
KEY `ID` (`ID`),
KEY `tid` (`tid`(40)),
KEY `cid` (`cid`),
KEY `sid` (`sid`),
KEY `status` (`status`)
) $collate;";
}
}

View File

@@ -0,0 +1,62 @@
<?php
class BWFAN_DB_Table_Automation_Contact extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_automation_contact';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"cid",
"aid",
"event",
"c_date",
"e_time",
"status",
"last",
"last_time",
"data",
"claim_id",
"attempts",
"trail",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`cid` bigint(20) unsigned NOT NULL,
`aid` bigint(10) unsigned NOT NULL,
`event` varchar(120) NOT NULL,
`c_date` datetime NOT NULL default '0000-00-00 00:00:00' COMMENT 'Start Date',
`e_time` bigint(12) unsigned NOT NULL,
`status` tinyint(1) UNSIGNED NOT NULL default 1 COMMENT '1 - Active | 2 - Failed | 3 - Paused | 4 - Waiting | 5 - Terminate | 6 - Retry',
`last` bigint(10) UNSIGNED NOT NULL default 0,
`last_time` bigint(12) UNSIGNED NOT NULL,
`data` longtext,
`claim_id` bigint(20) UNSIGNED NOT NULL default 0,
`attempts` tinyint(1) UNSIGNED NOT NULL default 0,
`trail` varchar(40) NULL COMMENT 'Trail ID',
PRIMARY KEY (`ID`),
KEY `ID` (`ID`),
KEY `cid` (`cid`),
KEY `aid` (`aid`),
KEY `e_time` (`e_time`),
KEY `status` (`status`),
KEY `claim_id` (`claim_id`),
UNIQUE KEY `trail` (`trail`)
) $collate;";
}
}

View File

@@ -0,0 +1,39 @@
<?php
class BWFAN_DB_Table_Automation_Events extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_automation_events';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"creation_time",
"execution_time",
"args",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`creation_time` datetime NOT NULL,
`execution_time` bigint(12) unsigned NOT NULL,
`args` longtext,
PRIMARY KEY (`ID`),
KEY `ID` (`ID`),
KEY `execution_time` (`execution_time`)
) $collate;";
}
}

View File

@@ -0,0 +1,47 @@
<?php
class BWFAN_DB_Table_Automation_Step extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_automation_step';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"aid",
"type",
"action",
"status",
"data",
"created_at",
"updated_at",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(10) UNSIGNED NOT NULL auto_increment,
`aid` bigint(10) UNSIGNED NOT NULL ,
`type` tinyint(1) UNSIGNED NOT NULL default 1 COMMENT '1 - Wait | 2 - Action | 3 - Goal | 4 - Conditional | 5 - Exit',
`action` varchar(255) NULL,
`status` tinyint(1) NOT NULL default 0 COMMENT '1 - Active | 2 - Draft | 3 - Deleted',
`data` longtext,
`created_at` datetime NOT NULL default '0000-00-00 00:00:00',
`updated_at` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`ID`),
KEY `aid` (`aid`),
KEY `type` (`type`)
) $collate;";
}
}

View File

@@ -0,0 +1,39 @@
<?php
class BWFAN_DB_Table_Automationmeta extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_automationmeta';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"bwfan_automation_id",
"meta_key",
"meta_value",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`bwfan_automation_id` bigint(20) unsigned NOT NULL default '0',
`meta_key` varchar(255) NULL,
`meta_value` longtext,
PRIMARY KEY (`ID`),
KEY `bwfan_automation_id` (`bwfan_automation_id`),
KEY `meta_key` (`meta_key`($this->max_index_length))
) $collate;";
}
}

View File

@@ -0,0 +1,49 @@
<?php
class BWFAN_DB_Table_Automations extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_automations';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"source",
"event",
"status",
"priority",
"start",
"v",
"benchmark",
"title",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`source` varchar(60) NOT NULL,
`event` varchar(60) NOT NULL,
`status` tinyint(1) NOT NULL default 0 COMMENT '1 - Active 2 - Inactive',
`priority` tinyint(3) NOT NULL default 0,
`start` bigint(10) UNSIGNED NOT NULL,
`v` tinyint(1) UNSIGNED NOT NULL default 1,
`benchmark` longtext,
`title` varchar(255) NULL,
PRIMARY KEY (`ID`),
KEY `ID` (`ID`),
KEY `status` (`status`)
) $collate;";
}
}

View File

@@ -0,0 +1,39 @@
<?php
class BWFAN_DB_Table_Contact_Automations extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_contact_automations';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"contact_id",
"automation_id",
"time",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`contact_id` bigint(20) NOT NULL,
`automation_id` bigint(20) NOT NULL,
`time` bigint(12) NOT NULL,
PRIMARY KEY (`ID`),
KEY `contact_id` (`contact_id`),
KEY `automation_id` (`automation_id`)
) $collate;";
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* bwf_contact_fields table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Contact_Fields' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Contact_Fields extends BWFAN_DB_Tables_Base {
public $table_name = 'bwf_contact_fields';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"cid",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`cid` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `cid` (`cid`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* bwfan_contact_note table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Contact_Note' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Contact_Note extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_contact_note';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"id",
"cid",
"type",
"created_by",
"created_date",
"private",
"title",
"body",
"modified_by",
"modified_date",
"date_time",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`id` bigint(20) unsigned NOT NULL auto_increment,
`cid` bigint(20) unsigned NOT NULL,
`type` varchar(255) NOT NULL,
`created_by` bigint(20),
`created_date` datetime,
`private` tinyint(1) unsigned not null default 0,
`title` varchar(255),
`body` longtext,
`modified_by` bigint(20) default null,
`modified_date` datetime default null,
`date_time` datetime default null,
PRIMARY KEY (`id`),
KEY `cid` (`cid`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* bwfan_conversions table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Conversions' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Conversions extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_conversions';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"wcid",
"cid",
"trackid",
"oid",
"otype",
"wctotal",
"date",
];
}
/**
* Get a query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`wcid` bigint(20) unsigned NOT NULL,
`cid` bigint(20) unsigned NOT NULL,
`trackid` bigint(20) unsigned NOT NULL,
`oid` bigint(20) unsigned NOT NULL,
`otype` tinyint(2) unsigned not null COMMENT '1 - Automation 2 - Campaign 3 - Note 4 - Email 5 - SMS',
`wctotal` varchar(32),
`date` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`ID`),
KEY `cid` (`cid`),
KEY `oid` (`oid`),
KEY `trackid` (`trackid`),
KEY `otype` (`otype`),
KEY `date` (`date`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,91 @@
<?php
/**
* bwfan_engagement_tracking table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Engagement_Tracking' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Engagement_Tracking extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_engagement_tracking';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"cid",
"hash_code",
"created_at",
"updated_at",
"mode",
"send_to",
"type",
"open",
"click",
"oid",
"sid",
"author_id",
"tid",
"o_interaction",
"f_open",
"c_interaction",
"f_click",
"c_status",
"day",
"hour",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`cid` bigint(20) unsigned NOT NULL default 0,
`hash_code` varchar(60) NOT NULL,
`created_at` datetime NOT NULL default '0000-00-00 00:00:00',
`updated_at` datetime default NULL,
`mode` tinyint(1) unsigned not null default 1 COMMENT '1 - Email 2 - SMS',
`send_to` varchar(255) NOT NULL,
`type` tinyint(2) unsigned not null default 1 COMMENT '1 - Automation 2 - Broadcast 3 - Note 4 - Email 5 - SMS',
`open` smallint(3) unsigned NOT NULL default 0,
`click` smallint(3) unsigned NOT NULL default 0,
`oid` bigint(20) unsigned NOT NULL,
`sid` bigint(20) unsigned NOT NULL default 0 COMMENT 'Step ID',
`author_id` bigint(10) unsigned NOT NULL default 1,
`tid` int(20) unsigned NOT NULL default 0 COMMENT 'Template ID',
`o_interaction` varchar(255),
`f_open` datetime default NULL,
`c_interaction` varchar(255),
`f_click` datetime default NULL,
`c_status` tinyint(2) unsigned default 1 COMMENT '1 - Draft 2 - Send 3 - Error 4 - Bounced',
`day` tinyint(1) unsigned default NUll,
`hour` tinyint(2) unsigned default NUll,
PRIMARY KEY (`ID`),
KEY `cid` (`cid`),
KEY `created_at` (`created_at`),
KEY `mode` (`mode`),
KEY `type` (`type`),
KEY `oid` (`oid`),
KEY `sid` (`sid`),
KEY `tid` (`tid`),
KEY `f_open` (`f_open`),
KEY `f_click` (`f_click`),
KEY `day` (`day`),
KEY `hour` (`hour`),
KEY `c_status` (`c_status`),
UNIQUE KEY `hash_code` (`hash_code`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* bwfan_engagement_trackingmeta table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Engagement_Trackingmeta' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Engagement_Trackingmeta extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_engagement_trackingmeta';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"eid",
"meta_key",
"meta_value",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`eid` bigint(20) unsigned NOT NULL,
`meta_key` varchar(255) default NULL,
`meta_value` longtext,
PRIMARY KEY (`ID`),
KEY `eid` (`eid`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* bwfan_field_groups table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Field_Groups' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Field_Groups extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_field_groups';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"name",
"created_at",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`created_at` datetime,
PRIMARY KEY (`ID`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* bwfan_fields table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Fields' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Fields extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_fields';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"name",
"slug",
"type",
"gid",
"meta",
"mode",
"vmode",
"search",
"view",
"created_at",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL,
`type` tinyint(2) unsigned NOT NULL,
`gid` bigint(20) unsigned NOT NULL,
`meta` text NOT NULL,
`mode` tinyint(2) unsigned NOT NULL default 1 COMMENT '1 - Editable 2 - Non-editable',
`vmode` tinyint(2) unsigned NOT NULL default 1 COMMENT '1 - Editable 2 - Non-editable',
`search` tinyint(1) unsigned NOT NULL default 2 COMMENT '1 - Searchable 2 - Non-searchable',
`view` tinyint(1) unsigned NOT NULL default 1 COMMENT '1 - Viwable 2 - Non-Viwable',
`created_at` datetime,
PRIMARY KEY (`ID`),
KEY `slug` (`slug`($this->max_index_length)),
KEY `gid` (`gid`),
KEY `mode` (`mode`),
KEY `vmode` (`vmode`),
KEY `search` (`search`),
KEY `view` (`view`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,50 @@
<?php
/**
* bwfan_link_metrics table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Link_Metrics' ) ) {
class BWFAN_DB_Table_Link_Metrics extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_link_metrics';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"link_id",
"contact_id",
"ip_address",
"count",
"created_at",
"updated_at"
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`link_id` int(12) unsigned NOT NULL default 0,
`contact_id` bigint(20) unsigned NOT NULL default 0,
`ip_address` varchar(45) NULL,
`count` int(7) unsigned NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`ID`),
KEY `link_id` (`link_id`),
KEY `contact_id` (`contact_id`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* bwfan_links table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Links' ) ) {
class BWFAN_DB_Table_Links extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_links';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"url",
"l_hash",
"created_at",
"clean_url",
"oid",
"sid",
"tid",
"type"
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` int(12) unsigned NOT NULL auto_increment,
`url` text NOT NULL,
`l_hash` varchar(40) NOT NULL,
`clean_url` varchar(190) NOT NULL,
`created_at` datetime NOT NULL,
`oid` bigint(20) UNSIGNED NOT NULL default 0 COMMENT 'Object ID',
`sid` int(12) UNSIGNED NOT NULL default 0 COMMENT 'Step ID',
`tid` int(12) UNSIGNED NOT NULL default 0 COMMENT 'Template ID',
`type` tinyint(1) UNSIGNED NOT NULL default 1 COMMENT '1 - Automation | 2 - Broadcast | 3 - Note | 4 - Email | 5 - SMS | 6 - Form | 9 - Transactional',
PRIMARY KEY (`ID`),
UNIQUE KEY `l_hash` (`l_hash`),
KEY `clean_url` (`clean_url`),
KEY `oid` (`oid`),
KEY `sid` (`sid`),
KEY `tid` (`tid`),
KEY `type` (`type`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,39 @@
<?php
class BWFAN_DB_Table_Logmeta extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_logmeta';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"bwfan_log_id",
"meta_key",
"meta_value",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`bwfan_log_id` bigint(20) unsigned NOT NULL default '0',
`meta_key` varchar(255) default NULL,
`meta_value` longtext,
PRIMARY KEY (`ID`),
KEY `bwfan_log_id` (`bwfan_log_id`),
KEY `meta_key` (`meta_key`($this->max_index_length))
) $collate;";
}
}

View File

@@ -0,0 +1,46 @@
<?php
class BWFAN_DB_Table_Logs extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_logs';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"c_date",
"e_date",
"status",
"integration_slug",
"integration_action",
"automation_id",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`c_date` datetime NOT NULL default '0000-00-00 00:00:00',
`e_date` bigint(12) NOT NULL,
`status` int(1) NOT NULL default 0 COMMENT '0 - Failed 1 - Success',
`integration_slug` varchar(50) default NULL,
`integration_action` varchar(100) default NULL,
`automation_id` int(10) NOT NULL,
PRIMARY KEY (`ID`),
KEY `ID` (`ID`),
KEY `status` (`status`),
KEY `automation_id` (`automation_id`)
) $collate;";
}
}

View File

@@ -0,0 +1,50 @@
<?php
class BWFAN_DB_Table_Message_Unsubscribe extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_message_unsubscribe';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"recipient",
"mode",
"c_date",
"automation_id",
"c_type",
"sid",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`recipient` varchar(255) default NULL,
`mode` tinyint(1) NOT NULL COMMENT '1 - Email 2 - SMS' default 1,
`c_date` datetime NOT NULL default '0000-00-00 00:00:00',
`automation_id` bigint(20) unsigned default '0',
`c_type` tinyint(1) NOT NULL default '1' COMMENT '1 - Automation 2 - Broadcast 3 - Manual 4 - Form',
`sid` bigint(20) unsigned NOT NULL default 0 COMMENT 'Step ID',
PRIMARY KEY (`ID`),
KEY `ID` (`ID`),
KEY `recipient` (`recipient`($this->max_index_length)),
KEY `mode` (`mode`),
KEY `c_date` (`c_date`),
KEY `automation_id` (`automation_id`),
KEY `c_type` (`c_type`),
KEY `sid` (`sid`)
) $collate;";
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* bwfan_message table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Message' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Message extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_message';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"track_id",
"sub",
"body",
"date",
"data",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`track_id` bigint(20) unsigned NOT NULL,
`sub` varchar(255) NOT NULL,
`body` longtext,
`date` datetime DEFAULT NULL,
`data` longtext,
PRIMARY KEY (`ID`),
KEY `track_id` (`track_id`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,37 @@
<?php
class BWFAN_DB_Table_Options extends BWFAN_DB_Tables_Base {
public $table_name = 'bwf_options';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"id",
"key",
"value",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`id` bigint(10) unsigned NOT NULL auto_increment,
`key` varchar(150) default NULL,
`value` longtext,
PRIMARY KEY (`id`),
KEY `id` (`id`),
KEY `key` (`key`)
) $collate;";
}
}

View File

@@ -0,0 +1,34 @@
<?php
class BWFAN_DB_Table_Task_Claim extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_task_claim';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"claim_id",
"date_created_gmt",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`claim_id` bigint(20) unsigned NOT NULL auto_increment,
`date_created_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`claim_id`),
KEY `date_created_gmt` (`date_created_gmt`)
) $collate;";
}
}

View File

@@ -0,0 +1,39 @@
<?php
class BWFAN_DB_Table_Taskmeta extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_taskmeta';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"bwfan_task_id",
"meta_key",
"meta_value",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`bwfan_task_id` bigint(20) unsigned NOT NULL default '0',
`meta_key` varchar(255) NULL,
`meta_value` longtext,
PRIMARY KEY (`ID`),
KEY `bwfan_task_id` (`bwfan_task_id`),
KEY `meta_key` (`meta_key`($this->max_index_length))
) $collate;";
}
}

View File

@@ -0,0 +1,54 @@
<?php
class BWFAN_DB_Table_Tasks extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_tasks';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"c_date",
"e_date",
"automation_id",
"integration_slug",
"integration_action",
"status",
"claim_id",
"attempts",
"priority",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`c_date` datetime NOT NULL default '0000-00-00 00:00:00',
`e_date` bigint(12) NOT NULL,
`automation_id` int(10) NOT NULL,
`integration_slug` varchar(50) NULL,
`integration_action` varchar(100) NULL,
`status` int(1) NOT NULL default 0 COMMENT '0 - Pending 1 - Paused',
`claim_id` bigint(20) unsigned default 0,
`attempts` tinyint(1) unsigned default 0,
`priority` int(5) unsigned default 10,
PRIMARY KEY (`ID`),
KEY `ID` (`ID`),
KEY `e_date` (`e_date`),
KEY `automation_id` (`automation_id`),
KEY `status` (`status`),
KEY `claim_id` (`claim_id`)
) $collate;";
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* bwfan_templates table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Templates' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Templates extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_templates';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"subject",
"template",
"type",
"title",
"mode",
"data",
"canned",
"created_at",
"updated_at",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`subject` varchar(255) default NULL,
`template` longtext,
`type` tinyint(1) unsigned not null default 1 COMMENT '1 - Email 2 - SMS',
`title` varchar(255) default NULL,
`mode` tinyint(1) NOT NULL default 1 COMMENT '1 - text only 2 - wc 3 - raw html 4 - drag and drop',
`data` longtext,
`canned` tinyint(1) default 0,
`created_at` datetime NOT NULL default '0000-00-00 00:00:00',
`updated_at` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`ID`),
KEY `type` (`type`),
KEY `mode` (`mode`),
KEY `canned` (`canned`)
) $collate;";
}
}
}

View File

@@ -0,0 +1,50 @@
<?php
/**
* bwfan_terms table class
*
*/
if ( ! class_exists( 'BWFAN_DB_Table_Terms' ) && BWFAN_Common::is_pro_3_0() ) {
class BWFAN_DB_Table_Terms extends BWFAN_DB_Tables_Base {
public $table_name = 'bwfan_terms';
/**
* Get table's columns
*
* @return string[]
*/
public function get_columns() {
return [
"ID",
"name",
"type",
"data",
"created_at",
"updated_at",
];
}
/**
* Get query for create table
*
* @return string
*/
public function get_create_table_query() {
global $wpdb;
$collate = $this->get_collation();
return "CREATE TABLE {$wpdb->prefix}$this->table_name (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`type` tinyint(2) unsigned NOT NULL,
`data` longtext,
`created_at` datetime,
`updated_at` datetime,
PRIMARY KEY (`ID`),
KEY `type` (`type`)
) $collate;";
}
}
}

View File

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