first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,731 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ES_DB_Actions extends ES_DB {
/**
* Table Name
*
* @since 4.2.1
* @var $table_name
*/
public $table_name;
/**
* Version
*
* @since 4.2.1
* @var $version
*/
public $version;
/**
* Primary Key
*
* @since 4.2.1
* @var $primary_key
*/
public $primary_key;
/**
* ES_DB_Lists constructor.
*
* @since 4.2.1
*/
public function __construct() {
global $wpdb;
$this->table_name = $wpdb->prefix . 'ig_actions';
$this->version = '1.0';
}
/**
* Get table columns
*
* @return array
*
* @since 4.2.1
*/
public function get_columns() {
return array(
'contact_id' => '%d',
'message_id' => '%d',
'campaign_id' => '%d',
'type' => '%d',
'count' => '%d',
'link_id' => '%d',
'list_id' => '%d',
'ip' => '%s',
'country' => '%s',
'device' => '%s',
'browser' => '%s',
'email_client' => '%s',
'os' => '%s',
'created_at' => '%d',
'updated_at' => '%d',
);
}
/**
* Get default column values
*
* @since 4.2.1
*/
public function get_column_defaults() {
return array(
'contact_id' => null,
'message_id' => null,
'campaign_id' => null,
'type' => 0,
'count' => 0,
'link_id' => 0,
'list_id' => 0,
'ip' => '',
'country' => '',
'device' => '',
'browser' => '',
'email_client' => '',
'os' => '',
'created_at' => ig_es_get_current_gmt_timestamp(),
'updated_at' => ig_es_get_current_gmt_timestamp(),
);
}
/**
* Track action
*
* @param $args
* @param bool $explicit
*
* @return bool
*
* @since 4.2.4
*/
public function add( $args, $explicit = true ) {
global $wpbd;
$ig_actions_table = IG_ACTIONS_TABLE;
$args_keys = array_keys( $args );
$args_keys_str = implode( ', ', $args_keys );
$sql = "INSERT INTO $ig_actions_table ($args_keys_str)";
$args_values_array = array();
if ( is_array( $args['contact_id'] ) ) {
$contact_ids = $args['contact_id'];
foreach ( $contact_ids as $contact_id ) {
$args['contact_id'] = $contact_id;
$args_values = array_values( $args );
$args_values = esc_sql( $args_values );
$args_values_array[] = $this->prepare_for_in_query( $args_values );
}
} else {
$args_values = array_values( $args );
$args_values = esc_sql( $args_values );
$args_values_array[] = $this->prepare_for_in_query( $args_values );
}
$sql .= ' VALUES ( ' . implode( '), (', $args_values_array ) . ' )';
$sql .= ' ON DUPLICATE KEY UPDATE';
$sql .= ( $explicit ) ? $wpbd->prepare( ' created_at = created_at, count = count+1, updated_at = %d, ip = %s, country = %s, browser = %s, device = %s, os = %s, email_client = %s', ig_es_get_current_gmt_timestamp(), $args['ip'], $args['country'], $args['browser'], $args['device'], $args['os'], $args['email_client'] ) : ' count = values(count)';
$result = $wpbd->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( false !== $result ) {
return true;
}
return false;
}
/**
* Insert data into bulk
*
* @param array $values
* @param int $length
* @param bool $return_insert_ids
*
* @since 4.2.1
*
* @since 4.3.5 Fixed issues and started using it.
*/
public function bulk_add( $values = array(), $length = 100, $explicit = true ) {
global $wpbd;
if ( ! is_array( $values ) ) {
return false;
}
// Get the first value from an array to check data structure
$first_value = array_slice( $values, 0, 1 );
$data = array_shift( $first_value );
// Set default values
$data = wp_parse_args( $data, $this->get_column_defaults() );
// Initialise column format array
$column_formats = $this->get_columns();
// Remove primary key as we don't require while inserting data
unset( $column_formats[ $this->primary_key ] );
// Force fields to lower case
$data = array_change_key_case( $data );
// White list columns
$data = array_intersect_key( $data, $column_formats );
// Reorder $column_formats to match the order of columns given in $data
$data = wp_parse_args( $data, $this->get_column_defaults() );
$data_keys = array_keys( $data );
$fields = array_keys( array_merge( array_flip( $data_keys ), $column_formats ) );
// Convert Batches into smaller chunk
$batches = array_chunk( $values, $length );
$success = false;
// Holds first and last row ids of each batch insert
$bulk_rows_start_end_ids = [];
foreach ( $batches as $key => $batch ) {
$place_holders = array();
$final_values = array();
$fields_str = '';
foreach ( $batch as $value ) {
$formats = array();
foreach ( $column_formats as $column => $format ) {
$final_values[] = isset( $value[ $column ] ) ? $value[ $column ] : $data[ $column ]; // set default if we don't have
$formats[] = $format;
}
$place_holders[] = '( ' . implode( ', ', $formats ) . ' )';
$fields_str = '`' . implode( '`, `', $fields ) . '`';
}
$query = "INSERT INTO $this->table_name ({$fields_str}) VALUES ";
$query .= implode( ', ', $place_holders );
$query .= ' ON DUPLICATE KEY UPDATE';
$query .= ' count = count + 1';
$sql = $wpbd->prepare( $query, $final_values );
if ( $wpbd->query( $sql ) ) {
$success = true;
}
}
return $success;
}
/**
* Get contacts count based on type, list id, days args
*
* @param int $args
*
* @return int $count
*
* @since 5.5.5
*/
public function get_count( $args = array(), $distinct = true ) {
global $wpbd;
$contacts_count = 0;
$query = 'SELECT';
if ( $distinct ) {
$query .= ' COUNT( DISTINCT ( contact_id ) )';
} else {
$query .= ' COUNT( contact_id )';
}
$query .= " FROM {$wpbd->prefix}ig_actions";
$where = array();
if ( ! empty( $args['type'] ) ) {
$where[] = $wpbd->prepare( 'type = %d', esc_sql( $args['type'] ) );
}
if ( ! empty( $args['list_id'] ) ) {
$where[] = $wpbd->prepare( 'list_id = %d', esc_sql( $args['list_id'] ) );
}
if ( ! empty( $args['days'] ) ) {
$where[] = $wpbd->prepare( 'created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))', esc_sql( $args['days'] ) );
}
if ( ! empty( $where ) ) {
$query .= ' WHERE ' . implode( ' AND ', $where );
}
$contacts_count = $wpbd->get_var(
$query
);
return $contacts_count;
}
public function get_actions( $args = array() ) {
global $wpbd;
$where = array();
if ( ! empty( $args['type'] ) ) {
$where[] = $wpbd->prepare( 'type = %d', esc_sql( $args['type'] ) );
}
if ( ! empty( $where ) ) {
$where = 'WHERE (' . implode( ') AND (', $where ) . ')';
} else {
$where = '';
}
$limit = '';
if ( ! empty( $args['limit'] ) ) {
$limit = $wpbd->prepare( 'LIMIT %d', esc_sql( $args['limit'] ) );
}
$order_by = '';
if ( ! empty( $args['order_by'] ) ) {
$order_by_column = esc_sql( $args['order_by'] );
$order = ! empty( $args['order'] ) ? esc_sql( $args['order'] ) : 'ASC';
$order_by = "ORDER BY {$order_by_column} {$order}";
}
$query = "SELECT * FROM {$wpbd->prefix}ig_actions {$where} {$order_by} {$limit}" ;
$actions = $wpbd->get_results(
$query,
ARRAY_A
);
return $actions;
}
public function get_actions_count( $args = array() ) {
global $wpbd;
$query = 'SELECT';
$columns = array();
if ( ! empty( $args['types'] ) ) {
$types = $args['types'];
foreach ( $types as $type ) {
$column_name = '';
switch ( $type ) {
case IG_CONTACT_SUBSCRIBE:
$column_name = 'subscribed';
break;
case IG_MESSAGE_SENT:
$column_name = 'sent';
break;
case IG_MESSAGE_OPEN:
$column_name = 'opened';
break;
case IG_LINK_CLICK:
$column_name = 'clicked';
break;
case IG_CONTACT_UNSUBSCRIBE:
$column_name = 'unsubscribed';
break;
case IG_MESSAGE_SOFT_BOUNCE:
$column_name = 'soft_bounced';
break;
case IG_MESSAGE_HARD_BOUNCE:
$column_name = 'hard_bounced';
break;
}
$columns[] = 'SUM( IF( type = ' . $type . ", 1, 0 ) ) AS '" . $column_name . "'";
}
}
if ( ! empty( $columns ) ) {
$query .= ' ' . implode( ',', $columns );
} else {
$query .= ' COUNT( contact_id )';
}
$query .= " FROM {$wpbd->prefix}ig_actions";
$where = array();
if ( ! empty( $args['list_id'] ) ) {
// Since list_id isn't store for sent/opened/clicked/bounced events, we are using contacts ids from list_contacts table from list id.
$where[] = $wpbd->prepare( "contact_id IN(SELECT contact_id FROM `{$wpbd->prefix}ig_lists_contacts` WHERE list_id = %d )", esc_sql( $args['list_id'] ) );
}
if ( ! empty( $args['campaign_id'] ) ) {
// Since list_id isn't store for sent/opened/clicked/bounced events, we are using contacts ids from list_contacts table from list id.
$where[] = $wpbd->prepare( 'campaign_id = %d', esc_sql( $args['campaign_id'] ) );
}
if ( ! empty( $args['days'] ) ) {
$where[] = $wpbd->prepare( 'created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))', esc_sql( $args['days'] ) );
}
if ( ! empty( $where ) ) {
$query .= ' WHERE ' . implode( ' AND ', $where );
}
$results = $wpbd->get_row(
$query,
ARRAY_A
);
return $results;
}
/**
* Get total contacts who have clicked links in last $days
*
* @param int $days
*
* @return string|null
*
* @since 4.3.2
*/
public function get_total_contacts_clicks_links( $days = 0, $distinct = true ) {
global $wpdb;
$args = array(
IG_LINK_CLICK,
);
$total_contacts_clicked = 0;
if ( $distinct ) {
if ( 0 != $days ) {
$days = esc_sql( $days );
$args[] = $days;
$total_contacts_clicked = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(`contact_id`)) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d AND created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))",
$args
)
);
} else {
$total_contacts_clicked = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(`contact_id`)) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d",
$args
)
);
}
} else {
if ( 0 != $days ) {
$days = esc_sql( $days );
$args[] = $days;
$total_contacts_clicked = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(`contact_id`) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d AND created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))",
$args
)
);
} else {
$total_contacts_clicked = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(`contact_id`) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d",
$args
)
);
}
}
return $total_contacts_clicked;
}
/**
* Get total contacts who have unsubscribed in last $days
*
* @param int $days
*
* @return string|null
*/
public function get_total_contact_unsubscribed( $days = 0, $distinct = true ) {
global $wpdb;
$args = array(
IG_CONTACT_UNSUBSCRIBE,
);
$total_emails_unsubscribed = 0;
if ( $distinct ) {
if ( 0 != $days ) {
$days = esc_sql( $days );
$args[] = $days;
$total_emails_unsubscribed = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(`contact_id`)) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d AND created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))",
$args
)
);
} else {
$total_emails_unsubscribed = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(`contact_id`)) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d",
$args
)
);
}
} else {
if ( 0 != $days ) {
$days = esc_sql( $days );
$args[] = $days;
$total_emails_unsubscribed = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(`contact_id`) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d AND created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))",
$args
)
);
} else {
$total_emails_unsubscribed = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(`contact_id`) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d",
$args
)
);
}
}
return $total_emails_unsubscribed;
}
/**
* Get total contacts who have opened message in last $days
*
* @param int $days
*
* @return string|null
*
* @since 4.4.0
*/
public function get_total_contacts_opened_message( $days = 0, $distinct = true ) {
global $wpdb;
$args = array(
IG_MESSAGE_OPEN,
);
$total_emails_opened = 0;
if ( $distinct ) {
if ( 0 != $days ) {
$days = esc_sql( $days );
$args[] = $days;
$total_emails_opened = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(`contact_id`)) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d AND created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))",
$args
)
);
} else {
$total_emails_opened = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(`contact_id`)) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d",
$args
)
);
}
} else {
if ( 0 != $days ) {
$days = esc_sql( $days );
$args[] = $days;
$total_emails_opened = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(`contact_id`) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d AND created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))",
$args
)
);
} else {
$total_emails_opened = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(`contact_id`) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d",
$args
)
);
}
}
return $total_emails_opened;
}
/**
* Get total emails sent in last $days
*
* @param int $days
*
* @return string|null
*
* @since 4.4.0
*/
public function get_total_emails_sent( $days = 0, $distinct = true ) {
global $wpdb;
$args = array(
IG_MESSAGE_SENT,
);
$total_emails_sent = 0;
if ( $distinct ) {
if ( 0 != $days ) {
$days = esc_sql( $days );
$args[] = $days;
$total_emails_sent = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(`contact_id`)) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d AND created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))",
$args
)
);
} else {
$total_emails_sent = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(`contact_id`)) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d",
$args
)
);
}
} else {
if ( 0 != $days ) {
$days = esc_sql( $days );
$args[] = $days;
$total_emails_sent = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(`contact_id`) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d AND created_at >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))",
$args
)
);
} else {
$total_emails_sent = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(`contact_id`) FROM {$wpdb->prefix}ig_actions WHERE `type` = %d",
$args
)
);
}
}
return $total_emails_sent;
}
/**
* Get contact count based on campaign_id and type
*
* @return string|null
*
* @since 4.5.2
*/
public function get_count_based_on_id_type( $campaign_id, $message_id, $type, $distinct = true ) {
global $wpbd;
$args = array();
$args[] = $campaign_id;
$args[] = $message_id;
$args[] = $type;
$count = 0;
if ( $distinct ) {
$query = $wpbd->prepare(
"SELECT COUNT(DISTINCT(`contact_id`)) as count FROM {$wpbd->prefix}ig_actions WHERE `campaign_id`= %d AND `message_id`= %d AND `type` = %d",
$args
);
} else {
$query = $wpbd->prepare(
"SELECT COUNT(`contact_id`) as count FROM {$wpbd->prefix}ig_actions WHERE `campaign_id`= %d AND `message_id`= %d AND `type` = %d",
$args
);
}
$cache_key = ES_Cache::generate_key( $query );
$exists_in_cache = ES_Cache::is_exists( $cache_key, 'query' );
if ( ! $exists_in_cache ) {
$count = $wpbd->get_var(
$query
);
ES_Cache::set( $cache_key, $count, 'query' );
} else {
$count = ES_Cache::get( $cache_key, 'query' );
}
return $count;
}
/**
* Get Last opened at based on contact_ids
*
* @param array $contact_ids
*
* @return array
*
* @since 4.6.5
*/
public function get_last_opened_of_contact_ids( $contact_ids = '', $filter = false ) {
global $wpbd;
if ( empty( $contact_ids ) ) {
return array();
}
$contact_ids_str = implode( ',', $contact_ids );
$result = $wpbd->get_results( $wpbd->prepare( "SELECT contact_id, MAX(created_at) as last_opened_at FROM {$wpbd->prefix}ig_actions WHERE contact_id IN ({$contact_ids_str}) AND type = %d GROUP BY contact_id", IG_MESSAGE_OPEN ), ARRAY_A );
if ( $filter ) {
$last_opened_at = array_column( $result, 'last_opened_at', 'contact_id' );
foreach ( $last_opened_at as $contact_id => $timestamp ) {
$convert_date_format = get_option( 'date_format' );
$convert_time_format = get_option( 'time_format' );
$last_opened_at[ $contact_id ] = get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $timestamp ), $convert_date_format . ' ' . $convert_time_format );
}
return $last_opened_at;
}
return $result;
}
public function delete_by_mailing_queue_id( $mailing_queue_ids ) {
global $wpbd;
if ( ! empty( $mailing_queue_ids ) ) {
$mailing_queue_ids = esc_sql( $mailing_queue_ids );
$mailing_queue_ids = implode( ',', array_map( 'absint', $mailing_queue_ids ) );
$wpbd->query(
"DELETE FROM {$wpbd->prefix}ig_actions WHERE message_id IN ($mailing_queue_ids)"
);
}
}
public function get_link_cliked_subscribers( $campaign_id = '', $link_id = '' ) {
global $wpdb;
$result = array();
if (!empty($campaign_id) && !empty($link_id) && is_numeric($campaign_id) && is_numeric($link_id)) {
$result = $wpdb->get_results($wpdb->prepare(
"SELECT c.email,c.first_name,c.last_name
FROM {$wpdb->prefix}ig_contacts AS c
INNER JOIN {$wpdb->prefix}ig_actions AS a ON c.id = a.contact_id
WHERE a.campaign_id = %d AND a.link_id = %d",
$campaign_id,
$link_id
), ARRAY_A);
}
return $result;
}
}

