| 1 |
<?php |
| 2 |
/** |
| 3 |
* Welcart CSV Importer. |
| 4 |
* |
| 5 |
* @package Welcart |
| 6 |
*/ |
| 7 |
|
| 8 |
// phpcs:disable WordPress.WP.AlternativeFunctions |
| 9 |
// phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter |
| 10 |
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 11 |
|
| 12 |
// Only run in the admin area. |
| 13 |
if ( is_admin() ) { |
| 14 |
wel_create_uploads_folder(); |
| 15 |
} |
| 16 |
|
| 17 |
require_once USCES_PLUGIN_DIR . 'includes/importer/csv-importer-class.php'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Start output buffering for admin notices. |
| 21 |
*/ |
| 22 |
function wel_csv_importer_start_admin_notices_buffer() { |
| 23 |
ob_start(); // Start output buffering. |
| 24 |
} |
| 25 |
add_action( 'admin_notices', 'wel_csv_importer_start_admin_notices_buffer', 1 ); |
| 26 |
|
| 27 |
/** |
| 28 |
* End and clean output buffering for admin notices. |
| 29 |
*/ |
| 30 |
function wel_csv_importer_end_admin_notices_buffer() { |
| 31 |
ob_end_clean(); // Discard output buffer contents. |
| 32 |
} |
| 33 |
add_action( 'all_admin_notices', 'wel_csv_importer_end_admin_notices_buffer', PHP_INT_MAX ); |
| 34 |
|
| 35 |
/** |
| 36 |
* Enqueue JS/CSS assets for the admin screen. |
| 37 |
* |
| 38 |
* @param string $hook Page hook. |
| 39 |
*/ |
| 40 |
function wel_csv_importer_enqueue_assets( $hook ) { |
| 41 |
// Load assets only on the welcart-csv-importer admin page. |
| 42 |
if ( 'admin_page_welcart-csv-importer' !== $hook ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
wp_enqueue_script( |
| 47 |
'wcex-csv-importer-js', |
| 48 |
USCES_PLUGIN_URL . 'includes/importer/js/csv-importer.js', |
| 49 |
array( 'jquery' ), |
| 50 |
'1.12', |
| 51 |
true |
| 52 |
); |
| 53 |
wp_enqueue_style( |
| 54 |
'wcex-csv-importer-css', |
| 55 |
USCES_PLUGIN_URL . 'includes/importer/css/csv-importer.css', |
| 56 |
array(), |
| 57 |
'1.8' |
| 58 |
); |
| 59 |
|
| 60 |
$script_data = array( |
| 61 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 62 |
'admin_url' => admin_url( 'admin.php' ), |
| 63 |
); |
| 64 |
wp_localize_script( 'wcex-csv-importer-js', 'wel_csv_importer_data', $script_data ); |
| 65 |
} |
| 66 |
add_action( 'admin_enqueue_scripts', 'wel_csv_importer_enqueue_assets' ); |
| 67 |
|
| 68 |
/** |
| 69 |
* Create the Welcart uploads folder. |
| 70 |
*/ |
| 71 |
function wel_create_uploads_folder() { |
| 72 |
// Retrieve WordPress upload directory information. |
| 73 |
$upload_dir = wp_upload_dir(); |
| 74 |
$base_dir = $upload_dir['basedir']; |
| 75 |
|
| 76 |
// Path for the folder to be created. |
| 77 |
$welcart_dir = $base_dir . '/welcart-uploads'; |
| 78 |
|
| 79 |
// Create the folder if it does not exist. |
| 80 |
if ( ! file_exists( $welcart_dir ) ) { |
| 81 |
wp_mkdir_p( $welcart_dir ); |
| 82 |
} |
| 83 |
|
| 84 |
// Path for the .htaccess file. |
| 85 |
$htaccess_file = $welcart_dir . '/.htaccess'; |
| 86 |
// If .htaccess does not exist, create it and write "deny from all". |
| 87 |
if ( ! file_exists( $htaccess_file ) ) { |
| 88 |
file_put_contents( $htaccess_file, 'deny from all' ); |
| 89 |
} |
| 90 |
|
| 91 |
// Path for index.html. |
| 92 |
$index_file = $welcart_dir . '/index.html'; |
| 93 |
// If index.html does not exist, create an empty file. |
| 94 |
if ( ! file_exists( $index_file ) ) { |
| 95 |
file_put_contents( $index_file, '' ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! defined( 'WELCART_UPLOADS_DIR' ) ) { |
| 99 |
define( 'WELCART_UPLOADS_DIR', $welcart_dir ); |
| 100 |
} |
| 101 |
} |
| 102 |
|