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 +778 -167 1.04.071.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,58 +17,333 @@
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
35 - $template = new Model_E2pdf_Template();
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 + }
36 132
37 - if ($template->load($template_id)) {
133 + $atts = array(
134 + 'user_id' => 0,
135 + 'inline' => $disposition == 'inline' ? '1' : '0',
136 + );
38 137
39 - if ($this->post->get()) {
40 - foreach ($this->post->get('options') as $key => $value) {
41 - $template->set($key, $value);
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 + }
42 148 }
149 + } else {
150 + $atts[$key] = stripslashes($value);
43 151 }
152 + }
153 + }
44 154
45 - $uid = false;
46 - $uid_params = array();
47 - $uid_params['template_id'] = $template_id;
48 - $uid_params['dataset'] = $dataset_id;
155 + if ($template_id && ($dataset || $dataset2)) {
49 156
157 + $template = new Model_E2pdf_Template();
158 + if ($template->load($template_id)) {
159 +
50 160 $entry = new Model_E2pdf_Entry();
51 - $entry->set('entry', $uid_params);
161 + $entry->set_data('template_id', $template_id);
162 + $template->extension()->set('template_id', $template_id);
52 163
53 - if ($entry->load_by_uid($entry->get('uid'))) {
54 - $uid = $entry->get('uid');
164 + if ($dataset) {
165 + $entry->set_data('dataset', $dataset);
166 + $template->extension()->set('dataset', $dataset);
55 167 }
56 168
57 - $template->extension()->set('dataset', $dataset_id);
169 + if ($dataset2) {
170 + $entry->set_data('dataset2', $dataset2);
171 + $template->extension()->set('dataset2', $dataset2);
172 + }
58 173
59 - $template->set('name', $template->extension()->render($template->get('name'), 'value'));
60 - $template->set('password', $template->extension()->render($template->get('password'), 'value'));
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 + }
61 178
62 - $template->fill($dataset_id, $uid);
63 - $request = $template->render();
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 + }
64 183
65 - if (isset($request['error'])) {
66 - $this->add_notification('error', $request['error']);
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 + }
196 +
197 + if (!empty($args)) {
198 + $entry->set_data('args', $args);
199 + $template->extension()->set('args', $args);
200 + }
201 +
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 + }
206 +
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'));
67 345 $this->render('blocks', 'notifications');
68 - } else {
69 - $filename = $template->get_filename();
70 - $file = $request['file'];
71 - $this->download_response('pdf', $file, $filename);
72 346 }
73 347 } else {
74 348 $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
75 349 $this->render('blocks', 'notifications');
@@ -74,19 +348,80 @@
74 348 $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
75 349 $this->render('blocks', 'notifications');
76 350 }
77 351 } else {
78 - $this->error('404');
352 + $this->handle_404();
79 353 }
80 354 }
81 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 +
82 417 /**
83 418 * Get activated templates list
84 - *
85 419 * @return array() - Activated templates list
86 420 */
87 421 public function get_active_templates() {
88 422 global $wpdb;
423 +
89 424 $model_e2pdf_template = new Model_E2pdf_Template();
90 425
91 426 $condition = array(
92 427 'trash' => array(
@@ -91,15 +426,15 @@
91 426 $condition = array(
92 427 'trash' => array(
93 428 'condition' => '<>',
94 429 'value' => '1',
95 - 'type' => '%d'
430 + 'type' => '%d',
96 431 ),
97 432 'activated' => array(
98 433 'condition' => '=',
99 434 'value' => '1',
100 - 'type' => '%d'
101 - )
435 + 'type' => '%d',
436 + ),
102 437 );
103 438
104 439 $order_condition = array(
105 440 'orderby' => 'id',
@@ -108,15 +443,14 @@
108 443
109 444 $where = $this->helper->load('db')->prepare_where($condition);
110 445 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
111 446
112 -
113 - $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']));
114 448 $export_templates = array();
115 449
116 450 $export_templates[] = array(
117 - 'key' => '',
118 - 'value' => __('--- Select ---', 'e2pdf')
451 + 'key' => '0',
452 + 'value' => __('--- Select ---', 'e2pdf'),
119 453 );
120 454
121 455 if (!empty($templates)) {
122 456 foreach ($templates as $key => $value) {
@@ -121,9 +455,9 @@
121 455 if (!empty($templates)) {
122 456 foreach ($templates as $key => $value) {
123 457 $export_templates[] = array(
124 458 'key' => $value->ID,
125 - 'value' => $value->title
459 + 'value' => $value->title,
126 460 );
127 461 }
128 462 }
129 463
@@ -131,29 +465,31 @@
131 465 }
132 466
133 467 /**
134 468 * Get entries for template
135 - *
136 469 * @return array() - Entries for template
137 470 */
138 - public function get_datasets($template_id = false) {
139 -
140 - $datasets = array();
141 -
142 - $datasets[] = array(
143 - 'key' => '',
144 - '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 + )
145 477 );
146 -
147 478 if ($template_id) {
148 479 $template = new Model_E2pdf_Template();
149 -
150 480 if ($template->load($template_id)) {
151 - $datasets_tmp = $template->extension()->
152 - datasets(
153 - $template->get('item'), $template->get('dataset_title')
154 - );
155 -
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 + }
156 492 if ($datasets_tmp && is_array($datasets_tmp)) {
157 493 $datasets = array_merge($datasets, $datasets_tmp);
158 494 }
159 495 }
@@ -162,53 +498,116 @@
162 498 return $datasets;
163 499 }
164 500
165 501 /**
166 - * Get options to overwrite on template
167 - *
168 - * @return array() - List of options to overwrite
169 - */
170 - public function get_options($template_id = false) {
171 -
172 - $options = array();
173 -
174 - if ($template_id) {
175 - $template = new Model_E2pdf_Template();
176 - $template->load($template_id);
177 - $options['name'] = $template->get('name');
178 - $options['password'] = $template->get('password');
179 - $options['flatten'] = $template->get('flatten');
180 - }
181 -
182 - return $options;
183 - }
184 -
185 - /**
186 502 * Get templates list via ajax
187 503 * action: wp_ajax_e2pdf_templates
188 504 * function: e2pdf_templates
189 - *
190 505 * @return json - Templates list
191 506 */
192 507 public function ajax_templates() {
508 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
193 509
194 - $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 + }
195 515
196 - $template_id = (int) $this->post->get('data');
197 - $content = array();
198 - $content['datasets'] = $this->get_datasets($template_id);
199 - $content['options'] = $this->get_options($template_id);
200 - $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 + }
201 559
202 - $template = new Model_E2pdf_Template();
203 - if ($template->load($template_id)) {
204 - $content['delete_items'] = $template->extension()->method('delete_items');
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');
205 577 }
578 + $this->json_response($response);
579 + }
206 580
207 - $response = array(
208 - 'content' => $content,
209 - );
210 -
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');
609 + }
211 610 $this->json_response($response);
212 611 }
213 612
214 613 /**
@@ -214,121 +613,333 @@
214 613 /**
215 614 * Get entries list via ajax
216 615 * action: wp_ajax_e2pdf_entry
217 616 * function: e2pdf_entry
218 - *
219 617 * @return json - Entries list
220 618 */
221 619 public function ajax_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 + );
656 + } else {
657 + $response['error'] = $this->message('wp_verify_nonce_error');
658 + }
659 + $this->json_response($response);
660 + }
222 661
223 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
662 + public function ajax_delete_item() {
224 663
225 - $data = $this->post->get('data');
664 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
226 665
227 - $template_id = (int) $data['template'];
228 - $dataset_id = (int) $data['dataset'];
666 + $data = $this->post->get('data');
229 667
230 - $template = new Model_E2pdf_Template();
668 + $template_id = (int) $data['template'];
669 + $dataset_id = (int) $data['dataset'];
231 670
232 - $content = array(
233 - 'export' => false,
234 - 'view' => false,
235 - 'delete_item' => false,
236 - 'dataset' => false
237 - );
671 + if (!$template_id || !$dataset_id) {
672 + return;
673 + }
238 674
239 - if ($template->load($template_id)) {
240 - $dataset = $template->extension()->dataset($dataset_id);
675 + $template = new Model_E2pdf_Template();
241 676
242 - if ($dataset) {
243 - $content = array(
244 - 'export' => true,
245 - 'view' => isset($dataset->url) && $dataset->url ? true : false,
246 - 'delete_item' => $template->extension()->method('delete_item'),
247 - 'dataset' => $dataset
677 + $action = false;
678 + if ($template->load($template_id)) {
679 + $action = $template->extension()->delete_item($template_id, $dataset_id);
680 + }
681 +
682 + if ($action) {
683 + $response = array(
684 + 'content' => true,
248 685 );
686 + } else {
687 + $response = array(
688 + 'error' => __("Dataset can't be removed!", 'e2pdf'),
689 + );
249 690 }
691 + } else {
692 + $response['error'] = $this->message('wp_verify_nonce_error');
250 693 }
251 -
252 - $response = array(
253 - 'content' => $content,
254 - );
255 -
256 694 $this->json_response($response);
257 695 }
258 696
259 - public function ajax_delete_item() {
697 + public function ajax_delete_items() {
698 + if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) {
260 699
261 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
700 + $data = $this->post->get('data');
701 + $template_id = (int) $data['template'];
262 702
263 - $data = $this->post->get('data');
703 + if (!$template_id) {
704 + return;
705 + }
264 706
265 - $template_id = (int) $data['template'];
266 - $dataset_id = (int) $data['dataset'];
707 + $template = new Model_E2pdf_Template();
267 708
268 - if (!$template_id || !$dataset_id) {
269 - return;
709 + $action = false;
710 + if ($template->load($template_id)) {
711 + $action = $template->extension()->delete_items($template_id);
712 + }
713 +
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');
270 725 }
726 + $this->json_response($response);
727 + }
271 728
272 - $template = new Model_E2pdf_Template();
273 -
274 - $action = false;
275 - if ($template->load($template_id)) {
276 - $action = $template->extension()->delete_item($template_id, $dataset_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 + }
783 + }
784 + } else {
785 + $response['error'] = $this->message('wp_verify_nonce_error');
277 786 }
787 + $this->json_response($response);
788 + }
278 789
279 - 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 + }
280 815 $response = array(
281 - 'redirect' => $this->helper->get_url(array(
282 - 'page' => 'e2pdf',
283 - 'template_id' => $template_id
284 - )
285 - )
816 + 'content' => array(
817 + 'action' => $action,
818 + ),
286 819 );
820 + } else {
821 + $response['error'] = $this->message('wp_verify_nonce_error');
822 + }
823 + $this->json_response($response);
824 + }
287 825
288 - $this->add_notification('update', __('Dataset removed successfully', 'e2pdf'));
289 - } 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 + }
290 842 $response = array(
291 - 'error' => __("Dataset can't be removed!", 'e2pdf')
843 + 'content' => array(
844 + 'bulks' => $progress,
845 + ),
292 846 );
847 + $this->cron_bulk_export();
848 + } else {
849 + $response['error'] = $this->message('wp_verify_nonce_error');
293 850 }
294 -
295 851 $this->json_response($response);
296 852 }
297 853
298 - public function ajax_delete_items() {
299 - $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
300 -
301 - $data = $this->post->get('data');
302 - $template_id = (int) $data['template'];
303 -
304 -
305 - if (!$template_id) {
306 - return;
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 + }
307 912 }
913 + }
308 914
309 - $template = new Model_E2pdf_Template();
310 -
311 - $action = false;
312 - if ($template->load($template_id)) {
313 - $action = $template->extension()->delete_items($template_id);
314 - }
315 -
316 - if ($action) {
317 - $response = array(
318 - 'redirect' => $this->helper->get_url(array(
319 - 'page' => 'e2pdf',
320 - 'template_id' => $template_id
321 - ))
322 - );
323 -
324 - $this->add_notification('update', __('Datasets removed successfully', 'e2pdf'));
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');
325 936 } else {
326 - $response = array(
327 - 'error' => __("Datasets can't be removed!", 'e2pdf')
328 - );
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 + }
329 942 }
330 -
331 - $this->json_response($response);
943 + $model_e2pdf_bulk->save();
332 944 }
333 -
334 945 }