View File

@@ -0,0 +1,80 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ES_DB_Blocked_Emails extends ES_DB {
/**
* Table Name
*
* @since 4.2.2
*
* @var $table_name
*/
public $table_name;
/**
* Version
*
* @since 4.2.2
*
* @var $version
*/
public $version;
/**
* Primary Key
*
* @since 4.2.2
*
* @var $primary_key
*/
public $primary_key;
/**
* ES_DB_Blocked_Emails constructor.
*
* @since 4.2.2
*/
public function __construct() {
global $wpdb;
parent::__construct();
$this->table_name = $wpdb->prefix . 'ig_blocked_emails';
$this->primary_key = 'id';
$this->version = '1.0';
}
/**
* Get columns and formats
*
* @since 2.1
*/
public function get_columns() {
return array(
'id' => '%d',
'email' => '%s',
'ip' => '%s',
'created_on' => '%s',
);
}
/**
* Get default column values
*
* @since 2.1
*/
public function get_column_defaults() {
return array(
'email' => null,
'ip' => null,
'created_on' => ig_get_current_date_time(),
);
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,253 @@
<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* ES_DB_Custom_Fields class
*
*@since 4.8.4
*/
class ES_DB_Custom_Fields extends ES_DB {
/**
* Table name
*
* @since 4.8.4
* @var string
*
*/
public $table_name;
/**
* Table DB version
*
* @since 4.8.4
* @var string
*
*/
public $version;
/**
* Table primary key column name
*
* @since 4.8.4
* @var string
*
*/
public $primary_key;
/**
* ES_DB_Custom_Fields constructor.
*
* @since 4.8.4
*/
public function __construct() {
global $wpdb;
parent::__construct();
$this->table_name = $wpdb->prefix . 'ig_custom_fields';
$this->primary_key = 'id';
$this->version = '1.0';
}
/**
* Get table columns
*
* @return array
*
* @since 4.8.4
*/
public function get_columns() {
return array(
'id' => '%d',
'slug' => '%s',
'label' => '%s',
'type' => '%s',
'meta' => '%s',
);
}
/**
* Get default column values
*
* @since 4.8.4
*/
public function get_column_defaults() {
return array(
'id' => null,
'slug' => null,
'label' => null,
'type' => null,
'meta' => null,
);
}
/**
* Get all custom fields
*
* @return array|object|null
*
* @since 4.8.4
*/
public function get_custom_fields() {
return $this->get_all();
}
public function get_custom_date_fields() {
$where_condition = "type = 'date'";
return ES()->custom_fields_db->get_by_conditions( $where_condition );
}
/**
* Add Custom Field
*
* @param $data
*
* @return int
*
* @since 4.8.4
*/
public function add_custom_field( $data ) {
return $this->insert( $data );
}
/**
* Update Custom Field
*
* @param int $row_id
* @param array $data
*
* @return bool|void
*
* @since 4.8.4
*/
public function update_custom_field( $row_id, $data = array() ) {
if ( empty( $row_id ) ) {
return;
}
$data = array(
'label' => $data['label'],
'meta' => $data['meta'],
);
return $this->update( $row_id, $data );
}
/**
* Get Custom field By ID
*
* @param $id
*
* @return array|mixed
*
* @since 4.8.4
*/
public function get_custom_field_by_id( $id ) {
if ( empty( $id ) ) {
return array();
}
return $this->get( $id );
}
/**
* Get Custom field By Slug
*
* @param $slug_name
*
* @return array|mixed
*
* @since 4.8.4
*/
public function get_custom_field_meta_by_slug( $slug ) {
if ( empty( $slug ) ) {
return array();
}
$meta = $this->get_column_by( 'meta', 'slug', $slug );
if ( $meta ) {
$meta = maybe_unserialize( $meta );
}
return $meta;
}
/**
* Check if custom field already exists
*
* @param $name
*
* @return bool
*
* @since 4.8.4
*/
public function is_custom_field_exists( $slug ) {
$col = $this->get_by( 'slug', $slug );
if ( is_null( $col ) ) {
return false;
}
return true;
}
/**
* Delete Custom fields
*
* @param $ids
*
* @since 4.8.4
*/
public function delete_custom_fields( $ids ) {
if ( ! is_array( $ids ) ) {
$ids = array( $ids );
}
$field_deleted = 0;
if ( is_array( $ids ) && count( $ids ) > 0 ) {
foreach ( $ids as $id ) {
$field_deleted = $this->delete( absint( $id ) );
/**
* Take necessary cleanup steps using this hook
*
* @since 4.8.4
*/
do_action( 'ig_es_custom_field_deleted', $id );
}
return $field_deleted;
}
return false;
}
/**
* Delete Custom fields
*
* @param $ids
*
* @since 4.8.4
*/
public function get_custom_field_slug_list_by_ids( $ids ) {
global $wpbd;
if ( ! is_array( $ids ) && ! count( $ids ) > 0 ) {
return array();
}
$ids_str = implode( ',', $ids );
$result = $wpbd->get_col( "SELECT slug FROM {$wpbd->prefix}ig_custom_fields WHERE id IN ({$ids_str})" );
return $result;
}
}

View File

@@ -0,0 +1,365 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ES_DB_Forms extends ES_DB {
/**
* Table name
*
* @since 4.2.2
* @var string
*/
public $table_name;
/**
* Table DB version
*
* @since 4.2.2
* @var string
*/
public $version;
/**
* Table primary key column name
*
* @since 4.2.2
* @var string
*/
public $primary_key;
/**
* ES_DB_Forms constructor.
*
* @since 4.0.0
*/
public function __construct() {
global $wpdb;
parent::__construct();
$this->table_name = $wpdb->prefix . 'ig_forms';
$this->primary_key = 'id';
$this->version = '1.0';
}
/**
* Get table columns
*
* @return array
*
* @since 4.2.2
*/
public function get_columns() {
return array(
'id' => '%d',
'name' => '%s',
'body' => '%s',
'settings' => '%s',
'styles' => '%s',
'created_at' => '%s',
'updated_at' => '%s',
'deleted_at' => '%s',
'af_id' => '%d',
);
}
/**
* Get default column values
*
* @since 4.2.2
*/
public function get_column_defaults() {
return array(
'name' => null,
'body' => null,
'settings' => null,
'styles' => null,
'created_at' => ig_get_current_date_time(),
'updated_at' => null,
'deleted_at' => null,
'af_id' => 0,
);
}
/**
* Get ID Name Map of Forms
*
* Note: We are using this static function in Icegram.
* Think about compatibility before any modification
*
* @return array
*
* @since 4.0.0
*
* @modify 4.2.2
*/
public static function get_forms_id_name_map() {
global $wpdb;
$results = $wpdb->get_results( "SELECT id, name FROM {$wpdb->prefix}ig_forms", ARRAY_A );
$id_name_map = array();
if ( count( $results ) > 0 ) {
foreach ( $results as $result ) {
$id_name_map[ $result['id'] ] = $result['name'];
}
}
return $id_name_map;
}
/**
* Add Form
*
* @param $data
*
* @return int
*
* @since 4.2.2
*/
public function add_form( $data ) {
return $this->insert( $data );
}
/**
* Get Form By ID
*
* @param $id
*
* @return array|mixed
*
* @since 4.0.0
*/
public function get_form_by_id( $id ) {
if ( empty( $id ) ) {
return array();
}
return $this->get( $id );
}
/**
* Get form based on advance form id
*
* @param $af_id
*
* @return array|mixed
*
* @since 4.0.0
*
* @modify 4.2.0
*/
public function get_form_by_af_id( $af_id ) {
global $wpdb;
$where = $wpdb->prepare( 'af_id = %d', $af_id );
$forms = $this->get_by_conditions( $where );
$form = array();
if ( ! empty( $forms ) ) {
$form = array_shift( $forms );
}
return $form;
}
/**
* Migrate advanced forms data
*
* @since 4.0.0
*
* @modify 4.2.2
*/
public function migrate_advanced_forms() {
global $wpdb;
$is_table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'es_advanced_form' ) ) === $wpdb->prefix . 'es_advanced_form';
$lists_name_id_map = ES()->lists_db->get_list_id_name_map( '', true );
if ( $is_table_exists ) {
$forms = $wpdb->get_results(
"SELECT * FROM {$wpdb->prefix}es_advanced_form",
ARRAY_A
);
if ( count( $forms ) > 0 ) {
$data = array();
foreach ( $forms as $key => $form ) {
$es_af_id = $form['es_af_id'];
$es_af_title = $form['es_af_title'];
$es_af_desc = $form['es_af_desc'];
$es_af_name = $form['es_af_name'];
$es_af_name_mand = $form['es_af_name_mand'];
$es_af_email = $form['es_af_email'];
$es_af_email_mand = $form['es_af_email_mand'];
$es_af_group = $form['es_af_group'];
$es_af_group_mand = $form['es_af_group_mand'];
$es_af_group_list = $form['es_af_group_list'];
$es_af_group_lists = explode( ',', $es_af_group_list );
$list_ids = array();
if ( count( $es_af_group_lists ) > 0 ) {
foreach ( $es_af_group_lists as $list ) {
if ( ! isset( $lists_name_id_map[ $list ] ) ) {
$list_id = ES()->lists_db->add_list( $list );
$lists_name_id_map[ $list ] = $list_id;
}
$list_ids[] = $lists_name_id_map[ $list ];
}
}
$body = array(
array(
'type' => 'text',
'name' => 'Name',
'id' => 'name',
'params' => array(
'label' => 'Name',
'show' => ( 'YES' === $es_af_name ) ? true : false,
'required' => ( 'YES' === $es_af_name_mand ) ? true : false,
),
'position' => 1,
),
array(
'type' => 'text',
'name' => 'Email',
'id' => 'email',
'params' => array(
'label' => 'Email',
'show' => ( 'YES' === $es_af_email ) ? true : false,
'required' => ( 'YES' === $es_af_email_mand ) ? true : false,
),
'position' => 2,
),
array(
'type' => 'checkbox',
'name' => 'Lists',
'id' => 'lists',
'params' => array(
'label' => 'Lists',
'show' => ( 'YES' === $es_af_group ) ? true : false,
'required' => ( 'YES' === $es_af_group_mand ) ? true : false,
'values' => $list_ids,
),
'position' => 3,
),
array(
'type' => 'submit',
'name' => 'submit',
'id' => 'submit',
'params' => array(
'label' => 'Subscribe',
'show' => true,
),
'position' => 4,
),
);
$settings = array(
'lists' => $list_ids,
'desc' => $es_af_desc,
);
$data[ $key ]['name'] = $es_af_title;
$data[ $key ]['body'] = maybe_serialize( $body );
$data[ $key ]['settings'] = maybe_serialize( $settings );
$data[ $key ]['styles'] = null;
$data[ $key ]['created_at'] = ig_get_current_date_time();
$data[ $key ]['updated_at'] = null;
$data[ $key ]['deleted_at'] = null;
$data[ $key ]['af_id'] = $es_af_id;
}
$this->bulk_insert( $data );
}
}
}
/**
* Get total forms count
*
* @return string|null
*
* @since 4.0.0
*
* @modify 4.2.2
*/
public function count_forms() {
return $this->count();
}
/**
* Get form settings based on form id
*
* @param $form_id
*
* @return $settings
*
* @since 5.6.2
*/
public function get_form_settings( $form_id ) {
if ( ! empty( $form_id ) ) {
$form_data = ES()->forms_db->get_form_by_id( $form_id );
$settings = ig_es_get_data( $form_data, 'settings', array() );
if ( ! empty( $settings ) ) {
$settings = maybe_unserialize( $settings );
return $settings;
}
}
}
/**
* Delete Forms
*
* @param $ids
*
* @since 4.3.1
*/
public function delete_forms( $ids ) {
if ( ! is_array( $ids ) ) {
$ids = array( $ids );
}
if ( is_array( $ids ) && count( $ids ) > 0 ) {
foreach ( $ids as $id ) {
$this->delete( absint( $id ) );
/**
* Take necessary cleanup steps using this hook
*
* @since 4.3.1
*/
do_action( 'ig_es_form_deleted', $id );
}
}
}
}

View File

@@ -0,0 +1,178 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'ES_DB_Links' ) ) {
/**
* Store Campaigns links
*
* Class ES_DB_Links
*
* @since 4.2.4
*/
class ES_DB_Links extends ES_DB {
/**
* Table Name
*
* @since 4.2.4
* @var string
*/
public $table_name;
/**
* Table Version
*
* @since 4.2.4
* @var string
*/
public $version;
/**
* Primary key
*
* @since 4.2.4
* @var string
*/
public $primary_key;
/**
* Initialize
*
* ES_DB_Links constructor.
*
* @since 4.2.4
*/
public function __construct() {
global $wpdb;
parent::__construct();
$this->table_name = $wpdb->prefix . 'ig_links';
$this->version = '1.0';
$this->primary_key = 'id';
}
/**
* Get columns and formats
*
* @since 4.2.4
*/
public function get_columns() {
return array(
'id' => '%d',
'message_id' => '%d',
'campaign_id' => '%d',
'link' => '%s',
'hash' => '%s',
'i' => '%d',
'created_at' => '%s',
);
}
/**
* Get default column values
*
* @since 4.2.4
*/
public function get_column_defaults() {
return array(
'message_id' => 0,
'campaign_id' => 0,
'link' => '',
'hash' => '',
'i' => '',
'created_at' => ig_get_current_date_time(),
);
}
/**
* Get link by hash
*
* @param null $hash
*
* @return array|object|void|null
*
* @since 4.2.4
*/
public function get_by_hash( $hash = null ) {
if ( empty( $hash ) ) {
return array();
}
return $this->get_by( 'hash', $hash );
}
/**
* Get link by id
*
* @param int $id
*
* @return array|object|void|null
*
* @since 4.2.4
*/
public function get_by_id( $id = 0 ) {
if ( empty( $id ) ) {
return;
}
return $this->get_by( 'id', $id );
}
/**
* Check whether link exists in campaign
*
* @param $link
* @param int $campaign_id
* @param int $message_id
* @param int $index
*
* @return string|null
*
* @since 4.2.4
*/
public function get_link_by_campaign_id( $link, $campaign_id = 0, $message_id = 0, $index = 0 ) {
global $wpdb;
ES_Cache::flush();
$where = $wpdb->prepare( ' link = %s AND campaign_id = %d AND message_id = %d AND i = %d', $link, $campaign_id, $message_id, $index );
return $this->get_by_conditions( $where );
}
/**
* Check whether link exists in campaign
*
* @param int $message_id
*
* @return string|null
*
* @since 4.2.4
*/
public function get_links_by_message_id( $message_id = 0 ) {
global $wpdb;
$where = $wpdb->prepare( ' message_id = %d', $message_id );
return $this->get_by_conditions( $where );
}
public function get_links_by_ids( $link_ids ) {
global $wpbd;
$ids_count = count( $link_ids );
$ids_placeholders = array_fill( 0, $ids_count, '%d' );
$where = $wpbd->prepare( ' id IN( ' . implode( ',', $ids_placeholders ) . ' )', $link_ids );
return $this->get_by_conditions( $where );
}
}
}

