PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.07.11
E2Pdf – Export Pdf Tool for WordPress v1.07.11
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / model / e2pdf-entry.php

e2pdf-entry.php in E2Pdf – Export Pdf Tool for WordPress 1.07.11, at classes/model/e2pdf-entry.php

203 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2pdf Template Model
5 *
6 * @copyright Copyright 2017 https://e2pdf.com
7 * @license GPL v2
8 * @version 1
9 * @link https://e2pdf.com
10 * @since 0.01.33
11 */
12 if (!defined('ABSPATH')) {
13 die('Access denied.');
14 }
15
16 class Model_E2pdf_Entry extends Model_E2pdf_Model {
17
18 private $table;
19 private $key;
20 private $entry = array();
21
22 /*
23 * On Template init
24 */
25
26 function __construct() {
27 global $wpdb;
28 parent::__construct();
29 $this->table = $wpdb->prefix . 'e2pdf_entries';
30 $this->key = hash('sha256', md5(NONCE_KEY));
31 }
32
33 /**
34 * Load Template by ID
35 *
36 * @param int $entry_id - ID of template
37 */
38 public function load($entry_id = false) {
39 global $wpdb;
40
41 $entry = false;
42 if ($this->helper->get('cache')) {
43 $entry = wp_cache_get($entry_id, 'e2pdf_entries');
44 }
45
46 if ($entry === false) {
47 $entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE ID = %d", $entry_id), ARRAY_A);
48 if ($this->helper->get('cache')) {
49 wp_cache_set($entry_id, $entry, 'e2pdf_entries');
50 }
51 }
52
53 if ($entry) {
54 $this->entry = $entry;
55 $this->set('entry', unserialize($entry['entry']));
56 return true;
57 }
58 return false;
59 }
60
61 /**
62 * Load Entry by UID
63 *
64 * @param int $entry_uid - ID of template
65 */
66 public function load_by_uid($entry_uid = false) {
67 global $wpdb;
68 if ($entry_uid) {
69
70 $entry = false;
71 if ($this->helper->get('cache')) {
72 $entry = wp_cache_get($entry_uid, 'e2pdf_uid_entries');
73 }
74
75 if ($entry === false) {
76 $entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE uid = %s", $entry_uid), ARRAY_A);
77 if ($this->helper->get('cache')) {
78 wp_cache_set($entry_uid, $entry, 'e2pdf_uid_entries');
79 }
80 }
81
82 if ($entry) {
83 $this->entry = $entry;
84 $this->set('entry', unserialize($entry['entry']));
85 return true;
86 }
87 }
88 return false;
89 }
90
91 /**
92 * Get loaded Template
93 *
94 * @return object
95 */
96 public function get_entry() {
97 return $this->entry;
98 }
99
100 /**
101 * Set Entry attribute
102 *
103 * @param string $key - Attribute Key
104 * @param string $value - Attribute Value
105 */
106 public function set($key, $value) {
107 $this->entry[$key] = $value;
108 }
109
110 /**
111 * Get Entry attribute by Key
112 *
113 * @param string $key - Attribute Key
114 *
115 * @return mixed
116 */
117 public function get($key) {
118 if (isset($this->entry[$key])) {
119 $value = $this->entry[$key];
120 return $value;
121 } else {
122 switch ($key) {
123 case 'pdf_num':
124 $value = 0;
125 break;
126
127 case 'entry':
128 $value = array();
129 break;
130
131 case 'uid':
132 $value = md5(md5(serialize($this->get('entry'))) . md5($this->key));
133 break;
134
135 default:
136 $value = '';
137 break;
138 }
139 return $value;
140 }
141 }
142
143 /**
144 * Before save template
145 */
146 public function pre_save() {
147
148 $entry = array(
149 'uid' => $this->get('uid'),
150 'entry' => serialize($this->get('entry')),
151 'pdf_num' => $this->get('pdf_num')
152 );
153 return $entry;
154 }
155
156 /**
157 * Save entry
158 */
159 public function save() {
160 global $wpdb;
161
162 $entry = $this->pre_save();
163
164 if ($this->get('ID')) {
165 $where = array(
166 'ID' => $this->get('ID')
167 );
168 $wpdb->update($this->get_table(), $entry, $where);
169 } else {
170 $wpdb->insert($this->get_table(), $entry);
171 $this->set('ID', $wpdb->insert_id);
172 }
173
174 if ($this->helper->get('cache') && $this->get('ID')) {
175 wp_cache_delete($this->get('ID'), 'e2pdf_entries');
176 wp_cache_delete($entry['uid'], 'e2pdf_uid_entries');
177 }
178 }
179
180 /**
181 * Delete loaded entry
182 */
183 public function delete() {
184 global $wpdb;
185 if ($this->get('ID')) {
186 $where = array(
187 'ID' => $this->get('ID')
188 );
189 $wpdb->delete($this->get_table(), $where);
190
191 if ($this->helper->get('cache')) {
192 wp_cache_delete($this->get('ID'), 'e2pdf_entries');
193 wp_cache_delete($this->get('uid'), 'e2pdf_uid_entries');
194 }
195 }
196 }
197
198 public function get_table() {
199 return $this->table;
200 }
201
202 }
203