PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.51
E2Pdf – Export Pdf Tool for WordPress v1.32.51
1.32.51 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 All 73 releases
← All changes | classes/controller/e2pdf-templates.php +1980 -1351 1.03.071.32.51 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,34 +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 88 $pages = $template->get('pages');
86 -
87 - foreach ($pages as $page_key => $page_value) {
88 -
89 - if (isset($page_value['elements'])) {
90 -
91 - foreach ($page_value['elements'] as $element_key => $element_value) {
92 -
93 - if (isset($element_value['type']) && $element_value['type'] == 'e2pdf-image') {
94 -
95 - if (!$this->helper->load('image')->get_image($element_value['value'])) {
96 - $pages[$page_key]['elements'][$element_key]['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png";
97 -
98 - if (isset($pages[$page_key]['elements'][$element_key]['properties']['dimension'])) {
99 - unset($pages[$page_key]['elements'][$element_key]['properties']['dimension']);
100 - }
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;
101 164 }
102 165 }
103 166 }
104 167 }
@@ -110,23 +173,23 @@
110 173 if (isset($request['error'])) {
111 174 $this->add_notification('error', $request['error']);
112 175 $this->render('blocks', 'notifications');
113 176 } else {
114 - $filename = $template->get_filename();
115 - $file = $request['file'];
116 - $this->download_response('pdf', $file, $filename);
177 + $this->download_response($template->get('format'), $request['file'], $template->get_name(), 'inline');
178 + exit;
117 179 }
118 180 } else {
119 - $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
181 + $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
120 182 $this->render('blocks', 'notifications');
121 183 }
122 184 } else {
123 - $location = $this->helper->get_url(
124 - array(
125 - 'page' => 'e2pdf',
185 + $this->redirect(
186 + $this->helper->get_url(
187 + [
188 + 'page' => 'e2pdf',
189 + ]
126 190 )
127 191 );
128 - $this->redirect($location);
129 192 }
130 193 }
131 194
132 195 /**
@@ -132,15 +195,12 @@
132 195 /**
133 196 * @url admin.php?page=e2pdf-templates&action=preview
134 197 */
135 198 public function preview_action() {
136 -
137 199 $preview = array();
138 -
139 200 if ($this->post->get('preview')) {
140 201 $preview = json_decode(stripslashes($this->post->get('preview')), true);
141 202 }
142 -
143 203 if (empty($preview)) {
144 204 $this->close_tab_response();
145 205 } else {
146 206 $data = $preview;
@@ -145,162 +205,146 @@
145 205 } else {
146 206 $data = $preview;
147 207 $pages = $preview['pages'];
148 208 unset($preview['pages']);
149 -
150 - $this->check_nonce($preview['_nonce'], 'e2pdf_post');
151 -
152 - $template = new Model_E2pdf_Template();
153 - foreach ($data as $key => $value) {
154 - $template->set($key, $value);
155 - }
156 -
157 - $preview_pages = array();
158 - foreach ($pages as $page_key => $page_value) {
159 - $page = new Model_E2pdf_Page();
160 - foreach ($page_value as $page_value_key => $page_value_value) {
161 - $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);
162 213 }
163 - $page->set('page_id', $page_key);
214 + $template->set('uid', $template->get('uid', true));
215 + $template->set('activated', $template->get('activated', true));
164 216
165 - $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);
166 224
167 - $this->helper->load('sort')->uasort($page_value['elements'], "sort_by_elementid");
168 - $this->helper->load('sort')->stable_uasort($page_value['elements'], "sort_by_zindex");
169 -
170 - foreach ($page_value['elements'] as $element_key => $element_value) {
171 - $element = new Model_E2pdf_Element();
172 - $esig = isset($element_value['properties']['esig']) && $element_value['properties']['esig'] ? true : false;
173 - if (isset($element_value['type']) && ($element_value['type'] == 'e2pdf-image' || ($element_value['type'] == 'e2pdf-signature' && !$esig))) {
174 - if (!$this->helper->load('image')->get_image($element_value['value'])) {
175 - $element_value['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png";
176 - if (isset($element_value['properties']['dimension'])) {
177 - 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;
178 300 }
179 301 }
180 - }
181 302
182 - foreach ($element_value as $element_value_key => $element_value_value) {
183 - $element->set($element_value_key, $element_value_value);
184 - }
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 + }
185 307
186 - if (!$element_value['properties']) {
187 - $element_properties = array();
188 - } else {
189 - $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();
190 315 }
191 316
192 - $element->set('properties', $element_properties);
193 - $elements[] = $element->get_element();
317 + $new_page->set('elements', $elements);
318 + $preview_pages[] = $new_page->get_page();
194 319 }
195 320
196 - $page->set('elements', $elements);
197 - $preview_pages[] = $page->get_page();
198 - }
321 + $template->set('pages', $preview_pages);
322 + $template->load_fonts();
199 323
200 - $template->set('pages', $preview_pages);
201 - $request = $template->render(true);
202 -
203 - if (isset($request['error'])) {
204 - $this->add_notification('error', $request['error']);
205 - $this->render('blocks', 'notifications');
206 - } else {
207 - $filename = $template->get_filename();
208 - $file = $request['file'];
209 - $this->download_response('pdf', $file, $filename);
210 - }
211 - }
212 - }
213 -
214 - /**
215 - * @url admin.php?page=e2pdf-templates&action=convert&type=php
216 - */
217 - public function convert_action() {
218 - $preview = array();
219 -
220 - if ($this->post->get('preview')) {
221 - $preview = json_decode(stripslashes($this->post->get('preview')), true);
222 - }
223 -
224 - if (empty($preview)) {
225 - $this->close_tab_response();
226 - } else {
227 - $data = $preview;
228 - $pages = $preview['pages'];
229 - unset($preview['pages']);
230 -
231 - $this->check_nonce($preview['_nonce'], 'e2pdf_post');
232 -
233 - $template = new Model_E2pdf_Template();
234 - foreach ($data as $key => $value) {
235 - $template->set($key, $value);
236 - }
237 -
238 - $preview_pages = array();
239 - foreach ($pages as $page_key => $page_value) {
240 - $page = new Model_E2pdf_Page();
241 - foreach ($page_value as $page_value_key => $page_value_value) {
242 - $page->set($page_value_key, $page_value_value);
243 - }
244 - $page->set('page_id', $page_key);
245 -
246 - $elements = array();
247 -
248 - $this->helper->load('sort')->uasort($page_value['elements'], "sort_by_elementid");
249 - $this->helper->load('sort')->stable_uasort($page_value['elements'], "sort_by_zindex");
250 -
251 - foreach ($page_value['elements'] as $element_key => $element_value) {
252 - $element = new Model_E2pdf_Element();
253 - $esig = isset($element_value['properties']['esig']) && $element_value['properties']['esig'] ? true : false;
254 - if (isset($element_value['type']) && ($element_value['type'] == 'e2pdf-image' || ($element_value['type'] == 'e2pdf-signature' && !$esig))) {
255 - if (!$this->helper->load('image')->get_image($element_value['value'])) {
256 - $element_value['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png";
257 - if (isset($element_value['properties']['dimension'])) {
258 - unset($element_value['properties']['dimension']);
259 - }
260 - }
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;
261 333 }
262 -
263 - foreach ($element_value as $element_value_key => $element_value_value) {
264 - $element->set($element_value_key, $element_value_value);
265 - }
266 -
267 - if (!$element_value['properties']) {
268 - $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');
269 339 } else {
270 - $element_properties = $element_value['properties'];
340 + $this->download_response($template->get('format'), $request['file'], $template->get_name(), 'inline', false, true);
341 + exit;
271 342 }
272 -
273 - $element->set('properties', $element_properties);
274 - $elements[] = $element->get_element();
275 343 }
276 -
277 - $page->set('elements', $elements);
278 - $preview_pages[] = $page->get_page();
279 - }
280 -
281 - $template->set('pages', $preview_pages);
282 -
283 - $allowed_types = array(
284 - 'php'
285 - );
286 -
287 - if (!in_array($this->get->get('type'), $allowed_types)) {
288 - $type = 'php';
289 344 } else {
290 - $type = $this->get->get('type');
345 + wp_die($this->message('wp_verify_nonce_error'));
291 346 }
292 -
293 - $request = $template->convert(true, $type);
294 -
295 - if (isset($request['error'])) {
296 - $this->add_notification('error', $request['error']);
297 - $this->render('blocks', 'notifications');
298 - } else {
299 - $filename = 'template';
300 - $file = $request['file'];
301 - $this->download_response($type, $file, $filename);
302 - }
303 347 }
304 348 }
305 349
306 350 /**
@@ -306,14 +350,13 @@
306 350 /**
307 351 * @url admin.php?page=e2pdf-templates&action=create
308 352 */
309 353 public function create_action() {
310 -
311 354 $template = new Model_E2pdf_Template();
312 355 $this->load_metaboxes();
313 356 $this->load_scripts();
314 357 $this->load_styles();
315 -
358 + $this->view('license', $this->helper->get('license'));
316 359 $this->view('template', $template);
317 360 }
318 361
319 362 /**
@@ -321,25 +364,34 @@
321 364 */
322 365 public function edit_action() {
323 366 if ($this->get->get('id')) {
324 367 $template = new Model_E2pdf_Template();
325 -
326 - 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'));
327 378 $this->view('template', $template);
328 379 $this->load_metaboxes();
329 380 $this->load_scripts();
330 381 $this->load_styles($template->get('extension'));
331 382 } else {
332 - $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
383 + $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
333 384 $this->render('blocks', 'notifications');
334 385 }
335 386 } else {
336 - $location = $this->helper->get_url(
337 - array(
338 - 'page' => 'e2pdf-templates',
387 + $this->redirect(
388 + $this->helper->get_url(
389 + [
390 + 'page' => 'e2pdf-templates',
391 + ]
339 392 )
340 393 );
341 - $this->redirect($location);
342 394 }
343 395 }
344 396
345 397 /**
@@ -345,23 +397,29 @@
345 397 /**
346 398 * @url admin.php?page=e2pdf-templates&action=trash&id=$id
347 399 */
348 400 public function trash_action() {
349 -
350 - if ($this->get->get('id')) {
351 - $this->trash_template($this->get->get('id'));
352 - $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'));
353 409 }
354 - $location = $this->helper->get_url(
355 - array(
356 - 'page' => 'e2pdf-templates',
357 - 'status' => $this->get->get('status'),
358 - 'orderby' => $this->get->get('orderby'),
359 - 'order' => $this->get->get('order'),
360 - '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 + ]
361 420 )
362 421 );
363 - $this->redirect($location);
364 422 }
365 423
366 424 /**
367 425 * @url admin.php?page=e2pdf-templates&action=restore&id=$id&status=$status
@@ -366,24 +424,29 @@
366 424 /**
367 425 * @url admin.php?page=e2pdf-templates&action=restore&id=$id&status=$status
368 426 */
369 427 public function restore_action() {
370 -
371 - if ($this->get->get('id')) {
372 - $this->restore_template($this->get->get('id'));
373 - $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'));
374 436 }
375 -
376 - $location = $this->helper->get_url(
377 - array(
378 - 'page' => 'e2pdf-templates',
379 - 'status' => $this->get->get('status'),
380 - 'orderby' => $this->get->get('orderby'),
381 - 'order' => $this->get->get('order'),
382 - '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 + ]
383 447 )
384 448 );
385 - $this->redirect($location);
386 449 }
387 450
388 451 /**
389 452 * @url admin.php?page=e2pdf-templates&action=delete&id=$id&status=$status
@@ -388,24 +451,30 @@
388 451 /**
389 452 * @url admin.php?page=e2pdf-templates&action=delete&id=$id&status=$status
390 453 */
391 454 public function delete_action() {
392 -
393 - if ($this->get->get('id')) {
394 - $this->delete_template($this->get->get('id'));
395 - $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'));
396 463 }
397 464
398 - $location = $this->helper->get_url(
399 - array(
400 - 'page' => 'e2pdf-templates',
401 - 'status' => $this->get->get('status'),
402 - 'orderby' => $this->get->get('orderby'),
403 - 'order' => $this->get->get('order'),
404 - '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 + ]
405 475 )
406 476 );
407 - $this->redirect($location);
408 477 }
409 478
410 479 /**
411 480 * @url admin.php?page=e2pdf-templates&action=duplicate&id=$id
@@ -410,441 +479,693 @@
410 479 /**
411 480 * @url admin.php?page=e2pdf-templates&action=duplicate&id=$id
412 481 */
413 482 public function duplicate_action() {
414 -
415 - if ($this->get->get('id')) {
416 - $this->duplicate_template($this->get->get('id'));
417 - $this->add_notification('update', __('Template copy created', 'e2pdf'));
418 - }
419 - $location = $this->helper->get_url(
420 - array(
421 - 'page' => 'e2pdf-templates',
422 - 'status' => $this->get->get('status'),
423 - 'orderby' => $this->get->get('orderby'),
424 - 'order' => $this->get->get('order'),
425 - 's' => $this->get->get('s'),
426 - )
427 - );
428 - $this->redirect($location);
429 - }
430 -
431 - /**
432 - * @url admin.php?page=e2pdf-templates&action=activate&id=$id
433 - */
434 - public function activate_action() {
435 -
436 - if ($this->get->get('id')) {
437 - $request = $this->activate_template($this->get->get('id'));
438 - if (isset($request['error'])) {
439 - $this->add_notification('error', $request['error']);
440 - } else {
441 - $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'));
442 488 }
489 + } else {
490 + wp_die($this->message('wp_verify_nonce_error'));
443 491 }
444 - $location = $this->helper->get_url(
445 - array(
446 - 'page' => 'e2pdf-templates',
447 - 'status' => $this->get->get('status'),
448 - 'orderby' => $this->get->get('orderby'),
449 - 'order' => $this->get->get('order'),
450 - '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 + ]
451 502 )
452 503 );
453 - $this->redirect($location);
454 504 }
455 505
456 506 /**
457 - * @url admin.php?page=e2pdf-templates&action=deactivate&id=$id
458 - */
459 - public function deactivate_action() {
460 -
461 - if ($this->get->get('id')) {
462 - $request = $this->deactivate_template($this->get->get('id'));
463 - if (isset($request['error'])) {
464 - $this->add_notification('error', $request['error']);
465 - } else {
466 - $this->add_notification('update', __('Template deactivated', 'e2pdf'));
467 - }
468 - }
469 - $location = $this->helper->get_url(
470 - array(
471 - 'page' => 'e2pdf-templates',
472 - 'status' => $this->get->get('status'),
473 - 'orderby' => $this->get->get('orderby'),
474 - 'order' => $this->get->get('order'),
475 - 's' => $this->get->get('s'),
476 - )
477 - );
478 - $this->redirect($location);
479 - }
480 -
481 - /**
482 507 * @url admin.php?page=e2pdf-templates&action=import
483 508 */
484 509 public function import_action() {
510 + if ($this->post->get('_wpnonce')) {
485 511
486 - 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 + }
487 515
488 - $this->check_nonce($this->post->get('_nonce'), 'e2pdf_post');
489 -
490 - $errors = array();
491 -
492 - $allowed_ext = array(
493 - 'xml'
494 - );
495 -
496 - $import = $this->files->get('template');
497 -
498 - $name = $import['name'];
499 - $tmp = $import['tmp_name'];
500 -
501 - $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
502 -
503 - if (!$tmp) {
504 - $this->add_notification('error', __('Please choose template to upload', 'e2pdf'));
505 - } else if ($import['error']) {
506 - $this->add_notification('error', $import['error']);
507 - } elseif (!in_array($ext, $allowed_ext)) {
508 - $this->add_notification('error', __('Incorrect file extension', 'e2pdf'));
509 - } elseif ($import['type'] != 'text/xml') {
510 - $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']);
511 519 } else {
512 -
520 + $errors = [];
513 521 $options = $this->post->get('options');
514 -
515 - $xml = simplexml_load_file($import['tmp_name'], "SimpleXMLElement", LIBXML_PARSEHUGE);
516 -
517 - $pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages)));
518 -
519 - foreach ($pages as $page_key => $page) {
520 - if (isset($page['elements']) && !empty($page['elements'])) {
521 - foreach ($page['elements'] as $element_key => $element) {
522 - if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
523 - if ($options['images']) {
524 - $image = base64_decode($element['base64']);
525 - $file_name = basename($element['value']);
526 - $upload_file = wp_upload_bits($file_name, null, $image);
527 - if (!$upload_file['error']) {
528 - $wp_filetype = wp_check_filetype($file_name, null);
529 - $attachment = array(
530 - 'post_mime_type' => $wp_filetype['type'],
531 - 'post_parent' => 0,
532 - 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
533 - 'post_content' => '',
534 - 'post_status' => 'inherit'
535 - );
536 - $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
537 - if (!is_wp_error($attachment_id)) {
538 - require_once(ABSPATH . "wp-admin" . '/includes/image.php');
539 - $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
540 - wp_update_attachment_metadata($attachment_id, $attachment_data);
541 - $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 + }
542 553 }
554 + unset($pages[$page_key]['elements'][$element_key]['base64']);
543 555 }
544 556 }
545 - unset($pages[$page_key]['elements'][$element_key]['base64']);
546 557 }
547 558 }
548 559 }
549 - }
550 560
551 - $template = new Model_E2pdf_Template();
552 - if ($options['overwrite']) {
553 - $template->load((String) $xml->template->ID);
554 - }
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 + }
555 597
556 - $template->set('title', (String) $xml->template->title);
557 - $template->set('flatten', (String) $xml->template->flatten);
558 - $template->set('format', (String) $xml->template->format);
559 - $template->set('compression', (String) $xml->template->compression);
560 - $template->set('appearance', (String) $xml->template->appearance);
561 - $template->set('width', (String) $xml->template->width);
562 - $template->set('height', (String) $xml->template->height);
563 - $template->set('extension', (String) $xml->template->extension);
598 + $fpro2pdf_backup = false;
599 + if ($xml->source) {
600 + if ((string) $xml->source == 'fpro2pdf') {
601 + $fpro2pdf_backup = true;
602 + }
603 + }
564 604
565 - if (!$options['overwrite']) {
566 - $template->set('item', "");
567 - }
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 + }
568 613
569 - $fpro2pdf_backup = false;
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 + }
647 + }
648 + }
570 649
571 - if ($xml->source) {
572 - if ((String) $xml->source == 'fpro2pdf') {
573 - $template->set('item', (String) $xml->template->item);
574 - $fpro2pdf_backup = true;
650 + $template->set('format', (string) $xml->template->format);
651 + if (isset($xml->template->resample)) {
652 + $template->set('resample', (string) $xml->template->resample);
575 653 }
576 - }
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 + }
577 725
578 - $template->set('inline', (String) $xml->template->inline);
579 - $template->set('auto', (String) $xml->template->auto);
580 - $template->set('font', (String) $xml->template->font);
581 - $template->set('font_size', (String) $xml->template->font_size);
582 - $template->set('font_color', (String) $xml->template->font_color);
583 - $template->set('line_height', (String) $xml->template->line_height);
584 - $template->set('fonts', stripslashes_deep(unserialize(base64_decode((String) $xml->template->fonts))));
585 -
586 - $extension = new Model_E2pdf_Extension();
587 - $extension->load($template->get('extension'));
588 -
589 - if ($options['item'] && $xml->item && !$fpro2pdf_backup) {
590 - $template->set('item', (String) $xml->template->item);
591 - $extension->set('item', $template->get('item'));
592 - if (method_exists($extension->extension(), 'import')) {
593 - $result = $extension->extension()->import($xml->item, (String) $xml->template->item);
594 - if (is_array($result) && array_key_exists('errors', $result)) {
595 - $errors = array_merge($errors, $result['errors']);
596 - } else {
597 - $template->set('item', $result);
598 - }
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);
599 744 }
600 - }
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 + );
601 765
602 - $pages = apply_filters('e2pdf_controller_templates_import_pages', $options, $xml, $template, $extension, $pages);
603 - $dataset_title = apply_filters('e2pdf_controller_templates_import_dataset_title', $options, $xml, $template, $extension, (String) $xml->template->dataset_title);
604 - $button_title = apply_filters('e2pdf_controller_templates_import_button_title', $options, $xml, $template, $extension, (String) $xml->template->button_title);
605 - $name = apply_filters('e2pdf_controller_templates_import_name', $options, $xml, $template, $extension, (String) $xml->template->name);
606 - $password = apply_filters('e2pdf_controller_templates_import_password', $options, $xml, $template, $extension, (String) $xml->template->password);
766 + if ($options['fonts'] && $xml->fonts) {
767 + $model_e2pdf_font = new Model_E2pdf_Font();
768 + $fonts = $model_e2pdf_font->get_fonts();
607 769
608 - $template->set('pages', $pages);
609 - $template->set('dataset_title', $dataset_title);
610 - $template->set('button_title', $button_title);
611 - $template->set('name', $name);
612 - $template->set('password', $password);
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;
613 775
614 - if ($options['fonts'] && $xml->fonts) {
615 - $model_e2pdf_font = new Model_E2pdf_Font();
616 - $fonts = $model_e2pdf_font->get_fonts();
776 + if ($title == 'Noto Sans' && $name == 'NotoSans-Regular.ttf') {
777 + $title = 'Noto Sans Regular';
778 + }
617 779
618 - if ($xml->fonts) {
619 - foreach ($xml->fonts->children() as $key => $font) {
620 - $title = (String) $font->title;
621 - $name = (String) $font->name;
622 - $value = (String) $font->value;
623 -
624 - $exist = array_search($title, $fonts);
625 - if (!$exist) {
626 - if (!file_exists($this->helper->get('fonts_dir') . $name)) {
627 - $f_name = $name;
628 - } else {
629 - $i = 0;
630 - do {
631 - $f_name = $i . '_' . $name;
632 - $i++;
633 - } 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 + }
634 795 }
635 - file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value));
636 796 }
637 797 }
638 798 }
639 - }
640 799
641 - if ($xml->pdf && $xml->pdf->source) {
800 + if ($xml->pdf && $xml->pdf->source) {
642 801
643 - $pdf_name = md5(time());
644 - $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
645 - $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/';
646 805
647 - $this->helper->create_dir($pdf_dir);
648 - $this->helper->create_dir($pdf_images_dir);
806 + $this->helper->create_dir($pdf_dir);
807 + $this->helper->create_dir($pdf_images_dir);
649 808
650 - $pdf_source = (String) $xml->pdf->source;
651 - 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);
652 811
653 - if ($xml->pdf->images) {
654 - foreach ($xml->pdf->images->children() as $key => $image) {
655 - $page_id = (String) $image->page_id;
656 - $image_source = (String) $image->source;
657 - 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 + }
658 818 }
819 + $template->set('pdf', $pdf_name);
659 820 }
660 - $template->set('pdf', $pdf_name);
661 - }
662 821
663 - if (empty($errors)) {
664 - $template->save(true);
665 - $this->add_notification('update', __('Template imported successfully', 'e2pdf'));
666 - } else {
667 - foreach ($errors as $key => $error) {
668 - $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 + }
669 855 }
670 856 }
671 -
672 - unlink($import['tmp_name']);
857 + if (is_file($file['tmp_name'])) {
858 + unlink($file['tmp_name']);
859 + }
673 860 }
674 861 }
675 862
676 863 $this->view('import_disabled', false);
677 -
678 864 if (!$this->helper->load('xml')->check()) {
679 - $this->add_notification('error', sprintf(__("PHP Extension <strong>%s</strong> not found", 'e2pdf'), 'SimpleXml'));
865 + $this->add_notification('error', sprintf(__('The PHP %s extension is required', 'e2pdf'), 'SimpleXml'));
680 866 $this->view('import_disabled', true);
681 867 }
682 868
869 + global $wpdb;
870 +
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 + }
884 + }
885 +
683 886 $options = array(
684 - 'common' => array(
685 - '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'),
686 930 'options' => array(
687 931 array(
688 - 'name' => __('Overwrite template by ID', 'e2pdf'),
689 - 'key' => 'options[overwrite]',
690 - 'value' => 0,
691 - 'default_value' => 0,
932 + 'name' => __('Import Images', 'e2pdf'),
933 + 'key' => 'options[images]',
934 + 'value' => '1',
935 + 'default_value' => '0',
692 936 'type' => 'checkbox',
693 937 'placeholder' => '',
694 938 ),
695 939 array(
696 - 'name' => __('Import Item', 'e2pdf'),
697 - 'key' => 'options[item]',
698 - 'value' => 0,
699 - 'default_value' => 0,
940 + 'name' => __('Import Fonts', 'e2pdf'),
941 + 'key' => 'options[fonts]',
942 + 'value' => '1',
943 + 'default_value' => '0',
700 944 'type' => 'checkbox',
701 945 'placeholder' => '',
702 946 ),
947 + ),
948 + ),
949 + 'item' => array(
950 + 'name' => __('Connection Options', 'e2pdf'),
951 + 'options' => array(
703 952 array(
704 - 'name' => __('Import Images', 'e2pdf'),
705 - 'key' => 'options[images]',
706 - 'value' => 1,
707 - 'default_value' => 0,
953 + 'name' => __('Import Connection', 'e2pdf'),
954 + 'key' => 'options[item]',
955 + 'value' => '0',
956 + 'default_value' => '0',
708 957 'type' => 'checkbox',
709 958 'placeholder' => '',
959 + 'class' => 'e2pdf-collapse',
960 + 'data-collapse' => 'e2pdf-import-extension-option',
710 961 ),
711 - array(
712 - 'name' => __('Import Fonts', 'e2pdf'),
713 - 'key' => 'options[fonts]',
714 - 'value' => 1,
715 - 'default_value' => 0,
716 - 'type' => 'checkbox',
717 - 'placeholder' => '',
718 - )
719 - )
720 - )
962 + ),
963 + ),
721 964 );
722 965
966 + $options = apply_filters('e2pdf_controller_templates_import_options', $options);
723 967 $this->view('options', $options);
968 + $this->view('upload_max_filesize', $this->helper->load('files')->get_upload_max_filesize());
724 969 }
725 970
726 971 /**
727 972 * @url admin.php?page=e2pdf-templates&action=backup&id=$id
728 973 */
729 - public function backup_action() {
730 -
731 - if ($this->post->get()) {
732 -
733 - $this->check_nonce($this->post->get('_nonce'), 'e2pdf_post');
734 -
735 - if ($this->helper->load('xml')->check()) {
736 -
737 - $options = $this->post->get('options');
738 -
739 - $xml = $this->helper->load('xml')->create('backup');
740 -
741 - $template = new Model_E2pdf_Template();
742 -
743 -
744 - if ($template->load($this->post->get('id'))) {
745 -
746 - if ($template->get('title') != '') {
747 - $filename = $template->get('title') . "." . date('Y-m-d.H-i-s') . ".e2pdf";
748 - } else {
749 - $filename = date('Y-m-d.H-i-s') . ".e2pdf";
750 - }
751 -
752 - $pages = $template->get('pages');
753 - if ($options['images']) {
754 - foreach ($pages as $page_key => $page) {
755 - if (isset($page['elements']) && !empty($page['elements'])) {
756 - foreach ($page['elements'] as $element_key => $element) {
757 - if ($element['type'] === 'e2pdf-image') {
758 - $pages[$page_key]['elements'][$element_key]['base64'] = $this->helper->load('image')->get_image($element['value']);
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 + }
759 1003 }
760 1004 }
761 1005 }
762 1006 }
763 - }
764 1007
765 - $pages = apply_filters('e2pdf_controller_templates_backup_pages', $options, $template, $template->extension(), $pages);
766 - $dataset_title = apply_filters('e2pdf_controller_templates_backup_dataset_title', $options, $template, $template->extension(), $template->get('dataset_title'));
767 - $button_title = apply_filters('e2pdf_controller_templates_backup_button_title', $options, $template, $template->extension(), $template->get('button_title'));
768 - $name = apply_filters('e2pdf_controller_templates_backup_name', $options, $template, $template->extension(), $template->get('name'));
769 - $password = apply_filters('e2pdf_controller_templates_backup_password', $options, $template, $template->extension(), $template->get('password'));
1008 + /*
1009 + * Build Xml
1010 + */
1011 + $xml->addChildCData('version', $this->helper->get('version'));
1012 + $xml->addChildCData('date', date('Y-m-d H:i:s'));
770 1013
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 + );
771 1125
772 - // build xml
773 - $xml->addChildCData('version', $this->helper->get('version'));
774 - $xml->addChildCData('date', date('Y-m-d H:i:s'));
775 -
776 - $options_xml = $xml->addChild('options');
777 - foreach ($options as $option_key => $option) {
778 - $options_xml->addChildCData($option_key, $option);
779 - }
780 -
781 - $template_xml = $xml->addChild('template');
782 - $template_xml->addChildCData('ID', $template->get('ID'));
783 - $template_xml->addChildCData('title', $template->get('title'));
784 - $template_xml->addChildCData('created_at', $template->get('created_at'));
785 - $template_xml->addChildCData('updated_at', $template->get('updated_at'));
786 - $template_xml->addChildCData('flatten', $template->get('flatten'));
787 - $template_xml->addChildCData('format', $template->get('format'));
788 - $template_xml->addChildCData('compression', $template->get('compression'));
789 - $template_xml->addChildCData('appearance', $template->get('appearance'));
790 - $template_xml->addChildCData('width', $template->get('width'));
791 - $template_xml->addChildCData('height', $template->get('height'));
792 - $template_xml->addChildCData('extension', $template->get('extension'));
793 - $template_xml->addChildCData('item', $template->get('item'));
794 - $template_xml->addChildCData('dataset_title', $dataset_title);
795 - $template_xml->addChildCData('button_title', $button_title);
796 - $template_xml->addChildCData('inline', $template->get('inline'));
797 - $template_xml->addChildCData('auto', $template->get('auto'));
798 - $template_xml->addChildCData('name', $name);
799 - $template_xml->addChildCData('password', $password);
800 - $template_xml->addChildCData('font', $template->get('font'));
801 - $template_xml->addChildCData('font_size', $template->get('font_size'));
802 - $template_xml->addChildCData('font_color', $template->get('font_color'));
803 - $template_xml->addChildCData('line_height', $template->get('line_height'));
804 - $template_xml->addChildCData('fonts', base64_encode(serialize($template->get('fonts'))));
805 - $template_xml->addChildCData('pages', base64_encode(serialize($pages)));
806 -
807 - //include pdf
808 - if ($template->get('pdf')) {
809 - $pdf_xml = $xml->addChild('pdf');
810 - $pdf_xml->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf")));
811 - $pdf_xml_images = $pdf_xml->addChild('images');
812 - foreach ($template->get('pages') as $key => $page) {
813 - $pdf_xml_image = $pdf_xml_images->addChild('image');
814 - $pdf_xml_image->addChildCData('page_id', $page['page_id']);
815 - $pdf_xml_image->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . "/images/" . $page['page_id'] . ".png")));
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 + }
816 1136 }
817 - }
818 1137
819 - //include fonts
820 - if ($options['fonts']) {
821 - $fonts_xml = $xml->addChild('fonts');
822 - if ($template->get('fonts')) {
823 - $model_e2pdf_font = new Model_E2pdf_Font();
824 - foreach ($template->get('fonts') as $key => $value) {
825 - $font = $fonts_xml->addChild('font');
826 - $font->addChildCData('title', $value);
827 - $font->addChildCData('name', $key);
828 - $font->addChildCData('value', $model_e2pdf_font->get_font($key));
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));
1148 + }
829 1149 }
830 1150 }
831 - }
832 1151
833 - //include item
834 - if ($options['item']) {
835 - if ($template->extension()->method('backup')) {
836 - $item = $xml->addChild('item');
837 - $template->extension()->backup($item);
1152 + // include item
1153 + if ($options['item']) {
1154 + if ($template->extension()->method('backup')) {
1155 + $item = $xml->addChild('item');
1156 + $template->extension()->backup($item);
1157 + }
838 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');
839 1164 }
840 -
841 - $file = $this->helper->load('xml')->get_xml();
842 - $this->download_response('xml', $file, $filename);
843 - } else {
844 - $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
845 - $this->render('blocks', 'notifications');
846 1165 }
1166 + } else {
1167 + wp_die($this->message('wp_verify_nonce_error'));
847 1168 }
848 1169 }
849 1170
850 1171 $this->view('export_disabled', false);
@@ -849,9 +1170,9 @@
849 1170
850 1171 $this->view('export_disabled', false);
851 1172
852 1173 if (!$this->helper->load('xml')->check()) {
853 - $this->add_notification('error', sprintf(__("PHP Extension <strong>%s</strong> not found", 'e2pdf'), 'SimpleXml'));
1174 + $this->add_notification('error', sprintf(__('The PHP %s extension is required', 'e2pdf'), 'SimpleXml'));
854 1175 $this->view('export_disabled', true);
855 1176 }
856 1177
857 1178 $template = new Model_E2pdf_Template();
@@ -863,15 +1184,15 @@
863 1184 'common' => array(
864 1185 'name' => __('Options', 'e2pdf'),
865 1186 'options' => array(
866 1187 array(
867 - 'name' => __('Include Item', 'e2pdf'),
1188 + 'name' => __('Include Connection', 'e2pdf'),
868 1189 'key' => 'options[item]',
869 - 'value' => $template->extension()->method('backup') ? 1 : 0,
1190 + 'value' => $template->extension()->method('backup') ? '1' : '0',
870 1191 'default_value' => 0,
871 1192 'type' => 'checkbox',
872 1193 'placeholder' => '',
873 - 'disabled' => $template->extension()->method('backup') ? false : 'disabled'
1194 + 'disabled' => $template->extension()->method('backup') ? false : 'disabled',
874 1195 ),
875 1196 array(
876 1197 'name' => __('Include Images', 'e2pdf'),
877 1198 'key' => 'options[images]',
@@ -886,872 +1207,891 @@
886 1207 'value' => 1,
887 1208 'default_value' => 0,
888 1209 'type' => 'checkbox',
889 1210 'placeholder' => '',
890 - )
891 - )
892 - )
1211 + ),
1212 + ),
1213 + ),
893 1214 );
894 1215
895 1216 $options = apply_filters('e2pdf_controller_templates_backup_options', $options, $template, $template->extension());
896 -
897 1217 $this->view('options', $options);
898 1218 } else {
899 - $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
1219 + $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
900 1220 $this->render('blocks', 'notifications');
901 1221 }
902 1222 }
903 1223
904 - /**
905 - * Save template via ajax
906 - * action: wp_ajax_e2pdf_save_form
907 - * function: e2pdf_save_form
908 - *
909 - * @return json
910 - */
911 - public function ajax_save_form() {
912 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
913 -
914 - $data = json_decode($this->post->get('data'), true);
915 -
916 - $template = new Model_E2pdf_Template();
917 - if (isset($data['ID'])) {
918 - $template->load($data['ID']);
919 - if ($template->get('pdf') && $template->get('pdf') !== $data['pdf']) {
920 - $old_pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/";
921 - $this->helper->delete_dir($old_pdf_path);
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);
922 1231 }
923 1232 }
1233 + $this->redirect(
1234 + $this->helper->get_url(
1235 + [
1236 + 'page' => 'e2pdf-templates',
1237 + ]
1238 + )
1239 + );
1240 + }
924 1241
925 - foreach ($data as $key => $value) {
926 - $template->set($key, $value);
1242 + // ajax save form
1243 + public function ajax_save_form() {
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']);
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');
927 1300 }
928 - $template->save(true);
929 -
930 - if ($template->get('activated')) {
931 - $this->activate_template($template->get('ID'));
932 - }
933 -
934 - $response = array(
935 - 'redirect' => $this->helper->get_url(array(
936 - 'page' => 'e2pdf-templates',
937 - 'action' => 'edit',
938 - 'id' => $template->get('ID'))
939 - )
940 - );
941 -
942 - $this->add_notification('update', __('Template saved successfully', 'e2pdf'));
943 1301 $this->json_response($response);
944 1302 }
945 1303
946 1304 public function ajax_get_styles() {
947 1305
948 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
949 - $data = $this->post->get('data');
950 -
951 - $extension = new Model_E2pdf_Extension();
952 - if ($extension->load($data)) {
953 - $content = $extension->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 + );
954 1318 } else {
955 - $content = array();
1319 + $response['error'] = $this->message('wp_verify_nonce_error');
956 1320 }
957 - $response = array(
958 - 'content' => $content,
959 - );
960 1321 $this->json_response($response);
961 1322 }
962 1323
963 - /**
964 - * Get extensions via ajax
965 - * action: wp_ajax_e2pdf_extension
966 - * function: e2pdf_extension
967 - *
968 - * @return json
969 - */
1324 + // ajax extension
970 1325 public function ajax_extension() {
971 -
972 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
973 - $data = $this->post->get('data');
974 -
975 - $extension = new Model_E2pdf_Extension();
976 -
977 - $content = array();
978 - if ($extension->load($data)) {
979 - $content = $extension->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');
980 1357 }
981 -
982 -
983 - $select = new stdClass();
984 - $select->id = '';
985 - $select->url = 'javascript:void(0);';
986 - $select->name = __("--- Select ---", "e2pdf");
987 -
988 - array_unshift($content, $select);
989 -
990 - $response = array(
991 - 'content' => $content,
992 - );
993 1358 $this->json_response($response);
994 1359 }
995 1360
996 - /**
997 - * Get extensions via ajax
998 - * action: wp_ajax_e2pdf_extension
999 - * function: e2pdf_extension
1000 - *
1001 - * @return json
1002 - */
1361 + // ajax visual mapper
1003 1362 public function ajax_visual_mapper() {
1004 -
1005 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1006 -
1007 - $data = $this->post->get('data');
1008 -
1009 - $item = isset($data['item']) ? $data['item'] : '';
1010 -
1011 - $extension = new Model_E2pdf_Extension();
1012 - $extension->load($data['extension']);
1013 - $extension->set('item', $item);
1014 -
1015 - if ($visual_mapper = $extension->visual_mapper()) {
1016 - $response = array(
1017 - 'content' => $visual_mapper
1018 - );
1019 - } elseif (!$item) {
1020 - $response = array(
1021 - 'content' => __("You must set Item to use <strong>Visual Mapper</strong>", "e2pdf"),
1022 - );
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 + }
1023 1389 } else {
1024 - $response = array(
1025 - 'content' => __("Sorry, this extension doesn't support <strong>Visual Mapper</strong>", "e2pdf"),
1026 - );
1390 + $response['error'] = $this->message('wp_verify_nonce_error');
1027 1391 }
1028 -
1029 1392 $this->json_response($response);
1030 1393 }
1031 1394
1032 - /**
1033 - * Upload Template via Ajax
1034 - * action: wp_ajax_e2pdf_upload
1035 - * function: e2pdf_upload
1036 - *
1037 - * @return json
1038 - */
1395 + // ajax upload
1039 1396 public function ajax_upload() {
1040 1397
1041 - $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 + }
1042 1405
1043 1406 $data = $this->post->get();
1044 -
1045 1407 $post_extension = $data['extension'];
1046 1408 $item = isset($data['item']) ? $data['item'] : '';
1047 1409 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1048 -
1049 1410 $font = isset($data['font']) ? $data['font'] : false;
1050 1411 $font_size = isset($data['font_size']) ? $data['font_size'] : false;
1051 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';
1052 1416
1053 - $allowed_ext = array(
1054 - 'pdf'
1055 - );
1056 -
1057 - $pdf = $this->files->get('pdf');
1058 -
1059 - $name = strtolower($pdf['name']);
1060 - $tmp = $pdf['tmp_name'];
1061 - $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
1062 -
1063 - if ($pdf['error']) {
1064 - $error = $pdf['error'];
1065 - } elseif (!in_array($ext, $allowed_ext)) {
1066 - $error = __('Incorrect file extension', 'e2pdf');
1067 - } elseif ($pdf['type'] != 'application/pdf') {
1068 - $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 + );
1069 1424 }
1070 1425
1071 - if ($error) {
1072 - $response = array(
1073 - 'error' => $error,
1074 - );
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;
1075 1429 } else {
1430 + $upload = base64_encode(file_get_contents($file['tmp_name']));
1431 + }
1076 1432
1077 - $model_e2pdf_api = new Model_E2pdf_Api();
1078 - $model_e2pdf_api->set(array(
1079 - 'action' => 'template/upload',
1080 - 'data' => array(
1081 - 'title' => $name,
1082 - '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 + ),
1083 1441 )
1084 - ));
1085 - $request = $model_e2pdf_api->request();
1442 + );
1443 + $request = $model_e2pdf_api->request();
1086 1444
1087 - if (!isset($request['error'])) {
1445 + if (isset($request['error'])) {
1446 + $this->json_response(
1447 + [
1448 + 'error' => $request['error'],
1449 + ]
1450 + );
1451 + }
1088 1452
1089 - $extension = new Model_E2pdf_Extension();
1090 - if ($post_extension && $item) {
1091 - $extension->load($post_extension);
1092 - $extension->set('item', $item);
1093 - }
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 + }
1094 1460
1095 - $pdf_name = md5(time());
1096 - $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
1097 - $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);
1098 1466
1099 - $this->helper->create_dir($pdf_dir);
1100 - $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 + }
1101 1474
1102 - move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . ".pdf");
1103 -
1104 - $xml = simplexml_load_string(base64_decode($request['file']), "SimpleXMLElement", LIBXML_PARSEHUGE);
1105 - $pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages)));
1106 -
1107 - if (is_array($pages) && !empty($pages)) {
1108 - foreach ($pages as $page_key => $page) {
1109 -
1110 - $pages[$page_key]['page_id'] = $page_key;
1111 -
1112 - if (isset($page['properties']['background'])) {
1113 - file_put_contents($pdf_images_dir . $page_key . ".png", base64_decode($page['properties']['background']));
1114 - unset($pages[$page_key]['properties']['background']);
1115 - }
1116 -
1117 - if (isset($page['elements']) && !empty($page['elements'])) {
1118 - foreach ($page['elements'] as $element_key => $element) {
1119 - if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
1120 -
1121 - $image = base64_decode($element['base64']);
1122 - $file_name = basename($element['value']);
1123 -
1124 - //exif_imagetype
1125 - if (!$file_name) {
1126 - $ext = $this->helper->load('image')->get_image_extension($image);
1127 - if ($ext) {
1128 - $file_name = md5(mktime()) . "." . $ext;
1129 - }
1130 - }
1131 -
1132 - if ($file_name) {
1133 - $upload_file = wp_upload_bits($file_name, null, $image);
1134 - if (!$upload_file['error']) {
1135 - $wp_filetype = wp_check_filetype($file_name, null);
1136 - $attachment = array(
1137 - 'post_mime_type' => $wp_filetype['type'],
1138 - 'post_parent' => 0,
1139 - 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
1140 - 'post_content' => '',
1141 - 'post_status' => 'inherit'
1142 - );
1143 - $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
1144 - if (!is_wp_error($attachment_id)) {
1145 - require_once(ABSPATH . "wp-admin" . '/includes/image.php');
1146 - $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
1147 - wp_update_attachment_metadata($attachment_id, $attachment_data);
1148 - $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
1149 - }
1150 - }
1151 -
1152 - unset($pages[$page_key]['elements'][$element_key]['base64']);
1153 - } else {
1154 - unset($pages[$page_key]['elements'][$element_key]);
1155 - }
1156 - } elseif (isset($element['name']) && $element['name']) {
1157 - $el_value = $extension->auto_map($element['name']);
1158 - if ($el_value !== false) {
1159 - $pages[$page_key]['elements'][$element_key]['value'] = $el_value;
1160 - }
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);
1161 1512 }
1162 - }
1513 + break;
1514 + default:
1515 + break;
1163 1516 }
1164 1517 }
1518 + }
1165 1519
1166 - $this->helper->load('sort')->stable_uasort($pages, "sort_by_pageid");
1167 -
1168 - $template = new Model_E2pdf_Template();
1169 -
1170 - if ($template_id) {
1171 - $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);
1172 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 + }
1173 1552
1174 - if ($template_id) {
1175 - if ($template->get('pdf')) {
1176 - $old_pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/";
1177 - $this->helper->delete_dir($old_pdf_path);
1178 - }
1179 - $template->set('pages', $pages);
1180 - $template->set('pdf', $pdf_name);
1181 - $template->set('width', (String) $xml->template->width);
1182 - $template->set('height', (String) $xml->template->height);
1183 - $template->set('extension', $post_extension);
1184 - $template->set('item', $item);
1185 - $template->set('font', $font ? $font : (String) $xml->template->font);
1186 - $template->set('font_size', $font_size ? $font_size : (String) $xml->template->font_size);
1187 - $template->set('line_height', $line_height ? $line_height : (String) $xml->template->font_size);
1188 - } else {
1189 - $template->set('title', (String) $xml->template->title);
1190 - $template->set('flatten', (String) $xml->template->flatten);
1191 - $template->set('format', (String) $xml->template->format);
1192 - $template->set('compression', (String) $xml->template->compression);
1193 - $template->set('appearance', (String) $xml->template->appearance);
1194 - $template->set('width', (String) $xml->template->width);
1195 - $template->set('height', (String) $xml->template->height);
1196 - $template->set('extension', $post_extension);
1197 - $template->set('item', $item);
1198 - $template->set('dataset_title', "");
1199 - $template->set('button_title', "");
1200 - $template->set('inline', "");
1201 - $template->set('auto', "");
1202 - $template->set('name', "");
1203 - $template->set('password', "");
1204 - $template->set('font', $font ? $font : (String) $xml->template->font);
1205 - $template->set('font_size', $font_size ? $font_size : (String) $xml->template->font_size);
1206 - $template->set('line_height', $line_height ? $line_height : (String) $xml->template->font_size);
1207 - $template->set('font_color', (String) $xml->template->font_color);
1553 + if (empty($pages) || !is_array($pages)) {
1554 + throw new Exception(__('Failed to parse PDF pages', 'e2pdf'));
1555 + }
1208 1556
1209 - $template->set('pages', $pages);
1210 - $template->set('pdf', $pdf_name);
1211 - }
1557 + $this->helper->load('sort')->stable_uasort($pages, 'sort_by_pageid');
1212 1558
1213 - if ($xml->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);
1214 1608
1215 - $model_e2pdf_font = new Model_E2pdf_Font();
1216 - $fonts = $model_e2pdf_font->get_fonts();
1609 + if ($item == '-1' && $extension->method('auto_form')) {
1610 + $template = $extension->auto_form($template, $data);
1611 + $template->save(true);
1612 + }
1217 1613
1218 - foreach ($xml->fonts->children() as $key => $font) {
1219 - $title = (String) $font->title;
1220 - $name = (String) $font->name;
1221 - $value = (String) $font->value;
1614 + if (!$template_id) {
1615 + $template->activate();
1616 + }
1222 1617
1223 - $exist = array_search($title, $fonts);
1224 - if (!$exist) {
1225 - if (!file_exists($this->helper->get('fonts_dir') . $name)) {
1226 - $f_name = $name;
1227 - } else {
1228 - $i = 0;
1229 - do {
1230 - $f_name = $i . '_' . $name;
1231 - $i++;
1232 - } while (file_exists($this->helper->get('fonts_dir') . $f_name));
1233 - }
1234 - file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value));
1235 - }
1236 - }
1237 - }
1238 - $template->save(true);
1239 -
1240 - $response = array(
1241 - 'redirect' => $this->helper->get_url(array(
1242 - 'page' => 'e2pdf-templates',
1243 - 'action' => 'edit',
1244 - 'id' => $template->get('ID'))
1245 - )
1246 - );
1247 - } else {
1248 - $this->helper->delete_dir($pdf_dir);
1249 - $response = array(
1250 - 'error' => __("PDF can't be parsed", 'e2pdf'),
1251 - );
1252 - }
1253 - } else {
1254 - $response = array(
1255 - 'error' => $request['error'],
1256 - );
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);
1257 1632 }
1633 + $this->json_response(
1634 + [
1635 + 'error' => $ex->getMessage(),
1636 + ]
1637 + );
1258 1638 }
1259 -
1260 - $this->json_response($response);
1261 1639 }
1262 1640
1263 - /**
1264 - * Upload Template via Ajax
1265 - * action: wp_ajax_e2pdf_upload
1266 - * function: e2pdf_upload
1267 - *
1268 - * @return json
1269 - */
1641 + // ajax reupload
1270 1642 public function ajax_reupload() {
1271 1643
1272 - $error = false;
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 + }
1273 1651
1274 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1275 -
1276 1652 $data = $this->post->get();
1277 -
1278 1653 $new = isset($data['new']) ? $data['new'] : array();
1279 1654 $flush = isset($data['flush']) ? $data['flush'] : array();
1280 1655 $positions = isset($data['positions']) ? $data['positions'] : array();
1281 -
1282 1656 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1283 -
1284 1657 if (!$template_id) {
1285 1658 return false;
1286 1659 }
1287 1660
1288 - $allowed_ext = array(
1289 - 'pdf'
1290 - );
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 + }
1291 1669
1292 - $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 + }
1293 1676
1294 - $name = strtolower($pdf['name']);
1295 - $tmp = $pdf['tmp_name'];
1296 - $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();
1297 1688
1298 - if ($pdf['error']) {
1299 - $error = $pdf['error'];
1300 - } elseif (!in_array($ext, $allowed_ext)) {
1301 - $error = __('Incorrect file extension', 'e2pdf');
1302 - } elseif ($pdf['type'] != 'application/pdf') {
1303 - $error = __('Incompatible type', 'e2pdf');
1689 + if (isset($request['error'])) {
1690 + $this->json_response(
1691 + [
1692 + 'error' => $request['error'],
1693 + ]
1694 + );
1304 1695 }
1305 1696
1306 - if ($error) {
1307 - $response = array(
1308 - '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 + ]
1309 1703 );
1310 - } else {
1704 + }
1311 1705
1312 - $model_e2pdf_api = new Model_E2pdf_Api();
1313 - $model_e2pdf_api->set(array(
1314 - 'action' => 'template/upload',
1315 - 'data' => array(
1316 - 'title' => $name,
1317 - 'pdf' => base64_encode(file_get_contents($pdf['tmp_name']))
1318 - )
1319 - ));
1320 - $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 + }
1321 1711
1322 - 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);
1323 1717
1324 - $template = new Model_E2pdf_Template();
1325 - 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 + }
1326 1725
1327 - if ($template->get('pdf')) {
1328 -
1329 - $extension = new Model_E2pdf_Extension();
1330 - if ($template->get('extension') && $template->get('item')) {
1331 - $extension->load($template->get('extension'));
1332 - $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'];
1333 1739 }
1334 -
1335 - $xml = simplexml_load_string(base64_decode($request['file']), "SimpleXMLElement", LIBXML_PARSEHUGE);
1336 - $pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages)));
1337 -
1338 - foreach ($pages as $page_key => $page) {
1339 - $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));
1340 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 + }
1341 1791
1342 - $this->helper->load('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 + }
1343 1795
1344 - $model_e2pdf_element = new Model_E2pdf_Element();
1345 - $last_element_id = $model_e2pdf_element->get_last_element_id($template_id);
1796 + $this->helper->load('sort')->stable_uasort($pages, 'sort_by_pageid');
1346 1797
1347 - $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');
1348 1801
1349 - foreach ($positions as $pos_key => $pos_page) {
1350 - if (!in_array($pos_key, $flush)) {
1351 - if (!isset($pages[$pos_page])) {
1352 - $error = sprintf(__("Position of %s page incorrect", 'e2pdf'), $pos_key);
1353 - } elseif ($pages[$pos_page]['properties']['width'] < $original_pages[$pos_key]['properties']['width']) {
1354 - $error = sprintf(__("Width of PDF Page %s can't be less width of Template Page %s", 'e2pdf'), $pos_page, $pos_key);
1355 - } elseif ($pages[$pos_page]['properties']['height'] < $original_pages[$pos_key]['properties']['height']) {
1356 - $error = sprintf(__("Height of PDF Page %s can't be less height of Template Page %s", 'e2pdf'), $pos_page, $pos_key);
1357 - }
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);
1358 1815 }
1359 - }
1360 -
1361 - if (!$error) {
1362 -
1363 - $pdf_name = md5(time());
1364 - $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
1365 - $pdf_images_dir = $pdf_dir . "images/";
1366 -
1367 - $this->helper->create_dir($pdf_dir);
1368 - $this->helper->create_dir($pdf_images_dir);
1369 -
1370 - move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . ".pdf");
1371 -
1372 - if (is_array($pages) && !empty($pages)) {
1373 - foreach ($pages as $page_key => $page) {
1374 -
1375 - if (isset($page['properties']['background'])) {
1376 - file_put_contents($pdf_images_dir . $page_key . ".png", base64_decode($page['properties']['background']));
1377 - unset($pages[$page_key]['properties']['background']);
1378 - }
1379 -
1380 - if (isset($page['elements']) && !empty($page['elements'])) {
1381 - foreach ($page['elements'] as $element_key => $element) {
1382 -
1383 - $last_element_id++;
1384 - $pages[$page_key]['elements'][$element_key]['element_id'] = $last_element_id;
1385 -
1386 - if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
1387 -
1388 - $image = base64_decode($element['base64']);
1389 - $file_name = basename($element['value']);
1390 -
1391 - //exif_imagetype
1392 - if (!$file_name) {
1393 - $ext = $this->helper->load('image')->get_image_extension($image);
1394 - if ($ext) {
1395 - $file_name = md5(mktime()) . "." . $ext;
1396 - }
1397 - }
1398 -
1399 - if ($file_name) {
1400 - $upload_file = wp_upload_bits($file_name, null, $image);
1401 - if (!$upload_file['error']) {
1402 - $wp_filetype = wp_check_filetype($file_name, null);
1403 - $attachment = array(
1404 - 'post_mime_type' => $wp_filetype['type'],
1405 - 'post_parent' => 0,
1406 - 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
1407 - 'post_content' => '',
1408 - 'post_status' => 'inherit'
1409 - );
1410 - $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
1411 - if (!is_wp_error($attachment_id)) {
1412 - require_once(ABSPATH . "wp-admin" . '/includes/image.php');
1413 - $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
1414 - wp_update_attachment_metadata($attachment_id, $attachment_data);
1415 - $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
1416 - }
1417 - }
1418 - unset($pages[$page_key]['elements'][$element_key]['base64']);
1419 - } else {
1420 - unset($pages[$page_key]['elements'][$element_key]);
1421 - }
1422 - } elseif (isset($element['name']) && $element['name']) {
1423 - $el_value = $extension->auto_map($element['name']);
1424 - if ($el_value !== false) {
1425 - $pages[$page_key]['elements'][$element_key]['value'] = $el_value;
1426 - }
1427 - }
1428 - }
1429 - }
1430 -
1431 -
1432 - foreach ($positions as $pos_key => $pos_page) {
1433 - if ($pos_page == $page_key) {
1434 - if (in_array($pos_key, $new)) {
1435 - if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1436 - $elements = $original_pages[$pos_key]['elements'];
1437 - $pages[$page_key]['elements'] = array_merge($pages[$page_key]['elements'], $elements);
1438 - }
1439 - } else {
1440 - if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1441 - $elements = $original_pages[$pos_key]['elements'];
1442 - $pages[$page_key]['elements'] = $elements;
1443 - } else {
1444 - $pages[$page_key]['elements'] = array();
1445 - }
1446 - }
1447 - }
1448 - }
1449 - }
1450 -
1451 - $pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/";
1452 - $this->helper->delete_dir($pdf_path);
1453 -
1454 - $template->set('pdf', $pdf_name);
1455 - $template->set('pages', $pages);
1456 - $template->save(true);
1457 -
1458 - $response = array(
1459 - 'redirect' => $this->helper->get_url(array(
1460 - 'page' => 'e2pdf-templates',
1461 - 'action' => 'edit',
1462 - 'id' => $template->get('ID'))
1463 - )
1464 - );
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;
1465 1820 } else {
1466 - $this->helper->delete_dir($pdf_dir);
1467 - $response = array(
1468 - 'error' => __("PDF can't be parsed", 'e2pdf'),
1469 - );
1821 + $pages[$page_key]['elements'] = array();
1470 1822 }
1471 - } else {
1472 - $response = array(
1473 - 'error' => $error,
1474 - );
1475 1823 }
1824 + if (isset($original_pages[$pos_key]['actions'])) {
1825 + $pages[$page_key]['actions'] = $original_pages[$pos_key]['actions'];
1826 + }
1476 1827 }
1477 1828 }
1478 - } else {
1479 - $response = array(
1480 - 'error' => $request['error'],
1481 - );
1482 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 + );
1483 1855 }
1484 -
1485 - $this->json_response($response);
1486 1856 }
1487 1857
1488 - /**
1489 - * Auto Generation of Template
1490 - * action: wp_ajax_e2pdf_auto
1491 - * function: e2pdf_auto
1492 - *
1493 - * @return json
1494 - */
1858 + // ajax auto
1495 1859 public function ajax_auto() {
1496 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1497 - $data = $this->post->get('data');
1860 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1861 + $data = $this->post->get('data');
1498 1862
1499 - $extension = $data['extension'];
1500 - $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';
1501 1869
1502 - if (!$extension || !$item) {
1503 - return;
1504 - }
1870 + if (!$extension || !$item) {
1871 + return;
1872 + }
1505 1873
1506 - $model_e2pdf_extension = new Model_E2pdf_Extension();
1507 - $model_e2pdf_extension->load($extension);
1508 - $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);
1509 1881
1510 - if (method_exists($model_e2pdf_extension->extension(), 'auto')) {
1511 - $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 + );
1512 1891 } else {
1513 - $content = array();
1892 + $response['error'] = $this->message('wp_verify_nonce_error');
1514 1893 }
1515 -
1516 - $response = array(
1517 - 'content' => $content,
1518 - );
1519 1894 $this->json_response($response);
1520 1895 }
1521 1896
1897 + // ajax activate template
1522 1898 public function ajax_activate_template() {
1523 -
1524 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1525 - $data = $this->post->get('data');
1526 -
1527 - $id = $data['id'];
1528 -
1529 - if (!$id) {
1530 - return;
1531 - }
1532 -
1533 - $request = $this->activate_template($id);
1534 - if (isset($request['error'])) {
1535 - $response = array(
1536 - 'error' => $request['error'],
1537 - );
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 + }
1538 1915 } else {
1539 - $response = array(
1540 - 'content' => true,
1541 - );
1916 + $response['error'] = $this->message('wp_verify_nonce_error');
1542 1917 }
1543 -
1544 -
1545 1918 $this->json_response($response);
1546 1919 }
1547 1920
1921 + // ajax deactivaate template
1548 1922 public function ajax_deactivate_template() {
1549 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1550 - $data = $this->post->get('data');
1551 -
1552 - $id = $data['id'];
1553 -
1554 - if (!$id) {
1555 - 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');
1556 1941 }
1942 + $this->json_response($response);
1943 + }
1557 1944
1558 - $request = $this->deactivate_template($id);
1559 - if (isset($request['error'])) {
1560 - $response = array(
1561 - '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 + )
1562 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 + }
1563 1985 } else {
1564 - $response = array(
1565 - 'content' => true,
1566 - );
1986 + $response['error'] = $this->message('wp_verify_nonce_error');
1567 1987 }
1568 1988 $this->json_response($response);
1569 1989 }
1570 1990
1571 - /**
1572 - * Move template to trash
1573 - *
1574 - * @param int $id - ID of template
1575 - */
1991 + // trash template
1576 1992 public function trash_template($id = false) {
1577 -
1578 1993 $id = (int) $id;
1579 -
1580 - if (!$id) {
1581 - 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;
1582 2000 }
1583 - $template = new Model_E2pdf_Template();
1584 - $template->load($id);
1585 - $template->set('trash', '1');
1586 - $template->save();
2001 + return false;
1587 2002 }
1588 2003
1589 - /**
1590 - * Delete template
1591 - *
1592 - * @param int $id - ID of template
1593 - */
2004 + // delete template
1594 2005 public function delete_template($id = false) {
1595 -
1596 2006 $id = (int) $id;
1597 -
1598 - if (!$id) {
1599 - 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;
1600 2012 }
1601 -
1602 - $template = new Model_E2pdf_Template();
1603 - $template->load($id);
1604 - $this->deactivate_template($template->get('ID'));
1605 - $template->delete();
2013 + return false;
1606 2014 }
1607 2015
1608 - /**
1609 - * Restore template
1610 - *
1611 - * @param int $id - ID of template
1612 - */
2016 + // restore template
1613 2017 public function restore_template($id = false) {
1614 -
1615 2018 $id = (int) $id;
1616 -
1617 - if (!$id) {
1618 - 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;
1619 2024 }
1620 - $template = new Model_E2pdf_Template();
1621 - $template->load($id);
1622 - $template->set('trash', '0');
1623 - $template->save();
2025 + return false;
1624 2026 }
1625 2027
1626 - /**
1627 - * Duplicate template
1628 - *
1629 - * @param int $id - ID of template
1630 - */
2028 + // duplicate template
1631 2029 public function duplicate_template($id = false) {
1632 2030
1633 2031 $id = (int) $id;
1634 2032
1635 - if (!$id) {
1636 - return false;
1637 - }
1638 -
1639 2033 $template = new Model_E2pdf_Template();
1640 - $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/';
1641 2044
1642 - $title = $template->get('title') . " " . __('(Copy)', 'e2pdf');
1643 - $template->set('ID', false);
1644 - $template->set('uid', '');
1645 - $template->set('title', $title);
1646 - $template->set('activated', '0');
2045 + $this->helper->create_dir($pdf_dir);
2046 + $this->helper->create_dir($pdf_images_dir);
1647 2047
1648 - if ($template->get('pdf')) {
1649 - $pdf_name = md5(time());
1650 - $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
1651 - $pdf_images_dir = $pdf_dir . "images/";
1652 -
1653 - $this->helper->create_dir($pdf_dir);
1654 - $this->helper->create_dir($pdf_images_dir);
1655 -
1656 -
1657 - if (file_exists($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf")) {
1658 - $images = glob($this->helper->get('pdf_dir') . $template->get('pdf') . "/images/*");
1659 - foreach ($images as $image) {
1660 - 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);
1661 2055 }
1662 - copy($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf", $this->helper->get('pdf_dir') . $pdf_name . "/" . $pdf_name . ".pdf");
1663 - $template->set('pdf', $pdf_name);
1664 2056 }
2057 + $template->save(true);
2058 + return true;
1665 2059 }
1666 -
1667 -
1668 - $template->save(true);
2060 + return false;
1669 2061 }
1670 2062
1671 - /**
1672 - * Activate template
1673 - *
1674 - * @param int $id - ID of template
1675 - */
2063 + // activate template
1676 2064 public function activate_template($id = false) {
1677 -
1678 2065 $id = (int) $id;
1679 -
1680 - if (!$id) {
1681 - return false;
1682 - }
1683 -
1684 2066 $template = new Model_E2pdf_Template();
1685 - $template->load($id);
1686 -
1687 - $model_e2pdf_api = new Model_E2pdf_Api();
1688 - $model_e2pdf_api->set(array(
1689 - 'action' => 'template/activate',
1690 - 'data' => array(
1691 - 'template_id' => $template->get('ID'),
1692 - 'template_uid' => $template->get('uid'),
1693 - 'template_title' => $template->get('title')
1694 - )
1695 - ));
1696 - $request = $model_e2pdf_api->request();
1697 -
1698 - if (!isset($request['error'])) {
1699 - $template->set('activated', 1);
1700 - $template->set('uid', $request['template_uid']);
1701 - $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 + );
1702 2073 }
1703 -
1704 - return $request;
1705 2074 }
1706 2075
1707 - /**
1708 - * Deactivate template
1709 - *
1710 - * @param int $id - ID of template
1711 - */
2076 + // deactivate template
1712 2077 public function deactivate_template($id = false) {
1713 -
1714 2078 $id = (int) $id;
1715 -
1716 - if (!$id) {
1717 - return false;
1718 - }
1719 -
1720 2079 $template = new Model_E2pdf_Template();
1721 - $template->load($id);
1722 -
1723 - $model_e2pdf_api = new Model_E2pdf_Api();
1724 - $model_e2pdf_api->set(array(
1725 - 'action' => 'template/deactivate',
1726 - 'data' => array(
1727 - 'template_id' => $template->get('ID'),
1728 - 'template_uid' => $template->get('uid'),
1729 - )
1730 - ));
1731 - $request = $model_e2pdf_api->request();
1732 -
1733 - if (!isset($request['error'])) {
1734 - $template->set('activated', 0);
1735 - $template->set('uid', '');
1736 - $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 + );
1737 2086 }
1738 -
1739 - return $request;
1740 2087 }
1741 2088
1742 - /**
1743 - * Get templates list
1744 - *
1745 - * @param array() $filters - Array of filter/order conditions
1746 - * @param bool $count - Return number of templates
1747 - *
1748 - * @return mixed - IF $count - int ELSE array()
1749 - */
2089 + // get templates list
1750 2090 public function get_templates_list($filters = array(), $count = false) {
1751 2091 global $wpdb;
2092 +
1752 2093 $model_e2pdf_template = new Model_E2pdf_Template();
1753 -
1754 2094 $condition = array();
1755 2095 if (isset($filters['status']) && $filters['status'] == 'trash') {
1756 2096 $condition = array(
1757 2097 'trash' => array(
@@ -1756,10 +2096,10 @@
1756 2096 $condition = array(
1757 2097 'trash' => array(
1758 2098 'condition' => '=',
1759 2099 'value' => '1',
1760 - 'type' => '%d'
1761 - )
2100 + 'type' => '%d',
2101 + ),
1762 2102 );
1763 2103 } else {
1764 2104 $condition = array(
1765 2105 'trash' => array(
@@ -1764,10 +2104,10 @@
1764 2104 $condition = array(
1765 2105 'trash' => array(
1766 2106 'condition' => '<>',
1767 2107 'value' => '1',
1768 - 'type' => '%d'
1769 - )
2108 + 'type' => '%d',
2109 + ),
1770 2110 );
1771 2111 }
1772 2112
1773 2113 if (isset($filters['s']) && $filters['s']) {
@@ -1773,18 +2113,27 @@
1773 2113 if (isset($filters['s']) && $filters['s']) {
1774 2114 $condition['title'] = array(
1775 2115 'condition' => 'LIKE',
1776 2116 'value' => '%%' . $filters['s'] . '%%',
1777 - '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 + ),
1778 2127 );
1779 2128 }
1780 2129
1781 -
1782 2130 $order_condition = array();
1783 - 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'])) {
1784 2133 $order_condition = array(
1785 2134 'orderby' => $filters['orderby'],
1786 - 'order' => $filters['order'],
2135 + 'order' => $filters['order'] == 'asc' ? 'asc' : 'desc',
1787 2136 );
1788 2137 } else {
1789 2138 $order_condition = array(
1790 2139 'orderby' => 'id',
@@ -1791,13 +2140,25 @@
1791 2140 'order' => 'desc',
1792 2141 );
1793 2142 }
1794 2143
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 + }
2154 +
1795 2155 $where = $this->helper->load('db')->prepare_where($condition);
1796 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']));
1797 2160
1798 - $tpls = $wpdb->get_results($wpdb->prepare("SELECT `ID` FROM " . $model_e2pdf_template->get_table() . $where['sql'] . $orderby . "", $where['filter']));
1799 -
1800 2161 if ($count) {
1801 2162 return count($tpls);
1802 2163 }
1803 2164
@@ -1803,35 +2164,33 @@
1803 2164
1804 2165 $templates = array();
1805 2166 foreach ($tpls as $key => $tpl) {
1806 2167 $template = new Model_E2pdf_Template();
1807 - $template->load($tpl->ID);
2168 + $template->load($tpl->ID, false);
1808 2169 if (is_array($this->helper->get('license')->get('activated_templates'))) {
1809 2170 if ($template->get('activated') && !in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
1810 - $this->deactivate_template($template->get('ID'));
2171 + $template->deactivate();
1811 2172 } elseif (!$template->get('activated') && in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
1812 - $this->activate_template($template->get('ID'));
2173 + $template->activate();
1813 2174 }
1814 2175 }
1815 -
1816 2176 $templates[] = $template;
1817 2177 }
1818 -
1819 2178 return $templates;
1820 2179 }
1821 2180
1822 - /**
1823 - * Get paper sizes list
1824 - *
1825 - * @return array() - Sizes list
1826 - */
2181 + // get sizes list
1827 2182 public function get_sizes_list($size = false, $attr = false) {
1828 2183
1829 2184 $sizes = array(
1830 - 'A4' => array(
2185 + 'A4 (PORTRAIT)' => array(
1831 2186 'width' => '595',
1832 2187 'height' => '842',
1833 2188 ),
2189 + 'A4 (LANDSCAPE)' => array(
2190 + 'width' => '842',
2191 + 'height' => '595',
2192 + ),
1834 2193 'LETTER' => array(
1835 2194 'width' => '612',
1836 2195 'height' => '792',
1837 2196 ),
@@ -1866,15 +2225,10 @@
1866 2225 return false;
1867 2226 }
1868 2227 }
1869 2228
1870 - /**
1871 - * Get fonts list
1872 - *
1873 - * @return array() - Fonts list
1874 - */
2229 + // get fonts
1875 2230 public function get_fonts($with_path = false) {
1876 -
1877 2231 $model_e2pdf_font = new Model_E2pdf_Font();
1878 2232 $fonts = $model_e2pdf_font->get_fonts();
1879 2233
1880 2234 if ($with_path) {
@@ -1883,9 +2237,9 @@
1883 2237 'key' => $value,
1884 2238 'subfield' => array(
1885 2239 'path' => $key,
1886 2240 ),
1887 - 'value' => $value
2241 + 'value' => $value,
1888 2242 );
1889 2243 }
1890 2244 }
1891 2245
@@ -1891,43 +2245,324 @@
1891 2245
1892 2246 return $fonts;
1893 2247 }
1894 2248
1895 - /**
1896 - * Get font sizes list
1897 - *
1898 - * @return array() - Font sizes list
1899 - */
2249 + // get font sizes
1900 2250 public function get_font_sizes() {
1901 -
2251 + $max_font_size = apply_filters('e2pdf_controller_templates_max_font_size', 512);
1902 2252 $sizes = array();
1903 - for ($i = 1; $i <= 72; $i++) {
1904 - $sizes[$i] = $i . 'px';
2253 + for ($i = 1; $i <= $max_font_size; $i++) {
2254 + $sizes[$i] = $i;
1905 2255 }
1906 2256 return $sizes;
1907 2257 }
1908 2258
1909 - /**
1910 - * Get line heights list
1911 - *
1912 - * @return array() - Line heights list
1913 - */
2259 + // get line heights
1914 2260 public function get_line_heights() {
1915 2261
2262 + $max_line_height = apply_filters('e2pdf_controller_templates_max_line_height', 512);
1916 2263 $line_heights = array();
1917 - for ($i = 1; $i <= 512; $i++) {
1918 - $line_heights[$i] = $i . 'px';
2264 + for ($i = 1; $i <= $max_line_height; $i++) {
2265 + $line_heights[$i] = $i;
1919 2266 }
1920 2267 return $line_heights;
1921 2268 }
1922 2269
1923 - /**
1924 - * Load metaboxes on template edit/create action
1925 - */
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
1926 2562 public function load_metaboxes() {
1927 -
1928 2563 add_meta_box(
1929 - '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')
1930 2565 );
1931 2566 add_meta_box(
1932 2567 'e2pdf_templates_builder', __('PDF Builder', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_builder')
1933 2568 );
@@ -1935,13 +2570,10 @@
1935 2570 'e2pdf_templates_settings', __('Settings', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_settings')
1936 2571 );
1937 2572 }
1938 2573
1939 - /**
1940 - * Load javascript on template edit/create action
1941 - */
2574 + // load scripts
1942 2575 public function load_scripts() {
1943 -
1944 2576 wp_enqueue_script('postbox');
1945 2577 wp_enqueue_script('jquery-ui-droppable');
1946 2578 wp_enqueue_script('jquery-ui-resizable');
1947 2579 wp_enqueue_script('jquery-ui-selectable');
@@ -1950,15 +2582,13 @@
1950 2582 wp_enqueue_script('wp-color-picker');
1951 2583 wp_enqueue_media();
1952 2584 }
1953 2585
1954 - /**
1955 - * Load styles on template edit/create action
1956 - */
2586 + // load styles
1957 2587 public function load_styles($ext = false) {
1958 - 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);
1959 2590 wp_enqueue_style('wp-color-picker');
1960 -
1961 2591 if ($ext) {
1962 2592 $extension = new Model_E2pdf_Extension();
1963 2593 if ($extension->load($ext)) {
1964 2594 $styles = $extension->styles();
@@ -1963,12 +2593,11 @@
1963 2593 if ($extension->load($ext)) {
1964 2594 $styles = $extension->styles();
1965 2595 if ($styles && is_array($styles)) {
1966 2596 foreach ($styles as $key => $value) {
1967 - wp_enqueue_style('e2pdf-dynamic-style-' . $key, $value, false, false, false);
2597 + wp_enqueue_style('e2pdf-dynamic-style-' . $key, $value, false, $version, false);
1968 2598 }
1969 2599 }
1970 2600 }
1971 2601 }
1972 2602 }
1973 -
1974 2603 }