| 1 |
<?php |
| 2 |
class SUE_External_Lists_Edit |
| 3 |
{ |
| 4 |
public static function init() |
| 5 |
{ |
| 6 |
add_action('sue_external_lists_edit-lists', [self::class, 'render_template'], 20, 2); |
| 7 |
add_action('wp_ajax_sue_update_external_list', [self::class, 'update_external_list']); |
| 8 |
add_action('wp_ajax_sue_remove_external_list', [self::class, 'delete_external_list']); |
| 9 |
add_action('admin_enqueue_scripts', function() { |
| 10 |
self::register_scripts(Send_Users_Email_Admin::get_version()); |
| 11 |
}); |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Summary of register_scripts |
| 16 |
* Handle the registration of scripts. |
| 17 |
* @param mixed $ver |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public static function register_scripts($ver) |
| 21 |
{ |
| 22 |
wp_register_script( |
| 23 |
'sue-external-list-edit', |
| 24 |
plugin_dir_url(dirname(__FILE__)) . 'admin/js/external-list-edit.js', |
| 25 |
['jquery'], |
| 26 |
$ver, |
| 27 |
true |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Summary of enqueue_scripts |
| 33 |
* Enqueue the necessary scripts. |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public static function enqueue_scripts() |
| 37 |
{ |
| 38 |
wp_enqueue_script('sue-external-list-edit'); |
| 39 |
wp_localize_script('sue-external-list-edit', 'sueExternalListEdit', [ |
| 40 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 41 |
'nonce' => wp_create_nonce('sue_update_external_list'), |
| 42 |
'deleteNonce' => wp_create_nonce('sue_external_lists_delete'), |
| 43 |
]); |
| 44 |
} |
| 45 |
|
| 46 |
public static function get_list_id() |
| 47 |
{ |
| 48 |
return isset($_GET['list_id']) ? $_GET['list_id'] : 0; |
| 49 |
} |
| 50 |
|
| 51 |
public static function render_template( $action, $args ) |
| 52 |
{ |
| 53 |
$get_list_id = self::get_list_id(); |
| 54 |
|
| 55 |
$per_page = 100; // Number of items per page |
| 56 |
|
| 57 |
$total_items = External_List_Model::count_total_external_lists_by_listid($get_list_id); |
| 58 |
|
| 59 |
$base_url = add_query_arg([ |
| 60 |
'page' => 'send-users-email-external-lists', |
| 61 |
'action' => External_Lists::get_action_name(), |
| 62 |
'list_id' => $get_list_id |
| 63 |
], admin_url('admin.php')); |
| 64 |
|
| 65 |
$paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1; |
| 66 |
|
| 67 |
if($total_items < $per_page){ |
| 68 |
$paged = 0; // Reset to first page if total items are less than or equal to items per page |
| 69 |
} |
| 70 |
|
| 71 |
$get_all_lists = External_List_Model::get_all_external_lists_by_listid($get_list_id, $per_page, $paged); |
| 72 |
|
| 73 |
// get count to display total items of remaining items in pagination |
| 74 |
$remaining_items_count = $total_items - ($paged * $per_page); |
| 75 |
if($remaining_items_count < 0){ |
| 76 |
$remaining_items_count = 0; // Ensure remaining items count does not go negative |
| 77 |
} |
| 78 |
//echo $total_items . ' of ' . $remaining_items_count; // Display remaining items count |
| 79 |
|
| 80 |
$paginator = new Bootstrap_Pagination([ |
| 81 |
'total_items' => $total_items, |
| 82 |
'per_page' => $per_page, |
| 83 |
'current_page' => $paged, |
| 84 |
'base_url' => $base_url . '&paged=%#%', |
| 85 |
]); |
| 86 |
|
| 87 |
$template_args = [ |
| 88 |
'action' => External_Lists::get_action_name(), |
| 89 |
'lists_data' => $get_all_lists, |
| 90 |
'pagination' => $paginator->render(), |
| 91 |
]; |
| 92 |
|
| 93 |
sue_render_admin_template('external-lists/edit.php', $template_args); |
| 94 |
} |
| 95 |
|
| 96 |
public static function update_external_list() |
| 97 |
{ |
| 98 |
// Handle the AJAX request to update the external list |
| 99 |
|
| 100 |
if ( ! check_ajax_referer('sue_update_external_list', 'security')) { |
| 101 |
wp_send_json_error('Invalid nonce'); |
| 102 |
} |
| 103 |
$data = isset($_POST['data']) ? $_POST['data'] : []; |
| 104 |
$list_id = isset($data['id']) ? intval($data['id']) : 0; |
| 105 |
|
| 106 |
if ($list_id <= 0) { |
| 107 |
wp_send_json_error('Invalid list ID'); |
| 108 |
} |
| 109 |
|
| 110 |
$update_data = [ |
| 111 |
'email' => isset($data['email']) ? sanitize_email($data['email']) : '', |
| 112 |
'first_name' => isset($data['first_name']) ? sanitize_text_field($data['first_name']) : '', |
| 113 |
'last_name' => isset($data['last_name']) ? sanitize_text_field($data['last_name']) : '', |
| 114 |
'title' => isset($data['title']) ? sanitize_text_field($data['title']) : '', |
| 115 |
'salutation' => isset($data['salutation']) ? sanitize_text_field($data['salutation']) : '', |
| 116 |
'field_01' => isset($data['field_01']) ? sanitize_text_field($data['field_01']) : '', |
| 117 |
'field_02' => isset($data['field_02']) ? sanitize_text_field($data['field_02']) : '', |
| 118 |
'field_03' => isset($data['field_03']) ? sanitize_text_field($data['field_03']) : '', |
| 119 |
'field_04' => isset($data['field_04']) ? sanitize_text_field($data['field_04']) : '', |
| 120 |
'field_05' => isset($data['field_05']) ? sanitize_text_field($data['field_05']) : '', |
| 121 |
'subscribed' => isset($data['subscribed']) ? intval($data['subscribed']) : 0, |
| 122 |
]; |
| 123 |
|
| 124 |
External_List_Model::update_list($list_id, $update_data); |
| 125 |
|
| 126 |
$arr_json_send = [ |
| 127 |
'success' => true, |
| 128 |
'message' => '', |
| 129 |
'list_id' => $list_id |
| 130 |
]; |
| 131 |
|
| 132 |
sleep(5); |
| 133 |
|
| 134 |
wp_send_json_success($arr_json_send); |
| 135 |
|
| 136 |
wp_die(); |
| 137 |
} |
| 138 |
|
| 139 |
public static function delete_external_list() |
| 140 |
{ |
| 141 |
$id = isset($_POST['list_id']) ? intval($_POST['list_id']) : 0; |
| 142 |
|
| 143 |
// Handle the AJAX request to delete the external list |
| 144 |
if ( ! check_ajax_referer('sue_external_lists_delete_' . $id, 'security')) { |
| 145 |
wp_send_json_error('Invalid nonce'); |
| 146 |
} |
| 147 |
|
| 148 |
if ($id <= 0) { |
| 149 |
wp_send_json_error('Invalid list ID'); |
| 150 |
} |
| 151 |
|
| 152 |
External_List_Model::delete_list($id); |
| 153 |
|
| 154 |
$arr_json_send = [ |
| 155 |
'success' => true, |
| 156 |
'message' => '', |
| 157 |
'id' => $id |
| 158 |
]; |
| 159 |
|
| 160 |
wp_send_json_success($arr_json_send); |
| 161 |
|
| 162 |
wp_die(); |
| 163 |
} |
| 164 |
} |