View File

@@ -0,0 +1,532 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ES_DB_Lists extends ES_DB {
/**
* Table name
*
* @since 4.0.0
* @var $table_name
*/
public $table_name;
/**
* Table DB version
*
* @since 4.0.0
* @var $version
*/
public $version;
/**
* Table primary key column name
*
* @since 4.0.0
* @var $primary_key
*/
public $primary_key;
/**
* ES_DB_Lists constructor.
*
* @since 4.0.0
*/
public function __construct() {
global $wpdb;
parent::__construct();
$this->table_name = $wpdb->prefix . 'ig_lists';
$this->primary_key = 'id';
$this->version = '1.0';
}
/**
* Get table columns
*
* @return array
*
* @since 4.2.1
*/
public function get_columns() {
return array(
'id' => '%d',
'slug' => '%s',
'name' => '%s',
'description' => '%s',
'hash' => '%s',
'created_at' => '%s',
'updated_at' => '%s',
'deleted_at' => '%s',
);
}
/**
* Get default column values
*
* @since 4.2.1
*/
public function get_column_defaults() {
return array(
'slug' => null,
'name' => null,
'description' => null,
'hash' => null,
'created_at' => ig_get_current_date_time(),
'updated_at' => null,
'deleted_at' => null,
);
}
/**
* Get Lists
*
* @return array|object|null
*
* @since 4.0.0
*/
public function get_lists() {
return $this->get_all();
}
/**
* Get list id name map
*
* @param string $list_id
* @param bool $flip
*
* @return array|mixed|string
*
* @since 4.0.0
*
* @modify 4.2.1
*/
public function get_list_id_name_map( $list_id = '', $flip = false ) {
$lists_map = array();
$lists = $this->get_lists();
if ( count( $lists ) > 0 ) {
foreach ( $lists as $list ) {
$lists_map[ $list['id'] ] = $list['name'];
}
if ( ! empty( $list_id ) ) {
$list_name = ! empty( $lists_map[ $list_id ] ) ? $lists_map[ $list_id ] : '';
return $list_name;
}
if ( $flip ) {
$lists_map = array_flip( $lists_map );
}
}
return $lists_map;
}
/**
* Get list by name
*
* @param $name
*
* @return array|mixed
*
* @since 4.0.0
*
* @modify 4.2.1
*/
public function get_list_by_name( $name ) {
$list = $this->get_by( 'name', $name );
if ( is_null( $list ) ) {
$list = array();
}
return $list;
/*
TODO: Keep for sometime. Remove it after complete verification/ testing
global $wpdb;
$lists = array();
if ( ! empty( $name ) ) {
$query = "SELECT * FROM " . IG_LISTS_TABLE . " WHERE `name` = %s LIMIT 0, 1";
$sql = $wpdb->prepare( $query, $name );
$lists = $wpdb->get_results( $sql, ARRAY_A );
}
$list = array();
if ( count( $lists ) > 0 ) {
$list = array_shift( $lists );
}
return $list;
*/
}
/**
* Get list by slug
*
* @param string $slug List slug.
*
* @return bool/array $list Returns list array if list exists else false.
*
* @since 4.4.3
*/
public function get_list_by_slug( $slug ) {
$list = $this->get_by( 'slug', $slug );
if ( is_null( $list ) ) {
return false;
}
return $list;
}
/**
* Get list by id
*
* @param array $list_ids List IDs.
*
* @return bool/array Returns lists array if list exists else false.
*
* @since 4.6.12
*/
public function get_lists_by_id( $list_ids = array() ) {
global $wpdb;
if ( empty( $list_ids ) ) {
return array();
}
// Check if we have got array of list ids.
if ( is_array( $list_ids ) ) {
$list_ids_str = implode( ',', $list_ids );
} else {
$list_ids_str = $list_ids;
}
$where = "id IN ({$list_ids_str})";
return $this->get_by_conditions( $where );
}
/**
* Get all lists name by contact_id
*
* @param $id
*
* @return array
*
* @since 4.0.0
*
* @modify 4.2.0
*/
public function get_all_lists_name_by_contact( $id ) {
global $wpdb;
$res = $wpdb->get_col(
$wpdb->prepare(
"SELECT `name` FROM {$wpdb->prefix}ig_lists WHERE id IN ( SELECT list_id FROM {$wpdb->prefix}ig_lists_contacts WHERE contact_id = %d )",
$id
)
);
return $res;
}
/**
* Add lists
*
* @param $lists
*
* @since 4.0.0
*
* @modify 4.2.1
*/
public function add_lists( $lists ) {
if ( ! is_array( $lists ) ) {
$lists = array( $lists );
}
if ( count( $lists ) > 0 ) {
foreach ( $lists as $key => $list ) {
$this->add_list( $list );
}
}
/**
* $query = "SELECT LOWER(name) FROM " . IG_LISTS_TABLE;
* $existing_lists = $wpdb->get_col( $query );
* foreach ( $lists as $key => $list ) {
* // Insert only if list is not exists.
* $lower_list = strtolower( $list );
* if ( ! in_array( $lower_list, $existing_lists ) ) {
* $sql = "INSERT INTO " . IG_LISTS_TABLE . " (`slug`, `name`, `created_at`) VALUES (%s, %s, %s)";
* $query = $wpdb->prepare( $sql, sanitize_title( $list ), $list, ig_get_current_date_time() );
* $wpdb->query( $query );
* $existing_lists[] = $list;
* }
* }
*/
}
/**
* Add List into database
*
* @param string $list List name.
*
* @param string $slug List slug.
*
* @return int
*
* @since 4.0.0
*
* @modified 4.4.3 Added $slug parameter.
*
* @modified 5.0.4 Updated $list parameter from string to array
*/
public function add_list( $list = array(), $slug = '' ) {
if ( empty( $list ) ) {
return 0;
}
$list_data = $list;
//To handle case where only list name is passed as a string
if ( ! is_array( $list ) ) {
$list_data = array(
'name' => $list,
);
}
$lower_list = strtolower( $list_data['name'] );
$is_list_exists = $this->is_list_exists( $lower_list );
if ( $is_list_exists ) {
return 0;
}
$data = array(
'slug' => ! empty( $slug ) ? $slug : sanitize_title( $list_data['name'] ),
'name' => $list_data['name'],
'description' => isset( $list_data['desc'] ) ? $list_data['desc'] : '',
'hash' => ES_Common::generate_hash( 12 ),
);
return $this->insert( $data );
/*
$list_table = IG_LISTS_TABLE;
$query = "SELECT LOWER(name) FROM {$list_table}";
$existing_lists = $wpdb->get_col( $query );
$lower_list = strtolower( $list );
if ( ! in_array( $lower_list, $existing_lists ) ) {
$data = array();
$data['slug'] = sanitize_title( $list );
$data['name'] = $list;
$data['created_at'] = ig_get_current_date_time();
$insert = $wpdb->insert( $list_table, $data );
if ( $insert ) {
return $wpdb->insert_id;
}
}
return 0;
*/
}
/**
* Update List
*
* @param int $row_id
* @param array $data
*
* @return bool|void
*
* @since 4.2.1
*/
public function update_list( $row_id, $list_data ) {
if ( empty( $row_id ) ) {
return;
}
$data = array(
'name' => $list_data['name'],
'description' => $list_data['desc'],
'updated_at' => ig_get_current_date_time(),
);
return $this->update( $row_id, $data );
}
/**
* Check if list is already exists
*
* @param $name
*
* @return bool
*
* @since 4.2.1
*/
public function is_list_exists( $name ) {
$col = $this->get_by( 'name', $name );
if ( is_null( $col ) ) {
return false;
}
return true;
}
/**
* Get total count of lists
*
* @return string|null
*
* @since 4.2.0
*/
public function count_lists() {
return $this->count();
}
/**
* Get List Name By Id
*
* @param $id
*
* @return string|null
*
* @since 4.2.0
*/
public function get_list_name_by_id( $id ) {
return $this->get_column_by( 'name', 'id', $id );
}
/**
* Get list names by ids
*
* @param array $list_ids List ids
*
* @return array $list_names List name
*/
public function get_list_name_by_ids( $list_ids = array() ) {
$lists_id_name_map = ES()->lists_db->get_list_id_name_map();
$list_names = array();
foreach ( $list_ids as $list_id ) {
if ( ! empty( $lists_id_name_map[ $list_id ] ) ) {
$list_names[ $list_id ] = $lists_id_name_map[ $list_id ];
}
}
return $list_names;
}
/**
* Delete lists
*
* @param $ids
*
* @since 4.2.1
*/
public function delete_lists( $ids ) {
if ( ! is_array( $ids ) ) {
$ids = array( $ids );
}
if ( is_array( $ids ) && count( $ids ) > 0 ) {
foreach ( $ids as $id ) {
$this->delete( absint( $id ) );
/**
* Take necessary cleanup steps using this hook
*
* @since 4.3.1
*/
do_action( 'ig_es_list_deleted', $id );
}
}
}
/**
* Get list id hash map
*
* @param array $list_ids
*
* @return array $list_hash_map
*
* @since 4.6.12.1
*/
public function get_list_id_hash_map( $list_ids = array() ) {
$list_hash_map = array();
if ( ! empty( $list_ids ) ) {
$lists = ES()->lists_db->get_lists_by_id( $list_ids );
if ( ! empty( $lists ) ) {
foreach ( $lists as $list ) {
if ( ! empty( $list ) && ! empty( $list['id'] ) ) {
$list_hash_map[ $list['id'] ] = $list['hash'];
}
}
}
}
return $list_hash_map;
}
/**
* Get lists by hash
*
* @param array $list_hashes
*
* @return array
*
* @since 4.7.5
*/
public function get_lists_by_hash( $list_hashes = array() ) {
global $wpbd;
if ( empty( $list_hashes ) ) {
return array();
}
if ( ! is_array( $list_hashes ) ) {
$list_hashes = array( $list_hashes );
}
$hash_count = count( $list_hashes );
$hash_placeholders = array_fill( 0, $hash_count, '%s' );
$where = $wpbd->prepare(
'hash IN( ' . implode( ',', $hash_placeholders ) . ')',
$list_hashes
);
return $this->get_by_conditions( $where );
}
}

View File

