PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.49
E2Pdf – Export Pdf Tool for WordPress v1.32.49
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
← All changes | classes/model/e2pdf-entry.php +59 -102 1.08.001.32.49 View file →
@@ -1,14 +1,12 @@
1 1 <?php
2 2
3 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
4 + * File: /model/e2pdf-entry.php
5 + *
6 + * @package E2Pdf
7 + * @license GPLv3
8 + * @link https://e2pdf.com
11 9 */
12 10 if (!defined('ABSPATH')) {
13 11 die('Access denied.');
14 12 }
@@ -18,145 +16,101 @@
18 16 private $table;
19 17 private $key;
20 18 private $entry = array();
21 19
22 - /*
23 - * On Template init
24 - */
25 -
26 - function __construct() {
20 + public function __construct() {
27 21 global $wpdb;
28 22 parent::__construct();
29 23 $this->table = $wpdb->prefix . 'e2pdf_entries';
30 - $this->key = hash('sha256', md5(NONCE_KEY));
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 + }
31 29 }
32 30
33 - /**
34 - * Load Template by ID
35 - *
36 - * @param int $entry_id - ID of template
37 - */
38 - public function load($entry_id = false) {
31 + public function load_by_uid($uid = false) {
39 32 global $wpdb;
40 -
33 + if (!$uid) {
34 + $uid = $this->get('uid');
35 + }
41 36 $entry = false;
42 37 if ($this->helper->get('cache')) {
43 - $entry = wp_cache_get($entry_id, 'e2pdf_entries');
38 + $entry = wp_cache_get($uid, 'e2pdf_uid_entries');
44 39 }
45 -
46 40 if ($entry === false) {
47 - $entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE ID = %d", $entry_id), ARRAY_A);
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);
48 44 if ($this->helper->get('cache')) {
49 - wp_cache_set($entry_id, $entry, 'e2pdf_entries');
45 + wp_cache_set($uid, $entry, 'e2pdf_uid_entries');
50 46 }
51 47 }
52 -
53 48 if ($entry) {
49 + $entry = apply_filters('e2pdf_load_entry_by_uid', $entry, $this);
54 50 $this->entry = $entry;
55 - $this->set('entry', unserialize($entry['entry']));
51 + $this->set('entry', $this->helper->load('convert')->unserialize($entry['entry']));
56 52 return true;
57 53 }
58 54 return false;
59 55 }
60 56
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 57 public function set($key, $value) {
107 58 $this->entry[$key] = $value;
108 59 }
109 60
110 - /**
111 - * Get Entry attribute by Key
112 - *
113 - * @param string $key - Attribute Key
114 - *
115 - * @return mixed
116 - */
117 61 public function get($key) {
118 62 if (isset($this->entry[$key])) {
119 63 $value = $this->entry[$key];
120 - return $value;
64 + if ($key == 'entry') {
65 + ksort($value);
66 + }
121 67 } else {
122 68 switch ($key) {
123 69 case 'pdf_num':
124 70 $value = 0;
125 71 break;
126 -
127 72 case 'entry':
128 73 $value = array();
129 74 break;
130 -
131 75 case 'uid':
76 + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
132 77 $value = md5(md5(serialize($this->get('entry'))) . md5($this->key));
133 78 break;
134 -
135 79 default:
136 80 $value = '';
137 81 break;
138 82 }
139 - return $value;
140 83 }
84 + return $value;
141 85 }
142 86
143 - /**
144 - * Before save template
145 - */
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 +
146 103 public function pre_save() {
147 104
148 105 $entry = array(
149 106 'uid' => $this->get('uid'),
150 - 'entry' => serialize($this->get('entry')),
151 - 'pdf_num' => $this->get('pdf_num')
107 + 'entry' => serialize($this->get('entry')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
108 + 'pdf_num' => $this->get('pdf_num'),
152 109 );
153 110 return $entry;
154 111 }
155 112
156 - /**
157 - * Save entry
158 - */
159 113 public function save() {
160 114 global $wpdb;
161 115
162 116 $entry = $this->pre_save();
@@ -162,35 +116,39 @@
162 116 $entry = $this->pre_save();
163 117
164 118 if ($this->get('ID')) {
165 119 $where = array(
166 - 'ID' => $this->get('ID')
120 + 'ID' => $this->get('ID'),
167 121 );
168 - $wpdb->update($this->get_table(), $entry, $where);
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 + }
169 127 } else {
170 - $wpdb->insert($this->get_table(), $entry);
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 + }
171 133 $this->set('ID', $wpdb->insert_id);
134 + $this->set('uid', $this->get('uid'));
172 135 }
173 136
174 137 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');
138 + wp_cache_delete($this->get('uid'), 'e2pdf_uid_entries');
177 139 }
178 140 }
179 141
180 - /**
181 - * Delete loaded entry
182 - */
183 142 public function delete() {
184 143 global $wpdb;
185 144 if ($this->get('ID')) {
186 145 $where = array(
187 - 'ID' => $this->get('ID')
146 + 'ID' => $this->get('ID'),
188 147 );
189 148 $wpdb->delete($this->get_table(), $where);
190 149
191 150 if ($this->helper->get('cache')) {
192 - wp_cache_delete($this->get('ID'), 'e2pdf_entries');
193 151 wp_cache_delete($this->get('uid'), 'e2pdf_uid_entries');
194 152 }
195 153 }
196 154 }
@@ -197,6 +155,5 @@
197 155
198 156 public function get_table() {
199 157 return $this->table;
200 158 }
201 -
202 159 }