export.php
8 years ago
feedback.php
8 years ago
help.php
8 years ago
manage.php
8 years ago
settings.php
8 years ago
settings.php
214 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 | public function index() { |
| 10 | |
| 11 | $this->data['post'] = $post = $this->input->post(PMXE_Plugin::getInstance()->getOption()); |
| 12 | |
| 13 | if ($this->input->post('is_settings_submitted')) { // save settings form |
| 14 | |
| 15 | check_admin_referer('edit-settings', '_wpnonce_edit-settings'); |
| 16 | |
| 17 | if ( ! $this->errors->get_error_codes()) { // no validation errors detected |
| 18 | |
| 19 | PMXE_Plugin::getInstance()->updateOption($post); |
| 20 | |
| 21 | if (empty($_POST['pmxe_license_activate']) and empty($_POST['pmxe_license_deactivate'])) { |
| 22 | $post['license_status'] = $this->check_license(); |
| 23 | PMXE_Plugin::getInstance()->updateOption($post); |
| 24 | } |
| 25 | |
| 26 | isset( $_POST['pmxe_license_activate'] ) and $this->activate_licenses(); |
| 27 | |
| 28 | wp_redirect(add_query_arg('pmxe_nt', urlencode(__('Settings saved', 'wp_all_export_plugin')), $this->baseUrl)); die(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if ($this->input->post('is_templates_submitted')) { // delete templates form |
| 33 | |
| 34 | check_admin_referer('delete-templates', '_wpnonce_delete-templates'); |
| 35 | |
| 36 | if ($this->input->post('import_templates')){ |
| 37 | |
| 38 | if (!empty($_FILES)){ |
| 39 | $file_name = $_FILES['template_file']['name']; |
| 40 | $file_size = $_FILES['template_file']['size']; |
| 41 | $tmp_name = $_FILES['template_file']['tmp_name']; |
| 42 | |
| 43 | if(isset($file_name)) |
| 44 | { |
| 45 | |
| 46 | $filename = stripslashes($file_name); |
| 47 | $extension = strtolower(pmxe_getExtension($filename)); |
| 48 | |
| 49 | if (($extension != "txt")) |
| 50 | { |
| 51 | $this->errors->add('form-validation', __('Unknown File extension. Only txt files are permitted', 'wp_all_export_plugin')); |
| 52 | } |
| 53 | else { |
| 54 | $import_data = @file_get_contents($tmp_name); |
| 55 | if (!empty($import_data)){ |
| 56 | $templates_data = json_decode($import_data, true); |
| 57 | |
| 58 | if (!empty($templates_data)){ |
| 59 | $templateOptions = empty($templates_data[0]['options']) ? false : unserialize($templates_data[0]['options']); |
| 60 | if ( empty($templateOptions) ){ |
| 61 | $this->errors->add('form-validation', __('The template is invalid. Options are missing.', 'wp_all_export_plugin')); |
| 62 | } |
| 63 | else{ |
| 64 | if (!isset($templateOptions['is_user_export'])){ |
| 65 | $this->errors->add('form-validation', __('The template you\'ve uploaded is intended to be used with WP All Import plugin.', 'wp_all_export_plugin')); |
| 66 | } |
| 67 | else{ |
| 68 | $template = new PMXE_Template_Record(); |
| 69 | foreach ($templates_data as $template_data) { |
| 70 | unset($template_data['id']); |
| 71 | $template->clear()->set($template_data)->insert(); |
| 72 | } |
| 73 | wp_redirect(add_query_arg('pmxe_nt', urlencode(sprintf(_n('%d template imported', '%d templates imported', count($templates_data), 'wp_all_export_plugin'), count($templates_data))), $this->baseUrl)); die(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | else $this->errors->add('form-validation', __('Wrong imported data format', 'wp_all_export_plugin')); |
| 78 | } |
| 79 | else $this->errors->add('form-validation', __('File is empty or doesn\'t exests', 'wp_all_export_plugin')); |
| 80 | } |
| 81 | } |
| 82 | else $this->errors->add('form-validation', __('Undefined entry!', 'wp_all_export_plugin')); |
| 83 | } |
| 84 | else $this->errors->add('form-validation', __('Please select file.', 'wp_all_export_plugin')); |
| 85 | |
| 86 | } |
| 87 | else{ |
| 88 | $templates_ids = $this->input->post('templates', array()); |
| 89 | if (empty($templates_ids)) { |
| 90 | $this->errors->add('form-validation', __('Templates must be selected', 'wp_all_export_plugin')); |
| 91 | } |
| 92 | |
| 93 | if ( ! $this->errors->get_error_codes()) { // no validation errors detected |
| 94 | if ($this->input->post('delete_templates')){ |
| 95 | $template = new PMXE_Template_Record(); |
| 96 | foreach ($templates_ids as $template_id) { |
| 97 | $template->clear()->set('id', $template_id)->delete(); |
| 98 | } |
| 99 | wp_redirect(add_query_arg('pmxe_nt', urlencode(sprintf(_n('%d template deleted', '%d templates deleted', count($templates_ids), 'wp_all_export_plugin'), count($templates_ids))), $this->baseUrl)); die(); |
| 100 | } |
| 101 | if ($this->input->post('export_templates')){ |
| 102 | $export_data = array(); |
| 103 | $template = new PMXE_Template_Record(); |
| 104 | foreach ($templates_ids as $template_id) { |
| 105 | $export_data[] = $template->clear()->getBy('id', $template_id)->toArray(TRUE); |
| 106 | } |
| 107 | |
| 108 | $uploads = wp_upload_dir(); |
| 109 | $targetDir = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::TEMP_DIRECTORY; |
| 110 | $export_file_name = "templates_".uniqid().".txt"; |
| 111 | file_put_contents($targetDir . DIRECTORY_SEPARATOR . $export_file_name, json_encode($export_data)); |
| 112 | |
| 113 | PMXE_download::csv($targetDir . DIRECTORY_SEPARATOR . $export_file_name); |
| 114 | |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | $this->render(); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | public function dismiss(){ |
| 125 | |
| 126 | PMXE_Plugin::getInstance()->updateOption("dismiss", 1); |
| 127 | |
| 128 | exit('OK'); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * |
| 133 | * Activate licenses for main plugin and all premium addons |
| 134 | * |
| 135 | */ |
| 136 | protected function activate_licenses() { |
| 137 | |
| 138 | // listen for our activate button to be clicked |
| 139 | if( isset( $_POST['pmxe_license_activate'] ) ) { |
| 140 | |
| 141 | // retrieve the license from the database |
| 142 | $options = PMXE_Plugin::getInstance()->getOption(); |
| 143 | |
| 144 | $product_name = PMXE_Plugin::getEddName(); |
| 145 | |
| 146 | if ( $product_name !== false ){ |
| 147 | // data to send in our API request |
| 148 | $api_params = array( |
| 149 | 'edd_action'=> 'activate_license', |
| 150 | 'license' => $options['license'], |
| 151 | 'item_name' => urlencode( $product_name ) // the name of our product in EDD |
| 152 | ); |
| 153 | |
| 154 | // Call the custom API. |
| 155 | $response = wp_remote_get( add_query_arg( $api_params, $options['info_api_url'] ), array( 'timeout' => 15, 'sslverify' => false ) ); |
| 156 | |
| 157 | // make sure the response came back okay |
| 158 | if ( is_wp_error( $response ) ) |
| 159 | return false; |
| 160 | |
| 161 | // decode the license data |
| 162 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 163 | |
| 164 | // $license_data->license will be either "active" or "inactive" |
| 165 | |
| 166 | $options['license_status'] = $license_data->license; |
| 167 | |
| 168 | PMXE_Plugin::getInstance()->updateOption($options); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * |
| 176 | * Check plugin's license |
| 177 | * |
| 178 | */ |
| 179 | public static function check_license() { |
| 180 | |
| 181 | global $wp_version; |
| 182 | |
| 183 | $options = PMXE_Plugin::getInstance()->getOption(); |
| 184 | |
| 185 | if (!empty($options['license'])){ |
| 186 | |
| 187 | $product_name = PMXE_Plugin::getEddName(); |
| 188 | |
| 189 | if ( $product_name !== false ){ |
| 190 | |
| 191 | $api_params = array( |
| 192 | 'edd_action' => 'check_license', |
| 193 | 'license' => $options['license'], |
| 194 | 'item_name' => urlencode( $product_name ) |
| 195 | ); |
| 196 | |
| 197 | // Call the custom API. |
| 198 | $response = wp_remote_get( add_query_arg( $api_params, $options['info_api_url'] ), array( 'timeout' => 15, 'sslverify' => false ) ); |
| 199 | |
| 200 | if ( is_wp_error( $response ) ) |
| 201 | return false; |
| 202 | |
| 203 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 204 | |
| 205 | return $license_data->license; |
| 206 | |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return false; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | } |