| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
* Plugin Name: Uji Countdown |
| 5 |
* Plugin URI: http://www.wpmanage.com/uji-countdown/ |
| 6 |
* Description: HTML5 Countdown. |
| 7 |
* Version: 2.0 |
| 8 |
* Author: WPmanage <info@wpmanage.com> |
| 9 |
* Author URI: http://www.wpmanage.com |
| 10 |
*/ |
| 11 |
|
| 12 |
require_once(ABSPATH . 'wp-admin/includes/template.php' ); |
| 13 |
|
| 14 |
class_exists('WP_Screen') || require_once( ABSPATH . 'wp-admin/includes/screen.php' ); |
| 15 |
|
| 16 |
class_exists( 'WP_List_Table', false) || require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 17 |
|
| 18 |
class UjiSubscriptionsTable extends WP_List_Table |
| 19 |
{ |
| 20 |
private $itemsPerPage = 10; |
| 21 |
|
| 22 |
public function __construct() |
| 23 |
{ |
| 24 |
|
| 25 |
parent::__construct( array( |
| 26 |
'singular' => __( 'subscriber', 'uji-countdown' ), |
| 27 |
'plural' => __( 'subscribers', 'uji-countdown' ), |
| 28 |
'ajax' => false, |
| 29 |
'screen' => 'subscribers-list' |
| 30 |
) ); |
| 31 |
|
| 32 |
$this->process_bulk_action(); |
| 33 |
$this->prepare_items(); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
function process_bulk_action() |
| 38 |
{ |
| 39 |
global $wpdb; |
| 40 |
if( 'delete' === $this->current_action()) |
| 41 |
{ |
| 42 |
if(!empty($_GET['subscriber'])) |
| 43 |
foreach ($_GET['subscriber'] as $subscriberId) { |
| 44 |
if (empty($subscriberId)) continue; |
| 45 |
$wpdb->delete(self::getSubscriptionsTableName(), array('Id' => $subscriberId), array('%d')); |
| 46 |
} |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if('cvs-export' === $this->current_action()) |
| 51 |
{ |
| 52 |
global $wpdb; |
| 53 |
$arrSubscribers = $wpdb->get_results('SELECT * FROM ' . self::getSubscriptionsTableName(), ARRAY_A); |
| 54 |
if(empty($arrSubscribers)) |
| 55 |
return; |
| 56 |
|
| 57 |
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate"); |
| 58 |
header('Content-Description: File Transfer'); |
| 59 |
header("Content-Transfer-Encoding: UTF-8"); |
| 60 |
header("Content-type: text/csv"); |
| 61 |
header("Content-Disposition: attachment; filename=subscribers.csv"); |
| 62 |
header("Content-Transfer-Encoding: binary"); |
| 63 |
header("Expires: 0"); |
| 64 |
header("Pragma: no-cache"); |
| 65 |
|
| 66 |
$fileHandler = @fopen('php://output', 'w'); |
| 67 |
if(false === $fileHandler) |
| 68 |
return; |
| 69 |
|
| 70 |
$arrHeaderColumns = array('EmailAddress', 'List', 'CreatedDate'); |
| 71 |
fputcsv($fileHandler, array('EmailAddress', 'Campaign', 'CreatedDate')); |
| 72 |
|
| 73 |
foreach ($arrSubscribers as $subscriberInfo) |
| 74 |
{ |
| 75 |
foreach($subscriberInfo as $info => $value) |
| 76 |
{ |
| 77 |
if(in_array($info, $arrHeaderColumns)) |
| 78 |
continue; |
| 79 |
unset($subscriberInfo[$info]); |
| 80 |
} |
| 81 |
|
| 82 |
fputcsv($fileHandler, $subscriberInfo); |
| 83 |
} |
| 84 |
|
| 85 |
fclose($fileHandler); |
| 86 |
exit; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
private static function getSubscriptionsTableName() |
| 92 |
{ |
| 93 |
global $wpdb; |
| 94 |
return $wpdb->prefix . 'uji_subscriptions'; |
| 95 |
} |
| 96 |
|
| 97 |
public function prepare_items() |
| 98 |
{ |
| 99 |
global $wpdb; |
| 100 |
|
| 101 |
$totalItems = $wpdb->get_var("Select count(*) from " . self::getSubscriptionsTableName()); |
| 102 |
|
| 103 |
$pageNumber = (int)(!empty($_GET["paged"]) && is_numeric(sanitize_text_field($_GET["paged"])) ? sanitize_text_field($_GET["paged"]) : 1); |
| 104 |
|
| 105 |
$orderBy = !empty($_GET['orderby']) && array_key_exists(trim($_GET['orderby']), $this->get_sortable_columns()) ? sanitize_text_field(trim($_GET['orderby'])) : 'Id'; |
| 106 |
$orderType = !empty($orderBy) && !empty($_GET['order']) && in_array(strtolower($_GET['order']), array('asc', 'desc')) ? sanitize_text_field($_GET['order']) : 'DESC'; |
| 107 |
|
| 108 |
$sqlStatement = 'SELECT * FROM ' . self::getSubscriptionsTableName(); |
| 109 |
$sqlStatement .= isset($orderBy) && isset($orderType) ? " ORDER BY $orderBy $orderType" : null; |
| 110 |
$sqlStatement .= " LIMIT " . (($pageNumber - 1) * $this->itemsPerPage) . ", $this->itemsPerPage"; |
| 111 |
|
| 112 |
$this->set_pagination_args( array( |
| 113 |
"total_items" => $totalItems, |
| 114 |
"total_pages" => ceil($totalItems / $this->itemsPerPage), |
| 115 |
"per_page" => $this->itemsPerPage, |
| 116 |
) ); |
| 117 |
|
| 118 |
|
| 119 |
$columns = $this->get_columns(); |
| 120 |
$hidden = array(); |
| 121 |
$sortable = $this->get_sortable_columns(); |
| 122 |
$this->_column_headers = array($columns, $hidden, $sortable); |
| 123 |
|
| 124 |
$this->items = $wpdb->get_results($sqlStatement); |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
public function get_sortable_columns() |
| 129 |
{ |
| 130 |
return array( |
| 131 |
'CreatedDate' => array('CreatedDate', false) |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
public function get_columns() |
| 136 |
{ |
| 137 |
return array( |
| 138 |
'cb' => '<input type="checkbox" />', |
| 139 |
'Email' => __( 'Email Address', 'uji-countdown'), |
| 140 |
'Campaign' => __( "Campaign", 'uji-countdown'), |
| 141 |
'CreatedDate' => __( "Subscribed", 'uji-countdown') |
| 142 |
); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
public function column_default($item, $column_name) |
| 147 |
{ |
| 148 |
switch($column_name) |
| 149 |
{ |
| 150 |
case 'Email' : return is_array($item) ? $item['EmailAddress'] : $item->EmailAddress; |
| 151 |
case 'Campaign' : return is_array($item) ? $item['List'] : $item->List; |
| 152 |
case 'CreatedDate' : return date_format(new DateTime(is_array($item) ? $item['CreatedDate'] : $item->CreatedDate), 'F jS, Y g:i a') ; |
| 153 |
} |
| 154 |
|
| 155 |
return ''; |
| 156 |
} |
| 157 |
|
| 158 |
public function column_cb($item) |
| 159 |
{ |
| 160 |
return sprintf( |
| 161 |
'<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 162 |
$this->_args['singular'], |
| 163 |
$item->Id |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
public function get_bulk_actions() |
| 169 |
{ |
| 170 |
return array( |
| 171 |
'delete' => __('Delete' , 'uji-countdown'), |
| 172 |
'cvs-export' => __('CSV Export', 'uji-countdown') |
| 173 |
); |
| 174 |
|
| 175 |
} |
| 176 |
} |