| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /helper/e2pdf-license.php |
| 5 |
* |
| 6 |
* @package E2Pdf |
| 7 |
* @license GPLv3 |
| 8 |
* @link https://e2pdf.com |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
die('Access denied.'); |
| 12 |
} |
| 13 |
|
| 14 |
class Helper_E2pdf_License { |
| 15 |
|
| 16 |
private $helper; |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 20 |
} |
| 21 |
|
| 22 |
// load license |
| 23 |
public function load($reload = false) { |
| 24 |
$license = new Model_E2pdf_License( |
| 25 |
(new Model_E2pdf_Api()) |
| 26 |
->set( |
| 27 |
[ |
| 28 |
'action' => 'license/info', |
| 29 |
'reload' => $reload, |
| 30 |
] |
| 31 |
) |
| 32 |
->request() |
| 33 |
); |
| 34 |
$this->helper->set('license', $license); |
| 35 |
} |
| 36 |
|
| 37 |
// activate license |
| 38 |
public function activate() { |
| 39 |
$request = (new Model_E2pdf_Api()) |
| 40 |
->set( |
| 41 |
[ |
| 42 |
'action' => 'common/activate', |
| 43 |
] |
| 44 |
) |
| 45 |
->request(); |
| 46 |
if (!empty($request['license_key'])) { |
| 47 |
update_option('e2pdf_license', $request['license_key']); |
| 48 |
} |
| 49 |
if (!empty($request['e2pdf_api']) && get_option('e2pdf_api') === false) { |
| 50 |
if ($request['e2pdf_api'] === 'api3.e2pdf.com') { |
| 51 |
update_option('e2pdf_api', 'api3.e2pdf.com'); |
| 52 |
} else { |
| 53 |
update_option('e2pdf_api', 'api.e2pdf.com'); |
| 54 |
} |
| 55 |
} |
| 56 |
$this->load(); |
| 57 |
$this->load_templates(); |
| 58 |
} |
| 59 |
|
| 60 |
// reactivate license |
| 61 |
public function reactivate() { |
| 62 |
update_option('e2pdf_version', '1.00.00'); |
| 63 |
(new Model_E2pdf_Loader())->action_plugins_loaded(); |
| 64 |
$this->load(true); |
| 65 |
if ($this->helper->get('license')->get('error') === 'Site Url Not Found. Please try to "Reactivate" plugin.') { |
| 66 |
return false; |
| 67 |
} |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
// restore license |
| 72 |
public function restore() { |
| 73 |
$restore_key = wp_generate_password(32, false, false); |
| 74 |
set_transient('e2pdf_restore_key', $restore_key, HOUR_IN_SECONDS); |
| 75 |
$request = (new Model_E2pdf_Api()) |
| 76 |
->set( |
| 77 |
[ |
| 78 |
'action' => 'license/restore', |
| 79 |
'data' => [ |
| 80 |
'restore_key' => $restore_key, |
| 81 |
], |
| 82 |
] |
| 83 |
) |
| 84 |
->request(); |
| 85 |
if (!empty($request['license_key'])) { |
| 86 |
update_option('e2pdf_license', $request['license_key']); |
| 87 |
} |
| 88 |
delete_transient('e2pdf_restore_key'); |
| 89 |
$this->load(true); |
| 90 |
if ($this->helper->get('license')->get('error') === 'License Key does not match this site. Please correct License Key to continue usage.') { |
| 91 |
return false; |
| 92 |
} |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
// change license |
| 97 |
public function change($license_key = '') { |
| 98 |
$request = (new Model_E2pdf_Api()) |
| 99 |
->set( |
| 100 |
[ |
| 101 |
'action' => 'license/update', |
| 102 |
'data' => [ |
| 103 |
'license_key' => trim($license_key), |
| 104 |
], |
| 105 |
] |
| 106 |
) |
| 107 |
->request(); |
| 108 |
if (!empty($request['license_key'])) { |
| 109 |
update_option('e2pdf_license', $request['license_key']); |
| 110 |
} |
| 111 |
$this->load(); |
| 112 |
} |
| 113 |
|
| 114 |
// load templates |
| 115 |
public function load_templates() { |
| 116 |
global $wpdb; |
| 117 |
|
| 118 |
$condition = [ |
| 119 |
'activated' => [ |
| 120 |
'condition' => '=', |
| 121 |
'value' => '1', |
| 122 |
'type' => '%d', |
| 123 |
], |
| 124 |
'uid' => [ |
| 125 |
'condition' => '=', |
| 126 |
'value' => '', |
| 127 |
'type' => '%s', |
| 128 |
], |
| 129 |
]; |
| 130 |
$where = $this->helper->load('db')->prepare_where($condition); |
| 131 |
$model_e2pdf_template = new Model_E2pdf_Template(); |
| 132 |
|
| 133 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 134 |
$tpls = $wpdb->get_results($wpdb->prepare('SELECT `ID` FROM `' . $model_e2pdf_template->get_table() . '`' . $where['sql'] . '', $where['filter'])); |
| 135 |
foreach ($tpls as $key => $tpl) { |
| 136 |
$template = new Model_E2pdf_Template(); |
| 137 |
$template->load($tpl->ID, false); |
| 138 |
$template->activate(); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|