| @@ -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.00.01 | |
| 4 | + * File: /model/e2pdf-page.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 | } |
| @@ -17,9 +15,9 @@ | ||
| 17 | 15 | |
| 18 | 16 | private $page = array(); |
| 19 | 17 | private $table; |
| 20 | 18 | |
| 21 | - function __construct() { | |
| 19 | + public function __construct() { | |
| 22 | 20 | global $wpdb; |
| 23 | 21 | parent::__construct(); |
| 24 | 22 | $this->table = $wpdb->prefix . 'e2pdf_pages'; |
| 25 | 23 | } |
| @@ -28,9 +26,22 @@ | ||
| 28 | 26 | return $this->table; |
| 29 | 27 | } |
| 30 | 28 | |
| 31 | 29 | public function set($key, $value) { |
| 32 | - $this->page[$key] = $value; | |
| 30 | + switch ($key) { | |
| 31 | + case 'properties': | |
| 32 | + case 'actions': | |
| 33 | + if (!is_array($value)) { | |
| 34 | + $this->page[$key] = array(); | |
| 35 | + } else { | |
| 36 | + $this->page[$key] = $value; | |
| 37 | + } | |
| 38 | + break; | |
| 39 | + | |
| 40 | + default: | |
| 41 | + $this->page[$key] = $value; | |
| 42 | + break; | |
| 43 | + } | |
| 33 | 44 | } |
| 34 | 45 | |
| 35 | 46 | public function get($key) { |
| 36 | 47 | if (isset($this->page[$key])) { |
| @@ -38,16 +49,15 @@ | ||
| 38 | 49 | return $value; |
| 39 | 50 | } else { |
| 40 | 51 | switch ($key) { |
| 41 | 52 | case 'template_id': |
| 53 | + case 'revision_id': | |
| 42 | 54 | $value = 0; |
| 43 | 55 | break; |
| 44 | - | |
| 45 | 56 | case 'properties': |
| 46 | 57 | case 'actions': |
| 47 | 58 | $value = array(); |
| 48 | 59 | break; |
| 49 | - | |
| 50 | 60 | default: |
| 51 | 61 | $value = ''; |
| 52 | 62 | break; |
| 53 | 63 | } |
| @@ -62,10 +72,11 @@ | ||
| 62 | 72 | public function before_save() { |
| 63 | 73 | $page = array( |
| 64 | 74 | 'template_id' => $this->get('template_id'), |
| 65 | 75 | 'page_id' => $this->get('page_id'), |
| 66 | - 'properties' => serialize($this->get('properties')), | |
| 67 | - 'actions' => serialize($this->get('actions')) | |
| 76 | + 'properties' => serialize($this->get('properties')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize | |
| 77 | + 'actions' => serialize($this->get('actions')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize | |
| 78 | + 'revision_id' => $this->get('revision_id'), | |
| 68 | 79 | ); |
| 69 | 80 | return $page; |
| 70 | 81 | } |
| 71 | 82 | |
| @@ -73,22 +84,38 @@ | ||
| 73 | 84 | global $wpdb; |
| 74 | 85 | |
| 75 | 86 | $page = $this->before_save(); |
| 76 | 87 | if ($this->get('page_id') && $this->get('template_id')) { |
| 77 | - $wpdb->insert($this->get_table(), $page); | |
| 88 | + $show_errors = false; | |
| 89 | + if ($wpdb->show_errors) { | |
| 90 | + $wpdb->show_errors(false); | |
| 91 | + $show_errors = true; | |
| 92 | + } | |
| 93 | + | |
| 94 | + $success = $wpdb->insert($this->get_table(), $page); | |
| 95 | + if ($success === false) { | |
| 96 | + $this->helper->load('db')->db_init($wpdb->prefix); | |
| 97 | + $wpdb->insert($this->get_table(), $page); | |
| 98 | + } | |
| 99 | + | |
| 100 | + if ($show_errors) { | |
| 101 | + $wpdb->show_errors(); | |
| 102 | + } | |
| 78 | 103 | return $this->get('page_id'); |
| 79 | 104 | } |
| 80 | 105 | } |
| 81 | 106 | |
| 82 | - public function load($page_id, $template_id) { | |
| 107 | + public function load($page_id, $template_id, $revision_id = 0) { | |
| 83 | 108 | global $wpdb; |
| 84 | - $page = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE page_id = %d AND template_id = %d", $page_id, $template_id), ARRAY_A); | |
| 109 | + | |
| 110 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare | |
| 111 | + $page = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE page_id = %d AND template_id = %d AND revision_id = %d', $page_id, $template_id, $revision_id), ARRAY_A); | |
| 85 | 112 | if ($page) { |
| 86 | 113 | $this->page = $page; |
| 87 | - $this->set('properties', unserialize($page['properties'])); | |
| 88 | - $this->set('actions', unserialize($page['actions'])); | |
| 114 | + $this->set('properties', $this->helper->load('convert')->unserialize($page['properties'])); | |
| 115 | + $this->set('actions', $this->helper->load('convert')->unserialize($page['actions'])); | |
| 89 | 116 | $model_e2pdf_element = new Model_E2pdf_Element(); |
| 90 | - $this->set('elements', $model_e2pdf_element->get_elements($this->get('page_id'), $this->get('template_id'))); | |
| 117 | + $this->set('elements', $model_e2pdf_element->get_elements($this->get('page_id'), $this->get('template_id'), $this->get('revision_id'))); | |
| 91 | 118 | return true; |
| 92 | 119 | } |
| 93 | 120 | return false; |
| 94 | 121 | } |
| @@ -97,28 +124,31 @@ | ||
| 97 | 124 | global $wpdb; |
| 98 | 125 | if ($this->get('page_id') && $this->get('template_id')) { |
| 99 | 126 | $where = array( |
| 100 | 127 | 'page_id' => $this->get('page_id'), |
| 101 | - 'template_id' => $this->get('template_id') | |
| 128 | + 'template_id' => $this->get('template_id'), | |
| 129 | + 'revision_id' => $this->get('revision_id'), | |
| 102 | 130 | ); |
| 103 | 131 | $wpdb->delete($this->get_table(), $where); |
| 104 | 132 | |
| 105 | 133 | foreach ($this->get('elements') as $element) { |
| 106 | 134 | $model_e2pdf_element = new Model_E2pdf_Element(); |
| 107 | - $model_e2pdf_element->load($element['element_id'], $element['template_id']); | |
| 135 | + $model_e2pdf_element->load($element['element_id'], $element['template_id'], $element['revision_id']); | |
| 108 | 136 | $model_e2pdf_element->delete(); |
| 109 | 137 | } |
| 110 | 138 | } |
| 111 | 139 | } |
| 112 | 140 | |
| 113 | - public function get_pages($template_id) { | |
| 141 | + public function get_pages($template_id, $revision_id = 0) { | |
| 114 | 142 | global $wpdb; |
| 115 | - $pages = $wpdb->get_results($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE template_id = %d ORDER BY page_id ASC", $template_id), ARRAY_A); | |
| 143 | + | |
| 144 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare | |
| 145 | + $pages = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE template_id = %d AND revision_id = %d ORDER BY page_id ASC', $template_id, $revision_id), ARRAY_A); | |
| 116 | 146 | if ($pages) { |
| 117 | 147 | $pages_list = array(); |
| 118 | 148 | $pages_list[] = array(); |
| 119 | 149 | foreach ($pages as $page_key => $page) { |
| 120 | - $this->load($page['page_id'], $page['template_id']); | |
| 150 | + $this->load($page['page_id'], $page['template_id'], $page['revision_id']); | |
| 121 | 151 | $pages_list[] = $this->get_page(); |
| 122 | 152 | } |
| 123 | 153 | unset($pages_list[0]); |
| 124 | 154 | return $pages_list; |
| @@ -124,6 +154,5 @@ | ||
| 124 | 154 | return $pages_list; |
| 125 | 155 | } |
| 126 | 156 | return array(); |
| 127 | 157 | } |
| 128 | - | |
| 129 | 158 | } |