PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.49
E2Pdf – Export Pdf Tool for WordPress v1.32.49
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
← All changes | classes/controller/e2pdf.php +786 -188 1.02.021.32.49 View file →
@@ -1,11 +1,10 @@
1 1 <?php
2 2
3 3 /**
4 - * E2pdf Controller
5 - *
4 + * E2Pdf 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,76 +17,411 @@
18 17 /**
19 18 * @url admin.php?page=e2pdf
20 19 */
21 20 public function index_action() {
22 -
21 +
22 + if ($this->get->get('template_id')) {
23 + $url = array(
24 + 'page' => 'e2pdf',
25 + 'id' => $this->get->get('template_id'),
26 + );
27 + if ($this->get->get('dataset_id')) {
28 + $url['dataset'] = $this->get->get('dataset_id');
29 + }
30 + if ($this->get->get('dataset_id2')) {
31 + $url['dataset2'] = $this->get->get('dataset_id2');
32 + }
33 + $this->redirect($this->helper->get_url($url));
34 + }
35 +
36 + $users_tmp = get_users(
37 + array(
38 + 'fields' => array(
39 + 'ID', 'user_login',
40 + ),
41 + )
42 + );
43 +
44 + $users = array(
45 + '0' => __('--- Select ---', 'e2pdf'),
46 + );
47 + foreach ($users_tmp as $user) {
48 + $users[$user->ID] = $user->user_login;
49 + }
50 +
51 + $this->view('users', $users);
23 52 }
24 53
25 54 /**
55 + * @url admin.php?page=e2pdf&action=bulk
56 + */
57 + public function bulk_action() {
58 +
59 + if ($this->get->get('uid')) {
60 + $model_e2pdf_bulk = new Model_E2pdf_Bulk();
61 + if ($model_e2pdf_bulk->load_by_uid($this->get->get('uid')) && file_exists($this->helper->get('bulk_dir') . $model_e2pdf_bulk->get('uid') . '.zip') && $model_e2pdf_bulk->load($model_e2pdf_bulk->get('ID'))) {
62 + $this->helper->set_time_limit(0);
63 + $this->download_response('zip', $this->helper->get('bulk_dir') . $model_e2pdf_bulk->get('uid') . '.zip', $model_e2pdf_bulk->get('ID') . ' - ' . $model_e2pdf_bulk->get('template')->get('title'), '', true);
64 + exit;
65 + }
66 + }
67 +
68 + if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
69 + if (defined('ALTERNATE_WP_CRON') && ALTERNATE_WP_CRON) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
70 + } else {
71 + $this->add_notification('error', __('WP Cron is disabled. You must stay on this page to complete the Bulk PDFs creation', 'e2pdf'));
72 + }
73 + }
74 +
75 + if ($this->helper->get('license')->get('type') == 'FREE' && !get_option('e2pdf_hide_warnings', '0')) {
76 + $this->add_notification('notice', sprintf(__("Bulk PDF generation is not available with the Free License Type. Please visit <a target='_blank' href='%s'>%s</a> for upgrade options", 'e2pdf'), 'https://e2pdf.com/price', 'https://e2pdf.com'));
77 + }
78 +
79 + $users_tmp = get_users(
80 + [
81 + 'fields' => [
82 + 'ID', 'user_login',
83 + ],
84 + ]
85 + );
86 +
87 + $users = [
88 + '0' => __('--- Select ---', 'e2pdf'),
89 + ];
90 + foreach ($users_tmp as $user) {
91 + $users[$user->ID] = $user->user_login;
92 + }
93 +
94 + $this->view('users', $users);
95 + }
96 +
97 + /**
26 98 * @url admin.php?page=e2pdf&action=export
27 99 */
28 100 public function export_action() {
29 101
30 - $template_id = (int) $this->get->get('template_id');
31 - $dataset_id = $this->get->get('dataset_id');
102 + $wpml = $this->helper->load('translator')->isWPML();
103 + if ($wpml) {
104 + $lang = apply_filters('wpml_current_language', null);
105 + do_action('wpml_switch_language', $lang);
106 + }
32 107
33 - if ($template_id && $dataset_id) {
108 + if ($this->get->get('template_id')) {
109 + $url = array(
110 + 'page' => 'e2pdf',
111 + 'action' => 'export',
112 + 'id' => $this->get->get('template_id'),
113 + );
114 + if ($this->get->get('dataset_id')) {
115 + $url['dataset'] = $this->get->get('dataset_id');
116 + }
117 + if ($this->get->get('dataset_id2')) {
118 + $url['dataset2'] = $this->get->get('dataset_id2');
119 + }
120 + $this->redirect($this->helper->get_url($url));
121 + }
34 122
123 + $template_id = (int) $this->get->get('id');
124 + $dataset = $this->get->get('dataset') ? $this->get->get('dataset') : false;
125 + $dataset2 = $this->get->get('dataset2') ? $this->get->get('dataset2') : false;
126 + $wc_order_id = $this->get->get('wc_order_id') ? $this->get->get('wc_order_id') : false;
127 + $wc_product_item_id = $this->get->get('wc_product_item_id') ? $this->get->get('wc_product_item_id') : false;
128 + $disposition = 'inline';
129 + if ($this->post->get('disposition') === 'attachment') {
130 + $disposition = 'attachment';
131 + }
132 +
133 + $atts = array(
134 + 'user_id' => 0,
135 + 'inline' => $disposition == 'inline' ? '1' : '0',
136 + );
137 +
138 + $args = array();
139 + if ($this->post->get('options')) {
140 + foreach ($this->post->get('options') as $key => $value) {
141 + if ($key == 'args') {
142 + if (is_array($value)) {
143 + foreach ($value as $sub_key => $sub_value) {
144 + if ($sub_value !== '') {
145 + $args['arg' . ($sub_key + 1)] = stripslashes($sub_value);
146 + }
147 + }
148 + }
149 + } else {
150 + $atts[$key] = stripslashes($value);
151 + }
152 + }
153 + }
154 +
155 + if ($template_id && ($dataset || $dataset2)) {
156 +
35 157 $template = new Model_E2pdf_Template();
36 - $template->load($template_id);
158 + if ($template->load($template_id)) {
37 159
38 - if ($this->post->get()) {
39 - foreach ($this->post->get('options') as $key => $value) {
40 - $template->set($key, $value);
160 + $entry = new Model_E2pdf_Entry();
161 + $entry->set_data('template_id', $template_id);
162 + $template->extension()->set('template_id', $template_id);
163 +
164 + if ($dataset) {
165 + $entry->set_data('dataset', $dataset);
166 + $template->extension()->set('dataset', $dataset);
41 167 }
42 - }
43 168
44 - /*
45 - * @since 0.01.34
46 - */
47 - $uid = false;
48 - $uid_params = array();
49 - $uid_params['template_id'] = $template_id;
50 - $uid_params['dataset'] = $dataset_id;
169 + if ($dataset2) {
170 + $entry->set_data('dataset2', $dataset2);
171 + $template->extension()->set('dataset2', $dataset2);
172 + }
51 173
52 - $entry = new Model_E2pdf_Entry();
53 - $entry->set('entry', $uid_params);
174 + if ($wc_order_id) {
175 + $entry->set_data('wc_order_id', $wc_order_id);
176 + $template->extension()->set('wc_order_id', $wc_order_id);
177 + }
54 178
55 - if ($entry->load_by_uid($entry->get('uid'))) {
56 - $uid = $entry->get('uid');
57 - }
179 + if ($wc_product_item_id) {
180 + $entry->set_data('wc_product_item_id', $wc_product_item_id);
181 + $template->extension()->set('wc_product_item_id', $wc_product_item_id);
182 + }
58 183
59 - $extension = new Model_E2pdf_Extension();
60 - $extension->load($template->get('extension'));
61 - $extension->set('item', $template->get('item'));
62 - $extension->set('dataset', $dataset_id);
184 + if ($template->get('extension') == 'wordpress' && $template->get('item') == '-3') { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
185 + } else {
186 + if (array_key_exists('user_id', $atts)) {
187 + $user_id = (int) $atts['user_id'];
188 + $entry->set_data('user_id', $user_id);
189 + $template->extension()->set('user_id', $user_id);
190 + } else {
191 + $user_id = get_current_user_id();
192 + $entry->set_data('user_id', $user_id);
193 + $template->extension()->set('user_id', $user_id);
194 + }
195 + }
63 196
64 - $template->set('name', $extension->render($template->get('name'), 'value'));
65 - $template->set('password', $extension->render($template->get('password'), 'value'));
197 + if (!empty($args)) {
198 + $entry->set_data('args', $args);
199 + $template->extension()->set('args', $args);
200 + }
66 201
67 - $template->fill($dataset_id, $uid);
68 - $request = $template->render();
202 + if ($template->extension()->get_storing_engine() !== false) {
203 + $entry->set_data('storing_engine', $template->extension()->get_storing_engine());
204 + $template->extension()->set('storing_engine', $template->extension()->get_storing_engine());
205 + }
69 206
70 - if (isset($request['error'])) {
71 - $this->add_notification('error', $request['error']);
207 + if ($template->extension()->verify()) {
208 + add_filter('acf/shortcode/prevent_access_to_fields_on_non_public_posts', [$this->helper, '__return_false'], 999);
209 + if (array_key_exists('inline', $atts)) {
210 + $inline = $atts['inline'];
211 + if ($template->get('inline') !== $inline) {
212 + $entry->set_data('inline', $inline);
213 + }
214 + }
215 +
216 + if (array_key_exists('flatten', $atts)) {
217 + $flatten = $atts['flatten'];
218 + if ($template->get('flatten') !== $flatten) {
219 + $entry->set_data('flatten', $flatten);
220 + $template->set('flatten', $flatten);
221 + }
222 + }
223 +
224 + if (array_key_exists('format', $atts)) {
225 + $format = $atts['format'];
226 + if ($template->get('format') !== $format) {
227 + if ($template->set('format', $format)) {
228 + $entry->set_data('format', $format);
229 + }
230 + }
231 + }
232 +
233 + if (array_key_exists('name', $atts) && $template->get('name') !== $atts['name']) {
234 + if (!array_key_exists('filter', $atts)) {
235 + $name = $template->extension()->render($atts['name']);
236 + } else {
237 + $name = $template->extension()->convert_shortcodes($atts['name'], true);
238 + }
239 + $entry->set_data('name', $name);
240 + $template->set('name', $name);
241 + } else {
242 + $template->set('name', $template->extension()->render($template->get('name')));
243 + }
244 +
245 + if (array_key_exists('savename', $atts) && $template->get('savename') !== $atts['savename']) {
246 + if (!array_key_exists('filter', $atts)) {
247 + $savename = $template->extension()->render($atts['savename']);
248 + } else {
249 + $savename = $template->extension()->convert_shortcodes($atts['savename'], true);
250 + }
251 + $entry->set_data('savename', $name);
252 + $template->set('savename', $name);
253 + } else {
254 + $template->set('savename', $template->extension()->render($template->get('savename')));
255 + }
256 +
257 + if (array_key_exists('password', $atts) && $template->get('password') !== $atts['password']) {
258 + if (!array_key_exists('filter', $atts)) {
259 + $password = $template->extension()->render($atts['password']);
260 + } else {
261 + $password = $template->extension()->convert_shortcodes($atts['password'], true);
262 + }
263 + $entry->set_data('password', $password);
264 + $template->set('password', $password);
265 + } else {
266 + $template->set('password', $template->extension()->render($template->get('password')));
267 + }
268 +
269 + if (array_key_exists('dpdf', $atts)) {
270 + if (!array_key_exists('filter', $atts)) {
271 + $dpdf = $template->extension()->render($atts['dpdf']);
272 + } else {
273 + $dpdf = $template->extension()->convert_shortcodes($atts['dpdf'], true);
274 + }
275 + $entry->set_data('dpdf', $dpdf);
276 + $template->set('dpdf', $dpdf);
277 + } else {
278 + $template->set('dpdf', $template->extension()->render($template->get('dpdf')));
279 + }
280 +
281 + if (array_key_exists('meta_title', $atts)) {
282 + if (!array_key_exists('filter', $atts)) {
283 + $meta_title = $template->extension()->render($atts['meta_title']);
284 + } else {
285 + $meta_title = $template->extension()->convert_shortcodes($atts['meta_title'], true);
286 + }
287 + $entry->set_data('meta_title', $meta_title);
288 + $template->set('meta_title', $meta_title);
289 + } else {
290 + $template->set('meta_title', $template->extension()->render($template->get('meta_title')));
291 + }
292 +
293 + if (array_key_exists('meta_subject', $atts)) {
294 + if (!array_key_exists('filter', $atts)) {
295 + $meta_subject = $template->extension()->render($atts['meta_subject']);
296 + } else {
297 + $meta_subject = $template->extension()->convert_shortcodes($atts['meta_subject'], true);
298 + }
299 + $entry->set_data('meta_subject', $meta_subject);
300 + $template->set('meta_subject', $meta_subject);
301 + } else {
302 + $template->set('meta_subject', $template->extension()->render($template->get('meta_subject')));
303 + }
304 +
305 + if (array_key_exists('meta_author', $atts)) {
306 + if (!array_key_exists('filter', $atts)) {
307 + $meta_author = $template->extension()->render($atts['meta_author']);
308 + } else {
309 + $meta_author = $template->extension()->convert_shortcodes($atts['meta_author'], true);
310 + }
311 + $entry->set_data('meta_author', $meta_author);
312 + $template->set('meta_author', $meta_author);
313 + } else {
314 + $template->set('meta_author', $template->extension()->render($template->get('meta_author')));
315 + }
316 +
317 + if (array_key_exists('meta_keywords', $atts)) {
318 + if (!array_key_exists('filter', $atts)) {
319 + $meta_keywords = $template->extension()->render($atts['meta_keywords']);
320 + } else {
321 + $meta_keywords = $template->extension()->convert_shortcodes($atts['meta_keywords'], true);
322 + }
323 + $entry->set_data('meta_keywords', $meta_keywords);
324 + $template->set('meta_keywords', $meta_keywords);
325 + } else {
326 + $template->set('meta_keywords', $template->extension()->render($template->get('meta_keywords')));
327 + }
328 +
329 + $template->extension()->set('entry', $entry);
330 + $template->fill();
331 + $request = $template->render();
332 +
333 + if (isset($request['error'])) {
334 + $this->add_notification('error', $request['error']);
335 + $this->render('blocks', 'notifications');
336 + $this->index_action();
337 + } else {
338 + do_action('e2pdf_controller_e2pdf_export_completed', $request['file'], $template);
339 + $this->download_response($template->get('format'), $request['file'], $template->get_name(), $disposition, false, true);
340 + exit;
341 + }
342 + remove_filter('acf/shortcode/prevent_access_to_fields_on_non_public_posts', [$this->helper, '__return_false'], 999);
343 + } else {
344 + $this->add_notification('error', __('Incorrect Dataset', 'e2pdf'));
345 + $this->render('blocks', 'notifications');
346 + }
347 + } else {
348 + $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
72 349 $this->render('blocks', 'notifications');
73 - } else {
74 - $filename = $template->get_filename();
75 - $file = $request['file'];
76 - $this->download_response('pdf', $file, $filename);
77 350 }
78 351 } else {
79 - $this->error('404');
352 + $this->handle_404();
80 353 }
81 354 }
82 355
356 + public function get_bulks_list($filters = array(), $count = false) {
357 + global $wpdb;
358 +
359 + $model_e2pdf_bulk = new Model_E2pdf_Bulk();
360 +
361 + $condition = array();
362 +
363 + if (isset($filters['s']) && $filters['s']) {
364 + $condition['title'] = array(
365 + 'condition' => 'LIKE',
366 + 'value' => '%%' . $filters['s'] . '%%',
367 + 'type' => '%s',
368 + );
369 + }
370 +
371 + $order_condition = array();
372 + $sortable = array('id', 'title', 'updated_at');
373 + if (isset($filters['orderby']) && in_array($filters['orderby'], $sortable) && isset($filters['order'])) {
374 + $order_condition = array(
375 + 'orderby' => $filters['orderby'],
376 + 'order' => $filters['order'] == 'asc' ? 'asc' : 'desc',
377 + );
378 + } else {
379 + $order_condition = array(
380 + 'orderby' => 'id',
381 + 'order' => 'desc',
382 + );
383 + }
384 +
385 + $limit_condition = array();
386 + if (!$count) {
387 + $paged = isset($filters['paged']) && $filters['paged'] ? $filters['paged'] : '0';
388 + $paged = (int) $paged <= 0 ? 1 : (int) $paged;
389 + $per_page = get_option('e2pdf_bulks_screen_per_page', '20');
390 +
391 + $limit_condition = array(
392 + 'limit' => (int) $per_page,
393 + 'offset' => (int) ($paged - 1) * $per_page,
394 + );
395 + }
396 +
397 + $where = $this->helper->load('db')->prepare_where($condition);
398 + $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
399 + $limit = $this->helper->load('db')->prepare_limit($limit_condition);
400 +
401 + $blks = $wpdb->get_results($wpdb->prepare('SELECT `ID` FROM `' . $model_e2pdf_bulk->get_table() . '`' . $where['sql'] . $orderby . $limit . '', $where['filter']));
402 +
403 + if ($count) {
404 + return count($blks);
405 + }
406 +
407 + $bulks = array();
408 + foreach ($blks as $key => $blk) {
409 + $bulk = new Model_E2pdf_Bulk();
410 + $bulk->load($blk->ID);
411 + $bulks[] = $bulk;
412 + }
413 +
414 + return $bulks;
415 + }
416 +
83 417 /**
84 418 * Get activated templates list
85 - *
86 419 * @return array() - Activated templates list
87 420 */
88 421 public function get_active_templates() {
89 422 global $wpdb;
423 +
90 424 $model_e2pdf_template = new Model_E2pdf_Template();
91 425
92 426 $condition = array(
93 427 'trash' => array(
@@ -92,15 +426,15 @@
92 426 $condition = array(
93 427 'trash' => array(
94 428 'condition' => '<>',
95 429 'value' => '1',
96 - 'type' => '%d'
430 + 'type' => '%d',
97 431 ),
98 432 'activated' => array(
99 433 'condition' => '=',
100 434 'value' => '1',
101 - 'type' => '%d'
102 - )
435 + 'type' => '%d',
436 + ),
103 437 );
104 438
105 439 $order_condition = array(
106 440 'orderby' => 'id',
@@ -109,15 +443,14 @@
109 443
110 444 $where = $this->helper->load('db')->prepare_where($condition);
111 445 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
112 446
113 -
114 - $templates = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $model_e2pdf_template->get_table() . $where['sql'] . $orderby . "", $where['filter']));
447 + $templates = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $model_e2pdf_template->get_table() . '`' . $where['sql'] . $orderby . '', $where['filter']));
115 448 $export_templates = array();
116 449
117 450 $export_templates[] = array(
118 - 'key' => '',
119 - 'value' => __('--- Select ---', 'e2pdf')
451 + 'key' => '0',
452 + 'value' => __('--- Select ---', 'e2pdf'),
120 453 );
121 454
122 455 if (!empty($templates)) {
123 456 foreach ($templates as $key => $value) {
@@ -122,9 +455,9 @@
122 455 if (!empty($templates)) {
123 456 foreach ($templates as $key => $value) {
124 457 $export_templates[] = array(
125 458 'key' => $value->ID,
126 - 'value' => $value->title
459 + 'value' => $value->title,
127 460 );
128 461 }
129 462 }
130 463
@@ -132,34 +465,34 @@
132 465 }
133 466
134 467 /**
135 468 * Get entries for template
136 - *
137 469 * @return array() - Entries for template
138 470 */
139 - public function get_datasets($template_id = false) {
140 -
141 - $datasets = array();
142 -
143 - $datasets[] = array(
144 - 'key' => '',
145 - 'value' => __('--- Select ---', 'e2pdf')
471 + public function get_datasets($template_id = false, $item = false, $dataset_title = false) {
472 + $datasets = array(
473 + array(
474 + 'key' => '',
475 + 'value' => __('--- Select ---', 'e2pdf'),
476 + )
146 477 );
147 -
148 478 if ($template_id) {
149 479 $template = new Model_E2pdf_Template();
150 - $template->load($template_id);
151 -
152 - $extension = new Model_E2pdf_Extension();
153 - $extension->load($template->get('extension'));
154 -
155 - $datasets_tmp = $extension->
156 - get_datasets(
157 - $template->get('item'), $template->get('dataset_title')
158 - );
159 -
160 - if ($datasets_tmp && is_array($datasets_tmp)) {
161 - $datasets = array_merge($datasets, $datasets_tmp);
480 + if ($template->load($template_id)) {
481 + if ($item) {
482 + $datasets_tmp = $template->extension()->
483 + datasets(
484 + $item, $dataset_title ? $dataset_title : $template->get('dataset_title')
485 + );
486 + } else {
487 + $datasets_tmp = $template->extension()->
488 + datasets(
489 + $template->get('item'), $dataset_title ? $dataset_title : $template->get('dataset_title')
490 + );
491 + }
492 + if ($datasets_tmp && is_array($datasets_tmp)) {
493 + $datasets = array_merge($datasets, $datasets_tmp);
494 + }
162 495 }
163 496 }
164 497
165 498 return $datasets;
@@ -165,55 +498,116 @@
165 498 return $datasets;
166 499 }
167 500
168 501 /**
169 - * Get options to overwrite on template
170 - *
171 - * @return array() - List of options to overwrite
172 - */
173 - public function get_options($template_id = false) {
174 -
175 - $options = array();
176 -
177 - if ($template_id) {
178 - $template = new Model_E2pdf_Template();
179 - $template->load($template_id);
180 - $options['name'] = $template->get('name');
181 - $options['password'] = $template->get('password');
182 - $options['flatten'] = $template->get('flatten');
183 - }
184 -
185 - return $options;
186 - }
187 -
188 - /**
189 502 * Get templates list via ajax
190 503 * action: wp_ajax_e2pdf_templates
191 504 * function: e2pdf_templates
192 - *
193 505 * @return json - Templates list
194 506 */
195 507 public function ajax_templates() {
508 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
196 509
197 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
510 + $wpml = $this->helper->load('translator')->isWPML();
511 + if ($wpml) {
512 + $lang = apply_filters('wpml_current_language', null);
513 + do_action('wpml_switch_language', $lang);
514 + }
198 515
199 - $template_id = (int) $this->post->get('data');
200 - $content = array();
201 - $content['datasets'] = $this->get_datasets($template_id);
202 - $content['options'] = $this->get_options($template_id);
203 - $content['url'] = $this->helper->get_url(array('page' => 'e2pdf-templates', 'action' => 'edit', 'id' => $template_id));
516 + $content = array(
517 + 'id' => 0,
518 + 'datasets' => array(),
519 + 'options' => array(),
520 + 'actions' => array(),
521 + );
522 + $template_id = (int) $this->post->get('data');
523 + $template = new Model_E2pdf_Template();
524 + if ($template->load($template_id)) {
525 + $content['id'] = $template_id;
526 + if ($template->get('item') == '-2') {
527 + $content['datasets']['dataset'] = $this->get_datasets(
528 + $template_id,
529 + $template->get('item1'),
530 + $this->helper->load('translator')->pre_translate($template->get('dataset_title1'), $template_id, 'dataset_title1', 'template')
531 + );
532 + $content['datasets']['dataset2'] = $this->get_datasets(
533 + $template_id,
534 + $template->get('item2'),
535 + $this->helper->load('translator')->pre_translate($template->get('dataset_title2'), $template_id, 'dataset_title2', 'template')
536 + );
537 + } else {
538 + $content['datasets']['dataset'] = $this->get_datasets(
539 + $template_id,
540 + false,
541 + $this->helper->load('translator')->pre_translate($template->get('dataset_title'), $template_id, 'dataset_title', 'template')
542 + );
543 + }
544 + $actions = $template->extension()->get_template_actions($template_id);
545 + if (isset($actions->delete) && $actions->delete) {
546 + $content['actions'][] = sprintf('<a target="_blank" class="e2pdf-delete-items e2pdf-link" template="%s" href="javascript:void(0);" _wpnonce="%s">%s</a>', $template_id, wp_create_nonce('e2pdf'), __('Delete All Datasets', 'e2pdf'));
547 + }
548 + $content['actions'][] = sprintf(
549 + '<a target="_blank" class="e2pdf-link" href="%s">%s</a>', $this->helper->get_url(
550 + array(
551 + 'page' => 'e2pdf-templates',
552 + 'action' => 'edit',
553 + 'id' => $template_id,
554 + )
555 + ),
556 + __('Edit Template', 'e2pdf')
557 + );
558 + }
204 559
205 - $template = new Model_E2pdf_Template();
206 - $extension = new Model_E2pdf_Extension();
560 + $content['options'] = array(
561 + 'name' => $this->helper->load('translator')->pre_translate($template->get('name'), $template_id, 'name', 'template'),
562 + 'savename' => $this->helper->load('translator')->pre_translate($template->get('savename'), $template_id, 'savename', 'template'),
563 + 'password' => $template->get('password'),
564 + 'user_id' => get_current_user_id(),
565 + 'flatten' => $template->get('flatten'),
566 + 'format' => $template->get('format'),
567 + 'args' => '',
568 + );
569 + if ($template->get('extension') == 'wordpress' && $template->get('item') == '-3') {
570 + unset($content['options']['user_id']);
571 + }
572 + $response = array(
573 + 'content' => $content,
574 + );
575 + } else {
576 + $response['error'] = $this->message('wp_verify_nonce_error');
577 + }
578 + $this->json_response($response);
579 + }
207 580
208 - if ($template->load($template_id) && $extension->load($template->get('extension'))) {
209 - $content['delete_items'] = method_exists($extension->extension(), 'delete_items');
581 + public function ajax_datasets_refresh() {
582 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
583 + $data = $this->post->get('data');
584 + $template_id = (int) $data['id'];
585 + $name = $data['name'];
586 + $dataset = $data['dataset'];
587 + $content = array(
588 + 'dataset' => $dataset,
589 + 'datasets' => array(),
590 + );
591 + $template = new Model_E2pdf_Template();
592 + if ($template->load($template_id)) {
593 + $content['id'] = $template_id;
594 + if ($template->get('item') == '-2') {
595 + if ($name == 'dataset') {
596 + $content['datasets'] = $this->get_datasets($template_id, $template->get('item1'), $template->get('dataset_title1'));
597 + } else {
598 + $content['datasets'] = $this->get_datasets($template_id, $template->get('item2'), $template->get('dataset_title2'));
599 + }
600 + } else {
601 + $content['datasets'] = $this->get_datasets($template_id);
602 + }
603 + }
604 + $response = array(
605 + 'content' => $content,
606 + );
607 + } else {
608 + $response['error'] = $this->message('wp_verify_nonce_error');
210 609 }
211 -
212 - $response = array(
213 - 'content' => $content,
214 - );
215 -
216 610 $this->json_response($response);
217 611 }
218 612
219 613 /**
@@ -219,129 +613,333 @@
219 613 /**
220 614 * Get entries list via ajax
221 615 * action: wp_ajax_e2pdf_entry
222 616 * function: e2pdf_entry
223 - *
224 617 * @return json - Entries list
225 618 */
226 619 public function ajax_dataset() {
227 -
228 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
229 -
230 - $data = $this->post->get('data');
231 -
232 - $template_id = (int) $data['template'];
233 - $dataset_id = (int) $data['dataset'];
234 -
235 - $template = new Model_E2pdf_Template();
236 - $template->load($template_id);
237 -
238 - $extension = new Model_E2pdf_Extension();
239 - $extension->load($template->get('extension'));
240 -
241 - $dataset = $extension->get_dataset($dataset_id);
242 -
243 - $content = array();
244 - if ($dataset) {
245 - $content['export'] = true;
246 - $content['view'] = isset($dataset->url) && $dataset->url ? true : false;
247 - $content['delete_item'] = method_exists($extension->extension(), 'delete_item');
248 - $content['dataset'] = $dataset;
620 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
621 + $data = $this->post->get('data');
622 + $template_id = (int) $data['id'];
623 + $datasets = isset($data['datasets']) ? $data['datasets'] : array();
624 + $template = new Model_E2pdf_Template();
625 + $content = array(
626 + 'id' => $template_id,
627 + 'export' => false,
628 + 'datasets' => array(),
629 + );
630 + if ($template->load($template_id)) {
631 + foreach ($datasets as $key => $dataset_id) {
632 + $actions = $template->extension()->get_dataset_actions($dataset_id);
633 + if ($actions) {
634 + $content['datasets'][$key] = array(
635 + 'id' => $dataset_id,
636 + 'actions' => array(),
637 + );
638 + if (isset($actions->view) && $actions->view) {
639 + $content['datasets'][$key]['actions'][] = sprintf('<a target="_blank" class="e2pdf-link" href="%s">%s</a>', $actions->view, __('View Dataset', 'e2pdf'));
640 + }
641 + if (isset($actions->delete) && $actions->delete) {
642 + $content['datasets'][$key]['actions'][] = sprintf('<a target="_blank" class="e2pdf-link e2pdf-delete-item" href="javascript:void(0);" template="%s" dataset="%s" _wpnonce="%s">%s</a>', $template_id, $dataset_id, wp_create_nonce('e2pdf'), __('Delete Dataset', 'e2pdf'));
643 + }
644 + $content['export'] = true;
645 + } else {
646 + $content['datasets'][$key] = array(
647 + 'id' => '',
648 + 'actions' => array(),
649 + );
650 + }
651 + }
652 + }
653 + $response = array(
654 + 'content' => $content,
655 + );
249 656 } else {
250 - $content['export'] = false;
251 - $content['view'] = false;
252 - $content['delete_item'] = false;
253 - $content['dataset'] = false;
657 + $response['error'] = $this->message('wp_verify_nonce_error');
254 658 }
255 -
256 - $response = array(
257 - 'content' => $content,
258 - );
259 -
260 659 $this->json_response($response);
261 660 }
262 661
263 662 public function ajax_delete_item() {
264 663
265 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
664 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
266 665
267 - $data = $this->post->get('data');
666 + $data = $this->post->get('data');
268 667
269 - $template_id = (int) $data['template'];
270 - $dataset_id = (int) $data['dataset'];
668 + $template_id = (int) $data['template'];
669 + $dataset_id = (int) $data['dataset'];
271 670
272 - if (!$template_id || !$dataset_id) {
273 - return;
274 - }
671 + if (!$template_id || !$dataset_id) {
672 + return;
673 + }
275 674
276 - $template = new Model_E2pdf_Template();
277 - $extension = new Model_E2pdf_Extension();
675 + $template = new Model_E2pdf_Template();
278 676
279 - $action = false;
677 + $action = false;
678 + if ($template->load($template_id)) {
679 + $action = $template->extension()->delete_item($template_id, $dataset_id);
680 + }
280 681
281 - if ($template->load($template_id) && $extension->load($template->get('extension'))) {
282 - if (method_exists($extension->extension(), 'delete_item')) {
283 - $action = $extension->delete_item($template_id, $dataset_id);
682 + if ($action) {
683 + $response = array(
684 + 'content' => true,
685 + );
686 + } else {
687 + $response = array(
688 + 'error' => __("Dataset can't be removed!", 'e2pdf'),
689 + );
284 690 }
285 - }
286 -
287 - if ($action) {
288 - $response = array(
289 - 'redirect' => $this->helper->get_url(array(
290 - 'page' => 'e2pdf',
291 - 'template_id' => $template_id
292 - )
293 - )
294 - );
295 -
296 - $this->add_notification('update', __('Dataset removed successfully', 'e2pdf'));
297 691 } else {
298 - $response = array(
299 - 'error' => __("Dataset can't be removed!", 'e2pdf')
300 - );
692 + $response['error'] = $this->message('wp_verify_nonce_error');
301 693 }
302 -
303 694 $this->json_response($response);
304 695 }
305 696
306 697 public function ajax_delete_items() {
307 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
698 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
308 699
309 - $data = $this->post->get('data');
310 - $template_id = (int) $data['template'];
700 + $data = $this->post->get('data');
701 + $template_id = (int) $data['template'];
311 702
703 + if (!$template_id) {
704 + return;
705 + }
312 706
313 - if (!$template_id) {
314 - return;
315 - }
707 + $template = new Model_E2pdf_Template();
316 708
317 - $template = new Model_E2pdf_Template();
318 - $extension = new Model_E2pdf_Extension();
709 + $action = false;
710 + if ($template->load($template_id)) {
711 + $action = $template->extension()->delete_items($template_id);
712 + }
319 713
320 - $action = false;
714 + if ($action) {
715 + $response = array(
716 + 'content' => true,
717 + );
718 + } else {
719 + $response = array(
720 + 'error' => __("Datasets can't be removed!", 'e2pdf'),
721 + );
722 + }
723 + } else {
724 + $response['error'] = $this->message('wp_verify_nonce_error');
725 + }
726 + $this->json_response($response);
727 + }
321 728
322 - if ($template->load($template_id) && $extension->load($template->get('extension'))) {
323 - if (method_exists($extension->extension(), 'delete_items')) {
324 - $action = $extension->delete_items($template_id);
729 + public function ajax_bulk_create() {
730 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
731 + $this->helper->load('license')->load();
732 + if ($this->helper->get('license')->get('type') === false) {
733 + $response = array(
734 + 'error' => __('Something went wrong!', 'e2pdf'),
735 + );
736 + } elseif ($this->helper->get('license')->get('type') == 'FREE') {
737 + $response = array(
738 + 'error' => __('Bulk PDF generation is not available with the Free License Type. Please visit https://e2pdf.com/price for upgrade options', 'e2pdf')
739 + );
740 + } elseif (!class_exists('ZipArchive')) {
741 + $response = array(
742 + 'error' => sprintf(__('The PHP %s extension is required', 'e2pdf'), 'Zip'),
743 + );
744 + } else {
745 + $data = json_decode($this->post->get('data'), true);
746 + $template_id = isset($data['id']) ? (int) $data['id'] : 0;
747 + $template = new Model_E2pdf_Template();
748 + if ($template->load($template_id)) {
749 + $model_e2pdf_bulk = new Model_E2pdf_Bulk();
750 + $model_e2pdf_bulk->set('template_id', $template->get('ID'));
751 + if (isset($data['options'])) {
752 + $wpml = $this->helper->load('translator')->isWPML();
753 + if ($wpml) {
754 + $lang = apply_filters('wpml_current_language', null);
755 + if ($lang) {
756 + $data['options']['lang'] = $lang;
757 + }
758 + }
759 + $data['options']['args'] = isset($data['options[args]']) ? $data['options[args]'] : array();
760 + $model_e2pdf_bulk->set('options', $data['options']);
761 + }
762 + $datasets = isset($data['dataset']) ? $data['dataset'] : array();
763 + if (isset($datasets['0']) && $datasets['0'] === '') {
764 + unset($datasets['0']);
765 + }
766 + if (!empty($datasets)) {
767 + $datasets = array_reverse($datasets);
768 + $model_e2pdf_bulk->set('datasets', $datasets);
769 + $model_e2pdf_bulk->set('total', count($datasets));
770 + $model_e2pdf_bulk->save();
771 + $response = array(
772 + 'redirect' => $this->helper->get_url(
773 + array(
774 + 'page' => 'e2pdf',
775 + 'action' => 'bulk',
776 + 'notification' => $this->add_notification('update', __('The Bulk PDFs generation Task is created successfully', 'e2pdf')),
777 + )
778 + ),
779 + );
780 + }
781 + $this->cron_bulk_export();
782 + }
325 783 }
784 + } else {
785 + $response['error'] = $this->message('wp_verify_nonce_error');
326 786 }
787 + $this->json_response($response);
788 + }
327 789
328 - if ($action) {
790 + public function ajax_bulk_action() {
791 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
792 + $data = $this->post->get('data');
793 + $bulk_id = (int) $data['bulk'];
794 + $action = isset($data['action']) && in_array($data['action'], array('delete', 'start', 'stop')) ? $data['action'] : false;
795 + if (!$bulk_id || !$action) {
796 + return;
797 + }
798 + $bulk = new Model_E2pdf_Bulk();
799 + if ($bulk->load($bulk_id)) {
800 + if ($action == 'delete') {
801 + $bulk->delete();
802 + } elseif ($action == 'start') {
803 + if ($bulk->get('status') != 'pending') {
804 + $bulk->set('status', 'pending');
805 + $bulk->save();
806 + }
807 + $this->cron_bulk_export();
808 + } elseif ($action == 'stop') {
809 + if ($bulk->get('status') != 'stop') {
810 + $bulk->set('status', 'stop');
811 + $bulk->save();
812 + }
813 + }
814 + }
329 815 $response = array(
330 - 'redirect' => $this->helper->get_url(array(
331 - 'page' => 'e2pdf',
332 - 'template_id' => $template_id
333 - )
334 - )
816 + 'content' => array(
817 + 'action' => $action,
818 + ),
335 819 );
820 + } else {
821 + $response['error'] = $this->message('wp_verify_nonce_error');
822 + }
823 + $this->json_response($response);
824 + }
336 825
337 - $this->add_notification('update', __('Datasets removed successfully', 'e2pdf'));
338 - } else {
826 + public function ajax_bulk_progress() {
827 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
828 + $data = $this->post->get('data');
829 + $bulks = isset($data['bulks']) && is_array($data['bulks']) ? $data['bulks'] : array();
830 + $progress = array();
831 + foreach ($bulks as $bulk_id) {
832 + $bulk = new Model_E2pdf_Bulk();
833 + if ($bulk->load($bulk_id)) {
834 + $progress[] = array(
835 + 'ID' => $bulk->get('ID'),
836 + 'uid' => $bulk->get('uid'),
837 + 'count' => $bulk->get('count'),
838 + 'status' => $bulk->get('status'),
839 + );
840 + }
841 + }
339 842 $response = array(
340 - 'error' => __("Datasets can't be removed!", 'e2pdf')
843 + 'content' => array(
844 + 'bulks' => $progress,
845 + ),
341 846 );
847 + $this->cron_bulk_export();
848 + } else {
849 + $response['error'] = $this->message('wp_verify_nonce_error');
342 850 }
851 + $this->json_response($response);
852 + }
343 853
344 - $this->json_response($response);
854 + public function cron_bulk_export() {
855 + $model_e2pdf_bulk = new Model_E2pdf_Bulk();
856 + if ($model_e2pdf_bulk->load_by_active_bulk()) {
857 + if (!get_transient('e2pdf_bulk_export')) {
858 + set_transient('e2pdf_bulk_export', true, max(10, (int) get_option('e2pdf_bulk_export_interval', '10')));
859 + if ($model_e2pdf_bulk->get('ID') && $model_e2pdf_bulk->get_active_status() !== 'busy' && $model_e2pdf_bulk->get_active_status() !== 'stop') {
860 + $model_e2pdf_bulk->set('status', 'busy');
861 + $model_e2pdf_bulk->save();
862 + $template_id = $model_e2pdf_bulk->get('template_id');
863 + $datasets = $model_e2pdf_bulk->get('datasets');
864 + $dataset = array_shift($datasets);
865 + $pdf_dir = $this->helper->get('bulk_dir') . $model_e2pdf_bulk->get('uid') . '/';
866 + if (!file_exists($pdf_dir)) {
867 + $this->helper->create_dir($pdf_dir, false, true, true);
868 + }
869 + register_shutdown_function(array($this, 'cron_bulk_export_shutdown'), $model_e2pdf_bulk, $datasets, $pdf_dir);
870 + if ($template_id && $dataset) {
871 + $model_e2pdf_shortcode = new Model_E2pdf_Shortcode();
872 + $atts = array(
873 + 'id' => $template_id,
874 + 'dataset' => $dataset,
875 + 'user_id' => 0,
876 + 'create_index' => 'false',
877 + 'create_htaccess' => 'false',
878 + 'overwrite' => '2',
879 + 'dir' => $pdf_dir,
880 + 'apply' => 'true',
881 + );
882 + if ($model_e2pdf_bulk->get('options')) {
883 + foreach ($model_e2pdf_bulk->get('options') as $key => $value) {
884 + if ($key == 'args') {
885 + if (is_array($value)) {
886 + foreach ($value as $sub_key => $sub_value) {
887 + if ($sub_value) {
888 + $atts['arg' . ($sub_key + 1)] = $sub_value;
889 + }
890 + }
891 + }
892 + } elseif ($key == 'lang') {
893 + do_action('wpml_switch_language', $value);
894 + } else {
895 + $atts[$key] = $value;
896 + }
897 + }
898 + }
899 + add_filter('acf/shortcode/prevent_access_to_fields_on_non_public_posts', [$this->helper, '__return_false'], 999);
900 + $model_e2pdf_shortcode->e2pdf_save($atts);
901 + remove_filter('acf/shortcode/prevent_access_to_fields_on_non_public_posts', [$this->helper, '__return_false'], 999);
902 + }
903 + }
904 + }
905 + if (!wp_next_scheduled('e2pdf_bulk_export_cron')) {
906 + wp_schedule_event(time(), 'e2pdf_bulk_export_interval', 'e2pdf_bulk_export_cron');
907 + }
908 + } else {
909 + if (wp_next_scheduled('e2pdf_bulk_export_cron')) {
910 + wp_clear_scheduled_hook('e2pdf_bulk_export_cron');
911 + }
912 + }
345 913 }
346 914
915 + public function cron_bulk_export_shutdown($model_e2pdf_bulk, $datasets, $pdf_dir) {
916 + $model_e2pdf_bulk->set('datasets', $datasets);
917 + $model_e2pdf_bulk->set('count', $model_e2pdf_bulk->get('count') + 1);
918 + if (empty($datasets)) {
919 + $zip_path = $this->helper->get('bulk_dir') . $model_e2pdf_bulk->get('uid') . '.zip';
920 + $zip = new ZipArchive();
921 + if ($zip->open($zip_path, ZipArchive::CREATE) === true) {
922 + $this->helper->set_time_limit(0);
923 + do_action('e2pdf_controller_e2pdf_bulk_export_completed_dir', $pdf_dir, $model_e2pdf_bulk);
924 + $dir = opendir($pdf_dir);
925 + while ($file = readdir($dir)) {
926 + if (is_file($pdf_dir . $file) && $file !== 'index.php' && $file !== '.htaccess') {
927 + $zip->addFile($pdf_dir . $file, $file);
928 + }
929 + }
930 + closedir($dir);
931 + $zip->close();
932 + do_action('e2pdf_controller_e2pdf_bulk_export_completed_zip', $zip_path, $model_e2pdf_bulk);
933 + }
934 + $this->helper->delete_dir($pdf_dir);
935 + $model_e2pdf_bulk->set('status', 'completed');
936 + } else {
937 + if ($model_e2pdf_bulk->get_active_status() == 'stop') {
938 + $model_e2pdf_bulk->set('status', 'stop');
939 + } else {
940 + $model_e2pdf_bulk->set('status', 'pending');
941 + }
942 + }
943 + $model_e2pdf_bulk->save();
944 + }
347 945 }