| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2Pdf Controller |
| 5 |
* @copyright Copyright 2017 https://e2pdf.com |
| 6 |
* @license GPLv3 |
| 7 |
* @version 1 |
| 8 |
* @link https://e2pdf.com |
| 9 |
* @since 0.00.01 |
| 10 |
*/ |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
die('Access denied.'); |
| 13 |
} |
| 14 |
|
| 15 |
class Controller_E2pdf extends Helper_E2pdf_View { |
| 16 |
|
| 17 |
/** |
| 18 |
* @url admin.php?page=e2pdf |
| 19 |
*/ |
| 20 |
public function index_action() { |
| 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); |
| 52 |
} |
| 53 |
|
| 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 |
/** |
| 98 |
* @url admin.php?page=e2pdf&action=export |
| 99 |
*/ |
| 100 |
public function export_action() { |
| 101 |
|
| 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 |
} |
| 107 |
|
| 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 |
} |
| 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 |
|
| 157 |
$template = new Model_E2pdf_Template(); |
| 158 |
if ($template->load($template_id)) { |
| 159 |
|
| 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); |
| 167 |
} |
| 168 |
|
| 169 |
if ($dataset2) { |
| 170 |
$entry->set_data('dataset2', $dataset2); |
| 171 |
$template->extension()->set('dataset2', $dataset2); |
| 172 |
} |
| 173 |
|
| 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 |
} |
| 178 |
|
| 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 |
} |
| 183 |
|
| 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')); |
| 345 |
$this->render('blocks', 'notifications'); |
| 346 |
} |
| 347 |
} else { |
| 348 |
$this->add_notification('error', __("Template can't be loaded", 'e2pdf')); |
| 349 |
$this->render('blocks', 'notifications'); |
| 350 |
} |
| 351 |
} else { |
| 352 |
$this->handle_404(); |
| 353 |
} |
| 354 |
} |
| 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 |
|
| 417 |
/** |
| 418 |
* Get activated templates list |
| 419 |
* @return array() - Activated templates list |
| 420 |
*/ |
| 421 |
public function get_active_templates() { |
| 422 |
global $wpdb; |
| 423 |
|
| 424 |
$model_e2pdf_template = new Model_E2pdf_Template(); |
| 425 |
|
| 426 |
$condition = array( |
| 427 |
'trash' => array( |
| 428 |
'condition' => '<>', |
| 429 |
'value' => '1', |
| 430 |
'type' => '%d', |
| 431 |
), |
| 432 |
'activated' => array( |
| 433 |
'condition' => '=', |
| 434 |
'value' => '1', |
| 435 |
'type' => '%d', |
| 436 |
), |
| 437 |
); |
| 438 |
|
| 439 |
$order_condition = array( |
| 440 |
'orderby' => 'id', |
| 441 |
'order' => 'desc', |
| 442 |
); |
| 443 |
|
| 444 |
$where = $this->helper->load('db')->prepare_where($condition); |
| 445 |
$orderby = $this->helper->load('db')->prepare_orderby($order_condition); |
| 446 |
|
| 447 |
$templates = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $model_e2pdf_template->get_table() . '`' . $where['sql'] . $orderby . '', $where['filter'])); |
| 448 |
$export_templates = array(); |
| 449 |
|
| 450 |
$export_templates[] = array( |
| 451 |
'key' => '0', |
| 452 |
'value' => __('--- Select ---', 'e2pdf'), |
| 453 |
); |
| 454 |
|
| 455 |
if (!empty($templates)) { |
| 456 |
foreach ($templates as $key => $value) { |
| 457 |
$export_templates[] = array( |
| 458 |
'key' => $value->ID, |
| 459 |
'value' => $value->title, |
| 460 |
); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
return $export_templates; |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* Get entries for template |
| 469 |
* @return array() - Entries for template |
| 470 |
*/ |
| 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 |
) |
| 477 |
); |
| 478 |
if ($template_id) { |
| 479 |
$template = new Model_E2pdf_Template(); |
| 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 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
return $datasets; |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Get templates list via ajax |
| 503 |
* action: wp_ajax_e2pdf_templates |
| 504 |
* function: e2pdf_templates |
| 505 |
* @return json - Templates list |
| 506 |
*/ |
| 507 |
public function ajax_templates() { |
| 508 |
if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) { |
| 509 |
|
| 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 |
} |
| 515 |
|
| 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 |
} |
| 559 |
|
| 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 |
} |
| 580 |
|
| 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 |
} |
| 610 |
$this->json_response($response); |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Get entries list via ajax |
| 615 |
* action: wp_ajax_e2pdf_entry |
| 616 |
* function: e2pdf_entry |
| 617 |
* @return json - Entries list |
| 618 |
*/ |
| 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 |
} |
| 661 |
|
| 662 |
public function ajax_delete_item() { |
| 663 |
|
| 664 |
if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) { |
| 665 |
|
| 666 |
$data = $this->post->get('data'); |
| 667 |
|
| 668 |
$template_id = (int) $data['template']; |
| 669 |
$dataset_id = (int) $data['dataset']; |
| 670 |
|
| 671 |
if (!$template_id || !$dataset_id) { |
| 672 |
return; |
| 673 |
} |
| 674 |
|
| 675 |
$template = new Model_E2pdf_Template(); |
| 676 |
|
| 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, |
| 685 |
); |
| 686 |
} else { |
| 687 |
$response = array( |
| 688 |
'error' => __("Dataset can't be removed!", 'e2pdf'), |
| 689 |
); |
| 690 |
} |
| 691 |
} else { |
| 692 |
$response['error'] = $this->message('wp_verify_nonce_error'); |
| 693 |
} |
| 694 |
$this->json_response($response); |
| 695 |
} |
| 696 |
|
| 697 |
public function ajax_delete_items() { |
| 698 |
if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf')) { |
| 699 |
|
| 700 |
$data = $this->post->get('data'); |
| 701 |
$template_id = (int) $data['template']; |
| 702 |
|
| 703 |
if (!$template_id) { |
| 704 |
return; |
| 705 |
} |
| 706 |
|
| 707 |
$template = new Model_E2pdf_Template(); |
| 708 |
|
| 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'); |
| 725 |
} |
| 726 |
$this->json_response($response); |
| 727 |
} |
| 728 |
|
| 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'); |
| 786 |
} |
| 787 |
$this->json_response($response); |
| 788 |
} |
| 789 |
|
| 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 |
} |
| 815 |
$response = array( |
| 816 |
'content' => array( |
| 817 |
'action' => $action, |
| 818 |
), |
| 819 |
); |
| 820 |
} else { |
| 821 |
$response['error'] = $this->message('wp_verify_nonce_error'); |
| 822 |
} |
| 823 |
$this->json_response($response); |
| 824 |
} |
| 825 |
|
| 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 |
} |
| 842 |
$response = array( |
| 843 |
'content' => array( |
| 844 |
'bulks' => $progress, |
| 845 |
), |
| 846 |
); |
| 847 |
$this->cron_bulk_export(); |
| 848 |
} else { |
| 849 |
$response['error'] = $this->message('wp_verify_nonce_error'); |
| 850 |
} |
| 851 |
$this->json_response($response); |
| 852 |
} |
| 853 |
|
| 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 |
} |
| 913 |
} |
| 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 |
} |
| 945 |
} |
| 946 |
|