| 1 |
<?php |
| 2 |
namespace WGMSRM\Traits; |
| 3 |
|
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Trait CategoryCRUD: Category CRUD operation doing here |
| 10 |
*/ |
| 11 |
trait CategoryCRUD |
| 12 |
{ |
| 13 |
/** |
| 14 |
* To save new category |
| 15 |
*/ |
| 16 |
public function save_category() |
| 17 |
{ |
| 18 |
|
| 19 |
global $wpdb; |
| 20 |
|
| 21 |
|
| 22 |
$data = isset($_POST['category_data']) && is_array($_POST['category_data']) ? map_deep(wp_unslash($_POST['category_data']), 'sanitize_text_field') : []; |
| 23 |
|
| 24 |
$name = isset($data['name']) ? sanitize_text_field($data['name']) : ''; |
| 25 |
$icon = isset($data['icon']) ? esc_url_raw($data['icon']) : ''; |
| 26 |
$parent_id = isset($data['parent_id']) ? intval($data['parent_id']) : 0; |
| 27 |
|
| 28 |
if (empty($name)) { |
| 29 |
wp_send_json_error(['message' => esc_html__('Category name is required.', 'gmap-embed')]); |
| 30 |
} |
| 31 |
|
| 32 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 33 |
$inserted = $wpdb->insert( |
| 34 |
"{$wpdb->prefix}wgm_categories", |
| 35 |
[ |
| 36 |
'name' => $name, |
| 37 |
'icon' => $icon, |
| 38 |
'parent_id' => $parent_id, |
| 39 |
'created_at' => current_time('mysql'), |
| 40 |
], |
| 41 |
['%s', '%s', '%d', '%s'] |
| 42 |
); |
| 43 |
|
| 44 |
if ($inserted) { |
| 45 |
wp_send_json_success([ |
| 46 |
'message' => esc_html__('Category saved successfully.', 'gmap-embed'), |
| 47 |
'id' => $wpdb->insert_id |
| 48 |
]); |
| 49 |
} else { |
| 50 |
wp_send_json_error(['message' => esc_html__('Failed to save category.', 'gmap-embed')]); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* To update existing category |
| 56 |
*/ |
| 57 |
public function update_category() |
| 58 |
{ |
| 59 |
|
| 60 |
global $wpdb; |
| 61 |
|
| 62 |
|
| 63 |
$data = isset($_POST['category_data']) && is_array($_POST['category_data']) ? map_deep(wp_unslash($_POST['category_data']), 'sanitize_text_field') : []; |
| 64 |
|
| 65 |
$id = isset($data['id']) ? intval($data['id']) : 0; |
| 66 |
$name = isset($data['name']) ? sanitize_text_field($data['name']) : ''; |
| 67 |
$icon = isset($data['icon']) ? esc_url_raw($data['icon']) : ''; |
| 68 |
$parent_id = isset($data['parent_id']) ? intval($data['parent_id']) : 0; |
| 69 |
|
| 70 |
if (empty($id) || empty($name)) { |
| 71 |
wp_send_json_error(['message' => esc_html__('Invalid data.', 'gmap-embed')]); |
| 72 |
} |
| 73 |
|
| 74 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 75 |
$updated = $wpdb->update( |
| 76 |
"{$wpdb->prefix}wgm_categories", |
| 77 |
[ |
| 78 |
'name' => $name, |
| 79 |
'icon' => $icon, |
| 80 |
'parent_id' => $parent_id, |
| 81 |
], |
| 82 |
['id' => $id], |
| 83 |
['%s', '%s', '%d'], |
| 84 |
['%d'] |
| 85 |
); |
| 86 |
|
| 87 |
if ($updated !== false) { |
| 88 |
wp_send_json_success(['message' => esc_html__('Category updated successfully.', 'gmap-embed')]); |
| 89 |
} else { |
| 90 |
wp_send_json_error(['message' => esc_html__('Failed to update category.', 'gmap-embed')]); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* To delete a category |
| 96 |
*/ |
| 97 |
public function delete_category() |
| 98 |
{ |
| 99 |
|
| 100 |
global $wpdb; |
| 101 |
|
| 102 |
|
| 103 |
$category_id = isset($_POST['id']) ? intval(sanitize_text_field(wp_unslash($_POST['id']))) : 0; |
| 104 |
if (empty($category_id)) { |
| 105 |
wp_send_json_error(['message' => esc_html__('Invalid ID.', 'gmap-embed')]); |
| 106 |
} |
| 107 |
|
| 108 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 109 |
$deleted = $wpdb->delete( |
| 110 |
"{$wpdb->prefix}wgm_categories", |
| 111 |
['id' => $category_id], |
| 112 |
['%d'] |
| 113 |
); |
| 114 |
|
| 115 |
if ($deleted) { |
| 116 |
wp_send_json_success(['message' => esc_html__('Category deleted successfully.', 'gmap-embed')]); |
| 117 |
} else { |
| 118 |
wp_send_json_error(['message' => esc_html__('Failed to delete category.', 'gmap-embed')]); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Fetch all categories for datatable |
| 124 |
*/ |
| 125 |
public function get_categories_for_dt() |
| 126 |
{ |
| 127 |
global $wpdb; |
| 128 |
|
| 129 |
$categories = wp_cache_get('gmap_embed_categories_dt', 'gmap_embed'); |
| 130 |
if (false === $categories) { |
| 131 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 132 |
$categories = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wgm_categories ORDER BY id DESC"); |
| 133 |
wp_cache_set('gmap_embed_categories_dt', $categories, 'gmap_embed'); |
| 134 |
} |
| 135 |
|
| 136 |
$data = []; |
| 137 |
if ($categories) { |
| 138 |
foreach ($categories as $cat) { |
| 139 |
$parent_name = 'None'; |
| 140 |
if ($cat->parent_id > 0) { |
| 141 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 142 |
$parent = $wpdb->get_var($wpdb->prepare("SELECT name FROM {$wpdb->prefix}wgm_categories WHERE id = %d", $cat->parent_id)); |
| 143 |
$parent_name = $parent ? esc_html($parent) : 'None'; |
| 144 |
} |
| 145 |
|
| 146 |
$icon_html = $cat->icon ? '<img src="' . esc_url($cat->icon) . '" width="30" height="30" style="object-fit: contain;">' : 'None'; |
| 147 |
|
| 148 |
$data[] = [ |
| 149 |
'id' => intval($cat->id), |
| 150 |
'name' => esc_html($cat->name), |
| 151 |
'icon' => $icon_html, |
| 152 |
'parent' => $parent_name, |
| 153 |
'action' => '<button class="button button-small wgm-edit-category" data-id="' . intval($cat->id) . '"><i class="fas fa-edit"></i></button> |
| 154 |
<button class="button button-small wgm-delete-category" data-id="' . intval($cat->id) . '" style="color:red;"><i class="fas fa-trash"></i></button>' |
| 155 |
]; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
wp_send_json(['data' => $data]); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get single category data |
| 164 |
*/ |
| 165 |
public function get_category_data() |
| 166 |
{ |
| 167 |
global $wpdb; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
$id = isset($_POST['id']) ? intval($_POST['id']) : 0; |
| 172 |
|
| 173 |
if (empty($id)) { |
| 174 |
wp_send_json_error(['message' => 'Invalid ID']); |
| 175 |
} |
| 176 |
|
| 177 |
$category = wp_cache_get('gmap_embed_category_' . $id, 'gmap_embed'); |
| 178 |
if (false === $category) { |
| 179 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 180 |
$category = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wgm_categories WHERE id = %d", $id)); |
| 181 |
wp_cache_set('gmap_embed_category_' . $id, $category, 'gmap_embed'); |
| 182 |
} |
| 183 |
|
| 184 |
if ($category) { |
| 185 |
wp_send_json_success($category); |
| 186 |
} else { |
| 187 |
wp_send_json_error(['message' => 'Category not found']); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|