@@ -0,0 +1,709 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ES_DB_Mailing_Queue {
public static $table_name = IG_MAILING_QUEUE_TABLE;
public static $primary_key = 'id';
public static $version = '1.0';
/**
* Get columns and formats
*
* @since 2.1
*/
public static function get_columns() {
return array(
'id' => '%d',
'hash' => '%s',
'campaign_id' => '%d',
'subject' => '%s',
'body' => '%s',
'count' => '%d',
'status' => '%s',
'start_at' => '%s',
'finish_at' => '%s',
'created_at' => '%s',
'updated_at' => '%s',
'meta' => '%s',
);
}
public static function get_column_defaults() {
return array(
'hash' => null,
'campaign_id' => 0,
'subject' => '',
'body' => '',
'count' => 0,
'status' => 'In Queue',
'start_at' => null,
'finish_at' => null,
'created_at' => ig_get_current_date_time(),
'updated_at' => null,
'meta' => null,
);
}
public static function get_notification_hash_to_be_sent() {
global $wpdb;
$hash = $wpdb->get_var(
$wpdb->prepare(
"SELECT hash FROM {$wpdb->prefix}ig_mailing_queue WHERE status = %s ORDER BY id LIMIT 0, 1",
'In Queue'
)
);
// TODO :: update start date
return $hash;
}
public static function get_notification_to_be_sent( $campaign_hash = '' ) {
global $wpdb;
$notification = array();
$ig_mailing_queue_table = IG_MAILING_QUEUE_TABLE;
$results = array();
if ( ! empty( $campaign_hash ) ) {
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ig_mailing_queue WHERE hash = %s",
array( $campaign_hash )
),
ARRAY_A
);
} else {
$current_time = ig_get_current_date_time();
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ig_mailing_queue WHERE status IN ('Sending', 'In Queue') AND start_at <= %s ORDER BY
CASE
WHEN start_at = '0000-00-00 00:00:00' THEN %s
ELSE start_at
END, id LIMIT 0, 1",
$current_time,
$current_time
),
ARRAY_A
);
}
if ( count( $results ) > 0 ) {
$notification = array_shift( $results );
}
return $notification;
}
/**
* Sync mailing queue content with campaign content
*
* @param array $notification
*
* @since 4.7.3
*/
public static function sync_content( $notification = array() ) {
if ( ! empty( $notification ) ) {
$meta = maybe_unserialize( $notification['meta'] );
if ( ! empty( $meta ) ) {
$filter = 'ig_es_refresh_' . $meta['type'] . '_content';
$post_id = ! empty( $meta['post_id'] ) ? $meta['post_id'] : 0;
$content = array();
$content = apply_filters(
$filter,
$content,
array(
'campaign_id' => $notification['campaign_id'],
'post_id' => $post_id,
)
);
// Update mailing queue with updated content data.
if ( ! empty( $content ) ) {
global $wpdb;
$notification['subject'] = ! empty( $content['subject'] ) ? $content['subject'] : $notification['subject'];
$notification['body'] = ! empty( $content['body'] ) ? $content['body'] : $notification['body'];
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}ig_mailing_queue SET subject = %s, body = %s WHERE hash = %s",
array(
$notification['subject'],
$notification['body'],
$notification['hash'],
)
)
);
}
}
}
return $notification;
}
// Query to insert sent emails (cron) records in table: es_sentdetails
public static function update_sent_status( $hash = '', $status = 'In Queue' ) {
global $wpdb;
// If status is sent then add finish_at time as well.
if ( 'Sent' === $status ) {
$current_date_time = ig_get_current_date_time();
$return_id = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}ig_mailing_queue SET status = %s, finish_at = %s WHERE hash = %s",
$status,
$current_date_time,
$hash
)
);
} elseif ( 'Sending' === $status ) {
$current_date_time = ig_get_current_date_time();
$return_id = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}ig_mailing_queue SET status = %s, start_at = %s WHERE hash = %s",
$status,
$current_date_time,
$hash
)
);
} else {
$return_id = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}ig_mailing_queue SET status = %s WHERE hash = %s",
$status,
$hash
)
);
}
return $return_id;
}
/* Get sent email count */
public static function get_sent_email_count( $notification_hash ) {
global $wpdb;
$email_count = $wpdb->get_col(
$wpdb->prepare(
"SELECT count FROM {$wpdb->prefix}ig_mailing_queue WHERE hash = %s ",
array( $notification_hash )
)
);
$email_count = array_shift( $email_count );
return $email_count;
}
public static function get_notification_by_hash( $notification_hash ) {
global $wpbd;
$notification = array();
$query = $wpbd->prepare(
"SELECT * FROM {$wpbd->prefix}ig_mailing_queue WHERE hash = %s",
$notification_hash
);
$cache_key = ES_Cache::generate_key( $query );
$exists_in_cache = ES_Cache::is_exists( $cache_key, 'query' );
if ( ! $exists_in_cache ) {
$results = $wpbd->get_results(
$query,
ARRAY_A
);
ES_Cache::set( $cache_key, $results, 'query' );
} else {
$results = ES_Cache::get( $cache_key, 'query' );
}
if ( count( $results ) > 0 ) {
$notification = array_shift( $results );
}
return $notification;
}
public static function get_notification_by_campaign_id( $campaign_id ) {
global $wpbd;
$notification = array();
$query = $wpbd->prepare( "SELECT * FROM {$wpbd->prefix}ig_mailing_queue WHERE campaign_id = %d", $campaign_id );
$cache_key = ES_Cache::generate_key( $query );
$exists_in_cache = ES_Cache::is_exists( $cache_key, 'query' );
if ( ! $exists_in_cache ) {
$results = $wpbd->get_results(
$query,
ARRAY_A
);
ES_Cache::set( $cache_key, $results, 'query' );
} else {
$results = ES_Cache::get( $cache_key, 'query' );
}
if ( is_array( $results ) && count( $results ) > 0 ) {
$notification = array_shift( $results );
}
return $notification;
}
public static function get_notifications( $per_page = 5, $page_number = 1 ) {
global $wpdb;
if ( ! empty( $per_page ) && ! empty( $page_number ) ) {
$start_limit = ( $page_number - 1 ) * $per_page;
$result = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ig_mailing_queue ORDER BY created_at DESC LIMIT %d, %d",
$start_limit,
$per_page
),
ARRAY_A
);
} else {
$result = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ig_mailing_queue ORDER BY created_at DESC "
),
ARRAY_A
);
}
return $result;
}
public static function get_notifications_count() {
global $wpdb;
$result = $wpdb->get_col(
$wpdb->prepare(
"SELECT count(*) as total_notifications FROM {$wpdb->prefix}ig_mailing_queue WHERE %d",
1
)
);
return $result[0];
}
public static function delete( $ids ) {
global $wpbd;
if ( empty( $ids ) ) {
return false;
}
$ids_count = count( $ids );
$ids_placeholders = array_fill( 0, $ids_count, '%d' );
// Delete notification.
$wpbd->query(
$wpbd->prepare(
"DELETE FROM `{$wpbd->prefix}ig_mailing_queue` WHERE id IN( " . implode( ',', $ids_placeholders ) . ' );',
$ids
)
);
}
public static function delete_notifications( $ids ) {
global $wpbd;
$ids_count = count( $ids );
$ids_placeholders = array_fill( 0, $ids_count, '%d' );
$campaign_query_args = array(
IG_ES_CAMPAIGN_STATUS_IN_ACTIVE, // First arguement for $wpbd->prepare
);
$campaign_query_args = array_merge( $campaign_query_args, $ids ); // Merge ids with other $wpbd->prepare arguements
// Update related broadcast campaign status to draft.
$wpbd->query(
$wpbd->prepare(
"UPDATE `{$wpbd->prefix}ig_campaigns` SET `status`= %d WHERE
id IN( SELECT `campaign_id` FROM `{$wpbd->prefix}ig_mailing_queue` WHERE
id IN( " . implode( ',', $ids_placeholders ) . " )
) AND type = 'newsletter';",
$campaign_query_args
)
);
// Delete notification.
$wpbd->query(
$wpbd->prepare(
"DELETE FROM `{$wpbd->prefix}ig_mailing_queue` WHERE id IN( " . implode( ',', $ids_placeholders ) . ' );',
$ids
)
);
}
public static function add_notification( $data ) {
global $wpdb;
$column_formats = self::get_columns();
$column_defaults = self::get_column_defaults();
$prepared_data = ES_DB::prepare_data( $data, $column_formats, $column_defaults, true );
$data = $prepared_data['data'];
$column_formats = $prepared_data['column_formats'];
$inserted = $wpdb->insert( IG_MAILING_QUEUE_TABLE, $data, $column_formats );
$last_report_id = 0;
if ( $inserted ) {
$last_report_id = $wpdb->insert_id;
}
return $last_report_id;
}
public static function update_notification( $notification_id, $data ) {
global $wpdb;
$column_formats = self::get_columns();
$column_defaults = self::get_column_defaults();
$prepared_data = ES_DB::prepare_data( $data, $column_formats, $column_defaults, true );
$data = $prepared_data['data'];
$column_formats = $prepared_data['column_formats'];
$wpdb->update( IG_MAILING_QUEUE_TABLE, $data, array( 'id' => $notification_id ), $column_formats );
}
public static function get_id_details_map() {
global $wpdb;
$query = 'SELECT id, start_at, hash FROM ' . IG_MAILING_QUEUE_TABLE;
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT id, start_at, hash FROM {$wpdb->prefix}ig_mailing_queue WHERE %d",
1
),
ARRAY_A
);
$details = array();
if ( count( $results ) > 0 ) {
foreach ( $results as $result ) {
$details[ $result['hash'] ]['id'] = $result['id'];
$details[ $result['hash'] ]['start_at'] = $result['start_at'];
}
}
return $details;
}
public static function get_mailing_queue_by_id( $mailing_queue_id ) {
global $wpdb;
$report = array();
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ig_mailing_queue WHERE id = %s",
$mailing_queue_id
),
ARRAY_A
);
if ( count( $results ) > 0 ) {
$report = array_shift( $results );
}
return $report;
}
/**
* Get recent campaigns data
*
* @param int $count
*
* @return array|object|null
*
* @since 4.4.0
*/
public static function get_recent_campaigns( $count = 5 ) {
global $wpdb;
if ( ! is_numeric( $count ) ) {
$count = 5;
}
return $wpdb->get_results(
$wpdb->prepare(
"SELECT id, hash, campaign_id, subject, start_at, status, finish_at FROM {$wpdb->prefix}ig_mailing_queue order by created_at DESC LIMIT 0, %d",
$count
),
ARRAY_A
);
}
public static function do_insert( $place_holders, $values ) {
global $wpdb, $wpbd;
$query = "INSERT INTO {$wpdb->prefix}ig_mailing_queue (`hash`, `campaign_id`, `subject`, `body`, `count`, `status`, `start_at`, `finish_at`, `created_at`, `updated_at`) VALUES ";
$query .= implode( ', ', $place_holders );
$sql = $wpbd->prepare( $query, $values );
$logger = get_ig_logger();
$logger->info( 'Query....<<<<<' . $sql );
if ( $wpbd->query( $sql ) ) {
return true;
} else {
return false;
}
}
public static function migrate_notifications() {
global $wpdb;
$total = $wpdb->get_var(
$wpdb->prepare(
"SELECT count(*) as total FROM {$wpdb->prefix}es_notification WHERE %d",
1
)
);
if ( $total > 0 ) {
$columns = self::get_columns();
unset( $columns['id'] );
$fields = array_keys( $columns );
$batch_size = IG_DEFAULT_BATCH_SIZE;
$total_bataches = ( $total > IG_DEFAULT_BATCH_SIZE ) ? ceil( $total / $batch_size ) : 1;
for ( $i = 0; $i < $total_bataches; $i ++ ) {
$batch_start = $i * $batch_size;
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}es_sentdetails LIMIT %d, %d",
$batch_start,
$batch_size
),
ARRAY_A
);
$values = array();
$place_holders = array();
foreach ( $results as $key => $result ) {
$queue_data['hash'] = $result['es_sent_guid'];
$queue_data['campaign_id'] = 0;
$queue_data['subject'] = $result['es_sent_subject'];
$queue_data['body'] = $result['es_sent_preview'];
$queue_data['count'] = $result['es_sent_count'];
$queue_data['status'] = $result['es_sent_status'];
$queue_data['start_at'] = $result['es_sent_starttime'];
$queue_data['finish_at'] = $result['es_sent_endtime'];
$queue_data['created_at'] = $result['es_sent_starttime'];
$queue_data = wp_parse_args( $queue_data, self::get_column_defaults() );
$formats = array();
foreach ( $columns as $column => $format ) {
$values[] = $queue_data[ $column ];
$formats[] = $format;
}
$place_holders[] = '( ' . implode( ', ', $formats ) . ' )';
}
ES_DB::do_insert( IG_MAILING_QUEUE_TABLE, $fields, $place_holders, $values );
}
}
}
/**
* Method to update subscribers count in mailing queue table.
*
* @param string $hash Mailing queue hash.
* @param int $count Subscribers count.
*
* @since 4.6.3
*/
public static function update_mailing_queue( $mailing_queue_id = 0, $data = array() ) {
if ( empty( $mailing_queue_id ) ) {
return;
}
global $wpdb;
// Row ID must be positive integer
$mailing_queue_id = absint( $mailing_queue_id );
if ( empty( $mailing_queue_id ) ) {
return false;
}
if ( empty( $where ) ) {
$where = self::$primary_key;
}
// Initialise column format array
$column_formats = self::get_columns();
// Force fields to lower case
$data = array_change_key_case( $data );
// White list columns
$data = array_intersect_key( $data, $column_formats );
// Reorder $column_formats to match the order of columns given in $data
$data_keys = array_keys( $data );
$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
if ( false === $wpdb->update( self::$table_name, $data, array( $where => $mailing_queue_id ), $column_formats ) ) {
return false;
}
return true;
}
/**
* Decrease subscribers count by given amount.
*
* @param int $mailing_queue_id Mailing queue id.
* @param int $decrease_by Subscribers count.
*
* @since 4.7.6
*/
public static function decrease_subscribers_count( $mailing_queue_id, $decrease_by = 0 ) {
global $wpdb;
if ( empty( $mailing_queue_id ) || empty( $decrease_by ) ) {
return;
}
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}ig_mailing_queue SET count = count - %d WHERE id = %d",
$decrease_by,
$mailing_queue_id
)
);
}
/**
* Get frequency of sending recent newsletter campaigns.
*
* @param int $max_campaigns_count fetch the recent n number of campaigns
*
* @since 5.5.7
*/
public static function get_campaign_sending_frequency( $max_campaigns_count = 10 ) {
$campaigns = self::get_recent_campaigns( $max_campaigns_count );
$campaigns_count = count($campaigns);
$total_interval = 0;
$avg_sending_interval = 0;
if ($campaigns_count > 1) {
for ($index=0; $index < $campaigns_count; $index++) {
if ( $index < $campaigns_count-1) {
$first_date = !empty($campaigns[$index]['start_at']) ? strtotime($campaigns[$index]['start_at']) : null;
$second_date = !empty($campaigns[$index+1]['start_at']) ? strtotime($campaigns[$index+1]['start_at']) : null;
if ( $first_date && $second_date ) {
$total_interval = $total_interval + ( $first_date-$second_date );
}
}
}
$avg_sending_interval = $total_interval / $campaigns_count;
}
return $avg_sending_interval;
}
public static function get( $args = array() ) {
global $wpbd;
$fields = ! empty( $args['fields'] ) ? $args['fields'] : array();
$date_query = ! empty( $args['date_query'] ) ? $args['date_query'] : array();
$campaign_types = ! empty( $args['campaign_types'] ) ? $args['campaign_types'] : array();
$statuses = ! empty( $args['statuses'] ) ? $args['statuses' ] : array();
$orders = ! empty( $args['orders'] ) ? $args['orders']: array();
$select = 'SELECT';
if ( ! empty( $fields ) ) {
$select .= ' ' . implode( ', ', $fields );
} else {
$select .= ' * ';
}
$from = "FROM `{$wpbd->prefix}ig_mailing_queue`";
$wheres = array();
if ( ! empty( $campaign_types ) ) {
$campaign_type_where = array();
foreach ( $campaign_types as $campaign_type ) {
$regex = '.*"type";s:[0-9]+:"' . $campaign_type . '".*';
$campaign_type_where[] = $wpbd->prepare( '`meta` REGEXP %s', $regex );
}
$campaign_type_where = implode( ' OR ', array_unique( $campaign_type_where ) );
$wheres[] = 'AND ( ' . $campaign_type_where . ' )';
}
if ( ! empty( $statuses ) ) {
$status_count = count( $statuses );
$status_placeholders = array_fill( 0, $status_count, '%s' );
$wheres[] .= $wpbd->prepare( 'AND status IN( ' . implode( ',', $status_placeholders ) . ' )', $statuses );
}
if ( ! empty( $date_query ) ) {
foreach ( $date_query as $query ) {
if ( ! empty( $query['column'] ) && ! empty( $query['operator'] ) && ! empty( $query['value'] ) ) {
$query_column = $query['column'];
$query_operator = $query['operator'];
$query_value = $query['value'];
$wheres[] = $wpbd->prepare( 'AND ' . $query_column . ' ' . $query_operator . ' %s', $query_value );
}
}
}
$where = '';
if ( ! empty( $wheres ) ) {
$where = 'WHERE 1=1 ' . implode( "\n ", array_unique( $wheres ) );
}
$order = '';
if ( ! empty( $orders ) ) {
$order = 'ORDER BY ' . implode( ', ', array_unique( $orders ) );
}
$query = $select . ' ' . $from . ' ' . $where . ' ' . $order;
if ( count( $fields ) === 1 ) {
$result = $wpbd->get_var(
$query
);
} else {
$result = $wpbd->get_results(
$query,
ARRAY_A
);
}
return $result;
}
}

View File

@@ -0,0 +1,60 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ES_DB_Notifications {
public $table_name;
public $version;
public $primary_key;
public function __construct() {
}
/**
* Migrate Post Notification Email Template Type
*
* @return bool|int
*
* @since 4.0.0
*/
public static function migrate_post_notification_es_template_type() {
global $wpdb;
$update = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}postmeta SET meta_value = %s WHERE meta_key = %s AND meta_value = %s",
array( 'post_notification', 'es_template_type', 'Post Notification' )
)
);
return $update;
}
/**
* Migrate Newsletter Email template type
*
* @return bool|int
*
* @since 4.0.0
*/
public static function migrate_newsletter_es_template_type() {
global $wpdb;
$update = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}postmeta SET meta_value = %s WHERE meta_key = %s AND meta_value = %s",
array( 'newsletter', 'es_template_type', 'Newsletter' )
)
);
return $update;
}
}

View File

@@ -0,0 +1,74 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* ES_DB_Queue class
*
* @since 4.2.1
*/
class ES_DB_Queue extends ES_DB {
/**
* Table name
*
* @since 4.2.1
* @var $table_name
*/
public $table_name;
/**
* Table DB version
*
* @since 4.2.1
* @var $version
*/
public $version;
/**
* Table primary key column name
*
* @since 4.2.1
* @var $primary_key
*/
public $primary_key;
/**
* ES_DB constructor.
*
* @since 4.0.0
*/
public function __construct() {
global $wpdb;
parent::__construct();
$this->table_name = $wpdb->prefix . 'ig_queue';
$this->version = '1.0';
}
/**
* Delete from queue based on campaign_id & contact_id
*
* @param $campaign_id
* @param $contact_id
*
* @return bool|int
*
* @since 4.2.1
*/
public function delete_from_queue( $campaign_id, $contact_id ) {
global $wpdb;
return $wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->prefix}ig_queue WHERE campaign_id = %d AND contact_id = %d",
$campaign_id,
$contact_id
)
);
}
}

View File

