PluginProbe
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP / trunk
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP vtrunk
3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 3.0.0 2.4.13 2.4.12 2.4.11 2.4.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 110 releases
betterlinks / includes / Tools / Import.php

Import.php in BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP trunk, at includes/Tools/Import.php

93 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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