| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: WP Smart Import |
| 4 |
* Plugin URI: https://wordpress.org/plugins/wp-smart-import/ |
| 5 |
* Description: The most powerful solution for importing XML files to WordPress. Create Posts and Pages with content from any XML or CSV file. |
| 6 |
* Version: 2.0.0 |
| 7 |
* Author: Xylus Themes |
| 8 |
* Author URI: http://xylusthemes.com |
| 9 |
* License: GPL-2.0+ |
| 10 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 11 |
* Text Domain: wp-smart-import |
| 12 |
* Domain Path: /languages |
| 13 |
*/ |
| 14 |
|
| 15 |
if (!defined('ABSPATH')) { exit; } // Exit if accessed directly |
| 16 |
if(!class_exists('wpSmartImport')) { |
| 17 |
class wpSmartImport { |
| 18 |
|
| 19 |
static public $_var = array(); |
| 20 |
public function __construct() { |
| 21 |
if ($this->restrict_current_user_role()) { |
| 22 |
$this->define_constants(); // Define constants |
| 23 |
$this->includes(); // Include required files |
| 24 |
$this->update_check(); |
| 25 |
$this->load_textdomain(); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
function restrict_current_user_role() { |
| 30 |
// Useful when you are checking something for the current user |
| 31 |
if (is_user_logged_in()) { |
| 32 |
$ID = get_current_user_id(); |
| 33 |
if (user_can($ID, 'manage_options')) |
| 34 |
return true; |
| 35 |
} |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
private function update_check() { |
| 40 |
$v1 = self::getVar('version'); |
| 41 |
$v2 = self::getVar('version', 'settings'); |
| 42 |
if ($v2 == '') { //first time install |
| 43 |
$defaultSettings = array('version' => $v1); |
| 44 |
update_option(self::getVar('wpsi_settings'), $defaultSettings); |
| 45 |
update_option(self::getVar('wpsi_session'), ""); |
| 46 |
require_once self::getVar('base', 'path').'table/schema.php'; |
| 47 |
} |
| 48 |
if (version_compare($v1, $v2) > 0) { //do upgrade |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static public function init() { |
| 53 |
// Named global variable to make access for other scripts easier. |
| 54 |
$GLOBALS[__CLASS__] = new self; |
| 55 |
} |
| 56 |
|
| 57 |
static public function activate() { |
| 58 |
self::create_files_table(); |
| 59 |
} |
| 60 |
|
| 61 |
static public function create_files_table() { |
| 62 |
global $wpdb; |
| 63 |
|
| 64 |
$table = $wpdb->prefix . 'wpsi_files'; |
| 65 |
$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ); |
| 66 |
|
| 67 |
if ( $table_exists === $table ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$charset_collate = $wpdb->get_charset_collate(); |
| 72 |
$sql = "CREATE TABLE $table ( |
| 73 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 74 |
name VARCHAR(255) NOT NULL DEFAULT '', |
| 75 |
file_name VARCHAR(255) NOT NULL DEFAULT '', |
| 76 |
file_path TEXT, |
| 77 |
folder_name TEXT, |
| 78 |
created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 79 |
updated_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 80 |
PRIMARY KEY (id) |
| 81 |
) $charset_collate;"; |
| 82 |
|
| 83 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 84 |
dbDelta( $sql ); |
| 85 |
} |
| 86 |
|
| 87 |
static public function wpsi_redirect($data) { |
| 88 |
if (!empty($data)) { |
| 89 |
wp_safe_redirect(add_query_arg($data, admin_url('admin.php'))); |
| 90 |
exit(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Loads the plugin language files. |
| 96 |
* |
| 97 |
* @access public |
| 98 |
* @since 1.0.0 |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
private function load_textdomain(){ |
| 102 |
|
| 103 |
load_plugin_textdomain( |
| 104 |
'wp-smart-import', |
| 105 |
false, |
| 106 |
basename( dirname( __FILE__ ) ) . '/languages' |
| 107 |
); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
private function define_constants() { |
| 112 |
$arr = array(); |
| 113 |
$arr['version'] = '2.0.0'; |
| 114 |
$arr['unique'] = 'wp_smart_import'; |
| 115 |
$arr['plugin'] = __FILE__; |
| 116 |
//paths |
| 117 |
$arr['path']['base'] = plugin_dir_path(__FILE__); |
| 118 |
$arr['path']['inc'] = $arr['path']['base'].'/includes/'; |
| 119 |
$arr['path']['admin_view'] = $arr['path']['base'].'/views/admin/'; |
| 120 |
$arr['lang'] = $arr['unique']; |
| 121 |
//urls |
| 122 |
$arr['url']['home'] = trim(plugin_dir_url(__FILE__),'/').'/'; |
| 123 |
$arr['url']['css'] = $arr['url']['home'].'assets/css/'; |
| 124 |
$arr['url']['js'] = $arr['url']['home'].'assets/js/'; |
| 125 |
$arr['url']['images'] = $arr['url']['home'].'assets/images/'; |
| 126 |
$arr['url']['admin'] = get_option('siteurl').'/wp-admin/admin.php'; |
| 127 |
//settings |
| 128 |
$arr['settings'] = get_option('wp-smart-import-settings'); |
| 129 |
$arr['settings'] = $arr['settings'] && is_array($arr['settings']) ? $arr['settings'] : array(); |
| 130 |
// $arr['session'] = isset( $_REQUEST['id'] ) && !empty( $_REQUEST['id'] ) ? self::get_import_option( sanitize_text_field( wp_unslash( $_REQUEST['id'] ) ) ) : get_option('wp-smart-import-session' ); |
| 131 |
if (isset($_REQUEST['id']) && !empty( $_REQUEST['id'])) { |
| 132 |
if (current_user_can('manage_options')) { |
| 133 |
$arr['session'] = self::get_import_option(sanitize_text_field( wp_unslash( $_REQUEST['id'] ) ) ); |
| 134 |
} else { |
| 135 |
wp_die( esc_html__( 'You do not have sufficient permissions to perform this action.', 'wp-smart-import' ) ); |
| 136 |
} |
| 137 |
} else { |
| 138 |
$arr['session'] = get_option('wp-smart-import-session'); |
| 139 |
} |
| 140 |
$arr['wpsi_settings'] = "wp-smart-import-settings"; |
| 141 |
$arr['wpsi_session'] = "wp-smart-import-session"; |
| 142 |
$arr['folder_name'] = $arr['unique']; |
| 143 |
$arr['pages'] = array($arr['unique'], $arr['unique'].'_manage', $arr['unique'].'_manage_file', $arr['unique'].'_settings'); |
| 144 |
self::$_var = $arr; |
| 145 |
} |
| 146 |
|
| 147 |
private function includes() { |
| 148 |
if (is_admin()) { |
| 149 |
require_once self::getVar('inc','path').'admin.php'; |
| 150 |
require_once self::getVar('inc','path').'view.php'; |
| 151 |
require_once self::getVar('inc','path').'common_function.php'; |
| 152 |
require_once self::getVar('inc','path').'lib/helper.php'; |
| 153 |
require_once self::getVar('inc','path').'admin_menu.php'; |
| 154 |
require_once self::getVar('inc','path').'admin_ajax.php'; |
| 155 |
require_once self::getVar('inc','path').'query.php'; |
| 156 |
require_once self::getVar('inc','path').'check.php'; |
| 157 |
require_once self::getVar('inc','path').'upload.php'; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static public function getVar($key, $key1 = '') { |
| 162 |
if ($key1 == '' && isset(self::$_var[$key])) { |
| 163 |
return self::$_var[$key]; |
| 164 |
} else if (isset(self::$_var[$key1][$key])) { |
| 165 |
return self::$_var[$key1][$key]; |
| 166 |
} |
| 167 |
return ''; |
| 168 |
} |
| 169 |
|
| 170 |
static public function setVar($key, $value) { |
| 171 |
self::$_var[$key] = $value; |
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
static public function get_import_option($import_id = 0) { |
| 177 |
global $wpdb; |
| 178 |
$esc_arr = array("," => " ", "&" => " ", "?" => " ", "|" => " "); |
| 179 |
$newstr = strtr($import_id, $esc_arr); |
| 180 |
foreach (explode(' ', $newstr) as $key => $value) { |
| 181 |
if(!empty($value)) { |
| 182 |
$import_id = $value; |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
if (!empty($import_id)) { |
| 187 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 188 |
$import = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpsi_imports WHERE id = %d", absint( $import_id ) ), ARRAY_A ); |
| 189 |
return !empty($import) && isset($import['options']) ? maybe_unserialize($import['options']) : []; |
| 190 |
} |
| 191 |
return false; |
| 192 |
} |
| 193 |
} // END wpSmartImport Class |
| 194 |
register_activation_hook(__FILE__, array('wpSmartImport', 'activate')); |
| 195 |
add_action('plugins_loaded', array('wpSmartImport', 'init'), 10); |
| 196 |
} |
| 197 |
|