PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.44
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.44
51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 All 40 releases
king-addons / includes / helpers / Theme_Builder / Admin_List_Table.php

Admin_List_Table.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.44, at includes/helpers/Theme_Builder/Admin_List_Table.php

268 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Theme Builder admin list table.
4 *
5 * @package King_Addons
6 */
7
8 namespace King_Addons\Theme_Builder;
9
10 use WP_List_Table;
11
12 if (!defined('ABSPATH')) {
13 exit;
14 }
15
16 if (!class_exists('WP_List_Table')) {
17 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
18 }
19
20 /**
21 * Renders Theme Builder templates list in admin.
22 */
23 class Admin_List_Table extends WP_List_Table
24 {
25 /**
26 * Templates data.
27 *
28 * @var array<int,array<string,mixed>>
29 */
30 private array $items_data;
31
32 /**
33 * Base page URL.
34 *
35 * @var string
36 */
37 private string $base_url;
38
39 /**
40 * Constructor.
41 *
42 * @param array<int,array<string,mixed>> $items Templates.
43 * @param string $base_url Base admin page URL.
44 */
45 public function __construct(array $items, string $base_url)
46 {
47 parent::__construct([
48 'plural' => 'templates',
49 'singular' => 'template',
50 'ajax' => false,
51 ]);
52
53 $this->items_data = $items;
54 $this->base_url = $base_url;
55 }
56
57 /**
58 * Define table columns.
59 *
60 * @return array<string,string>
61 */
62 public function get_columns(): array
63 {
64 return [
65 'cb' => '<input type="checkbox" />',
66 'title' => esc_html__('Title', 'king-addons'),
67 'type' => esc_html__('Type', 'king-addons'),
68 'location' => esc_html__('Location', 'king-addons'),
69 'conditions' => esc_html__('Conditions', 'king-addons'),
70 'priority' => esc_html__('Priority', 'king-addons'),
71 'status' => esc_html__('Status', 'king-addons'),
72 'pro' => esc_html__('Pro', 'king-addons'),
73 ];
74 }
75
76 /**
77 * Sortable columns.
78 *
79 * @return array<string,array<int,string>>
80 */
81 protected function get_sortable_columns(): array
82 {
83 return [
84 'title' => ['title', false],
85 'priority' => ['priority', false],
86 ];
87 }
88
89 /**
90 * Prepare items with filters and pagination.
91 *
92 * @return void
93 */
94 public function prepare_items(): void
95 {
96 $data = $this->filter_items($this->items_data);
97
98 $per_page = 20;
99 $current_page = $this->get_pagenum();
100 $total_items = count($data);
101
102 $this->items = array_slice($data, ($current_page - 1) * $per_page, $per_page);
103 $this->set_pagination_args([
104 'total_items' => $total_items,
105 'per_page' => $per_page,
106 ]);
107 }
108
109 /**
110 * Render checkbox column.
111 *
112 * @param array<string,mixed> $item Row item.
113 *
114 * @return string
115 */
116 protected function column_cb($item): string
117 {
118 return '<input type="checkbox" name="template_ids[]" value="' . esc_attr((string) $item['id']) . '" />';
119 }
120
121 /**
122 * Render title column with actions.
123 *
124 * @param array<string,mixed> $item Row item.
125 *
126 * @return string
127 */
128 public function column_title($item): string
129 {
130 $title = esc_html($item['title'] ?? '');
131 $edit_link = esc_url(admin_url('post.php?post=' . $item['id'] . '&action=elementor'));
132 $quick_link = '#';
133 $toggle_action = !empty($item['enabled']) ? 'disable_template' : 'enable_template';
134 $toggle_link = esc_url(wp_nonce_url(add_query_arg(['action' => $toggle_action, 'template_id' => $item['id']], $this->base_url), 'ka_theme_builder_toggle_' . $item['id']));
135 $delete_link = esc_url(wp_nonce_url(add_query_arg(['action' => 'delete_template', 'template_id' => $item['id']], $this->base_url), 'ka_theme_builder_delete_' . $item['id']));
136
137 $actions = [
138 'edit' => '<a href="' . $edit_link . '">' . esc_html__('Edit in Elementor', 'king-addons') . '</a>',
139 'quick_edit' => '<a href="' . $quick_link . '" class="ka-tb-quick-edit" data-template-id="' . esc_attr((string) $item['id']) . '" data-priority="' . esc_attr((string) ($item['priority'] ?? '')) . '" data-enabled="' . (!empty($item['enabled']) ? '1' : '0') . '">' . esc_html__('Quick Edit', 'king-addons') . '</a>',
140 'toggle' => '<a href="' . $toggle_link . '">' . (!empty($item['enabled']) ? esc_html__('Disable', 'king-addons') : esc_html__('Enable', 'king-addons')) . '</a>',
141 'delete' => '<a href="' . $delete_link . '">' . esc_html__('Delete', 'king-addons') . '</a>',
142 ];
143
144 if (function_exists('king_addons_freemius') && king_addons_freemius()->can_use_premium_code__premium_only()) {
145 $duplicate_link = wp_nonce_url(
146 admin_url('admin-post.php?action=ka_theme_builder_duplicate&template_id=' . $item['id']),
147 'ka_theme_builder_duplicate_' . $item['id']
148 );
149 $actions['duplicate'] = '<a href="' . esc_url($duplicate_link) . '">' . esc_html__('Duplicate', 'king-addons') . '</a>';
150 }
151
152 return $title . $this->row_actions($actions);
153 }
154
155 /**
156 * Render default column output.
157 *
158 * @param array<string,mixed> $item Row item.
159 * @param string $column_name Column name.
160 *
161 * @return string
162 */
163 public function column_default($item, $column_name): string
164 {
165 switch ($column_name) {
166 case 'type':
167 return esc_html($item['type'] ?? '');
168 case 'location':
169 return esc_html($item['sub_location'] ?? '');
170 case 'priority':
171 return esc_html((string) ($item['priority'] ?? ''));
172 case 'status':
173 return !empty($item['enabled']) ? esc_html__('Enabled', 'king-addons') : esc_html__('Disabled', 'king-addons');
174 case 'conditions':
175 return esc_html($this->summarize_conditions($item['conditions'] ?? []));
176 case 'pro':
177 return !empty($item['is_pro_only']) ? esc_html__('Pro', 'king-addons') : esc_html__('Free', 'king-addons');
178 default:
179 return '';
180 }
181 }
182
183 /**
184 * Views for filters.
185 *
186 * @return array<string,string>
187 */
188 protected function get_views(): array
189 {
190 $current = $_GET['status'] ?? 'all'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
191 $base = remove_query_arg(['status', 'paged'], $this->base_url);
192
193 $views = [
194 'all' => '<a href="' . esc_url($base) . '"' . ('all' === $current ? ' class="current"' : '') . '>' . esc_html__('All', 'king-addons') . '</a>',
195 'enabled' => '<a href="' . esc_url(add_query_arg('status', 'enabled', $base)) . '"' . ('enabled' === $current ? ' class="current"' : '') . '>' . esc_html__('Enabled', 'king-addons') . '</a>',
196 'disabled' => '<a href="' . esc_url(add_query_arg('status', 'disabled', $base)) . '"' . ('disabled' === $current ? ' class="current"' : '') . '>' . esc_html__('Disabled', 'king-addons') . '</a>',
197 ];
198
199 return $views;
200 }
201
202 /**
203 * Filter items by query args.
204 *
205 * @param array<int,array<string,mixed>> $items Items.
206 *
207 * @return array<int,array<string,mixed>>
208 */
209 private function filter_items(array $items): array
210 {
211 $status_filter = $_GET['status'] ?? ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
212 $type_filter = $_GET['type'] ?? ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
213 $location_filter = $_GET['location'] ?? ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
214
215 $filtered = array_filter(
216 $items,
217 static function ($item) use ($status_filter, $type_filter, $location_filter) {
218 if ('enabled' === $status_filter && empty($item['enabled'])) {
219 return false;
220 }
221 if ('disabled' === $status_filter && !empty($item['enabled'])) {
222 return false;
223 }
224 if (!empty($type_filter) && ($item['type'] ?? '') !== $type_filter) {
225 return false;
226 }
227 if (!empty($location_filter) && ($item['sub_location'] ?? '') !== $location_filter) {
228 return false;
229 }
230 return true;
231 }
232 );
233
234 return array_values($filtered);
235 }
236
237 /**
238 * Summarize conditions to human-friendly string.
239 *
240 * @param array<string,mixed> $conditions Conditions payload.
241 *
242 * @return string
243 */
244 private function summarize_conditions(array $conditions): string
245 {
246 $groups = $conditions['groups'] ?? [];
247 if (empty($groups)) {
248 return esc_html__('All', 'king-addons');
249 }
250
251 $summary = [];
252 foreach ($groups as $group) {
253 $rules = $group['rules'] ?? [];
254 $summary[] = sprintf(
255 /* translators: %d: number of rules */
256 esc_html__('%d rules', 'king-addons'),
257 count($rules)
258 );
259 }
260
261 return implode(' / ', $summary);
262 }
263 }
264
265
266
267
268