| 1 |
<?php |
| 2 |
namespace BetterLinks\Tools; |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 4 |
|
| 5 |
class Import |
| 6 |
{ |
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
add_action('admin_init', [$this, 'import_data']); |
| 10 |
add_action('wp_ajax_betterlinks/tools/get_import_info', [$this, 'get_import_info']); |
| 11 |
} |
| 12 |
public function import_data() |
| 13 |
{ |
| 14 |
$can_access_settings = apply_filters("betterlinks/admin/" . BETTERLINKS_PLUGIN_SLUG . "-settings_menu_capability", 'manage_options'); |
| 15 |
$nonce = isset($_GET['nonce']) ? sanitize_text_field( wp_unslash( $_GET['nonce'] ) ) : ''; |
| 16 |
if( !wp_verify_nonce($nonce, 'betterlinks_admin_nonce') || !is_user_logged_in() || !current_user_can($can_access_settings)){ |
| 17 |
return false; |
| 18 |
} |
| 19 |
$page = isset($_GET['page']) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 20 |
$import = isset($_GET['import']) ? sanitize_text_field( wp_unslash( $_GET['import'] ) ) : false; |
| 21 |
if ($page === 'betterlinks-settings' && $import == true) { |
| 22 |
\BetterLinks\Helper::clear_query_cache(); |
| 23 |
if (!empty($_FILES['upload_file']['tmp_name'])) { |
| 24 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- $_FILES sanitization handled per-key below. |
| 25 |
$file_raw = $_FILES['upload_file']; |
| 26 |
$file = array( |
| 27 |
'name' => isset( $file_raw['name'] ) ? sanitize_file_name( $file_raw['name'] ) : '', |
| 28 |
'tmp_name' => isset( $file_raw['tmp_name'] ) ? $file_raw['tmp_name'] : '', |
| 29 |
); |
| 30 |
$mode = isset( $_POST['mode'] ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : ''; |
| 31 |
if ('csv' === pathinfo($file['name'])[ 'extension' ]) { |
| 32 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- reading PHP-managed temp upload; WP_Filesystem does not cover $_FILES tmp_name. |
| 33 |
$fileContent = fopen($file['tmp_name'], "r"); |
| 34 |
if (!empty($fileContent)) { |
| 35 |
$this->run_csv_importer($fileContent, $mode); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
do_action('betterlinks/admin/after_import_data'); |
| 40 |
} |
| 41 |
} |
| 42 |
public function run_csv_importer($fileContent, $type = 'default') |
| 43 |
{ |
| 44 |
// phpcs:disable WordPress.Security.NonceVerification.Missing -- caller import_data() verifies the nonce before invoking this private CSV runner. |
| 45 |
$results = ''; |
| 46 |
$mode = isset( $_POST['mode'] ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : ''; |
| 47 |
if ($type == 'default') { |
| 48 |
$BetterLinks = new Migration\BLImportCSV(); |
| 49 |
$results = $BetterLinks->start_importing($fileContent); |
| 50 |
} elseif ( $mode == 'prettylinks' ) { |
| 51 |
$PrettyLinks = new Migration\PTLImportCSV(); |
| 52 |
$results = $PrettyLinks->start_importing($fileContent); |
| 53 |
} elseif ($type == 'thirstyaffiliates') { |
| 54 |
$ta_link_prefix = isset($_POST["ta_prefix"]) ? sanitize_text_field(wp_unslash( $_POST["ta_prefix"] )) : ""; |
| 55 |
$ThirstyAffiliates = new Migration\TAImportCSV(); |
| 56 |
$results = $ThirstyAffiliates->start_importing($fileContent, $ta_link_prefix); |
| 57 |
} elseif ($type == 'simple301redirects') { |
| 58 |
$migrator = new Migration\S30RImportCSV(); |
| 59 |
$results = $migrator->start_importing($fileContent); |
| 60 |
} |
| 61 |
set_transient('betterlinks_import_info', json_encode($results), 60 * 60 * 5); |
| 62 |
// phpcs:enable WordPress.Security.NonceVerification.Missing |
| 63 |
} |
| 64 |
|
| 65 |
public function get_import_info() |
| 66 |
{ |
| 67 |
// This reads *and deletes* the admin's import-status transient, so it |
| 68 |
// needs the same capability as the importer that writes it. The generic |
| 69 |
// `wp_rest` nonce it shipped with is held by every logged-in user and |
| 70 |
// authorized nobody. The admin bundle still sends that nonce, so accept |
| 71 |
// either action rather than break the Tools screen on upgrade. |
| 72 |
$nonce = isset($_REQUEST['security']) ? sanitize_text_field( wp_unslash( $_REQUEST['security'] ) ) : ''; |
| 73 |
if ( ! wp_verify_nonce($nonce, 'betterlinks_admin_nonce') && ! wp_verify_nonce($nonce, 'wp_rest') ) { |
| 74 |
wp_send_json_error(['message' => __('Invalid nonce', 'betterlinks')], 403); |
| 75 |
} |
| 76 |
|
| 77 |
$can_access_settings = apply_filters("betterlinks/admin/" . BETTERLINKS_PLUGIN_SLUG . "-settings_menu_capability", 'manage_options'); |
| 78 |
if ( ! current_user_can($can_access_settings) ) { |
| 79 |
wp_send_json_error(['message' => __('Insufficient permissions', 'betterlinks')], 403); |
| 80 |
} |
| 81 |
|
| 82 |
$results = json_encode([]); |
| 83 |
if (get_transient('betterlinks_import_info')) { |
| 84 |
\BetterLinks\Helper::clear_query_cache(); |
| 85 |
\BetterLinks\Helper::create_cron_jobs_for_json_links(); |
| 86 |
$results = get_transient('betterlinks_import_info'); |
| 87 |
delete_transient('betterlinks_import_info'); |
| 88 |
} |
| 89 |
wp_send_json_success($results); |
| 90 |
wp_die(); |
| 91 |
} |
| 92 |
} |
| 93 |
|