| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /model/e2pdf-bulk.php |
| 5 |
* |
| 6 |
* @package E2Pdf |
| 7 |
* @license GPLv3 |
| 8 |
* @link https://e2pdf.com |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
die('Access denied.'); |
| 12 |
} |
| 13 |
|
| 14 |
class Model_E2pdf_Bulk extends Model_E2pdf_Model { |
| 15 |
|
| 16 |
private $table; |
| 17 |
private $key; |
| 18 |
private $bulk = array(); |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
global $wpdb; |
| 22 |
parent::__construct(); |
| 23 |
$this->table = $wpdb->prefix . 'e2pdf_bulks'; |
| 24 |
if (defined('NONCE_KEY')) { |
| 25 |
$this->key = hash('sha256', md5(NONCE_KEY)); |
| 26 |
} else { |
| 27 |
$this->key = hash('sha256', md5(get_option('e2pdf_nonce_key'))); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public function load($bulk_id) { |
| 32 |
global $wpdb; |
| 33 |
|
| 34 |
$bulk = false; |
| 35 |
if ($this->helper->get('cache')) { |
| 36 |
$bulk = wp_cache_get($bulk_id, 'e2pdf_bulks'); |
| 37 |
} |
| 38 |
|
| 39 |
if ($bulk === false) { |
| 40 |
$this->helper->load('cache')->pre_objects_cache(); |
| 41 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 42 |
$bulk = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE ID = %d', $bulk_id), ARRAY_A); |
| 43 |
if ($this->helper->get('cache')) { |
| 44 |
wp_cache_set($bulk_id, $bulk, 'e2pdf_bulks'); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if ($bulk) { |
| 49 |
$this->bulk = $bulk; |
| 50 |
$template = new Model_E2pdf_Template(); |
| 51 |
if ($this->get('template_id') && $template->load($this->get('template_id'), false)) { |
| 52 |
$this->set('template', $template); |
| 53 |
} |
| 54 |
$this->set('options', $this->helper->load('convert')->unserialize($bulk['options'])); |
| 55 |
$this->set('datasets', $this->helper->load('convert')->unserialize($bulk['datasets'])); |
| 56 |
return true; |
| 57 |
} |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
// load by uid |
| 62 |
public function load_by_uid($uid = false) { |
| 63 |
global $wpdb; |
| 64 |
$bulk = false; |
| 65 |
if ($this->helper->get('cache')) { |
| 66 |
$bulk = wp_cache_get($uid, 'e2pdf_bulks_uids'); |
| 67 |
} |
| 68 |
if ($bulk === false) { |
| 69 |
$this->helper->load('cache')->pre_objects_cache(); |
| 70 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 71 |
$bulk = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE uid = %s', $uid), ARRAY_A); |
| 72 |
if ($this->helper->get('cache')) { |
| 73 |
wp_cache_set($uid, $bulk, 'e2pdf_bulks_uids'); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if ($bulk) { |
| 78 |
$this->bulk = $bulk; |
| 79 |
$this->set('options', $this->helper->load('convert')->unserialize($bulk['options'])); |
| 80 |
$this->set('datasets', $this->helper->load('convert')->unserialize($bulk['datasets'])); |
| 81 |
return true; |
| 82 |
} |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
public function load_by_active_bulk() { |
| 87 |
global $wpdb; |
| 88 |
|
| 89 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 90 |
$bulk = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE status = %s OR status = %s ORDER BY ID ASC', 'pending', 'busy'), ARRAY_A); |
| 91 |
if ($bulk) { |
| 92 |
$this->load($bulk['ID']); |
| 93 |
return true; |
| 94 |
} |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
public function get($key) { |
| 99 |
if (isset($this->bulk[$key])) { |
| 100 |
return $this->bulk[$key]; |
| 101 |
} else { |
| 102 |
switch ($key) { |
| 103 |
case 'uid': |
| 104 |
$value = md5(md5(time()) . md5($this->key)); |
| 105 |
break; |
| 106 |
case 'status': |
| 107 |
$value = 'pending'; |
| 108 |
break; |
| 109 |
case 'count': |
| 110 |
case 'total': |
| 111 |
$value = '0'; |
| 112 |
break; |
| 113 |
case 'options': |
| 114 |
$value = array(); |
| 115 |
break; |
| 116 |
default: |
| 117 |
$value = ''; |
| 118 |
break; |
| 119 |
} |
| 120 |
return $value; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public function set($key, $value) { |
| 125 |
$this->bulk[$key] = $value; |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
public function bulk() { |
| 130 |
return $this->bulk; |
| 131 |
} |
| 132 |
|
| 133 |
public function pre_save() { |
| 134 |
$bulk = array( |
| 135 |
'uid' => $this->get('uid'), |
| 136 |
'template_id' => $this->get('template_id'), |
| 137 |
'count' => $this->get('count'), |
| 138 |
'total' => $this->get('total'), |
| 139 |
'dataset' => $this->get('dataset'), |
| 140 |
'datasets' => serialize($this->get('datasets')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 141 |
'options' => serialize($this->get('options')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 142 |
'status' => $this->get('status'), |
| 143 |
); |
| 144 |
|
| 145 |
if (!$this->get('ID')) { |
| 146 |
$bulk['created_at'] = current_time('mysql', 1); |
| 147 |
} |
| 148 |
|
| 149 |
return $bulk; |
| 150 |
} |
| 151 |
|
| 152 |
public function save() { |
| 153 |
global $wpdb; |
| 154 |
$bulk = $this->pre_save(); |
| 155 |
|
| 156 |
$show_errors = false; |
| 157 |
if ($wpdb->show_errors) { |
| 158 |
$wpdb->show_errors(false); |
| 159 |
$show_errors = true; |
| 160 |
} |
| 161 |
|
| 162 |
if ($this->get('ID')) { |
| 163 |
$where = array( |
| 164 |
'ID' => $this->get('ID'), |
| 165 |
); |
| 166 |
$success = $wpdb->update($this->get_table(), $bulk, $where); |
| 167 |
if ($success === false) { |
| 168 |
$this->helper->load('db')->db_init($wpdb->prefix); |
| 169 |
$wpdb->update($this->get_table(), $bulk, $where); |
| 170 |
} |
| 171 |
} else { |
| 172 |
$success = $wpdb->insert($this->get_table(), $bulk); |
| 173 |
if ($success === false) { |
| 174 |
$this->helper->load('db')->db_init($wpdb->prefix); |
| 175 |
$wpdb->insert($this->get_table(), $bulk); |
| 176 |
} |
| 177 |
$this->set('ID', $wpdb->insert_id); |
| 178 |
} |
| 179 |
|
| 180 |
if ($show_errors) { |
| 181 |
$wpdb->show_errors(); |
| 182 |
} |
| 183 |
|
| 184 |
if ($this->helper->get('cache') && $this->get('ID')) { |
| 185 |
wp_cache_delete($this->get('ID'), 'e2pdf_bulks'); |
| 186 |
wp_cache_delete($this->get('uid'), 'e2pdf_bulks_uids'); |
| 187 |
} |
| 188 |
|
| 189 |
return $this->get('ID'); |
| 190 |
} |
| 191 |
|
| 192 |
public function get_table() { |
| 193 |
return $this->table; |
| 194 |
} |
| 195 |
|
| 196 |
public function get_active_status() { |
| 197 |
global $wpdb; |
| 198 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 199 |
$status = $wpdb->get_var($wpdb->prepare('SELECT status FROM `' . $this->get_table() . '` WHERE ID = %d', $this->get('ID'))); |
| 200 |
return $status; |
| 201 |
} |
| 202 |
|
| 203 |
// delete |
| 204 |
public function delete() { |
| 205 |
global $wpdb; |
| 206 |
if ($this->get('ID')) { |
| 207 |
|
| 208 |
$where = array( |
| 209 |
'ID' => $this->get('ID'), |
| 210 |
); |
| 211 |
$wpdb->delete($this->get_table(), $where); |
| 212 |
|
| 213 |
if ($this->helper->get('cache') && $this->get('ID')) { |
| 214 |
wp_cache_delete($this->get('ID'), 'e2pdf_bulks'); |
| 215 |
wp_cache_delete($this->get('uid'), 'e2pdf_bulks_uids'); |
| 216 |
} |
| 217 |
|
| 218 |
$pdf_dir = $this->helper->get('bulk_dir') . $this->get('uid') . '/'; |
| 219 |
if (file_exists($pdf_dir)) { |
| 220 |
$this->helper->delete_dir($pdf_dir); |
| 221 |
} |
| 222 |
|
| 223 |
$zip_path = $this->helper->get('bulk_dir') . $this->get('uid') . '.zip'; |
| 224 |
if (file_exists($zip_path)) { |
| 225 |
unlink($zip_path); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|