PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.50.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.50.0
3.54.0 3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 All 178 releases
simple-tags / inc / autoterms-table.php

autoterms-table.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.50.0, at inc/autoterms-table.php

406 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!class_exists('WP_List_Table')) {
3 require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
4 }
5
6 class Autoterms_List extends WP_List_Table
7 {
8 /** Class constructor */
9 public function __construct()
10 {
11
12 parent::__construct([
13 'singular' => __('Auto term', 'simple-tags'), //singular name of the listed records
14 'plural' => __('Auto terms', 'simple-tags'), //plural name of the listed records
15 'ajax' => false //does this table support ajax?
16 ]);
17
18 }
19
20 /**
21 * Retrieve st_autoterms data from the database
22 *
23 * @param int $per_page
24 * @param int $page_number
25 *
26 * @return mixed
27 */
28 public static function get_st_autoterms()
29 {
30 return taxopress_get_autoterm_data();
31 }
32
33 /**
34 * Returns the count of records in the database.
35 *
36 * @return null|string
37 */
38 public static function record_count()
39 {
40 return count(taxopress_get_autoterm_data());
41 }
42
43 /**
44 * Show single row item
45 *
46 * @param array $item
47 */
48 public function single_row($item)
49 {
50 $class = ['st-autoterm-tr'];
51 $id = 'st-autoterm-' . md5($item['ID']);
52 echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class)));
53 $this->single_row_columns($item);
54 echo '</tr>';
55 }
56
57 /**
58 * Associative array of columns
59 *
60 * @return array
61 */
62 public function get_columns()
63 {
64 $columns = [
65 'title' => __('Title', 'simple-tags'),
66 'taxonomy' => __('Taxonomy', 'simple-tags'),
67 'post_types' => __('Post Type', 'simple-tags'),
68 'source' => __('Source', 'simple-tags')
69 ];
70
71 return $columns;
72 }
73
74 /**
75 * Render a column when no column specific method exist.
76 *
77 * @param array $item
78 * @param string $column_name
79 *
80 * @return mixed
81 */
82 public function column_default($item, $column_name)
83 {
84 return !empty($item[$column_name]) ? $item[$column_name] : '&mdash;';
85 }
86
87 /** Text displayed when no stterm data is available */
88 public function no_items()
89 {
90 _e('No item avaliable.', 'simple-tags');
91 }
92
93 /**
94 * Displays the search box.
95 *
96 * @param string $text The 'submit' button label.
97 * @param string $input_id ID attribute value for the search input field.
98 *
99 *
100 */
101 public function search_box($text, $input_id)
102 {
103 if (empty($_REQUEST['s']) && !$this->has_items()) {
104 return;
105 }
106
107 $input_id = $input_id . '-search-input';
108
109 if (!empty($_REQUEST['orderby'])) {
110 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field($_REQUEST['orderby'])) . '" />';
111 }
112 if (!empty($_REQUEST['order'])) {
113 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field($_REQUEST['order'])) . '" />';
114 }
115 if (!empty($_REQUEST['page'])) {
116 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field($_REQUEST['page'])) . '" />';
117 }
118 ?>
119 <p class="search-box">
120 <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label>
121 <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s"
122 value="<?php _admin_search_query(); ?>"/>
123 <?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?>
124 </p>
125 <?php
126 }
127
128 /**
129 * Sets up the items (roles) to list.
130 */
131 public function prepare_items()
132 {
133
134 $this->_column_headers = $this->get_column_info();
135
136 /**
137 * First, lets decide how many records per page to show
138 */
139 $per_page = $this->get_items_per_page('st_autoterms_per_page', 20);
140
141 /**
142 * Fetch the data
143 */
144 $data = self::get_st_autoterms();
145
146 /**
147 * Handle search
148 */
149 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field($_REQUEST['s'])) {
150 $data_filtered = [];
151 foreach ($data as $item) {
152 if ($this->str_contains($item['title'], $search, false)) {
153 $data_filtered[] = $item;
154 }
155 }
156 $data = $data_filtered;
157 }
158
159 /**
160 * This checks for sorting input and sorts the data in our array accordingly.
161 */
162 function usort_reorder($a, $b)
163 {
164 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'ID'; //If no sort, default to role
165 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'desc'; //If no order, default to asc
166 $result = strnatcasecmp(
167 $a[$orderby],
168 $b[$orderby]
169 ); //Determine sort order, case insensitive, natural order
170
171 return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
172 }
173
174 usort($data, 'usort_reorder');
175
176 /**
177 * Pagination.
178 */
179 $current_page = $this->get_pagenum();
180 $total_items = count($data);
181
182
183 /**
184 * The WP_List_Table class does not handle pagination for us, so we need
185 * to ensure that the data is trimmed to only the current page. We can use
186 * array_slice() to
187 */
188 $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
189
190 /**
191 * Now we can add the data to the items property, where it can be used by the rest of the class.
192 */
193 $this->items = $data;
194
195 /**
196 * We also have to register our pagination options & calculations.
197 */
198 $this->set_pagination_args([
199 'total_items' => $total_items, //calculate the total number of items
200 'per_page' => $per_page, //determine how many items to show on a page
201 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
202 ]);
203 }
204
205 /**
206 * Determine if a given string contains a given substring.
207 *
208 * @param string $haystack
209 * @param string|array $needles
210 * @param bool $sensitive Use case sensitive search
211 *
212 * @return bool
213 */
214 public function str_contains($haystack, $needles, $sensitive = true)
215 {
216 foreach ((array)$needles as $needle) {
217 $function = $sensitive ? 'mb_strpos' : 'mb_stripos';
218 if ($needle !== '' && $function($haystack, $needle) !== false) {
219 return true;
220 }
221 }
222
223 return false;
224 }
225
226 /**
227 * Columns to make sortable.
228 *
229 * @return array
230 */
231 protected function get_sortable_columns()
232 {
233 $sortable_columns = [
234 'title' => ['title', true],
235 'taxonomy' => ['taxonomy', true],
236 ];
237
238 return $sortable_columns;
239 }
240
241 /**
242 * Generates and display row actions links for the list table.
243 *
244 * @param object $item The item being acted upon.
245 * @param string $column_name Current column name.
246 * @param string $primary Primary column name.
247 *
248 * @return string The row actions HTML, or an empty string if the current column is the primary column.
249 */
250 protected function handle_row_actions($item, $column_name, $primary)
251 {
252 //Build row actions
253 $actions = [
254 'edit' => sprintf(
255 '<a href="%s">%s</a>',
256 add_query_arg(
257 [
258 'page' => 'st_autoterms',
259 'add' => 'new_item',
260 'action' => 'edit',
261 'taxopress_autoterms' => $item['ID'],
262 ],
263 admin_url('admin.php')
264 ),
265 __('Edit', 'simple-tags')
266 ),
267 'delete' => sprintf(
268 '<a href="%s" class="delete-autoterm">%s</a>',
269 add_query_arg(
270 [
271 'page' => 'st_autoterms',
272 'action' => 'taxopress-delete-autoterm',
273 'taxopress_autoterms' => esc_attr($item['ID']),
274 '_wpnonce' => wp_create_nonce('autoterm-action-request-nonce')
275 ],
276 admin_url('admin.php')
277 ),
278 __('Delete', 'simple-tags')
279 ),
280 ];
281
282 $actions = apply_filters('taxopress_autoterm_row_actions', $actions, $item);
283 return $column_name === $primary ? $this->row_actions($actions, false) : '';
284 }
285
286 /**
287 * Method for title column
288 *
289 * @param array $item
290 *
291 * @return string
292 */
293 protected function column_title($item)
294 {
295 $title = sprintf(
296 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
297 add_query_arg(
298 [
299 'page' => 'st_autoterms',
300 'add' => 'new_item',
301 'action' => 'edit',
302 'taxopress_autoterms' => $item['ID'],
303 ],
304 admin_url('admin.php')
305 ),
306 esc_html($item['title'])
307 );
308
309 return $title;
310 }
311
312 /**
313 * Method for taxonomy column
314 *
315 * @param array $item
316 *
317 * @return string
318 */
319 protected function column_taxonomy($item)
320 {
321 $taxonomy = get_taxonomy($item['taxonomy']);
322
323 if ($taxonomy) {
324 $return = $taxonomy->labels->name;
325 } else {
326 $return = '&mdash;';
327 }
328
329 return $return;
330 }
331
332 /**
333 * The action column
334 *
335 * @param $item
336 *
337 * @return string
338 */
339 protected function column_post_types($item)
340 {
341 $embedded = (isset($item['post_types']) && is_array($item['post_types']) && count($item['post_types']) > 0) ? $item['post_types'] : false;
342 if ($embedded) {
343 $args = apply_filters('taxopress_attach_post_types_to_taxonomy', ['public' => true]);
344 if (!is_array($args)) {
345 $args = ['public' => true];
346 }
347 $output = 'objects'; // Or objects.
348 $post_types = apply_filters(
349 'taxopress_get_post_types_for_taxonomies',
350 get_post_types($args, $output),
351 $args,
352 $output
353 );
354
355 $result_array = [];
356 foreach ($post_types as $post_type) {
357 $embedded_options[$post_type->name] = $post_type->label;
358 }
359 foreach ($embedded as $location) {
360 $result_array[] = isset($embedded_options[$location]) ? $embedded_options[$location] : '';
361 }
362 $result = join(', ', $result_array);
363 } else {
364 $result = esc_html__('None', 'simple-tags');
365 }
366
367 return $result;
368 }
369
370 /**
371 * The action column
372 *
373 * @param $item
374 *
375 * @return string
376 */
377 protected function column_source($item)
378 {
379 $used_source = [];
380
381 if (isset($item['autoterm_use_taxonomy']) && !empty(taxopress_disp_boolean($item['autoterm_use_taxonomy']))) {
382 $used_source[] = esc_html__('Existing taxonomy terms', 'simple-tags');
383 }
384
385 if (isset($item['autoterm_use_open_ai']) && !empty(taxopress_disp_boolean($item['autoterm_use_open_ai']))) {
386 $used_source[] = esc_html__('OpenAI', 'simple-tags');
387 }
388
389 if (isset($item['autoterm_use_ibm_watson']) && !empty(taxopress_disp_boolean($item['autoterm_use_ibm_watson']))) {
390 $used_source[] = esc_html__('IBM Watson', 'simple-tags');
391 }
392
393 if (isset($item['autoterm_use_dandelion']) && !empty(taxopress_disp_boolean($item['autoterm_use_dandelion']))) {
394 $used_source[] = esc_html__('Dandelion', 'simple-tags');
395 }
396
397 if (isset($item['autoterm_use_opencalais']) && !empty(taxopress_disp_boolean($item['autoterm_use_opencalais']))) {
398 $used_source[] = esc_html__('LSEG / Refinitiv', 'simple-tags');
399 }
400
401 return join(', ', $used_source);
402 }
403
404
405 }
406