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/controller/e2pdf-templates.php +2029 -1434 1.00.131.32.49 View file →
@@ -1,11 +1,10 @@
1 1 <?php
2 2
3 3 /**
4 - * E2pdf Templates Controller
5 - *
4 + * E2Pdf Templates Controller
6 5 * @copyright Copyright 2017 https://e2pdf.com
7 - * @license GPL v2
6 + * @license GPLv3
8 7 * @version 1
9 8 * @link https://e2pdf.com
10 9 * @since 0.00.01
11 10 */
@@ -18,55 +17,63 @@
18 17 /**
19 18 * @url admin.php?page=e2pdf-templates
20 19 */
21 20 public function index_action() {
22 -
23 - if ($this->post->get()) {
24 -
25 - $this->check_nonce($this->post->get('_nonce'), 'e2pdf_post');
26 -
27 - if ($this->post->get('action') == '-1' && $this->post->get('action2') == '-1') {
28 - if ($this->post->get('s')) {
29 - $location = $this->helper->get_url(
30 - array(
31 - 'page' => 'e2pdf-templates',
32 - 'status' => $this->get->get('status'),
33 - 'orderby' => $this->get->get('orderby'),
34 - 'order' => $this->get->get('order'),
35 - 's' => $this->post->get('s')
36 - )
21 + if ($this->post->get('screenoptionnonce')) {
22 + if (wp_verify_nonce($this->post->get('screenoptionnonce'), 'screen-options-nonce')) {
23 + $this->screen_action();
24 + } else {
25 + wp_die($this->message('wp_verify_nonce_error'));
26 + }
27 + } elseif ($this->post->get('_wpnonce')) {
28 + if (wp_verify_nonce($this->post->get('_wpnonce'), 'e2pdf_templates')) {
29 + if ($this->post->get('action') == '-1' && $this->post->get('action2') == '-1') {
30 + $redirect_url = [
31 + 'page' => 'e2pdf-templates',
32 + 'status' => $this->get->get('status'),
33 + 'orderby' => $this->get->get('orderby'),
34 + 'order' => $this->get->get('order'),
35 + ];
36 + if ($this->post->get('s')) {
37 + $redirect_url['s'] = $this->post->get('s');
38 + }
39 + $this->redirect(
40 + $this->helper->get_url($redirect_url)
37 41 );
38 42 } else {
39 - $location = $this->helper->get_url(
40 - array(
41 - 'page' => 'e2pdf-templates',
42 - 'status' => $this->get->get('status'),
43 - 'orderby' => $this->get->get('orderby'),
44 - 'order' => $this->get->get('order'),
45 - )
46 - );
47 - }
48 - $this->redirect($location);
49 - } else {
50 - $action = $this->post->get('action') != '-1' ? $this->post->get('action') : $this->post->get('action2');
51 - if ($this->post->get('post')) {
52 - if ($action == 'trash') {
53 - foreach ($this->post->get('post') as $key => $value) {
54 - $this->trash_template($value);
43 + $action = $this->post->get('action') != '-1' ? $this->post->get('action') : $this->post->get('action2');
44 + if ($this->post->get('post')) {
45 + if ($action == 'trash') {
46 + foreach ($this->post->get('post') as $key => $value) {
47 + $this->trash_template($value);
48 + }
49 + $this->add_notification('update', sprintf(__('Templates Trashed: %d', 'e2pdf'), count($this->post->get('post'))));
50 + } elseif ($action == 'activate') {
51 + foreach ($this->post->get('post') as $key => $value) {
52 + $this->activate_template($value);
53 + }
54 + $this->add_notification('update', sprintf(__('Templates Activated: %d', 'e2pdf'), count($this->post->get('post'))));
55 + } elseif ($action == 'deactivate') {
56 + foreach ($this->post->get('post') as $key => $value) {
57 + $this->deactivate_template($value);
58 + }
59 + $this->add_notification('update', sprintf(__('Templates Deactivated: %d', 'e2pdf'), count($this->post->get('post'))));
60 + } elseif ($action == 'restore') {
61 + foreach ($this->post->get('post') as $key => $value) {
62 + $this->restore_template($value);
63 + }
64 + $this->add_notification('update', sprintf(__('Templates Restored: %d', 'e2pdf'), count($this->post->get('post'))));
65 + } elseif ($action == 'delete') {
66 + foreach ($this->post->get('post') as $key => $value) {
67 + $this->delete_template($value);
68 + }
69 + $this->add_notification('update', sprintf(__('Templates Deleted: %d', 'e2pdf'), count($this->post->get('post'))));
55 70 }
56 - $this->add_notification('update', __('Templates moved to trash', 'e2pdf'));
57 - } elseif ($action == 'restore') {
58 - foreach ($this->post->get('post') as $key => $value) {
59 - $this->restore_template($value);
60 - }
61 - $this->add_notification('update', __('Templates restored', 'e2pdf'));
62 - } elseif ($action == 'delete') {
63 - foreach ($this->post->get('post') as $key => $value) {
64 - $this->delete_template($value);
65 - }
66 - $this->add_notification('update', __('Templates deleted', 'e2pdf'));
71 + $this->helper->load('license')->load();
67 72 }
68 73 }
74 + } else {
75 + wp_die($this->message('wp_verify_nonce_error'));
69 76 }
70 77 }
71 78 }
72 79
@@ -71,36 +78,90 @@
71 78 }
72 79
73 80 /**
74 81 * @url admin.php?page=e2pdf-templates&action=view&id=$id
75 - *
76 82 * @return file
77 83 */
78 84 public function view_action() {
79 -
80 85 if ($this->get->get('id')) {
81 86 $template = new Model_E2pdf_Template();
82 -
83 87 if ($template->load($this->get->get('id'))) {
84 -
85 - $helper_e2pdf_image = new Helper_E2pdf_Image();
86 -
87 88 $pages = $template->get('pages');
88 -
89 - foreach ($pages as $page_key => $page_value) {
90 -
91 - if (isset($page_value['elements'])) {
92 -
93 - foreach ($page_value['elements'] as $element_key => $element_value) {
94 -
95 - if (isset($element_value['type']) && $element_value['type'] == 'e2pdf-image') {
96 -
97 - if (!$helper_e2pdf_image->get_image($element_value['value'])) {
98 - $pages[$page_key]['elements'][$element_key]['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png";
99 -
100 - if (isset($pages[$page_key]['elements'][$element_key]['properties']['dimension'])) {
101 - unset($pages[$page_key]['elements'][$element_key]['properties']['dimension']);
102 - }
89 + foreach ($pages as $page_key => $page) {
90 + if (!empty($page['elements'])) {
91 + foreach ($page['elements'] as $element_key => $element) {
92 + if (isset($element['type'])) {
93 + switch ($element['type']) {
94 + case 'e2pdf-image':
95 + if (isset($element['properties']['quality']) && $element['properties']['quality']) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
96 + } else {
97 + $element['properties']['quality'] = $template->get('optimization');
98 + $pages[$page_key]['elements'][$element_key]['properties']['quality'] = $template->get('optimization');
99 + }
100 + if ($image = $this->helper->load('image')->get_image($element['value'], null, $element)) {
101 + $pages[$page_key]['elements'][$element_key]['value'] = $image;
102 + } else {
103 + $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/upload.svg');
104 + $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
105 + }
106 + break;
107 + case 'e2pdf-signature':
108 + $esig = isset($element['properties']['esig']) && $element['properties']['esig'] ? true : false;
109 + if (!$esig) {
110 + if ($image = $this->helper->load('image')->get_image($element['value'])) {
111 + $pages[$page_key]['elements'][$element_key]['value'] = $image;
112 + } else {
113 + $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/signature.svg');
114 + $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
115 + }
116 + }
117 + break;
118 + case 'e2pdf-qrcode':
119 + $pages[$page_key]['elements'][$element_key]['type'] = 'e2pdf-image';
120 + $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/qrcode.svg');
121 + $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
122 + break;
123 + case 'e2pdf-barcode':
124 + $pages[$page_key]['elements'][$element_key]['type'] = 'e2pdf-image';
125 + $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/barcode.svg');
126 + $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
127 + break;
128 + case 'e2pdf-graph':
129 + $pages[$page_key]['elements'][$element_key]['type'] = 'e2pdf-image';
130 + $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/graph.svg');
131 + $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
132 + break;
133 + case 'e2pdf-html':
134 + if (isset($pages[$page_key]['elements'][$element_key]['properties']['hide_page_if_empty'])) {
135 + unset($pages[$page_key]['elements'][$element_key]['properties']['hide_page_if_empty']);
136 + }
137 + if (isset($pages[$page_key]['elements'][$element_key]['properties']['hide_if_empty'])) {
138 + unset($pages[$page_key]['elements'][$element_key]['properties']['hide_if_empty']);
139 + }
140 + if (isset($element['properties']['nl2br']) && $element['properties']['nl2br']) {
141 + $element['value'] = nl2br($element['value']);
142 + }
143 + if (isset($element['properties']['html_worker']) && $element['properties']['html_worker']) {
144 + $element['value'] = $this->helper->load('properties')->html_worker(
145 + $element['value']
146 + );
147 + }
148 + $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('filter')->filter_html_tags(
149 + $element['value']
150 + );
151 + break;
152 + case 'e2pdf-page-number':
153 + if (isset($element['properties']['html_worker']) && $element['properties']['html_worker']) {
154 + $element['value'] = $this->helper->load('properties')->html_worker(
155 + $element['value']
156 + );
157 + }
158 + $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('filter')->filter_html_tags(
159 + $element['value']
160 + );
161 + break;
162 + default:
163 + break;
103 164 }
104 165 }
105 166 }
106 167 }
@@ -112,23 +173,23 @@
112 173 if (isset($request['error'])) {
113 174 $this->add_notification('error', $request['error']);
114 175 $this->render('blocks', 'notifications');
115 176 } else {
116 - $filename = $template->get_filename();
117 - $file = $request['file'];
118 - $this->download_response('pdf', $file, $filename);
177 + $this->download_response($template->get('format'), $request['file'], $template->get_name(), 'inline');
178 + exit;
119 179 }
120 180 } else {
121 - $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
181 + $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
122 182 $this->render('blocks', 'notifications');
123 183 }
124 184 } else {
125 - $location = $this->helper->get_url(
126 - array(
127 - 'page' => 'e2pdf',
185 + $this->redirect(
186 + $this->helper->get_url(
187 + [
188 + 'page' => 'e2pdf',
189 + ]
128 190 )
129 191 );
130 - $this->redirect($location);
131 192 }
132 193 }
133 194
134 195 /**
@@ -134,15 +195,12 @@
134 195 /**
135 196 * @url admin.php?page=e2pdf-templates&action=preview
136 197 */
137 198 public function preview_action() {
138 -
139 199 $preview = array();
140 -
141 200 if ($this->post->get('preview')) {
142 201 $preview = json_decode(stripslashes($this->post->get('preview')), true);
143 202 }
144 -
145 203 if (empty($preview)) {
146 204 $this->close_tab_response();
147 205 } else {
148 206 $data = $preview;
@@ -147,166 +205,146 @@
147 205 } else {
148 206 $data = $preview;
149 207 $pages = $preview['pages'];
150 208 unset($preview['pages']);
151 -
152 - $this->check_nonce($preview['_nonce'], 'e2pdf_post');
153 -
154 - $template = new Model_E2pdf_Template();
155 - foreach ($data as $key => $value) {
156 - $template->set($key, $value);
157 - }
158 -
159 - $helper_e2pdf_sort = new Helper_E2pdf_Sort();
160 - $helper_e2pdf_image = new Helper_E2pdf_Image();
161 -
162 - $preview_pages = array();
163 - foreach ($pages as $page_key => $page_value) {
164 - $page = new Model_E2pdf_Page();
165 - foreach ($page_value as $page_value_key => $page_value_value) {
166 - $page->set($page_value_key, $page_value_value);
209 + if (wp_verify_nonce(isset($preview['_wpnonce']) ? $preview['_wpnonce'] : '', 'e2pdf_templates')) {
210 + $template = new Model_E2pdf_Template();
211 + foreach ($data as $key => $value) {
212 + $template->set($key, $value);
167 213 }
168 - $page->set('page_id', $page_key);
214 + $template->set('uid', $template->get('uid', true));
215 + $template->set('activated', $template->get('activated', true));
169 216
170 - $elements = array();
217 + $preview_pages = array();
218 + foreach ($pages as $page_key => $page) {
219 + $new_page = new Model_E2pdf_Page();
220 + foreach ($page as $page_set_key => $page_set_value) {
221 + $new_page->set($page_set_key, $page_set_value);
222 + }
223 + $new_page->set('page_id', $page_key);
171 224
172 - $helper_e2pdf_sort->stable_uasort($page_value['elements'], "sort_by_zindex");
173 -
174 - foreach ($page_value['elements'] as $element_key => $element_value) {
175 - $element = new Model_E2pdf_Element();
176 - $esig = isset($element_value['properties']['esig']) && $element_value['properties']['esig'] ? true : false;
177 - if (isset($element_value['type']) && ($element_value['type'] == 'e2pdf-image' || ($element_value['type'] == 'e2pdf-signature' && !$esig))) {
178 - if (!$helper_e2pdf_image->get_image($element_value['value'])) {
179 - $element_value['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png";
180 - if (isset($element_value['properties']['dimension'])) {
181 - unset($element_value['properties']['dimension']);
225 + $elements = array();
226 + foreach ($page['elements'] as $element_key => $element) {
227 + if (isset($element['type'])) {
228 + switch ($element['type']) {
229 + case 'e2pdf-image':
230 + if (isset($element['properties']['quality']) && $element['properties']['quality']) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
231 + } else {
232 + $element['properties']['quality'] = $template->get('optimization');
233 + $pages[$page_key]['elements'][$element_key]['properties']['quality'] = $template->get('optimization');
234 + }
235 + if ($image = $this->helper->load('image')->get_image($element['value'], null, $element)) {
236 + $element['value'] = $image;
237 + } else {
238 + $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/upload.svg');
239 + $element['properties']['preview'] = '1';
240 + }
241 + break;
242 + case 'e2pdf-signature':
243 + $esig = isset($element['properties']['esig']) && $element['properties']['esig'] ? true : false;
244 + if (!$esig) {
245 + if ($image = $this->helper->load('image')->get_image($element['value'])) {
246 + $element['value'] = $image;
247 + } else {
248 + $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/signature.svg');
249 + $element['properties']['preview'] = '1';
250 + }
251 + }
252 + break;
253 + case 'e2pdf-qrcode':
254 + $element['type'] = 'e2pdf-image';
255 + $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/qrcode.svg');
256 + $element['properties']['preview'] = '1';
257 + break;
258 + case 'e2pdf-barcode':
259 + $element['type'] = 'e2pdf-image';
260 + $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/barcode.svg');
261 + $element['properties']['preview'] = '1';
262 + break;
263 + case 'e2pdf-graph':
264 + $element['type'] = 'e2pdf-image';
265 + $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/graph.svg');
266 + $element['properties']['preview'] = '1';
267 + break;
268 + case 'e2pdf-html':
269 + if (isset($element['properties']['hide_page_if_empty'])) {
270 + unset($element['properties']['hide_page_if_empty']);
271 + }
272 + if (isset($element['properties']['hide_if_empty'])) {
273 + unset($element['properties']['hide_if_empty']);
274 + }
275 + if (isset($element['properties']['nl2br']) && $element['properties']['nl2br']) {
276 + $element['value'] = nl2br($element['value']);
277 + }
278 + if (isset($element['properties']['html_worker']) && $element['properties']['html_worker']) {
279 + $element['value'] = $this->helper->load('properties')->html_worker(
280 + $element['value']
281 + );
282 + }
283 + $element['value'] = $this->helper->load('filter')->filter_html_tags(
284 + $element['value']
285 + );
286 + break;
287 + case 'e2pdf-page-number':
288 + $element['value'] = str_replace(array('[e2pdf-page-number]', '[e2pdf-page-total]'), array('e2pdf-page-number', 'e2pdf-page-total'), $element['value']);
289 + if (isset($element['properties']['html_worker']) && $element['properties']['html_worker']) {
290 + $element['value'] = $this->helper->load('properties')->html_worker(
291 + $element['value']
292 + );
293 + }
294 + $element['value'] = $this->helper->load('filter')->filter_html_tags(
295 + $element['value']
296 + );
297 + break;
298 + default:
299 + break;
182 300 }
183 301 }
184 - }
185 302
186 - foreach ($element_value as $element_value_key => $element_value_value) {
187 - $element->set($element_value_key, $element_value_value);
188 - }
303 + $new_element = new Model_E2pdf_Element();
304 + foreach ($element as $element_set_key => $element_set_value) {
305 + $new_element->set($element_set_key, $element_set_value);
306 + }
189 307
190 - if (!$element_value['properties']) {
191 - $element_properties = array();
192 - } else {
193 - $element_properties = $element_value['properties'];
308 + if (isset($element['properties'])) {
309 + $element_properties = $element['properties'];
310 + } else {
311 + $element_properties = array();
312 + }
313 + $new_element->set('properties', $element_properties);
314 + $elements[] = $new_element->get_element();
194 315 }
195 316
196 - $element->set('properties', $element_properties);
197 - $elements[] = $element->get_element();
317 + $new_page->set('elements', $elements);
318 + $preview_pages[] = $new_page->get_page();
198 319 }
199 320
200 - $page->set('elements', $elements);
201 - $preview_pages[] = $page->get_page();
202 - }
321 + $template->set('pages', $preview_pages);
322 + $template->load_fonts();
203 323
204 - $template->set('pages', $preview_pages);
205 - $request = $template->render(true);
206 -
207 - if (isset($request['error'])) {
208 - $this->add_notification('error', $request['error']);
209 - $this->render('blocks', 'notifications');
210 - } else {
211 - $filename = $template->get_filename();
212 - $file = $request['file'];
213 - $this->download_response('pdf', $file, $filename);
214 - }
215 - }
216 - }
217 -
218 - /**
219 - * @url admin.php?page=e2pdf-templates&action=convert&type=php
220 - */
221 - public function convert_action() {
222 - $preview = array();
223 -
224 - if ($this->post->get('preview')) {
225 - $preview = json_decode(stripslashes($this->post->get('preview')), true);
226 - }
227 -
228 - if (empty($preview)) {
229 - $this->close_tab_response();
230 - } else {
231 - $data = $preview;
232 - $pages = $preview['pages'];
233 - unset($preview['pages']);
234 -
235 - $this->check_nonce($preview['_nonce'], 'e2pdf_post');
236 -
237 - $template = new Model_E2pdf_Template();
238 - foreach ($data as $key => $value) {
239 - $template->set($key, $value);
240 - }
241 -
242 - $helper_e2pdf_sort = new Helper_E2pdf_Sort();
243 - $helper_e2pdf_image = new Helper_E2pdf_Image();
244 -
245 - $preview_pages = array();
246 - foreach ($pages as $page_key => $page_value) {
247 - $page = new Model_E2pdf_Page();
248 - foreach ($page_value as $page_value_key => $page_value_value) {
249 - $page->set($page_value_key, $page_value_value);
250 - }
251 - $page->set('page_id', $page_key);
252 -
253 - $elements = array();
254 -
255 - $helper_e2pdf_sort->stable_uasort($page_value['elements'], "sort_by_zindex");
256 -
257 - foreach ($page_value['elements'] as $element_key => $element_value) {
258 - $element = new Model_E2pdf_Element();
259 - $esig = isset($element_value['properties']['esig']) && $element_value['properties']['esig'] ? true : false;
260 - if (isset($element_value['type']) && ($element_value['type'] == 'e2pdf-image' || ($element_value['type'] == 'e2pdf-signature' && !$esig))) {
261 - if (!$helper_e2pdf_image->get_image($element_value['value'])) {
262 - $element_value['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png";
263 - if (isset($element_value['properties']['dimension'])) {
264 - unset($element_value['properties']['dimension']);
265 - }
266 - }
324 + $type = isset($_GET['type']) ? sanitize_text_field(wp_unslash($_GET['type'])) : false;
325 + if ($type == 'php') {
326 + $request = $template->render('php');
327 + if (isset($request['error'])) {
328 + $this->add_notification('error', $request['error']);
329 + $this->render('blocks', 'notifications');
330 + } else {
331 + $this->download_response('php', $request['file'], 'template');
332 + exit;
267 333 }
268 -
269 - foreach ($element_value as $element_value_key => $element_value_value) {
270 - $element->set($element_value_key, $element_value_value);
271 - }
272 -
273 - if (!$element_value['properties']) {
274 - $element_properties = array();
334 + } else {
335 + $request = $template->render();
336 + if (isset($request['error'])) {
337 + $this->add_notification('error', $request['error']);
338 + $this->render('blocks', 'notifications');
275 339 } else {
276 - $element_properties = $element_value['properties'];
340 + $this->download_response($template->get('format'), $request['file'], $template->get_name(), 'inline', false, true);
341 + exit;
277 342 }
278 -
279 - $element->set('properties', $element_properties);
280 - $elements[] = $element->get_element();
281 343 }
282 -
283 - $page->set('elements', $elements);
284 - $preview_pages[] = $page->get_page();
285 - }
286 -
287 - $template->set('pages', $preview_pages);
288 -
289 - $allowed_types = array(
290 - 'php'
291 - );
292 -
293 - if (!in_array($this->get->get('type'), $allowed_types)) {
294 - $type = 'php';
295 344 } else {
296 - $type = $this->get->get('type');
345 + wp_die($this->message('wp_verify_nonce_error'));
297 346 }
298 -
299 - $request = $template->convert(true, $type);
300 -
301 - if (isset($request['error'])) {
302 - $this->add_notification('error', $request['error']);
303 - $this->render('blocks', 'notifications');
304 - } else {
305 - $filename = 'template';
306 - $file = $request['file'];
307 - $this->download_response($type, $file, $filename);
308 - }
309 347 }
310 348 }
311 349
312 350 /**
@@ -312,14 +350,13 @@
312 350 /**
313 351 * @url admin.php?page=e2pdf-templates&action=create
314 352 */
315 353 public function create_action() {
316 -
317 354 $template = new Model_E2pdf_Template();
318 355 $this->load_metaboxes();
319 356 $this->load_scripts();
320 357 $this->load_styles();
321 -
358 + $this->view('license', $this->helper->get('license'));
322 359 $this->view('template', $template);
323 360 }
324 361
325 362 /**
@@ -327,25 +364,34 @@
327 364 */
328 365 public function edit_action() {
329 366 if ($this->get->get('id')) {
330 367 $template = new Model_E2pdf_Template();
331 -
332 - if ($template->load($this->get->get('id'))) {
368 + $revision_id = (int) $this->get->get('revision_id');
369 + if ($template->load($this->get->get('id'), true, $revision_id)) {
370 + if (is_array($this->helper->get('license')->get('activated_templates'))) {
371 + if ($template->get('activated') && !in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
372 + $template->deactivate();
373 + } elseif (!$template->get('activated') && in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
374 + $template->activate();
375 + }
376 + }
377 + $this->view('license', $this->helper->get('license'));
333 378 $this->view('template', $template);
334 379 $this->load_metaboxes();
335 380 $this->load_scripts();
336 381 $this->load_styles($template->get('extension'));
337 382 } else {
338 - $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
383 + $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
339 384 $this->render('blocks', 'notifications');
340 385 }
341 386 } else {
342 - $location = $this->helper->get_url(
343 - array(
344 - 'page' => 'e2pdf-templates',
387 + $this->redirect(
388 + $this->helper->get_url(
389 + [
390 + 'page' => 'e2pdf-templates',
391 + ]
345 392 )
346 393 );
347 - $this->redirect($location);
348 394 }
349 395 }
350 396
351 397 /**
@@ -351,23 +397,29 @@
351 397 /**
352 398 * @url admin.php?page=e2pdf-templates&action=trash&id=$id
353 399 */
354 400 public function trash_action() {
355 -
356 - if ($this->get->get('id')) {
357 - $this->trash_template($this->get->get('id'));
358 - $this->add_notification('update', __('Template moved to trash', 'e2pdf'));
401 + $notification = '';
402 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
403 + if ($this->get->get('id')) {
404 + $this->trash_template($this->get->get('id'));
405 + $notification = $this->add_notification('update', __('Template Trashed', 'e2pdf'));
406 + }
407 + } else {
408 + wp_die($this->message('wp_verify_nonce_error'));
359 409 }
360 - $location = $this->helper->get_url(
361 - array(
362 - 'page' => 'e2pdf-templates',
363 - 'status' => $this->get->get('status'),
364 - 'orderby' => $this->get->get('orderby'),
365 - 'order' => $this->get->get('order'),
366 - 's' => $this->get->get('s'),
410 + $this->redirect(
411 + $this->helper->get_url(
412 + [
413 + 'page' => 'e2pdf-templates',
414 + 'status' => $this->get->get('status'),
415 + 'orderby' => $this->get->get('orderby'),
416 + 'order' => $this->get->get('order'),
417 + 's' => $this->get->get('s'),
418 + 'notification' => $notification,
419 + ]
367 420 )
368 421 );
369 - $this->redirect($location);
370 422 }
371 423
372 424 /**
373 425 * @url admin.php?page=e2pdf-templates&action=restore&id=$id&status=$status
@@ -372,24 +424,29 @@
372 424 /**
373 425 * @url admin.php?page=e2pdf-templates&action=restore&id=$id&status=$status
374 426 */
375 427 public function restore_action() {
376 -
377 - if ($this->get->get('id')) {
378 - $this->restore_template($this->get->get('id'));
379 - $this->add_notification('update', __('Template restored', 'e2pdf'));
428 + $notification = '';
429 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
430 + if ($this->get->get('id')) {
431 + $this->restore_template($this->get->get('id'));
432 + $notification = $this->add_notification('update', __('Template Restored', 'e2pdf'));
433 + }
434 + } else {
435 + wp_die($this->message('wp_verify_nonce_error'));
380 436 }
381 -
382 - $location = $this->helper->get_url(
383 - array(
384 - 'page' => 'e2pdf-templates',
385 - 'status' => $this->get->get('status'),
386 - 'orderby' => $this->get->get('orderby'),
387 - 'order' => $this->get->get('order'),
388 - 's' => $this->get->get('s'),
437 + $this->redirect(
438 + $this->helper->get_url(
439 + [
440 + 'page' => 'e2pdf-templates',
441 + 'status' => $this->get->get('status'),
442 + 'orderby' => $this->get->get('orderby'),
443 + 'order' => $this->get->get('order'),
444 + 's' => $this->get->get('s'),
445 + 'notification' => $notification,
446 + ]
389 447 )
390 448 );
391 - $this->redirect($location);
392 449 }
393 450
394 451 /**
395 452 * @url admin.php?page=e2pdf-templates&action=delete&id=$id&status=$status
@@ -394,24 +451,30 @@
394 451 /**
395 452 * @url admin.php?page=e2pdf-templates&action=delete&id=$id&status=$status
396 453 */
397 454 public function delete_action() {
398 -
399 - if ($this->get->get('id')) {
400 - $this->delete_template($this->get->get('id'));
401 - $this->add_notification('update', __('Template deleted', 'e2pdf'));
455 + $notification = '';
456 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
457 + if ($this->get->get('id')) {
458 + $this->delete_template($this->get->get('id'));
459 + $notification = $this->add_notification('update', __('Template Deleted', 'e2pdf'));
460 + }
461 + } else {
462 + wp_die($this->message('wp_verify_nonce_error'));
402 463 }
403 464
404 - $location = $this->helper->get_url(
405 - array(
406 - 'page' => 'e2pdf-templates',
407 - 'status' => $this->get->get('status'),
408 - 'orderby' => $this->get->get('orderby'),
409 - 'order' => $this->get->get('order'),
410 - 's' => $this->get->get('s'),
465 + $this->redirect(
466 + $this->helper->get_url(
467 + [
468 + 'page' => 'e2pdf-templates',
469 + 'status' => $this->get->get('status'),
470 + 'orderby' => $this->get->get('orderby'),
471 + 'order' => $this->get->get('order'),
472 + 's' => $this->get->get('s'),
473 + 'notification' => $notification,
474 + ]
411 475 )
412 476 );
413 - $this->redirect($location);
414 477 }
415 478
416 479 /**
417 480 * @url admin.php?page=e2pdf-templates&action=duplicate&id=$id
@@ -416,1358 +479,1619 @@
416 479 /**
417 480 * @url admin.php?page=e2pdf-templates&action=duplicate&id=$id
418 481 */
419 482 public function duplicate_action() {
420 -
421 - if ($this->get->get('id')) {
422 - $this->duplicate_template($this->get->get('id'));
423 - $this->add_notification('update', __('Template copy created', 'e2pdf'));
424 - }
425 - $location = $this->helper->get_url(
426 - array(
427 - 'page' => 'e2pdf-templates',
428 - 'status' => $this->get->get('status'),
429 - 'orderby' => $this->get->get('orderby'),
430 - 'order' => $this->get->get('order'),
431 - 's' => $this->get->get('s'),
432 - )
433 - );
434 - $this->redirect($location);
435 - }
436 -
437 - /**
438 - * @url admin.php?page=e2pdf-templates&action=activate&id=$id
439 - */
440 - public function activate_action() {
441 -
442 - if ($this->get->get('id')) {
443 - $request = $this->activate_template($this->get->get('id'));
444 - if (isset($request['error'])) {
445 - $this->add_notification('error', $request['error']);
446 - } else {
447 - $this->add_notification('update', __('Template activated', 'e2pdf'));
483 + $notification = '';
484 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
485 + if ($this->get->get('id')) {
486 + $this->duplicate_template($this->get->get('id'));
487 + $notification = $this->add_notification('update', __('Template Duplicated', 'e2pdf'));
448 488 }
489 + } else {
490 + wp_die($this->message('wp_verify_nonce_error'));
449 491 }
450 - $location = $this->helper->get_url(
451 - array(
452 - 'page' => 'e2pdf-templates',
453 - 'status' => $this->get->get('status'),
454 - 'orderby' => $this->get->get('orderby'),
455 - 'order' => $this->get->get('order'),
456 - 's' => $this->get->get('s'),
492 + $this->redirect(
493 + $this->helper->get_url(
494 + [
495 + 'page' => 'e2pdf-templates',
496 + 'status' => $this->get->get('status'),
497 + 'orderby' => $this->get->get('orderby'),
498 + 'order' => $this->get->get('order'),
499 + 's' => $this->get->get('s'),
500 + 'notification' => $notification,
501 + ]
457 502 )
458 503 );
459 - $this->redirect($location);
460 504 }
461 505
462 506 /**
463 - * @url admin.php?page=e2pdf-templates&action=deactivate&id=$id
464 - */
465 - public function deactivate_action() {
466 -
467 - if ($this->get->get('id')) {
468 - $request = $this->deactivate_template($this->get->get('id'));
469 - if (isset($request['error'])) {
470 - $this->add_notification('error', $request['error']);
471 - } else {
472 - $this->add_notification('update', __('Template deactivated', 'e2pdf'));
473 - }
474 - }
475 - $location = $this->helper->get_url(
476 - array(
477 - 'page' => 'e2pdf-templates',
478 - 'status' => $this->get->get('status'),
479 - 'orderby' => $this->get->get('orderby'),
480 - 'order' => $this->get->get('order'),
481 - 's' => $this->get->get('s'),
482 - )
483 - );
484 - $this->redirect($location);
485 - }
486 -
487 - /**
488 507 * @url admin.php?page=e2pdf-templates&action=import
489 508 */
490 509 public function import_action() {
510 + if ($this->post->get('_wpnonce')) {
491 511
492 - if ($this->post->get()) {
512 + if (!wp_verify_nonce($this->post->get('_wpnonce'), 'e2pdf_templates')) {
513 + wp_die($this->message('wp_verify_nonce_error'));
514 + }
493 515
494 - $this->check_nonce($this->post->get('_nonce'), 'e2pdf_post');
495 -
496 - $errors = array();
497 -
498 - $allowed_ext = array(
499 - 'xml'
500 - );
501 -
502 - $import = $this->files->get('template');
503 -
504 - $name = $import['name'];
505 - $tmp = $import['tmp_name'];
506 -
507 - $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
508 -
509 - if (!$tmp) {
510 - $this->add_notification('error', __('Please choose template to upload', 'e2pdf'));
511 - } else if ($import['error']) {
512 - $this->add_notification('error', $import['error']);
513 - } elseif (!in_array($ext, $allowed_ext)) {
514 - $this->add_notification('error', __('Incorrect file extension', 'e2pdf'));
515 - } elseif ($import['type'] != 'text/xml') {
516 - $this->add_notification('error', __('Incompatible type', 'e2pdf'));
516 + $file = $this->helper->load('files')->upload($this->files->get('template'), ['xml'], ['text/xml', 'application/xml']);
517 + if (isset($file['error'])) {
518 + $this->add_notification('error', $file['error']);
517 519 } else {
518 -
520 + $errors = [];
519 521 $options = $this->post->get('options');
520 -
521 - $xml = simplexml_load_file($import['tmp_name'], "SimpleXMLElement", LIBXML_PARSEHUGE);
522 -
523 - $pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages)));
524 -
525 - foreach ($pages as $page_key => $page) {
526 - if (isset($page['elements']) && !empty($page['elements'])) {
527 - foreach ($page['elements'] as $element_key => $element) {
528 - if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
529 - if ($options['images']) {
530 - $image = base64_decode($element['base64']);
531 - $file_name = basename($element['value']);
532 - $upload_file = wp_upload_bits($file_name, null, $image);
533 - if (!$upload_file['error']) {
534 - $wp_filetype = wp_check_filetype($file_name, null);
535 - $attachment = array(
536 - 'post_mime_type' => $wp_filetype['type'],
537 - 'post_parent' => 0,
538 - 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
539 - 'post_content' => '',
540 - 'post_status' => 'inherit'
541 - );
542 - $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
543 - if (!is_wp_error($attachment_id)) {
544 - require_once(ABSPATH . "wp-admin" . '/includes/image.php');
545 - $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
546 - wp_update_attachment_metadata($attachment_id, $attachment_data);
547 - $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
522 + $xml = simplexml_load_file($file['tmp_name'], 'SimpleXMLElement', LIBXML_PARSEHUGE);
523 + if (!isset($xml->template->pages)) {
524 + $this->add_notification('error', __('The file is not a valid E2Pdf Template', 'e2pdf'));
525 + } else {
526 + $pages = $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->pages));
527 + if (is_array($pages)) {
528 + foreach ($pages as $page_key => $page) {
529 + if (isset($page['elements']) && !empty($page['elements'])) {
530 + foreach ($page['elements'] as $element_key => $element) {
531 + if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
532 + if ($options['images']) {
533 + $image = base64_decode($element['base64']);
534 + $file_name = basename($element['value']);
535 + $upload_file = wp_upload_bits($file_name, null, $image);
536 + if (!$upload_file['error']) {
537 + $wp_filetype = wp_check_filetype($file_name, null);
538 + $attachment = array(
539 + 'post_mime_type' => $wp_filetype['type'],
540 + 'post_parent' => 0,
541 + 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
542 + 'post_content' => '',
543 + 'post_status' => 'inherit',
544 + );
545 + $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
546 + if (!is_wp_error($attachment_id)) {
547 + require_once ABSPATH . 'wp-admin/includes/image.php';
548 + $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
549 + wp_update_attachment_metadata($attachment_id, $attachment_data);
550 + $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
551 + }
552 + }
548 553 }
554 + unset($pages[$page_key]['elements'][$element_key]['base64']);
549 555 }
550 556 }
551 - unset($pages[$page_key]['elements'][$element_key]['base64']);
552 557 }
553 558 }
554 559 }
555 - }
556 560
557 - $template = new Model_E2pdf_Template();
558 - if ($options['overwrite']) {
559 - $template->load((String) $xml->template->ID);
560 - }
561 + $template = new Model_E2pdf_Template();
562 + if ($options['overwrite']) {
563 + if ($options['overwrite'] == '1' && $options['template_id']) {
564 + $template->load((int) $options['template_id']);
565 + } elseif ($options['overwrite'] == '2' && $options['custom_template_id']) {
566 + if (!$template->load((int) $options['custom_template_id'])) {
567 + $template->set('TMP_ID', (int) $options['custom_template_id']);
568 + }
569 + } elseif ($options['overwrite'] == '3') {
570 + if (!$template->load((int) $xml->template->ID)) {
571 + $template->set('TMP_ID', (int) $xml->template->ID);
572 + }
573 + }
574 + }
575 + $template->set('title', (string) $xml->template->title);
576 + $template->set('flatten', (string) $xml->template->flatten);
577 + if (isset($xml->template->tab_order)) {
578 + $template->set('tab_order', (string) $xml->template->tab_order);
579 + }
580 + $template->set('compression', (string) $xml->template->compression);
581 + if (isset($xml->template->optimization)) {
582 + $template->set('optimization', (string) $xml->template->optimization);
583 + }
584 + $template->set('appearance', (string) $xml->template->appearance);
585 + $template->set('width', (string) $xml->template->width);
586 + $template->set('height', (string) $xml->template->height);
587 + $template->set('extension', (string) $xml->template->extension);
588 + if (isset($xml->template->item)) {
589 + $template->set('item', (string) $xml->template->item);
590 + }
591 + if (isset($xml->template->item1)) {
592 + $template->set('item1', (string) $xml->template->item1);
593 + }
594 + if (isset($xml->template->item2)) {
595 + $template->set('item2', (string) $xml->template->item2);
596 + }
561 597
562 - $template->set('title', (String) $xml->template->title);
563 - $template->set('flatten', (String) $xml->template->flatten);
564 - $template->set('format', (String) $xml->template->format);
565 - $template->set('compression', (String) $xml->template->compression);
566 - $template->set('appearance', (String) $xml->template->appearance);
567 - $template->set('width', (String) $xml->template->width);
568 - $template->set('height', (String) $xml->template->height);
569 - $template->set('extension', (String) $xml->template->extension);
570 -
571 -
572 - if (!$options['overwrite']) {
573 - $template->set('item', "");
574 - }
575 -
576 - $fpro2pdf_backup = false;
577 -
578 - if ($xml->source) {
579 - if ((String) $xml->source == 'fpro2pdf') {
580 - $template->set('item', (String) $xml->template->item);
581 - $fpro2pdf_backup = true;
598 + $fpro2pdf_backup = false;
599 + if ($xml->source) {
600 + if ((string) $xml->source == 'fpro2pdf') {
601 + $fpro2pdf_backup = true;
602 + }
582 603 }
583 - }
584 604
605 + $extension = new Model_E2pdf_Extension();
606 + $extension->load($template->get('extension'));
607 + if ($options['item'] && $xml->item && !$fpro2pdf_backup) {
608 + $extension->set('item', $template->get('item'));
609 + if ($template->get('item') == '-2') {
610 + $extension->set('item1', $template->get('item1'));
611 + $extension->set('item2', $template->get('item2'));
612 + }
585 613
586 - $template->set('inline', (String) $xml->template->inline);
587 - $template->set('auto', (String) $xml->template->auto);
588 - $template->set('font', (String) $xml->template->font);
589 - $template->set('font_size', (String) $xml->template->font_size);
590 - $template->set('font_color', (String) $xml->template->font_color);
591 - $template->set('line_height', (String) $xml->template->line_height);
592 - $template->set('fonts', stripslashes_deep(unserialize(base64_decode((String) $xml->template->fonts))));
593 -
594 -
595 - $extension = new Model_E2pdf_Extension();
596 - $extension->load($template->get('extension'));
597 -
598 - if ($options['item'] && $xml->item && !$fpro2pdf_backup) {
599 - $template->set('item', (String) $xml->template->item);
600 - $extension->set('item', $template->get('item'));
601 - if (method_exists($extension->extension(), 'import')) {
602 - $result = $extension->extension()->import($xml->item, (String) $xml->template->item);
603 - if (is_array($result) && array_key_exists('errors', $result)) {
604 - $errors = array_merge($errors, $result['errors']);
605 - } else {
606 - $template->set('item', $result);
614 + $updated_items = $extension->import($xml->item, $options);
615 + if ($updated_items) {
616 + if (isset($updated_items['errors'])) {
617 + $errors = array_merge($errors, $updated_items['errors']);
618 + } else {
619 + if ($template->get('item') == '-2') {
620 + if ($template->get('item1') && isset($updated_items[$template->get('item1')])) {
621 + $item1 = $updated_items[$template->get('item1')];
622 + $template->set('item1', $item1);
623 + $extension->set('item1', $item1);
624 + } else {
625 + $template->set('item1', '');
626 + $extension->set('item1', '');
627 + }
628 + if ($template->get('item2') && isset($updated_items[$template->get('item2')])) {
629 + $item2 = $updated_items[$template->get('item2')];
630 + $template->set('item2', $item2);
631 + $extension->set('item2', $item2);
632 + } else {
633 + $template->set('item2', '');
634 + $extension->set('item2', '');
635 + }
636 + } else {
637 + if (isset($updated_items[$template->get('item')])) {
638 + $item = $updated_items[$template->get('item')];
639 + $template->set('item', $item);
640 + $extension->set('item', $item);
641 + } else {
642 + $template->set('item', '');
643 + $extension->set('item', '');
644 + }
645 + }
646 + }
607 647 }
608 648 }
609 - }
610 649
611 - $pages = apply_filters('e2pdf_controller_templates_import_pages', $options, $xml, $template, $extension, $pages);
612 - $dataset_title = apply_filters('e2pdf_controller_templates_import_dataset_title', $options, $xml, $template, $extension, (String) $xml->template->dataset_title);
613 - $button_title = apply_filters('e2pdf_controller_templates_import_button_title', $options, $xml, $template, $extension, (String) $xml->template->button_title);
614 - $name = apply_filters('e2pdf_controller_templates_import_name', $options, $xml, $template, $extension, (String) $xml->template->name);
615 - $password = apply_filters('e2pdf_controller_templates_import_password', $options, $xml, $template, $extension, (String) $xml->template->password);
650 + $template->set('format', (string) $xml->template->format);
651 + if (isset($xml->template->resample)) {
652 + $template->set('resample', (string) $xml->template->resample);
653 + }
654 + $template->set(
655 + 'dataset_title',
656 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->dataset_title, $options, $xml, $template, $extension)
657 + );
658 + if (isset($xml->template->dataset_title1)) {
659 + $template->set(
660 + 'dataset_title1',
661 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->dataset_title1, $options, $xml, $template, $extension)
662 + );
663 + }
664 + if (isset($xml->template->dataset_title2)) {
665 + $template->set(
666 + 'dataset_title2',
667 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->dataset_title2, $options, $xml, $template, $extension)
668 + );
669 + }
670 + $template->set(
671 + 'button_title',
672 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->button_title, $options, $xml, $template, $extension)
673 + );
674 + if (isset($xml->template->dpdf)) {
675 + $template->set(
676 + 'dpdf',
677 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->dpdf, $options, $xml, $template, $extension)
678 + );
679 + }
680 + if (isset($xml->template->attachments)) {
681 + $template->set(
682 + 'attachments',
683 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->attachments, $options, $xml, $template, $extension)
684 + );
685 + }
686 + $template->set('inline', (string) $xml->template->inline);
687 + $template->set('auto', (string) $xml->template->auto);
688 + if (isset($xml->template->rtl)) {
689 + $template->set('rtl', (string) $xml->template->rtl);
690 + }
691 + if (isset($xml->template->font_processor)) {
692 + $template->set('font_processor', (string) $xml->template->font_processor);
693 + }
694 + $template->set(
695 + 'name',
696 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->name, $options, $xml, $template, $extension)
697 + );
698 + if (isset($xml->template->savename)) {
699 + $template->set(
700 + 'savename',
701 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->savename, $options, $xml, $template, $extension)
702 + );
703 + }
704 + $template->set(
705 + 'password',
706 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->password, $options, $xml, $template, $extension)
707 + );
708 + if (isset($xml->template->owner_password)) {
709 + $template->set(
710 + 'owner_password',
711 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->owner_password, $options, $xml, $template, $extension)
712 + );
713 + }
714 + if (isset($xml->template->permissions)) {
715 + $template->set('permissions', $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->permissions)));
716 + } else {
717 + $template->set('permissions', array('printing'));
718 + }
719 + if (isset($xml->template->hooks)) {
720 + $template->set(
721 + 'hooks',
722 + (string) $xml->template->hooks
723 + );
724 + }
616 725
617 - $template->set('pages', $pages);
618 - $template->set('dataset_title', $dataset_title);
619 - $template->set('button_title', $button_title);
620 - $template->set('name', $name);
621 - $template->set('password', $password);
726 + $template->set(
727 + 'meta_title',
728 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->meta_title, $options, $xml, $template, $extension)
729 + );
730 + $template->set(
731 + 'meta_subject',
732 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->meta_subject, $options, $xml, $template, $extension)
733 + );
734 + $template->set(
735 + 'meta_author',
736 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->meta_author, $options, $xml, $template, $extension)
737 + );
738 + $template->set(
739 + 'meta_keywords',
740 + apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->meta_keywords, $options, $xml, $template, $extension)
741 + );
742 + if (isset($xml->template->lang_code)) {
743 + $template->set('lang_code', (string) $xml->template->lang_code);
744 + }
745 + $template->set('font', (string) $xml->template->font);
746 + $template->set('font_size', (string) $xml->template->font_size);
747 + $template->set('font_color', (string) $xml->template->font_color);
748 + $template->set('line_height', (string) $xml->template->line_height);
749 + if (isset($xml->template->text_align)) {
750 + $template->set('text_align', (string) $xml->template->text_align);
751 + }
752 + $template->set('fonts', $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->fonts)));
753 + $template->set(
754 + 'properties',
755 + apply_filters('e2pdf_controller_templates_import_properties', $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->properties)), $options, $xml, $template, $extension)
756 + );
757 + $template->set(
758 + 'actions',
759 + apply_filters('e2pdf_controller_templates_import_actions', $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->actions)), $options, $xml, $template, $extension)
760 + );
761 + $template->set(
762 + 'pages',
763 + apply_filters('e2pdf_controller_templates_import_pages', $pages, $options, $xml, $template, $extension)
764 + );
622 765
766 + if ($options['fonts'] && $xml->fonts) {
767 + $model_e2pdf_font = new Model_E2pdf_Font();
768 + $fonts = $model_e2pdf_font->get_fonts();
623 769
624 - if ($options['fonts'] && $xml->fonts) {
625 - $model_e2pdf_font = new Model_E2pdf_Font();
626 - $fonts = $model_e2pdf_font->get_fonts();
770 + if ($xml->fonts) {
771 + foreach ($xml->fonts->children() as $key => $font) {
772 + $title = (string) $font->title;
773 + $name = (string) $font->name;
774 + $value = (string) $font->value;
627 775
628 - if ($xml->fonts) {
629 - foreach ($xml->fonts->children() as $key => $font) {
630 - $title = (String) $font->title;
631 - $name = (String) $font->name;
632 - $value = (String) $font->value;
776 + if ($title == 'Noto Sans' && $name == 'NotoSans-Regular.ttf') {
777 + $title = 'Noto Sans Regular';
778 + }
633 779
634 - $exist = array_search($title, $fonts);
635 - if (!$exist) {
636 - if (!file_exists($this->helper->get('fonts_dir') . $name)) {
637 - $f_name = $name;
638 - } else {
639 - $i = 0;
640 - do {
641 - $f_name = $i . '_' . $name;
642 - $i++;
643 - } while (file_exists($this->helper->get('fonts_dir') . $f_name));
780 + $exist = array_search($title, $fonts);
781 + if (!$exist) {
782 + if (!file_exists($this->helper->get('fonts_dir') . $name)) {
783 + $f_name = $name;
784 + } else {
785 + $i = 0;
786 + do {
787 + $f_name = $i . '_' . $name;
788 + $i++;
789 + } while (file_exists($this->helper->get('fonts_dir') . $f_name));
790 + }
791 + $font_ext = strtolower(pathinfo($f_name, PATHINFO_EXTENSION));
792 + if (in_array($font_ext, $model_e2pdf_font->get_allowed_extensions())) {
793 + file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value), LOCK_EX);
794 + }
644 795 }
645 - file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value));
646 796 }
647 797 }
648 798 }
649 - }
650 799
651 - if ($xml->pdf && $xml->pdf->source) {
800 + if ($xml->pdf && $xml->pdf->source) {
652 801
653 - $pdf_name = md5(time());
654 - $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
655 - $pdf_images_dir = $pdf_dir . "images/";
802 + $pdf_name = md5(time());
803 + $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
804 + $pdf_images_dir = $pdf_dir . 'images/';
656 805
657 - $this->helper->create_dir($pdf_dir);
658 - $this->helper->create_dir($pdf_images_dir);
806 + $this->helper->create_dir($pdf_dir);
807 + $this->helper->create_dir($pdf_images_dir);
659 808
660 - $pdf_source = (String) $xml->pdf->source;
661 - file_put_contents($pdf_dir . $pdf_name . ".pdf", base64_decode($pdf_source));
809 + $pdf_source = (string) $xml->pdf->source;
810 + file_put_contents($pdf_dir . $pdf_name . '.pdf', base64_decode($pdf_source), LOCK_EX);
662 811
663 - if ($xml->pdf->images) {
664 - foreach ($xml->pdf->images->children() as $key => $image) {
665 - $page_id = (String) $image->page_id;
666 - $image_source = (String) $image->source;
667 - file_put_contents($pdf_dir . "images/" . $page_id . ".png", base64_decode($image_source));
812 + if ($xml->pdf->images) {
813 + foreach ($xml->pdf->images->children() as $key => $image) {
814 + $page_id = (string) $image->page_id;
815 + $image_source = (string) $image->source;
816 + file_put_contents($pdf_dir . 'images/' . $page_id . '.png', base64_decode($image_source), LOCK_EX);
817 + }
668 818 }
819 + $template->set('pdf', $pdf_name);
669 820 }
670 - $template->set('pdf', $pdf_name);
671 - }
672 821
673 -
674 - if (empty($errors)) {
675 - $template->save(true);
676 - $this->add_notification('update', __('Template imported successfully', 'e2pdf'));
677 - } else {
678 - foreach ($errors as $key => $error) {
679 - $this->add_notification('error', __($error, 'e2pdf'));
822 + if (empty($errors)) {
823 + $template->save(true);
824 + if ($options['item']) {
825 + $extension->after_import((string) $xml->template->ID, $template->get('ID'));
826 + }
827 + $template->activate();
828 + if ($template->get('ID')) {
829 + $this->add_notification(
830 + 'update', sprintf(
831 + __('Template Imported: <a target="_blank" href="%1$s">View</a> | <a target="_blank" href="%2$s">Edit</a>', 'e2pdf'),
832 + $this->helper->get_url(
833 + [
834 + 'page' => 'e2pdf-templates',
835 + 'action' => 'view',
836 + 'id' => $template->get('ID')
837 + ]
838 + ),
839 + $this->helper->get_url(
840 + [
841 + 'page' => 'e2pdf-templates',
842 + 'action' => 'edit',
843 + 'id' => $template->get('ID')
844 + ]
845 + )
846 + )
847 + );
848 + } else {
849 + $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
850 + }
851 + } else {
852 + foreach ($errors as $key => $error) {
853 + $this->add_notification('error', $error);
854 + }
680 855 }
681 856 }
682 -
683 - unlink($import['tmp_name']);
857 + if (is_file($file['tmp_name'])) {
858 + unlink($file['tmp_name']);
859 + }
684 860 }
685 861 }
686 862
687 - $helper_e2pdf_xml = new Helper_E2pdf_Xml();
863 + $this->view('import_disabled', false);
864 + if (!$this->helper->load('xml')->check()) {
865 + $this->add_notification('error', sprintf(__('The PHP %s extension is required', 'e2pdf'), 'SimpleXml'));
866 + $this->view('import_disabled', true);
867 + }
688 868
689 - $this->view('import_disabled', false);
869 + global $wpdb;
690 870
691 - if (!$helper_e2pdf_xml->check()) {
692 - $this->add_notification('error', sprintf(__("PHP Extension <strong>%s</strong> not found", 'e2pdf'), 'SimpleXml'));
693 - $this->view('import_disabled', true);
871 + $order_condition = array(
872 + 'orderby' => 'ID',
873 + 'order' => 'desc',
874 + );
875 + $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
876 + $tpls = $wpdb->get_results($wpdb->prepare('SELECT `ID`, `title` FROM `' . (new Model_E2pdf_Template())->get_table() . '`' . $orderby . ''));
877 + $templates = [
878 + '' => __('--- Select ---', 'e2pdf')
879 + ];
880 + if (!empty($tpls)) {
881 + foreach ($tpls as $tpl) {
882 + $templates[$tpl->ID] = $tpl->ID . ' | ' . $tpl->title;
883 + }
694 884 }
695 885
696 886 $options = array(
697 - 'common' => array(
698 - 'name' => __('Options', 'e2pdf'),
887 + 'overwrite' => [
888 + 'name' => __('Overwrite Options', 'e2pdf'),
889 + 'options' => [
890 + [
891 + 'name' => __('Overwrite Template by ID', 'e2pdf'),
892 + 'key' => 'options[overwrite]',
893 + 'value' => '',
894 + 'default_value' => '',
895 + 'type' => 'select',
896 + 'options' => [
897 + '' => __('Create New E2Pdf Template ID', 'e2pdf'),
898 + '1' => __('Existing Template ID', 'e2pdf'),
899 + '2' => __('Custom Template ID', 'e2pdf'),
900 + '3' => __('Backup Template ID', 'e2pdf'),
901 + ]
902 + ],
903 + [
904 + 'name' => __('Template ID', 'e2pdf'),
905 + 'key' => 'options[template_id]',
906 + 'value' => '',
907 + 'default_value' => '',
908 + 'type' => 'select',
909 + 'options' => $templates,
910 + 'li' => array(
911 + 'class' => 'e2pdf-import-overwrite-option e2pdf-hide',
912 + ),
913 + ],
914 + [
915 + 'name' => __('Template ID', 'e2pdf'),
916 + 'key' => 'options[custom_template_id]',
917 + 'value' => '',
918 + 'default_value' => '',
919 + 'type' => 'text',
920 + 'class' => 'e2pdf-numbers',
921 + 'placeholder' => __('Template ID', 'e2pdf'),
922 + 'li' => array(
923 + 'class' => 'e2pdf-import-overwrite-option e2pdf-hide',
924 + ),
925 + ],
926 + ],
927 + ],
928 + 'template' => array(
929 + 'name' => __('Template Options', 'e2pdf'),
699 930 'options' => array(
700 931 array(
701 - 'name' => __('Overwrite template by ID', 'e2pdf'),
702 - 'key' => 'options[overwrite]',
703 - 'value' => 0,
704 - 'default_value' => 0,
932 + 'name' => __('Import Images', 'e2pdf'),
933 + 'key' => 'options[images]',
934 + 'value' => '1',
935 + 'default_value' => '0',
705 936 'type' => 'checkbox',
706 937 'placeholder' => '',
707 938 ),
708 939 array(
709 - 'name' => __('Import Item', 'e2pdf'),
710 - 'key' => 'options[item]',
711 - 'value' => 0,
712 - 'default_value' => 0,
940 + 'name' => __('Import Fonts', 'e2pdf'),
941 + 'key' => 'options[fonts]',
942 + 'value' => '1',
943 + 'default_value' => '0',
713 944 'type' => 'checkbox',
714 945 'placeholder' => '',
715 946 ),
947 + ),
948 + ),
949 + 'item' => array(
950 + 'name' => __('Connection Options', 'e2pdf'),
951 + 'options' => array(
716 952 array(
717 - 'name' => __('Import Images', 'e2pdf'),
718 - 'key' => 'options[images]',
719 - 'value' => 1,
720 - 'default_value' => 0,
953 + 'name' => __('Import Connection', 'e2pdf'),
954 + 'key' => 'options[item]',
955 + 'value' => '0',
956 + 'default_value' => '0',
721 957 'type' => 'checkbox',
722 958 'placeholder' => '',
959 + 'class' => 'e2pdf-collapse',
960 + 'data-collapse' => 'e2pdf-import-extension-option',
723 961 ),
724 - array(
725 - 'name' => __('Import Fonts', 'e2pdf'),
726 - 'key' => 'options[fonts]',
727 - 'value' => 1,
728 - 'default_value' => 0,
729 - 'type' => 'checkbox',
730 - 'placeholder' => '',
731 - )
732 - )
733 - )
962 + ),
963 + ),
734 964 );
735 965
966 + $options = apply_filters('e2pdf_controller_templates_import_options', $options);
736 967 $this->view('options', $options);
968 + $this->view('upload_max_filesize', $this->helper->load('files')->get_upload_max_filesize());
737 969 }
738 970
739 971 /**
740 972 * @url admin.php?page=e2pdf-templates&action=backup&id=$id
741 973 */
742 - public function backup_action() {
974 + public function download_action() {
975 + if ($this->post->get('_wpnonce')) {
976 + if (wp_verify_nonce($this->post->get('_wpnonce'), 'e2pdf_templates')) {
977 + if ($this->helper->load('xml')->check()) {
978 + $options = $this->post->get('options');
979 + $xml = $this->helper->load('xml')->create('backup');
980 + $template = new Model_E2pdf_Template();
981 + if ($template->load($this->post->get('id'))) {
982 + if ($template->get('title') != '') {
983 + $filename = $template->get('ID') . ' - ' . $template->get('title') . '.' . date('Y-m-d.H-i-s') . '.e2pdf';
984 + } else {
985 + $filename = date('Y-m-d.H-i-s') . '.e2pdf';
986 + }
987 + $pages = $template->get('pages');
988 + if ($options['images']) {
989 + foreach ($pages as $page_key => $page) {
990 + if (isset($page['elements']) && !empty($page['elements'])) {
991 + foreach ($page['elements'] as $element_key => $element) {
992 + if ($element['type'] === 'e2pdf-image') {
993 + $file = false;
994 + if ($this->helper->load('pdf')->get_extension($element['value'])) {
995 + $file = $this->helper->load('pdf')->get_pdf($element['value']);
996 + } else {
997 + $file = $this->helper->load('image')->get_image($element['value']);
998 + }
999 + if ($file) {
1000 + $pages[$page_key]['elements'][$element_key]['base64'] = $file;
1001 + }
1002 + }
1003 + }
1004 + }
1005 + }
1006 + }
743 1007
744 - $helper_e2pdf_xml = new Helper_E2pdf_Xml();
1008 + /*
1009 + * Build Xml
1010 + */
1011 + $xml->addChildCData('version', $this->helper->get('version'));
1012 + $xml->addChildCData('date', date('Y-m-d H:i:s'));
745 1013
746 - if ($this->post->get()) {
1014 + $options_xml = $xml->addChild('options');
1015 + foreach ($options as $option_key => $option) {
1016 + $options_xml->addChildCData($option_key, $option);
1017 + }
1018 + $template_xml = $xml->addChild('template');
1019 + $template_xml->addChildCData('ID', $template->get('ID'));
1020 + $template_xml->addChildCData('title', $template->get('title'));
1021 + $template_xml->addChildCData('created_at', $template->get('created_at'));
1022 + $template_xml->addChildCData('updated_at', $template->get('updated_at'));
1023 + $template_xml->addChildCData('flatten', $template->get('flatten'));
1024 + $template_xml->addChildCData('tab_order', $template->get('tab_order'));
1025 + $template_xml->addChildCData('compression', $template->get('compression'));
1026 + $template_xml->addChildCData('optimization', $template->get('optimization'));
1027 + $template_xml->addChildCData('appearance', $template->get('appearance'));
1028 + $template_xml->addChildCData('width', $template->get('width'));
1029 + $template_xml->addChildCData('height', $template->get('height'));
1030 + $template_xml->addChildCData('extension', $template->get('extension'));
1031 + $template_xml->addChildCData('item', $template->get('item'));
1032 + $template_xml->addChildCData('item1', $template->get('item1'));
1033 + $template_xml->addChildCData('item2', $template->get('item2'));
1034 + $template_xml->addChildCData('format', $template->get('format'));
1035 + $template_xml->addChildCData('resample', $template->get('resample'));
1036 + $template_xml->addChildCData(
1037 + 'dataset_title',
1038 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dataset_title'), $options, $template, $template->extension())
1039 + );
1040 + $template_xml->addChildCData(
1041 + 'dataset_title1',
1042 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dataset_title1'), $options, $template, $template->extension())
1043 + );
1044 + $template_xml->addChildCData(
1045 + 'dataset_title2',
1046 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dataset_title2'), $options, $template, $template->extension())
1047 + );
1048 + $template_xml->addChildCData(
1049 + 'button_title',
1050 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('button_title'), $options, $template, $template->extension())
1051 + );
1052 + $template_xml->addChildCData(
1053 + 'dpdf',
1054 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dpdf'), $options, $template, $template->extension())
1055 + );
1056 + $template_xml->addChildCData(
1057 + 'attachments',
1058 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('attachments'), $options, $template, $template->extension())
1059 + );
1060 + $template_xml->addChildCData('inline', $template->get('inline'));
1061 + $template_xml->addChildCData('auto', $template->get('auto'));
1062 + $template_xml->addChildCData('rtl', $template->get('rtl'));
1063 + $template_xml->addChildCData('font_processor', $template->get('font_processor'));
1064 + $template_xml->addChildCData(
1065 + 'name',
1066 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('name'), $options, $template, $template->extension())
1067 + );
1068 + $template_xml->addChildCData(
1069 + 'savename',
1070 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('savename'), $options, $template, $template->extension())
1071 + );
1072 + $template_xml->addChildCData(
1073 + 'password',
1074 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('password'), $options, $template, $template->extension())
1075 + );
1076 + $template_xml->addChildCData(
1077 + 'owner_password',
1078 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('owner_password'), $options, $template, $template->extension())
1079 + );
1080 + $template_xml->addChildCData('permissions', base64_encode(serialize($template->get('permissions'))));
1081 + $template_xml->addChildCData('hooks', $template->get('hooks'));
1082 + $template_xml->addChildCData(
1083 + 'meta_title',
1084 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_title'), $options, $template, $template->extension())
1085 + );
1086 + $template_xml->addChildCData(
1087 + 'meta_subject',
1088 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_subject'), $options, $template, $template->extension())
1089 + );
1090 + $template_xml->addChildCData(
1091 + 'meta_author',
1092 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_author'), $options, $template, $template->extension())
1093 + );
1094 + $template_xml->addChildCData(
1095 + 'meta_keywords',
1096 + apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_keywords'), $options, $template, $template->extension())
1097 + );
1098 + $template_xml->addChildCData('lang_code', $template->get('lang_code'));
1099 + $template_xml->addChildCData('font', $template->get('font'));
1100 + $template_xml->addChildCData('font_size', $template->get('font_size'));
1101 + $template_xml->addChildCData('font_color', $template->get('font_color'));
1102 + $template_xml->addChildCData('line_height', $template->get('line_height'));
1103 + $template_xml->addChildCData('text_align', $template->get('text_align'));
1104 + $template_xml->addChildCData('fonts', base64_encode(serialize($template->get('fonts'))));
1105 + $template_xml->addChildCData(
1106 + 'properties', base64_encode(
1107 + serialize(
1108 + apply_filters('e2pdf_controller_templates_backup_properties', $template->get('properties'), $options, $template, $template->extension())
1109 + )
1110 + )
1111 + );
1112 + $template_xml->addChildCData(
1113 + 'actions', base64_encode(
1114 + serialize(
1115 + apply_filters('e2pdf_controller_templates_backup_actions', $template->get('actions'), $options, $template, $template->extension())
1116 + )
1117 + )
1118 + );
1119 + $template_xml->addChildCData('pages', base64_encode(
1120 + serialize(
1121 + apply_filters('e2pdf_controller_templates_backup_pages', $pages, $options, $template, $template->extension())
1122 + )
1123 + )
1124 + );
747 1125
748 - $this->check_nonce($this->post->get('_nonce'), 'e2pdf_post');
1126 + // include pdf
1127 + if ($template->get('pdf')) {
1128 + $pdf_xml = $xml->addChild('pdf');
1129 + $pdf_xml->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . '/' . $template->get('pdf') . '.pdf')));
1130 + $pdf_xml_images = $pdf_xml->addChild('images');
1131 + foreach ($template->get('pages') as $key => $page) {
1132 + $pdf_xml_image = $pdf_xml_images->addChild('image');
1133 + $pdf_xml_image->addChildCData('page_id', $page['page_id']);
1134 + $pdf_xml_image->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . '/images/' . $page['page_id'] . '.png')));
1135 + }
1136 + }
749 1137
750 - if ($helper_e2pdf_xml->check()) {
751 -
752 - $options = $this->post->get('options');
753 -
754 - $xml = $helper_e2pdf_xml->create('backup');
755 -
756 - $template = new Model_E2pdf_Template();
757 - $template->load($this->post->get('id'));
758 -
759 - $extension = new Model_E2pdf_Extension();
760 - $extension->load($template->get('extension'));
761 - $extension->set('item', $template->get('item'));
762 -
763 - if ($template->get('title') != '') {
764 - $filename = $template->get('title') . "." . date('Y-m-d.H-i-s') . ".e2pdf";
765 - } else {
766 - $filename = date('Y-m-d.H-i-s') . ".e2pdf";
767 - }
768 -
769 - $helper_e2pdf_image = new Helper_E2pdf_Image();
770 -
771 - $pages = $template->get('pages');
772 - if ($options['images']) {
773 - foreach ($pages as $page_key => $page) {
774 - if (isset($page['elements']) && !empty($page['elements'])) {
775 - foreach ($page['elements'] as $element_key => $element) {
776 - if ($element['type'] === 'e2pdf-image') {
777 - $pages[$page_key]['elements'][$element_key]['base64'] = $helper_e2pdf_image->get_image($element['value']);
1138 + // include fonts
1139 + if ($options['fonts']) {
1140 + $fonts_xml = $xml->addChild('fonts');
1141 + if ($template->get('fonts')) {
1142 + $model_e2pdf_font = new Model_E2pdf_Font();
1143 + foreach ($template->get('fonts') as $key => $value) {
1144 + $font = $fonts_xml->addChild('font');
1145 + $font->addChildCData('title', $value);
1146 + $font->addChildCData('name', $key);
1147 + $font->addChildCData('value', $model_e2pdf_font->get_font($key));
778 1148 }
779 1149 }
780 1150 }
781 - }
782 - }
783 1151
784 - $pages = apply_filters('e2pdf_controller_templates_backup_pages', $options, $template, $extension, $pages);
785 - $dataset_title = apply_filters('e2pdf_controller_templates_backup_dataset_title', $options, $template, $extension, $template->get('dataset_title'));
786 - $button_title = apply_filters('e2pdf_controller_templates_backup_button_title', $options, $template, $extension, $template->get('button_title'));
787 - $name = apply_filters('e2pdf_controller_templates_backup_name', $options, $template, $extension, $template->get('name'));
788 - $password = apply_filters('e2pdf_controller_templates_backup_password', $options, $template, $extension, $template->get('password'));
789 -
790 -
791 - // build xml
792 - $xml->addChildCData('version', $this->helper->get('version'));
793 - $xml->addChildCData('date', date('Y-m-d H:i:s'));
794 -
795 - $options_xml = $xml->addChild('options');
796 - foreach ($options as $option_key => $option) {
797 - $options_xml->addChildCData($option_key, $option);
798 - }
799 -
800 - $template_xml = $xml->addChild('template');
801 - $template_xml->addChildCData('ID', $template->get('ID'));
802 - $template_xml->addChildCData('title', $template->get('title'));
803 - $template_xml->addChildCData('created_at', $template->get('created_at'));
804 - $template_xml->addChildCData('updated_at', $template->get('updated_at'));
805 - $template_xml->addChildCData('flatten', $template->get('flatten'));
806 - $template_xml->addChildCData('format', $template->get('format'));
807 - $template_xml->addChildCData('compression', $template->get('compression'));
808 - $template_xml->addChildCData('appearance', $template->get('appearance'));
809 - $template_xml->addChildCData('width', $template->get('width'));
810 - $template_xml->addChildCData('height', $template->get('height'));
811 - $template_xml->addChildCData('extension', $template->get('extension'));
812 - $template_xml->addChildCData('item', $template->get('item'));
813 - $template_xml->addChildCData('dataset_title', $dataset_title);
814 - $template_xml->addChildCData('button_title', $button_title);
815 - $template_xml->addChildCData('inline', $template->get('inline'));
816 - $template_xml->addChildCData('auto', $template->get('auto'));
817 - $template_xml->addChildCData('name', $name);
818 - $template_xml->addChildCData('password', $password);
819 - $template_xml->addChildCData('font', $template->get('font'));
820 - $template_xml->addChildCData('font_size', $template->get('font_size'));
821 - $template_xml->addChildCData('font_color', $template->get('font_color'));
822 - $template_xml->addChildCData('line_height', $template->get('line_height'));
823 - $template_xml->addChildCData('fonts', base64_encode(serialize($template->get('fonts'))));
824 - $template_xml->addChildCData('pages', base64_encode(serialize($pages)));
825 -
826 -
827 - //include pdf
828 - if ($template->get('pdf')) {
829 - $pdf_xml = $xml->addChild('pdf');
830 - $pdf_xml->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf")));
831 - $pdf_xml_images = $pdf_xml->addChild('images');
832 - foreach ($template->get('pages') as $key => $page) {
833 - $pdf_xml_image = $pdf_xml_images->addChild('image');
834 - $pdf_xml_image->addChildCData('page_id', $page['page_id']);
835 - $pdf_xml_image->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . "/images/" . $page['page_id'] . ".png")));
836 - }
837 - }
838 -
839 - //include fonts
840 - if ($options['fonts']) {
841 - $fonts_xml = $xml->addChild('fonts');
842 - if ($template->get('fonts')) {
843 - $model_e2pdf_font = new Model_E2pdf_Font();
844 - foreach ($template->get('fonts') as $key => $value) {
845 - $font = $fonts_xml->addChild('font');
846 - $font->addChildCData('title', $value);
847 - $font->addChildCData('name', $key);
848 - $font->addChildCData('value', $model_e2pdf_font->get_font($key));
1152 + // include item
1153 + if ($options['item']) {
1154 + if ($template->extension()->method('backup')) {
1155 + $item = $xml->addChild('item');
1156 + $template->extension()->backup($item);
1157 + }
849 1158 }
1159 + $this->download_response('xml', $this->helper->load('xml')->get_xml(), $filename);
1160 + exit;
1161 + } else {
1162 + $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
1163 + $this->render('blocks', 'notifications');
850 1164 }
851 1165 }
852 -
853 -
854 - //include item
855 - if ($options['item']) {
856 - if (method_exists($extension->extension(), 'backup')) {
857 - $item = $xml->addChild('item');
858 - $extension->extension()->backup($item);
859 - }
860 - }
861 -
862 - $file = $helper_e2pdf_xml->get_xml();
863 - $this->download_response('xml', $file, $filename);
1166 + } else {
1167 + wp_die($this->message('wp_verify_nonce_error'));
864 1168 }
865 1169 }
866 1170
867 1171 $this->view('export_disabled', false);
868 1172
869 - if (!$helper_e2pdf_xml->check()) {
870 - $this->add_notification('error', sprintf(__("PHP Extension <strong>%s</strong> not found", 'e2pdf'), 'SimpleXml'));
1173 + if (!$this->helper->load('xml')->check()) {
1174 + $this->add_notification('error', sprintf(__('The PHP %s extension is required', 'e2pdf'), 'SimpleXml'));
871 1175 $this->view('export_disabled', true);
872 1176 }
873 1177
874 1178 $template = new Model_E2pdf_Template();
875 - $template->load($this->get->get('id'));
1179 + if ($template->load($this->get->get('id'))) {
876 1180
877 - $extension = new Model_E2pdf_Extension();
878 - $extension->load($template->get('extension'));
879 - $item = $extension->get_item($template->get('item'));
1181 + $this->view('template', $template);
880 1182
881 - $template->set('item_name', $item->name);
882 - $template->set('item_url', $item->url);
1183 + $options = array(
1184 + 'common' => array(
1185 + 'name' => __('Options', 'e2pdf'),
1186 + 'options' => array(
1187 + array(
1188 + 'name' => __('Include Connection', 'e2pdf'),
1189 + 'key' => 'options[item]',
1190 + 'value' => $template->extension()->method('backup') ? '1' : '0',
1191 + 'default_value' => 0,
1192 + 'type' => 'checkbox',
1193 + 'placeholder' => '',
1194 + 'disabled' => $template->extension()->method('backup') ? false : 'disabled',
1195 + ),
1196 + array(
1197 + 'name' => __('Include Images', 'e2pdf'),
1198 + 'key' => 'options[images]',
1199 + 'value' => 1,
1200 + 'default_value' => 0,
1201 + 'type' => 'checkbox',
1202 + 'placeholder' => '',
1203 + ),
1204 + array(
1205 + 'name' => __('Include Fonts', 'e2pdf'),
1206 + 'key' => 'options[fonts]',
1207 + 'value' => 1,
1208 + 'default_value' => 0,
1209 + 'type' => 'checkbox',
1210 + 'placeholder' => '',
1211 + ),
1212 + ),
1213 + ),
1214 + );
883 1215
884 - $this->view('extension', $extension->extension());
885 - $this->view('template', $template);
1216 + $options = apply_filters('e2pdf_controller_templates_backup_options', $options, $template, $template->extension());
1217 + $this->view('options', $options);
1218 + } else {
1219 + $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
1220 + $this->render('blocks', 'notifications');
1221 + }
1222 + }
886 1223
887 - $options = array(
888 - 'common' => array(
889 - 'name' => __('Options', 'e2pdf'),
890 - 'options' => array(
891 - array(
892 - 'name' => __('Include Item', 'e2pdf'),
893 - 'key' => 'options[item]',
894 - 'value' => $extension->export() ? 1 : 0,
895 - 'default_value' => 0,
896 - 'type' => 'checkbox',
897 - 'placeholder' => '',
898 - 'disabled' => $extension->export() ? false : 'disabled'
899 - ),
900 - array(
901 - 'name' => __('Include Images', 'e2pdf'),
902 - 'key' => 'options[images]',
903 - 'value' => 1,
904 - 'default_value' => 0,
905 - 'type' => 'checkbox',
906 - 'placeholder' => '',
907 - ),
908 - array(
909 - 'name' => __('Include Fonts', 'e2pdf'),
910 - 'key' => 'options[fonts]',
911 - 'value' => 1,
912 - 'default_value' => 0,
913 - 'type' => 'checkbox',
914 - 'placeholder' => '',
915 - )
1224 + // screen action
1225 + public function screen_action() {
1226 + $option = $this->post->get('wp_screen_options');
1227 + if (is_array($option) && isset($option['option'], $option['value']) && $option['option'] === 'e2pdf_templates_screen_per_page') {
1228 + $per_page = (int) $option['value'];
1229 + if ($per_page > 0) {
1230 + update_option($option['option'], $per_page);
1231 + }
1232 + }
1233 + $this->redirect(
1234 + $this->helper->get_url(
1235 + [
1236 + 'page' => 'e2pdf-templates',
1237 + ]
916 1238 )
917 - )
918 1239 );
919 -
920 - $options = apply_filters('e2pdf_controller_templates_backup_options', $options, $template, $extension);
921 -
922 - $this->view('options', $options);
923 1240 }
924 1241
925 - /**
926 - * Save template via ajax
927 - * action: wp_ajax_e2pdf_save_form
928 - * function: e2pdf_save_form
929 - *
930 - * @return json
931 - */
1242 + // ajax save form
932 1243 public function ajax_save_form() {
933 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
934 -
935 - $data = json_decode($this->post->get('data'), true);
936 -
937 - $template = new Model_E2pdf_Template();
938 - if (isset($data['ID'])) {
939 - $template->load($data['ID']);
940 - if ($template->get('pdf') && $template->get('pdf') !== $data['pdf']) {
941 - $old_pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/";
942 - $this->helper->delete_dir($old_pdf_path);
1244 + global $wpdb;
1245 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1246 + $data = json_decode($this->post->get('data'), true);
1247 + $template = new Model_E2pdf_Template();
1248 + if (isset($data['ID'])) {
1249 + $template->load($data['ID']);
943 1250 }
1251 + if (!isset($data['permissions'])) {
1252 + $data['permissions'] = array();
1253 + }
1254 + foreach ($data as $key => $value) {
1255 + if ($key == 'pdf' && $value) {
1256 + if (file_exists($this->helper->get('pdf_dir') . $value . '/' . $value . '.pdf')) {
1257 + $template->set($key, $value);
1258 + }
1259 + } else {
1260 + $template->set($key, $value);
1261 + }
1262 + }
1263 + $template_id = $template->save(true);
1264 + if (!$template_id) {
1265 + if ($wpdb->last_error) {
1266 + $response = array(
1267 + 'error' => $wpdb->last_error,
1268 + );
1269 + } else {
1270 + $response = array(
1271 + 'error' => __('Something went wrong!', 'e2pdf'),
1272 + );
1273 + }
1274 + } else {
1275 + if (isset($data['item']) && $data['item'] == '-1') {
1276 + $template->load($template_id);
1277 + if ($template->extension()->method('auto_form')) {
1278 + $template = $template->extension()->auto_form($template, $data);
1279 + } else {
1280 + $template->set('item', '');
1281 + }
1282 + $template->save(true);
1283 + }
1284 + if (!isset($data['ID'])) {
1285 + $template->activate();
1286 + }
1287 + $response = array(
1288 + 'redirect' => $this->helper->get_url(
1289 + array(
1290 + 'page' => 'e2pdf-templates',
1291 + 'action' => 'edit',
1292 + 'id' => $template->get('ID'),
1293 + 'notification' => $this->add_notification('update', __('Template Saved', 'e2pdf')),
1294 + )
1295 + ),
1296 + );
1297 + }
1298 + } else {
1299 + $response['error'] = $this->message('wp_verify_nonce_error');
944 1300 }
945 -
946 - foreach ($data as $key => $value) {
947 - $template->set($key, $value);
948 - }
949 - $template->save(true);
950 -
951 - if ($template->get('activated')) {
952 - $this->activate_template($template->get('ID'));
953 - }
954 -
955 - $response = array(
956 - 'redirect' => $this->helper->get_url(array(
957 - 'page' => 'e2pdf-templates',
958 - 'action' => 'edit',
959 - 'id' => $template->get('ID'))
960 - )
961 - );
962 -
963 - $this->add_notification('update', __('Template saved successfully', 'e2pdf'));
964 1301 $this->json_response($response);
965 1302 }
966 1303
967 1304 public function ajax_get_styles() {
968 1305
969 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
970 - $data = $this->post->get('data');
971 -
972 - $extension = new Model_E2pdf_Extension();
973 - if ($extension->load($data)) {
974 - $content = $extension->get_styles();
1306 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1307 + $data = $this->post->get('data');
1308 + $content = array();
1309 + if (isset($data['extension']) && isset($data['item'])) {
1310 + $extension = new Model_E2pdf_Extension();
1311 + if ($extension->load($data['extension'])) {
1312 + $content = $extension->styles($data['item']);
1313 + }
1314 + }
1315 + $response = array(
1316 + 'content' => $content,
1317 + );
975 1318 } else {
976 - $content = array();
1319 + $response['error'] = $this->message('wp_verify_nonce_error');
977 1320 }
978 - $response = array(
979 - 'content' => $content,
980 - );
981 1321 $this->json_response($response);
982 1322 }
983 1323
984 - /**
985 - * Get extensions via ajax
986 - * action: wp_ajax_e2pdf_extension
987 - * function: e2pdf_extension
988 - *
989 - * @return json
990 - */
1324 + // ajax extension
991 1325 public function ajax_extension() {
992 -
993 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
994 - $data = $this->post->get('data');
995 -
996 - $extension = new Model_E2pdf_Extension();
997 -
998 - $content = array();
999 - if ($extension->load($data)) {
1000 - $content = $extension->get_items();
1326 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1327 + $data = $this->post->get('data');
1328 + $extension = new Model_E2pdf_Extension();
1329 + $content = array();
1330 + if ($extension->load($data)) {
1331 + $content = $extension->items();
1332 + if ($extension->method('merged_items')) {
1333 + $select = new stdClass();
1334 + $select->id = '-2';
1335 + $select->url = '';
1336 + $select->name = __('Merged Connections', 'e2pdf');
1337 + array_unshift($content, $select);
1338 + }
1339 + if ($extension->method('auto_form')) {
1340 + $select = new stdClass();
1341 + $select->id = '-1';
1342 + $select->url = '';
1343 + $select->name = __('Auto Form from PDF', 'e2pdf');
1344 + array_unshift($content, $select);
1345 + }
1346 + }
1347 + $select = new stdClass();
1348 + $select->id = '';
1349 + $select->url = '';
1350 + $select->name = __('--- Select ---', 'e2pdf');
1351 + array_unshift($content, $select);
1352 + $response = array(
1353 + 'content' => $content,
1354 + );
1355 + } else {
1356 + $response['error'] = $this->message('wp_verify_nonce_error');
1001 1357 }
1002 - array_unshift($content, array(
1003 - 'key' => '',
1004 - 'value' => __("--- Select ---", "e2pdf"),
1005 - )
1006 - );
1007 -
1008 -
1009 - $response = array(
1010 - 'content' => $content,
1011 - );
1012 1358 $this->json_response($response);
1013 1359 }
1014 1360
1015 - /**
1016 - * Get extensions via ajax
1017 - * action: wp_ajax_e2pdf_extension
1018 - * function: e2pdf_extension
1019 - *
1020 - * @return json
1021 - */
1361 + // ajax visual mapper
1022 1362 public function ajax_visual_mapper() {
1023 -
1024 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1025 -
1026 - $data = $this->post->get('data');
1027 -
1028 - $item = isset($data['item']) ? $data['item'] : '';
1029 -
1030 - $extension = new Model_E2pdf_Extension();
1031 - $extension->load($data['extension']);
1032 - $extension->set('item', $item);
1033 -
1034 -
1035 - if ($visual_mapper = $extension->visual_mapper()) {
1036 - $response = array(
1037 - 'content' => $visual_mapper
1038 - );
1039 - } elseif (!$item) {
1040 - $response = array(
1041 - 'content' => __("You must set Item to use <strong>Visual Mapper</strong>", "e2pdf"),
1042 - );
1363 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1364 + $data = $this->post->get('data');
1365 + $item = isset($data['item']) ? $data['item'] : '';
1366 + $item1 = isset($data['item1']) ? $data['item1'] : '';
1367 + $item2 = isset($data['item2']) ? $data['item2'] : '';
1368 + $extension = new Model_E2pdf_Extension();
1369 + $extension->load($data['extension']);
1370 + $extension->set('item', $item);
1371 + if ($item == '-2') {
1372 + $extension->set('item1', $item1);
1373 + $extension->set('item2', $item2);
1374 + }
1375 + $visual_mapper = $extension->visual_mapper();
1376 + if ($visual_mapper) {
1377 + $response = array(
1378 + 'content' => $visual_mapper,
1379 + );
1380 + } elseif (!$item || ($item == '-2' && !$item1 && !$item2)) {
1381 + $response = array(
1382 + 'content' => __('You must set the Connection to use the Visual Mapper', 'e2pdf'),
1383 + );
1384 + } else {
1385 + $response = array(
1386 + 'content' => __("Sorry, this Extension doesn't support the Visual Mapper", 'e2pdf'),
1387 + );
1388 + }
1043 1389 } else {
1044 - $response = array(
1045 - 'content' => __("Sorry, this extension doesn't support <strong>Visual Mapper</strong>", "e2pdf"),
1046 - );
1390 + $response['error'] = $this->message('wp_verify_nonce_error');
1047 1391 }
1048 -
1049 1392 $this->json_response($response);
1050 1393 }
1051 1394
1052 - /**
1053 - * Upload Template via Ajax
1054 - * action: wp_ajax_e2pdf_upload
1055 - * function: e2pdf_upload
1056 - *
1057 - * @return json
1058 - */
1395 + // ajax upload
1059 1396 public function ajax_upload() {
1060 1397
1061 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1398 + if (!wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1399 + $this->json_response(
1400 + [
1401 + 'error' => $this->message('wp_verify_nonce_error'),
1402 + ]
1403 + );
1404 + }
1062 1405
1063 1406 $data = $this->post->get();
1064 -
1065 1407 $post_extension = $data['extension'];
1066 - $item = $data['item'];
1408 + $item = isset($data['item']) ? $data['item'] : '';
1067 1409 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1410 + $font = isset($data['font']) ? $data['font'] : false;
1411 + $font_size = isset($data['font_size']) ? $data['font_size'] : false;
1412 + $line_height = isset($data['line_height']) ? $data['line_height'] : false;
1413 + $title = isset($data['title']) ? $data['title'] : __('(no title)', 'e2pdf');
1414 + $rtl = isset($data['rtl']) && $data['rtl'] ? '1' : '0';
1415 + $text_align = isset($data['text_align']) ? $data['text_align'] : 'left';
1068 1416
1069 - $allowed_ext = array(
1070 - 'pdf'
1071 - );
1072 -
1073 - $pdf = $this->files->get('pdf');
1074 -
1075 - $name = strtolower($pdf['name']);
1076 - $tmp = $pdf['tmp_name'];
1077 - $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
1078 -
1079 - if ($pdf['error']) {
1080 - $error = $pdf['error'];
1081 - } elseif (!in_array($ext, $allowed_ext)) {
1082 - $error = __('Incorrect file extension', 'e2pdf');
1083 - } elseif ($pdf['type'] != 'application/pdf') {
1084 - $error = __('Incompatible type', 'e2pdf');
1417 + $file = $this->helper->load('files')->upload($this->files->get('pdf'), ['pdf'], ['application/pdf']);
1418 + if (isset($file['error'])) {
1419 + $this->json_response(
1420 + [
1421 + 'error' => $file['error'],
1422 + ]
1423 + );
1085 1424 }
1086 1425
1087 - if ($error) {
1088 - $response = array(
1089 - 'error' => $error,
1090 - );
1426 + wp_raise_memory_limit('admin');
1427 + if (get_option('e2pdf_api_protocol', '0') == '1' && ($tmp = realpath($file['tmp_name']))) {
1428 + $upload = class_exists('CURLFile') ? new CURLFile($tmp) : '@' . $tmp;
1091 1429 } else {
1430 + $upload = base64_encode(file_get_contents($file['tmp_name']));
1431 + }
1092 1432
1093 - $model_e2pdf_api = new Model_E2pdf_Api();
1094 - $model_e2pdf_api->set(array(
1095 - 'action' => 'template/upload',
1096 - 'data' => array(
1097 - 'title' => $name,
1098 - 'pdf' => base64_encode(file_get_contents($pdf['tmp_name']))
1433 + $model_e2pdf_api = new Model_E2pdf_Api();
1434 + $model_e2pdf_api->set(
1435 + array(
1436 + 'action' => 'template/upload2',
1437 + 'data' => array(
1438 + 'title' => $file['name'],
1439 + 'pdf' => $upload,
1440 + ),
1099 1441 )
1100 - ));
1101 - $request = $model_e2pdf_api->request();
1442 + );
1443 + $request = $model_e2pdf_api->request();
1102 1444
1103 - if (!isset($request['error'])) {
1445 + if (isset($request['error'])) {
1446 + $this->json_response(
1447 + [
1448 + 'error' => $request['error'],
1449 + ]
1450 + );
1451 + }
1104 1452
1105 - $extension = new Model_E2pdf_Extension();
1106 - if ($post_extension && $item) {
1107 - $extension->load($post_extension);
1108 - $extension->set('item', $item);
1109 - }
1453 + $extension = new Model_E2pdf_Extension();
1454 + if ($post_extension) {
1455 + $extension->load($post_extension);
1456 + if ($item && $item != '-1') {
1457 + $extension->set('item', $item);
1458 + }
1459 + }
1110 1460
1111 - $pdf_name = md5(time());
1112 - $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
1113 - $pdf_images_dir = $pdf_dir . "images/";
1461 + $pdf_name = md5(time());
1462 + $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
1463 + $pdf_images_dir = $pdf_dir . 'images/';
1464 + $this->helper->create_dir($pdf_dir);
1465 + $this->helper->create_dir($pdf_images_dir);
1114 1466
1115 - $this->helper->create_dir($pdf_dir);
1116 - $this->helper->create_dir($pdf_images_dir);
1467 + if (!move_uploaded_file($file['tmp_name'], $pdf_dir . $pdf_name . '.pdf')) {
1468 + $this->json_response(
1469 + [
1470 + 'error' => 'Failed to move uploaded file',
1471 + ]
1472 + );
1473 + }
1117 1474
1118 - move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . ".pdf");
1119 -
1120 - $xml = simplexml_load_string(base64_decode($request['file']), "SimpleXMLElement", LIBXML_PARSEHUGE);
1121 - $pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages)));
1122 -
1123 - if (is_array($pages) && !empty($pages)) {
1124 - foreach ($pages as $page_key => $page) {
1125 -
1126 - $pages[$page_key]['page_id'] = $page_key;
1127 -
1128 - if (isset($page['properties']['background'])) {
1129 - file_put_contents($pdf_images_dir . $page_key . ".png", base64_decode($page['properties']['background']));
1130 - unset($pages[$page_key]['properties']['background']);
1131 - }
1132 -
1133 - if (isset($page['elements']) && !empty($page['elements'])) {
1134 - foreach ($page['elements'] as $element_key => $element) {
1135 - if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
1136 -
1137 - $image = base64_decode($element['base64']);
1138 - $file_name = basename($element['value']);
1139 -
1140 - //exif_imagetype
1141 - if (!$file_name) {
1142 - $helper_e2pdf_image = new Helper_E2pdf_Image();
1143 - $ext = $helper_e2pdf_image->get_image_extension($image);
1144 - if ($ext) {
1145 - $file_name = md5(mktime()) . "." . $ext;
1146 - }
1147 - }
1148 -
1149 - if ($file_name) {
1150 - $upload_file = wp_upload_bits($file_name, null, $image);
1151 - if (!$upload_file['error']) {
1152 - $wp_filetype = wp_check_filetype($file_name, null);
1153 - $attachment = array(
1154 - 'post_mime_type' => $wp_filetype['type'],
1155 - 'post_parent' => 0,
1156 - 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
1157 - 'post_content' => '',
1158 - 'post_status' => 'inherit'
1159 - );
1160 - $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
1161 - if (!is_wp_error($attachment_id)) {
1162 - require_once(ABSPATH . "wp-admin" . '/includes/image.php');
1163 - $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
1164 - wp_update_attachment_metadata($attachment_id, $attachment_data);
1165 - $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
1166 - }
1167 - }
1168 -
1169 - unset($pages[$page_key]['elements'][$element_key]['base64']);
1170 - } else {
1171 - unset($pages[$page_key]['elements'][$element_key]);
1172 - }
1173 - } elseif (isset($element['name']) && $element['name']) {
1174 - $el_value = $extension->auto_map($element['name']);
1175 - if ($el_value !== false) {
1176 - $pages[$page_key]['elements'][$element_key]['value'] = $el_value;
1177 - }
1475 + try {
1476 + $xml_template = [];
1477 + $pages = [];
1478 + if (class_exists('XMLReader')) {
1479 + $reader = new XMLReader();
1480 + if (!$reader->XML($request['file'])) {
1481 + throw new Exception(__('Failed to parse PDF', 'e2pdf'));
1482 + }
1483 + while ($reader->read()) {
1484 + if ($reader->nodeType == XMLReader::ELEMENT) {
1485 + switch ($reader->name) {
1486 + case 'template':
1487 + $template_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1488 + $xml_template = [
1489 + 'width' => (string) $template_node->width,
1490 + 'height' => (string) $template_node->height,
1491 + 'format' => (string) $template_node->format,
1492 + 'compression' => (string) $template_node->compression,
1493 + 'appearance' => (string) $template_node->appearance,
1494 + 'meta_title' => (string) $template_node->meta_title,
1495 + 'meta_subject' => (string) $template_node->meta_subject,
1496 + 'meta_author' => (string) $template_node->meta_author,
1497 + 'meta_keywords' => (string) $template_node->meta_keywords,
1498 + 'lang_code' => (string) $template_node->lang_code,
1499 + 'font' => (string) $template_node->font,
1500 + 'font_size' => (string) $template_node->font_size,
1501 + 'font_color' => (string) $template_node->font_color,
1502 + ];
1503 + break;
1504 + case 'page':
1505 + $page_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1506 + $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1507 + break;
1508 + case 'fonts':
1509 + $fonts_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1510 + foreach ($fonts_node->children() as $xml_font) {
1511 + $this->helper->load('xml')->parse_xml_font($xml_font);
1178 1512 }
1179 - }
1513 + break;
1514 + default:
1515 + break;
1180 1516 }
1181 1517 }
1518 + }
1182 1519
1183 - $helper_e2pdf_sort = new Helper_E2pdf_Sort();
1184 - $helper_e2pdf_sort->stable_uasort($pages, "sort_by_pageid");
1185 -
1186 - $template = new Model_E2pdf_Template();
1187 -
1188 - if ($template_id) {
1189 - $template->load($template_id);
1520 + $reader->close();
1521 + unset($reader);
1522 + } elseif (is_callable('simplexml_load_string')) {
1523 + $xml = simplexml_load_string($request['file'], 'SimpleXMLElement', LIBXML_PARSEHUGE);
1524 + if (isset($xml->pages)) {
1525 + foreach ($xml->pages->page as $page_node) {
1526 + $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1190 1527 }
1528 + }
1529 + if (isset($xml->template)) {
1530 + $xml_template = [
1531 + 'width' => (string) $xml->template->width,
1532 + 'height' => (string) $xml->template->height,
1533 + 'format' => (string) $xml->template->format,
1534 + 'compression' => (string) $xml->template->compression,
1535 + 'appearance' => (string) $xml->template->appearance,
1536 + 'meta_title' => (string) $xml->template->meta_title,
1537 + 'meta_subject' => (string) $xml->template->meta_subject,
1538 + 'meta_author' => (string) $xml->template->meta_author,
1539 + 'meta_keywords' => (string) $xml->template->meta_keywords,
1540 + 'lang_code' => (string) $xml->template->lang_code,
1541 + 'font' => (string) $xml->template->font,
1542 + 'font_size' => (string) $xml->template->font_size,
1543 + 'font_color' => (string) $xml->template->font_color,
1544 + ];
1545 + }
1546 + if (isset($xml->fonts) && $xml->fonts) {
1547 + foreach ($xml->fonts->children() as $key => $xml_font) {
1548 + $this->helper->load('xml')->parse_xml_font($xml_font);
1549 + }
1550 + }
1551 + }
1191 1552
1192 - if ($template_id) {
1193 - if ($template->get('pdf')) {
1194 - $old_pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/";
1195 - $this->helper->delete_dir($old_pdf_path);
1196 - }
1197 - $template->set('pages', $pages);
1198 - $template->set('pdf', $pdf_name);
1199 - $template->set('width', (String) $xml->template->width);
1200 - $template->set('height', (String) $xml->template->height);
1201 - } else {
1202 - $template->set('title', (String) $xml->template->title);
1203 - $template->set('flatten', (String) $xml->template->flatten);
1204 - $template->set('format', (String) $xml->template->format);
1205 - $template->set('compression', (String) $xml->template->compression);
1206 - $template->set('appearance', (String) $xml->template->appearance);
1207 - $template->set('width', (String) $xml->template->width);
1208 - $template->set('height', (String) $xml->template->height);
1209 - $template->set('extension', $post_extension);
1210 - $template->set('item', $item);
1211 - $template->set('dataset_title', "");
1212 - $template->set('button_title', "");
1213 - $template->set('inline', "");
1214 - $template->set('auto', "");
1215 - $template->set('name', "");
1216 - $template->set('password', "");
1217 - $template->set('font', (String) $xml->template->font);
1218 - $template->set('font_size', (String) $xml->template->font_size);
1219 - $template->set('font_color', (String) $xml->template->font_color);
1220 - $template->set('line_height', (String) $xml->template->font_size);
1221 - $template->set('pages', $pages);
1222 - $template->set('pdf', $pdf_name);
1223 - }
1553 + if (empty($pages) || !is_array($pages)) {
1554 + throw new Exception(__('Failed to parse PDF pages', 'e2pdf'));
1555 + }
1224 1556
1225 - if ($xml->fonts) {
1557 + $this->helper->load('sort')->stable_uasort($pages, 'sort_by_pageid');
1226 1558
1227 - $model_e2pdf_font = new Model_E2pdf_Font();
1228 - $fonts = $model_e2pdf_font->get_fonts();
1559 + $template = new Model_E2pdf_Template();
1560 + if ($template_id) {
1561 + $template->load($template_id);
1562 + $template->set('title', $title);
1563 + $template->set('pages', $pages);
1564 + $template->set('pdf', $pdf_name);
1565 + $template->set('width', $xml_template['width']);
1566 + $template->set('height', $xml_template['height']);
1567 + $template->set('extension', $post_extension);
1568 + $template->set('item', $item);
1569 + $template->set('rtl', $rtl);
1570 + $template->set('font', $font ? $font : $xml_template['font']);
1571 + $template->set('font_size', $font_size ? $font_size : $xml_template['font_size']);
1572 + $template->set('line_height', $line_height ? $line_height : $xml_template['font_size']);
1573 + $template->set('text_align', $text_align);
1574 + } else {
1575 + $template->set('title', $title);
1576 + $template->set('flatten', '1');
1577 + $template->set('format', $xml_template['format']);
1578 + $template->set('compression', $xml_template['compression']);
1579 + $template->set('appearance', $xml_template['appearance']);
1580 + $template->set('width', $xml_template['width']);
1581 + $template->set('height', $xml_template['height']);
1582 + $template->set('extension', $post_extension);
1583 + $template->set('item', $item);
1584 + $template->set('dataset_title', '');
1585 + $template->set('button_title', '');
1586 + $template->set('dpdf', '');
1587 + $template->set('attachments', '');
1588 + $template->set('inline', '');
1589 + $template->set('auto', '');
1590 + $template->set('rtl', $rtl);
1591 + $template->set('name', '[e2pdf-dataset]');
1592 + $template->set('savename', '[e2pdf-dataset]');
1593 + $template->set('password', '');
1594 + $template->set('meta_title', $xml_template['meta_title']);
1595 + $template->set('meta_subject', $xml_template['meta_subject']);
1596 + $template->set('meta_author', $xml_template['meta_author']);
1597 + $template->set('meta_keywords', $xml_template['meta_keywords']);
1598 + $template->set('lang_code', $xml_template['lang_code']);
1599 + $template->set('font', $font ? $font : $xml_template['font']);
1600 + $template->set('font_size', $font_size ? $font_size : $xml_template['font_size']);
1601 + $template->set('line_height', $line_height ? $line_height : $xml_template['font_size']);
1602 + $template->set('text_align', $text_align);
1603 + $template->set('font_color', $xml_template['font_color']);
1604 + $template->set('pages', $pages);
1605 + $template->set('pdf', $pdf_name);
1606 + }
1607 + $template->save(true);
1229 1608
1230 - foreach ($xml->fonts->children() as $key => $font) {
1231 - $title = (String) $font->title;
1232 - $name = (String) $font->name;
1233 - $value = (String) $font->value;
1609 + if ($item == '-1' && $extension->method('auto_form')) {
1610 + $template = $extension->auto_form($template, $data);
1611 + $template->save(true);
1612 + }
1234 1613
1235 - $exist = array_search($title, $fonts);
1236 - if (!$exist) {
1237 - if (!file_exists($this->helper->get('fonts_dir') . $name)) {
1238 - $f_name = $name;
1239 - } else {
1240 - $i = 0;
1241 - do {
1242 - $f_name = $i . '_' . $name;
1243 - $i++;
1244 - } while (file_exists($this->helper->get('fonts_dir') . $f_name));
1245 - }
1246 - file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value));
1247 - }
1248 - }
1249 - }
1250 - $template->save(true);
1614 + if (!$template_id) {
1615 + $template->activate();
1616 + }
1251 1617
1252 - $response = array(
1253 - 'redirect' => $this->helper->get_url(array(
1254 - 'page' => 'e2pdf-templates',
1255 - 'action' => 'edit',
1256 - 'id' => $template->get('ID'))
1257 - )
1258 - );
1259 - } else {
1260 - $this->helper->delete_dir($pdf_dir);
1261 - $response = array(
1262 - 'error' => __("PDF can't be parsed", 'e2pdf'),
1263 - );
1264 - }
1265 - } else {
1266 - $response = array(
1267 - 'error' => $request['error'],
1268 - );
1618 + $this->json_response(
1619 + [
1620 + 'redirect' => $this->helper->get_url(
1621 + [
1622 + 'page' => 'e2pdf-templates',
1623 + 'action' => 'edit',
1624 + 'id' => $template->get('ID'),
1625 + ]
1626 + ),
1627 + ]
1628 + );
1629 + } catch (Exception $ex) {
1630 + if (file_exists($pdf_dir)) {
1631 + $this->helper->delete_dir($pdf_dir);
1269 1632 }
1633 + $this->json_response(
1634 + [
1635 + 'error' => $ex->getMessage(),
1636 + ]
1637 + );
1270 1638 }
1271 -
1272 - $this->json_response($response);
1273 1639 }
1274 1640
1275 - /**
1276 - * Upload Template via Ajax
1277 - * action: wp_ajax_e2pdf_upload
1278 - * function: e2pdf_upload
1279 - *
1280 - * @return json
1281 - */
1641 + // ajax reupload
1282 1642 public function ajax_reupload() {
1283 1643
1644 + if (!wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1645 + $this->json_response(
1646 + [
1647 + 'error' => $this->message('wp_verify_nonce_error'),
1648 + ]
1649 + );
1650 + }
1284 1651
1285 - $error = false;
1286 -
1287 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1288 -
1289 1652 $data = $this->post->get();
1290 -
1291 1653 $new = isset($data['new']) ? $data['new'] : array();
1292 1654 $flush = isset($data['flush']) ? $data['flush'] : array();
1293 1655 $positions = isset($data['positions']) ? $data['positions'] : array();
1294 -
1295 1656 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1296 -
1297 1657 if (!$template_id) {
1298 1658 return false;
1299 1659 }
1300 1660
1301 - $allowed_ext = array(
1302 - 'pdf'
1303 - );
1661 + $file = $this->helper->load('files')->upload($this->files->get('pdf'), ['pdf'], ['application/pdf']);
1662 + if (isset($file['error'])) {
1663 + $this->json_response(
1664 + [
1665 + 'error' => $file['error'],
1666 + ]
1667 + );
1668 + }
1304 1669
1305 - $pdf = $this->files->get('pdf');
1670 + wp_raise_memory_limit('admin');
1671 + if (get_option('e2pdf_api_protocol', '0') == '1' && ($tmp = realpath($file['tmp_name']))) {
1672 + $upload = class_exists('CURLFile') ? new CURLFile($tmp) : '@' . $tmp;
1673 + } else {
1674 + $upload = base64_encode(file_get_contents($file['tmp_name']));
1675 + }
1306 1676
1307 - $name = strtolower($pdf['name']);
1308 - $tmp = $pdf['tmp_name'];
1309 - $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
1677 + $model_e2pdf_api = new Model_E2pdf_Api();
1678 + $model_e2pdf_api->set(
1679 + array(
1680 + 'action' => 'template/upload2',
1681 + 'data' => array(
1682 + 'title' => $file['name'],
1683 + 'pdf' => $upload,
1684 + ),
1685 + )
1686 + );
1687 + $request = $model_e2pdf_api->request();
1310 1688
1311 - if ($pdf['error']) {
1312 - $error = $pdf['error'];
1313 - } elseif (!in_array($ext, $allowed_ext)) {
1314 - $error = __('Incorrect file extension', 'e2pdf');
1315 - } elseif ($pdf['type'] != 'application/pdf') {
1316 - $error = __('Incompatible type', 'e2pdf');
1689 + if (isset($request['error'])) {
1690 + $this->json_response(
1691 + [
1692 + 'error' => $request['error'],
1693 + ]
1694 + );
1317 1695 }
1318 1696
1319 - if ($error) {
1320 - $response = array(
1321 - 'error' => $error,
1697 + $template = new Model_E2pdf_Template();
1698 + if (!$template->load($template_id)) {
1699 + $this->json_response(
1700 + [
1701 + 'error' => __('Failed to load E2Pdf Template', 'e2pdf'),
1702 + ]
1322 1703 );
1323 - } else {
1704 + }
1324 1705
1325 - $model_e2pdf_api = new Model_E2pdf_Api();
1326 - $model_e2pdf_api->set(array(
1327 - 'action' => 'template/upload',
1328 - 'data' => array(
1329 - 'title' => $name,
1330 - 'pdf' => base64_encode(file_get_contents($pdf['tmp_name']))
1331 - )
1332 - ));
1333 - $request = $model_e2pdf_api->request();
1706 + $extension = new Model_E2pdf_Extension();
1707 + if ($template->get('extension') && $template->get('item')) {
1708 + $extension->load($template->get('extension'));
1709 + $extension->set('item', $template->get('item'));
1710 + }
1334 1711
1335 - if (!isset($request['error'])) {
1712 + $pdf_name = md5(time());
1713 + $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
1714 + $pdf_images_dir = $pdf_dir . 'images/';
1715 + $this->helper->create_dir($pdf_dir);
1716 + $this->helper->create_dir($pdf_images_dir);
1336 1717
1337 - $template = new Model_E2pdf_Template();
1338 - if ($template->load($template_id)) {
1718 + if (!move_uploaded_file($file['tmp_name'], $pdf_dir . $pdf_name . '.pdf')) {
1719 + $this->json_response(
1720 + [
1721 + 'error' => 'Failed to move uploaded file',
1722 + ]
1723 + );
1724 + }
1339 1725
1340 - if ($template->get('pdf')) {
1341 -
1342 - $extension = new Model_E2pdf_Extension();
1343 - if ($template->get('extension') && $template->get('item')) {
1344 - $extension->load($template->get('extension'));
1345 - $extension->set('item', $template->get('item'));
1726 + try {
1727 + $pages = [];
1728 + $pos_pages = [];
1729 + if (class_exists('XMLReader')) {
1730 + $reader = new XMLReader();
1731 + if (!$reader->XML($request['file'])) {
1732 + throw new Exception(__('Failed to parse PDF', 'e2pdf'));
1733 + }
1734 + while ($reader->read()) {
1735 + if ($reader->nodeType == XMLReader::ELEMENT) {
1736 + if ($reader->name == 'page') {
1737 + $page_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1738 + $pos_pages[] = (string) $page_node['number'];
1346 1739 }
1347 -
1348 - $xml = simplexml_load_string(base64_decode($request['file']), "SimpleXMLElement", LIBXML_PARSEHUGE);
1349 - $pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages)));
1350 -
1351 - foreach ($pages as $page_key => $page) {
1352 - $pages[$page_key]['page_id'] = $page_key;
1740 + }
1741 + }
1742 + $reader->close();
1743 + unset($reader);
1744 + foreach ($positions as $pos_key => $pos_page) {
1745 + if (!in_array($pos_key, $flush)) {
1746 + if ($pos_page == '0') {
1747 + $flush[] = $pos_key;
1748 + } elseif (!in_array($pos_page, $pos_pages, false)) {
1749 + throw new Exception(sprintf(__('Invalid position of %s page', 'e2pdf'), $pos_key));
1353 1750 }
1751 + }
1752 + }
1753 + $reader = new XMLReader();
1754 + $reader->XML($request['file']);
1755 + while ($reader->read()) {
1756 + if ($reader->nodeType == XMLReader::ELEMENT) {
1757 + switch ($reader->name) {
1758 + case 'page':
1759 + $page_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1760 + $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1761 + break;
1762 + default:
1763 + break;
1764 + }
1765 + }
1766 + }
1767 + $reader->close();
1768 + unset($reader);
1769 + } elseif (is_callable('simplexml_load_string')) {
1770 + $xml = simplexml_load_string($request['file'], 'SimpleXMLElement', LIBXML_PARSEHUGE);
1771 + if (isset($xml->pages)) {
1772 + foreach ($xml->pages->page as $page_node) {
1773 + $pos_pages[] = (string) $page_node['number'];
1774 + }
1775 + }
1776 + foreach ($positions as $pos_key => $pos_page) {
1777 + if (!in_array($pos_key, $flush)) {
1778 + if ($pos_page == '0') {
1779 + $flush[] = $pos_key;
1780 + } elseif (!in_array($pos_page, $pos_pages, false)) {
1781 + throw new Exception(sprintf(__('Invalid position of %s page', 'e2pdf'), $pos_key));
1782 + }
1783 + }
1784 + }
1785 + if (isset($xml->pages)) {
1786 + foreach ($xml->pages->page as $page_node) {
1787 + $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1788 + }
1789 + }
1790 + }
1354 1791
1355 - $helper_e2pdf_sort = new Helper_E2pdf_Sort();
1356 - $helper_e2pdf_sort->stable_uasort($pages, "sort_by_pageid");
1792 + if (empty($pages) || !is_array($pages)) {
1793 + throw new Exception(__('Failed to parse PDF pages', 'e2pdf'));
1794 + }
1357 1795
1358 - $model_e2pdf_element = new Model_E2pdf_Element();
1359 - $last_element_id = $model_e2pdf_element->get_last_element_id($template_id);
1796 + $this->helper->load('sort')->stable_uasort($pages, 'sort_by_pageid');
1360 1797
1361 - $original_pages = $template->get('pages');
1798 + $model_e2pdf_element = new Model_E2pdf_Element();
1799 + $last_element_id = $model_e2pdf_element->get_last_element_id($template_id);
1800 + $original_pages = $template->get('pages');
1362 1801
1363 - foreach ($positions as $pos_key => $pos_page) {
1364 - if (!in_array($pos_key, $flush)) {
1365 - if (!isset($pages[$pos_page])) {
1366 - $error = sprintf(__("Position of %s page incorrect", 'e2pdf'), $pos_key);
1367 - } elseif ($pages[$pos_page]['properties']['width'] < $original_pages[$pos_key]['properties']['width']) {
1368 - $error = sprintf(__("Width of PDF Page %s can't be less width of Template Page %s", 'e2pdf'), $pos_page, $pos_key);
1369 - } elseif ($pages[$pos_page]['properties']['height'] < $original_pages[$pos_key]['properties']['height']) {
1370 - $error = sprintf(__("Height of PDF Page %s can't be less height of Template Page %s", 'e2pdf'), $pos_page, $pos_key);
1371 - }
1802 + foreach ($pages as $page_key => $page) {
1803 + if (isset($page['elements']) && !empty($page['elements'])) {
1804 + foreach ($page['elements'] as $element_key => $element) {
1805 + $last_element_id++;
1806 + $pages[$page_key]['elements'][$element_key]['element_id'] = $last_element_id;
1807 + }
1808 + }
1809 + foreach ($positions as $pos_key => $pos_page) {
1810 + if ($pos_page == $page_key) {
1811 + if (in_array($pos_key, $new)) {
1812 + if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1813 + $elements = $original_pages[$pos_key]['elements'];
1814 + $pages[$page_key]['elements'] = array_merge($pages[$page_key]['elements'], $elements);
1372 1815 }
1373 - }
1374 -
1375 - if (!$error) {
1376 -
1377 - $pdf_name = md5(time());
1378 - $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
1379 - $pdf_images_dir = $pdf_dir . "images/";
1380 -
1381 - $this->helper->create_dir($pdf_dir);
1382 - $this->helper->create_dir($pdf_images_dir);
1383 -
1384 - move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . ".pdf");
1385 -
1386 - if (is_array($pages) && !empty($pages)) {
1387 - foreach ($pages as $page_key => $page) {
1388 -
1389 - if (isset($page['properties']['background'])) {
1390 - file_put_contents($pdf_images_dir . $page_key . ".png", base64_decode($page['properties']['background']));
1391 - unset($pages[$page_key]['properties']['background']);
1392 - }
1393 -
1394 - if (isset($page['elements']) && !empty($page['elements'])) {
1395 - foreach ($page['elements'] as $element_key => $element) {
1396 -
1397 - $last_element_id++;
1398 - $pages[$page_key]['elements'][$element_key]['element_id'] = $last_element_id;
1399 -
1400 - if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
1401 -
1402 - $image = base64_decode($element['base64']);
1403 - $file_name = basename($element['value']);
1404 -
1405 - //exif_imagetype
1406 - if (!$file_name) {
1407 - $helper_e2pdf_image = new Helper_E2pdf_Image();
1408 - $ext = $helper_e2pdf_image->get_image_extension($image);
1409 - if ($ext) {
1410 - $file_name = md5(mktime()) . "." . $ext;
1411 - }
1412 - }
1413 -
1414 - if ($file_name) {
1415 - $upload_file = wp_upload_bits($file_name, null, $image);
1416 - if (!$upload_file['error']) {
1417 - $wp_filetype = wp_check_filetype($file_name, null);
1418 - $attachment = array(
1419 - 'post_mime_type' => $wp_filetype['type'],
1420 - 'post_parent' => 0,
1421 - 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
1422 - 'post_content' => '',
1423 - 'post_status' => 'inherit'
1424 - );
1425 - $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
1426 - if (!is_wp_error($attachment_id)) {
1427 - require_once(ABSPATH . "wp-admin" . '/includes/image.php');
1428 - $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
1429 - wp_update_attachment_metadata($attachment_id, $attachment_data);
1430 - $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
1431 - }
1432 - }
1433 - unset($pages[$page_key]['elements'][$element_key]['base64']);
1434 - } else {
1435 - unset($pages[$page_key]['elements'][$element_key]);
1436 - }
1437 - } elseif (isset($element['name']) && $element['name']) {
1438 - $el_value = $extension->auto_map($element['name']);
1439 - if ($el_value !== false) {
1440 - $pages[$page_key]['elements'][$element_key]['value'] = $el_value;
1441 - }
1442 - }
1443 - }
1444 - }
1445 -
1446 -
1447 - foreach ($positions as $pos_key => $pos_page) {
1448 - if ($pos_page == $page_key) {
1449 - if (in_array($pos_key, $new)) {
1450 - if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1451 - $elements = $original_pages[$pos_key]['elements'];
1452 - $pages[$page_key]['elements'] = array_merge($pages[$page_key]['elements'], $elements);
1453 - }
1454 - } else {
1455 - if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1456 - $elements = $original_pages[$pos_key]['elements'];
1457 - $pages[$page_key]['elements'] = $elements;
1458 - } else {
1459 - $pages[$page_key]['elements'] = array();
1460 - }
1461 - }
1462 - }
1463 - }
1464 - }
1465 -
1466 - $pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/";
1467 - $this->helper->delete_dir($pdf_path);
1468 -
1469 - $template->set('pdf', $pdf_name);
1470 - $template->set('pages', $pages);
1471 - $template->save(true);
1472 -
1473 - $response = array(
1474 - 'redirect' => $this->helper->get_url(array(
1475 - 'page' => 'e2pdf-templates',
1476 - 'action' => 'edit',
1477 - 'id' => $template->get('ID'))
1478 - )
1479 - );
1816 + } else {
1817 + if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1818 + $elements = $original_pages[$pos_key]['elements'];
1819 + $pages[$page_key]['elements'] = $elements;
1480 1820 } else {
1481 - $this->helper->delete_dir($pdf_dir);
1482 - $response = array(
1483 - 'error' => __("PDF can't be parsed", 'e2pdf'),
1484 - );
1821 + $pages[$page_key]['elements'] = array();
1485 1822 }
1486 - } else {
1487 - $response = array(
1488 - 'error' => $error,
1489 - );
1490 1823 }
1824 + if (isset($original_pages[$pos_key]['actions'])) {
1825 + $pages[$page_key]['actions'] = $original_pages[$pos_key]['actions'];
1826 + }
1491 1827 }
1492 1828 }
1493 - } else {
1494 - $response = array(
1495 - 'error' => $request['error'],
1496 - );
1497 1829 }
1830 +
1831 + $template->set('pdf', $pdf_name);
1832 + $template->set('pages', $pages);
1833 + $template->save(true);
1834 +
1835 + $this->json_response(
1836 + [
1837 + 'redirect' => $this->helper->get_url(
1838 + array(
1839 + 'page' => 'e2pdf-templates',
1840 + 'action' => 'edit',
1841 + 'id' => $template->get('ID'),
1842 + )
1843 + ),
1844 + ]
1845 + );
1846 + } catch (Exception $ex) {
1847 + if (file_exists($pdf_dir)) {
1848 + $this->helper->delete_dir($pdf_dir);
1849 + }
1850 + $this->json_response(
1851 + [
1852 + 'error' => $ex->getMessage(),
1853 + ]
1854 + );
1498 1855 }
1499 -
1500 - $this->json_response($response);
1501 1856 }
1502 1857
1503 - /**
1504 - * Auto Generation of Template
1505 - * action: wp_ajax_e2pdf_auto
1506 - * function: e2pdf_auto
1507 - *
1508 - * @return json
1509 - */
1858 + // ajax auto
1510 1859 public function ajax_auto() {
1511 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1512 - $data = $this->post->get('data');
1860 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1861 + $data = $this->post->get('data');
1513 1862
1514 - $extension = $data['extension'];
1515 - $item = $data['item'];
1863 + $extension = $data['extension'];
1864 + $item = isset($data['item']) ? $data['item'] : '';
1865 + $item1 = isset($data['item1']) ? $data['item1'] : '';
1866 + $item2 = isset($data['item2']) ? $data['item2'] : '';
1867 + $font_size = isset($data['font_size']) ? $data['font_size'] : '14';
1868 + $line_height = isset($data['line_height']) ? $data['line_height'] : '14';
1516 1869
1517 - if (!$extension || !$item) {
1518 - return;
1519 - }
1870 + if (!$extension || !$item) {
1871 + return;
1872 + }
1520 1873
1521 - $model_e2pdf_extension = new Model_E2pdf_Extension();
1522 - $model_e2pdf_extension->load($extension);
1523 - $model_e2pdf_extension->set('item', $item);
1874 + $model_e2pdf_extension = new Model_E2pdf_Extension();
1875 + $model_e2pdf_extension->load($extension);
1876 + $model_e2pdf_extension->set('item', $item);
1877 + $model_e2pdf_extension->set('item1', $item1);
1878 + $model_e2pdf_extension->set('item2', $item2);
1879 + $model_e2pdf_extension->set('font_size', $font_size);
1880 + $model_e2pdf_extension->set('line_height', $line_height);
1524 1881
1525 - if (method_exists($model_e2pdf_extension->extension(), 'auto')) {
1526 - $content = $model_e2pdf_extension->auto();
1882 + if (method_exists($model_e2pdf_extension->extension(), 'auto')) {
1883 + $content = $model_e2pdf_extension->auto();
1884 + } else {
1885 + $content = array();
1886 + }
1887 +
1888 + $response = array(
1889 + 'content' => $content,
1890 + );
1527 1891 } else {
1528 - $content = array();
1892 + $response['error'] = $this->message('wp_verify_nonce_error');
1529 1893 }
1530 -
1531 - $response = array(
1532 - 'content' => $content,
1533 - );
1534 1894 $this->json_response($response);
1535 1895 }
1536 1896
1897 + // ajax activate template
1537 1898 public function ajax_activate_template() {
1538 -
1539 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1540 - $data = $this->post->get('data');
1541 -
1542 - $id = $data['id'];
1543 -
1544 - if (!$id) {
1545 - return;
1546 - }
1547 -
1548 - $request = $this->activate_template($id);
1549 - if (isset($request['error'])) {
1550 - $response = array(
1551 - 'error' => $request['error'],
1552 - );
1899 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1900 + $data = $this->post->get('data');
1901 + $id = $data['id'];
1902 + if (!$id) {
1903 + return;
1904 + }
1905 + $request = $this->activate_template($id);
1906 + if (isset($request['error'])) {
1907 + $response = array(
1908 + 'error' => $request['error'],
1909 + );
1910 + } else {
1911 + $response = array(
1912 + 'content' => 'activated',
1913 + );
1914 + }
1553 1915 } else {
1554 - $response = array(
1555 - 'content' => true,
1556 - );
1916 + $response['error'] = $this->message('wp_verify_nonce_error');
1557 1917 }
1558 -
1559 -
1560 1918 $this->json_response($response);
1561 1919 }
1562 1920
1921 + // ajax deactivaate template
1563 1922 public function ajax_deactivate_template() {
1564 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1565 - $data = $this->post->get('data');
1566 -
1567 - $id = $data['id'];
1568 -
1569 - if (!$id) {
1570 - return;
1923 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1924 + $data = $this->post->get('data');
1925 + $id = $data['id'];
1926 + if (!$id) {
1927 + return;
1928 + }
1929 + $request = $this->deactivate_template($id);
1930 + if (isset($request['error'])) {
1931 + $response = array(
1932 + 'error' => $request['error'],
1933 + );
1934 + } else {
1935 + $response = array(
1936 + 'content' => 'deactivated',
1937 + );
1938 + }
1939 + } else {
1940 + $response['error'] = $this->message('wp_verify_nonce_error');
1571 1941 }
1942 + $this->json_response($response);
1943 + }
1572 1944
1573 - $request = $this->deactivate_template($id);
1574 - if (isset($request['error'])) {
1575 - $response = array(
1576 - 'error' => $request['error'],
1945 + // ajax email
1946 + public function ajax_email() {
1947 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1948 + $data = $this->post->get('data');
1949 + $email = isset($data['email']) ? trim($data['email']) : '';
1950 + $email_code = isset($data['email_code']) ? trim($data['email_code']) : '';
1951 + $model_e2pdf_api = new Model_E2pdf_Api();
1952 + $model_e2pdf_api->set(
1953 + array(
1954 + 'action' => 'common/email',
1955 + 'data' => array(
1956 + 'email' => $email,
1957 + 'email_code' => $email_code,
1958 + 'email_confirm' => isset($data['email_code']),
1959 + ),
1960 + )
1577 1961 );
1962 + $request = $model_e2pdf_api->request();
1963 + if (isset($request['error'])) {
1964 + if ($request['error'] === 'incorrect_email') {
1965 + $response = array(
1966 + 'error' => __('Invalid E-mail address format', 'e2pdf'),
1967 + );
1968 + } elseif ($request['error'] === 'incorrect_code') {
1969 + $response = array(
1970 + 'error' => __('Invalid Confirmation Code', 'e2pdf'),
1971 + );
1972 + } else {
1973 + $response = array(
1974 + 'error' => $request['error'],
1975 + );
1976 + }
1977 + } elseif (isset($request['success'])) {
1978 + $response = array(
1979 + 'content' => $request['success'],
1980 + );
1981 + if ($request['success'] == 'subscribed') {
1982 + update_option('e2pdf_email', $email);
1983 + }
1984 + }
1578 1985 } else {
1579 - $response = array(
1580 - 'content' => true,
1581 - );
1986 + $response['error'] = $this->message('wp_verify_nonce_error');
1582 1987 }
1583 1988 $this->json_response($response);
1584 1989 }
1585 1990
1586 - /**
1587 - * Move template to trash
1588 - *
1589 - * @param int $id - ID of template
1590 - */
1991 + // trash template
1591 1992 public function trash_template($id = false) {
1592 -
1593 1993 $id = (int) $id;
1594 -
1595 - if (!$id) {
1596 - return false;
1994 + $template = new Model_E2pdf_Template();
1995 + if ($id && $template->load($id, false)) {
1996 + $template->set('trash', '1');
1997 + $template->update(array('trash'));
1998 + $this->deactivate_template($template->get('ID'));
1999 + return true;
1597 2000 }
1598 - $template = new Model_E2pdf_Template();
1599 - $template->load($id);
1600 - $template->set('trash', '1');
1601 - $template->save();
2001 + return false;
1602 2002 }
1603 2003
1604 - /**
1605 - * Delete template
1606 - *
1607 - * @param int $id - ID of template
1608 - */
2004 + // delete template
1609 2005 public function delete_template($id = false) {
1610 -
1611 2006 $id = (int) $id;
1612 -
1613 - if (!$id) {
1614 - return false;
2007 + $template = new Model_E2pdf_Template();
2008 + if ($id && $template->load($id)) {
2009 + $this->deactivate_template($template->get('ID'));
2010 + $template->delete();
2011 + return true;
1615 2012 }
1616 -
1617 - $template = new Model_E2pdf_Template();
1618 - $template->load($id);
1619 - $this->deactivate_template($template->get('ID'));
1620 - $template->delete();
2013 + return false;
1621 2014 }
1622 2015
1623 - /**
1624 - * Restore template
1625 - *
1626 - * @param int $id - ID of template
1627 - */
2016 + // restore template
1628 2017 public function restore_template($id = false) {
1629 -
1630 2018 $id = (int) $id;
1631 -
1632 - if (!$id) {
1633 - return false;
2019 + $template = new Model_E2pdf_Template();
2020 + if ($id && $template->load($id, false)) {
2021 + $template->set('trash', '0');
2022 + $template->update(array('trash'));
2023 + return true;
1634 2024 }
1635 - $template = new Model_E2pdf_Template();
1636 - $template->load($id);
1637 - $template->set('trash', '0');
1638 - $template->save();
2025 + return false;
1639 2026 }
1640 2027
1641 - /**
1642 - * Duplicate template
1643 - *
1644 - * @param int $id - ID of template
1645 - */
2028 + // duplicate template
1646 2029 public function duplicate_template($id = false) {
1647 2030
1648 2031 $id = (int) $id;
1649 2032
1650 - if (!$id) {
1651 - return false;
1652 - }
1653 -
1654 2033 $template = new Model_E2pdf_Template();
1655 - $template->load($id);
2034 + if ($id && $template->load($id)) {
2035 + $title = $template->get('title') . ' ' . __('(Copy)', 'e2pdf');
2036 + $template->set('ID', false);
2037 + $template->set('title', $title);
2038 + $template->set('uid', '');
2039 + $template->set('activated', '0');
2040 + if ($template->get('pdf')) {
2041 + $pdf_name = md5(time());
2042 + $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
2043 + $pdf_images_dir = $pdf_dir . 'images/';
1656 2044
1657 - $title = $template->get('title') . " " . __('(Copy)', 'e2pdf');
1658 - $template->set('ID', false);
1659 - $template->set('uid', '');
1660 - $template->set('title', $title);
1661 - $template->set('activated', '0');
2045 + $this->helper->create_dir($pdf_dir);
2046 + $this->helper->create_dir($pdf_images_dir);
1662 2047
1663 - if ($template->get('pdf')) {
1664 - $pdf_name = md5(time());
1665 - $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
1666 - $pdf_images_dir = $pdf_dir . "images/";
1667 -
1668 - $this->helper->create_dir($pdf_dir);
1669 - $this->helper->create_dir($pdf_images_dir);
1670 -
1671 -
1672 - if (file_exists($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf")) {
1673 - $images = glob($this->helper->get('pdf_dir') . $template->get('pdf') . "/images/*");
1674 - foreach ($images as $image) {
1675 - copy($image, $pdf_images_dir . pathinfo($image, PATHINFO_BASENAME));
2048 + if (file_exists($this->helper->get('pdf_dir') . $template->get('pdf') . '/' . $template->get('pdf') . '.pdf')) {
2049 + $images = glob($this->helper->get('pdf_dir') . $template->get('pdf') . '/images/*');
2050 + foreach ($images as $image) {
2051 + copy($image, $pdf_images_dir . pathinfo($image, PATHINFO_BASENAME));
2052 + }
2053 + copy($this->helper->get('pdf_dir') . $template->get('pdf') . '/' . $template->get('pdf') . '.pdf', $this->helper->get('pdf_dir') . $pdf_name . '/' . $pdf_name . '.pdf');
2054 + $template->set('pdf', $pdf_name);
1676 2055 }
1677 - copy($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf", $this->helper->get('pdf_dir') . $pdf_name . "/" . $pdf_name . ".pdf");
1678 - $template->set('pdf', $pdf_name);
1679 2056 }
2057 + $template->save(true);
2058 + return true;
1680 2059 }
1681 -
1682 -
1683 - $template->save(true);
2060 + return false;
1684 2061 }
1685 2062
1686 - /**
1687 - * Activate template
1688 - *
1689 - * @param int $id - ID of template
1690 - */
2063 + // activate template
1691 2064 public function activate_template($id = false) {
1692 -
1693 2065 $id = (int) $id;
1694 -
1695 - if (!$id) {
1696 - return false;
1697 - }
1698 -
1699 2066 $template = new Model_E2pdf_Template();
1700 - $template->load($id);
1701 -
1702 - $model_e2pdf_api = new Model_E2pdf_Api();
1703 - $model_e2pdf_api->set(array(
1704 - 'action' => 'template/activate',
1705 - 'data' => array(
1706 - 'template_id' => $template->get('ID'),
1707 - 'template_uid' => $template->get('uid'),
1708 - 'template_title' => $template->get('title')
1709 - )
1710 - ));
1711 - $request = $model_e2pdf_api->request();
1712 -
1713 - if (!isset($request['error'])) {
1714 - $template->set('activated', 1);
1715 - $template->set('uid', $request['template_uid']);
1716 - $template->save();
2067 + if ($id && $template->load($id, false)) {
2068 + return $template->activate();
2069 + } else {
2070 + return array(
2071 + 'error' => __('Something went wrong!', 'e2pdf'),
2072 + );
1717 2073 }
1718 -
1719 - return $request;
1720 2074 }
1721 2075
1722 - /**
1723 - * Deactivate template
1724 - *
1725 - * @param int $id - ID of template
1726 - */
2076 + // deactivate template
1727 2077 public function deactivate_template($id = false) {
1728 -
1729 2078 $id = (int) $id;
1730 -
1731 - if (!$id) {
1732 - return false;
1733 - }
1734 -
1735 2079 $template = new Model_E2pdf_Template();
1736 - $template->load($id);
1737 -
1738 - $model_e2pdf_api = new Model_E2pdf_Api();
1739 - $model_e2pdf_api->set(array(
1740 - 'action' => 'template/deactivate',
1741 - 'data' => array(
1742 - 'template_id' => $template->get('ID'),
1743 - 'template_uid' => $template->get('uid'),
1744 - )
1745 - ));
1746 - $request = $model_e2pdf_api->request();
1747 -
1748 - if (!isset($request['error'])) {
1749 - $template->set('activated', 0);
1750 - $template->set('uid', '');
1751 - $template->save();
2080 + if ($id && $template->load($id, false)) {
2081 + return $template->deactivate();
2082 + } else {
2083 + return array(
2084 + 'error' => __('Something went wrong!', 'e2pdf'),
2085 + );
1752 2086 }
1753 -
1754 - return $request;
1755 2087 }
1756 2088
1757 - /**
1758 - * Get templates list
1759 - *
1760 - * @param array() $filters - Array of filter/order conditions
1761 - * @param bool $count - Return number of templates
1762 - *
1763 - * @return mixed - IF $count - int ELSE array()
1764 - */
2089 + // get templates list
1765 2090 public function get_templates_list($filters = array(), $count = false) {
1766 2091 global $wpdb;
2092 +
1767 2093 $model_e2pdf_template = new Model_E2pdf_Template();
1768 - $helper_e2pdf_db = new Helper_E2pdf_Db();
1769 -
1770 2094 $condition = array();
1771 2095 if (isset($filters['status']) && $filters['status'] == 'trash') {
1772 2096 $condition = array(
1773 2097 'trash' => array(
@@ -1772,10 +2096,10 @@
1772 2096 $condition = array(
1773 2097 'trash' => array(
1774 2098 'condition' => '=',
1775 2099 'value' => '1',
1776 - 'type' => '%d'
1777 - )
2100 + 'type' => '%d',
2101 + ),
1778 2102 );
1779 2103 } else {
1780 2104 $condition = array(
1781 2105 'trash' => array(
@@ -1780,10 +2104,10 @@
1780 2104 $condition = array(
1781 2105 'trash' => array(
1782 2106 'condition' => '<>',
1783 2107 'value' => '1',
1784 - 'type' => '%d'
1785 - )
2108 + 'type' => '%d',
2109 + ),
1786 2110 );
1787 2111 }
1788 2112
1789 2113 if (isset($filters['s']) && $filters['s']) {
@@ -1789,18 +2113,27 @@
1789 2113 if (isset($filters['s']) && $filters['s']) {
1790 2114 $condition['title'] = array(
1791 2115 'condition' => 'LIKE',
1792 2116 'value' => '%%' . $filters['s'] . '%%',
1793 - 'type' => '%s'
2117 + 'type' => '%s',
2118 + 'or' => array(
2119 + array(
2120 + 'ID' => array(
2121 + 'condition' => 'LIKE',
2122 + 'value' => '%%' . $filters['s'] . '%%',
2123 + 'type' => '%s',
2124 + ),
2125 + ),
2126 + ),
1794 2127 );
1795 2128 }
1796 2129
1797 -
1798 2130 $order_condition = array();
1799 - if (isset($filters['orderby']) && isset($filters['order'])) {
2131 + $sortable = array('id', 'title', 'updated_at');
2132 + if (isset($filters['orderby']) && in_array($filters['orderby'], $sortable) && isset($filters['order'])) {
1800 2133 $order_condition = array(
1801 2134 'orderby' => $filters['orderby'],
1802 - 'order' => $filters['order'],
2135 + 'order' => $filters['order'] == 'asc' ? 'asc' : 'desc',
1803 2136 );
1804 2137 } else {
1805 2138 $order_condition = array(
1806 2139 'orderby' => 'id',
@@ -1807,65 +2140,57 @@
1807 2140 'order' => 'desc',
1808 2141 );
1809 2142 }
1810 2143
1811 - $where = $helper_e2pdf_db->prepare_where($condition);
1812 - $orderby = $helper_e2pdf_db->prepare_orderby($order_condition);
2144 + $limit_condition = array();
2145 + if (!$count) {
2146 + $paged = isset($filters['paged']) && $filters['paged'] ? $filters['paged'] : '0';
2147 + $paged = (int) $paged <= 0 ? 1 : (int) $paged;
2148 + $per_page = (int) get_option('e2pdf_templates_screen_per_page', '20');
2149 + $limit_condition = array(
2150 + 'limit' => (int) $per_page,
2151 + 'offset' => (int) ($paged - 1) * $per_page,
2152 + );
2153 + }
1813 2154
1814 - $templates = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $model_e2pdf_template->get_table() . $where['sql'] . $orderby . "", $where['filter']));
2155 + $where = $this->helper->load('db')->prepare_where($condition);
2156 + $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
2157 + $limit = $this->helper->load('db')->prepare_limit($limit_condition);
2158 + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
2159 + $tpls = $wpdb->get_results($wpdb->prepare('SELECT `ID` FROM `' . $model_e2pdf_template->get_table() . '`' . $where['sql'] . $orderby . $limit . '', $where['filter']));
1815 2160
1816 2161 if ($count) {
1817 - return count($templates);
2162 + return count($tpls);
1818 2163 }
1819 2164
1820 - $model_e2pdf_extension = new Model_E2pdf_Extension();
1821 -
1822 - foreach ($templates as $key => $template) {
1823 -
2165 + $templates = array();
2166 + foreach ($tpls as $key => $tpl) {
2167 + $template = new Model_E2pdf_Template();
2168 + $template->load($tpl->ID, false);
1824 2169 if (is_array($this->helper->get('license')->get('activated_templates'))) {
1825 - if ($template->activated && !in_array($template->uid, $this->helper->get('license')->get('activated_templates'))) {
1826 - $this->deactivate_template($template->ID);
1827 - } elseif (!$template->activated && in_array($template->uid, $this->helper->get('license')->get('activated_templates'))) {
1828 - $this->activate_template($template->ID);
2170 + if ($template->get('activated') && !in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
2171 + $template->deactivate();
2172 + } elseif (!$template->get('activated') && in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
2173 + $template->activate();
1829 2174 }
1830 2175 }
1831 -
1832 - if ($model_e2pdf_extension->load($template->extension)) {
1833 - $item = $model_e2pdf_extension->get_item($template->item);
1834 - $template->item_name = $item->name;
1835 - $template->item_url = $item->url;
1836 - } else {
1837 - $template->item_name = __('[unavailable]', 'e2pdf');
1838 - $template->item_url = 'javascript:void(0);';
1839 - }
2176 + $templates[] = $template;
1840 2177 }
1841 -
1842 2178 return $templates;
1843 2179 }
1844 2180
1845 - /**
1846 - * Get extensions list
1847 - *
1848 - * @return array() - Extensions list
1849 - */
1850 - public function get_extensions_list() {
1851 -
1852 - $model_e2pdf_extension = new Model_E2pdf_Extension();
1853 - return $model_e2pdf_extension->get_extensions_list();
1854 - }
1855 -
1856 - /**
1857 - * Get paper sizes list
1858 - *
1859 - * @return array() - Sizes list
1860 - */
2181 + // get sizes list
1861 2182 public function get_sizes_list($size = false, $attr = false) {
1862 2183
1863 2184 $sizes = array(
1864 - 'A4' => array(
2185 + 'A4 (PORTRAIT)' => array(
1865 2186 'width' => '595',
1866 2187 'height' => '842',
1867 2188 ),
2189 + 'A4 (LANDSCAPE)' => array(
2190 + 'width' => '842',
2191 + 'height' => '595',
2192 + ),
1868 2193 'LETTER' => array(
1869 2194 'width' => '612',
1870 2195 'height' => '792',
1871 2196 ),
@@ -1900,15 +2225,10 @@
1900 2225 return false;
1901 2226 }
1902 2227 }
1903 2228
1904 - /**
1905 - * Get fonts list
1906 - *
1907 - * @return array() - Fonts list
1908 - */
2229 + // get fonts
1909 2230 public function get_fonts($with_path = false) {
1910 -
1911 2231 $model_e2pdf_font = new Model_E2pdf_Font();
1912 2232 $fonts = $model_e2pdf_font->get_fonts();
1913 2233
1914 2234 if ($with_path) {
@@ -1917,9 +2237,9 @@
1917 2237 'key' => $value,
1918 2238 'subfield' => array(
1919 2239 'path' => $key,
1920 2240 ),
1921 - 'value' => $value
2241 + 'value' => $value,
1922 2242 );
1923 2243 }
1924 2244 }
1925 2245
@@ -1925,43 +2245,324 @@
1925 2245
1926 2246 return $fonts;
1927 2247 }
1928 2248
1929 - /**
1930 - * Get font sizes list
1931 - *
1932 - * @return array() - Font sizes list
1933 - */
2249 + // get font sizes
1934 2250 public function get_font_sizes() {
1935 -
2251 + $max_font_size = apply_filters('e2pdf_controller_templates_max_font_size', 512);
1936 2252 $sizes = array();
1937 - for ($i = 1; $i <= 72; $i++) {
1938 - $sizes[$i] = $i . 'px';
2253 + for ($i = 1; $i <= $max_font_size; $i++) {
2254 + $sizes[$i] = $i;
1939 2255 }
1940 2256 return $sizes;
1941 2257 }
1942 2258
1943 - /**
1944 - * Get line heights list
1945 - *
1946 - * @return array() - Line heights list
1947 - */
2259 + // get line heights
1948 2260 public function get_line_heights() {
1949 2261
2262 + $max_line_height = apply_filters('e2pdf_controller_templates_max_line_height', 512);
1950 2263 $line_heights = array();
1951 - for ($i = 1; $i <= 512; $i++) {
1952 - $line_heights[$i] = $i . 'px';
2264 + for ($i = 1; $i <= $max_line_height; $i++) {
2265 + $line_heights[$i] = $i;
1953 2266 }
1954 2267 return $line_heights;
1955 2268 }
1956 2269
1957 - /**
1958 - * Load metaboxes on template edit/create action
1959 - */
2270 + // get lang codes
2271 + public function get_lang_codes() {
2272 + return array(
2273 + '' => '',
2274 + 'aa' => 'aa',
2275 + 'ab' => 'ab',
2276 + 'ae' => 'ae',
2277 + 'af' => 'af',
2278 + 'ak' => 'ak',
2279 + 'am' => 'am',
2280 + 'an' => 'an',
2281 + 'ar' => 'ar',
2282 + 'ar-AE' => 'ar-AE',
2283 + 'ar-BH' => 'ar-BH',
2284 + 'ar-DZ' => 'ar-DZ',
2285 + 'ar-EG' => 'ar-EG',
2286 + 'ar-IQ' => 'ar-IQ',
2287 + 'ar-JO' => 'ar-JO',
2288 + 'ar-KW' => 'ar-KW',
2289 + 'ar-LB' => 'ar-LB',
2290 + 'ar-LY' => 'ar-LY',
2291 + 'ar-MA' => 'ar-MA',
2292 + 'ar-OM' => 'ar-OM',
2293 + 'ar-QA' => 'ar-QA',
2294 + 'ar-SA' => 'ar-SA',
2295 + 'ar-SY' => 'ar-SY',
2296 + 'ar-TN' => 'ar-TN',
2297 + 'ar-YE' => 'ar-YE',
2298 + 'as' => 'as',
2299 + 'av' => 'av',
2300 + 'ay' => 'ay',
2301 + 'az' => 'az',
2302 + 'ba' => 'ba',
2303 + 'be' => 'be',
2304 + 'bg' => 'bg',
2305 + 'bh' => 'bh',
2306 + 'bi' => 'bi',
2307 + 'bm' => 'bm',
2308 + 'bn' => 'bn',
2309 + 'bo' => 'bo',
2310 + 'br' => 'br',
2311 + 'bs' => 'bs',
2312 + 'ca' => 'ca',
2313 + 'ce' => 'ce',
2314 + 'ch' => 'ch',
2315 + 'co' => 'co',
2316 + 'cr' => 'cr',
2317 + 'cs' => 'cs',
2318 + 'cu' => 'cu',
2319 + 'cv' => 'cv',
2320 + 'cy' => 'cy',
2321 + 'da' => 'da',
2322 + 'de' => 'de',
2323 + 'de-AT' => 'de-AT',
2324 + 'de-CH' => 'de-CH',
2325 + 'de-DE' => 'de-DE',
2326 + 'de-LI' => 'de-LI',
2327 + 'de-LU' => 'de-LU',
2328 + 'div' => 'div',
2329 + 'dv' => 'dv',
2330 + 'dz' => 'dz',
2331 + 'ee' => 'ee',
2332 + 'el' => 'el',
2333 + 'en' => 'en',
2334 + 'en-AU' => 'en-AU',
2335 + 'en-BZ' => 'en-BZ',
2336 + 'en-CA' => 'en-CA',
2337 + 'en-CB' => 'en-CB',
2338 + 'en-GB' => 'en-GB',
2339 + 'en-IE' => 'en-IE',
2340 + 'en-JM' => 'en-JM',
2341 + 'en-NZ' => 'en-NZ',
2342 + 'en-PH' => 'en-PH',
2343 + 'en-TT' => 'en-TT',
2344 + 'en-US' => 'en-US',
2345 + 'en-ZA' => 'en-ZA',
2346 + 'en-ZW' => 'en-ZW',
2347 + 'eo' => 'eo',
2348 + 'es' => 'es',
2349 + 'es-AR' => 'es-AR',
2350 + 'es-BO' => 'es-BO',
2351 + 'es-CL' => 'es-CL',
2352 + 'es-CO' => 'es-CO',
2353 + 'es-CR' => 'es-CR',
2354 + 'es-DO' => 'es-DO',
2355 + 'es-EC' => 'es-EC',
2356 + 'es-ES' => 'es-ES',
2357 + 'es-GT' => 'es-GT',
2358 + 'es-HN' => 'es-HN',
2359 + 'es-MX' => 'es-MX',
2360 + 'es-NI' => 'es-NI',
2361 + 'es-PA' => 'es-PA',
2362 + 'es-PE' => 'es-PE',
2363 + 'es-PR' => 'es-PR',
2364 + 'es-PY' => 'es-PY',
2365 + 'es-SV' => 'es-SV',
2366 + 'es-US' => 'es-US',
2367 + 'es-UY' => 'es-UY',
2368 + 'es-VE' => 'es-VE',
2369 + 'et' => 'et',
2370 + 'eu' => 'eu',
2371 + 'fa' => 'fa',
2372 + 'ff' => 'ff',
2373 + 'fi' => 'fi',
2374 + 'fj' => 'fj',
2375 + 'fo' => 'fo',
2376 + 'fr' => 'fr',
2377 + 'fr-BE' => 'fr-BE',
2378 + 'fr-CA' => 'fr-CA',
2379 + 'fr-CH' => 'fr-CH',
2380 + 'fr-FR' => 'fr-FR',
2381 + 'fr-LU' => 'fr-LU',
2382 + 'fr-MC' => 'fr-MC',
2383 + 'fy' => 'fy',
2384 + 'ga' => 'ga',
2385 + 'gd' => 'gd',
2386 + 'gl' => 'gl',
2387 + 'gn' => 'gn',
2388 + 'gu' => 'gu',
2389 + 'gv' => 'gv',
2390 + 'ha' => 'ha',
2391 + 'he' => 'he',
2392 + 'hi' => 'hi',
2393 + 'ho' => 'ho',
2394 + 'hr' => 'hr',
2395 + 'hr-BA' => 'hr-BA',
2396 + 'hr-HR' => 'hr-HR',
2397 + 'ht' => 'ht',
2398 + 'hu' => 'hu',
2399 + 'hy' => 'hy',
2400 + 'hz' => 'hz',
2401 + 'ia' => 'ia',
2402 + 'id' => 'id',
2403 + 'ie' => 'ie',
2404 + 'ig' => 'ig',
2405 + 'ii' => 'ii',
2406 + 'ik' => 'ik',
2407 + 'in' => 'in',
2408 + 'io' => 'io',
2409 + 'is' => 'is',
2410 + 'it' => 'it',
2411 + 'it-CH' => 'it-CH',
2412 + 'it-IT' => 'it-IT',
2413 + 'iu' => 'iu',
2414 + 'iw' => 'iw',
2415 + 'ja' => 'ja',
2416 + 'ji' => 'ji',
2417 + 'jv' => 'jv',
2418 + 'jw' => 'jw',
2419 + 'ka' => 'ka',
2420 + 'kg' => 'kg',
2421 + 'ki' => 'ki',
2422 + 'kj' => 'kj',
2423 + 'kk' => 'kk',
2424 + 'kl' => 'kl',
2425 + 'km' => 'km',
2426 + 'kn' => 'kn',
2427 + 'ko' => 'ko',
2428 + 'kok' => 'kok',
2429 + 'kr' => 'kr',
2430 + 'ks' => 'ks',
2431 + 'ku' => 'ku',
2432 + 'kv' => 'kv',
2433 + 'kw' => 'kw',
2434 + 'ky' => 'ky',
2435 + 'kz' => 'kz',
2436 + 'la' => 'la',
2437 + 'lb' => 'lb',
2438 + 'lg' => 'lg',
2439 + 'li' => 'li',
2440 + 'ln' => 'ln',
2441 + 'lo' => 'lo',
2442 + 'ls' => 'ls',
2443 + 'lt' => 'lt',
2444 + 'lu' => 'lu',
2445 + 'lv' => 'lv',
2446 + 'mg' => 'mg',
2447 + 'mh' => 'mh',
2448 + 'mi' => 'mi',
2449 + 'mk' => 'mk',
2450 + 'ml' => 'ml',
2451 + 'mn' => 'mn',
2452 + 'mo' => 'mo',
2453 + 'mr' => 'mr',
2454 + 'ms' => 'ms',
2455 + 'ms-BN' => 'ms-BN',
2456 + 'ms-MY' => 'ms-MY',
2457 + 'mt' => 'mt',
2458 + 'my' => 'my',
2459 + 'na' => 'na',
2460 + 'nb' => 'nb',
2461 + 'nd' => 'nd',
2462 + 'ne' => 'ne',
2463 + 'ng' => 'ng',
2464 + 'nl' => 'nl',
2465 + 'nl-BE' => 'nl-BE',
2466 + 'nl-NL' => 'nl-NL',
2467 + 'nn' => 'nn',
2468 + 'no' => 'no',
2469 + 'nr' => 'nr',
2470 + 'ns' => 'ns',
2471 + 'nv' => 'nv',
2472 + 'ny' => 'ny',
2473 + 'oc' => 'oc',
2474 + 'oj' => 'oj',
2475 + 'om' => 'om',
2476 + 'or' => 'or',
2477 + 'os' => 'os',
2478 + 'pa' => 'pa',
2479 + 'pi' => 'pi',
2480 + 'pl' => 'pl',
2481 + 'ps' => 'ps',
2482 + 'pt' => 'pt',
2483 + 'pt-BR' => 'pt-BR',
2484 + 'pt-PT' => 'pt-PT',
2485 + 'qu' => 'qu',
2486 + 'qu-BO' => 'qu-BO',
2487 + 'qu-EC' => 'qu-EC',
2488 + 'qu-PE' => 'qu-PE',
2489 + 'rm' => 'rm',
2490 + 'rn' => 'rn',
2491 + 'ro' => 'ro',
2492 + 'ru' => 'ru',
2493 + 'rw' => 'rw',
2494 + 'sa' => 'sa',
2495 + 'sb' => 'sb',
2496 + 'sc' => 'sc',
2497 + 'sd' => 'sd',
2498 + 'se' => 'se',
2499 + 'se-FI' => 'se-FI',
2500 + 'se-NO' => 'se-NO',
2501 + 'se-SE' => 'se-SE',
2502 + 'sg' => 'sg',
2503 + 'sh' => 'sh',
2504 + 'si' => 'si',
2505 + 'sk' => 'sk',
2506 + 'sl' => 'sl',
2507 + 'sm' => 'sm',
2508 + 'sn' => 'sn',
2509 + 'so' => 'so',
2510 + 'sq' => 'sq',
2511 + 'sr' => 'sr',
2512 + 'sr-BA' => 'sr-BA',
2513 + 'sr-SP' => 'sr-SP',
2514 + 'ss' => 'ss',
2515 + 'st' => 'st',
2516 + 'su' => 'su',
2517 + 'sv' => 'sv',
2518 + 'sv-FI' => 'sv-FI',
2519 + 'sv-SE' => 'sv-SE',
2520 + 'sw' => 'sw',
2521 + 'sx' => 'sx',
2522 + 'syr' => 'syr',
2523 + 'ta' => 'ta',
2524 + 'te' => 'te',
2525 + 'tg' => 'tg',
2526 + 'th' => 'th',
2527 + 'ti' => 'ti',
2528 + 'tk' => 'tk',
2529 + 'tl' => 'tl',
2530 + 'tn' => 'tn',
2531 + 'to' => 'to',
2532 + 'tr' => 'tr',
2533 + 'ts' => 'ts',
2534 + 'tt' => 'tt',
2535 + 'tw' => 'tw',
2536 + 'ty' => 'ty',
2537 + 'ug' => 'ug',
2538 + 'uk' => 'uk',
2539 + 'ur' => 'ur',
2540 + 'us' => 'us',
2541 + 'uz' => 'uz',
2542 + 've' => 've',
2543 + 'vi' => 'vi',
2544 + 'vo' => 'vo',
2545 + 'wa' => 'wa',
2546 + 'wo' => 'wo',
2547 + 'xh' => 'xh',
2548 + 'yi' => 'yi',
2549 + 'yo' => 'yo',
2550 + 'za' => 'za',
2551 + 'zh' => 'zh',
2552 + 'zh-CN' => 'zh-CN',
2553 + 'zh-HK' => 'zh-HK',
2554 + 'zh-MO' => 'zh-MO',
2555 + 'zh-SG' => 'zh-SG',
2556 + 'zh-TW' => 'zh-TW',
2557 + 'zu' => 'zu',
2558 + );
2559 + }
2560 +
2561 + // load metaboxes
1960 2562 public function load_metaboxes() {
1961 -
1962 2563 add_meta_box(
1963 - 'e2pdf_templates_save', __('Save', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_save')
2564 + 'e2pdf_templates_save', __('Preset', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_save')
1964 2565 );
1965 2566 add_meta_box(
1966 2567 'e2pdf_templates_builder', __('PDF Builder', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_builder')
1967 2568 );
@@ -1969,13 +2570,10 @@
1969 2570 'e2pdf_templates_settings', __('Settings', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_settings')
1970 2571 );
1971 2572 }
1972 2573
1973 - /**
1974 - * Load javascript on template edit/create action
1975 - */
2574 + // load scripts
1976 2575 public function load_scripts() {
1977 -
1978 2576 wp_enqueue_script('postbox');
1979 2577 wp_enqueue_script('jquery-ui-droppable');
1980 2578 wp_enqueue_script('jquery-ui-resizable');
1981 2579 wp_enqueue_script('jquery-ui-selectable');
@@ -1984,25 +2582,22 @@
1984 2582 wp_enqueue_script('wp-color-picker');
1985 2583 wp_enqueue_media();
1986 2584 }
1987 2585
1988 - /**
1989 - * Load styles on template edit/create action
1990 - */
2586 + // load styles
1991 2587 public function load_styles($ext = false) {
1992 - wp_enqueue_style('plugin_name-admin-ui-css', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css', false, false, false);
2588 + $version = get_option('e2pdf_debug', '0') === '1' ? strtotime('now') : $this->helper->get('version');
2589 + wp_enqueue_style('css/e2pdf.jquery-ui', plugins_url('css/jquery-ui.css', $this->helper->get('plugin_file_path')), false, $version, false);
1993 2590 wp_enqueue_style('wp-color-picker');
1994 -
1995 2591 if ($ext) {
1996 2592 $extension = new Model_E2pdf_Extension();
1997 2593 if ($extension->load($ext)) {
1998 - $styles = $extension->get_styles();
2594 + $styles = $extension->styles();
1999 2595 if ($styles && is_array($styles)) {
2000 2596 foreach ($styles as $key => $value) {
2001 - wp_enqueue_style('e2pdf-dynamic-style-' . $key, $value, false, false, false);
2597 + wp_enqueue_style('e2pdf-dynamic-style-' . $key, $value, false, $version, false);
2002 2598 }
2003 2599 }
2004 2600 }
2005 2601 }
2006 2602 }
2007 -
2008 2603 }