1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
<?php
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Wcap_Templates_Table extends WP_List_Table {
public $per_page = 30;
public $base_url;
public $total_count;
public function __construct() {
parent::__construct( array(
'singular' => __( 'template_id', 'woocommerce-ac' ),
'plural' => __( 'template_ids', 'woocommerce-ac' ),
'ajax' => false
) );
$this->wcap_get_templates_count();
$this->process_bulk_action();
$this->base_url = admin_url( 'admin.php?page=woocommerce_ac_page&action=cart_recovery§ion=emailtemplates' );
}
public function wcap_templates_prepare_items() {
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->wcap_templates_get_sortable_columns();
$data = $this->wcap_templates_data();
$this->_column_headers = array( $columns, $hidden, $sortable);
$total_items = $this->total_count;
$this->items = $data;
$this->set_pagination_args( array(
'total_items' => $total_items,
'per_page' => $this->per_page,
'total_pages' => ceil( $total_items / $this->per_page )
)
);
}
public function get_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'sr' => __( 'Sr', 'woocommerce-ac' ),
'template_name' => __( 'Name Of Template', 'woocommerce-ac' ),
'sent_time' => __( 'Sent After Set Time', 'woocommerce-ac' ),
'template_filter' => __( 'Send To Segment(s)', 'woocommerce-ac' ),
'email_sent' => __( 'Number of Emails Sent', 'woocommerce-ac' ),
'percentage_recovery' => __( 'Percent Recovery', 'woocommerce-ac' ),
'activate' => __( 'Start Sending', 'woocommerce-ac' )
);
return apply_filters( 'wcap_templates_columns', $columns );
}
function column_cb( $item ){
$template_id = '';
if( isset( $item->id ) && "" != $item->id ){
$template_id = $item->id;
}
return sprintf(
'<input type="checkbox" name="%1$s[]" value="%2$s" />',
'template_id',
$template_id
);
}
public function wcap_templates_get_sortable_columns() {
$columns = array(
'template_name' => array( 'template_name', false ),
'sent_time' => array( 'sent_time',false),
);
return apply_filters( 'wcap_templates_sortable_columns', $columns );
}
public function column_template_name( $template_row_info ) {
$row_actions = array();
$value = '';
$template_id = 0;
if( isset( $template_row_info->template_name ) ) {
$template_id = $template_row_info->id ;
$row_actions['edit'] = '<a href="' . wp_nonce_url( add_query_arg( array( 'action' => 'cart_recovery', 'section' => 'emailtemplates', 'mode'=>'edittemplate', 'id' => $template_row_info->id ), $this->base_url ), 'abandoned_order_nonce') . '">' . __( 'Edit', 'woocommerce-ac' ) . '</a>';
$row_actions['copy'] = '<a href="' . wp_nonce_url( add_query_arg( array( 'action' => 'cart_recovery', 'section' => 'emailtemplates', 'mode'=>'copytemplate', 'id' => $template_row_info->id ), $this->base_url ), 'abandoned_order_nonce') . '">' . __( 'Duplicate', 'woocommerce-ac' ) . '</a>';
$row_actions['delete'] = '<a href="' . wp_nonce_url( add_query_arg( array( 'action' => 'wcap_delete_template', 'template_id' => $template_row_info->id ), $this->base_url ), 'abandoned_order_nonce') . '">' . __( 'Delete', 'woocommerce-ac' ) . '</a>';
$email = $template_row_info->template_name;
$value = $email . $this->row_actions( $row_actions );
}
return apply_filters( 'wcap_template_single_column', $value, $template_id, 'email' );
}
public function wcap_get_templates_count() {
global $wpdb;
$this->total_count = $wpdb->get_var( "SELECT COUNT(`id`) FROM `" .WCAP_EMAIL_TEMPLATE_TABLE."`" );
}
public function wcap_templates_data() {
global $wpdb;
$return_templates_data = array();
$per_page = $this->per_page;
$results = array();
$wcap_get_decimal = wc_get_price_decimals();
$query = "SELECT id, is_active, frequency, day_or_hour, wc_template_filter, template_name
FROM `" . WCAP_EMAIL_TEMPLATE_TABLE . "` ORDER BY day_or_hour asc , frequency asc";
$results = $wpdb->get_results( $query );
$i = 0;
$minute_seconds = 60;
$hour_seconds = 3600;
$day_seconds = 86400;
$time_to_send_template_after = '';
foreach( $results as $key => $value ) {
$return_templates_data[ $i ] = new stdClass();
$id = $value->id;
$query_no_emails = "SELECT COUNT(`id`) FROM " . WCAP_EMAIL_SENT_HISTORY_TABLE . " WHERE template_id= %d ";
$ac_emails_count = $wpdb->get_var( $wpdb->prepare( $query_no_emails, $id ) );
$query_no_recovers_test = "SELECT COUNT(`id`) FROM " . WCAP_EMAIL_SENT_HISTORY_TABLE . " WHERE recovered_order = '1' AND template_id = %d ";
$wcap_number_of_time_recover = $wpdb->get_var( $wpdb->prepare( $query_no_recovers_test, $id ) );
$is_active = $value->is_active;
$frequency = $value->frequency;
$day_or_hour = $value->day_or_hour;
if( 'Minutes' == $value->day_or_hour ) {
$time_to_send_template_after = $value->frequency * $minute_seconds;
} else if( 'Days' == $value->day_or_hour ) {
$time_to_send_template_after = $value->frequency * $day_seconds;
} else if( 'Hours' == $value->day_or_hour ) {
$time_to_send_template_after = $value->frequency * $hour_seconds;
}
$wcap_recover_ratio = 0;
if ( $ac_emails_count != 0 ) {
$wcap_recover_ratio = $wcap_number_of_time_recover / $ac_emails_count * 100;
}
$template_filter = str_replace( ",", ", ", $value->wc_template_filter );
$return_templates_data[ $i ]->sr = $i + 1;
$return_templates_data[ $i ]->id = $id;
$return_templates_data[ $i ]->template_name = $value->template_name;
$return_templates_data[ $i ]->sent_time = __( $frequency . " " . $day_or_hour . " After Abandonment", 'woocommerce-ac' );
$return_templates_data[ $i ]->template_time = $time_to_send_template_after;
$return_templates_data[ $i ]->template_filter = $template_filter;
$return_templates_data[ $i ]->email_sent = __( ( string )$ac_emails_count, 'woocommerce-ac' );
$return_templates_data[ $i ]->percentage_recovery = round ( $wcap_recover_ratio , $wcap_get_decimal )."%";
$return_templates_data[ $i ]->is_active = $is_active;
$i++;
}
if( isset( $_GET['orderby'] ) && $_GET['orderby'] == 'template_name' ) {
if( isset( $_GET['order'] ) && $_GET['order'] == 'asc') {
usort( $return_templates_data, array( __CLASS__ ,"wcap_class_template_name_asc") );
} else {
usort( $return_templates_data, array( __CLASS__ ,"wcap_class_template_name_dsc") );
}
}
else if( isset( $_GET['orderby']) && $_GET['orderby'] == 'sent_time' ) {
if( isset( $_GET['order'] ) && $_GET['order'] == 'asc' ) {
usort( $return_templates_data, array( __CLASS__ ,"wcap_class_sent_time_asc" ) );
} else {
usort( $return_templates_data, array( __CLASS__ ,"wcap_class_sent_time_dsc" ) );
}
}
if ( isset( $_GET['paged'] ) && $_GET['paged'] > 1 ) {
$page_number = $_GET['paged'] - 1;
$k = $per_page * $page_number;
}else {
$k = 0;
}
$return_templates_display = array();
for ( $j = $k; $j < ( $k + $per_page ); $j++ ) {
if ( isset( $return_templates_data[ $j ] ) ) {
$return_templates_display[ $j ] = $return_templates_data[ $j ];
}else {
break;
}
}
return apply_filters( 'wcap_templates_table_data', $return_templates_display );
}
function wcap_class_template_name_asc($value1,$value2) {
return strcasecmp($value1->template_name,$value2->template_name );
}
function wcap_class_template_name_dsc ($value1,$value2) {
return strcasecmp($value2->template_name,$value1->template_name );
}
function wcap_class_sent_time_asc($value1,$value2) {
return strnatcasecmp($value1->template_time,$value2->template_time );
}
function wcap_class_sent_time_dsc ($value1,$value2) {
return strnatcasecmp($value2->template_time,$value1->template_time );
}
public function column_default( $wcap_abandoned_orders, $column_name ) {
$value = '';
switch ( $column_name ) {
case 'activate' :
if( isset( $wcap_abandoned_orders->is_active ) ) {
$id = $wcap_abandoned_orders->id;
$is_active = $wcap_abandoned_orders->is_active;
$active = '';
if( $is_active == '1' ) {
$active = "on";
} else {
$active = "off";
}
$active_text = __( $active, 'woocommerce-ac' );
$value = '<button type="button" class="wcap-switch wcap-toggle-template-status" '
. 'wcap-template-id="'. $id .'" '
. 'wcap-template-switch="'. ( $active ) . '">'
. $active_text . '</button>';
}
break;
case 'template_filter' :
if( isset( $wcap_abandoned_orders->template_filter ) && '' != $wcap_abandoned_orders->template_filter) {
$value = $wcap_abandoned_orders->template_filter;
}else{
$value = 'All';
}
break;
default:
$value = isset( $wcap_abandoned_orders->$column_name ) ? $wcap_abandoned_orders->$column_name : '';
break;
}
return apply_filters( 'wcap_template_column_default', $value, $wcap_abandoned_orders, $column_name );
}
public function get_bulk_actions() {
return array(
'wcap_delete_template' => __( 'Delete', 'woocommerce-ac' )
);
}
}
?>