@@ -0,0 +1,926 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ES_DB_Sending_Queue {
public $table_name;
public $version;
public $primary_key;
public function __construct() {
}
public static function get_columns() {
return array(
'id' => '%d',
'mailing_queue_id' => '%d',
'mailing_queue_hash' => '%s',
'campaign_id' => '%d',
'contact_id' => '%d',
'contact_hash' => '%s',
'email' => '%s',
'status' => '%s',
'links' => '%s',
'opened' => '%d',
'sent_at' => '%s',
'opened_at' => '%s',
);
}
public static function get_column_defaults() {
return array(
'mailing_queue_id' => 0,
'mailing_queue_hash' => '',
'campaign_id' => 0,
'contact_id' => 0,
'contact_hash' => '',
'email' => '',
'status' => '',
'links' => '',
'opened' => 0,
'sent_at' => null,
'opened_at' => null,
);
}
public static function get_emails_to_be_sent_by_hash( $guid, $limit ) {
global $wpbd;
$where = apply_filters( 'ig_es_get_emails_to_be_sent_by_hash_condition', 'AND 1=1' );
$subscribers = $wpbd->get_results(
$wpbd->prepare(
"SELECT * FROM {$wpbd->prefix}ig_sending_queue WHERE status IN( %s, %s, %s ) AND mailing_queue_hash = %s $where ORDER BY FIELD(`status`, %s, %s, %s), id LIMIT 0, %d",
array(
IG_ES_SENDING_QUEUE_STATUS_QUEUED,
IG_ES_SENDING_QUEUE_STATUS_SENDING,
IG_ES_SENDING_QUEUE_STATUS_FAILED,
$guid,
IG_ES_SENDING_QUEUE_STATUS_QUEUED,
IG_ES_SENDING_QUEUE_STATUS_SENDING,
IG_ES_SENDING_QUEUE_STATUS_FAILED,
$limit,
)
),
ARRAY_A
);
return $subscribers;
}
/**
* Get contact queued emails
*
* @param int $contact_id
*
* @return array $queued_emails
*
* @since 4.7.6
*/
public static function get_queued_emails( $contact_id ) {
global $wpdb;
$queued_emails = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ig_sending_queue WHERE contact_id = %d AND status = %s",
$contact_id,
'In Queue'
),
ARRAY_A
);
return $queued_emails;
}
public static function update_sent_status( $contact_ids, $message_id = 0, $status = 'Sent' ) {
global $wpbd;
$updated = false;
if ( 0 == $message_id ) {
return $updated;
}
$id_str = '';
if ( is_array( $contact_ids ) && count( $contact_ids ) > 0 ) {
$id_str = implode( ',', $contact_ids );
} elseif ( is_string( $contact_ids ) ) {
$id_str = $contact_ids;
}
if ( ! empty( $id_str ) ) {
if ( 'Sent' === $status ) {
$current_time = ig_get_current_date_time();
$updated = $wpbd->query(
$wpbd->prepare(
"UPDATE {$wpbd->prefix}ig_sending_queue SET status = %s, sent_at = %s WHERE mailing_queue_id = %d AND contact_id IN($id_str)",
$status,
$current_time,
$message_id
)
);
} else {
$updated = $wpbd->query(
$wpbd->prepare(
"UPDATE {$wpbd->prefix}ig_sending_queue SET status = %s WHERE mailing_queue_id = %d AND contact_id IN($id_str)",
$status,
$message_id
)
);
}
}
return $updated;
}
/* count cron email */
public static function get_total_emails_to_be_sent_by_hash( $notification_hash = '', $statuses = array() ) {
global $wpbd;
$result = 0;
if ( ! empty( $notification_hash ) ) {
$params = array( $notification_hash );
if ( empty( $statuses ) ) {
$statuses = array(
IG_ES_SENDING_QUEUE_STATUS_QUEUED,
IG_ES_SENDING_QUEUE_STATUS_SENDING,
IG_ES_SENDING_QUEUE_STATUS_FAILED,
);
}
$statuses_count = count( $statuses );
$statuses_placeholders = array_fill( 0, $statuses_count, '%s' );
$params = array_merge( $params, $statuses );
$result = $wpbd->get_var(
$wpbd->prepare(
"SELECT COUNT(*) AS count FROM {$wpbd->prefix}ig_sending_queue WHERE mailing_queue_hash = %s AND status IN ( " . implode( ',', $statuses_placeholders ) . ' )',
$params
)
);
}
return $result;
}
public static function get_total_emails_to_be_sent() {
global $wpdb;
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) AS count FROM {$wpdb->prefix}ig_sending_queue
WHERE
`mailing_queue_id` IN( SELECT id FROM {$wpdb->prefix}ig_mailing_queue WHERE status IN( %s, %s ) )
AND
status IN ( %s, %s, %s )",
array(
IG_ES_MAILING_QUEUE_STATUS_QUEUED,
IG_ES_MAILING_QUEUE_STATUS_SENDING,
IG_ES_SENDING_QUEUE_STATUS_QUEUED,
IG_ES_SENDING_QUEUE_STATUS_SENDING,
IG_ES_SENDING_QUEUE_STATUS_FAILED
)
)
);
return $result;
}
public static function get_total_emails_sent_by_hash( $notification_hash ) {
global $wpdb;
$result = 0;
if ( '' != $notification_hash ) {
$sent_status = IG_ES_SENDING_QUEUE_STATUS_SENT;
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) AS count FROM {$wpdb->prefix}ig_sending_queue WHERE mailing_queue_hash = %s AND status = %s",
array( $notification_hash, $sent_status )
)
);
}
return $result;
}
public static function get_emails_by_hash( $notification_hash ) {
global $wpdb;
$emails = array();
if ( '' != $notification_hash ) {
$emails = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ig_sending_queue WHERE mailing_queue_hash = %s",
array( $notification_hash )
),
ARRAY_A
);
// We are not migrating reports data because it caused lots of migration issues
// in the past. So, we are fetching reports data from older table if we don't get
// the data from the new table.
// This is generally fetch the data for older campaigns
if ( count( $emails ) == 0 ) {
$result = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s ', $wpdb->prefix . 'es_deliverreport' ) );
if ( $result === $wpdb->prefix . 'es_deliverreport' ) {
$emails = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}es_deliverreport WHERE es_deliver_sentguid = %s",
array( $notification_hash )
),
ARRAY_A
);
}
}
}
return $emails;
}
public static function do_batch_insert( $delivery_data ) {
$status = ! empty( $delivery_data['status'] ) ? $delivery_data['status'] : 'In Queue';
$data['mailing_queue_id'] = $delivery_data['mailing_queue_id'];
$data['mailing_queue_hash'] = $delivery_data['hash'];
$data['campaign_id'] = $delivery_data['campaign_id'];
$data['status'] = $status;
$columns = self::get_columns();
unset( $columns['id'] );
$fields = array_keys( $columns );
$batches = array_chunk( $delivery_data['subscribers'], 50 );
$emails = array();
foreach ( $batches as $key => $batch ) {
$place_holders = array();
$values = array();
foreach ( $batch as $subscriber ) {
$email = ! empty( $subscriber['email'] ) ? $subscriber['email'] : '';
$contact_id = ! empty( $subscriber['id'] ) ? $subscriber['id'] : 0;
if ( ! empty( $email ) && ! in_array( $email, $emails ) ) {
$emails[] = $email;
$data['contact_id'] = $contact_id;
$data['email'] = $email;
$data['contact_hash'] = $subscriber['hash'];
$data = wp_parse_args( $data, self::get_column_defaults() );
$formats = array();
foreach ( $columns as $column => $format ) {
$values[] = $data[ $column ];
$formats[] = $format;
}
$place_holders[] = '( ' . implode( ', ', $formats ) . ' )';
}
}
ES_DB::do_insert( IG_SENDING_QUEUE_TABLE, $fields, $place_holders, $values );
}
return true;
}
public static function do_insert( $place_holders, $values ) {
global $wpbd;
$delivery_reports_table = IG_SENDING_QUEUE_TABLE;
$query = "INSERT INTO $delivery_reports_table (`mailing_queue_id`, `mailing_queue_hash`, `campaign_id`, `contact_id`, `contact_hash`, `email`, `status`, `links`, `opened`, `sent_at`, `opened_at`) VALUES ";
$query .= implode( ', ', $place_holders );
$sql = $wpbd->prepare( "$query ", $values );
if ( $wpbd->query( $sql ) ) {
return true;
} else {
return false;
}
}
/**
* Method to queue emails from contact table.
*
* @param int $mailing_queue_id Mailing queue ID.
* @param string $mailing_queue_hash Mailing Hash.
* @param int $campaign_id Campaign ID.
* @param array|string $list_ids List IDs seperated by commas if string i.e. '1,2,3' or array( 1, 2, 3 ) if array.
*
* @return bool $emails_queued Emails queued or not.
*
* @since 4.6.4
*/
public static function queue_emails( $mailing_queue_id = 0, $mailing_queue_hash = '', $campaign_id = 0, $list_ids = array() ) {
global $wpbd;
$emails_queued = false;
if ( empty( $mailing_queue_id ) || empty( $mailing_queue_hash ) || empty( $campaign_id ) ) {
return $emails_queued;
}
$column_defaults = self::get_column_defaults();
$queue_status = 'In Queue';
$queue_links = isset( $column_defaults['links'] ) ? $column_defaults['links'] : '';
$queue_opened = isset( $column_defaults['opened'] ) ? $column_defaults['opened'] : 0;
$queue_sent_at = isset( $column_defaults['sent_at'] ) ? $column_defaults['sent_at'] : null;
$queue_opened_at = isset( $column_defaults['opened_at'] ) ? $column_defaults['opened_at'] : null;
$campaign = ES()->campaigns_db->get( $campaign_id );
$args = array(
'select' => array( 'subscribers.id' ),
'lists' => $list_ids,
'return_sql' => true, // This flag will return the required sql query
'orderby' => array( 'id' ),
'status' => 'subscribed',
'subscriber_status' => array( 'verified' ),
);
$schedule_base_time = current_time( 'mysql' );
$scheduled_utc_time = current_time( 'mysql', true );
$current_utc_time = current_time( 'mysql', true );
$send_immediately = true;
if ( ! empty( $campaign ) && ! empty( $campaign['meta'] ) ) {
$campaign_meta = maybe_unserialize( $campaign['meta'] );
if ( ! empty( $campaign_meta['list_conditions'] ) ) {
$args['conditions'] = $campaign_meta['list_conditions'];
}
if ( isset( $campaign_meta['scheduling_option'] ) && isset( $campaign_meta['date'] ) && isset( $campaign_meta['es_schedule_date'] ) && isset( $campaign_meta['es_schedule_time'] ) && 'schedule_later' == $campaign_meta['scheduling_option'] ) {
$schedule_base_time = gmdate( 'Y-m-d G:i:s', strtotime( "{$campaign_meta['es_schedule_date']} {$campaign_meta['es_schedule_time']}" ) );
$scheduled_utc_time = $campaign_meta['date'];
$send_immediately = false;
}
}
$query = new IG_ES_Subscribers_Query();
$sql_query = $query->run( $args );
$query_args = array(
$mailing_queue_id,
$mailing_queue_hash,
$campaign_id,
$queue_status,
$queue_links,
$queue_opened,
$queue_sent_at,
$queue_opened_at,
);
$send_at_query = '';
if ($send_immediately) {
$send_at_query = $wpbd->prepare('%s AS `send_at`', array($current_utc_time));
} else {
if ( ES()->is_pro() ) {
$is_optimzation_enabled = ES_Email_Send_Time_Optimizer::is_optimizer_enabled();
if ( $is_optimzation_enabled ) {
$optimization_method = ES_Email_Send_Time_Optimizer::get_optimization_method();
if ( 'subscriber_average_open_time' === $optimization_method ) {
$send_at_query = $wpbd->prepare('
CASE
WHEN `average_opened_at` IS null THEN %s
ELSE
CASE
WHEN DATE_ADD(DATE(%s), INTERVAL `average_opened_at` HOUR_SECOND) < %s THEN DATE_ADD(DATE_ADD(DATE(%s),INTERVAL `average_opened_at` HOUR_SECOND), INTERVAL 86400 SECOND)
ELSE DATE_ADD(DATE(%s),INTERVAL `average_opened_at` HOUR_SECOND)
END
END AS `send_at`
', array($scheduled_utc_time,$schedule_base_time,$current_utc_time,$schedule_base_time,$schedule_base_time));
} else {
$send_at_query = $wpbd->prepare("
CASE
WHEN `timezone` IS null THEN %s
ELSE
CASE
WHEN CONVERT_TZ(%s,`timezone`,'+0:00') < %s THEN DATE_ADD(CONVERT_TZ(%s,`timezone`,'+0:00'), INTERVAL 86400 SECOND)
ELSE CONVERT_TZ(%s,`timezone`,'+0:00')
END
END AS `send_at`
", array($scheduled_utc_time,$schedule_base_time,$current_utc_time,$schedule_base_time,$schedule_base_time));
}
}
}
}
if ( empty( $send_at_query ) ) {
$send_at_query = $wpbd->prepare('%s AS `send_at`', array($current_utc_time));
}
$query = $wpbd->prepare(
"SELECT
%d AS `mailing_queue_id`,
%s AS `mailing_queue_hash`,
%d AS `campaign_id`,
MAX(`ig_contacts`.`id`) AS `contact_id`,
MAX(`ig_contacts`.`hash`) AS `contact_hash`,
`ig_contacts`.`email` AS `email`,
%s AS `status`,
%s AS `links`,
%d AS `opened`,
%s AS `sent_at`,
%s AS `opened_at`,
{$send_at_query}
FROM `{$wpbd->prefix}ig_contacts` AS `ig_contacts`
WHERE id IN ( " . $sql_query . ')
GROUP BY `ig_contacts`.`email`
ORDER BY `ig_contacts`.`email`',
$query_args
);
$total_contacts_added = $wpbd->query(
$wpbd->prepare(
"INSERT INTO `{$wpbd->prefix}ig_sending_queue`
(
`mailing_queue_id`,
`mailing_queue_hash`,
`campaign_id`,
`contact_id`,
`contact_hash`,
`email`,
`status`,
`links`,
`opened`,
`sent_at`,
`opened_at`,
`send_at`
)
SELECT
%d AS `mailing_queue_id`,
%s AS `mailing_queue_hash`,
%d AS `campaign_id`,
MAX(`ig_contacts`.`id`) AS `contact_id`,
MAX(`ig_contacts`.`hash`) AS `contact_hash`,
`ig_contacts`.`email` AS `email`,
%s AS `status`,
%s AS `links`,
%d AS `opened`,
%s AS `sent_at`,
%s AS `opened_at`,
{$send_at_query}
FROM `{$wpbd->prefix}ig_contacts` AS `ig_contacts`
WHERE id IN ( " . $sql_query . ')
GROUP BY `ig_contacts`.`email`
ORDER BY `ig_contacts`.`email`',
$query_args
)
);
// Check if contacts added.
if ( ! empty( $total_contacts_added ) ) {
$emails_queued = true;
} else {
// Delete report if there aren't any emails queued.
ES_DB_Mailing_Queue::delete_notifications( array( $mailing_queue_id ) );
if ( empty( $list_ids ) ) {
return false;
}
// If some how above sql query fails then queue emails using old approach.
// i.e. Preparing data for insert query in PHP and then doing insert.
// Converto to an array if already not an array.
if ( ! is_array( $list_ids ) ) {
$list_ids = explode( ',', $list_ids );
}
$subscribers = ES()->contacts_db->get_active_contacts_by_list_id( $list_ids );
$total_contacts_added = count( $subscribers );
if ( $total_contacts_added > 0 ) {
// Add entry into sending queue table
$delivery_data = array();
$delivery_data['hash'] = $mailing_queue_hash;
$delivery_data['status'] = $queue_status;
$delivery_data['subscribers'] = $subscribers;
$delivery_data['campaign_id'] = $campaign_id;
$delivery_data['mailing_queue_id'] = $mailing_queue_id;
$emails_queued = self::do_batch_insert( $delivery_data );
}
}
if ( $emails_queued ) {
$data = array(
'count' => $total_contacts_added,
'status' => IG_ES_MAILING_QUEUE_STATUS_QUEUED
);
ES_DB_Mailing_Queue::update_mailing_queue( $mailing_queue_id, $data );
}
return $emails_queued;
}
public static function update_viewed_status( $guid = '', $email = '', $message_id = 0 ) {
global $wpdb;
$current_date = ig_get_current_date_time();
return $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}ig_sending_queue SET opened_at = %s, opened = %d WHERE (mailing_queue_id = %d OR mailing_queue_hash = %s ) AND email = %s",
$current_date,
1,
$message_id,
$guid,
$email
)
);
}
/*
* Commenting for now as we might need this function in the future.
public static function migrate_reports_data() {
global $wpdb;
$mailing_queue_details = ES_DB_Mailing_Queue::get_id_details_map();
$email_details = ES()->contacts_db->get_email_details_map();
$query = 'SELECT count(*) as total FROM ' . EMAIL_SUBSCRIBERS_STATS_TABLE;
$total = $wpdb->get_var( $query );
if ( $total > 0 ) {
$columns = self::get_columns();
unset( $columns['id'] );
$fields = array_keys( $columns );
$batch_size = IG_DEFAULT_BATCH_SIZE;
$total_bataches = ( $total > IG_DEFAULT_BATCH_SIZE ) ? ceil( $total / $batch_size ) : 1;
$last_sending_queue_batch_run = get_transient( 'ig_es_last_sending_queue_batch_run' . false );
if ( false === $last_sending_queue_batch_run ) {
$batch_start_from = 0;
} else {
$batch_start_from = $last_sending_queue_batch_run + 1;
}
$logger = get_ig_logger();
$logger->info( 'Sending Queue Start From: ' . $batch_start_from, array( 'source' => 'es_update' ) );
for ( $i = $batch_start_from; $i < $total_bataches; $i ++ ) {
if ( false === get_transient( 'ig_es_running_migration_for_' . $i ) ) {
set_transient( 'ig_es_running_migration_for_' . $i, true, 300 );
$batch_start = $i * $batch_size;
$query = 'SELECT * FROM ' . EMAIL_SUBSCRIBERS_STATS_TABLE . " LIMIT {$batch_start}, {$batch_size}";
$results = $wpdb->get_results( $query, ARRAY_A );
$values = array();
$data = array();
$place_holders = array();
foreach ( $results as $key => $result ) {
$email = $result['es_deliver_emailmail'];
$is_opened = ( '0000-00-00 00:00:00' != $result['es_deliver_viewdate'] ) ? 1 : 0;
$contact_id = 0;
$hash = '';
if ( isset( $email_details[ $email ] ) ) {
$contact_id = $email_details[ $email ]['id'];
$hash = $email_details[ $email ]['hash'];
}
$mailing_queue_id = ! empty( $mailing_queue_details[ $result['es_deliver_sentguid'] ] ) ? $mailing_queue_details[ $result['es_deliver_sentguid'] ]['id'] : 0;
$start_at = ! empty( $mailing_queue_details[ $result['es_deliver_sentguid'] ] ) ? $mailing_queue_details[ $result['es_deliver_sentguid'] ]['start_at'] : '0000-00-00 00:00:00';
$data['mailing_queue_id'] = $mailing_queue_id;
$data['mailing_queue_hash'] = $result['es_deliver_sentguid'];
$data['contact_id'] = $contact_id;
$data['contact_hash'] = $hash;
$data['email'] = $email;
$data['status'] = $result['es_deliver_sentstatus'];
$data['opened'] = $is_opened;
$data['sent_at'] = $start_at;
$data['opened_at'] = $result['es_deliver_viewdate'];
$data = wp_parse_args( $data, self::get_column_defaults() );
$formats = array();
foreach ( $columns as $column => $format ) {
$values[] = $data[ $column ];
$formats[] = $format;
}
$place_holders[] = '( ' . implode( ', ', $formats ) . ' )';
}
$logger->info( '------------------[Running.....]: ' . $i, array( 'source' => 'es_update' ) );
ES_DB::do_insert( IG_SENDING_QUEUE_TABLE, $fields, $place_holders, $values );
delete_transient( 'ig_es_running_migration_for_' . $i );
$logger->info( '------------------[last_sending_queue_batch_run]: ' . $i, array( 'source' => 'es_update' ) );
set_transient( 'ig_es_last_sending_queue_batch_run', $i, MINUTE_IN_SECONDS * 100 );
}
}
}
}
*/
/*
public static function migrate_reports_data() {
global $wpdb;
$mailing_queue_details = ES_DB_Mailing_Queue::get_id_details_map();
$email_details = ES()->contacts_db->get_email_details_map();
$query = "SELECT count(*) as total FROM " . EMAIL_SUBSCRIBERS_STATS_TABLE;
$total = $wpdb->get_var( $query );
if ( $total > 0 ) {
$columns = self::get_columns();
unset( $columns['id'] );
$fields = array_keys( $columns );
$batch_size = IG_DEFAULT_BATCH_SIZE;
$total_bataches = ( $total > IG_DEFAULT_BATCH_SIZE ) ? ceil( $total / $batch_size ) : 1;
$logger = get_ig_logger();
for ( $i = 0; $i < $total_bataches; $i ++ ) {
if(false === get_transient('running_reports_migration_for')) {
set_transient( 'running_reports_migration_for', true, 300 );
$batch_start = 0;
$query = "SELECT * FROM " . EMAIL_SUBSCRIBERS_STATS_TABLE . " LIMIT {$batch_start}, {$batch_size}";
$results = $wpdb->get_results( $query, ARRAY_A );
$values = $data = $place_holders = array();
$es_delivery_ids = array();
foreach ( $results as $key => $result ) {
$es_delivery_ids[] = $result['es_deliver_id'];
$email = $result['es_deliver_emailmail'];
$is_opened = ( $result['es_deliver_viewdate'] != '0000-00-00 00:00:00' ) ? 1 : 0;
$contact_id = 0;
$hash = '';
if ( isset( $email_details[ $email ] ) ) {
$contact_id = $email_details[ $email ]['id'];
$hash = $email_details[ $email ]['hash'];
}
$mailing_queue_id = ! empty( $mailing_queue_details[ $result['es_deliver_sentguid'] ] ) ? $mailing_queue_details[ $result['es_deliver_sentguid'] ]['id'] : 0;
$start_at = ! empty( $mailing_queue_details[ $result['es_deliver_sentguid'] ] ) ? $mailing_queue_details[ $result['es_deliver_sentguid'] ]['start_at'] : '0000-00-00 00:00:00';
$data['mailing_queue_id'] = $mailing_queue_id;
$data['mailing_queue_hash'] = $result['es_deliver_sentguid'];
$data['contact_id'] = $contact_id;
$data['contact_hash'] = $hash;
$data['email'] = $email;
$data['status'] = $result['es_deliver_sentstatus'];
$data['opened'] = $is_opened;
$data['sent_at'] = $start_at;
$data['opened_at'] = $result['es_deliver_viewdate'];
$data = wp_parse_args( $data, self::get_column_defaults() );
$formats = array();
foreach ( $columns as $column => $format ) {
$values[] = $data[ $column ];
$formats[] = $format;
}
$place_holders[] = "( " . implode( ', ', $formats ) . " )";
}
$logger->info( '------------------[Running.....]: ' . $i, array( 'source' => 'es_update' ) );
ES_DB::do_insert( IG_SENDING_QUEUE_TABLE, $fields, $place_holders, $values );
$logger->info( '------------------[Deleting Records]: ', array( 'source' => 'es_update' ) );
self::delete_records_from_delivereport( $es_delivery_ids );
$logger->info( '------------------[Deleted]: ' . print_r($es_delivery_ids, true), array( 'source' => 'es_update' ) );
delete_transient( 'running_reports_migration_for' );
}
}
}
}
*/
public static function delete_records_from_delivereport( $ids ) {
global $wpbd;
$delivereport_ids = implode( ',', array_map( 'absint', $ids ) );
$query = 'DELETE FROM ' . EMAIL_SUBSCRIBERS_STATS_TABLE . " WHERE es_deliver_id IN ($delivereport_ids)";
$wpbd->query( $query );
}
public static function delete_by_mailing_queue_id( $mailing_queue_ids ) {
global $wpbd;
if ( ! empty( $mailing_queue_ids ) ) {
$mailing_queue_ids = esc_sql( $mailing_queue_ids );
$mailing_queue_ids = implode( ',', array_map( 'absint', $mailing_queue_ids ) );
$wpbd->query(
"DELETE FROM {$wpbd->prefix}ig_sending_queue WHERE mailing_queue_id IN ($mailing_queue_ids)"
);
}
}
// Query to get total viewed emails per report
public static function get_viewed_count_by_hash( $hash = '' ) {
global $wpdb;
$result = 0;
if ( '' !== $hash ) {
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) AS count FROM {$wpdb->prefix}ig_sending_queue WHERE opened = 1 AND mailing_queue_hash = %s",
array( $hash )
)
);
}
return $result;
}
public static function get_total_email_count_by_hash( $hash = '' ) {
global $wpdb;
$result = 0;
if ( '' != $hash ) {
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) AS count FROM {$wpdb->prefix}ig_sending_queue WHERE mailing_queue_hash = %s",
array( $hash )
)
);
if ( 0 == $result ) {
$es_deliver_report_table = EMAIL_SUBSCRIBERS_STATS_TABLE;
$table_name = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'es_deliverreport' ) );
if ( $table_name === $es_deliver_report_table ) {
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) AS count FROM {$wpdb->prefix}es_deliverreport WHERE es_deliver_sentguid = %s",
array( $hash )
)
);
}
}
}
return $result;
}
/**
* Get Total Opened emails count based on $days
*
* @param int $days
*
* @return string|null
*
* @since 4.3.2
*/
public static function get_total_contacts_opened_emails( $days = 0 ) {
global $wpbd;
$ig_sending_queue_table = IG_SENDING_QUEUE_TABLE;
$query = "SELECT COUNT(DISTINCT(`contact_id`)) FROM $ig_sending_queue_table WHERE `opened` = %d";
$args[] = 1;
if ( 0 != $days ) {
$days = esc_sql( $days );
$where = ' AND opened_at >= DATE_SUB(NOW(), INTERVAL %d DAY)';
$query .= $where;
$args[] = $days;
}
return $wpbd->get_var( $wpbd->prepare( $query, $args ) );
}
/**
* Get Email => ID map based on Sending Queue table
*
* @param int $campaign_id
* @param array $emails
*
* @return array
*
* @since 4.3.7
*/
public static function get_emails_id_map_by_campaign( $campaign_id = 0, $message_id = 0, $emails = array() ) {
global $wpbd;
$emails = esc_sql( $emails );
$campaign_id = esc_sql( absint( $campaign_id ) );
if ( 0 === $campaign_id || empty( $emails ) ) {
return array();
}
$emails_str = "'" . implode( "','", $emails ) . "'";
$results = $wpbd->get_results(
$wpbd->prepare(
"SELECT contact_id, email FROM {$wpbd->prefix}ig_sending_queue WHERE campaign_id = %d AND mailing_queue_id = %d AND email IN($emails_str)",
$campaign_id,
$message_id
),
ARRAY_A
);
$emails_id_map = array();
if ( count( $results ) > 0 ) {
foreach ( $results as $result ) {
$emails_id_map[ $result['email'] ] = $result['contact_id'];
}
}
return $emails_id_map;
}
/**
* Get Queue data
*
* @return array
*
* @since 4.6.12
*/
public static function get_queue_data( $campaign_id = '', $message_id = '' ) {
global $wpdb;
$emails = array();
if ( ! empty( $message_id ) && ! empty( $campaign_id ) ) {
$emails = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ig_sending_queue WHERE campaign_id = %d AND mailing_queue_id = %d AND contact_id NOT IN ( SELECT contact_id FROM {$wpdb->prefix}ig_actions WHERE campaign_id = %d AND message_id = %d )",
array( $campaign_id, $message_id, $campaign_id, $message_id )
),
ARRAY_A
);
}
return $emails;
}
/**
* Delete contacts from the sending queue
*
* @param array $contact_ids
* @param int $message_id
*
* @since 4.7.6
*/
public static function delete_contacts( $contact_ids, $message_id ) {
global $wpbd;
if ( ! empty( $contact_ids ) && ! empty( $message_id ) ) {
$ids_count = count( $contact_ids );
$ids_placeholders = array_fill( 0, $ids_count, '%d' );
$query_args = $contact_ids;
$query_args[] = $message_id;
$wpbd->query(
$wpbd->prepare(
"DELETE FROM {$wpbd->prefix}ig_sending_queue WHERE contact_id IN( " . implode( ',', $ids_placeholders ) . ' ) AND mailing_queue_id = %d',
$query_args
)
);
}
}
}

