| 1 |
<?php |
| 2 |
/** |
| 3 |
* Summary of External_Lists |
| 4 |
* Class to handle External Lists feature. |
| 5 |
*/ |
| 6 |
class External_Lists |
| 7 |
{ |
| 8 |
/** |
| 9 |
* Summary of init |
| 10 |
* Initialize the External Lists feature. |
| 11 |
* @return void |
| 12 |
*/ |
| 13 |
public static function init() |
| 14 |
{ |
| 15 |
// Initialization code if needed |
| 16 |
|
| 17 |
// Handle the post request for CSV import. |
| 18 |
add_action('admin_post_import_external_list', [self::class, 'handle_list_import']); |
| 19 |
add_action('wp_ajax_mock_chunk_process', [Import_User_Service::class, 'ajax_mock_chunk_process']); |
| 20 |
add_action('wp_ajax_process_external_import_chunk', [self::class, 'ajax_handle_import']); |
| 21 |
add_action('wp_ajax_import_external_list', [self::class, 'ajax_handle_upload']); |
| 22 |
add_action('wp_ajax_sue_delete_external_list', [External_List_Delete_Service::class, 'ajax_handle_delete']); |
| 23 |
|
| 24 |
// include necessary files |
| 25 |
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-import-csv.php'; |
| 26 |
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-external-list-schema.php'; |
| 27 |
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-import-user-service.php'; |
| 28 |
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-external-list-model.php'; |
| 29 |
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-external-list-delete-service.php'; |
| 30 |
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-external-lists-dashboard.php'; |
| 31 |
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-external-lists-edit.php'; |
| 32 |
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-bootstrap-pagination.php'; |
| 33 |
|
| 34 |
SUE_External_Lists_Dashboard::init(); |
| 35 |
SUE_External_Lists_Edit::init(); |
| 36 |
|
| 37 |
$version = Send_Users_Email_Admin::get_version(); |
| 38 |
add_action('admin_enqueue_scripts', function () use ($version) { |
| 39 |
External_Lists::register_scripts($version); |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
public static function get_action_name() |
| 44 |
{ |
| 45 |
$action_name = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : 'dashboard'; |
| 46 |
return $action_name; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Summary of register_scripts |
| 51 |
* Handle the registration of scripts. |
| 52 |
* @param mixed $ver |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public static function register_scripts($ver) |
| 56 |
{ |
| 57 |
wp_register_script( |
| 58 |
'sue-external-list-upload', |
| 59 |
plugin_dir_url(dirname(__FILE__)) . 'admin/js/external-list-import.js', |
| 60 |
['jquery'], |
| 61 |
$ver, |
| 62 |
true |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Summary of enqueue_scripts |
| 68 |
* Enqueue the necessary scripts. |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public static function enqueue_scripts() |
| 72 |
{ |
| 73 |
wp_enqueue_script('sue-external-list-upload'); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Summary of ajax_handle_upload |
| 78 |
* Handle the AJAX request for uploading external list CSV. |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public static function ajax_handle_upload() |
| 82 |
{ |
| 83 |
if ( !isset($_POST['sue_external_lists_nonce']) && ! wp_verify_nonce( $_POST['sue_external_lists_nonce'], 'sue_external_lists_import' ) ) { |
| 84 |
wp_send_json_error(['message' => 'Security check failed.']); |
| 85 |
} |
| 86 |
|
| 87 |
check_admin_referer('sue_external_lists_import', 'sue_external_lists_nonce'); |
| 88 |
|
| 89 |
if (isset($_FILES['external_list_file']) && $_FILES['external_list_file']['error'] === UPLOAD_ERR_OK) { |
| 90 |
$import = Import_CSV::handle_csv_import($_FILES['external_list_file']); |
| 91 |
|
| 92 |
if(isset($import['errors']) && count($import['errors']) > 0) { |
| 93 |
wp_send_json_error(['message' => 'Import failed.', 'errors' => $import['errors']]); |
| 94 |
} |
| 95 |
|
| 96 |
wp_send_json_success([ |
| 97 |
'message' => 'Upload successful.', |
| 98 |
'import' => $import |
| 99 |
]); |
| 100 |
|
| 101 |
} else { |
| 102 |
wp_send_json_error(['message' => 'File upload failed.']); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Summary of ajax_handle_import |
| 108 |
* Handle the AJAX request for importing external lists. |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public static function ajax_handle_import() |
| 112 |
{ |
| 113 |
if ( !isset($_POST['nonce']) && !wp_verify_nonce( $_POST['nonce'], 'sue_external_lists_import' ) ) { |
| 114 |
wp_send_json_error(['message' => 'Security check failed.']); |
| 115 |
} |
| 116 |
|
| 117 |
$current_user_id = get_current_user_id(); |
| 118 |
if (!isset($_POST['user_id'])) { |
| 119 |
$_POST['user_id'] = $current_user_id; |
| 120 |
} |
| 121 |
|
| 122 |
$import_ret = Import_User_Service::handle_import_request($_POST); |
| 123 |
if (isset($import_ret['errors']) && count($import_ret['errors']) > 0) { |
| 124 |
wp_send_json_error(['message' => 'Import failed.', 'errors' => $import_ret['errors']]); |
| 125 |
} |
| 126 |
|
| 127 |
wp_send_json_success(['message' => 'Import successful.', 'import' => $import_ret]); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Summary of add_submenu_page |
| 132 |
* Add submenu page for External Lists under Send Users Email menu. |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
public static function add_submenu_page() |
| 136 |
{ |
| 137 |
add_submenu_page( |
| 138 |
'send-users-email', |
| 139 |
__('External Lists (Beta)', 'send-users-email'), |
| 140 |
__('External Lists (Beta)', 'send-users-email'), |
| 141 |
'manage_options', |
| 142 |
'send-users-email-external-lists', |
| 143 |
[self::class, 'render_settings_page'], |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Summary of render_settings_page |
| 149 |
* Render the template for this External List. |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public static function render_settings_page() |
| 153 |
{ |
| 154 |
if (!current_user_can('manage_options')) { |
| 155 |
wp_die(__('You do not have sufficient permissions to access this page.', 'external-lists')); |
| 156 |
} |
| 157 |
|
| 158 |
$action = self::get_action_name(); |
| 159 |
|
| 160 |
$args = [ |
| 161 |
'action' => $action, |
| 162 |
]; |
| 163 |
|
| 164 |
sue_render_admin_template('admin-external-lists.php', $args); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Summary of handle_list_import |
| 169 |
* Handle the list import from the manual form submission. |
| 170 |
* @return void |
| 171 |
*/ |
| 172 |
public static function handle_list_import() |
| 173 |
{ |
| 174 |
// Implement for the manual form submission if needed |
| 175 |
$upload = Import_CSV::handle_csv_import($_FILES['external_list_file']); |
| 176 |
if (isset($upload['errors']) && count($upload['errors']) > 0) { |
| 177 |
$errors = implode(', ', $upload['errors']); |
| 178 |
wp_redirect(add_query_arg(['page' => 'send-users-email-external-lists', 'errors' => $errors], admin_url('admin.php'))); |
| 179 |
exit; |
| 180 |
} |
| 181 |
|
| 182 |
$_POST['file_path'] = $upload['file_path']; |
| 183 |
|
| 184 |
$current_user_id = get_current_user_id(); |
| 185 |
if (!isset($_POST['user_id'])) { |
| 186 |
$_POST['user_id'] = $current_user_id; |
| 187 |
} |
| 188 |
|
| 189 |
$import_list = Import_User_Service::handle_import_request($_POST); |
| 190 |
if ( isset($import_list['import']['errors']) && count($import_list['import']['errors']) > 0 ) { |
| 191 |
$errors = implode(', ', $import_list['import']['errors']); |
| 192 |
wp_redirect(add_query_arg(['page' => 'send-users-email-external-lists', 'errors' => $errors], admin_url('admin.php'))); |
| 193 |
exit; |
| 194 |
} |
| 195 |
|
| 196 |
wp_redirect(add_query_arg(['page' => 'send-users-email-external-lists', 'success' => 'Import successful.'], admin_url('admin.php'))); |
| 197 |
exit; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Summary of sue_email_queue_tooltip |
| 202 |
* Filter to change the table head role label. |
| 203 |
* @param string $label |
| 204 |
* @return string |
| 205 |
*/ |
| 206 |
public static function sue_email_queue_tooltip($label, $email_data) |
| 207 |
{ |
| 208 |
if (isset($email_data->via) && $email_data->via === 'external_list') { |
| 209 |
$label = "» Send to External List(s): "; |
| 210 |
} |
| 211 |
return $label; |
| 212 |
} |
| 213 |
} |