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