View File

@@ -0,0 +1,978 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* ES_DB base class
*
* @since 4.0
*/
abstract class ES_DB {
/**
* Table name
*
* @since 4.0.0
* @var $table_name
*/
public $table_name;
/**
* Table DB version
*
* @since 4.0.0
* @var $version
*/
public $version;
/**
* Table primary key column name
*
* @since 4.0.0
* @var $primary_key
*/
public $primary_key;
/**
* ES_DB constructor.
*
* @since 4.0.0
*/
public function __construct() {
}
/**
* Get default columns
*
* @return array
*
* @since 4.0.0
*/
public function get_columns() {
return array();
}
/**
* Get columns default values
*
* @return array
*
* @since 4.0.0
*/
public function get_column_defaults() {
return array();
}
/**
* Retrieve a row by the primary key
*
* @param int $row_id
* @param string $output
* @param false $use_cache
*
* @return false|mixed
*
* @since 4.0.0
*
* @modified 4.7.3 Added support to retrieve data from cache
*/
public function get( $row_id = 0, $output = ARRAY_A, $use_cache = false ) {
global $wpbd;
$query = $wpbd->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id );
if ( true === $use_cache ) {
// Considering $output also while considering key generation
$cache_key = $this->generate_cache_key( $query . $output );
$found = false;
$result = $this->get_cache( $cache_key, $found );
if ( ! $found ) {
$result = $wpbd->get_row( $query, $output );
$this->set_cache( $cache_key, $result );
}
} else {
$result = $wpbd->get_row( $query, $output );
}
return $result;
}
/**
* Retrieve a row by a specific column / value
*
* @param $column
* @param $row_id
* @param string $output
* @param false $use_cache
*
* @return false|mixed
*
* @since 4.0.0
*
* @modified 4.7.3 Added support to retrieve data from cache
*/
public function get_by( $column, $row_id, $output = ARRAY_A, $use_cache = false ) {
global $wpbd;
$column = esc_sql( $column );
$query = $wpbd->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1;", $row_id );
if ( true === $use_cache ) {
// Consider $output also while generating cache key
$cache_key = $this->generate_cache_key( $query . $output );
$found = false;
$result = $this->get_cache( $cache_key, $found );
if ( ! $found ) {
$result = $wpbd->get_row( $query, $output );
$this->set_cache( $cache_key, $result );
}
} else {
$result = $wpbd->get_row( $query, $output );
}
return $result;
}
/**
* Get rows by conditions
*
* @param string $where
* @param string $output
* @param false $use_cache
*
* @return false|mixed
*
* @since 4.2.4
*
* @modfiy 4.7.3 Added support to retrieve data from cache
*/
public function get_by_conditions( $where = '', $output = ARRAY_A, $use_cache = false, $order_by_column = '', $order = '' ) {
global $wpbd;
$query = "SELECT * FROM $this->table_name";
if ( ! empty( $where ) ) {
$query .= " WHERE $where";
}
if ( ! empty( $order_by_column ) ) {
$query .= ' ORDER BY ' . esc_sql( $order_by_column );
if ( ! empty( $order ) ) {
$query .= ' ' . esc_sql( $order );
}
}
if ( true === $use_cache ) {
// Consider $output also while generating cache key
$cache_key = $this->generate_cache_key( $query . $output );
$found = false;
$result = $this->get_cache( $cache_key, $found );
if ( ! $found ) {
$result = $wpbd->get_results( $query, $output );
$this->set_cache( $cache_key, $result );
}
} else {
$result = $wpbd->get_results( $query, $output );
}
return $result;
}
/**
* Get all data from table without any condition
*
* @return array|object|null
*
* @since 4.3.1
*/
public function get_all() {
return $this->get_by_conditions();
}
/**
* Retrieve a specific column's value by the primary key
*
* @param string $column
* @param int $row_id
* @param bool $use_cache
*
* @return null|string|array
*
* @since 4.0.0
*
* @since 4.7.3
*/
public function get_column( $column = '', $row_id = 0, $use_cache = false ) {
global $wpbd;
$column = esc_sql( $column );
if ( $row_id ) {
$query = $wpbd->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id );
} else {
$query = "SELECT $column FROM $this->table_name";
}
if ( true === $use_cache ) {
$cache_key = $this->generate_cache_key( $query );
$found = false;
$result = $this->get_cache( $cache_key, $found );
if ( ! $found ) {
if ( $row_id ) {
$result = $wpbd->get_var( $query );
} else {
$result = $wpbd->get_col( $query );
}
$this->set_cache( $cache_key, $result );
}
} else {
if ( $row_id ) {
$result = $wpbd->get_var( $query );
} else {
$result = $wpbd->get_col( $query );
}
}
return $result;
}
/**
* Retrieve a specific column's value by the the specified column / value
*
* @param string $column
* @param string $column_where
* @param string $column_value
* @param bool $only_one
* @param bool $use_cache
*
* @return array|string|null
*
* @since 4.0.0
*
* @modified 4.3.4 Added support to retrieve whole column
*
* @modified 4.7.3 Added support to retrieve data from cache
*/
public function get_column_by( $column = '', $column_where = '', $column_value = '', $only_one = true, $use_cache = false ) {
global $wpbd;
$column_where = esc_sql( $column_where );
$column = esc_sql( $column );
if ( $only_one ) {
$query = $wpbd->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;", $column_value );
} else {
$query = $wpbd->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s;", $column_value );
}
if ( true === $use_cache ) {
$cache_key = $this->generate_cache_key( $query );
$found = false;
$result = $this->get_cache( $cache_key, $found );
if ( ! $found ) {
if ( $only_one ) {
$result = $wpbd->get_var( $query );
} else {
$result = $wpbd->get_col( $query );
}
$this->set_cache( $cache_key, $result );
}
} else {
if ( $only_one ) {
$result = $wpbd->get_var( $query );
} else {
$result = $wpbd->get_col( $query );
}
}
return $result;
}
/**
* Get column based on where condition
*
* @param string $column
* @param string $where
* @param bool $use_cache
*
* @return array
*
* @since 4.3.5
*
* @modified 4.7.3 Added support to retrieve data from cache
*/
public function get_column_by_condition( $column = '', $where = '', $use_cache = false ) {
global $wpbd;
$column = esc_sql( $column );
$query = "SELECT $column FROM $this->table_name";
if ( ! empty( $where ) ) {
$query .= " WHERE $where";
}
if ( true === $use_cache ) {
$cache_key = $this->generate_cache_key( $query );
$found = false;
$result = $this->get_cache( $cache_key, $found );
if ( ! $found ) {
$result = $wpbd->get_col( $query );
$this->set_cache( $cache_key, $result );
}
} else {
$result = $wpbd->get_col( $query );
}
return $result;
}
/**
* Select few columns based on condition
*
* @param array $columns
* @param string $where
* @param string $output
* @param bool $use_cache
*
* @return array|object|null
*
* @since 4.3.5
*
* @modified 4.7.3 Added support to retrieve data from cache
*/
public function get_columns_by_condition( $columns = array(), $where = '', $output = ARRAY_A, $use_cache = false ) {
global $wpbd;
if ( ! is_array( $columns ) ) {
return array();
}
$columns = esc_sql( $columns );
$columns = implode( ', ', $columns );
$query = "SELECT $columns FROM $this->table_name";
if ( ! empty( $where ) ) {
$query .= " WHERE $where";
}
if ( true === $use_cache ) {
// Consider $output also while generating cache key
$cache_key = $this->generate_cache_key( $query . $output );
$found = false;
$result = $this->get_cache( $cache_key, $found );
if ( ! $found ) {
$result = $wpbd->get_results( $query, $output );
$this->set_cache( $cache_key, $result );
}
} else {
$result = $wpbd->get_results( $query, $output );
}
return $result;
}
/**
* Insert a new row
*
* @param $data
* @param string $type
*
* @return int
*
* @since 4.0.0
*/
public function insert( $data, $type = '' ) {
global $wpdb;
// Set default values
$data = wp_parse_args( $data, $this->get_column_defaults() );
do_action( 'ig_es_pre_insert_' . $type, $data );
// Initialise column format array
$column_formats = $this->get_columns();
// Force fields to lower case
$data = array_change_key_case( $data );
// White list columns
$data = array_intersect_key( $data, $column_formats );
// Reorder $column_formats to match the order of columns given in $data
$data_keys = array_keys( $data );
$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
$wpdb->insert( $this->table_name, $data, $column_formats );
$wpdb_insert_id = $wpdb->insert_id;
do_action( 'ig_es_post_insert_' . $type, $wpdb_insert_id, $data );
return $wpdb_insert_id;
}
/**
* Update a specific row
*
* @param $row_id
* @param array $data
* @param string $where
*
* @return bool
*
* @since 4.0.0
*/
public function update( $row_id, $data = array(), $where = '' ) {
global $wpdb;
// Row ID must be positive integer
$row_id = absint( $row_id );
if ( empty( $row_id ) ) {
return false;
}
if ( empty( $where ) ) {
$where = $this->primary_key;
}
// Initialise column format array
$column_formats = $this->get_columns();
// Force fields to lower case
$data = array_change_key_case( $data );
// White list columns
$data = array_intersect_key( $data, $column_formats );
// Reorder $column_formats to match the order of columns given in $data
$data_keys = array_keys( $data );
$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) {
return false;
}
return true;
}
/**
* Delete a row by primary key
*
* @param int $row_id
*
* @return bool
*
* @since 4.0.0
*/
public function delete( $row_id = 0 ) {
global $wpbd;
// Row ID must be positive integer
$row_id = absint( $row_id );
if ( empty( $row_id ) ) {
return false;
}
$where = $wpbd->prepare( "$this->primary_key = %d", $row_id );
if ( false === $this->delete_by_condition( $where ) ) {
return false;
}
return true;
}
/**
* Delete rows by primary key
*
* @param array $row_ids
*
* @return bool
*
* @since 4.3.4
*/
public function bulk_delete( $row_ids = array() ) {
if ( ! is_array( $row_ids ) && empty( $row_ids ) ) {
return false;
}
$row_ids_str = $this->prepare_for_in_query( $row_ids );
$where = "$this->primary_key IN( $row_ids_str )";
if ( false === $this->delete_by_condition( $where ) ) {
return false;
}
return true;
}
/**
* Delete records based on $where
*
* @param string $where
*
* @return bool
*
* @since 4.2.4
*/
public function delete_by_condition( $where = '' ) {
global $wpbd;
if ( empty( $where ) ) {
return false;
}
$query = "DELETE FROM $this->table_name WHERE $where";
if ( false === $wpbd->query( "DELETE FROM $this->table_name WHERE $where" ) ) {
return false;
}
return true;
}
/**
* Check whether table exists or not
*
* @param $table
*
* @return bool
*
* @since 4.0.0
*/
public function table_exists( $table ) {
global $wpdb;
$table = sanitize_text_field( $table );
return $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table;
}
/**
* Check whether table installed
*
* @return bool
*
* @since 4.0.0
*/
public function installed() {
return $this->table_exists( $this->table_name );
}
/**
* Get total count
*
* @param string $where
* @param bool $use_cache
*
* @return string|null
*
* @since 4.2.1
*
* @modified 4.7.3 Added support to retrieve data from cache
*/
public function count( $where = '', $use_cache = false ) {
global $wpbd;
$query = "SELECT count(*) FROM $this->table_name";
if ( ! empty( $where ) ) {
$query .= " WHERE $where";
}
if ( true === $use_cache ) {
$cache_key = $this->generate_cache_key( $query );
$found = false;
$result = $this->get_cache( $cache_key, $found );
if ( ! $found ) {
$result = $wpbd->get_var( $query );
$this->set_cache( $cache_key, $result );
}
} else {
$result = $wpbd->get_var( $query );
}
return $result;
}
/**
* Insert data into bulk
*
* @param array $values
* @param int $length
* @param bool $return_insert_ids
*
* @since 4.2.1
*
* @since 4.3.5 Fixed issues and started using it.
*/
public function bulk_insert( $values = array(), $length = 100, $return_insert_ids = false ) {
global $wpbd;
if ( ! is_array( $values ) ) {
return false;
}
// Get the first value from an array to check data structure
$first_value = array_slice( $values, 0, 1 );
$data = array_shift( $first_value );
// Set default values
$data = wp_parse_args( $data, $this->get_column_defaults() );
// Initialise column format array
$column_formats = $this->get_columns();
// Remove primary key as we don't require while inserting data
unset( $column_formats[ $this->primary_key ] );
// Force fields to lower case
$data = array_change_key_case( $data );
// White list columns
$data = array_intersect_key( $data, $column_formats );
// Reorder $column_formats to match the order of columns given in $data
$data = wp_parse_args( $data, $this->get_column_defaults() );
$data_keys = array_keys( $data );
$fields = array_keys( array_merge( array_flip( $data_keys ), $column_formats ) );
// Convert Batches into smaller chunk
$batches = array_chunk( $values, $length );
$error_flag = false;
// Holds first and last row ids of each batch insert
$bulk_rows_start_end_ids = [];
foreach ( $batches as $key => $batch ) {
$place_holders = array();
$final_values = array();
$fields_str = '';
foreach ( $batch as $value ) {
$formats = array();
foreach ( $column_formats as $column => $format ) {
$final_values[] = isset( $value[ $column ] ) ? $value[ $column ] : $data[ $column ]; // set default if we don't have
$formats[] = $format;
}
$place_holders[] = '( ' . implode( ', ', $formats ) . ' )';
$fields_str = '`' . implode( '`, `', $fields ) . '`';
}
$query = "INSERT INTO $this->table_name ({$fields_str}) VALUES ";
$query .= implode( ', ', $place_holders );
$sql = $wpbd->prepare( $query, $final_values );
if ( ! $wpbd->query( $sql ) ) {
$error_flag = true;
} else {
$start_id = $wpbd->insert_id;
$end_id = ( $start_id - 1 ) + count( $batch );
array_push( $bulk_rows_start_end_ids, $start_id );
array_push( $bulk_rows_start_end_ids, $end_id );
}
}
if ( $return_insert_ids && count( $bulk_rows_start_end_ids ) > 0 ) {
return array( min( $bulk_rows_start_end_ids ), max( $bulk_rows_start_end_ids ) );
}
// Check if error occured during executing the query.
if ( $error_flag ) {
return false;
}
return true;
}
/**
* Update data into bulk
*
* @param array $values
* @param int $length
*
*/
public function bulk_update( $values = array(), $length = 100 ) {
global $wpbd;
if ( ! is_array( $values ) ) {
return false;
}
// Get the first value from an array to check data structure
$first_value = array_slice( $values, 0, 1 );
$data = array_shift( $first_value );
// Set default values
$data = wp_parse_args( $data, $this->get_column_defaults() );
// Initialise column format array
$column_formats = $this->get_columns();
// Remove primary key as we don't require while inserting data
unset( $column_formats[ $this->primary_key ] );
// Convert Batches into smaller chunk
$batches = array_chunk( $values, $length );
foreach ( $batches as $key => $batch ) {
$formats = array();
$cases = array();
foreach ( $column_formats as $column => $format ) {
$formats[] = $format;
$when = array();
foreach ( $batch as $value ) {
if ( empty( $value[$column] ) ) {
continue;
}
$when[] = $wpbd->prepare( ' WHEN email = "' . $value['email'] . '" THEN ' . "$format", $value[$column] );
$emails[] = $value['email'];
}
if ( !empty( $when ) ) {
$case = $column . '=(CASE ' . implode( ' ', $when ) . ' END)';
$cases[] = $case;
}
}
$query = "UPDATE {$wpbd->prefix}ig_contacts SET " . implode( ' , ', $cases );
$query .= 'WHERE email IN( \'' . implode( '\',\'', $emails ) . '\')';
$result = $wpbd->query( $query );
return $result;
}
}
/**
* Bulk insert data into given table
*
* @param $table_name
* @param $fields
* @param $place_holders
* @param $values
*
* @return bool
*/
public static function do_insert( $table_name, $fields, $place_holders, $values ) {
global $wpbd;
$fields_str = '`' . implode( '`, `', $fields ) . '`';
$query = "INSERT INTO $table_name ({$fields_str}) VALUES ";
$query .= implode( ', ', $place_holders );
$sql = $wpbd->prepare( $query, $values );
if ( $wpbd->query( $sql ) ) {
return true;
} else {
return false;
}
}
/**
* Get ID, Name Map
*
* @param string $where
*
* @return array
*
* @since 4.2.2
* @since 4.3.5 Used get_columns_map method
*/
public function get_id_name_map( $where = '' ) {
return $this->get_columns_map( $this->primary_key, 'name', $where );
}
/**
* Get map of two columns
*
* E.g array($column_1 => $column_2)
*
* @param string $column_1
* @param string $column_2
* @param string $where
*
* @return array
*
* @since 4.3.5
*/
public function get_columns_map( $column_1 = '', $column_2 = '', $where = '' ) {
if ( empty( $column_1 ) || empty( $column_2 ) ) {
return array();
}
$columns = array( $column_1, $column_2 );
$results = $this->get_columns_by_condition( $columns, $where );
$map = array();
if ( count( $results ) > 0 ) {
foreach ( $results as $result ) {
$map[ $result[ $column_1 ] ] = $result[ $column_2 ];
}
}
return $map;
}
public static function prepare_data( $data, $column_formats, $column_defaults, $insert = true ) {
// Set default values
if ( $insert ) {
$data = wp_parse_args( $data, $column_defaults );
}
// Force fields to lower case
$data = array_change_key_case( $data );
// White list columns
$data = array_intersect_key( $data, $column_formats );
// Reorder $column_formats to match the order of columns given in $data
$data_keys = array_keys( $data );
$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
return array(
'data' => $data,
'column_formats' => $column_formats,
);
}
/**
* Prepare string for SQL IN query
*
* @param array $array
*
* @return string
*
* @since 4.3.4
*/
public function prepare_for_in_query( $array = array() ) {
$array = esc_sql( $array );
if ( is_array( $array ) && count( $array ) > 0 ) {
return "'" . implode( "', '", $array ) . "'";
}
return '';
}
/**
* Generate Cache key
*
* @param string $str
*
* @return bool|string
*
* @since 4.7.2
*/
public function generate_cache_key( $str = '' ) {
return ES_Cache::generate_key( $str );
}
/**
* Get data from cache
*
* @param $key
* @param null $found
*
* @return false|mixed
*
* @since 4.7.2
*/
public function get_cache( $key, &$found = null ) {
return ES_Cache::get( $key, 'query', false, $found );
}
/**
* Set data into cache
*
* @param string $key
* @param string $value
*
* @since 4.7.2
*/
public function set_cache( $key = '', $value = '' ) {
if ( ! empty( $key ) ) {
ES_Cache::set( $key, $value, 'query' );
}
}
/**
* Check if cache exists?
*
* @param string $key
*
* @return bool
*
* @since 4.7.2
*/
public function cache_exists( $key = '' ) {
return ES_Cache::is_exists( $key, 'query' );
}
}

