| @@ -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,121 +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 | - $entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE ID = %d", $entry_id), ARRAY_A); | |
| 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 | + } | |
| 41 | 48 | if ($entry) { |
| 49 | + $entry = apply_filters('e2pdf_load_entry_by_uid', $entry, $this); | |
| 42 | 50 | $this->entry = $entry; |
| 43 | - $this->set('entry', unserialize($entry['entry'])); | |
| 51 | + $this->set('entry', $this->helper->load('convert')->unserialize($entry['entry'])); | |
| 44 | 52 | return true; |
| 45 | 53 | } |
| 46 | 54 | return false; |
| 47 | 55 | } |
| 48 | 56 | |
| 49 | - /** | |
| 50 | - * Load Entry by UID | |
| 51 | - * | |
| 52 | - * @param int $entry_uid - ID of template | |
| 53 | - */ | |
| 54 | - public function load_by_uid($entry_uid = false) { | |
| 55 | - global $wpdb; | |
| 56 | - if ($entry_uid) { | |
| 57 | - $entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE uid = %s", $entry_uid), ARRAY_A); | |
| 58 | - if ($entry) { | |
| 59 | - $this->entry = $entry; | |
| 60 | - $this->set('entry', unserialize($entry['entry'])); | |
| 61 | - return true; | |
| 62 | - } | |
| 63 | - } | |
| 64 | - return false; | |
| 65 | - } | |
| 66 | - | |
| 67 | - /** | |
| 68 | - * Get loaded Template | |
| 69 | - * | |
| 70 | - * @return object | |
| 71 | - */ | |
| 72 | - public function get_entry() { | |
| 73 | - return $this->entry; | |
| 74 | - } | |
| 75 | - | |
| 76 | - /** | |
| 77 | - * Set Entry attribute | |
| 78 | - * | |
| 79 | - * @param string $key - Attribute Key | |
| 80 | - * @param string $value - Attribute Value | |
| 81 | - */ | |
| 82 | 57 | public function set($key, $value) { |
| 83 | 58 | $this->entry[$key] = $value; |
| 84 | 59 | } |
| 85 | 60 | |
| 86 | - /** | |
| 87 | - * Get Entry attribute by Key | |
| 88 | - * | |
| 89 | - * @param string $key - Attribute Key | |
| 90 | - * | |
| 91 | - * @return mixed | |
| 92 | - */ | |
| 93 | 61 | public function get($key) { |
| 94 | 62 | if (isset($this->entry[$key])) { |
| 95 | 63 | $value = $this->entry[$key]; |
| 96 | - return $value; | |
| 64 | + if ($key == 'entry') { | |
| 65 | + ksort($value); | |
| 66 | + } | |
| 97 | 67 | } else { |
| 98 | 68 | switch ($key) { |
| 99 | 69 | case 'pdf_num': |
| 100 | 70 | $value = 0; |
| 101 | 71 | break; |
| 102 | - | |
| 103 | 72 | case 'entry': |
| 104 | 73 | $value = array(); |
| 105 | 74 | break; |
| 106 | - | |
| 107 | 75 | case 'uid': |
| 76 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize | |
| 108 | 77 | $value = md5(md5(serialize($this->get('entry'))) . md5($this->key)); |
| 109 | 78 | break; |
| 110 | - | |
| 111 | 79 | default: |
| 112 | 80 | $value = ''; |
| 113 | 81 | break; |
| 114 | 82 | } |
| 115 | - return $value; | |
| 116 | 83 | } |
| 84 | + return $value; | |
| 117 | 85 | } |
| 118 | 86 | |
| 119 | - /** | |
| 120 | - * Before save template | |
| 121 | - */ | |
| 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 | + | |
| 122 | 103 | public function pre_save() { |
| 123 | 104 | |
| 124 | 105 | $entry = array( |
| 125 | 106 | 'uid' => $this->get('uid'), |
| 126 | - 'entry' => serialize($this->get('entry')), | |
| 127 | - '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'), | |
| 128 | 109 | ); |
| 129 | 110 | return $entry; |
| 130 | 111 | } |
| 131 | 112 | |
| 132 | - /** | |
| 133 | - * Save entry | |
| 134 | - */ | |
| 135 | 113 | public function save() { |
| 136 | 114 | global $wpdb; |
| 137 | 115 | |
| 138 | 116 | $entry = $this->pre_save(); |
| @@ -138,27 +116,41 @@ | ||
| 138 | 116 | $entry = $this->pre_save(); |
| 139 | 117 | |
| 140 | 118 | if ($this->get('ID')) { |
| 141 | 119 | $where = array( |
| 142 | - 'ID' => $this->get('ID') | |
| 120 | + 'ID' => $this->get('ID'), | |
| 143 | 121 | ); |
| 144 | - $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 | + } | |
| 145 | 127 | } else { |
| 146 | - $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 | + } | |
| 147 | 133 | $this->set('ID', $wpdb->insert_id); |
| 134 | + $this->set('uid', $this->get('uid')); | |
| 148 | 135 | } |
| 136 | + | |
| 137 | + if ($this->helper->get('cache') && $this->get('ID')) { | |
| 138 | + wp_cache_delete($this->get('uid'), 'e2pdf_uid_entries'); | |
| 139 | + } | |
| 149 | 140 | } |
| 150 | 141 | |
| 151 | - /** | |
| 152 | - * Delete loaded entry | |
| 153 | - */ | |
| 154 | 142 | public function delete() { |
| 155 | 143 | global $wpdb; |
| 156 | 144 | if ($this->get('ID')) { |
| 157 | 145 | $where = array( |
| 158 | - 'ID' => $this->get('ID') | |
| 146 | + 'ID' => $this->get('ID'), | |
| 159 | 147 | ); |
| 160 | 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 | + } | |
| 161 | 153 | } |
| 162 | 154 | } |
| 163 | 155 | |
| 164 | 156 | public function get_table() { |
| @@ -163,6 +155,5 @@ | ||
| 163 | 155 | |
| 164 | 156 | public function get_table() { |
| 165 | 157 | return $this->table; |
| 166 | 158 | } |
| 167 | - | |
| 168 | 159 | } |