class-give-import-core-settings.php
477 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Core Settings Import Class |
| 4 | * |
| 5 | * This class handles core setting import. |
| 6 | * |
| 7 | * @package Give |
| 8 | * @subpackage Classes/Give_Import_Core_Settings |
| 9 | * @copyright Copyright (c) 2017, WordImpress |
| 10 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 | * @since 1.8.17 |
| 12 | */ |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly |
| 16 | } |
| 17 | |
| 18 | if ( ! class_exists( 'Give_Import_Core_Settings' ) ) { |
| 19 | |
| 20 | /** |
| 21 | * Give_Import_Core_Settings. |
| 22 | * |
| 23 | * @since 1.8.17 |
| 24 | */ |
| 25 | final class Give_Import_Core_Settings { |
| 26 | |
| 27 | /** |
| 28 | * Importer type |
| 29 | * |
| 30 | * @since 1.8.17 |
| 31 | * @var string |
| 32 | */ |
| 33 | private $importer_type = 'import_core_setting'; |
| 34 | |
| 35 | /** |
| 36 | * Instance. |
| 37 | * |
| 38 | * @since 1.8.17 |
| 39 | */ |
| 40 | static private $instance; |
| 41 | |
| 42 | /** |
| 43 | * Importing donation per page. |
| 44 | * |
| 45 | * @since 1.8.17 |
| 46 | * |
| 47 | * @var int |
| 48 | */ |
| 49 | public static $per_page = 20; |
| 50 | |
| 51 | /** |
| 52 | * Singleton pattern. |
| 53 | * |
| 54 | * @since 1.8.17 |
| 55 | * |
| 56 | * @access private |
| 57 | */ |
| 58 | private function __construct() { |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get instance. |
| 63 | * |
| 64 | * @since 1.8.17 |
| 65 | * |
| 66 | * @access public |
| 67 | * |
| 68 | * @return static |
| 69 | */ |
| 70 | public static function get_instance() { |
| 71 | if ( null === static::$instance ) { |
| 72 | self::$instance = new static(); |
| 73 | } |
| 74 | |
| 75 | return self::$instance; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Setup |
| 80 | * |
| 81 | * @since 1.8.17 |
| 82 | * |
| 83 | * @return void |
| 84 | */ |
| 85 | public function setup() { |
| 86 | $this->setup_hooks(); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Setup Hooks. |
| 92 | * |
| 93 | * @since 1.8.17 |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | private function setup_hooks() { |
| 98 | if ( ! $this->is_donations_import_page() ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // Do not render main import tools page. |
| 103 | remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field', ) ); |
| 104 | |
| 105 | // Render donation import page |
| 106 | add_action( 'give_admin_field_tools_import', array( $this, 'render_page' ) ); |
| 107 | |
| 108 | // Print the HTML. |
| 109 | add_action( 'give_tools_import_core_settings_form_start', array( $this, 'html' ), 10 ); |
| 110 | |
| 111 | // Run when form submit. |
| 112 | add_action( 'give-tools_save_import', array( $this, 'save' ) ); |
| 113 | |
| 114 | add_action( 'give-tools_update_notices', array( $this, 'update_notices' ), 11, 1 ); |
| 115 | |
| 116 | // Used to add submit button. |
| 117 | add_action( 'give_tools_import_core_settings_form_end', array( $this, 'submit' ), 10 ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Update notice |
| 122 | * |
| 123 | * @since 1.8.17 |
| 124 | * |
| 125 | * @param $messages |
| 126 | * |
| 127 | * @return mixed |
| 128 | */ |
| 129 | public function update_notices( $messages ) { |
| 130 | if ( ! empty( $_GET['tab'] ) && 'import' === give_clean( $_GET['tab'] ) ) { |
| 131 | unset( $messages['give-setting-updated'] ); |
| 132 | } |
| 133 | |
| 134 | return $messages; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Print submit and nonce button. |
| 139 | * |
| 140 | * @since 1.8.17 |
| 141 | */ |
| 142 | public function submit() { |
| 143 | wp_nonce_field( 'give-save-settings', '_give-save-settings' ); |
| 144 | ?> |
| 145 | <input type="hidden" class="import-step" id="import-step" name="step" value="<?php echo $this->get_step(); ?>"/> |
| 146 | <input type="hidden" class="importer-type" value="<?php echo $this->importer_type; ?>"/> |
| 147 | <?php |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Print the HTML for core setting importer. |
| 152 | * |
| 153 | * @since 1.8.17 |
| 154 | */ |
| 155 | public function html() { |
| 156 | $step = $this->get_step(); |
| 157 | |
| 158 | // Show progress. |
| 159 | $this->render_progress(); |
| 160 | ?> |
| 161 | <section> |
| 162 | <table class="widefat export-options-table give-table <?php echo "step-{$step}"; ?>" |
| 163 | id="<?php echo "step-{$step}"; ?>"> |
| 164 | <tbody> |
| 165 | <?php |
| 166 | switch ( $this->get_step() ) { |
| 167 | case 1: |
| 168 | $this->render_upload_html(); |
| 169 | break; |
| 170 | |
| 171 | case 2: |
| 172 | $this->start_import(); |
| 173 | break; |
| 174 | |
| 175 | case 3: |
| 176 | $this->import_success(); |
| 177 | } |
| 178 | ?> |
| 179 | </tbody> |
| 180 | </table> |
| 181 | </section> |
| 182 | <?php |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Show message after the Core Settings Imported |
| 187 | * |
| 188 | * @since 1.8.17 |
| 189 | */ |
| 190 | public function import_success() { |
| 191 | // Imported successfully |
| 192 | |
| 193 | $success = (bool) ( isset( $_GET['success'] ) ? give_clean( $_GET['success'] ) : false ); |
| 194 | $undo = (bool) ( isset( $_GET['undo'] ) ? give_clean( $_GET['undo'] ) : false ); |
| 195 | $query_arg_setting = array( |
| 196 | 'post_type' => 'give_forms', |
| 197 | 'page' => 'give-settings', |
| 198 | ); |
| 199 | |
| 200 | if ( $undo ) { |
| 201 | $success = false; |
| 202 | } |
| 203 | |
| 204 | $query_arg_success = array( |
| 205 | 'post_type' => 'give_forms', |
| 206 | 'page' => 'give-tools', |
| 207 | 'tab' => 'import', |
| 208 | 'importer-type' => 'import_core_setting', |
| 209 | 'step' => '1', |
| 210 | 'undo' => 'true', |
| 211 | ); |
| 212 | |
| 213 | $title = __( 'Settings Importing Completed!', 'give' ); |
| 214 | if ( $success ) { |
| 215 | $query_arg_success['undo'] = '1'; |
| 216 | $query_arg_success['step'] = '3'; |
| 217 | $query_arg_success['success'] = '1'; |
| 218 | $text = __( 'Undo Importing', 'give' ); |
| 219 | } else { |
| 220 | if ( $undo ) { |
| 221 | $host_give_options = get_option( 'give_settings_old', array() ); |
| 222 | update_option( 'give_settings', $host_give_options ); |
| 223 | $title = __( 'Undo of Setting Imported Completed!', 'give' ); |
| 224 | } else { |
| 225 | $title = __( 'Failed to import', 'give' ); |
| 226 | } |
| 227 | |
| 228 | $text = __( 'Importing Again', 'give' ); |
| 229 | } |
| 230 | ?> |
| 231 | <tr valign="top" class="give-import-dropdown"> |
| 232 | <th colspan="2"> |
| 233 | <h2><?php echo $title; ?></h2> |
| 234 | <p> |
| 235 | <a class="button button-large button-secondary" href="<?php echo add_query_arg( $query_arg_success, admin_url( 'edit.php' ) ); ?>"><?php echo $text; ?></a> |
| 236 | <a class="button button-large button-secondary" href="<?php echo add_query_arg( $query_arg_setting, admin_url( 'edit.php' ) ); ?>"><?php echo __( 'View Settings', 'give' ); ?></a> |
| 237 | </p> |
| 238 | </th> |
| 239 | </tr> |
| 240 | <?php |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Will start Import |
| 245 | * |
| 246 | * @since 1.8.17 |
| 247 | */ |
| 248 | public function start_import() { |
| 249 | $type = ( ! empty( $_GET['type'] ) ? give_clean( $_GET['type'] ) : 'replace' ); |
| 250 | $file_name = ( ! empty( $_GET['file_name'] ) ? give_clean( $_GET['file_name'] ) : '' ); |
| 251 | |
| 252 | ?> |
| 253 | <tr valign="top" class="give-import-dropdown"> |
| 254 | <th colspan="2"> |
| 255 | <h2 id="give-import-title"><?php esc_html_e( 'Importing', 'give' ) ?></h2> |
| 256 | <p class="give-field-description"><?php esc_html_e( 'Your settings are now being imported...', 'give' ) ?></p> |
| 257 | </th> |
| 258 | </tr> |
| 259 | |
| 260 | <tr valign="top" class="give-import-dropdown"> |
| 261 | <th colspan="2"> |
| 262 | <div class="give-progress"> |
| 263 | <div style="width: 50%"></div> |
| 264 | </div> |
| 265 | <span class="spinner is-active"></span> |
| 266 | <input type="hidden" value="2" name="step"> |
| 267 | <input type="hidden" value="<?php echo $type; ?>" name="type"> |
| 268 | <input type="hidden" value="<?php echo $file_name; ?>" name="file_name"> |
| 269 | </th> |
| 270 | </tr> |
| 271 | |
| 272 | <script type="text/javascript"> |
| 273 | jQuery( document ).ready( function () { |
| 274 | give_on_core_settings_import_start(); |
| 275 | } ); |
| 276 | </script> |
| 277 | <?php |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Is used to show the process when user upload the donor form. |
| 282 | * |
| 283 | * @since 1.8.17 |
| 284 | */ |
| 285 | public function render_progress() { |
| 286 | $step = $this->get_step(); |
| 287 | ?> |
| 288 | <ol class="give-progress-steps"> |
| 289 | <li class="<?php echo( 1 === $step ? 'active' : '' ); ?>"> |
| 290 | <?php esc_html_e( 'Upload JSON file', 'give' ); ?> |
| 291 | </li> |
| 292 | <li class="<?php echo( 2 === $step ? 'active' : '' ); ?>"> |
| 293 | <?php esc_html_e( 'Import', 'give' ); ?> |
| 294 | </li> |
| 295 | <li class="<?php echo( 3 === $step ? 'active' : '' ); ?>"> |
| 296 | <?php esc_html_e( 'Done!', 'give' ); ?> |
| 297 | </li> |
| 298 | </ol> |
| 299 | <?php |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Will return the import step. |
| 304 | * |
| 305 | * @since 1.8.17 |
| 306 | * |
| 307 | * @return int $step on which step doest the import is on. |
| 308 | */ |
| 309 | public function get_step() { |
| 310 | $step = (int) ( isset( $_REQUEST['step'] ) ? give_clean( $_REQUEST['step'] ) : 0 ); |
| 311 | $on_step = 1; |
| 312 | |
| 313 | if ( empty( $step ) || 1 === $step ) { |
| 314 | $on_step = 1; |
| 315 | } elseif ( 2 === $step ) { |
| 316 | $on_step = 2; |
| 317 | } elseif ( 3 === $step ) { |
| 318 | $on_step = 3; |
| 319 | } |
| 320 | |
| 321 | return $on_step; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Render donations import page |
| 326 | * |
| 327 | * @since 1.8.17 |
| 328 | */ |
| 329 | public function render_page() { |
| 330 | include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-import-core-settings.php'; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Add json upload HTMl |
| 335 | * |
| 336 | * Print the html of the file upload from which json will be uploaded. |
| 337 | * |
| 338 | * @since 1.8.17 |
| 339 | * @return void |
| 340 | */ |
| 341 | public function render_upload_html() { |
| 342 | $json = ( isset( $_POST['json'] ) ? give_clean( $_POST['json'] ) : '' ); |
| 343 | $type = ( isset( $_POST['type'] ) ? give_clean( $_POST['type'] ) : 'merge' ); |
| 344 | $step = $this->get_step(); |
| 345 | |
| 346 | ?> |
| 347 | <tr valign="top"> |
| 348 | <th colspan="2"> |
| 349 | <h2 id="give-import-title"><?php esc_html_e( 'Import Core Settings from a JSON file', 'give' ) ?></h2> |
| 350 | <p class="give-field-description"><?php esc_html_e( 'This tool allows you to import Give settings from another Give installation. Settings imported contain data from Give core as well as any of our Premium Add-ons.', 'give' ) ?></p> |
| 351 | </th> |
| 352 | </tr> |
| 353 | |
| 354 | <tr valign="top"> |
| 355 | <th scope="row" class="titledesc"> |
| 356 | <label for="json"><?php esc_html_e( 'Choose a JSON file:', 'give' ) ?></label> |
| 357 | </th> |
| 358 | <td class="give-forminp"> |
| 359 | <div class="give-field-wrap"> |
| 360 | <label for="json"> |
| 361 | <input type="file" name="json" class="give-upload-json-file" value="<?php echo $json; ?>" |
| 362 | accept=".json"> |
| 363 | <p class="give-field-description"><?php esc_html_e( 'The file type must be JSON.', 'give' )?></p> |
| 364 | </label> |
| 365 | </div> |
| 366 | </td> |
| 367 | </tr> |
| 368 | <?php |
| 369 | $settings = array( |
| 370 | array( |
| 371 | 'id' => 'type', |
| 372 | 'name' => __( 'Merge Type:', 'give' ), |
| 373 | 'description' => __( 'Select "Merge" to retain existing settings, or "Replace" to overwrite with the settings from the JSON file', 'give' ), |
| 374 | 'default' => $type, |
| 375 | 'type' => 'radio_inline', |
| 376 | 'options' => array( |
| 377 | 'merge' => __( 'Merge', 'give' ), |
| 378 | 'replace' => __( 'Replace', 'give' ), |
| 379 | ), |
| 380 | ), |
| 381 | ); |
| 382 | |
| 383 | $settings = apply_filters( 'give_import_core_setting_html', $settings ); |
| 384 | |
| 385 | Give_Admin_Settings::output_fields( $settings, 'give_settings' ); |
| 386 | ?> |
| 387 | <tr valign="top"> |
| 388 | <th></th> |
| 389 | <th> |
| 390 | <input type="submit" |
| 391 | class="button button-primary button-large button-secondary <?php echo "step-{$step}"; ?>" |
| 392 | id="recount-stats-submit" |
| 393 | value="<?php esc_attr_e( 'Submit', 'give' ); ?>"/> |
| 394 | </th> |
| 395 | </tr> |
| 396 | <?php |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Run when user click on the submit button. |
| 401 | * |
| 402 | * @since 1.8.17 |
| 403 | */ |
| 404 | public function save() { |
| 405 | |
| 406 | // Get the current step. |
| 407 | $step = $this->get_step(); |
| 408 | |
| 409 | // Validation for first step. |
| 410 | if ( 1 === $step ) { |
| 411 | $type = ( ! empty( $_REQUEST['type'] ) ? give_clean( $_REQUEST['type'] ) : 'replace' ); |
| 412 | $core_settings = self::upload_widget_settings_file(); |
| 413 | if ( ! empty( $core_settings['error'] ) ) { |
| 414 | Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload a valid JSON settings file.', 'give' ) ); |
| 415 | } else { |
| 416 | $file_path = explode( '/', $core_settings['file'] ); |
| 417 | $count = ( count( $file_path ) - 1 ); |
| 418 | $url = give_import_page_url( (array) apply_filters( 'give_import_core_settings_importing_url', array( |
| 419 | 'step' => '2', |
| 420 | 'importer-type' => $this->importer_type, |
| 421 | 'type' => $type, |
| 422 | 'file_name' => $file_path[ $count ], |
| 423 | ) ) ); |
| 424 | |
| 425 | ?> |
| 426 | <script type="text/javascript"> |
| 427 | window.location = "<?php echo $url; ?>"; |
| 428 | </script> |
| 429 | <?php |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Get if current page import donations page or not |
| 436 | * |
| 437 | * @since 1.8.17 |
| 438 | * @return bool |
| 439 | */ |
| 440 | private function is_donations_import_page() { |
| 441 | return 'import' === give_get_current_setting_tab() && isset( $_GET['importer-type'] ) && $this->importer_type === give_clean( $_GET['importer-type'] ); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Upload JSON file |
| 446 | * @return boolean |
| 447 | */ |
| 448 | public static function upload_widget_settings_file() { |
| 449 | $upload = false; |
| 450 | if ( isset( $_FILES['json'] ) ) { |
| 451 | add_filter( 'upload_mimes', array( __CLASS__, 'json_upload_mimes' ) ); |
| 452 | |
| 453 | $upload = wp_handle_upload( $_FILES['json'], array( 'test_form' => false ) ); |
| 454 | |
| 455 | remove_filter( 'upload_mimes', array( __CLASS__, 'json_upload_mimes' ) ); |
| 456 | } else { |
| 457 | Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide a valid JSON file.', 'give' ) ); |
| 458 | } |
| 459 | |
| 460 | return $upload; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Add mime type for JSON |
| 465 | * |
| 466 | * @param array $existing_mimes |
| 467 | */ |
| 468 | public static function json_upload_mimes( $existing_mimes = array() ) { |
| 469 | $existing_mimes['json'] = 'application/json'; |
| 470 | |
| 471 | return $existing_mimes; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | Give_Import_Core_Settings::get_instance()->setup(); |
| 476 | } |
| 477 |