View File

@@ -0,0 +1,174 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class IG_ES_DB_Unsubscribe_Feedback extends ES_DB {
/**
* Table Name
*
* @since 4.6.8
* @var $table_name
*/
public $table_name;
/**
* Version
*
* @since 4.6.8
* @var $version
*/
public $version;
/**
* Primary Key
*
* @since 4.6.8
* @var $primary_key
*/
public $primary_key;
/**
* IG_ES_DB_Unsubscribe_Feedback constructor.
*
* @since 4.6.8
*/
public function __construct() {
global $wpdb;
parent::__construct();
$this->table_name = $wpdb->prefix . 'ig_unsubscribe_feedback';
$this->primary_key = 'id';
$this->version = '1.0';
}
/**
* Get table columns
*
* @return array
*
* @since 4.6.8
*/
public function get_columns() {
return array(
'id' => '%d',
'contact_id' => '%d',
'list_id' => '%d',
'campaign_id' => '%d',
'mailing_queue_id' => '%d',
'feedback_slug' => '%s',
'feedback_text' => '%s',
'created_at' => '%s',
'updated_at' => '%s',
'meta' => '%s',
);
}
/**
* Get default column values
*
* @return array Default column values
*
* @since 4.6.8
*/
public function get_column_defaults() {
return array(
'contact_id' => 0,
'list_id' => 0,
'campaign_id' => 0,
'mailing_queue_id' => 0,
'feedback_slug' => '',
'feedback_text' => '',
'created_at' => ig_get_current_date_time(),
'updated_at' => ig_get_current_date_time(),
'meta' => '',
);
}
/**
* Add feedback into database
*
* @param array $feedback_data Feedback data.
*
* @return int
*/
public function insert_feedback( $feedback_data = array() ) {
if ( empty( $feedback_data ) || ! is_array( $feedback_data ) ) {
return 0;
}
return $this->insert( $feedback_data );
}
/**
* Update feedback
*
* @param int $feedback_id Feedback ID.
* @param array $feedback_data Feedback data.
*
* @return bool|void
*/
public function update_feedback( $feedback_id = 0, $feedback_data = array() ) {
if ( empty( $feedback_id ) || empty( $feedback_data ) || ! is_array( $feedback_data ) ) {
return;
}
return $this->update( $feedback_id, $feedback_data );
}
/**
* Get existing feedback for given contact and list id
*
* @param int $contact_id Contact id
* @param int $list_id List id
*
* @return int $existing_feedback_id Existing feedback ID
*/
public function get_existing_feedback_id( $contact_id = 0, $list_id = 0 ) {
global $wpdb;
$existing_feedback_id = 0;
if ( empty( $contact_id ) || empty( $list_id ) ) {
return $existing_feedback_id;
}
$existing_feedback_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT id FROM {$wpdb->prefix}ig_unsubscribe_feedback WHERE `contact_id`= %d AND `list_id` = %d ORDER BY updated_at DESC LIMIT 1",
$contact_id,
$list_id
)
);
return $existing_feedback_id;
}
/**
* Get feedback count for given number of days
*
* @param int $number_of_days
*
* @return int $feedback_counts
*/
public static function get_feedback_counts( $number_of_days ) {
global $wpdb;
$feedback_counts = $wpdb->get_results(
$wpdb->prepare(
"SELECT feedback_slug, COUNT(feedback_slug) AS feedback_count FROM `{$wpdb->prefix}ig_unsubscribe_feedback` WHERE `updated_at` >= DATE_SUB(NOW(), INTERVAL %d DAY) GROUP BY `feedback_slug` ORDER BY feedback_count DESC"
, $number_of_days
),
ARRAY_A
);
return $feedback_counts;
}
}

