export.php
3 weeks ago
feedback.php
10 years ago
help.php
12 years ago
manage.php
3 weeks ago
partners.php
3 weeks ago
settings.php
3 weeks ago
settings.php
186 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Statistics page |
| 4 | * |
| 5 | * @author Pavel Kulbakin <p.kulbakin@gmail.com> |
| 6 | */ |
| 7 | class PMXE_Admin_Settings extends PMXE_Controller_Admin { |
| 8 | |
| 9 | /** @var \Wpae\App\Service\License\LicenseActivator */ |
| 10 | private $licenseActivator; |
| 11 | |
| 12 | protected function init() |
| 13 | { |
| 14 | $this->licenseActivator = new \Wpae\App\Service\License\LicenseActivator(); |
| 15 | } |
| 16 | |
| 17 | public function index() { |
| 18 | |
| 19 | global $wp_roles; |
| 20 | |
| 21 | $this->data['roles'] = $wp_roles->roles; |
| 22 | |
| 23 | $this->data['post'] = $post = $this->input->post(PMXE_Plugin::getInstance()->getOption()); |
| 24 | |
| 25 | if ($this->input->post('is_settings_submitted')) { // save settings form |
| 26 | |
| 27 | check_admin_referer('edit-settings', '_wpnonce_edit-settings'); |
| 28 | |
| 29 | if ( ! $this->errors->get_error_codes()) { // no validation errors detected |
| 30 | |
| 31 | PMXE_Plugin::getInstance()->updateOption($post); |
| 32 | |
| 33 | wp_safe_redirect(esc_url_raw(add_query_arg('pmxe_nt', urlencode(__('Settings saved', 'wp-all-export')), $this->baseUrl))); |
| 34 | die(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if ($this->input->post('is_scheduling_license_submitted')) { |
| 39 | |
| 40 | check_admin_referer('edit-license', '_wpnonce_edit-scheduling-license'); |
| 41 | |
| 42 | if (!$this->errors->get_error_codes()) { // no validation errors detected |
| 43 | |
| 44 | PMXE_Plugin::getInstance()->updateOption($post); |
| 45 | if (empty($_POST['pmxe_scheduling_license_activate']) and empty($_POST['pmxe_scheduling_license_deactivate'])) { |
| 46 | $post['scheduling_license_status'] = $this->check_scheduling_license(); |
| 47 | if ($post['scheduling_license_status'] == 'valid') { |
| 48 | |
| 49 | $this->data['scheduling_license_message'] = __('License activated.', 'wp-all-export'); |
| 50 | } |
| 51 | |
| 52 | PMXE_Plugin::getInstance()->updateOption($post); |
| 53 | $this->activate_scheduling_licenses(); |
| 54 | |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | $this->data['post'] = $post = PMXE_Plugin::getInstance()->getOption(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | $post['scheduling_license_status'] = $this->check_scheduling_license(); |
| 63 | $this->data['is_license_active'] = false; |
| 64 | if (!empty($post['license_status']) && $post['license_status'] == 'valid') { |
| 65 | $this->data['is_license_active'] = true; |
| 66 | } |
| 67 | |
| 68 | $this->data['is_scheduling_license_active'] = false; |
| 69 | if (!empty($post['scheduling_license_status']) && $post['scheduling_license_status'] == 'valid') { |
| 70 | $this->data['is_scheduling_license_active'] = true; |
| 71 | } |
| 72 | |
| 73 | if ($this->input->post('is_templates_submitted')) { // delete templates form |
| 74 | |
| 75 | check_admin_referer('delete-templates', '_wpnonce_delete-templates'); |
| 76 | |
| 77 | if ($this->input->post('import_templates')){ |
| 78 | |
| 79 | if (!empty($_FILES)){ |
| 80 | $file_name = isset( $_FILES['template_file']['name'] ) ? sanitize_file_name( wp_unslash( $_FILES['template_file']['name'] ) ) : ''; |
| 81 | $file_size = isset( $_FILES['template_file']['size'] ) ? absint( $_FILES['template_file']['size'] ) : 0; |
| 82 | $tmp_name = isset( $_FILES['template_file']['tmp_name'] ) ? sanitize_text_field( wp_unslash( $_FILES['template_file']['tmp_name'] ) ) : ''; |
| 83 | |
| 84 | if(isset($file_name)) |
| 85 | { |
| 86 | $filename = stripslashes($file_name); |
| 87 | $extension = strtolower(pmxe_getExtension($filename)); |
| 88 | |
| 89 | if (($extension != "txt")) |
| 90 | { |
| 91 | $this->errors->add('form-validation', __('Unknown File extension. Only txt files are permitted', 'wp-all-export')); |
| 92 | } |
| 93 | else { |
| 94 | $import_data = @file_get_contents($tmp_name); |
| 95 | if (!empty($import_data)){ |
| 96 | $templates_data = json_decode($import_data, true); |
| 97 | |
| 98 | if (!empty($templates_data)){ |
| 99 | $templateOptions = empty($templates_data[0]['options']) ? false : unserialize($templates_data[0]['options']); |
| 100 | if ( empty($templateOptions) ){ |
| 101 | $this->errors->add('form-validation', __('The template is invalid. Options are missing.', 'wp-all-export')); |
| 102 | } |
| 103 | else{ |
| 104 | if (!isset($templateOptions['is_user_export'])){ |
| 105 | $this->errors->add('form-validation', __('The template you\'ve uploaded is intended to be used with WP All Import plugin.', 'wp-all-export')); |
| 106 | } |
| 107 | else{ |
| 108 | $template = new PMXE_Template_Record(); |
| 109 | foreach ($templates_data as $template_data) { |
| 110 | unset($template_data['id']); |
| 111 | $template->clear()->set($template_data)->insert(); |
| 112 | } |
| 113 | /* translators: %d: number of templates imported */ |
| 114 | wp_safe_redirect(esc_url_raw(add_query_arg('pmxe_nt', urlencode(sprintf(_n('%d template imported', '%d templates imported', count($templates_data), 'wp-all-export'), count($templates_data))), $this->baseUrl))); |
| 115 | die(); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | else $this->errors->add('form-validation', __('Wrong imported data format', 'wp-all-export')); |
| 120 | } |
| 121 | else $this->errors->add('form-validation', __('File is empty or doesn\'t exests', 'wp-all-export')); |
| 122 | } |
| 123 | } |
| 124 | else $this->errors->add('form-validation', __('Undefined entry!', 'wp-all-export')); |
| 125 | } |
| 126 | else $this->errors->add('form-validation', __('Please select file.', 'wp-all-export')); |
| 127 | |
| 128 | } |
| 129 | else{ |
| 130 | $templates_ids = $this->input->post('templates', array()); |
| 131 | if (empty($templates_ids)) { |
| 132 | $this->errors->add('form-validation', __('Templates must be selected', 'wp-all-export')); |
| 133 | } |
| 134 | |
| 135 | if ( ! $this->errors->get_error_codes()) { // no validation errors detected |
| 136 | if ($this->input->post('delete_templates')){ |
| 137 | $template = new PMXE_Template_Record(); |
| 138 | foreach ($templates_ids as $template_id) { |
| 139 | $template->clear()->set('id', $template_id)->delete(); |
| 140 | } |
| 141 | /* translators: %d: number of templates deleted */ |
| 142 | wp_safe_redirect(esc_url_raw(add_query_arg('pmxe_nt', urlencode(sprintf(_n('%d template deleted', '%d templates deleted', count($templates_ids), 'wp-all-export'), count($templates_ids))), $this->baseUrl))); |
| 143 | die(); |
| 144 | } |
| 145 | if ($this->input->post('export_templates')){ |
| 146 | $export_data = array(); |
| 147 | $template = new PMXE_Template_Record(); |
| 148 | foreach ($templates_ids as $template_id) { |
| 149 | $export_data[] = $template->clear()->getBy('id', $template_id)->toArray(TRUE); |
| 150 | } |
| 151 | |
| 152 | $uploads = wp_upload_dir(); |
| 153 | $targetDir = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::TEMP_DIRECTORY; |
| 154 | $export_file_name = "templates_".uniqid().".txt"; |
| 155 | file_put_contents($targetDir . DIRECTORY_SEPARATOR . $export_file_name, json_encode($export_data)); |
| 156 | |
| 157 | PMXE_download::csv($targetDir . DIRECTORY_SEPARATOR . $export_file_name); |
| 158 | |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | $this->render(); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | public function dismiss(){ |
| 169 | |
| 170 | PMXE_Plugin::getInstance()->updateOption("dismiss", 1); |
| 171 | |
| 172 | exit('OK'); |
| 173 | } |
| 174 | |
| 175 | protected function activate_scheduling_licenses() |
| 176 | { |
| 177 | return $this->licenseActivator->activateLicense(PMXE_Plugin::getSchedulingName(),\Wpae\App\Service\License\LicenseActivator::CONTEXT_SCHEDULING); |
| 178 | } |
| 179 | |
| 180 | public function check_scheduling_license() |
| 181 | { |
| 182 | $options = PMXE_Plugin::getInstance()->getOption(); |
| 183 | |
| 184 | return $this->licenseActivator->checkLicense(PMXE_Plugin::getSchedulingName(), $options, \Wpae\App\Service\License\LicenseActivator::CONTEXT_SCHEDULING); |
| 185 | } |
| 186 | } |