| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /model/e2pdf-entry.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_Entry extends Model_E2pdf_Model { |
| 15 |
|
| 16 |
private $table; |
| 17 |
private $key; |
| 18 |
private $entry = array(); |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
global $wpdb; |
| 22 |
parent::__construct(); |
| 23 |
$this->table = $wpdb->prefix . 'e2pdf_entries'; |
| 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_by_uid($uid = false) { |
| 32 |
global $wpdb; |
| 33 |
if (!$uid) { |
| 34 |
$uid = $this->get('uid'); |
| 35 |
} |
| 36 |
$entry = false; |
| 37 |
if ($this->helper->get('cache')) { |
| 38 |
$entry = wp_cache_get($uid, 'e2pdf_uid_entries'); |
| 39 |
} |
| 40 |
if ($entry === false) { |
| 41 |
$this->helper->load('cache')->pre_objects_cache(); |
| 42 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 43 |
$entry = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE uid = %s', $uid), ARRAY_A); |
| 44 |
if ($this->helper->get('cache')) { |
| 45 |
wp_cache_set($uid, $entry, 'e2pdf_uid_entries'); |
| 46 |
} |
| 47 |
} |
| 48 |
if ($entry) { |
| 49 |
$entry = apply_filters('e2pdf_load_entry_by_uid', $entry, $this); |
| 50 |
$this->entry = $entry; |
| 51 |
$this->set('entry', $this->helper->load('convert')->unserialize($entry['entry'])); |
| 52 |
return true; |
| 53 |
} |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
public function set($key, $value) { |
| 58 |
$this->entry[$key] = $value; |
| 59 |
} |
| 60 |
|
| 61 |
public function get($key) { |
| 62 |
if (isset($this->entry[$key])) { |
| 63 |
$value = $this->entry[$key]; |
| 64 |
if ($key == 'entry') { |
| 65 |
ksort($value); |
| 66 |
} |
| 67 |
} else { |
| 68 |
switch ($key) { |
| 69 |
case 'pdf_num': |
| 70 |
$value = 0; |
| 71 |
break; |
| 72 |
case 'entry': |
| 73 |
$value = array(); |
| 74 |
break; |
| 75 |
case 'uid': |
| 76 |
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 77 |
$value = md5(md5(serialize($this->get('entry'))) . md5($this->key)); |
| 78 |
break; |
| 79 |
default: |
| 80 |
$value = ''; |
| 81 |
break; |
| 82 |
} |
| 83 |
} |
| 84 |
return $value; |
| 85 |
} |
| 86 |
|
| 87 |
public function get_data($key) { |
| 88 |
if (isset($this->entry['entry'][$key])) { |
| 89 |
$value = $this->entry['entry'][$key]; |
| 90 |
} else { |
| 91 |
$value = false; |
| 92 |
} |
| 93 |
return $value; |
| 94 |
} |
| 95 |
|
| 96 |
public function set_data($key, $value) { |
| 97 |
if (!isset($this->entry['entry'])) { |
| 98 |
$this->set('entry', array()); |
| 99 |
} |
| 100 |
$this->entry['entry'][$key] = $value; |
| 101 |
} |
| 102 |
|
| 103 |
public function pre_save() { |
| 104 |
|
| 105 |
$entry = array( |
| 106 |
'uid' => $this->get('uid'), |
| 107 |
'entry' => serialize($this->get('entry')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 108 |
'pdf_num' => $this->get('pdf_num'), |
| 109 |
); |
| 110 |
return $entry; |
| 111 |
} |
| 112 |
|
| 113 |
public function save() { |
| 114 |
global $wpdb; |
| 115 |
|
| 116 |
$entry = $this->pre_save(); |
| 117 |
|
| 118 |
if ($this->get('ID')) { |
| 119 |
$where = array( |
| 120 |
'ID' => $this->get('ID'), |
| 121 |
); |
| 122 |
$success = $wpdb->update($this->get_table(), $entry, $where); |
| 123 |
if ($success === false) { |
| 124 |
$this->helper->load('db')->db_init($wpdb->prefix); |
| 125 |
$wpdb->update($this->get_table(), $entry, $where); |
| 126 |
} |
| 127 |
} else { |
| 128 |
$success = $wpdb->insert($this->get_table(), $entry); |
| 129 |
if ($success === false) { |
| 130 |
$this->helper->load('db')->db_init($wpdb->prefix); |
| 131 |
$wpdb->insert($this->get_table(), $entry); |
| 132 |
} |
| 133 |
$this->set('ID', $wpdb->insert_id); |
| 134 |
$this->set('uid', $this->get('uid')); |
| 135 |
} |
| 136 |
|
| 137 |
if ($this->helper->get('cache') && $this->get('ID')) { |
| 138 |
wp_cache_delete($this->get('uid'), 'e2pdf_uid_entries'); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public function delete() { |
| 143 |
global $wpdb; |
| 144 |
if ($this->get('ID')) { |
| 145 |
$where = array( |
| 146 |
'ID' => $this->get('ID'), |
| 147 |
); |
| 148 |
$wpdb->delete($this->get_table(), $where); |
| 149 |
|
| 150 |
if ($this->helper->get('cache')) { |
| 151 |
wp_cache_delete($this->get('uid'), 'e2pdf_uid_entries'); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
public function get_table() { |
| 157 |
return $this->table; |
| 158 |
} |
| 159 |
} |
| 160 |
|