View File

@@ -0,0 +1,208 @@
<?php
/**
* Workflow Queue DB
*
* @since 4.4.1
* @version 1.0
* @package Email Subscribers
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* IG_ES_DB_WC_Cart class
*
* @since 4.6.5
*/
class IG_ES_DB_WC_Cart extends ES_DB {
/**
* Workflow queue table name
*
* @since 4.6.5
* @var $table_name
*/
public $table_name;
/**
* Workflow queue table version
*
* @since 4.6.5
* @var $version
*/
public $version;
/**
* Workflow queue table primary key
*
* @since 4.6.5
* @var $primary_key
*/
public $primary_key;
/**
* IG_ES_DB_WC_Cart constructor.
*
* @since 4.6.5
*/
public function __construct() {
global $wpdb;
parent::__construct();
$this->table_name = $wpdb->prefix . 'ig_wc_cart';
$this->primary_key = 'id';
$this->version = '1.0';
}
/**
* Returns workflow queue table's columns
*
* @since 4.6.5
*
* @return array workflow queue table columns
*/
public function get_columns() {
return array(
'id' => '%d',
'status' => '%s',
'user_id' => '%d',
'guest_id' => '%d',
'last_modified' => '%s',
'created' => '%s',
'items' => '%s',
'coupons' => '%s',
'fees' => '%s',
'shipping_tax_total' => '%f',
'shipping_total' => '%f',
'total' => '%f',
'token' => '%s',
'currency' => '%s',
);
}
/**
* Returns default values for workflow columns
*
* @since 4.6.5
*
* @return array default values for workflow columns
*/
public function get_column_defaults() {
return array(
'id' => 0,
'status' => '',
'user_id' => 0,
'guest_id' => 0,
'last_modified' => '',
'created' => '',
'items' => '',
'coupons' => '',
'fees' => '',
'shipping_tax_total' => 0,
'shipping_total' => 0,
'total' => 0,
'token' => '',
'currency' => '',
);
}
/**
* Get workflows based on arguements
*
* @param array $query_args Query arguements.
* @param string $output Output format.
* @param boolean $do_count_only Count only flag.
*
* @return mixed $result Query result
*
* @since 4.6.5
*/
public function get_carts( $query_args = array(), $output = ARRAY_A, $do_count_only = false ) {
global $wpdb, $wpbd;
if ( $do_count_only ) {
$sql = 'SELECT count(*) as total FROM ' . $wpdb->prefix . 'ig_wc_cart';
} else {
$sql = 'SELECT ';
if ( ! empty( $query_args['fields'] ) && is_array( $query_args['fields'] ) ) {
$sql .= implode( ' ,', $query_args['fields'] );
} else {
$sql .= '*';
}
$sql .= ' FROM ' . $wpdb->prefix . 'ig_wc_cart';
}
$args = array();
$query = array();
if ( ! empty( $query_args['ids'] ) ) {
$ids_count = count( $query_args['ids'] );
$ids_placeholders = array_fill( 0, $ids_count, '%d' );
$query[] = ' id IN( ' . implode( ',', $ids_placeholders ) . ' )';
$args = array_merge( $args, $query_args['ids'] );
}
if ( isset( $query_args['status'] ) ) {
$query[] = ' status = %s ';
$args[] = $query_args['status'];
}
if ( isset( $query_args['last_modified'] ) ) {
$query[] = ' last_modified <= %s ';
$args[] = $query_args['last_modified'];
}
$query = apply_filters( 'ig_es_wc_cart_where_caluse', $query );
if ( count( $query ) > 0 ) {
$sql .= ' WHERE ';
$sql .= implode( ' AND ', $query );
if ( count( $args ) > 0 ) {
$sql = $wpbd->prepare( $sql, $args ); // phpcs:ignore
}
}
if ( ! $do_count_only ) {
$order = ! empty( $query_args['order'] ) ? strtolower( $query_args['order'] ) : 'desc';
$expected_order_values = array( 'asc', 'desc' );
if ( ! in_array( $order, $expected_order_values, true ) ) {
$order = 'desc';
}
$default_order_by = esc_sql( 'created' );
$expected_order_by_values = array( 'created' );
if ( empty( $query_args['order_by'] ) || ! in_array( $query_args['order_by'], $expected_order_by_values, true ) ) {
$order_by_clause = " ORDER BY {$default_order_by} DESC";
} else {
$order_by = esc_sql( $query_args['order_by'] );
$order_by_clause = " ORDER BY {$order_by} {$order}, {$default_order_by} DESC";
}
$sql .= $order_by_clause;
if ( ! empty( $query_args['per_page'] ) ) {
$sql .= ' LIMIT ' . $query_args['per_page'];
if ( ! empty( $query_args['page_number'] ) ) {
$sql .= ' OFFSET ' . ( $query_args['page_number'] - 1 ) * $query_args['per_page'];
}
}
$result = $wpbd->get_results( $sql, $output ); // phpcs:ignore
} else {
$result = $wpbd->get_var( $sql ); // phpcs:ignore
}
return $result;
}
}

View File

@@ -0,0 +1,240 @@
<?php
/**
* Guest Queue DB
*
* @since 4.4.1
* @version 1.0
* @package Email Subscribers
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* IG_ES_DB_WC_Guest class
*
* @since 4.6.5
*/
class IG_ES_DB_WC_Guest extends ES_DB {
/**
* Guest queue table name
*
* @since 4.6.5
* @var $table_name
*/
public $table_name;
/**
* Guest queue table version
*
* @since 4.6.5
* @var $version
*/
public $version;
/**
* Guest queue table primary key
*
* @since 4.6.5
* @var $primary_key
*/
public $primary_key;
/**
* IG_ES_DB_WC_Guest constructor.
*
* @since 4.6.5
*/
public function __construct() {
global $wpdb;
parent::__construct();
$this->table_name = $wpdb->prefix . 'ig_wc_guests';
$this->primary_key = 'id';
$this->version = '1.0';
}
/**
* Returns Guest queue table's columns
*
* @since 4.6.5
*
* @return array Guest queue table columns
*/
public function get_columns() {
return array(
'id' => '%d',
'email' => '%s',
'tracking_key' => '%s',
'created' => '%s',
'last_active' => '%s',
'language' => '%s',
'most_recent_order' => '%d',
'version' => '%d',
'meta' => '%s',
);
}
/**
* Returns default values for Guest columns
*
* @since 4.6.5
*
* @return array default values for Guest columns
*/
public function get_column_defaults() {
return array(
'id' => 0,
'email' => '',
'tracking_key' => '',
'created' => '',
'last_active' => '',
'language' => '',
'most_recent_order' => 0,
'version' => 0,
'meta' => '',
);
}
/**
* Get Guests based on arguements
*
* @param array $query_args Query arguements.
* @param string $output Output format.
* @param boolean $do_count_only Count only flag.
*
* @return mixed $result Query result
*
* @since 4.6.5
*/
public function get_carts( $query_args = array(), $output = ARRAY_A, $do_count_only = false ) {
global $wpdb, $wpbd;
if ( $do_count_only ) {
$sql = 'SELECT count(*) as total FROM ' . $wpdb->prefix . 'ig_wc_cart';
} else {
$sql = 'SELECT ';
if ( ! empty( $query_args['fields'] ) && is_array( $query_args['fields'] ) ) {
$sql .= implode( ' ,', $query_args['fields'] );
} else {
$sql .= '*';
}
$sql .= ' FROM ' . $wpdb->prefix . 'ig_wc_cart';
}
$args = array();
$query = array();
if ( ! empty( $query_args['ids'] ) ) {
$ids_count = count( $query_args['ids'] );
$ids_placeholders = array_fill( 0, $ids_count, '%d' );
$query[] = ' id IN( ' . implode( ',', $ids_placeholders ) . ' )';
$args = array_merge( $args, $query_args['ids'] );
}
if ( isset( $query_args['status'] ) ) {
$query[] = ' status = %s ';
$args[] = $query_args['status'];
}
if ( isset( $query_args['last_modified'] ) ) {
$query[] = ' last_modified <= %s ';
$args[] = $query_args['last_modified'];
}
$query = apply_filters( 'ig_es_wc_cart_where_caluse', $query );
if ( count( $query ) > 0 ) {
$sql .= ' WHERE ';
$sql .= implode( ' AND ', $query );
if ( count( $args ) > 0 ) {
$sql = $wpbd->prepare( $sql, $args ); // phpcs:ignore
}
}
if ( ! $do_count_only ) {
$order = ! empty( $query_args['order'] ) ? strtolower( $query_args['order'] ) : 'desc';
$expected_order_values = array( 'asc', 'desc' );
if ( ! in_array( $order, $expected_order_values, true ) ) {
$order = 'desc';
}
$default_order_by = esc_sql( 'created' );
$expected_order_by_values = array( 'created' );
if ( empty( $query_args['order_by'] ) || ! in_array( $query_args['order_by'], $expected_order_by_values, true ) ) {
$order_by_clause = " ORDER BY {$default_order_by} DESC";
} else {
$order_by = esc_sql( $query_args['order_by'] );
$order_by_clause = " ORDER BY {$order_by} {$order}, {$default_order_by} DESC";
}
$sql .= $order_by_clause;
if ( ! empty( $query_args['per_page'] ) ) {
$sql .= ' LIMIT ' . $query_args['per_page'];
if ( ! empty( $query_args['page_number'] ) ) {
$sql .= ' OFFSET ' . ( $query_args['page_number'] - 1 ) * $query_args['per_page'];
}
}
$result = $wpbd->get_results( $sql, $output ); // phpcs:ignore
} else {
$result = $wpbd->get_var( $sql ); // phpcs:ignore
}
return $result;
}
/**
* Update meta value
*
* @since 4.4.1
*
* @param int $queue_id Queue ID.
* @param array $meta_data Meta data to update.
*
* @return bool|false|int
*/
public function update_meta( $queue_id = 0, $meta_data = array() ) {
$update = false;
if ( ! empty( $queue_id ) && ! empty( $meta_data ) ) {
$queue = $this->get( $queue_id );
if ( ! empty( $queue ) ) {
if ( isset( $queue['meta'] ) ) {
$meta = maybe_unserialize( $queue['meta'] );
// If $meta is an empty or isn't an array, then convert it to an array before adding data to it.
if ( empty( $meta ) || ! is_array( $meta ) ) {
$meta = array();
}
foreach ( $meta_data as $meta_key => $meta_value ) {
$meta[ $meta_key ] = $meta_value;
}
$queue['meta'] = maybe_serialize( $meta );
$update = $this->update( $queue_id, $queue );
}
}
}
return $update;
}
}